Ejemplo n.º 1
0
def test_metrics_parse():
    # parse
    assert_equal(len(address.parse('*****@*****.**', metrics=True)), 2)
    p, m = address.parse('*****@*****.**', metrics=True)
    assert_equal('parsing' in m, True)
    assert_equal(isinstance(address.parse('*****@*****.**', metrics=False), address.EmailAddress), True)
    assert_equal(isinstance(address.parse('*****@*****.**'), address.EmailAddress), True)
Ejemplo n.º 2
0
def test_metrics_parse():
    # parse
    assert_equal(len(address.parse('*****@*****.**', metrics=True)), 2)
    p, m = address.parse('*****@*****.**', metrics=True)
    assert_equal('parsing' in m, True)
    assert_equal(isinstance(address.parse('*****@*****.**', metrics=False), address.EmailAddress), True)
    assert_equal(isinstance(address.parse('*****@*****.**'), address.EmailAddress), True)
Ejemplo n.º 3
0
    def _parse_headers(self, headers):
        '''
        _parse_headers - Parses flanker MIME headers for valid EmailReport headers

        :param headers: The headers in (key, val) format
        '''
        # Parse out known headers
        for h in headers:
            header = Header(name=h[0], value=str(h[1]))
            if header.name == 'Subject':
                self.subject = header.value
            if header.name == 'To':
                for addr in header.value.split(','):
                    try:
                        parsed_addr = address.parse(addr)
                        self.to.append(
                            EmailAddress(
                                name=parsed_addr.display_name,
                                address=parsed_addr.address))
                    except Exception as e:
                        print 'Error parsing To field: {}'.format(e)
            if header.name == 'From':
                try:
                    parsed_addr = address.parse(header.value)
                    self.sender = EmailAddress(
                        name=parsed_addr.display_name,
                        address=parsed_addr.address)
                except Exception as e:
                    print 'Error parsing From field: {}'.format(e)
            self.headers.append(header)
Ejemplo n.º 4
0
def _validate_email(email):
    """
    Validate well-formness of email

    E.g. that subject and body are non-empty, and to and from email addresses are valid.

    :return: valid, reason where valid is a boolean and reason is a string
    """

    # TODO This should be more defensive. What if subject and body are not strings?
    # We could also return better error messages.

    # 1. subject and body are non-empty
    if email.get('subject', '').strip() == '':
        return False, 'Subject is empty'
    if email.get('body', '').strip() == '':
        return False, 'Body is empty'

    # 2. to_email and from_email are valid e-mails
    # We use mailgun's flanker (https://github.com/mailgun/flanker) for the heavy lifting
    email['to_email'] = address.parse(email.get('to_email', ''), addr_spec_only=True)
    email['from_email'] = address.parse(email.get('from_email', ''), addr_spec_only=True)  # TODO cache this check?

    if email['to_email'] is None:
        return False, 'To-Email is not valid'

    if email['from_email'] is None:
        return False, 'From-Email is not valid'

    return True, ''
Ejemplo n.º 5
0
def test_append_address_only():
    # Only Address types can be added to an AddressList.

    al = AddressList()
    al.append(parse(u'Федот <стрелец@почта.рф>'))
    al.append(parse('https://mailgun.net/webhooks'))

    with assert_raises(TypeError):
        al.append('*****@*****.**')
Ejemplo n.º 6
0
def test_append_address_only():
    # Only Address types can be added to an AddressList.

    al = AddressList()
    al.append(parse(u'Федот <стрелец@почта.рф>'))
    al.append(parse('https://mailgun.net/webhooks'))

    with assert_raises(TypeError):
        al.append('*****@*****.**')
Ejemplo n.º 7
0
def run_full_mailbox_test(string, expected, full_spec=None):
    mbox = address.parse(string)
    if mbox:
        assert_equal(expected.display_name, mbox.display_name)
        assert_equal(expected.address, mbox.address)
        if full_spec:
            assert_equal(full_spec, mbox.full_spec())
        assert_equal(mbox, address.parse(mbox.to_unicode()))  # check symmetry
        return
    assert_equal(expected, mbox)
Ejemplo n.º 8
0
def run_full_mailbox_test(string, expected, full_spec=None):
    mbox = address.parse(string, strict=True)
    if mbox:
        assert_equal(expected.display_name, mbox.display_name)
        assert_equal(expected.address, mbox.address)
        if full_spec:
            assert_equal(full_spec, mbox.full_spec())
        assert_equal(mbox, address.parse(mbox.to_unicode(), strict=True)) # check symmetry
        return
    assert_equal(expected, mbox)
Ejemplo n.º 9
0
def test_mx_aol_manage_flag_toggle():
    # Just checking if the domain provided from the sub is managed
    mailbox = '*****@*****.**'
    addr_obj = address.parse(mailbox)
    unmanaged = validate.aol.unmanaged_email(addr_obj.hostname)
    assert_equal(unmanaged, False)

    # Same but inversed, unmanaged aol mailbox
    mailbox = '*****@*****.**'
    addr_obj = address.parse(mailbox)
    unmanaged = validate.aol.unmanaged_email(addr_obj.hostname)
    assert_equal(unmanaged, True)
