Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 1 | # Various tools used by MIME-reading or MIME-writing programs. |
| 2 | |
| 3 | |
| 4 | import string |
| 5 | import rfc822 |
| 6 | |
| 7 | |
| 8 | # A derived class of rfc822.Message that knows about MIME headers and |
| 9 | # contains some hooks for decoding encoded and multipart messages. |
| 10 | |
| 11 | class Message(rfc822.Message): |
| 12 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 13 | def __init__(self, fp): |
| 14 | rfc822.Message.__init__(self, fp) |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 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 | |
| 22 | def parsetype(self): |
| 23 | str = self.typeheader |
| 24 | if str == None: |
| 25 | str = 'text/plain' |
| 26 | if ';' in str: |
| 27 | i = string.index(str, ';') |
| 28 | self.plisttext = str[i:] |
| 29 | str = str[:i] |
| 30 | else: |
| 31 | self.plisttext = '' |
| 32 | fields = string.splitfields(str, '/') |
| 33 | for i in range(len(fields)): |
| 34 | fields[i] = string.lower(string.strip(fields[i])) |
| 35 | self.type = string.joinfields(fields, '/') |
| 36 | self.maintype = fields[0] |
| 37 | self.subtype = string.joinfields(fields[1:], '/') |
| 38 | |
| 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 = string.index(str, ';') |
| 47 | else: |
| 48 | end = len(str) |
| 49 | f = str[:end] |
| 50 | if '=' in f: |
| 51 | i = string.index(f, '=') |
| 52 | f = string.lower(string.strip(f[:i])) + \ |
| 53 | '=' + string.strip(f[i+1:]) |
| 54 | self.plist.append(string.strip(f)) |
| 55 | |
| 56 | def getplist(self): |
| 57 | return self.plist |
| 58 | |
| 59 | def getparam(self, name): |
| 60 | name = string.lower(name) + '=' |
| 61 | n = len(name) |
| 62 | for p in self.plist: |
| 63 | if p[:n] == name: |
| 64 | return rfc822.unquote(p[n:]) |
| 65 | return None |
| 66 | |
| 67 | def getencoding(self): |
| 68 | if self.encodingheader == None: |
| 69 | return '7bit' |
| 70 | return self.encodingheader |
| 71 | |
| 72 | def gettype(self): |
| 73 | return self.type |
| 74 | |
| 75 | def getmaintype(self): |
| 76 | return self.maintype |
| 77 | |
| 78 | def getsubtype(self): |
| 79 | return self.subtype |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | # Utility functions |
| 85 | # ----------------- |
| 86 | |
| 87 | |
| 88 | # Return a random string usable as a multipart boundary. |
| 89 | # The method used is so that it is *very* unlikely that the same |
| 90 | # string of characters will every occur again in the Universe, |
| 91 | # so the caller needn't check the data it is packing for the |
| 92 | # occurrence of the boundary. |
| 93 | # |
| 94 | # The boundary contains dots so you have to quote it in the header. |
| 95 | |
| 96 | _prefix = None |
| 97 | |
| 98 | def choose_boundary(): |
| 99 | global _generation, _prefix, _timestamp |
| 100 | import time |
| 101 | import rand |
| 102 | if _prefix == None: |
| 103 | import socket |
| 104 | import os |
| 105 | hostid = socket.gethostbyname(socket.gethostname()) |
| 106 | uid = `os.getuid()` |
| 107 | pid = `os.getpid()` |
| 108 | seed = `rand.rand()` |
| 109 | _prefix = hostid + '.' + uid + '.' + pid |
Guido van Rossum | fea2af1 | 1993-01-04 09:16:51 +0000 | [diff] [blame] | 110 | timestamp = `int(time.time())` |
Guido van Rossum | 01ca336 | 1992-07-13 14:28:59 +0000 | [diff] [blame] | 111 | seed = `rand.rand()` |
| 112 | return _prefix + '.' + timestamp + '.' + seed |