ยปCore Development>Code coverage>Lib/mimify.py

Python code coverage for Lib/mimify.py

#countcontent
1n/a#! /usr/bin/env python
2n/a
3n/a"""Mimification and unmimification of mail messages.
4n/a
5n/aDecode quoted-printable parts of a mail message or encode using
6n/aquoted-printable.
7n/a
8n/aUsage:
9n/a mimify(input, output)
10n/a unmimify(input, output, decode_base64 = 0)
11n/ato encode and decode respectively. Input and output may be the name
12n/aof a file or an open file object. Only a readline() method is used
13n/aon the input file, only a write() method is used on the output file.
14n/aWhen using file names, the input and output file names may be the
15n/asame.
16n/a
17n/aInteractive usage:
18n/a mimify.py -e [infile [outfile]]
19n/a mimify.py -d [infile [outfile]]
20n/ato encode and decode respectively. Infile defaults to standard
21n/ainput and outfile to standard output.
221"""
23n/a
24n/a# Configure
251MAXLEN = 200 # if lines longer than this, encode as quoted-printable
261CHARSET = 'ISO-8859-1' # default charset for non-US-ASCII mail
271QUOTE = '> ' # string replies are quoted with
28n/a# End configure
29n/a
301import re
31n/a
321import warnings
331warnings.warn("the mimify module is deprecated; use the email package instead",
341 DeprecationWarning, 2)
35n/a
361__all__ = ["mimify","unmimify","mime_encode_header","mime_decode_header"]
37n/a
381qp = re.compile('^content-transfer-encoding:\\s*quoted-printable', re.I)
391base64_re = re.compile('^content-transfer-encoding:\\s*base64', re.I)
401mp = re.compile('^content-type:.*multipart/.*boundary="?([^;"\n]*)', re.I|re.S)
411chrset = re.compile('^(content-type:.*charset=")(us-ascii|iso-8859-[0-9]+)(".*)', re.I|re.S)
421he = re.compile('^-*\n')
431mime_code = re.compile('=([0-9a-f][0-9a-f])', re.I)
441mime_head = re.compile('=\\?iso-8859-1\\?q\\?([^? \t\n]+)\\?=', re.I)
451repl = re.compile('^subject:\\s+re: ', re.I)
46n/a
472class File:
48n/a """A simple fake file object that knows about limited read-ahead and
491 boundaries. The only supported method is readline()."""
50n/a
511 def __init__(self, file, boundary):
520 self.file = file
530 self.boundary = boundary
540 self.peek = None
55n/a
561 def readline(self):
570 if self.peek is not None:
580 return ''
590 line = self.file.readline()
600 if not line:
610 return line
620 if self.boundary:
630 if line == self.boundary + '\n':
640 self.peek = line
650 return ''
660 if line == self.boundary + '--\n':
670 self.peek = line
680 return ''
690 return line
70n/a
712class HeaderFile:
721 def __init__(self, file):
730 self.file = file
740 self.peek = None
75n/a
761 def readline(self):
770 if self.peek is not None:
780 line = self.peek
790 self.peek = None
80n/a else:
810 line = self.file.readline()
820 if not line:
830 return line
840 if he.match(line):
850 return line
860 while 1:
870 self.peek = self.file.readline()
880 if len(self.peek) == 0 or \
890 (self.peek[0] != ' ' and self.peek[0] != '\t'):
900 return line
910 line = line + self.peek
920 self.peek = None
93n/a
941def mime_decode(line):
95n/a """Decode a single line of quoted-printable text to 8bit."""
960 newline = ''
970 pos = 0
980 while 1:
990 res = mime_code.search(line, pos)
1000 if res is None:
1010 break
1020 newline = newline + line[pos:res.start(0)] + \
1030 chr(int(res.group(1), 16))
1040 pos = res.end(0)
1050 return newline + line[pos:]
106n/a
1071def mime_decode_header(line):
108n/a """Decode a header line to 8bit."""
1090 newline = ''
1100 pos = 0
1110 while 1:
1120 res = mime_head.search(line, pos)
1130 if res is None:
1140 break
1150 match = res.group(1)
116n/a # convert underscores to spaces (before =XX conversion!)
1170 match = ' '.join(match.split('_'))
1180 newline = newline + line[pos:res.start(0)] + mime_decode(match)
1190 pos = res.end(0)
1200 return newline + line[pos:]
121n/a
1221def unmimify_part(ifile, ofile, decode_base64 = 0):
123n/a """Convert a quoted-printable part of a MIME mail message to 8bit."""
1240 multipart = None
1250 quoted_printable = 0
1260 is_base64 = 0
1270 is_repl = 0
1280 if ifile.boundary and ifile.boundary[:2] == QUOTE:
1290 prefix = QUOTE
130n/a else:
1310 prefix = ''
132n/a
133n/a # read header
1340 hfile = HeaderFile(ifile)
1350 while 1:
1360 line = hfile.readline()
1370 if not line:
1380 return
1390 if prefix and line[:len(prefix)] == prefix:
1400 line = line[len(prefix):]
1410 pref = prefix
142n/a else:
1430 pref = ''
1440 line = mime_decode_header(line)
1450 if qp.match(line):
1460 quoted_printable = 1
1470 continue # skip this header
1480 if decode_base64 and base64_re.match(line):
1490 is_base64 = 1
1500 continue
1510 ofile.write(pref + line)
1520 if not prefix and repl.match(line):
153n/a # we're dealing with a reply message
1540 is_repl = 1
1550 mp_res = mp.match(line)
1560 if mp_res:
1570 multipart = '--' + mp_res.group(1)
1580 if he.match(line):
1590 break
1600 if is_repl and (quoted_printable or multipart):
1610 is_repl = 0
162n/a
163n/a # read body
1640 while 1:
1650 line = ifile.readline()
1660 if not line:
1670 return
1680 line = re.sub(mime_head, '\\1', line)
1690 if prefix and line[:len(prefix)] == prefix:
1700 line = line[len(prefix):]
1710 pref = prefix
172n/a else:
1730 pref = ''
174n/a## if is_repl and len(line) >= 4 and line[:4] == QUOTE+'--' and line[-3:] != '--\n':
175n/a## multipart = line[:-1]
1760 while multipart:
1770 if line == multipart + '--\n':
1780 ofile.write(pref + line)
1790 multipart = None
1800 line = None
1810 break
1820 if line == multipart + '\n':
1830 ofile.write(pref + line)
1840 nifile = File(ifile, multipart)
1850 unmimify_part(nifile, ofile, decode_base64)
1860 line = nifile.peek
1870 if not line:
188n/a # premature end of file
1890 break
1900 continue
191n/a # not a boundary between parts
1920 break
1930 if line and quoted_printable:
1940 while line[-2:] == '=\n':
1950 line = line[:-2]
1960 newline = ifile.readline()
1970 if newline[:len(QUOTE)] == QUOTE:
1980 newline = newline[len(QUOTE):]
1990 line = line + newline
2000 line = mime_decode(line)
2010 if line and is_base64 and not pref:
2020 import base64
2030 line = base64.decodestring(line)
2040 if line:
2050 ofile.write(pref + line)
206n/a
2071def unmimify(infile, outfile, decode_base64 = 0):
208n/a """Convert quoted-printable parts of a MIME mail message to 8bit."""
2090 if type(infile) == type(''):
2100 ifile = open(infile)
2110 if type(outfile) == type('') and infile == outfile:
2120 import os
2130 d, f = os.path.split(infile)
2140 os.rename(infile, os.path.join(d, ',' + f))
215n/a else:
2160 ifile = infile
2170 if type(outfile) == type(''):
2180 ofile = open(outfile, 'w')
219n/a else:
2200 ofile = outfile
2210 nifile = File(ifile, None)
2220 unmimify_part(nifile, ofile, decode_base64)
2230 ofile.flush()
224n/a
2251mime_char = re.compile('[=\177-\377]') # quote these chars in body
2261mime_header_char = re.compile('[=?\177-\377]') # quote these in header
227n/a
2281def mime_encode(line, header):
229n/a """Code a single line as quoted-printable.
230n/a If header is set, quote some extra characters."""
2310 if header:
2320 reg = mime_header_char
233n/a else:
2340 reg = mime_char
2350 newline = ''
2360 pos = 0
2370 if len(line) >= 5 and line[:5] == 'From ':
238n/a # quote 'From ' at the start of a line for stupid mailers
2390 newline = ('=%02x' % ord('F')).upper()
2400 pos = 1
2410 while 1:
2420 res = reg.search(line, pos)
2430 if res is None:
2440 break
2450 newline = newline + line[pos:res.start(0)] + \
2460 ('=%02x' % ord(res.group(0))).upper()
2470 pos = res.end(0)
2480 line = newline + line[pos:]
249n/a
2500 newline = ''
2510 while len(line) >= 75:
2520 i = 73
2530 while line[i] == '=' or line[i-1] == '=':
2540 i = i - 1
2550 i = i + 1
2560 newline = newline + line[:i] + '=\n'
2570 line = line[i:]
2580 return newline + line
259n/a
2601mime_header = re.compile('([ \t(]|^)([-a-zA-Z0-9_+]*[\177-\377][-a-zA-Z0-9_+\177-\377]*)(?=[ \t)]|\n)')
261n/a
2621def mime_encode_header(line):
263n/a """Code a single header line as quoted-printable."""
2640 newline = ''
2650 pos = 0
2660 while 1:
2670 res = mime_header.search(line, pos)
2680 if res is None:
2690 break
2700 newline = '%s%s%s=?%s?Q?%s?=' % \
2710 (newline, line[pos:res.start(0)], res.group(1),
2720 CHARSET, mime_encode(res.group(2), 1))
2730 pos = res.end(0)
2740 return newline + line[pos:]
275n/a
2761mv = re.compile('^mime-version:', re.I)
2771cte = re.compile('^content-transfer-encoding:', re.I)
2781iso_char = re.compile('[\177-\377]')
279n/a
2801def mimify_part(ifile, ofile, is_mime):
281n/a """Convert an 8bit part of a MIME mail message to quoted-printable."""
2820 has_cte = is_qp = is_base64 = 0
2830 multipart = None
2840 must_quote_body = must_quote_header = has_iso_chars = 0
285n/a
2860 header = []
2870 header_end = ''
2880 message = []
2890 message_end = ''
290n/a # read header
2910 hfile = HeaderFile(ifile)
2920 while 1:
2930 line = hfile.readline()
2940 if not line:
2950 break
2960 if not must_quote_header and iso_char.search(line):
2970 must_quote_header = 1
2980 if mv.match(line):
2990 is_mime = 1
3000 if cte.match(line):
3010 has_cte = 1
3020 if qp.match(line):
3030 is_qp = 1
3040 elif base64_re.match(line):
3050 is_base64 = 1
3060 mp_res = mp.match(line)
3070 if mp_res:
3080 multipart = '--' + mp_res.group(1)
3090 if he.match(line):
3100 header_end = line
3110 break
3120 header.append(line)
313n/a
314n/a # read body
3150 while 1:
3160 line = ifile.readline()
3170 if not line:
3180 break
3190 if multipart:
3200 if line == multipart + '--\n':
3210 message_end = line
3220 break
3230 if line == multipart + '\n':
3240 message_end = line
3250 break
3260 if is_base64:
3270 message.append(line)
3280 continue
3290 if is_qp:
3300 while line[-2:] == '=\n':
3310 line = line[:-2]
3320 newline = ifile.readline()
3330 if newline[:len(QUOTE)] == QUOTE:
3340 newline = newline[len(QUOTE):]
3350 line = line + newline
3360 line = mime_decode(line)
3370 message.append(line)
3380 if not has_iso_chars:
3390 if iso_char.search(line):
3400 has_iso_chars = must_quote_body = 1
3410 if not must_quote_body:
3420 if len(line) > MAXLEN:
3430 must_quote_body = 1
344n/a
345n/a # convert and output header and body
3460 for line in header:
3470 if must_quote_header:
3480 line = mime_encode_header(line)
3490 chrset_res = chrset.match(line)
3500 if chrset_res:
3510 if has_iso_chars:
352n/a # change us-ascii into iso-8859-1
3530 if chrset_res.group(2).lower() == 'us-ascii':
3540 line = '%s%s%s' % (chrset_res.group(1),
3550 CHARSET,
3560 chrset_res.group(3))
357n/a else:
358n/a # change iso-8859-* into us-ascii
3590 line = '%sus-ascii%s' % chrset_res.group(1, 3)
3600 if has_cte and cte.match(line):
3610 line = 'Content-Transfer-Encoding: '
3620 if is_base64:
3630 line = line + 'base64\n'
3640 elif must_quote_body:
3650 line = line + 'quoted-printable\n'
366n/a else:
3670 line = line + '7bit\n'
3680 ofile.write(line)
3690 if (must_quote_header or must_quote_body) and not is_mime:
3700 ofile.write('Mime-Version: 1.0\n')
3710 ofile.write('Content-Type: text/plain; ')
3720 if has_iso_chars:
3730 ofile.write('charset="%s"\n' % CHARSET)
374n/a else:
3750 ofile.write('charset="us-ascii"\n')
3760 if must_quote_body and not has_cte:
3770 ofile.write('Content-Transfer-Encoding: quoted-printable\n')
3780 ofile.write(header_end)
379n/a
3800 for line in message:
3810 if must_quote_body:
3820 line = mime_encode(line, 0)
3830 ofile.write(line)
3840 ofile.write(message_end)
385n/a
3860 line = message_end
3870 while multipart:
3880 if line == multipart + '--\n':
389n/a # read bit after the end of the last part
3900 while 1:
3910 line = ifile.readline()
3920 if not line:
3930 return
3940 if must_quote_body:
3950 line = mime_encode(line, 0)
3960 ofile.write(line)
3970 if line == multipart + '\n':
3980 nifile = File(ifile, multipart)
3990 mimify_part(nifile, ofile, 1)
4000 line = nifile.peek
4010 if not line:
402n/a # premature end of file
4030 break
4040 ofile.write(line)
4050 continue
406n/a # unexpectedly no multipart separator--copy rest of file
4070 while 1:
4080 line = ifile.readline()
4090 if not line:
4100 return
4110 if must_quote_body:
4120 line = mime_encode(line, 0)
4130 ofile.write(line)
414n/a
4151def mimify(infile, outfile):
416n/a """Convert 8bit parts of a MIME mail message to quoted-printable."""
4170 if type(infile) == type(''):
4180 ifile = open(infile)
4190 if type(outfile) == type('') and infile == outfile:
4200 import os
4210 d, f = os.path.split(infile)
4220 os.rename(infile, os.path.join(d, ',' + f))
423n/a else:
4240 ifile = infile
4250 if type(outfile) == type(''):
4260 ofile = open(outfile, 'w')
427n/a else:
4280 ofile = outfile
4290 nifile = File(ifile, None)
4300 mimify_part(nifile, ofile, 0)
4310 ofile.flush()
432n/a
4331import sys
4341if __name__ == '__main__' or (len(sys.argv) > 0 and sys.argv[0] == 'mimify'):
4350 import getopt
4360 usage = 'Usage: mimify [-l len] -[ed] [infile [outfile]]'
437n/a
4380 decode_base64 = 0
4390 opts, args = getopt.getopt(sys.argv[1:], 'l:edb')
4400 if len(args) not in (0, 1, 2):
4410 print usage
4420 sys.exit(1)
4430 if (('-e', '') in opts) == (('-d', '') in opts) or \
4440 ((('-b', '') in opts) and (('-d', '') not in opts)):
4450 print usage
4460 sys.exit(1)
4470 for o, a in opts:
4480 if o == '-e':
4490 encode = mimify
4500 elif o == '-d':
4510 encode = unmimify
4520 elif o == '-l':
4530 try:
4540 MAXLEN = int(a)
4550 except (ValueError, OverflowError):
4560 print usage
4570 sys.exit(1)
4580 elif o == '-b':
4590 decode_base64 = 1
4600 if len(args) == 0:
4610 encode_args = (sys.stdin, sys.stdout)
4620 elif len(args) == 1:
4630 encode_args = (args[0], sys.stdout)
464n/a else:
4650 encode_args = (args[0], args[1])
4660 if decode_base64:
4670 encode_args = encode_args + (decode_base64,)
4680 encode(*encode_args)