| 1 | n/a | # Copyright (C) 2001-2006 Python Software Foundation |
|---|
| 2 | n/a | # Author: Barry Warsaw |
|---|
| 3 | n/a | # Contact: email-sig@python.org |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | """Encodings and related functions.""" |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | __all__ = [ |
|---|
| 8 | n/a | 'encode_7or8bit', |
|---|
| 9 | n/a | 'encode_base64', |
|---|
| 10 | n/a | 'encode_noop', |
|---|
| 11 | n/a | 'encode_quopri', |
|---|
| 12 | n/a | ] |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | from base64 import encodebytes as _bencode |
|---|
| 16 | n/a | from quopri import encodestring as _encodestring |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | def _qencode(s): |
|---|
| 21 | n/a | enc = _encodestring(s, quotetabs=True) |
|---|
| 22 | n/a | # Must encode spaces, which quopri.encodestring() doesn't do |
|---|
| 23 | n/a | return enc.replace(b' ', b'=20') |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | def encode_base64(msg): |
|---|
| 27 | n/a | """Encode the message's payload in Base64. |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | Also, add an appropriate Content-Transfer-Encoding header. |
|---|
| 30 | n/a | """ |
|---|
| 31 | n/a | orig = msg.get_payload(decode=True) |
|---|
| 32 | n/a | encdata = str(_bencode(orig), 'ascii') |
|---|
| 33 | n/a | msg.set_payload(encdata) |
|---|
| 34 | n/a | msg['Content-Transfer-Encoding'] = 'base64' |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | def encode_quopri(msg): |
|---|
| 39 | n/a | """Encode the message's payload in quoted-printable. |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | Also, add an appropriate Content-Transfer-Encoding header. |
|---|
| 42 | n/a | """ |
|---|
| 43 | n/a | orig = msg.get_payload(decode=True) |
|---|
| 44 | n/a | encdata = _qencode(orig) |
|---|
| 45 | n/a | msg.set_payload(encdata) |
|---|
| 46 | n/a | msg['Content-Transfer-Encoding'] = 'quoted-printable' |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | def encode_7or8bit(msg): |
|---|
| 51 | n/a | """Set the Content-Transfer-Encoding header to 7bit or 8bit.""" |
|---|
| 52 | n/a | orig = msg.get_payload(decode=True) |
|---|
| 53 | n/a | if orig is None: |
|---|
| 54 | n/a | # There's no payload. For backwards compatibility we use 7bit |
|---|
| 55 | n/a | msg['Content-Transfer-Encoding'] = '7bit' |
|---|
| 56 | n/a | return |
|---|
| 57 | n/a | # We play a trick to make this go fast. If decoding from ASCII succeeds, |
|---|
| 58 | n/a | # we know the data must be 7bit, otherwise treat it as 8bit. |
|---|
| 59 | n/a | try: |
|---|
| 60 | n/a | orig.decode('ascii') |
|---|
| 61 | n/a | except UnicodeError: |
|---|
| 62 | n/a | msg['Content-Transfer-Encoding'] = '8bit' |
|---|
| 63 | n/a | else: |
|---|
| 64 | n/a | msg['Content-Transfer-Encoding'] = '7bit' |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | def encode_noop(msg): |
|---|
| 69 | n/a | """Do nothing.""" |
|---|