summaryrefslogtreecommitdiff
path: root/cogs/IRC_backup.py
blob: 858b0b786606a61cafd03672fce3b102fc98112d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
from discord.ext import commands
import discord
import aiojobs
import socket
import asyncio
import threading


class IRCBridge(commands.Cog, threading.Thread):
    def __init__(self, bot):
        self.bot = bot
        global extension_name
        extension_name = "[IRC Bridge] "
        threading.Thread.__init__(self)

    async def irc_run(self, ctx, con, sch_irc):
        data = con.recv(2048).decode('UTF-8')
        con.send(bytes("JOIN #discord\n", "UTF-8"))
        if data.startswith("PING :"):
            con.send(bytes('PONG %s\r\n' % data[6:], 'UTF-8'))

        print("cycle", data)
        await sch_irc.spawn(irc_run(ctx, con, sch_irc))
        await asyncio.sleep(0.1)

    @commands.command()
    async def irc(self, ctx):
        con = socket.socket()
        con.connect(("irc.blatech.net", 6667))
        con.send(bytes(f"USER discord1 discord2 discord3 'Hello!'\n", "UTF-8"))
        con.send(bytes('NICK %s\r\n' % "discord", 'UTF-8'))
        con.send(bytes('PASS %s\r\n' % "ircdiscord", 'UTF-8'))
        con.send(bytes('MSG NickServ IDENTIFY discordirc\r\n', 'UTF-8'))

        data = con.recv(2048).decode('UTF-8')
        if data.startswith("PING :"):
            con.send(bytes('PONG %s\r\n' % data[6:], 'UTF-8'))

        while 1:
            data = con.recv(2048).decode('UTF-8')
            con.send(bytes("JOIN #discord\n", "UTF-8"))
            if data.startswith("PING :"):
                con.send(bytes('PONG %s\r\n' % data[6:], 'UTF-8'))

            print("cycle", data)
            # await sch_irc.spawn(irc_run(ctx, con, sch_irc))
            await asyncio.sleep(0.1)



        # sch_irc = await aiojobs.create_scheduler()
        # await sch_irc.spawn(irc_run(self, ctx, con, sch_irc))


def setup(bot):
    bot.add_cog(IRCBridge(bot))




























from discord.ext import commands
import discord
import aiojobs
import socket
import asyncio
import threading
import _thread
import re
import json
import itertools
import requests
import discord
from discord import Webhook, RequestsWebhookAdapter
from datetime import datetime


