create .qmail and message file if needed

This commit is contained in:
2021-12-20 18:46:26 +01:00
parent 3c1fbf2cf2
commit 61548211ed
2 changed files with 57 additions and 15 deletions

2
.gitignore vendored
View File

@@ -142,7 +142,7 @@ dmypy.json
cython_debug/ cython_debug/
# Project specific # Project specific
message.txt message*.txt
config.py config.py
inventory.txt inventory.txt
ansible/host_vars/* ansible/host_vars/*

View File

@@ -1,7 +1,9 @@
"""Provide basic settings and views""" """Provide basic settings and views"""
import fileinput import fileinput
import os
import re import re
import shutil
from pathlib import Path from pathlib import Path
from flask import request, render_template from flask import request, render_template
from flask_basicauth import BasicAuth from flask_basicauth import BasicAuth
@@ -9,24 +11,66 @@ from autoreply_editor import app
basic_auth = BasicAuth(app) basic_auth = BasicAuth(app)
qmail_prefix = f"{str(Path.home())}/.qmail-"
maildrop_line = "|maildrop $HOME/.filter-autoreply" maildrop_line = "|maildrop $HOME/.filter-autoreply"
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(f"{qmail_prefix}{user}", encoding="utf8") as dotqmail: with open(get_qmailfile(app.config.get("MAIL_USER")), encoding="utf8") as qmailfile:
# TODO: RE not necessary here # trigger to check whether we have to re-open the file for initialisation
if not re.search( initialise = False
r"^\|maildrop \$HOME/\.filter-autoreply$", dotqmail.read(), re.MULTILINE
# try to find active filter
if re.search(
r"^\|maildrop \$HOME/\.filter-autoreply$", qmailfile.read(), re.MULTILINE
): ):
return False
else:
return True 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(
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
@app.route("/") @app.route("/")
def index(): def index():
with open(app.config.get("MESSAGE_FILE"), "r", encoding="utf-8") as messagefile: with open(get_messagefile("user"), "r", encoding="utf-8") as messagefile:
message = messagefile.read() message = messagefile.read()
return render_template( return render_template(
@@ -41,9 +85,7 @@ def index_post():
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( with open(get_messagefile("user"), "w", encoding="utf-8") as messagefile:
app.config.get("MESSAGE_FILE"), "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!"
@@ -57,11 +99,11 @@ def index_post():
preshould = "#" preshould = "#"
with fileinput.FileInput( with fileinput.FileInput(
f"{qmail_prefix}{app.config.get('MAIL_USER')}", get_qmailfile(app.config.get("MAIL_USER")),
inplace=True, inplace=True,
backup=".bak", backup=".bak",
) as dotqmail: ) as qmailfile:
for line in dotqmail: for line in qmailfile:
print( print(
line.replace( line.replace(
f"{preis}{maildrop_line}", f"{preshould}{maildrop_line}" f"{preis}{maildrop_line}", f"{preshould}{maildrop_line}"