headerregistry.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. nameset = set(self.username)
  63. if len(nameset) > len(nameset-parser.DOT_ATOM_ENDS):
  64. lp = parser.quote_string(self.username)
  65. else:
  66. lp = self.username
  67. if self.domain:
  68. return lp + '@' + self.domain
  69. if not lp:
  70. return '<>'
  71. return lp
  72. def __repr__(self):
  73. return "{}(display_name={!r}, username={!r}, domain={!r})".format(
  74. self.__class__.__name__,
  75. self.display_name, self.username, self.domain)
  76. def __str__(self):
  77. nameset = set(self.display_name)
  78. if len(nameset) > len(nameset-parser.SPECIALS):
  79. disp = parser.quote_string(self.display_name)
  80. else:
  81. disp = self.display_name
  82. if disp:
  83. addr_spec = '' if self.addr_spec=='<>' else self.addr_spec
  84. return "{} <{}>".format(disp, addr_spec)
  85. return self.addr_spec
  86. def __eq__(self, other):
  87. if type(other) != type(self):
  88. return False
  89. return (self.display_name == other.display_name and
  90. self.username == other.username and
  91. self.domain == other.domain)
  92. class Group:
  93. def __init__(self, display_name=None, addresses=None):
  94. """Create an object representing an address group.
  95. An address group consists of a display_name followed by colon and a
  96. list of addresses (see Address) terminated by a semi-colon. The Group
  97. is created by specifying a display_name and a possibly empty list of
  98. Address objects. A Group can also be used to represent a single
  99. address that is not in a group, which is convenient when manipulating
  100. lists that are a combination of Groups and individual Addresses. In
  101. this case the display_name should be set to None. In particular, the
  102. string representation of a Group whose display_name is None is the same
  103. as the Address object, if there is one and only one Address object in
  104. the addresses list.
  105. """
  106. self._display_name = display_name
  107. self._addresses = tuple(addresses) if addresses else tuple()
  108. @property
  109. def display_name(self):
  110. return self._display_name
  111. @property
  112. def addresses(self):
  113. return self._addresses
  114. def __repr__(self):
  115. return "{}(display_name={!r}, addresses={!r}".format(
  116. self.__class__.__name__,
  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, *, parse_tree, defects):
  171. self._name = name
  172. self._parse_tree = parse_tree
  173. self._defects = defects
  174. @property
  175. def name(self):
  176. return self._name
  177. @property
  178. def defects(self):
  179. return tuple(self._defects)
  180. def __reduce__(self):
  181. return (
  182. _reconstruct_header,
  183. (
  184. self.__class__.__name__,
  185. self.__class__.__bases__,
  186. str(self),
  187. ),
  188. self.__dict__)
  189. @classmethod
  190. def _reconstruct(cls, value):
  191. return str.__new__(cls, value)
  192. def fold(self, *, policy):
  193. """Fold header according to policy.
  194. The parsed representation of the header is folded according to
  195. RFC5322 rules, as modified by the policy. If the parse tree
  196. contains surrogateescaped bytes, the bytes are CTE encoded using
  197. the charset 'unknown-8bit".
  198. Any non-ASCII characters in the parse tree are CTE encoded using
  199. charset utf-8. XXX: make this a policy setting.
  200. The returned value is an ASCII-only string possibly containing linesep
  201. characters, and ending with a linesep character. The string includes
  202. the header name and the ': ' separator.
  203. """
  204. # At some point we need to put fws here if it was in the source.
  205. header = parser.Header([
  206. parser.HeaderLabel([
  207. parser.ValueTerminal(self.name, 'header-name'),
  208. parser.ValueTerminal(':', 'header-sep')]),
  209. ])
  210. if self._parse_tree:
  211. header.append(
  212. parser.CFWSList([parser.WhiteSpaceTerminal(' ', 'fws')]))
  213. header.append(self._parse_tree)
  214. return header.fold(policy=policy)
  215. def _reconstruct_header(cls_name, bases, value):
  216. return type(cls_name, bases, {})._reconstruct(value)
  217. class UnstructuredHeader:
  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:
  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:
  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:
  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:
  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'] = {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 MappingProxyType(self._params)
  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:
  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:
  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('_'+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)