Ejemplo n.º 10
0
def test_mx_yahoo_manage_flag_toggle():
    # Just checking if the domain provided from the sub is managed
    mailbox = '*****@*****.**'
    addr_obj = address.parse(mailbox)
    managed = validate.yahoo.managed_email(addr_obj.hostname)
    assert_equal(managed, True)

    # Same but inversed, unmanaged yahoo mailbox
    mailbox = '*****@*****.**'
    addr_obj = address.parse(mailbox)
    managed = validate.yahoo.managed_email(addr_obj.hostname)
    assert_equal(managed, False)
Ejemplo n.º 11
0
def test_mx_yahoo_manage_flag_toggle():
    # Just checking if the domain provided from the sub is managed
    mailbox = '*****@*****.**'
    addr_obj = address.parse(mailbox)
    managed = validate.yahoo.managed_email(addr_obj.hostname)
    assert_equal(managed, True)

    # Same but inversed, unmanaged yahoo mailbox
    mailbox = '*****@*****.**'
    addr_obj = address.parse(mailbox)
    managed = validate.yahoo.managed_email(addr_obj.hostname)
    assert_equal(managed, False)
Ejemplo n.º 12
0
def test_mx_aol_manage_flag_toggle():
    # Just checking if the domain provided from the sub is managed
    mailbox = '*****@*****.**'
    addr_obj = address.parse(mailbox)
    unmanaged = validate.aol.unmanaged_email(addr_obj.hostname)
    assert_equal(unmanaged, False)

    # Same but inversed, unmanaged aol mailbox
    mailbox = '*****@*****.**'
    addr_obj = address.parse(mailbox)
    unmanaged = validate.aol.unmanaged_email(addr_obj.hostname)
    assert_equal(unmanaged, True)
Ejemplo n.º 13
0
def test_addr_properties():
    email = parse('*****@*****.**')
    url = parse('http://host.com')
    non_ascii = parse(u'Gonzalo Bañuelos<*****@*****.**>')

    eq_(False, url.supports_routing)
    eq_(True,  email.supports_routing)

    eq_(Address.Type.Email, email.addr_type)
    eq_(Address.Type.Url, url.addr_type)
    eq_(non_ascii, "*****@*****.**")

    adr = parse("Zeka <*****@*****.**>")
    eq_(str(adr), '*****@*****.**')
Ejemplo n.º 14
0
def test_addr_properties():
    email = parse('*****@*****.**')
    url = parse('http://host.com')
    non_ascii = parse(u'Gonzalo Bañuelos<*****@*****.**>')

    eq_(False, url.supports_routing)
    eq_(True, email.supports_routing)

    eq_(Address.Type.Email, email.addr_type)
    eq_(Address.Type.Url, url.addr_type)
    eq_(non_ascii, "*****@*****.**")

    adr = parse("Zeka <*****@*****.**>")
    eq_(str(adr), '*****@*****.**')
Ejemplo n.º 15
0
def context_setup():
    """
    Sets up context for the request
    """
    g.user = users.get_current_user()
    g.domain = address.parse(g.user.email()).hostname
    g.base_report_query = EmailReport.domain_query(g.domain)
Ejemplo n.º 16
0
    def _processing(self):
        recvd_header = []
        try:
            for x in self.message.headers.getall('Received'):
                recvd_header.append(x)
            ip = None
        except Exception as e:
            raise ExamineHeaderError(e)
        for h in reversed(recvd_header):
            ip = self.extract_ip(h)
            if ip is None:
                continue
            if not ip.startswith(("127.", "192.168.", "10.")) \
                    and not re.search('(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)', ip):
                self.origin = h
                self.origin_ip = ip
                break

        if self.origin_ip is not None:
            logging.info("%s: Found IP address (%s), passing to module RBL lookup" % (self.name, ip))
            self.rbl_lookup()

        self.mailfrom = self.message.headers.get('From')
        if email is not None:
            parsed = address.parse(self.mailfrom)
            if parsed is not None:
                f.decode(parsed.hostname)
                self.origin_domain = f.get_domain()

        self.mailto = self.message.headers.get('To')
