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