· 6 years ago · Jul 20, 2019, 04:58 PM
1#include <sourcemod>
2#include <tf2_stocks>
3
4Database g_hDatabase;
5int g_iBanTime[MAXPLAYERS+1];
6Handle g_hTimer[MAXPLAYERS+1];
7
8public Plugin myinfo = {
9 name = "[TF2Jail] Guard Bans!",
10 author = "Sgt. Gremulock",
11 description = "Guard bans for TF2 Jailbreak.",
12 version = "1.0.1",
13 url = "http://sourcemod.net"
14};
15
16public void OnPluginStart()
17{
18 Database.Connect(DB_OnConnection, "guardbans");
19
20 RegAdminCmd("sm_guardban", Command_GuardBan, ADMFLAG_BAN);
21 RegAdminCmd("sm_guardunban", Command_GuardUnban, ADMFLAG_BAN);
22
23 HookEvent("player_team", Event_PlayerTeam);
24
25 LoadTranslations("common.phrases");
26}
27
28public void OnClientPostAdminCheck(int client)
29{
30 CheckGuardBan(client);
31}
32
33public void OnClientDisconnect(int client)
34{
35 g_iBanTime[client] = 0;
36 delete g_hTimer[client];
37}
38
39public void OnClientSettingsChanged(int client)
40{
41 if (g_iBanTime[client])
42 {
43 UpdateName(client);
44 }
45}
46
47public Action Command_GuardBan(int client, int args)
48{
49 if (args < 2)
50 {
51 ReplyToCommand(client, "[SM] Usage: sm_guardban <target> <time> <reason>");
52 } else {
53 char arg1[32];
54 GetCmdArg(1, arg1, sizeof(arg1));
55 int target = FindTarget(client, arg1);
56 if (target == -1)
57 {
58 return Plugin_Handled;
59 }
60
61 char arg2[11];
62 GetCmdArg(2, arg2, sizeof(arg2));
63 int time = StringToInt(arg2);
64 if (!time || time > 300)
65 {
66 ReplyToCommand(client, "[SM] Invalid time specified.");
67 return Plugin_Handled;
68 }
69
70 char reason[256];
71 if (args >= 3)
72 {
73 GetCmdArg(3, reason, sizeof(reason));
74 if (args > 3)
75 {
76 char buffer[255];
77 for (int i = 4; i <= args; i++)
78 {
79 GetCmdArg(i, buffer, sizeof(buffer));
80 Format(reason, sizeof(reason), "%s %s", reason, buffer);
81 }
82 }
83 } else {
84 Format(reason, sizeof(reason), "No reason given.");
85 }
86
87 int userid = GetClientUserId(target);
88 bool console = client == 0;
89 char steamid[2][32];
90 if (!GetClientAuthId(target, AuthId_Steam2, steamid[0], 32))
91 {
92 LogError("Failure retrieving the SteamID of target %N (UserID: %i) Callback: Command_GuardBan()", target, userid);
93 } else if (!console && !GetClientAuthId(client, AuthId_Steam2, steamid[1], 32)) {
94 LogError("Failure retrieving the SteamID of client %N (UserID: %i) Callback: Command_GuardBan()", client, GetClientUserId(client));
95 } else {
96 char ip[32], query[1024];
97 GetClientIP(target, ip, sizeof(ip));
98 if (g_iBanTime[target])
99 {
100 g_hDatabase.Format(query, sizeof(query), "UPDATE `guardbans` SET `time` = '%i', `timeleft` = '%i' WHERE `ip` = '%s';", time, time, ip);
101 ShowActivity2(client, "[SM] ", "Updated the guard ban of %N to %i minutes (Reason: %s)", target, time, reason);
102 } else {
103 g_hDatabase.Format(query, sizeof(query), "REPLACE INTO `guardbans` (`status`, `steamid`, `name`, `ip`, `time`, `timeleft`, `ban_admin_name`, `ban_admin_steamid`, `ban_reason`, `ban_date`) VALUES ('1', '%s', '%N', '%s', '%i', '%i', '%N', '%s', '%s', CURRENT_TIMESTAMP);", steamid[0], target, ip, time, time, client, console ? "CONSOLE" : steamid[1], reason);
104 ShowActivity2(client, "[SM] ", "Guard banned %N for %i minutes (Reason: %s)", target, time, reason);
105 }
106
107 g_iBanTime[target] = time;
108 delete g_hTimer[target];
109 g_hTimer[target] = CreateTimer(60.0, Timer_Callback, target, TIMER_REPEAT);
110 if (TF2_GetClientTeam(target) == TFTeam_Blue)
111 {
112 TF2_ChangeClientTeam(target, TFTeam_Red);
113 }
114
115 g_hDatabase.Query(DB_OnInsertClient, query, userid);
116 }
117 }
118
119 return Plugin_Handled;
120}
121
122public Action Command_GuardUnban(int client, int args)
123{
124 if (!args)
125 {
126 ReplyToCommand(client, "[SM] Usage: sm_guardunban <target> <reason>");
127 } else {
128 char arg1[32];
129 GetCmdArg(1, arg1, sizeof(arg1));
130 int target = FindTarget(client, arg1);
131 if (target == -1)
132 {
133 return Plugin_Handled;
134 }
135
136 if (!g_iBanTime[target])
137 {
138 ReplyToCommand(client, "[SM] %N is not currently guard banned.", target);
139 return Plugin_Handled;
140 }
141
142 char reason[256];
143 if (args >= 2)
144 {
145 GetCmdArg(2, reason, sizeof(reason));
146 if (args > 2)
147 {
148 char buffer[255];
149 for (int i = 3; i <= args; i++)
150 {
151 GetCmdArg(i, buffer, sizeof(buffer));
152 Format(reason, sizeof(reason), "%s %s", reason, buffer);
153 }
154 }
155 } else {
156 Format(reason, sizeof(reason), "No reason given.");
157 }
158
159 char steamid[32];
160 bool console = client == 0;
161 if (!console && !GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid)))
162 {
163 LogError("Failure retrieving the SteamID of client %N (UserID: %i) Callback: Command_GuardUnban()", client, GetClientUserId(client));
164 } else {
165 OnClientDisconnect(target);
166 int userid = GetClientUserId(target);
167 char ip[32], query[1024];
168 GetClientIP(target, ip, sizeof(ip));
169 g_hDatabase.Format(query, sizeof(query), "UPDATE `guardbans` SET `status` = '-1', `unban_admin_name` = '%N', `unban_admin_steamid` = '%s', `unban_reason` = '%s', `unban_date` = CURRENT_TIMESTAMP WHERE `ip` = '%s';", client, console ? "CONSOLE" : steamid, reason, ip);
170 ShowActivity2(client, "[SM] ", "Removed %N's guard ban (Reason: %s)", target, reason);
171 g_hDatabase.Query(DB_OnRemoveClient, query, userid);
172 }
173 }
174
175 return Plugin_Handled;
176}
177
178public void Event_PlayerTeam(Event event, const char[] name, bool dontBroadcast)
179{
180 if (view_as<TFTeam>(event.GetInt("team")) == TFTeam_Blue)
181 {
182 int userid = event.GetInt("userid");
183 TFTeam oldteam = view_as<TFTeam>(event.GetInt("oldteam"));
184 if (oldteam == TFTeam_Unassigned)
185 {
186 oldteam = TFTeam_Spectator;
187 }
188
189 DataPack pack = new DataPack();
190 pack.WriteCell(userid);
191 pack.WriteCell(oldteam);
192 RequestFrame(Frame_Callback, pack);
193 }
194}
195
196public void Frame_Callback(DataPack pack)
197{
198 pack.Reset();
199 int client = GetClientOfUserId(pack.ReadCell())
200 if (client && IsClientInGame(client) && g_iBanTime[client])
201 {
202 ChangePlayerTeam(client, pack.ReadCell());
203 }
204
205 delete pack;
206}
207
208public void DB_OnConnection(Database db, const char[] error, any data)
209{
210 if (!db)
211 {
212 SetFailState("Failure establishing a connection to the database! Error: %s", error);
213 } else {
214 g_hDatabase = db;
215 CreateTable();
216 }
217}
218
219void CreateTable()
220{
221 char[] query = "CREATE TABLE IF NOT EXISTS `guardbans` ("
222 ..."`status` int(11) NOT NULL, "
223 ..."`steamid` varchar(32) NOT NULL UNIQUE, "
224 ..."`name` varchar(32) NOT NULL, "
225 ..."`ip` varchar(32) NOT NULL, "
226 ..."`time` int(11) NOT NULL, "
227 ..."`timeleft` int(11) NOT NULL, "
228 ..."`ban_admin_name` varchar(32) NOT NULL, "
229 ..."`ban_admin_steamid` varchar(32) NOT NULL, "
230 ..."`ban_reason` varchar(256) NOT NULL, "
231 ..."`unban_admin_name` varchar(32), "
232 ..."`unban_admin_steamid` varchar(32), "
233 ..."`unban_reason` varchar(256), "
234 ..."`ban_date` TIMESTAMP, "
235 ..."`unban_date` TIMESTAMP, "
236 ..."`expire_date` TIMESTAMP);"
237 g_hDatabase.Query(DB_OnCreateTable, query);
238}
239
240public void DB_OnCreateTable(Database db, DBResultSet results, const char[] error, any data)
241{
242 if (!results)
243 {
244 delete g_hDatabase;
245 SetFailState("Failure creating the database table! Error: %s", error);
246 } else {
247 CheckGuardBanAll();
248 }
249}
250
251public void DB_OnCheckClient(Database db, DBResultSet results, const char[] error, int userid)
252{
253 int client = GetClientOfUserId(userid);
254 if (client && IsClientInGame(client))
255 {
256 if (!results)
257 {
258 LogError("Failure checking client %N (UserID: %i) in the database! Error: %s", client, userid, error);
259 } else if (results.HasResults && results.FetchRow()) {
260 if (results.FetchInt(0) == 1)
261 {
262 g_iBanTime[client] = results.FetchInt(5);
263 g_hTimer[client] = CreateTimer(60.0, Timer_Callback, client, TIMER_REPEAT);
264 if (TF2_GetClientTeam(client) == TFTeam_Blue)
265 {
266 ChangePlayerTeam(client, TFTeam_Red);
267 }
268 }
269
270 UpdateName(client);
271 }
272 }
273}
274
275public void DB_OnExpireBan(Database db, DBResultSet results, const char[] error, int userid)
276{
277 int client = GetClientOfUserId(userid);
278 if (client && IsClientInGame(client) && !results)
279 {
280 LogError("Failure expiring the guard ban of client %N (UserID: %i) in the database! Error: %s", client, userid, error);
281 }
282}
283
284public void DB_OnUpdateTime(Database db, DBResultSet results, const char[] error, int userid)
285{
286 int client = GetClientOfUserId(userid);
287 if (client && IsClientInGame(client) && !results)
288 {
289 LogError("Failure updating the guard ban timeleft of client %N (UserID: %i) in the database! Error: %s", client, userid, error);
290 }
291}
292
293public void DB_OnInsertClient(Database db, DBResultSet results, const char[] error, int userid)
294{
295 int client = GetClientOfUserId(userid);
296 if (client && IsClientInGame(client) && !results)
297 {
298 LogError("Failure guard banning client %N (UserID: %i) in the database! Error: %s", client, userid, error);
299 }
300}
301
302public void DB_OnRemoveClient(Database db, DBResultSet results, const char[] error, int userid)
303{
304 int client = GetClientOfUserId(userid);
305 if (client && IsClientInGame(client) && !results)
306 {
307 LogError("Failure unbanning the guard ban of client %N (UserID: %i) in the database! Error: %s", client, userid, error);
308 }
309}
310
311public void DB_OnUpdateName(Database db, DBResultSet results, const char[] error, int userid)
312{
313 int client = GetClientOfUserId(userid);
314 if (client && IsClientInGame(client) && !results)
315 {
316 LogError("Failure updating the name of client %N (UserID: %i) in the database! Error: %s", client, userid, error);
317 }
318}
319
320public Action Timer_Callback(Handle timer, int client)
321{
322 g_iBanTime[client]--;
323 int userid = GetClientUserId(client);
324 char ip[32], query[256];
325 GetClientIP(client, ip, sizeof(ip));
326 if (!g_iBanTime[client])
327 {
328 g_hDatabase.Format(query, sizeof(query), "UPDATE `guardbans` SET `status` = '0', `timeleft` = '0', `expire_date` = CURRENT_TIMESTAMP WHERE `ip` = '%s';", ip);
329 g_hDatabase.Query(DB_OnExpireBan, query, userid);
330 PrintToChat(client, "[SM] Your guard ban has expired.");
331 g_hTimer[client] = null;
332 return Plugin_Stop;
333 } else {
334 g_hDatabase.Format(query, sizeof(query), "UPDATE `guardbans` SET `timeleft` = '%i' WHERE `ip` = '%s';", g_iBanTime[client], ip);
335 g_hDatabase.Query(DB_OnUpdateTime, query, userid);
336 }
337
338 return Plugin_Continue;
339}
340
341void CheckGuardBan(int client)
342{
343 if (!IsFakeClient(client))
344 {
345 int userid = GetClientUserId(client);
346 char ip[32], query[256];
347 GetClientIP(client, ip, sizeof(ip));
348 g_hDatabase.Format(query, sizeof(query), "SELECT * FROM `guardbans` WHERE `ip` = '%s';", ip);
349 g_hDatabase.Query(DB_OnCheckClient, query, userid);
350 }
351}
352
353void CheckGuardBanAll()
354{
355 for (int i = 1; i <= MaxClients; i++)
356 {
357 if (IsClientInGame(i))
358 {
359 CheckGuardBan(i);
360 }
361 }
362}
363
364void UpdateName(int client)
365{
366 int userid = GetClientUserId(client);
367 char steamid[32];
368 if (!GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid)))
369 {
370 LogError("Failure retrieving the SteamID of client %N (UserID: %i) Callback: DB_OnCheckClient()", client, userid);
371 } else {
372 char query[256];
373 g_hDatabase.Format(query, sizeof(query), "UPDATE `guardbans` SET `name` = '%N' WHERE `steamid` = '%s';", client, steamid);
374 g_hDatabase.Query(DB_OnUpdateName, query, userid);
375 }
376}
377
378void ChangePlayerTeam(int client, TFTeam team)
379{
380 TF2_ChangeClientTeam(client, team);
381 PrintToChat(client, "[SM] You are guard banned for %i minutes.", g_iBanTime[client]);
382}