Ejemplo n.º 17
0
def various_encodings_test():
    v = '"=?utf-8?b?6ICD5Y+W5YiG5Lqr?=" <*****@*****.**>'
    eq_(u'"考取分享" <*****@*****.**>', encodedword.mime_to_unicode(v))

    v = """=?UTF-8?B?0JbQtdC60LA=?= <*****@*****.**>, =?UTF-8?B?0JrQvtC90YbQtdCy0L7QuQ==?= <*****@*****.**>"""
    eq_(u"Жека <*****@*****.**>, Концевой <*****@*****.**>", encodedword.mime_to_unicode(v))

    v = encodedword.mime_to_unicode("=?utf-8?b?0JrQvtC90YbQtdCy0L7QuQ==?= <*****@*****.**>, Bob <*****@*****.**>, =?utf-8?b?0JLQuNC90YE=?= <*****@*****.**>")
    eq_(u"Концевой <*****@*****.**>, Bob <*****@*****.**>, Винс <*****@*****.**>", v)

    v = '=?UTF-8?B?0J/RgNC+0LLQtdGA0Y/QtdC8INGA0YPRgdGB0LrQuNC1INGB0LDQsdC2?=\n =?UTF-8?B?0LXQutGC0Ysg0Lgg0Y7QvdC40LrQvtC0IOKYoA==?='
    eq_(u'Проверяем русские сабжекты и юникод ☠', encodedword.mime_to_unicode(v))

    v = '=?UTF-8?B?0J/RgNC+0LLQtdGA0Y/QtdC8INGA0YPRgdGB0LrQuNC1INGB0LDQsdC2?=\r\n =?UTF-8?B?0LXQutGC0Ysg0Lgg0Y7QvdC40LrQvtC0IOKYoA==?='
    eq_(u'Проверяем русские сабжекты и юникод ☠', encodedword.mime_to_unicode(v))

    v = '=?utf-8?Q?Evaneos-Concepci=C3=B3n.pdf?='
    eq_(u'Evaneos-Concepción.pdf', encodedword.mime_to_unicode(v))

    v = u'=?gb2312?Q?Hey_There=D7=B2=D8=B0?='
    eq_(u'Hey There撞匕', encodedword.mime_to_unicode(v))

    v = u'=?gb18030?Q?Hey_There=D7=B2=D8=B0?='
    eq_(u'Hey There撞匕', encodedword.mime_to_unicode(v))

    v = parse(u'Тест длинного дисплей нейма <*****@*****.**>')
    eq_(v.display_name, encodedword.mime_to_unicode(v.ace_display_name))
Ejemplo n.º 18
0
 def post(self):
     """
     Creates a new user.
     Decides if teacher based on email host name.
     """
     arguments = user_parser.parse_args()
     guc_id = arguments['guc_id']
     password = arguments['password']
     email = arguments['email']
     name = arguments['name']
     parsed_email = address.parse(email)
     if parsed_email is None or not parsed_email.hostname.endswith(
             'guc.edu.eg'):
         abort(400)
     if User.objects(email__iexact=email).count() != 0:
         abort(422)  # Duplicate found
     if parsed_email.hostname.startswith('student'):
         user = Student(guc_id=guc_id, email=email, name=name)
     else:
         user = User(email=email, name=name)
     user.password = password
     user.active = False
     user.save()
     if app.config['ENABLE_EMAIL_ACTIVATION']:
         user.send_activation_mail()
     return marshal(user.to_dict(), user_fields), 201
def create_user(email, password, first_name, last_name, organization):
    """ Create a User.

    Note: The db.session is not committed. Be sure to commit the session.

    """
    if len(password) < MIN_PASSWORD_LENGTH:
        raise ValueError(
            f"Passwords must have length of at least {MIN_PASSWORD_LENGTH}")
    if address.parse(email) is None:
        raise ValueError("Invalid email")
    if first_name is None or len(first_name) == 0:
        raise ValueError("Invalid first_name")
    if last_name is None or len(last_name) == 0:
        raise ValueError("Invalid last_name")

    p_hash, p_salt = utils.make_hash(password)
    user = User(
        uuid=uuid.uuid4().hex,
        email=email,
        password_hash=p_hash,
        password_salt=p_salt,
        first_name=first_name,
        last_name=last_name,
        organization=organization,
    )
    db.session.add(user)
    return user
Ejemplo n.º 20
0
    def parse_headers(self, header, meta):
        meta.title = header.get('Subject')

        if header.get('Message-Id'):
            meta.foreign_id = string_value(header.get('Message-Id'))

        if header.get('From'):
            addr = address.parse(header.get('From'))
            if addr is not None:
                meta.author = addr.to_unicode()
                meta.add_email(addr.address)

        for hdr in ['To', 'CC', 'BCC']:
            if header.get(hdr):
                for addr in address.parse_list(header.get(hdr)):
                    meta.add_email(addr.address)

        date = header.get('Date')
        date = rfc822.parsedate(date)
        if date is not None:
            dt = datetime.fromtimestamp(mktime(date))
            meta.add_date(dt)

        meta.headers = dict([(k, string_value(v)) for k, v in header.items()])
        return meta
Ejemplo n.º 21
0
def extract_data(json_data):
    unique_emails = OrderedDict()
    april_logins = []
    domain_counts = {}

    for login in json_data['data']:
        login_date_str = login.get('login_date')
        login_datetime = None
        if login_date_str:
            try:
                login_datetime = dateutil.parser.parse(login_date_str)
            except ValueError:
                pass
        email = login.get('email')
        if email:
            stripped_email = email.strip()
            if stripped_email:
                unique_emails[stripped_email] = True
        if in_april(login_datetime):
            april_logins.append(login)
        parsed_email = address.parse(email)
        if parsed_email is not None:
            domain = parsed_email.hostname
            domain_counts[domain] = domain_counts.get(domain, 0) + 1
    return (unique_emails, april_logins, domain_counts)
