Esempio n. 1
0
 def getaddrs(self, *hdrs):
     addrs = []
     for hdr in hdrs:
         for cmt, addr in Utils.getaddresses(self.get_all(hdr, [])):
             try:
                 valid_addr(addr)
             except Errors.MessageError, e:
                 raise Errors.MessageError('%s: %s' % (hdr, e))
             addrs.append(addr)
Esempio n. 2
0
 def getaddrs(self, *hdrs):
     addrs = []
     for hdr in hdrs:
         for cmt, addr in Utils.getaddresses(self.get_all(hdr, [])):
             try:
                 valid_addr(addr)
             except Errors.MessageError, e:
                 raise Errors.MessageError('%s: %s' % (hdr, e))
             addrs.append(addr)
Esempio n. 3
0
    def _deliver_message(self, msg, delivered_to, received):
        self.log.trace()
        matched = []
        header_addrs = []
        fieldnames = (
            ('delivered-to', ),
            ('envelope-to', ),
            ('x-envelope-to', ),
            ('apparently-to', ),
            ('resent-to', 'resent-cc', 'resent-bcc'),
            ('to', 'cc', 'bcc'),
        )
        for fields in fieldnames:
            for field in fields:
                self.log.debug(
                    'looking for addresses in %s header fields\n' % field
                )
                header_addrs.extend(
                    [addr for (name, addr) in Utils.getaddresses(
                        msg.get_all(field, [])
                     ) if addr]
                )
            if header_addrs:
                # Got some addresses, quit here
                self.log.debug('found total of %d addresses (%s)\n'
                               % (len(header_addrs), header_addrs))
                break
            else:
                self.log.debug('no addresses found, continuing\n')

        for (pattern, dest) in self.targets:
            for addr in header_addrs:
                self.log.debug('checking address %s against pattern %s\n'
                               % (addr, pattern.pattern))
                if pattern.search(addr):
                    self.log.debug('address %s matched target %s\n'
                                   % (addr, dest))
                    dest.deliver_message(msg, delivered_to, received)
                    matched.append(str(dest))
                    # Only deliver once to each destination; this one matched,
                    # so we don't need to check any remaining addresses against
                    # this pattern
                    break
        if not matched:
            if self.targets:
                self.log.debug('no addresses matched; using default %s\n'
                               % self.default)
            else:
                self.log.debug('using default %s\n' % self.default)
            return 'MultiGuesser (default %s)' % self.default.deliver_message(
                msg, delivered_to, received
            )
        return 'MultiGuesser (%s)' % matched
Esempio n. 4
0
    def get_message_addresses(message):
        """Get the origin and destination addresses of a message.

        :param message: A message object supporting get() to access its
            headers, like email.Message or bzrlib.email_message.EmailMessage.
        :return: A pair (from_email, to_emails), where from_email is the email
            address in the From header, and to_emails a list of all the
            addresses in the To, Cc, and Bcc headers.
        """
        from_email = Utils.parseaddr(message.get('From', None))[1]
        to_full_addresses = []
        for header in ['To', 'Cc', 'Bcc']:
            value = message.get(header, None)
            if value:
                to_full_addresses.append(value)
        to_emails = [pair[1] for pair in Utils.getaddresses(to_full_addresses)]

        return from_email, to_emails
Esempio n. 5
0
def __assignToKind(view, kindVar, messageObject, key, hType, attr, decode,
                   makeUnicode):
    header = messageObject.get(key)

    if header is None:
        return None

    if decode:
        header = decodeHeader(header)

    elif makeUnicode:
        header = getUnicodeValue(header)

    if hType == "String":
        setattr(kindVar, attr, header)

    elif hType == "EmailAddress":
        name, addr = emailUtils.parseaddr(messageObject.get(key))

        if decode:
            name = decodeHeader(name)

        if makeUnicode:
            addr = getUnicodeValue(addr)

        ea = EmailAddress.getEmailAddress(view, addr, name)

        if ea is not None:
            setattr(kindVar, attr, ea)

    elif hType == "EmailAddressList":
        for name, addr in emailUtils.getaddresses(
                messageObject.get_all(key, [])):
            if decode:
                name = decodeHeader(name)

            if makeUnicode:
                addr = getUnicodeValue(addr)

            ea = EmailAddress.getEmailAddress(view, addr, name)

            if ea is not None:
                kindVar.append(ea)
