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