(ifile, ofile, is_mime)
Convert an 8bit part of a MIME mail message to quoted-printable.
Convert an 8bit part of a MIME mail message to quoted-printable.
Convert an 8bit part of a MIME mail message to quoted - printable .
def mimify_part(ifile, ofile, is_mime):
    """Convert an 8bit part of a MIME mail message to quoted-printable."""
    has_cte = is_qp = is_base64 = 0
    multipart = None
    must_quote_body = must_quote_header = has_iso_chars = 0

    header = []
    header_end = ''
    message = []
    message_end = ''
    # read header
    hfile = HeaderFile(ifile)
    while 1:
        line = hfile.readline()
        if not line:
            break
        if not must_quote_header and iso_char.search(line):
            must_quote_header = 1
        if mv.match(line):
            is_mime = 1
        if cte.match(line):
            has_cte = 1
            if qp.match(line):
                is_qp = 1
            elif base64_re.match(line):
                is_base64 = 1
        mp_res = mp.match(line)
        if mp_res:
            multipart = '--' + mp_res.group(1)
        if he.match(line):
            header_end = line
            break
        header.append(line)

    # read body
    while 1:
        line = ifile.readline()
        if not line:
            break
        if multipart:
            if line == multipart + '--\n':
                message_end = line
                break
            if line == multipart + '\n':
                message_end = line
                break
        if is_base64:
            message.append(line)
            continue
        if is_qp:
            while line[-2:] == '=\n':
                line = line[:-2]
                newline = ifile.readline()
                if newline[:len(QUOTE)] == QUOTE:
                    newline = newline[len(QUOTE):]
                line = line + newline
            line = mime_decode(line)
        message.append(line)
        if not has_iso_chars:
            if iso_char.search(line):
                has_iso_chars = must_quote_body = 1
        if not must_quote_body:
            if len(line) > MAXLEN:
                must_quote_body = 1

    # convert and output header and body
    for line in header:
        if must_quote_header:
            line = mime_encode_header(line)
        chrset_res = chrset.match(line)
        if chrset_res:
            if has_iso_chars:
                # change us-ascii into iso-8859-1
                if chrset_res.group(2).lower() == 'us-ascii':
                    line = '%s%s%s' % (chrset_res.group(1),
                                       CHARSET,
                                       chrset_res.group(3))
            else:
                # change iso-8859-* into us-ascii
                line = '%sus-ascii%s' % chrset_res.group(1, 3)
        if has_cte and cte.match(line):
            line = 'Content-Transfer-Encoding: '
            if is_base64:
                line = line + 'base64\n'
            elif must_quote_body:
                line = line + 'quoted-printable\n'
            else:
                line = line + '7bit\n'
        ofile.write(line)
    if (must_quote_header or must_quote_body) and not is_mime:
        ofile.write('Mime-Version: 1.0\n')
        ofile.write('Content-Type: text/plain; ')
        if has_iso_chars:
            ofile.write('charset="%s"\n' % CHARSET)
        else:
            ofile.write('charset="us-ascii"\n')
    if must_quote_body and not has_cte:
        ofile.write('Content-Transfer-Encoding: quoted-printable\n')
    ofile.write(header_end)

    for line in message:
        if must_quote_body:
            line = mime_encode(line, 0)
        ofile.write(line)
    ofile.write(message_end)

    line = message_end
    while multipart:
        if line == multipart + '--\n':
            # read bit after the end of the last part
            while 1:
                line = ifile.readline()
                if not line:
                    return
                if must_quote_body:
                    line = mime_encode(line, 0)
                ofile.write(line)
        if line == multipart + '\n':
            nifile = File(ifile, multipart)
            mimify_part(nifile, ofile, 1)
            line = nifile.peek
            if not line:
                # premature end of file
                break
            ofile.write(line)
            continue
        # unexpectedly no multipart separator--copy rest of file
        while 1:
            line = ifile.readline()
            if not line:
                return
            if must_quote_body:
                line = mime_encode(line, 0)
            ofile.write(line)
def mimify_part ( ifile , ofile , is_mime ) : has_cte = is_qp = is_base64 = 0 multipart = None must_quote_body = must_quote_header = has_iso_chars = 0 header = [ ] header_end = '' message = [ ] message_end = '' # read header hfile = HeaderFile ( ifile ) while 1 : line = hfile . readline ( ) if not line : break if not must_quote_header and iso_char . search ( line ) : must_quote_header = 1 if mv . match ( line ) : is_mime = 1 if cte . match ( line ) : has_cte = 1 if qp . match ( line ) : is_qp = 1 elif base64_re . match ( line ) : is_base64 = 1 mp_res = mp . match ( line ) if mp_res : multipart = '--' + mp_res . group ( 1 ) if he . match ( line ) : header_end = line break header . append ( line ) # read body while 1 : line = ifile . readline ( ) if not line : break if multipart : if line == multipart + '--\n' : message_end = line break if line == multipart + '\n' : message_end = line break if is_base64 : message . append ( line ) continue if is_qp : while line [ - 2 : ] == '=\n' : line = line [ : - 2 ] newline = ifile . readline ( ) if newline [ : len ( QUOTE ) ] == QUOTE : newline = newline [ len ( QUOTE ) : ] line = line + newline line = mime_decode ( line ) message . append ( line ) if not has_iso_chars : if iso_char . search ( line ) : has_iso_chars = must_quote_body = 1 if not must_quote_body : if len ( line ) > MAXLEN : must_quote_body = 1 # convert and output header and body for line in header : if must_quote_header : line = mime_encode_header ( line ) chrset_res = chrset . match ( line ) if chrset_res : if has_iso_chars : # change us-ascii into iso-8859-1 if chrset_res . group ( 2 ) . lower ( ) == 'us-ascii' : line = '%s%s%s' % ( chrset_res . group ( 1 ) , CHARSET , chrset_res . group ( 3 ) ) else : # change iso-8859-* into us-ascii line = '%sus-ascii%s' % chrset_res . group ( 1 , 3 ) if has_cte and cte . match ( line ) : line = 'Content-Transfer-Encoding: ' if is_base64 : line = line + 'base64\n' elif must_quote_body : line = line + 'quoted-printable\n' else : line = line + '7bit\n' ofile . write ( line ) if ( must_quote_header or must_quote_body ) and not is_mime : ofile . write ( 'Mime-Version: 1.0\n' ) ofile . write ( 'Content-Type: text/plain; ' ) if has_iso_chars : ofile . write ( 'charset="%s"\n' % CHARSET ) else : ofile . write ( 'charset="us-ascii"\n' ) if must_quote_body and not has_cte : ofile . write ( 'Content-Transfer-Encoding: quoted-printable\n' ) ofile . write ( header_end ) for line in message : if must_quote_body : line = mime_encode ( line , 0 ) ofile . write ( line ) ofile . write ( message_end ) line = message_end while multipart : if line == multipart + '--\n' : # read bit after the end of the last part while 1 : line = ifile . readline ( ) if not line : return if must_quote_body : line = mime_encode ( line , 0 ) ofile . write ( line ) if line == multipart + '\n' : nifile = File ( ifile , multipart ) mimify_part ( nifile , ofile , 1 ) line = nifile . peek if not line : # premature end of file break ofile . write ( line ) continue # unexpectedly no multipart separator--copy rest of file while 1 : line = ifile . readline ( ) if not line : return if must_quote_body : line = mime_encode ( line , 0 ) ofile . write ( line )