Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
 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))
Ejemplo n.º 3
0
 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
Ejemplo n.º 4
0
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)
Ejemplo n.º 5
0
 def getraw(self, key, default=None):
     """
     Returns raw header value (case-insensitive, non-decoded.
     """
     return self._v.get(normalize(key), default)
Ejemplo n.º 6
0
 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]
Ejemplo n.º 7
0
 def getall(self, key):
     """
     Returns all header values by the given header name
     (case-insensitive)
     """
     return self._v.getall(normalize(key))
Ejemplo n.º 8
0
 def __getitem__(self, key):
     return self.v.get(normalize(key), None)
Ejemplo n.º 9
0
    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
Ejemplo n.º 10
0
 def get(self, key, default=None):
     """
     Returns header value (case-insensitive).
     """
     return self._v.get(normalize(key), default)
Ejemplo n.º 11
0
 def __delitem__(self, key):
     del self._v[normalize(key)]
     self.changed = True
Ejemplo n.º 12
0
 def __init__(self, items=()):
     self.v = MultiDict([(normalize(key), val) for (key, val) in items])
     self.changed = False
Ejemplo n.º 13
0
 def __init__(self, items=()):
     self._v = MultiDict([(normalize(key), remove_newlines(val))
                          for (key, val) in items])
     self.changed = False
     self.num_prepends = 0
Ejemplo n.º 14
0
 def __contains__(self, key):
     return normalize(key) in self._v
Ejemplo n.º 15
0
 def __setitem__(self, key, value):
     self.v[normalize(key)] = _remove_newlines(value)
     self.changed = True
Ejemplo n.º 16
0
 def __init__(self, items=()):
     self.v = MultiDict(
         [(normalize(key), val) for (key, val) in items])
     self.changed = False
Ejemplo n.º 17
0
 def getall(self, key):
     """
     Returns all header values by the given header name
     (case-insensitive)
     """
     return self.v.getall(normalize(key))
Ejemplo n.º 18
0
 def get(self, key, default=None):
     """
     Returns header value (case-insensitive).
     """
     return self.v.get(normalize(key), default)
Ejemplo n.º 19
0
    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
Ejemplo n.º 20
0
 def __getitem__(self, key):
     v = self._v.get(normalize(key), None)
     if v is not None:
         return encodedword.decode(v)
     return None
Ejemplo n.º 21
0
 def __getitem__(self, key):
     return self._v.get(normalize(key), None)
Ejemplo n.º 22
0
 def __setitem__(self, key, value):
     self._v[normalize(key)] = remove_newlines(value)
     self.changed = True
Ejemplo n.º 23
0
 def prepend(self, key, value):
     self._v._items.insert(0, (normalize(key), remove_newlines(value)))
     self.changed = True
Ejemplo n.º 24
0
 def prepend(self, key, value):
     self._v._items.insert(0, (normalize(key), remove_newlines(value)))
     self.num_prepends += 1
Ejemplo n.º 25
0
 def prepend(self, key, value):
     self._v._items.insert(0, (normalize(key), remove_newlines(value)))
     self.changed = True