New version from Sjoerd, small bugfix + optimizations.

This commit is contained in:
Guido van Rossum 1997-10-30 15:27:37 +00:00
parent e475d86bd9
commit 613418aa09
1 changed files with 28 additions and 24 deletions

View File

@ -89,28 +89,30 @@ class HeaderFile:
def mime_decode(line): def mime_decode(line):
'''Decode a single line of quoted-printable text to 8bit.''' '''Decode a single line of quoted-printable text to 8bit.'''
newline = '' newline = ''
pos = 0
while 1: while 1:
res = mime_code.search(line) res = mime_code.search(line, pos)
if res is None: if res is None:
break break
newline = newline + line[:res.start(0)] + \ newline = newline + line[pos:res.start(0)] + \
chr(string.atoi(res.group(1), 16)) chr(string.atoi(res.group(1), 16))
line = line[res.end(0):] pos = res.end(0)
return newline + line return newline + line[pos:]
def mime_decode_header(line): def mime_decode_header(line):
'''Decode a header line to 8bit.''' '''Decode a header line to 8bit.'''
newline = '' newline = ''
pos = 0
while 1: while 1:
res = mime_head.search(line) res = mime_head.search(line, pos)
if res is None: if res is None:
break break
match = res.group(1) match = res.group(1)
# convert underscores to spaces (before =XX conversion!) # convert underscores to spaces (before =XX conversion!)
match = string.join(string.split(match, '_'), ' ') match = string.join(string.split(match, '_'), ' ')
newline = newline + line[:res.start(0)] + mime_decode(match) newline = newline + line[pos:res.start(0)] + mime_decode(match)
line = line[res.end(0):] pos = res.end(0)
return newline + line return newline + line[pos:]
def unmimify_part(ifile, ofile, decode_base64 = 0): def unmimify_part(ifile, ofile, decode_base64 = 0):
'''Convert a quoted-printable part of a MIME mail message to 8bit.''' '''Convert a quoted-printable part of a MIME mail message to 8bit.'''
@ -223,18 +225,19 @@ def mime_encode(line, header):
else: else:
reg = mime_char reg = mime_char
newline = '' newline = ''
pos = 0
if len(line) >= 5 and line[:5] == 'From ': if len(line) >= 5 and line[:5] == 'From ':
# quote 'From ' at the start of a line for stupid mailers # quote 'From ' at the start of a line for stupid mailers
newline = string.upper('=%02x' % ord('F')) newline = string.upper('=%02x' % ord('F'))
line = line[1:] pos = 1
while 1: while 1:
res = reg.search(line) res = reg.search(line, pos)
if res is None: if res is None:
break break
newline = newline + line[:res.start(0)] + \ newline = newline + line[pos:res.start(0)] + \
string.upper('=%02x' % ord(line[res.group(0)])) string.upper('=%02x' % ord(res.group(0)))
line = line[res.end(0):] pos = res.end(0)
line = newline + line line = newline + line[pos:]
newline = '' newline = ''
while len(line) >= 75: while len(line) >= 75:
@ -251,16 +254,16 @@ mime_header = re.compile('([ \t(]|^)([-a-zA-Z0-9_+]*[\240-\377][-a-zA-Z0-9_+\240
def mime_encode_header(line): def mime_encode_header(line):
'''Code a single header line as quoted-printable.''' '''Code a single header line as quoted-printable.'''
newline = '' newline = ''
pos = 0
while 1: while 1:
res = mime_header.search(line) res = mime_header.search(line, pos)
if res is None: if res is None:
break break
newline = newline + line[:res.start(0)] + res.group(1) + \ newline = '%s%s%s=?%s?Q?%s?=%s' % \
'=?' + CHARSET + '?Q?' + \ (newline, line[pos:res.start(0)], res.group(1),
mime_encode(res.group(2), 1) + \ CHARSET, mime_encode(res.group(2), 1), res.group(3))
'?=' + res.group(3) pos = res.end(0)
line = line[res.end(0):] return newline + line[pos:]
return newline + line
mv = re.compile('^mime-version:', re.I) mv = re.compile('^mime-version:', re.I)
cte = re.compile('^content-transfer-encoding:', re.I) cte = re.compile('^content-transfer-encoding:', re.I)
@ -340,11 +343,12 @@ def mimify_part(ifile, ofile, is_mime):
if has_iso_chars: if has_iso_chars:
# change us-ascii into iso-8859-1 # change us-ascii into iso-8859-1
if string.lower(chrset_res.group(2)) == 'us-ascii': if string.lower(chrset_res.group(2)) == 'us-ascii':
line = chrset_res.group(1) + \ line = '%s%s%s' % (chrset_res.group(1),
CHARSET + chrset_res.group(3) CHARSET,
chrset_res.group(3))
else: else:
# change iso-8859-* into us-ascii # change iso-8859-* into us-ascii
line = chrset_res.group(1) + 'us-ascii' + chrset_res.group(3) line = '%sus-ascii%s' % chrset_res.group(1, 3)
if has_cte and cte.match(line): if has_cte and cte.match(line):
line = 'Content-Transfer-Encoding: ' line = 'Content-Transfer-Encoding: '
if is_base64: if is_base64: