| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 1 | # Copyright (C) 2001,2002 Python Software Foundation | 
| Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 2 | # Author: che@debian.org (Ben Gertzfield), barry@zope.com (Barry Warsaw) | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 3 |  | 
| Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 4 | from types import UnicodeType | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 5 | from email.Encoders import encode_7or8bit | 
 | 6 | import email.base64MIME | 
 | 7 | import email.quopriMIME | 
 | 8 |  | 
| Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 9 | def _isunicode(s): | 
 | 10 |     return isinstance(s, UnicodeType) | 
 | 11 |  | 
 | 12 | # Python 2.2.1 and beyond has these symbols | 
 | 13 | try: | 
 | 14 |     True, False | 
 | 15 | except NameError: | 
 | 16 |     True = 1 | 
 | 17 |     False = 0 | 
 | 18 |  | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 19 |  | 
 | 20 |  | 
 | 21 | # Flags for types of header encodings | 
| Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 22 | QP     = 1   # Quoted-Printable | 
 | 23 | BASE64 = 2   # Base64 | 
 | 24 | SHORTEST = 3 # the shorter of QP and base64, but only for headers | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 25 |  | 
 | 26 | # In "=?charset?q?hello_world?=", the =?, ?q?, and ?= add up to 7 | 
| Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 27 | MISC_LEN = 7 | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 28 |  | 
 | 29 | DEFAULT_CHARSET = 'us-ascii' | 
 | 30 |  | 
 | 31 |  | 
 | 32 |  | 
 | 33 | # Defaults | 
 | 34 | CHARSETS = { | 
 | 35 |     # input        header enc  body enc output conv | 
| Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 36 |     'iso-8859-1':  (QP,        QP,      None), | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 37 |     'iso-8859-2':  (QP,        QP,      None), | 
 | 38 |     'us-ascii':    (None,      None,    None), | 
 | 39 |     'big5':        (BASE64,    BASE64,  None), | 
| Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 40 |     'gb2312':      (BASE64,    BASE64,  None), | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 41 |     'euc-jp':      (BASE64,    None,    'iso-2022-jp'), | 
 | 42 |     'shift_jis':   (BASE64,    None,    'iso-2022-jp'), | 
 | 43 |     'iso-2022-jp': (BASE64,    None,    None), | 
 | 44 |     'koi8-r':      (BASE64,    BASE64,  None), | 
| Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 45 |     'utf-8':       (SHORTEST,  BASE64, 'utf-8'), | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 46 |     } | 
 | 47 |  | 
 | 48 | # Aliases for other commonly-used names for character sets.  Map | 
 | 49 | # them to the real ones used in email. | 
 | 50 | ALIASES = { | 
 | 51 |     'latin_1': 'iso-8859-1', | 
 | 52 |     'latin-1': 'iso-8859-1', | 
 | 53 |     'ascii':   'us-ascii', | 
 | 54 |     } | 
 | 55 |  | 
 | 56 | # Map charsets to their Unicode codec strings.  Note that the Japanese | 
 | 57 | # examples included below do not (yet) come with Python!  They are available | 
 | 58 | # from http://pseudo.grad.sccs.chukyo-u.ac.jp/~kajiyama/python/ | 
 | 59 |  | 
 | 60 | # The Chinese and Korean codecs are available from SourceForge: | 
 | 61 | # | 
 | 62 | #     http://sourceforge.net/projects/python-codecs/ | 
 | 63 | # | 
 | 64 | # although you'll need to check them out of cvs since they haven't been file | 
 | 65 | # released yet.  You might also try to use | 
 | 66 | # | 
 | 67 | #     http://www.freshports.org/port-description.php3?port=6702 | 
 | 68 | # | 
 | 69 | # if you can get logged in.  AFAICT, both the Chinese and Korean codecs are | 
 | 70 | # fairly experimental at this point. | 
 | 71 | CODEC_MAP = { | 
 | 72 |     'euc-jp':      'japanese.euc-jp', | 
 | 73 |     'iso-2022-jp': 'japanese.iso-2022-jp', | 
 | 74 |     'shift_jis':   'japanese.shift_jis', | 
 | 75 |     'gb2132':      'eucgb2312_cn', | 
 | 76 |     'big5':        'big5_tw', | 
 | 77 |     'utf-8':       'utf-8', | 
 | 78 |     # Hack: We don't want *any* conversion for stuff marked us-ascii, as all | 
 | 79 |     # sorts of garbage might be sent to us in the guise of 7-bit us-ascii. | 
 | 80 |     # Let that stuff pass through without conversion to/from Unicode. | 
 | 81 |     'us-ascii':    None, | 
 | 82 |     } | 
 | 83 |  | 
 | 84 |  | 
 | 85 |  | 
 | 86 | # Convenience functions for extending the above mappings | 
 | 87 | def add_charset(charset, header_enc=None, body_enc=None, output_charset=None): | 
