core/smn_player.cpp
author David Anderson <dvander@alliedmods.net>
Fri Feb 16 03:47:10 2007 +0000 (2007-02-16)
changeset 501 e209a7296936
parent 492 6533d89c8ef6
child 518 52810a1a9620
permissions -rw-r--r--
removed this potential feature, not useful enough
     1 /**
     2  * ===============================================================
     3  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     4  * ===============================================================
     5  *
     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
    10  *
    11  * Version: $Id$
    12  */
    13 
    14 #include "CPlayerManager.h"
    15 #include "AdminCache.h"
    16 #include "sm_stringutil.h"
    17 
    18 static cell_t sm_GetClientCount(IPluginContext *pCtx, const cell_t *params)
    19 {
    20 	if (params[1])
    21 	{
    22 		return g_Players.NumPlayers();
    23 	}
    24 
    25 	int maxplayers = g_Players.MaxClients();
    26 	int count = 0;
    27 	for (int i=1; i<=maxplayers; ++i)
    28 	{
    29 		CPlayer *pPlayer = g_Players.GetPlayerByIndex(i);
    30 		if ((pPlayer->IsConnected()) && !(pPlayer->IsInGame()))
    31 		{
    32 			count++;
    33 		}
    34 	}
    35 
    36 	return (g_Players.NumPlayers() + count);
    37 }
    38 
    39 static cell_t sm_GetMaxClients(IPluginContext *pCtx, const cell_t *params)
    40 {
    41 	return g_Players.MaxClients();
    42 }
    43 
    44 static cell_t sm_GetClientName(IPluginContext *pCtx, const cell_t *params)
    45 {
    46 	int index = params[1];
    47 	if ((index < 1) || (index > g_Players.GetMaxClients()))
    48 	{
    49 		return pCtx->ThrowNativeError("Invalid client index %d", index);
    50 	}
    51 
    52 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
    53 	if (!pPlayer->IsConnected())
    54 	{
    55 		return pCtx->ThrowNativeError("Client %d is not connected", index);
    56 	}
    57 
    58 	pCtx->StringToLocalUTF8(params[2], static_cast<size_t>(params[3]), pPlayer->GetName(), NULL);
    59 	return 1;
    60 }
    61 
    62 static cell_t sm_GetClientIP(IPluginContext *pCtx, const cell_t *params)
    63 {
    64 	int index = params[1];
    65 	if ((index < 1) || (index > g_Players.GetMaxClients()))
    66 	{
    67 		return pCtx->ThrowNativeError("Invalid client index %d", index);
    68 	}
    69 
    70 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
    71 	if (!pPlayer->IsConnected())
    72 	{
    73 		return pCtx->ThrowNativeError("Client %d is not connected", index);
    74 	}
    75 
    76 	char buf[64], *ptr;
    77 	strcpy(buf, pPlayer->GetIPAddress());
    78 
    79 	if (params[4] && (ptr = strchr(buf, ':')))
    80 	{
    81 		*ptr = '\0';
    82 	}
    83 
    84 	pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), buf);
    85 	return 1;
    86 }
    87 
    88 static cell_t sm_GetClientAuthStr(IPluginContext *pCtx, const cell_t *params)
    89 {
    90 	int index = params[1];
    91 	if ((index < 1) || (index > g_Players.GetMaxClients()))
    92 	{
    93 		return pCtx->ThrowNativeError("Invalid client index %d", index);
    94 	}
    95 
    96 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
    97 	if (!pPlayer->IsConnected())
    98 	{
    99 		return pCtx->ThrowNativeError("Client %d is not connected", index);
   100 	}
   101 
   102 	if (!pPlayer->IsAuthorized())
   103 	{
   104 		return 0;
   105 	}
   106 
   107 	pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), pPlayer->GetAuthString());
   108 
   109 	return 1;
   110 }
   111 
   112 static cell_t sm_IsPlayerConnected(IPluginContext *pCtx, const cell_t *params)
   113 {
   114 	int index = params[1];
   115 	if ((index < 1) || (index > g_Players.GetMaxClients()))
   116 	{
   117 		return pCtx->ThrowNativeError("Invalid client index %d", index);
   118 	}
   119 
   120 	return (g_Players.GetPlayerByIndex(index)->IsConnected()) ? 1 : 0;
   121 }
   122 
   123 static cell_t sm_IsPlayerIngame(IPluginContext *pCtx, const cell_t *params)
   124 {
   125 	int index = params[1];
   126 	if ((index < 1) || (index > g_Players.GetMaxClients()))
   127 	{
   128 		return pCtx->ThrowNativeError("Invalid client index %d", index);
   129 	}
   130 
   131 	return (g_Players.GetPlayerByIndex(index)->IsInGame()) ? 1 : 0;
   132 }
   133 
   134 static cell_t sm_IsPlayerAuthorized(IPluginContext *pCtx, const cell_t *params)
   135 {
   136 	int index = params[1];
   137 	if ((index < 1) || (index > g_Players.GetMaxClients()))
   138 	{
   139 		return pCtx->ThrowNativeError("Invalid client index %d", index);
   140 	}
   141 
   142 	return (g_Players.GetPlayerByIndex(index)->IsAuthorized()) ? 1 : 0;
   143 }
   144 
   145 static cell_t sm_IsPlayerFakeClient(IPluginContext *pCtx, const cell_t *params)
   146 {
   147 	int index = params[1];
   148 	if ((index < 1) || (index > g_Players.GetMaxClients()))
   149 	{
   150 		return pCtx->ThrowNativeError("Invalid client index %d", index);
   151 	}
   152 
   153 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
   154 	if (!pPlayer->IsConnected())
   155 	{
   156 		return pCtx->ThrowNativeError("Client %d is not connected", index);
   157 	}
   158 
   159 	return (pPlayer->IsFakeClient()) ? 1 : 0;
   160 }
   161 
   162 static cell_t sm_PrintToServer(IPluginContext *pCtx, const cell_t *params)
   163 {
   164 	char buffer[1024];
   165 	char *fmt;
   166 	int arg = 2;
   167 
   168 	pCtx->LocalToString(params[1], &fmt);
   169 	size_t res = atcprintf(buffer, sizeof(buffer)-2, fmt, pCtx, params, &arg);
   170 
   171 	buffer[res++] = '\n';
   172 	buffer[res] = '\0';
   173 
   174 	META_CONPRINT(buffer);
   175 
   176 	return 1;
   177 }
   178 
   179 static cell_t sm_GetClientInfo(IPluginContext *pContext, const cell_t *params)
   180 {
   181 	int client = params[1];
   182 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   183 	if (!pPlayer)
   184 	{
   185 		return pContext->ThrowNativeError("Invalid client index %d", client);
   186 	}
   187 	if (!pPlayer->IsConnected())
   188 	{
   189 		return pContext->ThrowNativeError("Client %d is not connected", client);
   190 	}
   191 
   192 	char *key;
   193 	pContext->LocalToString(params[2], &key);
   194 
   195 	const char *val = engine->GetClientConVarValue(client, key);
   196 	if (!val)
   197 	{
   198 		return false;
   199 	}
   200 
   201 	pContext->StringToLocalUTF8(params[3], params[4], val, NULL);
   202 	return 1;
   203 }
   204 
   205 static cell_t sm_PrintToConsole(IPluginContext *pCtx, const cell_t *params)
   206 {
   207 	int index = params[1];
   208 	if ((index < 1) || (index > g_Players.GetMaxClients()))
   209 	{
   210 		return pCtx->ThrowNativeError("Invalid client index %d", index);
   211 	}
   212 
   213 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
   214 	if (!pPlayer->IsInGame())
   215 	{
   216 		return pCtx->ThrowNativeError("Client %d is not in game", index);
   217 	}
   218 
   219 	char buffer[1024];
   220 	char *fmt;
   221 	int arg = 3;
   222 
   223 	pCtx->LocalToString(params[2], &fmt);
   224 	size_t res = atcprintf(buffer, sizeof(buffer)-2, fmt, pCtx, params, &arg);
   225 
   226 	buffer[res++] = '\n';
   227 	buffer[res] = '\0';
   228 
   229 	engine->ClientPrintf(pPlayer->GetEdict(), buffer);
   230 
   231 	return 1;
   232 }
   233 
   234 static cell_t SetUserAdmin(IPluginContext *pContext, const cell_t *params)
   235 {
   236 	int client = params[1];
   237 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   238 	if (!pPlayer)
   239 	{
   240 		return pContext->ThrowNativeError("Invalid client index %d", client);
   241 	}
   242 	if (!pPlayer->IsConnected())
   243 	{
   244 		return pContext->ThrowNativeError("Client %d is not connected", client);
   245 	}
   246 	if (!g_Admins.IsValidAdmin(params[2]))
   247 	{
   248 		return pContext->ThrowNativeError("AdminId %x is not valid", params[2]);
   249 	}
   250 
   251 	pPlayer->SetAdminId(params[2], params[3] ? true : false);
   252 
   253 	return 1;
   254 }
   255 
   256 static cell_t GetUserAdmin(IPluginContext *pContext, const cell_t *params)
   257 {
   258 	int client = params[1];
   259 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   260 	if (!pPlayer)
   261 	{
   262 		return pContext->ThrowNativeError("Invalid client index %d", client);
   263 	}
   264 	if (!pPlayer->IsConnected())
   265 	{
   266 		return pContext->ThrowNativeError("Client %d is not connected", client);
   267 	}
   268 
   269 	return pPlayer->GetAdminId();
   270 }
   271 
   272 static cell_t AddUserFlags(IPluginContext *pContext, const cell_t *params)
   273 {
   274 	int client = params[1];
   275 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   276 	if (!pPlayer)
   277 	{
   278 		return pContext->ThrowNativeError("Invalid client index %d", client);
   279 	}
   280 	if (!pPlayer->IsConnected())
   281 	{
   282 		return pContext->ThrowNativeError("Client %d is not connected", client);
   283 	}
   284 
   285 	AdminId id;
   286 	if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
   287 	{
   288 		id = g_Admins.CreateAdmin(NULL);
   289 		pPlayer->SetAdminId(id, true);
   290 	}
   291 
   292 	cell_t *addr;
   293 	for (int i=2; i<=params[0]; i++)
   294 	{
   295 		pContext->LocalToPhysAddr(params[i], &addr);
   296 		g_Admins.SetAdminFlag(id, (AdminFlag)*addr, true);
   297 	}
   298 
   299 	return 1;
   300 }
   301 
   302 static cell_t RemoveUserFlags(IPluginContext *pContext, const cell_t *params)
   303 {
   304 	int client = params[1];
   305 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   306 	if (!pPlayer)
   307 	{
   308 		return pContext->ThrowNativeError("Invalid client index %d", client);
   309 	}
   310 	if (!pPlayer->IsConnected())
   311 	{
   312 		return pContext->ThrowNativeError("Client %d is not connected", client);
   313 	}
   314 	
   315 	AdminId id;
   316 	if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
   317 	{
   318 		return 0;
   319 	}
   320 
   321 	cell_t *addr;
   322 	for (int i=2; i<=params[0]; i++)
   323 	{
   324 		pContext->LocalToPhysAddr(params[i], &addr);
   325 		g_Admins.SetAdminFlag(id, (AdminFlag)*addr, false);
   326 	}
   327 
   328 	return 1;
   329 }
   330 
   331 static cell_t SetUserFlagBits(IPluginContext *pContext, const cell_t *params)
   332 {
   333 	int client = params[1];
   334 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   335 	if (!pPlayer)
   336 	{
   337 		return pContext->ThrowNativeError("Invalid client index %d", client);
   338 	}
   339 	if (!pPlayer->IsConnected())
   340 	{
   341 		return pContext->ThrowNativeError("Client %d is not connected", client);
   342 	}
   343 
   344 	AdminId id;
   345 	if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
   346 	{
   347 		id = g_Admins.CreateAdmin(NULL);
   348 		pPlayer->SetAdminId(id, true);
   349 	}
   350 
   351 	g_Admins.SetAdminFlags(id, Access_Effective, params[2]);
   352 
   353 	return 1;
   354 }
   355 
   356 static cell_t GetUserFlagBits(IPluginContext *pContext, const cell_t *params)
   357 {
   358 	int client = params[1];
   359 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   360 	if (!pPlayer)
   361 	{
   362 		return pContext->ThrowNativeError("Invalid client index %d", client);
   363 	}
   364 	if (!pPlayer->IsConnected())
   365 	{
   366 		return pContext->ThrowNativeError("Client %d is not connected", client);
   367 	}
   368 
   369 	AdminId id;
   370 	if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
   371 	{
   372 		return 0;
   373 	}
   374 
   375 	return g_Admins.GetAdminFlags(id, Access_Effective);
   376 }
   377 
   378 static cell_t GetClientUserId(IPluginContext *pContext, const cell_t *params)
   379 {
   380 	int client = params[1];
   381 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   382 	if (!pPlayer)
   383 	{
   384 		return pContext->ThrowNativeError("Invalid client index %d", client);
   385 	}
   386 	if (!pPlayer->IsConnected())
   387 	{
   388 		return pContext->ThrowNativeError("Client %d is not connected", client);
   389 	}
   390 
   391 	return engine->GetPlayerUserId(pPlayer->GetEdict());
   392 }
   393 
   394 REGISTER_NATIVES(playernatives)
   395 {
   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},
   415 	{NULL,						NULL}
   416 };
   417