blob: 58e2f91d622a4c1ef07baecfa50fcbc822abe599 [file] [log] [blame]
Barry Warsaw409a4c02002-04-10 21:01:31 +00001# Copyright (C) 2001,2002 Python Software Foundation
Barry Warsawba925802001-09-23 03:17:28 +00002# Author: barry@zope.com (Barry Warsaw)
3
4"""Classes to generate plain text from a message object tree.
5"""
6
7import time
8import re
9import random
10
Barry Warsaw6c2bc462002-10-14 15:09:30 +000011from types import ListType, StringType
Barry Warsawba925802001-09-23 03:17:28 +000012from cStringIO import StringIO
13
Barry Warsaw062749a2002-06-28 23:41:42 +000014from email.Header import Header
15
Barry Warsawb1c1de32002-09-10 16:13:45 +000016try:
17 from email._compat22 import _isstring
18except SyntaxError:
19 from email._compat21 import _isstring
20
Barry Warsaw56835dd2002-09-28 18:04:55 +000021try:
22 True, False
23except NameError:
24 True = 1
25 False = 0
Barry Warsawb1c1de32002-09-10 16:13:45 +000026
Barry Warsawd1eeecb2001-10-17 20:51:42 +000027EMPTYSTRING = ''
Barry Warsawba925802001-09-23 03:17:28 +000028SEMISPACE = '; '
29BAR = '|'
30UNDERSCORE = '_'
31NL = '\n'
Barry Warsawd1eeecb2001-10-17 20:51:42 +000032NLTAB = '\n\t'
Barry Warsawba925802001-09-23 03:17:28 +000033SEMINLTAB = ';\n\t'
34SPACE8 = ' ' * 8
35
36fcre = re.compile(r'^From ', re.MULTILINE)
37
Barry Warsaw6c2bc462002-10-14 15:09:30 +000038def _is8bitstring(s):
39 if isinstance(s, StringType):
40 try:
41 unicode(s, 'us-ascii')
42 except UnicodeError:
43 return True
44 return False
45
Barry Warsawba925802001-09-23 03:17:28 +000046
Barry Warsawe968ead2001-10-04 17:05:11 +000047
Barry Warsawba925802001-09-23 03:17:28 +000048class Generator:
49 """Generates output from a Message object tree.
50
51 This basic generator writes the message to the given file object as plain
52 text.
53 """
54 #
55 # Public interface
56 #
57
Barry Warsaw56835dd2002-09-28 18:04:55 +000058 def __init__(self, outfp, mangle_from_=True, maxheaderlen=78):
Barry Warsawba925802001-09-23 03:17:28 +000059 """Create the generator for message flattening.
60
61 outfp is the output file-like object for writing the message to. It
62 must have a write() method.
63
Barry Warsaw56835dd2002-09-28 18:04:55 +000064 Optional mangle_from_ is a flag that, when True (the default), escapes
65 From_ lines in the body of the message by putting a `>' in front of
66 them.
Barry Warsawba925802001-09-23 03:17:28 +000067
68 Optional maxheaderlen specifies the longest length for a non-continued
69 header. When a header line is longer (in characters, with tabs
70 expanded to 8 spaces), than maxheaderlen, the header will be broken on
71 semicolons and continued as per RFC 2822. If no semicolon is found,
72 then the header is left alone. Set to zero to disable wrapping
73 headers. Default is 78, as recommended (but not required by RFC
74 2822.
75 """
76 self._fp = outfp
77 self._mangle_from_ = mangle_from_
Barry Warsawba925802001-09-23 03:17:28 +000078 self.__maxheaderlen = maxheaderlen
79
80 def write(self, s):
81 # Just delegate to the file object
82 self._fp.write(s)
83
Barry Warsaw56835dd2002-09-28 18:04:55 +000084 def flatten(self, msg, unixfrom=False):
Barry Warsawba925802001-09-23 03:17:28 +000085 """Print the message object tree rooted at msg to the output file
86 specified when the Generator instance was created.
87
88 unixfrom is a flag that forces the printing of a Unix From_ delimiter
89 before the first object in the message tree. If the original message
90 has no From_ delimiter, a `standard' one is crafted. By default, this
Barry Warsaw56835dd2002-09-28 18:04:55 +000091 is False to inhibit the printing of any From_ delimiter.
Barry Warsawba925802001-09-23 03:17:28 +000092
93 Note that for subobjects, no From_ line is printed.
94 """
95 if unixfrom:
96 ufrom = msg.get_unixfrom()
97 if not ufrom:
98 ufrom = 'From nobody ' + time.ctime(time.time())
99 print >> self._fp, ufrom
100 self._write(msg)
101
Barry Warsaw7dc865a2002-06-02 19:02:37 +0000102 # For backwards compatibility, but this is slower
103 __call__ = flatten
104
Barry Warsaw93c40f02002-07-09 02:43:47 +0000105 def clone(self, fp):
106 """Clone this generator with the exact same options."""
107 return self.__class__(fp, self._mangle_from_, self.__maxheaderlen)
108
Barry Warsawba925802001-09-23 03:17:28 +0000109 #
110 # Protected interface - undocumented ;/
111 #
112
113 def _write(self, msg):
114 # We can't write the headers yet because of the following scenario:
115 # say a multipart message includes the boundary string somewhere in
116 # its body. We'd have to calculate the new boundary /before/ we write
117 # the headers so that we can write the correct Content-Type:
118 # parameter.
119 #
120 # The way we do this, so as to make the _handle_*() methods simpler,
121 # is to cache any subpart writes into a StringIO. The we write the
122 # headers and the StringIO contents. That way, subpart handlers can
123 # Do The Right Thing, and can still modify the Content-Type: header if
124 # necessary.
125 oldfp = self._fp
126 try:
127 self._fp = sfp = StringIO()
128 self._dispatch(msg)
129 finally:
130 self._fp = oldfp
131 # Write the headers. First we see if the message object wants to
132 # handle that itself. If not, we'll do it generically.
133 meth = getattr(msg, '_write_headers', None)
134 if meth is None:
135 self._write_headers(msg)
136 else:
137 meth(self)
138 self._fp.write(sfp.getvalue())
139
140 def _dispatch(self, msg):
141 # Get the Content-Type: for the message, then try to dispatch to
Barry Warsawf488b2c2002-07-11 18:48:40 +0000142 # self._handle_<maintype>_<subtype>(). If there's no handler for the
143 # full MIME type, then dispatch to self._handle_<maintype>(). If
144 # that's missing too, then dispatch to self._writeBody().
Barry Warsawdfea3b32002-08-20 14:47:30 +0000145 main = msg.get_content_maintype()
146 sub = msg.get_content_subtype()
Barry Warsaw93c40f02002-07-09 02:43:47 +0000147 specific = UNDERSCORE.join((main, sub)).replace('-', '_')
148 meth = getattr(self, '_handle_' + specific, None)
149 if meth is None:
150 generic = main.replace('-', '_')
151 meth = getattr(self, '_handle_' + generic, None)
Barry Warsawba925802001-09-23 03:17:28 +0000152 if meth is None:
Barry Warsaw93c40f02002-07-09 02:43:47 +0000153 meth = self._writeBody
154 meth(msg)
Barry Warsawba925802001-09-23 03:17:28 +0000155
156 #
157 # Default handlers
158 #
159
160 def _write_headers(self, msg):
161 for h, v in msg.items():
Barry Warsawba925802001-09-23 03:17:28 +0000162 # RFC 2822 says that lines SHOULD be no more than maxheaderlen
163 # characters wide, so we're well within our rights to split long
164 # headers.
165 text = '%s: %s' % (h, v)
166 if self.__maxheaderlen > 0 and len(text) > self.__maxheaderlen:
Barry Warsaw56835dd2002-09-28 18:04:55 +0000167 text = self._split_header(text)
Barry Warsawba925802001-09-23 03:17:28 +0000168 print >> self._fp, text
169 # A blank line always separates headers from body
170 print >> self._fp
171
Barry Warsaw56835dd2002-09-28 18:04:55 +0000172 def _split_header(self, text):
Barry Warsawba925802001-09-23 03:17:28 +0000173 maxheaderlen = self.__maxheaderlen
174 # Find out whether any lines in the header are really longer than
175 # maxheaderlen characters wide. There could be continuation lines
176 # that actually shorten it. Also, replace hard tabs with 8 spaces.
Barry Warsaw062749a2002-06-28 23:41:42 +0000177 lines = [s.replace('\t', SPACE8) for s in text.splitlines()]
Barry Warsawba925802001-09-23 03:17:28 +0000178 for line in lines:
179 if len(line) > maxheaderlen:
180 break
181 else:
182 # No line was actually longer than maxheaderlen characters, so
183 # just return the original unchanged.
184 return text
Barry Warsaw6c2bc462002-10-14 15:09:30 +0000185 # If we have raw 8bit data in a byte string, we have no idea what the
186 # encoding is. I think there is no safe way to split this string. If
187 # it's ascii-subset, then we could do a normal ascii split, but if
188 # it's multibyte then we could break the string. There's no way to
189 # know so the least harm seems to be to not split the string and risk
190 # it being too long.
191 if _is8bitstring(text):
192 return text
Barry Warsaw062749a2002-06-28 23:41:42 +0000193 # The `text' argument already has the field name prepended, so don't
194 # provide it here or the first line will get folded too short.
195 h = Header(text, maxlinelen=maxheaderlen,
196 # For backwards compatibility, we use a hard tab here
197 continuation_ws='\t')
198 return h.encode()
Barry Warsawba925802001-09-23 03:17:28 +0000199
200 #
201 # Handlers for writing types and subtypes
202 #
203
204 def _handle_text(self, msg):
205 payload = msg.get_payload()
Barry Warsawb384e012001-09-26 05:32:41 +0000206 if payload is None:
207 return
Barry Warsaw409a4c02002-04-10 21:01:31 +0000208 cset = msg.get_charset()
209 if cset is not None:
210 payload = cset.body_encode(payload)
Barry Warsawb1c1de32002-09-10 16:13:45 +0000211 if not _isstring(payload):
Barry Warsawb384e012001-09-26 05:32:41 +0000212 raise TypeError, 'string payload expected: %s' % type(payload)
Barry Warsawba925802001-09-23 03:17:28 +0000213 if self._mangle_from_:
214 payload = fcre.sub('>From ', payload)
215 self._fp.write(payload)
216
217 # Default body handler
218 _writeBody = _handle_text
219
Barry Warsaw93c40f02002-07-09 02:43:47 +0000220 def _handle_multipart(self, msg):
Barry Warsawba925802001-09-23 03:17:28 +0000221 # The trick here is to write out each part separately, merge them all
222 # together, and then make sure that the boundary we've chosen isn't
223 # present in the payload.
224 msgtexts = []
Barry Warsaw409a4c02002-04-10 21:01:31 +0000225 subparts = msg.get_payload()
226 if subparts is None:
Barry Warsaw93c40f02002-07-09 02:43:47 +0000227 # Nothing has ever been attached
Barry Warsaw409a4c02002-04-10 21:01:31 +0000228 boundary = msg.get_boundary(failobj=_make_boundary())
229 print >> self._fp, '--' + boundary
230 print >> self._fp, '\n'
231 print >> self._fp, '--' + boundary + '--'
232 return
Barry Warsawb1c1de32002-09-10 16:13:45 +0000233 elif _isstring(subparts):
234 # e.g. a non-strict parse of a message with no starting boundary.
235 self._fp.write(subparts)
236 return
Barry Warsaw409a4c02002-04-10 21:01:31 +0000237 elif not isinstance(subparts, ListType):
238 # Scalar payload
239 subparts = [subparts]
240 for part in subparts:
Barry Warsawba925802001-09-23 03:17:28 +0000241 s = StringIO()
Barry Warsaw93c40f02002-07-09 02:43:47 +0000242 g = self.clone(s)
Barry Warsaw56835dd2002-09-28 18:04:55 +0000243 g.flatten(part, unixfrom=False)
Barry Warsawba925802001-09-23 03:17:28 +0000244 msgtexts.append(s.getvalue())
245 # Now make sure the boundary we've selected doesn't appear in any of
246 # the message texts.
247 alltext = NL.join(msgtexts)
248 # BAW: What about boundaries that are wrapped in double-quotes?
249 boundary = msg.get_boundary(failobj=_make_boundary(alltext))
250 # If we had to calculate a new boundary because the body text
251 # contained that string, set the new boundary. We don't do it
252 # unconditionally because, while set_boundary() preserves order, it
253 # doesn't preserve newlines/continuations in headers. This is no big
254 # deal in practice, but turns out to be inconvenient for the unittest
255 # suite.
256 if msg.get_boundary() <> boundary:
257 msg.set_boundary(boundary)
258 # Write out any preamble
259 if msg.preamble is not None:
260 self._fp.write(msg.preamble)
261 # First boundary is a bit different; it doesn't have a leading extra
262 # newline.
263 print >> self._fp, '--' + boundary
Barry Warsawba925802001-09-23 03:17:28 +0000264 # Join and write the individual parts
265 joiner = '\n--' + boundary + '\n'
Barry Warsawba925802001-09-23 03:17:28 +0000266 self._fp.write(joiner.join(msgtexts))
267 print >> self._fp, '\n--' + boundary + '--',
268 # Write out any epilogue
269 if msg.epilogue is not None:
Barry Warsaw856c32b2001-10-19 04:06:39 +0000270 if not msg.epilogue.startswith('\n'):
271 print >> self._fp
Barry Warsawba925802001-09-23 03:17:28 +0000272 self._fp.write(msg.epilogue)
273
Barry Warsawb384e012001-09-26 05:32:41 +0000274 def _handle_message_delivery_status(self, msg):
275 # We can't just write the headers directly to self's file object
276 # because this will leave an extra newline between the last header
277 # block and the boundary. Sigh.
278 blocks = []
279 for part in msg.get_payload():
280 s = StringIO()
Barry Warsaw93c40f02002-07-09 02:43:47 +0000281 g = self.clone(s)
Barry Warsaw56835dd2002-09-28 18:04:55 +0000282 g.flatten(part, unixfrom=False)
Barry Warsawb384e012001-09-26 05:32:41 +0000283 text = s.getvalue()
284 lines = text.split('\n')
285 # Strip off the unnecessary trailing empty line
286 if lines and lines[-1] == '':
287 blocks.append(NL.join(lines[:-1]))
288 else:
289 blocks.append(text)
290 # Now join all the blocks with an empty line. This has the lovely
291 # effect of separating each block with an empty line, but not adding
292 # an extra one after the last one.
293 self._fp.write(NL.join(blocks))
294
295 def _handle_message(self, msg):
Barry Warsawba925802001-09-23 03:17:28 +0000296 s = StringIO()
Barry Warsaw93c40f02002-07-09 02:43:47 +0000297 g = self.clone(s)
Barry Warsaw7dc865a2002-06-02 19:02:37 +0000298 # The payload of a message/rfc822 part should be a multipart sequence
299 # of length 1. The zeroth element of the list should be the Message
Barry Warsaw93c40f02002-07-09 02:43:47 +0000300 # object for the subpart. Extract that object, stringify it, and
301 # write it out.
Barry Warsaw56835dd2002-09-28 18:04:55 +0000302 g.flatten(msg.get_payload(0), unixfrom=False)
Barry Warsawba925802001-09-23 03:17:28 +0000303 self._fp.write(s.getvalue())
304
305
Barry Warsawe968ead2001-10-04 17:05:11 +0000306
Barry Warsawba925802001-09-23 03:17:28 +0000307class DecodedGenerator(Generator):
308 """Generator a text representation of a message.
309
310 Like the Generator base class, except that non-text parts are substituted
311 with a format string representing the part.
312 """
Barry Warsaw56835dd2002-09-28 18:04:55 +0000313 def __init__(self, outfp, mangle_from_=True, maxheaderlen=78, fmt=None):
Barry Warsawba925802001-09-23 03:17:28 +0000314 """Like Generator.__init__() except that an additional optional
315 argument is allowed.
316
317 Walks through all subparts of a message. If the subpart is of main
318 type `text', then it prints the decoded payload of the subpart.
319
320 Otherwise, fmt is a format string that is used instead of the message
321 payload. fmt is expanded with the following keywords (in
322 %(keyword)s format):
323
324 type : Full MIME type of the non-text part
325 maintype : Main MIME type of the non-text part
326 subtype : Sub-MIME type of the non-text part
327 filename : Filename of the non-text part
328 description: Description associated with the non-text part
329 encoding : Content transfer encoding of the non-text part
330
331 The default value for fmt is None, meaning
332
333 [Non-text (%(type)s) part of message omitted, filename %(filename)s]
334 """
335 Generator.__init__(self, outfp, mangle_from_, maxheaderlen)
336 if fmt is None:
337 fmt = ('[Non-text (%(type)s) part of message omitted, '
338 'filename %(filename)s]')
339 self._fmt = fmt
340
341 def _dispatch(self, msg):
342 for part in msg.walk():
Barry Warsawb384e012001-09-26 05:32:41 +0000343 maintype = part.get_main_type('text')
344 if maintype == 'text':
Barry Warsaw56835dd2002-09-28 18:04:55 +0000345 print >> self, part.get_payload(decode=True)
Barry Warsawb384e012001-09-26 05:32:41 +0000346 elif maintype == 'multipart':
347 # Just skip this
348 pass
Barry Warsawba925802001-09-23 03:17:28 +0000349 else:
350 print >> self, self._fmt % {
351 'type' : part.get_type('[no MIME type]'),
352 'maintype' : part.get_main_type('[no main MIME type]'),
353 'subtype' : part.get_subtype('[no sub-MIME type]'),
354 'filename' : part.get_filename('[no filename]'),
355 'description': part.get('Content-Description',
356 '[no description]'),
357 'encoding' : part.get('Content-Transfer-Encoding',
358 '[no encoding]'),
359 }
360
361
Barry Warsawe968ead2001-10-04 17:05:11 +0000362
Barry Warsawba925802001-09-23 03:17:28 +0000363# Helper
Barry Warsaw409a4c02002-04-10 21:01:31 +0000364def _make_boundary(text=None):
Barry Warsawba925802001-09-23 03:17:28 +0000365 # Craft a random boundary. If text is given, ensure that the chosen
366 # boundary doesn't appear in the text.
367 boundary = ('=' * 15) + repr(random.random()).split('.')[1] + '=='
368 if text is None:
369 return boundary
370 b = boundary
371 counter = 0
Barry Warsaw56835dd2002-09-28 18:04:55 +0000372 while True:
Barry Warsawba925802001-09-23 03:17:28 +0000373 cre = re.compile('^--' + re.escape(b) + '(--)?$', re.MULTILINE)
374 if not cre.search(text):
375 break
376 b = boundary + '.' + str(counter)
377 counter += 1
378 return b