commit 63ef8de457035f1d0f5c4be6bbb3cdb9dfc936a3 Author: Midgard Date: Tue Dec 4 20:07:17 2018 +0100 Initial commit diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..6596fa2 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +# https://editorconfig.org + +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +indent_style = tab + +[*.py] +indent_style = space +indent_size = 4 diff --git a/chat.py b/chat.py new file mode 100644 index 0000000..5731070 --- /dev/null +++ b/chat.py @@ -0,0 +1,90 @@ +#!/bin/false +# Don't run directly, use gunicorn + +from flask import Flask, request, escape, render_template +import subprocess +import json +from datetime import datetime +from collections import defaultdict +import mpd + + +mpc = mpd.MPDClient() +mpc.timeout = 0.2 + + +app = Flask(__name__) +messages = [] +last_sent = defaultdict(lambda: datetime(1970,1,1)) + +timeout = 1 + + +class Message: + def __init__(self, time, sender, sendertype, msg): + self.time = time + self.sender = sender + self.sendertype = sendertype + self.msg = msg + + +@app.route("/messages/") +def messages_get(): + try: + mpc.connect("localhost", 6600) + song = mpc.currentsong() + status = mpc.status() + except: + status = {"state": "not connected"} + song = {} + try: + mpc.disconnect() + except: + pass + + return render_template("chat.html", messages=messages[::-1], mpd_song=song, mpd_status=status) + + +@app.route("/messages/", methods = ["POST"]) +def messages_post(): + if request.data: + if "X-Username" in request.headers: + sender = request.headers["X-Username"] + sendertype = "name" + elif "X-Real-IP" in request.headers: + sender = request.headers["X-Real-IP"] + sendertype = "ip" + else: + sender = "somebody" + sendertype = "unknown" + + if sender[:6] == "abefor": + sender = "abeforkelder" + + time = datetime.now() + last_sent_from_ip = last_sent[sendertype + sender] + + last_sent[sendertype + sender] = time + + if (time-last_sent_from_ip).total_seconds() < timeout: + return "OK Felix" + + message = request.data + + if len(message) > 200: + return "Message too long, maximum 200" + + if len(message) == 0: + return "No message found" + + if "spam" in str(message, "UTF-8").lower(): + messages.append(Message(time, "1.3.3.7", "ip", "Nee")) + else: + messages.append(Message(time, sender, sendertype, str(message, "UTF-8"))) + + subprocess.Popen(("espeak", "-vnl"), input=message, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + return "OK" + + +if __name__ == "__main__": + app.run() diff --git a/restart.sh b/restart.sh new file mode 100755 index 0000000..74c34e7 --- /dev/null +++ b/restart.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +killall gunicorn +./runChat & disown +sleep 1 +DISPLAY=:0 xdotool key F5 diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..5f34d49 --- /dev/null +++ b/run.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +cd "$(dirname "$0")" +export FLASK_APP=chat.py +exec gunicorn -w 1 -b127.0.0.1:5000 chat:app diff --git a/templates/chat.html b/templates/chat.html new file mode 100644 index 0000000..cb78a66 --- /dev/null +++ b/templates/chat.html @@ -0,0 +1,71 @@ + + + + + + Messages from the world to kelder + + + + {% if mpd_status["state"] == "play" %} + {% if mpd_song["artist"] and mpd_song["title"] %} +
{{ mpd_song["artist"] }}{{ mpd_song["title"] }}
+ {% elif mpd_song["title"] %} +
{{ mpd_song["title"] }}
+ {% elif mpd_song["artist"] %} +
{{ mpd_song["artist"] }}
+ {% else %} +
Unknown music, fix your metadata!
+ {% endif %} + {% endif %} +

Messages

+ {% for m in messages %} +
+
- {{ m.sender }}: {{ m.msg }}
+
+ {% endfor %} + +