· 6 years ago · Dec 12, 2019, 03:00 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_FIND,
43 QUERY_BAN_FIND_ID,
44 QUERY_BAN_DISABLE_ID
45};
46
47enum _:ePlayerData
48{
49 szUserName[MAX_NAME_LENGTH],
50 szUserAuth[MAX_AUTHID_LENGTH]
51};
52
53new g_iPlayerData[MAX_PLAYERS+1][ePlayerData];
54
55new g_iMenuInfo[MAX_PLAYERS+1][3];
56
57public plugin_init()
58{
59 // Init Plugin
60 register_plugin("SQL Lite Ban System",AMXX_VERSION_STR,"SmileY");
61
62 // Set it to sqlite affinity
63 SQL_SetAffinity("sqlite");
64
65 // Try to create tuple using file name
66 g_hTuple = SQL_MakeDbTuple("","","",SQL_FILE_NAME);
67
68 // Execute query to create table if not exists
69 new szData[2];
70 szData[0] = QUERY_BAN_CREATE;
71
72 SQL_ThreadQuery(g_hTuple,"SQL_HandleGlobal",SQL_CREATE_TABLE,szData,sizeof(szData));
73
74 register_concmd("amx_ban_list","ACTION_FindBan",ADMIN_BAN,"Find a ban by user name or auth or ticket id");
75}
76
77public plugin_end()
78{
79 // If has tuple
80 if(g_hTuple)
81 {
82 // Freed it!
83 SQL_FreeHandle(g_hTuple);
84 }
85}
86
87public SQL_HandleGlobal(iState,Handle:hResult,szError[],iError,szData[],iSize)
88{
89 if(iState == TQUERY_SUCCESS)
90 {
91 switch(szData[0])
92 {
93 case QUERY_BAN_CHECK:
94 {
95 if(is_user_connected(szData[1]) && SQL_NumRows(hResult))
96 {
97 new szName[2][MAX_NAME_LENGTH],szAuth[2][MAX_AUTHID_LENGTH];
98 new szBannedTime[32],szExpireTime[32];
99 new szReason[128];
100
101 new iBanIndex = SQL_ReadResult(hResult,SQL_FieldNameToNum(hResult,SQL_COL_0));
102
103 if(iBanIndex)
104 {
105 SQL_ReadResult(hResult,SQL_FieldNameToNum(hResult,SQL_COL_1),szAuth[0],sizeof(szAuth[]));
106 SQL_ReadResult(hResult,SQL_FieldNameToNum(hResult,SQL_COL_2),szName[0],sizeof(szName[]));
107
108 SQL_ReadResult(hResult,SQL_FieldNameToNum(hResult,SQL_COL_3),szBannedTime,charsmax(szBannedTime));
109 SQL_ReadResult(hResult,SQL_FieldNameToNum(hResult,SQL_COL_4),szExpireTime,charsmax(szExpireTime));
110
111 SQL_ReadResult(hResult,SQL_FieldNameToNum(hResult,SQL_COL_5),szReason,charsmax(szReason));
112
113 SQL_ReadResult(hResult,SQL_FieldNameToNum(hResult,SQL_COL_6),szAuth[1],sizeof(szAuth[]));
114 SQL_ReadResult(hResult,SQL_FieldNameToNum(hResult,SQL_COL_7),szName[1],sizeof(szName[]));
115
116 ACTION_PrintBan(szData[1],iBanIndex,szName[0],szAuth[0],szBannedTime,szExpireTime,szReason,szName[1],szAuth[1]);
117
118 server_cmd("kick #%i ^"YOU HAVE BEEN BANNED! CHECK YOUR CONSOLE!!^"",get_user_userid(szData[1]));
119 }
120 }
121 }
122 case QUERY_BAN_ACTION:
123 {
124 if(SQL_GetInsertId(hResult))
125 {
126 ACTION_CheckBan(szData[1]);
127 }
128 }
129 case QUERY_BAN_FIND:
130 {
131 if(is_user_connected(szData[1]))
132 {
133 new szBanId[6];
134 new szName[MAX_NAME_LENGTH];
135 new szExpire[32];
136
137 new iMenu = menu_create("Banned Players:","ACTION_MenuHandle");
138
139 while(SQL_MoreResults(hResult))
140 {
141 SQL_ReadResult(hResult,SQL_FieldNameToNum(hResult,SQL_COL_0),szBanId,charsmax(szBanId));
142 SQL_ReadResult(hResult,SQL_FieldNameToNum(hResult,SQL_COL_2),szName,charsmax(szName));
143 SQL_ReadResult(hResult,SQL_FieldNameToNum(hResult,SQL_COL_4),szExpire,charsmax(szExpire));
144
145 menu_additem(iMenu,fmt("\r#%s\w %s \R\y%s",szBanId,szName,szExpire),szBanId);
146
147 SQL_NextRow(hResult);
148 }
149
150 menu_display(szData[1],iMenu,g_iMenuInfo[szData[1]][2]);
151 }
152 }
153 case QUERY_BAN_FIND_ID:
154 {
155 if(is_user_connected(szData[1]) && SQL_NumRows(hResult))
156 {
157 new szBan[12][64];
158
159 for(new i;i < SQL_NumColumns(hResult);i++)
160 {
161 SQL_ReadResult(hResult,i,szBan[i],charsmax(szBan[]));
162 }
163
164 new szTitle[512];
165 formatex
166 (
167 szTitle,
168 charsmax(szTitle),
169 "\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",
170 szBan[0],
171 szBan[7],
172 szBan[2],
173 szBan[1],
174 szBan[9],
175 szBan[10],
176 szBan[5]
177 );
178
179 new iMenu = menu_create(szTitle,"ACTION_MenuHandleRemoveBan");
180
181
182 menu_additem(iMenu,"Back to Ban List","0");
183 menu_additem(iMenu,"Yes, Remove Ban Record",szBan[0]);
184
185 menu_setprop(iMenu,MPROP_EXIT,MEXIT_NEVER);
186 menu_display(szData[1],iMenu);
187 }
188 }
189 case QUERY_BAN_DISABLE_ID:
190 {
191 if(is_user_connected(szData[1]))
192 {
193 if(SQL_AffectedRows(hResult))
194 {
195 client_print_color(szData[1],szData[1],"[BAN] Succefully disabled ticket: ^3#%i^1...",szData[2]);
196 }
197 else
198 {
199 client_print_color(szData[1],szData[1],"[BAN] Failed to disable or ticket not found: ^3#%i^1...",szData[2]);
200 }
201 }
202 }
203 }
204 }
205 else
206 {
207 server_print("ERRO: %s",szError);
208 }
209}
210
211public ACTION_MenuHandleRemoveBan(id,iMenu,iKey)
212{
213 if(iKey != MENU_EXIT)
214 {
215 if(iKey)
216 {
217 new szBanIndex[8];
218 menu_item_getinfo(iMenu,iKey,_,szBanIndex,charsmax(szBanIndex));
219
220 ACTION_DisableBan(id,szBanIndex);
221 }
222 else
223 {
224 ACTION_FindBan(id,ADMIN_BAN);
225 }
226 }
227 else
228 {
229 arrayset(g_iMenuInfo[id],0,sizeof(g_iMenuInfo[]));
230 }
231
232 menu_destroy(iMenu);
233 return PLUGIN_HANDLED;
234}
235
236public ACTION_MenuHandle(id,iMenu,iKey)
237{
238 if(is_user_connected(id))
239 {
240 player_menu_info(id,g_iMenuInfo[id][0],g_iMenuInfo[id][1],g_iMenuInfo[id][2]);
241
242 if(iKey != MENU_EXIT)
243 {
244 new szBanIndex[8];
245 menu_item_getinfo(iMenu,iKey,_,szBanIndex,charsmax(szBanIndex));
246
247 ACTION_FindBanById(id,szBanIndex);
248 }
249 else
250 {
251 arrayset(g_iMenuInfo[id],0,sizeof(g_iMenuInfo[]));
252 }
253 }
254
255 menu_destroy(iMenu);
256 return PLUGIN_HANDLED;
257}
258
259public client_putinserver(id)
260{
261 get_user_authid(id,g_iPlayerData[id][szUserAuth],MAX_AUTHID_LENGTH-1);
262 get_user_name(id,g_iPlayerData[id][szUserName],MAX_NAME_LENGTH-1);
263
264 set_task(2.0,"ACTION_CheckBan",id);
265}
266
267stock ACTION_BanPlayer(id,iPlayer,iMinutes,szReason[])
268{
269 new szQuery[512];
270 formatex
271 (
272 szQuery,
273 charsmax(szQuery),
274 SQL_QUERY_INSERT_BAN,'
275 SQL_TABLE,
276 g_iPlayerData[iPlayer][szUserAuth],
277 g_iPlayerData[iPlayer][szUserName],
278 iMinutes,
279 szReason,
280 g_iPlayerData[id][szUserAuth],
281 g_iPlayerData[id][szUserName]
282 );
283
284 new szData[2];
285 szData[0] = QUERY_BAN_ACTION;
286 szData[1] = iPlayer;
287
288 SQL_ThreadQuery(g_hTuple,"SQL_HandleGlobal",szQuery,szData,sizeof(szData));
289}
290
291public ACTION_CheckBan(id)
292{
293 new szQuery[256];
294 formatex
295 (
296 szQuery,
297 charsmax(szQuery),
298 SQL_QUERY_CHECK_BAN,
299 SQL_TABLE,
300 SQL_COL_8,
301 SQL_COL_1,
302 g_iPlayerData[id][szUserAuth],
303 SQL_COL_0
304 );
305
306 new szData[2];
307 szData[0] = QUERY_BAN_CHECK;
308 szData[1] = id;
309
310 SQL_ThreadQuery(g_hTuple,"SQL_HandleGlobal",szQuery,szData,sizeof(szData));
311}
312
313ACTION_PrintBan(id,iBanIndex,szName[],szAuth[],szBannedTime[],szExpireTime[],szReason[],szAdmin[],szAdminAuth[])
314{
315 console_print(id,"--------- YOU HAVE BEEN BANNED ---------");
316 console_print(id,"ADMIN: %s (%s)",szAdmin,szAdminAuth);
317 console_print(id,"PLAYER: %s (%s)",szName,szAuth);
318 console_print(id,"AT: %s",szBannedTime);
319 console_print(id,"UNTIL: %s",szExpireTime);
320 console_print(id,"REASON: %s",szReason);
321 console_print(id,"TICKET: #%i",iBanIndex);
322 console_print(id,"--------- YOU HAVE BEEN BANNED ---------",iBanIndex);
323}
324
325public ACTION_FindBan(id,iLevel)
326{
327 if(access(id,iLevel))
328 {
329 new szQuery[512];
330 formatex
331 (
332 szQuery,
333 charsmax(szQuery),
334 "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;"
335 );
336
337 new szData[2];
338 szData[0] = QUERY_BAN_FIND;
339 szData[1] = id;
340
341 SQL_ThreadQuery(g_hTuple,"SQL_HandleGlobal",szQuery,szData,sizeof(szData));
342 }
343
344 return PLUGIN_HANDLED;
345}
346
347ACTION_FindBanById(id,const szBanIndex[])
348{
349 new szQuery[256];
350 formatex
351 (
352 szQuery,
353 charsmax(szQuery),
354 "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';",
355 szBanIndex
356 );
357
358 new szData[3];
359 szData[0] = QUERY_BAN_FIND_ID;
360 szData[1] = id;
361
362 SQL_ThreadQuery(g_hTuple,"SQL_HandleGlobal",szQuery,szData,sizeof(szData));
363}
364
365ACTION_DisableBan(id,const szBanIndex[])
366{
367 new szQuery[256];
368 formatex
369 (
370 szQuery,
371 charsmax(szQuery),
372 "UPDATE lite_ban_list SET enabled = 0 WHERE id = '%s';",
373 szBanIndex
374 );
375
376 new szData[3];
377 szData[0] = QUERY_BAN_DISABLE_ID;
378 szData[1] = id;
379 szData[2] = str_to_num(szBanIndex);
380
381 SQL_ThreadQuery(g_hTuple,"SQL_HandleGlobal",szQuery,szData,sizeof(szData));
382
383 client_print_color(id,id,"[BAN] Trying to disable ^3#%s^1...",szBanIndex);
384}