Debian Codegraph @

Lib/email/contentmanager.py

raw
1 import binascii
2 import email.charset
3 import email.message
4 import email.errors
5 import sys
6 from email import quoprimime
7
8 class ContentManager:
9
10 def __init__(self):
11 self.get_handlers = {}
12 self.set_handlers = {}
13
14 def add_get_handler(self, key, handler):
15 self.get_handlers[key] = handler
16
17 def get_content(self, msg, *args, **kw):
18 content_type = msg.get_content_type()
19 if content_type in self.get_handlers:
20 return self.get_handlers[content_type](msg, *args, **kw)
21 maintype = msg.get_content_maintype()
22 if maintype in self.get_handlers:
23 return self.get_handlers[maintype](msg, *args, **kw)
24 if '' in self.get_handlers:
25 return self.get_handlers[''](msg, *args, **kw)
26 raise KeyError(content_type)
27
28 def add_set_handler(self, typekey, handler):
29 self.set_handlers[typekey] = handler
30
31 def set_content(self, msg, obj, *args, **kw):
32 if msg.get_content_maintype() == 'multipart':
33 # XXX: is this error a good idea or not? We can remove it later,
34 # but we can't add it later, so do it for now.
35 raise TypeError("set_content not valid on multipart")
36 handler = self._find_set_handler(msg, obj)
37 msg.clear_content()
38 handler(msg, obj, *args, **kw)
39
40 def _find_set_handler(self, msg, obj):
41 full_path_for_error = None
42 for typ in type(obj).__mro__:
43 if typ in self.set_handlers:
44 return self.set_handlers[typ]
45 qname = typ.__qualname__
46 modname = getattr(typ, '__module__', '')
47 full_path = '.'.join((modname, qname)) if modname else qname
48 if full_path_for_error is None:
49 full_path_for_error = full_path
50 if full_path in self.set_handlers:
51 return self.set_handlers[full_path]
52 if qname in self.set_handlers:
53 return self.set_handlers[qname]
54 name = typ.__name__
55 if name in self.set_handlers:
56 return self.set_handlers[name]
57 if None in self.set_handlers:
58 return self.set_handlers[None]
59 raise KeyError(full_path_for_error)
60
61
62 raw_data_manager = ContentManager()
63
64
65 def get_text_content(msg, errors='replace'):
66 content = msg.get_payload(decode=True)
67 charset = msg.get_param('charset', 'ASCII')
68 return content.decode(charset, errors=errors)
69 raw_data_manager.add_get_handler('text', get_text_content)
70
71
72 def get_non_text_content(msg):
73 return msg.get_payload(decode=True)
74 for maintype in 'audio image video application'.split():
75 raw_data_manager.add_get_handler(maintype, get_non_text_content)
76 del maintype
77
78
79 def get_message_content(msg):
80 return msg.get_payload(0)
81 for subtype in 'rfc822 external-body'.split():
82 raw_data_manager.add_get_handler('message/'+subtype, get_message_content)
83 del subtype
84
85
86 def get_and_fixup_unknown_message_content(msg):
87 # If we don't understand a message subtype, we are supposed to treat it as
88 # if it were application/octet-stream, per
89 # tools.ietf.org/html/rfc2046#section-5.2.4. Feedparser doesn't do that,
90 # so do our best to fix things up. Note that it is *not* appropriate to
91 # model message/partial content as Message objects, so they are handled
92 # here as well. (How to reassemble them is out of scope for this comment :)
93 return bytes(msg.get_payload(0))
94 raw_data_manager.add_get_handler('message',
95 get_and_fixup_unknown_message_content)
96
97
98 def _prepare_set(msg, maintype, subtype, headers):
99 msg['Content-Type'] = '/'.join((maintype, subtype))
100 if headers:
101 if not hasattr(headers[0], 'name'):
102 mp = msg.policy
103 headers = [mp.header_factory(*mp.header_source_parse([header]))
104 for header in headers]
105 try:
106 for header in headers:
107 if header.defects:
108 raise header.defects[0]
109 msg[header.name] = header
110 except email.errors.HeaderDefect as exc:
111 raise ValueError("Invalid header: {}".format(
112 header.fold(policy=msg.policy))) from exc
113
114
115 def _finalize_set(msg, disposition, filename, cid, params):
116 if disposition is None and filename is not None:
117 disposition = 'attachment'
118 if disposition is not None:
119 msg['Content-Disposition'] = disposition
120 if filename is not None:
121 msg.set_param('filename',
122 filename,
123 header='Content-Disposition',
124 replace=True)
125 if cid is not None:
126 msg['Content-ID'] = cid
127 if params is not None:
128 for key, value in params.items():
129 msg.set_param(key, value)
130
131
132 # XXX: This is a cleaned-up version of base64mime.body_encode (including a bug
133 # fix in the calculation of unencoded_bytes_per_line). It would be nice to
134 # drop both this and quoprimime.body_encode in favor of enhanced binascii
135 # routines that accepted a max_line_length parameter.
136 def _encode_base64(data, max_line_length):
137 encoded_lines = []
138 unencoded_bytes_per_line = max_line_length // 4 * 3
139 for i in range(0, len(data), unencoded_bytes_per_line):
140 thisline = data[i:i+unencoded_bytes_per_line]
141 encoded_lines.append(binascii.b2a_base64(thisline).decode('ascii'))
142 return ''.join(encoded_lines)
143
144
145 def _encode_text(string, charset, cte, policy):
146 # If max_line_length is 0 or None, there is no limit.
147 maxlen = policy.max_line_length or sys.maxsize
148 lines = string.encode(charset).splitlines()
149 linesep = policy.linesep.encode('ascii')
150 def embedded_body(lines): return linesep.join(lines) + linesep
151 def normal_body(lines): return b'\n'.join(lines) + b'\n'
152 if cte is None:
153 # Use heuristics to decide on the "best" encoding.
154 if max(map(len, lines), default=0) <= maxlen:
155 try:
156 return '7bit', normal_body(lines).decode('ascii')
157 except UnicodeDecodeError:
158 pass
159 if policy.cte_type == '8bit':
160 return '8bit', normal_body(lines).decode('ascii', 'surrogateescape')
161 sniff = embedded_body(lines[:10])
162 sniff_qp = quoprimime.body_encode(sniff.decode('latin-1'), maxlen)
163 sniff_base64 = binascii.b2a_base64(sniff)
164 # This is a little unfair to qp; it includes lineseps, base64 doesn't.
165 if len(sniff_qp) > len(sniff_base64):
166 cte = 'base64'
167 else:
168 cte = 'quoted-printable'
169 if len(lines) <= 10:
170 return cte, sniff_qp
171 if cte == '7bit':
172 data = normal_body(lines).decode('ascii')
173 elif cte == '8bit':
174 data = normal_body(lines).decode('ascii', 'surrogateescape')
175 elif cte == 'quoted-printable':
176 data = quoprimime.body_encode(normal_body(lines).decode('latin-1'),
177 maxlen)
178 elif cte == 'base64':
179 data = _encode_base64(embedded_body(lines), maxlen)
180 else:
181 raise ValueError("Unknown content transfer encoding {}".format(cte))
182 return cte, data
183
184
185 def set_text_content(msg, string, subtype="plain", charset='utf-8', cte=None,
186 disposition=None, filename=None, cid=None,
187 params=None, headers=None):
188 _prepare_set(msg, 'text', subtype, headers)
189 cte, payload = _encode_text(string, charset, cte, msg.policy)
190 msg.set_payload(payload)
191 msg.set_param('charset',
192 email.charset.ALIASES.get(charset, charset),
193 replace=True)
194 msg['Content-Transfer-Encoding'] = cte
195 _finalize_set(msg, disposition, filename, cid, params)
196 raw_data_manager.add_set_handler(str, set_text_content)
197
198
199 def set_message_content(msg, message, subtype="rfc822", cte=None,
200 disposition=None, filename=None, cid=None,
201 params=None, headers=None):
202 if subtype == 'partial':
203 raise ValueError("message/partial is not supported for Message objects")
204 if subtype == 'rfc822':
205 if cte not in (None, '7bit', '8bit', 'binary'):
206 # http://tools.ietf.org/html/rfc2046#section-5.2.1 mandate.
207 raise ValueError(
208 "message/rfc822 parts do not support cte={}".format(cte))
209 # 8bit will get coerced on serialization if policy.cte_type='7bit'. We
210 # may end up claiming 8bit when it isn't needed, but the only negative
211 # result of that should be a gateway that needs to coerce to 7bit
212 # having to look through the whole embedded message to discover whether
213 # or not it actually has to do anything.
214 cte = '8bit' if cte is None else cte
215 elif subtype == 'external-body':
216 if cte not in (None, '7bit'):
217 # http://tools.ietf.org/html/rfc2046#section-5.2.3 mandate.
218 raise ValueError(
219 "message/external-body parts do not support cte={}".format(cte))
220 cte = '7bit'
221 elif cte is None:
222 # http://tools.ietf.org/html/rfc2046#section-5.2.4 says all future
223 # subtypes should be restricted to 7bit, so assume that.
224 cte = '7bit'
225 _prepare_set(msg, 'message', subtype, headers)
226 msg.set_payload([message])
227 msg['Content-Transfer-Encoding'] = cte
228 _finalize_set(msg, disposition, filename, cid, params)
229 raw_data_manager.add_set_handler(email.message.Message, set_message_content)
230
231
232 def set_bytes_content(msg, data, maintype, subtype, cte='base64',
233 disposition=None, filename=None, cid=None,
234 params=None, headers=None):
235 _prepare_set(msg, maintype, subtype, headers)
236 if cte == 'base64':
237 data = _encode_base64(data, max_line_length=msg.policy.max_line_length)
238 elif cte == 'quoted-printable':
239 # XXX: quoprimime.body_encode won't encode newline characters in data,
240 # so we can't use it. This means max_line_length is ignored. Another
241 # bug to fix later. (Note: encoders.quopri is broken on line ends.)
242 data = binascii.b2a_qp(data, istext=False, header=False, quotetabs=True)
243 data = data.decode('ascii')
244 elif cte == '7bit':
245 data = data.decode('ascii')
246 elif cte in ('8bit', 'binary'):
247 data = data.decode('ascii', 'surrogateescape')
248 msg.set_payload(data)
249 msg['Content-Transfer-Encoding'] = cte
250 _finalize_set(msg, disposition, filename, cid, params)
251 for typ in (bytes, bytearray, memoryview):
252 raw_data_manager.add_set_handler(typ, set_bytes_content)
253 del typ