2019-02-06 00:57:47 +01:00
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import xmpp
|
|
|
|
from contextlib import contextmanager
|
2021-10-20 12:10:32 +02:00
|
|
|
import uuid
|
2019-02-06 00:57:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
@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)
|
|
|
|
|
2024-03-09 03:28:23 +01:00
|
|
|
presence = xmpp.Presence(
|
|
|
|
attrs={"id": uuid.uuid4()}, to="%s@conference.yunohost.org" % room
|
|
|
|
)
|
|
|
|
presence.setTag("x", namespace="http://jabber.org/protocol/muc")
|
2019-02-06 00:57:47 +01:00
|
|
|
|
|
|
|
client.send(presence)
|
|
|
|
|
2024-03-09 03:28:23 +01:00
|
|
|
client.send(
|
|
|
|
xmpp.Presence(
|
|
|
|
attrs={"id": uuid.uuid4()},
|
|
|
|
to="%s@conference.yunohost.org/GitBot" % room,
|
|
|
|
)
|
|
|
|
)
|
2019-02-06 00:57:47 +01:00
|
|
|
|
|
|
|
def sendToChatRoom(message):
|
|
|
|
if not client.connected:
|
|
|
|
connect()
|
|
|
|
client.connected = True
|
|
|
|
|
2024-03-09 03:28:23 +01:00
|
|
|
client.send(
|
|
|
|
xmpp.protocol.Message(
|
|
|
|
"%s@conference.yunohost.org" % room,
|
|
|
|
message,
|
|
|
|
typ="groupchat",
|
|
|
|
attrs={"id": uuid.uuid4()},
|
|
|
|
)
|
|
|
|
)
|
2019-02-06 00:57:47 +01:00
|
|
|
|
|
|
|
client.sendToChatRoom = sendToChatRoom
|
|
|
|
|
|
|
|
yield client
|
|
|
|
|
|
|
|
if client.connected:
|
|
|
|
client.disconnect()
|
|
|
|
|
|
|
|
|
2024-03-09 03:28:23 +01:00
|
|
|
if __name__ == "__main__":
|
2019-02-06 00:57:47 +01:00
|
|
|
if len(sys.argv[1:]) < 2:
|
2021-10-20 12:10:32 +02:00
|
|
|
print("Usage : python to_room.py <password> <message> [<room name>]")
|
2019-02-06 00:57:47 +01:00
|
|
|
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)
|