Barry Warsaw | a0f28ef | 2006-01-17 04:49:07 +0000 | [diff] [blame] | 1 | # Copyright (C) 2001-2006 Python Software Foundation |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 2 | # Author: Barry Warsaw |
| 3 | # Contact: email-sig@python.org |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 4 | |
Barry Warsaw | 5d84053 | 2004-05-09 03:44:55 +0000 | [diff] [blame] | 5 | """Basic message object for the email package object model.""" |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 6 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 7 | __all__ = ['Message'] |
| 8 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 9 | import re |
Barry Warsaw | 0889849 | 2003-03-11 04:33:30 +0000 | [diff] [blame] | 10 | import uu |
Barry Warsaw | 21191d3 | 2003-03-10 16:13:14 +0000 | [diff] [blame] | 11 | import binascii |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 12 | import warnings |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 13 | from cStringIO import StringIO |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 14 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 15 | # Intrapackage imports |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 16 | import email.charset |
| 17 | from email import utils |
| 18 | from email import errors |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 19 | |
Barry Warsaw | beb5945 | 2001-09-26 05:41:51 +0000 | [diff] [blame] | 20 | SEMISPACE = '; ' |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 21 | |
| 22 | # Regular expression used to split header parameters. BAW: this may be too |
| 23 | # simple. It isn't strictly RFC 2045 (section 5.1) compliant, but it catches |
| 24 | # most headers found in the wild. We may eventually need a full fledged |
| 25 | # parser eventually. |
Barry Warsaw | 2539cf5 | 2001-10-25 22:43:46 +0000 | [diff] [blame] | 26 | paramre = re.compile(r'\s*;\s*') |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 27 | # Regular expression that matches `special' characters in parameters, the |
| 28 | # existance of which force quoting of the parameter value. |
| 29 | tspecials = re.compile(r'[ \(\)<>@,;:\\"/\[\]\?=]') |
| 30 | |
| 31 | |
| 32 | |
Barry Warsaw | 908dc4b | 2002-06-29 05:56:15 +0000 | [diff] [blame] | 33 | # Helper functions |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 34 | def _formatparam(param, value=None, quote=True): |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 35 | """Convenience function to format and return a key=value pair. |
| 36 | |
Barry Warsaw | 908dc4b | 2002-06-29 05:56:15 +0000 | [diff] [blame] | 37 | This will quote the value if needed or if quote is true. |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 38 | """ |
| 39 | if value is not None and len(value) > 0: |
Barry Warsaw | 5d84053 | 2004-05-09 03:44:55 +0000 | [diff] [blame] | 40 | # A tuple is used for RFC 2231 encoded parameter values where items |
Barry Warsaw | 908dc4b | 2002-06-29 05:56:15 +0000 | [diff] [blame] | 41 | # are (charset, language, value). charset is a string, not a Charset |
| 42 | # instance. |
Barry Warsaw | 5d84053 | 2004-05-09 03:44:55 +0000 | [diff] [blame] | 43 | if isinstance(value, tuple): |
Barry Warsaw | 3c25535 | 2002-09-06 03:55:04 +0000 | [diff] [blame] | 44 | # Encode as per RFC 2231 |
| 45 | param += '*' |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 46 | value = utils.encode_rfc2231(value[2], value[0], value[1]) |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 47 | # BAW: Please check this. I think that if quote is set it should |
| 48 | # force quoting even if not necessary. |
| 49 | if quote or tspecials.search(value): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 50 | return '%s="%s"' % (param, utils.quote(value)) |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 51 | else: |
| 52 | return '%s=%s' % (param, value) |
| 53 | else: |
| 54 | return param |
Barry Warsaw | beb5945 | 2001-09-26 05:41:51 +0000 | [diff] [blame] | 55 | |
Barry Warsaw | a74e868 | 2003-09-03 04:08:13 +0000 | [diff] [blame] | 56 | def _parseparam(s): |
| 57 | plist = [] |
| 58 | while s[:1] == ';': |
| 59 | s = s[1:] |
| 60 | end = s.find(';') |
| 61 | while end > 0 and s.count('"', 0, end) % 2: |
| 62 | end = s.find(';', end + 1) |
| 63 | if end < 0: |
| 64 | end = len(s) |
| 65 | f = s[:end] |
| 66 | if '=' in f: |
| 67 | i = f.index('=') |
| 68 | f = f[:i].strip().lower() + '=' + f[i+1:].strip() |
| 69 | plist.append(f.strip()) |
| 70 | s = s[end:] |
| 71 | return plist |
| 72 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 73 | |
Barry Warsaw | 908dc4b | 2002-06-29 05:56:15 +0000 | [diff] [blame] | 74 | def _unquotevalue(value): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 75 | # This is different than utils.collapse_rfc2231_value() because it doesn't |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 76 | # try to convert the value to a unicode. Message.get_param() and |
| 77 | # Message.get_params() are both currently defined to return the tuple in |
| 78 | # the face of RFC 2231 parameters. |
Barry Warsaw | 5d84053 | 2004-05-09 03:44:55 +0000 | [diff] [blame] | 79 | if isinstance(value, tuple): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 80 | return value[0], value[1], utils.unquote(value[2]) |
Barry Warsaw | 908dc4b | 2002-06-29 05:56:15 +0000 | [diff] [blame] | 81 | else: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 82 | return utils.unquote(value) |
Barry Warsaw | 908dc4b | 2002-06-29 05:56:15 +0000 | [diff] [blame] | 83 | |
| 84 | |
Barry Warsaw | 48b0d36 | 2002-08-27 22:34:44 +0000 | [diff] [blame] | 85 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 86 | class Message: |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 87 | """Basic message object. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 88 | |
| 89 | A message object is defined as something that has a bunch of RFC 2822 |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 90 | headers and a payload. It may optionally have an envelope header |
| 91 | (a.k.a. Unix-From or From_ header). If the message is a container (i.e. a |
| 92 | multipart or a message/rfc822), then the payload is a list of Message |
| 93 | objects, otherwise it is a string. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 94 | |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 95 | Message objects implement part of the `mapping' interface, which assumes |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 96 | there is exactly one occurrance of the header per message. Some headers |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 97 | do in fact appear multiple times (e.g. Received) and for those headers, |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 98 | you must use the explicit API to set or get all the headers. Not all of |
| 99 | the mapping methods are implemented. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 100 | """ |
| 101 | def __init__(self): |
| 102 | self._headers = [] |
| 103 | self._unixfrom = None |
| 104 | self._payload = None |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 105 | self._charset = None |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 106 | # Defaults for multipart messages |
| 107 | self.preamble = self.epilogue = None |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 108 | self.defects = [] |
Barry Warsaw | a0c8b9d | 2002-07-09 02:46:12 +0000 | [diff] [blame] | 109 | # Default content type |
| 110 | self._default_type = 'text/plain' |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 111 | |
| 112 | def __str__(self): |
| 113 | """Return the entire formatted message as a string. |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 114 | This includes the headers, body, and envelope header. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 115 | """ |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 116 | return self.as_string(unixfrom=True) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 117 | |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 118 | def as_string(self, unixfrom=False): |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 119 | """Return the entire formatted message as a string. |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 120 | Optional `unixfrom' when True, means include the Unix From_ envelope |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 121 | header. |
Barry Warsaw | 482c5f7 | 2003-04-18 23:04:35 +0000 | [diff] [blame] | 122 | |
| 123 | This is a convenience method and may not generate the message exactly |
Barry Warsaw | 05bef93 | 2004-10-03 03:38:07 +0000 | [diff] [blame] | 124 | as you intend because by default it mangles lines that begin with |
| 125 | "From ". For more flexibility, use the flatten() method of a |
Barry Warsaw | 482c5f7 | 2003-04-18 23:04:35 +0000 | [diff] [blame] | 126 | Generator instance. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 127 | """ |
Barry Warsaw | 8ba76e8 | 2002-06-02 19:05:51 +0000 | [diff] [blame] | 128 | from email.Generator import Generator |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 129 | fp = StringIO() |
| 130 | g = Generator(fp) |
Barry Warsaw | 8ba76e8 | 2002-06-02 19:05:51 +0000 | [diff] [blame] | 131 | g.flatten(self, unixfrom=unixfrom) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 132 | return fp.getvalue() |
| 133 | |
| 134 | def is_multipart(self): |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 135 | """Return True if the message consists of multiple parts.""" |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 136 | return isinstance(self._payload, list) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 137 | |
| 138 | # |
| 139 | # Unix From_ line |
| 140 | # |
| 141 | def set_unixfrom(self, unixfrom): |
| 142 | self._unixfrom = unixfrom |
| 143 | |
| 144 | def get_unixfrom(self): |
| 145 | return self._unixfrom |
| 146 | |
| 147 | # |
| 148 | # Payload manipulation. |
| 149 | # |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 150 | def attach(self, payload): |
| 151 | """Add the given payload to the current payload. |
| 152 | |
| 153 | The current payload will always be a list of objects after this method |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 154 | is called. If you want to set the payload to a scalar object, use |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 155 | set_payload() instead. |
| 156 | """ |
| 157 | if self._payload is None: |
| 158 | self._payload = [payload] |
| 159 | else: |
| 160 | self._payload.append(payload) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 161 | |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 162 | def get_payload(self, i=None, decode=False): |
Barry Warsaw | fbcde75 | 2002-09-11 14:11:35 +0000 | [diff] [blame] | 163 | """Return a reference to the payload. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 164 | |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 165 | The payload will either be a list object or a string. If you mutate |
| 166 | the list object, you modify the message's payload in place. Optional |
| 167 | i returns that index into the payload. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 168 | |
Barry Warsaw | 0889849 | 2003-03-11 04:33:30 +0000 | [diff] [blame] | 169 | Optional decode is a flag indicating whether the payload should be |
| 170 | decoded or not, according to the Content-Transfer-Encoding header |
| 171 | (default is False). |
| 172 | |
| 173 | When True and the message is not a multipart, the payload will be |
| 174 | decoded if this header's value is `quoted-printable' or `base64'. If |
| 175 | some other encoding is used, or the header is missing, or if the |
| 176 | payload has bogus data (i.e. bogus base64 or uuencoded data), the |
| 177 | payload is returned as-is. |
Barry Warsaw | 21191d3 | 2003-03-10 16:13:14 +0000 | [diff] [blame] | 178 | |
| 179 | If the message is a multipart and the decode flag is True, then None |
| 180 | is returned. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 181 | """ |
| 182 | if i is None: |
| 183 | payload = self._payload |
Barry Warsaw | 5d84053 | 2004-05-09 03:44:55 +0000 | [diff] [blame] | 184 | elif not isinstance(self._payload, list): |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 185 | raise TypeError('Expected list, got %s' % type(self._payload)) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 186 | else: |
| 187 | payload = self._payload[i] |
| 188 | if decode: |
| 189 | if self.is_multipart(): |
| 190 | return None |
Barry Warsaw | 0889849 | 2003-03-11 04:33:30 +0000 | [diff] [blame] | 191 | cte = self.get('content-transfer-encoding', '').lower() |
| 192 | if cte == 'quoted-printable': |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 193 | return utils._qdecode(payload) |
Barry Warsaw | 0889849 | 2003-03-11 04:33:30 +0000 | [diff] [blame] | 194 | elif cte == 'base64': |
Barry Warsaw | 21191d3 | 2003-03-10 16:13:14 +0000 | [diff] [blame] | 195 | try: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 196 | return utils._bdecode(payload) |
Barry Warsaw | 21191d3 | 2003-03-10 16:13:14 +0000 | [diff] [blame] | 197 | except binascii.Error: |
| 198 | # Incorrect padding |
| 199 | return payload |
Barry Warsaw | 0889849 | 2003-03-11 04:33:30 +0000 | [diff] [blame] | 200 | elif cte in ('x-uuencode', 'uuencode', 'uue', 'x-uue'): |
| 201 | sfp = StringIO() |
| 202 | try: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 203 | uu.decode(StringIO(payload+'\n'), sfp, quiet=True) |
Barry Warsaw | 0889849 | 2003-03-11 04:33:30 +0000 | [diff] [blame] | 204 | payload = sfp.getvalue() |
| 205 | except uu.Error: |
| 206 | # Some decoding problem |
| 207 | return payload |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 208 | # Everything else, including encodings with 8bit or 7bit are returned |
| 209 | # unchanged. |
| 210 | return payload |
| 211 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 212 | def set_payload(self, payload, charset=None): |
| 213 | """Set the payload to the given value. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 214 | |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 215 | Optional charset sets the message's default character set. See |
| 216 | set_charset() for details. |
| 217 | """ |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 218 | self._payload = payload |
| 219 | if charset is not None: |
| 220 | self.set_charset(charset) |
| 221 | |
| 222 | def set_charset(self, charset): |
| 223 | """Set the charset of the payload to a given character set. |
| 224 | |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 225 | charset can be a Charset instance, a string naming a character set, or |
| 226 | None. If it is a string it will be converted to a Charset instance. |
| 227 | If charset is None, the charset parameter will be removed from the |
| 228 | Content-Type field. Anything else will generate a TypeError. |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 229 | |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 230 | The message will be assumed to be of type text/* encoded with |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 231 | charset.input_charset. It will be converted to charset.output_charset |
| 232 | and encoded properly, if needed, when generating the plain text |
| 233 | representation of the message. MIME headers (MIME-Version, |
| 234 | Content-Type, Content-Transfer-Encoding) will be added as needed. |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 235 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 236 | """ |
| 237 | if charset is None: |
| 238 | self.del_param('charset') |
| 239 | self._charset = None |
| 240 | return |
Barry Warsaw | 5d84053 | 2004-05-09 03:44:55 +0000 | [diff] [blame] | 241 | if isinstance(charset, str): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 242 | charset = email.charset.Charset(charset) |
| 243 | if not isinstance(charset, email.charset.Charset): |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 244 | raise TypeError(charset) |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 245 | # BAW: should we accept strings that can serve as arguments to the |
| 246 | # Charset constructor? |
| 247 | self._charset = charset |
| 248 | if not self.has_key('MIME-Version'): |
| 249 | self.add_header('MIME-Version', '1.0') |
| 250 | if not self.has_key('Content-Type'): |
| 251 | self.add_header('Content-Type', 'text/plain', |
| 252 | charset=charset.get_output_charset()) |
| 253 | else: |
| 254 | self.set_param('charset', charset.get_output_charset()) |
Barry Warsaw | e58df82 | 2006-02-08 14:34:21 +0000 | [diff] [blame] | 255 | if str(charset) <> charset.get_output_charset(): |
| 256 | self._payload = charset.body_encode(self._payload) |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 257 | if not self.has_key('Content-Transfer-Encoding'): |
| 258 | cte = charset.get_body_encoding() |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 259 | try: |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 260 | cte(self) |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 261 | except TypeError: |
Barry Warsaw | e58df82 | 2006-02-08 14:34:21 +0000 | [diff] [blame] | 262 | self._payload = charset.body_encode(self._payload) |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 263 | self.add_header('Content-Transfer-Encoding', cte) |
| 264 | |
| 265 | def get_charset(self): |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 266 | """Return the Charset instance associated with the message's payload. |
| 267 | """ |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 268 | return self._charset |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 269 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 270 | # |
| 271 | # MAPPING INTERFACE (partial) |
| 272 | # |
| 273 | def __len__(self): |
Barry Warsaw | beb5945 | 2001-09-26 05:41:51 +0000 | [diff] [blame] | 274 | """Return the total number of headers, including duplicates.""" |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 275 | return len(self._headers) |
| 276 | |
| 277 | def __getitem__(self, name): |
| 278 | """Get a header value. |
| 279 | |
| 280 | Return None if the header is missing instead of raising an exception. |
| 281 | |
| 282 | Note that if the header appeared multiple times, exactly which |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 283 | occurrance gets returned is undefined. Use get_all() to get all |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 284 | the values matching a header field name. |
| 285 | """ |
| 286 | return self.get(name) |
| 287 | |
| 288 | def __setitem__(self, name, val): |
| 289 | """Set the value of a header. |
| 290 | |
| 291 | Note: this does not overwrite an existing header with the same field |
| 292 | name. Use __delitem__() first to delete any existing headers. |
| 293 | """ |
| 294 | self._headers.append((name, val)) |
| 295 | |
| 296 | def __delitem__(self, name): |
| 297 | """Delete all occurrences of a header, if present. |
| 298 | |
| 299 | Does not raise an exception if the header is missing. |
| 300 | """ |
| 301 | name = name.lower() |
| 302 | newheaders = [] |
| 303 | for k, v in self._headers: |
| 304 | if k.lower() <> name: |
| 305 | newheaders.append((k, v)) |
| 306 | self._headers = newheaders |
| 307 | |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 308 | def __contains__(self, name): |
| 309 | return name.lower() in [k.lower() for k, v in self._headers] |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 310 | |
| 311 | def has_key(self, name): |
| 312 | """Return true if the message contains the header.""" |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 313 | missing = object() |
Barry Warsaw | beb5945 | 2001-09-26 05:41:51 +0000 | [diff] [blame] | 314 | return self.get(name, missing) is not missing |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 315 | |
| 316 | def keys(self): |
| 317 | """Return a list of all the message's header field names. |
| 318 | |
| 319 | These will be sorted in the order they appeared in the original |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 320 | message, or were added to the message, and may contain duplicates. |
| 321 | Any fields deleted and re-inserted are always appended to the header |
| 322 | list. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 323 | """ |
| 324 | return [k for k, v in self._headers] |
| 325 | |
| 326 | def values(self): |
| 327 | """Return a list of all the message's header values. |
| 328 | |
| 329 | These will be sorted in the order they appeared in the original |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 330 | message, or were added to the message, and may contain duplicates. |
| 331 | Any fields deleted and re-inserted are always appended to the header |
| 332 | list. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 333 | """ |
| 334 | return [v for k, v in self._headers] |
| 335 | |
| 336 | def items(self): |
| 337 | """Get all the message's header fields and values. |
| 338 | |
| 339 | These will be sorted in the order they appeared in the original |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 340 | message, or were added to the message, and may contain duplicates. |
| 341 | Any fields deleted and re-inserted are always appended to the header |
| 342 | list. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 343 | """ |
| 344 | return self._headers[:] |
| 345 | |
| 346 | def get(self, name, failobj=None): |
| 347 | """Get a header value. |
| 348 | |
| 349 | Like __getitem__() but return failobj instead of None when the field |
| 350 | is missing. |
| 351 | """ |
| 352 | name = name.lower() |
| 353 | for k, v in self._headers: |
| 354 | if k.lower() == name: |
| 355 | return v |
| 356 | return failobj |
| 357 | |
| 358 | # |
| 359 | # Additional useful stuff |
| 360 | # |
| 361 | |
| 362 | def get_all(self, name, failobj=None): |
| 363 | """Return a list of all the values for the named field. |
| 364 | |
| 365 | These will be sorted in the order they appeared in the original |
| 366 | message, and may contain duplicates. Any fields deleted and |
Greg Ward | 6253c2d | 2001-11-24 15:49:53 +0000 | [diff] [blame] | 367 | re-inserted are always appended to the header list. |
Barry Warsaw | 9300a75 | 2001-10-09 15:48:29 +0000 | [diff] [blame] | 368 | |
| 369 | If no such fields exist, failobj is returned (defaults to None). |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 370 | """ |
| 371 | values = [] |
| 372 | name = name.lower() |
| 373 | for k, v in self._headers: |
| 374 | if k.lower() == name: |
| 375 | values.append(v) |
Barry Warsaw | 9300a75 | 2001-10-09 15:48:29 +0000 | [diff] [blame] | 376 | if not values: |
| 377 | return failobj |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 378 | return values |
| 379 | |
| 380 | def add_header(self, _name, _value, **_params): |
| 381 | """Extended header setting. |
| 382 | |
| 383 | name is the header field to add. keyword arguments can be used to set |
| 384 | additional parameters for the header field, with underscores converted |
| 385 | to dashes. Normally the parameter will be added as key="value" unless |
| 386 | value is None, in which case only the key will be added. |
| 387 | |
| 388 | Example: |
| 389 | |
| 390 | msg.add_header('content-disposition', 'attachment', filename='bud.gif') |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 391 | """ |
| 392 | parts = [] |
| 393 | for k, v in _params.items(): |
| 394 | if v is None: |
| 395 | parts.append(k.replace('_', '-')) |
| 396 | else: |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 397 | parts.append(_formatparam(k.replace('_', '-'), v)) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 398 | if _value is not None: |
| 399 | parts.insert(0, _value) |
| 400 | self._headers.append((_name, SEMISPACE.join(parts))) |
| 401 | |
Barry Warsaw | 229727f | 2002-09-06 03:38:12 +0000 | [diff] [blame] | 402 | def replace_header(self, _name, _value): |
| 403 | """Replace a header. |
| 404 | |
| 405 | Replace the first matching header found in the message, retaining |
| 406 | header order and case. If no matching header was found, a KeyError is |
| 407 | raised. |
| 408 | """ |
| 409 | _name = _name.lower() |
| 410 | for i, (k, v) in zip(range(len(self._headers)), self._headers): |
| 411 | if k.lower() == _name: |
| 412 | self._headers[i] = (k, _value) |
| 413 | break |
| 414 | else: |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 415 | raise KeyError(_name) |
Barry Warsaw | 229727f | 2002-09-06 03:38:12 +0000 | [diff] [blame] | 416 | |
Barry Warsaw | c106864 | 2002-07-19 22:24:55 +0000 | [diff] [blame] | 417 | # |
Barry Warsaw | c106864 | 2002-07-19 22:24:55 +0000 | [diff] [blame] | 418 | # Use these three methods instead of the three above. |
| 419 | # |
| 420 | |
| 421 | def get_content_type(self): |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 422 | """Return the message's content type. |
Barry Warsaw | c106864 | 2002-07-19 22:24:55 +0000 | [diff] [blame] | 423 | |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 424 | The returned string is coerced to lower case of the form |
| 425 | `maintype/subtype'. If there was no Content-Type header in the |
| 426 | message, the default type as given by get_default_type() will be |
| 427 | returned. Since according to RFC 2045, messages always have a default |
| 428 | type this will always return a value. |
Barry Warsaw | c106864 | 2002-07-19 22:24:55 +0000 | [diff] [blame] | 429 | |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 430 | RFC 2045 defines a message's default type to be text/plain unless it |
| 431 | appears inside a multipart/digest container, in which case it would be |
| 432 | message/rfc822. |
Barry Warsaw | c106864 | 2002-07-19 22:24:55 +0000 | [diff] [blame] | 433 | """ |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 434 | missing = object() |
Barry Warsaw | c106864 | 2002-07-19 22:24:55 +0000 | [diff] [blame] | 435 | value = self.get('content-type', missing) |
| 436 | if value is missing: |
| 437 | # This should have no parameters |
| 438 | return self.get_default_type() |
Barry Warsaw | f36d804 | 2002-08-20 14:50:09 +0000 | [diff] [blame] | 439 | ctype = paramre.split(value)[0].lower().strip() |
| 440 | # RFC 2045, section 5.2 says if its invalid, use text/plain |
| 441 | if ctype.count('/') <> 1: |
| 442 | return 'text/plain' |
| 443 | return ctype |
Barry Warsaw | c106864 | 2002-07-19 22:24:55 +0000 | [diff] [blame] | 444 | |
| 445 | def get_content_maintype(self): |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 446 | """Return the message's main content type. |
Barry Warsaw | c106864 | 2002-07-19 22:24:55 +0000 | [diff] [blame] | 447 | |
| 448 | This is the `maintype' part of the string returned by |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 449 | get_content_type(). |
Barry Warsaw | c106864 | 2002-07-19 22:24:55 +0000 | [diff] [blame] | 450 | """ |
| 451 | ctype = self.get_content_type() |
Barry Warsaw | c106864 | 2002-07-19 22:24:55 +0000 | [diff] [blame] | 452 | return ctype.split('/')[0] |
| 453 | |
| 454 | def get_content_subtype(self): |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 455 | """Returns the message's sub-content type. |
Barry Warsaw | c106864 | 2002-07-19 22:24:55 +0000 | [diff] [blame] | 456 | |
| 457 | This is the `subtype' part of the string returned by |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 458 | get_content_type(). |
Barry Warsaw | c106864 | 2002-07-19 22:24:55 +0000 | [diff] [blame] | 459 | """ |
| 460 | ctype = self.get_content_type() |
Barry Warsaw | c106864 | 2002-07-19 22:24:55 +0000 | [diff] [blame] | 461 | return ctype.split('/')[1] |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 462 | |
Barry Warsaw | a0c8b9d | 2002-07-09 02:46:12 +0000 | [diff] [blame] | 463 | def get_default_type(self): |
| 464 | """Return the `default' content type. |
| 465 | |
| 466 | Most messages have a default content type of text/plain, except for |
| 467 | messages that are subparts of multipart/digest containers. Such |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 468 | subparts have a default content type of message/rfc822. |
Barry Warsaw | a0c8b9d | 2002-07-09 02:46:12 +0000 | [diff] [blame] | 469 | """ |
| 470 | return self._default_type |
| 471 | |
| 472 | def set_default_type(self, ctype): |
| 473 | """Set the `default' content type. |
| 474 | |
Barry Warsaw | c106864 | 2002-07-19 22:24:55 +0000 | [diff] [blame] | 475 | ctype should be either "text/plain" or "message/rfc822", although this |
| 476 | is not enforced. The default content type is not stored in the |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 477 | Content-Type header. |
Barry Warsaw | a0c8b9d | 2002-07-09 02:46:12 +0000 | [diff] [blame] | 478 | """ |
Barry Warsaw | a0c8b9d | 2002-07-09 02:46:12 +0000 | [diff] [blame] | 479 | self._default_type = ctype |
| 480 | |
Barry Warsaw | beb5945 | 2001-09-26 05:41:51 +0000 | [diff] [blame] | 481 | def _get_params_preserve(self, failobj, header): |
| 482 | # Like get_params() but preserves the quoting of values. BAW: |
| 483 | # should this be part of the public interface? |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 484 | missing = object() |
Barry Warsaw | beb5945 | 2001-09-26 05:41:51 +0000 | [diff] [blame] | 485 | value = self.get(header, missing) |
| 486 | if value is missing: |
| 487 | return failobj |
| 488 | params = [] |
Barry Warsaw | a74e868 | 2003-09-03 04:08:13 +0000 | [diff] [blame] | 489 | for p in _parseparam(';' + value): |
Barry Warsaw | beb5945 | 2001-09-26 05:41:51 +0000 | [diff] [blame] | 490 | try: |
| 491 | name, val = p.split('=', 1) |
Barry Warsaw | 7aeac91 | 2002-07-18 23:09:09 +0000 | [diff] [blame] | 492 | name = name.strip() |
| 493 | val = val.strip() |
Barry Warsaw | beb5945 | 2001-09-26 05:41:51 +0000 | [diff] [blame] | 494 | except ValueError: |
| 495 | # Must have been a bare attribute |
Barry Warsaw | 7aeac91 | 2002-07-18 23:09:09 +0000 | [diff] [blame] | 496 | name = p.strip() |
Barry Warsaw | beb5945 | 2001-09-26 05:41:51 +0000 | [diff] [blame] | 497 | val = '' |
| 498 | params.append((name, val)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 499 | params = utils.decode_params(params) |
Barry Warsaw | beb5945 | 2001-09-26 05:41:51 +0000 | [diff] [blame] | 500 | return params |
| 501 | |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 502 | def get_params(self, failobj=None, header='content-type', unquote=True): |
| 503 | """Return the message's Content-Type parameters, as a list. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 504 | |
Barry Warsaw | beb5945 | 2001-09-26 05:41:51 +0000 | [diff] [blame] | 505 | The elements of the returned list are 2-tuples of key/value pairs, as |
| 506 | split on the `=' sign. The left hand side of the `=' is the key, |
| 507 | while the right hand side is the value. If there is no `=' sign in |
Barry Warsaw | 15aefa9 | 2002-09-26 17:19:34 +0000 | [diff] [blame] | 508 | the parameter the value is the empty string. The value is as |
| 509 | described in the get_param() method. |
Barry Warsaw | beb5945 | 2001-09-26 05:41:51 +0000 | [diff] [blame] | 510 | |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 511 | Optional failobj is the object to return if there is no Content-Type |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 512 | header. Optional header is the header to search instead of |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 513 | Content-Type. If unquote is True, the value is unquoted. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 514 | """ |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 515 | missing = object() |
Barry Warsaw | beb5945 | 2001-09-26 05:41:51 +0000 | [diff] [blame] | 516 | params = self._get_params_preserve(missing, header) |
| 517 | if params is missing: |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 518 | return failobj |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 519 | if unquote: |
Barry Warsaw | 908dc4b | 2002-06-29 05:56:15 +0000 | [diff] [blame] | 520 | return [(k, _unquotevalue(v)) for k, v in params] |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 521 | else: |
| 522 | return params |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 523 | |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 524 | def get_param(self, param, failobj=None, header='content-type', |
| 525 | unquote=True): |
| 526 | """Return the parameter value if found in the Content-Type header. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 527 | |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 528 | Optional failobj is the object to return if there is no Content-Type |
Barry Warsaw | 15aefa9 | 2002-09-26 17:19:34 +0000 | [diff] [blame] | 529 | header, or the Content-Type header has no such parameter. Optional |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 530 | header is the header to search instead of Content-Type. |
Barry Warsaw | beb5945 | 2001-09-26 05:41:51 +0000 | [diff] [blame] | 531 | |
Barry Warsaw | 15aefa9 | 2002-09-26 17:19:34 +0000 | [diff] [blame] | 532 | Parameter keys are always compared case insensitively. The return |
| 533 | value can either be a string, or a 3-tuple if the parameter was RFC |
| 534 | 2231 encoded. When it's a 3-tuple, the elements of the value are of |
Barry Warsaw | 6208369 | 2003-08-19 03:53:02 +0000 | [diff] [blame] | 535 | the form (CHARSET, LANGUAGE, VALUE). Note that both CHARSET and |
| 536 | LANGUAGE can be None, in which case you should consider VALUE to be |
| 537 | encoded in the us-ascii charset. You can usually ignore LANGUAGE. |
| 538 | |
| 539 | Your application should be prepared to deal with 3-tuple return |
| 540 | values, and can convert the parameter to a Unicode string like so: |
Barry Warsaw | 15aefa9 | 2002-09-26 17:19:34 +0000 | [diff] [blame] | 541 | |
| 542 | param = msg.get_param('foo') |
| 543 | if isinstance(param, tuple): |
Barry Warsaw | 6208369 | 2003-08-19 03:53:02 +0000 | [diff] [blame] | 544 | param = unicode(param[2], param[0] or 'us-ascii') |
Barry Warsaw | 15aefa9 | 2002-09-26 17:19:34 +0000 | [diff] [blame] | 545 | |
| 546 | In any case, the parameter value (either the returned string, or the |
| 547 | VALUE item in the 3-tuple) is always unquoted, unless unquote is set |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 548 | to False. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 549 | """ |
Barry Warsaw | beb5945 | 2001-09-26 05:41:51 +0000 | [diff] [blame] | 550 | if not self.has_key(header): |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 551 | return failobj |
Barry Warsaw | beb5945 | 2001-09-26 05:41:51 +0000 | [diff] [blame] | 552 | for k, v in self._get_params_preserve(failobj, header): |
| 553 | if k.lower() == param.lower(): |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 554 | if unquote: |
Barry Warsaw | 908dc4b | 2002-06-29 05:56:15 +0000 | [diff] [blame] | 555 | return _unquotevalue(v) |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 556 | else: |
| 557 | return v |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 558 | return failobj |
| 559 | |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 560 | def set_param(self, param, value, header='Content-Type', requote=True, |
Barry Warsaw | 3c25535 | 2002-09-06 03:55:04 +0000 | [diff] [blame] | 561 | charset=None, language=''): |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 562 | """Set a parameter in the Content-Type header. |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 563 | |
| 564 | If the parameter already exists in the header, its value will be |
| 565 | replaced with the new value. |
| 566 | |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 567 | If header is Content-Type and has not yet been defined for this |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 568 | message, it will be set to "text/plain" and the new parameter and |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 569 | value will be appended as per RFC 2045. |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 570 | |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 571 | An alternate header can specified in the header argument, and all |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 572 | parameters will be quoted as necessary unless requote is False. |
Barry Warsaw | 3c25535 | 2002-09-06 03:55:04 +0000 | [diff] [blame] | 573 | |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 574 | If charset is specified, the parameter will be encoded according to RFC |
| 575 | 2231. Optional language specifies the RFC 2231 language, defaulting |
| 576 | to the empty string. Both charset and language should be strings. |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 577 | """ |
Barry Warsaw | 5d84053 | 2004-05-09 03:44:55 +0000 | [diff] [blame] | 578 | if not isinstance(value, tuple) and charset: |
Barry Warsaw | 3c25535 | 2002-09-06 03:55:04 +0000 | [diff] [blame] | 579 | value = (charset, language, value) |
| 580 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 581 | if not self.has_key(header) and header.lower() == 'content-type': |
| 582 | ctype = 'text/plain' |
| 583 | else: |
| 584 | ctype = self.get(header) |
| 585 | if not self.get_param(param, header=header): |
| 586 | if not ctype: |
| 587 | ctype = _formatparam(param, value, requote) |
| 588 | else: |
| 589 | ctype = SEMISPACE.join( |
| 590 | [ctype, _formatparam(param, value, requote)]) |
| 591 | else: |
| 592 | ctype = '' |
| 593 | for old_param, old_value in self.get_params(header=header, |
| 594 | unquote=requote): |
| 595 | append_param = '' |
| 596 | if old_param.lower() == param.lower(): |
| 597 | append_param = _formatparam(param, value, requote) |
| 598 | else: |
| 599 | append_param = _formatparam(old_param, old_value, requote) |
| 600 | if not ctype: |
| 601 | ctype = append_param |
| 602 | else: |
| 603 | ctype = SEMISPACE.join([ctype, append_param]) |
| 604 | if ctype <> self.get(header): |
| 605 | del self[header] |
| 606 | self[header] = ctype |
| 607 | |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 608 | def del_param(self, param, header='content-type', requote=True): |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 609 | """Remove the given parameter completely from the Content-Type header. |
| 610 | |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 611 | The header will be re-written in place without the parameter or its |
| 612 | value. All values will be quoted as necessary unless requote is |
| 613 | False. Optional header specifies an alternative to the Content-Type |
| 614 | header. |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 615 | """ |
| 616 | if not self.has_key(header): |
| 617 | return |
| 618 | new_ctype = '' |
Barry Warsaw | 06fa042 | 2004-08-16 15:47:34 +0000 | [diff] [blame] | 619 | for p, v in self.get_params(header=header, unquote=requote): |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 620 | if p.lower() <> param.lower(): |
| 621 | if not new_ctype: |
| 622 | new_ctype = _formatparam(p, v, requote) |
| 623 | else: |
| 624 | new_ctype = SEMISPACE.join([new_ctype, |
| 625 | _formatparam(p, v, requote)]) |
| 626 | if new_ctype <> self.get(header): |
| 627 | del self[header] |
| 628 | self[header] = new_ctype |
| 629 | |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 630 | def set_type(self, type, header='Content-Type', requote=True): |
| 631 | """Set the main type and subtype for the Content-Type header. |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 632 | |
| 633 | type must be a string in the form "maintype/subtype", otherwise a |
| 634 | ValueError is raised. |
| 635 | |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 636 | This method replaces the Content-Type header, keeping all the |
| 637 | parameters in place. If requote is False, this leaves the existing |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 638 | header's quoting as is. Otherwise, the parameters will be quoted (the |
| 639 | default). |
| 640 | |
Barry Warsaw | 42d1d3e | 2002-09-30 18:17:35 +0000 | [diff] [blame] | 641 | An alternative header can be specified in the header argument. When |
| 642 | the Content-Type header is set, we'll always also add a MIME-Version |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 643 | header. |
| 644 | """ |
| 645 | # BAW: should we be strict? |
| 646 | if not type.count('/') == 1: |
| 647 | raise ValueError |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 648 | # Set the Content-Type, you get a MIME-Version |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 649 | if header.lower() == 'content-type': |
| 650 | del self['mime-version'] |
| 651 | self['MIME-Version'] = '1.0' |
| 652 | if not self.has_key(header): |
| 653 | self[header] = type |
| 654 | return |
Barry Warsaw | 06fa042 | 2004-08-16 15:47:34 +0000 | [diff] [blame] | 655 | params = self.get_params(header=header, unquote=requote) |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 656 | del self[header] |
| 657 | self[header] = type |
| 658 | # Skip the first param; it's the old type. |
| 659 | for p, v in params[1:]: |
| 660 | self.set_param(p, v, header, requote) |
| 661 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 662 | def get_filename(self, failobj=None): |
| 663 | """Return the filename associated with the payload if present. |
| 664 | |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 665 | The filename is extracted from the Content-Disposition header's |
Barry Warsaw | a0f28ef | 2006-01-17 04:49:07 +0000 | [diff] [blame] | 666 | `filename' parameter, and it is unquoted. If that header is missing |
| 667 | the `filename' parameter, this method falls back to looking for the |
| 668 | `name' parameter. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 669 | """ |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 670 | missing = object() |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 671 | filename = self.get_param('filename', missing, 'content-disposition') |
| 672 | if filename is missing: |
Barry Warsaw | a0f28ef | 2006-01-17 04:49:07 +0000 | [diff] [blame] | 673 | filename = self.get_param('name', missing, 'content-disposition') |
| 674 | if filename is missing: |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 675 | return failobj |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 676 | return utils.collapse_rfc2231_value(filename).strip() |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 677 | |
| 678 | def get_boundary(self, failobj=None): |
| 679 | """Return the boundary associated with the payload if present. |
| 680 | |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 681 | The boundary is extracted from the Content-Type header's `boundary' |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 682 | parameter, and it is unquoted. |
| 683 | """ |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 684 | missing = object() |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 685 | boundary = self.get_param('boundary', missing) |
| 686 | if boundary is missing: |
| 687 | return failobj |
Barry Warsaw | 93d9d5f | 2004-11-06 00:04:52 +0000 | [diff] [blame] | 688 | # RFC 2046 says that boundaries may begin but not end in w/s |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 689 | return utils.collapse_rfc2231_value(boundary).rstrip() |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 690 | |
| 691 | def set_boundary(self, boundary): |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 692 | """Set the boundary parameter in Content-Type to 'boundary'. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 693 | |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 694 | This is subtly different than deleting the Content-Type header and |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 695 | adding a new one with a new boundary parameter via add_header(). The |
| 696 | main difference is that using the set_boundary() method preserves the |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 697 | order of the Content-Type header in the original message. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 698 | |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 699 | HeaderParseError is raised if the message has no Content-Type header. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 700 | """ |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 701 | missing = object() |
Barry Warsaw | beb5945 | 2001-09-26 05:41:51 +0000 | [diff] [blame] | 702 | params = self._get_params_preserve(missing, 'content-type') |
| 703 | if params is missing: |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 704 | # There was no Content-Type header, and we don't know what type |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 705 | # to set it to, so raise an exception. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 706 | raise errors.HeaderParseError('No Content-Type header found') |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 707 | newparams = [] |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 708 | foundp = False |
Barry Warsaw | beb5945 | 2001-09-26 05:41:51 +0000 | [diff] [blame] | 709 | for pk, pv in params: |
| 710 | if pk.lower() == 'boundary': |
| 711 | newparams.append(('boundary', '"%s"' % boundary)) |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 712 | foundp = True |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 713 | else: |
Barry Warsaw | beb5945 | 2001-09-26 05:41:51 +0000 | [diff] [blame] | 714 | newparams.append((pk, pv)) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 715 | if not foundp: |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 716 | # The original Content-Type header had no boundary attribute. |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 717 | # Tack one on the end. BAW: should we raise an exception |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 718 | # instead??? |
Barry Warsaw | beb5945 | 2001-09-26 05:41:51 +0000 | [diff] [blame] | 719 | newparams.append(('boundary', '"%s"' % boundary)) |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 720 | # Replace the existing Content-Type header with the new value |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 721 | newheaders = [] |
| 722 | for h, v in self._headers: |
| 723 | if h.lower() == 'content-type': |
Barry Warsaw | beb5945 | 2001-09-26 05:41:51 +0000 | [diff] [blame] | 724 | parts = [] |
| 725 | for k, v in newparams: |
| 726 | if v == '': |
| 727 | parts.append(k) |
| 728 | else: |
| 729 | parts.append('%s=%s' % (k, v)) |
| 730 | newheaders.append((h, SEMISPACE.join(parts))) |
| 731 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 732 | else: |
| 733 | newheaders.append((h, v)) |
| 734 | self._headers = newheaders |
| 735 | |
Barry Warsaw | 15aefa9 | 2002-09-26 17:19:34 +0000 | [diff] [blame] | 736 | def get_content_charset(self, failobj=None): |
| 737 | """Return the charset parameter of the Content-Type header. |
| 738 | |
Barry Warsaw | ee07cb1 | 2002-10-10 15:13:26 +0000 | [diff] [blame] | 739 | The returned string is always coerced to lower case. If there is no |
| 740 | Content-Type header, or if that header has no charset parameter, |
| 741 | failobj is returned. |
Barry Warsaw | 15aefa9 | 2002-09-26 17:19:34 +0000 | [diff] [blame] | 742 | """ |
Barry Warsaw | bb11386 | 2004-10-03 03:16:19 +0000 | [diff] [blame] | 743 | missing = object() |
Barry Warsaw | 15aefa9 | 2002-09-26 17:19:34 +0000 | [diff] [blame] | 744 | charset = self.get_param('charset', missing) |
| 745 | if charset is missing: |
| 746 | return failobj |
Barry Warsaw | 5d84053 | 2004-05-09 03:44:55 +0000 | [diff] [blame] | 747 | if isinstance(charset, tuple): |
Barry Warsaw | 15aefa9 | 2002-09-26 17:19:34 +0000 | [diff] [blame] | 748 | # RFC 2231 encoded, so decode it, and it better end up as ascii. |
Barry Warsaw | 6208369 | 2003-08-19 03:53:02 +0000 | [diff] [blame] | 749 | pcharset = charset[0] or 'us-ascii' |
| 750 | charset = unicode(charset[2], pcharset).encode('us-ascii') |
Barry Warsaw | ee07cb1 | 2002-10-10 15:13:26 +0000 | [diff] [blame] | 751 | # RFC 2046, $4.1.2 says charsets are not case sensitive |
| 752 | return charset.lower() |
Barry Warsaw | 15aefa9 | 2002-09-26 17:19:34 +0000 | [diff] [blame] | 753 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 754 | def get_charsets(self, failobj=None): |
| 755 | """Return a list containing the charset(s) used in this message. |
Tim Peters | 527e64f | 2001-10-04 05:36:56 +0000 | [diff] [blame] | 756 | |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 757 | The returned list of items describes the Content-Type headers' |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 758 | charset parameter for this message and all the subparts in its |
| 759 | payload. |
| 760 | |
| 761 | Each item will either be a string (the value of the charset parameter |
Barry Warsaw | c494549 | 2002-09-28 20:40:25 +0000 | [diff] [blame] | 762 | in the Content-Type header of that part) or the value of the |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 763 | 'failobj' parameter (defaults to None), if the part does not have a |
| 764 | main MIME type of "text", or the charset is not defined. |
| 765 | |
| 766 | The list will contain one string for each part of the message, plus |
| 767 | one for the container message (i.e. self), so that a non-multipart |
| 768 | message will still return a list of length 1. |
| 769 | """ |
Barry Warsaw | 15aefa9 | 2002-09-26 17:19:34 +0000 | [diff] [blame] | 770 | return [part.get_content_charset(failobj) for part in self.walk()] |
Barry Warsaw | 5d84053 | 2004-05-09 03:44:55 +0000 | [diff] [blame] | 771 | |
| 772 | # I.e. def walk(self): ... |
| 773 | from email.Iterators import walk |