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
|
--[[
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 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("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)
print(string.format("%q", original))
if userlist[source] then
userlist[source].nick = args[1]
userlist[source].nick_ts = args[2]
end
end,
["PRIVMSG"] = function(con, source, args, original)
print(string.format("%q", 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, 1) ~= "-" then return end
cmd = cmd:sub(2) -- remove leading '-'
else
resp = source
end
if commands[cmd] then
if has_permission(userlist[source], commands[cmd].privs) then
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)
print(string.format("%q", 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)}, {["s"] = {["+"] = true, ["-"] = false}}, 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)
print(string.format("%q", original))
userlist[source] = nil
end,
}
|