From 9e72a685d583ad83728c0d8bc173e5c744cf3edc Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Wed, 6 Feb 2019 00:57:47 +0100 Subject: [PATCH] [enh] talk to gitbot in a super dirty way --- server.py | 4 ++++ to_room.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 to_room.py diff --git a/server.py b/server.py index d4278f0..8d5b450 100644 --- a/server.py +++ b/server.py @@ -1,5 +1,7 @@ +import os import hmac import hashlib +import subprocess from sanic import Sanic from sanic.response import text @@ -9,6 +11,7 @@ from sanic.exceptions import abort app = Sanic() secret = open("./github_webhook_secret", "r").read().strip() +gitbot_password = open("./github_webhook_secret", "r").read().strip() other_chans = { "doc": "doc", @@ -23,6 +26,7 @@ other_chans = { def notify(message, chan="dev"): print(f"{chan} -> {message}") + subprocess.check_call(["python", "./to_room.py", gitbot_password, message, chan], shell=True) @app.route("/github", methods=['POST']) diff --git a/to_room.py b/to_room.py new file mode 100644 index 0000000..c176570 --- /dev/null +++ b/to_room.py @@ -0,0 +1,65 @@ +# coding: utf-8 + +import sys +import xmpp +from contextlib import contextmanager + + +@contextmanager +def XMPPBot(password, room="dev"): + jid = xmpp.protocol.JID("gitbot@im.yunohost.org") + + client = xmpp.Client(jid.getDomain(), debug=[]) + + # hack to connect only if I need to send messages + client.connected = False + + def connect(): + client.connect() + + # yes, this is how this stupid lib tells you that the connection + # succeed, it return the string "sasl", this doesn't make any sens and + # it documented nowhere, because xmpp is THE FUTUR + if client.auth(jid.getNode(), password) != "sasl": + print("Failed to connect, bad login/password combination") + sys.exit(1) + + client.sendInitPresence(requestRoster=0) + + presence = xmpp.Presence(to="%s@conference.yunohost.org" % room) + presence.setTag('x', namespace='http://jabber.org/protocol/muc') + + client.send(presence) + + client.send(xmpp.Presence(to='%s@conference.yunohost.org/GitBot' % room)) + + def sendToChatRoom(message): + if not client.connected: + connect() + client.connected = True + + print room + client.send(xmpp.protocol.Message("%s@conference.yunohost.org" % room, message, typ="groupchat")) + + client.sendToChatRoom = sendToChatRoom + + yield client + + if client.connected: + client.disconnect() + + +if __name__ == '__main__': + if len(sys.argv[1:]) < 2: + print "Usage : python to_room.py []" + sys.exit(1) + + password, message = sys.argv[1:3] + + if len(sys.argv[1:]) > 2: + room = sys.argv[1:][2] + else: + room = "dev" + + with XMPPBot(password, room=room) as bot: + bot.sendToChatRoom(message)