mimetools.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. """Various tools used by MIME-reading or MIME-writing programs."""
  2. import os
  3. import sys
  4. import tempfile
  5. from warnings import filterwarnings, catch_warnings
  6. with catch_warnings():
  7. if sys.py3kwarning:
  8. filterwarnings("ignore", ".*rfc822 has been removed", DeprecationWarning)
  9. import rfc822
  10. from warnings import warnpy3k
  11. warnpy3k("in 3.x, mimetools has been removed in favor of the email package",
  12. stacklevel=2)
  13. __all__ = ["Message","choose_boundary","encode","decode","copyliteral",
  14. "copybinary"]
  15. class Message(rfc822.Message):
  16. """A derived class of rfc822.Message that knows about MIME headers and
  17. contains some hooks for decoding encoded and multipart messages."""
  18. def __init__(self, fp, seekable = 1):
  19. rfc822.Message.__init__(self, fp, seekable)
  20. self.encodingheader = \
  21. self.getheader('content-transfer-encoding')
  22. self.typeheader = \
  23. self.getheader('content-type')
  24. self.parsetype()
  25. self.parseplist()
  26. def parsetype(self):
  27. str = self.typeheader
  28. if str is None:
  29. str = 'text/plain'
  30. if ';' in str:
  31. i = str.index(';')
  32. self.plisttext = str[i:]
  33. str = str[:i]
  34. else:
  35. self.plisttext = ''
  36. fields = str.split('/')
  37. for i in range(len(fields)):
  38. fields[i] = fields[i].strip().lower()
  39. self.type = '/'.join(fields)
  40. self.maintype = fields[0]
  41. self.subtype = '/'.join(fields[1:])
  42. def parseplist(self):
  43. str = self.plisttext
  44. self.plist = []
  45. while str[:1] == ';':
  46. str = str[1:]
  47. if ';' in str:
  48. # XXX Should parse quotes!
  49. end = str.index(';')
  50. else:
  51. end = len(str)
  52. f = str[:end]
  53. if '=' in f:
  54. i = f.index('=')
  55. f = f[:i].strip().lower() + \
  56. '=' + f[i+1:].strip()
  57. self.plist.append(f.strip())
  58. str = str[end:]
  59. def getplist(self):
  60. return self.plist
  61. def getparam(self, name):
  62. name = name.lower() + '='
  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. 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
  75. def getencoding(self):
  76. if self.encodingheader is None:
  77. return '7bit'
  78. return self.encodingheader.lower()
  79. def gettype(self):
  80. return self.type
  81. def getmaintype(self):
  82. return self.maintype
  83. def getsubtype(self):
  84. return self.subtype
  85. # Utility functions
  86. # -----------------
  87. try:
  88. import thread
  89. except ImportError:
  90. import dummy_thread as thread
  91. _counter_lock = thread.allocate_lock()
  92. del thread
  93. _counter = 0
  94. def _get_next_counter():
  95. global _counter
  96. _counter_lock.acquire()
  97. _counter += 1
  98. result = _counter
  99. _counter_lock.release()
  100. return result
  101. _prefix = None
  102. def choose_boundary():
  103. """Return a string usable as a multipart boundary.
  104. The string chosen is unique within a single program run, and
  105. incorporates the user id (if available), process id (if available),
  106. and current time. So it's very unlikely the returned string appears
  107. in message text, but there's no guarantee.
  108. The boundary contains dots so you have to quote it in the header."""
  109. global _prefix
  110. import time
  111. if _prefix is None:
  112. import socket
  113. try:
  114. hostid = socket.gethostbyname(socket.gethostname())
  115. except socket.gaierror:
  116. hostid = '127.0.0.1'
  117. try:
  118. uid = repr(os.getuid())
  119. except AttributeError:
  120. uid = '1'
  121. try:
  122. pid = repr(os.getpid())
  123. except AttributeError:
  124. pid = '1'
  125. _prefix = hostid + '.' + uid + '.' + pid
  126. return "%s.%.3f.%d" % (_prefix, time.time(), _get_next_counter())
  127. # Subroutines for decoding some common content-transfer-types
  128. def decode(input, output, encoding):
  129. """Decode common content-transfer-encodings (base64, quopri, uuencode)."""
  130. if encoding == 'base64':
  131. import base64
  132. return base64.decode(input, output)
  133. if encoding == 'quoted-printable':
  134. import quopri
  135. return quopri.decode(input, output)
  136. if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'):
  137. import uu
  138. return uu.decode(input, output)
  139. if encoding in ('7bit', '8bit'):
  140. return output.write(input.read())
  141. if encoding in decodetab:
  142. pipethrough(input, decodetab[encoding], output)
  143. else:
  144. raise ValueError, \
  145. 'unknown Content-Transfer-Encoding: %s' % encoding
  146. def encode(input, output, encoding):
  147. """Encode common content-transfer-encodings (base64, quopri, uuencode)."""
  148. if encoding == 'base64':
  149. import base64
  150. return base64.encode(input, output)
  151. if encoding == 'quoted-printable':
  152. import quopri
  153. return quopri.encode(input, output, 0)
  154. if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'):
  155. import uu
  156. return uu.encode(input, output)
  157. if encoding in ('7bit', '8bit'):
  158. return output.write(input.read())
  159. if encoding in encodetab:
  160. pipethrough(input, encodetab[encoding], output)
  161. else:
  162. raise ValueError, \
  163. 'unknown Content-Transfer-Encoding: %s' % encoding
  164. # The following is no longer used for standard encodings
  165. # XXX This requires that uudecode and mmencode are in $PATH
  166. uudecode_pipe = '''(
  167. TEMP=/tmp/@uu.$$
  168. sed "s%^begin [0-7][0-7]* .*%begin 600 $TEMP%" | uudecode
  169. cat $TEMP
  170. rm $TEMP
  171. )'''
  172. decodetab = {
  173. 'uuencode': uudecode_pipe,
  174. 'x-uuencode': uudecode_pipe,
  175. 'uue': uudecode_pipe,
  176. 'x-uue': uudecode_pipe,
  177. 'quoted-printable': 'mmencode -u -q',
  178. 'base64': 'mmencode -u -b',
  179. }
  180. encodetab = {
  181. 'x-uuencode': 'uuencode tempfile',
  182. 'uuencode': 'uuencode tempfile',
  183. 'x-uue': 'uuencode tempfile',
  184. 'uue': 'uuencode tempfile',
  185. 'quoted-printable': 'mmencode -q',
  186. 'base64': 'mmencode -b',
  187. }
  188. def pipeto(input, command):
  189. pipe = os.popen(command, 'w')
  190. copyliteral(input, pipe)
  191. pipe.close()
  192. def pipethrough(input, command, output):
  193. (fd, tempname) = tempfile.mkstemp()
  194. temp = os.fdopen(fd, 'w')
  195. copyliteral(input, temp)
  196. temp.close()
  197. pipe = os.popen(command + ' <' + tempname, 'r')
  198. copybinary(pipe, output)
  199. pipe.close()
  200. os.unlink(tempname)
  201. def copyliteral(input, output):
  202. while 1:
  203. line = input.readline()
  204. if not line: break
  205. output.write(line)
  206. def copybinary(input, output):
  207. BUFSIZE = 8192
  208. while 1:
  209. line = input.read(BUFSIZE)
  210. if not line: break
  211. output.write(line)