Ejemplo n.º 22
0
 def add_email(self, email):
     parsed = address.parse(email)
     if parsed is None:
         return
     self.add_domain(parsed.hostname)
     if parsed.address not in self._emails:
         self._emails.append(parsed.address)
Ejemplo n.º 23
0
    def parse_headers(self, header, meta):
        meta.title = header.get("Subject")

        if header.get("Message-Id"):
            meta.foreign_id = unicode(header.get("Message-Id"))

        if header.get("From"):
            addr = address.parse(header.get("From"))
            if addr is not None:
                meta.author = addr.to_unicode()
                meta.add_email(addr.address)

        for hdr in ["To", "CC", "BCC"]:
            if header.get(hdr):
                for addr in address.parse_list(header.get(hdr)):
                    meta.add_email(addr.address)

        date = header.get("Date")
        date = rfc822.parsedate(date)
        if date is not None:
            dt = datetime.fromtimestamp(mktime(date))
            meta.add_date(dt)

        meta.headers = dict([(k, unicode(v)) for k, v in header.items()])
        return meta
Ejemplo n.º 24
0
def cloudsearch_contact_repr(contact):
    # strip display name out of email address
    parsed = address.parse(contact.email_address)
    name = contact.name or ''
    email_address = parsed.address if parsed else ''
    name_contains_bad_codepoints = re.search(control_chars_re, contact.name
                                             or '')
    email_contains_bad_codepoints = re.search(control_chars_re, email_address)
    if name_contains_bad_codepoints or email_contains_bad_codepoints:
        log.warning("bad codepoint in contact",
                    contact_id=contact.id,
                    name=contact.name,
                    email_address=email_address)
        name = control_chars_re.sub('', name)
        email_address = control_chars_re.sub('', email_address)
    return {
        'id':
        contact.id,
        'namespace_id':
        contact.namespace_id,
        'name':
        name,
        'email_address':
        email_address,
        'phone_numbers':
        [_strip_non_numeric(p.number) for p in contact.phone_numbers]
    }
Ejemplo n.º 25
0
    def parse_headers(self, msg, meta):
        meta.title = msg.subject

        if msg.headers.get('Message-Id'):
            meta.foreign_id = unicode(msg.headers.get('Message-Id'))

        if msg.headers.get('From'):
            addr = address.parse(msg.headers.get('From'))
            if addr is not None:
                meta.author = addr.to_unicode()

        for hdr in ['To', 'CC', 'BCC']:
            if msg.headers.get(hdr):
                for addr in address.parse_list(msg.headers.get(hdr)):
                    meta.add_recipient(addr.to_unicode())

        date = msg.headers.get('Date')
        date = rfc822.parsedate(date)
        if date is not None:
            dt = datetime.fromtimestamp(mktime(date))
            meta.add_date(dt)

        meta.headers = dict([(k, unicode(v)) for k, v in
                             msg.headers.items()])
        return meta
Ejemplo n.º 26
0
    def process(self, filename):
        self.filename = filename
        with open(filename, "rb") as f:
            msg = mime.from_string(f.read())
        self.logging.info('Start working on {0}'.format(filename))

        # count how many time connect to neo4j
        self.count = 0
        # initialize message basic information
        self.message_id = str(msg.message_id)

        sender = address.parse(msg.headers.get('From'))
        self.sender = {'address': sender.address, 'name': sender.display_name}
        self.receivers = {'to': [], 'cc': [], 'bcc': []}
        for value in address.parse_list(msg.headers.get('To')):
            tmp = {'address': value.address, 'name': value.display_name}
            self.receivers['to'].append(tmp)

        for value in address.parse_list(msg.headers.get('Cc')):
            tmp = {'address': value.address, 'name': value.display_name}
            self.receivers['cc'].append(tmp)

        self.tx = self.graph.begin()

        try:
            for func in self.processors:
                self.logging.info('\t Start layer: {0}'.format(func))
                getattr(self, func)(msg)
                self.logging.info('\t End layer: {0}'.format(func))
        except:
            pass
        finally:
            # commit remainder
            self.tx.commit()
Ejemplo n.º 27
0
 def add_email(self, email):
     parsed = address.parse(email)
     if parsed is None:
         return
     self.add_domain(parsed.hostname)
     if parsed.address not in self._emails:
         self._emails.append(parsed.address)
Ejemplo n.º 28
0
def get_domain(email):
    try:
        clean = address.parse(email, addr_spec_only=True)
        if clean:
            return clean.hostname.lower()
    except:
        pass
