headerregistry.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. """Representing and manipulating email headers via custom objects.
  2. This module provides an implementation of the HeaderRegistry API.
  3. The implementation is designed to flexibly follow RFC5322 rules.
  4. Eventually HeaderRegistry will be a public API, but it isn't yet,
  5. and will probably change some before that happens.
  6. """
  7. from __future__ import unicode_literals
  8. from __future__ import division
  9. from __future__ import absolute_import
  10. from future.builtins import super
  11. from future.builtins import str
  12. from future.utils import text_to_native_str
  13. from future.backports.email import utils
  14. from future.backports.email import errors
  15. from future.backports.email import _header_value_parser as parser
  16. class Address(object):
  17. def __init__(self, display_name='', username='', domain='', addr_spec=None):
  18. """Create an object represeting a full email address.
  19. An address can have a 'display_name', a 'username', and a 'domain'. In
  20. addition to specifying the username and domain separately, they may be
  21. specified together by using the addr_spec keyword *instead of* the
  22. username and domain keywords. If an addr_spec string is specified it
  23. must be properly quoted according to RFC 5322 rules; an error will be
  24. raised if it is not.
  25. An Address object has display_name, username, domain, and addr_spec
  26. attributes, all of which are read-only. The addr_spec and the string
  27. value of the object are both quoted according to RFC5322 rules, but
  28. without any Content Transfer Encoding.
  29. """
  30. # This clause with its potential 'raise' may only happen when an
  31. # application program creates an Address object using an addr_spec
  32. # keyword. The email library code itself must always supply username
  33. # and domain.
  34. if addr_spec is not None:
  35. if username or domain:
  36. raise TypeError("addrspec specified when username and/or "
  37. "domain also specified")
  38. a_s, rest = parser.get_addr_spec(addr_spec)
  39. if rest:
  40. raise ValueError("Invalid addr_spec; only '{}' "
  41. "could be parsed from '{}'".format(
  42. a_s, addr_spec))
  43. if a_s.all_defects:
  44. raise a_s.all_defects[0]
  45. username = a_s.local_part
  46. domain = a_s.domain
  47. self._display_name = display_name
  48. self._username = username
  49. self._domain = domain
  50. @property
  51. def display_name(self):
  52. return self._display_name
  53. @property
  54. def username(self):
  55. return self._username
  56. @property
  57. def domain(self):
  58. return self._domain
  59. @property
  60. def addr_spec(self):
  61. """The addr_spec (username@domain) portion of the address, quoted
  62. according to RFC 5322 rules, but with no Content Transfer Encoding.
  63. """
  64. nameset = set(self.username)
  65. if len(nameset) > len(nameset-parser.DOT_ATOM_ENDS):
  66. lp = parser.quote_string(self.username)
  67. else:
  68. lp = self.username
  69. if self.domain:
  70. return lp + '@' + self.domain
  71. if not lp:
  72. return '<>'
  73. return lp
  74. def __repr__(self):
  75. return "Address(display_name={!r}, username={!r}, domain={!r})".format(
  76. self.display_name, self.username, self.domain)
  77. def __str__(self):
  78. nameset = set(self.display_name)
  79. if len(nameset) > len(nameset-parser.SPECIALS):
  80. disp = parser.quote_string(self.display_name)
  81. else:
  82. disp = self.display_name
  83. if disp:
  84. addr_spec = '' if self.addr_spec=='<>' else self.addr_spec
  85. return "{} <{}>".format(disp, addr_spec)
  86. return self.addr_spec
  87. def __eq__(self, other):
  88. if type(other) != type(self):
  89. return False
  90. return (self.display_name == other.display_name and
  91. self.username == other.username and
  92. self.domain == other.domain)
  93. class Group(object):
  94. def __init__(self, display_name=None, addresses=None):
  95. """Create an object representing an address group.
  96. An address group consists of a display_name followed by colon and an
  97. list of addresses (see Address) terminated by a semi-colon. The Group
  98. is created by specifying a display_name and a possibly empty list of
  99. Address objects. A Group can also be used to represent a single
  100. address that is not in a group, which is convenient when manipulating
  101. lists that are a combination of Groups and individual Addresses. In
  102. this case the display_name should be set to None. In particular, the
  103. string representation of a Group whose display_name is None is the same
  104. as the Address object, if there is one and only one Address object in
  105. the addresses list.
  106. """
  107. self._display_name = display_name
  108. self._addresses = tuple(addresses) if addresses else tuple()
  109. @property
  110. def display_name(self):
  111. return self._display_name
  112. @property
  113. def addresses(self):
  114. return self._addresses
  115. def __repr__(self):
  116. return "Group(display_name={!r}, addresses={!r}".format(
  117. self.display_name, self.addresses)
  118. def __str__(self):
  119. if self.display_name is None and len(self.addresses)==1:
  120. return str(self.addresses[0])
  121. disp = self.display_name
  122. if disp is not None:
  123. nameset = set(disp)
  124. if len(nameset) > len(nameset-parser.SPECIALS):
  125. disp = parser.quote_string(disp)
  126. adrstr = ", ".join(str(x) for x in self.addresses)
  127. adrstr = ' ' + adrstr if adrstr else adrstr
  128. return "{}:{};".format(disp, adrstr)
  129. def __eq__(self, other):
  130. if type(other) != type(self):
  131. return False
  132. return (self.display_name == other.display_name and
  133. self.addresses == other.addresses)
  134. # Header Classes #
  135. class BaseHeader(str):
  136. """Base class for message headers.
  137. Implements generic behavior and provides tools for subclasses.
  138. A subclass must define a classmethod named 'parse' that takes an unfolded
  139. value string and a dictionary as its arguments. The dictionary will
  140. contain one key, 'defects', initialized to an empty list. After the call
  141. the dictionary must contain two additional keys: parse_tree, set to the
  142. parse tree obtained from parsing the header, and 'decoded', set to the
  143. string value of the idealized representation of the data from the value.
  144. (That is, encoded words are decoded, and values that have canonical
  145. representations are so represented.)
  146. The defects key is intended to collect parsing defects, which the message
  147. parser will subsequently dispose of as appropriate. The parser should not,
  148. insofar as practical, raise any errors. Defects should be added to the
  149. list instead. The standard header parsers register defects for RFC
  150. compliance issues, for obsolete RFC syntax, and for unrecoverable parsing
  151. errors.
  152. The parse method may add additional keys to the dictionary. In this case
  153. the subclass must define an 'init' method, which will be passed the
  154. dictionary as its keyword arguments. The method should use (usually by
  155. setting them as the value of similarly named attributes) and remove all the
  156. extra keys added by its parse method, and then use super to call its parent
  157. class with the remaining arguments and keywords.
  158. The subclass should also make sure that a 'max_count' attribute is defined
  159. that is either None or 1. XXX: need to better define this API.
  160. """
  161. def __new__(cls, name, value):
  162. kwds = {'defects': []}
  163. cls.parse(value, kwds)
  164. if utils._has_surrogates(kwds['decoded']):
  165. kwds['decoded'] = utils._sanitize(kwds['decoded'])
  166. self = str.__new__(cls, kwds['decoded'])
  167. # del kwds['decoded']
  168. self.init(name, **kwds)
  169. return self
  170. def init(self, name, **_3to2kwargs):
  171. defects = _3to2kwargs['defects']; del _3to2kwargs['defects']
  172. parse_tree = _3to2kwargs['parse_tree']; del _3to2kwargs['parse_tree']
  173. self._name = name
  174. self._parse_tree = parse_tree
  175. self._defects = defects
  176. @property
  177. def name(self):
  178. return self._name
  179. @property
  180. def defects(self):
  181. return tuple(self._defects)
  182. def __reduce__(self):
  183. return (
  184. _reconstruct_header,
  185. (
  186. self.__class__.__name__,
  187. self.__class__.__bases__,
  188. str(self),
  189. ),
  190. self.__dict__)
  191. @classmethod
  192. def _reconstruct(cls, value):
  193. return str.__new__(cls, value)
  194. def fold(self, **_3to2kwargs):
  195. policy = _3to2kwargs['policy']; del _3to2kwargs['policy']
  196. """Fold header according to policy.
  197. The parsed representation of the header is folded according to
  198. RFC5322 rules, as modified by the policy. If the parse tree
  199. contains surrogateescaped bytes, the bytes are CTE encoded using
  200. the charset 'unknown-8bit".
  201. Any non-ASCII characters in the parse tree are CTE encoded using
  202. charset utf-8. XXX: make this a policy setting.
  203. The returned value is an ASCII-only string possibly containing linesep
  204. characters, and ending with a linesep character. The string includes
  205. the header name and the ': ' separator.
  206. """
  207. # At some point we need to only put fws here if it was in the source.
  208. header = parser.Header([
  209. parser.HeaderLabel([
  210. parser.ValueTerminal(self.name, 'header-name'),
  211. parser.ValueTerminal(':', 'header-sep')]),
  212. parser.CFWSList([parser.WhiteSpaceTerminal(' ', 'fws')]),
  213. self._parse_tree])
  214. return header.fold(policy=policy)
  215. def _reconstruct_header(cls_name, bases, value):
  216. return type(text_to_native_str(cls_name), bases, {})._reconstruct(value)
  217. class UnstructuredHeader(object):
  218. max_count = None
  219. value_parser = staticmethod(parser.get_unstructured)
  220. @classmethod
  221. def parse(cls, value, kwds):
  222. kwds['parse_tree'] = cls.value_parser(value)
  223. kwds['decoded'] = str(kwds['parse_tree'])
  224. class UniqueUnstructuredHeader(UnstructuredHeader):
  225. max_count = 1
  226. class DateHeader(object):
  227. """Header whose value consists of a single timestamp.
  228. Provides an additional attribute, datetime, which is either an aware
  229. datetime using a timezone, or a naive datetime if the timezone
  230. in the input string is -0000. Also accepts a datetime as input.
  231. The 'value' attribute is the normalized form of the timestamp,
  232. which means it is the output of format_datetime on the datetime.
  233. """
  234. max_count = None
  235. # This is used only for folding, not for creating 'decoded'.
  236. value_parser = staticmethod(parser.get_unstructured)
  237. @classmethod
  238. def parse(cls, value, kwds):
  239. if not value:
  240. kwds['defects'].append(errors.HeaderMissingRequiredValue())
  241. kwds['datetime'] = None
  242. kwds['decoded'] = ''
  243. kwds['parse_tree'] = parser.TokenList()
  244. return
  245. if isinstance(value, str):
  246. value = utils.parsedate_to_datetime(value)
  247. kwds['datetime'] = value
  248. kwds['decoded'] = utils.format_datetime(kwds['datetime'])
  249. kwds['parse_tree'] = cls.value_parser(kwds['decoded'])
  250. def init(self, *args, **kw):
  251. self._datetime = kw.pop('datetime')
  252. super().init(*args, **kw)
  253. @property
  254. def datetime(self):
  255. return self._datetime
  256. class UniqueDateHeader(DateHeader):
  257. max_count = 1
  258. class AddressHeader(object):
  259. max_count = None
  260. @staticmethod
  261. def value_parser(value):
  262. address_list, value = parser.get_address_list(value)
  263. assert not value, 'this should not happen'
  264. return address_list
  265. @classmethod
  266. def parse(cls, value, kwds):
  267. if isinstance(value, str):
  268. # We are translating here from the RFC language (address/mailbox)
  269. # to our API language (group/address).
  270. kwds['parse_tree'] = address_list = cls.value_parser(value)
  271. groups = []
  272. for addr in address_list.addresses:
  273. groups.append(Group(addr.display_name,
  274. [Address(mb.display_name or '',
  275. mb.local_part or '',
  276. mb.domain or '')
  277. for mb in addr.all_mailboxes]))
  278. defects = list(address_list.all_defects)
  279. else:
  280. # Assume it is Address/Group stuff
  281. if not hasattr(value, '__iter__'):
  282. value = [value]
  283. groups = [Group(None, [item]) if not hasattr(item, 'addresses')
  284. else item
  285. for item in value]
  286. defects = []
  287. kwds['groups'] = groups
  288. kwds['defects'] = defects
  289. kwds['decoded'] = ', '.join([str(item) for item in groups])
  290. if 'parse_tree' not in kwds:
  291. kwds['parse_tree'] = cls.value_parser(kwds['decoded'])
  292. def init(self, *args, **kw):
  293. self._groups = tuple(kw.pop('groups'))
  294. self._addresses = None
  295. super().init(*args, **kw)
  296. @property
  297. def groups(self):
  298. return self._groups
  299. @property
  300. def addresses(self):
  301. if self._addresses is None:
  302. self._addresses = tuple([address for group in self._groups
  303. for address in group.addresses])
  304. return self._addresses
  305. class UniqueAddressHeader(AddressHeader):
  306. max_count = 1
  307. class SingleAddressHeader(AddressHeader):
  308. @property
  309. def address(self):
  310. if len(self.addresses)!=1:
  311. raise ValueError(("value of single address header {} is not "
  312. "a single address").format(self.name))
  313. return self.addresses[0]
  314. class UniqueSingleAddressHeader(SingleAddressHeader):
  315. max_count = 1
  316. class MIMEVersionHeader(object):
  317. max_count = 1
  318. value_parser = staticmethod(parser.parse_mime_version)
  319. @classmethod
  320. def parse(cls, value, kwds):
  321. kwds['parse_tree'] = parse_tree = cls.value_parser(value)
  322. kwds['decoded'] = str(parse_tree)
  323. kwds['defects'].extend(parse_tree.all_defects)
  324. kwds['major'] = None if parse_tree.minor is None else parse_tree.major
  325. kwds['minor'] = parse_tree.minor
  326. if parse_tree.minor is not None:
  327. kwds['version'] = '{}.{}'.format(kwds['major'], kwds['minor'])
  328. else:
  329. kwds['version'] = None
  330. def init(self, *args, **kw):
  331. self._version = kw.pop('version')
  332. self._major = kw.pop('major')
  333. self._minor = kw.pop('minor')
  334. super().init(*args, **kw)
  335. @property
  336. def major(self):
  337. return self._major
  338. @property
  339. def minor(self):
  340. return self._minor
  341. @property
  342. def version(self):
  343. return self._version
  344. class ParameterizedMIMEHeader(object):
  345. # Mixin that handles the params dict. Must be subclassed and
  346. # a property value_parser for the specific header provided.
  347. max_count = 1
  348. @classmethod
  349. def parse(cls, value, kwds):
  350. kwds['parse_tree'] = parse_tree = cls.value_parser(value)
  351. kwds['decoded'] = str(parse_tree)
  352. kwds['defects'].extend(parse_tree.all_defects)
  353. if parse_tree.params is None:
  354. kwds['params'] = {}
  355. else:
  356. # The MIME RFCs specify that parameter ordering is arbitrary.
  357. kwds['params'] = dict((utils._sanitize(name).lower(),
  358. utils._sanitize(value))
  359. for name, value in parse_tree.params)
  360. def init(self, *args, **kw):
  361. self._params = kw.pop('params')
  362. super().init(*args, **kw)
  363. @property
  364. def params(self):
  365. return self._params.copy()
  366. class ContentTypeHeader(ParameterizedMIMEHeader):
  367. value_parser = staticmethod(parser.parse_content_type_header)
  368. def init(self, *args, **kw):
  369. super().init(*args, **kw)
  370. self._maintype = utils._sanitize(self._parse_tree.maintype)
  371. self._subtype = utils._sanitize(self._parse_tree.subtype)
  372. @property
  373. def maintype(self):
  374. return self._maintype
  375. @property
  376. def subtype(self):
  377. return self._subtype
  378. @property
  379. def content_type(self):
  380. return self.maintype + '/' + self.subtype
  381. class ContentDispositionHeader(ParameterizedMIMEHeader):
  382. value_parser = staticmethod(parser.parse_content_disposition_header)
  383. def init(self, *args, **kw):
  384. super().init(*args, **kw)
  385. cd = self._parse_tree.content_disposition
  386. self._content_disposition = cd if cd is None else utils._sanitize(cd)
  387. @property
  388. def content_disposition(self):
  389. return self._content_disposition
  390. class ContentTransferEncodingHeader(object):
  391. max_count = 1
  392. value_parser = staticmethod(parser.parse_content_transfer_encoding_header)
  393. @classmethod
  394. def parse(cls, value, kwds):
  395. kwds['parse_tree'] = parse_tree = cls.value_parser(value)
  396. kwds['decoded'] = str(parse_tree)
  397. kwds['defects'].extend(parse_tree.all_defects)
  398. def init(self, *args, **kw):
  399. super().init(*args, **kw)
  400. self._cte = utils._sanitize(self._parse_tree.cte)
  401. @property
  402. def cte(self):
  403. return self._cte
  404. # The header factory #
  405. _default_header_map = {
  406. 'subject': UniqueUnstructuredHeader,
  407. 'date': UniqueDateHeader,
  408. 'resent-date': DateHeader,
  409. 'orig-date': UniqueDateHeader,
  410. 'sender': UniqueSingleAddressHeader,
  411. 'resent-sender': SingleAddressHeader,
  412. 'to': UniqueAddressHeader,
  413. 'resent-to': AddressHeader,
  414. 'cc': UniqueAddressHeader,
  415. 'resent-cc': AddressHeader,
  416. 'bcc': UniqueAddressHeader,
  417. 'resent-bcc': AddressHeader,
  418. 'from': UniqueAddressHeader,
  419. 'resent-from': AddressHeader,
  420. 'reply-to': UniqueAddressHeader,
  421. 'mime-version': MIMEVersionHeader,
  422. 'content-type': ContentTypeHeader,
  423. 'content-disposition': ContentDispositionHeader,
  424. 'content-transfer-encoding': ContentTransferEncodingHeader,
  425. }
  426. class HeaderRegistry(object):
  427. """A header_factory and header registry."""
  428. def __init__(self, base_class=BaseHeader, default_class=UnstructuredHeader,
  429. use_default_map=True):
  430. """Create a header_factory that works with the Policy API.
  431. base_class is the class that will be the last class in the created
  432. header class's __bases__ list. default_class is the class that will be
  433. used if "name" (see __call__) does not appear in the registry.
  434. use_default_map controls whether or not the default mapping of names to
  435. specialized classes is copied in to the registry when the factory is
  436. created. The default is True.
  437. """
  438. self.registry = {}
  439. self.base_class = base_class
  440. self.default_class = default_class
  441. if use_default_map:
  442. self.registry.update(_default_header_map)
  443. def map_to_type(self, name, cls):
  444. """Register cls as the specialized class for handling "name" headers.
  445. """
  446. self.registry[name.lower()] = cls
  447. def __getitem__(self, name):
  448. cls = self.registry.get(name.lower(), self.default_class)
  449. return type(text_to_native_str('_'+cls.__name__), (cls, self.base_class), {})
  450. def __call__(self, name, value):
  451. """Create a header instance for header 'name' from 'value'.
  452. Creates a header instance by creating a specialized class for parsing
  453. and representing the specified header by combining the factory
  454. base_class with a specialized class from the registry or the
  455. default_class, and passing the name and value to the constructed
  456. class's constructor.
  457. """
  458. return self[name](name, value)