-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmessage_check.py
83 lines (55 loc) · 2.01 KB
/
message_check.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
#!/usr/bin/env python
import email
import logging
import re
logger = logging.getLogger(__name__)
def check_message(message, check_details):
"""
Check if the message can be parsed with the given details.
Parameters
----------
message : email.message.Message
https://docs.python.org/3.4/library/email.message.html#email.message.Message
check_details : dict
Contains checks and settings for the given message.
Returns
-------
dict
Dictionary of match details extracted from the message.
"""
check = check_details.get('check')
if check is None:
return None
if 'from' in check:
logger.debug(message['From'])
# parseaddr() splits "From" into name and address.
# https://docs.python.org/3/library/email.util.html#email.utils.parseaddr
email_from = email.utils.parseaddr(message['From'])[1]
logger.debug('Email is from "%s".', email_from)
required_from = check['from']
# Skip this message if it did not come from the correct sender.
if email_from != required_from:
msg = 'Email is not from the correct sender (%s != %s).'
logger.warning(msg, email_from, required_from)
return None
subject = message['Subject']
logger.debug('Email subject is "%s".', subject)
# Use the compiled RegEx if it is available.
subject_regex = check.get('subject_regex_compiled')
# Compile and save the RegEx otherwise.
if subject_regex is None:
subject_regex_list = [s.strip() for s in check['subject_regex']]
subject_regex_text = ''.join(subject_regex_list)
subject_regex = re.compile(subject_regex_text)
check['subject_regex_compiled'] = subject_regex
else:
subject_regex_text = subject_regex.pattern
# Check if the message subject matches the RegEx.
match_data = subject_regex.match(subject)
# Skip this message if the subject does not match the RegEx.
if match_data is None:
logger.debug('Regex is %s.', subject_regex_text)
logger.warning('Email subject does not match the required format.')
return None
# Return a dict of the values matched in the regex.
return match_data.groupdict()