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