diff --git a/requirements.txt b/requirements.txt index aa1095e..70e05b5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ Flask gunicorn requests cryptography +redis diff --git a/webhooks.py b/webhooks.py index f84ac3c..5f13d9a 100644 --- a/webhooks.py +++ b/webhooks.py @@ -5,6 +5,7 @@ import hashlib import requests import yaml import os +import redis from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey from flask import Flask, jsonify, request, abort @@ -15,6 +16,8 @@ app = Flask(__name__) SNAP_USER_DATA = os.environ.get('SNAP_USER_DATA') CONFIG_FILE = os.environ.get('CONFIG_FILE') LOG_FILE = os.environ.get('LOG_FILE') +REDIS_HOST = os.environ.get('REDIS_HOST', 'localhost') +REDIS_PORT = os.environ.get('REDIS_PORT', 6379) if SNAP_USER_DATA is None: if CONFIG_FILE is None: filename = 'config.yaml' @@ -34,6 +37,8 @@ logging.basicConfig(level=logging.DEBUG, filename=log_file) with open(filename, "rb") as config_file: config = yaml.load(config_file, Loader=yaml.SafeLoader) +rs = redis.Redis(host=REDIS_HOST, port=REDIS_PORT) + srht_public_key = Ed25519PublicKey.from_public_bytes( base64.b64decode(config['srht_payload_sig'])) # GitHub webhooks @@ -186,7 +191,7 @@ def gl_push_event(gateway, data): # logging.debug(data) project = data["project"]["path_with_namespace"] username = data["user_username"] - branch = data['ref'][len('refs/heads/'):] + branch = data['ref'][len('refs/heads/'):] count = data["total_commits_count"] homepage = data["repository"]["homepage"] s = "s" if count > 1 else "" @@ -335,3 +340,40 @@ def srht_event(user, repo, gateway): logging.exception('SRHT') return '', 200 + +@app.route('/tars/', methods=['POST']) +def feedbot_cmd(): + token = request.headers['Authorization'] + user_id = request.form['user_id'] + reply = {"username": "tars"} + cmd_text = request.form['text'] + cmd_help = ''' + The following commands are available: + + xpost-on: turn on cross posts from mastodon to pnut + xpost-off: turn off cross posts from mastodon to pnut + ''' + + if token != "Token " + config['mm_tars_token']: + logging.debug("INVALID TOKEN") + abort(401) + + if user_id != config['mm_user_id']: + logging.debug("UNAUTHORIZED USER") + abort(403) + + if cmd_text == "xpost-on": + logging.debug("enable crossposts") + rs.rpush('TARS', "feedbot_enable") + reply['text'] = "crossposts will be enabled" + + elif cmd_text == "xpost-off": + logging.debug("disable crossposts") + rs.rpush('TARS', "feedbot_disable") + reply['text'] = "crossposts will be disabled" + + else: + logging.debug("SHOW HELP?") + reply['text'] = cmd_help + + return reply, 200