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