| Barry Warsaw | 12272a2 | 2002-10-01 00:05:24 +0000 | [diff] [blame] | 88 |     """Add character set properties to the global registry. | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 89 |  | 
 | 90 |     charset is the input character set, and must be the canonical name of a | 
 | 91 |     character set. | 
 | 92 |  | 
 | 93 |     Optional header_enc and body_enc is either Charset.QP for | 
| Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 94 |     quoted-printable, Charset.BASE64 for base64 encoding, Charset.SHORTEST for | 
 | 95 |     the shortest of qp or base64 encoding, or None for no encoding.  SHORTEST | 
 | 96 |     is only valid for header_enc.  It describes how message headers and | 
 | 97 |     message bodies in the input charset are to be encoded.  Default is no | 
 | 98 |     encoding. | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 99 |  | 
 | 100 |     Optional output_charset is the character set that the output should be | 
 | 101 |     in.  Conversions will proceed from input charset, to Unicode, to the | 
 | 102 |     output charset when the method Charset.convert() is called.  The default | 
 | 103 |     is to output in the same character set as the input. | 
 | 104 |  | 
 | 105 |     Both input_charset and output_charset must have Unicode codec entries in | 
 | 106 |     the module's charset-to-codec mapping; use add_codec(charset, codecname) | 
| Barry Warsaw | 12272a2 | 2002-10-01 00:05:24 +0000 | [diff] [blame] | 107 |     to add codecs the module does not know about.  See the codecs module's | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 108 |     documentation for more information. | 
 | 109 |     """ | 
| Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 110 |     if body_enc == SHORTEST: | 
 | 111 |         raise ValueError, 'SHORTEST not allowed for body_enc' | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 112 |     CHARSETS[charset] = (header_enc, body_enc, output_charset) | 
 | 113 |  | 
 | 114 |  | 
 | 115 | def add_alias(alias, canonical): | 
 | 116 |     """Add a character set alias. | 
 | 117 |  | 
 | 118 |     alias is the alias name, e.g. latin-1 | 
 | 119 |     canonical is the character set's canonical name, e.g. iso-8859-1 | 
 | 120 |     """ | 
 | 121 |     ALIASES[alias] = canonical | 
 | 122 |  | 
 | 123 |  | 
 | 124 | def add_codec(charset, codecname): | 
 | 125 |     """Add a codec that map characters in the given charset to/from Unicode. | 
 | 126 |  | 
 | 127 |     charset is the canonical name of a character set.  codecname is the name | 
 | 128 |     of a Python codec, as appropriate for the second argument to the unicode() | 
| Barry Warsaw | 12272a2 | 2002-10-01 00:05:24 +0000 | [diff] [blame] | 129 |     built-in, or to the encode() method of a Unicode string. | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 130 |     """ | 
 | 131 |     CODEC_MAP[charset] = codecname | 
 | 132 |  | 
 | 133 |  | 
 | 134 |  | 
 | 135 | class Charset: | 
 | 136 |     """Map character sets to their email properties. | 
 | 137 |  | 
 | 138 |     This class provides information about the requirements imposed on email | 
 | 139 |     for a specific character set.  It also provides convenience routines for | 
 | 140 |     converting between character sets, given the availability of the | 
| Barry Warsaw | 12272a2 | 2002-10-01 00:05:24 +0000 | [diff] [blame] | 141 |     applicable codecs.  Given a character set, it will do its best to provide | 
 | 142 |     information on how to use that character set in an email in an | 
 | 143 |     RFC-compliant way. | 
| Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 144 |  | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 145 |     Certain character sets must be encoded with quoted-printable or base64 | 
 | 146 |     when used in email headers or bodies.  Certain character sets must be | 
 | 147 |     converted outright, and are not allowed in email.  Instances of this | 
 | 148 |     module expose the following information about a character set: | 
 | 149 |  | 
 | 150 |     input_charset: The initial character set specified.  Common aliases | 
 | 151 |                    are converted to their `official' email names (e.g. latin_1 | 
 | 152 |                    is converted to iso-8859-1).  Defaults to 7-bit us-ascii. | 
 | 153 |  | 
 | 154 |     header_encoding: If the character set must be encoded before it can be | 
 | 155 |                      used in an email header, this attribute will be set to | 
| Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 156 |                      Charset.QP (for quoted-printable), Charset.BASE64 (for | 
 | 157 |                      base64 encoding), or Charset.SHORTEST for the shortest of | 
 | 158 |                      QP or BASE64 encoding.  Otherwise, it will be None. | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 159 |  | 
 | 160 |     body_encoding: Same as header_encoding, but describes the encoding for the | 
 | 161 |                    mail message's body, which indeed may be different than the | 
| Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 162 |                    header encoding.  Charset.SHORTEST is not allowed for | 
 | 163 |                    body_encoding. | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 164 |  | 
 | 165 |     output_charset: Some character sets must be converted before the can be | 
 | 166 |                     used in email headers or bodies.  If the input_charset is | 
 | 167 |                     one of them, this attribute will contain the name of the | 
 | 168 |                     charset output will be converted to.  Otherwise, it will | 
 | 169 |                     be None. | 
 | 170 |  | 
 | 171 |     input_codec: The name of the Python codec used to convert the | 
 | 172 |                  input_charset to Unicode.  If no conversion codec is | 
 | 173 |                  necessary, this attribute will be None. | 
 | 174 |  | 
 | 175 |     output_codec: The name of the Python codec used to convert Unicode | 
 | 176 |                   to the output_charset.  If no conversion codec is necessary, | 
 | 177 |                   this attribute will have the same value as the input_codec. | 
 | 178 |     """ | 
 | 179 |     def __init__(self, input_charset=DEFAULT_CHARSET): | 
 | 180 |         # Set the input charset after filtering through the aliases | 
 | 181 |         self.input_charset = ALIASES.get(input_charset, input_charset) | 
 | 182 |         # We can try to guess which encoding and conversion to use by the | 
 | 183 |         # charset_map dictionary.  Try that first, but let the user override | 
 | 184 |         # it. | 
 | 185 |         henc, benc, conv = CHARSETS.get(self.input_charset, | 
| Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 186 |                                         (SHORTEST, SHORTEST, None)) | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 187 |         # Set the attributes, allowing the arguments to override the default. | 
 | 188 |         self.header_encoding = henc | 
 | 189 |         self.body_encoding = benc | 
 | 190 |         self.output_charset = ALIASES.get(conv, conv) | 
 | 191 |         # Now set the codecs.  If one isn't defined for input_charset, | 
 | 192 |         # guess and try a Unicode codec with the same name as input_codec. | 
 | 193 |         self.input_codec = CODEC_MAP.get(self.input_charset, | 
 | 194 |                                          self.input_charset) | 
 | 195 |         self.output_codec = CODEC_MAP.get(self.output_charset, | 
 | 196 |                                             self.input_codec) | 
 | 197 |  | 
 | 198 |     def __str__(self): | 
 | 199 |         return self.input_charset.lower() | 
 | 200 |  | 
 | 201 |     def __eq__(self, other): | 
 | 202 |         return str(self) == str(other).lower() | 
 | 203 |  | 
 | 204 |     def __ne__(self, other): | 
 | 205 |         return not self.__eq__(other) | 
 | 206 |  | 
 | 207 |     def get_body_encoding(self): | 
 | 208 |         """Return the content-transfer-encoding used for body encoding. | 
 | 209 |  | 
 | 210 |         This is either the string `quoted-printable' or `base64' depending on | 
 | 211 |         the encoding used, or it is a function in which case you should call | 
 | 212 |         the function with a single argument, the Message object being | 
| Barry Warsaw | 12272a2 | 2002-10-01 00:05:24 +0000 | [diff] [blame] | 213 |         encoded.  The function should then set the Content-Transfer-Encoding | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 214 |         header itself to whatever is appropriate. | 
 | 215 |  | 
 | 216 |         Returns "quoted-printable" if self.body_encoding is QP. | 
 | 217 |         Returns "base64" if self.body_encoding is BASE64. | 
 | 218 |         Returns "7bit" otherwise. | 
 | 219 |         """ | 
| Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 220 |         assert self.body_encoding <> SHORTEST | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 221 |         if self.body_encoding == QP: | 
 | 222 |             return 'quoted-printable' | 
 | 223 |         elif self.body_encoding == BASE64: | 
 | 224 |             return 'base64' | 
 | 225 |         else: | 
 | 226 |             return encode_7or8bit | 
 | 227 |  | 
 | 228 |     def convert(self, s): | 
 | 229 |         """Convert a string from the input_codec to the output_codec.""" | 
 | 230 |         if self.input_codec <> self.output_codec: | 
 | 231 |             return unicode(s, self.input_codec).encode(self.output_codec) | 
 | 232 |         else: | 
 | 233 |             return s | 
 | 234 |  | 
 | 235 |     def to_splittable(self, s): | 
 | 236 |         """Convert a possibly multibyte string to a safely splittable format. | 
 | 237 |  | 
 | 238 |         Uses the input_codec to try and convert the string to Unicode, so it | 
| Barry Warsaw | 12272a2 | 2002-10-01 00:05:24 +0000 | [diff] [blame] | 239 |         can be safely split on character boundaries (even for multibyte | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 240 |         characters). | 
 | 241 |  | 
| Barry Warsaw | 12272a2 | 2002-10-01 00:05:24 +0000 | [diff] [blame] | 242 |         Returns the string as-is if it isn't known how to convert it to | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 243 |         Unicode with the input_charset. | 
 | 244 |  | 
 | 245 |         Characters that could not be converted to Unicode will be replaced | 
 | 246 |         with the Unicode replacement character U+FFFD. | 
 | 247 |         """ | 
| Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 248 |         if _isunicode(s) or self.input_codec is None: | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 249 |             return s | 
 | 250 |         try: | 
 | 251 |             return unicode(s, self.input_codec, 'replace') | 
 | 252 |         except LookupError: | 
 | 253 |             # Input codec not installed on system, so return the original | 
 | 254 |             # string unchanged. | 
 | 255 |             return s | 
 | 256 |  | 
| Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 257 |     def from_splittable(self, ustr, to_output=True): | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 258 |         """Convert a splittable string back into an encoded string. | 
 | 259 |  | 
| Barry Warsaw | 12272a2 | 2002-10-01 00:05:24 +0000 | [diff] [blame] | 260 |         Uses the proper codec to try and convert the string from Unicode back | 
 | 261 |         into an encoded format.  Return the string as-is if it is not Unicode, | 
 | 262 |         or if it could not be converted from Unicode. | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 263 |  | 
 | 264 |         Characters that could not be converted from Unicode will be replaced | 
 | 265 |         with an appropriate character (usually '?'). | 
 | 266 |  | 
| Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 267 |         If to_output is True (the default), uses output_codec to convert to an | 
 | 268 |         encoded format.  If to_output is False, uses input_codec. | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 269 |         """ | 
 | 270 |         if to_output: | 
 | 271 |             codec = self.output_codec | 
 | 272 |         else: | 
 | 273 |             codec = self.input_codec | 
| Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 274 |         if not _isunicode(ustr) or codec is None: | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 275 |             return ustr | 
 | 276 |         try: | 
 | 277 |             return ustr.encode(codec, 'replace') | 
 | 278 |         except LookupError: | 
 | 279 |             # Output codec not installed | 
 | 280 |             return ustr | 
 | 281 |  | 
 | 282 |     def get_output_charset(self): | 
 | 283 |         """Return the output character set. | 
 | 284 |  | 
| Barry Warsaw | 12272a2 | 2002-10-01 00:05:24 +0000 | [diff] [blame] | 285 |         This is self.output_charset if that is not None, otherwise it is | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 286 |         self.input_charset. | 
 | 287 |         """ | 
 | 288 |         return self.output_charset or self.input_charset | 
 | 289 |  | 
 | 290 |     def encoded_header_len(self, s): | 
 | 291 |         """Return the length of the encoded header string.""" | 
 | 292 |         cset = self.get_output_charset() | 
 | 293 |         # The len(s) of a 7bit encoding is len(s) | 
| Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 294 |         if self.header_encoding == BASE64: | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 295 |             return email.base64MIME.base64_len(s) + len(cset) + MISC_LEN | 
| Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 296 |         elif self.header_encoding == QP: | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 297 |             return email.quopriMIME.header_quopri_len(s) + len(cset) + MISC_LEN | 
| Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 298 |         elif self.header_encoding == SHORTEST: | 
 | 299 |             lenb64 = email.base64MIME.base64_len(s) | 
 | 300 |             lenqp = email.quopriMIME.header_quopri_len(s) | 
 | 301 |             return min(lenb64, lenqp) + len(cset) + MISC_LEN | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 302 |         else: | 
 | 303 |             return len(s) | 
 | 304 |  | 
| Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 305 |     def header_encode(self, s, convert=False): | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 306 |         """Header-encode a string, optionally converting it to output_charset. | 
 | 307 |  | 
| Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 308 |         If convert is True, the string will be converted from the input | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 309 |         charset to the output charset automatically.  This is not useful for | 
 | 310 |         multibyte character sets, which have line length issues (multibyte | 
 | 311 |         characters must be split on a character, not a byte boundary); use the | 
 | 312 |         high-level Header class to deal with these issues.  convert defaults | 
| Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 313 |         to False. | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 314 |  | 
 | 315 |         The type of encoding (base64 or quoted-printable) will be based on | 
 | 316 |         self.header_encoding. | 
 | 317 |         """ | 
 | 318 |         cset = self.get_output_charset() | 
 | 319 |         if convert: | 
 | 320 |             s = self.convert(s) | 
 | 321 |         # 7bit/8bit encodings return the string unchanged (modulo conversions) | 
| Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 322 |         if self.header_encoding == BASE64: | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 323 |             return email.base64MIME.header_encode(s, cset) | 
| Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 324 |         elif self.header_encoding == QP: | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 325 |             return email.quopriMIME.header_encode(s, cset) | 
| Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 326 |         elif self.header_encoding == SHORTEST: | 
 | 327 |             lenb64 = email.base64MIME.base64_len(s) | 
 | 328 |             lenqp = email.quopriMIME.header_quopri_len(s) | 
 | 329 |             if lenb64 < lenqp: | 
 | 330 |                 return email.base64MIME.header_encode(s, cset) | 
 | 331 |             else: | 
 | 332 |                 return email.quopriMIME.header_encode(s, cset) | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 333 |         else: | 
 | 334 |             return s | 
 | 335 |  | 
| Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 336 |     def body_encode(self, s, convert=True): | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 337 |         """Body-encode a string and convert it to output_charset. | 
 | 338 |  | 
| Barry Warsaw | 5932c9b | 2002-09-28 17:47:56 +0000 | [diff] [blame] | 339 |         If convert is True (the default), the string will be converted from | 
| Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 340 |         the input charset to output charset automatically.  Unlike | 
 | 341 |         header_encode(), there are no issues with byte boundaries and | 
 | 342 |         multibyte charsets in email bodies, so this is usually pretty safe. | 
 | 343 |  | 
 | 344 |         The type of encoding (base64 or quoted-printable) will be based on | 
 | 345 |         self.body_encoding. | 
 | 346 |         """ | 
 | 347 |         if convert: | 
 | 348 |             s = self.convert(s) | 
 | 349 |         # 7bit/8bit encodings return the string unchanged (module conversions) | 
 | 350 |         if self.body_encoding is BASE64: | 
 | 351 |             return email.base64MIME.body_encode(s) | 
 | 352 |         elif self.header_encoding is QP: | 
 | 353 |             return email.quopriMIME.body_encode(s) | 
 | 354 |         else: | 
 | 355 |             return s |