]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | #! /usr/bin/env python\r |
2 | \r | |
3 | """Mimification and unmimification of mail messages.\r | |
4 | \r | |
5 | Decode quoted-printable parts of a mail message or encode using\r | |
6 | quoted-printable.\r | |
7 | \r | |
8 | Usage:\r | |
9 | mimify(input, output)\r | |
10 | unmimify(input, output, decode_base64 = 0)\r | |
11 | to encode and decode respectively. Input and output may be the name\r | |
12 | of a file or an open file object. Only a readline() method is used\r | |
13 | on the input file, only a write() method is used on the output file.\r | |
14 | When using file names, the input and output file names may be the\r | |
15 | same.\r | |
16 | \r | |
17 | Interactive usage:\r | |
18 | mimify.py -e [infile [outfile]]\r | |
19 | mimify.py -d [infile [outfile]]\r | |
20 | to encode and decode respectively. Infile defaults to standard\r | |
21 | input and outfile to standard output.\r | |
22 | """\r | |
23 | \r | |
24 | # Configure\r | |
25 | MAXLEN = 200 # if lines longer than this, encode as quoted-printable\r | |
26 | CHARSET = 'ISO-8859-1' # default charset for non-US-ASCII mail\r | |
27 | QUOTE = '> ' # string replies are quoted with\r | |
28 | # End configure\r | |
29 | \r | |
30 | import re\r | |
31 | \r | |
32 | import warnings\r | |
33 | warnings.warn("the mimify module is deprecated; use the email package instead",\r | |
34 | DeprecationWarning, 2)\r | |
35 | \r | |
36 | __all__ = ["mimify","unmimify","mime_encode_header","mime_decode_header"]\r | |
37 | \r | |
38 | qp = re.compile('^content-transfer-encoding:\\s*quoted-printable', re.I)\r | |
39 | base64_re = re.compile('^content-transfer-encoding:\\s*base64', re.I)\r | |
40 | mp = re.compile('^content-type:.*multipart/.*boundary="?([^;"\n]*)', re.I|re.S)\r | |
41 | chrset = re.compile('^(content-type:.*charset=")(us-ascii|iso-8859-[0-9]+)(".*)', re.I|re.S)\r | |
42 | he = re.compile('^-*\n')\r | |
43 | mime_code = re.compile('=([0-9a-f][0-9a-f])', re.I)\r | |
44 | mime_head = re.compile('=\\?iso-8859-1\\?q\\?([^? \t\n]+)\\?=', re.I)\r | |
45 | repl = re.compile('^subject:\\s+re: ', re.I)\r | |
46 | \r | |
47 | class File:\r | |
48 | """A simple fake file object that knows about limited read-ahead and\r | |
49 | boundaries. The only supported method is readline()."""\r | |
50 | \r | |
51 | def __init__(self, file, boundary):\r | |
52 | self.file = file\r | |
53 | self.boundary = boundary\r | |
54 | self.peek = None\r | |
55 | \r | |
56 | def readline(self):\r | |
57 | if self.peek is not None:\r | |
58 | return ''\r | |
59 | line = self.file.readline()\r | |
60 | if not line:\r | |
61 | return line\r | |
62 | if self.boundary:\r | |
63 | if line == self.boundary + '\n':\r | |
64 | self.peek = line\r | |
65 | return ''\r | |
66 | if line == self.boundary + '--\n':\r | |
67 | self.peek = line\r | |
68 | return ''\r | |
69 | return line\r | |
70 | \r | |
71 | class HeaderFile:\r | |
72 | def __init__(self, file):\r | |
73 | self.file = file\r | |
74 | self.peek = None\r | |
75 | \r | |
76 | def readline(self):\r | |
77 | if self.peek is not None:\r | |
78 | line = self.peek\r | |
79 | self.peek = None\r | |
80 | else:\r | |
81 | line = self.file.readline()\r | |
82 | if not line:\r | |
83 | return line\r | |
84 | if he.match(line):\r | |
85 | return line\r | |
86 | while 1:\r | |
87 | self.peek = self.file.readline()\r | |
88 | if len(self.peek) == 0 or \\r | |
89 | (self.peek[0] != ' ' and self.peek[0] != '\t'):\r | |
90 | return line\r | |
91 | line = line + self.peek\r | |
92 | self.peek = None\r | |
93 | \r | |
94 | def mime_decode(line):\r | |
95 | """Decode a single line of quoted-printable text to 8bit."""\r | |
96 | newline = ''\r | |
97 | pos = 0\r | |
98 | while 1:\r | |
99 | res = mime_code.search(line, pos)\r | |
100 | if res is None:\r | |
101 | break\r | |
102 | newline = newline + line[pos:res.start(0)] + \\r | |
103 | chr(int(res.group(1), 16))\r | |
104 | pos = res.end(0)\r | |
105 | return newline + line[pos:]\r | |
106 | \r | |
107 | def mime_decode_header(line):\r | |
108 | """Decode a header line to 8bit."""\r | |
109 | newline = ''\r | |
110 | pos = 0\r | |
111 | while 1:\r | |
112 | res = mime_head.search(line, pos)\r | |
113 | if res is None:\r | |
114 | break\r | |
115 | match = res.group(1)\r | |
116 | # convert underscores to spaces (before =XX conversion!)\r | |
117 | match = ' '.join(match.split('_'))\r | |
118 | newline = newline + line[pos:res.start(0)] + mime_decode(match)\r | |
119 | pos = res.end(0)\r | |
120 | return newline + line[pos:]\r | |
121 | \r | |
122 | def unmimify_part(ifile, ofile, decode_base64 = 0):\r | |
123 | """Convert a quoted-printable part of a MIME mail message to 8bit."""\r | |
124 | multipart = None\r | |
125 | quoted_printable = 0\r | |
126 | is_base64 = 0\r | |
127 | is_repl = 0\r | |
128 | if ifile.boundary and ifile.boundary[:2] == QUOTE:\r | |
129 | prefix = QUOTE\r | |
130 | else:\r | |
131 | prefix = ''\r | |
132 | \r | |
133 | # read header\r | |
134 | hfile = HeaderFile(ifile)\r | |
135 | while 1:\r | |
136 | line = hfile.readline()\r | |
137 | if not line:\r | |
138 | return\r | |
139 | if prefix and line[:len(prefix)] == prefix:\r | |
140 | line = line[len(prefix):]\r | |
141 | pref = prefix\r | |
142 | else:\r | |
143 | pref = ''\r | |
144 | line = mime_decode_header(line)\r | |
145 | if qp.match(line):\r | |
146 | quoted_printable = 1\r | |
147 | continue # skip this header\r | |
148 | if decode_base64 and base64_re.match(line):\r | |
149 | is_base64 = 1\r | |
150 | continue\r | |
151 | ofile.write(pref + line)\r | |
152 | if not prefix and repl.match(line):\r | |
153 | # we're dealing with a reply message\r | |
154 | is_repl = 1\r | |
155 | mp_res = mp.match(line)\r | |
156 | if mp_res:\r | |
157 | multipart = '--' + mp_res.group(1)\r | |
158 | if he.match(line):\r | |
159 | break\r | |
160 | if is_repl and (quoted_printable or multipart):\r | |
161 | is_repl = 0\r | |
162 | \r | |
163 | # read body\r | |
164 | while 1:\r | |
165 | line = ifile.readline()\r | |
166 | if not line:\r | |
167 | return\r | |
168 | line = re.sub(mime_head, '\\1', line)\r | |
169 | if prefix and line[:len(prefix)] == prefix:\r | |
170 | line = line[len(prefix):]\r | |
171 | pref = prefix\r | |
172 | else:\r | |
173 | pref = ''\r | |
174 | ## if is_repl and len(line) >= 4 and line[:4] == QUOTE+'--' and line[-3:] != '--\n':\r | |
175 | ## multipart = line[:-1]\r | |
176 | while multipart:\r | |
177 | if line == multipart + '--\n':\r | |
178 | ofile.write(pref + line)\r | |
179 | multipart = None\r | |
180 | line = None\r | |
181 | break\r | |
182 | if line == multipart + '\n':\r | |
183 | ofile.write(pref + line)\r | |
184 | nifile = File(ifile, multipart)\r | |
185 | unmimify_part(nifile, ofile, decode_base64)\r | |
186 | line = nifile.peek\r | |
187 | if not line:\r | |
188 | # premature end of file\r | |
189 | break\r | |
190 | continue\r | |
191 | # not a boundary between parts\r | |
192 | break\r | |
193 | if line and quoted_printable:\r | |
194 | while line[-2:] == '=\n':\r | |
195 | line = line[:-2]\r | |
196 | newline = ifile.readline()\r | |
197 | if newline[:len(QUOTE)] == QUOTE:\r | |
198 | newline = newline[len(QUOTE):]\r | |
199 | line = line + newline\r | |
200 | line = mime_decode(line)\r | |
201 | if line and is_base64 and not pref:\r | |
202 | import base64\r | |
203 | line = base64.decodestring(line)\r | |
204 | if line:\r | |
205 | ofile.write(pref + line)\r | |
206 | \r | |
207 | def unmimify(infile, outfile, decode_base64 = 0):\r | |
208 | """Convert quoted-printable parts of a MIME mail message to 8bit."""\r | |
209 | if type(infile) == type(''):\r | |
210 | ifile = open(infile)\r | |
211 | if type(outfile) == type('') and infile == outfile:\r | |
212 | import os\r | |
213 | d, f = os.path.split(infile)\r | |
214 | os.rename(infile, os.path.join(d, ',' + f))\r | |
215 | else:\r | |
216 | ifile = infile\r | |
217 | if type(outfile) == type(''):\r | |
218 | ofile = open(outfile, 'w')\r | |
219 | else:\r | |
220 | ofile = outfile\r | |
221 | nifile = File(ifile, None)\r | |
222 | unmimify_part(nifile, ofile, decode_base64)\r | |
223 | ofile.flush()\r | |
224 | \r | |
225 | mime_char = re.compile('[=\177-\377]') # quote these chars in body\r | |
226 | mime_header_char = re.compile('[=?\177-\377]') # quote these in header\r | |
227 | \r | |
228 | def mime_encode(line, header):\r | |
229 | """Code a single line as quoted-printable.\r | |
230 | If header is set, quote some extra characters."""\r | |
231 | if header:\r | |
232 | reg = mime_header_char\r | |
233 | else:\r | |
234 | reg = mime_char\r | |
235 | newline = ''\r | |
236 | pos = 0\r | |
237 | if len(line) >= 5 and line[:5] == 'From ':\r | |
238 | # quote 'From ' at the start of a line for stupid mailers\r | |
239 | newline = ('=%02x' % ord('F')).upper()\r | |
240 | pos = 1\r | |
241 | while 1:\r | |
242 | res = reg.search(line, pos)\r | |
243 | if res is None:\r | |
244 | break\r | |
245 | newline = newline + line[pos:res.start(0)] + \\r | |
246 | ('=%02x' % ord(res.group(0))).upper()\r | |
247 | pos = res.end(0)\r | |
248 | line = newline + line[pos:]\r | |
249 | \r | |
250 | newline = ''\r | |
251 | while len(line) >= 75:\r | |
252 | i = 73\r | |
253 | while line[i] == '=' or line[i-1] == '=':\r | |
254 | i = i - 1\r | |
255 | i = i + 1\r | |
256 | newline = newline + line[:i] + '=\n'\r | |
257 | line = line[i:]\r | |
258 | return newline + line\r | |
259 | \r | |
260 | mime_header = re.compile('([ \t(]|^)([-a-zA-Z0-9_+]*[\177-\377][-a-zA-Z0-9_+\177-\377]*)(?=[ \t)]|\n)')\r | |
261 | \r | |
262 | def mime_encode_header(line):\r | |
263 | """Code a single header line as quoted-printable."""\r | |
264 | newline = ''\r | |
265 | pos = 0\r | |
266 | while 1:\r | |
267 | res = mime_header.search(line, pos)\r | |
268 | if res is None:\r | |
269 | break\r | |
270 | newline = '%s%s%s=?%s?Q?%s?=' % \\r | |
271 | (newline, line[pos:res.start(0)], res.group(1),\r | |
272 | CHARSET, mime_encode(res.group(2), 1))\r | |
273 | pos = res.end(0)\r | |
274 | return newline + line[pos:]\r | |
275 | \r | |
276 | mv = re.compile('^mime-version:', re.I)\r | |
277 | cte = re.compile('^content-transfer-encoding:', re.I)\r | |
278 | iso_char = re.compile('[\177-\377]')\r | |
279 | \r | |
280 | def mimify_part(ifile, ofile, is_mime):\r | |
281 | """Convert an 8bit part of a MIME mail message to quoted-printable."""\r | |
282 | has_cte = is_qp = is_base64 = 0\r | |
283 | multipart = None\r | |
284 | must_quote_body = must_quote_header = has_iso_chars = 0\r | |
285 | \r | |
286 | header = []\r | |
287 | header_end = ''\r | |
288 | message = []\r | |
289 | message_end = ''\r | |
290 | # read header\r | |
291 | hfile = HeaderFile(ifile)\r | |
292 | while 1:\r | |
293 | line = hfile.readline()\r | |
294 | if not line:\r | |
295 | break\r | |
296 | if not must_quote_header and iso_char.search(line):\r | |
297 | must_quote_header = 1\r | |
298 | if mv.match(line):\r | |
299 | is_mime = 1\r | |
300 | if cte.match(line):\r | |
301 | has_cte = 1\r | |
302 | if qp.match(line):\r | |
303 | is_qp = 1\r | |
304 | elif base64_re.match(line):\r | |
305 | is_base64 = 1\r | |
306 | mp_res = mp.match(line)\r | |
307 | if mp_res:\r | |
308 | multipart = '--' + mp_res.group(1)\r | |
309 | if he.match(line):\r | |
310 | header_end = line\r | |
311 | break\r | |
312 | header.append(line)\r | |
313 | \r | |
314 | # read body\r | |
315 | while 1:\r | |
316 | line = ifile.readline()\r | |
317 | if not line:\r | |
318 | break\r | |
319 | if multipart:\r | |
320 | if line == multipart + '--\n':\r | |
321 | message_end = line\r | |
322 | break\r | |
323 | if line == multipart + '\n':\r | |
324 | message_end = line\r | |
325 | break\r | |
326 | if is_base64:\r | |
327 | message.append(line)\r | |
328 | continue\r | |
329 | if is_qp:\r | |
330 | while line[-2:] == '=\n':\r | |
331 | line = line[:-2]\r | |
332 | newline = ifile.readline()\r | |
333 | if newline[:len(QUOTE)] == QUOTE:\r | |
334 | newline = newline[len(QUOTE):]\r | |
335 | line = line + newline\r | |
336 | line = mime_decode(line)\r | |
337 | message.append(line)\r | |
338 | if not has_iso_chars:\r | |
339 | if iso_char.search(line):\r | |
340 | has_iso_chars = must_quote_body = 1\r | |
341 | if not must_quote_body:\r | |
342 | if len(line) > MAXLEN:\r | |
343 | must_quote_body = 1\r | |
344 | \r | |
345 | # convert and output header and body\r | |
346 | for line in header:\r | |
347 | if must_quote_header:\r | |
348 | line = mime_encode_header(line)\r | |
349 | chrset_res = chrset.match(line)\r | |
350 | if chrset_res:\r | |
351 | if has_iso_chars:\r | |
352 | # change us-ascii into iso-8859-1\r | |
353 | if chrset_res.group(2).lower() == 'us-ascii':\r | |
354 | line = '%s%s%s' % (chrset_res.group(1),\r | |
355 | CHARSET,\r | |
356 | chrset_res.group(3))\r | |
357 | else:\r | |
358 | # change iso-8859-* into us-ascii\r | |
359 | line = '%sus-ascii%s' % chrset_res.group(1, 3)\r | |
360 | if has_cte and cte.match(line):\r | |
361 | line = 'Content-Transfer-Encoding: '\r | |
362 | if is_base64:\r | |
363 | line = line + 'base64\n'\r | |
364 | elif must_quote_body:\r | |
365 | line = line + 'quoted-printable\n'\r | |
366 | else:\r | |
367 | line = line + '7bit\n'\r | |
368 | ofile.write(line)\r | |
369 | if (must_quote_header or must_quote_body) and not is_mime:\r | |
370 | ofile.write('Mime-Version: 1.0\n')\r | |
371 | ofile.write('Content-Type: text/plain; ')\r | |
372 | if has_iso_chars:\r | |
373 | ofile.write('charset="%s"\n' % CHARSET)\r | |
374 | else:\r | |
375 | ofile.write('charset="us-ascii"\n')\r | |
376 | if must_quote_body and not has_cte:\r | |
377 | ofile.write('Content-Transfer-Encoding: quoted-printable\n')\r | |
378 | ofile.write(header_end)\r | |
379 | \r | |
380 | for line in message:\r | |
381 | if must_quote_body:\r | |
382 | line = mime_encode(line, 0)\r | |
383 | ofile.write(line)\r | |
384 | ofile.write(message_end)\r | |
385 | \r | |
386 | line = message_end\r | |
387 | while multipart:\r | |
388 | if line == multipart + '--\n':\r | |
389 | # read bit after the end of the last part\r | |
390 | while 1:\r | |
391 | line = ifile.readline()\r | |
392 | if not line:\r | |
393 | return\r | |
394 | if must_quote_body:\r | |
395 | line = mime_encode(line, 0)\r | |
396 | ofile.write(line)\r | |
397 | if line == multipart + '\n':\r | |
398 | nifile = File(ifile, multipart)\r | |
399 | mimify_part(nifile, ofile, 1)\r | |
400 | line = nifile.peek\r | |
401 | if not line:\r | |
402 | # premature end of file\r | |
403 | break\r | |
404 | ofile.write(line)\r | |
405 | continue\r | |
406 | # unexpectedly no multipart separator--copy rest of file\r | |
407 | while 1:\r | |
408 | line = ifile.readline()\r | |
409 | if not line:\r | |
410 | return\r | |
411 | if must_quote_body:\r | |
412 | line = mime_encode(line, 0)\r | |
413 | ofile.write(line)\r | |
414 | \r | |
415 | def mimify(infile, outfile):\r | |
416 | """Convert 8bit parts of a MIME mail message to quoted-printable."""\r | |
417 | if type(infile) == type(''):\r | |
418 | ifile = open(infile)\r | |
419 | if type(outfile) == type('') and infile == outfile:\r | |
420 | import os\r | |
421 | d, f = os.path.split(infile)\r | |
422 | os.rename(infile, os.path.join(d, ',' + f))\r | |
423 | else:\r | |
424 | ifile = infile\r | |
425 | if type(outfile) == type(''):\r | |
426 | ofile = open(outfile, 'w')\r | |
427 | else:\r | |
428 | ofile = outfile\r | |
429 | nifile = File(ifile, None)\r | |
430 | mimify_part(nifile, ofile, 0)\r | |
431 | ofile.flush()\r | |
432 | \r | |
433 | import sys\r | |
434 | if __name__ == '__main__' or (len(sys.argv) > 0 and sys.argv[0] == 'mimify'):\r | |
435 | import getopt\r | |
436 | usage = 'Usage: mimify [-l len] -[ed] [infile [outfile]]'\r | |
437 | \r | |
438 | decode_base64 = 0\r | |
439 | opts, args = getopt.getopt(sys.argv[1:], 'l:edb')\r | |
440 | if len(args) not in (0, 1, 2):\r | |
441 | print usage\r | |
442 | sys.exit(1)\r | |
443 | if (('-e', '') in opts) == (('-d', '') in opts) or \\r | |
444 | ((('-b', '') in opts) and (('-d', '') not in opts)):\r | |
445 | print usage\r | |
446 | sys.exit(1)\r | |
447 | for o, a in opts:\r | |
448 | if o == '-e':\r | |
449 | encode = mimify\r | |
450 | elif o == '-d':\r | |
451 | encode = unmimify\r | |
452 | elif o == '-l':\r | |
453 | try:\r | |
454 | MAXLEN = int(a)\r | |
455 | except (ValueError, OverflowError):\r | |
456 | print usage\r | |
457 | sys.exit(1)\r | |
458 | elif o == '-b':\r | |
459 | decode_base64 = 1\r | |
460 | if len(args) == 0:\r | |
461 | encode_args = (sys.stdin, sys.stdout)\r | |
462 | elif len(args) == 1:\r | |
463 | encode_args = (args[0], sys.stdout)\r | |
464 | else:\r | |
465 | encode_args = (args[0], args[1])\r | |
466 | if decode_base64:\r | |
467 | encode_args = encode_args + (decode_base64,)\r | |
468 | encode(*encode_args)\r |