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 = '' |
| 92 | while 1: |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 93 | res = mime_code.search(line) |
| 94 | if res is None: |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 95 | break |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 96 | newline = newline + line[:res.start(0)] + \ |
| 97 | chr(string.atoi(res.group(1), 16)) |
| 98 | line = line[res.end(0):] |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 99 | return newline + line |
| 100 | |
| 101 | def mime_decode_header(line): |
| 102 | '''Decode a header line to 8bit.''' |
| 103 | newline = '' |
| 104 | while 1: |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 105 | res = mime_head.search(line) |
| 106 | if res is None: |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 107 | break |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 108 | match = res.group(1) |
Guido van Rossum | 88bb808 | 1997-08-14 14:10:37 +0000 | [diff] [blame] | 109 | # convert underscores to spaces (before =XX conversion!) |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 110 | match = string.join(string.split(match, '_'), ' ') |
| 111 | newline = newline + line[:res.start(0)] + mime_decode(match) |
| 112 | line = line[res.end(0):] |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 113 | return newline + line |
| 114 | |
Guido van Rossum | a3d9e02 | 1997-04-11 15:22:56 +0000 | [diff] [blame] | 115 | def unmimify_part(ifile, ofile, decode_base64 = 0): |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 116 | '''Convert a quoted-printable part of a MIME mail message to 8bit.''' |
| 117 | multipart = None |
| 118 | quoted_printable = 0 |
Guido van Rossum | a3d9e02 | 1997-04-11 15:22:56 +0000 | [diff] [blame] | 119 | is_base64 = 0 |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 120 | is_repl = 0 |
| 121 | if ifile.boundary and ifile.boundary[:2] == QUOTE: |
| 122 | prefix = QUOTE |
| 123 | else: |
| 124 | prefix = '' |
| 125 | |
| 126 | # read header |
| 127 | hfile = HeaderFile(ifile) |
| 128 | while 1: |
| 129 | line = hfile.readline() |
| 130 | if not line: |
| 131 | return |
| 132 | if prefix and line[:len(prefix)] == prefix: |
| 133 | line = line[len(prefix):] |
| 134 | pref = prefix |
| 135 | else: |
| 136 | pref = '' |
| 137 | line = mime_decode_header(line) |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 138 | if qp.match(line): |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 139 | quoted_printable = 1 |
| 140 | continue # skip this header |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 141 | if decode_base64 and base64_re.match(line): |
Guido van Rossum | a3d9e02 | 1997-04-11 15:22:56 +0000 | [diff] [blame] | 142 | is_base64 = 1 |
| 143 | continue |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 144 | ofile.write(pref + line) |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 145 | if not prefix and repl.match(line): |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 146 | # we're dealing with a reply message |
| 147 | is_repl = 1 |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 148 | mp_res = mp.match(line) |
| 149 | if mp_res: |
| 150 | multipart = '--' + mp_res.group(1) |
| 151 | if he.match(line): |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 152 | break |
| 153 | if is_repl and (quoted_printable or multipart): |
| 154 | is_repl = 0 |
| 155 | |
| 156 | # read body |
| 157 | while 1: |
| 158 | line = ifile.readline() |
| 159 | if not line: |
| 160 | return |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 161 | line = re.sub(mime_head, '\\1', line) |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 162 | if prefix and line[:len(prefix)] == prefix: |
| 163 | line = line[len(prefix):] |
| 164 | pref = prefix |
| 165 | else: |
| 166 | pref = '' |
| 167 | ## if is_repl and len(line) >= 4 and line[:4] == QUOTE+'--' and line[-3:] != '--\n': |
| 168 | ## multipart = line[:-1] |
| 169 | while multipart: |
| 170 | if line == multipart + '--\n': |
| 171 | ofile.write(pref + line) |
| 172 | multipart = None |
| 173 | line = None |
| 174 | break |
| 175 | if line == multipart + '\n': |
| 176 | ofile.write(pref + line) |
| 177 | nifile = File(ifile, multipart) |
Guido van Rossum | a3d9e02 | 1997-04-11 15:22:56 +0000 | [diff] [blame] | 178 | unmimify_part(nifile, ofile, decode_base64) |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 179 | line = nifile.peek |
| 180 | continue |
| 181 | # not a boundary between parts |
| 182 | break |
| 183 | if line and quoted_printable: |
| 184 | while line[-2:] == '=\n': |
| 185 | line = line[:-2] |
| 186 | newline = ifile.readline() |
| 187 | if newline[:len(QUOTE)] == QUOTE: |
| 188 | newline = newline[len(QUOTE):] |
| 189 | line = line + newline |
| 190 | line = mime_decode(line) |
Guido van Rossum | a3d9e02 | 1997-04-11 15:22:56 +0000 | [diff] [blame] | 191 | if line and is_base64 and not pref: |
| 192 | import base64 |
| 193 | line = base64.decodestring(line) |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 194 | if line: |
| 195 | ofile.write(pref + line) |
| 196 | |
Guido van Rossum | a3d9e02 | 1997-04-11 15:22:56 +0000 | [diff] [blame] | 197 | def unmimify(infile, outfile, decode_base64 = 0): |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 198 | '''Convert quoted-printable parts of a MIME mail message to 8bit.''' |
| 199 | if type(infile) == type(''): |
| 200 | ifile = open(infile) |
| 201 | if type(outfile) == type('') and infile == outfile: |
| 202 | import os |
| 203 | d, f = os.path.split(infile) |
| 204 | os.rename(infile, os.path.join(d, ',' + f)) |
| 205 | else: |
| 206 | ifile = infile |
| 207 | if type(outfile) == type(''): |
| 208 | ofile = open(outfile, 'w') |
| 209 | else: |
| 210 | ofile = outfile |
| 211 | nifile = File(ifile, None) |
Guido van Rossum | a3d9e02 | 1997-04-11 15:22:56 +0000 | [diff] [blame] | 212 | unmimify_part(nifile, ofile, decode_base64) |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 213 | ofile.flush() |
| 214 | |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 215 | mime_char = re.compile('[=\240-\377]') # quote these chars in body |
| 216 | mime_header_char = re.compile('[=?\240-\377]') # quote these in header |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 217 | |
| 218 | def mime_encode(line, header): |
| 219 | '''Code a single line as quoted-printable. |
| 220 | If header is set, quote some extra characters.''' |
| 221 | if header: |
| 222 | reg = mime_header_char |
| 223 | else: |
| 224 | reg = mime_char |
| 225 | newline = '' |
| 226 | if len(line) >= 5 and line[:5] == 'From ': |
| 227 | # quote 'From ' at the start of a line for stupid mailers |
| 228 | newline = string.upper('=%02x' % ord('F')) |
| 229 | line = line[1:] |
| 230 | while 1: |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 231 | res = reg.search(line) |
| 232 | if res is None: |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 233 | break |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 234 | newline = newline + line[:res.start(0)] + \ |
| 235 | string.upper('=%02x' % ord(line[res.group(0)])) |
| 236 | line = line[res.end(0):] |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 237 | line = newline + line |
| 238 | |
| 239 | newline = '' |
| 240 | while len(line) >= 75: |
| 241 | i = 73 |
| 242 | while line[i] == '=' or line[i-1] == '=': |
| 243 | i = i - 1 |
| 244 | i = i + 1 |
| 245 | newline = newline + line[:i] + '=\n' |
| 246 | line = line[i:] |
| 247 | return newline + line |
| 248 | |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 249 | mime_header = re.compile('([ \t(]|^)([-a-zA-Z0-9_+]*[\240-\377][-a-zA-Z0-9_+\240-\377]*)([ \t)]|\n)') |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 250 | |
| 251 | def mime_encode_header(line): |
| 252 | '''Code a single header line as quoted-printable.''' |
| 253 | newline = '' |
| 254 | while 1: |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 255 | res = mime_header.search(line) |
| 256 | if res is None: |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 257 | break |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 258 | newline = newline + line[:res.start(0)] + res.group(1) + \ |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 259 | '=?' + CHARSET + '?Q?' + \ |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 260 | mime_encode(res.group(2), 1) + \ |
| 261 | '?=' + res.group(3) |
| 262 | line = line[res.end(0):] |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 263 | return newline + line |
| 264 | |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 265 | mv = re.compile('^mime-version:', re.I) |
| 266 | cte = re.compile('^content-transfer-encoding:', re.I) |
| 267 | iso_char = re.compile('[\240-\377]') |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 268 | |
| 269 | def mimify_part(ifile, ofile, is_mime): |
| 270 | '''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] | 271 | has_cte = is_qp = is_base64 = 0 |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 272 | multipart = None |
| 273 | must_quote_body = must_quote_header = has_iso_chars = 0 |
| 274 | |
| 275 | header = [] |
| 276 | header_end = '' |
| 277 | message = [] |
| 278 | message_end = '' |
| 279 | # read header |
| 280 | hfile = HeaderFile(ifile) |
| 281 | while 1: |
| 282 | line = hfile.readline() |
| 283 | if not line: |
| 284 | break |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 285 | if not must_quote_header and iso_char.search(line): |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 286 | must_quote_header = 1 |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 287 | if mv.match(line): |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 288 | is_mime = 1 |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 289 | if cte.match(line): |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 290 | has_cte = 1 |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 291 | if qp.match(line): |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 292 | is_qp = 1 |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 293 | elif base64_re.match(line): |
Guido van Rossum | 6915568 | 1996-06-10 19:04:02 +0000 | [diff] [blame] | 294 | is_base64 = 1 |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 295 | mp_res = mp.match(line) |
| 296 | if mp_res: |
| 297 | multipart = '--' + mp_res.group(1) |
| 298 | if he.match(line): |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 299 | header_end = line |
| 300 | break |
| 301 | header.append(line) |
| 302 | |
| 303 | # read body |
| 304 | while 1: |
| 305 | line = ifile.readline() |
| 306 | if not line: |
| 307 | break |
| 308 | if multipart: |
| 309 | if line == multipart + '--\n': |
| 310 | message_end = line |
| 311 | break |
| 312 | if line == multipart + '\n': |
| 313 | message_end = line |
| 314 | break |
Guido van Rossum | 6915568 | 1996-06-10 19:04:02 +0000 | [diff] [blame] | 315 | if is_base64: |
| 316 | message.append(line) |
| 317 | continue |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 318 | if is_qp: |
| 319 | while line[-2:] == '=\n': |
| 320 | line = line[:-2] |
| 321 | newline = ifile.readline() |
| 322 | if newline[:len(QUOTE)] == QUOTE: |
| 323 | newline = newline[len(QUOTE):] |
| 324 | line = line + newline |
| 325 | line = mime_decode(line) |
| 326 | message.append(line) |
| 327 | if not has_iso_chars: |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 328 | if iso_char.search(line): |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 329 | has_iso_chars = must_quote_body = 1 |
| 330 | if not must_quote_body: |
| 331 | if len(line) > MAXLEN: |
| 332 | must_quote_body = 1 |
| 333 | |
| 334 | # convert and output header and body |
| 335 | for line in header: |
| 336 | if must_quote_header: |
| 337 | line = mime_encode_header(line) |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 338 | chrset_res = chrset.match(line) |
| 339 | if chrset_res: |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 340 | if has_iso_chars: |
| 341 | # change us-ascii into iso-8859-1 |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 342 | if string.lower(chrset_res.group(2)) == 'us-ascii': |
| 343 | line = chrset_res.group(1) + \ |
| 344 | CHARSET + chrset_res.group(3) |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 345 | else: |
| 346 | # change iso-8859-* into us-ascii |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 347 | line = chrset_res.group(1) + 'us-ascii' + chrset_res.group(3) |
| 348 | if has_cte and cte.match(line): |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 349 | line = 'Content-Transfer-Encoding: ' |
Guido van Rossum | 6915568 | 1996-06-10 19:04:02 +0000 | [diff] [blame] | 350 | if is_base64: |
| 351 | line = line + 'base64\n' |
| 352 | elif must_quote_body: |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 353 | line = line + 'quoted-printable\n' |
| 354 | else: |
| 355 | line = line + '7bit\n' |
| 356 | ofile.write(line) |
| 357 | if (must_quote_header or must_quote_body) and not is_mime: |
| 358 | ofile.write('Mime-Version: 1.0\n') |
| 359 | ofile.write('Content-Type: text/plain; ') |
| 360 | if has_iso_chars: |
| 361 | ofile.write('charset="%s"\n' % CHARSET) |
| 362 | else: |
| 363 | ofile.write('charset="us-ascii"\n') |
| 364 | if must_quote_body and not has_cte: |
| 365 | ofile.write('Content-Transfer-Encoding: quoted-printable\n') |
| 366 | ofile.write(header_end) |
| 367 | |
| 368 | for line in message: |
| 369 | if must_quote_body: |
| 370 | line = mime_encode(line, 0) |
| 371 | ofile.write(line) |
| 372 | ofile.write(message_end) |
| 373 | |
| 374 | line = message_end |
| 375 | while multipart: |
| 376 | if line == multipart + '--\n': |
Guido van Rossum | f789ee4 | 1997-03-20 14:42:17 +0000 | [diff] [blame] | 377 | # read bit after the end of the last part |
| 378 | while 1: |
| 379 | line = ifile.readline() |
| 380 | if not line: |
| 381 | return |
| 382 | if must_quote_body: |
| 383 | line = mime_encode(line, 0) |
| 384 | ofile.write(line) |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 385 | if line == multipart + '\n': |
| 386 | nifile = File(ifile, multipart) |
| 387 | mimify_part(nifile, ofile, 1) |
| 388 | line = nifile.peek |
| 389 | ofile.write(line) |
| 390 | continue |
| 391 | |
| 392 | def mimify(infile, outfile): |
| 393 | '''Convert 8bit parts of a MIME mail message to quoted-printable.''' |
| 394 | if type(infile) == type(''): |
| 395 | ifile = open(infile) |
| 396 | if type(outfile) == type('') and infile == outfile: |
| 397 | import os |
| 398 | d, f = os.path.split(infile) |
| 399 | os.rename(infile, os.path.join(d, ',' + f)) |
| 400 | else: |
| 401 | ifile = infile |
| 402 | if type(outfile) == type(''): |
| 403 | ofile = open(outfile, 'w') |
| 404 | else: |
| 405 | ofile = outfile |
| 406 | nifile = File(ifile, None) |
| 407 | mimify_part(nifile, ofile, 0) |
| 408 | ofile.flush() |
| 409 | |
| 410 | import sys |
| 411 | if __name__ == '__main__' or (len(sys.argv) > 0 and sys.argv[0] == 'mimify'): |
| 412 | import getopt |
| 413 | usage = 'Usage: mimify [-l len] -[ed] [infile [outfile]]' |
| 414 | |
Guido van Rossum | a3d9e02 | 1997-04-11 15:22:56 +0000 | [diff] [blame] | 415 | decode_base64 = 0 |
| 416 | opts, args = getopt.getopt(sys.argv[1:], 'l:edb') |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 417 | if len(args) not in (0, 1, 2): |
| 418 | print usage |
| 419 | sys.exit(1) |
Guido van Rossum | a3d9e02 | 1997-04-11 15:22:56 +0000 | [diff] [blame] | 420 | if (('-e', '') in opts) == (('-d', '') in opts) or \ |
| 421 | ((('-b', '') in opts) and (('-d', '') not in opts)): |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 422 | print usage |
| 423 | sys.exit(1) |
| 424 | for o, a in opts: |
| 425 | if o == '-e': |
| 426 | encode = mimify |
| 427 | elif o == '-d': |
| 428 | encode = unmimify |
| 429 | elif o == '-l': |
| 430 | try: |
| 431 | MAXLEN = string.atoi(a) |
| 432 | except: |
| 433 | print usage |
| 434 | sys.exit(1) |
Guido van Rossum | a3d9e02 | 1997-04-11 15:22:56 +0000 | [diff] [blame] | 435 | elif o == '-b': |
| 436 | decode_base64 = 1 |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 437 | if len(args) == 0: |
Guido van Rossum | a3d9e02 | 1997-04-11 15:22:56 +0000 | [diff] [blame] | 438 | encode_args = (sys.stdin, sys.stdout) |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 439 | elif len(args) == 1: |
Guido van Rossum | a3d9e02 | 1997-04-11 15:22:56 +0000 | [diff] [blame] | 440 | encode_args = (args[0], sys.stdout) |
Sjoerd Mullender | e8a0a5c | 1996-02-14 10:40:03 +0000 | [diff] [blame] | 441 | else: |
Guido van Rossum | a3d9e02 | 1997-04-11 15:22:56 +0000 | [diff] [blame] | 442 | encode_args = (args[0], args[1]) |
| 443 | if decode_base64: |
| 444 | encode_args = encode_args + (decode_base64,) |
| 445 | apply(encode, encode_args) |
Guido van Rossum | 31626bc | 1997-10-24 14:46:16 +0000 | [diff] [blame^] | 446 | |