Esempio n. 6
0
    def get_message_addresses(message):
        """Get the origin and destination addresses of a message.

        :param message: A message object supporting get() to access its
            headers, like email.Message or bzrlib.email_message.EmailMessage.
        :return: A pair (from_email, to_emails), where from_email is the email
            address in the From header, and to_emails a list of all the
            addresses in the To, Cc, and Bcc headers.
        """
        from_email = Utils.parseaddr(message.get('From', None))[1]
        to_full_addresses = []
        for header in ['To', 'Cc', 'Bcc']:
            value = message.get(header, None)
            if value:
                to_full_addresses.append(value)
        to_emails = [ pair[1] for pair in
                Utils.getaddresses(to_full_addresses) ]

        return from_email, to_emails
Esempio n. 7
0
def __assignToKind(view, kindVar, messageObject, key, hType, attr, decode, makeUnicode):
    header = messageObject.get(key)

    if header is None:
        return None

    if decode:
        header = decodeHeader(header)

    elif makeUnicode:
        header = getUnicodeValue(header)

    if hType == "String":
        setattr(kindVar, attr, header)

    elif hType == "EmailAddress":
        name, addr = emailUtils.parseaddr(messageObject.get(key))

        if decode:
            name = decodeHeader(name)

        if makeUnicode:
            addr = getUnicodeValue(addr)

        ea = EmailAddress.getEmailAddress(view, addr, name)

        if ea is not None:
            setattr(kindVar, attr, ea)

    elif hType == "EmailAddressList":
        for name, addr in emailUtils.getaddresses(messageObject.get_all(key, [])):
            if decode:
                name = decodeHeader(name)

            if makeUnicode:
                addr = getUnicodeValue(addr)

            ea = EmailAddress.getEmailAddress(view, addr, name)

            if ea is not None:
                kindVar.append(ea)
Esempio n. 8
0
def __assignToKind(view, kindVar, messageObject, key, type, attr=None, decode=True):
    header = messageObject.get(key)

    if header is None:
        return

    if decode:
        header = decodeHeader(header)

    if type == "String":
        setattr(kindVar, attr, header)

    # XXX: This logic will need to be expanded
    elif type == "StringList":
        kindVar.append(header)

    elif type == "EmailAddress":
        name, addr = emailUtils.parseaddr(messageObject.get(key))

        ea = __getEmailAddress(view, decodeHeader(name), addr)

        if ea is not None:
            setattr(kindVar, attr, ea)

        elif __debug__:
            logging.error("__assignToKind: invalid email address found %s: %s" % (key, addr))

    elif type == "EmailAddressList":
        for name, addr in emailUtils.getaddresses(messageObject.get_all(key, [])):
            ea = __getEmailAddress(view, decodeHeader(name), addr)

            if ea is not None:
                kindVar.append(ea)

            elif __debug__:
                logging.error("__assignToKind: invalid email address found %s: %s" % (key, addr))

    del messageObject[key]
Esempio n. 9
0
def unicode_getalladdrs(msg, *flds):
    values = []
    for f in flds:
        values += unicode_getall(msg, f)
    return Utils.getaddresses(rmsp(v) for v in values)
Esempio n. 10
0
def unicode_getaddrs(v):
    return Utils.getaddresses([rmsp(unicode_header(v))])
Esempio n. 11
0
def unicode_getalladdrs(msg, *flds):
    values = []
    for f in flds:
        values += unicode_getall(msg, f)
    return Utils.getaddresses(rmsp(v) for v in values)
Esempio n. 12
0
def unicode_getaddrs(v):
    return Utils.getaddresses([rmsp(unicode_header(v))])