summaryrefslogtreecommitdiff
path: root/commands.lua
blob: e07852840c6fc52c95af08a5eb70e0bdb297c8e3 (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
299
300
301
302
303
304
305
306
307
308
309
310
311
--[[

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

commands = {
	["SANICK"] = {
		func = function(con, user, cmd, args, resp)
			if #args < 2 then
				con:send(":1HC000000 NOTICE "..resp.." :Not enough args.\n")
			else
				con:send("SANICK "..args[1].." :"..table.concat(args, " ", 2).."\n")
			end
		end,
		privs = {"Admin"},
		args = "<target> <new nick>",
	},

	["RELOAD"] = {
		func = function(con, user, cmd, args, resp)
			if #args == 0 then
				config_file:seek("set", 0)
				local success, value_or_err = pcall(json.decode, config_file:read("*a"))
				if success then
					config = value_or_err
					con:send(":1HC000000 NOTICE "..resp.." :Successfully reloaded config.json\n")
				else
					con:send(":1HC000000 NOTICE "..resp.." :Unable to reload config.json, check /dev/stdout for details.\n")
					print("config.json")
					print(value_or_err)
				end

				for file, _ in pairs(config.files) do
					local success, err = pcall(dofile, path..file)
					if success then
						con:send(":1HC000000 NOTICE "..resp.." :Successfully reloaded "..file.."\n")
					else
						con:send(":1HC000000 NOTICE "..resp.." :Unable to reload "..file..", check /dev/stdout for details.\n")
						print(file)
						print(err)
					end
				end
			else
				local file = args[1]..".lua"
				if config.files[file] then
					local success, err = pcall(dofile, path..file)
					if success then
						con:send(":1HC000000 NOTICE "..resp.." :Successfully reloaded "..file.."\n")
					else
						con:send(":1HC000000 NOTICE "..resp.." :Unable to reload "..file..", check /dev/stdout for details.\n")
						print(file)
						print(err)
					end
				elseif args[1] == "config" then
					config_file:seek("set", 0)
					local success, value_or_err = pcall(json.decode, config_file:read("*a"))
					if success then
						config = value_or_err
						con:send(":1HC000000 NOTICE "..resp.." :Successfully reloaded config.json\n")
					else
						con:send(":1HC000000 NOTICE "..resp.." :Unable to reload config.json, check /dev/stdout for details.\n")
						print("CoupServConfig.json")
						print(value_or_err)
					end
				else
					con:send(":1HC000000 NOTICE "..resp.." :Invalid section.\n")
				end
			end
		end,
		privs = {"Admin"},
		args = "[<section>]",
	},

	["RECONNECT"] = {
		func = function(con, user, cmd, args, resp)
			return true
		end,
		privs = {"Admin"},
	},

	[":"] = {
		func = function(con, user, cmd, args, resp)
			con:send(table.concat(args, " ").."\n")
		end,
		privs = {"Admin"},
		args = "<raw IRC message>",
	},

	["HELP"] = {
		func = function(con, user, cmd, args, resp)
			for command, tbl in pairs(commands) do
				if has_permission(userlist[user], tbl.privs) then
					if tbl.args then
						con:send(":1HC000000 NOTICE "..resp.." :"..command.." "..tbl.args.."\n")
					else
						con:send(":1HC000000 NOTICE "..resp.." :"..command.."\n")
					end
				end
			end
		end,
	},

	["SHUTDOWN"] = {
		func = function(con, user, cmd, args, resp)
			local crash_me = nil
			crash_me()
		end,
		privs = {"Owner"},
	},

	["SPAM"] = {
		func = function(con, user, cmd, args, resp)
			if #args < 3 then
				con:send(":1HC000000 NOTICE "..resp.." :Not enough args.\n")
			elseif tonumber(args[2]) == nil then
				con:send(":1HC000000 NOTICE "..resp.." :"..args[2]..": Not a valid positive integer.\n")
			elseif tonumber(args[2]) < 1 then
				con:send(":1HC000000 NOTICE "..resp.." :"..args[2]..": Not a valid positive integer.\n")
			elseif tonumber(args[2]) > 1000 then
				con:send(":1HC000000 NOTICE "..resp.." :"..args[2]..": Too large of a number, max is 1000.\n")
			else
				local msg = ":1HC000000 PRIVMSG "..args[1].." :"..table.concat(args, " ", 3).."\n"
				for i=1, tonumber(args[2]), 1 do
					con:send(msg)
				end
			end
		end,
		privs = {"Admin"},
		args = "<target> <count> <message>",
	},

	["OP"] = {
		func = function(con, user, cmd, args, resp)
			if resp:sub(1, 1) ~= "#" then
				con:send(":1HC000000 NOTICE "..resp.." :This command must be executed within a channel.\n")
				return
			end

			if #args == 0 then
				con:send(":1HC000000 MODE "..resp.." +o "..user.."\n")
			else
				con:send(":1HC000000 MODE "..resp.." +o "..args[1].."\n")
			end
		end,
		privs = {"Admin"},
		args = "[<target>]",
	},

	["GETUID"] = {
		func = function(con, user, cmd, args, resp)
			if #args == 0 then
				con:send(":1HC000000 NOTICE "..resp.." :"..user.."\n")
			else
				local nick = table.concat(args, " ")
				for uid, tbl in pairs(userlist) do
					if tbl.nick == nick then
						con:send(":1HC000000 NOTICE "..resp.." :"..uid.."\n")
						return
					end
				end
				con:send(":1HC000000 NOTICE "..resp.." :Nick not found.\n")
			end
		end,
		args = "[<nick>]",
	},

	["PRINT"] = {
		func = function(con, user, cmd, args, resp)
			if #args == 0 then
				con:send(":1HC000000 NOTICE "..resp.." :Not enough args.\n")
			else
				local list
				if args[1] == "userlist" then
					list = userlist
				elseif args[1] == "chanlist" then
					list = chanlist
				elseif args[1] == "servlist" then
					list = servlist
				else
					con:send(":1HC000000 NOTICE "..resp.." :Unknown list.\n")
					return
				end

				for k, v in pairs(list) do
					local msg = {}
					for key, val in pairs(v) do
						table.insert(msg, "["..(type(key) == "string" and ("%q"):format(key) or tostring(key)).."] = "..(type(val) == "string" and ("%q"):format(val) or tostring(val)))
					end
					print("["..(type(k) == "string" and ("%q"):format(k) or tostring(k)).."] = {"..table.concat(msg, ", ").."}")
				end
			end
		end,
		privs = {"Admin"},
		args = "<list>",
	},

	["GETUSERINFO"] = {
		func = function(con, user, cmd, args, resp)
			if #args == 0 then
				args[1] = source
			end

			if userlist[args[1]] then
				local msg = {}
				for key, val in pairs(userlist[args[1]]) do
					table.insert(msg, "["..(type(key) == "string" and ("%q"):format(key) or tostring(key)).."] = "..(type(val) == "string" and ("%q"):format(val) or tostring(val)))
				end
				con:send(":1HC000000 PRIVMSG "..resp.." :{"..table.concat(msg, ", ").."}\n")
			else
				con:send(":1HC000000 PRIVMSG "..resp.." :Nonexistant UID\n")
			end
		end,
		privs = {"Admin"},
		args = "[<UID>]",
	},

	["GETNICK"] = {
		func = function(con, user, cmd, args, resp)
			if userlist[args[1]] then
				con:send(":1HC000000 NOTICE "..resp.." :"..userlist[args[1]].nick.."\n")
			else
				con:send(":1HC000000 NOTICE "..resp.." :Nonexistant UID\n")
			end
		end,
		args = "[<UID>]",
	},

	["JUPE"] = {
		func = function(con, user, cmd, args, resp)
			if #args == 0 then
				con:send(":1HC000000 NOTICE "..resp.." :Not enough args.\n")
			else
				for id, tbl in pairs(servlist) do
					if tbl.address == args[1] then
						con:send("RSQUIT "..args[1].." :"..table.concat(args, " ", 2).."\n")
						con:send(":1HC SERVER "..args[1].." * 0 "..id.." :Juped.\n")
						return
					end
				end
				con:send(":1HC000000 NOTICE "..resp.." :Server not found.\n")
			end
		end,
		privs = {"Admin"},
		args = "<server>",
	},

	["ALLOW"] = {
		func = function(con, user, cmd, args, resp)
			if #args == 0 then
				con:send(":1HC000000 NOTICE "..resp.." :Not enough args.\n")
			else
				for id, tbl in pairs(userlist) do
					if tbl.nick == args[1] then
						userlist[id].opertype = "Admin"
						con:send(":1HC000000 NOTICE "..resp.." :"..args[1].." is now considered an oper.\n")
						return
					end
				end
				con:send(":1HC000000 NOTICE "..resp.." :Nick not found.\n")
			end
		end,
		privs = {"Admin"},
		args = "<user>",
	},

	["DENY"] = {
		func = function(con, user, cmd, args, resp)
			if #args == 0 then
				con:send(":1HC000000 NOTICE "..resp.." :Not enough args.\n")
			else
				for id, tbl in pairs(userlist) do
					if tbl.nick == args[1] then
						userlist[id].opertype = nil
						con:send(":1HC000000 MODE "..id.." -o\n")
						con:send(":1HC000000 NOTICE "..resp.." :"..args[1].." is no longer an oper.\n")
						return
					end
				end
				con:send(":1HC000000 NOTICE "..resp.." :Nick not found.\n")
			end
		end,
		privs = {"Admin"},
		args = "<user>",
	},
}