From 71bed5ab1799a2d93d5b2b2683531b26b5b1fcfb Mon Sep 17 00:00:00 2001 From: Tyler Hallada Date: Sat, 2 Dec 2017 01:36:44 -0500 Subject: [PATCH] Add chat form that sends messages to Flask server --- chat/minecraft-chat.ini | 11 +++++++++ chat/server.py | 34 ++++++++++++++++++++++++++++ css/styles.css | 49 +++++++++++++++++++++++++++++++++++++++++ index.html | 25 +++++++++++++++++++-- js/sendChat.js | 34 ++++++++++++++++++++++++++++ 5 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 chat/minecraft-chat.ini create mode 100644 chat/server.py create mode 100644 js/sendChat.js diff --git a/chat/minecraft-chat.ini b/chat/minecraft-chat.ini new file mode 100644 index 0000000..dccc3e8 --- /dev/null +++ b/chat/minecraft-chat.ini @@ -0,0 +1,11 @@ +[uwsgi] +module = wsgi:application + +master = true +processes = 2 + +socket = minecraft-chat.sock +chmod-socket = 664 +vacuum = true + +die-on-term = true diff --git a/chat/server.py b/chat/server.py new file mode 100644 index 0000000..cd86e2d --- /dev/null +++ b/chat/server.py @@ -0,0 +1,34 @@ +import logging +import subprocess + +from flask import Flask, request + +app = Flask(__name__) + + +@app.before_first_request +def setup_logging(): + app.logger.addHandler(logging.StreamHandler()) + app.logger.setLevel(logging.INFO) + + +@app.route('/chat/', methods=['GET', 'POST']) +def send_chat(): + if request.method == 'POST': + if request.form.get('email', None): + return 'Text was entered into honeypot!', 200 + if not request.form.get('say-text', None): + return 'No message to send!', 422 + if request.form.get('say-username', None): + subprocess.call(['/usr/bin/screen', '-S', 'mc-panic-shack', '-p', '0', '-X', 'stuff', + '/say [{}]: {}\015'.format(request.form['say-username'], request.form['say-text'])]) + else: + subprocess.call(['/usr/bin/screen', '-S', 'mc-panic-shack', '-p', '0', '-X', 'stuff', + '/say {}\015'.format(request.form['say-text'])]) + return 'Sending chat: ' + request.form.get('say-username', '') + ': ' + request.form['say-text'] + else: + app.logger.info('Hello, world!') + return 'Hello, world!' + +if __name__ == "__main__": + app.run(host='0.0.0.0', port="8888") diff --git a/css/styles.css b/css/styles.css index dd32bb2..732ddde 100644 --- a/css/styles.css +++ b/css/styles.css @@ -81,3 +81,52 @@ ol li code { max-height: 600px; overflow: scroll; } + +#say-header { + margin-top: 15px; + margin-bottom: 10px; +} + +#say-form { + border: 1px dashed darkgray; + padding: 10px; +} + +#say-form label { + width: 100%; +} + +#say-form, #say-send { + margin-top: 5px; +} + +#say-username, #say-text { + margin-bottom: 5px; + margin-right: 5px; +} + +#say-text { + width: 100%; +} + +#say-dnf { + position: absolute; + left: -2000px; +} + +#say-sending { + display: none; +} + +#say-error { + color: darkred; +} + +#say-success { + display: none; +} + +#say-notice { + margin-top: 5px; + margin-bottom: 0; +} diff --git a/index.html b/index.html index 02b2dde..d2d127a 100644 --- a/index.html +++ b/index.html @@ -85,10 +85,30 @@

Updated every 10 seconds.

+

Send Chat to Server

+
+ + +
+ + + Sending... + Sent! + +

It may take a minute or two before the message appears in the above server log.

+
@@ -96,5 +116,6 @@ + diff --git a/js/sendChat.js b/js/sendChat.js new file mode 100644 index 0000000..a0501e5 --- /dev/null +++ b/js/sendChat.js @@ -0,0 +1,34 @@ +var form = document.getElementById('say-form'); +var username = document.getElementById('say-username'); +var text = document.getElementById('say-text'); +var send = document.getElementById('say-send'); +var sending = document.getElementById('say-sending'); +var success = document.getElementById('say-success'); +var error = document.getElementById('say-error'); + +function sendChat(e) { + e.preventDefault(); + + var xhr = new XMLHttpRequest(); + var formData = new FormData(form); + xhr.addEventListener('load', function (event) { + console.log(event.target.responseText); + if (event.target.status === 200) { + error.textContent = ''; + text.value = ''; + success.style.display = 'inline-block'; + } else if (event.target.status === 422) { + error.textContent = 'You must give a message to send! (' + event.target.status + ')'; + success.style.display = 'none'; + } else { + error.textContent = 'Error Sending! (' + event.target.status + ')'; + success.style.display = 'none'; + } + sending.style.display = 'none'; + }); + xhr.open('POST', '/chat/'); + xhr.send(formData); + sending.style.display = 'inline-block'; +} + +form.addEventListener('submit', sendChat);