· 8 years ago · Feb 05, 2018, 04:00 PM
1#include <amxmodx>
2#include <amxmisc>
3#include <engine>
4#include <regex>
5
6#define PLUGIN_NAME "Advanced Bans"
7#define PLUGIN_VERSION "0.8.1"
8#define PLUGIN_AUTHOR "Exolent"
9
10#pragma semicolon 1
11
12#if defined USING_SQL
13#include <sqlx>
14
15#define TABLE_NAME "advanced_bans1"
16#define KEY_NAME "name"
17#define KEY_STEAMID "steamid"
18#define KEY_BANLENGTH "banlength"
19#define KEY_UNBANTIME "unbantime"
20#define KEY_REASON "reason"
21#define KEY_ADMIN_NAME "admin_name"
22#define KEY_ADMIN_STEAMID "admin_steamid"
23
24#define RELOAD_BANS_INTERVAL 60.0
25#endif
26
27#define REGEX_IP_PATTERN "\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
28#define REGEX_STEAMID_PATTERN "^^STEAM_0:(0|1):\d+$"
29
30new Regex:g_IP_pattern;
31new Regex:g_SteamID_pattern;
32new g_regex_return;
33
34/*bool:IsValidIP(const ip[])
35{
36 return regex_match_c(ip, g_IP_pattern, g_regex_return) > 0;
37}*/
38
39#define IsValidIP(%1) (regex_match_c(%1, g_IP_pattern, g_regex_return) > 0)
40
41/*bool:IsValidAuthid(const authid[])
42{
43 return regex_match_c(authid, g_SteamID_pattern, g_regex_return) > 0;
44}*/
45
46#define IsValidAuthid(%1) (regex_match_c(%1, g_SteamID_pattern, g_regex_return) > 0)
47
48
49enum // for name displaying
50{
51 ACTIVITY_NONE, // nothing is shown
52 ACTIVITY_HIDE, // admin name is hidden
53 ACTIVITY_SHOW // admin name is shown
54};
55new const g_admin_activity[] =
56{
57 ACTIVITY_NONE, // amx_show_activity 0 = show nothing to everyone
58 ACTIVITY_HIDE, // amx_show_activity 1 = hide admin name from everyone
59 ACTIVITY_SHOW, // amx_show_activity 2 = show admin name to everyone
60 ACTIVITY_SHOW, // amx_show_activity 3 = show name to admins but hide it from normal users
61 ACTIVITY_SHOW, // amx_show_activity 4 = show name to admins but show nothing to normal users
62 ACTIVITY_HIDE // amx_show_activity 5 = hide name from admins but show nothing to normal users
63};
64new const g_normal_activity[] =
65{
66 ACTIVITY_NONE, // amx_show_activity 0 = show nothing to everyone
67 ACTIVITY_HIDE, // amx_show_activity 1 = hide admin name from everyone
68 ACTIVITY_SHOW, // amx_show_activity 2 = show admin name to everyone
69 ACTIVITY_HIDE, // amx_show_activity 3 = show name to admins but hide it from normal users
70 ACTIVITY_NONE, // amx_show_activity 4 = show name to admins but show nothing to normal users
71 ACTIVITY_NONE // amx_show_activity 5 = hide name from admins but show nothing to normal users
72};
73
74
75#if MAX_BANS <= 0
76enum _:BannedData
77{
78 bd_name[32],
79 bd_steamid[35],
80 bd_banlength,
81 bd_unbantime[32],
82 bd_reason[128],
83 bd_admin_name[64],
84 bd_admin_steamid[35]
85};
86
87new Trie:g_trie;
88new Array:g_array;
89#else
90new g_names[MAX_BANS][32];
91new g_steamids[MAX_BANS][35];
92new g_banlengths[MAX_BANS];
93new g_unbantimes[MAX_BANS][32];
94new g_reasons[MAX_BANS][128];
95new g_admin_names[MAX_BANS][64];
96new g_admin_steamids[MAX_BANS][35];
97#endif
98new g_total_bans;
99
100#if !defined USING_SQL
101new g_ban_file[64];
102#else
103new Handle:g_sql_tuple;
104new bool:g_loading_bans = true;
105#endif
106
107new ab_website;
108new ab_immunity;
109new ab_bandelay;
110new ab_unbancheck;
111
112new amx_show_activity;
113
114#if MAX_BANS <= 0
115new Array:g_maxban_times;
116new Array:g_maxban_flags;
117#else
118#define MAX_BANLIMITS 30
119new g_maxban_times[MAX_BANLIMITS];
120new g_maxban_flags[MAX_BANLIMITS];
121#endif
122new g_total_maxban_times;
123
124new g_unban_entity;
125
126new g_max_clients;
127
128new g_msgid_SayText;
129
130public plugin_init()
131{
132 register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
133 register_cvar("advanced_bans", PLUGIN_VERSION, FCVAR_SPONLY);
134
135 register_dictionary("advanced_bans.txt");
136
137 register_concmd("amx_ban", "CmdBan", ADMIN_BAN, "<nick, #userid, authid> <time in minutes> <reason>");
138 register_concmd("amx_banip", "CmdBanIp", ADMIN_BAN, "<nick, #userid, authid> <time in minutes> <reason>");
139 register_concmd("amx_addban", "CmdAddBan", ADMIN_BAN, "<name> <authid or ip> <time in minutes> <reason>");
140 register_concmd("amx_unban", "CmdUnban", ADMIN_BAN, "<authid or ip>");
141 register_concmd("amx_banlist", "CmdBanList", ADMIN_BAN, "[start] -- shows everyone who is banned");
142 register_srvcmd("amx_addbanlimit", "CmdAddBanLimit", -1, "<flag> <time in minutes>");
143
144 ab_website = register_cvar("ab_website", "");
145 ab_immunity = register_cvar("ab_immunity", "1");
146 ab_bandelay = register_cvar("ab_bandelay", "1.0");
147 ab_unbancheck = register_cvar("ab_unbancheck", "5.0");
148
149 amx_show_activity = register_cvar("amx_show_activity", "2");
150
151 #if MAX_BANS <= 0
152 g_trie = TrieCreate();
153 g_array = ArrayCreate(BannedData);
154 #endif
155
156 #if !defined MAX_BANLIMITS
157 g_maxban_times = ArrayCreate(1);
158 g_maxban_flags = ArrayCreate(1);
159 #endif
160
161 #if !defined USING_SQL
162 get_datadir(g_ban_file, sizeof(g_ban_file) - 1);
163 add(g_ban_file, sizeof(g_ban_file) - 1, "/advanced_bans.txt");
164
165 LoadBans();
166 #else
167 g_sql_tuple = SQL_MakeStdTuple();
168 PrepareTable();
169 #endif
170
171 new error[2];
172 g_IP_pattern = regex_compile(REGEX_IP_PATTERN, g_regex_return, error, sizeof(error) - 1);
173 g_SteamID_pattern = regex_compile(REGEX_STEAMID_PATTERN, g_regex_return, error, sizeof(error) - 1);
174
175 g_max_clients = get_maxplayers();
176
177 g_msgid_SayText = get_user_msgid("SayText");
178}
179
180#if defined USING_SQL
181PrepareTable()
182{
183 new query[128];
184 formatex(query, sizeof(query) - 1,\
185 "CREATE TABLE IF NOT EXISTS `%s` (`%s` varchar(32) NOT NULL, `%s` varchar(35) NOT NULL, `%s` int(10) NOT NULL, `%s` varchar(32) NOT NULL, `%s` varchar(128) NOT NULL, `%s` varchar(64) NOT NULL, `%s` varchar(35) NOT NULL);",\
186 TABLE_NAME, KEY_NAME, KEY_STEAMID, KEY_BANLENGTH, KEY_UNBANTIME, KEY_REASON, KEY_ADMIN_NAME, KEY_ADMIN_STEAMID
187 );
188
189 SQL_ThreadQuery(g_sql_tuple, "QueryCreateTable", query);
190}
191
192public QueryCreateTable(failstate, Handle:query, error[], errcode, data[], datasize, Float:queuetime)
193{
194 if( failstate == TQUERY_CONNECT_FAILED )
195 {
196 set_fail_state("Could not connect to database.");
197 }
198 else if( failstate == TQUERY_QUERY_FAILED )
199 {
200 set_fail_state("Query failed.");
201 }
202 else if( errcode )
203 {
204 log_amx("Error on query: %s", error);
205 }
206 else
207 {
208 LoadBans();
209 }
210}
211#endif
212
213public plugin_cfg()
214{
215 CreateUnbanEntity();
216}
217
218public CreateUnbanEntity()
219{
220 static failtimes;
221
222 g_unban_entity = create_entity("info_target");
223
224 if( !is_valid_ent(g_unban_entity) )
225 {
226 ++failtimes;
227
228 log_amx("[ERROR] Failed to create unban entity (%i/10)", failtimes);
229
230 if( failtimes < 10 )
231 {
232 set_task(1.0, "CreateUnbanEntity");
233 }
234 else
235 {
236 log_amx("[ERROR] Could not create unban entity!");
237 }
238
239 return;
240 }
241
242 entity_set_string(g_unban_entity, EV_SZ_classname, "unban_entity");
243 entity_set_float(g_unban_entity, EV_FL_nextthink, get_gametime() + 1.0);
244
245 register_think("unban_entity", "FwdThink");
246}
247
248public client_authorized(client)
249{
250 static authid[35];
251 get_user_authid(client, authid, sizeof(authid) - 1);
252
253 static ip[35];
254 get_user_ip(client, ip, sizeof(ip) - 1, 1);
255
256 #if MAX_BANS > 0
257 static banned_authid[35], bool:is_ip;
258 for( new i = 0; i < g_total_bans; i++ )
259 {
260 copy(banned_authid, sizeof(banned_authid) - 1, g_steamids[i]);
261
262 is_ip = bool:(containi(banned_authid, ".") != -1);
263
264 if( is_ip && equal(ip, banned_authid) || !is_ip && equal(authid, banned_authid) )
265 {
266 static name[32], reason[128], unbantime[32], admin_name[32], admin_steamid[64];
267 copy(name, sizeof(name) - 1, g_names[i]);
268 copy(reason, sizeof(reason) - 1, g_reasons[i]);
269 new banlength = g_banlengths[i];
270 copy(unbantime, sizeof(unbantime) - 1, g_unbantimes[i]);
271 copy(admin_name, sizeof(admin_name) - 1, g_admin_names[i]);
272 copy(admin_steamid, sizeof(admin_steamid) - 1, g_admin_steamids[i]);
273
274 PrintBanInformation(client, name, banned_authid, reason, banlength, unbantime, admin_name, admin_steamid, true, true);
275
276 set_task(get_pcvar_float(ab_bandelay), "TaskDisconnectPlayer", client);
277 break;
278 }
279 }
280 #else
281 static array_pos;
282
283 if( TrieGetCell(g_trie, authid, array_pos) || TrieGetCell(g_trie, ip, array_pos) )
284 {
285 static data[BannedData];
286 ArrayGetArray(g_array, array_pos, data);
287
288 PrintBanInformation(client, data[bd_name], data[bd_steamid], data[bd_reason], data[bd_banlength], data[bd_unbantime], data[bd_admin_name], data[bd_admin_steamid], true, true);
289
290 set_task(get_pcvar_float(ab_bandelay), "TaskDisconnectPlayer", client);
291 }
292 #endif
293}
294
295public CmdBan(client, level, cid)
296{
297 if( !cmd_access(client, level, cid, 4) ) return PLUGIN_HANDLED;
298
299 static arg[128];
300 read_argv(1, arg, sizeof(arg) - 1);
301
302 new target = cmd_target(client, arg, GetTargetFlags(client));
303 if( !target ) return PLUGIN_HANDLED;
304
305 static target_authid[35];
306 get_user_authid(target, target_authid, sizeof(target_authid) - 1);
307
308 if( !IsValidAuthid(target_authid) )
309 {
310 console_print(client, "[AdvancedBans] %L", client, "AB_NOT_AUTHORIZED");
311 return PLUGIN_HANDLED;
312 }
313
314 #if MAX_BANS <= 0
315 if( TrieKeyExists(g_trie, target_authid) )
316 {
317 console_print(client, "[AdvancedBans] %L", client, "AB_ALREADY_BANNED_STEAMID");
318 return PLUGIN_HANDLED;
319 }
320 #else
321 for( new i = 0; i < g_total_bans; i++ )
322 {
323 if( !strcmp(target_authid, g_steamids[i], 1) )
324 {
325 console_print(client, "[AdvancedBans] %L", client, "AB_ALREADY_BANNED_STEAMID");
326 return PLUGIN_HANDLED;
327 }
328 }
329 #endif
330
331 read_argv(2, arg, sizeof(arg) - 1);
332
333 new length = str_to_num(arg);
334 new maxlength = GetMaxBanTime(client);
335
336 if( maxlength && (!length || length > maxlength) )
337 {
338 console_print(client, "[AdvancedBans] %L", client, "AB_MAX_BAN_TIME", maxlength);
339 return PLUGIN_HANDLED;
340 }
341
342 static unban_time[64];
343 if( length == 0 )
344 {
345 formatex(unban_time, sizeof(unban_time) - 1, "%L", client, "AB_PERMANENT_BAN");
346 }
347 else
348 {
349 GenerateUnbanTime(length, unban_time, sizeof(unban_time) - 1);
350 }
351
352 read_argv(3, arg, sizeof(arg) - 1);
353
354 static admin_name[64], target_name[32];
355 get_user_name(client, admin_name, sizeof(admin_name) - 1);
356 get_user_name(target, target_name, sizeof(target_name) - 1);
357
358 static admin_authid[35];
359 get_user_authid(client, admin_authid, sizeof(admin_authid) - 1);
360
361 AddBan(target_name, target_authid, arg, length, unban_time, admin_name, admin_authid);
362
363 PrintBanInformation(target, target_name, target_authid, arg, length, unban_time, admin_name, admin_authid, true, true);
364 PrintBanInformation(client, target_name, target_authid, arg, length, unban_time, admin_name, admin_authid, false, false);
365
366 set_task(get_pcvar_float(ab_bandelay), "TaskDisconnectPlayer", target);
367
368 GetBanTime(length, unban_time, sizeof(unban_time) - 1);
369
370 PrintActivity(admin_name, "^x04[AdvancedBans] $name^x01 :^x03 banned %s. Reason: %s. Ban Length: %s", target_name, arg, unban_time);
371
372 Log("%s <%s> banned %s <%s> || Reason: ^"%s^" || Ban Length: %s", admin_name, admin_authid, target_name, target_authid, arg, unban_time);
373
374 return PLUGIN_HANDLED;
375}
376
377public CmdBanIp(client, level, cid)
378{
379 if( !cmd_access(client, level, cid, 4) ) return PLUGIN_HANDLED;
380
381 static arg[128];
382 read_argv(1, arg, sizeof(arg) - 1);
383
384 new target = cmd_target(client, arg, GetTargetFlags(client));
385 if( !target ) return PLUGIN_HANDLED;
386
387 static target_ip[35];
388 get_user_ip(target, target_ip, sizeof(target_ip) - 1, 1);
389
390 #if MAX_BANS <= 0
391 if( TrieKeyExists(g_trie, target_ip) )
392 {
393 console_print(client, "[AdvancedBans] %L", client, "AB_ALREADY_BANNED_IP");
394 return PLUGIN_HANDLED;
395 }
396 #else
397 for( new i = 0; i < g_total_bans; i++ )
398 {
399 if( !strcmp(target_ip, g_steamids[i], 1) )
400 {
401 console_print(client, "[AdvancedBans] %L", client, "AB_ALREADY_BANNED_IP");
402 return PLUGIN_HANDLED;
403 }
404 }
405 #endif
406
407 read_argv(2, arg, sizeof(arg) - 1);
408
409 new length = str_to_num(arg);
410 new maxlength = GetMaxBanTime(client);
411
412 if( maxlength && (!length || length > maxlength) )
413 {
414 console_print(client, "[AdvancedBans] %L", client, "AB_MAX_BAN_TIME", maxlength);
415 return PLUGIN_HANDLED;
416 }
417
418 static unban_time[32];
419
420 if( length == 0 )
421 {
422 formatex(unban_time, sizeof(unban_time) - 1, "%L", client, "AB_PERMANENT_BAN");
423 }
424 else
425 {
426 GenerateUnbanTime(length, unban_time, sizeof(unban_time) - 1);
427 }
428
429 read_argv(3, arg, sizeof(arg) - 1);
430
431 static admin_name[64], target_name[32];
432 get_user_name(client, admin_name, sizeof(admin_name) - 1);
433 get_user_name(target, target_name, sizeof(target_name) - 1);
434
435 static admin_authid[35];
436 get_user_authid(client, admin_authid, sizeof(admin_authid) - 1);
437
438 AddBan(target_name, target_ip, arg, length, unban_time, admin_name, admin_authid);
439
440 PrintBanInformation(target, target_name, target_ip, arg, length, unban_time, admin_name, admin_authid, true, true);
441 PrintBanInformation(client, target_name, target_ip, arg, length, unban_time, admin_name, admin_authid, false, false);
442
443 set_task(get_pcvar_float(ab_bandelay), "TaskDisconnectPlayer", target);
444
445 GetBanTime(length, unban_time, sizeof(unban_time) - 1);
446
447 PrintActivity(admin_name, "^x04[AdvancedBans] $name^x01 :^x03 banned %s. Reason: %s. Ban Length: %s", target_name, arg, unban_time);
448
449 Log("%s <%s> banned %s <%s> || Reason: ^"%s^" || Ban Length: %s", admin_name, admin_authid, target_name, target_ip, arg, unban_time);
450
451 return PLUGIN_HANDLED;
452}
453
454public CmdAddBan(client, level, cid)
455{
456 if( !cmd_access(client, level, cid, 5) ) return PLUGIN_HANDLED;
457
458 static target_name[32], target_authid[35], bantime[10], reason[128];
459 read_argv(1, target_name, sizeof(target_name) - 1);
460 read_argv(2, target_authid, sizeof(target_authid) - 1);
461 read_argv(3, bantime, sizeof(bantime) - 1);
462 read_argv(4, reason, sizeof(reason) - 1);
463
464 new bool:is_ip = bool:(containi(target_authid, ".") != -1);
465
466 if( !is_ip && !IsValidAuthid(target_authid) )
467 {
468 console_print(client, "[AdvancedBans] %L", client, "AB_INVALID_STEAMID");
469 console_print(client, "[AdvancedBans] %L", client, "AB_VALID_STEAMID_FORMAT");
470
471 return PLUGIN_HANDLED;
472 }
473 else if( is_ip )
474 {
475 new pos = contain(target_authid, ":");
476 if( pos > 0 )
477 {
478 target_authid[pos] = 0;
479 }
480
481 if( !IsValidIP(target_authid) )
482 {
483 console_print(client, "[AdvancedBans] %L", client, "AB_INVALID_IP");
484
485 return PLUGIN_HANDLED;
486 }
487 }
488
489 #if MAX_BANS <= 0
490 if( TrieKeyExists(g_trie, target_authid) )
491 {
492 console_print(client, "[AdvancedBans] %L", client, is_ip ? "AB_ALREADY_BANNED_IP" : "AB_ALREADY_BANNED_STEAMID");
493 return PLUGIN_HANDLED;
494 }
495 #else
496 for( new i = 0; i < g_total_bans; i++ )
497 {
498 if( !strcmp(target_authid, g_steamids[i], 1) )
499 {
500 console_print(client, "[AdvancedBans] %L", client, is_ip ? "AB_ALREADY_BANNED_IP" : "AB_ALREADY_BANNED_STEAMID");
501 return PLUGIN_HANDLED;
502 }
503 }
504 #endif
505
506 new length = str_to_num(bantime);
507 new maxlength = GetMaxBanTime(client);
508
509 if( maxlength && (!length || length > maxlength) )
510 {
511 console_print(client, "[AdvancedBans] %L", client, "AB_MAX_BAN_TIME", maxlength);
512 return PLUGIN_HANDLED;
513 }
514
515 if( is_user_connected(find_player(is_ip ? "d" : "c", target_authid)) )
516 {
517 client_cmd(client, "amx_ban ^"%s^" %i ^"%s^"", target_authid, length, reason);
518 return PLUGIN_HANDLED;
519 }
520
521 static unban_time[32];
522 if( length == 0 )
523 {
524 formatex(unban_time, sizeof(unban_time) - 1, "%L", client, "AB_PERMANENT_BAN");
525 }
526 else
527 {
528 GenerateUnbanTime(length, unban_time, sizeof(unban_time) - 1);
529 }
530
531 static admin_name[64], admin_authid[35];
532 get_user_name(client, admin_name, sizeof(admin_name) - 1);
533 get_user_authid(client, admin_authid, sizeof(admin_authid) - 1);
534
535 AddBan(target_name, target_authid, reason, length, unban_time, admin_name, admin_authid);
536
537 PrintBanInformation(client, target_name, target_authid, reason, length, unban_time, "", "", false, false);
538
539 GetBanTime(length, unban_time, sizeof(unban_time) - 1);
540
541 PrintActivity(admin_name, "^x04[AdvancedBans] $name^x01 :^x03 banned %s %s. Reason: %s. Ban Length: %s", is_ip ? "IP" : "SteamID", target_authid, reason, unban_time);
542
543 Log("%s <%s> banned %s <%s> || Reason: ^"%s^" || Ban Length: %s", admin_name, admin_authid, target_name, target_authid, reason, unban_time);
544
545 return PLUGIN_HANDLED;
546}
547
548public CmdUnban(client, level, cid)
549{
550 if( !cmd_access(client, level, cid, 2) ) return PLUGIN_HANDLED;
551
552 static arg[35];
553 read_argv(1, arg, sizeof(arg) - 1);
554
555 #if MAX_BANS > 0
556 static banned_authid[35];
557 for( new i = 0; i < g_total_bans; i++ )
558 {
559 copy(banned_authid, sizeof(banned_authid) - 1, g_steamids[i]);
560
561 if( equal(arg, banned_authid) )
562 {
563 static admin_name[64];
564 get_user_name(client, admin_name, sizeof(admin_name) - 1);
565
566 static name[32], reason[128];
567 copy(name, sizeof(name) - 1, g_names[i]);
568 copy(reason, sizeof(reason) - 1, g_reasons[i]);
569
570 PrintActivity(admin_name, "^x04[AdvancedBans] $name^x01 :^x03 unbanned %s^x01 [%s] [Ban Reason: %s]", name, arg, reason);
571
572 static authid[35];
573 get_user_authid(client, authid, sizeof(authid) - 1);
574
575 Log("%s <%s> unbanned %s <%s> || Ban Reason: ^"%s^"", admin_name, authid, name, arg, reason);
576
577 RemoveBan(i);
578
579 return PLUGIN_HANDLED;
580 }
581 }
582 #else
583 if( TrieKeyExists(g_trie, arg) )
584 {
585 static array_pos;
586 TrieGetCell(g_trie, arg, array_pos);
587
588 static data[BannedData];
589 ArrayGetArray(g_array, array_pos, data);
590
591 static unban_name[32];
592 get_user_name(client, unban_name, sizeof(unban_name) - 1);
593
594 PrintActivity(unban_name, "^x04[AdvancedBans] $name^x01 :^x03 unbanned %s^x01 [%s] [Ban Reason: %s]", data[bd_name], data[bd_steamid], data[bd_reason]);
595
596 static admin_name[64];
597 get_user_name(client, admin_name, sizeof(admin_name) - 1);
598
599 static authid[35];
600 get_user_authid(client, authid, sizeof(authid) - 1);
601
602 Log("%s <%s> unbanned %s <%s> || Ban Reason: ^"%s^"", admin_name, authid, data[bd_name], data[bd_steamid], data[bd_reason]);
603
604 RemoveBan(array_pos, data[bd_steamid]);
605
606 return PLUGIN_HANDLED;
607 }
608 #endif
609
610 console_print(client, "[AdvancedBans] %L", client, "AB_NOT_IN_BAN_LIST", arg);
611
612 return PLUGIN_HANDLED;
613}
614
615public CmdBanList(client, level, cid)
616{
617 if( !cmd_access(client, level, cid, 1) ) return PLUGIN_HANDLED;
618
619 if( !g_total_bans )
620 {
621 console_print(client, "[AdvancedBans] %L", client, "AB_NO_BANS");
622 return PLUGIN_HANDLED;
623 }
624
625 static start;
626
627 if( read_argc() > 1 )
628 {
629 static arg[5];
630 read_argv(1, arg, sizeof(arg) - 1);
631
632 start = min(str_to_num(arg), g_total_bans) - 1;
633 }
634 else
635 {
636 start = 0;
637 }
638
639 new last = min(start + 10, g_total_bans);
640
641 if( client == 0 )
642 {
643 server_cmd("echo ^"%L^"", client, "AB_BAN_LIST_NUM", start + 1, last);
644 }
645 else
646 {
647 client_cmd(client, "echo ^"%L^"", client, "AB_BAN_LIST_NUM", start + 1, last);
648 }
649
650 for( new i = start; i < last; i++ )
651 {
652 #if MAX_BANS <= 0
653 static data[BannedData];
654 ArrayGetArray(g_array, i, data);
655
656 PrintBanInformation(client, data[bd_name], data[bd_steamid], data[bd_reason], data[bd_banlength], data[bd_unbantime], data[bd_admin_name], data[bd_admin_steamid], true, false);
657 #else
658 static name[32], steamid[35], reason[128], banlength, unbantime[32], admin_name[32], admin_steamid[35];
659
660 copy(name, sizeof(name) - 1, g_names[i]);
661 copy(steamid, sizeof(steamid) - 1, g_steamids[i]);
662 copy(reason, sizeof(reason) - 1, g_reasons[i]);
663 banlength = g_banlengths[i];
664 copy(unbantime, sizeof(unbantime) - 1, g_unbantimes[i]);
665 copy(admin_name, sizeof(admin_name) - 1, g_admin_names[i]);
666 copy(admin_steamid, sizeof(admin_steamid) - 1, g_admin_steamids[i]);
667
668 PrintBanInformation(client, name, steamid, reason, banlength, unbantime, admin_name, admin_steamid, true, false);
669 #endif
670 }
671
672 if( ++last < g_total_bans )
673 {
674 if( client == 0 )
675 {
676 server_cmd("echo ^"%L^"", client, "AB_BAN_LIST_NEXT", last);
677 }
678 else
679 {
680 client_cmd(client, "echo ^"%L^"", client, "AB_BAN_LIST_NEXT", last);
681 }
682 }
683
684 return PLUGIN_HANDLED;
685}
686
687public CmdAddBanLimit()
688{
689 if( read_argc() != 3 )
690 {
691 log_amx("amx_addbanlimit was used with incorrect parameters!");
692 log_amx("Usage: amx_addbanlimit <flags> <time in minutes>");
693 return PLUGIN_HANDLED;
694 }
695
696 static arg[16];
697
698 read_argv(1, arg, sizeof(arg) - 1);
699 new flags = read_flags(arg);
700
701 read_argv(2, arg, sizeof(arg) - 1);
702 new minutes = str_to_num(arg);
703
704 #if !defined MAX_BANLIMITS
705 ArrayPushCell(g_maxban_flags, flags);
706 ArrayPushCell(g_maxban_times, minutes);
707 #else
708 if( g_total_maxban_times >= MAX_BANLIMITS )
709 {
710 static notified;
711 if( !notified )
712 {
713 log_amx("The amx_addbanlimit has reached its maximum!");
714 notified = 1;
715 }
716 return PLUGIN_HANDLED;
717 }
718
719 g_maxban_flags[g_total_maxban_times] = flags;
720 g_maxban_times[g_total_maxban_times] = minutes;
721 #endif
722 g_total_maxban_times++;
723
724 return PLUGIN_HANDLED;
725}
726
727public FwdThink(entity)
728{
729 if( entity != g_unban_entity ) return;
730
731 #if defined USING_SQL
732 if( g_total_bans > 0 && !g_loading_bans )
733 #else
734 if( g_total_bans > 0 )
735 #endif
736 {
737 static _hours[5], _minutes[5], _seconds[5], _month[5], _day[5], _year[7];
738 format_time(_hours, sizeof(_hours) - 1, "%H");
739 format_time(_minutes, sizeof(_minutes) - 1, "%M");
740 format_time(_seconds, sizeof(_seconds) - 1, "%S");
741 format_time(_month, sizeof(_month) - 1, "%m");
742 format_time(_day, sizeof(_day) - 1, "%d");
743 format_time(_year, sizeof(_year) - 1, "%Y");
744
745 // c = current
746 // u = unban
747
748 new c_hours = str_to_num(_hours);
749 new c_minutes = str_to_num(_minutes);
750 new c_seconds = str_to_num(_seconds);
751 new c_month = str_to_num(_month);
752 new c_day = str_to_num(_day);
753 new c_year = str_to_num(_year);
754
755 static unban_time[32];
756 static u_hours, u_minutes, u_seconds, u_month, u_day, u_year;
757
758 for( new i = 0; i < g_total_bans; i++ )
759 {
760 #if MAX_BANS <= 0
761 static data[BannedData];
762 ArrayGetArray(g_array, i, data);
763
764 if( data[bd_banlength] == 0 ) continue;
765 #else
766 if( g_banlengths[i] == 0 ) continue;
767 #endif
768
769 #if MAX_BANS <= 0
770 copy(unban_time, sizeof(unban_time) - 1, data[bd_unbantime]);
771 #else
772 copy(unban_time, sizeof(unban_time) - 1, g_unbantimes[i]);
773 #endif
774 replace_all(unban_time, sizeof(unban_time) - 1, ":", " ");
775 replace_all(unban_time, sizeof(unban_time) - 1, "/", " ");
776
777 parse(unban_time,\
778 _hours, sizeof(_hours) - 1,\
779 _minutes, sizeof(_minutes) - 1,\
780 _seconds, sizeof(_seconds) - 1,\
781 _month, sizeof(_month) - 1,\
782 _day, sizeof(_day) - 1,\
783 _year, sizeof(_year) - 1
784 );
785
786 u_hours = str_to_num(_hours);
787 u_minutes = str_to_num(_minutes);
788 u_seconds = str_to_num(_seconds);
789 u_month = str_to_num(_month);
790 u_day = str_to_num(_day);
791 u_year = str_to_num(_year);
792
793 if( u_year < c_year
794 || u_year == c_year && u_month < c_month
795 || u_year == c_year && u_month == c_month && u_day < c_day
796 || u_year == c_year && u_month == c_month && u_day == c_day && u_hours < c_hours
797 || u_year == c_year && u_month == c_month && u_day == c_day && u_hours == c_hours && u_minutes < c_minutes
798 || u_year == c_year && u_month == c_month && u_day == c_day && u_hours == c_hours && u_minutes == c_minutes && u_seconds <= c_seconds )
799 {
800 #if MAX_BANS <= 0
801 Log("Ban time is up for: %s [%s]", data[bd_name], data[bd_steamid]);
802
803 Print("^x04[AdvancedBans]^x03 %s^x01[^x04%s^x01]^x03 ban time is up!^x01 [Ban Reason: %s]", data[bd_name], data[bd_steamid], data[bd_reason]);
804
805 RemoveBan(i, data[bd_steamid]);
806 #else
807 Log("Ban time is up for: %s [%s]", g_names[i], g_steamids[i]);
808
809 Print("^x04[AdvancedBans]^x03 %s^x01[^x04%s^x01]^x03 ban time is up!^x01 [Ban Reason: %s]", g_names[i], g_steamids[i], g_reasons[i]);
810
811 RemoveBan(i);
812 #endif
813
814 i--; // current pos was replaced with another ban, so we need to check it again.
815 }
816 }
817 }
818
819 entity_set_float(g_unban_entity, EV_FL_nextthink, get_gametime() + get_pcvar_float(ab_unbancheck));
820}
821
822public TaskDisconnectPlayer(client)
823{
824 server_cmd("kick #%i ^"You are banned from this server. Check your console^"", get_user_userid(client));
825}
826
827AddBan(const target_name[], const target_steamid[], const reason[], const length, const unban_time[], const admin_name[], const admin_steamid[])
828{
829 #if MAX_BANS > 0
830 if( g_total_bans == MAX_BANS )
831 {
832 log_amx("Ban list is full! (%i)", g_total_bans);
833 return;
834 }
835 #endif
836
837 #if defined USING_SQL
838 static target_name2[32], reason2[128], admin_name2[32];
839 MakeStringSQLSafe(target_name, target_name2, sizeof(target_name2) - 1);
840 MakeStringSQLSafe(reason, reason2, sizeof(reason2) - 1);
841 MakeStringSQLSafe(admin_name, admin_name2, sizeof(admin_name2) - 1);
842
843 static query[512];
844 formatex(query, sizeof(query) - 1,\
845 "INSERT INTO `%s` (`%s`, `%s`, `%s`, `%s`, `%s`, `%s`, `%s`) VALUES ('%s', '%s', '%i', '%s', '%s', '%s', '%s');",\
846 TABLE_NAME, KEY_NAME, KEY_STEAMID, KEY_BANLENGTH, KEY_UNBANTIME, KEY_REASON, KEY_ADMIN_NAME, KEY_ADMIN_STEAMID,\
847 target_name2, target_steamid, length, unban_time, reason2, admin_name2, admin_steamid
848 );
849
850 SQL_ThreadQuery(g_sql_tuple, "QueryAddBan", query);
851 #else
852 new f = fopen(g_ban_file, "a+");
853
854 fprintf(f, "^"%s^" ^"%s^" %i ^"%s^" ^"%s^" ^"%s^" ^"%s^"^n",\
855 target_steamid,\
856 target_name,\
857 length,\
858 unban_time,\
859 reason,\
860 admin_name,\
861 admin_steamid
862 );
863
864 fclose(f);
865 #endif
866
867 #if MAX_BANS <= 0
868 static data[BannedData];
869 copy(data[bd_name], sizeof(data[bd_name]) - 1, target_name);
870 copy(data[bd_steamid], sizeof(data[bd_steamid]) - 1, target_steamid);
871 data[bd_banlength] = length;
872 copy(data[bd_unbantime], sizeof(data[bd_unbantime]) - 1, unban_time);
873 copy(data[bd_reason], sizeof(data[bd_reason]) - 1, reason);
874 copy(data[bd_admin_name], sizeof(data[bd_admin_name]) - 1, admin_name);
875 copy(data[bd_admin_steamid], sizeof(data[bd_admin_steamid]) - 1, admin_steamid);
876
877 TrieSetCell(g_trie, target_steamid, g_total_bans);
878 ArrayPushArray(g_array, data);
879 #else
880 copy(g_names[g_total_bans], sizeof(g_names[]) - 1, target_name);
881 copy(g_steamids[g_total_bans], sizeof(g_steamids[]) - 1, target_steamid);
882 g_banlengths[g_total_bans] = length;
883 copy(g_unbantimes[g_total_bans], sizeof(g_unbantimes[]) - 1, unban_time);
884 copy(g_reasons[g_total_bans], sizeof(g_reasons[]) - 1, reason);
885 copy(g_admin_names[g_total_bans], sizeof(g_admin_names[]) - 1, admin_name);
886 copy(g_admin_steamids[g_total_bans], sizeof(g_admin_steamids[]) - 1, admin_steamid);
887 #endif
888
889 g_total_bans++;
890
891 #if MAX_BANS > 0
892 if( g_total_bans == MAX_BANS )
893 {
894 log_amx("Ban list is full! (%i)", g_total_bans);
895 }
896 #endif
897}
898
899#if defined USING_SQL
900public QueryAddBan(failstate, Handle:query, error[], errcode, data[], datasize, Float:queuetime)
901{
902 if( failstate == TQUERY_CONNECT_FAILED )
903 {
904 set_fail_state("Could not connect to database.");
905 }
906 else if( failstate == TQUERY_QUERY_FAILED )
907 {
908 set_fail_state("Query failed.");
909 }
910 else if( errcode )
911 {
912 log_amx("Error on query: %s", error);
913 }
914 else
915 {
916 // Yay, ban was added! We can all rejoice!
917 }
918}
919
920public QueryDeleteBan(failstate, Handle:query, error[], errcode, data[], datasize, Float:queuetime)
921{
922 if( failstate == TQUERY_CONNECT_FAILED )
923 {
924 set_fail_state("Could not connect to database.");
925 }
926 else if( failstate == TQUERY_QUERY_FAILED )
927 {
928 set_fail_state("Query failed.");
929 }
930 else if( errcode )
931 {
932 log_amx("Error on query: %s", error);
933 }
934 else
935 {
936 // Yay, ban was deleted! We can all rejoice!
937 }
938}
939
940public QueryLoadBans(failstate, Handle:query, error[], errcode, data[], datasize, Float:queuetime)
941{
942 if( failstate == TQUERY_CONNECT_FAILED )
943 {
944 set_fail_state("Could not connect to database.");
945 }
946 else if( failstate == TQUERY_QUERY_FAILED )
947 {
948 set_fail_state("Query failed.");
949 }
950 else if( errcode )
951 {
952 log_amx("Error on query: %s", error);
953 }
954 else
955 {
956 if( SQL_NumResults(query) )
957 {
958 #if MAX_BANS <= 0
959 static data[BannedData];
960 while( SQL_MoreResults(query) )
961 #else
962 while( SQL_MoreResults(query) && g_total_bans < MAX_BANS )
963 #endif
964 {
965 #if MAX_BANS <= 0
966 SQL_ReadResult(query, 0, data[bd_name], sizeof(data[bd_name]) - 1);
967 SQL_ReadResult(query, 1, data[bd_steamid], sizeof(data[bd_steamid]) - 1);
968 data[bd_banlength] = SQL_ReadResult(query, 2);
969 SQL_ReadResult(query, 3, data[bd_unbantime], sizeof(data[bd_unbantime]) - 1);
970 SQL_ReadResult(query, 4, data[bd_reason], sizeof(data[bd_reason]) - 1);
971 SQL_ReadResult(query, 5, data[bd_admin_name], sizeof(data[bd_admin_name]) - 1);
972 SQL_ReadResult(query, 6, data[bd_admin_steamid], sizeof(data[bd_admin_steamid]) - 1);
973
974 ArrayPushArray(g_array, data);
975 TrieSetCell(g_trie, data[bd_steamid], g_total_bans);
976 #else
977 SQL_ReadResult(query, 0, g_names[g_total_bans], sizeof(g_names[]) - 1);
978 SQL_ReadResult(query, 1, g_steamids[g_total_bans], sizeof(g_steamids[]) - 1);
979 g_banlengths[g_total_bans] = SQL_ReadResult(query, 2);
980 SQL_ReadResult(query, 3, g_unbantimes[g_total_bans], sizeof(g_unbantimes[]) - 1);
981 SQL_ReadResult(query, 4, g_reasons[g_total_bans], sizeof(g_reasons[]) - 1);
982 SQL_ReadResult(query, 5, g_admin_names[g_total_bans], sizeof(g_admin_names[]) - 1);
983 SQL_ReadResult(query, 6, g_admin_steamids[g_total_bans], sizeof(g_admin_steamids[]) - 1);
984 #endif
985
986 g_total_bans++;
987
988 SQL_NextRow(query);
989 }
990 }
991
992 set_task(RELOAD_BANS_INTERVAL, "LoadBans");
993
994 g_loading_bans = false;
995 }
996}
997#endif
998
999#if MAX_BANS > 0
1000RemoveBan(remove)
1001{
1002 #if defined USING_SQL
1003 static query[128];
1004 formatex(query, sizeof(query) - 1,\
1005 "DELETE FROM `%s` WHERE `%s` = '%s';",\
1006 TABLE_NAME, KEY_STEAMID, g_steamids[remove]
1007 );
1008
1009 SQL_ThreadQuery(g_sql_tuple, "QueryDeleteBan", query);
1010 #endif
1011
1012 for( new i = remove; i < g_total_bans; i++ )
1013 {
1014 if( (i + 1) == g_total_bans )
1015 {
1016 copy(g_names[i], sizeof(g_names[]) - 1, "");
1017 copy(g_steamids[i], sizeof(g_steamids[]) - 1, "");
1018 g_banlengths[i] = 0;
1019 copy(g_unbantimes[i], sizeof(g_unbantimes[]) - 1, "");
1020 copy(g_reasons[i], sizeof(g_reasons[]) - 1, "");
1021 copy(g_admin_names[i], sizeof(g_admin_names[]) - 1, "");
1022 copy(g_admin_steamids[i], sizeof(g_admin_steamids[]) - 1, "");
1023 }
1024 else
1025 {
1026 copy(g_names[i], sizeof(g_names[]) - 1, g_names[i + 1]);
1027 copy(g_steamids[i], sizeof(g_steamids[]) - 1, g_steamids[i + 1]);
1028 g_banlengths[i] = g_banlengths[i + 1];
1029 copy(g_unbantimes[i], sizeof(g_unbantimes[]) - 1, g_unbantimes[i + 1]);
1030 copy(g_reasons[i], sizeof(g_reasons[]) - 1, g_reasons[i + 1]);
1031 copy(g_admin_names[i], sizeof(g_admin_names[]) - 1, g_admin_names[i + 1]);
1032 copy(g_admin_steamids[i], sizeof(g_admin_steamids[]) - 1, g_admin_steamids[i + 1]);
1033 }
1034 }
1035
1036 g_total_bans--;
1037
1038 #if !defined USING_SQL
1039 new f = fopen(g_ban_file, "wt");
1040
1041 static name[32], steamid[35], banlength, unbantime[32], reason[128], admin_name[32], admin_steamid[35];
1042 for( new i = 0; i < g_total_bans; i++ )
1043 {
1044 copy(name, sizeof(name) - 1, g_names[i]);
1045 copy(steamid, sizeof(steamid) - 1, g_steamids[i]);
1046 banlength = g_banlengths[i];
1047 copy(unbantime, sizeof(unbantime) - 1, g_unbantimes[i]);
1048 copy(reason, sizeof(reason) - 1, g_reasons[i]);
1049 copy(admin_name, sizeof(admin_name) - 1, g_admin_names[i]);
1050 copy(admin_steamid, sizeof(admin_steamid) - 1, g_admin_steamids[i]);
1051
1052 fprintf(f, "^"%s^" ^"%s^" %i ^"%s^" ^"%s^" ^"%s^" ^"%s^"^n",\
1053 steamid,\
1054 name,\
1055 banlength,\
1056 unbantime,\
1057 reason,\
1058 admin_name,\
1059 admin_steamid
1060 );
1061 }
1062
1063 fclose(f);
1064 #endif
1065}
1066#else
1067RemoveBan(pos, const authid[])
1068{
1069 TrieDeleteKey(g_trie, authid);
1070 ArrayDeleteItem(g_array, pos);
1071
1072 g_total_bans--;
1073
1074 #if defined USING_SQL
1075 static query[128];
1076 formatex(query, sizeof(query) - 1,\
1077 "DELETE FROM `%s` WHERE `%s` = '%s';",\
1078 TABLE_NAME, KEY_STEAMID, authid
1079 );
1080
1081 SQL_ThreadQuery(g_sql_tuple, "QueryDeleteBan", query);
1082
1083 new data[BannedData];
1084 for( new i = 0; i < g_total_bans; i++ )
1085 {
1086 ArrayGetArray(g_array, i, data);
1087 TrieSetCell(g_trie, data[bd_steamid], i);
1088 }
1089 #else
1090 new f = fopen(g_ban_file, "wt");
1091
1092 new data[BannedData];
1093 for( new i = 0; i < g_total_bans; i++ )
1094 {
1095 ArrayGetArray(g_array, i, data);
1096 TrieSetCell(g_trie, data[bd_steamid], i);
1097
1098 fprintf(f, "^"%s^" ^"%s^" %i ^"%s^" ^"%s^" ^"%s^" ^"%s^"^n",\
1099 data[bd_steamid],\
1100 data[bd_name],\
1101 data[bd_banlength],\
1102 data[bd_unbantime],\
1103 data[bd_reason],\
1104 data[bd_admin_name],\
1105 data[bd_admin_steamid]
1106 );
1107 }
1108
1109 fclose(f);
1110 #endif
1111}
1112#endif
1113
1114#if defined KEEP_DEFAULT_BANS
1115LoadOldBans(filename[])
1116{
1117 if( file_exists(filename) )
1118 {
1119 new f = fopen(filename, "rt");
1120
1121 static data[96];
1122 static command[10], minutes[10], steamid[35], length, unban_time[32];
1123
1124 while( !feof(f) )
1125 {
1126 fgets(f, data, sizeof(data) - 1);
1127 if( !data[0] ) continue;
1128
1129 parse(data, command, sizeof(command) - 1, minutes, sizeof(minutes) - 1, steamid, sizeof(steamid) - 1);
1130 if( filename[0] == 'b' && !equali(command, "banid") || filename[0] == 'l' && !equali(command, "addip") ) continue;
1131
1132 length = str_to_num(minutes);
1133 GenerateUnbanTime(length, unban_time, sizeof(unban_time) - 1);
1134
1135 AddBan("", steamid, "", length, unban_time, "", "");
1136 }
1137
1138 fclose(f);
1139
1140 static filename2[32];
1141
1142 // copy current
1143 copy(filename2, sizeof(filename2) - 1, filename);
1144
1145 // cut off at the "."
1146 // banned.cfg = banned
1147 // listip.cfg = listip
1148 filename2[containi(filename2, ".")] = 0;
1149
1150 // add 2.cfg
1151 // banned = banned2.cfg
1152 // listip = listip2.cfg
1153 add(filename2, sizeof(filename2) - 1, "2.cfg");
1154
1155 // rename file so that it isnt loaded again
1156 while( !rename_file(filename, filename2, 1) ) { }
1157 }
1158}
1159#endif
1160
1161public LoadBans()
1162{
1163 if( g_total_bans )
1164 {
1165 #if MAX_BANS <= 0
1166 TrieClear(g_trie);
1167 ArrayClear(g_array);
1168 #endif
1169
1170 g_total_bans = 0;
1171 }
1172
1173 #if defined USING_SQL
1174 static query[128];
1175 formatex(query, sizeof(query) - 1,\
1176 "SELECT * FROM `%s`;",\
1177 TABLE_NAME
1178 );
1179
1180 SQL_ThreadQuery(g_sql_tuple, "QueryLoadBans", query);
1181
1182 g_loading_bans = true;
1183 #else
1184 if( file_exists(g_ban_file) )
1185 {
1186 new f = fopen(g_ban_file, "rt");
1187
1188 static filedata[512], length[10];
1189
1190 #if MAX_BANS <= 0
1191 static data[BannedData];
1192 while( !feof(f) )
1193 #else
1194 while( !feof(f) && g_total_bans < MAX_BANS )
1195 #endif
1196 {
1197 fgets(f, filedata, sizeof(filedata) - 1);
1198
1199 if( !filedata[0] ) continue;
1200
1201 #if MAX_BANS <= 0
1202 parse(filedata,\
1203 data[bd_steamid], sizeof(data[bd_steamid]) - 1,\
1204 data[bd_name], sizeof(data[bd_name]) - 1,\
1205 length, sizeof(length) - 1,\
1206 data[bd_unbantime], sizeof(data[bd_unbantime]) - 1,\
1207 data[bd_reason], sizeof(data[bd_reason]) - 1,\
1208 data[bd_admin_name], sizeof(data[bd_admin_name]) - 1,\
1209 data[bd_admin_steamid], sizeof(data[bd_admin_steamid]) - 1
1210 );
1211
1212 data[bd_banlength] = str_to_num(length);
1213
1214 ArrayPushArray(g_array, data);
1215 TrieSetCell(g_trie, data[bd_steamid], g_total_bans);
1216 #else
1217 static steamid[35], name[32], unbantime[32], reason[128], admin_name[32], admin_steamid[35];
1218
1219 parse(filedata,\
1220 steamid, sizeof(steamid) - 1,\
1221 name, sizeof(name) - 1,\
1222 length, sizeof(length) - 1,\
1223 unbantime, sizeof(unbantime) - 1,\
1224 reason, sizeof(reason) - 1,\
1225 admin_name, sizeof(admin_name) - 1,\
1226 admin_steamid, sizeof(admin_steamid) - 1
1227 );
1228
1229 copy(g_names[g_total_bans], sizeof(g_names[]) - 1, name);
1230 copy(g_steamids[g_total_bans], sizeof(g_steamids[]) - 1, steamid);
1231 g_banlengths[g_total_bans] = str_to_num(length);
1232 copy(g_unbantimes[g_total_bans], sizeof(g_unbantimes[]) - 1, unbantime);
1233 copy(g_reasons[g_total_bans], sizeof(g_reasons[]) - 1, reason);
1234 copy(g_admin_names[g_total_bans], sizeof(g_admin_names[]) - 1, admin_name);
1235 copy(g_admin_steamids[g_total_bans], sizeof(g_admin_steamids[]) - 1, admin_steamid);
1236 #endif
1237
1238 g_total_bans++;
1239 }
1240
1241 fclose(f);
1242 }
1243 #endif
1244
1245 // load these after, so when they are added to the file with AddBan(), they aren't loaded again from above.
1246
1247 #if defined KEEP_DEFAULT_BANS
1248 LoadOldBans("banned.cfg");
1249 LoadOldBans("listip.cfg");
1250 #endif
1251}
1252
1253#if defined USING_SQL
1254MakeStringSQLSafe(const input[], output[], len)
1255{
1256 copy(output, len, input);
1257 replace_all(output, len, "'", "*");
1258 replace_all(output, len, "^"", "*");
1259 replace_all(output, len, "`", "*");
1260}
1261#endif
1262
1263GetBanTime(const bantime, length[], len)
1264{
1265 new minutes = bantime;
1266 new hours = 0;
1267 new days = 0;
1268
1269 while( minutes >= 60 )
1270 {
1271 minutes -= 60;
1272 hours++;
1273 }
1274
1275 while( hours >= 24 )
1276 {
1277 hours -= 24;
1278 days++;
1279 }
1280
1281 new bool:add_before;
1282 if( minutes )
1283 {
1284 formatex(length, len, "%i minute%s", minutes, minutes == 1 ? "" : "s");
1285
1286 add_before = true;
1287 }
1288 if( hours )
1289 {
1290 if( add_before )
1291 {
1292 format(length, len, "%i hour%s, %s", hours, hours == 1 ? "" : "s", length);
1293 }
1294 else
1295 {
1296 formatex(length, len, "%i hour%s", hours, hours == 1 ? "" : "s");
1297
1298 add_before = true;
1299 }
1300 }
1301 if( days )
1302 {
1303 if( add_before )
1304 {
1305 format(length, len, "%i day%s, %s", days, days == 1 ? "" : "s", length);
1306 }
1307 else
1308 {
1309 formatex(length, len, "%i day%s", days, days == 1 ? "" : "s");
1310
1311 add_before = true;
1312 }
1313 }
1314 if( !add_before )
1315 {
1316 // minutes, hours, and days = 0
1317 // assume permanent ban
1318 copy(length, len, "Permanent Ban");
1319 }
1320}
1321
1322GenerateUnbanTime(const bantime, unban_time[], len)
1323{
1324 static _hours[5], _minutes[5], _seconds[5], _month[5], _day[5], _year[7];
1325 format_time(_hours, sizeof(_hours) - 1, "%H");
1326 format_time(_minutes, sizeof(_minutes) - 1, "%M");
1327 format_time(_seconds, sizeof(_seconds) - 1, "%S");
1328 format_time(_month, sizeof(_month) - 1, "%m");
1329 format_time(_day, sizeof(_day) - 1, "%d");
1330 format_time(_year, sizeof(_year) - 1, "%Y");
1331
1332 new hours = str_to_num(_hours);
1333 new minutes = str_to_num(_minutes);
1334 new seconds = str_to_num(_seconds);
1335 new month = str_to_num(_month);
1336 new day = str_to_num(_day);
1337 new year = str_to_num(_year);
1338
1339 minutes += bantime;
1340
1341 while( minutes >= 60 )
1342 {
1343 minutes -= 60;
1344 hours++;
1345 }
1346
1347 while( hours >= 24 )
1348 {
1349 hours -= 24;
1350 day++;
1351 }
1352
1353 new max_days = GetDaysInMonth(month, year);
1354 while( day > max_days )
1355 {
1356 day -= max_days;
1357 month++;
1358 }
1359
1360 while( month > 12 )
1361 {
1362 month -= 12;
1363 year++;
1364 }
1365
1366 formatex(unban_time, len, "%i:%02i:%02i %i/%i/%i", hours, minutes, seconds, month, day, year);
1367}
1368
1369GetDaysInMonth(month, year=0)
1370{
1371 switch( month )
1372 {
1373 case 1: return 31; // january
1374 case 2: return ((year % 4) == 0) ? 29 : 28; // february
1375 case 3: return 31; // march
1376 case 4: return 30; // april
1377 case 5: return 31; // may
1378 case 6: return 30; // june
1379 case 7: return 31; // july
1380 case 8: return 31; // august
1381 case 9: return 30; // september
1382 case 10: return 31; // october
1383 case 11: return 30; // november
1384 case 12: return 31; // december
1385 }
1386
1387 return 30;
1388}
1389
1390GetTargetFlags(client)
1391{
1392 static const flags_no_immunity = (CMDTARGET_ALLOW_SELF|CMDTARGET_NO_BOTS);
1393 static const flags_immunity = (CMDTARGET_ALLOW_SELF|CMDTARGET_NO_BOTS|CMDTARGET_OBEY_IMMUNITY);
1394
1395 switch( get_pcvar_num(ab_immunity) )
1396 {
1397 case 1: return flags_immunity;
1398 case 2: return access(client, ADMIN_IMMUNITY) ? flags_no_immunity : flags_immunity;
1399 }
1400
1401 return flags_no_immunity;
1402}
1403
1404GetMaxBanTime(client)
1405{
1406 if( !g_total_maxban_times ) return 0;
1407
1408 new flags = get_user_flags(client);
1409
1410 for( new i = 0; i < g_total_maxban_times; i++ )
1411 {
1412 #if !defined MAX_BANLIMITS
1413 if( flags & ArrayGetCell(g_maxban_flags, i) )
1414 {
1415 return ArrayGetCell(g_maxban_times, i);
1416 }
1417 #else
1418 if( flags & g_maxban_flags[i] )
1419 {
1420 return g_maxban_times[i];
1421 }
1422 #endif
1423 }
1424
1425 return 0;
1426}
1427
1428PrintBanInformation(client, const target_name[], const target_authid[], const reason[], const length, const unban_time[], const admin_name[], const admin_authid[], bool:show_admin, bool:show_website)
1429{
1430 static website[64], ban_length[64];
1431 if( client == 0 )
1432 {
1433 server_print("************************************************");
1434 server_print("%L", client, "AB_BAN_INFORMATION");
1435 server_print("%L: %s", client, "AB_NAME", target_name);
1436 server_print("%L: %s", client, IsValidAuthid(target_authid) ? "AB_STEAMID" : "AB_IP", target_authid);
1437 server_print("%L: %s", client, "AB_REASON", reason);
1438 if( length > 0 )
1439 {
1440 GetBanTime(length, ban_length, sizeof(ban_length) - 1);
1441 server_print("%L: %s", client, "AB_BAN_LENGTH", ban_length);
1442 }
1443 server_print("%L: %s", client, "AB_UNBAN_TIME", unban_time);
1444 if( show_admin )
1445 {
1446 server_print("%L: %s", client, "AB_ADMIN_NAME", admin_name);
1447 server_print("%L: %s", client, "AB_ADMIN_STEAMID", admin_authid);
1448 }
1449 if( show_website )
1450 {
1451 get_pcvar_string(ab_website, website, sizeof(website) - 1);
1452 if( website[0] )
1453 {
1454 server_print("");
1455 server_print("%L", client, "AB_WEBSITE");
1456 server_print("%s", website);
1457 }
1458 }
1459 server_print("************************************************");
1460 }
1461 else
1462 {
1463 client_cmd(client, "echo ^"************************************************^"");
1464 client_cmd(client, "echo ^"%L^"", client, "AB_BAN_INFORMATION");
1465 client_cmd(client, "echo ^"%L: %s^"", client, "AB_NAME", target_name);
1466 client_cmd(client, "echo ^"%L: %s^"", client, IsValidAuthid(target_authid) ? "AB_STEAMID" : "AB_IP", target_authid);
1467 client_cmd(client, "echo ^"%L: %s^"", client, "AB_REASON", reason);
1468 if( length > 0 )
1469 {
1470 GetBanTime(length, ban_length, sizeof(ban_length) - 1);
1471 client_cmd(client, "echo ^"%L: %s^"", client, "AB_BAN_LENGTH", ban_length);
1472 }
1473 client_cmd(client, "echo ^"%L: %s^"", client, "AB_UNBAN_TIME", unban_time);
1474 if( show_admin )
1475 {
1476 client_cmd(client, "echo ^"%L: %s^"", client, "AB_ADMIN_NAME", admin_name);
1477 client_cmd(client, "echo ^"%L: %s^"", client, "AB_ADMIN_STEAMID", admin_authid);
1478 }
1479 if( show_website )
1480 {
1481 get_pcvar_string(ab_website, website, sizeof(website) - 1);
1482 if( website[0] )
1483 {
1484 client_cmd(client, "echo ^"^"");
1485 client_cmd(client, "echo ^"%L^"", client, "AB_WEBSITE");
1486 client_cmd(client, "echo ^"%s^"", website);
1487 }
1488 }
1489 client_cmd(client, "echo ^"************************************************^"");
1490 }
1491}
1492
1493PrintActivity(const admin_name[], const message_fmt[], any:...)
1494{
1495 if( !get_playersnum() ) return;
1496
1497 new activity = get_pcvar_num(amx_show_activity);
1498 if( !(0 <= activity <= 5) )
1499 {
1500 set_pcvar_num(amx_show_activity, (activity = 2));
1501 }
1502
1503 static message[192], temp[192];
1504 vformat(message, sizeof(message) - 1, message_fmt, 3);
1505
1506 for( new client = 1; client <= g_max_clients; client++ )
1507 {
1508 if( !is_user_connected(client) ) continue;
1509
1510 switch( is_user_admin(client) ? g_admin_activity[activity] : g_normal_activity[activity] )
1511 {
1512 case ACTIVITY_NONE:
1513 {
1514
1515 }
1516 case ACTIVITY_HIDE:
1517 {
1518 copy(temp, sizeof(temp) - 1, message);
1519 replace(temp, sizeof(temp) - 1, "$name", "ADMIN");
1520
1521 message_begin(MSG_ONE_UNRELIABLE, g_msgid_SayText, _, client);
1522 write_byte(client);
1523 write_string(temp);
1524 message_end();
1525 }
1526 case ACTIVITY_SHOW:
1527 {
1528 copy(temp, sizeof(temp) - 1, message);
1529 replace(temp, sizeof(temp) - 1, "$name", admin_name);
1530
1531 message_begin(MSG_ONE_UNRELIABLE, g_msgid_SayText, _, client);
1532 write_byte(client);
1533 write_string(temp);
1534 message_end();
1535 }
1536 }
1537 }
1538}
1539
1540Print(const message_fmt[], any:...)
1541{
1542 if( !get_playersnum() ) return;
1543
1544 static message[192];
1545 vformat(message, sizeof(message) - 1, message_fmt, 2);
1546
1547 for( new client = 1; client <= g_max_clients; client++ )
1548 {
1549 if( !is_user_connected(client) ) continue;
1550
1551 message_begin(MSG_ONE_UNRELIABLE, g_msgid_SayText, _, client);
1552 write_byte(client);
1553 write_string(message);
1554 message_end();
1555 }
1556}
1557
1558Log(const message_fmt[], any:...)
1559{
1560 static message[256];
1561 vformat(message, sizeof(message) - 1, message_fmt, 2);
1562
1563 static filename[96];
1564 #if defined HISTORY_ONE_FILE
1565 if( !filename[0] )
1566 {
1567 get_basedir(filename, sizeof(filename) - 1);
1568 add(filename, sizeof(filename) - 1, "/logs/ban_history.log");
1569 }
1570 #else
1571 static dir[64];
1572 if( !dir[0] )
1573 {
1574 get_basedir(dir, sizeof(dir) - 1);
1575 add(dir, sizeof(dir) - 1, "/logs");
1576 }
1577
1578 format_time(filename, sizeof(filename) - 1, "%m%d%Y");
1579 format(filename, sizeof(filename) - 1, "%s/BAN_HISTORY_%s.log", dir, filename);
1580 #endif
1581
1582 log_amx("%s", message);
1583 log_to_file(filename, "%s", message);
1584}
1585/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
1586*{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang1033\\ f0\\ fs16 \n\\ par }
1587*/