core/PlayerManager.cpp
author Borja Ferrer <faluco@alliedmods.net>
Sat Feb 23 20:18:49 2008 +0000 (2008-02-23)
changeset 1894 e6efc8ffa47a
parent 1831 af5c93c936d7
child 1905 70e7cadd76a2
permissions -rw-r--r--
Executable memory is now provided by MM:S's allocator, this should reduce virtual memory usage.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "PlayerManager.h"
    33 #include "ForwardSys.h"
    34 #include "ShareSys.h"
    35 #include "AdminCache.h"
    36 #include "ConCmdManager.h"
    37 #include "MenuStyle_Valve.h"
    38 #include "MenuStyle_Radio.h"
    39 #include "sm_stringutil.h"
    40 #include "CoreConfig.h"
    41 #include "TimerSys.h"
    42 #include "Translator.h"
    43 #include "Logger.h"
    44 #include "ChatTriggers.h"
    45 #include "HalfLife2.h"
    46 #include <inetchannel.h>
    47 #include <iclient.h>
    48 #include "GameConfigs.h"
    49 
    50 PlayerManager g_Players;
    51 bool g_OnMapStarted = false;
    52 IForward *PreAdminCheck = NULL;
    53 IForward *PostAdminCheck = NULL;
    54 IForward *PostAdminFilter = NULL;
    55 const unsigned int *g_NumPlayersToAuth = NULL;
    56 int lifestate_offset = -1;
    57 List<ICommandTargetProcessor *> target_processors;
    58 
    59 SH_DECL_HOOK5(IServerGameClients, ClientConnect, SH_NOATTRIB, 0, bool, edict_t *, const char *, const char *, char *, int);
    60 SH_DECL_HOOK2_void(IServerGameClients, ClientPutInServer, SH_NOATTRIB, 0, edict_t *, const char *);
    61 SH_DECL_HOOK1_void(IServerGameClients, ClientDisconnect, SH_NOATTRIB, 0, edict_t *);
    62 #if defined ORANGEBOX_BUILD
    63 SH_DECL_HOOK2_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *, const CCommand &);
    64 #else
    65 SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *);
    66 #endif
    67 SH_DECL_HOOK1_void(IServerGameClients, ClientSettingsChanged, SH_NOATTRIB, 0, edict_t *);
    68 SH_DECL_HOOK3_void(IServerGameDLL, ServerActivate, SH_NOATTRIB, 0, edict_t *, int, int);
    69 
    70 class KickPlayerTimer : public ITimedEvent
    71 {
    72 public:
    73 	ResultType OnTimer(ITimer *pTimer, void *pData)
    74 	{
    75 		int userid = (int)pData;
    76 		int client = g_Players.GetClientOfUserId(userid);
    77 		if (client)
    78 		{
    79 			CPlayer *player = g_Players.GetPlayerByIndex(client);
    80 			player->Kick("Your name is reserved by SourceMod; set your password to use it.");
    81 		}
    82 		return Pl_Stop;
    83 	}
    84 	void OnTimerEnd(ITimer *pTimer, void *pData)
    85 	{
    86 	}
    87 } s_KickPlayerTimer;
    88 
    89 PlayerManager::PlayerManager()
    90 {
    91 	m_AuthQueue = NULL;
    92 	m_FirstPass = false;
    93 
    94 	m_UserIdLookUp = new int[USHRT_MAX+1];
    95 	memset(m_UserIdLookUp, 0, sizeof(int) * (USHRT_MAX+1));
    96 }
    97 
    98 PlayerManager::~PlayerManager()
    99 {
   100 	g_NumPlayersToAuth = NULL;
   101 
   102 	delete [] m_AuthQueue;
   103 	delete [] m_UserIdLookUp;
   104 }
   105 
   106 void PlayerManager::OnSourceModAllInitialized()
   107 {
   108 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientConnect, serverClients, this, &PlayerManager::OnClientConnect, false);
   109 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientConnect, serverClients, this, &PlayerManager::OnClientConnect_Post, true);
   110 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, serverClients, this, &PlayerManager::OnClientPutInServer, true);
   111 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, serverClients, this, &PlayerManager::OnClientDisconnect, false);
   112 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, serverClients, this, &PlayerManager::OnClientDisconnect_Post, true);
   113 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientCommand, serverClients, this, &PlayerManager::OnClientCommand, false);
   114 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, serverClients, this, &PlayerManager::OnClientSettingsChanged, true);
   115 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, gamedll, this, &PlayerManager::OnServerActivate, true);
   116 
   117 	g_ShareSys.AddInterface(NULL, this);
   118 
   119 	ParamType p1[] = {Param_Cell, Param_String, Param_Cell};
   120 	ParamType p2[] = {Param_Cell};
   121 
   122 	m_clconnect = g_Forwards.CreateForward("OnClientConnect", ET_Event, 3, p1);
   123 	m_clputinserver = g_Forwards.CreateForward("OnClientPutInServer", ET_Ignore, 1, p2);
   124 	m_cldisconnect = g_Forwards.CreateForward("OnClientDisconnect", ET_Ignore, 1, p2);
   125 	m_cldisconnect_post = g_Forwards.CreateForward("OnClientDisconnect_Post", ET_Ignore, 1, p2);
   126 	m_clcommand = g_Forwards.CreateForward("OnClientCommand", ET_Hook, 2, NULL, Param_Cell, Param_Cell);
   127 	m_clinfochanged = g_Forwards.CreateForward("OnClientSettingsChanged", ET_Ignore, 1, p2);
   128 	m_clauth = g_Forwards.CreateForward("OnClientAuthorized", ET_Ignore, 2, NULL, Param_Cell, Param_String);
   129 	m_onActivate = g_Forwards.CreateForward("OnServerLoad", ET_Ignore, 0, NULL);
   130 	m_onActivate2 = g_Forwards.CreateForward("OnMapStart", ET_Ignore, 0, NULL);
   131 
   132 	PreAdminCheck = g_Forwards.CreateForward("OnClientPreAdminCheck", ET_Event, 1, p1);
   133 	PostAdminCheck = g_Forwards.CreateForward("OnClientPostAdminCheck", ET_Ignore, 1, p1);
   134 	PostAdminFilter = g_Forwards.CreateForward("OnClientPostAdminFilter", ET_Ignore, 1, p1);
   135 }
   136 
   137 void PlayerManager::OnSourceModShutdown()
   138 {
   139 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientConnect, serverClients, this, &PlayerManager::OnClientConnect, false);
   140 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, serverClients, this, &PlayerManager::OnClientPutInServer, true);
   141 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, serverClients, this, &PlayerManager::OnClientDisconnect, false);
   142 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, serverClients, this, &PlayerManager::OnClientDisconnect_Post, true);
   143 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientCommand, serverClients, this, &PlayerManager::OnClientCommand, false);
   144 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, serverClients, this, &PlayerManager::OnClientSettingsChanged, true);
   145 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, gamedll, this, &PlayerManager::OnServerActivate, true);
   146 
   147 	/* Release forwards */
   148 	g_Forwards.ReleaseForward(m_clconnect);
   149 	g_Forwards.ReleaseForward(m_clputinserver);
   150 	g_Forwards.ReleaseForward(m_cldisconnect);
   151 	g_Forwards.ReleaseForward(m_cldisconnect_post);
   152 	g_Forwards.ReleaseForward(m_clcommand);
   153 	g_Forwards.ReleaseForward(m_clinfochanged);
   154 	g_Forwards.ReleaseForward(m_clauth);
   155 	g_Forwards.ReleaseForward(m_onActivate);
   156 	g_Forwards.ReleaseForward(m_onActivate2);
   157 
   158 	g_Forwards.ReleaseForward(PreAdminCheck);
   159 	g_Forwards.ReleaseForward(PostAdminCheck);
   160 	g_Forwards.ReleaseForward(PostAdminFilter);
   161 
   162 	delete [] m_Players;
   163 }
   164 
   165 ConfigResult PlayerManager::OnSourceModConfigChanged(const char *key, 
   166 												 const char *value, 
   167 												 ConfigSource source, 
   168 												 char *error, 
   169 												 size_t maxlength)
   170 {
   171 	if (strcmp(key, "PassInfoVar") == 0)
   172 	{
   173 		if (strcmp(value, "_password") != 0)
   174 		{
   175 			m_PassInfoVar.assign(value);
   176 		}
   177 		return ConfigResult_Accept;
   178 	} else if (strcmp(key, "AllowClLanguageVar") == 0) {
   179 		if (strcasecmp(value, "on") == 0)
   180 		{
   181 			m_QueryLang = true;
   182 		} else if (strcasecmp(value, "off") == 0) {
   183 			m_QueryLang = false;
   184 		} else {
   185 			UTIL_Format(error, maxlength, "Invalid value: must be \"on\" or \"off\"");
   186 			return ConfigResult_Reject;
   187 		}
   188 		return ConfigResult_Accept;
   189 	}
   190 	return ConfigResult_Ignore;
   191 }
   192 
   193 void PlayerManager::OnServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
   194 {
   195 	if (!m_FirstPass)
   196 	{
   197 		/* Initialize all players */
   198 		m_maxClients = clientMax;
   199 		m_PlayerCount = 0;
   200 		m_Players = new CPlayer[m_maxClients + 1];
   201 		m_AuthQueue = new unsigned int[m_maxClients + 1];
   202 		m_FirstPass = true;
   203 
   204 		memset(m_AuthQueue, 0, sizeof(unsigned int) * (m_maxClients + 1));
   205 
   206 		g_NumPlayersToAuth = &m_AuthQueue[0];
   207 	}
   208 	m_onActivate->Execute(NULL);
   209 	m_onActivate2->Execute(NULL);
   210 
   211 	List<IClientListener *>::iterator iter;
   212 	for (iter = m_hooks.begin(); iter != m_hooks.end(); iter++)
   213 	{
   214 		if ((*iter)->GetClientListenerVersion() >= 5)
   215 		{
   216 			(*iter)->OnServerActivated(clientMax);
   217 		}
   218 	}
   219 
   220 	g_OnMapStarted = true;
   221 
   222 	SM_ExecuteAllConfigs();
   223 }
   224 
   225 bool PlayerManager::IsServerActivated()
   226 {
   227 	return m_FirstPass;
   228 }
   229 
   230 bool PlayerManager::CheckSetAdmin(int index, CPlayer *pPlayer, AdminId id)
   231 {
   232 	const char *password = g_Admins.GetAdminPassword(id);
   233 	if (password != NULL)
   234 	{
   235 		if (m_PassInfoVar.size() < 1)
   236 		{
   237 			return false;
   238 		}
   239 
   240 		/* Whoa... the user needs a password! */
   241 		const char *given = engine->GetClientConVarValue(index, m_PassInfoVar.c_str());
   242 		if (!given || strcmp(given, password) != 0)
   243 		{
   244 			return false;
   245 		}
   246 	}
   247 
   248 	pPlayer->SetAdminId(id, false);
   249 
   250 	return true;
   251 }
   252 
   253 bool PlayerManager::CheckSetAdminName(int index, CPlayer *pPlayer, AdminId id)
   254 {
   255 	const char *password = g_Admins.GetAdminPassword(id);
   256 	if (password == NULL)
   257 	{
   258 		return false;
   259 	}
   260 
   261 	if (m_PassInfoVar.size() < 1)
   262 	{
   263 		return false;
   264 	}
   265 
   266 	/* Whoa... the user needs a password! */
   267 	const char *given = engine->GetClientConVarValue(index, m_PassInfoVar.c_str());
   268 	if (!given || strcmp(given, password) != 0)
   269 	{
   270 		return false;
   271 	}
   272 
   273 	pPlayer->SetAdminId(id, false);
   274 
   275 	return true;
   276 }
   277 
   278 void PlayerManager::RunAuthChecks()
   279 {
   280 	CPlayer *pPlayer;
   281 	const char *authstr;
   282 	unsigned int removed = 0;
   283 	for (unsigned int i=1; i<=m_AuthQueue[0]; i++)
   284 	{
   285 		pPlayer = GetPlayerByIndex(m_AuthQueue[i]);
   286 		authstr = engine->GetPlayerNetworkIDString(pPlayer->m_pEdict);
   287 		if (authstr && authstr[0] != '\0'
   288 			&& (strcmp(authstr, "STEAM_ID_PENDING") != 0))
   289 		{
   290 			/* Set authorization */
   291 			pPlayer->Authorize(authstr);
   292 
   293 			/* Mark as removed from queue */
   294 			unsigned int client = m_AuthQueue[i];
   295 			m_AuthQueue[i] = 0;
   296 			removed++;
   297 
   298 			/* Send to extensions */
   299 			List<IClientListener *>::iterator iter;
   300 			IClientListener *pListener;
   301 			for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
   302 			{
   303 				pListener = (*iter);
   304 				pListener->OnClientAuthorized(client, authstr);
   305 				if (!pPlayer->IsConnected())
   306 				{
   307 					break;
   308 				}
   309 			}
   310 
   311 			/* Send to plugins if player is still connected */
   312 			if (pPlayer->IsConnected() && m_clauth->GetFunctionCount())
   313 			{
   314 				/* :TODO: handle the case of a player disconnecting in the middle */
   315 				m_clauth->PushCell(client);
   316 				m_clauth->PushString(authstr);
   317 				m_clauth->Execute(NULL);
   318 			}
   319 
   320 			if (pPlayer->IsConnected())
   321 			{
   322 				pPlayer->Authorize_Post();
   323 			}
   324 		}
   325 	}
   326 
   327 	/* Clean up the queue */
   328 	if (removed)
   329 	{
   330 		/* We don't have to compact the list if it's empty */
   331 		if (removed != m_AuthQueue[0])
   332 		{
   333 			unsigned int diff = 0;
   334 			for (unsigned int i=1; i<=m_AuthQueue[0]; i++)
   335 			{
   336 				/* If this member is removed... */
   337 				if (m_AuthQueue[i] == 0)
   338 				{
   339 					/* Increase the differential */
   340 					diff++;
   341 				} else {
   342 					/* diff cannot increase faster than i+1 */
   343 					assert(i > diff);
   344 					assert(i - diff >= 1);
   345 					/* move this index down */
   346 					m_AuthQueue[i - diff] = m_AuthQueue[i];
   347 				}
   348 			}
   349 			m_AuthQueue[0] -= removed;
   350 		} else {
   351 			m_AuthQueue[0] = 0;
   352 		}
   353 	}
   354 }
   355 
   356 bool PlayerManager::OnClientConnect(edict_t *pEntity, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen)
   357 {
   358 	int client = engine->IndexOfEdict(pEntity);
   359 
   360 	List<IClientListener *>::iterator iter;
   361 	IClientListener *pListener = NULL;
   362 	for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
   363 	{
   364 		pListener = (*iter);
   365 		if (!pListener->InterceptClientConnect(client, reject, maxrejectlen))
   366 		{
   367 			return false;
   368 		}
   369 	}
   370 
   371 	cell_t res = 1;
   372 
   373 	m_Players[client].Initialize(pszName, pszAddress, pEntity);
   374 	m_clconnect->PushCell(client);
   375 	m_clconnect->PushStringEx(reject, maxrejectlen, SM_PARAM_STRING_UTF8, SM_PARAM_COPYBACK);
   376 	m_clconnect->PushCell(maxrejectlen);
   377 	m_clconnect->Execute(&res, NULL);
   378 
   379 	if (res)
   380 	{
   381 		if (!m_Players[client].IsAuthorized())
   382 		{
   383 			m_AuthQueue[++m_AuthQueue[0]] = client;
   384 		}
   385 	} else {
   386 		RETURN_META_VALUE(MRES_SUPERCEDE, false);
   387 	}
   388 
   389 	m_UserIdLookUp[engine->GetPlayerUserId(pEntity)] = client;
   390 
   391 	return true;
   392 }
   393 
   394 bool PlayerManager::OnClientConnect_Post(edict_t *pEntity, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen)
   395 {
   396 	int client = engine->IndexOfEdict(pEntity);
   397 	bool orig_value = META_RESULT_ORIG_RET(bool);
   398 	CPlayer *pPlayer = GetPlayerByIndex(client);
   399 
   400 	if (orig_value)
   401 	{
   402 		List<IClientListener *>::iterator iter;
   403 		IClientListener *pListener = NULL;
   404 		for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
   405 		{
   406 			pListener = (*iter);
   407 			pListener->OnClientConnected(client);
   408 			if (!pPlayer->IsConnected())
   409 			{
   410 				break;
   411 			}
   412 		}
   413 	}
   414 
   415 	return true;
   416 }
   417 
   418 void PlayerManager::OnClientPutInServer(edict_t *pEntity, const char *playername)
   419 {
   420 	cell_t res;
   421 	int client = engine->IndexOfEdict(pEntity);
   422 
   423 	CPlayer *pPlayer = GetPlayerByIndex(client);
   424 	/* If they're not connected, they're a bot */
   425 	if (!pPlayer->IsConnected())
   426 	{
   427 		/* Run manual connection routines */
   428 		char error[255];
   429 		const char *authid = engine->GetPlayerNetworkIDString(pEntity);
   430 		pPlayer->Authorize(authid);
   431 		if (!OnClientConnect(pEntity, playername, "127.0.0.1", error, sizeof(error)))
   432 		{
   433 			/* :TODO: kick the bot if it's rejected */
   434 			return;
   435 		}
   436 		List<IClientListener *>::iterator iter;
   437 		IClientListener *pListener = NULL;
   438 		for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
   439 		{
   440 			pListener = (*iter);
   441 			pListener->OnClientConnected(client);
   442 			/* See if bot was kicked */
   443 			if (!pPlayer->IsConnected())
   444 			{
   445 				return;
   446 			}
   447 		}
   448 		/* Now do authorization */
   449 		for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
   450 		{
   451 			pListener = (*iter);
   452 			pListener->OnClientAuthorized(client, authid);
   453 		}
   454 		/* Finally, tell plugins */
   455 		if (m_clauth->GetFunctionCount())
   456 		{
   457 			m_clauth->PushCell(client);
   458 			m_clauth->PushString(authid);
   459 			m_clauth->Execute(NULL);
   460 		}
   461 		pPlayer->Authorize_Post();
   462 	}
   463 
   464 	if (playerinfo)
   465 	{
   466 		pPlayer->m_Info = playerinfo->GetPlayerInfo(pEntity);
   467 	}
   468 
   469 	/* Get the client's language */
   470 	if (m_QueryLang)
   471 	{
   472 		const char *name;
   473 		if (!pPlayer->IsFakeClient() && (name=engine->GetClientConVarValue(client, "cl_language")))
   474 		{
   475 			unsigned int langid;
   476 			pPlayer->m_LangId = (g_Translator.GetLanguageByName(name, &langid)) ? langid : g_Translator.GetServerLanguage();
   477 		} else {
   478 			pPlayer->m_LangId = g_Translator.GetServerLanguage();
   479 		}
   480 	}
   481 
   482 	m_Players[client].Connect();
   483 	m_PlayerCount++;
   484 
   485 	List<IClientListener *>::iterator iter;
   486 	IClientListener *pListener = NULL;
   487 	for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
   488 	{
   489 		pListener = (*iter);
   490 		pListener->OnClientPutInServer(client);
   491 	}
   492 
   493 	m_clputinserver->PushCell(client);
   494 	m_clputinserver->Execute(&res, NULL);
   495 
   496 	if (m_Players[client].IsAuthorized())
   497 	{
   498 		m_Players[client].DoPostConnectAuthorization();
   499 	}
   500 }
   501 
   502 void PlayerManager::OnSourceModLevelEnd()
   503 {
   504 	/* Disconnect all bots still in game */
   505 	for (int i=1; i<=m_maxClients; i++)
   506 	{
   507 		if (m_Players[i].IsConnected())
   508 		{
   509 			OnClientDisconnect(m_Players[i].GetEdict());
   510 		}
   511 	}
   512 	m_PlayerCount = 0;
   513 }
   514 
   515 void PlayerManager::OnClientDisconnect(edict_t *pEntity)
   516 {
   517 	cell_t res;
   518 	int client = engine->IndexOfEdict(pEntity);
   519 
   520 	if (m_Players[client].IsConnected())
   521 	{
   522 		m_cldisconnect->PushCell(client);
   523 		m_cldisconnect->Execute(&res, NULL);
   524 	} else {
   525 		/* We don't care, prevent a double call */
   526 		return;
   527 	}
   528 
   529 	if (m_Players[client].WasCountedAsInGame())
   530 	{
   531 		m_PlayerCount--;
   532 	}
   533 
   534 	List<IClientListener *>::iterator iter;
   535 	IClientListener *pListener = NULL;
   536 	for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
   537 	{
   538 		pListener = (*iter);
   539 		pListener->OnClientDisconnecting(client);
   540 	}
   541 
   542 	/**
   543 	 * Remove client from auth queue if necessary
   544 	 */
   545 	if (!m_Players[client].IsAuthorized())
   546 	{
   547 		for (unsigned int i=1; i<=m_AuthQueue[0]; i++)
   548 		{
   549 			if (m_AuthQueue[i] == (unsigned)client)
   550 			{
   551 				/* Move everything ahead of us back by one */
   552 				for (unsigned int j=i+1; j<=m_AuthQueue[0]; j++)
   553 				{
   554 					m_AuthQueue[j-1] = m_AuthQueue[j];
   555 				}
   556 				/* Remove us and break */
   557 				m_AuthQueue[0]--;
   558 				break;
   559 			}
   560 		}
   561 	}
   562 
   563 	m_Players[client].Disconnect();
   564 	m_UserIdLookUp[engine->GetPlayerUserId(pEntity)] = 0;
   565 }
   566 
   567 void PlayerManager::OnClientDisconnect_Post(edict_t *pEntity)
   568 {
   569 	cell_t res;
   570 	int client = engine->IndexOfEdict(pEntity);
   571 
   572 	m_cldisconnect_post->PushCell(client);
   573 	m_cldisconnect_post->Execute(&res, NULL);
   574 
   575 	List<IClientListener *>::iterator iter;
   576 	IClientListener *pListener = NULL;
   577 	for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
   578 	{
   579 		pListener = (*iter);
   580 		pListener->OnClientDisconnected(client);
   581 	}
   582 }
   583 
   584 #if defined ORANGEBOX_BUILD
   585 void PlayerManager::OnClientCommand(edict_t *pEntity, const CCommand &args)
   586 {
   587 #else
   588 void PlayerManager::OnClientCommand(edict_t *pEntity)
   589 {
   590 	CCommand args;
   591 #endif
   592 	int client = engine->IndexOfEdict(pEntity);
   593 	cell_t res = Pl_Continue;
   594 
   595 	CPlayer *pPlayer = GetPlayerByIndex(client);
   596 	if (!pPlayer || !pPlayer->IsConnected())
   597 	{
   598 		return;
   599 	}
   600 
   601 	g_HL2.PushCommandStack(&args);
   602 
   603 	int argcount = args.ArgC() - 1;
   604 	const char *cmd = g_HL2.CurrentCommandName();
   605 
   606 	bool result = g_ValveMenuStyle.OnClientCommand(client, cmd, args);
   607 	if (result)
   608 	{
   609 		res = Pl_Handled;
   610 	} else {
   611 		result = g_RadioMenuStyle.OnClientCommand(client, cmd, args);
   612 		if (result)
   613 		{
   614 			res = Pl_Handled;
   615 		}
   616 	}
   617 
   618 	cell_t res2 = Pl_Continue;
   619 	m_clcommand->PushCell(client);
   620 	m_clcommand->PushCell(argcount);
   621 	m_clcommand->Execute(&res2, NULL);
   622 
   623 	if (res2 > res)
   624 	{
   625 		res = res2;
   626 	}
   627 
   628 	if (res >= Pl_Stop)
   629 	{
   630 		g_HL2.PopCommandStack();
   631 		RETURN_META(MRES_SUPERCEDE);
   632 	}
   633 
   634 	res = g_ConCmds.DispatchClientCommand(client, cmd, argcount, (ResultType)res);
   635 
   636 	g_HL2.PopCommandStack();
   637 
   638 	if (res >= Pl_Handled)
   639 	{
   640 		RETURN_META(MRES_SUPERCEDE);
   641 	}
   642 }
   643 
   644 void PlayerManager::OnClientSettingsChanged(edict_t *pEntity)
   645 {
   646 	cell_t res;
   647 	int client = engine->IndexOfEdict(pEntity);
   648 
   649 	if (!m_Players[client].IsConnected())
   650 	{
   651 		return;
   652 	}
   653 
   654 	m_clinfochanged->PushCell(engine->IndexOfEdict(pEntity));
   655 	m_clinfochanged->Execute(&res, NULL);
   656 
   657 	IPlayerInfo *info = m_Players[client].GetPlayerInfo();
   658 	const char *new_name = info ? info->GetName() : engine->GetClientConVarValue(client, "name");
   659 	const char *old_name = m_Players[client].m_Name.c_str();
   660 
   661 	if (strcmp(old_name, new_name) != 0)
   662 	{
   663 		AdminId id = g_Admins.FindAdminByIdentity("name", new_name);
   664 		if (id != INVALID_ADMIN_ID && m_Players[client].GetAdminId() != id)
   665 		{
   666 			if (!CheckSetAdminName(client, &m_Players[client], id))
   667 			{
   668 				m_Players[client].Kick("Your name is reserved by SourceMod; set your password to use it.");
   669 				RETURN_META(MRES_IGNORED);
   670 			}
   671 		} else if ((id = g_Admins.FindAdminByIdentity("name", old_name)) != INVALID_ADMIN_ID) {
   672 			if (id == m_Players[client].GetAdminId())
   673 			{
   674 				/* This player is changing their name; force them to drop admin privileges! */
   675 				m_Players[client].SetAdminId(INVALID_ADMIN_ID, false);
   676 			}
   677 		}
   678 		m_Players[client].SetName(new_name);
   679 	}
   680 	
   681 	if (m_PassInfoVar.size() > 0)
   682 	{
   683 		/* Try for a password change */
   684 		const char *old_pass = m_Players[client].m_LastPassword.c_str();
   685 		const char *new_pass = engine->GetClientConVarValue(client, m_PassInfoVar.c_str());
   686 		if (strcmp(old_pass, new_pass) != 0)
   687 		{
   688 			m_Players[client].m_LastPassword.assign(new_pass);
   689 			if (m_Players[client].IsInGame() && m_Players[client].IsAuthorized())
   690 			{
   691 				/* If there is already an admin id assigned, this will just bail out. */
   692 				m_Players[client].DoBasicAdminChecks();
   693 			}
   694 		}
   695 	}
   696 }
   697 
   698 int PlayerManager::GetMaxClients()
   699 {
   700 	return m_maxClients;
   701 }
   702 
   703 CPlayer *PlayerManager::GetPlayerByIndex(int client) const
   704 {
   705 	if (client > m_maxClients || client < 1)
   706 	{
   707 		return NULL;
   708 	}
   709 	return &m_Players[client];
   710 }
   711 
   712 int PlayerManager::GetNumPlayers()
   713 {
   714 	return m_PlayerCount;
   715 }
   716 
   717 int PlayerManager::GetClientOfUserId(int userid)
   718 {
   719 	if (userid < 0 || userid > USHRT_MAX)
   720 	{
   721 		return 0;
   722 	}
   723 	
   724 	int client = m_UserIdLookUp[userid];
   725 
   726 	/* Verify the userid.  The cache can get messed up with older
   727 	 * Valve engines.  :TODO: If this gets fixed, do an old engine 
   728 	 * check before invoking this backwards compat code.
   729 	 */
   730 	if (client)
   731 	{
   732 		CPlayer *player = GetPlayerByIndex(client);
   733 		if (player && player->IsConnected())
   734 		{
   735 			int realUserId = engine->GetPlayerUserId(player->GetEdict());
   736 			if (realUserId == userid)
   737 			{
   738 				return client;
   739 			}
   740 		}
   741 	}
   742 
   743 	/* If we can't verify the userid, we have to do a manual loop */
   744 	CPlayer *player;
   745 	for (int i = 1; i <= m_maxClients; i++)
   746 	{
   747 		player = GetPlayerByIndex(i);
   748 		if (!player || !player->IsConnected())
   749 		{
   750 			continue;
   751 		}
   752 		if (engine->GetPlayerUserId(player->GetEdict()) == userid)
   753 		{
   754 			m_UserIdLookUp[userid] = i;
   755 			return i;
   756 		}
   757 	}
   758 
   759 	return 0;
   760 }
   761 
   762 void PlayerManager::AddClientListener(IClientListener *listener)
   763 {
   764 	m_hooks.push_back(listener);
   765 }
   766 
   767 void PlayerManager::RemoveClientListener(IClientListener *listener)
   768 {
   769 	m_hooks.remove(listener);
   770 }
   771 
   772 IGamePlayer *PlayerManager::GetGamePlayer(edict_t *pEdict)
   773 {
   774 	int index = engine->IndexOfEdict(pEdict);
   775 	return GetGamePlayer(index);
   776 }
   777 
   778 IGamePlayer *PlayerManager::GetGamePlayer(int client)
   779 {
   780 	return GetPlayerByIndex(client);
   781 }
   782 
   783 void PlayerManager::ClearAdminId(AdminId id)
   784 {
   785 	for (int i=1; i<=m_maxClients; i++)
   786 	{
   787 		if (m_Players[i].m_Admin == id)
   788 		{
   789 			m_Players[i].DumpAdmin(true);
   790 		}
   791 	}
   792 }
   793 
   794 void PlayerManager::ClearAllAdmins()
   795 {
   796 	for (int i=1; i<=m_maxClients; i++)
   797 	{
   798 		m_Players[i].DumpAdmin(true);
   799 	}
   800 }
   801 
   802 const char *PlayerManager::GetPassInfoVar()
   803 {
   804 	return m_PassInfoVar.c_str();
   805 }
   806 
   807 void PlayerManager::RecheckAnyAdmins()
   808 {
   809 	for (int i=1; i<=m_maxClients; i++)
   810 	{
   811 		if (m_Players[i].IsInGame() && m_Players[i].IsAuthorized())
   812 		{
   813 			m_Players[i].DoBasicAdminChecks();
   814 		}
   815 	}
   816 }
   817 
   818 unsigned int PlayerManager::GetReplyTo()
   819 {
   820 	return g_ChatTriggers.GetReplyTo();
   821 }
   822 
   823 unsigned int PlayerManager::SetReplyTo(unsigned int reply)
   824 {
   825 	return g_ChatTriggers.SetReplyTo(reply);
   826 }
   827 
   828 int PlayerManager::FilterCommandTarget(IGamePlayer *pAdmin, IGamePlayer *pTarget, int flags)
   829 {
   830 	return InternalFilterCommandTarget((CPlayer *)pAdmin, (CPlayer *)pTarget, flags);
   831 }
   832 
   833 void PlayerManager::RegisterCommandTargetProcessor(ICommandTargetProcessor *pHandler)
   834 {
   835 	target_processors.push_back(pHandler);
   836 }
   837 
   838 void PlayerManager::UnregisterCommandTargetProcessor(ICommandTargetProcessor *pHandler)
   839 {
   840 	target_processors.remove(pHandler);
   841 }
   842 
   843 int PlayerManager::InternalFilterCommandTarget(CPlayer *pAdmin, CPlayer *pTarget, int flags)
   844 {
   845 	if ((flags & COMMAND_FILTER_CONNECTED) == COMMAND_FILTER_CONNECTED
   846 		&& !pTarget->IsConnected())
   847 	{
   848 		return COMMAND_TARGET_NONE;
   849 	}
   850 	else if ((flags & COMMAND_FILTER_CONNECTED) != COMMAND_FILTER_CONNECTED && 
   851 		!pTarget->IsInGame())
   852 	{
   853 		return COMMAND_TARGET_NOT_IN_GAME;
   854 	}
   855 
   856 	if ((flags & COMMAND_FILTER_NO_BOTS) == COMMAND_FILTER_NO_BOTS
   857 		&& pTarget->IsFakeClient())
   858 	{
   859 		return COMMAND_TARGET_NOT_HUMAN;
   860 	}
   861 
   862 	if (pAdmin != NULL)
   863 	{
   864 		if ((flags & COMMAND_FILTER_NO_IMMUNITY) != COMMAND_FILTER_NO_IMMUNITY
   865 			&& !g_Admins.CanAdminTarget(pAdmin->GetAdminId(), pTarget->GetAdminId()))
   866 		{
   867 			return COMMAND_TARGET_IMMUNE;
   868 		}
   869 	}
   870 
   871 	if ((flags & COMMAND_FILTER_ALIVE) == COMMAND_FILTER_ALIVE 
   872 		&& pTarget->GetLifeState() != PLAYER_LIFE_ALIVE)
   873 	{
   874 		return COMMAND_TARGET_NOT_ALIVE;
   875 	}
   876 
   877 	if ((flags & COMMAND_FILTER_DEAD) == COMMAND_FILTER_DEAD
   878 		&& pTarget->GetLifeState() != PLAYER_LIFE_DEAD)
   879 	{
   880 		return COMMAND_TARGET_NOT_DEAD;
   881 	}
   882 
   883 	return COMMAND_TARGET_VALID;
   884 }
   885 
   886 void PlayerManager::ProcessCommandTarget(cmd_target_info_t *info)
   887 {
   888 	CPlayer *pTarget, *pAdmin;
   889 	int max_clients, total = 0;
   890 
   891 	max_clients = GetMaxClients();
   892 
   893 	if (info->max_targets < 1)
   894 	{
   895 		info->reason = COMMAND_TARGET_NONE;
   896 		info->num_targets = 0;
   897 	}
   898 
   899 	if (info->admin == 0)
   900 	{
   901 		pAdmin = NULL;
   902 	}
   903 	else
   904 	{
   905 		pAdmin = GetPlayerByIndex(info->admin);
   906 	}
   907 
   908 	if (info->pattern[0] == '#')
   909 	{
   910 		int userid = atoi(&info->pattern[1]);
   911 		int client = GetClientOfUserId(userid);
   912 
   913 		/* See if a valid userid matched */
   914 		if (client > 0)
   915 		{
   916 			IGamePlayer *pTarget = GetPlayerByIndex(client);
   917 			if (pTarget != NULL)
   918 			{
   919 				if ((info->reason = FilterCommandTarget(pAdmin, pTarget, info->flags)) == COMMAND_TARGET_VALID)
   920 				{
   921 					info->targets[0] = client;
   922 					info->num_targets = 1;
   923 					strncopy(info->target_name, pTarget->GetName(), info->target_name_maxlength);
   924 					info->target_name_style = COMMAND_TARGETNAME_RAW;
   925 				}
   926 				else
   927 				{
   928 					info->num_targets = 0;
   929 				}
   930 				return;
   931 			}
   932 		}
   933 
   934 		/* See if an exact name matches */
   935 		for (int i = 1; i <= max_clients; i++)
   936 		{
   937 			if ((pTarget = GetPlayerByIndex(i)) == NULL)
   938 			{
   939 				continue;
   940 			}
   941 			if  (!pTarget->IsConnected())
   942 			{
   943 				continue;
   944 			}
   945 			if (strcmp(pTarget->GetName(), &info->pattern[1]) == 0)
   946 			{
   947 				if ((info->reason = FilterCommandTarget(pAdmin, pTarget, info->flags))
   948 					== COMMAND_TARGET_VALID)
   949 				{
   950 					info->targets[0] = i;
   951 					info->num_targets = 1;
   952 					strncopy(info->target_name, pTarget->GetName(), info->target_name_maxlength);
   953 					info->target_name_style = COMMAND_TARGETNAME_RAW;
   954 				}
   955 				else
   956 				{
   957 					info->num_targets = 0;
   958 				}
   959 				return;
   960 			}
   961 		}
   962 	}
   963 
   964 	if ((info->flags & COMMAND_FILTER_NO_MULTI) != COMMAND_FILTER_NO_MULTI)
   965 	{
   966 		bool is_multi = false;
   967 		bool bots_only = false;
   968 
   969 		if (strcmp(info->pattern, "@all") == 0)
   970 		{
   971 			is_multi = true;
   972 			strncopy(info->target_name, "all players", info->target_name_maxlength);
   973 			info->target_name_style = COMMAND_TARGETNAME_ML;
   974 		}
   975 		else if (strcmp(info->pattern, "@dead") == 0)
   976 		{
   977 			is_multi = true;
   978 			if ((info->flags & COMMAND_FILTER_ALIVE) == COMMAND_FILTER_ALIVE)
   979 			{
   980 				info->num_targets = 0;
   981 				info->reason = COMMAND_TARGET_NOT_ALIVE;
   982 				return;
   983 			}
   984 			info->flags |= COMMAND_FILTER_DEAD;
   985 			strncopy(info->target_name, "all dead players", info->target_name_maxlength);
   986 			info->target_name_style = COMMAND_TARGETNAME_ML;
   987 		}
   988 		else if (strcmp(info->pattern, "@alive") == 0)
   989 		{
   990 			is_multi = true;
   991 			if ((info->flags & COMMAND_FILTER_DEAD) == COMMAND_FILTER_DEAD)
   992 			{
   993 				info->num_targets = 0;
   994 				info->reason = COMMAND_TARGET_NOT_DEAD;
   995 				return;
   996 			}
   997 			strncopy(info->target_name, "all alive players", info->target_name_maxlength);
   998 			info->target_name_style = COMMAND_TARGETNAME_ML;
   999 			info->flags |= COMMAND_FILTER_ALIVE;
  1000 		}
  1001 		else if (strcmp(info->pattern, "@bots") == 0)
  1002 		{
  1003 			is_multi = true;
  1004 			if ((info->flags & COMMAND_FILTER_NO_BOTS) == COMMAND_FILTER_NO_BOTS)
  1005 			{
  1006 				info->num_targets = 0;
  1007 				info->reason = COMMAND_FILTER_NO_BOTS;
  1008 				return;
  1009 			}
  1010 			strncopy(info->target_name, "all bots", info->target_name_maxlength);
  1011 			info->target_name_style = COMMAND_TARGETNAME_ML;
  1012 			bots_only = true;
  1013 		}
  1014 		else if (strcmp(info->pattern, "@humans") == 0)
  1015 		{
  1016 			is_multi = true;
  1017 			strncopy(info->target_name, "all humans", info->target_name_maxlength);
  1018 			info->target_name_style = COMMAND_TARGETNAME_ML;
  1019 			info->flags |= COMMAND_FILTER_NO_BOTS;
  1020 		}
  1021 
  1022 		if (is_multi)
  1023 		{
  1024 			for (int i = 1; i <= max_clients && total < info->max_targets; i++)
  1025 			{
  1026 				if ((pTarget = GetPlayerByIndex(i)) == NULL)
  1027 				{
  1028 					continue;
  1029 				}
  1030 				if (FilterCommandTarget(pAdmin, pTarget, info->flags) > 0)
  1031 				{
  1032 					if (!bots_only || pTarget->IsFakeClient())
  1033 					{
  1034 						info->targets[total++] = i;
  1035 					}
  1036 				}
  1037 			}
  1038 
  1039 			info->num_targets = total;
  1040 			info->reason = (info->num_targets) ? COMMAND_TARGET_VALID : COMMAND_TARGET_EMPTY_FILTER;
  1041 			return;
  1042 		}
  1043 	}
  1044 
  1045 	List<ICommandTargetProcessor *>::iterator iter;
  1046 	for (iter = target_processors.begin(); iter != target_processors.end(); iter++)
  1047 	{
  1048 		ICommandTargetProcessor *pProcessor = (*iter);
  1049 		if (pProcessor->ProcessCommandTarget(info))
  1050 		{
  1051 			return;
  1052 		}
  1053 	}
  1054 
  1055 	/* Check partial names */
  1056 	int found_client = 0;
  1057 	CPlayer *pFoundClient = NULL;
  1058 	for (int i = 1; i <= max_clients; i++)
  1059 	{
  1060 		if ((pTarget = GetPlayerByIndex(i)) == NULL)
  1061 		{
  1062 			continue;
  1063 		}
  1064 
  1065 		if (stristr(pTarget->GetName(), info->pattern) != NULL)
  1066 		{
  1067 			if (found_client)
  1068 			{
  1069 				info->num_targets = 0;
  1070 				info->reason = COMMAND_TARGET_AMBIGUOUS;
  1071 				return;
  1072 			}
  1073 			else
  1074 			{
  1075 				found_client = i;
  1076 				pFoundClient = pTarget;
  1077 			}
  1078 		}
  1079 	}
  1080 
  1081 	if (found_client)
  1082 	{
  1083 		if ((info->reason = FilterCommandTarget(pAdmin, pFoundClient, info->flags)) 
  1084 			== COMMAND_TARGET_VALID)
  1085 		{
  1086 			info->targets[0] = found_client;
  1087 			info->num_targets = 1;
  1088 			strncopy(info->target_name, pFoundClient->GetName(), info->target_name_maxlength);
  1089 			info->target_name_style = COMMAND_TARGETNAME_RAW;
  1090 		}
  1091 		else
  1092 		{
  1093 			info->num_targets = 0;
  1094 		}
  1095 	}
  1096 	else
  1097 	{
  1098 		info->num_targets = 0;
  1099 		info->reason = COMMAND_TARGET_NONE;
  1100 	}
  1101 }
  1102 
  1103 /*******************
  1104  *** PLAYER CODE ***
  1105  *******************/
  1106 
  1107 CPlayer::CPlayer()
  1108 {
  1109 	m_IsConnected = false;
  1110 	m_IsInGame = false;
  1111 	m_IsAuthorized = false;
  1112 	m_pEdict = NULL;
  1113 	m_Admin = INVALID_ADMIN_ID;
  1114 	m_TempAdmin = false;
  1115 	m_Info = NULL;
  1116 	m_bAdminCheckSignalled = false;
  1117 	m_bIsInKickQueue = false;
  1118 	m_LastPassword.clear();
  1119 	m_LangId = LANGUAGE_ENGLISH;
  1120 }
  1121 
  1122 void CPlayer::Initialize(const char *name, const char *ip, edict_t *pEntity)
  1123 {
  1124 	m_IsConnected = true;
  1125 	m_Name.assign(name);
  1126 	m_Ip.assign(ip);
  1127 	m_pEdict = pEntity;
  1128 	m_iIndex = engine->IndexOfEdict(pEntity);
  1129 	m_LangId = g_Translator.GetServerLanguage();
  1130 
  1131 	char ip2[24], *ptr;
  1132 	strncopy(ip2, ip, sizeof(ip2));
  1133 	if ((ptr = strchr(ip2, ':')) != NULL)
  1134 	{
  1135 		*ptr = '\0';
  1136 	}
  1137 	m_IpNoPort.assign(ip2);
  1138 }
  1139 
  1140 void CPlayer::Connect()
  1141 {
  1142 	if (m_IsInGame)
  1143 	{
  1144 		return;
  1145 	}
  1146 
  1147 	m_IsInGame = true;
  1148 
  1149 	const char *var = g_Players.GetPassInfoVar();
  1150 	int client = engine->IndexOfEdict(m_pEdict);
  1151 	if (var[0] != '\0')
  1152 	{
  1153 		const char *pass = engine->GetClientConVarValue(client, var);
  1154 		m_LastPassword.assign(pass ? pass : "");
  1155 	}
  1156 	else
  1157 	{
  1158 		m_LastPassword.assign("");
  1159 	}
  1160 }
  1161 
  1162 void CPlayer::Authorize(const char *steamid)
  1163 {
  1164 	if (m_IsAuthorized)
  1165 	{
  1166 		return;
  1167 	}
  1168 
  1169 	m_IsAuthorized = true;
  1170 	m_AuthID.assign(steamid);
  1171 }
  1172 
  1173 void CPlayer::Disconnect()
  1174 {
  1175 	DumpAdmin(false);
  1176 	m_IsConnected = false;
  1177 	m_IsInGame = false;
  1178 	m_IsAuthorized = false;
  1179 	m_Name.clear();
  1180 	m_Ip.clear();
  1181 	m_AuthID.clear();
  1182 	m_pEdict = NULL;
  1183 	m_Info = NULL;
  1184 	m_bAdminCheckSignalled = false;
  1185 	m_UserId = -1;
  1186 	m_bIsInKickQueue = false;
  1187 }
  1188 
  1189 void CPlayer::SetName(const char *name)
  1190 {
  1191 	m_Name.assign(name);
  1192 }
  1193 
  1194 const char *CPlayer::GetName()
  1195 {
  1196 	if (m_Info && m_pEdict->GetUnknown())
  1197 	{
  1198 		return m_Info->GetName();
  1199 	}
  1200 	
  1201 	return m_Name.c_str();
  1202 }
  1203 
  1204 const char *CPlayer::GetIPAddress()
  1205 {
  1206 	return m_Ip.c_str();
  1207 }
  1208 
  1209 const char *CPlayer::GetAuthString()
  1210 {
  1211 	return m_AuthID.c_str();
  1212 }
  1213 
  1214 edict_t *CPlayer::GetEdict()
  1215 {
  1216 	return m_pEdict;
  1217 }
  1218 
  1219 bool CPlayer::IsInGame()
  1220 {
  1221 	return m_IsInGame && (m_pEdict->GetUnknown() != NULL);
  1222 }
  1223 
  1224 bool CPlayer::WasCountedAsInGame()
  1225 {
  1226 	return m_IsInGame;
  1227 }
  1228 
  1229 bool CPlayer::IsConnected()
  1230 {
  1231 	return m_IsConnected;
  1232 }
  1233 
  1234 bool CPlayer::IsAuthorized()
  1235 {
  1236 	return m_IsAuthorized;
  1237 }
  1238 
  1239 IPlayerInfo *CPlayer::GetPlayerInfo()
  1240 {
  1241 	if (m_pEdict->GetUnknown())
  1242 	{
  1243 		return m_Info;
  1244 	}
  1245 
  1246 	return NULL;
  1247 }
  1248 
  1249 bool CPlayer::IsFakeClient()
  1250 {
  1251 	return (strcmp(m_AuthID.c_str(), "BOT") == 0);
  1252 }
  1253 
  1254 void CPlayer::SetAdminId(AdminId id, bool temporary)
  1255 {
  1256 	if (!m_IsConnected)
  1257 	{
  1258 		return;
  1259 	}
  1260 
  1261 	DumpAdmin(false);
  1262 
  1263 	m_Admin = id;
  1264 	m_TempAdmin = temporary;
  1265 }
  1266 
  1267 AdminId CPlayer::GetAdminId()
  1268 {
  1269 	return m_Admin;
  1270 }
  1271 
  1272 void CPlayer::DumpAdmin(bool deleting)
  1273 {
  1274 	if (m_Admin != INVALID_ADMIN_ID)
  1275 	{
  1276 		if (m_TempAdmin && !deleting)
  1277 		{
  1278 			g_Admins.InvalidateAdmin(m_Admin);
  1279 		}
  1280 		m_Admin = INVALID_ADMIN_ID;
  1281 		m_TempAdmin = false;
  1282 	}
  1283 }
  1284 
  1285 void CPlayer::Kick(const char *str)
  1286 {
  1287 	MarkAsBeingKicked();
  1288 	INetChannel *pNetChan = static_cast<INetChannel *>(engine->GetPlayerNetInfo(m_iIndex));
  1289 	IClient *pClient = static_cast<IClient *>(pNetChan->GetMsgHandler());
  1290 	pClient->Disconnect("%s", str);
  1291 }
  1292 
  1293 void CPlayer::Authorize_Post()
  1294 {
  1295 	if (m_IsInGame)
  1296 	{
  1297 		DoPostConnectAuthorization();
  1298 	}
  1299 }
  1300 
  1301 void CPlayer::DoPostConnectAuthorization()
  1302 {
  1303 	bool delay = false;
  1304 
  1305 	List<IClientListener *>::iterator iter;
  1306 	for (iter = g_Players.m_hooks.begin();
  1307 		 iter != g_Players.m_hooks.end();
  1308 		 iter++)
  1309 	{
  1310 		IClientListener *pListener = (*iter);
  1311 #if defined MIN_API_FOR_ADMINCALLS
  1312 		if (pListener->GetClientListenerVersion() < MIN_API_FOR_ADMINCALLS)
  1313 		{
  1314 			continue;
  1315 		}
  1316 #endif
  1317 		if (!pListener->OnClientPreAdminCheck(m_iIndex))
  1318 		{
  1319 			delay = true;
  1320 		}
  1321 	}
  1322 	
  1323 	cell_t result = 0;
  1324 	PreAdminCheck->PushCell(m_iIndex);
  1325 	PreAdminCheck->Execute(&result);
  1326 
  1327 	/* Defer, for better or worse */
  1328 	if (delay || (ResultType)result >= Pl_Handled)
  1329 	{
  1330 		return;
  1331 	}
  1332 
  1333 	/* Sanity check */
  1334 	if (!IsConnected())
  1335 	{
  1336 		return;
  1337 	}
  1338 
  1339 	/* Otherwise, go ahead and do admin checks */
  1340 	DoBasicAdminChecks();
  1341 
  1342 	/* Send the notification out */
  1343 	NotifyPostAdminChecks();
  1344 }
  1345 
  1346 bool CPlayer::RunAdminCacheChecks()
  1347 {
  1348 	AdminId old_id = GetAdminId();
  1349 
  1350 	DoBasicAdminChecks();
  1351 
  1352 	return (GetAdminId() != old_id);
  1353 }
  1354 
  1355 void CPlayer::NotifyPostAdminChecks()
  1356 {
  1357 	if (m_bAdminCheckSignalled)
  1358 	{
  1359 		return;
  1360 	}
  1361 
  1362 	/* Block beforehand so they can't double-call */
  1363 	m_bAdminCheckSignalled = true;
  1364 
  1365 	List<IClientListener *>::iterator iter;
  1366 	for (iter = g_Players.m_hooks.begin();
  1367 		iter != g_Players.m_hooks.end();
  1368 		iter++)
  1369 	{
  1370 		IClientListener *pListener = (*iter);
  1371 #if defined MIN_API_FOR_ADMINCALLS
  1372 		if (pListener->GetClientListenerVersion() < MIN_API_FOR_ADMINCALLS)
  1373 		{
  1374 			continue;
  1375 		}
  1376 #endif
  1377 		pListener->OnClientPostAdminCheck(m_iIndex);
  1378 	}
  1379 
  1380 	PostAdminFilter->PushCell(m_iIndex);
  1381 	PostAdminFilter->Execute(NULL);
  1382 
  1383 	PostAdminCheck->PushCell(m_iIndex);
  1384 	PostAdminCheck->Execute(NULL);
  1385 }
  1386 
  1387 void CPlayer::DoBasicAdminChecks()
  1388 {
  1389 	if (GetAdminId() != INVALID_ADMIN_ID)
  1390 	{
  1391 		return;
  1392 	}
  1393 
  1394 	/* First check the name */
  1395 	AdminId id;
  1396 	int client = engine->IndexOfEdict(m_pEdict);
  1397 
  1398 	if ((id = g_Admins.FindAdminByIdentity("name", GetName())) != INVALID_ADMIN_ID)
  1399 	{
  1400 		if (!g_Players.CheckSetAdminName(client, this, id))
  1401 		{
  1402 			int userid = engine->GetPlayerUserId(m_pEdict);
  1403 			g_Timers.CreateTimer(&s_KickPlayerTimer, 0.1f, (void *)userid, 0);
  1404 		}
  1405 		return;
  1406 	}
  1407 	
  1408 	/* Check IP */
  1409 	if ((id = g_Admins.FindAdminByIdentity("ip", m_IpNoPort.c_str())) != INVALID_ADMIN_ID)
  1410 	{
  1411 		if (g_Players.CheckSetAdmin(client, this, id))
  1412 		{
  1413 			return;
  1414 		}
  1415 	}
  1416 
  1417 	/* Check IP address */
  1418 	if ((id = g_Admins.FindAdminByIdentity("steam", m_AuthID.c_str())) != INVALID_ADMIN_ID)
  1419 	{
  1420 		if (g_Players.CheckSetAdmin(client, this, id))
  1421 		{
  1422 			return;
  1423 		}
  1424 	}
  1425 }
  1426 
  1427 unsigned int CPlayer::GetLanguageId()
  1428 {
  1429 	return m_LangId;
  1430 }
  1431 
  1432 int CPlayer::GetUserId()
  1433 {
  1434 	if (m_UserId == -1)
  1435 	{
  1436 		m_UserId = engine->GetPlayerUserId(GetEdict());
  1437 	}
  1438 
  1439 	return m_UserId;
  1440 }
  1441 
  1442 bool CPlayer::IsInKickQueue()
  1443 {
  1444 	return m_bIsInKickQueue;
  1445 }
  1446 
  1447 void CPlayer::MarkAsBeingKicked()
  1448 {
  1449 	m_bIsInKickQueue = true;
  1450 }
  1451 
  1452 int CPlayer::GetLifeState()
  1453 {
  1454 	if (lifestate_offset == -1)
  1455 	{
  1456 		if (!g_pGameConf->GetOffset("m_lifeState", &lifestate_offset))
  1457 		{
  1458 			lifestate_offset = -2;
  1459 		}
  1460 	}
  1461 
  1462 	if (lifestate_offset < 0)
  1463 	{
  1464 		IPlayerInfo *info = GetPlayerInfo();
  1465 		if (info == NULL)
  1466 		{
  1467 			return PLAYER_LIFE_UNKNOWN;
  1468 		}
  1469 		return info->IsDead() ? PLAYER_LIFE_DEAD : PLAYER_LIFE_ALIVE;
  1470 	}
  1471 
  1472 	if (m_pEdict == NULL)
  1473 	{
  1474 		return PLAYER_LIFE_UNKNOWN;
  1475 	}
  1476 
  1477 	CBaseEntity *pEntity;
  1478 	IServerUnknown *pUnknown = m_pEdict->GetUnknown();
  1479 	if (pUnknown == NULL || (pEntity = pUnknown->GetBaseEntity()) == NULL)
  1480 	{
  1481 		return PLAYER_LIFE_UNKNOWN;
  1482 	}
  1483 
  1484 	if (*((uint8_t *)pEntity + lifestate_offset) == LIFE_ALIVE)
  1485 	{
  1486 		return PLAYER_LIFE_ALIVE;
  1487 	}
  1488 	else
  1489 	{
  1490 		return PLAYER_LIFE_DEAD;
  1491 	}
  1492 }