From a098e63c9c4877f8c1a673a23585be528d271025 Mon Sep 17 00:00:00 2001 From: Christian Haschek Date: Tue, 9 Nov 2021 23:59:23 +0100 Subject: [PATCH] added new config methods for cleanup and discarding. closes #33 --- example.config.ini | 9 ++++++++- python/mailserver.py | 41 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/example.config.ini b/example.config.ini index 2c5f341..6285e77 100644 --- a/example.config.ini +++ b/example.config.ini @@ -11,7 +11,14 @@ DOMAINS=yourdomain,sub.yourdomain [MAILSERVER] ; Port that the Mailserver will run on (default 25 but that needs root) MAILPORT=25 +; true or false depending on if you only want to save emails to the above set domains +; this greatly reduces the amount of spam you will receive +DISCARD_UNKNOWN=true [DATETIME] ; The moment.js format you'd like your date to be formatted in -DATEFORMAT="D.M.YYYY HH:mm" \ No newline at end of file +DATEFORMAT="D.M.YYYY HH:mm" + +[CLEANUP] +; Emails older than these amount of days will be deleted. false for never +DELETE_OLDER_THAN_DAYS=false \ No newline at end of file diff --git a/python/mailserver.py b/python/mailserver.py index 371e4db..962ae7f 100644 --- a/python/mailserver.py +++ b/python/mailserver.py @@ -12,6 +12,26 @@ import json logger = logging.getLogger(__name__) +# globals for settings +DISCARD_UNKNOWN = False +DELETE_OLDER_THAN_DAYS = False +DOMAINS = [] +LAST_CLEANUP = 0 + +def cleanup(): + if(DELETE_OLDER_THAN_DAYS == False or time.time() - LAST_CLEANUP < 86400): + return + logger.info("Cleaning up") + rootdir = '../data/' + for subdir, dirs, files in os.walk(rootdir): + for file in files: + if(file.endswith(".json")): + filepath = os.path.join(subdir, file) + file_modified = os.path.getmtime(filepath) + if(time.time() - file_modified > (DELETE_OLDER_THAN_DAYS * 86400)): + os.remove(filepath) + logger.info("Deleted file: " + filepath) + class CustomSMTPServer(smtpd.SMTPServer): def process_message(self, peer, mailfrom, rcpttos, data): try: @@ -91,13 +111,19 @@ class CustomSMTPServer(smtpd.SMTPServer): for em in rcpttos: em = em.lower() + + domain = em.split('@')[1] + if(DISCARD_UNKNOWN and not domain in DOMAINS): + logger.info('Discarding email for unknown domain: %s' % domain) + continue + if not os.path.exists("../data/"+em): - os.mkdir( "../data/"+em, 0755 ) + os.mkdir( "../data/"+em, 0o755 ) #same attachments if any for att in attachments: if not os.path.exists("../data/"+em+"/attachments"): - os.mkdir( "../data/"+em+"/attachments", 0755 ) + os.mkdir( "../data/"+em+"/attachments", 0o755 ) attd = attachments[att] file = open("../data/"+em+"/attachments/"+filenamebase+"-"+attd[0], 'wb') file.write(attd[1]) @@ -123,11 +149,18 @@ if __name__ == '__main__': print "[ERR] Config.ini not found. Rename example.config.ini to config.ini. Defaulting to port 25" port = 25 else : - Config = ConfigParser.ConfigParser() + Config = ConfigParser.ConfigParser(allow_no_value=True) Config.read("../config.ini") port = int(Config.get("MAILSERVER","MAILPORT")) + if("discard_unknown" in Config.options("MAILSERVER")): + DISCARD_UNKNOWN = (Config.get("MAILSERVER","DISCARD_UNKNOWN").lower() == "true") + DOMAINS = Config.get("GENERAL","DOMAINS").lower().split(",") - + if("CLEANUP" in Config.sections() and "delete_older_than_days" in Config.options("CLEANUP")): + DELETE_OLDER_THAN_DAYS = (Config.get("CLEANUP","DELETE_OLDER_THAN_DAYS").lower() == "true") + + cleanup() + quit() print "[i] Starting Mailserver on port",port