· 5 years ago · Feb 24, 2020, 10:14 AM
1/*
2 SourcePawn is Copyright (C) 2006-2008 AlliedModders LLC. All rights reserved.
3 SourceMod is Copyright (C) 2006-2008 AlliedModders LLC. All rights reserved.
4 Pawn and SMALL are Copyright (C) 1997-2008 ITB CompuPhase.
5 Source is Copyright (C) Valve Corporation.
6 All trademarks are property of their respective owners.
7 This program is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation, either version 3 of the License, or (at your
10 option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#include <sourcemod>
20#include <multicolors>
21
22#pragma semicolon 1
23#pragma newdecls required
24
25enum Arg_Types
26{
27 Arg_Steam2ID,
28 Arg_Steam3ID,
29 Arg_Steam32ID,
30 Arg_Steam64ID,
31 Arg_Name,
32 Arg_IP,
33}
34
35Database g_hDatabase = null;
36
37public Plugin myinfo = {
38 name = "Player Info Recorder",
39 author = "Mis",
40 description = "Stores info in a database about each player that connects to the server.",
41 version = "0.1.0",
42 url = "https://github.com/misdocumeno/"
43};
44
45// Stock Functions
46
47public void OnPluginStart()
48{
49 // Info commands
50
51 RegAdminCmd("sm_playerinfo", OnPlayerInfoCMD, ADMFLAG_UNBAN, "Shows last name, all types of SteamID, and last seen IP of a given SteamID of any type, IP or name.");
52
53 RegAdminCmd("sm_nameinfo", OnNameInfoCMD, ADMFLAG_UNBAN, "Shows last name, all types of SteamID, and last seen IP explicitly searching with a nickname.");
54 RegAdminCmd("sm_steamid2info", OnSteamId2InfoCMD, ADMFLAG_UNBAN, "Shows last name, all types of SteamID, and last seen IP explicitly searching with a SteamID2.");
55 RegAdminCmd("sm_steamid3info", OnSteamId3InfoCMD, ADMFLAG_UNBAN, "Shows last name, all types of SteamID, and last seen IP explicitly searching with a SteamID3.");
56 RegAdminCmd("sm_steamid64info", OnSteamId64InfoCMD, ADMFLAG_UNBAN, "Shows last name, all types of SteamID, and last seen IP explicitly searching with a SteamID64.");
57 RegAdminCmd("sm_ipinfo", OnIpInfoCMD, ADMFLAG_UNBAN, "Shows last name, all types of SteamID, and last seen IP explicitly searching with an IP.");
58
59 // History commands
60
61 RegAdminCmd("sm_playerall", OnPlayerAll, ADMFLAG_UNBAN, "Shows a history of all names, all SteamID2, and all IPs of a given SteamID of any type, IP or name.");
62
63 RegAdminCmd("sm_namenames", OnNameNamesCMD, ADMFLAG_UNBAN, "Shows a history of all names explicitly searching with a nickname.");
64 RegAdminCmd("sm_steamid2names", OnSteamId2NamesCMD, ADMFLAG_UNBAN, "Shows a history of all names explicitly searching with a SteamID2.");
65 RegAdminCmd("sm_steamid3names", OnSteamId3NamesCMD, ADMFLAG_UNBAN, "Shows a history of all names explicitly searching with a SteamID3.");
66 RegAdminCmd("sm_steamid64names", OnSteamId64NamesCMD, ADMFLAG_UNBAN, "Shows a history of all names explicitly searching with a SteamID64.");
67 RegAdminCmd("sm_nameips", OnNameIpsCMD, ADMFLAG_UNBAN, "Shows a history of all IPs explicitly searching with a nickname.");
68 RegAdminCmd("sm_steamid2ips", OnSteamId2IpsCMD, ADMFLAG_UNBAN, "Shows a history of all IPs explicitly searching with SteamID2.");
69 RegAdminCmd("sm_steamid3ips", OnSteamId3IpsCMD, ADMFLAG_UNBAN, "Shows a history of all IPs explicitly searching with SteamID3.");
70 RegAdminCmd("sm_steamid64ips", OnSteamId64IpsCMD, ADMFLAG_UNBAN, "Shows a history of all IPs explicitly searching with SteamID64.");
71 RegAdminCmd("sm_ipsteamid", OnIpSteamIds, ADMFLAG_UNBAN, "Shows a history of all types of SteamID seen on an IP.");
72
73
74 HookEvent("player_changename", OnPlayerChangeName_Event, EventHookMode_Post);
75
76 Database.Connect(SQL_FirstConnect, "PlayerRecord");
77}
78
79public void SQL_FirstConnect(Database db, const char[] error, any data)
80{
81 if (db == null || error[0] != '\0')
82 {
83 LogError("Database connect failure: %s", error);
84 return;
85 }
86
87 g_hDatabase = db;
88
89 g_hDatabase.SetCharset("utf8mb4");
90
91 g_hDatabase.Query(SQL_CreateAndInsertQuery, "CREATE TABLE IF NOT EXISTS `Players`("
92 ..."`Date` TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, "
93 ..."`AccountID` INT(1) NOT NULL, "
94 ..."`IP` varchar(16) NOT NULL, "
95 ..."`Name` varchar(256) NOT NULL, "
96 ..."UNIQUE INDEX `authId_IP_Name`(`AccountID`, `IP`, `Name`)"
97 ...") COLLATE 'utf8mb4_unicode_ci' ENGINE = InnoDB ROW_FORMAT = COMPRESSED;");
98}
99
100public void OnClientAuthorized(int client)
101{
102 char sIP[16];
103 char sUserName[MAX_NAME_LENGTH];
104 int iAccountId = GetSteamAccountID(client);
105 bool bValidIP = GetClientIP(client, sIP, sizeof(sIP));
106 bool bValidName = GetClientName(client, sUserName, sizeof(sUserName));
107
108 if (iAccountId != 0 && bValidIP && bValidName)
109 {
110 char query[256];
111
112 g_hDatabase.Format(query, sizeof(query), "INSERT INTO "
113 ..."`Players`(`AccountID`, `IP`, `Name`) "
114 ..."VALUES ('%i', '%s', '%s') ON DUPLICATE KEY UPDATE "
115 ..."`Date` = CURRENT_TIMESTAMP", iAccountId, sIP, sUserName);
116
117 g_hDatabase.Query(SQL_CreateAndInsertQuery, query);
118 }
119}
120
121public Action OnPlayerChangeName_Event(Event event, const char[] name, bool dontBroadcast)
122{
123 int userid = event.GetInt("userid");
124 int client = GetClientOfUserId(userid);
125
126 char sIP[16];
127 char sUserName[MAX_NAME_LENGTH];
128 int iAccountId = GetSteamAccountID(client);
129 bool bValidIP = GetClientIP(client, sIP, sizeof(sIP));
130 event.GetString("newname", sUserName, sizeof(sUserName));
131
132 if (iAccountId != 0 && bValidIP)
133 {
134 char query[256];
135
136 g_hDatabase.Format(query, sizeof(query), "INSERT INTO "
137 ..."`Players`(`AccountID`, `IP`, `Name`) "
138 ..."VALUES ('%i', '%s', '%s') ON DUPLICATE KEY UPDATE "
139 ..."`Date` = CURRENT_TIMESTAMP", iAccountId, sIP, sUserName);
140
141 g_hDatabase.Query(SQL_CreateAndInsertQuery, query);
142 }
143}
144
145// Commands Functions
146
147static Action OnPlayerInfoCMD(int client, any args)
148{
149 if (!client)
150 return;
151
152 int iAccountId;
153
154 char arg[256];
155 GetCmdArg(1, arg, sizeof(arg));
156
157 if (GetArgType(arg) == Arg_Steam2ID)
158 iAccountId = GetAccountIdByAny(client, arg, Arg_Steam2ID);
159 else if (GetArgType(arg) == Arg_Steam3ID)
160 iAccountId = GetAccountIdByAny(client, arg, Arg_Steam3ID);
161 else if (GetArgType(arg) == Arg_Steam32ID)
162 iAccountId = GetAccountIdByAny(client, arg, Arg_Steam32ID);
163 else if (GetArgType(arg) == Arg_Steam64ID)
164 iAccountId = GetAccountIdByAny(client, arg, Arg_Steam64ID);
165 else if (GetArgType(arg) == Arg_Name)
166 iAccountId = GetAccountIdByAny(client, arg, Arg_Steam64ID);
167
168 char query[256];
169
170 if (GetArgType(arg) == Arg_Name)
171 g_hDatabase.Format(query, sizeof(query), "SELECT Name, IP, AccountID FROM Players WHERE Name LIKE '%s' ORDER BY `Players`.`Date` DESC LIMIT 1", arg);
172 else if (GetArgType(arg) == Arg_IP)
173 g_hDatabase.Format(query, sizeof(query), "SELECT Name, IP, AccountID FROM Players WHERE IP = '%s' ORDER BY `Players`.`Date` DESC LIMIT 1", arg);
174 else
175 g_hDatabase.Format(query, sizeof(query), "SELECT Name, IP, AccountID FROM Players WHERE AccountId = '%i' ORDER BY `Players`.`Date` DESC LIMIT 1", iAccountId);
176
177 int userid = GetClientUserId(client);
178
179 DataPack hPlayerInfoPack1 = new DataPack();
180 hPlayerInfoPack1.WriteString(arg);
181 hPlayerInfoPack1.WriteCell(userid);
182 hPlayerInfoPack1.WriteCell(GetArgType(arg));
183
184 g_hDatabase.Query(OnInfoQuery, query, hPlayerInfoPack1);
185}
186
187static Action OnNameInfoCMD(int client, any args)
188{
189 if (!client)
190 return;
191
192 char arg[256];
193 GetCmdArg(1, arg, sizeof(arg));
194
195 char query[256];
196 g_hDatabase.Format(query, sizeof(query), "SELECT Name, IP, AccountID FROM Players WHERE Name LIKE '%s' ORDER BY `Players`.`Date` DESC LIMIT 1", arg);
197
198 int userid = GetClientUserId(client);
199
200 DataPack hPlayerInfoPack1 = new DataPack();
201 hPlayerInfoPack1.WriteString(arg);
202 hPlayerInfoPack1.WriteCell(userid);
203 hPlayerInfoPack1.WriteCell(Arg_Name);
204
205 g_hDatabase.Query(OnInfoQuery, query, hPlayerInfoPack1);
206}
207
208static Action OnSteamId2InfoCMD(int client, any args)
209{
210 if (!client)
211 return;
212
213 int iAccountId;
214
215 char arg[256];
216 GetCmdArg(1, arg, sizeof(arg));
217
218 iAccountId = GetAccountIdByAny(client, arg, Arg_Steam2ID);
219
220 char query[256];
221 g_hDatabase.Format(query, sizeof(query), "SELECT Name, IP, AccountID FROM Players WHERE AccountId = '%s' ORDER BY `Players`.`Date` DESC LIMIT 1", iAccountId);
222
223 int userid = GetClientUserId(client);
224
225 DataPack hPlayerInfoPack1 = new DataPack();
226 hPlayerInfoPack1.WriteString(arg);
227 hPlayerInfoPack1.WriteCell(userid);
228 hPlayerInfoPack1.WriteCell(Arg_Steam2ID);
229
230 g_hDatabase.Query(OnInfoQuery, query, hPlayerInfoPack1);
231}
232
233static Action OnSteamId3InfoCMD(int client, any args)
234{
235 if (!client)
236 return;
237
238 int iAccountId;
239
240 char arg[256];
241 GetCmdArg(1, arg, sizeof(arg));
242
243 iAccountId = GetAccountIdByAny(client, arg, Arg_Steam3ID);
244
245 char query[256];
246 g_hDatabase.Format(query, sizeof(query), "SELECT Name, IP, AccountID FROM Players WHERE AccountId = '%s' ORDER BY `Players`.`Date` DESC LIMIT 1", iAccountId);
247
248 int userid = GetClientUserId(client);
249
250 DataPack hPlayerInfoPack1 = new DataPack();
251 hPlayerInfoPack1.WriteString(arg);
252 hPlayerInfoPack1.WriteCell(userid);
253 hPlayerInfoPack1.WriteCell(Arg_Steam3ID);
254
255 g_hDatabase.Query(OnInfoQuery, query, hPlayerInfoPack1);
256}
257
258static Action OnSteamId64InfoCMD(int client, any args)
259{
260 if (!client)
261 return;
262
263 int iAccountId;
264
265 char arg[256];
266 GetCmdArg(1, arg, sizeof(arg));
267
268 iAccountId = GetAccountIdByAny(client, arg, Arg_Steam64ID);
269
270 char query[256];
271 g_hDatabase.Format(query, sizeof(query), "SELECT Name, IP, AccountID FROM Players WHERE AccountId = '%s' ORDER BY `Players`.`Date` DESC LIMIT 1", iAccountId);
272
273 int userid = GetClientUserId(client);
274
275 DataPack hPlayerInfoPack1 = new DataPack();
276 hPlayerInfoPack1.WriteString(arg);
277 hPlayerInfoPack1.WriteCell(userid);
278 hPlayerInfoPack1.WriteCell(Arg_Steam64ID);
279
280 g_hDatabase.Query(OnInfoQuery, query, hPlayerInfoPack1);
281}
282
283static Action OnIpInfoCMD(int client, any args)
284{
285 if (!client)
286 return;
287
288 char arg[256];
289 GetCmdArg(1, arg, sizeof(arg));
290
291 char query[256];
292 g_hDatabase.Format(query, sizeof(query), "SELECT Name, IP, AccountID FROM Players WHERE IP = '%s' ORDER BY `Players`.`Date` DESC LIMIT 1", arg);
293
294 int userid = GetClientUserId(client);
295
296 DataPack hPlayerInfoPack1 = new DataPack();
297 hPlayerInfoPack1.WriteString(arg);
298 hPlayerInfoPack1.WriteCell(userid);
299 hPlayerInfoPack1.WriteCell(Arg_IP);
300
301 g_hDatabase.Query(OnInfoQuery, query, hPlayerInfoPack1);
302}
303
304
305static Action OnPlayerAll(int client, any args)
306{
307 if (!client)
308 return;
309
310 int iAccountId;
311
312 char arg[256];
313 GetCmdArg(1, arg, sizeof(arg));
314
315 if (GetArgType(arg) == Arg_Steam2ID)
316 iAccountId = GetAccountIdByAny(client, arg, Arg_Steam2ID);
317 else if (GetArgType(arg) == Arg_Steam3ID)
318 iAccountId = GetAccountIdByAny(client, arg, Arg_Steam3ID);
319 else if (GetArgType(arg) == Arg_Steam32ID)
320 iAccountId = GetAccountIdByAny(client, arg, Arg_Steam32ID);
321 else if (GetArgType(arg) == Arg_Steam64ID)
322 iAccountId = GetAccountIdByAny(client, arg, Arg_Steam64ID);
323 else if (GetArgType(arg) == Arg_Name)
324 iAccountId = GetAccountIdByAny(client, arg, Arg_Steam64ID);
325
326 char query[256];
327
328 if (GetArgType(arg) == Arg_Name)
329 g_hDatabase.Format(query, sizeof(query), "SELECT Name, IP, AccountID FROM Players WHERE Name LIKE '%s' ORDER BY `Players`.`Date` DESC", arg);
330 else if (GetArgType(arg) == Arg_IP)
331 g_hDatabase.Format(query, sizeof(query), "SELECT Name, IP, AccountID FROM Players WHERE IP = '%s' ORDER BY `Players`.`Date` DESC", arg);
332 else
333 g_hDatabase.Format(query, sizeof(query), "SELECT Name, IP, AccountID FROM Players WHERE AccountId = '%i' ORDER BY `Players`.`Date` DESC", iAccountId);
334
335 int userid = GetClientUserId(client);
336
337 DataPack hHistoryAllPack = new DataPack();
338 hHistoryAllPack.WriteString(arg);
339 hHistoryAllPack.WriteCell(userid);
340
341 g_hDatabase.Query(OnHistoryAllQuery, query, hHistoryAllPack);
342}
343
344static Action OnNameNamesCMD(int client, any args)
345{
346 if (!client)
347 return;
348
349 char arg[256];
350 GetCmdArg(1, arg, sizeof(arg));
351
352 char query[256];
353 g_hDatabase.Format(query, sizeof(query), "SELECT Name FROM Players WHERE Name LIKE '%s' ORDER BY `Players`.`Date` DESC", arg);
354
355 int userid = GetClientUserId(client);
356
357 DataPack hNameOrIpPack = new DataPack();
358 hNameOrIpPack.WriteCell(userid);
359
360 g_hDatabase.Query(OnHistoryQuery, query, hNameOrIpPack);
361}
362
363static Action OnSteamId2NamesCMD(int client, any args)
364{
365 if (!client)
366 return;
367
368 int iAccountId;
369
370 char arg[256];
371 GetCmdArg(1, arg, sizeof(arg));
372
373 iAccountId = GetAccountIdByAny(client, arg, Arg_Steam2ID);
374
375 char query[256];
376 g_hDatabase.Format(query, sizeof(query), "SELECT Name FROM Players WHERE AccountID = '%i' ORDER BY `Players`.`Date` DESC", iAccountId);
377
378 int userid = GetClientUserId(client);
379
380 DataPack hNameOrIpPack = new DataPack();
381 hNameOrIpPack.WriteCell(userid);
382
383 g_hDatabase.Query(OnHistoryQuery, query, hNameOrIpPack);
384}
385
386static Action OnSteamId3NamesCMD(int client, any args)
387{
388 if (!client)
389 return;
390
391 int iAccountId;
392
393 char arg[256];
394 GetCmdArg(1, arg, sizeof(arg));
395
396 iAccountId = GetAccountIdByAny(client, arg, Arg_Steam3ID);
397
398 char query[256];
399 g_hDatabase.Format(query, sizeof(query), "SELECT Name FROM Players WHERE AccountID = '%i' ORDER BY `Players`.`Date` DESC", iAccountId);
400
401 int userid = GetClientUserId(client);
402
403 DataPack hNameOrIpPack = new DataPack();
404 hNameOrIpPack.WriteCell(userid);
405
406 g_hDatabase.Query(OnHistoryQuery, query, hNameOrIpPack);
407}
408
409static Action OnSteamId64NamesCMD(int client, any args)
410{
411 if (!client)
412 return;
413
414 int iAccountId;
415
416 char arg[256];
417 GetCmdArg(1, arg, sizeof(arg));
418
419 iAccountId = GetAccountIdByAny(client, arg, Arg_Steam64ID);
420
421 char query[256];
422 g_hDatabase.Format(query, sizeof(query), "SELECT Name FROM Players WHERE AccountID = '%i' ORDER BY `Players`.`Date` DESC", iAccountId);
423
424 int userid = GetClientUserId(client);
425
426 DataPack hNameOrIpPack = new DataPack();
427 hNameOrIpPack.WriteCell(userid);
428
429 g_hDatabase.Query(OnHistoryQuery, query, hNameOrIpPack);
430}
431
432static Action OnNameIpsCMD(int client, any args)
433{
434 if (!client)
435 return;
436
437 char arg[256];
438 GetCmdArg(1, arg, sizeof(arg));
439
440 char query[256];
441 g_hDatabase.Format(query, sizeof(query), "SELECT IP FROM Players WHERE Name LIKE '%s' ORDER BY `Players`.`Date` DESC", arg);
442
443 int userid = GetClientUserId(client);
444
445 DataPack hNameOrIpPack = new DataPack();
446 hNameOrIpPack.WriteCell(userid);
447
448 g_hDatabase.Query(OnHistoryQuery, query, hNameOrIpPack);
449}
450
451static Action OnSteamId2IpsCMD(int client, any args)
452{
453 if (!client)
454 return;
455
456 int iAccountId;
457
458 char arg[256];
459 GetCmdArg(1, arg, sizeof(arg));
460
461 iAccountId = GetAccountIdByAny(client, arg, Arg_Steam2ID);
462
463 char query[256];
464 g_hDatabase.Format(query, sizeof(query), "SELECT IP FROM Players WHERE AccountID = '%i' ORDER BY `Players`.`Date` DESC", iAccountId);
465
466 int userid = GetClientUserId(client);
467
468 DataPack hNameOrIpPack = new DataPack();
469 hNameOrIpPack.WriteCell(userid);
470
471 g_hDatabase.Query(OnHistoryQuery, query, hNameOrIpPack);
472}
473
474static Action OnSteamId3IpsCMD(int client, any args)
475{
476 if (!client)
477 return;
478
479 int iAccountId;
480
481 char arg[256];
482 GetCmdArg(1, arg, sizeof(arg));
483
484 iAccountId = GetAccountIdByAny(client, arg, Arg_Steam3ID);
485
486 char query[256];
487 g_hDatabase.Format(query, sizeof(query), "SELECT IP FROM Players WHERE AccountID = '%i' ORDER BY `Players`.`Date` DESC", iAccountId);
488
489 int userid = GetClientUserId(client);
490
491 DataPack hNameOrIpPack = new DataPack();
492 hNameOrIpPack.WriteCell(userid);
493
494 g_hDatabase.Query(OnHistoryQuery, query, hNameOrIpPack);
495}
496
497static Action OnSteamId64IpsCMD(int client, any args)
498{
499 if (!client)
500 return;
501
502 int iAccountId;
503
504 char arg[256];
505 GetCmdArg(1, arg, sizeof(arg));
506
507 iAccountId = GetAccountIdByAny(client, arg, Arg_Steam64ID);
508
509 char query[256];
510 g_hDatabase.Format(query, sizeof(query), "SELECT IP FROM Players WHERE AccountID = '%i' ORDER BY `Players`.`Date` DESC", iAccountId);
511
512 int userid = GetClientUserId(client);
513
514 DataPack hNameOrIpPack = new DataPack();
515 hNameOrIpPack.WriteCell(userid);
516
517 g_hDatabase.Query(OnHistoryQuery, query, hNameOrIpPack);
518}
519
520static Action OnIpSteamIds(int client, any args)
521{
522 if (!client)
523 return;
524
525 char arg[256];
526 GetCmdArg(1, arg, sizeof(arg));
527
528 char query[256];
529 g_hDatabase.Format(query, sizeof(query), "SELECT AccountId FROM Players WHERE IP = '%s' ORDER BY `Players`.`Date` DESC", arg);
530
531 int userid = GetClientUserId(client);
532
533 DataPack hSteamIdPack = new DataPack();
534 hNameOrIpPack.WriteCell(userid);
535
536 g_hDatabase.Query(OnHistoryIpQuery, query, hSteamIdPack);
537}
538
539// SQL Query Functions
540
541public void OnInfoQuery(Database db, DBResultSet results, const char[] sError, DataPack hPlayerInfoPack1)
542{
543 char arg[256];
544 hPlayerInfoPack1.Reset();
545 hPlayerInfoPack1.ReadString(arg, sizeof(arg));
546 int userid = hPlayerInfoPack1.ReadCell();
547 int iArgType = hPlayerInfoPack1.ReadCell();
548 delete hPlayerInfoPack1;
549
550 int client = GetClientOfUserId(userid);
551
552 if(db == null || results == null || sError[0] != '\0')
553 {
554 LogError("Database query failure: %s", sError);
555 return;
556 }
557
558 if (!client)
559 return;
560
561 char sName[MAX_NAME_LENGTH];
562 char sIP[16];
563 char sAccountId[32];
564
565 while (results.FetchRow())
566 {
567 results.FetchString(0, sName, sizeof(sName));
568 results.FetchString(1, sIP, sizeof(sIP));
569 results.FetchString(2, sAccountId, sizeof(sAccountId));
570 }
571
572 if (results.RowCount != 0)
573 {
574 int iAccountId = StringToInt(sAccountId);
575
576 char sSteamId2[32];
577 char sSteamId3[32];
578 char sSteamId64[64];
579
580 GetSteamId2ByAccountId(iAccountId, sSteamId2, sizeof(sSteamId2));
581 GetSteamId3ByAccountId(iAccountId, sSteamId3, sizeof(sSteamId3));
582 GetSteamId64ByAccountId(iAccountId, sSteamId64, sizeof(sSteamId64));
583
584 CPrintToChat(client, "[{green}!{default}]{blue}========================={default}[{green}!{default}]");
585 CPrintToChat(client, "{default}Last Name {green}- {olive}%s", sName);
586 CPrintToChat(client, "{default}SteamID2 {green}- {olive}%s", sSteamId2);
587 CPrintToChat(client, "{default}SteamID3 {green}- {olive}%s", sSteamId3);
588 CPrintToChat(client, "{default}SteamID64 {green}- {olive}%s", sSteamId64);
589 CPrintToChat(client, "{default}Last IP {green}- {olive}%s", sIP);
590 CPrintToChat(client, "[{green}!{default}]{blue}========================={default}[{green}!{default}]");
591 }
592 else
593 {
594 if (iArgType == Arg_Name)
595 {
596 char query[256];
597 g_hDatabase.Format(query, sizeof(query), "SELECT Name, IP, AccountID FROM Players WHERE Name LIKE '%%%s' ORDER BY `Players`.`Date` DESC LIMIT 1", arg);
598
599 DataPack hPlayerInfoPack2 = new DataPack();
600 hPlayerInfoPack2.WriteString(arg);
601 hPlayerInfoPack2.WriteCell(userid);
602
603 g_hDatabase.Query(OnInfoQueryRetry1, query, hPlayerInfoPack2);
604 }
605 else
606 CPrintToChat(client, "[{green}!{default}] {red}No player found");
607 }
608}
609
610public void OnInfoQueryRetry1(Database db, DBResultSet results, const char[] sError, DataPack hPlayerInfoPack2)
611{
612 char arg[256];
613 hPlayerInfoPack2.Reset();
614 hPlayerInfoPack2.ReadString(arg, sizeof(arg));
615 int userid = hPlayerInfoPack2.ReadCell();
616 delete hPlayerInfoPack2;
617
618 int client = GetClientOfUserId(userid);
619
620 if(db == null || results == null || sError[0] != '\0')
621 {
622 LogError("Database query failure: %s", sError);
623 return;
624 }
625
626 char sName[MAX_NAME_LENGTH];
627 char sIP[16];
628 char sAccountId[32];
629
630 while (results.FetchRow())
631 {
632 results.FetchString(0, sName, sizeof(sName));
633 results.FetchString(1, sIP, sizeof(sIP));
634 results.FetchString(2, sAccountId, sizeof(sAccountId));
635 }
636
637 if (results.RowCount != 0)
638 {
639 int iAccountId = StringToInt(sAccountId);
640
641 char sSteamId2[32];
642 char sSteamId3[32];
643 char sSteamId64[64];
644
645 GetSteamId2ByAccountId(iAccountId, sSteamId2, sizeof(sSteamId2));
646 GetSteamId3ByAccountId(iAccountId, sSteamId3, sizeof(sSteamId3));
647 GetSteamId64ByAccountId(iAccountId, sSteamId64, sizeof(sSteamId64));
648
649 CPrintToChat(client, "[{green}!{default}]{blue}========================={default}[{green}!{default}]");
650 CPrintToChat(client, "{default}Last Name {green}- {olive}%s", sName);
651 CPrintToChat(client, "{default}SteamID2 {green}- {olive}%s", sSteamId2);
652 CPrintToChat(client, "{default}SteamID3 {green}- {olive}%s", sSteamId3);
653 CPrintToChat(client, "{default}SteamID64 {green}- {olive}%s", sSteamId64);
654 CPrintToChat(client, "{default}Last IP {green}- {olive}%s", sIP);
655 CPrintToChat(client, "[{green}!{default}]{blue}========================={default}[{green}!{default}]");
656 }
657 else
658 {
659 // add % at the end
660 char query[256];
661 g_hDatabase.Format(query, sizeof(query), "SELECT Name, IP, AccountID FROM Players WHERE Name LIKE '%s%' ORDER BY `Players`.`Date` DESC LIMIT 1", arg);
662
663 DataPack hPlayerInfoPack3 = new DataPack();
664 hPlayerInfoPack3.WriteString(arg);
665 hPlayerInfoPack3.WriteCell(userid);
666
667 g_hDatabase.Query(OnInfoQueryRetry2, query, hPlayerInfoPack3);
668 }
669}
670
671public void OnInfoQueryRetry2(Database db, DBResultSet results, const char[] sError, DataPack hPlayerInfoPack3)
672{
673 char arg[256];
674 hPlayerInfoPack3.Reset();
675 hPlayerInfoPack3.ReadString(arg, sizeof(arg));
676 int userid = hPlayerInfoPack3.ReadCell();
677 delete hPlayerInfoPack3;
678
679 int client = GetClientOfUserId(userid);
680
681 if(db == null || results == null || sError[0] != '\0')
682 {
683 LogError("Database query failure: %s", sError);
684 return;
685 }
686
687 if(db == null || results == null || sError[0] != '\0')
688 {
689 LogError("Database query failure: %s", sError);
690 return;
691 }
692
693 char sName[MAX_NAME_LENGTH];
694 char sIP[16];
695 char sAccountId[32];
696
697 while (results.FetchRow())
698 {
699 results.FetchString(0, sName, sizeof(sName));
700 results.FetchString(1, sIP, sizeof(sIP));
701 results.FetchString(2, sAccountId, sizeof(sAccountId));
702 }
703
704 if (results.RowCount != 0)
705 {
706 int iAccountId = StringToInt(sAccountId);
707
708 char sSteamId2[32];
709 char sSteamId3[32];
710 char sSteamId64[64];
711
712 GetSteamId2ByAccountId(iAccountId, sSteamId2, sizeof(sSteamId2));
713 GetSteamId3ByAccountId(iAccountId, sSteamId3, sizeof(sSteamId3));
714 GetSteamId64ByAccountId(iAccountId, sSteamId64, sizeof(sSteamId64));
715
716 CPrintToChat(client, "[{green}!{default}]{blue}========================={default}[{green}!{default}]");
717 CPrintToChat(client, "{default}Last Name {green}- {olive}%s", sName);
718 CPrintToChat(client, "{default}SteamID2 {green}- {olive}%s", sSteamId2);
719 CPrintToChat(client, "{default}SteamID3 {green}- {olive}%s", sSteamId3);
720 CPrintToChat(client, "{default}SteamID64 {green}- {olive}%s", sSteamId64);
721 CPrintToChat(client, "{default}Last IP {green}- {olive}%s", sIP);
722 CPrintToChat(client, "[{green}!{default}]{blue}========================={default}[{green}!{default}]");
723 }
724 else
725 {
726 // add % at the end and start
727 char query[256];
728 g_hDatabase.Format(query, sizeof(query), "SELECT Name, IP, AccountID FROM Players WHERE Name LIKE '%%%s%%' ORDER BY `Players`.`Date` DESC LIMIT 1", arg);
729
730 DataPack hPlayerInfoPack4 = new DataPack();
731 hPlayerInfoPack4.WriteString(arg);
732 hPlayerInfoPack4.WriteCell(userid);
733
734 g_hDatabase.Query(OnInfoQueryRetry3, query, hPlayerInfoPack4);
735 }
736}
737
738public void OnInfoQueryRetry3(Database db, DBResultSet results, const char[] sError, DataPack hPlayerInfoPack4)
739{
740 char arg[256];
741 hPlayerInfoPack4.Reset();
742 hPlayerInfoPack4.ReadString(arg, sizeof(arg));
743 int userid = hPlayerInfoPack4.ReadCell();
744 delete hPlayerInfoPack4;
745
746 int client = GetClientOfUserId(userid);
747
748 if(db == null || results == null || sError[0] != '\0')
749 {
750 LogError("Database query failure: %s", sError);
751 return;
752 }
753
754 if(db == null || results == null || sError[0] != '\0')
755 {
756 LogError("Database query failure: %s", sError);
757 return;
758 }
759
760 char sName[MAX_NAME_LENGTH];
761 char sIP[16];
762 char sAccountId[32];
763
764 while (results.FetchRow())
765 {
766 results.FetchString(0, sName, sizeof(sName));
767 results.FetchString(1, sIP, sizeof(sIP));
768 results.FetchString(2, sAccountId, sizeof(sAccountId));
769 }
770
771 if (results.RowCount != 0)
772 {
773 int iAccountId = StringToInt(sAccountId);
774
775 char sSteamId2[32];
776 char sSteamId3[32];
777 char sSteamId64[64];
778
779 GetSteamId2ByAccountId(iAccountId, sSteamId2, sizeof(sSteamId2));
780 GetSteamId3ByAccountId(iAccountId, sSteamId3, sizeof(sSteamId3));
781 GetSteamId64ByAccountId(iAccountId, sSteamId64, sizeof(sSteamId64));
782
783 CPrintToChat(client, "[{green}!{default}]{blue}========================={default}[{green}!{default}]");
784 CPrintToChat(client, "{default}Last Name {green}- {olive}%s", sName);
785 CPrintToChat(client, "{default}SteamID2 {green}- {olive}%s", sSteamId2);
786 CPrintToChat(client, "{default}SteamID3 {green}- {olive}%s", sSteamId3);
787 CPrintToChat(client, "{default}SteamID64 {green}- {olive}%s", sSteamId64);
788 CPrintToChat(client, "{default}Last IP {green}- {olive}%s", sIP);
789 CPrintToChat(client, "[{green}!{default}]{blue}========================={default}[{green}!{default}]");
790 }
791 else
792 {
793 CPrintToChat(client, "[{green}!{default}] {red}No player found");
794 }
795}
796
797public void OnHistoryAllQuery(Database db, DBResultSet results, const char[] sError, DataPack hHistoryAllPack)
798{
799 char arg[256];
800 hHistoryAllPack.Reset();
801 hHistoryAllPack.ReadString(arg, sizeof(arg));
802 int userid = hHistoryAllPack.ReadCell();
803 delete hHistoryAllPack;
804
805 int client = GetClientOfUserId(userid);
806
807 if(db == null || results == null || sError[0] != '\0')
808 {
809 LogError("Database query failure: %s", sError);
810 return;
811 }
812
813 if (!client)
814 return;
815
816 DataPack hAllHistory = new DataPack();
817 int iAllHistoryAmount;
818
819 while (results.FetchRow())
820 {
821 char sNameBuffer1[MAX_NAME_LENGTH];
822 char sIPBuffer1[16];
823 char sAccountIdBuffer1[32];
824
825 results.FetchString(0, sNameBuffer1, sizeof(sNameBuffer1));
826 results.FetchString(1, sIPBuffer1, sizeof(sIPBuffer1));
827 results.FetchString(2, sAccountIdBuffer1, sizeof(sAccountIdBuffer1));
828
829 hAllHistory.WriteString(sNameBuffer1);
830 hAllHistory.WriteString(sIPBuffer1);
831 hAllHistory.WriteString(sAccountIdBuffer1);
832 iAllHistoryAmount++;
833 }
834
835 if (results.RowCount > 0)
836 {
837 CPrintToChat(client, "[{green}!{default}]{blue}========================={default}[{green}!{default}]");
838
839 hAllHistory.Reset();
840
841 for (int i = 1; i < iAllHistoryAmount; i++)
842 {
843 if (i != 1)
844 CPrintToChat(client, "{green}----------");
845
846 char sNameBuffer2[MAX_NAME_LENGTH];
847 char sIPBuffer2[16];
848 char sAccountIdBuffer2[32];
849 hAllHistory.ReadString(sNameBuffer2, sizeof(sNameBuffer2));
850 hAllHistory.ReadString(sIPBuffer2, sizeof(sIPBuffer2));
851 hAllHistory.ReadString(sAccountIdBuffer2, sizeof(sAccountIdBuffer2));
852
853 int iAccountId = StringToInt(sAccountIdBuffer2);
854
855 char sSteamId2[32];
856 char sSteamId3[32];
857 char sSteamId64[64];
858
859 GetSteamId2ByAccountId(iAccountId, sSteamId2, sizeof(sSteamId2));
860 GetSteamId3ByAccountId(iAccountId, sSteamId3, sizeof(sSteamId3));
861 GetSteamId64ByAccountId(iAccountId, sSteamId64, sizeof(sSteamId64));
862
863 CPrintToChat(client, "{default}Name: {olive}%s", sNameBuffer2);
864 CPrintToChat(client, "{default}SteamId2: {olive}%s"
865 ..."\n{default}SteamId3: {olive}%s"
866 ..."\n{default}SteamId64: {olive}%s", sSteamId2, sSteamId3, sSteamId64);
867 CPrintToChat(client, "{default}IP: {olive}%s", sIPBuffer2);
868 }
869
870 CPrintToChat(client, "[{green}!{default}]{blue}========================={default}[{green}!{default}]");
871 }
872 else
873 CPrintToChat(client, "[{green}!{default}] {red}No player found");
874
875 delete hAllHistory;
876}
877
878public void SQL_CreateAndInsertQuery(Database db, DBResultSet results, const char[] sError, any data)
879{
880 if(db == null || results == null || sError[0] != '\0')
881 {
882 LogError("Database query failure: %s", sError);
883 return;
884 }
885}
886
887public void OnHistoryQuery(Database db, DBResultSet results, const char[] sError, DataPack hNameOrIpPack)
888{
889 hNameOrIpPack.Reset();
890 int userid = hNameOrIpPack.ReadCell();
891 delete hNameOrIpPack;
892
893 int client = GetClientOfUserId(userid);
894
895 if(db == null || results == null || sError[0] != '\0')
896 {
897 LogError("Database query failure: %s", sError);
898 return;
899 }
900
901 if (!client)
902 return;
903
904 DataPack hNameOrIpHistory = new DataPack();
905 int iNameOrIpHistoryAmount;
906
907 while (results.FetchRow())
908 {
909 char sNameOrIpBuffer1[MAX_NAME_LENGTH];
910 results.FetchString(0, sNameOrIpBuffer1, sizeof(sNameOrIpBuffer1));
911 hNameOrIpHistory.WriteString(sNameOrIpBuffer1);
912 iNameOrIpHistoryAmount++;
913 }
914
915 if (results.RowCount > 0)
916 {
917 hNameOrIpHistory.Reset();
918
919 for (int i = 0; i < iNameOrIpHistoryAmount; i++)
920 {
921 char sNameOrIpBuffer2[MAX_NAME_LENGTH];
922 hNameOrIpHistory.ReadString(sNameOrIpBuffer2, sizeof(sNameOrIpBuffer2));
923 CPrintToChat(client, "%s", sNameOrIpBuffer2);
924 }
925 }
926 else
927 CPrintToChat(client, "[{green}!{default}] {red}No player found");
928
929 delete hNameOrIpHistory;
930}
931
932public void OnHistoryIpQuery(Database db, DBResultSet results, const char[] sError, DataPack hSteamIdPack)
933{
934 hSteamIdPack.Reset();
935 int userid = hSteamIdPack.ReadCell();
936 delete hSteamIdPack;
937
938 int client = GetClientOfUserId(userid);
939
940 if(db == null || results == null || sError[0] != '\0')
941 {
942 LogError("Database query failure: %s", sError);
943 return;
944 }
945
946 if (!client)
947 return;
948
949 DataPack hSteamIdHistory = new DataPack();
950 int iSteamIdHistoryAmount;
951
952 while (results.FetchRow())
953 {
954 char sSteamIdBuffer1[32];
955 results.FetchString(0, sSteamIdBuffer1, sizeof(sSteamIdBuffer1));
956 hSteamIdHistory.WriteString(sSteamIdBuffer1);
957 iSteamIdHistoryAmount++;
958 }
959
960 if (results.RowCount > 0)
961 {
962 hSteamIdHistory.Reset();
963
964 CPrintToChat(client, "[{green}!{default}]{blue}========================={default}[{green}!{default}]");
965
966 for (int i = 1; i <= iSteamIdHistoryAmount; i++)
967 {
968 if (i != 1)
969 CPrintToChat(client, "{green}----------");
970
971 char sSteamIdBuffer2[MAX_NAME_LENGTH];
972 hSteamIdHistory.ReadString(sSteamIdBuffer2, sizeof(sSteamIdBuffer2));
973
974 int iAccountId = StringToInt(sSteamIdBuffer2);
975 char sSteamId2[32];
976 char sSteamId3[32];
977 char sSteamId64[64];
978
979 GetSteamId2ByAccountId(iAccountId, sSteamId2, sizeof(sSteamId2));
980 GetSteamId3ByAccountId(iAccountId, sSteamId3, sizeof(sSteamId3));
981 GetSteamId64ByAccountId(iAccountId, sSteamId64, sizeof(sSteamId64));
982
983 CPrintToChat(client, "{default}SteamId2: {olive}%s"
984 ..."\n{default}SteamId3: {olive}%s"
985 ..."\n{default}SteamId64: {olive}%s", sSteamId2, sSteamId3, sSteamId64);
986 }
987
988 CPrintToChat(client, "[{green}!{default}]{blue}========================={default}[{green}!{default}]");
989 }
990 else
991 CPrintToChat(client, "[{green}!{default}] {red}No player found");
992
993 delete hSteamIdHistory;
994}
995
996// SteamID Classification Functions
997
998int GetArgType(const char[] arg)
999{
1000 if (StrContains(arg, "STEAM_") != -1)
1001 return Arg_Steam2ID;
1002
1003 if (StrContains(arg, "U:1:") != -1)
1004 return Arg_Steam3ID;
1005
1006 if (IsIP(arg))
1007 return Arg_IP;
1008
1009 if (StringToInt(arg))
1010 return strlen(arg) > 16 ? Arg_Steam64ID : Arg_Steam32ID;
1011
1012 return Arg_Name;
1013}
1014
1015bool IsIP(const char[] arg)
1016{
1017 int iDots;
1018
1019 for (int i = 0; i < strlen(arg); i++)
1020 {
1021 if (arg[i] == '.')
1022 iDots++;
1023 if (!IsCharNumeric(arg[i]) && arg[i] != '.')
1024 return false;
1025 }
1026
1027 if (iDots == 3)
1028 return true;
1029
1030 return false;
1031}
1032
1033// SteamID Convert Functions
1034
1035stock int GetAccountIdByAny(int client, const char[] arg, int iArgType)
1036{
1037 int iAccountId;
1038
1039 if (GetArgType(arg) == Arg_Steam2ID)
1040 {
1041 iAccountId = GetAccountIdBySteamId2(arg);
1042
1043 if (arg[7] == '\0')
1044 {
1045 CPrintToChat(client, "{blue}[{default}!{blue}] {default}If you are trying to use a {blue}SteamID2\n"
1046 ..."{blue}[{default}!{blue}] {default}(e.g.: {olive}STEAM_0:1:191972089{default})\n"
1047 ..."{blue}[{default}!{blue}] {default}Don't forget the {green}\"quotes\"{default}!");
1048 }
1049
1050 return iAccountId;
1051 }
1052 else if (GetArgType(arg) == Arg_Steam3ID)
1053 {
1054 iAccountId = GetAccountIdBySteamId3(arg);
1055
1056 return iAccountId;
1057 }
1058 else if (GetArgType(arg) == Arg_Steam32ID)
1059 {
1060 iAccountId = StringToInt(arg);
1061
1062 return iAccountId;
1063 }
1064 else if (GetArgType(arg) == Arg_Steam64ID)
1065 {
1066 iAccountId = GetAccountIdBySteamId64(arg);
1067
1068 return iAccountId;
1069 }
1070 else if (GetArgType(arg) == Arg_Name)
1071 {
1072 if (arg[0] == 'U' && arg[1] == '\0')
1073 {
1074 CPrintToChat(client, "{blue}[{default}!{blue}] {default}If you are trying to use a {blue}SteamID3\n"
1075 ..."{blue}[{default}!{blue}] {default}(e.g.: {olive}U:1:383944179{default})\n"
1076 ..."{blue}[{default}!{blue}] {default}Don't forget the {green}\"quotes\"{default}!");
1077 }
1078 else if (arg[0] == '[' && arg[1] == 'U' && arg[2] == '\0')
1079 {
1080 CPrintToChat(client, "{blue}[{default}!{blue}] {default}If you are trying to use a {blue}SteamID3\n"
1081 ..."{blue}[{default}!{blue}] {default}(e.g.: {olive}[U:1:383944179]{default})\n"
1082 ..."{blue}[{default}!{blue}] {default}Don't forget the {green}\"quotes\"{default}!");
1083 }
1084
1085 return 0;
1086 }
1087
1088 return 0;
1089}
1090
1091stock int GetAccountIdBySteamId2(const char[] sSteamID2)
1092{
1093 char sBuffer[3][12];
1094 ExplodeString(sSteamID2, ":", sBuffer, sizeof(sBuffer), sizeof(sBuffer[]));
1095
1096 return StringToInt(sBuffer[1]) + (StringToInt(sBuffer[2]) << 1);
1097}
1098
1099stock int GetAccountIdBySteamId3(const char[] sSteamID3)
1100{
1101 char sBuffer[19];
1102 strcopy(sBuffer, sizeof(sBuffer), sSteamID3);
1103
1104 int iBuffer = strlen(sBuffer) - 1;
1105
1106 if (sBuffer[iBuffer] == ']')
1107 sBuffer[iBuffer] = '\0';
1108
1109 return StringToInt(sBuffer[FindCharInString(sBuffer, ':', true) + 1]);
1110}
1111
1112stock int GetAccountIdBySteamId64(const char[] sSteam64)
1113{
1114 static const char sBase[] = "76561197960265728";
1115 int iBorrow = 0;
1116 char sAccount[17];
1117 int iTemp;
1118
1119 for (int i = 16; i >= 0; --i)
1120 {
1121 if (iBorrow > 0)
1122 {
1123 iTemp = (sSteam64[i] - '0') - 1;
1124
1125 if (iTemp >= (sBase[i] - '0'))
1126 {
1127 iBorrow = 0;
1128 sAccount[i] = (iTemp - ((sBase[i]) - '0')) + '0';
1129 }
1130 else
1131 {
1132 iBorrow = 1;
1133 sAccount[i] = ((iTemp + 10) - (sBase[i] - '0')) + '0';
1134 }
1135 }
1136 else
1137 {
1138 if ((sSteam64[i] - '0') >= (sBase[i] - '0'))
1139 {
1140 iBorrow = 0;
1141 sAccount[i] = ((sSteam64[i] - '0') - (sBase[i] - '0')) + '0';
1142 }
1143 else
1144 {
1145 iBorrow = 1;
1146 sAccount[i] = (((sSteam64[i] - '0') + 10) - (sBase[i] - '0') + '0');
1147 }
1148 }
1149 }
1150
1151 return StringToInt(sAccount);
1152}
1153
1154stock Action GetSteamId2ByAccountId(int iAccountId, char[] sSteamId2, int isizeof)
1155{
1156 EngineVersion e = GetEngineVersion();
1157 int iEngine = (e == Engine_Left4Dead || e == Engine_Left4Dead2 || Engine_CSGO);
1158 FormatEx(sSteamId2, isizeof, "STEAM_%i:%i:%i", iEngine, iAccountId % 2, iAccountId / 2);
1159}
1160
1161stock Action GetSteamId3ByAccountId(int iAccountId, char[] sSteamId3, int isizeof)
1162{
1163 FormatEx(sSteamId3, isizeof, "[U:1:%i]", iAccountId);
1164}
1165
1166stock void GetSteamId64ByAccountId(const int iAccountId, char[] sSteamId64, int isizeof)
1167{
1168 strcopy(sSteamId64, isizeof, "76561");
1169
1170 char sBuffer[11];
1171 FormatEx(sBuffer, sizeof(sBuffer), "%010u", iAccountId);
1172 FormatEx(sSteamId64[5], isizeof - 5, "%i", 1979 + 10 * (sBuffer[0] - '0') + sBuffer[1] - '0');
1173
1174 int iBuffer = StringToInt(sBuffer[2]) + 60265728;
1175
1176 if (iBuffer > 100000000)
1177 {
1178 iBuffer -= 100000000;
1179 ++sSteamId64[8];
1180
1181 for (int i = 8; i > 4; --i)
1182 {
1183 if ( sSteamId64[i] > '9')
1184 {
1185 sSteamId64[i] = '0';
1186 ++sSteamId64[i-1];
1187 }
1188 else
1189 {
1190 break;
1191 }
1192 }
1193 }
1194
1195 FormatEx(sSteamId64[9], isizeof - 9, "%i", iBuffer);
1196}