annotate Lib/mimify.py @ 4566:6403c94de50d legacy-trunk

Added safeguard against failure in __del__.
author Guido van Rossum <guido@python.org>
date Wed, 27 Nov 1996 19:39:58 +0000
parents 35be5df6ef77
children 73758db2f464
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3389
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1 #!/usr/local/bin/python
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
35be5df6ef77 small fix by Sjoerd
Guido van Rossum <guido@python.org>
parents: 3618
diff changeset
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)
3658
35be5df6ef77 small fix by Sjoerd
Guido van Rossum <guido@python.org>
parents: 3618
diff changeset
34 base64 = regex.compile('^content-transfer-encoding:[ \t]*base64',
35be5df6ef77 small fix by Sjoerd
Guido van Rossum <guido@python.org>
parents: 3618
diff changeset
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
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
118 def unmimify_part(ifile, ofile):
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
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
122 is_repl = 0
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
123 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
124 prefix = QUOTE
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
125 else:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
126 prefix = ''
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
127
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
128 # read header
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
129 hfile = HeaderFile(ifile)
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
130 while 1:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
131 line = hfile.readline()
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
132 if not line:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
133 return
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
134 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
135 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
136 pref = prefix
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
137 else:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
138 pref = ''
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
139 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
140 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
141 quoted_printable = 1
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
142 continue # skip this header
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
143 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
144 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
145 # 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
146 is_repl = 1
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
147 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
148 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
149 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
150 break
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
151 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
152 is_repl = 0
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
153
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
154 # read body
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
155 while 1:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
156 line = ifile.readline()
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
157 if not line:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
158 return
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
159 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
160 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
161 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
162 pref = prefix
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
163 else:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
164 pref = ''
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
165 ## 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
166 ## multipart = line[:-1]
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
167 while multipart:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
168 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
169 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
170 multipart = None
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
171 line = None
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
172 break
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
173 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
174 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
175 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
176 unmimify_part(nifile, ofile)
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
177 line = nifile.peek
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
178 continue
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
179 # 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
180 break
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
181 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
182 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
183 line = line[:-2]
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
184 newline = ifile.readline()
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
185 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
186 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
187 line = line + newline
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
188 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
189 if line:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
190 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
191
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
192 def unmimify(infile, outfile):
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
193 '''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
194 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
195 ifile = open(infile)
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
196 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
197 import os
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
198 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
199 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
200 else:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
201 ifile = infile
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
202 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
203 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
204 else:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
205 ofile = outfile
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
206 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
207 unmimify_part(nifile, ofile)
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
208 ofile.flush()
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
209
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
210 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
211 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
212
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
213 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
214 '''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
215 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
216 if header:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
217 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
218 else:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
219 reg = mime_char
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
220 newline = ''
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
221 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
222 # 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
223 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
224 line = line[1:]
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
225 while 1:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
226 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
227 if i < 0:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
228 break
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
229 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
230 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
231 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
232 line = newline + line
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
233
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
234 newline = ''
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
235 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
236 i = 73
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
237 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
238 i = i - 1
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
239 i = i + 1
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
240 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
241 line = line[i:]
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
242 return newline + line
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
243
3618
a3c97e205847 minor fix to mime header regex --Sjoerd
Guido van Rossum <guido@python.org>
parents: 3389
diff changeset
244 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
245
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
246 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
247 '''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
248 newline = ''
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
249 while 1:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
250 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
251 if i < 0:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
252 break
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
253 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
254 '=?' + CHARSET + '?Q?' + \
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
255 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
256 '?=' + 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
257 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
258 return newline + line
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
259
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
260 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
261 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
262 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
263
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
264 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
265 '''Convert an 8bit part of a MIME mail message to quoted-printable.'''
3658
35be5df6ef77 small fix by Sjoerd
Guido van Rossum <guido@python.org>
parents: 3618
diff changeset
266 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
267 multipart = None
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
268 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
269
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
270 header = []
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
271 header_end = ''
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
272 message = []
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
273 message_end = ''
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
274 # read header
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
275 hfile = HeaderFile(ifile)
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
276 while 1:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
277 line = hfile.readline()
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
278 if not line:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
279 break
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
280 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
281 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
282 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
283 is_mime = 1
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
284 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
285 has_cte = 1
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
286 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
287 is_qp = 1
3658
35be5df6ef77 small fix by Sjoerd
Guido van Rossum <guido@python.org>
parents: 3618
diff changeset
288 elif base64.match(line) >= 0:
35be5df6ef77 small fix by Sjoerd
Guido van Rossum <guido@python.org>
parents: 3618
diff changeset
289 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
290 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
291 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
292 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
293 header_end = line
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
294 break
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
295 header.append(line)
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
296
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
297 # read body
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
298 while 1:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
299 line = ifile.readline()
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
300 if not 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 if multipart:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
303 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
304 message_end = line
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
305 break
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
306 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
307 message_end = line
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
308 break
3658
35be5df6ef77 small fix by Sjoerd
Guido van Rossum <guido@python.org>
parents: 3618
diff changeset
309 if is_base64:
35be5df6ef77 small fix by Sjoerd
Guido van Rossum <guido@python.org>
parents: 3618
diff changeset
310 message.append(line)
35be5df6ef77 small fix by Sjoerd
Guido van Rossum <guido@python.org>
parents: 3618
diff changeset
311 continue
3389
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
312 if is_qp:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
313 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
314 line = line[:-2]
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
315 newline = ifile.readline()
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
316 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
317 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
318 line = line + newline
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
319 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
320 message.append(line)
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
321 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
322 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
323 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
324 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
325 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
326 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
327
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
328 # 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
329 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
330 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
331 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
332 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
333 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
334 # 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
335 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
336 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
337 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
338 else:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
339 # 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
340 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
341 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
342 line = 'Content-Transfer-Encoding: '
3658
35be5df6ef77 small fix by Sjoerd
Guido van Rossum <guido@python.org>
parents: 3618
diff changeset
343 if is_base64:
35be5df6ef77 small fix by Sjoerd
Guido van Rossum <guido@python.org>
parents: 3618
diff changeset
344 line = line + 'base64\n'
35be5df6ef77 small fix by Sjoerd
Guido van Rossum <guido@python.org>
parents: 3618
diff changeset
345 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
346 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
347 else:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
348 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
349 ofile.write(line)
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
350 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
351 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
352 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
353 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
354 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
355 else:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
356 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
357 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
358 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
359 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
360
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
361 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
362 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
363 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
364 ofile.write(line)
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
365 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
366
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
367 line = message_end
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
368 while multipart:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
369 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
370 return
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
371 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
372 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
373 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
374 line = nifile.peek
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
375 ofile.write(line)
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
376 continue
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
377
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
378 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
379 '''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
380 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
381 ifile = open(infile)
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
382 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
383 import os
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
384 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
385 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
386 else:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
387 ifile = infile
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
388 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
389 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
390 else:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
391 ofile = outfile
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
392 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
393 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
394 ofile.flush()
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
395
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
396 import sys
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
397 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
398 import getopt
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
399 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
400
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
401 opts, args = getopt.getopt(sys.argv[1:], 'l:ed')
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
402 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
403 print usage
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
404 sys.exit(1)
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
405 if (('-e', '') in opts) == (('-d', '') in opts):
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
406 print usage
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
407 sys.exit(1)
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
408 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
409 if o == '-e':
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
410 encode = mimify
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
411 elif o == '-d':
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
412 encode = unmimify
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
413 elif o == '-l':
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
414 try:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
415 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
416 except:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
417 print usage
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
418 sys.exit(1)
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
419 if len(args) == 0:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
420 encode(sys.stdin, sys.stdout)
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
421 elif len(args) == 1:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
422 encode(args[0], sys.stdout)
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
423 else:
d15d29bbc1ce Module to convert a mail message to or from quoted-printable. The
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
424 encode(args[0], args[1])