Beispiel #1
0
    def _parse(self):
        contact = NewContact()
        if 'n' in self._vcard.contents:
            contact.given_name = self._vcard.n.value.given
            contact.family_name = self._vcard.n.value.family
            contact.title = '{0} {1}'.format(contact.given_name,
                                             contact.family_name)
        elif 'fn' in self._vcard.contents:
            contact.title = self._vcard.fn.value
        elif 'cn' in self._vcard.contents:
            contact.title = self._vcard.contents['cn'][0].value
        elif self._vcard.contents.get('email'):
            contact.title = self._vcard.contents['email'][0].value

        if not contact.infos:
            contact.infos = {}
        for prop in ['nickname']:
            value = self._get_not_empty(prop)
            if value and len(value):
                contact.infos[prop] = value[0]
        for prop in ['uid', 'rev']:
            value = self._get_not_empty(prop)
            if value:
                self._meta[prop] = value

        contact.phones = self.__parse_phones()
        contact.emails = self.__parse_emails()
        contact.addresses = self.__parse_addresses()
        contact.organizations = self.__parse_organizations()
        contact.ims = self.__parse_impps()
        contact.identities = self.__parse_social_identities()
        self.contact = contact
Beispiel #2
0
def create_user(**kwargs):
    """Create user in Caliopen instance."""
    from caliopen_main.user.core import User
    from caliopen_main.user.parameters import NewUser
    from caliopen_main.contact.parameters import NewContact
    from caliopen_main.contact.parameters import NewEmail

    # Fill core registry
    from caliopen_main.message.objects.message import Message

    param = NewUser()
    param.name = kwargs['email']
    if '@' in param.name:
        username, domain = param.name.split('@')
        param.name = username
        # Monkey patch configuration local_domain with provided value
        conf = Configuration('global').configuration
        conf['default_domain'] = domain
    param.password = kwargs['password']
    param.recovery_email = u'{}@caliopen.local'.format(param.name)

    contact = NewContact()
    contact.given_name = kwargs.get('given_name')
    contact.family_name = kwargs.get('family_name')
    email = NewEmail()
    email.address = param.recovery_email
    contact.emails = [email]
    param.contact = contact
    user = User.create(param)
    log.info('User %s (%s) created' % (user.user_id, user.name))
Beispiel #3
0
def create_user(**kwargs):
    """Create user in Caliopen instance."""
    from caliopen_main.user.core import User
    from caliopen_main.user.parameters import NewUser
    from caliopen_main.contact.parameters import NewContact
    from caliopen_main.contact.parameters import NewEmail

    # Fill core registry
    from caliopen_main.message.objects.message import Message

    param = NewUser()
    param.name = kwargs['email']
    if '@' in param.name:
        username, domain = param.name.split('@')
        param.name = username
        # Monkey patch configuration local_domain with provided value
        conf = Configuration('global').configuration
        conf['default_domain'] = domain
    param.password = kwargs['password']
    param.recovery_email = u'{}@recovery-caliopen.local'.format(param.name)

    contact = NewContact()
    contact.given_name = kwargs.get('given_name')
    contact.family_name = kwargs.get('family_name')
    email = NewEmail()
    email.address = param.recovery_email
    contact.emails = [email]
    param.contact = contact
    user = User.create(param)
    log.info('User %s (%s) created' % (user.user_id, user.name))
Beispiel #4
0
    def collection_post(self):
        """Create a new user."""
        contact = self.request.swagger_data['user']['contact']
        settings = Settings()
        settings.import_data(self.request.swagger_data['user']['settings'])
        settings.validate()
        param = NewUser({'name': self.request.swagger_data['user']['username'],
                         'password': self.request.swagger_data['user'][
                             'password'],
                         'recovery_email': self.request.swagger_data['user'][
                             'recovery_email'],
                         'contact': contact,
                         'settings': settings,
                         })

        if self.request.swagger_data['user']['contact'] is not None:
            param.contact = self.request.swagger_data['user']['contact']
        else:
            c = NewContact()
            c.given_name = param.name
            email = NewEmail()
            email.address = param.recovery_email
            c.emails = [email]
            param.contact = c

        try:
            user = User.create(param)
        except Exception as exc:
            raise AuthenticationError(exc.message)  # why this class of error ?

        log.info('Created user {} with name {}'.
                 format(user.user_id, user.name))
        user_url = self.request.route_path('User', user_id=user.user_id)
        self.request.response.location = user_url.encode('utf-8')
        return {'location': user_url}
    def _parse(self):
        contact = NewContact()
        if 'n' in self._vcard.contents:
            contact.given_name = self._vcard.n.value.given
            contact.family_name = self._vcard.n.value.family
            contact.title = '{0} {1}'.format(contact.given_name,
                                             contact.family_name)
        elif 'fn' in self._vcard.contents:
            contact.title = self._vcard.fn.value
        elif 'cn' in self._vcard.contents:
            contact.title = self._vcard.contents['cn'][0].value
        elif self._vcard.contents.get('email'):
            contact.title = self._vcard.contents['email'][0].value

        if not contact.infos:
            contact.infos = {}
        for prop in ['nickname']:
            value = self._get_not_empty(prop)
            if value and len(value):
                contact.infos[prop] = value[0]
        for prop in ['uid', 'rev']:
            value = self._get_not_empty(prop)
            if value:
                self._meta[prop] = value

        contact.phones = self.__parse_phones()
        contact.emails = self.__parse_emails()
        contact.addresses = self.__parse_addresses()
        contact.organizations = self.__parse_organizations()
        contact.ims = self.__parse_impps()
        contact.identities = self.__parse_social_identities()
        self.contact = contact
