-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathaddress.py
71 lines (50 loc) · 1.91 KB
/
address.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
from __future__ import annotations
import re
from email.utils import parseaddr
from django.conf import settings
from django.utils.encoding import force_bytes
from sentry import options
from .signer import _CaseInsensitiveSigner
# cache the domain_from_email calculation
# This is just a tuple of (email, email-domain)
_from_email_domain_cache: tuple[str, str] | None = None
# Pull email from the string: "lauryn <lauryn@sentry.io>"
EMAIL_PARSER = re.compile(r"<([^>]*)>")
signer = _CaseInsensitiveSigner()
def get_from_email_domain() -> str:
global _from_email_domain_cache
from_ = options.get("mail.from")
if _from_email_domain_cache is None or not _from_email_domain_cache[0] == from_:
_from_email_domain_cache = (from_, domain_from_email(from_))
return _from_email_domain_cache[1]
def email_to_group_id(address: str) -> int:
"""
Email address should be in the form of:
{group_id}+{signature}@example.com
"""
address = address.split("@", 1)[0]
signed_data = address.replace("+", ":")
return int(force_bytes(signer.unsign(signed_data)))
def group_id_to_email(group_id: int) -> str:
signed_data = signer.sign(str(group_id))
return "@".join(
(
signed_data.replace(":", "+"),
options.get("mail.reply-hostname") or get_from_email_domain(),
)
)
def domain_from_email(email: str) -> str:
email = parseaddr(email)[1]
try:
return email.split("@", 1)[1]
except IndexError:
# The email address is likely malformed or something
return email
def is_valid_email_address(value: str) -> bool:
return not settings.INVALID_EMAIL_ADDRESS_PATTERN.search(value)
def parse_email(email: str) -> str:
matches = EMAIL_PARSER.search(email)
return matches.group(1) if matches else ""
def parse_user_name(email: str) -> str:
# captures content before angle bracket
return email.split("<")[0].strip()