File: hotmail.py

package info (click to toggle)
python-flanker 0.9.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,976 kB
  • sloc: python: 9,308; makefile: 4
file content (118 lines) | stat: -rw-r--r-- 3,192 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# coding:utf-8

'''
    Email address validation plugin for hotmail.com email addresses.

    Notes:

        1-64 characters
        must start with letter or number
        must end with letter, number, hyphen (-), or underscore (_)
        must use letters, numbers, periods (.), hypens (-), or underscores (_)
        only one plus (+) is allowed
        case is ignored

    Grammar:

        local-part      ->  main-part [ tags ]
        main-part       ->  hotmail-prefix hotmail-root hotmail-suffix
        hotmail-prefix  ->  alpha | number
        hotmail-root    ->  alpha | number | period | hyphen | underscore
        hotmail-suffix  ->  alpha | number | hyphen | underscore
        tags            ->  + [ hotmail-root ]


    Other limitations:

        1. Only one consecutive period (.) is allowed in the local-part
        2. Length of local-part must be no more than 64 characters, and no
           less than 1 characters.

'''
import re
from flanker.addresslib.plugins._tokenizer import TokenStream
from flanker.addresslib._parser.lexer import _UNICODE_CHAR

HOTMAIL_PREFIX  = re.compile(r'''
                            ( [A-Za-z0-9]
                            | {unicode_char}
                            )+
                            '''.format(unicode_char=_UNICODE_CHAR),
                            re.MULTILINE | re.VERBOSE)

HOTMAIL_BASE    = re.compile(r'''
                            ( [A-Za-z0-9\.\-\_]
                            | {unicode_char}
                            )+
                            '''.format(unicode_char=_UNICODE_CHAR),
                            re.MULTILINE | re.VERBOSE)

HOTMAIL_SUFFIX  = re.compile(r'''
                            ( [A-Za-z0-9\-\_]
                            | {unicode_char}
                            )+
                            '''.format(unicode_char=_UNICODE_CHAR),
                            re.MULTILINE | re.VERBOSE)

PLUS            = '+'


def validate(email_addr):
    # Setup for handling EmailAddress type instead of literal string
    localpart = email_addr.mailbox

    # check string exists and not empty
    if not localpart:
        return False

    # remove tag if it exists
    lparts = localpart.split('+')
    real_localpart = lparts[0]

    # length check
    l = len(real_localpart)
    if l < 1 or l > 64:
        return False

    # start can only be alphanumeric
    if HOTMAIL_PREFIX.match(real_localpart[0]) is None:
        return False

    # can not end with dot
    if HOTMAIL_SUFFIX.match(real_localpart[-1]) is None:
        return False

    # no more than one plus (+)
    if localpart.count('+') > 1:
        return False

    # grammar check
    retval = _validate(real_localpart)
    return retval


def _validate(localpart):
    stream = TokenStream(localpart)

    # get the hotmail base
    mpart = stream.get_token(HOTMAIL_BASE)
    if mpart is None:
        return False

    # optional tags
    tgs = _tags(stream)

    if not stream.end_of_stream():
        return False

    return True


def _tags(stream):
    pls = stream.get_token(PLUS)
    bse = stream.get_token(HOTMAIL_BASE)

    if bse and pls is None:
        return False

    return True