From 5df9c0de932a9c90bfc9cbcec4fd463782f00bb5 Mon Sep 17 00:00:00 2001 From: "max.mehl" Date: Thu, 23 Dec 2021 12:36:15 +0100 Subject: [PATCH] embed active toggle get_maildropline --- autoreply_editor/__init__.py | 3 +- autoreply_editor/main.py | 67 +++++++++++++++++++++++------------- wsgi.py | 1 + 3 files changed, 46 insertions(+), 25 deletions(-) diff --git a/autoreply_editor/__init__.py b/autoreply_editor/__init__.py index 64a9a3b..bb3777f 100644 --- a/autoreply_editor/__init__.py +++ b/autoreply_editor/__init__.py @@ -1,8 +1,9 @@ +"""Initialise flask app""" from flask import Flask app = Flask(__name__) -import autoreply_editor.main +import autoreply_editor.main #pylint: disable=wrong-import-position app.config['BASIC_AUTH_FORCE'] = True app.config.from_pyfile("../config.py") diff --git a/autoreply_editor/main.py b/autoreply_editor/main.py index b24541e..593cd99 100644 --- a/autoreply_editor/main.py +++ b/autoreply_editor/main.py @@ -12,18 +12,19 @@ from autoreply_editor import app basic_auth = BasicAuth(app) + def qmail_status(user): """Find out whether the filter is currently activated for the given mail user""" with open(get_qmailfile(user), encoding="utf8") as qmailfile: # trigger to check whether we have to re-open the file for initialisation initialise = False - # try to find active filter if re.search( - rf"^{get_maildropline(user, regex=True)}$", qmailfile.read(), re.MULTILINE + rf"{get_maildropline(user, active=True, regex=True)}", + qmailfile.read(), + re.MULTILINE, ): - # if get_maildropline(user) in qmailfile.read(): return True else: # jump back to top of file because we've read it in the "if" @@ -31,21 +32,19 @@ def qmail_status(user): # filter is deactivated (commented) if re.search( - rf"^#{get_maildropline(user, regex=True)}$", + rf"{get_maildropline(user, active=False, regex=True)}", qmailfile.read(), re.MULTILINE, ): - # if f"#{get_maildropline(user)}" in qmailfile.read(): return False + # The maildrop line does not exist in the expected form else: initialise = True # Append a commented filter command to the file if initialise: - with open( - get_qmailfile(user), mode="a", encoding="utf8" - ) as qmailfile: - qmailfile.write(f"#{get_maildropline(user)}") + with open(get_qmailfile(user), mode="a", encoding="utf8") as qmailfile: + qmailfile.write(f"{get_maildropline(user, active=False)}") return False @@ -74,12 +73,14 @@ def get_filterfile(user, name): filter_base = f"{str(Path.home())}/.filter-autoreply" filepath = f"{filter_base}-{user.split('@')[0]}" if not os.path.isfile(filepath): - with open(os.path.join(app.root_path, "templates/filterfile.j2"), encoding="utf-8") as template: + with open( + os.path.join(app.root_path, "templates/filterfile.j2"), encoding="utf-8" + ) as template: filterconfig = Template(template.read()).render( email=user, - user=user.split('@')[0], + user=user.split("@")[0], name=name, - messagefile=get_messagefile(user) + messagefile=get_messagefile(user), ) with open(filepath, "w", encoding="utf-8") as filterfile: @@ -88,16 +89,29 @@ def get_filterfile(user, name): return filepath -def get_maildropline(user, regex=False): +def get_maildropline(user, active=True, regex=False): + """ + Define the line calling maildrop, with options to have the line disabled or + in Regex format + """ + base = f"|maildrop {get_filterfile(user, 'FOOBAR')}" + # If the filter should be inactive, we comment it + if not active: + base = f"#{base}" + + # depending on whether it should be RE escaped or not, return the line if regex: - return re.escape(f"|maildrop {get_filterfile(user, 'FOOBAR')}") + return f"^{re.escape(base)}$" else: - return f"|maildrop {get_filterfile(user, 'FOOBAR')}" + return base @app.route("/") def index(): - with open(get_messagefile(app.config.get("MAIL_USER")), "r", encoding="utf-8") as messagefile: + """Index page""" + with open( + get_messagefile(app.config.get("MAIL_USER")), "r", encoding="utf-8" + ) as messagefile: message = messagefile.read() return render_template( @@ -109,21 +123,28 @@ def index(): @app.route("/", methods=["POST"]) def index_post(): + """Index page with POST request""" if request.method == "POST": if request.form["action"] == "message": input_message = request.form["message"] - with open(get_messagefile(app.config.get("MAIL_USER")), "w", encoding="utf-8") as messagefile: + with open( + get_messagefile(app.config.get("MAIL_USER")), "w", encoding="utf-8" + ) as messagefile: messagefile.write(str(input_message)) result = "Success: The autoreply message has been updated!" if request.form["action"] == "qmail": # define whether to set a comment if request.form["status"] == "on": - preis = "#" - preshould = "" + state_current = get_maildropline( + app.config.get("MAIL_USER"), active=False + ) + state_desired = get_maildropline(app.config.get("MAIL_USER")) else: - preis = "" - preshould = "#" + state_current = get_maildropline(app.config.get("MAIL_USER")) + state_desired = get_maildropline( + app.config.get("MAIL_USER"), active=False + ) with fileinput.FileInput( get_qmailfile(app.config.get("MAIL_USER")), @@ -132,9 +153,7 @@ def index_post(): ) as qmailfile: for line in qmailfile: print( - line.replace( - f"{preis}{get_maildropline(app.config.get('MAIL_USER'))}", f"{preshould}{get_maildropline(app.config.get('MAIL_USER'))}" - ), + line.replace(state_current, state_desired), end="", ) diff --git a/wsgi.py b/wsgi.py index 96640b0..3809072 100644 --- a/wsgi.py +++ b/wsgi.py @@ -1,3 +1,4 @@ +"""WSGI for Flask app""" from autoreply_editor import app if __name__ == "__main__":