| 1 | """Various tools used by MIME-reading or MIME-writing programs."""
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 | import os
|
|---|
| 5 | import rfc822
|
|---|
| 6 | import tempfile
|
|---|
| 7 |
|
|---|
| 8 | __all__ = ["Message","choose_boundary","encode","decode","copyliteral",
|
|---|
| 9 | "copybinary"]
|
|---|
| 10 |
|
|---|
| 11 | class Message(rfc822.Message):
|
|---|
| 12 | """A derived class of rfc822.Message that knows about MIME headers and
|
|---|
| 13 | contains some hooks for decoding encoded and multipart messages."""
|
|---|
| 14 |
|
|---|
| 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()
|
|---|
| 23 |
|
|---|
| 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:])
|
|---|
| 40 |
|
|---|
| 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:]
|
|---|
| 58 |
|
|---|
| 59 | def getplist(self):
|
|---|
| 60 | return self.plist
|
|---|
| 61 |
|
|---|
| 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
|
|---|
| 69 |
|
|---|
| 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
|
|---|
| 77 |
|
|---|
| 78 | def getencoding(self):
|
|---|
| 79 | if self.encodingheader is None:
|
|---|
| 80 | return '7bit'
|
|---|
| 81 | return self.encodingheader.lower()
|
|---|
| 82 |
|
|---|
| 83 | def gettype(self):
|
|---|
| 84 | return self.type
|
|---|
| 85 |
|
|---|
| 86 | def getmaintype(self):
|
|---|
| 87 | return self.maintype
|
|---|
| 88 |
|
|---|
| 89 | def getsubtype(self):
|
|---|
| 90 | return self.subtype
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 | # Utility functions
|
|---|
| 96 | # -----------------
|
|---|
| 97 |
|
|---|
| 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
|
|---|
| 113 |
|
|---|
| 114 | _prefix = None
|
|---|
| 115 |
|
|---|
| 116 | def choose_boundary():
|
|---|
| 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
|
|---|
|
|---|