Ejemplo n.º 29
0
 def send_mail(self,
               sender_display_name,
               sender_address,
               to_address,
               subject='',
               text='',
               html='',
               original_to_address=None,
               original_cc_address=None,
               attachments=None,
               inlines=None,
               message_id=None):
     logger.debug(
         'in send_mail: sender_address=%s, to_address=%s, '
         'mailing_list.address=%s ', sender_address, to_address,
         self.address)
     mailing_list_address = addresslib_address.parse('{} {}'.format(
         sender_display_name, self.address))
     listserv_client.send_mail(mailing_list_address.full_spec(),
                               sender_address, to_address, subject, text,
                               html, original_to_address,
                               original_cc_address, attachments, inlines,
                               message_id)
     cache_key = settings.CACHE_KEY_MESSAGE_HANDLED_BY_MESSAGE_ID_AND_RECIPIENT % (
         message_id, to_address)
     cache.set(cache_key,
               True,
               timeout=settings.CACHE_KEY_MESSAGE_HANDLED_TIMEOUT)
Ejemplo n.º 30
0
    def _processing(self):
        recvd_header = []
        try:
            for x in self.message.headers.getall('Received'):
                recvd_header.append(x)
            ip = None
        except Exception as e:
            raise ExamineHeaderError(e)
        for h in reversed(recvd_header):
            ip = self.extract_ip(h)
            if ip is None:
                continue
            if not ip.startswith(("127.", "192.168.", "10.")) \
                    and not re.search('(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)', ip):
                self.origin = h
                self.origin_ip = ip
                break

        if self.origin_ip is not None:
            logging.info("%s: Found IP address (%s), passing to module RBL lookup" % (self.name, ip))
            self.rbl_lookup()

        self.mailfrom = self.message.headers.get('From')
        if email is not None:
            parsed = address.parse(self.mailfrom)
            if parsed is not None:
                f.decode(parsed.hostname)
                self.origin_domain = f.get_domain()

        self.mailto = self.message.headers.get('To')
Ejemplo n.º 31
0
def get_domain(email):
    try:
        clean = address.parse(email, addr_spec_only=True)
        if clean:
            return clean.hostname.lower()
    except:
        pass
Ejemplo n.º 32
0
def identify(sender=None, client_address=None, rcpt=[]):
    """
    Alimente group_name, domain_name, is_in (1 ou 0)
    
    TODO: passer la liste des domaines et mynetwork à la méthode
    TODO: intégrer dans completed()
    
    """
    data = {}

    net = Mynetwork.objects(ip_address=client_address).first()

    if net:
        data['group_name'] = net.group_name
        data['is_in'] = 0

        if sender:

            sender = address.parse(sender, addr_spec_only=True)

            if sender:
                domain_sender = sender.hostname  #utils.parse_domain(sender)
                if domain_sender:
                    d = Domain.objects(name__iexact=domain_sender).first()
                    if d:
                        data['domain_name'] = d.name
    else:
        data['is_in'] = 1
        recipients_domain = []
        for r in rcpt:
            recipient = address.parse(r, addr_spec_only=True)
            if recipient:
                recipients_domain.append(recipient.hostname)

        domain = Domain.objects(name__in=recipients_domain).first()
        if domain:
            data['domain_name'] = domain.name
            data['group_name'] = domain.group_name

        #country for is_in only
        if client_address:
            country = get_country(client_address)
            if country and len(country.strip()) > 0:
                data['country'] = country

    return data
Ejemplo n.º 33
0
 def add_email(self, email):
     emails = self.emails
     parsed = address.parse(email)
     if parsed is None:
         return
     emails.append(parsed.address)
     self.add_domain(parsed.hostname)
     self.data['emails'] = list(emails)
Ejemplo n.º 34
0
	def get_emails(self, text):
		''' extracts emails from webpage '''
		email = re.findall(r"[\w\.-]+@[\w\.-]+[.]+[a-zA-Z]{2,5}", text)
		email = [e for e in email if e not in ['', None]]
		email = [e for e in email if address.parse(e) != None]
		email = [e for e in email if e not in ['', None]]
		email = [e.lower() for e in list(set(email))]
		return list(set(email+self.info['emails']))
Ejemplo n.º 35
0
 def clean(self, value, prop, record):
     value = super(EmailProperty, self).clean(value, prop, record)
     if value is None:
         return
     parsed = address.parse(value)
     if parsed is not None:
         return parsed.address
     return value
Ejemplo n.º 36
0
def canonicalize_address(addr):
    """Gmail addresses with and without periods are the same, so we want to
    treat them as belonging to the same contact."""
    parsed_address = address.parse(addr, addr_spec_only=True)
    local_part = parsed_address.mailbox
    if parsed_address.hostname in ('gmail.com', 'googlemail.com'):
        local_part = local_part.replace('.', '')
    return '@'.join((local_part, parsed_address.hostname))
Ejemplo n.º 37
0
def canonicalize_address(addr):
    """Gmail addresses with and without periods are the same, so we want to
    treat them as belonging to the same contact."""
    parsed_address = address.parse(addr, addr_spec_only=True)
    local_part = parsed_address.mailbox
    if parsed_address.hostname in ('gmail.com', 'googlemail.com'):
        local_part = local_part.replace('.', '')
    return '@'.join((local_part, parsed_address.hostname))
