Exemplo n.º 1
0
 def _handle_text(self, msg):
     payload = msg.get_payload()
     if payload is None:
         return
     cset = msg.get_charset()
     if cset is not None:
         payload = cset.body_encode(payload)
     if not _isstring(payload):
         raise TypeError, 'string payload expected: %s' % type(payload)
     if self._mangle_from_:
         payload = fcre.sub('>From ', payload)
     self._fp.write(payload)
Exemplo n.º 2
0
 def _handle_text(self, msg):
     payload = msg.get_payload()
     if payload is None:
         return
     cset = msg.get_charset()
     if cset is not None:
         payload = cset.body_encode(payload)
     if not _isstring(payload):
         raise TypeError, 'string payload expected: %s' % type(payload)
     if self._mangle_from_:
         payload = fcre.sub('>From ', payload)
     self._fp.write(payload)
Exemplo n.º 3
0
 def _handle_multipart(self, msg):
     # The trick here is to write out each part separately, merge them all
     # together, and then make sure that the boundary we've chosen isn't
     # present in the payload.
     msgtexts = []
     subparts = msg.get_payload()
     if subparts is None:
         # Nothing has ever been attached
         boundary = msg.get_boundary(failobj=_make_boundary())
         print >> self._fp, '--' + boundary
         print >> self._fp, '\n'
         print >> self._fp, '--' + boundary + '--'
         return
     elif _isstring(subparts):
         # e.g. a non-strict parse of a message with no starting boundary.
         self._fp.write(subparts)
         return
     elif not isinstance(subparts, ListType):
         # Scalar payload
         subparts = [subparts]
     for part in subparts:
         s = StringIO()
         g = self.clone(s)
         g.flatten(part, unixfrom=False)
         msgtexts.append(s.getvalue())
     # Now make sure the boundary we've selected doesn't appear in any of
     # the message texts.
     alltext = NL.join(msgtexts)
     # BAW: What about boundaries that are wrapped in double-quotes?
     boundary = msg.get_boundary(failobj=_make_boundary(alltext))
     # If we had to calculate a new boundary because the body text
     # contained that string, set the new boundary.  We don't do it
     # unconditionally because, while set_boundary() preserves order, it
     # doesn't preserve newlines/continuations in headers.  This is no big
     # deal in practice, but turns out to be inconvenient for the unittest
     # suite.
     if msg.get_boundary() <> boundary:
         msg.set_boundary(boundary)
     # Write out any preamble
     if msg.preamble is not None:
         self._fp.write(msg.preamble)
         # If preamble is the empty string, the length of the split will be
         # 1, but the last element will be the empty string.  If it's
         # anything else but does not end in a line separator, the length
         # will be > 1 and not end in an empty string.  We need to
         # guarantee a newline after the preamble, but don't add too many.
         plines = NLCRE.split(msg.preamble)
         if plines <> [''] and plines[-1] <> '':
             self._fp.write('\n')
     # First boundary is a bit different; it doesn't have a leading extra
     # newline.
     print >> self._fp, '--' + boundary
     # Join and write the individual parts
     joiner = '\n--' + boundary + '\n'
     self._fp.write(joiner.join(msgtexts))
     print >> self._fp, '\n--' + boundary + '--',
     # Write out any epilogue
     if msg.epilogue is not None:
         if not msg.epilogue.startswith('\n'):
             print >> self._fp
         self._fp.write(msg.epilogue)
Exemplo n.º 4
0
 def _handle_multipart(self, msg):
     # The trick here is to write out each part separately, merge them all
     # together, and then make sure that the boundary we've chosen isn't
     # present in the payload.
     msgtexts = []
     subparts = msg.get_payload()
     if subparts is None:
         # Nothing has ever been attached
         boundary = msg.get_boundary(failobj=_make_boundary())
         print >> self._fp, '--' + boundary
         print >> self._fp, '\n'
         print >> self._fp, '--' + boundary + '--'
         return
     elif _isstring(subparts):
         # e.g. a non-strict parse of a message with no starting boundary.
         self._fp.write(subparts)
         return
     elif not isinstance(subparts, ListType):
         # Scalar payload
         subparts = [subparts]
     for part in subparts:
         s = StringIO()
         g = self.clone(s)
         g.flatten(part, unixfrom=False)
         msgtexts.append(s.getvalue())
     # Now make sure the boundary we've selected doesn't appear in any of
     # the message texts.
     alltext = NL.join(msgtexts)
     # BAW: What about boundaries that are wrapped in double-quotes?
     boundary = msg.get_boundary(failobj=_make_boundary(alltext))
     # If we had to calculate a new boundary because the body text
     # contained that string, set the new boundary.  We don't do it
     # unconditionally because, while set_boundary() preserves order, it
     # doesn't preserve newlines/continuations in headers.  This is no big
     # deal in practice, but turns out to be inconvenient for the unittest
     # suite.
     if msg.get_boundary() <> boundary:
         msg.set_boundary(boundary)
     # Write out any preamble
     if msg.preamble is not None:
         self._fp.write(msg.preamble)
         # If preamble is the empty string, the length of the split will be
         # 1, but the last element will be the empty string.  If it's
         # anything else but does not end in a line separator, the length
         # will be > 1 and not end in an empty string.  We need to
         # guarantee a newline after the preamble, but don't add too many.
         plines = NLCRE.split(msg.preamble)
         if plines <> [''] and plines[-1] <> '':
             self._fp.write('\n')
     # First boundary is a bit different; it doesn't have a leading extra
     # newline.
     print >> self._fp, '--' + boundary
     # Join and write the individual parts
     joiner = '\n--' + boundary + '\n'
     self._fp.write(joiner.join(msgtexts))
     print >> self._fp, '\n--' + boundary + '--',
     # Write out any epilogue
     if msg.epilogue is not None:
         if not msg.epilogue.startswith('\n'):
             print >> self._fp
         self._fp.write(msg.epilogue)