forked from VANCOLD/fm_drive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FM_NAME_LOG.sma
393 lines (308 loc) · 14.2 KB
/
FM_NAME_LOG.sma
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include "feckinmad/fm_global"
#include "feckinmad/fm_sql_player"
#include "feckinmad/fm_sql_tquery"
#include "feckinmad/fm_player_get"
#include "feckinmad/fm_admin_access" // fm_CommandAccess()
#include "feckinmad/fm_time"
#include <fakemeta> // engfunc(), register_forward()
#define QUERY_LIMIT 10
enum eNameData_t
{
m_sPlayerName[MAX_NAME_LEN * 2],
m_iPlayerNameIdent,
m_iPlayerNameTimeStamp
}
new Float:g_fPlayerNextNameChange[MAX_PLAYERS + 1]
new g_sQuery[512]
public plugin_init()
{
fm_RegisterPlugin()
register_concmd("admin_listidsbyname", "Admin_ListIdsByName", ADMIN_MEMBER, "<name> [start] - Lists the 10 most recent steam ids that have used specified name")
register_concmd("admin_listcommonnames", "Admin_ListCommonNames", ADMIN_MEMBER, "<target|steamid> - Lists 10 most commonly used names by target")
register_concmd("admin_listrecentnames", "Admin_ListRecentNames", ADMIN_MEMBER, "<target|steamid> - Lists 10 most recently used names by target")
register_forward(FM_ClientUserInfoChanged, "Forward_ClientUserInfoChanged")
}
//----------------------------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------------------------
new const g_sAuthidByNameQuery[] = "SELECT players.player_authid, last_used FROM players, player_names, player_names_link \
WHERE players.player_id = player_names_link.player_id AND player_names.name_id = player_names_link.name_id \
AND player_names.player_name = '%s' ORDER BY last_used DESC LIMIT %d,%d";
public Admin_ListIdsByName(id, iLevel, iCommand)
{
//----------------------------------------------------------------------------------------------------
// Check the user has access and entered the correct number of arguments
//----------------------------------------------------------------------------------------------------
if (!fm_CommandAccess(id, iLevel, true) || !fm_CommandUsage(id, iCommand, 2))
{
return PLUGIN_HANDLED
}
new sArgs[64], sName[MAX_NAME_LEN * 2], sStart[16]
read_args(sArgs, charsmax(sArgs))
parse(sArgs, sName, charsmax(sName), sStart, charsmax(sStart))
new iStart = str_to_num(sStart)
if (iStart < 0)
{
iStart = 0
}
fm_SQLMakeStringSafe(sName, (MAX_NAME_LEN * 2) - 1)
formatex(g_sQuery, charsmax(g_sQuery), g_sAuthidByNameQuery, sName, iStart, QUERY_LIMIT)
new Data[1]; Data[0] = id
fm_SQLAddThreadedQuery(g_sQuery, "Handle_ListNames", QUERY_DISPOSABLE, PRIORITY_HIGH, Data, 1)
console_print(id, "Players who have used the name: \"%s\":", QUERY_LIMIT, sName)
return PLUGIN_HANDLED
}
//----------------------------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------------------------
new const g_sRecentNameQuery[] = "SELECT player_names.player_name, last_used FROM players, player_names, player_names_link \
WHERE players.player_id = player_names_link.player_id AND player_names.name_id = player_names_link.name_id \
AND players.player_authid = '%s' \
ORDER BY last_used DESC LIMIT %d;"
public Admin_ListRecentNames(id, iLevel, iCommand)
{
//----------------------------------------------------------------------------------------------------
// Check the user has access and entered the correct number of arguments
//----------------------------------------------------------------------------------------------------
if (!fm_CommandAccess(id, iLevel, true) || !fm_CommandUsage(id, iCommand, 2))
{
return PLUGIN_HANDLED
}
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
new sArgs[192], sAuthid[MAX_AUTHID_LEN * 2]
read_args(sArgs, charsmax(sArgs))
new iPlayer = fm_CommandGetPlayer(id, sArgs)
if (!iPlayer)
{
if (!equal(sArgs, "STEAM_", 6))
{
return PLUGIN_HANDLED
}
copy(sAuthid, charsmax(sAuthid), sArgs)
}
else
{
get_user_authid(iPlayer, sAuthid, charsmax(sAuthid))
}
fm_SQLMakeStringSafe(sAuthid, (MAX_AUTHID_LEN * 2) - 1)
formatex(g_sQuery, charsmax(g_sQuery), g_sRecentNameQuery, sAuthid, QUERY_LIMIT)
new Data[1]; Data[0] = id
fm_SQLAddThreadedQuery(g_sQuery, "Handle_ListNames", QUERY_DISPOSABLE, PRIORITY_HIGH, Data, 1)
console_print(id, "Most recent names used by \"%s\":", sAuthid)
return PLUGIN_HANDLED
}
new const g_sCommonNameQuery[] = "SELECT player_names.player_name, last_used FROM players, player_names, player_names_link \
WHERE players.player_id = player_names_link.player_id AND player_names.name_id = player_names_link.name_id \
AND players.player_authid = '%s' \
ORDER BY times_used DESC LIMIT %d;"
public Admin_ListCommonNames(id, iLevel, iCommand)
{
//----------------------------------------------------------------------------------------------------
// Check the user has access and entered the correct number of arguments
//----------------------------------------------------------------------------------------------------
if (!fm_CommandAccess(id, iLevel, true) || !fm_CommandUsage(id, iCommand, 2))
{
return PLUGIN_HANDLED
}
new sArgs[192], sAuthid[MAX_AUTHID_LEN * 2]
read_args(sArgs, charsmax(sArgs))
new iPlayer = fm_CommandGetPlayer(id, sArgs)
if (!iPlayer)
{
if (!equal(sArgs, "STEAM_", 6))
{
return PLUGIN_HANDLED
}
copy(sAuthid, charsmax(sAuthid), sArgs)
}
else
{
get_user_authid(iPlayer, sAuthid, charsmax(sAuthid))
}
replace_all(sAuthid, (MAX_AUTHID_LEN * 2) - 1, "\\", "\\\\'")
replace_all(sAuthid, (MAX_AUTHID_LEN * 2) - 1, "'", "\\'")
new Data[1]; Data[0] = id
formatex(g_sQuery, charsmax(g_sQuery), g_sCommonNameQuery, sAuthid, QUERY_LIMIT)
fm_SQLAddThreadedQuery(g_sQuery, "Handle_ListNames", QUERY_DISPOSABLE, PRIORITY_HIGH, Data, 1)
console_print(id, "Most common names used by \"%s\":", sAuthid)
return PLUGIN_HANDLED
}
public Handle_ListNames(iFailState, Handle:hQuery, sError[], iError, Data[], iDataLen, Float:fQueueTime, iQueryIdent)
{
fm_DebugPrintLevel(1, "Handle_ListCommonNames: %f", fQueueTime)
new id = Data[0]
if(fm_SQLCheckThreadedError(iFailState, hQuery, sError, iError))
{
console_print(id, "There was problem querying the database")
return PLUGIN_HANDLED
}
new iCount = SQL_NumResults(hQuery)
if (!iCount)
{
console_print(id, "No results returned from the database")
return PLUGIN_HANDLED
}
new sName[MAX_NAME_LEN], sTime[64], iSecs, i, iLastUsed
new iTimeStamp = get_systime()
while(SQL_MoreResults(hQuery))
{
SQL_ReadResult(hQuery, 0, sName, charsmax(sName))
// Check last_used != 0 because of importing name data collected from a previous plugin that didn't include timestamps
iLastUsed = SQL_ReadResult(hQuery, 1)
if (!iLastUsed)
{
copy(sTime, charsmax(sTime), "Unknown")
}
else
{
iSecs = iTimeStamp - iLastUsed
fm_SecondsToText(iSecs, sTime, charsmax(sTime), 1)
add(sTime, charsmax(sTime), " ago")
}
console_print(Data[0], "\t\t#%d %s - %s", ++i, sName, sTime)
SQL_NextRow(hQuery)
}
console_print(id, "Total: %d", iCount)
return PLUGIN_HANDLED
}
//----------------------------------------------------------------------------------------------------
// Use this forward from FM_SQL_PLAYER.amxx to log names on connect
//----------------------------------------------------------------------------------------------------
public fm_SQLPlayerIdent(id, iPlayerIdent)
{
new sName[MAX_NAME_LEN]; get_user_name(id, sName, charsmax(sName))
LogPlayerName(iPlayerIdent, sName)
}
//----------------------------------------------------------------------------------------------------
// Hook to detect name changes
//----------------------------------------------------------------------------------------------------
public Forward_ClientUserInfoChanged(id, Buffer)
{
//----------------------------------------------------------------------------------------------------
// Check the user is actually connected to prevent blank name changes
//----------------------------------------------------------------------------------------------------
if (!is_user_connected(id))
{
return FMRES_IGNORED
}
//----------------------------------------------------------------------------------------------------
// Check if the player name has changed
//----------------------------------------------------------------------------------------------------
static sOldName[MAX_NAME_LEN]; get_user_name(id, sOldName, charsmax(sOldName))
static sNewName[MAX_NAME_LEN]; engfunc(EngFunc_InfoKeyValue, Buffer, "name", sNewName, charsmax(sNewName))
if (equal(sOldName, sNewName))
{
return FMRES_IGNORED
}
//----------------------------------------------------------------------------------------------------
// Player name has changed, check if they recently changed it to prevent spam
//----------------------------------------------------------------------------------------------------
new Float:fGameTime = get_gametime()
if (fGameTime < g_fPlayerNextNameChange[id])
{
//----------------------------------------------------------------------------------------------------
// They changed their name recently so don't allow
//----------------------------------------------------------------------------------------------------
engfunc(EngFunc_SetClientKeyValue, id, Buffer, "name", sOldName)
client_cmd(id, "name \"%s\"; setinfo name \"%s\"", sOldName, sOldName)
console_print(id, "Please wait another %d seconds before changing your name", floatround(g_fPlayerNextNameChange[id] - fGameTime, floatround_ceil))
return FMRES_SUPERCEDE
}
else
{
//----------------------------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------------------------
new iPlayerIdent = fm_SQLGetUserIdent(id)
if (!iPlayerIdent)
{
fm_WarningLog("Unable to log name change. iPlayerIdent is 0")
return FMRES_IGNORED
}
LogPlayerName(iPlayerIdent, sNewName)
g_fPlayerNextNameChange[id] = fGameTime + 5.0
}
return FMRES_IGNORED
}
//----------------------------------------------------------------------------------------------------
// Reset player name change time on disconnect
//----------------------------------------------------------------------------------------------------
public client_disconnected(id)
{
g_fPlayerNextNameChange[id] = 0.0
}
LogPlayerName(iPlayerIdent, sName[])
{
static Data[eNameData_t]
//----------------------------------------------------------------------------------------------------
// Fill the data struct to send with the query
//----------------------------------------------------------------------------------------------------
copy(Data[m_sPlayerName], (MAX_NAME_LEN * 2) - 1, sName)
replace_all(Data[m_sPlayerName], (MAX_NAME_LEN * 2) - 1, "'", "\\'")
Data[m_iPlayerNameIdent] = iPlayerIdent
Data[m_iPlayerNameTimeStamp] = get_systime()
//----------------------------------------------------------------------------------------------------
// Run a query to locate the name_id in the database
//----------------------------------------------------------------------------------------------------
formatex(g_sQuery, charsmax(g_sQuery), "SELECT name_id FROM player_names WHERE player_name = '%s'", Data[m_sPlayerName])
fm_SQLAddThreadedQuery(g_sQuery, "Handle_SelectName", QUERY_DISPOSABLE, PRIORITY_LOWEST, Data, _:eNameData_t)
}
public Handle_SelectName(iFailState, Handle:hQuery, sError[], iError, Data[], iDataLen, Float:fQueueTime, iQueryIdent)
{
fm_DebugPrintLevel(1, "Handle_SelectName: %f", fQueueTime)
if(fm_SQLCheckThreadedError(iFailState, hQuery, sError, iError))
{
return PLUGIN_HANDLED
}
if (!SQL_NumResults(hQuery))
{
//----------------------------------------------------------------------------------------------------
// Name was not found in the database, add it
//----------------------------------------------------------------------------------------------------
formatex(g_sQuery, charsmax(g_sQuery), "INSERT INTO player_names (player_name) VALUES ('%s');", Data[m_sPlayerName])
fm_SQLAddThreadedQuery(g_sQuery, "Handle_InsertName", QUERY_DISPOSABLE, PRIORITY_LOWEST, Data, _:eNameData_t)
}
else
{
//----------------------------------------------------------------------------------------------------
// Name was found in the database, add a record of the player using it using the name_id
//----------------------------------------------------------------------------------------------------
formatex(g_sQuery, charsmax(g_sQuery), "INSERT INTO player_names_link (player_id, name_id, last_used, times_used) VALUES ('%d', '%d', '%d', '1') ON DUPLICATE KEY UPDATE times_used = times_used + 1, last_used = %d;", Data[m_iPlayerNameIdent], SQL_ReadResult(hQuery, 0), Data[m_iPlayerNameTimeStamp], Data[m_iPlayerNameTimeStamp])
fm_SQLAddThreadedQuery(g_sQuery, "Handle_InsertPlayerName", QUERY_DISPOSABLE, PRIORITY_LOWEST, Data, _:eNameData_t)
}
return PLUGIN_HANDLED
}
public Handle_InsertName(iFailState, Handle:hQuery, sError[], iError, Data[], iDataLen, Float:fQueueTime, iQueryIdent)
{
fm_DebugPrintLevel(1, "Handle_InsertName: %f", fQueueTime)
if(fm_SQLCheckThreadedError(iFailState, hQuery, sError, iError))
{
return PLUGIN_HANDLED
}
new iNameIndex = SQL_GetInsertId(hQuery)
if (!iNameIndex)
{
fm_WarningLog("Unable to insert name into database. iNameIndex is 0")
}
else
{
formatex(g_sQuery, charsmax(g_sQuery), "INSERT INTO player_names_link (player_id, name_id, last_used, times_used) VALUES ('%d', '%d', '%d', '1') ON DUPLICATE KEY UPDATE times_used = times_used + 1, last_used = %d;", Data[m_iPlayerNameIdent], iNameIndex, Data[m_iPlayerNameTimeStamp], Data[m_iPlayerNameTimeStamp])
fm_SQLAddThreadedQuery(g_sQuery, "Handle_InsertPlayerName", QUERY_DISPOSABLE, PRIORITY_LOWEST, Data, eNameData_t)
}
return PLUGIN_HANDLED
}
public Handle_InsertPlayerName(iFailState, Handle:hQuery, sError[], iError, Data[], iDataLen, Float:fQueueTime, iQueryIdent)
{
fm_DebugPrintLevel(1, "Handle_InsertPlayerName: %f", fQueueTime)
if(fm_SQLCheckThreadedError(iFailState, hQuery, sError, iError))
{
return PLUGIN_HANDLED
}
new iPlayerNameIndex = SQL_GetInsertId(hQuery)
if (!iPlayerNameIndex)
{
fm_WarningLog("iPlayerNameIndex == 0")
}
return PLUGIN_HANDLED
}