def search_header_addresses(self, fieldname, searchfunc): """ Searches all headers of the given field name for the first address under which the passed search function returns True; searchfunc must take in a tuple (x,y) parsed RFC822 header address, where x is the text associated with address y and return a boolean result; or searchfunc can be a string which is matched against each address in the field; """ if self.msg_hdrs_changed: self.unload_headers() fname = fieldname.lower() for hdr in self.headers: if fname == hdr[0].lower(): phdr = address.getaddresses([hdr[1]]) for entry in phdr: if (isinstance(searchfunc,function) \ and searchfunc(entry)) \ or (entry.address == searchfunc): yield entry return
def _replace_addresses(self, fieldname, saddx, daddx, replace_all=False): if self.msg_hdrs_changed: self.unload_headers() fname = fieldname.lower() for hdr in self.headers: if fname == hdr[0].lower(): phdr = address.getaddresses([hdr[1]]) replaced_any = False for i in range(len(phdr)): if (phdr[i] == saddx) or (saddx is None): if daddx is not None: phdr[i].set_address(str(daddx)) else: del phdr[i] replaced_any = True if not replace_all: break if replaced_any: hdr[1] = ', '.join(map(str,phdr)) self.alias_hdrs_changed = True return True
def get_header_addresses(self, fieldname): """ Returns a list of Address objects representing all address entries in the given field. """ return address.getaddresses(self.get_all(fieldname))
def apply_aliasing(self, user, mailfrom, rcpttos, msg): """ Applies an alias as the sender of a message or attempts to infer it if none was given by the sender. Handles cases 1d and 1e of the specification. """ usralias = None alias_addx = None is_alias_address = lambda entry: entry.parse_alias_address() logging.debug('Attempting to apply aliasing') # look for use of existing alias in To field (case 1d); for cur_addx in msg.search_header_addresses('to', is_alias_address): alias_pair = cur_addx.parse_alias_address() alias_data = self.db.get_alias_data(*alias_pair, \ uid=user.get_uid()) if not alias_data: continue usralias = Alias(**alias_data) alias_addx = cur_addx #if not usralias.is_active(): # continue # remove alias from rcpttos and all To fields for i in range(len(rcpttos)): if alias_addx == rcpttos[i]: del rcpttos[i] break msg.replace_address('to', alias_addx, None) break # if no alias in To field, try to infer the correct one (case 1e); if not alias_addx: logging.debug("Couldn't find alias to use in headers; " \ 'attempting to infer correct alias') alias_data = self.db.infer_alias(user.get_uid(), msg.get_header_addresses('to'), user.get_salt()) if not alias_data: logging.debug('Failed to infer alias') err = ErrorMessage('applyalias.noinfer', fromaddx = self.cfg.SVCALIAS, toaddx = user.get_account_address(), subject = msg['Subject']) self.send(err['From'], [user.get_forwarding_address()], err) return False usralias = Alias(**alias_data) #if not usralias.is_active(): # return False logging.debug('Succesfully inferred alias "%s"', str(usralias)) alias_addx = Address(usralias.get_alias_address()) # if we found an alias to use, apply it, send the # message, and record in history table; alias_addx.realname = Address(mailfrom).realname msg.replace_address('from', None, alias_addx) #del msg['message-id'] if rcpttos == []: logging.info('No recipients left; ignoring'); return rcpt_aliases = [] rcpt_nonaliases = [] for entry in rcpttos: rcpt_addx = Address(entry) if rcpt_addx.is_servername(): rcpt_aliases.append(entry) else: rcpt_nonaliases.append(entry) self.send(str(alias_addx), rcpt_nonaliases, msg) self.forward(str(alias_addx), rcpt_aliases, msg) self.db.add_history(usralias.get_rid(), True, address.getaddresses(rcpttos), msg['Message-ID'], user.get_salt()) return