Beispiel #6
0
    def collection_post(self):
        """Create a new user."""
        settings = Settings()
        settings.import_data(self.request.swagger_data['user']['settings'])
        try:
            settings.validate()
        except Exception as exc:
            raise Unprocessable(detail=exc.message)

        param = NewUser({
            'name':
            self.request.swagger_data['user']['username'],
            'password':
            self.request.swagger_data['user']['password'],
            'recovery_email':
            self.request.swagger_data['user']['recovery_email'],
            'settings':
            settings,
        })

        if self.request.swagger_data['user']['contact'] is not None:
            param.contact = self.request.swagger_data['user']['contact']
        else:
            c = NewContact()
            c.given_name = param.name
            c.family_name = ""  # can't guess it !
            email = NewEmail()
            email.address = param.recovery_email
            c.emails = [email]
            param.contact = c

        try:
            user = User.create(param)
        except Exception as exc:
            log.exception('Error during user creation {0}'.format(exc))
            raise NotAcceptable(detail=exc.message)

        log.info('Created user {} with name {}'.format(user.user_id,
                                                       user.name))
        # default device management
        in_device = self.request.swagger_data['user']['device']
        if in_device:
            try:
                in_device['name'] = 'default'
                device = Device.create_from_parameter(user, in_device,
                                                      self.request.headers)
                log.info('Device %r created' % device.device_id)
            except Exception as exc:
                log.exception('Error during default device creation %r' % exc)
        else:
            log.warn('Missing default device parameter')
        user_url = self.request.route_path('User', user_id=user.user_id)
        self.request.response.location = user_url.encode('utf-8')
        return {'location': user_url}
Beispiel #7
0
    def collection_post(self):
        """Create a new user."""
        settings = Settings()
        settings.import_data(self.request.swagger_data['user']['settings'])
        try:
            settings.validate()
        except Exception as exc:
            raise Unprocessable(detail=exc.message)

        param = NewUser({
            'name':
            self.request.swagger_data['user']['username'],
            'password':
            self.request.swagger_data['user']['password'],
            'recovery_email':
            self.request.swagger_data['user']['recovery_email'],
            'settings':
            settings,
        })

        if self.request.swagger_data['user']['contact'] is not None:
            param.contact = self.request.swagger_data['user']['contact']
        else:
            c = NewContact()
            c.given_name = param.name
            c.family_name = ""  # can't guess it !
            email = NewEmail()
            email.address = param.recovery_email
            c.emails = [email]
            param.contact = c

        try:
            user = User.create(param)
        except Exception as exc:
            raise NotAcceptable(detail=exc.message)

        log.info('Created user {} with name {}'.format(user.user_id,
                                                       user.name))
        user_url = self.request.route_path('User', user_id=user.user_id)
        self.request.response.location = user_url.encode('utf-8')
        return {'location': user_url}
