Get rid of command functions. Cleanup code. modified: listfix.py
Brian Barto bartobrian@gmail.com
Mon, 28 Mar 2022 22:07:20 -0400
1 files changed,
64 insertions(+),
193 deletions(-)
jump to
M
listfix.py
→
listfix.py
@@ -5,26 +5,8 @@ import os
import re sys.path.insert(1, sys.path[0] + "/mods") -from listfixdb import ListfixDB -######################## -## Variable Defs -######################## - -re_header_cont = re.compile("^\s+\S+") -re_header_end = re.compile("^\s*$") -re_email_arg = re.compile("([^<>\"\s]+)@(\S+\.[^<>\"\s]+)") -re_email_parts = re.compile("(\S+)@(\S+\.\S+)") -re_sender_info = re.compile("^From:\s+\"?([^<>\"]*?)\"?\s*<?(([^<>\"\s]+)@\S+\.[^<>\"\s]+)>?$") -re_auto_reply = re.compile("^Auto-Submitted: (auto-generated|auto-replied)", re.IGNORECASE) - -# 1 - Error -# 2 - Info -# 3 - Debug -# 4 - Trace -debug_level = 3 -command = None -rval = None +from listfixdb import ListfixDB ######################## ## Function Defs@@ -110,40 +92,57 @@ f = open("/tmp/listfix_log.txt", "a")
f.write(f"[{levels[level]}] {line}\n") f.close() -def command_filter(db): +######################## +## Main Program +######################## + +re_header_cont = re.compile("^\s+\S+") +re_header_end = re.compile("^\s*$") +re_email_arg = re.compile("([^<>\"\s]+)@(\S+\.[^<>\"\s]+)") +re_email_parts = re.compile("(\S+)@(\S+\.\S+)") +re_sender_info = re.compile("^From:\s+\"?([^<>\"]*?)\"?\s*<?(([^<>\"\s]+)@\S+\.[^<>\"\s]+)>?$") +re_auto_reply = re.compile("^Auto-Submitted: (auto-generated|auto-replied)", re.IGNORECASE) + +# 1 - Error +# 2 - Info +# 3 - Debug +# 4 - Trace +debug_level = 3 +command = None +rval = None + +## Connect to DB (create DB if needed) and check tables. + +listfix_dir = os.path.dirname(os.path.realpath(__file__)) +db = ListfixDB(listfix_dir + "/listfix.sqlite3") - recipient = None - sender = None - sender_name = None - content = [] - content_filtered = [] - list_name = None - list_recipients = [] - - ## Process Args +## Get command + +if (len(sys.argv) >= 1): + command = sys.argv[1] +else: + raise ValueError("Missing Command Argument") + +## Evaluate command + +if (command == "filter"): if (len(sys.argv) >= 3): if (not re_email_arg.match(sys.argv[2])): - debug_line(1, f"Invalid argument for filter command: {sys.argv[2]}") - return False + raise ValueError(f"Error while executing filter.") else: - debug_line(1, f"Missing argument for filter command.") - return False + raise ValueError(f"Error while executing filter.") recipient = sys.argv[2] - debug_line(2, f"Original Recipient - {recipient}") - - ## Get email contents - + content = [] for line in sys.stdin: content.append(line) if (len(content) == 0): - debug_line(1, "STDIN does not contain email data.") - return False - - ## Get sender info + raise ValueError(f"Error while executing filter. No content.") + sender = None + sender_name = None from_line = get_header(content, "From") if (from_line): if (re_sender_info.match(from_line)):@@ -151,36 +150,23 @@ results = re_sender_info.search(from_line)
sender_name = results.group(1) if results.group(1) else results.group(3) sender = results.group(2) else: - debug_line(1, "From line does not match regular expression.") - debug_line(3, from_line) - return False + raise ValueError(f"Error while executing filter. No matching email in From header.") else: - debug_line(1, "Could not find From line in email contents.") - return False - - debug_line(2, f"Original Sender - {sender}") - - ## Skip if this is an auto-reply + raise ValueError(f"Error while executing filter. No From header.") auto_sub_line = get_header(content, "Auto-Submitted") if (auto_sub_line and re_auto_reply.match(auto_sub_line)): - return True - - ## Get email list info + exit() list_name = db.get_list_name(recipient) if (not list_name): - debug_line(1, f"Recipient email list {recipient} not defined in database.") - return False - - ## Get recipient list + raise ValueError(f"Error while executing filter. List {recipient} not defined.") list_recipients = db.get_list_recipients(recipient) if (len(list_recipients) == 0): - debug_line(1, f"No recipients defined for email list: {recipient}.") - - ## Costruct Filtered Email + raise ValueError(f"Error while executing filter. No recipients defined for list {recipient}.") + content_filtered = [] content_filtered.append(f"From: \"{sender_name} via {list_name}\" <{recipient}>\n") if (sender not in list_recipients): content_filtered.append(f"Reply-To: {recipient}, {sender}\n")@@ -189,212 +175,97 @@ content_filtered.append(f"Reply-To: {recipient}\n")
exclude_headers = ["To", "Cc", "Subject", "Content-[^:]+", "MIME-Version"] content_filtered.extend(strip_headers(content, exclude_headers)) - ## Remove sender from list - if sender in list_recipients: list_recipients.remove(sender) - ## Send emails - for r in list_recipients: - send_email(r, content_filtered) - debug_line(3, f"List Recipient: {r}") + #send_email(r, content_filtered) + print(f"Sent to {r}") - return True +elif (command == "lists"): -def command_lists(db): lists = db.get_lists() - for l in lists: list_name = db.get_list_name(l) print(f"{l} ({list_name})") - return True - -def command_dump(db): - - list_email = None - - ## Check args +elif (command == "dump"): if (len(sys.argv) >= 3): if (not re_email_arg.match(sys.argv[2])): - print(f"Invalid argument for dump command: {sys.argv[2]}") - return False + raise ValueError(f"Error while executing dump.") else: - print(f"Missing argument for dump command.") - return False + raise ValueError(f"Error while executing dump.") list_email = sys.argv[2] - ## Print list recipients - recipients = db.get_list_recipients(list_email) - for r in recipients: recipient_name = db.get_recipient_name(list_email, r) print(f"{r} ({recipient_name})") - return True - -def command_create(db): - - list_email = None - list_name = None - - ## Check args +elif (command == "create"): if (len(sys.argv) >= 4): if (not re_email_arg.match(sys.argv[2])): - print(f"Invalid argument for create command: {sys.argv[2]}") - return False + raise ValueError(f"Error while executing create.") else: - print(f"Missing argument for create command.") - return False + raise ValueError(f"Error while executing create.") list_email = sys.argv[2] list_name = sys.argv[3] - - ## Insert into database db.create_list(list_email, list_name) print(f"New list ({list_email}) added.") - return True - -def command_destroy(db): - - list_email = None - - ## Check args +elif (command == "destroy"): if (len(sys.argv) >= 3): if (not re_email_arg.match(sys.argv[2])): - print(f"Invalid argument for destroy command: {sys.argv[2]}") - return False + raise ValueError(f"Error while executing destroy.") else: - print(f"Missing argument for destroy command.") - return False + raise ValueError(f"Error while executing destroy.") list_email = sys.argv[2] - - ## Destroy list db.destroy_list(list_email) print(f"Email list ({list_email}) destroyed.") - return True - -def command_add(db): - - list_email = None - recipient_email = None - recipient_name = None - - ## Check Args +elif (command == "add"): if (len(sys.argv) >= 5): if (not re_email_arg.match(sys.argv[2]) or not re_email_arg.match(sys.argv[3])): - print(f"Invalid arguments for add command: {sys.argv[2]}, {sys.argv[3]}, {sys.argv[4]}") - return False + raise ValueError(f"Error while executing add.") else: - print(f"Missing arguments for add command.") - return False + raise ValueError(f"Error while executing add.") list_email = sys.argv[2] recipient_email = sys.argv[3] recipient_name = sys.argv[4] - ## Check if recipient already exists in list - - if (db.check_recipient_exists(list_email, recipient_email)): - print(f"Recipient ({recipient_email}) already exists in list ({list_email})") - return False - - ## Insert into database - db.create_recipient(list_email, recipient_email, recipient_name) print(f"New recipient ({recipient_email}) added to list ({list_email})") - return True - -def command_remove(db): - - list_email = None - recipient_email = None - - ## Check Args +elif (command == "remove"): if (len(sys.argv) >= 4): if (not re_email_arg.match(sys.argv[2]) or not re_email_arg.match(sys.argv[3])): - print(f"Invalid arguments for remove command: {sys.argv[2]}, {sys.argv[3]}") - return False + raise ValueError(f"Error while executing remove.") else: - print(f"Missing arguments for remove command.") - return False + raise ValueError(f"Error while executing remove.") list_email = sys.argv[2] recipient_email = sys.argv[3] - - ## Destroy Recipient db.destroy_recipient(list_email, recipient_email) print(f"Recipient ({recipient_email}) removed from list ({list_email})") - return True - - -######################## -## Main Program -######################## - -## Connect to DB (create DB if needed) and check tables. - -listfix_dir = os.path.dirname(os.path.realpath(__file__)) -db = ListfixDB(listfix_dir + "/listfix.sqlite3") - -## Get command - -if (len(sys.argv) >= 1): - command = sys.argv[1] else: - raise ValueError("Missing Command Argument") -## Evaluate command - -if (command == "filter"): - rval = command_filter(db) - if (not rval): - raise ValueError(f"Error while executing command_filter()") -elif (command == "lists"): - rval = command_lists(db) - if (not rval): - raise ValueError(f"Error while executing command_lists()") -elif (command == "dump"): - rval = command_dump(db) - if (not rval): - raise ValueError(f"Error while executing command_dump()") -elif (command == "create"): - rval = command_create(db) - if (not rval): - raise ValueError(f"Error while executing command_create()") -elif (command == "destroy"): - rval = command_destroy(db) - if (not rval): - raise ValueError(f"Error while executing command_destroy()") -elif (command == "add"): - rval = command_add(db) - if (not rval): - raise ValueError(f"Error while executing command_add()") -elif (command == "remove"): - rval = command_remove(db) - if (not rval): - raise ValueError(f"Error while executing command_remove()") -else: raise ValueError(f"Unknown Command: {command}") ## Disconnect from DB