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