· 6 years ago · Dec 12, 2019, 04:12 AM
1#include <amxmodx>
2#include <amxmisc>
3#include <sqlx>
4
5#pragma semicolon 1
6
7// File nale .sq3
8#define SQL_FILE_NAME "sql_ban"
9
10// Table name
11#define SQL_TABLE "lite_ban_list"
12
13// Create query
14#define SQL_CREATE_TABLE "CREATE TABLE IF NOT EXISTS lite_ban_list (id INTEGER, auth TEXT, name TEXT, now TEXT, expire TEXT, reason TEXT, admin_auth TEXT, admin TEXT, enabled INTEGER DEFAULT 1, PRIMARY KEY(id AUTOINCREMENT));"
15
16// Ban Query
17#define SQL_QUERY_INSERT_BAN "INSERT INTO %s VALUES(NULL, '%s', '%s', CURRENT_TIMESTAMP, DATETIME(CURRENT_TIMESTAMP, '+%i MINUTES'), '%s', '%s', '%s', 1);"
18
19// Check BAN
20#define SQL_QUERY_CHECK_BAN "SELECT *, (expire > CURRENT_TIMESTAMP) AS banned FROM %s WHERE banned = 1 AND %s = 1 AND %s = '%s' ORDER BY %s DESC LIMIT 1;"
21
22// Column nammes and position
23#define SQL_COL_0 "id"
24#define SQL_COL_1 "auth"
25#define SQL_COL_2 "name"
26#define SQL_COL_3 "now"
27#define SQL_COL_4 "expire"
28#define SQL_COL_5 "reason"
29#define SQL_COL_6 "admin_auth"
30#define SQL_COL_7 "admin"
31#define SQL_COL_8 "enabled"
32
33// Global handler
34new Handle:g_hTuple;
35
36// Query Types
37enum _:SQL_QUERY_TYPES
38{
39 QUERY_BAN_CREATE,
40 QUERY_BAN_CHECK,
41 QUERY_BAN_ACTION,
42 QUERY_BAN_LIST,
43 QUERY_BAN_FIND_ID,
44 QUERY_BAN_DISABLE_ID,
45 QUERY_BAN_FIND
46};
47
48enum _:ePlayerData
49{
50 szUserName[MAX_NAME_LENGTH],
51 szUserAuth[MAX_AUTHID_LENGTH]
52};
53
54new g_iPlayerData[MAX_PLAYERS+1][ePlayerData];
55
56new g_iMenuInfo[MAX_PLAYERS+1][3];
57new g_iMenuType[MAX_PLAYERS+1];
58new g_szLastQuery[MAX_PLAYERS+1][64];
59
60public plugin_init()
61{
62 // Init Plugin
63 register_plugin("SQL Lite Ban System",AMXX_VERSION_STR,"SmileY");
64
65 // Set it to sqlite affinity
66 SQL_SetAffinity("sqlite");
67
68 // Try to create tuple using file name
69 g_hTuple = SQL_MakeDbTuple("","","",SQL_FILE_NAME);
70
71 // Execute query to create table if not exists
72 new szData[2];
73 szData[0] = QUERY_BAN_CREATE;
74
75 SQL_ThreadQuery(g_hTuple,"SQL_HandleGlobal",SQL_CREATE_TABLE,szData,sizeof(szData));
76
77 register_concmd("amx_ban_list","ACTION_ListBan",ADMIN_BAN,"List banned players in the database");
78 register_concmd("amx_ban_find","ACTION_FindBan",ADMIN_BAN,"<query> - Find a ban by name, auth, id, or admin data");
79}
80
81public plugin_end()
82{
83 // If has tuple
84 if(g_hTuple)
85 {
86 // Freed it!
87 SQL_FreeHandle(g_hTuple);
88 }
89}
90
91public SQL_HandleGlobal(iState,Handle:hResult,szError[],iError,szData[],iSize)
92{
93 if(iState == TQUERY_SUCCESS)
94 {
95 switch(szData[0])
96 {
97 case QUERY_BAN_CHECK:
98 {
99 if(is_user_connected(szData[1]) && SQL_NumRows(hResult))
100 {
101 new szName[2][MAX_NAME_LENGTH],szAuth[2][MAX_AUTHID_LENGTH];
102 new szBannedTime[32],szExpireTime[32];
103 new szReason[128];
104
105 new iBanIndex = SQL_ReadResult(hResult,SQL_FieldNameToNum(hResult,SQL_COL_0));
106
107 if(iBanIndex)
108 {
109 SQL_ReadResult(hResult,SQL_FieldNameToNum(hResult,SQL_COL_1),szAuth[0],sizeof(szAuth[]));
110 SQL_ReadResult(hResult,SQL_FieldNameToNum(hResult,SQL_COL_2),szName[0],sizeof(szName[]));
111
112 SQL_ReadResult(hResult,SQL_FieldNameToNum(hResult,SQL_COL_3),szBannedTime,charsmax(szBannedTime));
113 SQL_ReadResult(hResult,SQL_FieldNameToNum(hResult,SQL_COL_4),szExpireTime,charsmax(szExpireTime));
114
115 SQL_ReadResult(hResult,SQL_FieldNameToNum(hResult,SQL_COL_5),szReason,charsmax(szReason));
116
117 SQL_ReadResult(hResult,SQL_FieldNameToNum(hResult,SQL_COL_6),szAuth[1],sizeof(szAuth[]));
118 SQL_ReadResult(hResult,SQL_FieldNameToNum(hResult,SQL_COL_7),szName[1],sizeof(szName[]));
119
120 ACTION_PrintBan(szData[1],iBanIndex,szName[0],szAuth[0],szBannedTime,szExpireTime,szReason,szName[1],szAuth[1]);
121
122 server_cmd("kick #%i ^"YOU HAVE BEEN BANNED! CHECK YOUR CONSOLE!!^"",get_user_userid(szData[1]));
123 }
124 }
125 }
126 case QUERY_BAN_ACTION:
127 {
128 if(SQL_GetInsertId(hResult))
129 {
130 ACTION_CheckBan(szData[1]);
131 }
132 }
133 case QUERY_BAN_FIND_ID:
134 {
135 if(is_user_connected(szData[1]) && SQL_NumRows(hResult))
136 {
137 new szBan[12][64];
138
139 for(new i;i < SQL_NumColumns(hResult);i++)
140 {
141 SQL_ReadResult(hResult,i,szBan[i],charsmax(szBan[]));
142 }
143
144 new szTitle[512];
145 formatex
146 (
147 szTitle,
148 charsmax(szTitle),
149 "\wID: \r#%s\w^nAdmin: \y%s\w^nPlayer: \y%s\w (\y%s\w)^nAt: \y%s\w^nExpire: \y%s\w^nReason: \r%s",
150 szBan[0],
151 szBan[7],
152 szBan[2],
153 szBan[1],
154 szBan[9],
155 szBan[10],
156 szBan[5]
157 );
158
159 new iMenu = menu_create(szTitle,"ACTION_MenuHandleRemoveBan");
160
161 menu_additem(iMenu,"Back to Ban List","0");
162 menu_additem(iMenu,"Yes, Remove Ban Record",szBan[0]);
163
164 menu_setprop(iMenu,MPROP_EXIT,MEXIT_NEVER);
165 menu_display(szData[1],iMenu);
166 }
167 }
168 case QUERY_BAN_DISABLE_ID:
169 {
170 if(is_user_connected(szData[1]))
171 {
172 if(SQL_AffectedRows(hResult))
173 {
174 client_print_color(szData[1],szData[1],"[BAN] Succefully disabled ticket: ^3#%i^1...",szData[2]);
175 }
176 else
177 {
178 client_print_color(szData[1],szData[1],"[BAN] Failed to disable or ticket not found: ^3#%i^1...",szData[2]);
179 }
180 }
181 }
182 case QUERY_BAN_FIND,QUERY_BAN_LIST:
183 {
184 if(is_user_connected(szData[1]))
185 {
186 if(is_user_connected(szData[1]))
187 {
188 new szBanId[6];
189 new szName[MAX_NAME_LENGTH];
190 new szExpire[32];
191
192 new iMenu = menu_create("Banned Players:","ACTION_MenuHandle");
193
194 while(SQL_MoreResults(hResult))
195 {
196 SQL_ReadResult(hResult,SQL_FieldNameToNum(hResult,SQL_COL_0),szBanId,charsmax(szBanId));
197 SQL_ReadResult(hResult,SQL_FieldNameToNum(hResult,SQL_COL_2),szName,charsmax(szName));
198 SQL_ReadResult(hResult,SQL_FieldNameToNum(hResult,SQL_COL_4),szExpire,charsmax(szExpire));
199
200 menu_additem(iMenu,fmt("\r#%s\w %s \R\y%s",szBanId,szName,szExpire),szBanId);
201
202 SQL_NextRow(hResult);
203 }
204
205 menu_display(szData[1],iMenu,g_iMenuInfo[szData[1]][2]);
206 }
207 }
208 }
209 }
210 }
211 else
212 {
213 server_print("ERRO: %s",szError);
214 }
215}
216
217public ACTION_MenuHandleRemoveBan(id,iMenu,iKey)
218{
219 if(iKey != MENU_EXIT)
220 {
221 if(iKey)
222 {
223 new szBanIndex[8];
224 menu_item_getinfo(iMenu,iKey,_,szBanIndex,charsmax(szBanIndex));
225
226 ACTION_DisableBan(id,szBanIndex);
227 }
228 else
229 {
230 if(g_iMenuType[id] == QUERY_BAN_LIST)
231 {
232 ACTION_ListBan(id,ADMIN_BAN);
233 }
234 else
235 {
236 ACTION_FindBanByQuery(id,g_szLastQuery[id]);
237 }
238
239 }
240 }
241
242 menu_destroy(iMenu);
243 return PLUGIN_HANDLED;
244}
245
246public ACTION_MenuHandle(id,iMenu,iKey)
247{
248 if(is_user_connected(id))
249 {
250 player_menu_info(id,g_iMenuInfo[id][0],g_iMenuInfo[id][1],g_iMenuInfo[id][2]);
251
252 if(iKey != MENU_EXIT)
253 {
254 new szBanIndex[8];
255 menu_item_getinfo(iMenu,iKey,_,szBanIndex,charsmax(szBanIndex));
256
257 ACTION_FindBanById(id,szBanIndex);
258 }
259 }
260
261 menu_destroy(iMenu);
262 return PLUGIN_HANDLED;
263}
264
265public client_putinserver(id)
266{
267 get_user_authid(id,g_iPlayerData[id][szUserAuth],MAX_AUTHID_LENGTH-1);
268 get_user_name(id,g_iPlayerData[id][szUserName],MAX_NAME_LENGTH-1);
269
270 set_task(2.0,"ACTION_CheckBan",id);
271}
272
273ACTION_BanPlayer(id,iPlayer,iMinutes,szReason[])
274{
275 new szQuery[512];
276 formatex
277 (
278 szQuery,
279 charsmax(szQuery),
280 SQL_QUERY_INSERT_BAN,
281 SQL_TABLE,
282 g_iPlayerData[iPlayer][szUserAuth],
283 g_iPlayerData[iPlayer][szUserName],
284 iMinutes,
285 szReason,
286 g_iPlayerData[id][szUserAuth],
287 g_iPlayerData[id][szUserName]
288 );
289
290 new szData[2];
291 szData[0] = QUERY_BAN_ACTION;
292 szData[1] = iPlayer;
293
294 SQL_ThreadQuery(g_hTuple,"SQL_HandleGlobal",szQuery,szData,sizeof(szData));
295}
296
297public ACTION_CheckBan(id)
298{
299 new szQuery[256];
300 formatex
301 (
302 szQuery,
303 charsmax(szQuery),
304 SQL_QUERY_CHECK_BAN,
305 SQL_TABLE,
306 SQL_COL_8,
307 SQL_COL_1,
308 g_iPlayerData[id][szUserAuth],
309 SQL_COL_0
310 );
311
312 new szData[2];
313 szData[0] = QUERY_BAN_CHECK;
314 szData[1] = id;
315
316 SQL_ThreadQuery(g_hTuple,"SQL_HandleGlobal",szQuery,szData,sizeof(szData));
317}
318
319ACTION_PrintBan(id,iBanIndex,szName[],szAuth[],szBannedTime[],szExpireTime[],szReason[],szAdmin[],szAdminAuth[])
320{
321 console_print(id,"--------- YOU HAVE BEEN BANNED ---------");
322 console_print(id,"ADMIN: %s (%s)",szAdmin,szAdminAuth);
323 console_print(id,"PLAYER: %s (%s)",szName,szAuth);
324 console_print(id,"AT: %s",szBannedTime);
325 console_print(id,"UNTIL: %s",szExpireTime);
326 console_print(id,"REASON: %s",szReason);
327 console_print(id,"TICKET: #%i",iBanIndex);
328 console_print(id,"--------- YOU HAVE BEEN BANNED ---------",iBanIndex);
329}
330
331public ACTION_ListBan(id,iLevel)
332{
333 if(access(id,iLevel))
334 {
335 new szQuery[512];
336 formatex
337 (
338 szQuery,
339 charsmax(szQuery),
340 "SELECT id, name, enabled, strftime('%%d/%%m/%%Y %%H:%%M', expire) AS expire, (CURRENT_TIMESTAMP > expire) AS expired FROM lite_ban_list WHERE expired = 0 AND enabled = 1 ORDER BY id DESC;"
341 );
342
343 new szData[2];
344 szData[0] = QUERY_BAN_LIST;
345 szData[1] = id;
346
347 g_iMenuType[szData[1]] = QUERY_BAN_LIST;
348
349 SQL_ThreadQuery(g_hTuple,"SQL_HandleGlobal",szQuery,szData,sizeof(szData));
350 }
351
352 return PLUGIN_HANDLED;
353}
354
355public ACTION_FindBan(id,iLevel,iCid)
356{
357 if(!cmd_access(id,iLevel,iCid,2))
358 {
359 return PLUGIN_HANDLED;
360 }
361
362 new szLike[64];
363 read_args(szLike,charsmax(szLike));
364 remove_quotes(szLike);
365
366 ACTION_FindBanByQuery(id,szLike);
367
368 return PLUGIN_HANDLED;
369}
370
371ACTION_FindBanByQuery(id,const szLike[])
372{
373 new szQuery[256];
374 formatex
375 (
376 szQuery,
377 charsmax(szQuery),
378 "SELECT * FROM lite_ban_list WHERE (id = '%s' OR auth LIKE '%%%s%%' OR name LIKE '%%%s%%' OR reason LIKE '%%%s%%' OR admin_auth LIKE '%%%s%%' OR admin LIKE '%%%s%%') ORDER BY id DESC;",
379 szLike,
380 szLike,
381 szLike,
382 szLike,
383 szLike,
384 szLike
385 );
386
387 copy(g_szLastQuery[id],charsmax(g_szLastQuery[]),szLike);
388
389 new szData[2];
390 szData[0] = QUERY_BAN_FIND;
391 szData[1] = id;
392
393 g_iMenuType[szData[1]] = QUERY_BAN_FIND;
394
395 SQL_ThreadQuery(g_hTuple,"SQL_HandleGlobal",szQuery,szData,sizeof(szData));
396}
397
398ACTION_FindBanById(id,const szBanIndex[])
399{
400 new szQuery[256];
401 formatex
402 (
403 szQuery,
404 charsmax(szQuery),
405 "SELECT *, strftime('%%d/%%m/%%Y %%H:%%M', now) AS now, strftime('%%d/%%m/%%Y %%H:%%M', expire) AS expire, (CURRENT_TIMESTAMP > expire) AS expired FROM lite_ban_list WHERE id = '%s';",
406 szBanIndex
407 );
408
409 new szData[3];
410 szData[0] = QUERY_BAN_FIND_ID;
411 szData[1] = id;
412
413 SQL_ThreadQuery(g_hTuple,"SQL_HandleGlobal",szQuery,szData,sizeof(szData));
414}
415
416ACTION_DisableBan(id,const szBanIndex[])
417{
418 new szQuery[256];
419 formatex
420 (
421 szQuery,
422 charsmax(szQuery),
423 "UPDATE lite_ban_list SET enabled = 0 WHERE id = '%s';",
424 szBanIndex
425 );
426
427 new szData[3];
428 szData[0] = QUERY_BAN_DISABLE_ID;
429 szData[1] = id;
430 szData[2] = str_to_num(szBanIndex);
431
432 SQL_ThreadQuery(g_hTuple,"SQL_HandleGlobal",szQuery,szData,sizeof(szData));
433
434 client_print_color(id,id,"[BAN] Trying to disable ^3#%s^1...",szBanIndex);
435}