jpayne@69: # Copyright (C) 2001-2007 Python Software Foundation jpayne@69: # Author: Ben Gertzfield, Barry Warsaw jpayne@69: # Contact: email-sig@python.org jpayne@69: jpayne@69: __all__ = [ jpayne@69: 'Charset', jpayne@69: 'add_alias', jpayne@69: 'add_charset', jpayne@69: 'add_codec', jpayne@69: ] jpayne@69: jpayne@69: from functools import partial jpayne@69: jpayne@69: import email.base64mime jpayne@69: import email.quoprimime jpayne@69: jpayne@69: from email import errors jpayne@69: from email.encoders import encode_7or8bit jpayne@69: jpayne@69: jpayne@69: jpayne@69: # Flags for types of header encodings jpayne@69: QP = 1 # Quoted-Printable jpayne@69: BASE64 = 2 # Base64 jpayne@69: SHORTEST = 3 # the shorter of QP and base64, but only for headers jpayne@69: jpayne@69: # In "=?charset?q?hello_world?=", the =?, ?q?, and ?= add up to 7 jpayne@69: RFC2047_CHROME_LEN = 7 jpayne@69: jpayne@69: DEFAULT_CHARSET = 'us-ascii' jpayne@69: UNKNOWN8BIT = 'unknown-8bit' jpayne@69: EMPTYSTRING = '' jpayne@69: jpayne@69: jpayne@69: jpayne@69: # Defaults jpayne@69: CHARSETS = { jpayne@69: # input header enc body enc output conv jpayne@69: 'iso-8859-1': (QP, QP, None), jpayne@69: 'iso-8859-2': (QP, QP, None), jpayne@69: 'iso-8859-3': (QP, QP, None), jpayne@69: 'iso-8859-4': (QP, QP, None), jpayne@69: # iso-8859-5 is Cyrillic, and not especially used jpayne@69: # iso-8859-6 is Arabic, also not particularly used jpayne@69: # iso-8859-7 is Greek, QP will not make it readable jpayne@69: # iso-8859-8 is Hebrew, QP will not make it readable jpayne@69: 'iso-8859-9': (QP, QP, None), jpayne@69: 'iso-8859-10': (QP, QP, None), jpayne@69: # iso-8859-11 is Thai, QP will not make it readable jpayne@69: 'iso-8859-13': (QP, QP, None), jpayne@69: 'iso-8859-14': (QP, QP, None), jpayne@69: 'iso-8859-15': (QP, QP, None), jpayne@69: 'iso-8859-16': (QP, QP, None), jpayne@69: 'windows-1252':(QP, QP, None), jpayne@69: 'viscii': (QP, QP, None), jpayne@69: 'us-ascii': (None, None, None), jpayne@69: 'big5': (BASE64, BASE64, None), jpayne@69: 'gb2312': (BASE64, BASE64, None), jpayne@69: 'euc-jp': (BASE64, None, 'iso-2022-jp'), jpayne@69: 'shift_jis': (BASE64, None, 'iso-2022-jp'), jpayne@69: 'iso-2022-jp': (BASE64, None, None), jpayne@69: 'koi8-r': (BASE64, BASE64, None), jpayne@69: 'utf-8': (SHORTEST, BASE64, 'utf-8'), jpayne@69: } jpayne@69: jpayne@69: # Aliases for other commonly-used names for character sets. Map jpayne@69: # them to the real ones used in email. jpayne@69: ALIASES = { jpayne@69: 'latin_1': 'iso-8859-1', jpayne@69: 'latin-1': 'iso-8859-1', jpayne@69: 'latin_2': 'iso-8859-2', jpayne@69: 'latin-2': 'iso-8859-2', jpayne@69: 'latin_3': 'iso-8859-3', jpayne@69: 'latin-3': 'iso-8859-3', jpayne@69: 'latin_4': 'iso-8859-4', jpayne@69: 'latin-4': 'iso-8859-4', jpayne@69: 'latin_5': 'iso-8859-9', jpayne@69: 'latin-5': 'iso-8859-9', jpayne@69: 'latin_6': 'iso-8859-10', jpayne@69: 'latin-6': 'iso-8859-10', jpayne@69: 'latin_7': 'iso-8859-13', jpayne@69: 'latin-7': 'iso-8859-13', jpayne@69: 'latin_8': 'iso-8859-14', jpayne@69: 'latin-8': 'iso-8859-14', jpayne@69: 'latin_9': 'iso-8859-15', jpayne@69: 'latin-9': 'iso-8859-15', jpayne@69: 'latin_10':'iso-8859-16', jpayne@69: 'latin-10':'iso-8859-16', jpayne@69: 'cp949': 'ks_c_5601-1987', jpayne@69: 'euc_jp': 'euc-jp', jpayne@69: 'euc_kr': 'euc-kr', jpayne@69: 'ascii': 'us-ascii', jpayne@69: } jpayne@69: jpayne@69: jpayne@69: # Map charsets to their Unicode codec strings. jpayne@69: CODEC_MAP = { jpayne@69: 'gb2312': 'eucgb2312_cn', jpayne@69: 'big5': 'big5_tw', jpayne@69: # Hack: We don't want *any* conversion for stuff marked us-ascii, as all jpayne@69: # sorts of garbage might be sent to us in the guise of 7-bit us-ascii. jpayne@69: # Let that stuff pass through without conversion to/from Unicode. jpayne@69: 'us-ascii': None, jpayne@69: } jpayne@69: jpayne@69: jpayne@69: jpayne@69: # Convenience functions for extending the above mappings jpayne@69: def add_charset(charset, header_enc=None, body_enc=None, output_charset=None): jpayne@69: """Add character set properties to the global registry. jpayne@69: jpayne@69: charset is the input character set, and must be the canonical name of a jpayne@69: character set. jpayne@69: jpayne@69: Optional header_enc and body_enc is either Charset.QP for jpayne@69: quoted-printable, Charset.BASE64 for base64 encoding, Charset.SHORTEST for jpayne@69: the shortest of qp or base64 encoding, or None for no encoding. SHORTEST jpayne@69: is only valid for header_enc. It describes how message headers and jpayne@69: message bodies in the input charset are to be encoded. Default is no jpayne@69: encoding. jpayne@69: jpayne@69: Optional output_charset is the character set that the output should be jpayne@69: in. Conversions will proceed from input charset, to Unicode, to the jpayne@69: output charset when the method Charset.convert() is called. The default jpayne@69: is to output in the same character set as the input. jpayne@69: jpayne@69: Both input_charset and output_charset must have Unicode codec entries in jpayne@69: the module's charset-to-codec mapping; use add_codec(charset, codecname) jpayne@69: to add codecs the module does not know about. See the codecs module's jpayne@69: documentation for more information. jpayne@69: """ jpayne@69: if body_enc == SHORTEST: jpayne@69: raise ValueError('SHORTEST not allowed for body_enc') jpayne@69: CHARSETS[charset] = (header_enc, body_enc, output_charset) jpayne@69: jpayne@69: jpayne@69: def add_alias(alias, canonical): jpayne@69: """Add a character set alias. jpayne@69: jpayne@69: alias is the alias name, e.g. latin-1 jpayne@69: canonical is the character set's canonical name, e.g. iso-8859-1 jpayne@69: """ jpayne@69: ALIASES[alias] = canonical jpayne@69: jpayne@69: jpayne@69: def add_codec(charset, codecname): jpayne@69: """Add a codec that map characters in the given charset to/from Unicode. jpayne@69: jpayne@69: charset is the canonical name of a character set. codecname is the name jpayne@69: of a Python codec, as appropriate for the second argument to the unicode() jpayne@69: built-in, or to the encode() method of a Unicode string. jpayne@69: """ jpayne@69: CODEC_MAP[charset] = codecname jpayne@69: jpayne@69: jpayne@69: jpayne@69: # Convenience function for encoding strings, taking into account jpayne@69: # that they might be unknown-8bit (ie: have surrogate-escaped bytes) jpayne@69: def _encode(string, codec): jpayne@69: if codec == UNKNOWN8BIT: jpayne@69: return string.encode('ascii', 'surrogateescape') jpayne@69: else: jpayne@69: return string.encode(codec) jpayne@69: jpayne@69: jpayne@69: jpayne@69: class Charset: jpayne@69: """Map character sets to their email properties. jpayne@69: jpayne@69: This class provides information about the requirements imposed on email jpayne@69: for a specific character set. It also provides convenience routines for jpayne@69: converting between character sets, given the availability of the jpayne@69: applicable codecs. Given a character set, it will do its best to provide jpayne@69: information on how to use that character set in an email in an jpayne@69: RFC-compliant way. jpayne@69: jpayne@69: Certain character sets must be encoded with quoted-printable or base64 jpayne@69: when used in email headers or bodies. Certain character sets must be jpayne@69: converted outright, and are not allowed in email. Instances of this jpayne@69: module expose the following information about a character set: jpayne@69: jpayne@69: input_charset: The initial character set specified. Common aliases jpayne@69: are converted to their `official' email names (e.g. latin_1 jpayne@69: is converted to iso-8859-1). Defaults to 7-bit us-ascii. jpayne@69: jpayne@69: header_encoding: If the character set must be encoded before it can be jpayne@69: used in an email header, this attribute will be set to jpayne@69: Charset.QP (for quoted-printable), Charset.BASE64 (for jpayne@69: base64 encoding), or Charset.SHORTEST for the shortest of jpayne@69: QP or BASE64 encoding. Otherwise, it will be None. jpayne@69: jpayne@69: body_encoding: Same as header_encoding, but describes the encoding for the jpayne@69: mail message's body, which indeed may be different than the jpayne@69: header encoding. Charset.SHORTEST is not allowed for jpayne@69: body_encoding. jpayne@69: jpayne@69: output_charset: Some character sets must be converted before they can be jpayne@69: used in email headers or bodies. If the input_charset is jpayne@69: one of them, this attribute will contain the name of the jpayne@69: charset output will be converted to. Otherwise, it will jpayne@69: be None. jpayne@69: jpayne@69: input_codec: The name of the Python codec used to convert the jpayne@69: input_charset to Unicode. If no conversion codec is jpayne@69: necessary, this attribute will be None. jpayne@69: jpayne@69: output_codec: The name of the Python codec used to convert Unicode jpayne@69: to the output_charset. If no conversion codec is necessary, jpayne@69: this attribute will have the same value as the input_codec. jpayne@69: """ jpayne@69: def __init__(self, input_charset=DEFAULT_CHARSET): jpayne@69: # RFC 2046, $4.1.2 says charsets are not case sensitive. We coerce to jpayne@69: # unicode because its .lower() is locale insensitive. If the argument jpayne@69: # is already a unicode, we leave it at that, but ensure that the jpayne@69: # charset is ASCII, as the standard (RFC XXX) requires. jpayne@69: try: jpayne@69: if isinstance(input_charset, str): jpayne@69: input_charset.encode('ascii') jpayne@69: else: jpayne@69: input_charset = str(input_charset, 'ascii') jpayne@69: except UnicodeError: jpayne@69: raise errors.CharsetError(input_charset) jpayne@69: input_charset = input_charset.lower() jpayne@69: # Set the input charset after filtering through the aliases jpayne@69: self.input_charset = ALIASES.get(input_charset, input_charset) jpayne@69: # We can try to guess which encoding and conversion to use by the jpayne@69: # charset_map dictionary. Try that first, but let the user override jpayne@69: # it. jpayne@69: henc, benc, conv = CHARSETS.get(self.input_charset, jpayne@69: (SHORTEST, BASE64, None)) jpayne@69: if not conv: jpayne@69: conv = self.input_charset jpayne@69: # Set the attributes, allowing the arguments to override the default. jpayne@69: self.header_encoding = henc jpayne@69: self.body_encoding = benc jpayne@69: self.output_charset = ALIASES.get(conv, conv) jpayne@69: # Now set the codecs. If one isn't defined for input_charset, jpayne@69: # guess and try a Unicode codec with the same name as input_codec. jpayne@69: self.input_codec = CODEC_MAP.get(self.input_charset, jpayne@69: self.input_charset) jpayne@69: self.output_codec = CODEC_MAP.get(self.output_charset, jpayne@69: self.output_charset) jpayne@69: jpayne@69: def __repr__(self): jpayne@69: return self.input_charset.lower() jpayne@69: jpayne@69: def __eq__(self, other): jpayne@69: return str(self) == str(other).lower() jpayne@69: jpayne@69: def get_body_encoding(self): jpayne@69: """Return the content-transfer-encoding used for body encoding. jpayne@69: jpayne@69: This is either the string `quoted-printable' or `base64' depending on jpayne@69: the encoding used, or it is a function in which case you should call jpayne@69: the function with a single argument, the Message object being jpayne@69: encoded. The function should then set the Content-Transfer-Encoding jpayne@69: header itself to whatever is appropriate. jpayne@69: jpayne@69: Returns "quoted-printable" if self.body_encoding is QP. jpayne@69: Returns "base64" if self.body_encoding is BASE64. jpayne@69: Returns conversion function otherwise. jpayne@69: """ jpayne@69: assert self.body_encoding != SHORTEST jpayne@69: if self.body_encoding == QP: jpayne@69: return 'quoted-printable' jpayne@69: elif self.body_encoding == BASE64: jpayne@69: return 'base64' jpayne@69: else: jpayne@69: return encode_7or8bit jpayne@69: jpayne@69: def get_output_charset(self): jpayne@69: """Return the output character set. jpayne@69: jpayne@69: This is self.output_charset if that is not None, otherwise it is jpayne@69: self.input_charset. jpayne@69: """ jpayne@69: return self.output_charset or self.input_charset jpayne@69: jpayne@69: def header_encode(self, string): jpayne@69: """Header-encode a string by converting it first to bytes. jpayne@69: jpayne@69: The type of encoding (base64 or quoted-printable) will be based on jpayne@69: this charset's `header_encoding`. jpayne@69: jpayne@69: :param string: A unicode string for the header. It must be possible jpayne@69: to encode this string to bytes using the character set's jpayne@69: output codec. jpayne@69: :return: The encoded string, with RFC 2047 chrome. jpayne@69: """ jpayne@69: codec = self.output_codec or 'us-ascii' jpayne@69: header_bytes = _encode(string, codec) jpayne@69: # 7bit/8bit encodings return the string unchanged (modulo conversions) jpayne@69: encoder_module = self._get_encoder(header_bytes) jpayne@69: if encoder_module is None: jpayne@69: return string jpayne@69: return encoder_module.header_encode(header_bytes, codec) jpayne@69: jpayne@69: def header_encode_lines(self, string, maxlengths): jpayne@69: """Header-encode a string by converting it first to bytes. jpayne@69: jpayne@69: This is similar to `header_encode()` except that the string is fit jpayne@69: into maximum line lengths as given by the argument. jpayne@69: jpayne@69: :param string: A unicode string for the header. It must be possible jpayne@69: to encode this string to bytes using the character set's jpayne@69: output codec. jpayne@69: :param maxlengths: Maximum line length iterator. Each element jpayne@69: returned from this iterator will provide the next maximum line jpayne@69: length. This parameter is used as an argument to built-in next() jpayne@69: and should never be exhausted. The maximum line lengths should jpayne@69: not count the RFC 2047 chrome. These line lengths are only a jpayne@69: hint; the splitter does the best it can. jpayne@69: :return: Lines of encoded strings, each with RFC 2047 chrome. jpayne@69: """ jpayne@69: # See which encoding we should use. jpayne@69: codec = self.output_codec or 'us-ascii' jpayne@69: header_bytes = _encode(string, codec) jpayne@69: encoder_module = self._get_encoder(header_bytes) jpayne@69: encoder = partial(encoder_module.header_encode, charset=codec) jpayne@69: # Calculate the number of characters that the RFC 2047 chrome will jpayne@69: # contribute to each line. jpayne@69: charset = self.get_output_charset() jpayne@69: extra = len(charset) + RFC2047_CHROME_LEN jpayne@69: # Now comes the hard part. We must encode bytes but we can't split on jpayne@69: # bytes because some character sets are variable length and each jpayne@69: # encoded word must stand on its own. So the problem is you have to jpayne@69: # encode to bytes to figure out this word's length, but you must split jpayne@69: # on characters. This causes two problems: first, we don't know how jpayne@69: # many octets a specific substring of unicode characters will get jpayne@69: # encoded to, and second, we don't know how many ASCII characters jpayne@69: # those octets will get encoded to. Unless we try it. Which seems jpayne@69: # inefficient. In the interest of being correct rather than fast (and jpayne@69: # in the hope that there will be few encoded headers in any such jpayne@69: # message), brute force it. :( jpayne@69: lines = [] jpayne@69: current_line = [] jpayne@69: maxlen = next(maxlengths) - extra jpayne@69: for character in string: jpayne@69: current_line.append(character) jpayne@69: this_line = EMPTYSTRING.join(current_line) jpayne@69: length = encoder_module.header_length(_encode(this_line, charset)) jpayne@69: if length > maxlen: jpayne@69: # This last character doesn't fit so pop it off. jpayne@69: current_line.pop() jpayne@69: # Does nothing fit on the first line? jpayne@69: if not lines and not current_line: jpayne@69: lines.append(None) jpayne@69: else: jpayne@69: separator = (' ' if lines else '') jpayne@69: joined_line = EMPTYSTRING.join(current_line) jpayne@69: header_bytes = _encode(joined_line, codec) jpayne@69: lines.append(encoder(header_bytes)) jpayne@69: current_line = [character] jpayne@69: maxlen = next(maxlengths) - extra jpayne@69: joined_line = EMPTYSTRING.join(current_line) jpayne@69: header_bytes = _encode(joined_line, codec) jpayne@69: lines.append(encoder(header_bytes)) jpayne@69: return lines jpayne@69: jpayne@69: def _get_encoder(self, header_bytes): jpayne@69: if self.header_encoding == BASE64: jpayne@69: return email.base64mime jpayne@69: elif self.header_encoding == QP: jpayne@69: return email.quoprimime jpayne@69: elif self.header_encoding == SHORTEST: jpayne@69: len64 = email.base64mime.header_length(header_bytes) jpayne@69: lenqp = email.quoprimime.header_length(header_bytes) jpayne@69: if len64 < lenqp: jpayne@69: return email.base64mime jpayne@69: else: jpayne@69: return email.quoprimime jpayne@69: else: jpayne@69: return None jpayne@69: jpayne@69: def body_encode(self, string): jpayne@69: """Body-encode a string by converting it first to bytes. jpayne@69: jpayne@69: The type of encoding (base64 or quoted-printable) will be based on jpayne@69: self.body_encoding. If body_encoding is None, we assume the jpayne@69: output charset is a 7bit encoding, so re-encoding the decoded jpayne@69: string using the ascii codec produces the correct string version jpayne@69: of the content. jpayne@69: """ jpayne@69: if not string: jpayne@69: return string jpayne@69: if self.body_encoding is BASE64: jpayne@69: if isinstance(string, str): jpayne@69: string = string.encode(self.output_charset) jpayne@69: return email.base64mime.body_encode(string) jpayne@69: elif self.body_encoding is QP: jpayne@69: # quopromime.body_encode takes a string, but operates on it as if jpayne@69: # it were a list of byte codes. For a (minimal) history on why jpayne@69: # this is so, see changeset 0cf700464177. To correctly encode a jpayne@69: # character set, then, we must turn it into pseudo bytes via the jpayne@69: # latin1 charset, which will encode any byte as a single code point jpayne@69: # between 0 and 255, which is what body_encode is expecting. jpayne@69: if isinstance(string, str): jpayne@69: string = string.encode(self.output_charset) jpayne@69: string = string.decode('latin1') jpayne@69: return email.quoprimime.body_encode(string) jpayne@69: else: jpayne@69: if isinstance(string, str): jpayne@69: string = string.encode(self.output_charset).decode('ascii') jpayne@69: return string