Ejemplo n.º 38
0
def validate_email(txt):
    if isinstance(txt, address.EmailAddress):
        return txt
    email = address.parse(txt)
    if address.is_email(email.address):
        return email
    else:
        return None
Ejemplo n.º 39
0
def identify(sender=None, client_address=None, rcpt=[]):
    """
    Alimente group_name, domain_name, is_in (1 ou 0)
    
    TODO: passer la liste des domaines et mynetwork à la méthode
    TODO: intégrer dans completed()
    
    """
    data = {}
    
    net = Mynetwork.objects(ip_address=client_address).first()
    
    if net:
        data['group_name'] = net.group_name
        data['is_in'] = 0
        
        if sender: 
            
            sender = address.parse(sender, addr_spec_only=True)
    
            if sender:
                domain_sender = sender.hostname #utils.parse_domain(sender)
                if domain_sender:
                    d = Domain.objects(name__iexact=domain_sender).first()
                    if d:
                        data['domain_name'] = d.name
    else:
        data['is_in'] = 1
        recipients_domain = []
        for r in rcpt:
            recipient = address.parse(r, addr_spec_only=True)
            if recipient:
                recipients_domain.append(recipient.hostname)
        
        domain = Domain.objects(name__in=recipients_domain).first()
        if domain:
            data['domain_name'] = domain.name
            data['group_name'] = domain.group_name

        #country for is_in only        
        if client_address:
            country = get_country(client_address)
            if country and len(country.strip()) > 0:
                data['country'] = country
        
    return data
def handle_mailing_list_email_route(request):
    '''
    Handles the Mailgun route action when email is sent to a Mailgun mailing list.
    :param request:
    :return JsonResponse:
    '''
    logger.debug('Full mailgun post: %s', request.POST)

    from_ = addresslib_address.parse(request.POST.get('from'))
    message_id = request.POST.get('Message-Id')
    recipients = set(
        addresslib_address.parse_list(request.POST.get('recipient')))
    sender = addresslib_address.parse(
        _remove_batv_prefix(request.POST.get('sender')))
    subject = request.POST.get('subject')
    user_alt_email_cache = CommChannelCache()

    logger.info(
        'Handling Mailgun mailing list email from %s (sender %s) to '
        '%s, subject %s, message id %s', from_, sender, recipients, subject,
        message_id)

    # short circuit if we detect a bounce loop
    sender_address = sender.address.lower() if sender else ''
    from_address = from_.address.lower() if from_ else ''
    if settings.NO_REPLY_ADDRESS.lower() in (sender_address, from_address):
        logger.error('Caught a bounce loop, dropping it. POST data:\n%s\n',
                     json.dumps(request.POST))
        return JsonResponse({'success': True})

    for recipient in recipients:
        # shortcut if we've already handled this message for this recipient
        if message_id:
            cache_key = (
                settings.CACHE_KEY_MESSAGE_HANDLED_BY_MESSAGE_ID_AND_RECIPIENT
                % (message_id, recipient))
            if cache.get(cache_key):
                logger.warning(
                    'Message-Id %s was posted to the route handler '
                    "for %s, but we've already handled that.  "
                    'Skipping.', recipient, message_id)
                continue
        _handle_recipient(request, recipient, user_alt_email_cache)

    return JsonResponse({'success': True})
Ejemplo n.º 41
0
def run_full_mailbox_test(string, expected, full_spec=None):
    mbox = address.parse(string)
    if mbox:
        assert_equal(mbox.display_name, expected.display_name)
        assert_equal(mbox.address, expected.address)
        if full_spec:
            assert_equal(mbox.full_spec(), full_spec)
        return
    assert_equal(mbox, expected)
Ejemplo n.º 42
0
def canonicalize_address(addr):
    """Gmail addresses with and without periods are the same."""
    parsed_address = address.parse(addr, addr_spec_only=True)
    if not isinstance(parsed_address, address.EmailAddress):
        return addr
    local_part = parsed_address.mailbox
    if parsed_address.hostname in ('gmail.com', 'googlemail.com'):
        local_part = local_part.replace('.', '')
    return '@'.join((local_part, parsed_address.hostname))
Ejemplo n.º 43
0
 def clean_email(self):
     value = self.cleaned_data['email']
     email = address.parse(value)
     if not email:
         raise forms.ValidationError("Not a valid email address")
     email = unicode(email)
     for w in models.Wishlist.objects.filter(email__iexact=email):
         return w.email
     raise forms.ValidationError("No Wish List set up with that address")
Ejemplo n.º 44
0
 def canonicalize_address(addr):
     """Gmail addresses with and without periods are the same."""
     parsed_address = address.parse(addr, addr_spec_only=True)
     if not isinstance(parsed_address, address.EmailAddress):
         return addr
     local_part = parsed_address.mailbox
     if parsed_address.hostname in ('gmail.com', 'googlemail.com'):
         local_part = local_part.replace('.', '')
     return '@'.join((local_part, parsed_address.hostname))
