2018-12-04 20:07:17 +01:00
|
|
|
#!/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
|
2019-08-02 20:34:45 +02:00
|
|
|
from urllib.parse import urlencode
|
2019-08-02 20:45:33 +02:00
|
|
|
import urllib.request
|
2019-08-02 20:38:56 +02:00
|
|
|
import base64
|
2018-12-04 20:07:17 +01:00
|
|
|
|
|
|
|
mpc = mpd.MPDClient()
|
|
|
|
mpc.timeout = 0.2
|
|
|
|
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
messages = []
|
|
|
|
last_sent = defaultdict(lambda: datetime(1970,1,1))
|
|
|
|
|
|
|
|
timeout = 1
|
|
|
|
|
|
|
|
|
2018-12-04 21:01:57 +01:00
|
|
|
def speak(text):
|
|
|
|
espk_proc = subprocess.Popen(("espeak", "-vnl"), stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
|
|
espk_proc.stdin.write(text)
|
|
|
|
espk_proc.stdin.close()
|
|
|
|
|
|
|
|
|
2018-12-04 20:07:17 +01:00
|
|
|
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"))
|
2018-12-04 21:01:57 +01:00
|
|
|
return "OK"
|
2019-09-03 00:27:49 +02:00
|
|
|
|
2019-09-02 23:51:44 +02:00
|
|
|
if sender != "somebody":
|
2019-09-03 00:33:45 +02:00
|
|
|
message = "<" + sender + "> " + message.decode()
|
2019-09-03 00:27:49 +02:00
|
|
|
|
2019-09-03 00:43:53 +02:00
|
|
|
messages.append(Message(time, sender, sendertype, message))
|
2019-08-02 20:34:45 +02:00
|
|
|
url = 'http://10.0.5.42:8000/' # Set destination URL here
|
2019-08-02 20:52:58 +02:00
|
|
|
post_fields = {'X-Messages': base64.b64encode(message)}
|
2019-08-02 20:34:45 +02:00
|
|
|
|
2019-08-02 20:46:53 +02:00
|
|
|
assembly_request = urllib.request.Request(url, urlencode(post_fields).encode())
|
2019-08-02 20:52:58 +02:00
|
|
|
assembly_request.add_header('X-Messages', base64.b64encode(message))
|
2019-08-02 20:46:53 +02:00
|
|
|
urllib.request.urlopen(assembly_request).read().decode()
|
2018-12-04 21:01:57 +01:00
|
|
|
speak(message)
|
2018-12-04 20:07:17 +01:00
|
|
|
|
|
|
|
return "OK"
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run()
|