Beispiel #8
0
def import_email(email, import_path, format, contact_probability, **kwargs):
    """Import emails for an user."""
    from caliopen_main.user.core import User
    from caliopen_main.contact.core import Contact, ContactLookup
    from caliopen_main.message.parsers.mail import MailMessage
    from caliopen_main.contact.parameters import NewContact, NewEmail
    from caliopen_nats.delivery import UserMessageDelivery
    from caliopen_main.message.core import RawMessage
    from caliopen_storage.config import Configuration

    max_size = int(Configuration("global").get("object_store.db_size_limit"))

    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 os.listdir(import_path)
                if os.path.isfile(os.path.join(import_path, f))
            ]
            for f in files:
                try:
                    log.debug('Importing mail from file {}'.format(f))
                    with open('%s/%s' % (import_path, f)) as fh:
                        emails[f] = message_from_file(fh)
                except Exception as exc:
                    log.error('Error importing email {}'.format(exc))
        else:
            mode = 'mbox'
            emails = mbox(import_path)

    user = User.by_local_identity(email)

    log.info("Processing mode %s" % mode)

    for key, data in emails.iteritems():
        # Prevent creating message too large to fit in db.
        # (should use inject cmd for large messages)
        size = len(data.as_string())
        if size > max_size:
            log.warn("Message too large to fit into db. \
            Please, use 'inject' cmd for importing large emails.")
            continue

        raw = RawMessage.create(data.as_string())
        log.debug('Created raw message {}'.format(raw.raw_msg_id))
        message = MailMessage(data.as_string())
        dice = random()
        if dice <= contact_probability:
            for participant in message.participants:
                try:
                    ContactLookup.get(user, participant.address)
                except NotFound:
                    log.info('Creating contact %s' % participant.address)
                    name, domain = participant.address.split('@')
                    contact_param = NewContact()
                    contact_param.family_name = name
                    if participant.address:
                        e_mail = NewEmail()
                        e_mail.address = participant.address
                        contact_param.emails = [e_mail]
                    Contact.create(user, contact_param)
        log.info('No contact associated to raw {} '.format(raw.raw_msg_id))

        processor = UserMessageDelivery(user)
        obj_message = processor.process_raw(raw.raw_msg_id)
        log.info('Created message {}'.format(obj_message.message_id))
def import_email(email, import_path, format, contact_probability, **kwargs):
    """Import emails for an user."""
    from caliopen_main.user.core import User
    from caliopen_main.contact.core import Contact, ContactLookup
    from caliopen_main.message.parsers.mail import MailMessage
    from caliopen_main.contact.parameters import NewContact, NewEmail
    from caliopen_nats.delivery import UserMailDelivery
    from caliopen_main.message.core import RawMessage
    from caliopen_storage.config import Configuration

    max_size = int(Configuration("global").get("object_store.db_size_limit"))

    if 'to' in kwargs and kwargs['to']:
        dest_email = kwargs['to']
    else:
        dest_email = email

    if format == 'maildir':
        if dest_email != email:
            raise Exception('Cannot change To email using maildir format')
        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 os.listdir(import_path)
                if os.path.isfile(os.path.join(import_path, f))
            ]
            for f in files:
                try:
                    log.debug('Importing mail from file {}'.format(f))
                    with open('%s/%s' % (import_path, f)) as fh:
                        data = fh.read()
                        data = re.sub('^To: (.*)',
                                      'To: %s' % dest_email,
                                      data,
                                      flags=re.MULTILINE)
                        emails[f] = message_from_string(data)
                except Exception as exc:
                    log.error('Error importing email {}'.format(exc))
        else:
            mode = 'mbox'
            emails = mbox(import_path)

    user = User.by_local_identifier(dest_email, 'email')

    log.info("Processing mode %s" % mode)

    for key, data in emails.iteritems():
        # Prevent creating message too large to fit in db.
        # (should use inject cmd for large messages)
        size = len(data.as_string())
        if size > max_size:
            log.warn("Message too large to fit into db. \
            Please, use 'inject' cmd for importing large emails.")
            continue

        raw = RawMessage.create(data.as_string())
        log.debug('Created raw message {}'.format(raw.raw_msg_id))
        message = MailMessage(data.as_string())
        dice = random()
        if dice <= contact_probability:
            for participant in message.participants:
                try:
                    ContactLookup.get(user, participant.address)
                except NotFound:
                    log.info('Creating contact %s' % participant.address)
                    name, domain = participant.address.split('@')
                    contact_param = NewContact()
                    contact_param.family_name = name
                    if participant.address:
                        e_mail = NewEmail()
                        e_mail.address = participant.address
                        contact_param.emails = [e_mail]
                    Contact.create(user, contact_param)
        else:
            log.info('No contact associated to raw {} '.format(raw.raw_msg_id))

        processor = UserMailDelivery(
            user, user.local_identities[0])  # assume one local identity
        try:
            obj_message = processor.process_raw(raw.raw_msg_id)
        except Exception as exc:
            if isinstance(exc, DuplicateMessage):
                log.info('duplicate message {}, not imported'.format(
                    raw.raw_msg_id))
            else:
                log.exception(exc)
        else:
            log.info('Created message {}'.format(obj_message.message_id))
