summaryrefslogtreecommitdiff
path: root/ServBotPublic.py
blob: 3fe4227297d86ba2a58d6578905bfb342ec5f6ad (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
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
#!/usr/bin/env python3

import threading
import socket
import math
import time
import ssl
import sys

send_password = b""
accept_password = b""

trusted_IDs = [[b"hax", b"173.249.27.223"], [b"Andrew", b"173.249.27.223"]]

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.connect(("irc.andrewyu.org", 6666))

s.sendall(
    b"PASS "
    + send_password
    + b" TS 6 1HC\r\nCAPAB BAN CHW CLUSTER ECHO ENCAP EOPMOD EUID EX IE KLN KNOCK MLOCK QS RSFNC SAVE SERVICES TB UNKLN\r\nSERVER coup.irc.andrewyu.org 0 :Coup Serv\r\nSVINFO 6 6 0 :"
    + str(time.time()).encode("UTF-8")
    + b"\r\n"
)

msg = b""

connected_server = b""

servlist = {}
userlist = {}
chanlist = {}


def read_and_send():
    for line in sys.stdin:
        try:
            s.sendall(line.encode("UTF-8"))
        except Exception:
            pass


threading.Thread(target=read_and_send, daemon=True).start()

while True:
    newmsg = s.recv(512)
    if newmsg == b"":
        break
    msg += newmsg
    split_msg = msg.split(b"\r\n")
    if len(split_msg) < 2:
        continue
    commands = split_msg[0:-1]
    msg = split_msg[-1]
    for command in commands:
        print(command)

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

        lastarg = b" :".join(command.split(b" :")[1:])
        args = command.split(b" :")[0].split(b" ")[1:]
        args.append(lastarg)
        command = command.split(b" :")[0].split(b" ")[0]

        if command.upper() == b"PING":
            try:
                s.sendall(args[1] + b" PONG " + args[0] + b"\r\n")
            except IndexError:
                s.sendall(b":1HC PONG " + args[0] + b"\r\n")
        elif command.upper() == b"PASS":
            if source != b"":
                print("Received PASS from another source!")
                continue
            if connected_server != b"":
                print("Received PASS from the server more than once!")
                continue
            if args[0] != accept_password:
                print("Invalid password given by the server!")
                exit()
            connected_server = args[3]
        elif command.upper() == b"SERVER":
            if source != b"":
                print("Received SERVER from another source!")
                continue
            if connected_server == b"":
                print("Server sent SERVER before PASS!")
                exit()
            servlist[connected_server] = {
                "server name": args[0],
                "hopcount": args[1],
                "server description": args[2],
            }

            s.sendall(
                b"EUID CoupServ 0 "
                + str(math.floor(time.time())).encode("UTF-8")
                + b" +SZio CoupServ hax/CoupServ 127.0.0.1 1HC000001 localhost * :CoupServ\r\n:1HC000001 OPER CoupServ admin\r\n"
            )
            s.sendall(
                b"EUID SpamServ 0 "
                + str(math.floor(time.time())).encode("UTF-8")
                + b" +Zi SpamServ hax/SpamServ 0.0.0.0 1HC000002 0.0.0.0 * :SpamServ\r\n"
            )
        elif command.upper() == b"SID":
            servlist[args[2]] = {
                "server name": args[0],
                "hopcount": args[1],
                "server description": args[3],
            }
        elif command.upper() == b"EUID":
            userlist[args[7]] = {
                "nickname": args[0],
                "hopcount": args[1],
                "nickTS": args[2],
                "umodes": args[3],
                "username": args[4],
                "visible hostname": args[5],
                "IP address": args[6],
                "real hostname": args[8],
                "account name": args[9],
                "gecos": args[10],
            }
        elif command.upper() == b"SJOIN":
            chanlist[args[1]] = {
                "channelTS": args[0],
                "simple modes": args[2],
                "mode params": args[3:-1],
                "nicklist": {},
            }
            for uid in args[-1].split(b" "):
                mode = {"@": False, "+": False}
                if uid.startswith(b"@"):
                    mode["@"] = True
                    uid = uid[1:]
                if uid.startswith(b"+"):
                    mode["+"] = True
                    uid = uid[1:]
                chanlist[args[1]]["nicklist"][uid] = mode
            s.sendall(
                b":1HC000001 JOIN "
                + str(time.time()).encode("UTF-8")
                + b" "
                + args[1]
                + b" +\r\n:1HC MODE "
                + args[1]
                + b" +ov 1HC000001 1HC000001\r\n"
            )
        elif command.upper() == b"NICK":
            if len(source) == 9:
                userlist[source]["nickname"] = args[0]
                userlist[source]["nickTS"] = args[1]
        elif command.upper() == b"QUIT":
            try:
                del userlist[source]
            except KeyError:
                continue
            for channel in chanlist:
                try:
                    del chanlist[channel]["nicklist"][source]
                except KeyError:
                    pass
        elif command.upper() == b"KILL":
            try:
                del userlist[args[0]]
            except KeyError:
                continue
            for channel in chanlist:
                try:
                    del chanlist[channel]["nicklist"][args[0]]
                except KeyError:
                    pass
        elif command.upper() == b"TMODE":
            if int(args[0].decode("UTF-8")) <= int(
                chanlist[args[1]]["channelTS"].decode("UTF-8")
            ):
                dir = b"-"
                for i in range(0, len(args[2])):
                    if args[2][i : i + 1] == b"+":
                        dir = b"+"
                    elif args[2][i : i + 1] == b"-":
                        dir = b"-"
                    elif args[2][i : i + 1] == b"o":
                        try:
                            chanlist[args[1]]["nicklist"][args[3]]["@"] = dir == b"+"
                        except KeyError:
                            pass
                        del args[3]
                    elif args[2][i : i + 1] == b"v":
                        try:
                            chanlist[args[1]]["nicklist"][args[3]]["+"] = dir == b"+"
                        except KeyError:
                            pass
                        del args[3]
                    elif args[2][i : i + 1] in [
                        b"e",
                        b"I",
                        b"b",
                        b"q",
                        b"k",
                        b"f",
                        b"l",
                        b"j",
                    ]:
                        del args[3]
        elif command.upper() == b"JOIN":
            if len(args) == 1:
                if args[0] == b"0":
                    for channel in chanlist:
                        try:
                            del chanlist[channel]["nicklist"][source]
                        except KeyError:
                            pass
            else:
                chanlist[args[1]]["nicklist"][source] = {"@": False, "+": False}
        elif command.upper() == b"PRIVMSG":
            if [
                userlist[source]["username"],
                userlist[source]["IP address"],
            ] in trusted_IDs:
                if args[0] == b"1HC000001" or (
                    args[0].startswith(b"#") and args[1].startswith(b"-")
                ):
                    if args[0] != b"1HC000001":
                        args[1] = args[1][1:]

                    if args[1] == b"print userlist":
                        print(userlist)
                    elif args[1] == b"print chanlist":
                        print(chanlist)
                    elif args[1] == b"print servlist":
                        print(servlist)
                    elif args[1].startswith(b"join "):
                        s.sendall(
                            b":1HC000001 JOIN "
                            + str(time.time()).encode("UTF-8")
                            + b" "
                            + args[1].split(b" ")[1]
                            + b" +\r\n"
                        )
                    elif args[1].startswith(b"sanick "):
                        current = args[1].split(b" ")[1]
                        target = args[1].split(b" ")[2]
                        for id in userlist:
                            if userlist[id]["nickname"] == current:
                                current = id
                                break
                        try:
                            s.sendall(
                                b":1HC ENCAP * RSFNC "
                                + current
                                + b" "
                                + target
                                + b" "
                                + str(math.floor(time.time())).encode("UTF-8")
                                + b" :"
                                + userlist[current]["nickTS"]
                                + b"\r\n"
                            )
                        except KeyError:
                            continue
                    elif args[1].startswith(b"coup "):  # temp disabled
                        chan = args[1].split(b" ")[1]
                        modechanges = []
                        for user in chanlist[chan]["nicklist"]:
                            if [
                                userlist[user]["username"],
                                userlist[user]["IP address"],
                            ] in trusted_IDs:
                                if not chanlist[chan]["nicklist"][user]["@"]:
                                    chanlist[chan]["nicklist"][user]["@"] = True
                                    modechanges.append([b"+", b"o", user])
                            else:
                                if chanlist[chan]["nicklist"][user]["@"]:
                                    chanlist[chan]["nicklist"][user]["@"] = False
                                    modechanges.append([b"-", b"o", user])

                        while len(modechanges) > 0:
                            modechange = [b" "]
                            dir = b""
                            for change in modechanges[:4]:
                                if change[0] != dir:
                                    modechange[0] += change[0]
                                    dir = change[0]
                                modechange[0] += change[1]
                                modechange.append(change[2])
                            s.sendall(
                                b":1HC000001 MODE "
                                + chan
                                + b" ".join(modechange)
                                + b"\r\n"
                            )
                            modechanges = modechanges[4:]
                    elif args[1].startswith(b"spam "):
                        chan = args[1].split(b" ")[1]
                        s.sendall(
                            b":1HC000002 JOIN "
                            + str(time.time()).encode("UTF-8")
                            + b" "
                            + chan
                            + b" +\r\n"
                        )
                        for _ in range(1000):
                            s.sendall(
                                b":1HC000002 PRIVMSG "
                                + chan
                                + b" :\x02\x03\x04\x05\x06\x07YAY SPAMMY SPAM https://users.andrewyu.org/~hax\r\n"
                            )
                        s.sendall(b":1HC000002 PART " + chan + b"\r\n")
                    elif args[1].startswith(b":"):
                        s.sendall(args[1] + b"\r\n")