summaryrefslogtreecommitdiff
path: root/bot.py
blob: 94dda72039070743de4c0d3b50d7b0131cf56396 (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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
#!/usr/bin/env pythn3
#
# Single-connection IRC bot.
# Copyright (C) 2022  Andrew Yu <https://www.andrewyu.org/>
# Copyright (C) 2022  luk3yx <https://luk3yx.github.io/>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program in a file called LICENSE, under the heading "AGPLv3"
# until a single line with two dashes ("--") and no more.  If not, see
# <https://www.gnu.org/licenses/>.
#
# The function match_hostmask is under the license described from the heading
# "3BSD" until a single line two dashes and no more in the LICENSE file, with
# the following copyright notice:
#
#    Copyright (c) 2022  Andrew Yu <https://www.andrewyu.org/>
#    Copyright (c) 2002-2009  Jeremiah Fincher and others
#


from __future__ import annotations
from dataclasses import dataclass, field
from typing import Optional, Callable, Iterator
import socket
import sys
import base64
import re
import time


from config import (
    SERVER,
    PORT,
    NICK,
    IDENT,
    GECOS,
    CHANNELS,
    PREFIX,
    LOGIN,
    PASSWORD,
    ADMINS,
)

car = lambda l: l[0]
cdr = lambda l: l[1:]


@dataclass
class User:
    nick: bytes
    ident: Optional[bytes] = None
    host: Optional[bytes] = None
    on_server: Optional[bytes] = None
    in_channels: list[Channel] = field(default_factory=list)
    last_activity_time: Optional[float] = None
    last_activity: Optional[bytes] = None


@dataclass
class State:
    initiated: bool = False
    negotiated_caps: list[bytes] = field(default_factory=list)
    deal_with_these_caps_before_cap_end: list[bytes] = field(default_factory=list)


@dataclass
class Channel:
    name: bytes
    # joined = None: didn't even talk about this to the server, False: not joined, True: joined
    joined: Optional[bool] = None
    authorized: list[tuple[bytes, bytes, bytes]] = field(default_factory=list)
    users: list[User] = field(default_factory=list)
    ops: list[User] = field(default_factory=list)
    voices: list[User] = field(default_factory=list)
    actual_bans: list[tuple[bytes, bytes, bytes]] = field(default_factory=list)
    active_bans: list[tuple[bytes, bytes, bytes]] = field(default_factory=list)
    active_exceptions: list[tuple[bytes, bytes, bytes]] = field(default_factory=list)


@dataclass
class Message:
    cmd: bytes
    source: Optional[User]
    args: list[bytes]


class ProtocolViolation(Exception):
    pass

def match_hostmask(hostmask: bytes, pattern: bytes) -> bool:
    regexp = b""
    for i in list(pattern):
        c = i.to_bytes(1, "big")
        if c == b"*":
            regexp += b".*"
        elif c == b"?":
            regexp += b"."
        elif c in b"[{":
            regexp += b"[\\[{]"
        elif c in b"}]":
            regexp += b"[}\\]]"
        elif c in b"|\\":
            regexp += b"[|\\\\]"
        elif c in b"^~":
            regexp += b"[~^]"
        else:
            regexp += re.escape(c)
    regexp += b"$"
    return bool(re.compile(regexp, re.I).match(hostmask))

def ban_mask(s: socket.socket, channel: Channel, mask: bytes, protect: list[User] = []) -> Optional[list[User]]:
    nicks_to_kick: list[bytes] = []
    users_to_kick: list[User] = []
    for potential_target in channel.users:
        if not (potential_target.nick and potential_target.ident and potential_target.host): continue
        if match_hostmask(potential_target.nick + b"!" + potential_target.ident + b"@" + potential_target.host, mask):
            if potential_target in protect:
                return None
            nicks_to_kick.append(potential_target.nick)
            users_to_kick.append(potential_target)

    send(s, b"MODE", channel.name, b"+b", mask)
    for nick_to_kick in nicks_to_kick:
        send(s, b"KICK", channel.name, nick_to_kick)
    return users_to_kick


def is_admin(user: User) -> Optional[bool]:
    if not (user.nick and user.ident and user.host):
        return None
    for a in ADMINS:
        if match_hostmask((user.nick + b"!" + user.ident + b"@" + user.host), a):
            return True
    return False


def human_list_sep(l: list[bytes]) -> Optional[bytes]:
    if len(l) == 0:
        return None
    if len(l) == 1:
        return l[0]
    return b", ".join(l[:-1]) + b" and " + l[-1]


def parse_nih(nih: bytes) -> tuple[Optional[bytes], Optional[bytes], Optional[bytes]]:
    "Parse a nick!username@host into tuples"
    n_ih = nih.split(b"!", 1)
    if len(n_ih) < 2:
        return n_ih[0], None, None
    ih = n_ih[1].split(b"@", 1)
    if len(ih) < 2:
        return n_ih[0], ih[0], None
    return n_ih[0], ih[0], ih[1]


def parse_message(
    line: bytes,
) -> tuple[
    bytes, tuple[Optional[bytes], Optional[bytes], Optional[bytes]], list[bytes]
]:
    "Break a message into its command, source and list of arguments."
    split_line = line.split(b" ")
    del line

    if split_line[0].startswith(b":"):
        nih = split_line.pop(0)[1:]
        hostmask = parse_nih(nih)
        cmd = split_line.pop(0)
    else:
        cmd = split_line.pop(0)
        hostmask = (None, None, None)

    args = []
    c = 0
    for i in split_line:
        if i.startswith(b":"):
            args.append(b" ".join(split_line[c:]))
            args[-1] = args[-1][1:]
            break
        else:
            args.append(i)
        c += 1

    return cmd, hostmask, args


# def break_up(s: bytes, chunk_size: int) -> list[bytes]:
#     "Breaks some bytes up to smaller chunks, and return the chunks in a list."
#     if len(s) <= chunk_size:
#         return [s]
#     else:
#         return [s[0 : chunk_size + 1]] + break_up(s[chunk_size + 1 :], chunk_size)


def break_up(s: bytes, chunk_size: int) -> Iterator[bytes]:
    while len(s) > chunk_size:
        yield s[: chunk_size + 1]
        s = s[chunk_size + 1 :]
    yield s


def send(s: socket.socket, *args: bytes) -> bytes:
    if len(args) < 1:
        raise ValueError("No arguments to send")
    elif len(args) < 2:
        # When there's only one argument, i.e. the command, do not add a ":" before it, otherwise it would be treated as a message source, which is illegal.
        s.sendall(args[0] + b"\r\n")
        print("<", args[0].decode("utf-8", "surrogateescape"))
        return args[0]
    line: bytes = b""
    for arg in args[:-1]:
        if b" " in arg:
            raise ValueError("Illegal space in argument in %s to send()", repr(args))
        else:
            line += arg + b" "
    line += b":" + args[-1]
    s.sendall(line + b"\r\n")
    print("<", line.decode("utf-8", "surrogateescape"))
    return line


def reply(s: socket.socket, msg: Message, text: bytes) -> bytes:
    if msg.source is None:
        raise TypeError("Cannot reply to a message that doesn't have a source")
    if msg.cmd != b"PRIVMSG":
        raise TypeError(
            "reply() only accepts incoming PRIVMSG Message's as the msg argument"
        )
    if msg.args[0].startswith(b"#"):
        text = (b"%s: " % msg.source.nick) + text
        reply_to = msg.args[0]
    else:
        reply_to = msg.source.nick
    return send(s, b"PRIVMSG", reply_to, text)


IRC_CALLBACK_FUNCTION_TYPE = Callable[
    [socket.socket, State, User, dict[bytes, User], dict[bytes, Channel], Message], None
]
irc_callbacks: dict[bytes, list[tuple[IRC_CALLBACK_FUNCTION_TYPE, bool]]] = {}
# The bool here indicates if this callback is single-use.

def register_irc_callback(
    cmd: bytes,
) -> Callable[[IRC_CALLBACK_FUNCTION_TYPE], IRC_CALLBACK_FUNCTION_TYPE]:
    def add_handler(f: IRC_CALLBACK_FUNCTION_TYPE, single_use: bool = False) -> IRC_CALLBACK_FUNCTION_TYPE:
        try:
            irc_callbacks[cmd].append((f, single_use))
        except KeyError:
            irc_callbacks[cmd] = [(f, single_use)]

        return f

    return add_handler


@register_irc_callback(b"PING")
def handle_ping(
    s: socket.socket,
    state: State,
    me: User,
    users: dict[bytes, User],
    channels: dict[bytes, Channel],
    msg: Message,
) -> None:
    try:
        send(s, b"PONG", msg.args[0])
    except IndexError:
        raise ProtocolViolation("PING without cookie") from None


@register_irc_callback(b"376")  # MOTD end: Ready to join channels
def handle_376(
    s: socket.socket,
    state: State,
    me: User,
    users: dict[bytes, User],
    channels: dict[bytes, Channel],
    msg: Message,
) -> None:
    for channel in channels.values():
        send(s, b"JOIN", channel.name)


@register_irc_callback(b"422")  # No MOTD: Ready to join channels
def handle_422(
    s: socket.socket,
    state: State,
    me: User,
    users: dict[bytes, User],
    channels: dict[bytes, Channel],
    msg: Message,
) -> None:
    for channel in channels.values():
        send(s, b"JOIN", channel.name)


@register_irc_callback(b"JOIN")
def handle_join(
    s: socket.socket,
    state: State,
    me: User,
    users: dict[bytes, User],
    channels: dict[bytes, Channel],
    msg: Message,
) -> None:
    if msg.source is None:
        raise ProtocolViolation("JOIN without source")
    if msg.source.nick.lower() == me.nick.lower():
        try:
            channels[msg.args[0]].joined = True
        except KeyError:
            channels[msg.args[0]] = Channel(joined=True, name=msg.args[0])
        except IndexError:
            raise ProtocolViolation("JOIN without channel name") from None
    else:
        msg.source.in_channels.append(channels[msg.args[0]])
        channels[msg.args[0]].users.append(msg.source)


@register_irc_callback(b"PART")
def handle_part(
    s: socket.socket,
    state: State,
    me: User,
    users: dict[bytes, User],
    channels: dict[bytes, Channel],
    msg: Message,
) -> None:
    if msg.source is None:
        raise ProtocolViolation("PART without source")
    if msg.source.nick.lower() == me.nick.lower():
        try:
            channels[msg.args[0]].joined = False
        except KeyError:
            # channels[msg.args[0]] = Channel(joined=False, name=msg.args[0])
            raise ProtocolViolation(
                "Received self-PART from channel that hasn't been JOINed"
            )
        except IndexError:
            raise ProtocolViolation("PART without channel name") from None
    else:
        try:
            msg.source.in_channels.remove(channels[msg.args[0]])
            channels[msg.args[0]].users.remove(msg.source)
        except ValueError:
            raise ProtocolViolation("Received PART for a user that hasn't joined the channel")


@register_irc_callback(b"352")  # WHO
def handle_352(
    s: socket.socket,
    state: State,
    me: User,
    users: dict[bytes, User],
    channels: dict[bytes, Channel],
    msg: Message,
) -> None:
    try:
        user = users[msg.args[5].lower()]
    except KeyError:
        user = User(nick=msg.args[5], ident=msg.args[2], host=msg.args[3])
        users[msg.args[5].lower()] = user


@register_irc_callback(b"NICK")
def handle_nick(
    s: socket.socket,
    state: State,
    me: User,
    users: dict[bytes, User],
    channels: dict[bytes, Channel],
    msg: Message,
) -> None:
    if msg.source is None:
        raise ProtocolViolation("NICK without source")
    del users[
        msg.source.nick.lower()
    ]  # We can't rename dictionary keys, so we remove and readd it.
    msg.source.nick = msg.args[0]
    users[msg.source.nick.lower()] = msg.source


@register_irc_callback(b"353")  # NAMES
def handle_353(
    s: socket.socket,
    state: State,
    me: User,
    users: dict[bytes, User],
    channels: dict[bytes, Channel],
    msg: Message,
) -> None:
    channel = channels[msg.args[2]]
    nicklist = msg.args[3].split(b" ")
    for nick in nicklist:
        if nick.startswith(b"@") or nick.startswith(b"+"):
            nick = nick[1:]
        try:
            user = users[nick.lower()]
        except KeyError:
            user = User(nick=nick)
            users[nick.lower()] = user
        channel.users.append(user)
        user.in_channels.append(channel)
        if nick.startswith(b"@"):
            channel.ops.append(user)
        elif nick.startswith(b"+"):
            channel.voices.append(user)


@register_irc_callback(b"CAP")
def handle_cap(
    s: socket.socket,
    state: State,
    me: User,
    users: dict[bytes, User],
    channels: dict[bytes, Channel],
    msg: Message,
) -> None:
    if msg.args[0] == b"*" and msg.args[1] == b"LS":
        capspecs = msg.args[2].split(b" ")
        for capspec in capspecs:
            kvp = capspec.split(b"=", 1)
            cap: bytes
            cap_arg: Optional[bytes]
            try:
                cap, cap_arg = kvp[0], kvp[1]
            except IndexError:
                cap, cap_arg = kvp[0], None

            if LOGIN and PASSWORD and cap == b"sasl":
                if cap_arg is None:
                    raise ProtocolViolation(
                        "Received capability list contains SASL entry without methods"
                    )
                if b"PLAIN" in cap_arg.split(b","):
                    send(s, b"CAP", b"REQ", b"sasl")

    if msg.args[1] == b"ACK":
        if msg.args[2] == b"sasl":
            state.negotiated_caps.append(b"sasl")
            state.deal_with_these_caps_before_cap_end.append(b"sasl")
            send(s, b"AUTHENTICATE", b"PLAIN")


@register_irc_callback(b"AUTHENTICATE")
def handle_authenticate(
    s: socket.socket,
    state: State,
    me: User,
    users: dict[bytes, User],
    channels: dict[bytes, Channel],
    msg: Message,
) -> None:
    assert LOGIN and PASSWORD
    send(
        s,
        b"AUTHENTICATE",
        base64.b64encode(LOGIN + b"\x00" + LOGIN + b"\x00" + PASSWORD),
    )


@register_irc_callback(b"903")  # SASL success
def handle_903(
    s: socket.socket,
    state: State,
    me: User,
    users: dict[bytes, User],
    channels: dict[bytes, Channel],
    msg: Message,
) -> None:
    state.deal_with_these_caps_before_cap_end.remove(b"sasl")
    if not state.deal_with_these_caps_before_cap_end:
        send(s, b"CAP", b"END")


@register_irc_callback(b"PRIVMSG")
def handle_privmsg(
    s: socket.socket,
    state: State,
    me: User,
    users: dict[bytes, User],
    channels: dict[bytes, Channel],
    msg: Message,
) -> None:
    if not msg.source:
        raise ProtocolViolation("PRIVMSG without source")

    target = msg.args[0]
    text = msg.args[1]

    channel: Optional[Channel]
    if target.startswith(b"#"):
        channel = channels[target]
        if not text.startswith(PREFIX):
            return
        text = text[len(PREFIX) :]
    elif target.lower() == me.nick.lower():
        channel = None
        if text.startswith(PREFIX):
            text = text[len(PREFIX) :]
    else:
        # A message addressed to a non-channel that isn't the bot itself?  Weird!
        return

    chat_command = text.split(b" ")
    chat_command[0] = chat_command[0].upper()

    if chat_command[0] == b"DUMP":
        print(repr(channels))
        print(repr(users))
        reply(s, msg, b"I dumped my channel and user database to standard output.")
        return
    elif chat_command[0] == b"BAN":
        if not channel:
            reply(s, msg, b"You may only do this in a channel.")
            return
        elif msg.source not in channel.users:
            reply(
                s,
                msg,
                b"You are not on the channel, and you're haxsending messages to it.  This isn't nice.",
            )
            return
        priv = is_admin(msg.source)
        if not priv:
            reply(s, msg, b"Access denied.")
            return
        elif not me in channel.ops:
            reply(
                s,
                msg,
                b"I am not a channel operator, so I can't kickban people.",
            )
            return

        kickban_string = chat_command[1]
        if b"@" in kickban_string:
            kickban_mask = kickban_string
            if b"!" not in kickban_mask:
                kickban_mask = b"*!" + kickban_mask
            n = ban_mask(s, channel, kickban_mask, protect=[me, msg.source])
            if n is None:
                reply(s, msg, b"I refuse to kick you or myself, for the sake of being kind!")
            else:
                reply(s, msg, b"Banned mask %s and kicked %d clients." % (kickban_mask, len(n)))

            return
        else:
            try:
                kickban_target = users[kickban_string.lower()]
                assert kickban_target.nick and kickban_target.ident and kickban_target.host
                kickban_mask = b"*!%s@%s" % (kickban_target.ident if (not kickban_target.ident.startswith(b"~")) else b"*", kickban_target.host)
                n = ban_mask(s, channel, kickban_mask, protect=[me, msg.source])
                if n is None:
                    reply(s, msg, b"I refuse to kick you or myself, for the sake of being kind!")
                else:
                    reply(s, msg, b"Banned mask %s and kicked %d clients." % (kickban_mask, len(n)))
                    return
            except KeyError:
                reply(s, msg, b"You didn't provide a hostmask for me to kickban, so I assumed it's a nickname, but I don't know %s." % kickban_string)
                return
    elif chat_command[0] == b"FAKEOP":
        if not channel:
            reply(s, msg, b"You may only do this in a channel.")
            return
        elif msg.source not in channel.users:
            reply(
                s,
                msg,
                b"You are not on the channel, and you're haxsending messages to it.  This isn't nice.",
            )
            return
        priv = is_admin(msg.source)
        if not priv:
            reply(s, msg, b"Access denied.")
            return
        elif not me in channel.ops:
            channel.ops.append(me)
            reply(
                s,
                msg,
                b"I didn't think that I was a channel operator before, now I assume so.",
            )
            return
        else:
            reply(s, msg, b"No need!")
    elif chat_command[0] == b"OP":
        if not channel:
            reply(s, msg, b"You may only do this in a channel.")
            return
        elif msg.source not in channel.users:
            reply(
                s,
                msg,
                b"You are not on the channel, and you're haxsending messages to it.  This isn't nice.",
            )
            return
        priv = is_admin(msg.source)
        if priv is False:
            reply(s, msg, b"Access denied.")
            return
        elif priv is None:
            reply(
                s,
                msg,
                b"I don't know your ident or host, so I can't check your permissions.  Something is wrong.",
            )
            return
        elif msg.source in channel.ops:
            reply(s, msg, b"You are already a channel operator.")
            return
        elif not me in channel.ops:
            reply(
                s,
                msg,
                b"I am not a channel operator, so I can't make you a channel operator.",
            )
            return
        else:
            send(s, b"MODE", channel.name, b"+o", msg.source.nick)
            reply(s, msg, b"I tried to make you a channel operator.")
    elif chat_command[0] == b"WHOIS":
        if not is_admin(msg.source):
            reply(s, msg, b"Access denied.")
            return
        whois_target_nick = chat_command[1]
        if whois_target_nick.lower() == me.nick.lower():
            reply(s, msg, b"That's me, right?")
            return
        elif whois_target_nick.lower() == msg.source.nick.lower():
            reply(s, msg, b"You should know enough about yourself.  But oh well...")
        try:
            whois_target = users[whois_target_nick.lower()]
        except KeyError:
            reply(s, msg, b"I know nothing about %s." % whois_target_nick)
            return
        if whois_target.last_activity_time is None:
            reply(
                s,
                msg,
                b"I didn't see %s do anything while I was here." % whois_target.nick,
            )
        else:
            reply(
                s,
                msg,
                b"%s's last activity was %s seconds ago: %s"
                % (
                    whois_target.nick,
                    str(int(time.time() - whois_target.last_activity_time)).encode("ascii"),
                    whois_target.last_activity
                ),
            )
        if whois_target.in_channels:
            reply(s, msg, b"I am actively keeping track of %s through %s." % (whois_target.nick, human_list_sep([c.name for c in whois_target.in_channels])))
        else:
            reply(s, msg, b"I am not actively keeping track of %s as we don't have common channels." % whois_target.nick)
    elif chat_command[0] == b"HELP":
        # TODO for forkers: If you run this bot for public use, and you've changed the code (well, except for config.py), you must update the information below, in accordance with the AGPL.
        reply(
            s,
            msg,
            b"Hi!  I'm a simple IRC bot.  Consult git://git.andrewyu.org/irc-mod-bot.git/ for documentation.",
        )
    else:
        reply(s, msg, b"Unknown command: %s" % chat_command[0])


def handle_incoming_message(
    s: socket.socket,
    state: State,
    me: User,
    users: dict[bytes, User],
    channels: dict[bytes, Channel],
    msg: Message,
) -> None:
    try:
        for callback in irc_callbacks[msg.cmd]:
            callback[0](s, state, me, users, channels, msg)
            if callback[1]:
                # Remove single-use callbacks after use.
                irc_callbacks[msg.cmd].remove(callback)
    except KeyError:
        pass


def handle_incoming_line(
    s: socket.socket,
    state: State,
    me: User,
    users: dict[bytes, User],
    channels: dict[bytes, Channel],
    m: bytes,
) -> None:
    print(">", m.decode("utf-8", "surrogateescape"))
    parsed: tuple[
        bytes, tuple[Optional[bytes], Optional[bytes], Optional[bytes]], list[bytes]
    ] = parse_message(m)
    source: Optional[User]
    try:
        source = users[parsed[1][0].lower()] if parsed[1][0] else None
    except KeyError:
        assert parsed[1][0]
        source = User(nick=parsed[1][0])
        users[parsed[1][0].lower()] = source

    if source:
        # These assignments are to update the ident and the host from incoming messages to users in our database, though this may be a bit redundent to do every time.
        source.ident = parsed[1][1]
        source.host = parsed[1][2]
        source.last_activity_time = time.time()
        source.last_activity = m
    msg = Message(parsed[0].upper(), source, parsed[2])

    handle_incoming_message(s, state, me, users, channels, msg)


def main() -> None:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((SERVER, PORT))

    me = User(nick=NICK)
    users: dict[bytes, User] = {NICK: me}
    channels: dict[bytes, Channel] = {}
    for channel_name in CHANNELS:
        channels[channel_name] = Channel(name=channel_name, joined=None)
    state = State()

    send(s, b"CAP", b"LS", b"302")

    # if LOGIN and PASSWORD:
    #     send(s, b"PASS", LOGIN + b":" + PASSWORD)
    # elif LOGIN or PASSWORD:
    #     print(
    #         "[-] Login and password must be both set to authenticate", file=sys.stderr
    #     )
    #     sys.exit(2)

    send(s, b"NICK", NICK)
    send(s, b"USER", IDENT, b"0", b"0", GECOS)

    recv_msg: bytes = b""
    while True:
        new_recv_msg: bytes = s.recv(512)
        if new_recv_msg == b"":
            sys.exit(0)
        recv_msg += new_recv_msg
        split_recv_msg: list[bytes] = recv_msg.split(b"\r\n")
        if len(split_recv_msg) < 2:
            continue
        for m in split_recv_msg[0:-1]:
            handle_incoming_line(s, state, me, users, channels, m)
        recv_msg = split_recv_msg[-1]


if __name__ == "__main__":
    main()