diff --git a/.gitignore b/.gitignore index 0700268..5796b11 100644 --- a/.gitignore +++ b/.gitignore @@ -147,3 +147,4 @@ config.py inventory.txt ansible/host_vars/* !.keep +TODO.md diff --git a/autoreply_editor/main.py b/autoreply_editor/main.py index 039a2a0..bb65606 100644 --- a/autoreply_editor/main.py +++ b/autoreply_editor/main.py @@ -7,23 +7,23 @@ import shutil from pathlib import Path from flask import request, render_template from flask_basicauth import BasicAuth +from jinja2 import Template from autoreply_editor import app basic_auth = BasicAuth(app) -maildrop_line = "|maildrop $HOME/.filter-autoreply" - - def qmail_status(user): """Find out whether the filter is currently activated for the given mail user""" - with open(get_qmailfile(app.config.get("MAIL_USER")), encoding="utf8") as qmailfile: + 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( - r"^\|maildrop \$HOME/\.filter-autoreply$", qmailfile.read(), re.MULTILINE + rf"^{re.escape(get_maildropline(user))}$", 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,10 +31,11 @@ def qmail_status(user): # filter is deactivated (commented) if re.search( - r"^#\|maildrop \$HOME/\.filter-autoreply$", + rf"^#{re.escape(get_maildropline(user))}$", qmailfile.read(), re.MULTILINE, ): + # if f"#{get_maildropline(user)}" in qmailfile.read(): return False else: initialise = True @@ -42,16 +43,16 @@ def qmail_status(user): # Append a commented filter command to the file if initialise: with open( - get_qmailfile(app.config.get("MAIL_USER")), mode="a", encoding="utf8" + get_qmailfile(user), mode="a", encoding="utf8" ) as qmailfile: - qmailfile.write("#|maildrop $HOME/.filter-autoreply") + qmailfile.write(f"#{get_maildropline(user)}") return False def get_messagefile(user): """Return message file of user, and create if necessary""" - filepath = f"message-{user}.txt" + filepath = f"message-{user.split('@')[0]}.txt" if not os.path.isfile(filepath): Path(filepath).touch() @@ -61,16 +62,39 @@ 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}" + filepath = f"{qmail_base}-{user.split('@')[0]}" if not os.path.isfile(filepath): shutil.copy(f"{qmail_base}-default", filepath) return filepath +def get_filterfile(user, name): + """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]}" + 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, + messagefile=get_messagefile(user) + ) + + with open(filepath, "w", encoding="utf-8") as filterfile: + filterfile.write(filterconfig) + + return filepath + + +def get_maildropline(user): + return f"|maildrop {get_filterfile(user, 'FOOBAR')}" + + @app.route("/") def index(): - with open(get_messagefile("user"), "r", encoding="utf-8") as messagefile: + with open(get_messagefile(app.config.get("MAIL_USER")), "r", encoding="utf-8") as messagefile: message = messagefile.read() return render_template( @@ -85,7 +109,7 @@ def index_post(): if request.method == "POST": if request.form["action"] == "message": input_message = request.form["message"] - with open(get_messagefile("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!" @@ -106,7 +130,7 @@ def index_post(): for line in qmailfile: print( line.replace( - f"{preis}{maildrop_line}", f"{preshould}{maildrop_line}" + f"{preis}{get_maildropline(app.config.get('MAIL_USER'))}", f"{preshould}{get_maildropline(app.config.get('MAIL_USER'))}" ), end="", ) diff --git a/autoreply_editor/templates/filterfile.j2 b/autoreply_editor/templates/filterfile.j2 new file mode 100644 index 0000000..3284dae --- /dev/null +++ b/autoreply_editor/templates/filterfile.j2 @@ -0,0 +1,5 @@ +logfile "$HOME/mailfilter-autoreply-{{ user }}.log" + +FROM="{{ name }} <{{ email }}>" + +to "| mailbot -T reply -t {{ messagefile }} -d $HOME/autoresponders/autoresponsedb-{{ user }} -N -A 'From: $FROM' /var/qmail/bin/qmail-inject -f ''" diff --git a/message.txt.sample b/message.txt.sample deleted file mode 100644 index bc8e8b8..0000000 --- a/message.txt.sample +++ /dev/null @@ -1,6 +0,0 @@ -Hello, - -This is my autoreply message. - -Best, -Me diff --git a/requirements.txt b/requirements.txt index 9550338..4cd0436 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ Flask Flask-BasicAuth gunicorn +jinja2