class IRCClient:
    def __init__(self, chan_pairs, config, discord_client):
        self.connected = False
        self.chan_pairs = chan_pairs
        self.config = config
        self.discord_client = discord_client

    def irc_connect(self, server, port, nickname):
        print("Connecting to {}:{}".format(server, port))

        ircsocket = socket.socket()

        ircsocket.connect((server, port))
        ircsocket.send(f"NICK {nickname}\r\n".encode())
        ircsocket.send(f"USER {nickname} * * {nickname}\r\n".encode())

        self.connected = True
        print("Connected.")
        self.send_to_webhook("[IRC Bridge]", "Connected!")

        return ircsocket, server, nickname

    def join_channels(self):
        for channel in [pair[0] for pair in self.chan_pairs]:
            print(f"Joining {channel}")
            self.ircsocket.send(f"JOIN {channel}\r\n".encode())
        return

    def msg_process(self, rawmsg):  # figure out what we want to do with our irc message
        prefix, command, args, msg = self.split_msg(rawmsg)

        # msg format is "nick PRIVMSG #channel :message"
        if command in ["376", "422"]:
            print("END OF MOTD")
            self.join_channels()

        elif command == "PING":
            self.ircsocket.send("PONG {}\r\n".format(msg).encode())

        elif command == "PRIVMSG":
            author = prefix.split("!")[0]

            # send message to run comm coroutine
            for pair in self.chan_pairs:
                if args[0] == pair[0]:
                    if msg.startswith("=status") and len(msg.split()) > 1:
                        name = msg.split(" ", 1)[1].lower()
                        status_msg = ""
                        for member in self.discord_client.get_channel(pair[1]).guild.members:
                            if member.name.lower() == name or (member.nick and member.nick.lower() == name):
                                status_msg += f"{ember.name} is currently {member.status}"

                        self.send_message(args[0], status_msg)
                        continue

                    clean_msg = msg
                    # clean_msg = uniformatter.ircToDiscord(msg, pair[1], self.discord_client)
                    print(rawmsg)
                    action_regex = re.match(r"\u0001ACTION (.+)\u0001", clean_msg)  # format /me
                    # if action_regex:
                    #     formatted_msg = "**\* {}** {}".format(author, action_regex.group(1))
                    # else:
                    #     formatted_msg = "**<{}>** {}".format(author, clean_msg)

                    discord_channel = self.discord_client.get_channel(pair[1])
                    # Create webhook
                    self.send_to_webhook(author, clean_msg)

        else:
            print(f"[IRC] {rawmsg}")

        return

    def split_msg(self, rawmsg):  # interpret irc message
        msgpre, sep, msg = rawmsg.partition(" :")

        if not sep:  # if sep is empty
            msg = None

        msgpre_list = msgpre.split()

        if msgpre_list[0].startswith(":"):
            prefix = msgpre_list.pop(0).lstrip(":")
        else:
            prefix = None

        command = msgpre_list.pop(0)

        args = msgpre_list

        return prefix, command, args, msg

    def irc_run(self):  # start main irc loop
        if not self.connected:
            self.s, self.server, self.nick = self.irc_connect(**self.config)

        line_buffer = ""

        while True:
            responce = self.ircsocket.recv(2048)

            line_buffer += responce.decode()
            lines = line_buffer.split("\n")

            line_buffer = lines.pop()

            for line in lines:
                line = line.rstrip()

                if line:
                    self.msg_process(line)
                else:
                    pass

    def send_to_webhook(self, author, msg):
        webhook = Webhook.partial(676940734112333847,
                                  "7TxjLhpmmGWKhTAvfBctiBcdO-T5INVkrweK39B74vH9VHvxhHy3eG_qBh368q4E4v6Z",
                                  adapter=RequestsWebhookAdapter())
        webhook.send(content=msg,
                     wait=False,
                     username=author,
                     avatar_url=None,
                     tts=False,
                     file=None,
                     files=None,
                     embed=None,
                     embeds=None)
        webhook = None

        # profile image: f"https://eu.ui-avatars.com/api/name={author}?length=1?&background=0088ff&color=FFF"

    def send_message(self, channel, msg):  # send irc message
        try:
            self.ircsocket.send("PRIVMSG {channel} :{msg}\r\n".encode())

        except BrokenPipeError as e:
            _thread.interrupt_main()
        # exit("Error in message size too large. Exiting...")
        return


