blob: fb79db7629ce53cb11c166fca243b40864aec044 [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +00002
3'''Mimification and unmimification of mail messages.
4
5decode quoted-printable parts of a mail message or encode using
6quoted-printable.
7
8Usage:
9 mimify(input, output)
Guido van Rossum74d25e71997-07-30 22:02:28 +000010 unmimify(input, output, decode_base64 = 0)
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +000011to encode and decode respectively. Input and output may be the name
12of a file or an open file object. Only a readline() method is used
13on the input file, only a write() method is used on the output file.
14When using file names, the input and output file names may be the
15same.
16
17Interactive usage:
18 mimify.py -e [infile [outfile]]
19 mimify.py -d [infile [outfile]]
20to encode and decode respectively. Infile defaults to standard
21input and outfile to standard output.
22'''
23
24# Configure
25MAXLEN = 200 # if lines longer than this, encode as quoted-printable
26CHARSET = 'ISO-8859-1' # default charset for non-US-ASCII mail
27QUOTE = '> ' # string replies are quoted with
28# End configure
29
Guido van Rossum31626bc1997-10-24 14:46:16 +000030import re, string
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +000031
Guido van Rossum31626bc1997-10-24 14:46:16 +000032qp = re.compile('^content-transfer-encoding:\\s*quoted-printable', re.I)
33base64_re = re.compile('^content-transfer-encoding:\\s*base64', re.I)
34mp = re.compile('^content-type:.*multipart/.*boundary="?([^;"\n]*)', re.I|re.S)
35chrset = re.compile('^(content-type:.*charset=")(us-ascii|iso-8859-[0-9]+)(".*)', re.I|re.S)
36he = re.compile('^-*\n')
37mime_code = re.compile('=([0-9a-f][0-9a-f])', re.I)
38mime_head = re.compile('=\\?iso-8859-1\\?q\\?([^? \t\n]+)\\?=', re.I)
39repl = re.compile('^subject:\\s+re: ', re.I)
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +000040
41class File:
42 '''A simple fake file object that knows about limited
43 read-ahead and boundaries.
44 The only supported method is readline().'''
45
46 def __init__(self, file, boundary):
47 self.file = file
48 self.boundary = boundary
49 self.peek = None
50
51 def readline(self):
52 if self.peek is not None:
53 return ''
54 line = self.file.readline()
55 if not line:
56 return line
57 if self.boundary:
58 if line == self.boundary + '\n':
59 self.peek = line
60 return ''
61 if line == self.boundary + '--\n':
62 self.peek = line
63 return ''
64 return line
65
66class HeaderFile:
67 def __init__(self, file):
68 self.file = file
69 self.peek = None
70
71 def readline(self):
72 if self.peek is not None:
73 line = self.peek
74 self.peek = None
75 else:
76 line = self.file.readline()
77 if not line:
78 return line
Guido van Rossum31626bc1997-10-24 14:46:16 +000079 if he.match(line):
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +000080 return line
81 while 1:
82 self.peek = self.file.readline()
83 if len(self.peek) == 0 or \
84 (self.peek[0] != ' ' and self.peek[0] != '\t'):
85 return line
86 line = line + self.peek
87 self.peek = None
88
89def mime_decode(line):
90 '''Decode a single line of quoted-printable text to 8bit.'''
91 newline = ''
Guido van Rossum613418a1997-10-30 15:27:37 +000092 pos = 0
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +000093 while 1:
Guido van Rossum613418a1997-10-30 15:27:37 +000094 res = mime_code.search(line, pos)
Guido van Rossum31626bc1997-10-24 14:46:16 +000095 if res is None:
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +000096 break
Guido van Rossum613418a1997-10-30 15:27:37 +000097 newline = newline + line[pos:res.start(0)] + \
Guido van Rossum31626bc1997-10-24 14:46:16 +000098 chr(string.atoi(res.group(1), 16))
Guido van Rossum613418a1997-10-30 15:27:37 +000099 pos = res.end(0)
100 return newline + line[pos:]
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000101
102def mime_decode_header(line):
103 '''Decode a header line to 8bit.'''
104 newline = ''
Guido van Rossum613418a1997-10-30 15:27:37 +0000105 pos = 0
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000106 while 1:
Guido van Rossum613418a1997-10-30 15:27:37 +0000107 res = mime_head.search(line, pos)
Guido van Rossum31626bc1997-10-24 14:46:16 +0000108 if res is None:
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000109 break
Guido van Rossum31626bc1997-10-24 14:46:16 +0000110 match = res.group(1)
Guido van Rossum88bb8081997-08-14 14:10:37 +0000111 # convert underscores to spaces (before =XX conversion!)
Guido van Rossum31626bc1997-10-24 14:46:16 +0000112 match = string.join(string.split(match, '_'), ' ')
Guido van Rossum613418a1997-10-30 15:27:37 +0000113 newline = newline + line[pos:res.start(0)] + mime_decode(match)
114 pos = res.end(0)
115 return newline + line[pos:]
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000116
Guido van Rossuma3d9e021997-04-11 15:22:56 +0000117def unmimify_part(ifile, ofile, decode_base64 = 0):
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000118 '''Convert a quoted-printable part of a MIME mail message to 8bit.'''
119 multipart = None
120 quoted_printable = 0
Guido van Rossuma3d9e021997-04-11 15:22:56 +0000121 is_base64 = 0
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000122 is_repl = 0
123 if ifile.boundary and ifile.boundary[:2] == QUOTE:
124 prefix = QUOTE
125 else:
126 prefix = ''
127
128 # read header
129 hfile = HeaderFile(ifile)
130 while 1:
131 line = hfile.readline()
132 if not line:
133 return
134 if prefix and line[:len(prefix)] == prefix:
135 line = line[len(prefix):]
136 pref = prefix
137 else:
138 pref = ''
139 line = mime_decode_header(line)
Guido van Rossum31626bc1997-10-24 14:46:16 +0000140 if qp.match(line):
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000141 quoted_printable = 1
142 continue # skip this header
Guido van Rossum31626bc1997-10-24 14:46:16 +0000143 if decode_base64 and base64_re.match(line):
Guido van Rossuma3d9e021997-04-11 15:22:56 +0000144 is_base64 = 1
145 continue
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000146 ofile.write(pref + line)
Guido van Rossum31626bc1997-10-24 14:46:16 +0000147 if not prefix and repl.match(line):
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000148 # we're dealing with a reply message
149 is_repl = 1
Guido van Rossum31626bc1997-10-24 14:46:16 +0000150 mp_res = mp.match(line)
151 if mp_res:
152 multipart = '--' + mp_res.group(1)
153 if he.match(line):
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000154 break
155 if is_repl and (quoted_printable or multipart):
156 is_repl = 0
157
158 # read body
159 while 1:
160 line = ifile.readline()
161 if not line:
162 return
Guido van Rossum31626bc1997-10-24 14:46:16 +0000163 line = re.sub(mime_head, '\\1', line)
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000164 if prefix and line[:len(prefix)] == prefix:
165 line = line[len(prefix):]
166 pref = prefix
167 else:
168 pref = ''
169## if is_repl and len(line) >= 4 and line[:4] == QUOTE+'--' and line[-3:] != '--\n':
170## multipart = line[:-1]
171 while multipart:
172 if line == multipart + '--\n':
173 ofile.write(pref + line)
174 multipart = None
175 line = None
176 break
177 if line == multipart + '\n':
178 ofile.write(pref + line)
179 nifile = File(ifile, multipart)
Guido van Rossuma3d9e021997-04-11 15:22:56 +0000180 unmimify_part(nifile, ofile, decode_base64)
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000181 line = nifile.peek
182 continue
183 # not a boundary between parts
184 break
185 if line and quoted_printable:
186 while line[-2:] == '=\n':
187 line = line[:-2]
188 newline = ifile.readline()
189 if newline[:len(QUOTE)] == QUOTE:
190 newline = newline[len(QUOTE):]
191 line = line + newline
192 line = mime_decode(line)
Guido van Rossuma3d9e021997-04-11 15:22:56 +0000193 if line and is_base64 and not pref:
194 import base64
195 line = base64.decodestring(line)
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000196 if line:
197 ofile.write(pref + line)
198
Guido van Rossuma3d9e021997-04-11 15:22:56 +0000199def unmimify(infile, outfile, decode_base64 = 0):
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000200 '''Convert quoted-printable parts of a MIME mail message to 8bit.'''
201 if type(infile) == type(''):
202 ifile = open(infile)
203 if type(outfile) == type('') and infile == outfile:
204 import os
205 d, f = os.path.split(infile)
206 os.rename(infile, os.path.join(d, ',' + f))
207 else:
208 ifile = infile
209 if type(outfile) == type(''):
210 ofile = open(outfile, 'w')
211 else:
212 ofile = outfile
213 nifile = File(ifile, None)
Guido van Rossuma3d9e021997-04-11 15:22:56 +0000214 unmimify_part(nifile, ofile, decode_base64)
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000215 ofile.flush()
216
Guido van Rossum11fbef51997-12-02 17:45:39 +0000217mime_char = re.compile('[=\177-\377]') # quote these chars in body
218mime_header_char = re.compile('[=?\177-\377]') # quote these in header
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000219
220def mime_encode(line, header):
221 '''Code a single line as quoted-printable.
222 If header is set, quote some extra characters.'''
223 if header:
224 reg = mime_header_char
225 else:
226 reg = mime_char
227 newline = ''
Guido van Rossum613418a1997-10-30 15:27:37 +0000228 pos = 0
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000229 if len(line) >= 5 and line[:5] == 'From ':
230 # quote 'From ' at the start of a line for stupid mailers
231 newline = string.upper('=%02x' % ord('F'))
Guido van Rossum613418a1997-10-30 15:27:37 +0000232 pos = 1
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000233 while 1:
Guido van Rossum613418a1997-10-30 15:27:37 +0000234 res = reg.search(line, pos)
Guido van Rossum31626bc1997-10-24 14:46:16 +0000235 if res is None:
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000236 break
Guido van Rossum613418a1997-10-30 15:27:37 +0000237 newline = newline + line[pos:res.start(0)] + \
238 string.upper('=%02x' % ord(res.group(0)))
239 pos = res.end(0)
240 line = newline + line[pos:]
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000241
242 newline = ''
243 while len(line) >= 75:
244 i = 73
245 while line[i] == '=' or line[i-1] == '=':
246 i = i - 1
247 i = i + 1
248 newline = newline + line[:i] + '=\n'
249 line = line[i:]
250 return newline + line
251
Guido van Rossum11fbef51997-12-02 17:45:39 +0000252mime_header = re.compile('([ \t(]|^)([-a-zA-Z0-9_+]*[\177-\377][-a-zA-Z0-9_+\177-\377]*)([ \t)]|\n)')
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000253
254def mime_encode_header(line):
255 '''Code a single header line as quoted-printable.'''
256 newline = ''
Guido van Rossum613418a1997-10-30 15:27:37 +0000257 pos = 0
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000258 while 1:
Guido van Rossum613418a1997-10-30 15:27:37 +0000259 res = mime_header.search(line, pos)
Guido van Rossum31626bc1997-10-24 14:46:16 +0000260 if res is None:
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000261 break
Guido van Rossum613418a1997-10-30 15:27:37 +0000262 newline = '%s%s%s=?%s?Q?%s?=%s' % \
263 (newline, line[pos:res.start(0)], res.group(1),
264 CHARSET, mime_encode(res.group(2), 1), res.group(3))
265 pos = res.end(0)
266 return newline + line[pos:]
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000267
Guido van Rossum31626bc1997-10-24 14:46:16 +0000268mv = re.compile('^mime-version:', re.I)
269cte = re.compile('^content-transfer-encoding:', re.I)
Guido van Rossum11fbef51997-12-02 17:45:39 +0000270iso_char = re.compile('[\177-\377]')
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000271
272def mimify_part(ifile, ofile, is_mime):
273 '''Convert an 8bit part of a MIME mail message to quoted-printable.'''
Guido van Rossum69155681996-06-10 19:04:02 +0000274 has_cte = is_qp = is_base64 = 0
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000275 multipart = None
276 must_quote_body = must_quote_header = has_iso_chars = 0
277
278 header = []
279 header_end = ''
280 message = []
281 message_end = ''
282 # read header
283 hfile = HeaderFile(ifile)
284 while 1:
285 line = hfile.readline()
286 if not line:
287 break
Guido van Rossum31626bc1997-10-24 14:46:16 +0000288 if not must_quote_header and iso_char.search(line):
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000289 must_quote_header = 1
Guido van Rossum31626bc1997-10-24 14:46:16 +0000290 if mv.match(line):
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000291 is_mime = 1
Guido van Rossum31626bc1997-10-24 14:46:16 +0000292 if cte.match(line):
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000293 has_cte = 1
Guido van Rossum31626bc1997-10-24 14:46:16 +0000294 if qp.match(line):
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000295 is_qp = 1
Guido van Rossum31626bc1997-10-24 14:46:16 +0000296 elif base64_re.match(line):
Guido van Rossum69155681996-06-10 19:04:02 +0000297 is_base64 = 1
Guido van Rossum31626bc1997-10-24 14:46:16 +0000298 mp_res = mp.match(line)
299 if mp_res:
300 multipart = '--' + mp_res.group(1)
301 if he.match(line):
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000302 header_end = line
303 break
304 header.append(line)
305
306 # read body
307 while 1:
308 line = ifile.readline()
309 if not line:
310 break
311 if multipart:
312 if line == multipart + '--\n':
313 message_end = line
314 break
315 if line == multipart + '\n':
316 message_end = line
317 break
Guido van Rossum69155681996-06-10 19:04:02 +0000318 if is_base64:
319 message.append(line)
320 continue
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000321 if is_qp:
322 while line[-2:] == '=\n':
323 line = line[:-2]
324 newline = ifile.readline()
325 if newline[:len(QUOTE)] == QUOTE:
326 newline = newline[len(QUOTE):]
327 line = line + newline
328 line = mime_decode(line)
329 message.append(line)
330 if not has_iso_chars:
Guido van Rossum31626bc1997-10-24 14:46:16 +0000331 if iso_char.search(line):
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000332 has_iso_chars = must_quote_body = 1
333 if not must_quote_body:
334 if len(line) > MAXLEN:
335 must_quote_body = 1
336
337 # convert and output header and body
338 for line in header:
339 if must_quote_header:
340 line = mime_encode_header(line)
Guido van Rossum31626bc1997-10-24 14:46:16 +0000341 chrset_res = chrset.match(line)
342 if chrset_res:
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000343 if has_iso_chars:
344 # change us-ascii into iso-8859-1
Guido van Rossum31626bc1997-10-24 14:46:16 +0000345 if string.lower(chrset_res.group(2)) == 'us-ascii':
Guido van Rossum613418a1997-10-30 15:27:37 +0000346 line = '%s%s%s' % (chrset_res.group(1),
347 CHARSET,
348 chrset_res.group(3))
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000349 else:
350 # change iso-8859-* into us-ascii
Guido van Rossum613418a1997-10-30 15:27:37 +0000351 line = '%sus-ascii%s' % chrset_res.group(1, 3)
Guido van Rossum31626bc1997-10-24 14:46:16 +0000352 if has_cte and cte.match(line):
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000353 line = 'Content-Transfer-Encoding: '
Guido van Rossum69155681996-06-10 19:04:02 +0000354 if is_base64:
355 line = line + 'base64\n'
356 elif must_quote_body:
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000357 line = line + 'quoted-printable\n'
358 else:
359 line = line + '7bit\n'
360 ofile.write(line)
361 if (must_quote_header or must_quote_body) and not is_mime:
362 ofile.write('Mime-Version: 1.0\n')
363 ofile.write('Content-Type: text/plain; ')
364 if has_iso_chars:
365 ofile.write('charset="%s"\n' % CHARSET)
366 else:
367 ofile.write('charset="us-ascii"\n')
368 if must_quote_body and not has_cte:
369 ofile.write('Content-Transfer-Encoding: quoted-printable\n')
370 ofile.write(header_end)
371
372 for line in message:
373 if must_quote_body:
374 line = mime_encode(line, 0)
375 ofile.write(line)
376 ofile.write(message_end)
377
378 line = message_end
379 while multipart:
380 if line == multipart + '--\n':
Guido van Rossumf789ee41997-03-20 14:42:17 +0000381 # read bit after the end of the last part
382 while 1:
383 line = ifile.readline()
384 if not line:
385 return
386 if must_quote_body:
387 line = mime_encode(line, 0)
388 ofile.write(line)
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000389 if line == multipart + '\n':
390 nifile = File(ifile, multipart)
391 mimify_part(nifile, ofile, 1)
392 line = nifile.peek
393 ofile.write(line)
394 continue
395
396def mimify(infile, outfile):
397 '''Convert 8bit parts of a MIME mail message to quoted-printable.'''
398 if type(infile) == type(''):
399 ifile = open(infile)
400 if type(outfile) == type('') and infile == outfile:
401 import os
402 d, f = os.path.split(infile)
403 os.rename(infile, os.path.join(d, ',' + f))
404 else:
405 ifile = infile
406 if type(outfile) == type(''):
407 ofile = open(outfile, 'w')
408 else:
409 ofile = outfile
410 nifile = File(ifile, None)
411 mimify_part(nifile, ofile, 0)
412 ofile.flush()
413
414import sys
415if __name__ == '__main__' or (len(sys.argv) > 0 and sys.argv[0] == 'mimify'):
416 import getopt
417 usage = 'Usage: mimify [-l len] -[ed] [infile [outfile]]'
418
Guido van Rossuma3d9e021997-04-11 15:22:56 +0000419 decode_base64 = 0
420 opts, args = getopt.getopt(sys.argv[1:], 'l:edb')
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000421 if len(args) not in (0, 1, 2):
422 print usage
423 sys.exit(1)
Guido van Rossuma3d9e021997-04-11 15:22:56 +0000424 if (('-e', '') in opts) == (('-d', '') in opts) or \
425 ((('-b', '') in opts) and (('-d', '') not in opts)):
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000426 print usage
427 sys.exit(1)
428 for o, a in opts:
429 if o == '-e':
430 encode = mimify
431 elif o == '-d':
432 encode = unmimify
433 elif o == '-l':
434 try:
435 MAXLEN = string.atoi(a)
436 except:
437 print usage
438 sys.exit(1)
Guido van Rossuma3d9e021997-04-11 15:22:56 +0000439 elif o == '-b':
440 decode_base64 = 1
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000441 if len(args) == 0:
Guido van Rossuma3d9e021997-04-11 15:22:56 +0000442 encode_args = (sys.stdin, sys.stdout)
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000443 elif len(args) == 1:
Guido van Rossuma3d9e021997-04-11 15:22:56 +0000444 encode_args = (args[0], sys.stdout)
Sjoerd Mullendere8a0a5c1996-02-14 10:40:03 +0000445 else:
Guido van Rossuma3d9e021997-04-11 15:22:56 +0000446 encode_args = (args[0], args[1])
447 if decode_base64:
448 encode_args = encode_args + (decode_base64,)
449 apply(encode, encode_args)
Guido van Rossum31626bc1997-10-24 14:46:16 +0000450