-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail_helper.py
191 lines (145 loc) · 6.27 KB
/
email_helper.py
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import re
def email_parts(email) -> dict:
"""
Returns the search keys from a given email address as a list (in priority order)
>>> email_parts("")
{}
>>> email_parts("not.an.email.com")
{}
>>> email_parts("bad@single_domain")
{}
>>> email_parts(" @bad.com")
{}
>>> email_parts("good@single_domain.com")
{'identifier': 'good', 'with_plus': 'good', 'domain': 'single_domain.com', 'email': 'good@single_domain.com', 'email_with_plus': 'good@single_domain.com'}
>>> email_parts("good+should_be_ignored@single_domain.com")
{'identifier': 'good', 'with_plus': 'good+should_be_ignored', 'domain': 'single_domain.com', 'email': 'good@single_domain.com', 'email_with_plus': 'good+should_be_ignored@single_domain.com'}
>>> email_parts("good+should_also+be_ignored@single_domain.com")
{'identifier': 'good', 'with_plus': 'good+should_alsobe_ignored', 'domain': 'single_domain.com', 'email': 'good@single_domain.com', 'email_with_plus': 'good+should_alsobe_ignored@single_domain.com'}
>>> email_parts("[email protected]")
{'identifier': 'case', 'with_plus': 'case', 'domain': 'doesnotmatter.com', 'email': '[email protected]', 'email_with_plus': '[email protected]'}
>>> email_parts("url_encoded%40allowed-too.com")
{'identifier': 'url_encoded', 'with_plus': 'url_encoded', 'domain': 'allowed-too.com', 'email': '[email protected]', 'email_with_plus': '[email protected]'}
>>> email_parts("url_encoded%[email protected]")
{'identifier': 'url_encoded', 'with_plus': 'url_encoded', 'domain': 'this-could-be-bad.com', 'email': '[email protected]', 'email_with_plus': '[email protected]'}
"""
if type(email) == list:
for le in email:
if type(le) == str and "@" in le:
email = le
break
if type(email) == dict:
if "email" in email and "@" in email["email"]:
email = email["email"]
if type(email) != str:
email = None
res = {}
if email:
email = email.strip().lower()
if len(email) >= 320:
return res
x = re.search("(?P<identifier>.*)(\@|%40)(?P<domain>.*)", email)
if x:
identifiers = extractIdentifiers(x.group("identifier"))
if not identifiers:
return res
else:
identifier = identifiers["identifier"]
with_plus = identifiers["with_plus"]
if len(identifier) > 64:
return res
domain = x.group("domain")
if len(domain.strip()) == 0:
return res
elif "." not in domain:
return res
elif len(domain) > 254:
return res
res["identifier"] = identifier
res["with_plus"] = with_plus
res["domain"] = domain
res["email"] = f"{identifier}@{domain}"
res["email_with_plus"] = f"{with_plus}@{domain}"
return res
def extractIdentifiers(identifier: str) -> dict:
identifier = identifier.strip()
re_match = re.search("^(?P<r>[^\+\%]+)(?P<a>\+.*)?", identifier)
if re_match:
res = re_match.groupdict()["r"]
add = re_match.groupdict()["a"]
if res:
res = res.strip()
if add:
add = add.replace("+", "").strip()
return {"identifier": res, "with_plus": f"{res}+{add}"}
return {"identifier": res, "with_plus": res}
return {}
def emailKeys(email: str) -> list:
"""
Returns the search keys from a given email address as a list (in priority order)
>>> emailKeys("")
[]
>>> emailKeys("not.an.email.com")
[]
>>> emailKeys("bad@single_domain")
[]
>>> emailKeys("[email protected]")
[]
>>> emailKeys(" @bad.com")
[]
>>> emailKeys("good@single_domain.com")
['email:good@single_domain.com', 'domain:single_domain.com']
>>> emailKeys("url_encoded%40allowed-too.com")
['email:[email protected]', 'domain:allowed-too.com']
>>> r = emailKeys("[email protected]")
>>> e = ['email:[email protected]', \
'email:[email protected]', \
'domain:test.service.gov.uk', 'domain:service.gov.uk', 'domain:gov.uk']
>>> r == e
True
>>> r = emailKeys("[email protected]")
>>> e = ['email:[email protected]', \
'domain:test.service.gov.uk', 'domain:service.gov.uk', 'domain:gov.uk']
>>> r == e
True
>>> r = emailKeys("[email protected]")
>>> e = ['email:[email protected]', 'domain:abc.abc.abc.com', \
'domain:abc.abc.com', 'domain:abc.com']
>>> r == e
True
"""
res = []
parts = email_parts(email)
if parts:
domain = parts["domain"]
if parts["email_with_plus"] != parts["email"]:
# add the full email with plus, e.g: [email protected]
res.append(f"email:{parts['email_with_plus']}")
# add the normalised email, e.g: [email protected]
res.append(f"email:{parts['email']}")
# add the full domain, e.g: test.service.gov.uk
res.append(f"domain:{domain}")
# split the full domain, iterate through each except the last 2 objects
# e.g. for test.service.gov.uk this would add the following to res list:
# - domain:service.gov.uk
# - domain:gov.uk
splitDomain = domain.split(".")
if len(splitDomain) >= 2:
for i in splitDomain[:-2]:
# if i has no value, e.g. [email protected] then return empty list
if len(i) == 0:
return []
# cut the domain up by this i (the sub domain)
domain = domain[(len(i) + 1) :]
res.append(f"domain:{domain}")
else:
# if there's only one entry, e.g. for email bad@example
# then something has gone wrong, return an empty list
return []
return res
if __name__ == "__main__":
"""
If this python is called directly, test using doctest
"""
import doctest
doctest.testmod()