Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 1 | """Various tools used by MIME-reading or MIME-writing programs.""" |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 2 | |
| 3 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4 | import os |
Brett Cannon | 1eaf074 | 2008-09-02 01:25:16 +0000 | [diff] [blame] | 5 | import sys |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6 | import tempfile |
Brett Cannon | 1eaf074 | 2008-09-02 01:25:16 +0000 | [diff] [blame] | 7 | from warnings import filterwarnings, catch_warnings |
| 8 | with catch_warnings(record=False): |
| 9 | if sys.py3kwarning: |
| 10 | filterwarnings("ignore", ".*rfc822 has been removed", DeprecationWarning) |
Brett Cannon | bf3157b | 2008-08-14 05:00:03 +0000 | [diff] [blame] | 11 | import rfc822 |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 12 | |
Benjamin Peterson | a03722f | 2008-06-12 14:23:49 +0000 | [diff] [blame] | 13 | from warnings import warnpy3k |
Benjamin Peterson | a6864e0 | 2008-07-14 17:42:17 +0000 | [diff] [blame] | 14 | warnpy3k("in 3.x, mimetools has been removed in favor of the email package", |
| 15 | stacklevel=2) |
Benjamin Peterson | a03722f | 2008-06-12 14:23:49 +0000 | [diff] [blame] | 16 | |
Skip Montanaro | 03d9014 | 2001-01-25 15:29:22 +0000 | [diff] [blame] | 17 | __all__ = ["Message","choose_boundary","encode","decode","copyliteral", |
| 18 | "copybinary"] |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 19 | |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 20 | class Message(rfc822.Message): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 21 | """A derived class of rfc822.Message that knows about MIME headers and |
| 22 | contains some hooks for decoding encoded and multipart messages.""" |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 23 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 24 | def __init__(self, fp, seekable = 1): |
| 25 | rfc822.Message.__init__(self, fp, seekable) |
| 26 | self.encodingheader = \ |
| 27 | self.getheader('content-transfer-encoding') |
| 28 | self.typeheader = \ |
| 29 | self.getheader('content-type') |
| 30 | self.parsetype() |
| 31 | self.parseplist() |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 32 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 33 | def parsetype(self): |
| 34 | str = self.typeheader |
| 35 | if str is None: |
| 36 | str = 'text/plain' |
| 37 | if ';' in str: |
| 38 | i = str.index(';') |
| 39 | self.plisttext = str[i:] |
| 40 | str = str[:i] |
| 41 | else: |
| 42 | self.plisttext = '' |
| 43 | fields = str.split('/') |
| 44 | for i in range(len(fields)): |
| 45 | fields[i] = fields[i].strip().lower() |
| 46 | self.type = '/'.join(fields) |
| 47 | self.maintype = fields[0] |
| 48 | self.subtype = '/'.join(fields[1:]) |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 49 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 50 | def parseplist(self): |
| 51 | str = self.plisttext |
| 52 | self.plist = [] |
| 53 | while str[:1] == ';': |
| 54 | str = str[1:] |
| 55 | if ';' in str: |
| 56 | # XXX Should parse quotes! |
| 57 | end = str.index(';') |
| 58 | else: |
| 59 | end = len(str) |
| 60 | f = str[:end] |
| 61 | if '=' in f: |
| 62 | i = f.index('=') |
| 63 | f = f[:i].strip().lower() + \ |
| 64 | '=' + f[i+1:].strip() |
| 65 | self.plist.append(f.strip()) |
| 66 | str = str[end:] |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 67 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 68 | def getplist(self): |
| 69 | return self.plist |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 70 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 71 | def getparam(self, name): |
| 72 | name = name.lower() + '=' |
| 73 | n = len(name) |
| 74 | for p in self.plist: |
| 75 | if p[:n] == name: |
| 76 | return rfc822.unquote(p[n:]) |
| 77 | return None |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 78 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 79 | def getparamnames(self): |
| 80 | result = [] |
| 81 | for p in self.plist: |
| 82 | i = p.find('=') |
| 83 | if i >= 0: |
| 84 | result.append(p[:i].lower()) |
| 85 | return result |
Guido van Rossum | 4be63d1 | 1996-10-04 20:14:02 +0000 | [diff] [blame] | 86 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 87 | def getencoding(self): |
| 88 | if self.encodingheader is None: |
| 89 | return '7bit' |
| 90 | return self.encodingheader.lower() |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 91 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 92 | def gettype(self): |
| 93 | return self.type |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 94 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 95 | def getmaintype(self): |
| 96 | return self.maintype |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 97 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 98 | def getsubtype(self): |
| 99 | return self.subtype |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | # Utility functions |
| 105 | # ----------------- |
| 106 | |
Tim Peters | 080da28 | 2003-06-15 22:05:58 +0000 | [diff] [blame] | 107 | try: |
| 108 | import thread |
| 109 | except ImportError: |
| 110 | import dummy_thread as thread |
| 111 | _counter_lock = thread.allocate_lock() |
| 112 | del thread |
| 113 | |
| 114 | _counter = 0 |
| 115 | def _get_next_counter(): |
| 116 | global _counter |
| 117 | _counter_lock.acquire() |
| 118 | _counter += 1 |
| 119 | result = _counter |
| 120 | _counter_lock.release() |
| 121 | return result |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 122 | |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 123 | _prefix = None |
| 124 | |
| 125 | def choose_boundary(): |
Tim Peters | 080da28 | 2003-06-15 22:05:58 +0000 | [diff] [blame] | 126 | """Return a string usable as a multipart boundary. |
| 127 | |
| 128 | The string chosen is unique within a single program run, and |
| 129 | incorporates the user id (if available), process id (if available), |
| 130 | and current time. So it's very unlikely the returned string appears |
| 131 | in message text, but there's no guarantee. |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 132 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 133 | The boundary contains dots so you have to quote it in the header.""" |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 134 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 135 | global _prefix |
| 136 | import time |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 137 | if _prefix is None: |
| 138 | import socket |
Georg Brandl | dd2245f | 2006-03-31 17:18:06 +0000 | [diff] [blame] | 139 | try: |
| 140 | hostid = socket.gethostbyname(socket.gethostname()) |
| 141 | except socket.gaierror: |
| 142 | hostid = '127.0.0.1' |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 143 | try: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 144 | uid = repr(os.getuid()) |
Skip Montanaro | 91cc17d | 2002-03-23 05:58:52 +0000 | [diff] [blame] | 145 | except AttributeError: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 146 | uid = '1' |
| 147 | try: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 148 | pid = repr(os.getpid()) |
Skip Montanaro | 91cc17d | 2002-03-23 05:58:52 +0000 | [diff] [blame] | 149 | except AttributeError: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 150 | pid = '1' |
| 151 | _prefix = hostid + '.' + uid + '.' + pid |
Tim Peters | 080da28 | 2003-06-15 22:05:58 +0000 | [diff] [blame] | 152 | return "%s.%.3f.%d" % (_prefix, time.time(), _get_next_counter()) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 153 | |
| 154 | |
| 155 | # Subroutines for decoding some common content-transfer-types |
| 156 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 157 | def decode(input, output, encoding): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 158 | """Decode common content-transfer-encodings (base64, quopri, uuencode).""" |
| 159 | if encoding == 'base64': |
| 160 | import base64 |
| 161 | return base64.decode(input, output) |
| 162 | if encoding == 'quoted-printable': |
| 163 | import quopri |
| 164 | return quopri.decode(input, output) |
| 165 | if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'): |
| 166 | import uu |
| 167 | return uu.decode(input, output) |
| 168 | if encoding in ('7bit', '8bit'): |
| 169 | return output.write(input.read()) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 170 | if encoding in decodetab: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 171 | pipethrough(input, decodetab[encoding], output) |
| 172 | else: |
| 173 | raise ValueError, \ |
| 174 | 'unknown Content-Transfer-Encoding: %s' % encoding |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 175 | |
| 176 | def encode(input, output, encoding): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 177 | """Encode common content-transfer-encodings (base64, quopri, uuencode).""" |
| 178 | if encoding == 'base64': |
| 179 | import base64 |
| 180 | return base64.encode(input, output) |
| 181 | if encoding == 'quoted-printable': |
| 182 | import quopri |
| 183 | return quopri.encode(input, output, 0) |
| 184 | if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'): |
| 185 | import uu |
| 186 | return uu.encode(input, output) |
| 187 | if encoding in ('7bit', '8bit'): |
| 188 | return output.write(input.read()) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 189 | if encoding in encodetab: |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 190 | pipethrough(input, encodetab[encoding], output) |
| 191 | else: |
| 192 | raise ValueError, \ |
| 193 | 'unknown Content-Transfer-Encoding: %s' % encoding |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 194 | |
Guido van Rossum | e3cd151 | 1997-07-11 16:33:26 +0000 | [diff] [blame] | 195 | # The following is no longer used for standard encodings |
| 196 | |
| 197 | # XXX This requires that uudecode and mmencode are in $PATH |
| 198 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 199 | uudecode_pipe = '''( |
| 200 | TEMP=/tmp/@uu.$$ |
| 201 | sed "s%^begin [0-7][0-7]* .*%begin 600 $TEMP%" | uudecode |
| 202 | cat $TEMP |
| 203 | rm $TEMP |
| 204 | )''' |
| 205 | |
| 206 | decodetab = { |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 207 | 'uuencode': uudecode_pipe, |
| 208 | 'x-uuencode': uudecode_pipe, |
| 209 | 'uue': uudecode_pipe, |
| 210 | 'x-uue': uudecode_pipe, |
| 211 | 'quoted-printable': 'mmencode -u -q', |
| 212 | 'base64': 'mmencode -u -b', |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | encodetab = { |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 216 | 'x-uuencode': 'uuencode tempfile', |
| 217 | 'uuencode': 'uuencode tempfile', |
| 218 | 'x-uue': 'uuencode tempfile', |
| 219 | 'uue': 'uuencode tempfile', |
| 220 | 'quoted-printable': 'mmencode -q', |
| 221 | 'base64': 'mmencode -b', |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | def pipeto(input, command): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 225 | pipe = os.popen(command, 'w') |
| 226 | copyliteral(input, pipe) |
| 227 | pipe.close() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 228 | |
| 229 | def pipethrough(input, command, output): |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 230 | (fd, tempname) = tempfile.mkstemp() |
| 231 | temp = os.fdopen(fd, 'w') |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 232 | copyliteral(input, temp) |
| 233 | temp.close() |
| 234 | pipe = os.popen(command + ' <' + tempname, 'r') |
| 235 | copybinary(pipe, output) |
| 236 | pipe.close() |
| 237 | os.unlink(tempname) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 238 | |
| 239 | def copyliteral(input, output): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 240 | while 1: |
| 241 | line = input.readline() |
| 242 | if not line: break |
| 243 | output.write(line) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 244 | |
| 245 | def copybinary(input, output): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 246 | BUFSIZE = 8192 |
| 247 | while 1: |
| 248 | line = input.read(BUFSIZE) |
| 249 | if not line: break |
| 250 | output.write(line) |