- 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
-
- charset = l[1].lower()
- encoding = l[2].lower()
- s = l[3]
-
- if '*' in charset:
- charset, language = charset.split('*', 1) # language ignored
-
- infile = StringIO(s)
- outfile = StringIO()
-
- if encoding == "b":
- from base64 import decode
- elif encoding == "q":
- from quopri import decode
- else:
- raise ValueError, "wrong encoding `%s' (expected 'b' or 'q')" % encoding
-
- decode(infile, outfile)
- s = outfile.getvalue()
-
- if charset == GlobalOptions.default_charset:
- new_parts.append(s) # do not recode
- continue
-
- s = recode(s, charset)
- new_parts.append(s)
-
- if new_parts and new_parts[0] == ' ':
- del new_parts[0]
- return ''.join(new_parts)
-
-
-def decode_rfc2231(s):
- "Decode string according to rfc2231"
-
- charset, language, s = s.split("'", 2) # language ignored
-
- i = 0
- result = []
-
- while i < len(s):
- c = s[i]
- if c == '%': # hex
- i += 1
- c = chr(int(s[i:i+2], 16))
- i += 1
- result.append(c)
- i += 1
-
- s = ''.join(result)
- s = recode2(s, charset)
- return s
+ new_value = _decode_header(value)
+ if new_value <> value: # do not bother to touch msg if not changed
+ msg.set_param(param, new_value, header)