class uniformatter:
    def discordToIrc(msg):
        def replaceFormatting(form, replacement, string):
            start_form = re.escape(form)
            end_form = re.escape(form[::-1])  # reverse it

            pattern = r"{}((?:(?!{}).)*?){}".format(start_form, start_form, end_form)
            str_split = re.split(pattern, string)

            if len(str_split) == 1:  # no formatting required
                return str_split[0]

            new_str = ""
            for idx, part in enumerate(str_split):
                if idx % 2 == 1:
                    if re.search(r"https?:\/\/[^ \n]*$", new_str):  # make sure this formatting is not part of a url
                        new_str += "{}{}{}".format(form, part, form[::-1])
                    else:
                        new_str += "{}{}\x0F".format(replacement, part)
                else:
                    new_str += part

            return new_str

        def createHaste(code):
            response = requests.post("https://hastebin.com/documents", data=code)
            key = response.json()["key"]
            url = "https://hastebin.com/" + key
            return url

        formatting_table = [  # comment lines of this table to disable certain types of formatting relay
            ("***__", "\x02\x1D\x1F"),  # ***__UNDERLINE BOLD ITALICS__***
            ("__***", "\x02\x1D\x1F"),  # __***UNDERLINE BOLD ITALICS***__
            ("**__", "\x02\x1F"),  # **__UNDERLINE BOLD__**
            ("__**", "\x02\x1F"),  # __**UNDERLINE BOLD**__
            ("*__", "\x1D\x1F"),  # *__UNDERLINE ITALICS__*
            ("__*", "\x1D\x1F"),  # __*UNDERLINE ITALICS*__
            ("***", "\x02\x1D"),  # ***BOLD ITALICS***
            ("**_", "\x02\x1D"),  # **_BOLD ITALICS_**
            ("_**", "\x02\x1D"),  # _**BOLD ITALICS**_
            ("__", "\x1F"),  # __UNDERLINE__
            ("**", "\x02"),  # **BOLD**
            ("*", "\x1D"),  # *ITALICS*
            ("_", "\x1D"),  # _ITALICS_
            ("`", "\x11"),  # `MONOSPACE`
            ("~~", "\x1e")  # ~~STRIKETHROUGH~~
        ]

        # replace codeblocks
        msg = re.sub(r"```(?:\w+\n|\n)?(.+?)```", lambda m: createHaste(m.group(1)), msg, flags=re.S)

        # replace newlines
        if "\n" in msg:
            msg = msg.replace("\n", " ")

        # replace formatting
        for form in formatting_table:
            msg = replaceFormatting(form[0], form[1], msg)

        # clean up emotes
        msg = re.sub(r"<(:\w+:)\d+>", lambda m: m.group(1), msg)

        return msg

    def ircToDiscord(msg, channel, discord_client):
        # print(f"[IRC] {msg}")
        msg = re.sub(r"\x03\d{0,2}(?:,\d{0,2})?", "", msg)

        formatting_table = [
            (["\x02", "\x1D", "\x1F"], "***__"),  # bold italics underline
            (["\x1D", "\x1F"], "*__"),  # italics underline
            (["\x02", "\x1F"], "**_"),  # bold underline
            (["\x02", "\x1D"], "***"),  # bold italics
            (["\x02"], "**"),  # bold
            (["\x1D"], "*"),  # italics
            (["\x1F"], "__"),  # underline
            (["\x11"], "`"),  # code
            (["\x1e"], "~~")  # strikethrough
        ]

        for form in formatting_table:
            # check for matches for all permutation of the list
            perms = itertools.permutations(form[0])
            for perm in perms:
                if "\x0F" not in msg:
                    msg += "\x0F"
                msg = re.sub(r"{}(.*?)\x0F".format("".join(perm)),
                             lambda m: "{}{}{}".format(form[1], m.group(1), form[1][::-1]), msg)

        for char in ["\x02", "\x1D", "\x1F", "\x0F"]:
            msg = msg.replace(char, "")

        mentions = re.findall(r"@(\S+)", msg)
        if mentions:
            def mentionGetter(name_match):
                name = name_match.group(1)
                for member in discord_client.get_channel(channel).guild.members:  # dota2mods serverid
                    if member.name.lower() == name.lower() or (member.nick and member.nick.lower() == name.lower()):
                        return member.mention
                # user was not found, just return original text
                return "@" + name

            msg = re.sub(r"@(\S+)", mentionGetter, msg)

        return msg


class IRCBridge(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        global extension_name
        global irc_client
        irc_client = None
        extension_name = "[IRC Bridge] "

        # sch_irc = await aiojobs.create_scheduler()
        # await sch_irc.spawn(irc_run(self, ctx, con, sch_irc))

    @commands.command(hidden=True)
    async def irc(self, ctx):
        """Create a link to an IRC channel"""
        with open("./cogs/config.json") as fp:
            config = json.load(fp)

        chan_pairs = [(pair["irc_channel"], pair["discord_channel"]) for pair in config["pairs"]]
        print(chan_pairs)
        client = self.bot

        global irc_thread
        global irc_client
        irc_client = IRCClient(chan_pairs=chan_pairs, config=config["irc"], discord_client=client)
        irc_thread = None

        irc_thread = threading.Thread(target=irc_client.irc_run, daemon=True)
        irc_thread.start()

    @commands.Cog.listener('on_message')
    async def irc_on_message(self, message):
        global irc_client
        if message.webhook_id is not None:
            return

        if len(message.embeds) == 1 and message.content == "":
            # message.content = message.embeds[0].description
            message.content = "Embedded. Cannot show this message."

        if irc_client is not None:
            # print(f"Message from bot: {message.author != self.bot.user}")
            # print(f"Message is webhook: {message.webhook_id is None}")
            # if message.author != self.bot.user:
            with open("./cogs/config.json") as fp:
                config = json.load(fp)

            chan_pairs = [(pair["irc_channel"], pair["discord_channel"]) for pair in config["pairs"]]
            if chan_pairs[0][1] == message.channel.id:
                msg_string = f"{message.author}: {message.content}"
                irc_client.send_message(chan_pairs[0][0], msg_string)


def setup(bot):
    print("INFO: Loading [IRC]... ", end="")
    bot.add_cog(IRCBridge(bot))
    print("Done!")


def teardown(bot):
    print("INFO: Unloading [IRC]")