From a05456cda46342c97f7fd8e2f04fdc83c056f682 Mon Sep 17 00:00:00 2001 From: Lars Bogner Date: Sun, 24 May 2020 12:46:58 +0200 Subject: [PATCH] Add a little bit of functionality.... in theory it should be able to run.... in theory, not yet tested --- MailDispatcher.py | 22 ++++++++++++++++++++++ SubjectHandler.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + secrets.txt | 2 ++ 4 files changed, 71 insertions(+) create mode 100644 SubjectHandler.py create mode 100644 secrets.txt diff --git a/MailDispatcher.py b/MailDispatcher.py index 44e0f72..b78f817 100644 --- a/MailDispatcher.py +++ b/MailDispatcher.py @@ -1,6 +1,12 @@ import email import imaplib +import time +from SubjectHandler import SubjectHandler +SubHandler = SubjectHandler() + +mail_addr = None +mail_pwd = None """ CONFIG SECTION @@ -8,6 +14,13 @@ CONFIG SECTION imap_port = 993 imap_server = "imap.gmail.com" mail_domain = "gmail.com" +mail_time_delta = 30 # in seconds + +secrets_file = "./secrets.txt" + +# add all nodes here in this(↓) pattern +# SubHandler += SubjectHandler.Node(regex, function) +SubHandler += SubjectHandler.Node("Test[0-9]*", lambda x: print(str(x))) """ END OF CONFIG SECTION @@ -20,6 +33,8 @@ def import_secrets(file): :param file: path to file: content is '[MAIL]\n[PWD]' :return: secrets, that can be saved to global variables """ + global mail_addr + global mail_pwd with open(file) as f: mail_addr = f.readline() mail_pwd = f.readline() @@ -53,5 +68,12 @@ def mail_handling(): return messages +import_secrets(secrets_file) +while True: + SubHandler.run(mail_handling()) + time.sleep(mail_time_delta) + + + diff --git a/SubjectHandler.py b/SubjectHandler.py new file mode 100644 index 0000000..978a70c --- /dev/null +++ b/SubjectHandler.py @@ -0,0 +1,46 @@ +import re + + +class SubjectHandler: + """ + class for managing all the sub-functions(nodes) + """ + class Node: + """ + node containing a sub-function + """ + def __init__(self, regex, f): + """ + creating node + :param regex: regex checked against subject + :param f: function to be called if keyword in subject + """ + self.regex = regex + self.func = f + + def __init__(self): + self.nodes = [] + + def __add__(self, other): + """ + adding node to self + :param other: node + :return: self with added node + """ + self.nodes.append(other) + return self + + def __len__(self): + return len(self.nodes) + + def run(self, messages): + """ + running the class and checking if there's a regex match in any node and then running the node + :param messages: array of messages, which should be checked + :return: - + """ + for message in messages: + subject = message[0] + for node in self.nodes: + if re.match(node.regex, subject): + node.func(message) diff --git a/requirements.txt b/requirements.txt index e69de29..d7aa816 100644 --- a/requirements.txt +++ b/requirements.txt @@ -0,0 +1 @@ +setuptools~=46.4.0 \ No newline at end of file diff --git a/secrets.txt b/secrets.txt new file mode 100644 index 0000000..4991729 --- /dev/null +++ b/secrets.txt @@ -0,0 +1,2 @@ +mail@example.com +Password123 \ No newline at end of file