Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 613418a

Browse files
committed
New version from Sjoerd, small bugfix + optimizations.
1 parent e475d86 commit 613418a

1 file changed

Lines changed: 28 additions & 24 deletions

File tree

Lib/mimify.py

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -89,28 +89,30 @@ def readline(self):
8989
def mime_decode(line):
9090
'''Decode a single line of quoted-printable text to 8bit.'''
9191
newline = ''
92+
pos = 0
9293
while 1:
93-
res = mime_code.search(line)
94+
res = mime_code.search(line, pos)
9495
if res is None:
9596
break
96-
newline = newline + line[:res.start(0)] + \
97+
newline = newline + line[pos:res.start(0)] + \
9798
chr(string.atoi(res.group(1), 16))
98-
line = line[res.end(0):]
99-
return newline + line
99+
pos = res.end(0)
100+
return newline + line[pos:]
100101

101102
def mime_decode_header(line):
102103
'''Decode a header line to 8bit.'''
103104
newline = ''
105+
pos = 0
104106
while 1:
105-
res = mime_head.search(line)
107+
res = mime_head.search(line, pos)
106108
if res is None:
107109
break
108110
match = res.group(1)
109111
# convert underscores to spaces (before =XX conversion!)
110112
match = string.join(string.split(match, '_'), ' ')
111-
newline = newline + line[:res.start(0)] + mime_decode(match)
112-
line = line[res.end(0):]
113-
return newline + line
113+
newline = newline + line[pos:res.start(0)] + mime_decode(match)
114+
pos = res.end(0)
115+
return newline + line[pos:]
114116

115117
def unmimify_part(ifile, ofile, decode_base64 = 0):
116118
'''Convert a quoted-printable part of a MIME mail message to 8bit.'''
@@ -223,18 +225,19 @@ def mime_encode(line, header):
223225
else:
224226
reg = mime_char
225227
newline = ''
228+
pos = 0
226229
if len(line) >= 5 and line[:5] == 'From ':
227230
# quote 'From ' at the start of a line for stupid mailers
228231
newline = string.upper('=%02x' % ord('F'))
229-
line = line[1:]
232+
pos = 1
230233
while 1:
231-
res = reg.search(line)
234+
res = reg.search(line, pos)
232235
if res is None:
233236
break
234-
newline = newline + line[:res.start(0)] + \
235-
string.upper('=%02x' % ord(line[res.group(0)]))
236-
line = line[res.end(0):]
237-
line = newline + line
237+
newline = newline + line[pos:res.start(0)] + \
238+
string.upper('=%02x' % ord(res.group(0)))
239+
pos = res.end(0)
240+
line = newline + line[pos:]
238241

239242
newline = ''
240243
while len(line) >= 75:
@@ -251,16 +254,16 @@ def mime_encode(line, header):
251254
def mime_encode_header(line):
252255
'''Code a single header line as quoted-printable.'''
253256
newline = ''
257+
pos = 0
254258
while 1:
255-
res = mime_header.search(line)
259+
res = mime_header.search(line, pos)
256260
if res is None:
257261
break
258-
newline = newline + line[:res.start(0)] + res.group(1) + \
259-
'=?' + CHARSET + '?Q?' + \
260-
mime_encode(res.group(2), 1) + \
261-
'?=' + res.group(3)
262-
line = line[res.end(0):]
263-
return newline + line
262+
newline = '%s%s%s=?%s?Q?%s?=%s' % \
263+
(newline, line[pos:res.start(0)], res.group(1),
264+
CHARSET, mime_encode(res.group(2), 1), res.group(3))
265+
pos = res.end(0)
266+
return newline + line[pos:]
264267

265268
mv = re.compile('^mime-version:', re.I)
266269
cte = re.compile('^content-transfer-encoding:', re.I)
@@ -340,11 +343,12 @@ def mimify_part(ifile, ofile, is_mime):
340343
if has_iso_chars:
341344
# change us-ascii into iso-8859-1
342345
if string.lower(chrset_res.group(2)) == 'us-ascii':
343-
line = chrset_res.group(1) + \
344-
CHARSET + chrset_res.group(3)
346+
line = '%s%s%s' % (chrset_res.group(1),
347+
CHARSET,
348+
chrset_res.group(3))
345349
else:
346350
# change iso-8859-* into us-ascii
347-
line = chrset_res.group(1) + 'us-ascii' + chrset_res.group(3)
351+
line = '%sus-ascii%s' % chrset_res.group(1, 3)
348352
if has_cte and cte.match(line):
349353
line = 'Content-Transfer-Encoding: '
350354
if is_base64:

0 commit comments

Comments
 (0)