"""Provide basic settings and views""" import re from pathlib import Path from flask import request, render_template from flask_basicauth import BasicAuth 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(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 @app.route("/") def index(): 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")), ) @app.route("/", methods=["POST"]) def index_post(): 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!" if request.form["action"] == "qmail": result = f"Success: the autoreply is now {request.form['status']}." try: return render_template("result.html", result=result) except UnboundLocalError: return render_template("result.html", result="Something went terribly wrong!")