/
commands.lua
336 lines (263 loc) · 12 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
-- WolfAdmin module for Wolfenstein: Enemy Territory servers.
-- Copyright (C) 2015-2016 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/>.
require "luascripts.wolfadmin.util.debug"
local util = require "luascripts.wolfadmin.util.util"
local events = require "luascripts.wolfadmin.util.events"
local files = require "luascripts.wolfadmin.util.files"
local admin = require "luascripts.wolfadmin.admin.admin"
local stats = require "luascripts.wolfadmin.players.stats"
local commands = {}
-- available shrubflags: lqyFHY
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)
clientcmds[command] = {
["function"] = func,
["flag"] = flag,
["syntax"] = "^7"..command..(syntax and " "..syntax or ""),
["chat"] = chat,
}
end
function commands.addserver(command, func)
servercmds[command] = {
["function"] = func,
}
end
function commands.addadmin(command, func, flag, help, syntax, hidden)
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-z]+%.lua$") then
require("luascripts/wolfadmin/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, cmdArguments)
local functionStart = et.trap_Milliseconds()
local fileDescriptor = files.open(et.trap_Cvar_Get("g_logAdmin"), et.FS_APPEND)
local logLine
local levelTime = wolfa_getLevelTime() / 1000
local clientGUID = clientId and stats.get(clientId, "playerGUID") or "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
local clientName = clientId and stats.get(clientId, "playerName") or "console"
local clientFlags = ""
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 cmdArguments[1] then
local cmdClient
if tonumber(cmdArguments[1]) == nil then
cmdClient = et.ClientNumberFromString(cmdArguments[1])
else
cmdClient = tonumber(cmdArguments[1])
end
if cmdClient ~= -1 and et.gentity_get(cmdClient, "pers.netname") then
victimId = cmdClient
end
end
if victimId then
local victimName = stats.get(victimId, "playerName")
logLine = string.format("%3i:%02i: %i: %s: %s: %s: %s: %s: %s: \"%s\"\n", math.floor(levelTime / 60), (levelTime % 60), clientId, clientGUID, clientName, clientFlags, command, victimId, victimName, table.concat(cmdArguments, " ", 2))
else
logLine = string.format("%3i:%02i: %i: %s: %s: %s: %s: \"%s\"\n", math.floor(levelTime / 60), (levelTime % 60), clientId, clientGUID, clientName, clientFlags, command, table.concat(cmdArguments, " "))
end
et.trap_FS_Write(logLine, string.len(logLine), fileDescriptor)
et.trap_FS_FCloseFile(fileDescriptor)
end
function commands.oninit()
commands.load()
end
events.handle("onGameInit", commands.oninit)
function commands.onservercommand(cmdText)
local wolfCmd = string.lower(et.trap_Argv(0))
local cmdArguments = {}
if servercmds[wolfCmd] and servercmds[wolfCmd]["function"] then
for i = 1, et.trap_Argc() - 1 do
cmdArguments[i] = et.trap_Argv(i)
end
return servercmds[wolfCmd]["function"](clientId, cmdArguments) and 1 or 0
end
-- TODO: merge with commands.onclientcommand
local shrubCmd = cmdText
if string.find(cmdText, "!") == 1 then
shrubCmd = string.lower(string.sub(cmdText, 2, string.len(cmdText)))
end
if admincmds[shrubCmd] and admincmds[shrubCmd]["function"] and admincmds[shrubCmd]["flag"] then
for i = 1, et.trap_Argc() - 1 do
cmdArguments[i] = et.trap_Argv(i)
end
admincmds[shrubCmd]["function"](-1337, cmdArguments)
if not admincmds[shrubCmd]["hidden"] then
commands.log(-1, shrubCmd, cmdArguments)
end
end
end
events.handle("onServerCommand", commands.onservercommand)
function commands.onclientcommand(clientId, cmdText)
local wolfCmd = string.lower(et.trap_Argv(0))
local cmdArguments = {}
-- mod-specific or custom commands loading
if clientcmds[wolfCmd] and clientcmds[wolfCmd]["function"] and clientcmds[wolfCmd]["flag"] then
if clientcmds[wolfCmd]["flag"] == "" or et.G_shrubbot_permission(clientId, clientcmds[wolfCmd]["flag"]) == 1 then
for i = 1, et.trap_Argc() - 1 do
cmdArguments[i] = et.trap_Argv(i)
end
return clientcmds[wolfCmd]["function"](clientId, cmdArguments) and 1 or 0
end
end
-- all Wolfenstein-related commands defined separately for now
if wolfCmd == "team" then
if admin.isPlayerLocked(clientId) then
local clientTeam = tonumber(et.gentity_get(clientId, "sess.sessionTeam"))
local teamName = util.getTeamName(clientTeam)
local teamColor = util.getTeamColor(clientTeam)
et.trap_SendServerCommand(clientId, "cp \"^7You are locked to the "..teamColor..teamName.." ^7team")
return 1
end
stats.set(clientId, "currentKillSpree", 0)
stats.set(clientId, "currentDeathSpree", 0)
stats.set(clientId, "currentReviveSpree", 0)
elseif wolfCmd == "callvote" then
local voteArguments = {}
for i = 2, et.trap_Argc() - 1 do
voteArguments[(i - 1)] = et.trap_Argv(i)
end
return events.trigger("onCallvote", clientId, et.trap_Argv(1), voteArguments)
elseif wolfCmd == "say" or wolfCmd == "say_team" or wolfCmd == "say_teamnl" or wolfCmd == "say_buddy" then
if et.gentity_get(clientId, "sess.muted") == 1 then
et.trap_SendServerCommand(clientId, "cp \"^1You are muted\"")
return 1
end
elseif wolfCmd == "vsay" or wolfCmd == "vsay_team" then
if admin.isVoiceMuted(clientId) then
et.trap_SendServerCommand(clientId, "cp \"^1You are voicemuted\"")
return 1
end
end
-- client cmds
local clientCmd = nil
-- say and say_*
if (wolfCmd == "say" or wolfCmd == "say_team" or wolfCmd == "say_buddy") and string.find(et.trap_Argv(1), "/") == 1 then
cmdArguments = util.split(et.trap_Argv(1), " ")
-- say "/command param1 param2 paramN"
if #cmdArguments > 1 then
clientCmd = string.sub(cmdArguments[1], 2, string.len(cmdArguments[1]))
table.remove(cmdArguments, 1)
-- say /command param1 param2 paramN
else
clientCmd = string.sub(et.trap_Argv(1), 2, string.len(et.trap_Argv(1)))
for i = 2, et.trap_Argc() - 1 do
cmdArguments[(i - 1)] = et.trap_Argv(i)
end
if cmdArguments[1] == et.trap_Argv(1) then table.remove(cmdArguments, 1) end
end
-- !command
elseif string.find(wolfCmd, "!") == 1 then
clientCmd = string.sub(wolfCmd, 2, string.len(wolfCmd))
for i = 1, et.trap_Argc() - 1 do
cmdArguments[i] = et.trap_Argv(i)
end
end
if clientCmd then
clientCmd = string.lower(clientCmd)
if clientcmds[clientCmd] and clientcmds[clientCmd]["function"] and clientcmds[clientCmd]["chat"] then
if clientcmds[clientCmd]["flag"] == "" or et.G_shrubbot_permission(clientId, clientcmds[clientCmd]["flag"]) == 1 then
return clientcmds[clientCmd]["function"](clientId, cmdArguments) and 1 or 0
end
end
end
-- shrub cmds
local shrubCmd = nil
-- say and say_*
if (wolfCmd == "say" or wolfCmd == "say_team" or wolfCmd == "say_buddy") and string.find(et.trap_Argv(1), "!") == 1 then
cmdArguments = util.split(et.trap_Argv(1), " ")
-- say "!command param1 param2 paramN"
if #cmdArguments > 1 then
shrubCmd = string.sub(cmdArguments[1], 2, string.len(cmdArguments[1]))
table.remove(cmdArguments, 1)
-- say !command param1 param2 paramN
else
shrubCmd = string.sub(et.trap_Argv(1), 2, string.len(et.trap_Argv(1)))
for i = 2, et.trap_Argc() - 1 do
cmdArguments[(i - 1)] = et.trap_Argv(i)
end
if cmdArguments[1] == et.trap_Argv(1) then table.remove(cmdArguments, 1) end
end
-- !command
elseif string.find(wolfCmd, "!") == 1 then
shrubCmd = string.sub(wolfCmd, 2, string.len(wolfCmd))
for i = 1, et.trap_Argc() - 1 do
cmdArguments[i] = et.trap_Argv(i)
end
end
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 et.G_shrubbot_permission(clientId, "9") == 1) or (wolfCmd == "!"..shrubCmd and et.G_shrubbot_permission(clientId, "3") == 1) then
if admincmds[shrubCmd]["flag"] ~= "" and et.G_shrubbot_permission(clientId, admincmds[shrubCmd]["flag"]) == 1 then
local isFinished = admincmds[shrubCmd]["function"](clientId, cmdArguments)
if not admincmds[shrubCmd]["hidden"] then
commands.log(clientId, shrubCmd, cmdArguments)
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