From 63f8d027aed463e2fcec2f35463f1c35d6294453 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sun, 30 Jan 2022 21:54:15 -0500 Subject: [PATCH] first commit --- .gitignore | 1 + README.md | 5 ++++ config.json | 7 +++++ plugins/bridge.py | 38 ++++++++++++++++++++++++++ plugins/ircked/__init__.py | 0 plugins/ircked/bot.py | 36 ++++++++++++++++++++++++ plugins/ircked/message.py | 56 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 143 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 config.json create mode 100644 plugins/bridge.py create mode 100644 plugins/ircked/__init__.py create mode 100644 plugins/ircked/bot.py create mode 100644 plugins/ircked/message.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed8ebf5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..26769d1 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +links up crusty IRC old people and the strapping young lads (?) on d*scord + +built on [mdbf](https://github.com/kurisufriend/modular-discord-bot-framework) and [ircked](https://github.com/kurisufriend/ircked) + +point mbdf at `config.json` and let 'er rip \ No newline at end of file diff --git a/config.json b/config.json new file mode 100644 index 0000000..a589a61 --- /dev/null +++ b/config.json @@ -0,0 +1,7 @@ +{ + "token": "YOUR_TOKEN_HERE", + "plugin_path": "./plugins", + "authed": [ + + ] +} \ No newline at end of file diff --git a/plugins/bridge.py b/plugins/bridge.py new file mode 100644 index 0000000..a918c66 --- /dev/null +++ b/plugins/bridge.py @@ -0,0 +1,38 @@ +from ircked.bot import irc_bot +from ircked.message import * + +hooks = ["READY", "MESSAGE_CREATE"] + +# replace all this stuff and it should maybe probablyt just werk (tm) +_discord_webhook = "https://discord.com/api/webhooks/id/token" +_discord_channel = "id" +_irc_channel = "#channel" +_irc_nick = "nick" + +dorfl = None + +init = False + +def run(event, ctx, bot): + global init + global dorfl + if event == "READY": + init = True + dorfl = irc_bot(nick = _irc_nick) + dorfl.connect_register("irc.rizon.net", 7000) + + def magic(msg, ctx): + if msg.command == "PING": + message.manual("", "PONG", msg.parameters).send(ctx.socket) + elif msg.command == "001": + message.manual("", "JOIN", [_irc_channel]).send(ctx.socket) + elif msg.command == "PRIVMSG" and "\x01VERSION\x01" in msg.parameters: + message.manual(":"+msg.parameters[0], "PRIVMSG", [msg.prefix[1:].split("!")[0], ":\x01dorfl bot\x01"]).send(ctx.socket) + if msg.command == "PRIVMSG" and ("py-ctcp" not in msg.prefix): + pm = privmsg.parse(msg) + bot.execute_webhook(_discord_webhook, pm.bod, pm.fr.split("!")[0]+" (IRC)") + + dorfl.run(event_handler = magic) + elif event == "MESSAGE_CREATE" and init == True and ctx["channel_id"] == _discord_channel and not(bool(ctx.get("webhook_id"))): + dorfl.sendraw(privmsg.build(_irc_nick, _irc_channel, ctx["member"]["nick"]+" (discord): "+ctx["content"]).msg) + \ No newline at end of file diff --git a/plugins/ircked/__init__.py b/plugins/ircked/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/plugins/ircked/bot.py b/plugins/ircked/bot.py new file mode 100644 index 0000000..468f32e --- /dev/null +++ b/plugins/ircked/bot.py @@ -0,0 +1,36 @@ +import socket +from ircked.message import * +def default_event_handler(msg, ctx): + print("<<", str(msg)) + if msg.command == "PING": + message.manual("", "PONG", msg.parameters).send(ctx.socket) + elif msg.command == "001": + message.manual("", "JOIN", ["#qrs"]).send(ctx.socket) + elif msg.command == "PRIVMSG" and "\x01VERSION\x01" in msg.parameters: + message.manual(":"+msg.parameters[0], "PRIVMSG", [msg.prefix[1:].split("!")[0], ":\x01dorfl bot\x01"]).send(ctx.socket) + if msg.command == "PRIVMSG": + pm = privmsg.parse(msg) + if pm.bod == ".hello": + privmsg.build(ctx.nick, pm.to, "hello, world!").msg.send(ctx.socket) +class irc_bot: + def __init__(self, nick="dorfl", user="dorfl", real="dorfl"): + self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.con_to = () + self.nick = nick + self.user = user + self.real = real + self.behaviour = {} + def connect_register(self, addy, port): + self.socket.connect((addy, port)) + self.socket.send(f"USER {self.user} 0 * :{self.real}\r\n".encode("utf-8")) + self.socket.send(f"NICK {self.nick}\r\n".encode("utf-8")) + self.con_to = (addy, port) + def run(self, event_handler = default_event_handler): + while True: + data = self.socket.recv(512).decode("utf-8") + if not data: continue + msgs = [message.parse(raw) for raw in [part for part in data.split("\r\n") if part]] + for msg in msgs: + event_handler(msg, self) + def sendraw(self, msg): + msg.send(self.socket) \ No newline at end of file diff --git a/plugins/ircked/message.py b/plugins/ircked/message.py new file mode 100644 index 0000000..80d6e96 --- /dev/null +++ b/plugins/ircked/message.py @@ -0,0 +1,56 @@ +class message: + def __init__(self): + self.prefix = "" + self.command = "" + self.parameters = [] + @staticmethod + def parse(raw): + msg = message() + try: + raw = raw.split(" ") + msg.prefix = "" if not(raw[0].startswith(":")) else raw[0] + if msg.prefix: + raw.pop(0) + msg.command = raw[0] + msg.parameters = raw[1:][0:15] + except: + pass + return msg + @staticmethod + def manual(pre, com, par): + msg = message() + msg.prefix = pre + msg.command = com + msg.parameters = par + return msg + def __str__(self): + return ("" if not self.prefix else self.prefix+" ") + self.command + " " + (" ".join(self.parameters) if not(type(self.parameters) == type(str)) else self.parameters) + def send(self, sock): + sock.send((str(self)+"\r\n").encode("utf-8")) + print(self.parameters) + print(">>", str(self)) + +class privmsg: + def __init__(self): + self.msg = message() + self.fr = "" + self.to = "" + self.bod = "" + @staticmethod + def build(fro, to, body): + pm = privmsg() + pm.msg.prefix = ":"+fro + pm.msg.command = "PRIVMSG" + pm.msg.parameters.append(to) + for word in body.split(" "): + pm.msg.parameters.append(word) + pm.msg.parameters[1] = ":"+pm.msg.parameters[1] + return pm + @staticmethod + def parse(msg): + pm = privmsg() + pm.msg = msg + pm.fr = msg.prefix[1:] + pm.to = msg.parameters[0] + pm.bod = (" ".join(msg.parameters) if not(type(msg.parameters) == type(str)) else msg.parameters).split(":")[1] + return pm \ No newline at end of file