diff --git a/autoreply_editor/main.py b/autoreply_editor/main.py index 06a676f..08bc9ba 100644 --- a/autoreply_editor/main.py +++ b/autoreply_editor/main.py @@ -5,7 +5,7 @@ import os import re import shutil from pathlib import Path -from flask import request, render_template +from flask import abort, request, render_template from flask_basicauth import BasicAuth from jinja2 import Template from autoreply_editor import app @@ -49,9 +49,21 @@ def qmail_status(user): return False +def check_user(user, output=False): + """Check if user exists""" + fulluser = [item for item in app.config.get("USERS") if item[1].split('@')[0] == user] + if not fulluser: + return False + else: + if output: + return fulluser[0] + else: + return True + + def get_messagefile(user): """Return message file of user, and create if necessary""" - filepath = f"message-{user.split('@')[0]}.txt" + filepath = f"message-{user}.txt" if not os.path.isfile(filepath): Path(filepath).touch() @@ -61,25 +73,25 @@ def get_messagefile(user): def get_qmailfile(user): """Return qmail file of user, and create if necessary based on default qmail""" qmail_base = f"{str(Path.home())}/.qmail" - filepath = f"{qmail_base}-{user.split('@')[0]}" + filepath = f"{qmail_base}-{user}" if not os.path.isfile(filepath): shutil.copy(f"{qmail_base}-default", filepath) return filepath -def get_filterfile(user, name): +def get_filterfile(user): """Return .filter-autoreply file of user, and create if necessary""" filter_base = f"{str(Path.home())}/.filter-autoreply" - filepath = f"{filter_base}-{user.split('@')[0]}" + filepath = f"{filter_base}-{user}" if not os.path.isfile(filepath): 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], - name=name, + email=check_user(user, output=True)[1], + user=user, + name=check_user(user, output=True)[0], messagefile=get_messagefile(user), ) @@ -94,7 +106,7 @@ 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')}" + base = f"|maildrop {get_filterfile(user)}" # If the filter should be inactive, we comment it if not active: base = f"#{base}" @@ -108,26 +120,42 @@ def get_maildropline(user, active=True, regex=False): @app.route("/") def index(): - """Index page""" + """Index page listing available users""" + return render_template( + "index.html", + users=app.config.get("USERS"), + ) + +@app.route("/user/") +def user_status(user): + """Status for specific user""" + # Check if user is valid + if not check_user(user): + abort(404) + with open( - get_messagefile(app.config.get("MAIL_USER")), "r", encoding="utf-8" + get_messagefile(user), "r", encoding="utf-8" ) as messagefile: message = messagefile.read() return render_template( - "index.html", + "user.html", message=message, - qmail_status=qmail_status(app.config.get("MAIL_USER")), + qmail_status=qmail_status(user), ) -@app.route("/", methods=["POST"]) -def index_post(): - """Index page with POST request""" +@app.route("/user/", methods=["POST"]) +def user_update(user): + """Update user with POST request""" + # Check if user is valid + if not check_user(user): + abort(404) + if request.form["action"] == "message": input_message = request.form["message"] with open( - get_messagefile(app.config.get("MAIL_USER")), "w", encoding="utf-8" + get_messagefile(user), "w", encoding="utf-8" ) as messagefile: messagefile.write(str(input_message)) result = "Success: The autoreply message has been updated!" @@ -135,18 +163,14 @@ def index_post(): if request.form["action"] == "qmail": # define whether to set a comment if request.form["status"] == "on": - state_current = get_maildropline( - app.config.get("MAIL_USER"), active=False - ) - state_desired = get_maildropline(app.config.get("MAIL_USER")) + state_current = get_maildropline(user, active=False) + state_desired = get_maildropline(user) else: - state_current = get_maildropline(app.config.get("MAIL_USER")) - state_desired = get_maildropline( - app.config.get("MAIL_USER"), active=False - ) + state_current = get_maildropline(user) + state_desired = get_maildropline(user, active=False) with fileinput.FileInput( - get_qmailfile(app.config.get("MAIL_USER")), + get_qmailfile(user), inplace=True, backup=".bak", ) as qmailfile: @@ -159,6 +183,11 @@ def index_post(): result = f"Success: the autoreply is now {request.form['status']}." try: - return render_template("result.html", result=result) + return render_template("update.html", user=user, result=result) except UnboundLocalError: - return render_template("result.html", result="Something went terribly wrong!") + return render_template("update.html", user=user, result="Something went terribly wrong!") + + +@app.errorhandler(404) +def page_not_found(e): + return render_template('error.html', error=e), 404 diff --git a/autoreply_editor/templates/result.html b/autoreply_editor/templates/error.html similarity index 87% rename from autoreply_editor/templates/result.html rename to autoreply_editor/templates/error.html index 84624af..7cc79fc 100644 --- a/autoreply_editor/templates/result.html +++ b/autoreply_editor/templates/error.html @@ -6,7 +6,7 @@

Autoreply Editor

-

{{ result }}

+

{{ error }}

Go back

diff --git a/autoreply_editor/templates/index.html b/autoreply_editor/templates/index.html index 2009ad7..212ab0f 100644 --- a/autoreply_editor/templates/index.html +++ b/autoreply_editor/templates/index.html @@ -6,33 +6,11 @@

Autoreply Editor

-

Toggle autoreply message

+ -

- The autoreply is {%if qmail_status%}active{%else%}inactive{%endif%} -

-
- - {% if qmail_status %} - - - {% else %} - - - {% endif %} -
- -

Change autoreply message

- -

- Below you can see the current text of your autoreply message. You - can edit and update it directly below: -

-
- - -
- -
diff --git a/autoreply_editor/templates/update.html b/autoreply_editor/templates/update.html new file mode 100644 index 0000000..1f20dca --- /dev/null +++ b/autoreply_editor/templates/update.html @@ -0,0 +1,14 @@ + + + Autoreply Editor + + + +

Autoreply Editor

+ +

{{ result }}

+ +

Go back

+ + + diff --git a/autoreply_editor/templates/user.html b/autoreply_editor/templates/user.html new file mode 100644 index 0000000..2009ad7 --- /dev/null +++ b/autoreply_editor/templates/user.html @@ -0,0 +1,38 @@ + + + Autoreply Editor + + + +

Autoreply Editor

+ +

Toggle autoreply message

+ +

+ The autoreply is {%if qmail_status%}active{%else%}inactive{%endif%} +

+
+ + {% if qmail_status %} + + + {% else %} + + + {% endif %} +
+ +

Change autoreply message

+ +

+ Below you can see the current text of your autoreply message. You + can edit and update it directly below: +

+
+ + +
+ +
+ +