def import_email(email, import_path, format, **kwargs): from caliop.helpers.log import log from caliop.core.user import User from caliop.core.contact import Contact, ContactLookup from caliop.core.mail import MailMessage from caliop.smtp.agent import DeliveryAgent AVATAR_DIR = '../../caliop.ng/src/assets/images/avatars' if format == 'maildir': emails = Maildir(import_path, factory=message_from_file) mode = 'maildir' else: if os.path.isdir(import_path): mode = 'mbox_directory' emails = {} files = [f for f in listdir(import_path) if os.path.isfile(os.path.join(import_path, f))] for f in files: with open('%s/%s' % (import_path, f)) as fh: emails[f] = message_from_file(fh) else: mode = 'mbox' emails = mbox(import_path) user = User.get(email) agent = DeliveryAgent() mailfrom = '' rcpts = [email] log.info("Processing mode %s" % mode) msgs = [] for key, mail in emails.iteritems(): # Create contact for user log.info('Processing mail %s' % key) msgs.append(MailMessage(mail)) msgs = sorted(msgs, key=lambda msg: msg.date) for msg in msgs: for type, addresses in msg.recipients.iteritems(): if not addresses: continue for alias, _address in addresses: lookup = ContactLookup.get(user, alias) if not lookup: log.info('Creating contact %s' % alias) infos = {'mail': alias} name, domain = alias.split('@') if os.path.isfile('%s/%s.png' % (AVATAR_DIR, name)): infos.update({'avatar': '%s.png' % name}) Contact.create(user, infos) res = agent.process(mailfrom, rcpts, msg.mail.as_string()) log.info('Process result %r' % res)
def process(self, user_id, message_id): user = User.get(user_id) msg = RawMail.get(message_id) mail = MailMessage(msg.data) contacts = self._resolve_user_contacts(user, mail) tags = self._get_tags(user, mail) sec_level = (mail.transport_security_level + \ mail.content_security_level) / 2 user_msg = UserMessage(user, mail, sec_level, contacts, tags, []) return Message.from_user_message(user_msg)
def create_user(**kwargs): from caliop.helpers.log import log from caliop.core.user import User email = kwargs['name'] password = kwargs['password'] first_name = kwargs.get('first_name') last_name = kwargs.get('last_name') user = User.create(user_id=email, password=password, first_name=first_name, last_name=last_name) user.save() log.info('User %s created' % user.user_id)
def __call__(self): credentials = self.request.json class BadCredentials(Exception): pass try: user = User.authenticate(credentials['login'], credentials['password']) self.request.session['user'] = user.user_id return user.to_api() except (KeyError, BadCredentials, Exception), exc: # XXX raise correct exception in authenticate log.error('Authentication failed for %s : %r' % (credentials['login'], exc)) return Response('BadCredentials', status='403 Forbidden')
def resolve_users(self, rpcts): users = [] for rcpt in rpcts: user = User.get(rcpt) users.append(user) return users
def check_user(self): if 'user' in self.request.session: return User.get(self.request.session['user']) return None
def __call__(self): user = User.get(self.request.session['user']) return user.to_api()