# -*- coding: utf-8 -*-

import email.quoprimime

def quoted_printable(val):
    try:
        if type(val) is str:
            return email.quoprimime.header_decode(val)
        else:
            return unicode(email.quoprimime.header_decode(str(val)), 'utf-8')

    except Exception, e:
        # We ignore errors here. Most of these originate from a spam
        # report adding a synopsis of a message with broken encodings.
        pass

    return val

from parse_mail import parse_mail

# -*- coding: utf-8 -*-

import email
import re

def header_decode(s):
    def unquote_match(match):
        s = match.group(0)
        return chr(int(s[1:3], 16))

    s = s.replace('_', ' ')
    return re.sub(r'=\w{2}', unquote_match, s)

def quoted_printable(val):
    try:
        if type(val) is str:
            save = header_decode(val)
            val = ' '.join([chunk.decode(encoding or 'ascii', 'replace') for chunk, encoding in
                email.Header.decode_header(val)])

            if len(val) > len(save):
                val = unicode(save, 'utf-8', 'replace')

        else:
            return unicode(email.quoprimime.header_decode(str(val)), 'utf-8', 'replace')

    except Exception, e:
        # We ignore errors here. Most of these originate from a spam
        # report adding a synopsis of a message with broken encodings.
        pass

    return val

from parse_mail import parse_mail

Update header_decode to handle bare and non-bare quoted-printable chars
Update header_decode to handle bare and non-bare quoted-printable chars

Signed-off-by: Chris Lamb <711c73f64afdce07b7e38039a96d2224209e9a6c@chris-lamb.co.uk>
