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