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