Beispiel #10
0
    def collection_post(self):
        """Create a new user."""
        settings = Settings()
        settings.import_data(self.request.swagger_data['user']['settings'])
        try:
            settings.validate()
        except Exception as exc:
            raise Unprocessable(detail=exc.message)

        param = NewUser({'name': self.request.swagger_data['user']['username'],
                         'password': self.request.swagger_data['user'][
                             'password'],
                         'recovery_email': self.request.swagger_data['user'][
                             'recovery_email'],
                         'settings': settings,
                         })

        if self.request.swagger_data['user']['contact'] is not None:
            param.contact = self.request.swagger_data['user']['contact']
        else:
            c = NewContact()
            c.given_name = param.name
            c.family_name = ""  # can't guess it !
            email = NewEmail()
            email.address = param.recovery_email
            c.emails = [email]
            param.contact = c

        try:
            user = User.create(param)
        except Exception as exc:
            log.exception('Error during user creation {0}'.format(exc))
            raise NotAcceptable(detail=exc.message)

        log.info('Created user {} with name {}'.
                 format(user.user_id, user.name))
        # default device management
        in_device = self.request.swagger_data['user']['device']
        if in_device:
            try:
                in_device['name'] = 'default'
                device = Device.create_from_parameter(user, in_device,
                                                      self.request.headers)
                log.info('Device %r created' % device.device_id)
            except Exception as exc:
                log.exception('Error during default device creation %r' % exc)
        else:
            log.warn('Missing default device parameter')
        user_url = self.request.route_path('User', user_id=user.user_id)
        self.request.response.location = user_url.encode('utf-8')

        # send notification to apiv2 to trigger post-registration actions
        config = Configuration('global').get("message_queue")
        try:
            tornado.ioloop.IOLoop.current().run_sync(
                lambda: notify_new_user(user, config), timeout=5)
        except Exception as exc:
            log.exception(
                'Error when sending new_user notification on NATS : {0}'.
                    format(exc))

        return {'location': user_url}
Beispiel #11
0
def import_email(email, import_path, format, contact_probability,
                 **kwargs):
    """Import emails for an user."""
    from caliopen_main.user.core import User, UserIdentity
    from caliopen_main.contact.core import Contact, ContactLookup
    from caliopen_main.message.parsers.mail import MailMessage
    from caliopen_main.contact.parameters import NewContact, NewEmail
    from caliopen_nats.delivery import UserMailDelivery
    from caliopen_main.message.core import RawMessage
    from caliopen_storage.config import Configuration

    max_size = int(Configuration("global").get("object_store.db_size_limit"))

    if 'to' in kwargs and kwargs['to']:
        dest_email = kwargs['to']
    else:
        dest_email = email

    if format == 'maildir':
        if dest_email != email:
            raise Exception('Cannot change To email using maildir format')
        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 os.listdir(import_path) if
                     os.path.isfile(os.path.join(import_path, f))]
            for f in files:
                try:
                    log.debug('Importing mail from file {}'.format(f))
                    with open('%s/%s' % (import_path, f)) as fh:
                        data = fh.read()
                        data = re.sub('^To: (.*)', 'To: %s' % dest_email,
                                      data, flags=re.MULTILINE)
                        emails[f] = message_from_string(data)
                except Exception as exc:
                    log.error('Error importing email {}'.format(exc))
        else:
            mode = 'mbox'
            emails = mbox(import_path)

    user = User.by_local_identifier(dest_email, 'email')

    log.info("Processing mode %s" % mode)

    for key, data in emails.iteritems():
        # Prevent creating message too large to fit in db.
        # (should use inject cmd for large messages)
        size = len(data.as_string())
        if size > max_size:
            log.warn("Message too large to fit into db. \
            Please, use 'inject' cmd for importing large emails.")
            continue

        raw = RawMessage.create(data.as_string())
        log.debug('Created raw message {}'.format(raw.raw_msg_id))
        message = MailMessage(data.as_string())
        dice = random()
        if dice <= contact_probability:
            for participant in message.participants:
                try:
                    ContactLookup.get(user, participant.address)
                except NotFound:
                    log.info('Creating contact %s' % participant.address)
                    name, domain = participant.address.split('@')
                    contact_param = NewContact()
                    contact_param.family_name = name
                    if participant.address:
                        e_mail = NewEmail()
                        e_mail.address = participant.address
                        contact_param.emails = [e_mail]
                    Contact.create(user, contact_param)
        log.info('No contact associated to raw {} '.format(raw.raw_msg_id))

        processor = UserMailDelivery(user,
                        user.local_identities[0])  # assume one local identity
        try:
            obj_message = processor.process_raw(raw.raw_msg_id)
        except Exception as exc:
            log.exception(exc)
        else:
            log.info('Created message {}'.format(obj_message.message_id))