headerregistry.py 20 KB

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