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