Ejemplo n.º 45
0
def run_full_mailbox_test(string, expected, full_spec=None):
    mbox = address.parse(string)
    if mbox:
        assert_equal(expected.display_name, mbox.display_name)
        assert_equal(expected.address, mbox.address)
        if full_spec:
            assert_equal(full_spec, mbox.full_spec())
        return
    assert_equal(expected, mbox)
Ejemplo n.º 46
0
def test_parse_relaxed():
    eq_(u'foo <*****@*****.**>',             parse('foo <*****@*****.**>').to_unicode())
    eq_(u'foo <*****@*****.**>',             parse('foo [email protected]').to_unicode())
    eq_(u'foo <*****@*****.**>',             parse('foo (comment) <*****@*****.**>').to_unicode())
    eq_(u'"foo (comment)" <*****@*****.**>', parse('foo (comment) [email protected]').to_unicode())
    eq_(u'"not@valid" <*****@*****.**>',     parse('not@valid <*****@*****.**>').to_unicode())
    eq_(u'"not@valid" <*****@*****.**>',     parse('not@valid [email protected]').to_unicode())
    eq_(u'Маруся <мария@example.com>',    parse('Маруся мария@example.com').to_unicode())
Ejemplo n.º 47
0
def create_contact_route(alias_id):
    """
    Create contact for an alias
    Input:
        alias_id: in url
        contact: in body
    Output:
        201 if success
        409 if contact already added


    """
    data = request.get_json()
    if not data:
        return jsonify(error="request body cannot be empty"), 400

    user = g.user
    alias: Alias = Alias.get(alias_id)

    if alias.user_id != user.id:
        return jsonify(error="Forbidden"), 403

    contact_addr = data.get("contact")

    if not contact_addr:
        return jsonify(error="Contact cannot be empty"), 400

    full_address: EmailAddress = address.parse(contact_addr)
    if not full_address:
        return jsonify(error=f"invalid contact email {contact_addr}"), 400

    contact_name, contact_email = full_address.display_name, full_address.address

    contact_email = sanitize_email(contact_email, not_lower=True)

    # already been added
    contact = Contact.get_by(alias_id=alias.id, website_email=contact_email)
    if contact:
        return jsonify(**serialize_contact(contact, existed=True)), 200

    try:
        contact = Contact.create(
            user_id=alias.user_id,
            alias_id=alias.id,
            website_email=contact_email,
            name=contact_name,
            reply_email=generate_reply_email(contact_email, user),
        )
    except CannotCreateContactForReverseAlias:
        return jsonify(
            error="You can't create contact for a reverse alias"), 400

    LOG.d("create reverse-alias for %s %s", contact_addr, alias)
    Session.commit()

    return jsonify(**serialize_contact(contact)), 201
Ejemplo n.º 48
0
def validate_draft_recipients(draft):
    """Check that all recipient emails are at least plausible email
    addresses, before we try to send a draft."""
    for field in draft.to_addr, draft.bcc_addr, draft.cc_addr:
        if field is not None:
            for _, email_address in field:
                if not isinstance(address.parse(email_address),
                                  address.EmailAddress):
                    raise InputError('Invalid recipient address {}'.
                                     format(email_address))
Ejemplo n.º 49
0
def canonicalize_address(addr):
    """Gmail addresses with and without periods are the same."""
    parsed_address = address.parse(addr, addr_spec_only=True)
    if not isinstance(parsed_address, address.EmailAddress):
        return addr
    local_part = parsed_address.mailbox.lower()
    hostname = parsed_address.hostname.lower()
    if hostname in ("gmail.com", "googlemail.com"):
        local_part = local_part.replace(".", "")
    return "@".join((local_part, hostname))
Ejemplo n.º 50
0
def canonicalize_address(addr):
    """Gmail addresses with and without periods are the same."""
    parsed_address = address.parse(addr, addr_spec_only=True)
    if not isinstance(parsed_address, address.EmailAddress):
        return addr
    local_part = parsed_address.mailbox.lower()
    hostname = parsed_address.hostname.lower()
    if hostname in ("gmail.com", "googlemail.com"):
        local_part = local_part.replace(".", "")
    return "@".join((local_part, hostname))
Ejemplo n.º 51
0
def validate_draft_recipients(draft):
    """Check that all recipient emails are at least plausible email
    addresses, before we try to send a draft."""
    for field in draft.to_addr, draft.bcc_addr, draft.cc_addr:
        if field is not None:
            for _, email_address in field:
                if not isinstance(address.parse(email_address),
                                  address.EmailAddress):
                    raise InputError(
                        'Invalid recipient address {}'.format(email_address))
