blob: baac2a345883e548164dc313bf46f615e2ec5039 [file] [log] [blame]
Barry Warsaw61e56162004-05-13 22:50:12 +00001# Copyright (C) 2001-2004 Python Software Foundation
Barry Warsawbb113862004-10-03 03:16:19 +00002# Author: Barry Warsaw
3# Contact: email-sig@python.org
Barry Warsawba925802001-09-23 03:17:28 +00004
Barry Warsaw61e56162004-05-13 22:50:12 +00005"""Encodings and related functions."""
Barry Warsawba925802001-09-23 03:17:28 +00006
7import base64
Barry Warsawbb113862004-10-03 03:16:19 +00008from quopri import encodestring as _encodestring
Barry Warsawba925802001-09-23 03:17:28 +00009
Barry Warsawbb113862004-10-03 03:16:19 +000010def _qencode(s):
11 enc = _encodestring(s, quotetabs=True)
12 # Must encode spaces, which quopri.encodestring() doesn't do
13 return enc.replace(' ', '=20')
Barry Warsawba925802001-09-23 03:17:28 +000014
Barry Warsaw6f70c412001-09-26 05:26:22 +000015
Barry Warsawba925802001-09-23 03:17:28 +000016def _bencode(s):
17 # We can't quite use base64.encodestring() since it tacks on a "courtesy
18 # newline". Blech!
19 if not s:
20 return s
21 hasnewline = (s[-1] == '\n')
22 value = base64.encodestring(s)
23 if not hasnewline and value[-1] == '\n':
24 return value[:-1]
25 return value
26
27
Barry Warsawe968ead2001-10-04 17:05:11 +000028
Barry Warsawba925802001-09-23 03:17:28 +000029def encode_base64(msg):
30 """Encode the message's payload in Base64.
31
Barry Warsaw12272a22002-10-01 00:05:24 +000032 Also, add an appropriate Content-Transfer-Encoding header.
Barry Warsawba925802001-09-23 03:17:28 +000033 """
34 orig = msg.get_payload()
35 encdata = _bencode(orig)
36 msg.set_payload(encdata)
37 msg['Content-Transfer-Encoding'] = 'base64'
38
39
Barry Warsawe968ead2001-10-04 17:05:11 +000040
Barry Warsawba925802001-09-23 03:17:28 +000041def encode_quopri(msg):
Barry Warsaw12272a22002-10-01 00:05:24 +000042 """Encode the message's payload in quoted-printable.
Barry Warsawba925802001-09-23 03:17:28 +000043
Barry Warsaw12272a22002-10-01 00:05:24 +000044 Also, add an appropriate Content-Transfer-Encoding header.
Barry Warsawba925802001-09-23 03:17:28 +000045 """
46 orig = msg.get_payload()
47 encdata = _qencode(orig)
48 msg.set_payload(encdata)
49 msg['Content-Transfer-Encoding'] = 'quoted-printable'
50
51
Barry Warsawe968ead2001-10-04 17:05:11 +000052
Barry Warsawba925802001-09-23 03:17:28 +000053def encode_7or8bit(msg):
Barry Warsaw12272a22002-10-01 00:05:24 +000054 """Set the Content-Transfer-Encoding header to 7bit or 8bit."""
Barry Warsawba925802001-09-23 03:17:28 +000055 orig = msg.get_payload()
Barry Warsaw409a4c02002-04-10 21:01:31 +000056 if orig is None:
57 # There's no payload. For backwards compatibility we use 7bit
58 msg['Content-Transfer-Encoding'] = '7bit'
59 return
Barry Warsawba925802001-09-23 03:17:28 +000060 # We play a trick to make this go fast. If encoding to ASCII succeeds, we
61 # know the data must be 7bit, otherwise treat it as 8bit.
62 try:
63 orig.encode('ascii')
64 except UnicodeError:
Barry Warsaw61e56162004-05-13 22:50:12 +000065 # iso-2022-* is non-ASCII but still 7-bit
66 charset = msg.get_charset()
67 output_cset = charset and charset.output_charset
68 if output_cset and output_cset.lower().startswith('iso-2202-'):
69 msg['Content-Transfer-Encoding'] = '7bit'
70 else:
71 msg['Content-Transfer-Encoding'] = '8bit'
Barry Warsawba925802001-09-23 03:17:28 +000072 else:
73 msg['Content-Transfer-Encoding'] = '7bit'
74
75
Barry Warsawe968ead2001-10-04 17:05:11 +000076
Barry Warsawba925802001-09-23 03:17:28 +000077def encode_noop(msg):
78 """Do nothing."""