+++ /dev/null
-#! /usr/bin/env python\r
-\r
-"""Mimification and unmimification of mail messages.\r
-\r
-Decode quoted-printable parts of a mail message or encode using\r
-quoted-printable.\r
-\r
-Usage:\r
- mimify(input, output)\r
- unmimify(input, output, decode_base64 = 0)\r
-to encode and decode respectively. Input and output may be the name\r
-of a file or an open file object. Only a readline() method is used\r
-on the input file, only a write() method is used on the output file.\r
-When using file names, the input and output file names may be the\r
-same.\r
-\r
-Interactive usage:\r
- mimify.py -e [infile [outfile]]\r
- mimify.py -d [infile [outfile]]\r
-to encode and decode respectively. Infile defaults to standard\r
-input and outfile to standard output.\r
-"""\r
-\r
-# Configure\r
-MAXLEN = 200 # if lines longer than this, encode as quoted-printable\r
-CHARSET = 'ISO-8859-1' # default charset for non-US-ASCII mail\r
-QUOTE = '> ' # string replies are quoted with\r
-# End configure\r
-\r
-import re\r
-\r
-import warnings\r
-warnings.warn("the mimify module is deprecated; use the email package instead",\r
- DeprecationWarning, 2)\r
-\r
-__all__ = ["mimify","unmimify","mime_encode_header","mime_decode_header"]\r
-\r
-qp = re.compile('^content-transfer-encoding:\\s*quoted-printable', re.I)\r
-base64_re = re.compile('^content-transfer-encoding:\\s*base64', re.I)\r
-mp = re.compile('^content-type:.*multipart/.*boundary="?([^;"\n]*)', re.I|re.S)\r
-chrset = re.compile('^(content-type:.*charset=")(us-ascii|iso-8859-[0-9]+)(".*)', re.I|re.S)\r
-he = re.compile('^-*\n')\r
-mime_code = re.compile('=([0-9a-f][0-9a-f])', re.I)\r
-mime_head = re.compile('=\\?iso-8859-1\\?q\\?([^? \t\n]+)\\?=', re.I)\r
-repl = re.compile('^subject:\\s+re: ', re.I)\r
-\r
-class File:\r
- """A simple fake file object that knows about limited read-ahead and\r
- boundaries. The only supported method is readline()."""\r
-\r
- def __init__(self, file, boundary):\r
- self.file = file\r
- self.boundary = boundary\r
- self.peek = None\r
-\r
- def readline(self):\r
- if self.peek is not None:\r
- return ''\r
- line = self.file.readline()\r
- if not line:\r
- return line\r
- if self.boundary:\r
- if line == self.boundary + '\n':\r
- self.peek = line\r
- return ''\r
- if line == self.boundary + '--\n':\r
- self.peek = line\r
- return ''\r
- return line\r
-\r
-class HeaderFile:\r
- def __init__(self, file):\r
- self.file = file\r
- self.peek = None\r
-\r
- def readline(self):\r
- if self.peek is not None:\r
- line = self.peek\r
- self.peek = None\r
- else:\r
- line = self.file.readline()\r
- if not line:\r
- return line\r
- if he.match(line):\r
- return line\r
- while 1:\r
- self.peek = self.file.readline()\r
- if len(self.peek) == 0 or \\r
- (self.peek[0] != ' ' and self.peek[0] != '\t'):\r
- return line\r
- line = line + self.peek\r
- self.peek = None\r
-\r
-def mime_decode(line):\r
- """Decode a single line of quoted-printable text to 8bit."""\r
- newline = ''\r
- pos = 0\r
- while 1:\r
- res = mime_code.search(line, pos)\r
- if res is None:\r
- break\r
- newline = newline + line[pos:res.start(0)] + \\r
- chr(int(res.group(1), 16))\r
- pos = res.end(0)\r
- return newline + line[pos:]\r
-\r
-def mime_decode_header(line):\r
- """Decode a header line to 8bit."""\r
- newline = ''\r
- pos = 0\r
- while 1:\r
- res = mime_head.search(line, pos)\r
- if res is None:\r
- break\r
- match = res.group(1)\r
- # convert underscores to spaces (before =XX conversion!)\r
- match = ' '.join(match.split('_'))\r
- newline = newline + line[pos:res.start(0)] + mime_decode(match)\r
- pos = res.end(0)\r
- return newline + line[pos:]\r
-\r
-def unmimify_part(ifile, ofile, decode_base64 = 0):\r
- """Convert a quoted-printable part of a MIME mail message to 8bit."""\r
- multipart = None\r
- quoted_printable = 0\r
- is_base64 = 0\r
- is_repl = 0\r
- if ifile.boundary and ifile.boundary[:2] == QUOTE:\r
- prefix = QUOTE\r
- else:\r
- prefix = ''\r
-\r
- # read header\r
- hfile = HeaderFile(ifile)\r
- while 1:\r
- line = hfile.readline()\r
- if not line:\r
- return\r
- if prefix and line[:len(prefix)] == prefix:\r
- line = line[len(prefix):]\r
- pref = prefix\r
- else:\r
- pref = ''\r
- line = mime_decode_header(line)\r
- if qp.match(line):\r
- quoted_printable = 1\r
- continue # skip this header\r
- if decode_base64 and base64_re.match(line):\r
- is_base64 = 1\r
- continue\r
- ofile.write(pref + line)\r
- if not prefix and repl.match(line):\r
- # we're dealing with a reply message\r
- is_repl = 1\r
- mp_res = mp.match(line)\r
- if mp_res:\r
- multipart = '--' + mp_res.group(1)\r
- if he.match(line):\r
- break\r
- if is_repl and (quoted_printable or multipart):\r
- is_repl = 0\r
-\r
- # read body\r
- while 1:\r
- line = ifile.readline()\r
- if not line:\r
- return\r
- line = re.sub(mime_head, '\\1', line)\r
- if prefix and line[:len(prefix)] == prefix:\r
- line = line[len(prefix):]\r
- pref = prefix\r
- else:\r
- pref = ''\r
-## if is_repl and len(line) >= 4 and line[:4] == QUOTE+'--' and line[-3:] != '--\n':\r
-## multipart = line[:-1]\r
- while multipart:\r
- if line == multipart + '--\n':\r
- ofile.write(pref + line)\r
- multipart = None\r
- line = None\r
- break\r
- if line == multipart + '\n':\r
- ofile.write(pref + line)\r
- nifile = File(ifile, multipart)\r
- unmimify_part(nifile, ofile, decode_base64)\r
- line = nifile.peek\r
- if not line:\r
- # premature end of file\r
- break\r
- continue\r
- # not a boundary between parts\r
- break\r
- if line and quoted_printable:\r
- while line[-2:] == '=\n':\r
- line = line[:-2]\r
- newline = ifile.readline()\r
- if newline[:len(QUOTE)] == QUOTE:\r
- newline = newline[len(QUOTE):]\r
- line = line + newline\r
- line = mime_decode(line)\r
- if line and is_base64 and not pref:\r
- import base64\r
- line = base64.decodestring(line)\r
- if line:\r
- ofile.write(pref + line)\r
-\r
-def unmimify(infile, outfile, decode_base64 = 0):\r
- """Convert quoted-printable parts of a MIME mail message to 8bit."""\r
- if type(infile) == type(''):\r
- ifile = open(infile)\r
- if type(outfile) == type('') and infile == outfile:\r
- import os\r
- d, f = os.path.split(infile)\r
- os.rename(infile, os.path.join(d, ',' + f))\r
- else:\r
- ifile = infile\r
- if type(outfile) == type(''):\r
- ofile = open(outfile, 'w')\r
- else:\r
- ofile = outfile\r
- nifile = File(ifile, None)\r
- unmimify_part(nifile, ofile, decode_base64)\r
- ofile.flush()\r
-\r
-mime_char = re.compile('[=\177-\377]') # quote these chars in body\r
-mime_header_char = re.compile('[=?\177-\377]') # quote these in header\r
-\r
-def mime_encode(line, header):\r
- """Code a single line as quoted-printable.\r
- If header is set, quote some extra characters."""\r
- if header:\r
- reg = mime_header_char\r
- else:\r
- reg = mime_char\r
- newline = ''\r
- pos = 0\r
- if len(line) >= 5 and line[:5] == 'From ':\r
- # quote 'From ' at the start of a line for stupid mailers\r
- newline = ('=%02x' % ord('F')).upper()\r
- pos = 1\r
- while 1:\r
- res = reg.search(line, pos)\r
- if res is None:\r
- break\r
- newline = newline + line[pos:res.start(0)] + \\r
- ('=%02x' % ord(res.group(0))).upper()\r
- pos = res.end(0)\r
- line = newline + line[pos:]\r
-\r
- newline = ''\r
- while len(line) >= 75:\r
- i = 73\r
- while line[i] == '=' or line[i-1] == '=':\r
- i = i - 1\r
- i = i + 1\r
- newline = newline + line[:i] + '=\n'\r
- line = line[i:]\r
- return newline + line\r
-\r
-mime_header = re.compile('([ \t(]|^)([-a-zA-Z0-9_+]*[\177-\377][-a-zA-Z0-9_+\177-\377]*)(?=[ \t)]|\n)')\r
-\r
-def mime_encode_header(line):\r
- """Code a single header line as quoted-printable."""\r
- newline = ''\r
- pos = 0\r
- while 1:\r
- res = mime_header.search(line, pos)\r
- if res is None:\r
- break\r
- newline = '%s%s%s=?%s?Q?%s?=' % \\r
- (newline, line[pos:res.start(0)], res.group(1),\r
- CHARSET, mime_encode(res.group(2), 1))\r
- pos = res.end(0)\r
- return newline + line[pos:]\r
-\r
-mv = re.compile('^mime-version:', re.I)\r
-cte = re.compile('^content-transfer-encoding:', re.I)\r
-iso_char = re.compile('[\177-\377]')\r
-\r
-def mimify_part(ifile, ofile, is_mime):\r
- """Convert an 8bit part of a MIME mail message to quoted-printable."""\r
- has_cte = is_qp = is_base64 = 0\r
- multipart = None\r
- must_quote_body = must_quote_header = has_iso_chars = 0\r
-\r
- header = []\r
- header_end = ''\r
- message = []\r
- message_end = ''\r
- # read header\r
- hfile = HeaderFile(ifile)\r
- while 1:\r
- line = hfile.readline()\r
- if not line:\r
- break\r
- if not must_quote_header and iso_char.search(line):\r
- must_quote_header = 1\r
- if mv.match(line):\r
- is_mime = 1\r
- if cte.match(line):\r
- has_cte = 1\r
- if qp.match(line):\r
- is_qp = 1\r
- elif base64_re.match(line):\r
- is_base64 = 1\r
- mp_res = mp.match(line)\r
- if mp_res:\r
- multipart = '--' + mp_res.group(1)\r
- if he.match(line):\r
- header_end = line\r
- break\r
- header.append(line)\r
-\r
- # read body\r
- while 1:\r
- line = ifile.readline()\r
- if not line:\r
- break\r
- if multipart:\r
- if line == multipart + '--\n':\r
- message_end = line\r
- break\r
- if line == multipart + '\n':\r
- message_end = line\r
- break\r
- if is_base64:\r
- message.append(line)\r
- continue\r
- if is_qp:\r
- while line[-2:] == '=\n':\r
- line = line[:-2]\r
- newline = ifile.readline()\r
- if newline[:len(QUOTE)] == QUOTE:\r
- newline = newline[len(QUOTE):]\r
- line = line + newline\r
- line = mime_decode(line)\r
- message.append(line)\r
- if not has_iso_chars:\r
- if iso_char.search(line):\r
- has_iso_chars = must_quote_body = 1\r
- if not must_quote_body:\r
- if len(line) > MAXLEN:\r
- must_quote_body = 1\r
-\r
- # convert and output header and body\r
- for line in header:\r
- if must_quote_header:\r
- line = mime_encode_header(line)\r
- chrset_res = chrset.match(line)\r
- if chrset_res:\r
- if has_iso_chars:\r
- # change us-ascii into iso-8859-1\r
- if chrset_res.group(2).lower() == 'us-ascii':\r
- line = '%s%s%s' % (chrset_res.group(1),\r
- CHARSET,\r
- chrset_res.group(3))\r
- else:\r
- # change iso-8859-* into us-ascii\r
- line = '%sus-ascii%s' % chrset_res.group(1, 3)\r
- if has_cte and cte.match(line):\r
- line = 'Content-Transfer-Encoding: '\r
- if is_base64:\r
- line = line + 'base64\n'\r
- elif must_quote_body:\r
- line = line + 'quoted-printable\n'\r
- else:\r
- line = line + '7bit\n'\r
- ofile.write(line)\r
- if (must_quote_header or must_quote_body) and not is_mime:\r
- ofile.write('Mime-Version: 1.0\n')\r
- ofile.write('Content-Type: text/plain; ')\r
- if has_iso_chars:\r
- ofile.write('charset="%s"\n' % CHARSET)\r
- else:\r
- ofile.write('charset="us-ascii"\n')\r
- if must_quote_body and not has_cte:\r
- ofile.write('Content-Transfer-Encoding: quoted-printable\n')\r
- ofile.write(header_end)\r
-\r
- for line in message:\r
- if must_quote_body:\r
- line = mime_encode(line, 0)\r
- ofile.write(line)\r
- ofile.write(message_end)\r
-\r
- line = message_end\r
- while multipart:\r
- if line == multipart + '--\n':\r
- # read bit after the end of the last part\r
- while 1:\r
- line = ifile.readline()\r
- if not line:\r
- return\r
- if must_quote_body:\r
- line = mime_encode(line, 0)\r
- ofile.write(line)\r
- if line == multipart + '\n':\r
- nifile = File(ifile, multipart)\r
- mimify_part(nifile, ofile, 1)\r
- line = nifile.peek\r
- if not line:\r
- # premature end of file\r
- break\r
- ofile.write(line)\r
- continue\r
- # unexpectedly no multipart separator--copy rest of file\r
- while 1:\r
- line = ifile.readline()\r
- if not line:\r
- return\r
- if must_quote_body:\r
- line = mime_encode(line, 0)\r
- ofile.write(line)\r
-\r
-def mimify(infile, outfile):\r
- """Convert 8bit parts of a MIME mail message to quoted-printable."""\r
- if type(infile) == type(''):\r
- ifile = open(infile)\r
- if type(outfile) == type('') and infile == outfile:\r
- import os\r
- d, f = os.path.split(infile)\r
- os.rename(infile, os.path.join(d, ',' + f))\r
- else:\r
- ifile = infile\r
- if type(outfile) == type(''):\r
- ofile = open(outfile, 'w')\r
- else:\r
- ofile = outfile\r
- nifile = File(ifile, None)\r
- mimify_part(nifile, ofile, 0)\r
- ofile.flush()\r
-\r
-import sys\r
-if __name__ == '__main__' or (len(sys.argv) > 0 and sys.argv[0] == 'mimify'):\r
- import getopt\r
- usage = 'Usage: mimify [-l len] -[ed] [infile [outfile]]'\r
-\r
- decode_base64 = 0\r
- opts, args = getopt.getopt(sys.argv[1:], 'l:edb')\r
- if len(args) not in (0, 1, 2):\r
- print usage\r
- sys.exit(1)\r
- if (('-e', '') in opts) == (('-d', '') in opts) or \\r
- ((('-b', '') in opts) and (('-d', '') not in opts)):\r
- print usage\r
- sys.exit(1)\r
- for o, a in opts:\r
- if o == '-e':\r
- encode = mimify\r
- elif o == '-d':\r
- encode = unmimify\r
- elif o == '-l':\r
- try:\r
- MAXLEN = int(a)\r
- except (ValueError, OverflowError):\r
- print usage\r
- sys.exit(1)\r
- elif o == '-b':\r
- decode_base64 = 1\r
- if len(args) == 0:\r
- encode_args = (sys.stdin, sys.stdout)\r
- elif len(args) == 1:\r
- encode_args = (args[0], sys.stdout)\r
- else:\r
- encode_args = (args[0], args[1])\r
- if decode_base64:\r
- encode_args = encode_args + (decode_base64,)\r
- encode(*encode_args)\r