Ejemplo n.º 52
0
    def __init__(self, email_string):
        """
        Takes a raw email string and processes it into something useful
        """
        self.str = email_string
        self.raw = mime.from_string(self.str)

        to = self.raw.headers['To']
        if to is None:
            self.recipients = []
        else:
            to = to.lower()
            self.recipients = address.parse_list(to) if ',' in to else [address.parse(to)]

        # It's possible a recipient is None if it is something like
        # 'Undisclosed recipients:;'
        self.recipients = [r for r in self.recipients if r is not None]
        self.sender = address.parse(self.raw.headers['From'].lower())

        self.subject = self.raw.subject
        self.id = self.raw.message_id
        self.date = parse(self.raw.headers['Date'])
        self.content_encoding = self.raw.content_encoding[0]

        # Extract plaintext body
        if self.raw.content_type.is_singlepart():
            self.full_body = self.raw.body
        elif self.raw.content_type.is_multipart():
            for p in self.raw.parts:
                if p.content_type == 'text/plain':
                    self.full_body = p.body
                    break

        # Try to get signature
        self.body, self.signature = extract_signature(self.full_body)

        # Try ML approach if necessary
        if self.signature is None:
            self.body, self.signature = signature.extract(self.full_body, sender=self.sender)

        # Get replies only, not the quotes
        self.body = quotations.extract_from(self.body, 'text/plain')
Ejemplo n.º 53
0
def validate_draft_recipients(draft):
    """Check that a draft has at least one recipient, and that all recipient
    emails are at least plausible email addresses, before we try to send it."""
    if not any((draft.to_addr, draft.bcc_addr, draft.cc_addr)):
        raise InputError("No recipients specified")
    for field in draft.to_addr, draft.bcc_addr, draft.cc_addr:
        if field is not None:
            for _, email_address in field:
                parsed = address.parse(email_address, addr_spec_only=True)
                if not isinstance(parsed, address.EmailAddress):
                    raise InputError(u"Invalid recipient address {}".format(email_address))
Ejemplo n.º 54
0
def get_email_from_to(headers):
    sender = address.parse(fix_addr_if_necessary(headers['from']))
    if not sender:
        print "could not parse sender: {}".format(headers['from'])
        return None, None
    if sender.hostname != 'enron.com':
        # skipping b/c we only care about enron communications
        return None, None
    receivers = address.parse_list(headers['to'])
    enron_receivers = [r for r in receivers if r.hostname == 'enron.com']
    return sender, enron_receivers
def handle_mailing_list_email_route(request):
    '''
    Handles the Mailgun route action when email is sent to a Mailgun mailing list.
    :param request:
    :return JsonResponse:
    '''
    logger.debug(u'Full mailgun post: %s', request.POST)

    from_ = address.parse(request.POST.get('from'))
    message_id = request.POST.get('Message-Id')
    recipients = set(address.parse_list(request.POST.get('recipient')))
    sender = address.parse(_remove_batv_prefix(request.POST.get('sender')))
    subject = request.POST.get('subject')
    user_alt_email_cache = CommChannelCache()

    logger.info(u'Handling Mailgun mailing list email from %s (sender %s) to '
                u'%s, subject %s, message id %s',
                from_, sender, recipients, subject, message_id)

    # short circuit if we detect a bounce loop
    sender_address = sender.address.lower() if sender else ''
    from_address = from_.address.lower() if from_ else ''
    if settings.NO_REPLY_ADDRESS.lower() in (sender_address, from_address):
        logger.error(u'Caught a bounce loop, dropping it. POST data:\n%s\n',
                     json.dumps(request.POST))
        return JsonResponse({'success': True})

    for recipient in recipients:
        # shortcut if we've already handled this message for this recipient
        if message_id:
            cache_key = (
                settings.CACHE_KEY_MESSAGE_HANDLED_BY_MESSAGE_ID_AND_RECIPIENT
                    % (message_id, recipient))
            if cache.get(cache_key):
                logger.warning(u'Message-Id %s was posted to the route handler '
                               u"for %s, but we've already handled that.  "
                               u'Skipping.', recipient, message_id)
                continue
        _handle_recipient(request, recipient, user_alt_email_cache)

    return JsonResponse({'success': True})
Ejemplo n.º 56
0
 def check_for_emails(active_sheet):
   
     for sample_row in active_sheet.rows:
         for sample_cell in sample_row:
             try:
                 if "@" in sample_cell.value:
                     if "." in sample_cell.value:
                         if address.parse(sample_cell.value,addr_spec_only=True) == sample_cell.value:
                             if sample_cell.value not in result:
                                 result.append(sample_cell.value)
                                 print sample_cell.value
             except TypeError:
                 continue
Ejemplo n.º 57
0
def cloudsearch_contact_repr(contact):
    # strip display name out of email address
    parsed = address.parse(contact.email_address)
    email_address = parsed.address if parsed else ''
    name = non_printable_chars_regex.sub('', contact.name or '')
    return {
        'id': contact.id,
        'namespace_id': contact.namespace_id,
        'name': name,
        'email_address': email_address,
        'phone_numbers': [_strip_non_numeric(p.number)
                          for p in contact.phone_numbers]
    }