2006-01-17 00:49:07 -04:00
|
|
|
|
# Copyright (C) 2001-2006 Python Software Foundation
|
2004-10-03 00:16:19 -03:00
|
|
|
|
# Author: Barry Warsaw
|
|
|
|
|
# Contact: email-sig@python.org
|
2001-09-23 00:17:28 -03:00
|
|
|
|
|
2004-05-09 00:44:55 -03:00
|
|
|
|
"""Basic message object for the email package object model."""
|
2001-09-23 00:17:28 -03:00
|
|
|
|
|
2006-03-18 11:41:53 -04:00
|
|
|
|
__all__ = ['Message']
|
|
|
|
|
|
2001-09-23 00:17:28 -03:00
|
|
|
|
import re
|
2003-03-11 00:33:30 -04:00
|
|
|
|
import uu
|
2003-03-10 12:13:14 -04:00
|
|
|
|
import binascii
|
2002-04-10 18:01:31 -03:00
|
|
|
|
import warnings
|
2001-09-23 00:17:28 -03:00
|
|
|
|
from cStringIO import StringIO
|
|
|
|
|
|
|
|
|
|
# Intrapackage imports
|
2006-03-18 11:41:53 -04:00
|
|
|
|
import email.charset
|
|
|
|
|
from email import utils
|
|
|
|
|
from email import errors
|
2001-09-23 00:17:28 -03:00
|
|
|
|
|
2001-09-26 02:41:51 -03:00
|
|
|
|
SEMISPACE = '; '
|
2002-04-10 18:01:31 -03:00
|
|
|
|
|
|
|
|
|
# Regular expression that matches `special' characters in parameters, the
|
2009-07-02 12:37:21 -03:00
|
|
|
|
# existence of which force quoting of the parameter value.
|
2002-04-10 18:01:31 -03:00
|
|
|
|
tspecials = re.compile(r'[ \(\)<>@,;:\\"/\[\]\?=]')
|
|
|
|
|
|
|
|
|
|
|
2002-06-29 02:56:15 -03:00
|
|
|
|
# Helper functions
|
2008-08-15 18:03:21 -03:00
|
|
|
|
def _splitparam(param):
|
|
|
|
|
# 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.
|
|
|
|
|
a, sep, b = param.partition(';')
|
|
|
|
|
if not sep:
|
|
|
|
|
return a.strip(), None
|
|
|
|
|
return a.strip(), b.strip()
|
|
|
|
|
|
2002-09-28 17:40:25 -03:00
|
|
|
|
def _formatparam(param, value=None, quote=True):
|
2002-04-10 18:01:31 -03:00
|
|
|
|
"""Convenience function to format and return a key=value pair.
|
|
|
|
|
|
2002-06-29 02:56:15 -03:00
|
|
|
|
This will quote the value if needed or if quote is true.
|
2002-04-10 18:01:31 -03:00
|
|
|
|
"""
|
|
|
|
|
if value is not None and len(value) > 0:
|
2004-05-09 00:44:55 -03:00
|
|
|
|
# A tuple is used for RFC 2231 encoded parameter values where items
|
2002-06-29 02:56:15 -03:00
|
|
|
|
# are (charset, language, value). charset is a string, not a Charset
|
|
|
|
|
# instance.
|
2004-05-09 00:44:55 -03:00
|
|
|
|
if isinstance(value, tuple):
|
2002-09-06 00:55:04 -03:00
|
|
|
|
# Encode as per RFC 2231
|
|
|
|
|
param += '*'
|
2006-03-18 11:41:53 -04:00
|
|
|
|
value = utils.encode_rfc2231(value[2], value[0], value[1])
|
2002-04-10 18:01:31 -03:00
|
|
|
|
# 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):
|
2006-03-18 11:41:53 -04:00
|
|
|
|
return '%s="%s"' % (param, utils.quote(value))
|
2002-04-10 18:01:31 -03:00
|
|
|
|
else:
|
|
|
|
|
return '%s=%s' % (param, value)
|
|
|
|
|
else:
|
|
|
|
|
return param
|
2001-09-26 02:41:51 -03:00
|
|
|
|
|
2003-09-03 01:08:13 -03:00
|
|
|
|
def _parseparam(s):
|
|
|
|
|
plist = []
|
|
|
|
|
while s[:1] == ';':
|
|
|
|
|
s = s[1:]
|
|
|
|
|
end = s.find(';')
|
2010-04-13 18:05:24 -03:00
|
|
|
|
while end > 0 and (s.count('"', 0, end) - s.count('\\"', 0, end)) % 2:
|
2003-09-03 01:08:13 -03:00
|
|
|
|
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
|
|
|
|
|
|
2001-09-23 00:17:28 -03:00
|
|
|
|
|
2002-06-29 02:56:15 -03:00
|
|
|
|
def _unquotevalue(value):
|
2006-03-18 11:41:53 -04:00
|
|
|
|
# This is different than utils.collapse_rfc2231_value() because it doesn't
|
2004-10-03 00:16:19 -03:00
|
|
|
|
# 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.
|
2004-05-09 00:44:55 -03:00
|
|
|
|
if isinstance(value, tuple):
|
2006-03-18 11:41:53 -04:00
|
|
|
|
return value[0], value[1], utils.unquote(value[2])
|
2002-06-29 02:56:15 -03:00
|
|
|
|
else:
|
2006-03-18 11:41:53 -04:00
|
|
|
|
return utils.unquote(value)
|
2002-06-29 02:56:15 -03:00
|
|
|
|
|
|
|
|
|
|
2002-08-27 19:34:44 -03:00
|
|
|
|
|
2001-09-23 00:17:28 -03:00
|
|
|
|
class Message:
|
2002-09-30 15:17:35 -03:00
|
|
|
|
"""Basic message object.
|
2001-09-23 00:17:28 -03:00
|
|
|
|
|
|
|
|
|
A message object is defined as something that has a bunch of RFC 2822
|
2002-09-30 15:17:35 -03:00
|
|
|
|
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.
|
2001-09-23 00:17:28 -03:00
|
|
|
|
|
2002-09-30 15:17:35 -03:00
|
|
|
|
Message objects implement part of the `mapping' interface, which assumes
|
2001-09-23 00:17:28 -03:00
|
|
|
|
there is exactly one occurrance of the header per message. Some headers
|
2002-09-28 17:40:25 -03:00
|
|
|
|
do in fact appear multiple times (e.g. Received) and for those headers,
|
2001-09-23 00:17:28 -03:00
|
|
|
|
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
|
2002-04-10 18:01:31 -03:00
|
|
|
|
self._charset = None
|
2001-09-23 00:17:28 -03:00
|
|
|
|
# Defaults for multipart messages
|
|
|
|
|
self.preamble = self.epilogue = None
|
2004-10-03 00:16:19 -03:00
|
|
|
|
self.defects = []
|
2002-07-08 23:46:12 -03:00
|
|
|
|
# Default content type
|
|
|
|
|
self._default_type = 'text/plain'
|
2001-09-23 00:17:28 -03:00
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
"""Return the entire formatted message as a string.
|
2002-09-30 15:17:35 -03:00
|
|
|
|
This includes the headers, body, and envelope header.
|
2001-09-23 00:17:28 -03:00
|
|
|
|
"""
|
2002-09-28 17:40:25 -03:00
|
|
|
|
return self.as_string(unixfrom=True)
|
2001-09-23 00:17:28 -03:00
|
|
|
|
|
2002-09-28 17:40:25 -03:00
|
|
|
|
def as_string(self, unixfrom=False):
|
2001-09-23 00:17:28 -03:00
|
|
|
|
"""Return the entire formatted message as a string.
|
2002-09-28 17:40:25 -03:00
|
|
|
|
Optional `unixfrom' when True, means include the Unix From_ envelope
|
2001-09-23 00:17:28 -03:00
|
|
|
|
header.
|
2003-04-18 20:04:35 -03:00
|
|
|
|
|
|
|
|
|
This is a convenience method and may not generate the message exactly
|
2004-10-03 00:38:07 -03:00
|
|
|
|
as you intend because by default it mangles lines that begin with
|
|
|
|
|
"From ". For more flexibility, use the flatten() method of a
|
2003-04-18 20:04:35 -03:00
|
|
|
|
Generator instance.
|
2001-09-23 00:17:28 -03:00
|
|
|
|
"""
|
2009-07-11 11:37:20 -03:00
|
|
|
|
from email.generator import Generator
|
2001-09-23 00:17:28 -03:00
|
|
|
|
fp = StringIO()
|
|
|
|
|
g = Generator(fp)
|
2002-06-02 16:05:51 -03:00
|
|
|
|
g.flatten(self, unixfrom=unixfrom)
|
2001-09-23 00:17:28 -03:00
|
|
|
|
return fp.getvalue()
|
|
|
|
|
|
|
|
|
|
def is_multipart(self):
|
2002-09-28 17:40:25 -03:00
|
|
|
|
"""Return True if the message consists of multiple parts."""
|
2004-10-03 00:16:19 -03:00
|
|
|
|
return isinstance(self._payload, list)
|
2001-09-23 00:17:28 -03:00
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Unix From_ line
|
|
|
|
|
#
|
|
|
|
|
def set_unixfrom(self, unixfrom):
|
|
|
|
|
self._unixfrom = unixfrom
|
|
|
|
|
|
|
|
|
|
def get_unixfrom(self):
|
|
|
|
|
return self._unixfrom
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Payload manipulation.
|
|
|
|
|
#
|
2002-04-10 18:01:31 -03:00
|
|
|
|
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
|
2002-09-30 15:17:35 -03:00
|
|
|
|
is called. If you want to set the payload to a scalar object, use
|
2002-04-10 18:01:31 -03:00
|
|
|
|
set_payload() instead.
|
|
|
|
|
"""
|
|
|
|
|
if self._payload is None:
|
|
|
|
|
self._payload = [payload]
|
|
|
|
|
else:
|
|
|
|
|
self._payload.append(payload)
|
2001-09-23 00:17:28 -03:00
|
|
|
|
|
2002-09-28 17:40:25 -03:00
|
|
|
|
def get_payload(self, i=None, decode=False):
|
2002-09-11 11:11:35 -03:00
|
|
|
|
"""Return a reference to the payload.
|
2001-09-23 00:17:28 -03:00
|
|
|
|
|
2002-09-30 15:17:35 -03:00
|
|
|
|
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.
|
|
|
|
|
|
2003-03-11 00:33:30 -04:00
|
|
|
|
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.
|
2003-03-10 12:13:14 -04:00
|
|
|
|
|
|
|
|
|
If the message is a multipart and the decode flag is True, then None
|
|
|
|
|
is returned.
|
2001-09-23 00:17:28 -03:00
|
|
|
|
"""
|
|
|
|
|
if i is None:
|
|
|
|
|
payload = self._payload
|
2004-05-09 00:44:55 -03:00
|
|
|
|
elif not isinstance(self._payload, list):
|
2004-10-03 00:16:19 -03:00
|
|
|
|
raise TypeError('Expected list, got %s' % type(self._payload))
|
2001-09-23 00:17:28 -03:00
|
|
|
|
else:
|
|
|
|
|
payload = self._payload[i]
|
|
|
|
|
if decode:
|
|
|
|
|
if self.is_multipart():
|
|
|
|
|
return None
|
2003-03-11 00:33:30 -04:00
|
|
|
|
cte = self.get('content-transfer-encoding', '').lower()
|
|
|
|
|
if cte == 'quoted-printable':
|
2006-03-18 11:41:53 -04:00
|
|
|
|
return utils._qdecode(payload)
|
2003-03-11 00:33:30 -04:00
|
|
|
|
elif cte == 'base64':
|
2003-03-10 12:13:14 -04:00
|
|
|
|
try:
|
2006-03-18 11:41:53 -04:00
|
|
|
|
return utils._bdecode(payload)
|
2003-03-10 12:13:14 -04:00
|
|
|
|
except binascii.Error:
|
|
|
|
|
# Incorrect padding
|
|
|
|
|
return payload
|
2003-03-11 00:33:30 -04:00
|
|
|
|
elif cte in ('x-uuencode', 'uuencode', 'uue', 'x-uue'):
|
|
|
|
|
sfp = StringIO()
|
|
|
|
|
try:
|
2006-03-18 11:41:53 -04:00
|
|
|
|
uu.decode(StringIO(payload+'\n'), sfp, quiet=True)
|
2003-03-11 00:33:30 -04:00
|
|
|
|
payload = sfp.getvalue()
|
|
|
|
|
except uu.Error:
|
|
|
|
|
# Some decoding problem
|
|
|
|
|
return payload
|
2001-09-23 00:17:28 -03:00
|
|
|
|
# Everything else, including encodings with 8bit or 7bit are returned
|
|
|
|
|
# unchanged.
|
|
|
|
|
return payload
|
|
|
|
|
|
2002-04-10 18:01:31 -03:00
|
|
|
|
def set_payload(self, payload, charset=None):
|
|
|
|
|
"""Set the payload to the given value.
|
|
|
|
|
|
2002-09-30 15:17:35 -03:00
|
|
|
|
Optional charset sets the message's default character set. See
|
|
|
|
|
set_charset() for details.
|
|
|
|
|
"""
|
2001-09-23 00:17:28 -03:00
|
|
|
|
self._payload = payload
|
2002-04-10 18:01:31 -03:00
|
|
|
|
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.
|
|
|
|
|
|
2002-09-30 15:17:35 -03:00
|
|
|
|
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.
|
2002-04-10 18:01:31 -03:00
|
|
|
|
|
2002-09-30 15:17:35 -03:00
|
|
|
|
The message will be assumed to be of type text/* encoded with
|
2002-04-10 18:01:31 -03:00
|
|
|
|
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.
|
2002-09-30 15:17:35 -03:00
|
|
|
|
|
2002-04-10 18:01:31 -03:00
|
|
|
|
"""
|
|
|
|
|
if charset is None:
|
|
|
|
|
self.del_param('charset')
|
|
|
|
|
self._charset = None
|
|
|
|
|
return
|
2007-03-13 07:24:00 -03:00
|
|
|
|
if isinstance(charset, basestring):
|
2006-03-18 11:41:53 -04:00
|
|
|
|
charset = email.charset.Charset(charset)
|
|
|
|
|
if not isinstance(charset, email.charset.Charset):
|
2004-10-03 00:16:19 -03:00
|
|
|
|
raise TypeError(charset)
|
2002-04-10 18:01:31 -03:00
|
|
|
|
# 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())
|
2010-06-02 19:11:01 -03:00
|
|
|
|
if isinstance(self._payload, unicode):
|
|
|
|
|
self._payload = self._payload.encode(charset.output_charset)
|
2008-08-03 20:27:32 -03:00
|
|
|
|
if str(charset) != charset.get_output_charset():
|
2006-02-08 10:34:21 -04:00
|
|
|
|
self._payload = charset.body_encode(self._payload)
|
2002-04-10 18:01:31 -03:00
|
|
|
|
if not self.has_key('Content-Transfer-Encoding'):
|
|
|
|
|
cte = charset.get_body_encoding()
|
2004-10-03 00:16:19 -03:00
|
|
|
|
try:
|
2002-04-10 18:01:31 -03:00
|
|
|
|
cte(self)
|
2004-10-03 00:16:19 -03:00
|
|
|
|
except TypeError:
|
2006-02-08 10:34:21 -04:00
|
|
|
|
self._payload = charset.body_encode(self._payload)
|
2002-04-10 18:01:31 -03:00
|
|
|
|
self.add_header('Content-Transfer-Encoding', cte)
|
2001-09-23 00:17:28 -03:00
|
|
|
|
|
2002-04-10 18:01:31 -03:00
|
|
|
|
def get_charset(self):
|
2002-09-30 15:17:35 -03:00
|
|
|
|
"""Return the Charset instance associated with the message's payload.
|
|
|
|
|
"""
|
2002-04-10 18:01:31 -03:00
|
|
|
|
return self._charset
|
2002-05-23 12:15:30 -03:00
|
|
|
|
|
2001-09-23 00:17:28 -03:00
|
|
|
|
#
|
|
|
|
|
# MAPPING INTERFACE (partial)
|
|
|
|
|
#
|
|
|
|
|
def __len__(self):
|
2001-09-26 02:41:51 -03:00
|
|
|
|
"""Return the total number of headers, including duplicates."""
|
2001-09-23 00:17:28 -03:00
|
|
|
|
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
|
2004-10-03 00:16:19 -03:00
|
|
|
|
occurrance gets returned is undefined. Use get_all() to get all
|
2001-09-23 00:17:28 -03:00
|
|
|
|
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:
|
2008-08-03 20:27:32 -03:00
|
|
|
|
if k.lower() != name:
|
2001-09-23 00:17:28 -03:00
|
|
|
|
newheaders.append((k, v))
|
|
|
|
|
self._headers = newheaders
|
|
|
|
|
|
2002-09-30 15:17:35 -03:00
|
|
|
|
def __contains__(self, name):
|
|
|
|
|
return name.lower() in [k.lower() for k, v in self._headers]
|
2001-09-23 00:17:28 -03:00
|
|
|
|
|
|
|
|
|
def has_key(self, name):
|
|
|
|
|
"""Return true if the message contains the header."""
|
2004-10-03 00:16:19 -03:00
|
|
|
|
missing = object()
|
2001-09-26 02:41:51 -03:00
|
|
|
|
return self.get(name, missing) is not missing
|
2001-09-23 00:17:28 -03:00
|
|
|
|
|
|
|
|
|
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
|
2002-09-30 15:17:35 -03:00
|
|
|
|
message, or were added to the message, and may contain duplicates.
|
|
|
|
|
Any fields deleted and re-inserted are always appended to the header
|
|
|
|
|
list.
|
2001-09-23 00:17:28 -03:00
|
|
|
|
"""
|
|
|
|
|
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
|
2002-09-30 15:17:35 -03:00
|
|
|
|
message, or were added to the message, and may contain duplicates.
|
|
|
|
|
Any fields deleted and re-inserted are always appended to the header
|
|
|
|
|
list.
|
2001-09-23 00:17:28 -03:00
|
|
|
|
"""
|
|
|
|
|
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
|
2002-09-30 15:17:35 -03:00
|
|
|
|
message, or were added to the message, and may contain duplicates.
|
|
|
|
|
Any fields deleted and re-inserted are always appended to the header
|
|
|
|
|
list.
|
2001-09-23 00:17:28 -03:00
|
|
|
|
"""
|
|
|
|
|
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
|
2001-11-24 11:49:53 -04:00
|
|
|
|
re-inserted are always appended to the header list.
|
2001-10-09 12:48:29 -03:00
|
|
|
|
|
|
|
|
|
If no such fields exist, failobj is returned (defaults to None).
|
2001-09-23 00:17:28 -03:00
|
|
|
|
"""
|
|
|
|
|
values = []
|
|
|
|
|
name = name.lower()
|
|
|
|
|
for k, v in self._headers:
|
|
|
|
|
if k.lower() == name:
|
|
|
|
|
values.append(v)
|
2001-10-09 12:48:29 -03:00
|
|
|
|
if not values:
|
|
|
|
|
return failobj
|
2001-09-23 00:17:28 -03:00
|
|
|
|
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:
|
2002-04-10 18:01:31 -03:00
|
|
|
|
parts.append(_formatparam(k.replace('_', '-'), v))
|
2001-09-23 00:17:28 -03:00
|
|
|
|
if _value is not None:
|
|
|
|
|
parts.insert(0, _value)
|
|
|
|
|
self._headers.append((_name, SEMISPACE.join(parts)))
|
|
|
|
|
|
2002-09-06 00:38:12 -03:00
|
|
|
|
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:
|
2004-10-03 00:16:19 -03:00
|
|
|
|
raise KeyError(_name)
|
2002-09-06 00:38:12 -03:00
|
|
|
|
|
2002-07-19 19:24:55 -03:00
|
|
|
|
#
|
|
|
|
|
# Use these three methods instead of the three above.
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
def get_content_type(self):
|
2002-09-30 15:17:35 -03:00
|
|
|
|
"""Return the message's content type.
|
2002-07-19 19:24:55 -03:00
|
|
|
|
|
2002-09-30 15:17:35 -03:00
|
|
|
|
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.
|
2002-07-19 19:24:55 -03:00
|
|
|
|
|
2002-09-30 15:17:35 -03:00
|
|
|
|
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.
|
2002-07-19 19:24:55 -03:00
|
|
|
|
"""
|
2004-10-03 00:16:19 -03:00
|
|
|
|
missing = object()
|
2002-07-19 19:24:55 -03:00
|
|
|
|
value = self.get('content-type', missing)
|
|
|
|
|
if value is missing:
|
|
|
|
|
# This should have no parameters
|
|
|
|
|
return self.get_default_type()
|
2008-08-15 18:03:21 -03:00
|
|
|
|
ctype = _splitparam(value)[0].lower()
|
2002-08-20 11:50:09 -03:00
|
|
|
|
# RFC 2045, section 5.2 says if its invalid, use text/plain
|
2008-08-03 20:27:32 -03:00
|
|
|
|
if ctype.count('/') != 1:
|
2002-08-20 11:50:09 -03:00
|
|
|
|
return 'text/plain'
|
|
|
|
|
return ctype
|
2002-07-19 19:24:55 -03:00
|
|
|
|
|
|
|
|
|
def get_content_maintype(self):
|
2002-09-30 15:17:35 -03:00
|
|
|
|
"""Return the message's main content type.
|
2002-07-19 19:24:55 -03:00
|
|
|
|
|
|
|
|
|
This is the `maintype' part of the string returned by
|
2002-09-30 15:17:35 -03:00
|
|
|
|
get_content_type().
|
2002-07-19 19:24:55 -03:00
|
|
|
|
"""
|
|
|
|
|
ctype = self.get_content_type()
|
|
|
|
|
return ctype.split('/')[0]
|
|
|
|
|
|
|
|
|
|
def get_content_subtype(self):
|
2002-09-30 15:17:35 -03:00
|
|
|
|
"""Returns the message's sub-content type.
|
2002-07-19 19:24:55 -03:00
|
|
|
|
|
|
|
|
|
This is the `subtype' part of the string returned by
|
2002-09-30 15:17:35 -03:00
|
|
|
|
get_content_type().
|
2002-07-19 19:24:55 -03:00
|
|
|
|
"""
|
|
|
|
|
ctype = self.get_content_type()
|
|
|
|
|
return ctype.split('/')[1]
|
2001-09-23 00:17:28 -03:00
|
|
|
|
|
2002-07-08 23:46:12 -03:00
|
|
|
|
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
|
2002-09-30 15:17:35 -03:00
|
|
|
|
subparts have a default content type of message/rfc822.
|
2002-07-08 23:46:12 -03:00
|
|
|
|
"""
|
|
|
|
|
return self._default_type
|
|
|
|
|
|
|
|
|
|
def set_default_type(self, ctype):
|
|
|
|
|
"""Set the `default' content type.
|
|
|
|
|
|
2002-07-19 19:24:55 -03:00
|
|
|
|
ctype should be either "text/plain" or "message/rfc822", although this
|
|
|
|
|
is not enforced. The default content type is not stored in the
|
2002-09-28 17:40:25 -03:00
|
|
|
|
Content-Type header.
|
2002-07-08 23:46:12 -03:00
|
|
|
|
"""
|
|
|
|
|
self._default_type = ctype
|
|
|
|
|
|
2001-09-26 02:41:51 -03:00
|
|
|
|
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?
|
2004-10-03 00:16:19 -03:00
|
|
|
|
missing = object()
|
2001-09-26 02:41:51 -03:00
|
|
|
|
value = self.get(header, missing)
|
|
|
|
|
if value is missing:
|
|
|
|
|
return failobj
|
|
|
|
|
params = []
|
2003-09-03 01:08:13 -03:00
|
|
|
|
for p in _parseparam(';' + value):
|
2001-09-26 02:41:51 -03:00
|
|
|
|
try:
|
|
|
|
|
name, val = p.split('=', 1)
|
2002-07-18 20:09:09 -03:00
|
|
|
|
name = name.strip()
|
|
|
|
|
val = val.strip()
|
2001-09-26 02:41:51 -03:00
|
|
|
|
except ValueError:
|
|
|
|
|
# Must have been a bare attribute
|
2002-07-18 20:09:09 -03:00
|
|
|
|
name = p.strip()
|
2001-09-26 02:41:51 -03:00
|
|
|
|
val = ''
|
|
|
|
|
params.append((name, val))
|
2006-03-18 11:41:53 -04:00
|
|
|
|
params = utils.decode_params(params)
|
2001-09-26 02:41:51 -03:00
|
|
|
|
return params
|
|
|
|
|
|
2002-09-28 17:40:25 -03:00
|
|
|
|
def get_params(self, failobj=None, header='content-type', unquote=True):
|
|
|
|
|
"""Return the message's Content-Type parameters, as a list.
|
2001-09-23 00:17:28 -03:00
|
|
|
|
|
2001-09-26 02:41:51 -03:00
|
|
|
|
The elements of the returned list are 2-tuples of key/value pairs, as
|
|
|
|
|
split on the `=' sign. The left hand side of the `=' is the key,
|
|
|
|
|
while the right hand side is the value. If there is no `=' sign in
|
2002-09-26 14:19:34 -03:00
|
|
|
|
the parameter the value is the empty string. The value is as
|
|
|
|
|
described in the get_param() method.
|
2001-09-26 02:41:51 -03:00
|
|
|
|
|
2002-09-28 17:40:25 -03:00
|
|
|
|
Optional failobj is the object to return if there is no Content-Type
|
2001-09-23 00:17:28 -03:00
|
|
|
|
header. Optional header is the header to search instead of
|
2002-09-28 17:40:25 -03:00
|
|
|
|
Content-Type. If unquote is True, the value is unquoted.
|
2001-09-23 00:17:28 -03:00
|
|
|
|
"""
|
2004-10-03 00:16:19 -03:00
|
|
|
|
missing = object()
|
2001-09-26 02:41:51 -03:00
|
|
|
|
params = self._get_params_preserve(missing, header)
|
|
|
|
|
if params is missing:
|
2001-09-23 00:17:28 -03:00
|
|
|
|
return failobj
|
2002-04-10 18:01:31 -03:00
|
|
|
|
if unquote:
|
2002-06-29 02:56:15 -03:00
|
|
|
|
return [(k, _unquotevalue(v)) for k, v in params]
|
2002-04-10 18:01:31 -03:00
|
|
|
|
else:
|
|
|
|
|
return params
|
2001-09-23 00:17:28 -03:00
|
|
|
|
|
2002-09-28 17:40:25 -03:00
|
|
|
|
def get_param(self, param, failobj=None, header='content-type',
|
|
|
|
|
unquote=True):
|
|
|
|
|
"""Return the parameter value if found in the Content-Type header.
|
2001-09-23 00:17:28 -03:00
|
|
|
|
|
2002-09-28 17:40:25 -03:00
|
|
|
|
Optional failobj is the object to return if there is no Content-Type
|
2002-09-26 14:19:34 -03:00
|
|
|
|
header, or the Content-Type header has no such parameter. Optional
|
2002-09-28 17:40:25 -03:00
|
|
|
|
header is the header to search instead of Content-Type.
|
2002-09-26 14:19:34 -03:00
|
|
|
|
|
|
|
|
|
Parameter keys are always compared case insensitively. The return
|
|
|
|
|
value can either be a string, or a 3-tuple if the parameter was RFC
|
|
|
|
|
2231 encoded. When it's a 3-tuple, the elements of the value are of
|
2003-08-19 00:53:02 -03:00
|
|
|
|
the form (CHARSET, LANGUAGE, VALUE). Note that both CHARSET and
|
|
|
|
|
LANGUAGE can be None, in which case you should consider VALUE to be
|
|
|
|
|
encoded in the us-ascii charset. You can usually ignore LANGUAGE.
|
|
|
|
|
|
|
|
|
|
Your application should be prepared to deal with 3-tuple return
|
|
|
|
|
values, and can convert the parameter to a Unicode string like so:
|
2002-09-26 14:19:34 -03:00
|
|
|
|
|
|
|
|
|
param = msg.get_param('foo')
|
|
|
|
|
if isinstance(param, tuple):
|
2003-08-19 00:53:02 -03:00
|
|
|
|
param = unicode(param[2], param[0] or 'us-ascii')
|
2002-09-26 14:19:34 -03:00
|
|
|
|
|
|
|
|
|
In any case, the parameter value (either the returned string, or the
|
|
|
|
|
VALUE item in the 3-tuple) is always unquoted, unless unquote is set
|
2002-09-28 17:40:25 -03:00
|
|
|
|
to False.
|
2001-09-23 00:17:28 -03:00
|
|
|
|
"""
|
2001-09-26 02:41:51 -03:00
|
|
|
|
if not self.has_key(header):
|
2001-09-23 00:17:28 -03:00
|
|
|
|
return failobj
|
2001-09-26 02:41:51 -03:00
|
|
|
|
for k, v in self._get_params_preserve(failobj, header):
|
|
|
|
|
if k.lower() == param.lower():
|
2002-04-10 18:01:31 -03:00
|
|
|
|
if unquote:
|
2002-06-29 02:56:15 -03:00
|
|
|
|
return _unquotevalue(v)
|
2002-04-10 18:01:31 -03:00
|
|
|
|
else:
|
|
|
|
|
return v
|
2001-09-23 00:17:28 -03:00
|
|
|
|
return failobj
|
|
|
|
|
|
2002-09-28 17:40:25 -03:00
|
|
|
|
def set_param(self, param, value, header='Content-Type', requote=True,
|
2002-09-06 00:55:04 -03:00
|
|
|
|
charset=None, language=''):
|
2002-09-28 17:40:25 -03:00
|
|
|
|
"""Set a parameter in the Content-Type header.
|
2002-04-10 18:01:31 -03:00
|
|
|
|
|
|
|
|
|
If the parameter already exists in the header, its value will be
|
|
|
|
|
replaced with the new value.
|
|
|
|
|
|
2002-09-30 15:17:35 -03:00
|
|
|
|
If header is Content-Type and has not yet been defined for this
|
2002-04-10 18:01:31 -03:00
|
|
|
|
message, it will be set to "text/plain" and the new parameter and
|
2002-09-30 15:17:35 -03:00
|
|
|
|
value will be appended as per RFC 2045.
|
2002-04-10 18:01:31 -03:00
|
|
|
|
|
2002-09-28 17:40:25 -03:00
|
|
|
|
An alternate header can specified in the header argument, and all
|
2002-09-30 15:17:35 -03:00
|
|
|
|
parameters will be quoted as necessary unless requote is False.
|
2002-09-06 00:55:04 -03:00
|
|
|
|
|
2002-09-30 15:17:35 -03:00
|
|
|
|
If charset is specified, the parameter will be encoded according to RFC
|
|
|
|
|
2231. Optional language specifies the RFC 2231 language, defaulting
|
|
|
|
|
to the empty string. Both charset and language should be strings.
|
2002-04-10 18:01:31 -03:00
|
|
|
|
"""
|
2004-05-09 00:44:55 -03:00
|
|
|
|
if not isinstance(value, tuple) and charset:
|
2002-09-06 00:55:04 -03:00
|
|
|
|
value = (charset, language, value)
|
|
|
|
|
|
2002-04-10 18:01:31 -03:00
|
|
|
|
if not self.has_key(header) and header.lower() == 'content-type':
|
|
|
|
|
ctype = 'text/plain'
|
|
|
|
|
else:
|
|
|
|
|
ctype = self.get(header)
|
|
|
|
|
if not self.get_param(param, header=header):
|
|
|
|
|
if not ctype:
|
|
|
|
|
ctype = _formatparam(param, value, requote)
|
|
|
|
|
else:
|
|
|
|
|
ctype = SEMISPACE.join(
|
|
|
|
|
[ctype, _formatparam(param, value, requote)])
|
|
|
|
|
else:
|
|
|
|
|
ctype = ''
|
|
|
|
|
for old_param, old_value in self.get_params(header=header,
|
|
|
|
|
unquote=requote):
|
|
|
|
|
append_param = ''
|
|
|
|
|
if old_param.lower() == param.lower():
|
|
|
|
|
append_param = _formatparam(param, value, requote)
|
|
|
|
|
else:
|
|
|
|
|
append_param = _formatparam(old_param, old_value, requote)
|
|
|
|
|
if not ctype:
|
|
|
|
|
ctype = append_param
|
|
|
|
|
else:
|
|
|
|
|
ctype = SEMISPACE.join([ctype, append_param])
|
2008-08-03 20:27:32 -03:00
|
|
|
|
if ctype != self.get(header):
|
2002-04-10 18:01:31 -03:00
|
|
|
|
del self[header]
|
|
|
|
|
self[header] = ctype
|
|
|
|
|
|
2002-09-28 17:40:25 -03:00
|
|
|
|
def del_param(self, param, header='content-type', requote=True):
|
2002-04-10 18:01:31 -03:00
|
|
|
|
"""Remove the given parameter completely from the Content-Type header.
|
|
|
|
|
|
2002-09-30 15:17:35 -03:00
|
|
|
|
The header will be re-written in place without the parameter or its
|
|
|
|
|
value. All values will be quoted as necessary unless requote is
|
|
|
|
|
False. Optional header specifies an alternative to the Content-Type
|
|
|
|
|
header.
|
2002-04-10 18:01:31 -03:00
|
|
|
|
"""
|
|
|
|
|
if not self.has_key(header):
|
|
|
|
|
return
|
|
|
|
|
new_ctype = ''
|
2004-08-16 12:47:34 -03:00
|
|
|
|
for p, v in self.get_params(header=header, unquote=requote):
|
2008-08-03 20:27:32 -03:00
|
|
|
|
if p.lower() != param.lower():
|
2002-04-10 18:01:31 -03:00
|
|
|
|
if not new_ctype:
|
|
|
|
|
new_ctype = _formatparam(p, v, requote)
|
|
|
|
|
else:
|
|
|
|
|
new_ctype = SEMISPACE.join([new_ctype,
|
|
|
|
|
_formatparam(p, v, requote)])
|
2008-08-03 20:27:32 -03:00
|
|
|
|
if new_ctype != self.get(header):
|
2002-04-10 18:01:31 -03:00
|
|
|
|
del self[header]
|
|
|
|
|
self[header] = new_ctype
|
|
|
|
|
|
2002-09-28 17:40:25 -03:00
|
|
|
|
def set_type(self, type, header='Content-Type', requote=True):
|
|
|
|
|
"""Set the main type and subtype for the Content-Type header.
|
2002-04-10 18:01:31 -03:00
|
|
|
|
|
|
|
|
|
type must be a string in the form "maintype/subtype", otherwise a
|
|
|
|
|
ValueError is raised.
|
|
|
|
|
|
2002-09-28 17:40:25 -03:00
|
|
|
|
This method replaces the Content-Type header, keeping all the
|
|
|
|
|
parameters in place. If requote is False, this leaves the existing
|
2002-04-10 18:01:31 -03:00
|
|
|
|
header's quoting as is. Otherwise, the parameters will be quoted (the
|
|
|
|
|
default).
|
|
|
|
|
|
2002-09-30 15:17:35 -03:00
|
|
|
|
An alternative header can be specified in the header argument. When
|
|
|
|
|
the Content-Type header is set, we'll always also add a MIME-Version
|
2002-04-10 18:01:31 -03:00
|
|
|
|
header.
|
|
|
|
|
"""
|
|
|
|
|
# BAW: should we be strict?
|
|
|
|
|
if not type.count('/') == 1:
|
|
|
|
|
raise ValueError
|
2002-09-28 17:40:25 -03:00
|
|
|
|
# Set the Content-Type, you get a MIME-Version
|
2002-04-10 18:01:31 -03:00
|
|
|
|
if header.lower() == 'content-type':
|
|
|
|
|
del self['mime-version']
|
|
|
|
|
self['MIME-Version'] = '1.0'
|
|
|
|
|
if not self.has_key(header):
|
|
|
|
|
self[header] = type
|
|
|
|
|
return
|
2004-08-16 12:47:34 -03:00
|
|
|
|
params = self.get_params(header=header, unquote=requote)
|
2002-04-10 18:01:31 -03:00
|
|
|
|
del self[header]
|
|
|
|
|
self[header] = type
|
|
|
|
|
# Skip the first param; it's the old type.
|
|
|
|
|
for p, v in params[1:]:
|
|
|
|
|
self.set_param(p, v, header, requote)
|
|
|
|
|
|
2001-09-23 00:17:28 -03:00
|
|
|
|
def get_filename(self, failobj=None):
|
|
|
|
|
"""Return the filename associated with the payload if present.
|
|
|
|
|
|
2002-09-28 17:40:25 -03:00
|
|
|
|
The filename is extracted from the Content-Disposition header's
|
2006-01-17 00:49:07 -04:00
|
|
|
|
`filename' parameter, and it is unquoted. If that header is missing
|
|
|
|
|
the `filename' parameter, this method falls back to looking for the
|
|
|
|
|
`name' parameter.
|
2001-09-23 00:17:28 -03:00
|
|
|
|
"""
|
2004-10-03 00:16:19 -03:00
|
|
|
|
missing = object()
|
2001-09-23 00:17:28 -03:00
|
|
|
|
filename = self.get_param('filename', missing, 'content-disposition')
|
2006-01-17 00:49:07 -04:00
|
|
|
|
if filename is missing:
|
2009-10-27 20:39:06 -03:00
|
|
|
|
filename = self.get_param('name', missing, 'content-type')
|
2001-09-23 00:17:28 -03:00
|
|
|
|
if filename is missing:
|
|
|
|
|
return failobj
|
2006-03-18 11:41:53 -04:00
|
|
|
|
return utils.collapse_rfc2231_value(filename).strip()
|
2001-09-23 00:17:28 -03:00
|
|
|
|
|
|
|
|
|
def get_boundary(self, failobj=None):
|
|
|
|
|
"""Return the boundary associated with the payload if present.
|
|
|
|
|
|
2002-09-28 17:40:25 -03:00
|
|
|
|
The boundary is extracted from the Content-Type header's `boundary'
|
2001-09-23 00:17:28 -03:00
|
|
|
|
parameter, and it is unquoted.
|
|
|
|
|
"""
|
2004-10-03 00:16:19 -03:00
|
|
|
|
missing = object()
|
2001-09-23 00:17:28 -03:00
|
|
|
|
boundary = self.get_param('boundary', missing)
|
|
|
|
|
if boundary is missing:
|
|
|
|
|
return failobj
|
2004-11-05 20:04:52 -04:00
|
|
|
|
# RFC 2046 says that boundaries may begin but not end in w/s
|
2006-03-18 11:41:53 -04:00
|
|
|
|
return utils.collapse_rfc2231_value(boundary).rstrip()
|
2001-09-23 00:17:28 -03:00
|
|
|
|
|
|
|
|
|
def set_boundary(self, boundary):
|
2002-09-28 17:40:25 -03:00
|
|
|
|
"""Set the boundary parameter in Content-Type to 'boundary'.
|
2001-09-23 00:17:28 -03:00
|
|
|
|
|
2002-09-28 17:40:25 -03:00
|
|
|
|
This is subtly different than deleting the Content-Type header and
|
2001-09-23 00:17:28 -03:00
|
|
|
|
adding a new one with a new boundary parameter via add_header(). The
|
|
|
|
|
main difference is that using the set_boundary() method preserves the
|
2002-09-28 17:40:25 -03:00
|
|
|
|
order of the Content-Type header in the original message.
|
2001-09-23 00:17:28 -03:00
|
|
|
|
|
2002-09-28 17:40:25 -03:00
|
|
|
|
HeaderParseError is raised if the message has no Content-Type header.
|
2001-09-23 00:17:28 -03:00
|
|
|
|
"""
|
2004-10-03 00:16:19 -03:00
|
|
|
|
missing = object()
|
2001-09-26 02:41:51 -03:00
|
|
|
|
params = self._get_params_preserve(missing, 'content-type')
|
|
|
|
|
if params is missing:
|
2002-09-28 17:40:25 -03:00
|
|
|
|
# There was no Content-Type header, and we don't know what type
|
2001-09-23 00:17:28 -03:00
|
|
|
|
# to set it to, so raise an exception.
|
2006-03-18 11:41:53 -04:00
|
|
|
|
raise errors.HeaderParseError('No Content-Type header found')
|
2001-09-23 00:17:28 -03:00
|
|
|
|
newparams = []
|
2002-09-28 17:40:25 -03:00
|
|
|
|
foundp = False
|
2001-09-26 02:41:51 -03:00
|
|
|
|
for pk, pv in params:
|
|
|
|
|
if pk.lower() == 'boundary':
|
|
|
|
|
newparams.append(('boundary', '"%s"' % boundary))
|
2002-09-28 17:40:25 -03:00
|
|
|
|
foundp = True
|
2001-09-23 00:17:28 -03:00
|
|
|
|
else:
|
2001-09-26 02:41:51 -03:00
|
|
|
|
newparams.append((pk, pv))
|
2001-09-23 00:17:28 -03:00
|
|
|
|
if not foundp:
|
2002-09-28 17:40:25 -03:00
|
|
|
|
# The original Content-Type header had no boundary attribute.
|
2003-10-20 11:01:56 -03:00
|
|
|
|
# Tack one on the end. BAW: should we raise an exception
|
2001-09-23 00:17:28 -03:00
|
|
|
|
# instead???
|
2001-09-26 02:41:51 -03:00
|
|
|
|
newparams.append(('boundary', '"%s"' % boundary))
|
2002-09-28 17:40:25 -03:00
|
|
|
|
# Replace the existing Content-Type header with the new value
|
2001-09-23 00:17:28 -03:00
|
|
|
|
newheaders = []
|
|
|
|
|
for h, v in self._headers:
|
|
|
|
|
if h.lower() == 'content-type':
|
2001-09-26 02:41:51 -03:00
|
|
|
|
parts = []
|
|
|
|
|
for k, v in newparams:
|
|
|
|
|
if v == '':
|
|
|
|
|
parts.append(k)
|
|
|
|
|
else:
|
|
|
|
|
parts.append('%s=%s' % (k, v))
|
|
|
|
|
newheaders.append((h, SEMISPACE.join(parts)))
|
|
|
|
|
|
2001-09-23 00:17:28 -03:00
|
|
|
|
else:
|
|
|
|
|
newheaders.append((h, v))
|
|
|
|
|
self._headers = newheaders
|
|
|
|
|
|
2002-09-26 14:19:34 -03:00
|
|
|
|
def get_content_charset(self, failobj=None):
|
|
|
|
|
"""Return the charset parameter of the Content-Type header.
|
|
|
|
|
|
2002-10-10 12:13:26 -03:00
|
|
|
|
The returned string is always coerced to lower case. If there is no
|
|
|
|
|
Content-Type header, or if that header has no charset parameter,
|
|
|
|
|
failobj is returned.
|
2002-09-26 14:19:34 -03:00
|
|
|
|
"""
|
2004-10-03 00:16:19 -03:00
|
|
|
|
missing = object()
|
2002-09-26 14:19:34 -03:00
|
|
|
|
charset = self.get_param('charset', missing)
|
|
|
|
|
if charset is missing:
|
|
|
|
|
return failobj
|
2004-05-09 00:44:55 -03:00
|
|
|
|
if isinstance(charset, tuple):
|
2002-09-26 14:19:34 -03:00
|
|
|
|
# RFC 2231 encoded, so decode it, and it better end up as ascii.
|
2003-08-19 00:53:02 -03:00
|
|
|
|
pcharset = charset[0] or 'us-ascii'
|
2006-07-26 02:54:46 -03:00
|
|
|
|
try:
|
|
|
|
|
# LookupError will be raised if the charset isn't known to
|
|
|
|
|
# Python. UnicodeError will be raised if the encoded text
|
|
|
|
|
# contains a character not in the charset.
|
|
|
|
|
charset = unicode(charset[2], pcharset).encode('us-ascii')
|
|
|
|
|
except (LookupError, UnicodeError):
|
|
|
|
|
charset = charset[2]
|
|
|
|
|
# charset character must be in us-ascii range
|
|
|
|
|
try:
|
2007-03-13 07:24:00 -03:00
|
|
|
|
if isinstance(charset, str):
|
|
|
|
|
charset = unicode(charset, 'us-ascii')
|
|
|
|
|
charset = charset.encode('us-ascii')
|
2006-07-26 02:54:46 -03:00
|
|
|
|
except UnicodeError:
|
|
|
|
|
return failobj
|
2002-10-10 12:13:26 -03:00
|
|
|
|
# RFC 2046, $4.1.2 says charsets are not case sensitive
|
|
|
|
|
return charset.lower()
|
2002-09-26 14:19:34 -03:00
|
|
|
|
|
2001-09-23 00:17:28 -03:00
|
|
|
|
def get_charsets(self, failobj=None):
|
|
|
|
|
"""Return a list containing the charset(s) used in this message.
|
2001-10-04 02:36:56 -03:00
|
|
|
|
|
2002-09-28 17:40:25 -03:00
|
|
|
|
The returned list of items describes the Content-Type headers'
|
2001-09-23 00:17:28 -03:00
|
|
|
|
charset parameter for this message and all the subparts in its
|
|
|
|
|
payload.
|
|
|
|
|
|
|
|
|
|
Each item will either be a string (the value of the charset parameter
|
2002-09-28 17:40:25 -03:00
|
|
|
|
in the Content-Type header of that part) or the value of the
|
2001-09-23 00:17:28 -03:00
|
|
|
|
'failobj' parameter (defaults to None), if the part does not have a
|
|
|
|
|
main MIME type of "text", or the charset is not defined.
|
|
|
|
|
|
|
|
|
|
The list will contain one string for each part of the message, plus
|
|
|
|
|
one for the container message (i.e. self), so that a non-multipart
|
|
|
|
|
message will still return a list of length 1.
|
|
|
|
|
"""
|
2002-09-26 14:19:34 -03:00
|
|
|
|
return [part.get_content_charset(failobj) for part in self.walk()]
|
2004-05-09 00:44:55 -03:00
|
|
|
|
|
|
|
|
|
# I.e. def walk(self): ...
|
2009-07-11 11:37:20 -03:00
|
|
|
|
from email.iterators import walk
|