forked from server/reporter
Initialer Commit
This commit is contained in:
commit
9ec7d6fe8c
10
Dockerfile
Normal file
10
Dockerfile
Normal file
@ -0,0 +1,10 @@
|
||||
FROM alpine:3.8
|
||||
ADD src /app
|
||||
RUN apk add --update --no-cache python3 && \
|
||||
pip3 --no-cache-dir install -r /app/requirements.txt && \
|
||||
adduser -D app
|
||||
USER app
|
||||
EXPOSE 5000
|
||||
ENV FLASK_APP=spammer.py
|
||||
WORKDIR /app
|
||||
CMD ["flask", "run", "-h", "0.0.0.0"]
|
||||
6
docker-compose.yml
Normal file
6
docker-compose.yml
Normal file
@ -0,0 +1,6 @@
|
||||
version: "3"
|
||||
|
||||
services:
|
||||
rzspam:
|
||||
build: .
|
||||
image: docker.wiai.de/fswiai/rzspam:0.1
|
||||
28
src/config.json
Normal file
28
src/config.json
Normal file
@ -0,0 +1,28 @@
|
||||
{
|
||||
"placeholders": {
|
||||
"port": {
|
||||
"type": "text",
|
||||
"desc": "Portnummer/Dienst"
|
||||
},
|
||||
"raum": {
|
||||
"desc": "Raumnummer"
|
||||
},
|
||||
"wlan": {
|
||||
"type": "text",
|
||||
"desc": "Wlan-name",
|
||||
"default": "eduroam"
|
||||
},
|
||||
"anzahl": {
|
||||
"type": "number",
|
||||
"desc": "Anzahl"
|
||||
}
|
||||
},
|
||||
"RZ": {
|
||||
"mail": "fachschaft.wiai@uni-bamberg.de",
|
||||
"templates": {
|
||||
"Portsperre": "liebes RZ, Port {port} ist zu. das ist doof :(",
|
||||
"WLAN": "hallo RZ, in Raum {raum} funkt das WLAN {wlan} nicht. das ist doof :(",
|
||||
"test": "ich bin ein test mit einer zahl: {anzahl}"
|
||||
}
|
||||
}
|
||||
}
|
||||
2
src/requirements.txt
Normal file
2
src/requirements.txt
Normal file
@ -0,0 +1,2 @@
|
||||
Flask==1.0.2
|
||||
Flask-Mail==0.9.1
|
||||
81
src/spammer.py
Normal file
81
src/spammer.py
Normal file
@ -0,0 +1,81 @@
|
||||
import json
|
||||
from collections import namedtuple
|
||||
from string import Formatter
|
||||
|
||||
from flask import Flask
|
||||
from flask import request
|
||||
from flask_mail import Mail, Message
|
||||
|
||||
import templates
|
||||
|
||||
Placeholder = namedtuple("Placeholder", ["name", "type", "desc", "default"])
|
||||
|
||||
def load_config(conf="config.json"):
|
||||
config = json.load(open(conf))
|
||||
placeholders = []
|
||||
for i in config["placeholders"]:
|
||||
p_h = config["placeholders"][i]
|
||||
p = Placeholder(
|
||||
name=i,
|
||||
type=p_h.get("type", "text"),
|
||||
desc=p_h["desc"],
|
||||
default=p_h.get("default"))
|
||||
placeholders.append(p)
|
||||
config.pop("placeholders")
|
||||
flat = {}
|
||||
for org in config:
|
||||
target = config[org]
|
||||
templates = []
|
||||
for issue_name in target["templates"]:
|
||||
text = target["templates"][issue_name]
|
||||
fields = [name for _, name, _, _ in Formatter().parse(text)]
|
||||
flat[f"{org}: {issue_name}"] = {
|
||||
"org": org,
|
||||
"name": issue_name,
|
||||
"mail": config[org]["mail"],
|
||||
"text": text,
|
||||
"placeholders": [i._asdict() for i in placeholders if i.name in fields]
|
||||
}
|
||||
return flat
|
||||
|
||||
MAIL_SERVER = "smtp.uni-bamberg.de"
|
||||
MAIL_PORT = 587
|
||||
#TESTING=True
|
||||
#MAIL_USE_TLS=True
|
||||
#MAIL_USE_SSL=True
|
||||
MAIL_DEBUG=True
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config.from_object(__name__)
|
||||
|
||||
|
||||
|
||||
mail = Mail(app)
|
||||
|
||||
issues = load_config()
|
||||
|
||||
@app.route("/")
|
||||
def index():
|
||||
return templates.format_index(issues)
|
||||
|
||||
@app.route("/send", methods=["POST"])
|
||||
def send():
|
||||
if all([field in request.form for field in ("text", "sender", "target")]):
|
||||
text = request.form["text"]
|
||||
print("all fields present")
|
||||
fields = [name for _, name, _, _ in Formatter().parse(text)]
|
||||
if None in fields:
|
||||
fields.remove(None)
|
||||
if all([field in request.form for field in fields]):
|
||||
values = {field: request.form[field] for field in fields}
|
||||
text = text.format(**values)
|
||||
sender = request.form["sender"]
|
||||
recipients = [request.form["target"]]
|
||||
msg = Message("Störungsmeldung", body=text, sender=sender, recipients=recipients)
|
||||
print(msg)
|
||||
return str(mail.send(msg))
|
||||
print([(field,field in request.form) for field in fields])
|
||||
return "2"
|
||||
print([(field,field in request.form) for field in ("text", "sender", "target")])
|
||||
print(request.form)
|
||||
return "1"
|
||||
76
src/templates.py
Normal file
76
src/templates.py
Normal file
@ -0,0 +1,76 @@
|
||||
import json
|
||||
BASE = """<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>spammer</title>
|
||||
<script>
|
||||
function selection() {
|
||||
var x = document.getElementById("issue").value;
|
||||
var data = JSON.parse(x)
|
||||
console.log(data);
|
||||
var textarea = document.getElementById("text");
|
||||
var emailfield = document.getElementById("sender");
|
||||
textarea.value = data.text;
|
||||
textarea.readOnly = false;
|
||||
emailfield.readOnly = false;
|
||||
var placeholders = document.getElementById("placeholders");
|
||||
placeholders.innerHTML = "";
|
||||
var target = document.createElement("input");
|
||||
target.setAttribute("name", "target");
|
||||
target.setAttribute("value", data.mail);
|
||||
target.setAttribute("hidden", "true");
|
||||
placeholders.appendChild(target);
|
||||
for (i in data.placeholders) {
|
||||
var placeholder = data.placeholders[i];
|
||||
var x = document.createElement("input");
|
||||
x.setAttribute("type", placeholder.type);
|
||||
x.setAttribute("placeholder", placeholder.desc);
|
||||
x.setAttribute("name", placeholder.name);
|
||||
x.setAttribute("id", placeholder.name);
|
||||
x.setAttribute("required", "true");
|
||||
if (placeholder.default !== null){
|
||||
x.setAttribute("value", placeholder.default);
|
||||
}
|
||||
var y = document.createElement("label");
|
||||
y.setAttribute("for", placeholder.name);
|
||||
y.innerHTML = placeholder.desc;
|
||||
var z = document.createElement("p");
|
||||
z.appendChild(y);
|
||||
z.appendChild(x);
|
||||
placeholders.appendChild(z);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
"""
|
||||
|
||||
INDEX = """<h1>Welcome</h1>
|
||||
<p>choose an issue, fill the placeholders, enter your uni-mail, then hit send</p>
|
||||
|
||||
<form action="send" method="post">
|
||||
<p>
|
||||
<select name="issue" required onchange="selection()" id="issue">
|
||||
<option selected disabled="true">---</option>
|
||||
{issues}
|
||||
</select>
|
||||
</p>
|
||||
<p><textarea id="text" readonly required name="text"></textarea></p>
|
||||
<p>
|
||||
<label for="sender">absender:</label>
|
||||
<input type="email" required="true" name="sender" id="sender" placeholder="sender email" readonly>
|
||||
</p>
|
||||
<div id="placeholders" ></div>
|
||||
<input type="submit">
|
||||
</form>
|
||||
"""
|
||||
|
||||
def format_page(body):
|
||||
return BASE + str(body) + "</body>\n</html>"
|
||||
|
||||
def format_index(issues):
|
||||
html = ""
|
||||
for issue in issues:
|
||||
html+=f"<option value='{json.dumps(issues[issue])}'>{issue}</option>\n"
|
||||
index = INDEX.format(issues=html)
|
||||
return format_page(index)
|
||||
Loading…
x
Reference in New Issue
Block a user