Files
autoreply-editor/autoreply_editor/main.py

166 lines
5.2 KiB
Python

"""Provide basic settings and views"""
import fileinput
import os
import re
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)
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, active=True, regex=True)}",
qmailfile.read(),
re.MULTILINE,
):
return True
else:
# jump back to top of file because we've read it in the "if"
qmailfile.seek(0)
# filter is deactivated (commented)
if re.search(
rf"{get_maildropline(user, active=False, regex=True)}",
qmailfile.read(),
re.MULTILINE,
):
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, active=False)}")
return False
def get_messagefile(user):
"""Return message file of user, and create if necessary"""
filepath = f"message-{user.split('@')[0]}.txt"
if not os.path.isfile(filepath):
Path(filepath).touch()
return filepath
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]}"
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, 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 f"^{re.escape(base)}$"
else:
return base
@app.route("/")
def index():
"""Index page"""
with open(
get_messagefile(app.config.get("MAIL_USER")), "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():
"""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:
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":
state_current = get_maildropline(
app.config.get("MAIL_USER"), active=False
)
state_desired = get_maildropline(app.config.get("MAIL_USER"))
else:
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")),
inplace=True,
backup=".bak",
) as qmailfile:
for line in qmailfile:
print(
line.replace(state_current, state_desired),
end="",
)
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!")