786 lines · 29.5 KB · Python
RawDownload
# Copyright (C) 2001-2006 Python Software Foundation# Author: Barry Warsaw# Contact: email-sig@python.org """Basic message object for the email package object model.""" __all__ = ['Message'] import reimport uuimport binasciiimport warningsfrom cStringIO import StringIO # Intrapackage importsimport email.charsetfrom email import utilsfrom email import errors SEMISPACE = '; ' # Regular expression used to split header parameters.  BAW: this may be too# simple.  It isn't strictly RFC 2045 (section 5.1) compliant, but it catches# most headers found in the wild.  We may eventually need a full fledged# parser eventually.paramre = re.compile(r'\s*;\s*')# Regular expression that matches `special' characters in parameters, the# existance of which force quoting of the parameter value.tspecials = re.compile(r'[ \(\)<>@,;:\\"/\[\]\?=]')  # Helper functionsdef _formatparam(param, value=None, quote=True):    """Convenience function to format and return a key=value pair.     This will quote the value if needed or if quote is true.    """    if value is not None and len(value) > 0:        # A tuple is used for RFC 2231 encoded parameter values where items        # are (charset, language, value).  charset is a string, not a Charset        # instance.        if isinstance(value, tuple):            # Encode as per RFC 2231            param += '*'            value = utils.encode_rfc2231(value[2], value[0], value[1])        # BAW: Please check this.  I think that if quote is set it should        # force quoting even if not necessary.        if quote or tspecials.search(value):            return '%s="%s"' % (param, utils.quote(value))        else:            return '%s=%s' % (param, value)    else:        return param def _parseparam(s):    plist = []    while s[:1] == ';':        s = s[1:]        end = s.find(';')        while end > 0 and s.count('"', 0, end) % 2:            end = s.find(';', end + 1)        if end < 0:            end = len(s)        f = s[:end]        if '=' in f:            i = f.index('=')            f = f[:i].strip().lower() + '=' + f[i+1:].strip()        plist.append(f.strip())        s = s[end:]    return plist  def _unquotevalue(value):    # This is different than utils.collapse_rfc2231_value() because it doesn't    # try to convert the value to a unicode.  Message.get_param() and    # Message.get_params() are both currently defined to return the tuple in    # the face of RFC 2231 parameters.    if isinstance(value, tuple):        return value[0], value[1], utils.unquote(value[2])    else:        return utils.unquote(value)  class Message:    """Basic message object.     A message object is defined as something that has a bunch of RFC 2822    headers and a payload.  It may optionally have an envelope header    (a.k.a. Unix-From or From_ header).  If the message is a container (i.e. a    multipart or a message/rfc822), then the payload is a list of Message    objects, otherwise it is a string.     Message objects implement part of the `mapping' interface, which assumes    there is exactly one occurrance of the header per message.  Some headers    do in fact appear multiple times (e.g. Received) and for those headers,    you must use the explicit API to set or get all the headers.  Not all of    the mapping methods are implemented.    """    def __init__(self):        self._headers = []        self._unixfrom = None        self._payload = None        self._charset = None        # Defaults for multipart messages        self.preamble = self.epilogue = None        self.defects = []        # Default content type        self._default_type = 'text/plain'     def __str__(self):        """Return the entire formatted message as a string.        This includes the headers, body, and envelope header.        """        return self.as_string(unixfrom=True)     def as_string(self, unixfrom=False):        """Return the entire formatted message as a string.        Optional `unixfrom' when True, means include the Unix From_ envelope        header.         This is a convenience method and may not generate the message exactly        as you intend because by default it mangles lines that begin with        "From ".  For more flexibility, use the flatten() method of a        Generator instance.        """        from email.Generator import Generator        fp = StringIO()        g = Generator(fp)        g.flatten(self, unixfrom=unixfrom)        return fp.getvalue()     def is_multipart(self):        """Return True if the message consists of multiple parts."""        return isinstance(self._payload, list)     #    # Unix From_ line    #    def set_unixfrom(self, unixfrom):        self._unixfrom = unixfrom     def get_unixfrom(self):        return self._unixfrom     #    # Payload manipulation.    #    def attach(self, payload):        """Add the given payload to the current payload.         The current payload will always be a list of objects after this method        is called.  If you want to set the payload to a scalar object, use        set_payload() instead.        """        if self._payload is None:            self._payload = [payload]        else:            self._payload.append(payload)     def get_payload(self, i=None, decode=False):        """Return a reference to the payload.         The payload will either be a list object or a string.  If you mutate        the list object, you modify the message's payload in place.  Optional        i returns that index into the payload.         Optional decode is a flag indicating whether the payload should be        decoded or not, according to the Content-Transfer-Encoding header        (default is False).         When True and the message is not a multipart, the payload will be        decoded if this header's value is `quoted-printable' or `base64'.  If        some other encoding is used, or the header is missing, or if the        payload has bogus data (i.e. bogus base64 or uuencoded data), the        payload is returned as-is.         If the message is a multipart and the decode flag is True, then None        is returned.        """        if i is None:            payload = self._payload        elif not isinstance(self._payload, list):            raise TypeError('Expected list, got %s' % type(self._payload))        else:            payload = self._payload[i]        if decode:            if self.is_multipart():                return None            cte = self.get('content-transfer-encoding', '').lower()            if cte == 'quoted-printable':                return utils._qdecode(payload)            elif cte == 'base64':                try:                    return utils._bdecode(payload)                except binascii.Error:                    # Incorrect padding                    return payload            elif cte in ('x-uuencode', 'uuencode', 'uue', 'x-uue'):                sfp = StringIO()                try:                    uu.decode(StringIO(payload+'\n'), sfp, quiet=True)                    payload = sfp.getvalue()                except uu.Error:                    # Some decoding problem                    return payload        # Everything else, including encodings with 8bit or 7bit are returned        # unchanged.        return payload     def set_payload(self, payload, charset=None):        """Set the payload to the given value.         Optional charset sets the message's default character set.  See        set_charset() for details.        """        self._payload = payload        if charset is not None:            self.set_charset(charset)     def set_charset(self, charset):        """Set the charset of the payload to a given character set.         charset can be a Charset instance, a string naming a character set, or        None.  If it is a string it will be converted to a Charset instance.        If charset is None, the charset parameter will be removed from the        Content-Type field.  Anything else will generate a TypeError.         The message will be assumed to be of type text/* encoded with        charset.input_charset.  It will be converted to charset.output_charset        and encoded properly, if needed, when generating the plain text        representation of the message.  MIME headers (MIME-Version,        Content-Type, Content-Transfer-Encoding) will be added as needed.         """        if charset is None:            self.del_param('charset')            self._charset = None            return        if isinstance(charset, basestring):            charset = email.charset.Charset(charset)        if not isinstance(charset, email.charset.Charset):            raise TypeError(charset)        # BAW: should we accept strings that can serve as arguments to the        # Charset constructor?        self._charset = charset        if not self.has_key('MIME-Version'):            self.add_header('MIME-Version', '1.0')        if not self.has_key('Content-Type'):            self.add_header('Content-Type', 'text/plain',                            charset=charset.get_output_charset())        else:            self.set_param('charset', charset.get_output_charset())        if str(charset) <> charset.get_output_charset():            self._payload = charset.body_encode(self._payload)        if not self.has_key('Content-Transfer-Encoding'):            cte = charset.get_body_encoding()            try:                cte(self)            except TypeError:                self._payload = charset.body_encode(self._payload)                self.add_header('Content-Transfer-Encoding', cte)     def get_charset(self):        """Return the Charset instance associated with the message's payload.        """        return self._charset     #    # MAPPING INTERFACE (partial)    #    def __len__(self):        """Return the total number of headers, including duplicates."""        return len(self._headers)     def __getitem__(self, name):        """Get a header value.         Return None if the header is missing instead of raising an exception.         Note that if the header appeared multiple times, exactly which        occurrance gets returned is undefined.  Use get_all() to get all        the values matching a header field name.        """        return self.get(name)     def __setitem__(self, name, val):        """Set the value of a header.         Note: this does not overwrite an existing header with the same field        name.  Use __delitem__() first to delete any existing headers.        """        self._headers.append((name, val))     def __delitem__(self, name):        """Delete all occurrences of a header, if present.         Does not raise an exception if the header is missing.        """        name = name.lower()        newheaders = []        for k, v in self._headers:            if k.lower() <> name:                newheaders.append((k, v))        self._headers = newheaders     def __contains__(self, name):        return name.lower() in [k.lower() for k, v in self._headers]     def has_key(self, name):        """Return true if the message contains the header."""        missing = object()        return self.get(name, missing) is not missing     def keys(self):        """Return a list of all the message's header field names.         These will be sorted in the order they appeared in the original        message, or were added to the message, and may contain duplicates.        Any fields deleted and re-inserted are always appended to the header        list.        """        return [k for k, v in self._headers]     def values(self):        """Return a list of all the message's header values.         These will be sorted in the order they appeared in the original        message, or were added to the message, and may contain duplicates.        Any fields deleted and re-inserted are always appended to the header        list.        """        return [v for k, v in self._headers]     def items(self):        """Get all the message's header fields and values.         These will be sorted in the order they appeared in the original        message, or were added to the message, and may contain duplicates.        Any fields deleted and re-inserted are always appended to the header        list.        """        return self._headers[:]     def get(self, name, failobj=None):        """Get a header value.         Like __getitem__() but return failobj instead of None when the field        is missing.        """        name = name.lower()        for k, v in self._headers:            if k.lower() == name:                return v        return failobj     #    # Additional useful stuff    #     def get_all(self, name, failobj=None):        """Return a list of all the values for the named field.         These will be sorted in the order they appeared in the original        message, and may contain duplicates.  Any fields deleted and        re-inserted are always appended to the header list.         If no such fields exist, failobj is returned (defaults to None).        """        values = []        name = name.lower()        for k, v in self._headers:            if k.lower() == name:                values.append(v)        if not values:            return failobj        return values     def add_header(self, _name, _value, **_params):        """Extended header setting.         name is the header field to add.  keyword arguments can be used to set        additional parameters for the header field, with underscores converted        to dashes.  Normally the parameter will be added as key="value" unless        value is None, in which case only the key will be added.         Example:         msg.add_header('content-disposition', 'attachment', filename='bud.gif')        """        parts = []        for k, v in _params.items():            if v is None:                parts.append(k.replace('_', '-'))            else:                parts.append(_formatparam(k.replace('_', '-'), v))        if _value is not None:            parts.insert(0, _value)        self._headers.append((_name, SEMISPACE.join(parts)))     def replace_header(self, _name, _value):        """Replace a header.         Replace the first matching header found in the message, retaining        header order and case.  If no matching header was found, a KeyError is        raised.        """        _name = _name.lower()        for i, (k, v) in zip(range(len(self._headers)), self._headers):            if k.lower() == _name:                self._headers[i] = (k, _value)                break        else:            raise KeyError(_name)     #    # Use these three methods instead of the three above.    #     def get_content_type(self):        """Return the message's content type.         The returned string is coerced to lower case of the form        `maintype/subtype'.  If there was no Content-Type header in the        message, the default type as given by get_default_type() will be        returned.  Since according to RFC 2045, messages always have a default        type this will always return a value.         RFC 2045 defines a message's default type to be text/plain unless it        appears inside a multipart/digest container, in which case it would be        message/rfc822.        """        missing = object()        value = self.get('content-type', missing)        if value is missing:            # This should have no parameters            return self.get_default_type()        ctype = paramre.split(value)[0].lower().strip()        # RFC 2045, section 5.2 says if its invalid, use text/plain        if ctype.count('/') <> 1:            return 'text/plain'        return ctype     def get_content_maintype(self):        """Return the message's main content type.         This is the `maintype' part of the string returned by        get_content_type().        """        ctype = self.get_content_type()        return ctype.split('/')[0]     def get_content_subtype(self):        """Returns the message's sub-content type.         This is the `subtype' part of the string returned by        get_content_type().        """        ctype = self.get_content_type()        return ctype.split('/')[1]     def get_default_type(self):        """Return the `default' content type.         Most messages have a default content type of text/plain, except for        messages that are subparts of multipart/digest containers.  Such        subparts have a default content type of message/rfc822.        """        return self._default_type     def set_default_type(self, ctype):        """Set the `default' content type.         ctype should be either "text/plain" or "message/rfc822", although this        is not enforced.  The default content type is not stored in the        Content-Type header.        """        self._default_type = ctype     def _get_params_preserve(self, failobj, header):        # Like get_params() but preserves the quoting of values.  BAW:        # should this be part of the public interface?        missing = object()        value = self.get(header, missing)        if value is missing:            return failobj        params = []        for p in _parseparam(';' + value):            try:                name, val = p.split('=', 1)                name = name.strip()                val = val.strip()            except ValueError:                # Must have been a bare attribute                name = p.strip()                val = ''            params.append((name, val))        params = utils.decode_params(params)        return params

Lines 1–500 of 786.