2727QUOTE = '> ' # string replies are quoted with
2828# End configure
2929
30- import regex , regsub , string
31-
32- qp = regex .compile ('^content-transfer-encoding:[ \t ]*quoted-printable' ,
33- regex .casefold )
34- base64_re = regex .compile ('^content-transfer-encoding:[ \t ]*base64' ,
35- regex .casefold )
36- mp = regex .compile ('^content-type:[\000 -\377 ]*multipart/[\000 -\377 ]*boundary="?\\ ([^;"\n ]*\\ )' ,
37- regex .casefold )
38- chrset = regex .compile ('^\\ (content-type:.*charset="\\ )\\ (us-ascii\\ |iso-8859-[0-9]+\\ )\\ ("[\000 -\377 ]*\\ )' ,
39- regex .casefold )
40- he = regex .compile ('^-*$' )
41- mime_code = regex .compile ('=\\ ([0-9a-f][0-9a-f]\\ )' , regex .casefold )
42- mime_head = regex .compile ('=\\ ?iso-8859-1\\ ?q\\ ?\\ ([^? \t \n ]+\\ )\\ ?=' ,
43- regex .casefold )
44- repl = regex .compile ('^subject:[ \t ]+re: ' , regex .casefold )
30+ import re , string
31+
32+ qp = re .compile ('^content-transfer-encoding:\\ s*quoted-printable' , re .I )
33+ base64_re = re .compile ('^content-transfer-encoding:\\ s*base64' , re .I )
34+ mp = re .compile ('^content-type:.*multipart/.*boundary="?([^;"\n ]*)' , re .I | re .S )
35+ chrset = re .compile ('^(content-type:.*charset=")(us-ascii|iso-8859-[0-9]+)(".*)' , re .I | re .S )
36+ he = re .compile ('^-*\n ' )
37+ mime_code = re .compile ('=([0-9a-f][0-9a-f])' , re .I )
38+ mime_head = re .compile ('=\\ ?iso-8859-1\\ ?q\\ ?([^? \t \n ]+)\\ ?=' , re .I )
39+ repl = re .compile ('^subject:\\ s+re: ' , re .I )
4540
4641class File :
4742 '''A simple fake file object that knows about limited
@@ -81,7 +76,7 @@ def readline(self):
8176 line = self .file .readline ()
8277 if not line :
8378 return line
84- if he .match (line ) >= 0 :
79+ if he .match (line ):
8580 return line
8681 while 1 :
8782 self .peek = self .file .readline ()
@@ -95,26 +90,26 @@ def mime_decode(line):
9590 '''Decode a single line of quoted-printable text to 8bit.'''
9691 newline = ''
9792 while 1 :
98- i = mime_code .search (line )
99- if i < 0 :
93+ res = mime_code .search (line )
94+ if res is None :
10095 break
101- newline = newline + line [:i ] + \
102- chr (string .atoi (mime_code .group (1 ), 16 ))
103- line = line [i + 3 :]
96+ newline = newline + line [:res . start ( 0 ) ] + \
97+ chr (string .atoi (res .group (1 ), 16 ))
98+ line = line [res . end ( 0 ) :]
10499 return newline + line
105100
106101def mime_decode_header (line ):
107102 '''Decode a header line to 8bit.'''
108103 newline = ''
109104 while 1 :
110- i = mime_head .search (line )
111- if i < 0 :
105+ res = mime_head .search (line )
106+ if res is None :
112107 break
113- match0 , match1 = mime_head .group (0 , 1 )
108+ match = res .group (1 )
114109 # convert underscores to spaces (before =XX conversion!)
115- match1 = string .join (string .split (match1 , '_' ), ' ' )
116- newline = newline + line [:i ] + mime_decode (match1 )
117- line = line [i + len ( match0 ):]
110+ match = string .join (string .split (match , '_' ), ' ' )
111+ newline = newline + line [:res . start ( 0 ) ] + mime_decode (match )
112+ line = line [res . end ( 0 ):]
118113 return newline + line
119114
120115def unmimify_part (ifile , ofile , decode_base64 = 0 ):
@@ -140,19 +135,20 @@ def unmimify_part(ifile, ofile, decode_base64 = 0):
140135 else :
141136 pref = ''
142137 line = mime_decode_header (line )
143- if qp .match (line ) >= 0 :
138+ if qp .match (line ):
144139 quoted_printable = 1
145140 continue # skip this header
146- if decode_base64 and base64_re .match (line ) >= 0 :
141+ if decode_base64 and base64_re .match (line ):
147142 is_base64 = 1
148143 continue
149144 ofile .write (pref + line )
150- if not prefix and repl .match (line ) >= 0 :
145+ if not prefix and repl .match (line ):
151146 # we're dealing with a reply message
152147 is_repl = 1
153- if mp .match (line ) >= 0 :
154- multipart = '--' + mp .group (1 )
155- if he .match (line ) >= 0 :
148+ mp_res = mp .match (line )
149+ if mp_res :
150+ multipart = '--' + mp_res .group (1 )
151+ if he .match (line ):
156152 break
157153 if is_repl and (quoted_printable or multipart ):
158154 is_repl = 0
@@ -162,7 +158,7 @@ def unmimify_part(ifile, ofile, decode_base64 = 0):
162158 line = ifile .readline ()
163159 if not line :
164160 return
165- line = regsub . gsub (mime_head , '\\ 1' , line )
161+ line = re . sub (mime_head , '\\ 1' , line )
166162 if prefix and line [:len (prefix )] == prefix :
167163 line = line [len (prefix ):]
168164 pref = prefix
@@ -216,8 +212,8 @@ def unmimify(infile, outfile, decode_base64 = 0):
216212 unmimify_part (nifile , ofile , decode_base64 )
217213 ofile .flush ()
218214
219- mime_char = regex .compile ('[=\240 -\377 ]' ) # quote these chars in body
220- mime_header_char = regex .compile ('[=?\240 -\377 ]' ) # quote these in header
215+ mime_char = re .compile ('[=\240 -\377 ]' ) # quote these chars in body
216+ mime_header_char = re .compile ('[=?\240 -\377 ]' ) # quote these in header
221217
222218def mime_encode (line , header ):
223219 '''Code a single line as quoted-printable.
@@ -232,12 +228,12 @@ def mime_encode(line, header):
232228 newline = string .upper ('=%02x' % ord ('F' ))
233229 line = line [1 :]
234230 while 1 :
235- i = reg .search (line )
236- if i < 0 :
231+ res = reg .search (line )
232+ if res is None :
237233 break
238- newline = newline + line [:i ] + \
239- string .upper ('=%02x' % ord (line [i ]))
240- line = line [i + 1 :]
234+ newline = newline + line [:res . start ( 0 ) ] + \
235+ string .upper ('=%02x' % ord (line [res . group ( 0 ) ]))
236+ line = line [res . end ( 0 ) :]
241237 line = newline + line
242238
243239 newline = ''
@@ -250,25 +246,25 @@ def mime_encode(line, header):
250246 line = line [i :]
251247 return newline + line
252248
253- mime_header = regex .compile ('\\ ([ \t (]\\ |^ \\ ) \\ ([-a-zA-Z0-9_+]*[\240 -\377 ][-a-zA-Z0-9_+\240 -\377 ]*\\ ) \\ ([ \t )]\\ |$ \\ )' )
249+ mime_header = re .compile ('([ \t (]|^) ([-a-zA-Z0-9_+]*[\240 -\377 ][-a-zA-Z0-9_+\240 -\377 ]*) ([ \t )]| \n )' )
254250
255251def mime_encode_header (line ):
256252 '''Code a single header line as quoted-printable.'''
257253 newline = ''
258254 while 1 :
259- i = mime_header .search (line )
260- if i < 0 :
255+ res = mime_header .search (line )
256+ if res is None :
261257 break
262- newline = newline + line [:i ] + mime_header .group (1 ) + \
258+ newline = newline + line [:res . start ( 0 ) ] + res .group (1 ) + \
263259 '=?' + CHARSET + '?Q?' + \
264- mime_encode (mime_header .group (2 ), 1 ) + \
265- '?=' + mime_header .group (3 )
266- line = line [i + len ( mime_header . group ( 0 ) ):]
260+ mime_encode (res .group (2 ), 1 ) + \
261+ '?=' + res .group (3 )
262+ line = line [res . end ( 0 ):]
267263 return newline + line
268264
269- mv = regex .compile ('^mime-version:' , regex . casefold )
270- cte = regex .compile ('^content-transfer-encoding:' , regex . casefold )
271- iso_char = regex .compile ('[\240 -\377 ]' )
265+ mv = re .compile ('^mime-version:' , re . I )
266+ cte = re .compile ('^content-transfer-encoding:' , re . I )
267+ iso_char = re .compile ('[\240 -\377 ]' )
272268
273269def mimify_part (ifile , ofile , is_mime ):
274270 '''Convert an 8bit part of a MIME mail message to quoted-printable.'''
@@ -286,19 +282,20 @@ def mimify_part(ifile, ofile, is_mime):
286282 line = hfile .readline ()
287283 if not line :
288284 break
289- if not must_quote_header and iso_char .search (line ) >= 0 :
285+ if not must_quote_header and iso_char .search (line ):
290286 must_quote_header = 1
291- if mv .match (line ) >= 0 :
287+ if mv .match (line ):
292288 is_mime = 1
293- if cte .match (line ) >= 0 :
289+ if cte .match (line ):
294290 has_cte = 1
295- if qp .match (line ) >= 0 :
291+ if qp .match (line ):
296292 is_qp = 1
297- elif base64_re .match (line ) >= 0 :
293+ elif base64_re .match (line ):
298294 is_base64 = 1
299- if mp .match (line ) >= 0 :
300- multipart = '--' + mp .group (1 )
301- if he .match (line ) >= 0 :
295+ mp_res = mp .match (line )
296+ if mp_res :
297+ multipart = '--' + mp_res .group (1 )
298+ if he .match (line ):
302299 header_end = line
303300 break
304301 header .append (line )
@@ -328,7 +325,7 @@ def mimify_part(ifile, ofile, is_mime):
328325 line = mime_decode (line )
329326 message .append (line )
330327 if not has_iso_chars :
331- if iso_char .search (line ) >= 0 :
328+ if iso_char .search (line ):
332329 has_iso_chars = must_quote_body = 1
333330 if not must_quote_body :
334331 if len (line ) > MAXLEN :
@@ -338,16 +335,17 @@ def mimify_part(ifile, ofile, is_mime):
338335 for line in header :
339336 if must_quote_header :
340337 line = mime_encode_header (line )
341- if chrset .match (line ) >= 0 :
338+ chrset_res = chrset .match (line )
339+ if chrset_res :
342340 if has_iso_chars :
343341 # change us-ascii into iso-8859-1
344- if string .lower (chrset .group (2 )) == 'us-ascii' :
345- line = chrset .group (1 ) + \
346- CHARSET + chrset .group (3 )
342+ if string .lower (chrset_res .group (2 )) == 'us-ascii' :
343+ line = chrset_res .group (1 ) + \
344+ CHARSET + chrset_res .group (3 )
347345 else :
348346 # change iso-8859-* into us-ascii
349- line = chrset .group (1 ) + 'us-ascii' + chrset .group (3 )
350- if has_cte and cte .match (line ) >= 0 :
347+ line = chrset_res .group (1 ) + 'us-ascii' + chrset_res .group (3 )
348+ if has_cte and cte .match (line ):
351349 line = 'Content-Transfer-Encoding: '
352350 if is_base64 :
353351 line = line + 'base64\n '
@@ -445,3 +443,4 @@ def mimify(infile, outfile):
445443 if decode_base64 :
446444 encode_args = encode_args + (decode_base64 ,)
447445 apply (encode , encode_args )
446+
0 commit comments