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

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()

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()

try:
	while True:
		newmsg = recv()
		if newmsg == "":
			break
		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 != "":
					print("Received SERVER from another source!")
					continue
				if config["recv_password"] != args[1]:
					print("Received invalid password from the server!")
					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()))+" +BHILkrio :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]
							argv = args[1].split(" ")[1:]

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

							if 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!")
				except KeyError:
					pass

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