embed active toggle get_maildropline

This commit is contained in:
2021-12-23 12:36:15 +01:00
parent 6cbdfcecaa
commit 5df9c0de93
3 changed files with 46 additions and 25 deletions

View File

@@ -1,8 +1,9 @@
"""Initialise flask app"""
from flask import Flask from flask import Flask
app = Flask(__name__) app = Flask(__name__)
import autoreply_editor.main import autoreply_editor.main #pylint: disable=wrong-import-position
app.config['BASIC_AUTH_FORCE'] = True app.config['BASIC_AUTH_FORCE'] = True
app.config.from_pyfile("../config.py") app.config.from_pyfile("../config.py")

View File

@@ -12,18 +12,19 @@ from autoreply_editor import app
basic_auth = BasicAuth(app) basic_auth = BasicAuth(app)
def qmail_status(user): def qmail_status(user):
"""Find out whether the filter is currently activated for the given mail user""" """Find out whether the filter is currently activated for the given mail user"""
with open(get_qmailfile(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 # trigger to check whether we have to re-open the file for initialisation
initialise = False initialise = False
# try to find active filter # try to find active filter
if re.search( if re.search(
rf"^{get_maildropline(user, regex=True)}$", qmailfile.read(), re.MULTILINE rf"{get_maildropline(user, active=True, regex=True)}",
qmailfile.read(),
re.MULTILINE,
): ):
# if get_maildropline(user) in qmailfile.read():
return True return True
else: else:
# jump back to top of file because we've read it in the "if" # jump back to top of file because we've read it in the "if"
@@ -31,21 +32,19 @@ def qmail_status(user):
# filter is deactivated (commented) # filter is deactivated (commented)
if re.search( if re.search(
rf"^#{get_maildropline(user, regex=True)}$", rf"{get_maildropline(user, active=False, regex=True)}",
qmailfile.read(), qmailfile.read(),
re.MULTILINE, re.MULTILINE,
): ):
# if f"#{get_maildropline(user)}" in qmailfile.read():
return False return False
# The maildrop line does not exist in the expected form
else: else:
initialise = True initialise = True
# Append a commented filter command to the file # Append a commented filter command to the file
if initialise: if initialise:
with open( with open(get_qmailfile(user), mode="a", encoding="utf8") as qmailfile:
get_qmailfile(user), mode="a", encoding="utf8" qmailfile.write(f"{get_maildropline(user, active=False)}")
) as qmailfile:
qmailfile.write(f"#{get_maildropline(user)}")
return False return False
@@ -74,12 +73,14 @@ def get_filterfile(user, name):
filter_base = f"{str(Path.home())}/.filter-autoreply" filter_base = f"{str(Path.home())}/.filter-autoreply"
filepath = f"{filter_base}-{user.split('@')[0]}" filepath = f"{filter_base}-{user.split('@')[0]}"
if not os.path.isfile(filepath): if not os.path.isfile(filepath):
with open(os.path.join(app.root_path, "templates/filterfile.j2"), encoding="utf-8") as template: with open(
os.path.join(app.root_path, "templates/filterfile.j2"), encoding="utf-8"
) as template:
filterconfig = Template(template.read()).render( filterconfig = Template(template.read()).render(
email=user, email=user,
user=user.split('@')[0], user=user.split("@")[0],
name=name, name=name,
messagefile=get_messagefile(user) messagefile=get_messagefile(user),
) )
with open(filepath, "w", encoding="utf-8") as filterfile: with open(filepath, "w", encoding="utf-8") as filterfile:
@@ -88,16 +89,29 @@ def get_filterfile(user, name):
return filepath return filepath
def get_maildropline(user, regex=False): 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: if regex:
return re.escape(f"|maildrop {get_filterfile(user, 'FOOBAR')}") return f"^{re.escape(base)}$"
else: else:
return f"|maildrop {get_filterfile(user, 'FOOBAR')}" return base
@app.route("/") @app.route("/")
def index(): def index():
with open(get_messagefile(app.config.get("MAIL_USER")), "r", encoding="utf-8") as messagefile: """Index page"""
with open(
get_messagefile(app.config.get("MAIL_USER")), "r", encoding="utf-8"
) as messagefile:
message = messagefile.read() message = messagefile.read()
return render_template( return render_template(
@@ -109,21 +123,28 @@ def index():
@app.route("/", methods=["POST"]) @app.route("/", methods=["POST"])
def index_post(): def index_post():
"""Index page with POST request"""
if request.method == "POST": if request.method == "POST":
if request.form["action"] == "message": if request.form["action"] == "message":
input_message = request.form["message"] input_message = request.form["message"]
with open(get_messagefile(app.config.get("MAIL_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)) messagefile.write(str(input_message))
result = "Success: The autoreply message has been updated!" result = "Success: The autoreply message has been updated!"
if request.form["action"] == "qmail": if request.form["action"] == "qmail":
# define whether to set a comment # define whether to set a comment
if request.form["status"] == "on": if request.form["status"] == "on":
preis = "#" state_current = get_maildropline(
preshould = "" app.config.get("MAIL_USER"), active=False
)
state_desired = get_maildropline(app.config.get("MAIL_USER"))
else: else:
preis = "" state_current = get_maildropline(app.config.get("MAIL_USER"))
preshould = "#" state_desired = get_maildropline(
app.config.get("MAIL_USER"), active=False
)
with fileinput.FileInput( with fileinput.FileInput(
get_qmailfile(app.config.get("MAIL_USER")), get_qmailfile(app.config.get("MAIL_USER")),
@@ -132,9 +153,7 @@ def index_post():
) as qmailfile: ) as qmailfile:
for line in qmailfile: for line in qmailfile:
print( print(
line.replace( line.replace(state_current, state_desired),
f"{preis}{get_maildropline(app.config.get('MAIL_USER'))}", f"{preshould}{get_maildropline(app.config.get('MAIL_USER'))}"
),
end="", end="",
) )

View File

@@ -1,3 +1,4 @@
"""WSGI for Flask app"""
from autoreply_editor import app from autoreply_editor import app
if __name__ == "__main__": if __name__ == "__main__":