2021-05-19 09:30:01 +02:00
|
|
|
"""Provide basic settings and views"""
|
|
|
|
|
|
2021-12-19 22:25:09 +01:00
|
|
|
import fileinput
|
2021-12-20 18:46:26 +01:00
|
|
|
import os
|
2021-12-19 22:00:41 +01:00
|
|
|
import re
|
2021-12-20 18:46:26 +01:00
|
|
|
import shutil
|
2021-12-19 22:00:41 +01:00
|
|
|
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:25:09 +01:00
|
|
|
maildrop_line = "|maildrop $HOME/.filter-autoreply"
|
2021-12-19 22:00:41 +01:00
|
|
|
|
2021-12-20 18:46:26 +01: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"""
|
2021-12-20 18:46:26 +01:00
|
|
|
with open(get_qmailfile(app.config.get("MAIL_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
|
2021-12-19 22:00:41 +01:00
|
|
|
):
|
|
|
|
|
return True
|
2021-12-20 18:46:26 +01:00
|
|
|
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(
|
|
|
|
|
r"^#\|maildrop \$HOME/\.filter-autoreply$",
|
|
|
|
|
qmailfile.read(),
|
|
|
|
|
re.MULTILINE,
|
|
|
|
|
):
|
|
|
|
|
return False
|
|
|
|
|
else:
|
|
|
|
|
initialise = True
|
|
|
|
|
|
|
|
|
|
# Append a commented filter command to the file
|
|
|
|
|
if initialise:
|
|
|
|
|
with open(
|
|
|
|
|
get_qmailfile(app.config.get("MAIL_USER")), mode="a", encoding="utf8"
|
|
|
|
|
) as qmailfile:
|
|
|
|
|
qmailfile.write("#|maildrop $HOME/.filter-autoreply")
|
|
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_messagefile(user):
|
|
|
|
|
"""Return message file of user, and create if necessary"""
|
|
|
|
|
filepath = f"message-{user}.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}"
|
|
|
|
|
if not os.path.isfile(filepath):
|
|
|
|
|
shutil.copy(f"{qmail_base}-default", filepath)
|
|
|
|
|
|
|
|
|
|
return filepath
|
2021-12-19 22:00:41 +01:00
|
|
|
|
|
|
|
|
|
2021-05-18 19:15:49 +02:00
|
|
|
@app.route("/")
|
|
|
|
|
def index():
|
2021-12-20 18:46:26 +01:00
|
|
|
with open(get_messagefile("user"), "r", encoding="utf-8") as messagefile:
|
2021-12-19 22:00:41 +01:00
|
|
|
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"]
|
2021-12-20 18:46:26 +01:00
|
|
|
with open(get_messagefile("user"), "w", encoding="utf-8") as messagefile:
|
2021-12-19 22:00:41 +01:00
|
|
|
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":
|
2021-12-19 22:25:09 +01:00
|
|
|
# define whether to set a comment
|
|
|
|
|
if request.form["status"] == "on":
|
|
|
|
|
preis = "#"
|
|
|
|
|
preshould = ""
|
|
|
|
|
else:
|
|
|
|
|
preis = ""
|
|
|
|
|
preshould = "#"
|
|
|
|
|
|
|
|
|
|
with fileinput.FileInput(
|
2021-12-20 18:46:26 +01:00
|
|
|
get_qmailfile(app.config.get("MAIL_USER")),
|
2021-12-19 22:25:09 +01:00
|
|
|
inplace=True,
|
|
|
|
|
backup=".bak",
|
2021-12-20 18:46:26 +01:00
|
|
|
) as qmailfile:
|
|
|
|
|
for line in qmailfile:
|
2021-12-19 22:25:09 +01:00
|
|
|
print(
|
|
|
|
|
line.replace(
|
|
|
|
|
f"{preis}{maildrop_line}", f"{preshould}{maildrop_line}"
|
|
|
|
|
),
|
|
|
|
|
end="",
|
|
|
|
|
)
|
|
|
|
|
|
2021-12-19 22:00:41 +01:00
|
|
|
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!")
|