integrate multi-user approach, based on pre-defined list of tuples

This commit is contained in:
2021-12-23 13:25:17 +01:00
parent 67b72d935d
commit ee5d179922
5 changed files with 115 additions and 56 deletions

View File

@@ -5,7 +5,7 @@ import os
import re
import shutil
from pathlib import Path
from flask import request, render_template
from flask import abort, request, render_template
from flask_basicauth import BasicAuth
from jinja2 import Template
from autoreply_editor import app
@@ -49,9 +49,21 @@ def qmail_status(user):
return False
def check_user(user, output=False):
"""Check if user exists"""
fulluser = [item for item in app.config.get("USERS") if item[1].split('@')[0] == user]
if not fulluser:
return False
else:
if output:
return fulluser[0]
else:
return True
def get_messagefile(user):
"""Return message file of user, and create if necessary"""
filepath = f"message-{user.split('@')[0]}.txt"
filepath = f"message-{user}.txt"
if not os.path.isfile(filepath):
Path(filepath).touch()
@@ -61,25 +73,25 @@ def get_messagefile(user):
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]}"
filepath = f"{qmail_base}-{user}"
if not os.path.isfile(filepath):
shutil.copy(f"{qmail_base}-default", filepath)
return filepath
def get_filterfile(user, name):
def get_filterfile(user):
"""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]}"
filepath = f"{filter_base}-{user}"
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,
email=check_user(user, output=True)[1],
user=user,
name=check_user(user, output=True)[0],
messagefile=get_messagefile(user),
)
@@ -94,7 +106,7 @@ 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')}"
base = f"|maildrop {get_filterfile(user)}"
# If the filter should be inactive, we comment it
if not active:
base = f"#{base}"
@@ -108,26 +120,42 @@ def get_maildropline(user, active=True, regex=False):
@app.route("/")
def index():
"""Index page"""
"""Index page listing available users"""
return render_template(
"index.html",
users=app.config.get("USERS"),
)
@app.route("/user/<user>")
def user_status(user):
"""Status for specific user"""
# Check if user is valid
if not check_user(user):
abort(404)
with open(
get_messagefile(app.config.get("MAIL_USER")), "r", encoding="utf-8"
get_messagefile(user), "r", encoding="utf-8"
) as messagefile:
message = messagefile.read()
return render_template(
"index.html",
"user.html",
message=message,
qmail_status=qmail_status(app.config.get("MAIL_USER")),
qmail_status=qmail_status(user),
)
@app.route("/", methods=["POST"])
def index_post():
"""Index page with POST request"""
@app.route("/user/<user>", methods=["POST"])
def user_update(user):
"""Update user with POST request"""
# Check if user is valid
if not check_user(user):
abort(404)
if request.form["action"] == "message":
input_message = request.form["message"]
with open(
get_messagefile(app.config.get("MAIL_USER")), "w", encoding="utf-8"
get_messagefile(user), "w", encoding="utf-8"
) as messagefile:
messagefile.write(str(input_message))
result = "Success: The autoreply message has been updated!"
@@ -135,18 +163,14 @@ def index_post():
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"))
state_current = get_maildropline(user, active=False)
state_desired = get_maildropline(user)
else:
state_current = get_maildropline(app.config.get("MAIL_USER"))
state_desired = get_maildropline(
app.config.get("MAIL_USER"), active=False
)
state_current = get_maildropline(user)
state_desired = get_maildropline(user, active=False)
with fileinput.FileInput(
get_qmailfile(app.config.get("MAIL_USER")),
get_qmailfile(user),
inplace=True,
backup=".bak",
) as qmailfile:
@@ -159,6 +183,11 @@ def index_post():
result = f"Success: the autoreply is now {request.form['status']}."
try:
return render_template("result.html", result=result)
return render_template("update.html", user=user, result=result)
except UnboundLocalError:
return render_template("result.html", result="Something went terribly wrong!")
return render_template("update.html", user=user, result="Something went terribly wrong!")
@app.errorhandler(404)
def page_not_found(e):
return render_template('error.html', error=e), 404

View File

@@ -6,7 +6,7 @@
<body>
<h1>Autoreply Editor</h1>
<p>{{ result }}</p>
<p>{{ error }}</p>
<p><a href="/">Go back</a></p>

View File

@@ -6,33 +6,11 @@
<body>
<h1>Autoreply Editor</h1>
<h2>Toggle autoreply message</h2>
<ul>
{% for user in users %}
<li><a href="/user/{{ user[1].split('@')[0] }}">{{ user[0] }} {{"<"}}{{ user[1] }}{{">"}}</a></li>
{% endfor %}
</ul>
<p>
The autoreply is <strong>{%if qmail_status%}active{%else%}inactive{%endif%}</strong>
</p>
<form method="POST">
<input type="hidden" name="action" value="qmail" />
{% if qmail_status %}
<input type="hidden" name="status" value="off" />
<input type="submit" value="Deactivate">
{% else %}
<input type="hidden" name="status" value="on" />
<input type="submit" value="Activate">
{% endif %}
</form>
<h2>Change autoreply message</h2>
<p>
Below you can see the current text of your autoreply message. You
can edit and update it directly below:
</p>
<form method="POST">
<input type="hidden" name="action" value="message" />
<textarea name="message" cols="120" rows="20">{{ message }}</textarea>
<br />
<input type="submit" value="Update">
</form>
</body>
</html>

View File

@@ -0,0 +1,14 @@
<!DOCTYPE html>
<head>
<title>Autoreply Editor</title>
</head>
<body>
<h1>Autoreply Editor</h1>
<p>{{ result }}</p>
<p><a href="/user/{{ user }}">Go back</a></p>
</body>
</html>

View File

@@ -0,0 +1,38 @@
<!DOCTYPE html>
<head>
<title>Autoreply Editor</title>
</head>
<body>
<h1>Autoreply Editor</h1>
<h2>Toggle autoreply message</h2>
<p>
The autoreply is <strong>{%if qmail_status%}active{%else%}inactive{%endif%}</strong>
</p>
<form method="POST">
<input type="hidden" name="action" value="qmail" />
{% if qmail_status %}
<input type="hidden" name="status" value="off" />
<input type="submit" value="Deactivate">
{% else %}
<input type="hidden" name="status" value="on" />
<input type="submit" value="Activate">
{% endif %}
</form>
<h2>Change autoreply message</h2>
<p>
Below you can see the current text of your autoreply message. You
can edit and update it directly below:
</p>
<form method="POST">
<input type="hidden" name="action" value="message" />
<textarea name="message" cols="120" rows="20">{{ message }}</textarea>
<br />
<input type="submit" value="Update">
</form>
</body>
</html>