blob: 12b9fdf74b911df2ed0256b915af809710399332 [file] [log] [blame]
Barry Warsawba925802001-09-23 03:17:28 +00001# Copyright (C) 2001 Python Software Foundation
2# 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
11from types import ListType, StringType
12from cStringIO import StringIO
13
14# Intrapackage imports
15import Message
16import Errors
17
Barry Warsawd1eeecb2001-10-17 20:51:42 +000018EMPTYSTRING = ''
Barry Warsawba925802001-09-23 03:17:28 +000019SEMISPACE = '; '
20BAR = '|'
21UNDERSCORE = '_'
22NL = '\n'
Barry Warsawd1eeecb2001-10-17 20:51:42 +000023NLTAB = '\n\t'
Barry Warsawba925802001-09-23 03:17:28 +000024SEMINLTAB = ';\n\t'
25SPACE8 = ' ' * 8
26
27fcre = re.compile(r'^From ', re.MULTILINE)
28
29
Barry Warsawe968ead2001-10-04 17:05:11 +000030
Barry Warsawba925802001-09-23 03:17:28 +000031class Generator:
32 """Generates output from a Message object tree.
33
34 This basic generator writes the message to the given file object as plain
35 text.
36 """
37 #
38 # Public interface
39 #
40
41 def __init__(self, outfp, mangle_from_=1, maxheaderlen=78):
42 """Create the generator for message flattening.
43
44 outfp is the output file-like object for writing the message to. It
45 must have a write() method.
46
47 Optional mangle_from_ is a flag that, when true, escapes From_ lines
48 in the body of the message by putting a `>' in front of them.
49
50 Optional maxheaderlen specifies the longest length for a non-continued
51 header. When a header line is longer (in characters, with tabs
52 expanded to 8 spaces), than maxheaderlen, the header will be broken on
53 semicolons and continued as per RFC 2822. If no semicolon is found,
54 then the header is left alone. Set to zero to disable wrapping
55 headers. Default is 78, as recommended (but not required by RFC
56 2822.
57 """
58 self._fp = outfp
59 self._mangle_from_ = mangle_from_
60 self.__first = 1
61 self.__maxheaderlen = maxheaderlen
62
63 def write(self, s):
64 # Just delegate to the file object
65 self._fp.write(s)
66
67 def __call__(self, msg, unixfrom=0):
68 """Print the message object tree rooted at msg to the output file
69 specified when the Generator instance was created.
70
71 unixfrom is a flag that forces the printing of a Unix From_ delimiter
72 before the first object in the message tree. If the original message
73 has no From_ delimiter, a `standard' one is crafted. By default, this
74 is 0 to inhibit the printing of any From_ delimiter.
75
76 Note that for subobjects, no From_ line is printed.
77 """
78 if unixfrom:
79 ufrom = msg.get_unixfrom()
80 if not ufrom:
81 ufrom = 'From nobody ' + time.ctime(time.time())
82 print >> self._fp, ufrom
83 self._write(msg)
84
85 #
86 # Protected interface - undocumented ;/
87 #
88
89 def _write(self, msg):
90 # We can't write the headers yet because of the following scenario:
91 # say a multipart message includes the boundary string somewhere in
92 # its body. We'd have to calculate the new boundary /before/ we write
93 # the headers so that we can write the correct Content-Type:
94 # parameter.
95 #
96 # The way we do this, so as to make the _handle_*() methods simpler,
97 # is to cache any subpart writes into a StringIO. The we write the
98 # headers and the StringIO contents. That way, subpart handlers can
99 # Do The Right Thing, and can still modify the Content-Type: header if
100 # necessary.
101 oldfp = self._fp
102 try:
103 self._fp = sfp = StringIO()
104 self._dispatch(msg)
105 finally:
106 self._fp = oldfp
107 # Write the headers. First we see if the message object wants to
108 # handle that itself. If not, we'll do it generically.
109 meth = getattr(msg, '_write_headers', None)
110 if meth is None:
111 self._write_headers(msg)
112 else:
113 meth(self)
114 self._fp.write(sfp.getvalue())
115
116 def _dispatch(self, msg):
117 # Get the Content-Type: for the message, then try to dispatch to
118 # self._handle_maintype_subtype(). If there's no handler for the full
119 # MIME type, then dispatch to self._handle_maintype(). If that's
120 # missing too, then dispatch to self._writeBody().
121 ctype = msg.get_type()
122 if ctype is None:
123 # No Content-Type: header so try the default handler
124 self._writeBody(msg)
125 else:
126 # We do have a Content-Type: header.
127 specific = UNDERSCORE.join(ctype.split('/')).replace('-', '_')
128 meth = getattr(self, '_handle_' + specific, None)
129 if meth is None:
130 generic = msg.get_main_type().replace('-', '_')
131 meth = getattr(self, '_handle_' + generic, None)
132 if meth is None:
133 meth = self._writeBody
134 meth(msg)
135
136 #
137 # Default handlers
138 #
139
140 def _write_headers(self, msg):
141 for h, v in msg.items():
142 # We only write the MIME-Version: header for the outermost
143 # container message. Unfortunately, we can't use same technique
144 # as for the Unix-From above because we don't know when
145 # MIME-Version: will occur.
146 if h.lower() == 'mime-version' and not self.__first:
147 continue
148 # RFC 2822 says that lines SHOULD be no more than maxheaderlen
149 # characters wide, so we're well within our rights to split long
150 # headers.
151 text = '%s: %s' % (h, v)
152 if self.__maxheaderlen > 0 and len(text) > self.__maxheaderlen:
153 text = self._split_header(text)
154 print >> self._fp, text
155 # A blank line always separates headers from body
156 print >> self._fp
157
158 def _split_header(self, text):
159 maxheaderlen = self.__maxheaderlen
160 # Find out whether any lines in the header are really longer than
161 # maxheaderlen characters wide. There could be continuation lines
162 # that actually shorten it. Also, replace hard tabs with 8 spaces.
163 lines = [s.replace('\t', SPACE8) for s in text.split('\n')]
164 for line in lines:
165 if len(line) > maxheaderlen:
166 break
167 else:
168 # No line was actually longer than maxheaderlen characters, so
169 # just return the original unchanged.
170 return text
171 rtn = []
172 for line in text.split('\n'):
173 # Short lines can remain unchanged
174 if len(line.replace('\t', SPACE8)) <= maxheaderlen:
175 rtn.append(line)
Barry Warsawd1eeecb2001-10-17 20:51:42 +0000176 SEMINLTAB.join(rtn)
Barry Warsawba925802001-09-23 03:17:28 +0000177 else:
Barry Warsawd1eeecb2001-10-17 20:51:42 +0000178 oldlen = len(text)
Barry Warsawba925802001-09-23 03:17:28 +0000179 # Try to break the line on semicolons, but if that doesn't
Barry Warsawd1eeecb2001-10-17 20:51:42 +0000180 # work, try to split on folding whitespace.
Barry Warsawba925802001-09-23 03:17:28 +0000181 while len(text) > maxheaderlen:
182 i = text.rfind(';', 0, maxheaderlen)
183 if i < 0:
Barry Warsawba925802001-09-23 03:17:28 +0000184 break
185 rtn.append(text[:i])
186 text = text[i+1:].lstrip()
Barry Warsawd1eeecb2001-10-17 20:51:42 +0000187 if len(text) <> oldlen:
188 # Splitting on semis worked
189 rtn.append(text)
190 return SEMINLTAB.join(rtn)
191 # Splitting on semis didn't help, so try to split on
192 # whitespace.
193 parts = re.split(r'(\s+)', text)
194 # Watch out though for "Header: longnonsplittableline"
195 if parts[0].endswith(':') and len(parts) == 3:
196 return text
197 first = parts.pop(0)
198 sublines = [first]
199 acc = len(first)
200 while parts:
201 len0 = len(parts[0])
202 len1 = len(parts[1])
203 if acc + len0 + len1 < maxheaderlen:
204 sublines.append(parts.pop(0))
205 sublines.append(parts.pop(0))
206 acc += len0 + len1
207 else:
208 # Split it here, but don't forget to ignore the
209 # next whitespace-only part
210 rtn.append(EMPTYSTRING.join(sublines))
211 del parts[0]
212 first = parts.pop(0)
213 sublines = [first]
214 acc = len(first)
215 rtn.append(EMPTYSTRING.join(sublines))
216 return NLTAB.join(rtn)
Barry Warsawba925802001-09-23 03:17:28 +0000217
218 #
219 # Handlers for writing types and subtypes
220 #
221
222 def _handle_text(self, msg):
223 payload = msg.get_payload()
Barry Warsawb384e012001-09-26 05:32:41 +0000224 if payload is None:
225 return
Barry Warsawba925802001-09-23 03:17:28 +0000226 if not isinstance(payload, StringType):
Barry Warsawb384e012001-09-26 05:32:41 +0000227 raise TypeError, 'string payload expected: %s' % type(payload)
Barry Warsawba925802001-09-23 03:17:28 +0000228 if self._mangle_from_:
229 payload = fcre.sub('>From ', payload)
230 self._fp.write(payload)
231
232 # Default body handler
233 _writeBody = _handle_text
234
235 def _handle_multipart(self, msg, isdigest=0):
236 # The trick here is to write out each part separately, merge them all
237 # together, and then make sure that the boundary we've chosen isn't
238 # present in the payload.
239 msgtexts = []
240 for part in msg.get_payload():
241 s = StringIO()
Barry Warsawb384e012001-09-26 05:32:41 +0000242 g = self.__class__(s, self._mangle_from_, self.__maxheaderlen)
Barry Warsawba925802001-09-23 03:17:28 +0000243 g(part, unixfrom=0)
244 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
264 if isdigest:
265 print >> self._fp
266 # Join and write the individual parts
267 joiner = '\n--' + boundary + '\n'
268 if isdigest:
269 # multipart/digest types effectively add an extra newline between
270 # the boundary and the body part.
271 joiner += '\n'
272 self._fp.write(joiner.join(msgtexts))
273 print >> self._fp, '\n--' + boundary + '--',
274 # Write out any epilogue
275 if msg.epilogue is not None:
276 self._fp.write(msg.epilogue)
277
278 def _handle_multipart_digest(self, msg):
279 self._handle_multipart(msg, isdigest=1)
280
Barry Warsawb384e012001-09-26 05:32:41 +0000281 def _handle_message_delivery_status(self, msg):
282 # We can't just write the headers directly to self's file object
283 # because this will leave an extra newline between the last header
284 # block and the boundary. Sigh.
285 blocks = []
286 for part in msg.get_payload():
287 s = StringIO()
288 g = self.__class__(s, self._mangle_from_, self.__maxheaderlen)
289 g(part, unixfrom=0)
290 text = s.getvalue()
291 lines = text.split('\n')
292 # Strip off the unnecessary trailing empty line
293 if lines and lines[-1] == '':
294 blocks.append(NL.join(lines[:-1]))
295 else:
296 blocks.append(text)
297 # Now join all the blocks with an empty line. This has the lovely
298 # effect of separating each block with an empty line, but not adding
299 # an extra one after the last one.
300 self._fp.write(NL.join(blocks))
301
302 def _handle_message(self, msg):
Barry Warsawba925802001-09-23 03:17:28 +0000303 s = StringIO()
Barry Warsawb384e012001-09-26 05:32:41 +0000304 g = self.__class__(s, self._mangle_from_, self.__maxheaderlen)
Barry Warsawba925802001-09-23 03:17:28 +0000305 # A message/rfc822 should contain a scalar payload which is another
306 # Message object. Extract that object, stringify it, and write that
307 # out.
308 g(msg.get_payload(), unixfrom=0)
309 self._fp.write(s.getvalue())
310
311
Barry Warsawe968ead2001-10-04 17:05:11 +0000312
Barry Warsawba925802001-09-23 03:17:28 +0000313class DecodedGenerator(Generator):
314 """Generator a text representation of a message.
315
316 Like the Generator base class, except that non-text parts are substituted
317 with a format string representing the part.
318 """
319 def __init__(self, outfp, mangle_from_=1, maxheaderlen=78, fmt=None):
320 """Like Generator.__init__() except that an additional optional
321 argument is allowed.
322
323 Walks through all subparts of a message. If the subpart is of main
324 type `text', then it prints the decoded payload of the subpart.
325
326 Otherwise, fmt is a format string that is used instead of the message
327 payload. fmt is expanded with the following keywords (in
328 %(keyword)s format):
329
330 type : Full MIME type of the non-text part
331 maintype : Main MIME type of the non-text part
332 subtype : Sub-MIME type of the non-text part
333 filename : Filename of the non-text part
334 description: Description associated with the non-text part
335 encoding : Content transfer encoding of the non-text part
336
337 The default value for fmt is None, meaning
338
339 [Non-text (%(type)s) part of message omitted, filename %(filename)s]
340 """
341 Generator.__init__(self, outfp, mangle_from_, maxheaderlen)
342 if fmt is None:
343 fmt = ('[Non-text (%(type)s) part of message omitted, '
344 'filename %(filename)s]')
345 self._fmt = fmt
346
347 def _dispatch(self, msg):
348 for part in msg.walk():
Barry Warsawb384e012001-09-26 05:32:41 +0000349 maintype = part.get_main_type('text')
350 if maintype == 'text':
Barry Warsawba925802001-09-23 03:17:28 +0000351 print >> self, part.get_payload(decode=1)
Barry Warsawb384e012001-09-26 05:32:41 +0000352 elif maintype == 'multipart':
353 # Just skip this
354 pass
Barry Warsawba925802001-09-23 03:17:28 +0000355 else:
356 print >> self, self._fmt % {
357 'type' : part.get_type('[no MIME type]'),
358 'maintype' : part.get_main_type('[no main MIME type]'),
359 'subtype' : part.get_subtype('[no sub-MIME type]'),
360 'filename' : part.get_filename('[no filename]'),
361 'description': part.get('Content-Description',
362 '[no description]'),
363 'encoding' : part.get('Content-Transfer-Encoding',
364 '[no encoding]'),
365 }
366
367
Barry Warsawe968ead2001-10-04 17:05:11 +0000368
Barry Warsawba925802001-09-23 03:17:28 +0000369# Helper
370def _make_boundary(self, text=None):
371 # Craft a random boundary. If text is given, ensure that the chosen
372 # boundary doesn't appear in the text.
373 boundary = ('=' * 15) + repr(random.random()).split('.')[1] + '=='
374 if text is None:
375 return boundary
376 b = boundary
377 counter = 0
378 while 1:
379 cre = re.compile('^--' + re.escape(b) + '(--)?$', re.MULTILINE)
380 if not cre.search(text):
381 break
382 b = boundary + '.' + str(counter)
383 counter += 1
384 return b