home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyos2bin.zip / Lib / mimify.py < prev    next >
Text File  |  1997-12-02  |  12KB  |  451 lines

  1. #! /usr/bin/env python
  2.  
  3. '''Mimification and unmimification of mail messages.
  4.  
  5. decode quoted-printable parts of a mail message or encode using
  6. quoted-printable.
  7.  
  8. Usage:
  9.     mimify(input, output)
  10.     unmimify(input, output, decode_base64 = 0)
  11. to encode and decode respectively.  Input and output may be the name
  12. of a file or an open file object.  Only a readline() method is used
  13. on the input file, only a write() method is used on the output file.
  14. When using file names, the input and output file names may be the
  15. same.
  16.  
  17. Interactive usage:
  18.     mimify.py -e [infile [outfile]]
  19.     mimify.py -d [infile [outfile]]
  20. to encode and decode respectively.  Infile defaults to standard
  21. input and outfile to standard output.
  22. '''
  23.  
  24. # Configure
  25. MAXLEN = 200    # if lines longer than this, encode as quoted-printable
  26. CHARSET = 'ISO-8859-1'    # default charset for non-US-ASCII mail
  27. QUOTE = '> '        # string replies are quoted with
  28. # End configure
  29.  
  30. import re, string
  31.  
  32. qp = re.compile('^content-transfer-encoding:\\s*quoted-printable', re.I)
  33. base64_re = re.compile('^content-transfer-encoding:\\s*base64', re.I)
  34. mp = re.compile('^content-type:.*multipart/.*boundary="?([^;"\n]*)', re.I|re.S)
  35. chrset = re.compile('^(content-type:.*charset=")(us-ascii|iso-8859-[0-9]+)(".*)', re.I|re.S)
  36. he = re.compile('^-*\n')
  37. mime_code = re.compile('=([0-9a-f][0-9a-f])', re.I)
  38. mime_head = re.compile('=\\?iso-8859-1\\?q\\?([^? \t\n]+)\\?=', re.I)
  39. repl = re.compile('^subject:\\s+re: ', re.I)
  40.  
  41. class File:
  42.     '''A simple fake file object that knows about limited
  43.        read-ahead and boundaries.
  44.        The only supported method is readline().'''
  45.  
  46.     def __init__(self, file, boundary):
  47.         self.file = file
  48.         self.boundary = boundary
  49.         self.peek = None
  50.  
  51.     def readline(self):
  52.         if self.peek is not None:
  53.             return ''
  54.         line = self.file.readline()
  55.         if not line:
  56.             return line
  57.         if self.boundary:
  58.             if line == self.boundary + '\n':
  59.                 self.peek = line
  60.                 return ''
  61.             if line == self.boundary + '--\n':
  62.                 self.peek = line
  63.                 return ''
  64.         return line
  65.  
  66. class HeaderFile:
  67.     def __init__(self, file):
  68.         self.file = file
  69.         self.peek = None
  70.  
  71.     def readline(self):
  72.         if self.peek is not None:
  73.             line = self.peek
  74.             self.peek = None
  75.         else:
  76.             line = self.file.readline()
  77.         if not line:
  78.             return line
  79.         if he.match(line):
  80.             return line
  81.         while 1:
  82.             self.peek = self.file.readline()
  83.             if len(self.peek) == 0 or \
  84.                (self.peek[0] != ' ' and self.peek[0] != '\t'):
  85.                 return line
  86.             line = line + self.peek
  87.             self.peek = None
  88.  
  89. def mime_decode(line):
  90.     '''Decode a single line of quoted-printable text to 8bit.'''
  91.     newline = ''
  92.     pos = 0
  93.     while 1:
  94.         res = mime_code.search(line, pos)
  95.         if res is None:
  96.             break
  97.         newline = newline + line[pos:res.start(0)] + \
  98.               chr(string.atoi(res.group(1), 16))
  99.         pos = res.end(0)
  100.     return newline + line[pos:]
  101.  
  102. def mime_decode_header(line):
  103.     '''Decode a header line to 8bit.'''
  104.     newline = ''
  105.     pos = 0
  106.     while 1:
  107.         res = mime_head.search(line, pos)
  108.         if res is None:
  109.             break
  110.         match = res.group(1)
  111.         # convert underscores to spaces (before =XX conversion!)
  112.         match = string.join(string.split(match, '_'), ' ')
  113.         newline = newline + line[pos:res.start(0)] + mime_decode(match)
  114.         pos = res.end(0)
  115.     return newline + line[pos:]
  116.  
  117. def unmimify_part(ifile, ofile, decode_base64 = 0):
  118.     '''Convert a quoted-printable part of a MIME mail message to 8bit.'''
  119.     multipart = None
  120.     quoted_printable = 0
  121.     is_base64 = 0
  122.     is_repl = 0
  123.     if ifile.boundary and ifile.boundary[:2] == QUOTE:
  124.         prefix = QUOTE
  125.     else:
  126.         prefix = ''
  127.  
  128.     # read header
  129.     hfile = HeaderFile(ifile)
  130.     while 1:
  131.         line = hfile.readline()
  132.         if not line:
  133.             return
  134.         if prefix and line[:len(prefix)] == prefix:
  135.             line = line[len(prefix):]
  136.             pref = prefix
  137.         else:
  138.             pref = ''
  139.         line = mime_decode_header(line)
  140.         if qp.match(line):
  141.             quoted_printable = 1
  142.             continue    # skip this header
  143.         if decode_base64 and base64_re.match(line):
  144.             is_base64 = 1
  145.             continue
  146.         ofile.write(pref + line)
  147.         if not prefix and repl.match(line):
  148.             # we're dealing with a reply message
  149.             is_repl = 1
  150.         mp_res = mp.match(line)
  151.         if mp_res:
  152.             multipart = '--' + mp_res.group(1)
  153.         if he.match(line):
  154.             break
  155.     if is_repl and (quoted_printable or multipart):
  156.         is_repl = 0
  157.  
  158.     # read body
  159.     while 1:
  160.         line = ifile.readline()
  161.         if not line:
  162.             return
  163.         line = re.sub(mime_head, '\\1', line)
  164.         if prefix and line[:len(prefix)] == prefix:
  165.             line = line[len(prefix):]
  166.             pref = prefix
  167.         else:
  168.             pref = ''
  169. ##        if is_repl and len(line) >= 4 and line[:4] == QUOTE+'--' and line[-3:] != '--\n':
  170. ##            multipart = line[:-1]
  171.         while multipart:
  172.             if line == multipart + '--\n':
  173.                 ofile.write(pref + line)
  174.                 multipart = None
  175.                 line = None
  176.                 break
  177.             if line == multipart + '\n':
  178.                 ofile.write(pref + line)
  179.                 nifile = File(ifile, multipart)
  180.                 unmimify_part(nifile, ofile, decode_base64)
  181.                 line = nifile.peek
  182.                 continue
  183.             # not a boundary between parts
  184.             break
  185.         if line and quoted_printable:
  186.             while line[-2:] == '=\n':
  187.                 line = line[:-2]
  188.                 newline = ifile.readline()
  189.                 if newline[:len(QUOTE)] == QUOTE:
  190.                     newline = newline[len(QUOTE):]
  191.                 line = line + newline
  192.             line = mime_decode(line)
  193.         if line and is_base64 and not pref:
  194.             import base64
  195.             line = base64.decodestring(line)
  196.         if line:
  197.             ofile.write(pref + line)
  198.  
  199. def unmimify(infile, outfile, decode_base64 = 0):
  200.     '''Convert quoted-printable parts of a MIME mail message to 8bit.'''
  201.     if type(infile) == type(''):
  202.         ifile = open(infile)
  203.         if type(outfile) == type('') and infile == outfile:
  204.             import os
  205.             d, f = os.path.split(infile)
  206.             os.rename(infile, os.path.join(d, ',' + f))
  207.     else:
  208.         ifile = infile
  209.     if type(outfile) == type(''):
  210.         ofile = open(outfile, 'w')
  211.     else:
  212.         ofile = outfile
  213.     nifile = File(ifile, None)
  214.     unmimify_part(nifile, ofile, decode_base64)
  215.     ofile.flush()
  216.  
  217. mime_char = re.compile('[=\177-\377]') # quote these chars in body
  218. mime_header_char = re.compile('[=?\177-\377]') # quote these in header
  219.  
  220. def mime_encode(line, header):
  221.     '''Code a single line as quoted-printable.
  222.        If header is set, quote some extra characters.'''
  223.     if header:
  224.         reg = mime_header_char
  225.     else:
  226.         reg = mime_char
  227.     newline = ''
  228.     pos = 0
  229.     if len(line) >= 5 and line[:5] == 'From ':
  230.         # quote 'From ' at the start of a line for stupid mailers
  231.         newline = string.upper('=%02x' % ord('F'))
  232.         pos = 1
  233.     while 1:
  234.         res = reg.search(line, pos)
  235.         if res is None:
  236.             break
  237.         newline = newline + line[pos:res.start(0)] + \
  238.               string.upper('=%02x' % ord(res.group(0)))
  239.         pos = res.end(0)
  240.     line = newline + line[pos:]
  241.  
  242.     newline = ''
  243.     while len(line) >= 75:
  244.         i = 73
  245.         while line[i] == '=' or line[i-1] == '=':
  246.             i = i - 1
  247.         i = i + 1
  248.         newline = newline + line[:i] + '=\n'
  249.         line = line[i:]
  250.     return newline + line
  251.  
  252. mime_header = re.compile('([ \t(]|^)([-a-zA-Z0-9_+]*[\177-\377][-a-zA-Z0-9_+\177-\377]*)([ \t)]|\n)')
  253.  
  254. def mime_encode_header(line):
  255.     '''Code a single header line as quoted-printable.'''
  256.     newline = ''
  257.     pos = 0
  258.     while 1:
  259.         res = mime_header.search(line, pos)
  260.         if res is None:
  261.             break
  262.         newline = '%s%s%s=?%s?Q?%s?=%s' % \
  263.               (newline, line[pos:res.start(0)], res.group(1),
  264.                CHARSET, mime_encode(res.group(2), 1), res.group(3))
  265.         pos = res.end(0)
  266.     return newline + line[pos:]
  267.  
  268. mv = re.compile('^mime-version:', re.I)
  269. cte = re.compile('^content-transfer-encoding:', re.I)
  270. iso_char = re.compile('[\177-\377]')
  271.  
  272. def mimify_part(ifile, ofile, is_mime):
  273.     '''Convert an 8bit part of a MIME mail message to quoted-printable.'''
  274.     has_cte = is_qp = is_base64 = 0
  275.     multipart = None
  276.     must_quote_body = must_quote_header = has_iso_chars = 0
  277.  
  278.     header = []
  279.     header_end = ''
  280.     message = []
  281.     message_end = ''
  282.     # read header
  283.     hfile = HeaderFile(ifile)
  284.     while 1:
  285.         line = hfile.readline()
  286.         if not line:
  287.             break
  288.         if not must_quote_header and iso_char.search(line):
  289.             must_quote_header = 1
  290.         if mv.match(line):
  291.             is_mime = 1
  292.         if cte.match(line):
  293.             has_cte = 1
  294.             if qp.match(line):
  295.                 is_qp = 1
  296.             elif base64_re.match(line):
  297.                 is_base64 = 1
  298.         mp_res = mp.match(line)
  299.         if mp_res:
  300.             multipart = '--' + mp_res.group(1)
  301.         if he.match(line):
  302.             header_end = line
  303.             break
  304.         header.append(line)
  305.  
  306.     # read body
  307.     while 1:
  308.         line = ifile.readline()
  309.         if not line:
  310.             break
  311.         if multipart:
  312.             if line == multipart + '--\n':
  313.                 message_end = line
  314.                 break
  315.             if line == multipart + '\n':
  316.                 message_end = line
  317.                 break
  318.         if is_base64:
  319.             message.append(line)
  320.             continue
  321.         if is_qp:
  322.             while line[-2:] == '=\n':
  323.                 line = line[:-2]
  324.                 newline = ifile.readline()
  325.                 if newline[:len(QUOTE)] == QUOTE:
  326.                     newline = newline[len(QUOTE):]
  327.                 line = line + newline
  328.             line = mime_decode(line)
  329.         message.append(line)
  330.         if not has_iso_chars:
  331.             if iso_char.search(line):
  332.                 has_iso_chars = must_quote_body = 1
  333.         if not must_quote_body:
  334.             if len(line) > MAXLEN:
  335.                 must_quote_body = 1
  336.  
  337.     # convert and output header and body
  338.     for line in header:
  339.         if must_quote_header:
  340.             line = mime_encode_header(line)
  341.         chrset_res = chrset.match(line)
  342.         if chrset_res:
  343.             if has_iso_chars:
  344.                 # change us-ascii into iso-8859-1
  345.                 if string.lower(chrset_res.group(2)) == 'us-ascii':
  346.                     line = '%s%s%s' % (chrset_res.group(1),
  347.                                CHARSET,
  348.                                chrset_res.group(3))
  349.             else:
  350.                 # change iso-8859-* into us-ascii
  351.                 line = '%sus-ascii%s' % chrset_res.group(1, 3)
  352.         if has_cte and cte.match(line):
  353.             line = 'Content-Transfer-Encoding: '
  354.             if is_base64:
  355.                 line = line + 'base64\n'
  356.             elif must_quote_body:
  357.                 line = line + 'quoted-printable\n'
  358.             else:
  359.                 line = line + '7bit\n'
  360.         ofile.write(line)
  361.     if (must_quote_header or must_quote_body) and not is_mime:
  362.         ofile.write('Mime-Version: 1.0\n')
  363.         ofile.write('Content-Type: text/plain; ')
  364.         if has_iso_chars:
  365.             ofile.write('charset="%s"\n' % CHARSET)
  366.         else:
  367.             ofile.write('charset="us-ascii"\n')
  368.     if must_quote_body and not has_cte:
  369.         ofile.write('Content-Transfer-Encoding: quoted-printable\n')
  370.     ofile.write(header_end)
  371.  
  372.     for line in message:
  373.         if must_quote_body:
  374.             line = mime_encode(line, 0)
  375.         ofile.write(line)
  376.     ofile.write(message_end)
  377.  
  378.     line = message_end
  379.     while multipart:
  380.         if line == multipart + '--\n':
  381.             # read bit after the end of the last part
  382.             while 1:
  383.                 line = ifile.readline()
  384.                 if not line:
  385.                     return
  386.                 if must_quote_body:
  387.                     line = mime_encode(line, 0)
  388.                 ofile.write(line)
  389.         if line == multipart + '\n':
  390.             nifile = File(ifile, multipart)
  391.             mimify_part(nifile, ofile, 1)
  392.             line = nifile.peek
  393.             ofile.write(line)
  394.             continue
  395.  
  396. def mimify(infile, outfile):
  397.     '''Convert 8bit parts of a MIME mail message to quoted-printable.'''
  398.     if type(infile) == type(''):
  399.         ifile = open(infile)
  400.         if type(outfile) == type('') and infile == outfile:
  401.             import os
  402.             d, f = os.path.split(infile)
  403.             os.rename(infile, os.path.join(d, ',' + f))
  404.     else:
  405.         ifile = infile
  406.     if type(outfile) == type(''):
  407.         ofile = open(outfile, 'w')
  408.     else:
  409.         ofile = outfile
  410.     nifile = File(ifile, None)
  411.     mimify_part(nifile, ofile, 0)
  412.     ofile.flush()
  413.  
  414. import sys
  415. if __name__ == '__main__' or (len(sys.argv) > 0 and sys.argv[0] == 'mimify'):
  416.     import getopt
  417.     usage = 'Usage: mimify [-l len] -[ed] [infile [outfile]]'
  418.  
  419.     decode_base64 = 0
  420.     opts, args = getopt.getopt(sys.argv[1:], 'l:edb')
  421.     if len(args) not in (0, 1, 2):
  422.         print usage
  423.         sys.exit(1)
  424.     if (('-e', '') in opts) == (('-d', '') in opts) or \
  425.        ((('-b', '') in opts) and (('-d', '') not in opts)):
  426.         print usage
  427.         sys.exit(1)
  428.     for o, a in opts:
  429.         if o == '-e':
  430.             encode = mimify
  431.         elif o == '-d':
  432.             encode = unmimify
  433.         elif o == '-l':
  434.             try:
  435.                 MAXLEN = string.atoi(a)
  436.             except:
  437.                 print usage
  438.                 sys.exit(1)
  439.         elif o == '-b':
  440.             decode_base64 = 1
  441.     if len(args) == 0:
  442.         encode_args = (sys.stdin, sys.stdout)
  443.     elif len(args) == 1:
  444.         encode_args = (args[0], sys.stdout)
  445.     else:
  446.         encode_args = (args[0], args[1])
  447.     if decode_base64:
  448.         encode_args = encode_args + (decode_base64,)
  449.     apply(encode, encode_args)
  450.  
  451.