0001 #! /usr/bin/env python 0002 0003 """Mimification and unmimification of mail messages. 0004 0005 Decode quoted-printable parts of a mail message or encode using 0006 quoted-printable. 0007 0008 Usage: 0009 mimify(input, output) 0010 unmimify(input, output, decode_base64 = 0) 0011 to encode and decode respectively. Input and output may be the name 0012 of a file or an open file object. Only a readline() method is used 0013 on the input file, only a write() method is used on the output file. 0014 When using file names, the input and output file names may be the 0015 same. 0016 0017 Interactive usage: 0018 mimify.py -e [infile [outfile]] 0019 mimify.py -d [infile [outfile]] 0020 to encode and decode respectively. Infile defaults to standard 0021 input and outfile to standard output. 0022 """ 0023 0024 # Configure 0025 MAXLEN = 200 # if lines longer than this, encode as quoted-printable 0026 CHARSET = 'ISO-8859-1' # default charset for non-US-ASCII mail 0027 QUOTE = '> ' # string replies are quoted with 0028 # End configure 0029 0030 import re 0031 0032 __all__ = ["mimify","unmimify","mime_encode_header","mime_decode_header"] 0033 0034 qp = re.compile('^content-transfer-encoding:\\s*quoted-printable', re.I) 0035 base64_re = re.compile('^content-transfer-encoding:\\s*base64', re.I) 0036 mp = re.compile('^content-type:.*multipart/.*boundary="?([^;"\n]*)', re.I|re.S) 0037 chrset = re.compile('^(content-type:.*charset=")(us-ascii|iso-8859-[0-9]+)(".*)', re.I|re.S) 0038 he = re.compile('^-*\n') 0039 mime_code = re.compile('=([0-9a-f][0-9a-f])', re.I) 0040 mime_head = re.compile('=\\?iso-8859-1\\?q\\?([^? \t\n]+)\\?=', re.I) 0041 repl = re.compile('^subject:\\s+re: ', re.I) 0042 0043 class File: 0044 """A simple fake file object that knows about limited read-ahead and 0045 boundaries. The only supported method is readline().""" 0046 0047 def __init__(self, file, boundary): 0048 self.file = file 0049 self.boundary = boundary 0050 self.peek = None 0051 0052 def readline(self): 0053 if self.peek is not None: 0054 return '' 0055 line = self.file.readline() 0056 if not line: 0057 return line 0058 if self.boundary: 0059 if line == self.boundary + '\n': 0060 self.peek = line 0061 return '' 0062 if line == self.boundary + '--\n': 0063 self.peek = line 0064 return '' 0065 return line 0066 0067 class HeaderFile: 0068 def __init__(self, file): 0069 self.file = file 0070 self.peek = None 0071 0072 def readline(self): 0073 if self.peek is not None: 0074 line = self.peek 0075 self.peek = None 0076 else: 0077 line = self.file.readline() 0078 if not line: 0079 return line 0080 if he.match(line): 0081 return line 0082 while 1: 0083 self.peek = self.file.readline() 0084 if len(self.peek) == 0 or \ 0085 (self.peek[0] != ' ' and self.peek[0] != '\t'): 0086 return line 0087 line = line + self.peek 0088 self.peek = None 0089 0090 def mime_decode(line): 0091 """Decode a single line of quoted-printable text to 8bit.""" 0092 newline = '' 0093 pos = 0 0094 while 1: 0095 res = mime_code.search(line, pos) 0096 if res is None: 0097 break 0098 newline = newline + line[pos:res.start(0)] + \ 0099 chr(int(res.group(1), 16)) 0100 pos = res.end(0) 0101 return newline + line[pos:] 0102 0103 def mime_decode_header(line): 0104 """Decode a header line to 8bit.""" 0105 newline = '' 0106 pos = 0 0107 while 1: 0108 res = mime_head.search(line, pos) 0109 if res is None: 0110 break 0111 match = res.group(1) 0112 # convert underscores to spaces (before =XX conversion!) 0113 match = ' '.join(match.split('_')) 0114 newline = newline + line[pos:res.start(0)] + mime_decode(match) 0115 pos = res.end(0) 0116 return newline + line[pos:] 0117 0118 def unmimify_part(ifile, ofile, decode_base64 = 0): 0119 """Convert a quoted-printable part of a MIME mail message to 8bit.""" 0120 multipart = None 0121 quoted_printable = 0 0122 is_base64 = 0 0123 is_repl = 0 0124 if ifile.boundary and ifile.boundary[:2] == QUOTE: 0125 prefix = QUOTE 0126 else: 0127 prefix = '' 0128 0129 # read header 0130 hfile = HeaderFile(ifile) 0131 while 1: 0132 line = hfile.readline() 0133 if not line: 0134 return 0135 if prefix and line[:len(prefix)] == prefix: 0136 line = line[len(prefix):] 0137 pref = prefix 0138 else: 0139 pref = '' 0140 line = mime_decode_header(line) 0141 if qp.match(line): 0142 quoted_printable = 1 0143 continue # skip this header 0144 if decode_base64 and base64_re.match(line): 0145 is_base64 = 1 0146 continue 0147 ofile.write(pref + line) 0148 if not prefix and repl.match(line): 0149 # we're dealing with a reply message 0150 is_repl = 1 0151 mp_res = mp.match(line) 0152 if mp_res: 0153 multipart = '--' + mp_res.group(1) 0154 if he.match(line): 0155 break 0156 if is_repl and (quoted_printable or multipart): 0157 is_repl = 0 0158 0159 # read body 0160 while 1: 0161 line = ifile.readline() 0162 if not line: 0163 return 0164 line = re.sub(mime_head, '\\1', line) 0165 if prefix and line[:len(prefix)] == prefix: 0166 line = line[len(prefix):] 0167 pref = prefix 0168 else: 0169 pref = '' 0170 ## if is_repl and len(line) >= 4 and line[:4] == QUOTE+'--' and line[-3:] != '--\n': 0171 ## multipart = line[:-1] 0172 while multipart: 0173 if line == multipart + '--\n': 0174 ofile.write(pref + line) 0175 multipart = None 0176 line = None 0177 break 0178 if line == multipart + '\n': 0179 ofile.write(pref + line) 0180 nifile = File(ifile, multipart) 0181 unmimify_part(nifile, ofile, decode_base64) 0182 line = nifile.peek 0183 if not line: 0184 # premature end of file 0185 break 0186 continue 0187 # not a boundary between parts 0188 break 0189 if line and quoted_printable: 0190 while line[-2:] == '=\n': 0191 line = line[:-2] 0192 newline = ifile.readline() 0193 if newline[:len(QUOTE)] == QUOTE: 0194 newline = newline[len(QUOTE):] 0195 line = line + newline 0196 line = mime_decode(line) 0197 if line and is_base64 and not pref: 0198 import base64 0199 line = base64.decodestring(line) 0200 if line: 0201 ofile.write(pref + line) 0202 0203 def unmimify(infile, outfile, decode_base64 = 0): 0204 """Convert quoted-printable parts of a MIME mail message to 8bit.""" 0205 if type(infile) == type(''): 0206 ifile = open(infile) 0207 if type(outfile) == type('') and infile == outfile: 0208 import os 0209 d, f = os.path.split(infile) 0210 os.rename(infile, os.path.join(d, ',' + f)) 0211 else: 0212 ifile = infile 0213 if type(outfile) == type(''): 0214 ofile = open(outfile, 'w') 0215 else: 0216 ofile = outfile 0217 nifile = File(ifile, None) 0218 unmimify_part(nifile, ofile, decode_base64) 0219 ofile.flush() 0220 0221 mime_char = re.compile('[=\177-\377]') # quote these chars in body 0222 mime_header_char = re.compile('[=?\177-\377]') # quote these in header 0223 0224 def mime_encode(line, header): 0225 """Code a single line as quoted-printable. 0226 If header is set, quote some extra characters.""" 0227 if header: 0228 reg = mime_header_char 0229 else: 0230 reg = mime_char 0231 newline = '' 0232 pos = 0 0233 if len(line) >= 5 and line[:5] == 'From ': 0234 # quote 'From ' at the start of a line for stupid mailers 0235 newline = ('=%02x' % ord('F')).upper() 0236 pos = 1 0237 while 1: 0238 res = reg.search(line, pos) 0239 if res is None: 0240 break 0241 newline = newline + line[pos:res.start(0)] + \ 0242 ('=%02x' % ord(res.group(0))).upper() 0243 pos = res.end(0) 0244 line = newline + line[pos:] 0245 0246 newline = '' 0247 while len(line) >= 75: 0248 i = 73 0249 while line[i] == '=' or line[i-1] == '=': 0250 i = i - 1 0251 i = i + 1 0252 newline = newline + line[:i] + '=\n' 0253 line = line[i:] 0254 return newline + line 0255 0256 mime_header = re.compile('([ \t(]|^)([-a-zA-Z0-9_+]*[\177-\377][-a-zA-Z0-9_+\177-\377]*)(?=[ \t)]|\n)') 0257 0258 def mime_encode_header(line): 0259 """Code a single header line as quoted-printable.""" 0260 newline = '' 0261 pos = 0 0262 while 1: 0263 res = mime_header.search(line, pos) 0264 if res is None: 0265 break 0266 newline = '%s%s%s=?%s?Q?%s?=' % \ 0267 (newline, line[pos:res.start(0)], res.group(1), 0268 CHARSET, mime_encode(res.group(2), 1)) 0269 pos = res.end(0) 0270 return newline + line[pos:] 0271 0272 mv = re.compile('^mime-version:', re.I) 0273 cte = re.compile('^content-transfer-encoding:', re.I) 0274 iso_char = re.compile('[\177-\377]') 0275 0276 def mimify_part(ifile, ofile, is_mime): 0277 """Convert an 8bit part of a MIME mail message to quoted-printable.""" 0278 has_cte = is_qp = is_base64 = 0 0279 multipart = None 0280 must_quote_body = must_quote_header = has_iso_chars = 0 0281 0282 header = [] 0283 header_end = '' 0284 message = [] 0285 message_end = '' 0286 # read header 0287 hfile = HeaderFile(ifile) 0288 while 1: 0289 line = hfile.readline() 0290 if not line: 0291 break 0292 if not must_quote_header and iso_char.search(line): 0293 must_quote_header = 1 0294 if mv.match(line): 0295 is_mime = 1 0296 if cte.match(line): 0297 has_cte = 1 0298 if qp.match(line): 0299 is_qp = 1 0300 elif base64_re.match(line): 0301 is_base64 = 1 0302 mp_res = mp.match(line) 0303 if mp_res: 0304 multipart = '--' + mp_res.group(1) 0305 if he.match(line): 0306 header_end = line 0307 break 0308 header.append(line) 0309 0310 # read body 0311 while 1: 0312 line = ifile.readline() 0313 if not line: 0314 break 0315 if multipart: 0316 if line == multipart + '--\n': 0317 message_end = line 0318 break 0319 if line == multipart + '\n': 0320 message_end = line 0321 break 0322 if is_base64: 0323 message.append(line) 0324 continue 0325 if is_qp: 0326 while line[-2:] == '=\n': 0327 line = line[:-2] 0328 newline = ifile.readline() 0329 if newline[:len(QUOTE)] == QUOTE: 0330 newline = newline[len(QUOTE):] 0331 line = line + newline 0332 line = mime_decode(line) 0333 message.append(line) 0334 if not has_iso_chars: 0335 if iso_char.search(line): 0336 has_iso_chars = must_quote_body = 1 0337 if not must_quote_body: 0338 if len(line) > MAXLEN: 0339 must_quote_body = 1 0340 0341 # convert and output header and body 0342 for line in header: 0343 if must_quote_header: 0344 line = mime_encode_header(line) 0345 chrset_res = chrset.match(line) 0346 if chrset_res: 0347 if has_iso_chars: 0348 # change us-ascii into iso-8859-1 0349 if chrset_res.group(2).lower() == 'us-ascii': 0350 line = '%s%s%s' % (chrset_res.group(1), 0351 CHARSET, 0352 chrset_res.group(3)) 0353 else: 0354 # change iso-8859-* into us-ascii 0355 line = '%sus-ascii%s' % chrset_res.group(1, 3) 0356 if has_cte and cte.match(line): 0357 line = 'Content-Transfer-Encoding: ' 0358 if is_base64: 0359 line = line + 'base64\n' 0360 elif must_quote_body: 0361 line = line + 'quoted-printable\n' 0362 else: 0363 line = line + '7bit\n' 0364 ofile.write(line) 0365 if (must_quote_header or must_quote_body) and not is_mime: 0366 ofile.write('Mime-Version: 1.0\n') 0367 ofile.write('Content-Type: text/plain; ') 0368 if has_iso_chars: 0369 ofile.write('charset="%s"\n' % CHARSET) 0370 else: 0371 ofile.write('charset="us-ascii"\n') 0372 if must_quote_body and not has_cte: 0373 ofile.write('Content-Transfer-Encoding: quoted-printable\n') 0374 ofile.write(header_end) 0375 0376 for line in message: 0377 if must_quote_body: 0378 line = mime_encode(line, 0) 0379 ofile.write(line) 0380 ofile.write(message_end) 0381 0382 line = message_end 0383 while multipart: 0384 if line == multipart + '--\n': 0385 # read bit after the end of the last part 0386 while 1: 0387 line = ifile.readline() 0388 if not line: 0389 return 0390 if must_quote_body: 0391 line = mime_encode(line, 0) 0392 ofile.write(line) 0393 if line == multipart + '\n': 0394 nifile = File(ifile, multipart) 0395 mimify_part(nifile, ofile, 1) 0396 line = nifile.peek 0397 if not line: 0398 # premature end of file 0399 break 0400 ofile.write(line) 0401 continue 0402 # unexpectedly no multipart separator--copy rest of file 0403 while 1: 0404 line = ifile.readline() 0405 if not line: 0406 return 0407 if must_quote_body: 0408 line = mime_encode(line, 0) 0409 ofile.write(line) 0410 0411 def mimify(infile, outfile): 0412 """Convert 8bit parts of a MIME mail message to quoted-printable.""" 0413 if type(infile) == type(''): 0414 ifile = open(infile) 0415 if type(outfile) == type('') and infile == outfile: 0416 import os 0417 d, f = os.path.split(infile) 0418 os.rename(infile, os.path.join(d, ',' + f)) 0419 else: 0420 ifile = infile 0421 if type(outfile) == type(''): 0422 ofile = open(outfile, 'w') 0423 else: 0424 ofile = outfile 0425 nifile = File(ifile, None) 0426 mimify_part(nifile, ofile, 0) 0427 ofile.flush() 0428 0429 import sys 0430 if __name__ == '__main__' or (len(sys.argv) > 0 and sys.argv[0] == 'mimify'): 0431 import getopt 0432 usage = 'Usage: mimify [-l len] -[ed] [infile [outfile]]' 0433 0434 decode_base64 = 0 0435 opts, args = getopt.getopt(sys.argv[1:], 'l:edb') 0436 if len(args) not in (0, 1, 2): 0437 print usage 0438 sys.exit(1) 0439 if (('-e', '') in opts) == (('-d', '') in opts) or \ 0440 ((('-b', '') in opts) and (('-d', '') not in opts)): 0441 print usage 0442 sys.exit(1) 0443 for o, a in opts: 0444 if o == '-e': 0445 encode = mimify 0446 elif o == '-d': 0447 encode = unmimify 0448 elif o == '-l': 0449 try: 0450 MAXLEN = int(a) 0451 except (ValueError, OverflowError): 0452 print usage 0453 sys.exit(1) 0454 elif o == '-b': 0455 decode_base64 = 1 0456 if len(args) == 0: 0457 encode_args = (sys.stdin, sys.stdout) 0458 elif len(args) == 1: 0459 encode_args = (args[0], sys.stdout) 0460 else: 0461 encode_args = (args[0], args[1]) 0462 if decode_base64: 0463 encode_args = encode_args + (decode_base64,) 0464 encode(*encode_args) 0465
Generated by PyXR 0.9.4