- "Decode mail header (if exists) and put it back, if it was encoded"
-
- if msg.has_key(header):
- value = msg[header]
- new_value = decode_rfc2047(value)
- if value <> new_value: # do not bother to touch msg if not changed
- msg[header] = new_value
-
-
-def decode_header_param(msg, header, param):
- "Decode mail header's parameter (if exists) and put it back, if it was encoded"
-
- if msg.has_key(header):
- new_value, pstr, rfc2231_encoded = getparam(msg, header, param)
- if pstr is not None:
- if rfc2231_encoded:
- new_str = decode_rfc2231(pstr)
- else:
- new_str = decode_rfc2047(pstr)
- if pstr <> new_str: # do not bother to touch msg if not changed
- msg[header] = "%s; %s=\"%s\"" % (new_value, param, new_str)
-
-
-def decode_rfc2047(s):
- "Decode string according to rfc2047"
-
- parts = s.split() # by whitespaces
- new_parts = []
- got_encoded = 0
-
- for s in parts:
- l = s.split('?')
-
- if l[0] <> '=' or l[4] <> '=': # assert correct format
- new_parts.append(' ')
- new_parts.append(s) # if not encoded - just put it into output
- got_encoded = 0
- continue
-
- if not got_encoded:
- new_parts.append(' ') # no space between encoded parts, one space otherwise
- got_encoded = 1