summaryrefslogtreecommitdiff
path: root/ChannelCoupBotPublic.py
blob: 22b3daf41dd51d5f0b18bae8ffddc03d7be0bc82 (plain) (blame)
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
#!/usr/bin/env python3

import threading
import socket
import time
import ssl

names = []

trusted = [b"Andrew", b"Noisytoot"]

channel = b"#librespeech"


def init(name):
    while True:
        run(name)


def run(name):
    global names

    chanmodes = []
    coupmodes = []

    context = ssl.create_default_context()

    joined = False
    ready = False

    s = context.wrap_socket(
        socket.socket(socket.AF_INET, socket.SOCK_STREAM),
        server_hostname="irc.libera.chat",
    )

    s.connect(("irc.libera.chat", 6697))

    s.sendall(
        b"USER librespeech librespeech librespeech :librespeech\r\nNICK "
        + name
        + b"\r\n"
    )

    while True:
        msg = s.recv(512)
        while not msg.endswith(b"\r\n"):
            newmsg = s.recv(512)
            if newmsg == b"":
                global names
                print(
                    name.decode("UTF-8", "backslashreplace")
                    + " disconnected. Remaining: "
                    + str(names)
                )
                try:
                    names.remove(name)
                except ValueError:
                    pass
                return
            msg = msg + newmsg
        for line in [line for line in msg.split(b"\r\n") if line != b""]:
            if not joined and ready:
                s.sendall(b"JOIN " + channel + b"\r\n")

            source = b""
            lastarg = b""
            if line.startswith(b":"):
                source = line.split(b" ")[0][1:]
                line = b" ".join(line.split(b" ")[1:])

            command = line.split(b" ")[0]
            line = b" ".join(line.split(b" ")[1:])

            if line.startswith(b":"):
                line = b" " + line
            if len(line.split(b" :")) > 1:
                lastarg = b" :".join(line.split(b" :")[1:])
                line = line.split(b" :")[0]

            args = line.split(b" ")
            if args == [b""]:
                args = []

            if command == b"PING":
                if lastarg == b"":
                    if args == []:
                        s.sendall(b"PONG\r\n")
                    else:
                        s.sendall(b"PONG " + b" ".join(args) + b"\r\n")
                else:
                    if args == []:
                        s.sendall(b"PONG :" + lastarg + b"\r\n")
                    else:
                        s.sendall(
                            b"PONG " + b" ".join(args) + b" :" + lastarg + b"\r\n"
                        )
            elif command == b"005":
                for arg in args:
                    if arg.startswith(b"CHANMODES="):
                        arg = arg[10:]
                        for section in arg.split(b",")[0:3]:
                            for i in range(0, len(section)):
                                chanmodes.append(section[i : i + 1])
                    elif arg.startswith(b"PREFIX="):
                        arg = (arg.split(b"(")[1]).split(b")")[0]
                        for i in range(0, len(arg)):
                            chanmodes.append(arg[i : i + 1])
                            coupmodes.append(arg[i : i + 1])
            elif command == b"376":
                ready = True
                # 				s.sendall(b"OPER <insert oper stuff here>\r\n") #EDIT
                s.sendall(b"MODE " + name + b" +p\r\n")
                names.append(name)
                print(
                    name.decode("UTF-8", "backslashreplace")
                    + " connected. Connected clients: "
                    + str(names)
                )
            elif command == b"MODE":
                if lastarg != b"":
                    args.append(lastarg)
                target = args[0]
                del args[0]
                modes = args[0]
                del args[0]
                dir = b"-"
                if target == channel:
                    for i in range(0, len(modes)):
                        if modes[i : i + 1] == b"+":
                            dir = b"+"
                        elif modes[i : i + 1] == b"-":
                            dir = b"-"
                        else:
                            if modes[i : i + 1] in chanmodes:
                                arg = args[0]
                                del args[0]
                                if modes[i : i + 1] in coupmodes:
                                    if (
                                        arg not in names
                                        and arg not in trusted
                                        and dir == b"+"
                                    ):
                                        s.sendall(
                                            b"MODE "
                                            + channel
                                            + b" -"
                                            + modes[i : i + 1]
                                            + b" "
                                            + arg
                                            + b"\r\n"
                                        )
                                        if (
                                            source.split(b"!")[0] not in trusted
                                            and source.split(b"!")[0] not in names
                                        ):
                                            s.sendall(
                                                b"KICK "
                                                + channel
                                                + b" "
                                                + source.split(b"!")[0]
                                                + b" :COUP\r\n"
                                            )
                                    elif (
                                        arg in names or arg in trusted
                                    ) and dir == b"-":
                                        s.sendall(
                                            b"MODE "
                                            + channel
                                            + b" +"
                                            + modes[i : i + 1]
                                            + b" "
                                            + arg
                                            + b"\r\n"
                                        )
                                        if (
                                            source.split(b"!")[0] not in trusted
                                            and source.split(b"!")[0] not in names
                                        ):
                                            s.sendall(
                                                b"KICK "
                                                + channel
                                                + b" "
                                                + source.split(b"!")[0]
                                                + b" :COUP\r\n"
                                            )
                                elif modes[i : i + 1] == b"b" and dir == b"+":
                                    s.sendall(
                                        b"MODE " + channel + b" -b " + arg + b"\r\n"
                                    )
                elif target == name:
                    for i in range(0, len(modes)):
                        if modes[i : i + 1] == b"+":
                            dir = b"+"
                        elif modes[i : i + 1] == b"-":
                            dir = b"-"
                        else:
                            if modes[i : i + 1] == b"o" and dir == b"-":
                                s.sendall(
                                    b"OPER <insert oper stuff here>\r\nMODE "
                                    + name
                                    + b" +p\r\n"
                                )  # EDIT
                            elif modes[i : i + 1] == b"p" and dir == b"-":
                                s.sendall(b"MODE " + name + b" +p\r\n")
            elif command == b"JOIN":
                if lastarg != b"":
                    args.append(lastarg)
                if args[0] == channel:
                    if source.split(b"!")[0] == name:
                        joined = True
                    elif (
                        source.split(b"!")[0] in names
                        or source.split(b"!")[0] in trusted
                    ):
                        for mode in coupmodes:
                            s.sendall(
                                b"MODE "
                                + channel
                                + b" +"
                                + mode
                                + b" "
                                + source.split(b"!")[0]
                                + b"\r\n"
                            )
            elif command == b"KICK":
                if lastarg != b"":
                    args.append(lastarg)
                if args[0] == channel:
                    if args[1] == name:
                        s.sendall(b"JOIN " + channel + b"\r\n")
                        joined = False
                    elif args[1] in names or args[1] in trusted:
                        if (
                            source.split(b"!")[0] not in names
                            and source.split(b"!")[0] not in trusted
                        ):
                            s.sendall(
                                b"KICK "
                                + channel
                                + b" "
                                + source.split(b"!")[0]
                                + b" :COUP\r\n"
                            )


for i in range(0, 3):
    print("Starting librespeech" + str(i) + "...")
    threading.Thread(
        target=init, args=(b"librespeech" + str(i).encode("UTF-8"),)
    ).start()
    time.sleep(3)

print("All clients started.")