def parse_mimepart_address_header(mimepart, header_name): # type: (MimePart, str) -> List[List[str]] # Header parsing is complicated by the fact that: # (1) You can have multiple occurrences of the same header; # (2) Phrases or comments can be RFC2047-style encoded words; # (3) Everything is terrible. # Here, for each occurrence of the header in question, we first parse # it into a list of (phrase, addrspec) tuples and then use flanker to # decode any encoded words. # You want to do it in that order, because otherwise if you get a header # like # From: =?utf-8?Q?FooCorp=2C=20Inc.=? <*****@*****.**> # you can end up parsing 'FooCorp, Inc. <*****@*****.**> (note lack of # quoting) into two separate addresses. # Consult RFC822 Section 6.1 and RFC2047 section 5 for details. addresses = set() # type: Set[Tuple[str, str]] for section in mimepart.headers._v.getall(normalize(header_name)): for phrase, addrspec in email.utils.getaddresses([section]): if not addrspec and not phrase: continue addresses.add((decode(phrase), decode(addrspec))) # Return a list of lists because it makes it easier to compare an address # field to one which has been fetched from the db. return sorted(list(elem) for elem in addresses)
def __setitem__(self, key, value): key = normalize(key) if key in self._v: self._v[key] = remove_newlines(value) self.changed = True else: self.prepend(key, remove_newlines(value))
def get(self, key, default=None): """ Returns header value (case-insensitive). """ v = self._v.get(normalize(key), default) if v is not None: return encodedword.decode(v) return None
def parse_mimepart_address_header(mimepart, header_name): # Header parsing is complicated by the fact that: # (1) You can have multiple occurrences of the same header; # (2) Phrases or comments can be RFC2047-style encoded words; # (3) Everything is terrible. # Here, for each occurrence of the header in question, we first parse # it into a list of (phrase, addrspec) tuples and then use flanker to # decode any encoded words. # You want to do it in that order, because otherwise if you get a header # like # From: =?utf-8?Q?FooCorp=2C=20Inc.=? <*****@*****.**> # you can end up parsing 'FooCorp, Inc. <*****@*****.**> (note lack of # quoting) into two separate addresses. # Consult RFC822 Section 6.1 and RFC2047 section 5 for details. addresses = set() for section in mimepart.headers._v.getall(normalize(header_name)): for phrase, addrspec in rfc822.AddressList(section).addresslist: addresses.add((decode(phrase), decode(addrspec))) # Return a list of lists because it makes it easier to compare an address # field to one which has been fetched from the db. return sorted(list(elem) for elem in addresses)
def getraw(self, key, default=None): """ Returns raw header value (case-insensitive, non-decoded. """ return self._v.get(normalize(key), default)
def getall(self, key): """ Returns all header values by the given header name (case-insensitive). """ v = self._v.getall(normalize(key)) return [encodedword.decode(x) for x in v]
def getall(self, key): """ Returns all header values by the given header name (case-insensitive) """ return self._v.getall(normalize(key))
def __getitem__(self, key): return self.v.get(normalize(key), None)
def add(self, key, value): """Adds header without changing the existing headers with same name""" self._v.add(normalize(key), remove_newlines(value)) self.changed = True
def get(self, key, default=None): """ Returns header value (case-insensitive). """ return self._v.get(normalize(key), default)
def __delitem__(self, key): del self._v[normalize(key)] self.changed = True
def __init__(self, items=()): self.v = MultiDict([(normalize(key), val) for (key, val) in items]) self.changed = False
def __init__(self, items=()): self._v = MultiDict([(normalize(key), remove_newlines(val)) for (key, val) in items]) self.changed = False self.num_prepends = 0
def __contains__(self, key): return normalize(key) in self._v
def __setitem__(self, key, value): self.v[normalize(key)] = _remove_newlines(value) self.changed = True
def __init__(self, items=()): self.v = MultiDict( [(normalize(key), val) for (key, val) in items]) self.changed = False
def getall(self, key): """ Returns all header values by the given header name (case-insensitive) """ return self.v.getall(normalize(key))
def get(self, key, default=None): """ Returns header value (case-insensitive). """ return self.v.get(normalize(key), default)
def add(self, key, value): """Adds header without changing the existing headers with same name""" self.v.add(normalize(key), _remove_newlines(value)) self.changed = True
def __getitem__(self, key): v = self._v.get(normalize(key), None) if v is not None: return encodedword.decode(v) return None
def __getitem__(self, key): return self._v.get(normalize(key), None)
def __setitem__(self, key, value): self._v[normalize(key)] = remove_newlines(value) self.changed = True
def prepend(self, key, value): self._v._items.insert(0, (normalize(key), remove_newlines(value))) self.changed = True
def prepend(self, key, value): self._v._items.insert(0, (normalize(key), remove_newlines(value))) self.num_prepends += 1