| 1 | n/a | #! /usr/bin/env python |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | """Mimification and unmimification of mail messages. |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | Decode quoted-printable parts of a mail message or encode using |
|---|
| 6 | n/a | quoted-printable. |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | Usage: |
|---|
| 9 | n/a | mimify(input, output) |
|---|
| 10 | n/a | unmimify(input, output, decode_base64 = 0) |
|---|
| 11 | n/a | to encode and decode respectively. Input and output may be the name |
|---|
| 12 | n/a | of a file or an open file object. Only a readline() method is used |
|---|
| 13 | n/a | on the input file, only a write() method is used on the output file. |
|---|
| 14 | n/a | When using file names, the input and output file names may be the |
|---|
| 15 | n/a | same. |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | Interactive usage: |
|---|
| 18 | n/a | mimify.py -e [infile [outfile]] |
|---|
| 19 | n/a | mimify.py -d [infile [outfile]] |
|---|
| 20 | n/a | to encode and decode respectively. Infile defaults to standard |
|---|
| 21 | n/a | input and outfile to standard output. |
|---|
| 22 | 1 | """ |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | # Configure |
|---|
| 25 | 1 | MAXLEN = 200 # if lines longer than this, encode as quoted-printable |
|---|
| 26 | 1 | CHARSET = 'ISO-8859-1' # default charset for non-US-ASCII mail |
|---|
| 27 | 1 | QUOTE = '> ' # string replies are quoted with |
|---|
| 28 | n/a | # End configure |
|---|
| 29 | n/a | |
|---|
| 30 | 1 | import re |
|---|
| 31 | n/a | |
|---|
| 32 | 1 | import warnings |
|---|
| 33 | 1 | warnings.warn("the mimify module is deprecated; use the email package instead", |
|---|
| 34 | 1 | DeprecationWarning, 2) |
|---|
| 35 | n/a | |
|---|
| 36 | 1 | __all__ = ["mimify","unmimify","mime_encode_header","mime_decode_header"] |
|---|
| 37 | n/a | |
|---|
| 38 | 1 | qp = re.compile('^content-transfer-encoding:\\s*quoted-printable', re.I) |
|---|
| 39 | 1 | base64_re = re.compile('^content-transfer-encoding:\\s*base64', re.I) |
|---|
| 40 | 1 | mp = re.compile('^content-type:.*multipart/.*boundary="?([^;"\n]*)', re.I|re.S) |
|---|
| 41 | 1 | chrset = re.compile('^(content-type:.*charset=")(us-ascii|iso-8859-[0-9]+)(".*)', re.I|re.S) |
|---|
| 42 | 1 | he = re.compile('^-*\n') |
|---|
| 43 | 1 | mime_code = re.compile('=([0-9a-f][0-9a-f])', re.I) |
|---|
| 44 | 1 | mime_head = re.compile('=\\?iso-8859-1\\?q\\?([^? \t\n]+)\\?=', re.I) |
|---|
| 45 | 1 | repl = re.compile('^subject:\\s+re: ', re.I) |
|---|
| 46 | n/a | |
|---|
| 47 | 2 | class File: |
|---|
| 48 | n/a | """A simple fake file object that knows about limited read-ahead and |
|---|
| 49 | 1 | boundaries. The only supported method is readline().""" |
|---|
| 50 | n/a | |
|---|
| 51 | 1 | def __init__(self, file, boundary): |
|---|
| 52 | 0 | self.file = file |
|---|
| 53 | 0 | self.boundary = boundary |
|---|
| 54 | 0 | self.peek = None |
|---|
| 55 | n/a | |
|---|
| 56 | 1 | def readline(self): |
|---|
| 57 | 0 | if self.peek is not None: |
|---|
| 58 | 0 | return '' |
|---|
| 59 | 0 | line = self.file.readline() |
|---|
| 60 | 0 | if not line: |
|---|
| 61 | 0 | return line |
|---|
| 62 | 0 | if self.boundary: |
|---|
| 63 | 0 | if line == self.boundary + '\n': |
|---|
| 64 | 0 | self.peek = line |
|---|
| 65 | 0 | return '' |
|---|
| 66 | 0 | if line == self.boundary + '--\n': |
|---|
| 67 | 0 | self.peek = line |
|---|
| 68 | 0 | return '' |
|---|
| 69 | 0 | return line |
|---|
| 70 | n/a | |
|---|
| 71 | 2 | class HeaderFile: |
|---|
| 72 | 1 | def __init__(self, file): |
|---|
| 73 | 0 | self.file = file |
|---|
| 74 | 0 | self.peek = None |
|---|
| 75 | n/a | |
|---|
| 76 | 1 | def readline(self): |
|---|
| 77 | 0 | if self.peek is not None: |
|---|
| 78 | 0 | line = self.peek |
|---|
| 79 | 0 | self.peek = None |
|---|
| 80 | n/a | else: |
|---|
| 81 | 0 | line = self.file.readline() |
|---|
| 82 | 0 | if not line: |
|---|
| 83 | 0 | return line |
|---|
| 84 | 0 | if he.match(line): |
|---|
| 85 | 0 | return line |
|---|
| 86 | 0 | while 1: |
|---|
| 87 | 0 | self.peek = self.file.readline() |
|---|
| 88 | 0 | if len(self.peek) == 0 or \ |
|---|
| 89 | 0 | (self.peek[0] != ' ' and self.peek[0] != '\t'): |
|---|
| 90 | 0 | return line |
|---|
| 91 | 0 | line = line + self.peek |
|---|
| 92 | 0 | self.peek = None |
|---|
| 93 | n/a | |
|---|
| 94 | 1 | def mime_decode(line): |
|---|
| 95 | n/a | """Decode a single line of quoted-printable text to 8bit.""" |
|---|
| 96 | 0 | newline = '' |
|---|
| 97 | 0 | pos = 0 |
|---|
| 98 | 0 | while 1: |
|---|
| 99 | 0 | res = mime_code.search(line, pos) |
|---|
| 100 | 0 | if res is None: |
|---|
| 101 | 0 | break |
|---|
| 102 | 0 | newline = newline + line[pos:res.start(0)] + \ |
|---|
| 103 | 0 | chr(int(res.group(1), 16)) |
|---|
| 104 | 0 | pos = res.end(0) |
|---|
| 105 | 0 | return newline + line[pos:] |
|---|
| 106 | n/a | |
|---|
| 107 | 1 | def mime_decode_header(line): |
|---|
| 108 | n/a | """Decode a header line to 8bit.""" |
|---|
| 109 | 0 | newline = '' |
|---|
| 110 | 0 | pos = 0 |
|---|
| 111 | 0 | while 1: |
|---|
| 112 | 0 | res = mime_head.search(line, pos) |
|---|
| 113 | 0 | if res is None: |
|---|
| 114 | 0 | break |
|---|
| 115 | 0 | match = res.group(1) |
|---|
| 116 | n/a | # convert underscores to spaces (before =XX conversion!) |
|---|
| 117 | 0 | match = ' '.join(match.split('_')) |
|---|
| 118 | 0 | newline = newline + line[pos:res.start(0)] + mime_decode(match) |
|---|
| 119 | 0 | pos = res.end(0) |
|---|
| 120 | 0 | return newline + line[pos:] |
|---|
| 121 | n/a | |
|---|
| 122 | 1 | def unmimify_part(ifile, ofile, decode_base64 = 0): |
|---|
| 123 | n/a | """Convert a quoted-printable part of a MIME mail message to 8bit.""" |
|---|
| 124 | 0 | multipart = None |
|---|
| 125 | 0 | quoted_printable = 0 |
|---|
| 126 | 0 | is_base64 = 0 |
|---|
| 127 | 0 | is_repl = 0 |
|---|
| 128 | 0 | if ifile.boundary and ifile.boundary[:2] == QUOTE: |
|---|
| 129 | 0 | prefix = QUOTE |
|---|
| 130 | n/a | else: |
|---|
| 131 | 0 | prefix = '' |
|---|
| 132 | n/a | |
|---|
| 133 | n/a | # read header |
|---|
| 134 | 0 | hfile = HeaderFile(ifile) |
|---|
| 135 | 0 | while 1: |
|---|
| 136 | 0 | line = hfile.readline() |
|---|
| 137 | 0 | if not line: |
|---|
| 138 | 0 | return |
|---|
| 139 | 0 | if prefix and line[:len(prefix)] == prefix: |
|---|
| 140 | 0 | line = line[len(prefix):] |
|---|
| 141 | 0 | pref = prefix |
|---|
| 142 | n/a | else: |
|---|
| 143 | 0 | pref = '' |
|---|
| 144 | 0 | line = mime_decode_header(line) |
|---|
| 145 | 0 | if qp.match(line): |
|---|
| 146 | 0 | quoted_printable = 1 |
|---|
| 147 | 0 | continue # skip this header |
|---|
| 148 | 0 | if decode_base64 and base64_re.match(line): |
|---|
| 149 | 0 | is_base64 = 1 |
|---|
| 150 | 0 | continue |
|---|
| 151 | 0 | ofile.write(pref + line) |
|---|
| 152 | 0 | if not prefix and repl.match(line): |
|---|
| 153 | n/a | # we're dealing with a reply message |
|---|
| 154 | 0 | is_repl = 1 |
|---|
| 155 | 0 | mp_res = mp.match(line) |
|---|
| 156 | 0 | if mp_res: |
|---|
| 157 | 0 | multipart = '--' + mp_res.group(1) |
|---|
| 158 | 0 | if he.match(line): |
|---|
| 159 | 0 | break |
|---|
| 160 | 0 | if is_repl and (quoted_printable or multipart): |
|---|
| 161 | 0 | is_repl = 0 |
|---|
| 162 | n/a | |
|---|
| 163 | n/a | # read body |
|---|
| 164 | 0 | while 1: |
|---|
| 165 | 0 | line = ifile.readline() |
|---|
| 166 | 0 | if not line: |
|---|
| 167 | 0 | return |
|---|
| 168 | 0 | line = re.sub(mime_head, '\\1', line) |
|---|
| 169 | 0 | if prefix and line[:len(prefix)] == prefix: |
|---|
| 170 | 0 | line = line[len(prefix):] |
|---|
| 171 | 0 | pref = prefix |
|---|
| 172 | n/a | else: |
|---|
| 173 | 0 | pref = '' |
|---|
| 174 | n/a | ## if is_repl and len(line) >= 4 and line[:4] == QUOTE+'--' and line[-3:] != '--\n': |
|---|
| 175 | n/a | ## multipart = line[:-1] |
|---|
| 176 | 0 | while multipart: |
|---|
| 177 | 0 | if line == multipart + '--\n': |
|---|
| 178 | 0 | ofile.write(pref + line) |
|---|
| 179 | 0 | multipart = None |
|---|
| 180 | 0 | line = None |
|---|
| 181 | 0 | break |
|---|
| 182 | 0 | if line == multipart + '\n': |
|---|
| 183 | 0 | ofile.write(pref + line) |
|---|
| 184 | 0 | nifile = File(ifile, multipart) |
|---|
| 185 | 0 | unmimify_part(nifile, ofile, decode_base64) |
|---|
| 186 | 0 | line = nifile.peek |
|---|
| 187 | 0 | if not line: |
|---|
| 188 | n/a | # premature end of file |
|---|
| 189 | 0 | break |
|---|
| 190 | 0 | continue |
|---|
| 191 | n/a | # not a boundary between parts |
|---|
| 192 | 0 | break |
|---|
| 193 | 0 | if line and quoted_printable: |
|---|
| 194 | 0 | while line[-2:] == '=\n': |
|---|
| 195 | 0 | line = line[:-2] |
|---|
| 196 | 0 | newline = ifile.readline() |
|---|
| 197 | 0 | if newline[:len(QUOTE)] == QUOTE: |
|---|
| 198 | 0 | newline = newline[len(QUOTE):] |
|---|
| 199 | 0 | line = line + newline |
|---|
| 200 | 0 | line = mime_decode(line) |
|---|
| 201 | 0 | if line and is_base64 and not pref: |
|---|
| 202 | 0 | import base64 |
|---|
| 203 | 0 | line = base64.decodestring(line) |
|---|
| 204 | 0 | if line: |
|---|
| 205 | 0 | ofile.write(pref + line) |
|---|
| 206 | n/a | |
|---|
| 207 | 1 | def unmimify(infile, outfile, decode_base64 = 0): |
|---|
| 208 | n/a | """Convert quoted-printable parts of a MIME mail message to 8bit.""" |
|---|
| 209 | 0 | if type(infile) == type(''): |
|---|
| 210 | 0 | ifile = open(infile) |
|---|
| 211 | 0 | if type(outfile) == type('') and infile == outfile: |
|---|
| 212 | 0 | import os |
|---|
| 213 | 0 | d, f = os.path.split(infile) |
|---|
| 214 | 0 | os.rename(infile, os.path.join(d, ',' + f)) |
|---|
| 215 | n/a | else: |
|---|
| 216 | 0 | ifile = infile |
|---|
| 217 | 0 | if type(outfile) == type(''): |
|---|
| 218 | 0 | ofile = open(outfile, 'w') |
|---|
| 219 | n/a | else: |
|---|
| 220 | 0 | ofile = outfile |
|---|
| 221 | 0 | nifile = File(ifile, None) |
|---|
| 222 | 0 | unmimify_part(nifile, ofile, decode_base64) |
|---|
| 223 | 0 | ofile.flush() |
|---|
| 224 | n/a | |
|---|
| 225 | 1 | mime_char = re.compile('[=\177-\377]') # quote these chars in body |
|---|
| 226 | 1 | mime_header_char = re.compile('[=?\177-\377]') # quote these in header |
|---|
| 227 | n/a | |
|---|
| 228 | 1 | def mime_encode(line, header): |
|---|
| 229 | n/a | """Code a single line as quoted-printable. |
|---|
| 230 | n/a | If header is set, quote some extra characters.""" |
|---|
| 231 | 0 | if header: |
|---|
| 232 | 0 | reg = mime_header_char |
|---|
| 233 | n/a | else: |
|---|
| 234 | 0 | reg = mime_char |
|---|
| 235 | 0 | newline = '' |
|---|
| 236 | 0 | pos = 0 |
|---|
| 237 | 0 | if len(line) >= 5 and line[:5] == 'From ': |
|---|
| 238 | n/a | # quote 'From ' at the start of a line for stupid mailers |
|---|
| 239 | 0 | newline = ('=%02x' % ord('F')).upper() |
|---|
| 240 | 0 | pos = 1 |
|---|
| 241 | 0 | while 1: |
|---|
| 242 | 0 | res = reg.search(line, pos) |
|---|
| 243 | 0 | if res is None: |
|---|
| 244 | 0 | break |
|---|
| 245 | 0 | newline = newline + line[pos:res.start(0)] + \ |
|---|
| 246 | 0 | ('=%02x' % ord(res.group(0))).upper() |
|---|
| 247 | 0 | pos = res.end(0) |
|---|
| 248 | 0 | line = newline + line[pos:] |
|---|
| 249 | n/a | |
|---|
| 250 | 0 | newline = '' |
|---|
| 251 | 0 | while len(line) >= 75: |
|---|
| 252 | 0 | i = 73 |
|---|
| 253 | 0 | while line[i] == '=' or line[i-1] == '=': |
|---|
| 254 | 0 | i = i - 1 |
|---|
| 255 | 0 | i = i + 1 |
|---|
| 256 | 0 | newline = newline + line[:i] + '=\n' |
|---|
| 257 | 0 | line = line[i:] |
|---|
| 258 | 0 | return newline + line |
|---|
| 259 | n/a | |
|---|
| 260 | 1 | mime_header = re.compile('([ \t(]|^)([-a-zA-Z0-9_+]*[\177-\377][-a-zA-Z0-9_+\177-\377]*)(?=[ \t)]|\n)') |
|---|
| 261 | n/a | |
|---|
| 262 | 1 | def mime_encode_header(line): |
|---|
| 263 | n/a | """Code a single header line as quoted-printable.""" |
|---|
| 264 | 0 | newline = '' |
|---|
| 265 | 0 | pos = 0 |
|---|
| 266 | 0 | while 1: |
|---|
| 267 | 0 | res = mime_header.search(line, pos) |
|---|
| 268 | 0 | if res is None: |
|---|
| 269 | 0 | break |
|---|
| 270 | 0 | newline = '%s%s%s=?%s?Q?%s?=' % \ |
|---|
| 271 | 0 | (newline, line[pos:res.start(0)], res.group(1), |
|---|
| 272 | 0 | CHARSET, mime_encode(res.group(2), 1)) |
|---|
| 273 | 0 | pos = res.end(0) |
|---|
| 274 | 0 | return newline + line[pos:] |
|---|
| 275 | n/a | |
|---|
| 276 | 1 | mv = re.compile('^mime-version:', re.I) |
|---|
| 277 | 1 | cte = re.compile('^content-transfer-encoding:', re.I) |
|---|
| 278 | 1 | iso_char = re.compile('[\177-\377]') |
|---|
| 279 | n/a | |
|---|
| 280 | 1 | def mimify_part(ifile, ofile, is_mime): |
|---|
| 281 | n/a | """Convert an 8bit part of a MIME mail message to quoted-printable.""" |
|---|
| 282 | 0 | has_cte = is_qp = is_base64 = 0 |
|---|
| 283 | 0 | multipart = None |
|---|
| 284 | 0 | must_quote_body = must_quote_header = has_iso_chars = 0 |
|---|
| 285 | n/a | |
|---|
| 286 | 0 | header = [] |
|---|
| 287 | 0 | header_end = '' |
|---|
| 288 | 0 | message = [] |
|---|
| 289 | 0 | message_end = '' |
|---|
| 290 | n/a | # read header |
|---|
| 291 | 0 | hfile = HeaderFile(ifile) |
|---|
| 292 | 0 | while 1: |
|---|
| 293 | 0 | line = hfile.readline() |
|---|
| 294 | 0 | if not line: |
|---|
| 295 | 0 | break |
|---|
| 296 | 0 | if not must_quote_header and iso_char.search(line): |
|---|
| 297 | 0 | must_quote_header = 1 |
|---|
| 298 | 0 | if mv.match(line): |
|---|
| 299 | 0 | is_mime = 1 |
|---|
| 300 | 0 | if cte.match(line): |
|---|
| 301 | 0 | has_cte = 1 |
|---|
| 302 | 0 | if qp.match(line): |
|---|
| 303 | 0 | is_qp = 1 |
|---|
| 304 | 0 | elif base64_re.match(line): |
|---|
| 305 | 0 | is_base64 = 1 |
|---|
| 306 | 0 | mp_res = mp.match(line) |
|---|
| 307 | 0 | if mp_res: |
|---|
| 308 | 0 | multipart = '--' + mp_res.group(1) |
|---|
| 309 | 0 | if he.match(line): |
|---|
| 310 | 0 | header_end = line |
|---|
| 311 | 0 | break |
|---|
| 312 | 0 | header.append(line) |
|---|
| 313 | n/a | |
|---|
| 314 | n/a | # read body |
|---|
| 315 | 0 | while 1: |
|---|
| 316 | 0 | line = ifile.readline() |
|---|
| 317 | 0 | if not line: |
|---|
| 318 | 0 | break |
|---|
| 319 | 0 | if multipart: |
|---|
| 320 | 0 | if line == multipart + '--\n': |
|---|
| 321 | 0 | message_end = line |
|---|
| 322 | 0 | break |
|---|
| 323 | 0 | if line == multipart + '\n': |
|---|
| 324 | 0 | message_end = line |
|---|
| 325 | 0 | break |
|---|
| 326 | 0 | if is_base64: |
|---|
| 327 | 0 | message.append(line) |
|---|
| 328 | 0 | continue |
|---|
| 329 | 0 | if is_qp: |
|---|
| 330 | 0 | while line[-2:] == '=\n': |
|---|
| 331 | 0 | line = line[:-2] |
|---|
| 332 | 0 | newline = ifile.readline() |
|---|
| 333 | 0 | if newline[:len(QUOTE)] == QUOTE: |
|---|
| 334 | 0 | newline = newline[len(QUOTE):] |
|---|
| 335 | 0 | line = line + newline |
|---|
| 336 | 0 | line = mime_decode(line) |
|---|
| 337 | 0 | message.append(line) |
|---|
| 338 | 0 | if not has_iso_chars: |
|---|
| 339 | 0 | if iso_char.search(line): |
|---|
| 340 | 0 | has_iso_chars = must_quote_body = 1 |
|---|
| 341 | 0 | if not must_quote_body: |
|---|
| 342 | 0 | if len(line) > MAXLEN: |
|---|
| 343 | 0 | must_quote_body = 1 |
|---|
| 344 | n/a | |
|---|
| 345 | n/a | # convert and output header and body |
|---|
| 346 | 0 | for line in header: |
|---|
| 347 | 0 | if must_quote_header: |
|---|
| 348 | 0 | line = mime_encode_header(line) |
|---|
| 349 | 0 | chrset_res = chrset.match(line) |
|---|
| 350 | 0 | if chrset_res: |
|---|
| 351 | 0 | if has_iso_chars: |
|---|
| 352 | n/a | # change us-ascii into iso-8859-1 |
|---|
| 353 | 0 | if chrset_res.group(2).lower() == 'us-ascii': |
|---|
| 354 | 0 | line = '%s%s%s' % (chrset_res.group(1), |
|---|
| 355 | 0 | CHARSET, |
|---|
| 356 | 0 | chrset_res.group(3)) |
|---|
| 357 | n/a | else: |
|---|
| 358 | n/a | # change iso-8859-* into us-ascii |
|---|
| 359 | 0 | line = '%sus-ascii%s' % chrset_res.group(1, 3) |
|---|
| 360 | 0 | if has_cte and cte.match(line): |
|---|
| 361 | 0 | line = 'Content-Transfer-Encoding: ' |
|---|
| 362 | 0 | if is_base64: |
|---|
| 363 | 0 | line = line + 'base64\n' |
|---|
| 364 | 0 | elif must_quote_body: |
|---|
| 365 | 0 | line = line + 'quoted-printable\n' |
|---|
| 366 | n/a | else: |
|---|
| 367 | 0 | line = line + '7bit\n' |
|---|
| 368 | 0 | ofile.write(line) |
|---|
| 369 | 0 | if (must_quote_header or must_quote_body) and not is_mime: |
|---|
| 370 | 0 | ofile.write('Mime-Version: 1.0\n') |
|---|
| 371 | 0 | ofile.write('Content-Type: text/plain; ') |
|---|
| 372 | 0 | if has_iso_chars: |
|---|
| 373 | 0 | ofile.write('charset="%s"\n' % CHARSET) |
|---|
| 374 | n/a | else: |
|---|
| 375 | 0 | ofile.write('charset="us-ascii"\n') |
|---|
| 376 | 0 | if must_quote_body and not has_cte: |
|---|
| 377 | 0 | ofile.write('Content-Transfer-Encoding: quoted-printable\n') |
|---|
| 378 | 0 | ofile.write(header_end) |
|---|
| 379 | n/a | |
|---|
| 380 | 0 | for line in message: |
|---|
| 381 | 0 | if must_quote_body: |
|---|
| 382 | 0 | line = mime_encode(line, 0) |
|---|
| 383 | 0 | ofile.write(line) |
|---|
| 384 | 0 | ofile.write(message_end) |
|---|
| 385 | n/a | |
|---|
| 386 | 0 | line = message_end |
|---|
| 387 | 0 | while multipart: |
|---|
| 388 | 0 | if line == multipart + '--\n': |
|---|
| 389 | n/a | # read bit after the end of the last part |
|---|
| 390 | 0 | while 1: |
|---|
| 391 | 0 | line = ifile.readline() |
|---|
| 392 | 0 | if not line: |
|---|
| 393 | 0 | return |
|---|
| 394 | 0 | if must_quote_body: |
|---|
| 395 | 0 | line = mime_encode(line, 0) |
|---|
| 396 | 0 | ofile.write(line) |
|---|
| 397 | 0 | if line == multipart + '\n': |
|---|
| 398 | 0 | nifile = File(ifile, multipart) |
|---|
| 399 | 0 | mimify_part(nifile, ofile, 1) |
|---|
| 400 | 0 | line = nifile.peek |
|---|
| 401 | 0 | if not line: |
|---|
| 402 | n/a | # premature end of file |
|---|
| 403 | 0 | break |
|---|
| 404 | 0 | ofile.write(line) |
|---|
| 405 | 0 | continue |
|---|
| 406 | n/a | # unexpectedly no multipart separator--copy rest of file |
|---|
| 407 | 0 | while 1: |
|---|
| 408 | 0 | line = ifile.readline() |
|---|
| 409 | 0 | if not line: |
|---|
| 410 | 0 | return |
|---|
| 411 | 0 | if must_quote_body: |
|---|
| 412 | 0 | line = mime_encode(line, 0) |
|---|
| 413 | 0 | ofile.write(line) |
|---|
| 414 | n/a | |
|---|
| 415 | 1 | def mimify(infile, outfile): |
|---|
| 416 | n/a | """Convert 8bit parts of a MIME mail message to quoted-printable.""" |
|---|
| 417 | 0 | if type(infile) == type(''): |
|---|
| 418 | 0 | ifile = open(infile) |
|---|
| 419 | 0 | if type(outfile) == type('') and infile == outfile: |
|---|
| 420 | 0 | import os |
|---|
| 421 | 0 | d, f = os.path.split(infile) |
|---|
| 422 | 0 | os.rename(infile, os.path.join(d, ',' + f)) |
|---|
| 423 | n/a | else: |
|---|
| 424 | 0 | ifile = infile |
|---|
| 425 | 0 | if type(outfile) == type(''): |
|---|
| 426 | 0 | ofile = open(outfile, 'w') |
|---|
| 427 | n/a | else: |
|---|
| 428 | 0 | ofile = outfile |
|---|
| 429 | 0 | nifile = File(ifile, None) |
|---|
| 430 | 0 | mimify_part(nifile, ofile, 0) |
|---|
| 431 | 0 | ofile.flush() |
|---|
| 432 | n/a | |
|---|
| 433 | 1 | import sys |
|---|
| 434 | 1 | if __name__ == '__main__' or (len(sys.argv) > 0 and sys.argv[0] == 'mimify'): |
|---|
| 435 | 0 | import getopt |
|---|
| 436 | 0 | usage = 'Usage: mimify [-l len] -[ed] [infile [outfile]]' |
|---|
| 437 | n/a | |
|---|
| 438 | 0 | decode_base64 = 0 |
|---|
| 439 | 0 | opts, args = getopt.getopt(sys.argv[1:], 'l:edb') |
|---|
| 440 | 0 | if len(args) not in (0, 1, 2): |
|---|
| 441 | 0 | print usage |
|---|
| 442 | 0 | sys.exit(1) |
|---|
| 443 | 0 | if (('-e', '') in opts) == (('-d', '') in opts) or \ |
|---|
| 444 | 0 | ((('-b', '') in opts) and (('-d', '') not in opts)): |
|---|
| 445 | 0 | print usage |
|---|
| 446 | 0 | sys.exit(1) |
|---|
| 447 | 0 | for o, a in opts: |
|---|
| 448 | 0 | if o == '-e': |
|---|
| 449 | 0 | encode = mimify |
|---|
| 450 | 0 | elif o == '-d': |
|---|
| 451 | 0 | encode = unmimify |
|---|
| 452 | 0 | elif o == '-l': |
|---|
| 453 | 0 | try: |
|---|
| 454 | 0 | MAXLEN = int(a) |
|---|
| 455 | 0 | except (ValueError, OverflowError): |
|---|
| 456 | 0 | print usage |
|---|
| 457 | 0 | sys.exit(1) |
|---|
| 458 | 0 | elif o == '-b': |
|---|
| 459 | 0 | decode_base64 = 1 |
|---|
| 460 | 0 | if len(args) == 0: |
|---|
| 461 | 0 | encode_args = (sys.stdin, sys.stdout) |
|---|
| 462 | 0 | elif len(args) == 1: |
|---|
| 463 | 0 | encode_args = (args[0], sys.stdout) |
|---|
| 464 | n/a | else: |
|---|
| 465 | 0 | encode_args = (args[0], args[1]) |
|---|
| 466 | 0 | if decode_base64: |
|---|
| 467 | 0 | encode_args = encode_args + (decode_base64,) |
|---|
| 468 | 0 | encode(*encode_args) |
|---|