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