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