Exemplo n.º 1
0
 def collection_post(self):
     thread_id = int(self.request.matchdict.get('thread_id'))
     reply_to = self.request.json.get('reply_to')
     if reply_to:
         parent = CoreMessage.get(self.user, reply_to)
         parent_message_id = parent.external_id
         thread_id = parent.thread_id
         sec_level = parent.security_level
     else:
         parent_message_id = None
         thread_id = None
         # XXX : how to compute ?
         sec_level = 0
     recipients = self.extract_recipients()
     # XXX : make recipient for UserMessage using Recipient class
     subject = self.request.json.get('subject')
     text = self.request.json.get('text')
     tags = self.request.json.get('tags', [])
     base_msg = BaseMessage(recipients,
                            subject=subject,
                            text=text,
                            tags=tags,
                            date=datetime.utcnow(),
                            security_level=sec_level,
                            thread_id=thread_id,
                            parent_message_id=parent_message_id)
     user_msg = UserMessage(self.user, base_msg, sec_level, [], tags, [])
     msg = CoreMessage.from_user_message(user_msg)
     idx_msg = CoreMessage.by_id(self.user, msg.message_id)
     log.info('Post new message %r' % msg.message_id)
     # XXX return redirect to newly created message ?
     return idx_msg
Exemplo n.º 2
0
def process_message(body, message):
    # XXX not import here, but configuration have to be loaded
    from caliop.core.deliver import UserMessageDelivery
    deliver = UserMessageDelivery()
    log.info("Will process %r" % body)
    msg = deliver.process(body['user_id'], body['message_id'])
    log.info('Have deliver message %r' % (msg))
    message.ack()
Exemplo n.º 3
0
def process_message(body, message):
    # XXX not import here, but configuration have to be loaded
    from caliop.core.deliver import UserMessageDelivery
    deliver = UserMessageDelivery()
    log.info("Will process %r" % body)
    msg = deliver.process(body['user_id'], body['message_id'])
    log.info('Have deliver message %r' % (msg))
    message.ack()
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
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)
Exemplo n.º 6
0
 def process_message(self, peer, mailfrom, rcpttos, data):
     agent = DeliveryAgent()
     messages = agent.process(mailfrom, rcpttos, data)
     log.info('Deliver of %d messages' % len(messages))
     return None