-
Notifications
You must be signed in to change notification settings - Fork 216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added 'since' option to search for messages since a certain time #527
Open
ramspoluri
wants to merge
1
commit into
domainaware:master
Choose a base branch
from
ramspoluri:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ | |
from base64 import b64decode | ||
from collections import OrderedDict | ||
from csv import DictWriter | ||
from datetime import datetime | ||
from datetime import datetime, timedelta | ||
from io import BytesIO, StringIO | ||
from typing import Callable | ||
|
||
|
@@ -28,7 +28,8 @@ | |
from mailsuite.smtp import send_email | ||
|
||
from parsedmarc.log import logger | ||
from parsedmarc.mail import MailboxConnection | ||
from parsedmarc.mail import MailboxConnection, IMAPConnection, \ | ||
MSGraphConnection, GmailConnection | ||
from parsedmarc.utils import get_base_domain, get_ip_address_info | ||
from parsedmarc.utils import is_outlook_msg, convert_outlook_msg | ||
from parsedmarc.utils import parse_email | ||
|
@@ -1371,6 +1372,7 @@ | |
strip_attachment_payloads=False, | ||
results=None, | ||
batch_size=10, | ||
since=None, | ||
create_folders=True): | ||
""" | ||
Fetches and parses DMARC reports from a mailbox | ||
|
@@ -1393,6 +1395,8 @@ | |
results (dict): Results from the previous run | ||
batch_size (int): Number of messages to read and process before saving | ||
(use 0 for no limit) | ||
since: Search for messages since certain time | ||
(units - {"m":"minutes", "h":"hours", "d":"days", "w":"weeks"}) | ||
create_folders (bool): Whether to create the destination folders | ||
(not used in watch) | ||
|
||
|
@@ -1405,6 +1409,9 @@ | |
if connection is None: | ||
raise ValueError("Must supply a connection") | ||
|
||
# current_time useful to fetch_messages later in the program | ||
current_time = None | ||
|
||
aggregate_reports = [] | ||
forensic_reports = [] | ||
smtp_tls_reports = [] | ||
|
@@ -1428,12 +1435,44 @@ | |
connection.create_folder(smtp_tls_reports_folder) | ||
connection.create_folder(invalid_reports_folder) | ||
|
||
messages = connection.fetch_messages(reports_folder, batch_size=batch_size) | ||
if since: | ||
_since = 1440 # default one day | ||
if re.match(r'\d{1,2}[mhd]$', since): | ||
s = re.split(r'(\d+)', since) | ||
match s[2]: | ||
case 'm': _since = int(s[1]) | ||
case 'h': _since = int(s[1])*60 | ||
case 'd': _since = int(s[1])*60*24 | ||
case 'w': _since = int(s[1])*60*24*7 | ||
else: | ||
logger.warning("Incorrect format for \'since\' option. \ | ||
Provided value:{0}, Expected values:(5m|3h|2d|1w). \ | ||
Ignoring option, fetching messages for last 24hrs" | ||
.format(since)) | ||
|
||
if isinstance(connection, IMAPConnection): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add a reference to RFC3501 in order to indicate that the restriction (no time, no timezone) is on the SMTP protocol |
||
logger.debug("Only days and weeks values in \'since\' option are \ | ||
considered for IMAP conections. Examples: 2d or 1w") | ||
since = (datetime.utcnow() - timedelta(minutes=_since)).date() | ||
current_time = datetime.utcnow().date() | ||
elif isinstance(connection, MSGraphConnection): | ||
since = (datetime.utcnow() - timedelta(minutes=_since)) \ | ||
.isoformat() + 'Z' | ||
current_time = datetime.utcnow().isoformat() + 'Z' | ||
elif isinstance(connection, GmailConnection): | ||
since = (datetime.utcnow() - timedelta(minutes=_since)) \ | ||
.strftime('%s') | ||
current_time = datetime.utcnow().strftime('%s') | ||
else: | ||
pass | ||
|
||
messages = connection.fetch_messages(reports_folder, batch_size=batch_size, | ||
since=since) | ||
total_messages = len(messages) | ||
logger.debug("Found {0} messages in {1}".format(len(messages), | ||
reports_folder)) | ||
|
||
if batch_size: | ||
if batch_size and not since: | ||
message_limit = min(total_messages, batch_size) | ||
else: | ||
message_limit = total_messages | ||
|
@@ -1445,7 +1484,15 @@ | |
logger.debug("Processing message {0} of {1}: UID {2}".format( | ||
i+1, message_limit, msg_uid | ||
)) | ||
msg_content = connection.fetch_message(msg_uid) | ||
if isinstance(mailbox, MSGraphConnection): | ||
if test: | ||
msg_content = connection.fetch_message(msg_uid, | ||
mark_read=False) | ||
else: | ||
msg_content = connection.fetch_message(msg_uid, | ||
mark_read=True) | ||
else: | ||
msg_content = connection.fetch_message(msg_uid) | ||
try: | ||
sa = strip_attachment_payloads | ||
parsed_email = parse_report_email( | ||
|
@@ -1564,7 +1611,11 @@ | |
("forensic_reports", forensic_reports), | ||
("smtp_tls_reports", smtp_tls_reports)]) | ||
|
||
total_messages = len(connection.fetch_messages(reports_folder)) | ||
if current_time: | ||
total_messages = len(connection.fetch_messages(reports_folder, | ||
since=current_time)) | ||
else: | ||
total_messages = len(connection.fetch_messages(reports_folder)) | ||
|
||
if not test and not batch_size and total_messages > 0: | ||
# Process emails that came in during the last run | ||
|
@@ -1582,7 +1633,8 @@ | |
always_use_local_files=always_use_local_files, | ||
reverse_dns_map_path=reverse_dns_map_path, | ||
reverse_dns_map_url=reverse_dns_map_url, | ||
offline=offline | ||
offline=offline, | ||
since=current_time, | ||
) | ||
|
||
return results | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why restrict to two digits ? (eg: 365 days)
I suggest to use \d+ as the next regex