2 * ===============================================================
3 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
4 * ===============================================================
6 * This file is not open source and may not be copied without explicit
7 * written permission of AlliedModders LLC. This file may not be redistributed
8 * in whole or significant part.
9 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
14 #include "CPlayerManager.h"
15 #include "AdminCache.h"
16 #include "sm_stringutil.h"
18 static cell_t sm_GetClientCount(IPluginContext *pCtx, const cell_t *params)
22 return g_Players.NumPlayers();
25 int maxplayers = g_Players.MaxClients();
27 for (int i=1; i<=maxplayers; ++i)
29 CPlayer *pPlayer = g_Players.GetPlayerByIndex(i);
30 if ((pPlayer->IsConnected()) && !(pPlayer->IsInGame()))
36 return (g_Players.NumPlayers() + count);
39 static cell_t sm_GetMaxClients(IPluginContext *pCtx, const cell_t *params)
41 return g_Players.MaxClients();
44 static cell_t sm_GetClientName(IPluginContext *pCtx, const cell_t *params)
46 int index = params[1];
47 if ((index < 1) || (index > g_Players.GetMaxClients()))
49 return pCtx->ThrowNativeError("Invalid client index %d", index);
52 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
53 if (!pPlayer->IsConnected())
55 return pCtx->ThrowNativeError("Client %d is not connected", index);
58 pCtx->StringToLocalUTF8(params[2], static_cast<size_t>(params[3]), pPlayer->GetName(), NULL);
62 static cell_t sm_GetClientIP(IPluginContext *pCtx, const cell_t *params)
64 int index = params[1];
65 if ((index < 1) || (index > g_Players.GetMaxClients()))
67 return pCtx->ThrowNativeError("Invalid client index %d", index);
70 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
71 if (!pPlayer->IsConnected())
73 return pCtx->ThrowNativeError("Client %d is not connected", index);
77 strcpy(buf, pPlayer->GetIPAddress());
79 if (params[4] && (ptr = strchr(buf, ':')))
84 pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), buf);
88 static cell_t sm_GetClientAuthStr(IPluginContext *pCtx, const cell_t *params)
90 int index = params[1];
91 if ((index < 1) || (index > g_Players.GetMaxClients()))
93 return pCtx->ThrowNativeError("Invalid client index %d", index);
96 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
97 if (!pPlayer->IsConnected())
99 return pCtx->ThrowNativeError("Client %d is not connected", index);
102 if (!pPlayer->IsAuthorized())
107 pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), pPlayer->GetAuthString());
112 static cell_t sm_IsPlayerConnected(IPluginContext *pCtx, const cell_t *params)
114 int index = params[1];
115 if ((index < 1) || (index > g_Players.GetMaxClients()))
117 return pCtx->ThrowNativeError("Invalid client index %d", index);
120 return (g_Players.GetPlayerByIndex(index)->IsConnected()) ? 1 : 0;
123 static cell_t sm_IsPlayerIngame(IPluginContext *pCtx, const cell_t *params)
125 int index = params[1];
126 if ((index < 1) || (index > g_Players.GetMaxClients()))
128 return pCtx->ThrowNativeError("Invalid client index %d", index);
131 return (g_Players.GetPlayerByIndex(index)->IsInGame()) ? 1 : 0;
134 static cell_t sm_IsPlayerAuthorized(IPluginContext *pCtx, const cell_t *params)
136 int index = params[1];
137 if ((index < 1) || (index > g_Players.GetMaxClients()))
139 return pCtx->ThrowNativeError("Invalid client index %d", index);
142 return (g_Players.GetPlayerByIndex(index)->IsAuthorized()) ? 1 : 0;
145 static cell_t sm_IsPlayerFakeClient(IPluginContext *pCtx, const cell_t *params)
147 int index = params[1];
148 if ((index < 1) || (index > g_Players.GetMaxClients()))
150 return pCtx->ThrowNativeError("Invalid client index %d", index);
153 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
154 if (!pPlayer->IsConnected())
156 return pCtx->ThrowNativeError("Client %d is not connected", index);
159 return (pPlayer->IsFakeClient()) ? 1 : 0;
162 static cell_t sm_PrintToServer(IPluginContext *pCtx, const cell_t *params)
168 pCtx->LocalToString(params[1], &fmt);
169 size_t res = atcprintf(buffer, sizeof(buffer)-2, fmt, pCtx, params, &arg);
171 buffer[res++] = '\n';
174 META_CONPRINT(buffer);
179 static cell_t sm_GetClientInfo(IPluginContext *pContext, const cell_t *params)
181 int client = params[1];
182 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
185 return pContext->ThrowNativeError("Invalid client index %d", client);
187 if (!pPlayer->IsConnected())
189 return pContext->ThrowNativeError("Client %d is not connected", client);
193 pContext->LocalToString(params[2], &key);
195 const char *val = engine->GetClientConVarValue(client, key);
201 pContext->StringToLocalUTF8(params[3], params[4], val, NULL);
205 static cell_t sm_PrintToConsole(IPluginContext *pCtx, const cell_t *params)
207 int index = params[1];
208 if ((index < 1) || (index > g_Players.GetMaxClients()))
210 return pCtx->ThrowNativeError("Invalid client index %d", index);
213 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
214 if (!pPlayer->IsInGame())
216 return pCtx->ThrowNativeError("Client %d is not in game", index);
223 pCtx->LocalToString(params[2], &fmt);
224 size_t res = atcprintf(buffer, sizeof(buffer)-2, fmt, pCtx, params, &arg);
226 buffer[res++] = '\n';
229 engine->ClientPrintf(pPlayer->GetEdict(), buffer);
234 static cell_t SetUserAdmin(IPluginContext *pContext, const cell_t *params)
236 int client = params[1];
237 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
240 return pContext->ThrowNativeError("Invalid client index %d", client);
242 if (!pPlayer->IsConnected())
244 return pContext->ThrowNativeError("Client %d is not connected", client);
246 if (!g_Admins.IsValidAdmin(params[2]))
248 return pContext->ThrowNativeError("AdminId %x is not valid", params[2]);
251 pPlayer->SetAdminId(params[2], params[3] ? true : false);
256 static cell_t GetUserAdmin(IPluginContext *pContext, const cell_t *params)
258 int client = params[1];
259 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
262 return pContext->ThrowNativeError("Invalid client index %d", client);
264 if (!pPlayer->IsConnected())
266 return pContext->ThrowNativeError("Client %d is not connected", client);
269 return pPlayer->GetAdminId();
272 static cell_t AddUserFlags(IPluginContext *pContext, const cell_t *params)
274 int client = params[1];
275 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
278 return pContext->ThrowNativeError("Invalid client index %d", client);
280 if (!pPlayer->IsConnected())
282 return pContext->ThrowNativeError("Client %d is not connected", client);
286 if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
288 id = g_Admins.CreateAdmin(NULL);
289 pPlayer->SetAdminId(id, true);
293 for (int i=2; i<=params[0]; i++)
295 pContext->LocalToPhysAddr(params[i], &addr);
296 g_Admins.SetAdminFlag(id, (AdminFlag)*addr, true);
302 static cell_t RemoveUserFlags(IPluginContext *pContext, const cell_t *params)
304 int client = params[1];
305 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
308 return pContext->ThrowNativeError("Invalid client index %d", client);
310 if (!pPlayer->IsConnected())
312 return pContext->ThrowNativeError("Client %d is not connected", client);
316 if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
322 for (int i=2; i<=params[0]; i++)
324 pContext->LocalToPhysAddr(params[i], &addr);
325 g_Admins.SetAdminFlag(id, (AdminFlag)*addr, false);
331 static cell_t SetUserFlagBits(IPluginContext *pContext, const cell_t *params)
333 int client = params[1];
334 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
337 return pContext->ThrowNativeError("Invalid client index %d", client);
339 if (!pPlayer->IsConnected())
341 return pContext->ThrowNativeError("Client %d is not connected", client);
345 if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
347 id = g_Admins.CreateAdmin(NULL);
348 pPlayer->SetAdminId(id, true);
351 g_Admins.SetAdminFlags(id, Access_Effective, params[2]);
356 static cell_t GetUserFlagBits(IPluginContext *pContext, const cell_t *params)
358 int client = params[1];
359 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
362 return pContext->ThrowNativeError("Invalid client index %d", client);
364 if (!pPlayer->IsConnected())
366 return pContext->ThrowNativeError("Client %d is not connected", client);
370 if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
375 return g_Admins.GetAdminFlags(id, Access_Effective);
378 static cell_t GetClientUserId(IPluginContext *pContext, const cell_t *params)
380 int client = params[1];
381 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
384 return pContext->ThrowNativeError("Invalid client index %d", client);
386 if (!pPlayer->IsConnected())
388 return pContext->ThrowNativeError("Client %d is not connected", client);
391 return engine->GetPlayerUserId(pPlayer->GetEdict());
394 REGISTER_NATIVES(playernatives)
396 {"GetMaxClients", sm_GetMaxClients},
397 {"GetClientCount", sm_GetClientCount},
398 {"GetClientName", sm_GetClientName},
399 {"GetClientIP", sm_GetClientIP},
400 {"GetClientAuthString", sm_GetClientAuthStr},
401 {"IsClientConnected", sm_IsPlayerConnected},
402 {"IsPlayerInGame", sm_IsPlayerIngame},
403 {"IsClientAuthorized", sm_IsPlayerAuthorized},
404 {"IsFakeClient", sm_IsPlayerFakeClient},
405 {"PrintToServer", sm_PrintToServer},
406 {"PrintToConsole", sm_PrintToConsole},
407 {"GetClientInfo", sm_GetClientInfo},
408 {"SetUserAdmin", SetUserAdmin},
409 {"GetUserAdmin", GetUserAdmin},
410 {"AddUserFlags", AddUserFlags},
411 {"RemoveUserFlags", RemoveUserFlags},
412 {"SetUserFlagBits", SetUserFlagBits},
413 {"GetUserFlagBits", GetUserFlagBits},
414 {"GetClientUserId", GetClientUserId},