Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 2 | |
| 3 | '''Mimification and unmimification of mail messages. |
| 4 | |
| 5 | decode quoted-printable parts of a mail message or encode using |
| 6 | quoted-printable. |
| 7 | |
| 8 | Usage: |
| 9 | mimify(input, output) |
Guido van Rossum | 74d25e7 | 1997-07-30 22:02:28 +0000 | [diff] [blame] | 10 | unmimify(input, output, decode_base64 = 0) |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 11 | to encode and decode respectively. Input and output may be the name |
| 12 | of a file or an open file object. Only a readline() method is used |
| 13 | on the input file, only a write() method is used on the output file. |
| 14 | When using file names, the input and output file names may be the |
| 15 | same. |
| 16 | |
| 17 | Interactive usage: |
| 18 | mimify.py -e [infile [outfile]] |
| 19 | mimify.py -d [infile [outfile]] |
| 20 | to encode and decode respectively. Infile defaults to standard |
| 21 | input and outfile to standard output. |
| 22 | ''' |
| 23 | |
| 24 | # Configure |
| 25 | MAXLEN = 200 # if lines longer than this, encode as quoted-printable |
| 26 | CHARSET = 'ISO-8859-1' # default charset for non-US-ASCII mail |
| 27 | QUOTE = '> ' # string replies are quoted with |
| 28 | # End configure |
| 29 | |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame] | 30 | import re, string |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 31 | |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame] | 32 | qp = re.compile('^content-transfer-encoding:\\s*quoted-printable', re.I) |
| 33 | base64_re = re.compile('^content-transfer-encoding:\\s*base64', re.I) |
| 34 | mp = re.compile('^content-type:.*multipart/.*boundary="?([^;"\n]*)', re.I|re.S) |
| 35 | chrset = re.compile('^(content-type:.*charset=")(us-ascii|iso-8859-[0-9]+)(".*)', re.I|re.S) |
| 36 | he = re.compile('^-*\n') |
| 37 | mime_code = re.compile('=([0-9a-f][0-9a-f])', re.I) |
| 38 | mime_head = re.compile('=\\?iso-8859-1\\?q\\?([^? \t\n]+)\\?=', re.I) |
| 39 | repl = re.compile('^subject:\\s+re: ', re.I) |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 40 | |
| 41 | class 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 | |
| 66 | class 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 Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame] | 79 | if he.match(line): |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 80 | 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 | |
| 89 | def mime_decode(line): |
| 90 | '''Decode a single line of quoted-printable text to 8bit.''' |
| 91 | newline = '' |
Guido van Rossum | 613418a | 1997-10-30 15:27:37 +0000 | [diff] [blame] | 92 | pos = 0 |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 93 | while 1: |
Guido van Rossum | 613418a | 1997-10-30 15:27:37 +0000 | [diff] [blame] | 94 | res = mime_code.search(line, pos) |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame] | 95 | if res is None: |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 96 | break |
Guido van Rossum | 613418a | 1997-10-30 15:27:37 +0000 | [diff] [blame] | 97 | newline = newline + line[pos:res.start(0)] + \ |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame] | 98 | chr(string.atoi(res.group(1), 16)) |
Guido van Rossum | 613418a | 1997-10-30 15:27:37 +0000 | [diff] [blame] | 99 | pos = res.end(0) |
| 100 | return newline + line[pos:] |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 101 | |
| 102 | def mime_decode_header(line): |
| 103 | '''Decode a header line to 8bit.''' |
| 104 | newline = '' |
Guido van Rossum | 613418a | 1997-10-30 15:27:37 +0000 | [diff] [blame] | 105 | pos = 0 |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 106 | while 1: |
Guido van Rossum | 613418a | 1997-10-30 15:27:37 +0000 | [diff] [blame] | 107 | res = mime_head.search(line, pos) |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame] | 108 | if res is None: |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 109 | break |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame] | 110 | match = res.group(1) |
Guido van Rossum | 88bb808 | 1997-08-14 14:10:37 +0000 | [diff] [blame] | 111 | # convert underscores to spaces (before =XX conversion!) |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame] | 112 | match = string.join(string.split(match, '_'), ' ') |
Guido van Rossum | 613418a | 1997-10-30 15:27:37 +0000 | [diff] [blame] | 113 | newline = newline + line[pos:res.start(0)] + mime_decode(match) |
| 114 | pos = res.end(0) |
| 115 | return newline + line[pos:] |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 116 | |
Guido van Rossum | a3d9e02 | 1997-04-11 15:22:56 +0000 | [diff] [blame] | 117 | def unmimify_part(ifile, ofile, decode_base64 = 0): |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 118 | '''Convert a quoted-printable part of a MIME mail message to 8bit.''' |
| 119 | multipart = None |
| 120 | quoted_printable = 0 |
Guido van Rossum | a3d9e02 | 1997-04-11 15:22:56 +0000 | [diff] [blame] | 121 | is_base64 = 0 |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 122 | 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 Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame] | 140 | if qp.match(line): |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 141 | quoted_printable = 1 |
| 142 | continue # skip this header |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame] | 143 | if decode_base64 and base64_re.match(line): |
Guido van Rossum | a3d9e02 | 1997-04-11 15:22:56 +0000 | [diff] [blame] | 144 | is_base64 = 1 |
| 145 | continue |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 146 | ofile.write(pref + line) |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame] | 147 | if not prefix and repl.match(line): |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 148 | # we're dealing with a reply message |
| 149 | is_repl = 1 |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame] | 150 | mp_res = mp.match(line) |
| 151 | if mp_res: |
| 152 | multipart = '--' + mp_res.group(1) |
| 153 | if he.match(line): |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 154 | 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 Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame] | 163 | line = re.sub(mime_head, '\\1', line) |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 164 | 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 Rossum | a3d9e02 | 1997-04-11 15:22:56 +0000 | [diff] [blame] | 180 | unmimify_part(nifile, ofile, decode_base64) |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 181 | 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 Rossum | a3d9e02 | 1997-04-11 15:22:56 +0000 | [diff] [blame] | 193 | if line and is_base64 and not pref: |
| 194 | import base64 |
| 195 | line = base64.decodestring(line) |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 196 | if line: |
| 197 | ofile.write(pref + line) |
| 198 | |
Guido van Rossum | a3d9e02 | 1997-04-11 15:22:56 +0000 | [diff] [blame] | 199 | def unmimify(infile, outfile, decode_base64 = 0): |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 200 | '''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 Rossum | a3d9e02 | 1997-04-11 15:22:56 +0000 | [diff] [blame] | 214 | unmimify_part(nifile, ofile, decode_base64) |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 215 | ofile.flush() |
| 216 | |
Guido van Rossum | 11fbef5 | 1997-12-02 17:45:39 +0000 | [diff] [blame] | 217 | mime_char = re.compile('[=\177-\377]') # quote these chars in body |
| 218 | mime_header_char = re.compile('[=?\177-\377]') # quote these in header |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 219 | |
| 220 | def 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 Rossum | 613418a | 1997-10-30 15:27:37 +0000 | [diff] [blame] | 228 | pos = 0 |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 229 | 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 Rossum | 613418a | 1997-10-30 15:27:37 +0000 | [diff] [blame] | 232 | pos = 1 |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 233 | while 1: |
Guido van Rossum | 613418a | 1997-10-30 15:27:37 +0000 | [diff] [blame] | 234 | res = reg.search(line, pos) |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame] | 235 | if res is None: |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 236 | break |
Guido van Rossum | 613418a | 1997-10-30 15:27:37 +0000 | [diff] [blame] | 237 | 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 Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 241 | |
| 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 Rossum | 11fbef5 | 1997-12-02 17:45:39 +0000 | [diff] [blame] | 252 | mime_header = re.compile('([ \t(]|^)([-a-zA-Z0-9_+]*[\177-\377][-a-zA-Z0-9_+\177-\377]*)([ \t)]|\n)') |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 253 | |
| 254 | def mime_encode_header(line): |
| 255 | '''Code a single header line as quoted-printable.''' |
| 256 | newline = '' |
Guido van Rossum | 613418a | 1997-10-30 15:27:37 +0000 | [diff] [blame] | 257 | pos = 0 |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 258 | while 1: |
Guido van Rossum | 613418a | 1997-10-30 15:27:37 +0000 | [diff] [blame] | 259 | res = mime_header.search(line, pos) |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame] | 260 | if res is None: |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 261 | break |
Guido van Rossum | 613418a | 1997-10-30 15:27:37 +0000 | [diff] [blame] | 262 | 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 Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 267 | |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame] | 268 | mv = re.compile('^mime-version:', re.I) |
| 269 | cte = re.compile('^content-transfer-encoding:', re.I) |
Guido van Rossum | 11fbef5 | 1997-12-02 17:45:39 +0000 | [diff] [blame] | 270 | iso_char = re.compile('[\177-\377]') |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 271 | |
| 272 | def mimify_part(ifile, ofile, is_mime): |
| 273 | '''Convert an 8bit part of a MIME mail message to quoted-printable.''' |
Guido van Rossum | 6915568 | 1996-06-10 19:04:02 +0000 | [diff] [blame] | 274 | has_cte = is_qp = is_base64 = 0 |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 275 | 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 Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame] | 288 | if not must_quote_header and iso_char.search(line): |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 289 | must_quote_header = 1 |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame] | 290 | if mv.match(line): |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 291 | is_mime = 1 |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame] | 292 | if cte.match(line): |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 293 | has_cte = 1 |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame] | 294 | if qp.match(line): |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 295 | is_qp = 1 |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame] | 296 | elif base64_re.match(line): |
Guido van Rossum | 6915568 | 1996-06-10 19:04:02 +0000 | [diff] [blame] | 297 | is_base64 = 1 |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame] | 298 | mp_res = mp.match(line) |
| 299 | if mp_res: |
| 300 | multipart = '--' + mp_res.group(1) |
| 301 | if he.match(line): |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 302 | 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 Rossum | 6915568 | 1996-06-10 19:04:02 +0000 | [diff] [blame] | 318 | if is_base64: |
| 319 | message.append(line) |
| 320 | continue |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 321 | 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 Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame] | 331 | if iso_char.search(line): |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 332 | 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 Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame] | 341 | chrset_res = chrset.match(line) |
| 342 | if chrset_res: |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 343 | if has_iso_chars: |
| 344 | # change us-ascii into iso-8859-1 |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame] | 345 | if string.lower(chrset_res.group(2)) == 'us-ascii': |
Guido van Rossum | 613418a | 1997-10-30 15:27:37 +0000 | [diff] [blame] | 346 | line = '%s%s%s' % (chrset_res.group(1), |
| 347 | CHARSET, |
| 348 | chrset_res.group(3)) |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 349 | else: |
| 350 | # change iso-8859-* into us-ascii |
Guido van Rossum | 613418a | 1997-10-30 15:27:37 +0000 | [diff] [blame] | 351 | line = '%sus-ascii%s' % chrset_res.group(1, 3) |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame] | 352 | if has_cte and cte.match(line): |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 353 | line = 'Content-Transfer-Encoding: ' |
Guido van Rossum | 6915568 | 1996-06-10 19:04:02 +0000 | [diff] [blame] | 354 | if is_base64: |
| 355 | line = line + 'base64\n' |
| 356 | elif must_quote_body: |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 357 | 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 Rossum | f789ee4 | 1997-03-20 14:42:17 +0000 | [diff] [blame] | 381 | # 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 Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 389 | 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 | |
| 396 | def 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 | |
| 414 | import sys |
| 415 | if __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 Rossum | a3d9e02 | 1997-04-11 15:22:56 +0000 | [diff] [blame] | 419 | decode_base64 = 0 |
| 420 | opts, args = getopt.getopt(sys.argv[1:], 'l:edb') |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 421 | if len(args) not in (0, 1, 2): |
| 422 | print usage |
| 423 | sys.exit(1) |
Guido van Rossum | a3d9e02 | 1997-04-11 15:22:56 +0000 | [diff] [blame] | 424 | if (('-e', '') in opts) == (('-d', '') in opts) or \ |
| 425 | ((('-b', '') in opts) and (('-d', '') not in opts)): |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 426 | 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 Rossum | a3d9e02 | 1997-04-11 15:22:56 +0000 | [diff] [blame] | 439 | elif o == '-b': |
| 440 | decode_base64 = 1 |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 441 | if len(args) == 0: |
Guido van Rossum | a3d9e02 | 1997-04-11 15:22:56 +0000 | [diff] [blame] | 442 | encode_args = (sys.stdin, sys.stdout) |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 443 | elif len(args) == 1: |
Guido van Rossum | a3d9e02 | 1997-04-11 15:22:56 +0000 | [diff] [blame] | 444 | encode_args = (args[0], sys.stdout) |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 445 | else: |
Guido van Rossum | a3d9e02 | 1997-04-11 15:22:56 +0000 | [diff] [blame] | 446 | encode_args = (args[0], args[1]) |
| 447 | if decode_base64: |
| 448 | encode_args = encode_args + (decode_base64,) |
| 449 | apply(encode, encode_args) |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame] | 450 | |