integrate multi-user approach, based on pre-defined list of tuples
This commit is contained in:
@@ -5,7 +5,7 @@ import os
|
|||||||
import re
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from flask import request, render_template
|
from flask import abort, request, render_template
|
||||||
from flask_basicauth import BasicAuth
|
from flask_basicauth import BasicAuth
|
||||||
from jinja2 import Template
|
from jinja2 import Template
|
||||||
from autoreply_editor import app
|
from autoreply_editor import app
|
||||||
@@ -49,9 +49,21 @@ def qmail_status(user):
|
|||||||
return False
|
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):
|
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.split('@')[0]}.txt"
|
filepath = f"message-{user}.txt"
|
||||||
if not os.path.isfile(filepath):
|
if not os.path.isfile(filepath):
|
||||||
Path(filepath).touch()
|
Path(filepath).touch()
|
||||||
|
|
||||||
@@ -61,25 +73,25 @@ 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.split('@')[0]}"
|
filepath = f"{qmail_base}-{user}"
|
||||||
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):
|
def get_filterfile(user):
|
||||||
"""Return .filter-autoreply file of user, and create if necessary"""
|
"""Return .filter-autoreply file of user, and create if necessary"""
|
||||||
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}"
|
||||||
if not os.path.isfile(filepath):
|
if not os.path.isfile(filepath):
|
||||||
with open(
|
with open(
|
||||||
os.path.join(app.root_path, "templates/filterfile.j2"), encoding="utf-8"
|
os.path.join(app.root_path, "templates/filterfile.j2"), encoding="utf-8"
|
||||||
) as template:
|
) as template:
|
||||||
filterconfig = Template(template.read()).render(
|
filterconfig = Template(template.read()).render(
|
||||||
email=user,
|
email=check_user(user, output=True)[1],
|
||||||
user=user.split("@")[0],
|
user=user,
|
||||||
name=name,
|
name=check_user(user, output=True)[0],
|
||||||
messagefile=get_messagefile(user),
|
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
|
Define the line calling maildrop, with options to have the line disabled or
|
||||||
in Regex format
|
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 the filter should be inactive, we comment it
|
||||||
if not active:
|
if not active:
|
||||||
base = f"#{base}"
|
base = f"#{base}"
|
||||||
@@ -108,26 +120,42 @@ def get_maildropline(user, active=True, regex=False):
|
|||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def index():
|
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(
|
with open(
|
||||||
get_messagefile(app.config.get("MAIL_USER")), "r", encoding="utf-8"
|
get_messagefile(user), "r", encoding="utf-8"
|
||||||
) as messagefile:
|
) as messagefile:
|
||||||
message = messagefile.read()
|
message = messagefile.read()
|
||||||
|
|
||||||
return render_template(
|
return render_template(
|
||||||
"index.html",
|
"user.html",
|
||||||
message=message,
|
message=message,
|
||||||
qmail_status=qmail_status(app.config.get("MAIL_USER")),
|
qmail_status=qmail_status(user),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/", methods=["POST"])
|
@app.route("/user/<user>", methods=["POST"])
|
||||||
def index_post():
|
def user_update(user):
|
||||||
"""Index page with POST request"""
|
"""Update user with POST request"""
|
||||||
|
# Check if user is valid
|
||||||
|
if not check_user(user):
|
||||||
|
abort(404)
|
||||||
|
|
||||||
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(app.config.get("MAIL_USER")), "w", encoding="utf-8"
|
get_messagefile(user), "w", encoding="utf-8"
|
||||||
) as messagefile:
|
) 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!"
|
||||||
@@ -135,18 +163,14 @@ def index_post():
|
|||||||
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":
|
||||||
state_current = get_maildropline(
|
state_current = get_maildropline(user, active=False)
|
||||||
app.config.get("MAIL_USER"), active=False
|
state_desired = get_maildropline(user)
|
||||||
)
|
|
||||||
state_desired = get_maildropline(app.config.get("MAIL_USER"))
|
|
||||||
else:
|
else:
|
||||||
state_current = get_maildropline(app.config.get("MAIL_USER"))
|
state_current = get_maildropline(user)
|
||||||
state_desired = get_maildropline(
|
state_desired = get_maildropline(user, active=False)
|
||||||
app.config.get("MAIL_USER"), active=False
|
|
||||||
)
|
|
||||||
|
|
||||||
with fileinput.FileInput(
|
with fileinput.FileInput(
|
||||||
get_qmailfile(app.config.get("MAIL_USER")),
|
get_qmailfile(user),
|
||||||
inplace=True,
|
inplace=True,
|
||||||
backup=".bak",
|
backup=".bak",
|
||||||
) as qmailfile:
|
) as qmailfile:
|
||||||
@@ -159,6 +183,11 @@ def index_post():
|
|||||||
result = f"Success: the autoreply is now {request.form['status']}."
|
result = f"Success: the autoreply is now {request.form['status']}."
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return render_template("result.html", result=result)
|
return render_template("update.html", user=user, result=result)
|
||||||
except UnboundLocalError:
|
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
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
<body>
|
<body>
|
||||||
<h1>Autoreply Editor</h1>
|
<h1>Autoreply Editor</h1>
|
||||||
|
|
||||||
<p>{{ result }}</p>
|
<p>{{ error }}</p>
|
||||||
|
|
||||||
<p><a href="/">Go back</a></p>
|
<p><a href="/">Go back</a></p>
|
||||||
|
|
||||||
@@ -6,33 +6,11 @@
|
|||||||
<body>
|
<body>
|
||||||
<h1>Autoreply Editor</h1>
|
<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>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
14
autoreply_editor/templates/update.html
Normal file
14
autoreply_editor/templates/update.html
Normal 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>
|
||||||
38
autoreply_editor/templates/user.html
Normal file
38
autoreply_editor/templates/user.html
Normal 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>
|
||||||
Reference in New Issue
Block a user