From 220ce976f29b204e2e64e5cd52918afad12d8d3e Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Tue, 9 Aug 2022 07:20:30 +0100 Subject: [PATCH] Fire a fake event when we send a message --- api/views.py | 24 ++++++++++++++++++++++++ core/bot.py | 4 ++++ 2 files changed, 28 insertions(+) diff --git a/api/views.py b/api/views.py index d899e5b..602bc50 100644 --- a/api/views.py +++ b/api/views.py @@ -478,3 +478,27 @@ class API(object): return dumps({"success": False, "reason": f"could not get first relay for {net}"}) first_relay.list() return dumps({"success": True, "message": f"requested list with instance {first_relay.num} of {net}"}) + + @app.route("/irc/msg////", methods=["PUT"]) + @login_required + def irc_send_message(self, request, net, num, channel): + print("IRC SEND", net, num, channel) + try: + data = loads(request.content.read()) + except JSONDecodeError: + return "Invalid JSON" + if "msg" not in data: + return dumps({"success": False, "reason": "no msg."}) + msg = data["msg"] + if net not in main.network.keys(): + return dumps({"success": False, "reason": "no such net."}) + if not num.isdigit(): + return dumps({"success": False, "reason": "invalid num: not a number."}) + num = int(num) + if num not in main.network[net].relays.keys(): + return dumps({"success": False, "reason": f"no relay {num} on {net}"}) + name = f"{net}{num}" + if name not in main.IRCPool.keys(): + return dumps({"success": False, "reason": f"relay {num} not on {net}"}) + main.IRCPool[name].sendmsg(channel, msg) + return dumps({"success": True, "message": f"sent message to {channel} on {name}"}) diff --git a/core/bot.py b/core/bot.py index 579bab8..81a74b0 100644 --- a/core/bot.py +++ b/core/bot.py @@ -252,6 +252,10 @@ class IRCBot(IRCClient): def action(self, user, channel, msg): self.event(type="action", muser=user, channel=channel, msg=msg) + def sendmsg(self, channel, msg): + self.event(type="self", mtype="msg", channel=channel, nick=self.nickname, msg=msg) + self.msg(channel, msg) + def get(self, var): try: result = getattr(self, var)