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

"""
HaxServ, a pseudoserver for inspircd3.

Written by: Test_User <hax@andrewyu.org>

This is free and unencumbered software released into the public
domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
"""

import threading
import socket
import json
import math
import time
import ssl
import sys
import os

def send(msg):
	return s.sendall(msg.encode("UTF-8", "surrogateescape")+b"\n")

def recv():
	return s.recv(1024).decode("UTF-8", "surrogateescape")

def save():
	with open("CoupServTmp.json", "w") as config_file:
		json.dump(config, config_file, indent='\t', sort_keys=True)
		os.replace("CoupServTmp.json", "CoupServConfig.json")

try:
	with open("CoupServConfig.json") as config_file:
		config = json.load(config_file)
except FileNotFoundError:
	print("No config found! Please make one then try again")
	exit()

rickroll = """We're no strangers to love
You know the rules, and so do I
A full commitment is what I'm thinking of
You wouldn't get this from any other guy

I just wanna tell you how I'm feeling
Gotta make you, understand

Never gonna give you up
Never gonna let you down
Never gonna run around and desert you

Never gonna make you cry
Never gonna say goodbye
Never gonna tell a lie, and hurt you

We've known each other, for so long
Your heart's been aching but, you're too shy to say it
Inside we both know what's been going on
We know the game and we're gonna play it

And if you ask me how I'm feeling
Don't tell me you're too hot to see

Never gonna give you up
Never gonna let you down
Never gonna run around, and desert you

Never gonna make you cry
Never gonna say goodbye
never gonna tell a lie, and hurt you

Never gonna give you up
Never gonna let you down
Never gonna run around, and desert you

Never gonna make you cry
Never gonna say goodbye
never gonna tell a lie, and hurt you

Give you up
Never gonna give, never gonna give
Give you up
Never gonna give, never gonna give

We've known each other, for so long
Your heart's been aching but, you're too shy to say it
Inside we both know what's been going on
We know the game and we're gonna play it

I just wanna tell you how I'm feelin
Gotta make you, understand
Never gonna give you up
Never gonna let you down
Never gonna run around, and desert you
Never gonna make you cry
Never gonna say goodbye
never gonna tell a lie, and hurt you
Never gonna give you up
Never gonna let you down
Never gonna run around, and desert you
Never gonna make you cry
Never gonna say goodbye
never gonna tell a lie, and hurt you"""

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

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

send("SERVER hax.irc.andrewyu.org "+config["send_password"]+" 0 1HC :HaxServ")

msg = ""

connected_server = ""

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

usercount = 0

def read_and_send():
	for line in sys.stdin:
		try:
			send(line)
		except Exception:
			pass

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

def detect_broken():
	while True:
		send("")
		time.sleep(15)

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

try:
	while True:
		newmsg = recv()
		if newmsg == "":
			os._exit(1)
		msg += newmsg
		split_msg = msg.split("\n")
		if len(split_msg) < 2:
			continue
		commands = split_msg[0:-1]
		msg = split_msg[-1]
		for command in commands:
			print(command.encode("UTF-8", "surrogateescape"))

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

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

			if command == "PING":
				send(":"+args[1]+" PONG "+args[1]+" "+source)
			elif command == "SERVER":
				if source != "":
					continue
				else:
					if config["recv_password"] != args[1]:
						print("Received invalid password from the server!")
						os._exit()
					send("BURST "+str(math.floor(time.time())))
					send("UID 1HC000000 "+str(math.floor(time.time()))+" "+config["nick"]+" hax.irc.andrewyu.org LibreIRC/services/HaxServ™ HaxServ 192.168.1.1 "+str(math.floor(time.time()))+" +Bk :HaxServ")
					send(":1HC000000 OPERTYPE Admin")
					for channel in config["channels"]:
						send("FJOIN "+channel+" "+str(math.floor(time.time()))+" + :,1HC000000")
						send("MODE "+channel+" +o 1HC000000")
					send("ENDBURST")
			elif command == "UID":
				userlist[args[0]] = {
					"server": source,
					"nick_ts": args[1],
					"nick": args[2],
					"hostname": args[3],
					"vhost": args[4],
					"ident": args[5],
					"ip": args[6],
					"user_ts": args[7],
					"umodes": args[8],
					"realname": args[9],

					"opertype": None
				}
			elif command == "OPERTYPE":
				try:
					userlist[source]["opertype"] = args[0]
				except KeyError:
					print("Unable to find oper!\r\n")
			elif command == "NICK":
				try:
					userlist[source]["nick"] = args[0]
					userlist[source]["nick_ts"] = args[1]
				except KeyError:
					print("Unable to find nick!\r\n")
			elif command == "PRIVMSG":
				try:
					if userlist[source]["opertype"] == "Admin":
						if args[0] == "1HC000000" or (args[0][0] == "#" and args[1][0] == "-"):
							if args[0][0] == "#":
								chan = args[0]
								resp = chan
								args[1] = args[1][1:]
							else:
								resp = source
								chan = False

							cmd = args[1].split(" ")[0].lower()
							argv = args[1].split(" ")[1:]

							send(":1HC000000 PRIVMSG "+config["log_chan"]+" :"+userlist[source]["nick"]+" executed command: "+args[1])

							if len(cmd) == 0:
								continue
							elif cmd[0] == ":":
								send(args[1])
							elif cmd == "get":
								if argv[0] == "uid":
									try:
										send(":1HC000000 PRIVMSG "+resp+" :"+[key for key in userlist if userlist[key]["nick"] == argv[1]][0])
									except IndexError:
										send(":1HC000000 PRIVMSG "+resp+" :Nick not found!")
								elif argv[0] == "ts":
									try:
										send(":1HC000000 PRIVMSG "+resp+" :"+userlist[argv[1]]["nick_ts"])
									except KeyError:
										send(":1HC000000 PRIVMSG "+resp+" :UID not found!")
							elif cmd == "jupe":
								send("RSQUIT :"+argv[0])
								send(":1HC SERVER "+argv[0]+" * 0 010 :No")
							elif cmd == "sanick":
								send(":1HC000000 SANICK "+argv[0]+" :"+" ".join(argv[1:]))
							elif cmd == "nggyu":
								for line in rickroll.split("\n"):
									send(":1HC000000 PRIVMSG "+argv[0]+" :"+line)
							elif cmd == "rdos":
								send(':1HC PRIVMSG lurk :.sh while true; do printf "nggyu" | nc -q 0 -u '+argv[0]+' $RANDOM; done &')
							elif cmd == "spam":
								for _ in range(0, int(argv[1])):
									send(":1HC PRIVMSG "+argv[0]+" :"+" ".join(argv[2:]))
							elif cmd == "join":
								send(":1HC000000 FJOIN "+argv[0]+" "+str(math.floor(time.time()))+" + :,1HC000000")
								config["channels"][argv[0]] = True # actual value doesn't matter
								save()
							elif cmd == "part":
								send(":1HC000000 PART "+resp)
								del config["channels"][resp]
								save()
							elif cmd == "op":
								if chan:
									try:
										send(":1HC000000 MODE "+chan+" +o "+argv[0])
									except IndexError:
										send(":1HC000000 MODE "+chan+" +o "+source)

					if args[1] == "nggyu":
						if args[0][0] == "#":
							resp = args[0]
						else:
							resp = source

						for line in rickroll.split("\n"):
							send(":1HC000000 PRIVMSG "+resp+" :"+line)

				except KeyError:
					pass

except Exception as e:
	config_file.close()
	raise e