Skip to content

Commit

Permalink
Merge pull request #46 from JohnnyGrey86/master
Browse files Browse the repository at this point in the history
Two new parser options for regex matching
  • Loading branch information
jamesridgway authored Dec 3, 2023
2 parents a60e4fc + 1215804 commit f5b95d3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ Usage:
--imap-folder=IMAP_FOLDER
IMAP Folder to extract attachments from
--subject-regex=SUBJECT_REGEX
Regex that the subject must match against
Regex that the subject must start with
--subject-regex-ignore-case
Ignore case when matching subject
--subject-regex-match-anywhere
Search entire subject for regex match
--date-after=DATE_AFTER
Select messages after this date
--date-before=DATE_BEFORE
Expand Down
19 changes: 15 additions & 4 deletions bin/attachment-downloader
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,19 @@ def process_message(filename_template, options, message):
options.date_before)
return

if options.subject_regex and not re.match(options.subject_regex, subject):
logging.warning("Skipping message '%s' subject '%s' because it does not match %s", uid, subject,
options.subject_regex)
return
if options.subject_regex:
regex_flags = re.IGNORECASE if options.subject_regex_ignore_case else 0

if options.subject_regex_match_anywhere:
if not re.search(options.subject_regex, subject, flags=regex_flags):
logging.warning("Skipping message '%s' subject '%s' because '%s' was not found in the string (case-%sensitive)",
uid, subject, options.subject_regex, "in" if options.subject_regex_ignore_case else "s")
return
else:
if not re.match(options.subject_regex, subject, flags=regex_flags):
logging.warning("Skipping message '%s' subject '%s' because it does not start with '%s' (case-%sensitive)",
uid, subject, options.subject_regex, "in" if options.subject_regex_ignore_case else "s")
return

logging.info("Processing message '%s' subject '%s'", uid, subject)

Expand Down Expand Up @@ -115,6 +124,8 @@ if __name__ == '__main__':
parser.add_option("--password", dest="password", help="IMAP Password")
parser.add_option("--imap-folder", dest="imap_folder", help="IMAP Folder to extract attachments from")
parser.add_option("--subject-regex", dest="subject_regex", help="Regex that the subject must match against")
parser.add_option("--subject-regex-ignore-case", dest="subject_regex_ignore_case", action="store_true", default=False, help="Provide this option to ignore regex case in subject.")
parser.add_option("--subject-regex-match-anywhere", dest="subject_regex_match_anywhere", action="store_true", default=False, help="Provide this option to search anywhere in subject")
parser.add_option("--date-after", dest="date_after", type='date',
help='Select messages after this date')
parser.add_option("--date-before", dest="date_before",
Expand Down

0 comments on commit f5b95d3

Please sign in to comment.