· 8 years ago · Mar 15, 2018, 08:18 PM
1#include <amxmodx>
2#include <fakemeta>
3#include <sqlx>
4
5static const
6 HOST[]="localhost",
7 USER[]="valiboss",
8 PASS[]="justdapz",
9 DB[]="admins",
10 TABEL[]="",
11 COL_ACC[]="unique",
12 COL_USER[]="auth"
13
14new Handle:g_SqlTuple,query[512]
15
16#define MAX_NAMES 65
17#define TASK_MESS 1000
18#define TASK_KICK 2000
19#define TASK_TIMER 4000
20new g_count,g_logtime,g_player_time[33],bool:is_logged[33],bool:changing_name[33],bool:data_ready = false,password[33][34],g_saytxt,g_sync_hud,
21g_maxplayers,type,sz_time[9],count,col_name,input[32],part_names[MAX_NAMES][32],ending_names[MAX_NAMES][32],starting_names[MAX_NAMES][32],
22Trie:g_full_nmaes,bool:is_registered[33],g_client_data[33][35],check_pass[34],params[2]
23new const separator_1[] = "==============================================================================="
24new const separator_2[] = "-------------------------------------------------------------------------------"
25new const prefix[] = "[LOG-IN SYSTEM]";
26
27public plugin_init()
28{
29 g_count=register_cvar("vjd_logcount", "1")
30 g_logtime=register_cvar("vjd_logtime", "60.0")
31
32 register_concmd("amx_passcode","VERIFY_USER")
33
34 register_forward(FM_ClientUserInfoChanged, "ClientInfoChanged")
35
36 g_full_nmaes = TrieCreate()
37 g_saytxt = get_user_msgid("SayText")
38 g_sync_hud = CreateHudSyncObj()
39 g_maxplayers = get_maxplayers()
40
41 set_task(1.0, "init_mysql")
42}
43
44public init_mysql()
45{
46 g_SqlTuple = SQL_MakeDbTuple(HOST, USER, PASS, DB)
47
48 formatex(query, charsmax(query), "CREATE TABLE IF NOT EXISTS %s (Name VARCHAR(65)) ; CREATE TABLE IF NOT EXISTS %s (Password VARCHAR(65))",COL_USER,COL_ACC)
49 SQL_ThreadQuery(g_SqlTuple, "QueryCreateTable", query)
50}
51public QueryCreateTable(failstate, Handle:query1, error[], errcode, data[], datasize, Float:queuetime)
52{
53 if(failstate == TQUERY_CONNECT_FAILED) set_fail_state("[LOG-IN SYSTEM] Could not connect to database!")
54 else if(failstate == TQUERY_QUERY_FAILED) set_fail_state("[LOG-IN SYSTEM] Query failed!")
55 else if(errcode) server_print("%s Error on query: %s", prefix, error)
56 else
57 {
58 server_print("%s MYSQL connection succesful!", prefix)
59
60 data_ready = true
61
62 for(new i = 1 ; i <= g_maxplayers ; i++)
63 {
64 if(!is_user_connecting(i) && !is_user_connected(i)) continue//da
65
66 CheckClient(i)
67 }
68 }
69}
70public QueryCollectData(failstate, Handle:query, error[], errcode, data[], datasize, Float:queuetime)
71{
72 if(failstate == TQUERY_CONNECT_FAILED || failstate == TQUERY_QUERY_FAILED)
73 {
74 log_amx("%s", error)
75 return
76 }
77 else
78 {
79 type = data[0]
80 get_time("%H:%M:%S", sz_time, charsmax(sz_time))
81
82 if(type)
83 {
84 //new count1 = 0, count2 = 0, count3 = 0, count4 = 0
85 count = 0
86 col_name = SQL_FieldNameToNum(query, COL_USER)
87
88 server_print(" ")
89 server_print(separator_1)
90 server_print(prefix)
91 server_print(separator_2)
92 server_print("[%s] > Loading SQL table ^"%s^"", sz_time,COL_USER)
93
94 while(SQL_MoreResults(query))
95 {
96 SQL_ReadResult(query, col_name/*, input, charsmax(input)*/)
97
98 /*if(input[0] == '%')
99 {
100 if(input[strlen(input) - 1] == '%') //Part name
101 {
102 if(count1 >= MAX_NAMES) continue
103
104 replace_all(input, charsmax(input), "%", "")
105 part_names[count1++] = input
106 }
107 else // Starting name
108 {
109 if(count2 >= MAX_NAMES) continue
110
111 replace_all(input, charsmax(input), "%", "")
112 ending_names[count2++] = input
113 }
114 }
115 else
116 {
117 if(input[strlen(input) - 1] == '%') //Ending name
118 {
119 if(count3 >= MAX_NAMES) continue
120
121 replace_all(input, charsmax(input), "%", "")
122 starting_names[count3++] = input
123 }
124 else //Full name
125 {
126 if(++count4 >= MAX_NAMES) continue
127
128 replace_all(input, charsmax(input), "%", "")
129 TrieSetCell(g_full_nmaes, input, 1)
130 }
131 }*/
132
133 count++
134 SQL_NextRow(query)
135 }
136
137
138 if(count)
139 {
140 server_print("[%s] [OK] > %d access%s loaded!", sz_time, count, count > 1 ? "es" : "")
141 server_print("[%s] [OK] > SQL table ^"%s^" loaded successfully!", sz_time,COL_USER)
142 }
143 else
144 {
145 server_print("[%s] [ERROR] > There were no columns in SQL table ^"%s^"", sz_time,COL_USER)
146 server_print("[%s] [WARNING] > Reading some data from the table failed!", sz_time)
147 server_print("> Please check [ERROR] messages above for solving this problem!")
148 }
149
150 server_print(separator_1)
151 server_print(" ")
152
153 data_ready = true
154
155 for(new i = 1 ; i <= g_maxplayers ; i++)
156 {
157 if(!is_user_connecting(i) && !is_user_connected(i)) continue
158
159 CheckClient(i)
160 }
161 }
162 }
163}
164
165public client_authorized(id)
166{
167 if(!is_user_connected(id)||is_user_bot(id)||is_user_hltv(id)) return
168
169 clear_user(id)
170 remove_tasks(id)
171
172 if(data_ready) CheckClient(id)
173
174 /*new name[32],authid[65],data[1]
175 get_user_name(id,name,charsmax(name))
176 get_user_authid(id,authid,charsmax(authid))
177
178 if(!is_user_steam(id)) formatex(query, charsmax(query), "SELECT `%s` FROM `%s` WHERE %s = ^"%s^";",COL_ACC,TABEL,COL_USER,g_client_data[id])
179 else if(is_user_steam(id)) formatex(query, charsmax(query), "SELECT `%s` FROM `%s` WHERE %s = ^"%s^";",COL_ACC,TABEL,COL_USER,g_client_data[id])
180
181 data[0] = id
182
183 SQL_ThreadQuery(g_SqlTuple, "QuerySelectData", query, data, 1)*/
184}
185public CheckClient(id)
186{
187 if(is_user_bot(id) || is_user_hltv(id) || !data_ready) return PLUGIN_HANDLED
188
189 new name[32],authid[65]
190 get_user_name(id,name,charsmax(name))
191 get_user_authid(id,authid,charsmax(authid))
192
193 remove_tasks(id)
194 is_registered[id] = false
195 is_logged[id] = false
196
197 if(!is_user_steam(id)) formatex(query, charsmax(query), "SELECT `%s` FROM `%s` WHERE %s = ^"%s^";",COL_ACC,TABEL,COL_USER,name)
198 else if(is_user_steam(id)) formatex(query, charsmax(query), "SELECT `%s` FROM `%s` WHERE %s = ^"%s^";",COL_ACC,TABEL,COL_USER,authid)
199
200 new data[1]
201 data[0] = id
202
203 //formatex(query, charsmax(query), "SELECT `%s` FROM `%s` WHERE %s = ^"%s^";",COL_ACC,TABEL,COL_USER,g_client_data[id])
204 SQL_ThreadQuery(g_SqlTuple, "QuerySelectData", query, data, 1)
205
206 return PLUGIN_CONTINUE
207}
208public QuerySelectData(FailState, Handle:Query, error[], errorcode, data[], datasize, Float:fQueueTime)
209{
210 if(FailState == TQUERY_CONNECT_FAILED || FailState == TQUERY_QUERY_FAILED)
211 {
212 log_amx("%s", error)
213 return
214 }
215 else
216 {
217 new id = data[0];
218 new col_pass = SQL_FieldNameToNum(Query, COL_ACC)
219
220 while(SQL_MoreResults(Query))
221 {
222 SQL_ReadResult(Query, col_pass, check_pass, charsmax(check_pass))
223 is_registered[id] = true
224 password[id] = check_pass
225
226 if(is_user_connected(id))
227 {
228 //user_silentkill(id)
229 //cs_set_user_team(id, CS_TEAM_UNASSIGNED)
230 ShowMsg(id)
231 }
232
233 SQL_NextRow(Query)
234 }
235 }
236}
237public client_putinserver(id) if(data_ready) ShowMsg(id)
238public ShowMsg(id)
239{
240 remove_tasks(id)
241
242 set_task(5.0, "Messages", id+TASK_MESS)
243
244 params[0] = id
245
246 if(!is_logged[id])
247 {
248 if(!changing_name[id])
249 {
250 if(get_pcvar_num(g_count))
251 {
252 g_player_time[id] = get_pcvar_num(g_logtime)
253 ShowTimer(id+TASK_TIMER)
254 }
255 params[1] = 2
256 set_task(get_pcvar_float(g_logtime) + 3, "KickPlayer", id+TASK_KICK, params, sizeof params)
257 return PLUGIN_HANDLED
258 }
259 else
260 {
261 g_player_time[id] = -1
262 set_task(1.0, "ShowTimer", id+TASK_TIMER)
263 }
264 }
265 return PLUGIN_CONTINUE
266}
267public KickPlayer(parameters[])
268{
269 new id = parameters[0]
270 new reason = parameters[1]
271
272 if(!is_user_connecting(id) && !is_user_connected(id)) return PLUGIN_HANDLED
273
274 new userid = get_user_userid(id)
275
276 switch(reason)
277 {
278 case 1:
279 {
280 if(is_registered[id]) return PLUGIN_HANDLED
281
282 console_print(id, "^n* Cont invalid!")
283 server_cmd("kick #%i ^"You must register your name. Please, retry and register it.^"", userid)
284 }
285 case 2:
286 {
287 if(is_logged[id]) return PLUGIN_HANDLED
288
289 console_print(id, "^n* Cont invalid!")
290 server_cmd("kick #%i ^"This name is registered. Please, retry and login.^"", userid)
291 }
292 case 3:
293 {
294 console_print(id, "^n* Cont invalid!")
295 server_cmd("kick #%i ^"You have successfully Logged out! Your Auto Login function is currently disabled!^"", userid)
296 }
297 }
298 return PLUGIN_CONTINUE
299}
300public ShowTimer(id)
301{
302 id -= TASK_TIMER
303
304 if(!is_user_connected(id)) return PLUGIN_HANDLED
305
306 switch(g_player_time[id])
307 {
308 case 10..19: set_hudmessage(255, 255, 0, -1.0, -1.0, 0, 0.02, 1.0,_,_, -1)
309 case 0..9: set_hudmessage(255, 0, 0, -1.0, -1.0, 1, 0.02, 1.0,_,_, -1)
310 case -1: set_hudmessage(255, 255, 255, -1.0, -1.0, 1, 0.02, 1.0,_,_, -1)
311 default: set_hudmessage(0, 255, 0, -1.0, -1.0, 0, 0.02, 1.0,_,_, -1)
312 }
313
314 if(g_player_time[id] == 0)
315 {
316 ShowSyncHudMsg(id, g_sync_hud, "You will be kicked!")
317 return PLUGIN_CONTINUE
318 }
319 else if(!is_logged[id])
320 {
321 if(g_player_time[id] == -1)
322 {
323 ShowSyncHudMsg(id, g_sync_hud, "You will can login after the your name is changed!")
324 set_task(1.0, "ShowTimer", id+TASK_TIMER)
325 return PLUGIN_HANDLED
326 }
327
328 ShowSyncHudMsg(id, g_sync_hud, "You have %d second%s to Login!", g_player_time[id] > 1 ? "s" : "", g_player_time[id])
329 }
330 else return PLUGIN_HANDLED
331
332 g_player_time[id]--
333
334 set_task(1.0, "ShowTimer", id+TASK_TIMER)
335
336 return PLUGIN_CONTINUE
337}
338public Messages(id)
339{
340 id -= TASK_MESS
341
342 if(!is_logged[id]) client_printcolor(id, "!g%s!t You have!g %d!t seconds to login!", prefix, get_pcvar_num(g_logtime))
343}
344
345public ClientInfoChanged(id)
346{
347 if(!is_user_connected(id)) return FMRES_IGNORED
348
349 new oldname[32], newname[32];
350 get_user_name(id, oldname, charsmax(oldname))
351 get_user_info(id, "name", newname, charsmax(newname))
352
353 if(!equal(oldname, newname))
354 {
355 replace_all(newname, charsmax(newname), "%", " ")
356
357 changing_name[id] = false
358
359 if(!is_user_alive(id)) changing_name[id] = true
360 else
361 {
362 if(is_logged[id])
363 {
364 set_user_info(id, "name", oldname)
365 client_printcolor(id, "!g%s!t Sorry, but name change is not allowed for Logged players!", prefix)
366 return FMRES_HANDLED
367 }
368
369 set_task(1.0, "CheckClient", id)
370 }
371 }
372 return FMRES_IGNORED
373}
374
375public VERIFY_USER(id)
376{
377 new arg[32]
378 read_argv(1,arg,charsmax(arg))
379 if(!is_logged[id]&&is_registered[id])
380 {
381 if(!(equali(arg,password[id]))) server_cmd("kick #%d ^"Nu te-ai putut autentifica^"",get_user_userid(id))
382 else is_logged[id]=true
383 }
384}
385
386public client_disconnect(id)
387{
388 clear_user(id)
389 remove_tasks(id)
390}
391
392stock clear_user(const id)
393{
394 is_logged[id] = false
395 is_registered[id] = false
396 changing_name[id] = false
397}
398
399stock remove_tasks(const id)
400{
401 remove_task(id+TASK_MESS)
402 remove_task(id+TASK_KICK)
403 remove_task(id+TASK_TIMER)
404 remove_task(id)
405}
406
407stock client_printcolor(const id, const message[], any:...)
408{
409 new g_message[191];
410 new i = 1, players[32];
411
412 vformat(g_message, charsmax(g_message), message, 3)
413
414 replace_all(g_message, charsmax(g_message), "!g", "^4")
415 replace_all(g_message, charsmax(g_message), "!n", "^1")
416 replace_all(g_message, charsmax(g_message), "!t", "^3")
417
418 if(id) players[0] = id
419 else get_players(players, i, "ch")
420
421 for(new j = 0; j < i; j++)
422 {
423 if(is_user_connected(players[j]))
424 {
425 message_begin(MSG_ONE_UNRELIABLE, g_saytxt,_, players[j])
426 write_byte(players[j])
427 write_string(g_message)
428 message_end()
429 }
430 }
431}
432
433stock bool: is_user_steam(id)
434{
435 static dp_pointer
436 if(dp_pointer||(dp_pointer=get_cvar_pointer("dp_r_id_provider")))
437 {
438 server_cmd("dp_clientinfo %d",id)
439 server_exec()
440 return (get_pcvar_num(dp_pointer)==2) ? true : false
441 }
442 return false
443}