summaryrefslogtreecommitdiff
path: root/network.lua
blob: c581759756ef6bfadd32cc6105c39e0df0061917 (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
--[[

Network protocol file for HaxServ.

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.
]]

local function mode_snomask(current, mode, dir, arg)
	if dir == "+" then
		current[mode] = current[mode] or {}
		parse_modes(arg, {}, {}, current[mode])
		if not next(current[mode]) then
			current[mode] = nil
		end
		return true
	else
		current[mode] = nil
	end
end

local function mode_replace(current, mode, dir, arg)
	if dir == "+" then
		current[mode] = arg
		return true
	else
		current[mode] = nil
	end
end

local function mode_multi(current, mode, dir, arg)
	if dir == "+" then
		current[mode] = current[mode] or {}
		current[mode][arg] = true
	else
		if current[mode] == nil then
			print("Invalid mode change attempt!\n")
			current[mode] = {}
		end
		current[mode][arg] = nil
		if next(current[mode]) == nil then
			current[mode] = nil
		end
	end
	return true
end

local usermodes = {
	["s"] = mode_snomask,
}

local chanmodes = {
	["l"] = mode_replace,
	["L"] = mode_replace,
	["v"] = mode_multi,
	["o"] = mode_multi,
	["h"] = mode_multi,
	["a"] = mode_multi,
	["q"] = mode_multi,
	["Y"] = mode_multi,
	["d"] = mode_replace,
	["f"] = mode_replace,
	["g"] = mode_multi,
	["b"] = mode_multi,
	["e"] = mode_multi,
	["I"] = mode_multi,
	["k"] = mode_replace,
	["w"] = mode_multi,
	["E"] = mode_replace,
	["F"] = mode_replace,
	["H"] = mode_replace,
	["J"] = mode_replace,
	["X"] = mode_multi,
}

local function parse_modes(modes, args, has_args, current)
	local dir = "-"
	for i = 1, #modes do
		local mode = modes:sub(i, i)

		if mode == "+" or mode == "-" then
			dir = mode
		elseif has_args[mode] and has_args[mode][dir] then
			if not args[1] then return false end

			if dir == "+" then
				current[mode] = (type(current[mode]) == "table" and current[mode] or {})
				current[mode][args[1]] = true
			else
				if current[mode] then
					current[mode][args[1]] = nil
				end
			end
			table.remove(args, 1)
		else
			current[mode] = (dir == "+" and true or nil)
		end
	end
	return true
end

message_handler = {
	["PING"] = function(con, source, args, original)
		con:send(":"..args[2].." PONG "..args[2].." "..source.."\n")
	end,

	["SERVER"] = function(con, source, args, original)
		if source then
			servlist[args[4]] = {address = args[1], distance = args[3] + 1 + servlist[source].distance, name = args[5], metadata = {}}
		else
			servlist[args[4]] = {address = args[1], distance = args[3], name = args[5], metadata = {}}
		end
	end,

	["METADATA"] = function(con, source, args, original)
		if args[1] == "*" then
			if servlist[source] then
				servlist[source].metadata[args[2]] = args[3]
			else
				print("Got metadata command from an unknown server!\n")
			end
		elseif args[1]:sub(1, 1) == "#" then
			print(string.format("%q", original))
			print("Channels not yet handled!\n")
		else
			if userlist[args[1]] then
				userlist[args[1]].metadata[args[2]] = args[3]
			else
				print(("%q"):format(original))
				print("Got metadata for an unknown user!\n")
			end
		end
	end,

	["UID"] = function(con, source, args, original)
		userlist[args[1]] = {
			server = source,
			nick_ts = args[2],
			nick = args[3],
			hostname = args[4],
			vhost = args[5],
			ident = args[6],
			ip = args[7],
			user_ts = args[8],
			modes = {},
			realname = args[-1], -- last one is safer as any extra are arguments to umodes (or protocol violations, but at that point nothing is a safe option)

			metadata = {}, -- controlled by METADATA network commands
		}

		if not parse_modes(args[9], {table.unpack(args, 10, #args-1)}, {["s"] = {["+"] = true, ["-"] = false}}, userlist[args[1]].modes) then return true end
	end,

	["OPERTYPE"] = function(con, source, args, original)
		if userlist[source] then
			userlist[source].opertype = args[1]
		else
			print("Server "..source.." attempted to set OPERTYPE on a nonexistent user!\n")
		end
	end,

	["NICK"] = function(con, source, args, original)
		if userlist[source] then
			userlist[source].nick = args[1]
			userlist[source].nick_ts = args[2]
		end
	end,

	["PRIVMSG"] = function(con, source, args, original)
		local cmd_args = {}
		for part in args[2]:gmatch("[^ ]*") do
			table.insert(cmd_args, part)
		end
		cmd = cmd_args[1]:upper()
		table.remove(cmd_args, 1)

		local resp
		if args[1]:sub(1, 1) == "#" then
			resp = args[1]

			if cmd:sub(1, 3) ~= "\x0304" then return end

			cmd = cmd:sub(4) -- remove leading '-'
		else
			resp = source
		end

		if commands[cmd] then
			if has_permission(userlist[source], commands[cmd].privs) then
				print(("%q"):format(userlist[source].nick.." executed command: "..cmd))
				return commands[cmd].func(con, source, cmd, cmd_args, resp)
			else
				con:send(":1HC000000 NOTICE "..resp.." :You are not authorized to execute that command.\n")
			end
		else
			con:send(":1HC000000 NOTICE "..resp.." :Unknown command: "..cmd.."\n")
		end
	end,

	["MODE"] = function(con, source, args, original)
		if args[1]:sub(1, 1) == "#" then
			print("Channels not handled yet!\n")
		else
			if not userlist[args[1]] then
				print("Attempted to set mode on an unknown user!\n")
			elseif not parse_modes(args[2], {table.unpack(args, 3)}, usermodes, userlist[args[1]].modes) then
				return true
			elseif not userlist[args[1]].modes.o then
				userlist[args[1]].opertype = nil
			end
		end
	end,

	["QUIT"] = function(con, source, args, original)
		userlist[source] = nil
		for name, chan in pairs(chanlist) do
			chan["users"][source] = nil
			if next(chan["users"]) == nil and not chan["modes"]["P"] then
				chanlist[name] = nil
			end
		end
	end,

	["FJOIN"] = function(con, source, args, original)
		
	end
}