/
commands.lua
292 lines (227 loc) · 9.49 KB
/
commands.lua
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
-- WolfAdmin module for Wolfenstein: Enemy Territory servers.
-- Copyright (C) 2015-2017 Timo 'Timothy' Smit
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- at your option any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
local auth = require (wolfa_getLuaPath()..".auth.auth")
local util = require (wolfa_getLuaPath()..".util.util")
local events = require (wolfa_getLuaPath()..".util.events")
local files = require (wolfa_getLuaPath()..".util.files")
local logs = require (wolfa_getLuaPath()..".util.logs")
local tables = require (wolfa_getLuaPath()..".util.tables")
local commands = {}
local clientcmds = {}
local servercmds = {}
local admincmds = {}
function commands.getclient(command)
if command then
return clientcmds[command]
end
return clientcmds
end
function commands.getserver(command)
if command then
return servercmds[command]
end
return servercmds
end
function commands.getadmin(command)
if command then
return admincmds[command]
end
return admincmds
end
function commands.addclient(command, func, flag, syntax, chat, disabled)
if disabled then
return
end
clientcmds[command] = {
["function"] = func,
["flag"] = flag,
["syntax"] = "^7"..command..(syntax and " "..syntax or ""),
["chat"] = chat,
}
end
function commands.addserver(command, func, disabled)
if disabled then
return
end
servercmds[command] = {
["function"] = func,
}
end
function commands.addadmin(command, func, flag, help, syntax, hidden, disabled)
if disabled then
return
end
admincmds[command] = {
["function"] = func,
["flag"] = flag,
["help"] = help or "N/A",
["syntax"] = "^2!"..command..(syntax and " "..syntax or ""),
["hidden"] = hidden
}
end
function commands.loadFiles(dir)
local amount = 0
local files = files.ls("commands/"..dir.."/")
for _, file in pairs(files) do
if string.match(string.lower(file), "^[a-z0-9]+%.lua$") then
require (wolfa_getLuaPath()..".commands."..dir.."."..string.sub(file, 1, string.len(file) - 4))
amount = amount + 1
end
end
return amount
end
function commands.load()
local functionStart = et.trap_Milliseconds()
local clientAmount = commands.loadFiles("client")
local serverAmount = commands.loadFiles("server")
local adminAmount = commands.loadFiles("admin")
local totalAmount = clientAmount + serverAmount + adminAmount
outputDebug("commands.load(): "..totalAmount.." entries loaded in "..et.trap_Milliseconds() - functionStart.." ms")
return totalAmount
end
function commands.log(clientId, command, victim, ...)
local victimId
-- funny, NoQuarter actually checks EACH command for a victim (so even
-- !help [playername] will log a victimname). so why not do the same :D
-- todo: do this more nicely, maybe change .register() function
if victim then
local cmdClient
if tonumber(victim) == nil or tonumber(victim) < 0 or tonumber(victim) > tonumber(et.trap_Cvar_Get("sv_maxclients")) then
cmdClient = et.ClientNumberFromString(victim)
else
cmdClient = tonumber(victim)
end
if cmdClient ~= -1 and cmdClient ~= nil and et.gentity_get(cmdClient, "pers.netname") then
victimId = cmdClient
end
end
logs.writeAdmin(clientId, command, victimId, ...)
end
function commands.onGameInit()
commands.load()
end
events.handle("onGameInit", commands.onGameInit)
function commands.onServerCommand(command)
local wolfCmd = string.lower(command)
local args = {}
if servercmds[wolfCmd] and servercmds[wolfCmd]["function"] then
for i = 1, et.trap_Argc() - 1 do
table.insert(args, et.trap_Argv(i))
end
return servercmds[wolfCmd]["function"](wolfCmd, tables.unpack(args)) and 1 or 0
end
local shrubCmd = command
if string.find(command, "!") == 1 then
shrubCmd = string.lower(string.sub(command, 2, string.len(command)))
end
if admincmds[shrubCmd] and admincmds[shrubCmd]["function"] and admincmds[shrubCmd]["flag"] then
for i = 1, et.trap_Argc() - 1 do
table.insert(args, et.trap_Argv(i))
end
if not admincmds[shrubCmd]["hidden"] then
commands.log(-1337, shrubCmd, tables.unpack(args))
end
return admincmds[shrubCmd]["function"](-1337, shrubCmd, tables.unpack(args)) and 1 or 0
end
end
events.handle("onServerCommand", commands.onServerCommand)
function commands.onClientCommand(clientId, command)
local wolfCmd = string.lower(command)
local args = {}
-- mod-specific or custom commands loading
-- syntax: command arg1 arg2 ... argN
if clientcmds[wolfCmd] and clientcmds[wolfCmd]["function"] and clientcmds[wolfCmd]["flag"] then
if clientcmds[wolfCmd]["flag"] == "" or auth.isPlayerAllowed(clientId, clientcmds[wolfCmd]["flag"]) then
for i = 1, et.trap_Argc() - 1 do
table.insert(args, et.trap_Argv(i))
end
local isFinished = clientcmds[wolfCmd]["function"](clientId, wolfCmd, tables.unpack(args))
if isFinished ~= nil then
return isFinished and 1 or 0
end
end
end
-- client cmds
-- syntax: say or say_*
local clientCmd
if (wolfCmd == "say" or wolfCmd == "say_team" or wolfCmd == "say_buddy") and string.find(et.trap_Argv(1), "/") == 1 then
args = util.split(et.trap_Argv(1), " ")
-- say "/command arg1 arg2 argN"
if #args > 1 then
clientCmd = string.sub(args[1], 2, string.len(args[1]))
table.remove(args, 1)
-- say /command arg1 arg2 argN
else
clientCmd = string.sub(et.trap_Argv(1), 2, string.len(et.trap_Argv(1)))
for i = 2, et.trap_Argc() - 1 do
table.insert(args, et.trap_Argv(i))
end
if args[1] == et.trap_Argv(1) then table.remove(args, 1) end
end
end
-- handle client cmds
if clientCmd then
clientCmd = string.lower(clientCmd)
if clientcmds[clientCmd] and clientcmds[clientCmd]["function"] and clientcmds[clientCmd]["chat"] then
if clientcmds[clientCmd]["flag"] == "" or auth.isPlayerAllowed(clientId, clientcmds[clientCmd]["flag"]) then
return clientcmds[clientCmd]["function"](clientId, clientCmd, tables.unpack(args)) and 1 or 0
end
end
end
-- shrub cmds
local shrubCmd
-- syntax: say or say_*
if (wolfCmd == "say" or wolfCmd == "say_team" or wolfCmd == "say_buddy") and string.find(et.trap_Argv(1), "!") == 1 then
args = util.split(et.trap_Argv(1), " ")
-- syntax: say "!command arg1 arg2 ... argN"
if #args > 1 then
shrubCmd = string.sub(args[1], 2, string.len(args[1]))
table.remove(args, 1)
-- syntax: say !command arg1 arg2 ... argN
else
shrubCmd = string.sub(et.trap_Argv(1), 2, string.len(et.trap_Argv(1)))
for i = 2, et.trap_Argc() - 1 do
table.insert(args, et.trap_Argv(i))
end
if args[1] == et.trap_Argv(1) then table.remove(args, 1) end
end
-- syntax: !command arg1 arg2 ... argN
elseif string.find(wolfCmd, "!") == 1 then
shrubCmd = string.sub(wolfCmd, 2, string.len(wolfCmd))
for i = 1, et.trap_Argc() - 1 do
table.insert(args, et.trap_Argv(i))
end
end
-- handle shrub commands
if shrubCmd then
shrubCmd = string.lower(shrubCmd)
if admincmds[shrubCmd] and admincmds[shrubCmd]["function"] and admincmds[shrubCmd]["flag"] then
if wolfCmd == "say" or (((wolfCmd == "say_team" and et.gentity_get(cmdClient, "sess.sessionTeam") ~= et.TEAM_SPECTATORS) or wolfCmd == "say_buddy") and auth.isPlayerAllowed(clientId, auth.PERM_TEAMCMDS)) or (wolfCmd == "!"..shrubCmd and auth.isPlayerAllowed(clientId, auth.PERM_SILENTCMDS)) then
if admincmds[shrubCmd]["flag"] ~= "" and auth.isPlayerAllowed(clientId, admincmds[shrubCmd]["flag"]) then
local isFinished = admincmds[shrubCmd]["function"](clientId, shrubCmd, tables.unpack(args))
if not admincmds[shrubCmd]["hidden"] then
commands.log(clientId, shrubCmd, tables.unpack(args))
end
if isFinished and "!"..shrubCmd == wolfCmd then -- silent command via console, removes "unknown command" message
return 1
end
else
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \""..shrubCmd..": permission denied\";")
end
end
end
end
return 0
end
events.handle("onClientCommand", commands.onClientCommand)
return commands