def read_forward(self): mail_forw = Email.EmailForward(self._db) for row in mail_forw.search(enable=True): # if the target is recorded as having spread_exchange_acc # the whole row is skipped because we don't want to # export forwards for such targets to LDAP t_id = int(row['target_id']) if t_id not in self.targ2spread: self.targ2forward[t_id].append(row['forward_to'])
def email_forward_info(self, operator, forward_to): """List owners of email forwards.""" self.ba.can_email_forward_info(operator.get_entity_id()) ef = Email.EmailForward(self.db) et = Email.EmailTarget(self.db) ac = Utils.Factory.get('Account')(self.db) ret = [] # Different output format for different input. def rfun(r): return (r if '%' not in forward_to else '%-12s %s' % (r, fwd['forward_to'])) for fwd in ef.search(forward_to): try: et.clear() ac.clear() et.find(fwd['target_id']) ac.find(et.email_target_entity_id) ret.append({'id': rfun(ac.account_name)}) except Errors.NotFoundError: ret.append({'id': rfun('id:%s' % et.entity_id)}) return ret
def add_forward(user_id, addr): ef = Email.EmailForward(db) ef.find_by_target_entity(user_id) # clean up input a little if addr.startswith('\\'): addr = addr[1:] addr = addr.strip() if addr.startswith('|') or addr.startswith('"|'): logger.warn("forward to pipe ignored: %s", addr) return elif not addr.count('@'): try: acc = get_account(name=addr) except Errors.NotFoundError: logger.warn("forward to unknown username: %s", addr) return addr = acc.get_primary_mailaddress() for r in ef.get_forward(): if r['forward_to'] == addr: return ef.add_forward(addr) ef.write_db()
def get_email_addresses(self, account_id, template_nr, forward): """ Get email addresses for a given account_id """ email_addrs = [] ac = Factory.get('Account')(self.db) ac.find(account_id) uname = ac.account_name owner_type = self.co.EntityType(ac.owner_type) if owner_type == self.co.entity_person: if template_nr != email_templates.email_soon: # Don't send mail to account that is expired try: email_addrs.append(ac.get_primary_mailaddress()) except Errors.NotFoundError: # This account does not have an email address. # Return empty list to indicate no email to be sent logger.info( "account_id=%r (%s) does not have an " "associated email address", account_id, uname) return [] # Get account owner's primary mail address or use current account prs = Factory.get('Person')(self.db) try: prs.find(ac.owner_id) ac_prim = prs.get_primary_account() if ac_prim: ac.clear() ac.find(ac_prim) email_addrs.append(ac.get_primary_mailaddress()) except Errors.NotFoundError: logger.warning( "Could not find primary email address for " "account_id=%r (%s), owner_id=%r", account_id, uname, ac.owner_id) else: logger.debug("Impersonal account_id=%r (%s), owner_type=%s", account_id, uname, owner_type) # Aargh! Impersonal account. Get all direct members of group # that owns account. grp = Factory.get('Group')(self.db) grp.find(ac.owner_id) members = [] for row in grp.search_members(group_id=grp.entity_id, member_type=self.co.entity_account): member_id = int(row["member_id"]) if member_id not in self.entity2name: logger.warn("No name for member id=%s in group %s %s", member_id, grp.group_name, grp.entity_id) continue members.append({ 'id': member_id, 'type': str(row["member_type"]), 'name': self.entity2name[member_id], }) # get email_addrs for members for m in sorted(members, key=lambda d: (d.get('type'), d.get('name'))): ac.clear() try: ac.find(m['id']) email_addrs.append(ac.get_primary_mailaddress()) except Errors.NotFoundError: logger.warning( "Could not find member email address for " "owner_id=%r, member_id=%r (%s)", ac.owner_id, m['id'], m['name']) if forward: ef = Email.EmailForward(self.db) ef.find_by_target_entity(account_id) for r in ef.get_forward(): logger.debug("Forwarding on: %s", repr(r)) if r['enable'] == 'T': email_addrs.append(r['forward_to']) return unique_list(email_addrs)
def read_local_delivery(self): mail_forw = Email.EmailForward(self._db) self.targ2localdelivery = set( [x['target_id'] for x in mail_forw.list_local_delivery()])
def read_forward(self): mail_forw = Email.EmailForward(self._db) for row in mail_forw.search(enable=True): self.targ2forward[int(row['target_id'])].append(row['forward_to'])