· 8 years ago · Mar 15, 2018, 08:28 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}
174public CheckClient(id)
175{
176 if(is_user_bot(id) || is_user_hltv(id) || !data_ready) return PLUGIN_HANDLED
177
178 //new name[32],authid[65]
179 //get_user_name(id,name,charsmax(name))
180 //get_user_authid(id,authid,charsmax(authid))
181
182 remove_tasks(id)
183 is_registered[id] = false
184 is_logged[id] = false
185
186 if(!is_user_steam(id))
187 {
188 get_user_name(id,g_client_data[id],charsmax(g_client_data))
189 //formatex(query, charsmax(query), "SELECT `%s` FROM `%s` WHERE %s = ^"%s^";",COL_ACC,TABEL,COL_USER,name)
190 }
191 else if(is_user_steam(id))
192 {
193 get_user_authid(id,g_client_data[id],charsmax(g_client_data))
194 //formatex(query, charsmax(query), "SELECT `%s` FROM `%s` WHERE %s = ^"%s^";",COL_ACC,TABEL,COL_USER,authid)
195 }
196
197 new data[1]
198 data[0] = id
199
200 formatex(query, charsmax(query), "SELECT `%s` FROM `%s` WHERE %s = ^"%s^";",COL_ACC,TABEL,COL_USER,g_client_data[id])
201 SQL_ThreadQuery(g_SqlTuple, "QuerySelectData", query, data, 1)
202
203 return PLUGIN_CONTINUE
204}
205public QuerySelectData(FailState, Handle:Query, error[], errorcode, data[], datasize, Float:fQueueTime)
206{
207 if(FailState == TQUERY_CONNECT_FAILED || FailState == TQUERY_QUERY_FAILED)
208 {
209 log_amx("%s", error)
210 return
211 }
212 else
213 {
214 new id = data[0];
215 new col_pass = SQL_FieldNameToNum(Query, COL_ACC)
216
217 while(SQL_MoreResults(Query))
218 {
219 SQL_ReadResult(Query, col_pass, check_pass, charsmax(check_pass))
220 is_registered[id] = true
221 password[id] = check_pass
222
223 if(is_user_connected(id))
224 {
225 //user_silentkill(id)
226 //cs_set_user_team(id, CS_TEAM_UNASSIGNED)
227 ShowMsg(id)
228 }
229
230 SQL_NextRow(Query)
231 }
232 }
233}
234public client_putinserver(id) if(data_ready) ShowMsg(id)
235public ShowMsg(id)
236{
237 remove_tasks(id)
238
239 set_task(5.0, "Messages", id+TASK_MESS)
240
241 params[0] = id
242
243 if(!is_logged[id])
244 {
245 if(!changing_name[id])
246 {
247 if(get_pcvar_num(g_count))
248 {
249 g_player_time[id] = get_pcvar_num(g_logtime)
250 ShowTimer(id+TASK_TIMER)
251 }
252 params[1] = 2
253 set_task(get_pcvar_float(g_logtime) + 3, "KickPlayer", id+TASK_KICK, params, sizeof params)
254 return PLUGIN_HANDLED
255 }
256 else
257 {
258 g_player_time[id] = -1
259 set_task(1.0, "ShowTimer", id+TASK_TIMER)
260 }
261 }
262 return PLUGIN_CONTINUE
263}
264public KickPlayer(parameters[])
265{
266 new id = parameters[0]
267 new reason = parameters[1]
268
269 if(!is_user_connecting(id) && !is_user_connected(id)) return PLUGIN_HANDLED
270
271 new userid = get_user_userid(id)
272
273 switch(reason)
274 {
275 case 1:
276 {
277 if(is_registered[id]) return PLUGIN_HANDLED
278
279 console_print(id, "^n* Cont invalid!")
280 server_cmd("kick #%i ^"You must register your name. Please, retry and register it.^"", userid)
281 }
282 case 2:
283 {
284 if(is_logged[id]) return PLUGIN_HANDLED
285
286 console_print(id, "^n* Cont invalid!")
287 server_cmd("kick #%i ^"This name is registered. Please, retry and login.^"", userid)
288 }
289 case 3:
290 {
291 console_print(id, "^n* Cont invalid!")
292 server_cmd("kick #%i ^"You have successfully Logged out! Your Auto Login function is currently disabled!^"", userid)
293 }
294 }
295 return PLUGIN_CONTINUE
296}
297public ShowTimer(id)
298{
299 id -= TASK_TIMER
300
301 if(!is_user_connected(id)) return PLUGIN_HANDLED
302
303 switch(g_player_time[id])
304 {
305 case 10..19: set_hudmessage(255, 255, 0, -1.0, -1.0, 0, 0.02, 1.0,_,_, -1)
306 case 0..9: set_hudmessage(255, 0, 0, -1.0, -1.0, 1, 0.02, 1.0,_,_, -1)
307 case -1: set_hudmessage(255, 255, 255, -1.0, -1.0, 1, 0.02, 1.0,_,_, -1)
308 default: set_hudmessage(0, 255, 0, -1.0, -1.0, 0, 0.02, 1.0,_,_, -1)
309 }
310
311 if(g_player_time[id] == 0)
312 {
313 ShowSyncHudMsg(id, g_sync_hud, "You will be kicked!")
314 return PLUGIN_CONTINUE
315 }
316 else if(!is_logged[id])
317 {
318 if(g_player_time[id] == -1)
319 {
320 ShowSyncHudMsg(id, g_sync_hud, "You will can login after the your name is changed!")
321 set_task(1.0, "ShowTimer", id+TASK_TIMER)
322 return PLUGIN_HANDLED
323 }
324
325 ShowSyncHudMsg(id, g_sync_hud, "You have %d second%s to Login!", g_player_time[id] > 1 ? "s" : "", g_player_time[id])
326 }
327 else return PLUGIN_HANDLED
328
329 g_player_time[id]--
330
331 set_task(1.0, "ShowTimer", id+TASK_TIMER)
332
333 return PLUGIN_CONTINUE
334}
335public Messages(id)
336{
337 id -= TASK_MESS
338
339 if(!is_logged[id]) client_printcolor(id, "!g%s!t You have!g %d!t seconds to login!", prefix, get_pcvar_num(g_logtime))
340}
341
342public ClientInfoChanged(id)
343{
344 if(!is_user_connected(id)) return FMRES_IGNORED
345
346 new oldname[32], newname[32];
347 get_user_name(id, oldname, charsmax(oldname))
348 get_user_info(id, "name", newname, charsmax(newname))
349
350 if(!equal(oldname, newname))
351 {
352 replace_all(newname, charsmax(newname), "%", " ")
353
354 changing_name[id] = false
355
356 if(!is_user_alive(id)) changing_name[id] = true
357 else
358 {
359 if(is_logged[id])
360 {
361 set_user_info(id, "name", oldname)
362 client_printcolor(id, "!g%s!t Sorry, but name change is not allowed for Logged players!", prefix)
363 return FMRES_HANDLED
364 }
365
366 set_task(1.0, "CheckClient", id)
367 }
368 }
369 return FMRES_IGNORED
370}
371
372public VERIFY_USER(id)
373{
374 new arg[32]
375 read_argv(1,arg,charsmax(arg))
376 if(!is_logged[id]&&is_registered[id])
377 {
378 if(!(equali(arg,password[id]))) server_cmd("kick #%d ^"Nu te-ai putut autentifica^"",get_user_userid(id))
379 else is_logged[id]=true
380 }
381}
382
383public client_disconnect(id)
384{
385 clear_user(id)
386 remove_tasks(id)
387}
388
389stock clear_user(const id)
390{
391 is_logged[id] = false
392 is_registered[id] = false
393 changing_name[id] = false
394}
395
396stock remove_tasks(const id)
397{
398 remove_task(id+TASK_MESS)
399 remove_task(id+TASK_KICK)
400 remove_task(id+TASK_TIMER)
401 remove_task(id)
402}
403
404stock client_printcolor(const id, const message[], any:...)
405{
406 new g_message[191];
407 new i = 1, players[32];
408
409 vformat(g_message, charsmax(g_message), message, 3)
410
411 replace_all(g_message, charsmax(g_message), "!g", "^4")
412 replace_all(g_message, charsmax(g_message), "!n", "^1")
413 replace_all(g_message, charsmax(g_message), "!t", "^3")
414
415 if(id) players[0] = id
416 else get_players(players, i, "ch")
417
418 for(new j = 0; j < i; j++)
419 {
420 if(is_user_connected(players[j]))
421 {
422 message_begin(MSG_ONE_UNRELIABLE, g_saytxt,_, players[j])
423 write_byte(players[j])
424 write_string(g_message)
425 message_end()
426 }
427 }
428}
429
430stock bool: is_user_steam(id)
431{
432 static dp_pointer
433 if(dp_pointer||(dp_pointer=get_cvar_pointer("dp_r_id_provider")))
434 {
435 server_cmd("dp_clientinfo %d",id)
436 server_exec()
437 return (get_pcvar_num(dp_pointer)==2) ? true : false
438 }
439 return false
440}