create .filter file from template, and make maildrop command in .qmail variable depending on user

This commit is contained in:
2021-12-23 12:04:18 +01:00
parent 61548211ed
commit 002512cf20
5 changed files with 44 additions and 19 deletions

1
.gitignore vendored
View File

@@ -147,3 +147,4 @@ config.py
inventory.txt inventory.txt
ansible/host_vars/* ansible/host_vars/*
!.keep !.keep
TODO.md

View File

@@ -7,23 +7,23 @@ 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
from jinja2 import Template
from autoreply_editor import app from autoreply_editor import app
basic_auth = BasicAuth(app) basic_auth = BasicAuth(app)
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(get_qmailfile(app.config.get("MAIL_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(
r"^\|maildrop \$HOME/\.filter-autoreply$", qmailfile.read(), re.MULTILINE rf"^{re.escape(get_maildropline(user))}$", 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,10 +31,11 @@ def qmail_status(user):
# filter is deactivated (commented) # filter is deactivated (commented)
if re.search( if re.search(
r"^#\|maildrop \$HOME/\.filter-autoreply$", rf"^#{re.escape(get_maildropline(user))}$",
qmailfile.read(), qmailfile.read(),
re.MULTILINE, re.MULTILINE,
): ):
# if f"#{get_maildropline(user)}" in qmailfile.read():
return False return False
else: else:
initialise = True initialise = True
@@ -42,16 +43,16 @@ def qmail_status(user):
# 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(app.config.get("MAIL_USER")), mode="a", encoding="utf8" get_qmailfile(user), mode="a", encoding="utf8"
) as qmailfile: ) as qmailfile:
qmailfile.write("#|maildrop $HOME/.filter-autoreply") qmailfile.write(f"#{get_maildropline(user)}")
return False return False
def get_messagefile(user): def get_messagefile(user):
"""Return message file of user, and create if necessary""" """Return message file of user, and create if necessary"""
filepath = f"message-{user}.txt" filepath = f"message-{user.split('@')[0]}.txt"
if not os.path.isfile(filepath): if not os.path.isfile(filepath):
Path(filepath).touch() Path(filepath).touch()
@@ -61,16 +62,39 @@ def get_messagefile(user):
def get_qmailfile(user): def get_qmailfile(user):
"""Return qmail file of user, and create if necessary based on default qmail""" """Return qmail file of user, and create if necessary based on default qmail"""
qmail_base = f"{str(Path.home())}/.qmail" qmail_base = f"{str(Path.home())}/.qmail"
filepath = f"{qmail_base}-{user}" filepath = f"{qmail_base}-{user.split('@')[0]}"
if not os.path.isfile(filepath): if not os.path.isfile(filepath):
shutil.copy(f"{qmail_base}-default", filepath) shutil.copy(f"{qmail_base}-default", filepath)
return 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):
return f"|maildrop {get_filterfile(user, 'FOOBAR')}"
@app.route("/") @app.route("/")
def index(): def index():
with open(get_messagefile("user"), "r", encoding="utf-8") as messagefile: 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(
@@ -85,7 +109,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(get_messagefile("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!"
@@ -106,7 +130,7 @@ def index_post():
for line in qmailfile: for line in qmailfile:
print( print(
line.replace( line.replace(
f"{preis}{maildrop_line}", f"{preshould}{maildrop_line}" f"{preis}{get_maildropline(app.config.get('MAIL_USER'))}", f"{preshould}{get_maildropline(app.config.get('MAIL_USER'))}"
), ),
end="", end="",
) )

View File

@@ -0,0 +1,5 @@
logfile "$HOME/mailfilter-autoreply-{{ user }}.log"
FROM="{{ name }} <{{ email }}>"
to "| mailbot -T reply -t {{ messagefile }} -d $HOME/autoresponders/autoresponsedb-{{ user }} -N -A 'From: $FROM' /var/qmail/bin/qmail-inject -f ''"

View File

@@ -1,6 +0,0 @@
Hello,
This is my autoreply message.
Best,
Me

View File

@@ -1,3 +1,4 @@
Flask Flask
Flask-BasicAuth Flask-BasicAuth
gunicorn gunicorn
jinja2