PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib-python/2.7/mimify.py

https://bitbucket.org/cpher/pypy
Python | 468 lines | 432 code | 10 blank | 26 comment | 12 complexity | 4bb88519bd81f14d8c0583cf4be1a832 MD5 | raw file
Possible License(s): Apache-2.0
  1. #! /usr/bin/env python
  2. """Mimification and unmimification of mail messages.
  3. Decode quoted-printable parts of a mail message or encode using
  4. quoted-printable.
  5. Usage:
  6. mimify(input, output)
  7. unmimify(input, output, decode_base64 = 0)
  8. to encode and decode respectively. Input and output may be the name
  9. of a file or an open file object. Only a readline() method is used
  10. on the input file, only a write() method is used on the output file.
  11. When using file names, the input and output file names may be the
  12. same.
  13. Interactive usage:
  14. mimify.py -e [infile [outfile]]
  15. mimify.py -d [infile [outfile]]
  16. to encode and decode respectively. Infile defaults to standard
  17. input and outfile to standard output.
  18. """
  19. # Configure
  20. MAXLEN = 200 # if lines longer than this, encode as quoted-printable
  21. CHARSET = 'ISO-8859-1' # default charset for non-US-ASCII mail
  22. QUOTE = '> ' # string replies are quoted with
  23. # End configure
  24. import re
  25. import warnings
  26. warnings.warn("the mimify module is deprecated; use the email package instead",
  27. DeprecationWarning, 2)
  28. __all__ = ["mimify","unmimify","mime_encode_header","mime_decode_header"]
  29. qp = re.compile('^content-transfer-encoding:\\s*quoted-printable', re.I)
  30. base64_re = re.compile('^content-transfer-encoding:\\s*base64', re.I)
  31. mp = re.compile('^content-type:.*multipart/.*boundary="?([^;"\n]*)', re.I|re.S)
  32. chrset = re.compile('^(content-type:.*charset=")(us-ascii|iso-8859-[0-9]+)(".*)', re.I|re.S)
  33. he = re.compile('^-*\n')
  34. mime_code = re.compile('=([0-9a-f][0-9a-f])', re.I)
  35. mime_head = re.compile('=\\?iso-8859-1\\?q\\?([^? \t\n]+)\\?=', re.I)
  36. repl = re.compile('^subject:\\s+re: ', re.I)
  37. class File:
  38. """A simple fake file object that knows about limited read-ahead and
  39. boundaries. The only supported method is readline()."""
  40. def __init__(self, file, boundary):
  41. self.file = file
  42. self.boundary = boundary
  43. self.peek = None
  44. def readline(self):
  45. if self.peek is not None:
  46. return ''
  47. line = self.file.readline()
  48. if not line:
  49. return line
  50. if self.boundary:
  51. if line == self.boundary + '\n':
  52. self.peek = line
  53. return ''
  54. if line == self.boundary + '--\n':
  55. self.peek = line
  56. return ''
  57. return line
  58. class HeaderFile:
  59. def __init__(self, file):
  60. self.file = file
  61. self.peek = None
  62. def readline(self):
  63. if self.peek is not None:
  64. line = self.peek
  65. self.peek = None
  66. else:
  67. line = self.file.readline()
  68. if not line:
  69. return line
  70. if he.match(line):
  71. return line
  72. while 1:
  73. self.peek = self.file.readline()
  74. if len(self.peek) == 0 or \
  75. (self.peek[0] != ' ' and self.peek[0] != '\t'):
  76. return line
  77. line = line + self.peek
  78. self.peek = None
  79. def mime_decode(line):
  80. """Decode a single line of quoted-printable text to 8bit."""
  81. newline = ''
  82. pos = 0
  83. while 1:
  84. res = mime_code.search(line, pos)
  85. if res is None:
  86. break
  87. newline = newline + line[pos:res.start(0)] + \
  88. chr(int(res.group(1), 16))
  89. pos = res.end(0)
  90. return newline + line[pos:]
  91. def mime_decode_header(line):
  92. """Decode a header line to 8bit."""
  93. newline = ''
  94. pos = 0
  95. while 1:
  96. res = mime_head.search(line, pos)
  97. if res is None:
  98. break
  99. match = res.group(1)
  100. # convert underscores to spaces (before =XX conversion!)
  101. match = ' '.join(match.split('_'))
  102. newline = newline + line[pos:res.start(0)] + mime_decode(match)
  103. pos = res.end(0)
  104. return newline + line[pos:]
  105. def unmimify_part(ifile, ofile, decode_base64 = 0):
  106. """Convert a quoted-printable part of a MIME mail message to 8bit."""
  107. multipart = None
  108. quoted_printable = 0
  109. is_base64 = 0
  110. is_repl = 0
  111. if ifile.boundary and ifile.boundary[:2] == QUOTE:
  112. prefix = QUOTE
  113. else:
  114. prefix = ''
  115. # read header
  116. hfile = HeaderFile(ifile)
  117. while 1:
  118. line = hfile.readline()
  119. if not line:
  120. return
  121. if prefix and line[:len(prefix)] == prefix:
  122. line = line[len(prefix):]
  123. pref = prefix
  124. else:
  125. pref = ''
  126. line = mime_decode_header(line)
  127. if qp.match(line):
  128. quoted_printable = 1
  129. continue # skip this header
  130. if decode_base64 and base64_re.match(line):
  131. is_base64 = 1
  132. continue
  133. ofile.write(pref + line)
  134. if not prefix and repl.match(line):
  135. # we're dealing with a reply message
  136. is_repl = 1
  137. mp_res = mp.match(line)
  138. if mp_res:
  139. multipart = '--' + mp_res.group(1)
  140. if he.match(line):
  141. break
  142. if is_repl and (quoted_printable or multipart):
  143. is_repl = 0
  144. # read body
  145. while 1:
  146. line = ifile.readline()
  147. if not line:
  148. return
  149. line = re.sub(mime_head, '\\1', line)
  150. if prefix and line[:len(prefix)] == prefix:
  151. line = line[len(prefix):]
  152. pref = prefix
  153. else:
  154. pref = ''
  155. ## if is_repl and len(line) >= 4 and line[:4] == QUOTE+'--' and line[-3:] != '--\n':
  156. ## multipart = line[:-1]
  157. while multipart:
  158. if line == multipart + '--\n':
  159. ofile.write(pref + line)
  160. multipart = None
  161. line = None
  162. break
  163. if line == multipart + '\n':
  164. ofile.write(pref + line)
  165. nifile = File(ifile, multipart)
  166. unmimify_part(nifile, ofile, decode_base64)
  167. line = nifile.peek
  168. if not line:
  169. # premature end of file
  170. break
  171. continue
  172. # not a boundary between parts
  173. break
  174. if line and quoted_printable:
  175. while line[-2:] == '=\n':
  176. line = line[:-2]
  177. newline = ifile.readline()
  178. if newline[:len(QUOTE)] == QUOTE:
  179. newline = newline[len(QUOTE):]
  180. line = line + newline
  181. line = mime_decode(line)
  182. if line and is_base64 and not pref:
  183. import base64
  184. line = base64.decodestring(line)
  185. if line:
  186. ofile.write(pref + line)
  187. def unmimify(infile, outfile, decode_base64 = 0):
  188. """Convert quoted-printable parts of a MIME mail message to 8bit."""
  189. if type(infile) == type(''):
  190. ifile = open(infile)
  191. if type(outfile) == type('') and infile == outfile:
  192. import os
  193. d, f = os.path.split(infile)
  194. os.rename(infile, os.path.join(d, ',' + f))
  195. else:
  196. ifile = infile
  197. if type(outfile) == type(''):
  198. ofile = open(outfile, 'w')
  199. else:
  200. ofile = outfile
  201. nifile = File(ifile, None)
  202. unmimify_part(nifile, ofile, decode_base64)
  203. ofile.flush()
  204. mime_char = re.compile('[=\177-\377]') # quote these chars in body
  205. mime_header_char = re.compile('[=?\177-\377]') # quote these in header
  206. def mime_encode(line, header):
  207. """Code a single line as quoted-printable.
  208. If header is set, quote some extra characters."""
  209. if header:
  210. reg = mime_header_char
  211. else:
  212. reg = mime_char
  213. newline = ''
  214. pos = 0
  215. if len(line) >= 5 and line[:5] == 'From ':
  216. # quote 'From ' at the start of a line for stupid mailers
  217. newline = ('=%02x' % ord('F')).upper()
  218. pos = 1
  219. while 1:
  220. res = reg.search(line, pos)
  221. if res is None:
  222. break
  223. newline = newline + line[pos:res.start(0)] + \
  224. ('=%02x' % ord(res.group(0))).upper()
  225. pos = res.end(0)
  226. line = newline + line[pos:]
  227. newline = ''
  228. while len(line) >= 75:
  229. i = 73
  230. while line[i] == '=' or line[i-1] == '=':
  231. i = i - 1
  232. i = i + 1
  233. newline = newline + line[:i] + '=\n'
  234. line = line[i:]
  235. return newline + line
  236. mime_header = re.compile('([ \t(]|^)([-a-zA-Z0-9_+]*[\177-\377][-a-zA-Z0-9_+\177-\377]*)(?=[ \t)]|\n)')
  237. def mime_encode_header(line):
  238. """Code a single header line as quoted-printable."""
  239. newline = ''
  240. pos = 0
  241. while 1:
  242. res = mime_header.search(line, pos)
  243. if res is None:
  244. break
  245. newline = '%s%s%s=?%s?Q?%s?=' % \
  246. (newline, line[pos:res.start(0)], res.group(1),
  247. CHARSET, mime_encode(res.group(2), 1))
  248. pos = res.end(0)
  249. return newline + line[pos:]
  250. mv = re.compile('^mime-version:', re.I)
  251. cte = re.compile('^content-transfer-encoding:', re.I)
  252. iso_char = re.compile('[\177-\377]')
  253. def mimify_part(ifile, ofile, is_mime):
  254. """Convert an 8bit part of a MIME mail message to quoted-printable."""
  255. has_cte = is_qp = is_base64 = 0
  256. multipart = None
  257. must_quote_body = must_quote_header = has_iso_chars = 0
  258. header = []
  259. header_end = ''
  260. message = []
  261. message_end = ''
  262. # read header
  263. hfile = HeaderFile(ifile)
  264. while 1:
  265. line = hfile.readline()
  266. if not line:
  267. break
  268. if not must_quote_header and iso_char.search(line):
  269. must_quote_header = 1
  270. if mv.match(line):
  271. is_mime = 1
  272. if cte.match(line):
  273. has_cte = 1
  274. if qp.match(line):
  275. is_qp = 1
  276. elif base64_re.match(line):
  277. is_base64 = 1
  278. mp_res = mp.match(line)
  279. if mp_res:
  280. multipart = '--' + mp_res.group(1)
  281. if he.match(line):
  282. header_end = line
  283. break
  284. header.append(line)
  285. # read body
  286. while 1:
  287. line = ifile.readline()
  288. if not line:
  289. break
  290. if multipart:
  291. if line == multipart + '--\n':
  292. message_end = line
  293. break
  294. if line == multipart + '\n':
  295. message_end = line
  296. break
  297. if is_base64:
  298. message.append(line)
  299. continue
  300. if is_qp:
  301. while line[-2:] == '=\n':
  302. line = line[:-2]
  303. newline = ifile.readline()
  304. if newline[:len(QUOTE)] == QUOTE:
  305. newline = newline[len(QUOTE):]
  306. line = line + newline
  307. line = mime_decode(line)
  308. message.append(line)
  309. if not has_iso_chars:
  310. if iso_char.search(line):
  311. has_iso_chars = must_quote_body = 1
  312. if not must_quote_body:
  313. if len(line) > MAXLEN:
  314. must_quote_body = 1
  315. # convert and output header and body
  316. for line in header:
  317. if must_quote_header:
  318. line = mime_encode_header(line)
  319. chrset_res = chrset.match(line)
  320. if chrset_res:
  321. if has_iso_chars:
  322. # change us-ascii into iso-8859-1
  323. if chrset_res.group(2).lower() == 'us-ascii':
  324. line = '%s%s%s' % (chrset_res.group(1),
  325. CHARSET,
  326. chrset_res.group(3))
  327. else:
  328. # change iso-8859-* into us-ascii
  329. line = '%sus-ascii%s' % chrset_res.group(1, 3)
  330. if has_cte and cte.match(line):
  331. line = 'Content-Transfer-Encoding: '
  332. if is_base64:
  333. line = line + 'base64\n'
  334. elif must_quote_body:
  335. line = line + 'quoted-printable\n'
  336. else:
  337. line = line + '7bit\n'
  338. ofile.write(line)
  339. if (must_quote_header or must_quote_body) and not is_mime:
  340. ofile.write('Mime-Version: 1.0\n')
  341. ofile.write('Content-Type: text/plain; ')
  342. if has_iso_chars:
  343. ofile.write('charset="%s"\n' % CHARSET)
  344. else:
  345. ofile.write('charset="us-ascii"\n')
  346. if must_quote_body and not has_cte:
  347. ofile.write('Content-Transfer-Encoding: quoted-printable\n')
  348. ofile.write(header_end)
  349. for line in message:
  350. if must_quote_body:
  351. line = mime_encode(line, 0)
  352. ofile.write(line)
  353. ofile.write(message_end)
  354. line = message_end
  355. while multipart:
  356. if line == multipart + '--\n':
  357. # read bit after the end of the last part
  358. while 1:
  359. line = ifile.readline()
  360. if not line:
  361. return
  362. if must_quote_body:
  363. line = mime_encode(line, 0)
  364. ofile.write(line)
  365. if line == multipart + '\n':
  366. nifile = File(ifile, multipart)
  367. mimify_part(nifile, ofile, 1)
  368. line = nifile.peek
  369. if not line:
  370. # premature end of file
  371. break
  372. ofile.write(line)
  373. continue
  374. # unexpectedly no multipart separator--copy rest of file
  375. while 1:
  376. line = ifile.readline()
  377. if not line:
  378. return
  379. if must_quote_body:
  380. line = mime_encode(line, 0)
  381. ofile.write(line)
  382. def mimify(infile, outfile):
  383. """Convert 8bit parts of a MIME mail message to quoted-printable."""
  384. if type(infile) == type(''):
  385. ifile = open(infile)
  386. if type(outfile) == type('') and infile == outfile:
  387. import os
  388. d, f = os.path.split(infile)
  389. os.rename(infile, os.path.join(d, ',' + f))
  390. else:
  391. ifile = infile
  392. if type(outfile) == type(''):
  393. ofile = open(outfile, 'w')
  394. else:
  395. ofile = outfile
  396. nifile = File(ifile, None)
  397. mimify_part(nifile, ofile, 0)
  398. ofile.flush()
  399. import sys
  400. if __name__ == '__main__' or (len(sys.argv) > 0 and sys.argv[0] == 'mimify'):
  401. import getopt
  402. usage = 'Usage: mimify [-l len] -[ed] [infile [outfile]]'
  403. decode_base64 = 0
  404. opts, args = getopt.getopt(sys.argv[1:], 'l:edb')
  405. if len(args) not in (0, 1, 2):
  406. print usage
  407. sys.exit(1)
  408. if (('-e', '') in opts) == (('-d', '') in opts) or \
  409. ((('-b', '') in opts) and (('-d', '') not in opts)):
  410. print usage
  411. sys.exit(1)
  412. for o, a in opts:
  413. if o == '-e':
  414. encode = mimify
  415. elif o == '-d':
  416. encode = unmimify
  417. elif o == '-l':
  418. try:
  419. MAXLEN = int(a)
  420. except (ValueError, OverflowError):
  421. print usage
  422. sys.exit(1)
  423. elif o == '-b':
  424. decode_base64 = 1
  425. if len(args) == 0:
  426. encode_args = (sys.stdin, sys.stdout)
  427. elif len(args) == 1:
  428. encode_args = (args[0], sys.stdout)
  429. else:
  430. encode_args = (args[0], args[1])
  431. if decode_base64:
  432. encode_args = encode_args + (decode_base64,)
  433. encode(*encode_args)