-
Notifications
You must be signed in to change notification settings - Fork 1
/
sourcemod.lua
356 lines (324 loc) · 12.8 KB
/
sourcemod.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
--[[
~ SourceMod Replacement Service ~
Copyright (c) 2011 Lexi Robinson
This script is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
This script 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 Lesser General Public License
for more details.
You should have received a copy of the GNU Lesser General Public License
along with this script. If not, see <https://www.gnu.org/licenses/>.
*WARNING* This is not a module! Put it in /lua/autorun!
This file provides various concommands from SourceMod/SourceBans
v1.0.0-alpha1
--]]
local sourcebans = require "sourcebans"
-- Edit this config to match the one the website gives you
sourcebans.SetConfig("hostname", "localhost") -- Database Hostname
sourcebans.SetConfig("username", "root") -- Database Login name
sourcebans.SetConfig("password", "") -- Database Login Password
sourcebans.SetConfig("database", "sourcebans") -- Database 'database' or 'schema' selection
sourcebans.SetConfig("dbprefix", "sbans") -- Prefix for tables in the database. (This example would say your tables are called sbans_bans and so on)
sourcebans.SetConfig("portnumb", 3306) -- Database Port number
sourcebans.SetConfig("serverid", 1) -- The ID given to this server by the SourceBans website
sourcebans.SetConfig("website", "bans.example.com") -- The URL where people can find your sourcebans install (Do not put http:// or the kick reason will break!)
sourcebans.SetConfig("showbanreason", false) -- Show the ban reason in the kick message. Do not use if you do not have gatekeeper installed or you will crash people sometimes.
sourcebans.SetConfig("dogroups", false) -- Set user groups or not. Set this to false unless your admins are in a servergroup called 'Admin' and your superadmins are in 'SuperAdmin'.
-- Do not edit below this line
sourcebans.Activate()
local table, os, string, player, concommand = table, os, string, player, concommand
local tonumber, pairs, Msg, ServerLog = tonumber, pairs, Msg, ServerLog
local HUD_PRINTCONSOLE, HUD_PRINTTALK, HUD_PRINTCENTER = HUD_PRINTCONSOLE, HUD_PRINTTALK, HUD_PRINTCENTER
local function notifymessage(...)
local words = table.concat({ "[", os.date(), "][sourcemod.lua] ", ... }, "") .. "\n"
ServerLog(words)
Msg(words)
end
-- Borrowed from my gamemode, Applejack
local function playerGet(id)
local res, len, name, num, pname, lon
id = string.Trim(id)
name = string.lower(id)
num = tonumber(id)
id = string.upper(id)
for _, ply in pairs(player.GetAll()) do
pname = ply:Name():lower()
if ply:UserID() == num or ply:SteamID() == id or pname == name then
return ply
elseif string.find(pname, name, 1, true) then
lon = pname:len()
if res then
if lon < len then
res = ply
len = lon
end
else
res = ply
len = lon
end
end
end
return res
end
local function complain(ply, msg, lvl)
if not (ply and ply:IsValid()) then
print(msg)
else
ply:PrintMessage(lvl or HUD_PRINTCONSOLE, msg)
end
return false
end
local function not4u(ply, cmd)
ply:ChatPrint("Unknown Command: '" .. cmd .. "'\n")
end
-- Gets a steamID from concommand calls that don't bother to quote it.
local function getSteamID(tab)
local a, b, c, d, e = tab[1], tab[2], tonumber(tab[3]), tab[4], tonumber(tab[5])
if string.find(a, "STEAM_%d:%d:%d+") then
return table.remove(tab, 1)
elseif string.find(a, "STEAM_") and b == ":" and c and d == ":" and e then
-- Kill the five entries as if they were one
table.remove(tab, 1)
table.remove(tab, 1)
table.remove(tab, 1)
table.remove(tab, 1)
table.remove(tab, 1)
return a .. b .. c .. d .. e
end
end
-- Check if a player has authorisation to run the command.
local function authorised(ply, flag)
if not (ply and ply:IsValid()) then
return true
elseif not ply.sourcebansinfo then
return false
end
return ply.sourcebansinfo.zflag or string.find(ply.sourcebansinfo.srv_flags, flag)
end
local function complainPlayer(ply, pl, usage)
if not pl then
return complain(ply, "Invalid player!" .. usage)
end
return true
end
local function complainTime(ply, time, usage)
if not time or time < 0 then
return complain(ply, "Invalid time!" .. usage)
elseif time == 0 and not authorised(ply, sourcebans.FLAG_PERMA) then
return complain(ply, "You are not authorised to permaban!")
end
return true
end
local function complainReason(ply, reason, usage)
if reason == "" then
return complain(ply, "Invalid reason!" .. usage)
end
return true
end
local function complainer(ply, pl, time, reason, usage)
return complainPlayer(ply, pl, usage)
and complainTime(ply, time, usage)
and complainReason(ply, reason, usage)
end
concommand.Add("sm_rehash", function(ply, cmd)
if ply:IsValid() then return not4u(ply, cmd); end
notifymessage("Rehash command recieved, reloading admins:")
sourcebans.ReloadAdmins()
end, nil, "Reload the admin list from the SQL")
local usage = "\n - Usage: sm_psay <#userid|name|steamid> <words>"
concommand.Add("sm_psay", function(ply, _, args)
if #args < 2 then
return complain(ply, usage:sub(4))
elseif not authorised(ply, sourcebans.FLAG_CHAT) then
return complain(ply, "You do not have access to this command!")
end
local pl, words = table.remove(args, 1), table.concat(args, " "):Trim()
pl = playerGet(pl)
if not pl then
return complain(ply, "Invalid player!" .. usage)
elseif words == "" then
return complain(ply, "Invalid message!" .. usage)
end
local name1 = ply:IsValid() and ply:Name() or "CONSOLE"
local name2 = pl:Name()
complain(ply, "( Private: " .. name2 .. " ) " .. name1 .. ": " .. words, HUD_PRINTTALK)
complain(pl, "( Private: " .. name2 .. " ) " .. name1 .. ": " .. words, HUD_PRINTTALK)
notifymessage(name1, " triggered sm_psay to ", name2, " ( text ", words, " )")
end, nil, "Sends a private message to a player" .. usage)
local usage = "\n - Usage: sm_say <words>"
concommand.Add("sm_say", function(ply, _, args)
if #args < 1 then
return complain(ply, usage:sub(4))
elseif not authorised(ply, sourcebans.FLAG_CHAT) then
return complain(ply, "You do not have access to this command!")
end
local words = table.concat(args, " "):Trim()
if words == "" then
return complain(ply, "Invalid message!" .. usage)
end
local name1 = ply:IsValid() and ply:Name() or "CONSOLE"
for _, pl in pairs(player.GetAll()) do
if pl:IsValid() and not pl:IsBot() then
complain(pl, name1 .. ": " .. words, HUD_PRINTTALK)
end
end
notifymessage(name1, " triggered sm_say ( text ", words, " )")
end, nil, "Sends a message to everyone" .. usage)
local usage = "\n - Usage: sm_csay <words>"
concommand.Add("sm_csay", function(ply, _, args)
if #args < 1 then
return complain(ply, usage:sub(4))
elseif not authorised(ply, sourcebans.FLAG_CHAT) then
return complain(ply, "You do not have access to this command!")
end
local words = table.concat(args, " "):Trim()
if words == "" then
return complain(ply, "Invalid message!" .. usage)
end
local name1 = ply:IsValid() and ply:Name() or "CONSOLE"
for _, pl in pairs(player.GetAll()) do
if pl:IsValid() and not pl:IsBot() then
complain(pl, name1 .. ": " .. words, HUD_PRINTCENTER)
end
end
notifymessage(name1, " triggered sm_csay ( text ", words, " )")
end, nil, "Sends a message to everyone in the center of their screen" .. usage)
local usage = "\n - Usage: sm_chat <words>"
concommand.Add("sm_chat", function(ply, _, args)
if #args < 1 then
return complain(ply, usage:sub(4))
elseif not authorised(ply, sourcebans.FLAG_CHAT) then
return complain(ply, "You do not have access to this command!")
end
local words = table.concat(args, " "):Trim()
if words == "" then
return complain(ply, "Invalid message!" .. usage)
end
local name1 = ply:IsValid() and ply:Name() or "CONSOLE"
for _, pl in pairs(player.GetAll()) do
if pl:IsValid() and pl:IsAdmin() then
complain(pl, "( ADMINS ) " .. name1 .. ": " .. words, HUD_PRINTTALK)
end
end
notifymessage(name1, " triggered sm_chat ( text ", words, " )")
end, nil, "Sends a message to all online admins" .. usage)
local usage = "\n - Usage: sm_ban <#userid|name> <minutes|0> <reason>"
concommand.Add("sm_ban", function(ply, _, args)
if #args < 3 then
return complain(ply, usage:sub(4))
elseif not sourcebans.IsActive() then
return complain(ply, "Sourcebans has not been activated! Your command could not be completed.")
elseif not authorised(ply, sourcebans.FLAG_BAN) then
return complain(ply, "You do not have access to this command!")
end
local pl, time, reason = table.remove(args, 1), tonumber(table.remove(args, 1)), table.concat(args, " "):Trim()
pl = playerGet(pl)
if not complainer(ply, pl, time, reason, usage) then
return
end
local name = pl:Name()
local function callback(res, err)
if res then
complain(ply, "sm_ban: " .. name .. " has been banned successfully.")
else
complain(ply, "sm_ban: " .. name .. " has not been banned. " .. err)
end
end
sourcebans.BanPlayer(pl, time * 60, reason, ply, callback)
complain(ply, "sm_ban: Your ban request has been sent to the database.")
end, nil, "Bans a player" .. usage)
-- Author's note: Why would you want to only ban someone by only their IP when you have their SteamID? This is a stupid concommand. Hopefully no one will use it.
local usage = "\n - Usage: sm_banip <ip|#userid|name> <minutes|0> <reason>"
concommand.Add("sm_banip", function(ply, _, args)
if #args < 3 then
return complain(ply, usage:sub(4))
elseif not sourcebans.IsActive() then
return complain(ply, "Sourcebans has not been activated! Your command could not be completed.")
elseif not authorised(ply, sourcebans.FLAG_BAN) then
return complain(ply, "You do not have access to this command!")
end
local id, time, reason = table.remove(args, 1), tonumber(table.remove(args, 1)), table.concat(args, " "):Trim()
local pl
if string.find(id, "%d+%.%d+%.%d+%.%d+") then
id = string.match(id, "(%d+%.%d+%.%d+%.%d+)")
for _, ply in pairs(player.GetAll()) do
if ply:IPAddress() == id then
pl = ply
break
end
end
-- FIXME: If there is no player connected on this ip but ply has FLAG_RCON, this should add the IP on its own.
else
pl = playerGet(id)
if pl then
id = ply:IPAddress()
end
end
if not complainer(ply, pl, time, reason, usage) then
return
end
local function callback(res, err)
if res then
complain(ply, "sm_banip: " .. id .. " has been banned successfully.")
else
complain(ply, "sm_banip: " .. id .. " has not been banned. " .. err)
end
end
sourcebans.BanPlayer(pl, time * 60, reason, ply, callback)
complain(ply, "sm_banip: Your ban request has been sent to the database.")
end, nil, "Bans a player by only their IP" .. usage)
local usage = "\n - Usage: sm_addban <minutes|0> <steamid> <reason>"
concommand.Add("sm_addban", function(ply, _, args)
if #args < 3 then
return complain(ply, usage:sub(4))
elseif not sourcebans.IsActive() then
return complain(ply, "Sourcebans has not been activated! Your command could not be completed.")
elseif not authorised(ply, sourcebans.FLAG_ADDBAN) then
return complain(ply, "You do not have access to this command!")
end
local time, id, reason = tonumber(table.remove(args, 1)), getSteamID(args), table.concat(args, " "):Trim()
if not id then
return complain(ply, "Invalid SteamID!" .. usage)
elseif not (complainTime(ply, time, usage) and complainReason(ply, reason, usage)) then
return
end
local function callback(res, err)
if res then
complain(ply, "sm_addban: " .. id .. " has been banned successfully.")
else
complain(ply, "sm_addban: " .. id .. " has not been banned. " .. err)
end
end
sourcebans.BanPlayerBySteamID(id, time * 60, reason, ply, "", callback)
complain(ply, "sm_addban: Your ban request has been sent to the database.")
end, nil, "Bans a player by their SteamID" .. usage)
local usage = "\n - Usage: sm_unban <steamid|ip> <reason>"
concommand.Add("sm_unban", function(ply, _, args)
if #args < 2 then
return complain(ply, usage:sub(4))
elseif not sourcebans.IsActive() then
return complain(ply, "Sourcebans has not been activated! Your command could not be completed.")
elseif not authorised(ply, sourcebans.FLAG_UNBAN) then
return complain(ply, "You do not have access to this command!")
end
local id, reason, func
if string.find(args[1], "%d+%.%d+%.%d+%.%d+") then
id = table.remove(args, 1)
func = sourcebans.UnbanPlayerByIPAddress
else
id = getSteamID(args)
func = sourcebans.UnbanPlayerBySteamID
end
if not id then
return complain(ply, "Invalid SteamID!" .. usage)
end
reason = table.concat(args, " "):Trim()
if not complainReason(ply, reason, usage) then
return
end
func(id, reason, ply)
complain(ply, "Your unban request has been sent to the database.")
end, nil, "Unbans a player" .. usage)