blob: 06016cdea7c08064b655ac167c06594a23aadfb7 [file] [log] [blame]
Barry Warsaw40ef0062006-03-18 15:41:53 +00001# Copyright (C) 2001-2006 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
Barry Warsaw40ef0062006-03-18 15:41:53 +00007__all__ = [
8 'encode_7or8bit',
9 'encode_base64',
10 'encode_noop',
11 'encode_quopri',
12 ]
13
Barry Warsawba925802001-09-23 03:17:28 +000014import base64
Barry Warsaw40ef0062006-03-18 15:41:53 +000015
Barry Warsawbb113862004-10-03 03:16:19 +000016from quopri import encodestring as _encodestring
Barry Warsawba925802001-09-23 03:17:28 +000017
Barry Warsaw40ef0062006-03-18 15:41:53 +000018
19
Barry Warsawbb113862004-10-03 03:16:19 +000020def _qencode(s):
21 enc = _encodestring(s, quotetabs=True)
22 # Must encode spaces, which quopri.encodestring() doesn't do
23 return enc.replace(' ', '=20')
Barry Warsawba925802001-09-23 03:17:28 +000024
Barry Warsaw6f70c412001-09-26 05:26:22 +000025
Barry Warsawba925802001-09-23 03:17:28 +000026def _bencode(s):
27 # We can't quite use base64.encodestring() since it tacks on a "courtesy
28 # newline". Blech!
29 if not s:
30 return s
31 hasnewline = (s[-1] == '\n')
32 value = base64.encodestring(s)
33 if not hasnewline and value[-1] == '\n':
34 return value[:-1]
35 return value
36
37
Barry Warsawe968ead2001-10-04 17:05:11 +000038
Barry Warsawba925802001-09-23 03:17:28 +000039def encode_base64(msg):
40 """Encode the message's payload in Base64.
41
Barry Warsaw12272a22002-10-01 00:05:24 +000042 Also, add an appropriate Content-Transfer-Encoding header.
Barry Warsawba925802001-09-23 03:17:28 +000043 """
44 orig = msg.get_payload()
45 encdata = _bencode(orig)
46 msg.set_payload(encdata)
47 msg['Content-Transfer-Encoding'] = 'base64'
48
49
Barry Warsawe968ead2001-10-04 17:05:11 +000050
Barry Warsawba925802001-09-23 03:17:28 +000051def encode_quopri(msg):
Barry Warsaw12272a22002-10-01 00:05:24 +000052 """Encode the message's payload in quoted-printable.
Barry Warsawba925802001-09-23 03:17:28 +000053
Barry Warsaw12272a22002-10-01 00:05:24 +000054 Also, add an appropriate Content-Transfer-Encoding header.
Barry Warsawba925802001-09-23 03:17:28 +000055 """
56 orig = msg.get_payload()
57 encdata = _qencode(orig)
58 msg.set_payload(encdata)
59 msg['Content-Transfer-Encoding'] = 'quoted-printable'
60
61
Barry Warsawe968ead2001-10-04 17:05:11 +000062
Barry Warsawba925802001-09-23 03:17:28 +000063def encode_7or8bit(msg):
Barry Warsaw12272a22002-10-01 00:05:24 +000064 """Set the Content-Transfer-Encoding header to 7bit or 8bit."""
Barry Warsawba925802001-09-23 03:17:28 +000065 orig = msg.get_payload()
Barry Warsaw409a4c02002-04-10 21:01:31 +000066 if orig is None:
67 # There's no payload. For backwards compatibility we use 7bit
68 msg['Content-Transfer-Encoding'] = '7bit'
69 return
Barry Warsawba925802001-09-23 03:17:28 +000070 # We play a trick to make this go fast. If encoding to ASCII succeeds, we
71 # know the data must be 7bit, otherwise treat it as 8bit.
72 try:
73 orig.encode('ascii')
74 except UnicodeError:
Barry Warsaw61e56162004-05-13 22:50:12 +000075 # iso-2022-* is non-ASCII but still 7-bit
76 charset = msg.get_charset()
77 output_cset = charset and charset.output_charset
78 if output_cset and output_cset.lower().startswith('iso-2202-'):
79 msg['Content-Transfer-Encoding'] = '7bit'
80 else:
81 msg['Content-Transfer-Encoding'] = '8bit'
Barry Warsawba925802001-09-23 03:17:28 +000082 else:
83 msg['Content-Transfer-Encoding'] = '7bit'
84
85
Barry Warsawe968ead2001-10-04 17:05:11 +000086
Barry Warsawba925802001-09-23 03:17:28 +000087def encode_noop(msg):
88 """Do nothing."""