Barry Warsaw | e58df82 | 2006-02-08 14:34:21 +0000 | [diff] [blame] | 1 | # Copyright (C) 2001-2006 Python Software Foundation |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 2 | # Author: Barry Warsaw |
| 3 | # Contact: email-sig@python.org |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 4 | |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 5 | """Classes to generate plain text from a message object tree.""" |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 6 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7 | __all__ = ['Generator', 'DecodedGenerator'] |
| 8 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 9 | import re |
Barry Warsaw | db6888b | 2003-05-29 19:39:33 +0000 | [diff] [blame] | 10 | import sys |
Barry Warsaw | 5d384ef | 2003-03-06 05:22:02 +0000 | [diff] [blame] | 11 | import time |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 12 | import random |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 13 | import warnings |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 14 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 15 | from cStringIO import StringIO |
| 16 | from email.header import Header |
Barry Warsaw | 062749a | 2002-06-28 23:41:42 +0000 | [diff] [blame] | 17 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 18 | UNDERSCORE = '_' |
| 19 | NL = '\n' |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 20 | |
| 21 | fcre = re.compile(r'^From ', re.MULTILINE) |
| 22 | |
Barry Warsaw | 6c2bc46 | 2002-10-14 15:09:30 +0000 | [diff] [blame] | 23 | def _is8bitstring(s): |
Guido van Rossum | d4eda82 | 2007-07-21 00:21:26 +0000 | [diff] [blame] | 24 | if isinstance(s, bytes): |
Barry Warsaw | 6c2bc46 | 2002-10-14 15:09:30 +0000 | [diff] [blame] | 25 | try: |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 26 | str(s, 'us-ascii') |
Barry Warsaw | 6c2bc46 | 2002-10-14 15:09:30 +0000 | [diff] [blame] | 27 | return True |
Guido van Rossum | d4eda82 | 2007-07-21 00:21:26 +0000 | [diff] [blame] | 28 | except UnicodeError: |
| 29 | pass |
| 30 | elif isinstance(s, str): |
| 31 | try: |
| 32 | s.decode('us-ascii') |
| 33 | return True |
| 34 | except UnicodeError: |
| 35 | pass |
Barry Warsaw | 6c2bc46 | 2002-10-14 15:09:30 +0000 | [diff] [blame] | 36 | return False |
| 37 | |
Barry Warsaw | e968ead | 2001-10-04 17:05:11 +0000 | [diff] [blame] | 38 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 39 | class Generator: |
| 40 | """Generates output from a Message object tree. |
| 41 | |
| 42 | This basic generator writes the message to the given file object as plain |
| 43 | text. |
| 44 | """ |
| 45 | # |
| 46 | # Public interface |
| 47 | # |
| 48 | |
Barry Warsaw | 56835dd | 2002-09-28 18:04:55 +0000 | [diff] [blame] | 49 | def __init__(self, outfp, mangle_from_=True, maxheaderlen=78): |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 50 | """Create the generator for message flattening. |
| 51 | |
| 52 | outfp is the output file-like object for writing the message to. It |
| 53 | must have a write() method. |
| 54 | |
Barry Warsaw | 56835dd | 2002-09-28 18:04:55 +0000 | [diff] [blame] | 55 | Optional mangle_from_ is a flag that, when True (the default), escapes |
| 56 | From_ lines in the body of the message by putting a `>' in front of |
| 57 | them. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 58 | |
| 59 | Optional maxheaderlen specifies the longest length for a non-continued |
| 60 | header. When a header line is longer (in characters, with tabs |
Barry Warsaw | b03136a | 2003-11-19 02:23:01 +0000 | [diff] [blame] | 61 | expanded to 8 spaces) than maxheaderlen, the header will split as |
| 62 | defined in the Header class. Set maxheaderlen to zero to disable |
| 63 | header wrapping. The default is 78, as recommended (but not required) |
| 64 | by RFC 2822. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 65 | """ |
| 66 | self._fp = outfp |
| 67 | self._mangle_from_ = mangle_from_ |
Barry Warsaw | 36112f2 | 2004-05-09 03:35:17 +0000 | [diff] [blame] | 68 | self._maxheaderlen = maxheaderlen |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 69 | |
| 70 | def write(self, s): |
| 71 | # Just delegate to the file object |
| 72 | self._fp.write(s) |
| 73 | |
Barry Warsaw | 56835dd | 2002-09-28 18:04:55 +0000 | [diff] [blame] | 74 | def flatten(self, msg, unixfrom=False): |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 75 | """Print the message object tree rooted at msg to the output file |
| 76 | specified when the Generator instance was created. |
| 77 | |
| 78 | unixfrom is a flag that forces the printing of a Unix From_ delimiter |
| 79 | before the first object in the message tree. If the original message |
| 80 | has no From_ delimiter, a `standard' one is crafted. By default, this |
Barry Warsaw | 56835dd | 2002-09-28 18:04:55 +0000 | [diff] [blame] | 81 | is False to inhibit the printing of any From_ delimiter. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 82 | |
| 83 | Note that for subobjects, no From_ line is printed. |
| 84 | """ |
| 85 | if unixfrom: |
| 86 | ufrom = msg.get_unixfrom() |
| 87 | if not ufrom: |
| 88 | ufrom = 'From nobody ' + time.ctime(time.time()) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 89 | print(ufrom, file=self._fp) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 90 | self._write(msg) |
| 91 | |
Barry Warsaw | 93c40f0 | 2002-07-09 02:43:47 +0000 | [diff] [blame] | 92 | def clone(self, fp): |
| 93 | """Clone this generator with the exact same options.""" |
Barry Warsaw | 36112f2 | 2004-05-09 03:35:17 +0000 | [diff] [blame] | 94 | return self.__class__(fp, self._mangle_from_, self._maxheaderlen) |
Barry Warsaw | 93c40f0 | 2002-07-09 02:43:47 +0000 | [diff] [blame] | 95 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 96 | # |
| 97 | # Protected interface - undocumented ;/ |
| 98 | # |
| 99 | |
| 100 | def _write(self, msg): |
| 101 | # We can't write the headers yet because of the following scenario: |
| 102 | # say a multipart message includes the boundary string somewhere in |
| 103 | # its body. We'd have to calculate the new boundary /before/ we write |
| 104 | # the headers so that we can write the correct Content-Type: |
| 105 | # parameter. |
| 106 | # |
| 107 | # The way we do this, so as to make the _handle_*() methods simpler, |
| 108 | # is to cache any subpart writes into a StringIO. The we write the |
| 109 | # headers and the StringIO contents. That way, subpart handlers can |
| 110 | # Do The Right Thing, and can still modify the Content-Type: header if |
| 111 | # necessary. |
| 112 | oldfp = self._fp |
| 113 | try: |
| 114 | self._fp = sfp = StringIO() |
| 115 | self._dispatch(msg) |
| 116 | finally: |
| 117 | self._fp = oldfp |
| 118 | # Write the headers. First we see if the message object wants to |
| 119 | # handle that itself. If not, we'll do it generically. |
| 120 | meth = getattr(msg, '_write_headers', None) |
| 121 | if meth is None: |
| 122 | self._write_headers(msg) |
| 123 | else: |
| 124 | meth(self) |
| 125 | self._fp.write(sfp.getvalue()) |
| 126 | |
| 127 | def _dispatch(self, msg): |
| 128 | # Get the Content-Type: for the message, then try to dispatch to |
Barry Warsaw | f488b2c | 2002-07-11 18:48:40 +0000 | [diff] [blame] | 129 | # self._handle_<maintype>_<subtype>(). If there's no handler for the |
| 130 | # full MIME type, then dispatch to self._handle_<maintype>(). If |
| 131 | # that's missing too, then dispatch to self._writeBody(). |
Barry Warsaw | dfea3b3 | 2002-08-20 14:47:30 +0000 | [diff] [blame] | 132 | main = msg.get_content_maintype() |
| 133 | sub = msg.get_content_subtype() |
Barry Warsaw | 93c40f0 | 2002-07-09 02:43:47 +0000 | [diff] [blame] | 134 | specific = UNDERSCORE.join((main, sub)).replace('-', '_') |
| 135 | meth = getattr(self, '_handle_' + specific, None) |
| 136 | if meth is None: |
| 137 | generic = main.replace('-', '_') |
| 138 | meth = getattr(self, '_handle_' + generic, None) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 139 | if meth is None: |
Barry Warsaw | 93c40f0 | 2002-07-09 02:43:47 +0000 | [diff] [blame] | 140 | meth = self._writeBody |
| 141 | meth(msg) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 142 | |
| 143 | # |
| 144 | # Default handlers |
| 145 | # |
| 146 | |
| 147 | def _write_headers(self, msg): |
| 148 | for h, v in msg.items(): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 149 | print('%s:' % h, end=' ', file=self._fp) |
Barry Warsaw | 36112f2 | 2004-05-09 03:35:17 +0000 | [diff] [blame] | 150 | if self._maxheaderlen == 0: |
Barry Warsaw | ce6bf59 | 2003-03-07 15:43:17 +0000 | [diff] [blame] | 151 | # Explicit no-wrapping |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 152 | print(v, file=self._fp) |
Barry Warsaw | ce6bf59 | 2003-03-07 15:43:17 +0000 | [diff] [blame] | 153 | elif isinstance(v, Header): |
| 154 | # Header instances know what to do |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 155 | print(v.encode(), file=self._fp) |
Barry Warsaw | ce6bf59 | 2003-03-07 15:43:17 +0000 | [diff] [blame] | 156 | elif _is8bitstring(v): |
| 157 | # If we have raw 8bit data in a byte string, we have no idea |
| 158 | # what the encoding is. There is no safe way to split this |
| 159 | # string. If it's ascii-subset, then we could do a normal |
| 160 | # ascii split, but if it's multibyte then we could break the |
| 161 | # string. There's no way to know so the least harm seems to |
| 162 | # be to not split the string and risk it being too long. |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 163 | print(v, file=self._fp) |
Barry Warsaw | ce6bf59 | 2003-03-07 15:43:17 +0000 | [diff] [blame] | 164 | else: |
| 165 | # Header's got lots of smarts, so use it. |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 166 | print(Header( |
Barry Warsaw | 36112f2 | 2004-05-09 03:35:17 +0000 | [diff] [blame] | 167 | v, maxlinelen=self._maxheaderlen, |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 168 | header_name=h, continuation_ws='\t').encode(), file=self._fp) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 169 | # A blank line always separates headers from body |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 170 | print(file=self._fp) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 171 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 172 | # |
| 173 | # Handlers for writing types and subtypes |
| 174 | # |
| 175 | |
| 176 | def _handle_text(self, msg): |
| 177 | payload = msg.get_payload() |
Barry Warsaw | b384e01 | 2001-09-26 05:32:41 +0000 | [diff] [blame] | 178 | if payload is None: |
| 179 | return |
Barry Warsaw | 36112f2 | 2004-05-09 03:35:17 +0000 | [diff] [blame] | 180 | if not isinstance(payload, basestring): |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 181 | raise TypeError('string payload expected: %s' % type(payload)) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 182 | if self._mangle_from_: |
| 183 | payload = fcre.sub('>From ', payload) |
| 184 | self._fp.write(payload) |
| 185 | |
| 186 | # Default body handler |
| 187 | _writeBody = _handle_text |
| 188 | |
Barry Warsaw | 93c40f0 | 2002-07-09 02:43:47 +0000 | [diff] [blame] | 189 | def _handle_multipart(self, msg): |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 190 | # The trick here is to write out each part separately, merge them all |
| 191 | # together, and then make sure that the boundary we've chosen isn't |
| 192 | # present in the payload. |
| 193 | msgtexts = [] |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 194 | subparts = msg.get_payload() |
| 195 | if subparts is None: |
Barry Warsaw | 36112f2 | 2004-05-09 03:35:17 +0000 | [diff] [blame] | 196 | subparts = [] |
| 197 | elif isinstance(subparts, basestring): |
Barry Warsaw | b1c1de3 | 2002-09-10 16:13:45 +0000 | [diff] [blame] | 198 | # e.g. a non-strict parse of a message with no starting boundary. |
| 199 | self._fp.write(subparts) |
| 200 | return |
Barry Warsaw | 36112f2 | 2004-05-09 03:35:17 +0000 | [diff] [blame] | 201 | elif not isinstance(subparts, list): |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 202 | # Scalar payload |
| 203 | subparts = [subparts] |
| 204 | for part in subparts: |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 205 | s = StringIO() |
Barry Warsaw | 93c40f0 | 2002-07-09 02:43:47 +0000 | [diff] [blame] | 206 | g = self.clone(s) |
Barry Warsaw | 56835dd | 2002-09-28 18:04:55 +0000 | [diff] [blame] | 207 | g.flatten(part, unixfrom=False) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 208 | msgtexts.append(s.getvalue()) |
| 209 | # Now make sure the boundary we've selected doesn't appear in any of |
| 210 | # the message texts. |
| 211 | alltext = NL.join(msgtexts) |
| 212 | # BAW: What about boundaries that are wrapped in double-quotes? |
| 213 | boundary = msg.get_boundary(failobj=_make_boundary(alltext)) |
| 214 | # If we had to calculate a new boundary because the body text |
| 215 | # contained that string, set the new boundary. We don't do it |
| 216 | # unconditionally because, while set_boundary() preserves order, it |
| 217 | # doesn't preserve newlines/continuations in headers. This is no big |
| 218 | # deal in practice, but turns out to be inconvenient for the unittest |
| 219 | # suite. |
Guido van Rossum | b053cd8 | 2006-08-24 03:53:23 +0000 | [diff] [blame] | 220 | if msg.get_boundary() != boundary: |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 221 | msg.set_boundary(boundary) |
Barry Warsaw | 36112f2 | 2004-05-09 03:35:17 +0000 | [diff] [blame] | 222 | # If there's a preamble, write it out, with a trailing CRLF |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 223 | if msg.preamble is not None: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 224 | print(msg.preamble, file=self._fp) |
Barry Warsaw | 36112f2 | 2004-05-09 03:35:17 +0000 | [diff] [blame] | 225 | # dash-boundary transport-padding CRLF |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 226 | print('--' + boundary, file=self._fp) |
Barry Warsaw | 36112f2 | 2004-05-09 03:35:17 +0000 | [diff] [blame] | 227 | # body-part |
| 228 | if msgtexts: |
| 229 | self._fp.write(msgtexts.pop(0)) |
| 230 | # *encapsulation |
| 231 | # --> delimiter transport-padding |
| 232 | # --> CRLF body-part |
| 233 | for body_part in msgtexts: |
| 234 | # delimiter transport-padding CRLF |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 235 | print('\n--' + boundary, file=self._fp) |
Barry Warsaw | 36112f2 | 2004-05-09 03:35:17 +0000 | [diff] [blame] | 236 | # body-part |
| 237 | self._fp.write(body_part) |
| 238 | # close-delimiter transport-padding |
| 239 | self._fp.write('\n--' + boundary + '--') |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 240 | if msg.epilogue is not None: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 241 | print(file=self._fp) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 242 | self._fp.write(msg.epilogue) |
| 243 | |
Barry Warsaw | b384e01 | 2001-09-26 05:32:41 +0000 | [diff] [blame] | 244 | def _handle_message_delivery_status(self, msg): |
| 245 | # We can't just write the headers directly to self's file object |
| 246 | # because this will leave an extra newline between the last header |
| 247 | # block and the boundary. Sigh. |
| 248 | blocks = [] |
| 249 | for part in msg.get_payload(): |
| 250 | s = StringIO() |
Barry Warsaw | 93c40f0 | 2002-07-09 02:43:47 +0000 | [diff] [blame] | 251 | g = self.clone(s) |
Barry Warsaw | 56835dd | 2002-09-28 18:04:55 +0000 | [diff] [blame] | 252 | g.flatten(part, unixfrom=False) |
Barry Warsaw | b384e01 | 2001-09-26 05:32:41 +0000 | [diff] [blame] | 253 | text = s.getvalue() |
| 254 | lines = text.split('\n') |
| 255 | # Strip off the unnecessary trailing empty line |
| 256 | if lines and lines[-1] == '': |
| 257 | blocks.append(NL.join(lines[:-1])) |
| 258 | else: |
| 259 | blocks.append(text) |
| 260 | # Now join all the blocks with an empty line. This has the lovely |
| 261 | # effect of separating each block with an empty line, but not adding |
| 262 | # an extra one after the last one. |
| 263 | self._fp.write(NL.join(blocks)) |
| 264 | |
| 265 | def _handle_message(self, msg): |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 266 | s = StringIO() |
Barry Warsaw | 93c40f0 | 2002-07-09 02:43:47 +0000 | [diff] [blame] | 267 | g = self.clone(s) |
Barry Warsaw | 7dc865a | 2002-06-02 19:02:37 +0000 | [diff] [blame] | 268 | # The payload of a message/rfc822 part should be a multipart sequence |
| 269 | # of length 1. The zeroth element of the list should be the Message |
Barry Warsaw | 93c40f0 | 2002-07-09 02:43:47 +0000 | [diff] [blame] | 270 | # object for the subpart. Extract that object, stringify it, and |
| 271 | # write it out. |
Barry Warsaw | 56835dd | 2002-09-28 18:04:55 +0000 | [diff] [blame] | 272 | g.flatten(msg.get_payload(0), unixfrom=False) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 273 | self._fp.write(s.getvalue()) |
| 274 | |
| 275 | |
Barry Warsaw | e968ead | 2001-10-04 17:05:11 +0000 | [diff] [blame] | 276 | |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 277 | _FMT = '[Non-text (%(type)s) part of message omitted, filename %(filename)s]' |
| 278 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 279 | class DecodedGenerator(Generator): |
| 280 | """Generator a text representation of a message. |
| 281 | |
| 282 | Like the Generator base class, except that non-text parts are substituted |
| 283 | with a format string representing the part. |
| 284 | """ |
Barry Warsaw | 56835dd | 2002-09-28 18:04:55 +0000 | [diff] [blame] | 285 | def __init__(self, outfp, mangle_from_=True, maxheaderlen=78, fmt=None): |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 286 | """Like Generator.__init__() except that an additional optional |
| 287 | argument is allowed. |
| 288 | |
| 289 | Walks through all subparts of a message. If the subpart is of main |
| 290 | type `text', then it prints the decoded payload of the subpart. |
| 291 | |
| 292 | Otherwise, fmt is a format string that is used instead of the message |
| 293 | payload. fmt is expanded with the following keywords (in |
| 294 | %(keyword)s format): |
| 295 | |
| 296 | type : Full MIME type of the non-text part |
| 297 | maintype : Main MIME type of the non-text part |
| 298 | subtype : Sub-MIME type of the non-text part |
| 299 | filename : Filename of the non-text part |
| 300 | description: Description associated with the non-text part |
| 301 | encoding : Content transfer encoding of the non-text part |
| 302 | |
| 303 | The default value for fmt is None, meaning |
| 304 | |
| 305 | [Non-text (%(type)s) part of message omitted, filename %(filename)s] |
| 306 | """ |
| 307 | Generator.__init__(self, outfp, mangle_from_, maxheaderlen) |
| 308 | if fmt is None: |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 309 | self._fmt = _FMT |
| 310 | else: |
| 311 | self._fmt = fmt |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 312 | |
| 313 | def _dispatch(self, msg): |
| 314 | for part in msg.walk(): |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 315 | maintype = part.get_content_maintype() |
Barry Warsaw | b384e01 | 2001-09-26 05:32:41 +0000 | [diff] [blame] | 316 | if maintype == 'text': |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 317 | print(part.get_payload(decode=True), file=self) |
Barry Warsaw | b384e01 | 2001-09-26 05:32:41 +0000 | [diff] [blame] | 318 | elif maintype == 'multipart': |
| 319 | # Just skip this |
| 320 | pass |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 321 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 322 | print(self._fmt % { |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 323 | 'type' : part.get_content_type(), |
| 324 | 'maintype' : part.get_content_maintype(), |
| 325 | 'subtype' : part.get_content_subtype(), |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 326 | 'filename' : part.get_filename('[no filename]'), |
| 327 | 'description': part.get('Content-Description', |
| 328 | '[no description]'), |
| 329 | 'encoding' : part.get('Content-Transfer-Encoding', |
| 330 | '[no encoding]'), |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 331 | }, file=self) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 332 | |
| 333 | |
Barry Warsaw | e968ead | 2001-10-04 17:05:11 +0000 | [diff] [blame] | 334 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 335 | # Helper |
Barry Warsaw | db6888b | 2003-05-29 19:39:33 +0000 | [diff] [blame] | 336 | _width = len(repr(sys.maxint-1)) |
| 337 | _fmt = '%%0%dd' % _width |
| 338 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 339 | def _make_boundary(text=None): |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 340 | # Craft a random boundary. If text is given, ensure that the chosen |
| 341 | # boundary doesn't appear in the text. |
Barry Warsaw | 663219a | 2003-06-24 20:19:34 +0000 | [diff] [blame] | 342 | token = random.randrange(sys.maxint) |
Barry Warsaw | db6888b | 2003-05-29 19:39:33 +0000 | [diff] [blame] | 343 | boundary = ('=' * 15) + (_fmt % token) + '==' |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 344 | if text is None: |
| 345 | return boundary |
| 346 | b = boundary |
| 347 | counter = 0 |
Barry Warsaw | 56835dd | 2002-09-28 18:04:55 +0000 | [diff] [blame] | 348 | while True: |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 349 | cre = re.compile('^--' + re.escape(b) + '(--)?$', re.MULTILINE) |
| 350 | if not cre.search(text): |
| 351 | break |
| 352 | b = boundary + '.' + str(counter) |
| 353 | counter += 1 |
| 354 | return b |