Files
autoreply-editor/autoreply_editor/main.py

51 lines
1.6 KiB
Python
Raw Normal View History

2021-05-19 09:30:01 +02:00
"""Provide basic settings and views"""
2021-12-19 22:00:41 +01:00
import re
from pathlib import Path
from flask import request, render_template
2021-05-19 09:17:41 +02:00
from flask_basicauth import BasicAuth
2021-05-19 09:30:01 +02:00
from autoreply_editor import app
2021-05-19 09:17:41 +02:00
basic_auth = BasicAuth(app)
2021-05-18 19:15:49 +02:00
2021-12-19 22:00:41 +01:00
def qmail_status(user):
"""Find out whether the filter is currently activated for the given mail user"""
with open(f"{str(Path.home())}/.qmail-{user}", encoding="utf8") as dotqmail:
if not re.search(
r"^\|maildrop \$HOME/\.filter-autoreply$", dotqmail.read(), re.MULTILINE
):
return False
else:
return True
2021-05-18 19:15:49 +02:00
@app.route("/")
def index():
2021-12-19 22:00:41 +01:00
with open(app.config.get("MESSAGE_FILE"), "r", encoding="utf-8") as messagefile:
message = messagefile.read()
return render_template(
"index.html",
message=message,
qmail_status=qmail_status(app.config.get("MAIL_USER")),
)
2021-05-18 19:15:49 +02:00
2021-05-18 19:32:59 +02:00
2021-12-19 22:00:41 +01:00
@app.route("/", methods=["POST"])
2021-05-18 19:32:59 +02:00
def index_post():
2021-12-19 22:00:41 +01:00
if request.method == "POST":
if request.form["action"] == "message":
input_message = request.form["message"]
with open(app.config.get("MESSAGE_FILE"), "w", encoding="utf-8") as messagefile:
messagefile.write(str(input_message))
result = "Success: The autoreply message has been updated!"
2021-05-18 19:53:31 +02:00
2021-12-19 22:00:41 +01:00
if request.form["action"] == "qmail":
result = f"Success: the autoreply is now {request.form['status']}."
2021-05-19 10:23:47 +02:00
2021-12-19 22:00:41 +01:00
try:
return render_template("result.html", result=result)
except UnboundLocalError:
return render_template("result.html", result="Something went terribly wrong!")