| 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:
|
|---|
|
|---|