core/PlayerManager.cpp
author Scott Ehlert <ds@alliedmods.net>
Sun Aug 24 04:35:48 2008 +0000 (2008-08-24)
changeset 2237 4403a982ec91
parent 2181 7c85684253ed
child 2289 44045ead61cd
permissions -rw-r--r--
Improved bot detection slightly. This was meant to go along with the fix for amb1801.
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@2181
   124
	m_clconnect = g_Forwards.CreateForward("OnClientConnect", ET_LowEvent, 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@2181
   291
		pPlayer = &m_Players[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@2181
   365
	CPlayer *pPlayer = &m_Players[client];
ds@1952
   366
ds@1952
   367
	List<IClientListener *>::iterator iter;
ds@1952
   368
	IClientListener *pListener = NULL;
ds@1952
   369
	for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
ds@1952
   370
	{
ds@1952
   371
		pListener = (*iter);
ds@1952
   372
		if (!pListener->InterceptClientConnect(client, reject, maxrejectlen))
ds@1952
   373
		{
ds@1952
   374
			return false;
ds@1952
   375
		}
ds@1952
   376
	}
ds@1952
   377
ds@1952
   378
	cell_t res = 1;
ds@1952
   379
ds@2181
   380
	pPlayer->Initialize(pszName, pszAddress, pEntity);
ds@1952
   381
	m_clconnect->PushCell(client);
ds@1952
   382
	m_clconnect->PushStringEx(reject, maxrejectlen, SM_PARAM_STRING_UTF8, SM_PARAM_COPYBACK);
ds@1952
   383
	m_clconnect->PushCell(maxrejectlen);
ds@2181
   384
	m_clconnect->Execute(&res);
ds@1952
   385
ds@1952
   386
	if (res)
ds@1952
   387
	{
ds@2181
   388
		if (!pPlayer->IsAuthorized())
ds@1952
   389
		{
ds@1952
   390
			m_AuthQueue[++m_AuthQueue[0]] = client;
ds@1952
   391
		}
ds@2181
   392
ds@2181
   393
		m_UserIdLookUp[engine->GetPlayerUserId(pEntity)] = client;
dvander@1996
   394
	}
dvander@1996
   395
	else
dvander@1996
   396
	{
ds@2181
   397
		if (!pPlayer->IsFakeClient())
ds@2181
   398
		{
ds@2181
   399
			RETURN_META_VALUE(MRES_SUPERCEDE, false);
ds@2181
   400
		}
ds@1952
   401
	}
ds@1952
   402
ds@1952
   403
	return true;
ds@1952
   404
}
ds@1952
   405
ds@1952
   406
bool PlayerManager::OnClientConnect_Post(edict_t *pEntity, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen)
ds@1952
   407
{
ds@1952
   408
	int client = engine->IndexOfEdict(pEntity);
ds@1952
   409
	bool orig_value = META_RESULT_ORIG_RET(bool);
ds@2181
   410
	CPlayer *pPlayer = &m_Players[client];
ds@1952
   411
ds@1952
   412
	if (orig_value)
ds@1952
   413
	{
ds@1952
   414
		List<IClientListener *>::iterator iter;
ds@1952
   415
		IClientListener *pListener = NULL;
ds@1952
   416
		for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
ds@1952
   417
		{
ds@1952
   418
			pListener = (*iter);
ds@1952
   419
			pListener->OnClientConnected(client);
ds@1952
   420
			if (!pPlayer->IsConnected())
ds@1952
   421
			{
ds@1952
   422
				break;
ds@1952
   423
			}
ds@1952
   424
		}
ds@2181
   425
ds@2181
   426
		if (!pPlayer->IsFakeClient() 
ds@2181
   427
			&& m_bIsListenServer
ds@2181
   428
			&& strncmp(pszAddress, "127.0.0.1", 9) == 0)
ds@2181
   429
		{
ds@2181
   430
			m_ListenClient = client;
ds@2181
   431
		}
ds@1952
   432
	}
ds@2181
   433
	else
dvander@1996
   434
	{
ds@2181
   435
		InvalidatePlayer(pPlayer);
dvander@1996
   436
	}
dvander@1996
   437
ds@1952
   438
	return true;
ds@1952
   439
}
ds@1952
   440
ds@1952
   441
void PlayerManager::OnClientPutInServer(edict_t *pEntity, const char *playername)
ds@1952
   442
{
ds@1952
   443
	cell_t res;
ds@1952
   444
	int client = engine->IndexOfEdict(pEntity);
ds@2181
   445
	CPlayer *pPlayer = &m_Players[client];
ds@1952
   446
ds@1952
   447
	/* If they're not connected, they're a bot */
ds@1952
   448
	if (!pPlayer->IsConnected())
ds@1952
   449
	{
ds@1952
   450
		/* Run manual connection routines */
ds@1952
   451
		char error[255];
ds@1952
   452
		const char *authid = engine->GetPlayerNetworkIDString(pEntity);
ds@1952
   453
		pPlayer->Authorize(authid);
ds@2237
   454
		pPlayer->m_bFakeClient = true;
ds@1952
   455
		if (!OnClientConnect(pEntity, playername, "127.0.0.1", error, sizeof(error)))
ds@1952
   456
		{
ds@1952
   457
			/* :TODO: kick the bot if it's rejected */
ds@1952
   458
			return;
ds@1952
   459
		}
ds@1952
   460
		List<IClientListener *>::iterator iter;
ds@1952
   461
		IClientListener *pListener = NULL;
ds@1952
   462
		for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
ds@1952
   463
		{
ds@1952
   464
			pListener = (*iter);
ds@1952
   465
			pListener->OnClientConnected(client);
ds@1952
   466
			/* See if bot was kicked */
ds@1952
   467
			if (!pPlayer->IsConnected())
ds@1952
   468
			{
ds@1952
   469
				return;
ds@1952
   470
			}
ds@1952
   471
		}
ds@1952
   472
		/* Now do authorization */
ds@1952
   473
		for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
ds@1952
   474
		{
ds@1952
   475
			pListener = (*iter);
ds@1952
   476
			pListener->OnClientAuthorized(client, authid);
ds@1952
   477
		}
ds@1952
   478
		/* Finally, tell plugins */
ds@1952
   479
		if (m_clauth->GetFunctionCount())
ds@1952
   480
		{
ds@1952
   481
			m_clauth->PushCell(client);
ds@1952
   482
			m_clauth->PushString(authid);
ds@1952
   483
			m_clauth->Execute(NULL);
ds@1952
   484
		}
ds@1952
   485
		pPlayer->Authorize_Post();
ds@1952
   486
	}
ds@1952
   487
ds@1952
   488
	if (playerinfo)
ds@1952
   489
	{
ds@1952
   490
		pPlayer->m_Info = playerinfo->GetPlayerInfo(pEntity);
ds@1952
   491
	}
ds@1952
   492
ds@1952
   493
	/* Get the client's language */
ds@1952
   494
	if (m_QueryLang)
ds@1952
   495
	{
ds@1952
   496
		const char *name;
ds@1952
   497
		if (!pPlayer->IsFakeClient() && (name=engine->GetClientConVarValue(client, "cl_language")))
ds@1952
   498
		{
ds@1952
   499
			unsigned int langid;
ds@1952
   500
			pPlayer->m_LangId = (g_Translator.GetLanguageByName(name, &langid)) ? langid : g_Translator.GetServerLanguage();
ds@1952
   501
		} else {
ds@1952
   502
			pPlayer->m_LangId = g_Translator.GetServerLanguage();
ds@1952
   503
		}
ds@1952
   504
	}
ds@1952
   505
ds@2181
   506
	pPlayer->Connect();
ds@1952
   507
	m_PlayerCount++;
ds@1952
   508
ds@1952
   509
	List<IClientListener *>::iterator iter;
ds@1952
   510
	IClientListener *pListener = NULL;
ds@1952
   511
	for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
ds@1952
   512
	{
ds@1952
   513
		pListener = (*iter);
ds@1952
   514
		pListener->OnClientPutInServer(client);
ds@1952
   515
	}
ds@1952
   516
ds@1952
   517
	m_clputinserver->PushCell(client);
ds@1952
   518
	m_clputinserver->Execute(&res, NULL);
ds@1952
   519
ds@2181
   520
	if (pPlayer->IsAuthorized())
ds@1952
   521
	{
ds@2181
   522
		pPlayer->DoPostConnectAuthorization();
ds@1952
   523
	}
ds@1952
   524
}
ds@1952
   525
ds@1952
   526
void PlayerManager::OnSourceModLevelEnd()
ds@1952
   527
{
ds@1952
   528
	/* Disconnect all bots still in game */
ds@1952
   529
	for (int i=1; i<=m_maxClients; i++)
ds@1952
   530
	{
ds@1952
   531
		if (m_Players[i].IsConnected())
ds@1952
   532
		{
ds@1952
   533
			OnClientDisconnect(m_Players[i].GetEdict());
ds@1952
   534
		}
ds@1952
   535
	}
ds@1952
   536
	m_PlayerCount = 0;
ds@1952
   537
}
ds@1952
   538
ds@1952
   539
void PlayerManager::OnClientDisconnect(edict_t *pEntity)
ds@1952
   540
{
ds@1952
   541
	cell_t res;
ds@1952
   542
	int client = engine->IndexOfEdict(pEntity);
ds@2181
   543
	CPlayer *pPlayer = &m_Players[client];
ds@1952
   544
ds@2181
   545
	if (pPlayer->IsConnected())
ds@1952
   546
	{
ds@1952
   547
		m_cldisconnect->PushCell(client);
ds@1952
   548
		m_cldisconnect->Execute(&res, NULL);
dvander@1996
   549
	}
dvander@1996
   550
	else
dvander@1996
   551
	{
ds@1952
   552
		/* We don't care, prevent a double call */
ds@1952
   553
		return;
ds@1952
   554
	}
ds@1952
   555
ds@2181
   556
	if (pPlayer->WasCountedAsInGame())
ds@1952
   557
	{
ds@1952
   558
		m_PlayerCount--;
ds@1952
   559
	}
ds@1952
   560
ds@1952
   561
	List<IClientListener *>::iterator iter;
ds@1952
   562
	IClientListener *pListener = NULL;
ds@1952
   563
	for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
ds@1952
   564
	{
ds@1952
   565
		pListener = (*iter);
ds@1952
   566
		pListener->OnClientDisconnecting(client);
ds@1952
   567
	}
ds@1952
   568
ds@2181
   569
	InvalidatePlayer(pPlayer);
dvander@1996
   570
dvander@1996
   571
	if (m_ListenClient == client)
dvander@1996
   572
	{
dvander@1996
   573
		m_ListenClient = 0;
dvander@1996
   574
	}
ds@1952
   575
}
ds@1952
   576
ds@1952
   577
void PlayerManager::OnClientDisconnect_Post(edict_t *pEntity)
ds@1952
   578
{
ds@1952
   579
	cell_t res;
ds@1952
   580
	int client = engine->IndexOfEdict(pEntity);
ds@1952
   581
ds@1952
   582
	m_cldisconnect_post->PushCell(client);
ds@1952
   583
	m_cldisconnect_post->Execute(&res, NULL);
ds@1952
   584
ds@1952
   585
	List<IClientListener *>::iterator iter;
ds@1952
   586
	IClientListener *pListener = NULL;
ds@1952
   587
	for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
ds@1952
   588
	{
ds@1952
   589
		pListener = (*iter);
ds@1952
   590
		pListener->OnClientDisconnected(client);
ds@1952
   591
	}
ds@1952
   592
}
ds@1952
   593
ds@1952
   594
#if defined ORANGEBOX_BUILD
ds@1952
   595
void PlayerManager::OnClientCommand(edict_t *pEntity, const CCommand &args)
ds@1952
   596
{
ds@1952
   597
#else
ds@1952
   598
void PlayerManager::OnClientCommand(edict_t *pEntity)
ds@1952
   599
{
ds@1952
   600
	CCommand args;
ds@1952
   601
#endif
ds@1952
   602
	int client = engine->IndexOfEdict(pEntity);
ds@1952
   603
	cell_t res = Pl_Continue;
ds@2181
   604
	CPlayer *pPlayer = &m_Players[client];
ds@1952
   605
ds@2181
   606
	if (!pPlayer->IsConnected())
ds@1952
   607
	{
ds@1952
   608
		return;
ds@1952
   609
	}
ds@1952
   610
ds@1952
   611
	g_HL2.PushCommandStack(&args);
ds@1952
   612
ds@1952
   613
	int argcount = args.ArgC() - 1;
ds@1952
   614
	const char *cmd = g_HL2.CurrentCommandName();
ds@1952
   615
ds@1952
   616
	bool result = g_ValveMenuStyle.OnClientCommand(client, cmd, args);
ds@1952
   617
	if (result)
ds@1952
   618
	{
ds@1952
   619
		res = Pl_Handled;
ds@1952
   620
	} else {
ds@1952
   621
		result = g_RadioMenuStyle.OnClientCommand(client, cmd, args);
ds@1952
   622
		if (result)
ds@1952
   623
		{
ds@1952
   624
			res = Pl_Handled;
ds@1952
   625
		}
ds@1952
   626
	}
ds@1952
   627
ds@1952
   628
	cell_t res2 = Pl_Continue;
ds@1952
   629
	m_clcommand->PushCell(client);
ds@1952
   630
	m_clcommand->PushCell(argcount);
ds@1952
   631
	m_clcommand->Execute(&res2, NULL);
ds@1952
   632
ds@1952
   633
	if (res2 > res)
ds@1952
   634
	{
ds@1952
   635
		res = res2;
ds@1952
   636
	}
ds@1952
   637
ds@1952
   638
	if (res >= Pl_Stop)
ds@1952
   639
	{
ds@1952
   640
		g_HL2.PopCommandStack();
ds@1952
   641
		RETURN_META(MRES_SUPERCEDE);
ds@1952
   642
	}
ds@1952
   643
ds@1952
   644
	res = g_ConCmds.DispatchClientCommand(client, cmd, argcount, (ResultType)res);
ds@1952
   645
ds@1952
   646
	g_HL2.PopCommandStack();
ds@1952
   647
ds@1952
   648
	if (res >= Pl_Handled)
ds@1952
   649
	{
ds@1952
   650
		RETURN_META(MRES_SUPERCEDE);
ds@1952
   651
	}
ds@1952
   652
}
ds@1952
   653
ds@1952
   654
void PlayerManager::OnClientSettingsChanged(edict_t *pEntity)
ds@1952
   655
{
ds@1952
   656
	cell_t res;
ds@1952
   657
	int client = engine->IndexOfEdict(pEntity);
ds@2181
   658
	CPlayer *pPlayer = &m_Players[client];
ds@1952
   659
ds@2181
   660
	if (!pPlayer->IsConnected())
ds@1952
   661
	{
ds@1952
   662
		return;
ds@1952
   663
	}
ds@1952
   664
ds@1952
   665
	m_clinfochanged->PushCell(engine->IndexOfEdict(pEntity));
ds@1952
   666
	m_clinfochanged->Execute(&res, NULL);
ds@1952
   667
ds@2181
   668
	IPlayerInfo *info = pPlayer->GetPlayerInfo();
ds@1952
   669
	const char *new_name = info ? info->GetName() : engine->GetClientConVarValue(client, "name");
ds@2181
   670
	const char *old_name = pPlayer->m_Name.c_str();
ds@1952
   671
ds@1952
   672
	if (strcmp(old_name, new_name) != 0)
ds@1952
   673
	{
ds@1952
   674
		AdminId id = g_Admins.FindAdminByIdentity("name", new_name);
ds@2181
   675
		if (id != INVALID_ADMIN_ID && pPlayer->GetAdminId() != id)
ds@1952
   676
		{
ds@2181
   677
			if (!CheckSetAdminName(client, pPlayer, id))
ds@1952
   678
			{
ds@2181
   679
				pPlayer->Kick("Your name is reserved by SourceMod; set your password to use it.");
ds@1952
   680
				RETURN_META(MRES_IGNORED);
ds@1952
   681
			}
ds@1952
   682
		} else if ((id = g_Admins.FindAdminByIdentity("name", old_name)) != INVALID_ADMIN_ID) {
ds@2181
   683
			if (id == pPlayer->GetAdminId())
ds@1952
   684
			{
ds@1952
   685
				/* This player is changing their name; force them to drop admin privileges! */
ds@2181
   686
				pPlayer->SetAdminId(INVALID_ADMIN_ID, false);
ds@1952
   687
			}
ds@1952
   688
		}
ds@2181
   689
		pPlayer->SetName(new_name);
ds@1952
   690
	}
ds@1952
   691
	
ds@1952
   692
	if (m_PassInfoVar.size() > 0)
ds@1952
   693
	{
ds@1952
   694
		/* Try for a password change */
ds@2181
   695
		const char *old_pass = pPlayer->m_LastPassword.c_str();
ds@1952
   696
		const char *new_pass = engine->GetClientConVarValue(client, m_PassInfoVar.c_str());
ds@1952
   697
		if (strcmp(old_pass, new_pass) != 0)
ds@1952
   698
		{
ds@2181
   699
			pPlayer->m_LastPassword.assign(new_pass);
ds@2181
   700
			if (pPlayer->IsInGame() && pPlayer->IsAuthorized())
ds@1952
   701
			{
ds@1952
   702
				/* If there is already an admin id assigned, this will just bail out. */
ds@2181
   703
				pPlayer->DoBasicAdminChecks();
ds@1952
   704
			}
ds@1952
   705
		}
ds@1952
   706
	}
ds@1952
   707
}
ds@1952
   708
ds@1952
   709
int PlayerManager::GetMaxClients()
ds@1952
   710
{
ds@1952
   711
	return m_maxClients;
ds@1952
   712
}
ds@1952
   713
ds@1952
   714
CPlayer *PlayerManager::GetPlayerByIndex(int client) const
ds@1952
   715
{
ds@1952
   716
	if (client > m_maxClients || client < 1)
ds@1952
   717
	{
ds@1952
   718
		return NULL;
ds@1952
   719
	}
ds@1952
   720
	return &m_Players[client];
ds@1952
   721
}
ds@1952
   722
ds@1952
   723
int PlayerManager::GetNumPlayers()
ds@1952
   724
{
ds@1952
   725
	return m_PlayerCount;
ds@1952
   726
}
ds@1952
   727
ds@1952
   728
int PlayerManager::GetClientOfUserId(int userid)
ds@1952
   729
{
ds@1952
   730
	if (userid < 0 || userid > USHRT_MAX)
ds@1952
   731
	{
ds@1952
   732
		return 0;
ds@1952
   733
	}
ds@1952
   734
	
ds@1952
   735
	int client = m_UserIdLookUp[userid];
ds@1952
   736
ds@1952
   737
	/* Verify the userid.  The cache can get messed up with older
ds@1952
   738
	 * Valve engines.  :TODO: If this gets fixed, do an old engine 
ds@1952
   739
	 * check before invoking this backwards compat code.
ds@1952
   740
	 */
ds@1952
   741
	if (client)
ds@1952
   742
	{
ds@1952
   743
		CPlayer *player = GetPlayerByIndex(client);
ds@1952
   744
		if (player && player->IsConnected())
ds@1952
   745
		{
ds@1952
   746
			int realUserId = engine->GetPlayerUserId(player->GetEdict());
ds@1952
   747
			if (realUserId == userid)
ds@1952
   748
			{
ds@1952
   749
				return client;
ds@1952
   750
			}
ds@1952
   751
		}
ds@1952
   752
	}
ds@1952
   753
ds@1952
   754
	/* If we can't verify the userid, we have to do a manual loop */
ds@1952
   755
	CPlayer *player;
ds@1952
   756
	for (int i = 1; i <= m_maxClients; i++)
ds@1952
   757
	{
ds@1952
   758
		player = GetPlayerByIndex(i);
ds@1952
   759
		if (!player || !player->IsConnected())
ds@1952
   760
		{
ds@1952
   761
			continue;
ds@1952
   762
		}
ds@1952
   763
		if (engine->GetPlayerUserId(player->GetEdict()) == userid)
ds@1952
   764
		{
ds@1952
   765
			m_UserIdLookUp[userid] = i;
ds@1952
   766
			return i;
ds@1952
   767
		}
ds@1952
   768
	}
ds@1952
   769
ds@1952
   770
	return 0;
ds@1952
   771
}
ds@1952
   772
ds@1952
   773
void PlayerManager::AddClientListener(IClientListener *listener)
ds@1952
   774
{
ds@1952
   775
	m_hooks.push_back(listener);
ds@1952
   776
}
ds@1952
   777
ds@1952
   778
void PlayerManager::RemoveClientListener(IClientListener *listener)
ds@1952
   779
{
ds@1952
   780
	m_hooks.remove(listener);
ds@1952
   781
}
ds@1952
   782
ds@1952
   783
IGamePlayer *PlayerManager::GetGamePlayer(edict_t *pEdict)
ds@1952
   784
{
ds@1952
   785
	int index = engine->IndexOfEdict(pEdict);
ds@1952
   786
	return GetGamePlayer(index);
ds@1952
   787
}
ds@1952
   788
ds@1952
   789
IGamePlayer *PlayerManager::GetGamePlayer(int client)
ds@1952
   790
{
ds@1952
   791
	return GetPlayerByIndex(client);
ds@1952
   792
}
ds@1952
   793
ds@1952
   794
void PlayerManager::ClearAdminId(AdminId id)
ds@1952
   795
{
ds@1952
   796
	for (int i=1; i<=m_maxClients; i++)
ds@1952
   797
	{
ds@1952
   798
		if (m_Players[i].m_Admin == id)
ds@1952
   799
		{
ds@1952
   800
			m_Players[i].DumpAdmin(true);
ds@1952
   801
		}
ds@1952
   802
	}
ds@1952
   803
}
ds@1952
   804
ds@1952
   805
void PlayerManager::ClearAllAdmins()
ds@1952
   806
{
ds@1952
   807
	for (int i=1; i<=m_maxClients; i++)
ds@1952
   808
	{
ds@1952
   809
		m_Players[i].DumpAdmin(true);
ds@1952
   810
	}
ds@1952
   811
}
ds@1952
   812
ds@1952
   813
const char *PlayerManager::GetPassInfoVar()
ds@1952
   814
{
ds@1952
   815
	return m_PassInfoVar.c_str();
ds@1952
   816
}
ds@1952
   817
ds@1952
   818
void PlayerManager::RecheckAnyAdmins()
ds@1952
   819
{
ds@1952
   820
	for (int i=1; i<=m_maxClients; i++)
ds@1952
   821
	{
ds@1952
   822
		if (m_Players[i].IsInGame() && m_Players[i].IsAuthorized())
ds@1952
   823
		{
ds@1952
   824
			m_Players[i].DoBasicAdminChecks();
ds@1952
   825
		}
ds@1952
   826
	}
ds@1952
   827
}
ds@1952
   828
ds@1952
   829
unsigned int PlayerManager::GetReplyTo()
ds@1952
   830
{
ds@1952
   831
	return g_ChatTriggers.GetReplyTo();
ds@1952
   832
}
ds@1952
   833
ds@1952
   834
unsigned int PlayerManager::SetReplyTo(unsigned int reply)
ds@1952
   835
{
ds@1952
   836
	return g_ChatTriggers.SetReplyTo(reply);
ds@1952
   837
}
ds@1952
   838
ds@1952
   839
int PlayerManager::FilterCommandTarget(IGamePlayer *pAdmin, IGamePlayer *pTarget, int flags)
ds@1952
   840
{
ds@1952
   841
	return InternalFilterCommandTarget((CPlayer *)pAdmin, (CPlayer *)pTarget, flags);
ds@1952
   842
}
ds@1952
   843
ds@1952
   844
void PlayerManager::RegisterCommandTargetProcessor(ICommandTargetProcessor *pHandler)
ds@1952
   845
{
ds@1952
   846
	target_processors.push_back(pHandler);
ds@1952
   847
}
ds@1952
   848
ds@1952
   849
void PlayerManager::UnregisterCommandTargetProcessor(ICommandTargetProcessor *pHandler)
ds@1952
   850
{
ds@1952
   851
	target_processors.remove(pHandler);
ds@1952
   852
}
ds@1952
   853
ds@2181
   854
void PlayerManager::InvalidatePlayer(CPlayer *pPlayer)
ds@2181
   855
{
ds@2181
   856
	/**
ds@2181
   857
	* Remove client from auth queue if necessary
ds@2181
   858
	*/
ds@2181
   859
	if (!pPlayer->IsAuthorized())
ds@2181
   860
	{
ds@2181
   861
		for (unsigned int i=1; i<=m_AuthQueue[0]; i++)
ds@2181
   862
		{
ds@2181
   863
			if (m_AuthQueue[i] == (unsigned)pPlayer->m_iIndex)
ds@2181
   864
			{
ds@2181
   865
				/* Move everything ahead of us back by one */
ds@2181
   866
				for (unsigned int j=i+1; j<=m_AuthQueue[0]; j++)
ds@2181
   867
				{
ds@2181
   868
					m_AuthQueue[j-1] = m_AuthQueue[j];
ds@2181
   869
				}
ds@2181
   870
				/* Remove us and break */
ds@2181
   871
				m_AuthQueue[0]--;
ds@2181
   872
				break;
ds@2181
   873
			}
ds@2181
   874
		}
ds@2181
   875
	}
ds@2181
   876
	
ds@2181
   877
	m_UserIdLookUp[engine->GetPlayerUserId(pPlayer->m_pEdict)] = 0;
ds@2181
   878
	pPlayer->Disconnect();
ds@2181
   879
}
ds@2181
   880
ds@1952
   881
int PlayerManager::InternalFilterCommandTarget(CPlayer *pAdmin, CPlayer *pTarget, int flags)
ds@1952
   882
{
ds@1952
   883
	if ((flags & COMMAND_FILTER_CONNECTED) == COMMAND_FILTER_CONNECTED
ds@1952
   884
		&& !pTarget->IsConnected())
ds@1952
   885
	{
ds@1952
   886
		return COMMAND_TARGET_NONE;
ds@1952
   887
	}
ds@1952
   888
	else if ((flags & COMMAND_FILTER_CONNECTED) != COMMAND_FILTER_CONNECTED && 
ds@1952
   889
		!pTarget->IsInGame())
ds@1952
   890
	{
ds@1952
   891
		return COMMAND_TARGET_NOT_IN_GAME;
ds@1952
   892
	}
ds@1952
   893
ds@1952
   894
	if ((flags & COMMAND_FILTER_NO_BOTS) == COMMAND_FILTER_NO_BOTS
ds@1952
   895
		&& pTarget->IsFakeClient())
ds@1952
   896
	{
ds@1952
   897
		return COMMAND_TARGET_NOT_HUMAN;
ds@1952
   898
	}
ds@1952
   899
ds@1952
   900
	if (pAdmin != NULL)
ds@1952
   901
	{
ds@1952
   902
		if ((flags & COMMAND_FILTER_NO_IMMUNITY) != COMMAND_FILTER_NO_IMMUNITY
ds@1952
   903
			&& !g_Admins.CanAdminTarget(pAdmin->GetAdminId(), pTarget->GetAdminId()))
ds@1952
   904
		{
ds@1952
   905
			return COMMAND_TARGET_IMMUNE;
ds@1952
   906
		}
ds@1952
   907
	}
ds@1952
   908
ds@1952
   909
	if ((flags & COMMAND_FILTER_ALIVE) == COMMAND_FILTER_ALIVE 
ds@1952
   910
		&& pTarget->GetLifeState() != PLAYER_LIFE_ALIVE)
ds@1952
   911
	{
ds@1952
   912
		return COMMAND_TARGET_NOT_ALIVE;
ds@1952
   913
	}
ds@1952
   914
ds@1952
   915
	if ((flags & COMMAND_FILTER_DEAD) == COMMAND_FILTER_DEAD
ds@1952
   916
		&& pTarget->GetLifeState() != PLAYER_LIFE_DEAD)
ds@1952
   917
	{
ds@1952
   918
		return COMMAND_TARGET_NOT_DEAD;
ds@1952
   919
	}
ds@1952
   920
ds@1952
   921
	return COMMAND_TARGET_VALID;
ds@1952
   922
}
ds@1952
   923
ds@1952
   924
void PlayerManager::ProcessCommandTarget(cmd_target_info_t *info)
ds@1952
   925
{
ds@1952
   926
	CPlayer *pTarget, *pAdmin;
ds@1952
   927
	int max_clients, total = 0;
ds@1952
   928
ds@1952
   929
	max_clients = GetMaxClients();
ds@1952
   930
ds@1952
   931
	if (info->max_targets < 1)
ds@1952
   932
	{
ds@1952
   933
		info->reason = COMMAND_TARGET_NONE;
ds@1952
   934
		info->num_targets = 0;
ds@1952
   935
	}
ds@1952
   936
ds@1952
   937
	if (info->admin == 0)
ds@1952
   938
	{
ds@1952
   939
		pAdmin = NULL;
ds@1952
   940
	}
ds@1952
   941
	else
ds@1952
   942
	{
ds@1952
   943
		pAdmin = GetPlayerByIndex(info->admin);
ds@1952
   944
	}
ds@1952
   945
ds@1952
   946
	if (info->pattern[0] == '#')
ds@1952
   947
	{
ds@1952
   948
		int userid = atoi(&info->pattern[1]);
ds@1952
   949
		int client = GetClientOfUserId(userid);
ds@1952
   950
ds@1952
   951
		/* See if a valid userid matched */
ds@1952
   952
		if (client > 0)
ds@1952
   953
		{
ds@1952
   954
			IGamePlayer *pTarget = GetPlayerByIndex(client);
ds@1952
   955
			if (pTarget != NULL)
ds@1952
   956
			{
ds@1952
   957
				if ((info->reason = FilterCommandTarget(pAdmin, pTarget, info->flags)) == COMMAND_TARGET_VALID)
ds@1952
   958
				{
ds@1952
   959
					info->targets[0] = client;
ds@1952
   960
					info->num_targets = 1;
ds@1952
   961
					strncopy(info->target_name, pTarget->GetName(), info->target_name_maxlength);
ds@1952
   962
					info->target_name_style = COMMAND_TARGETNAME_RAW;
ds@1952
   963
				}
ds@1952
   964
				else
ds@1952
   965
				{
ds@1952
   966
					info->num_targets = 0;
ds@1952
   967
				}
ds@1952
   968
				return;
ds@1952
   969
			}
ds@1952
   970
		}
ds@1952
   971
ds@1952
   972
		/* Do we need to look for a steam id? */
ds@1952
   973
		if (strncmp(&info->pattern[1], "STEAM_", 6) == 0)
ds@1952
   974
		{
ds@1952
   975
			size_t p, len;
ds@1952
   976
			char new_pattern[256];
ds@1952
   977
ds@1952
   978
			strcpy(new_pattern, "STEAM_");
ds@1952
   979
			len = strlen(&info->pattern[7]);
ds@1952
   980
			for (p = 0; p < len; p++)
ds@1952
   981
			{
ds@1952
   982
				new_pattern[6 + p] = info->pattern[7 + p];
ds@1952
   983
				if (new_pattern[6 + p] == '_')
ds@1952
   984
				{
ds@1952
   985
					new_pattern[6 + p] = ':';
ds@1952
   986
				}
ds@1952
   987
			}
ds@1952
   988
			new_pattern[6 + p] = '\0';
ds@1952
   989
ds@1952
   990
			for (int i = 1; i <= max_clients; i++)
ds@1952
   991
			{
ds@1952
   992
				if ((pTarget = GetPlayerByIndex(i)) == NULL)
ds@1952
   993
				{
ds@1952
   994
					continue;
ds@1952
   995
				}
ds@1952
   996
				if (!pTarget->IsConnected() || !pTarget->IsAuthorized())
ds@1952
   997
				{
ds@1952
   998
					continue;
ds@1952
   999
				}
ds@1952
  1000
				if (strcmp(pTarget->GetAuthString(), new_pattern) == 0)
ds@1952
  1001
				{
ds@1952
  1002
					if ((info->reason = FilterCommandTarget(pAdmin, pTarget, info->flags))
ds@1952
  1003
						== COMMAND_TARGET_VALID)
ds@1952
  1004
					{
ds@1952
  1005
						info->targets[0] = i;
ds@1952
  1006
						info->num_targets = 1;
ds@1952
  1007
						strncopy(info->target_name, pTarget->GetName(), info->target_name_maxlength);
ds@1952
  1008
						info->target_name_style = COMMAND_TARGETNAME_RAW;
ds@1952
  1009
					}
ds@1952
  1010
					else
ds@1952
  1011
					{
ds@1952
  1012
						info->num_targets = 0;
ds@1952
  1013
					}
ds@1952
  1014
					return;
ds@1952
  1015
				}
ds@1952
  1016
			}
ds@1952
  1017
		}
ds@1952
  1018
ds@1952
  1019
		/* See if an exact name matches */
ds@1952
  1020
		for (int i = 1; i <= max_clients; i++)
ds@1952
  1021
		{
ds@1952
  1022
			if ((pTarget = GetPlayerByIndex(i)) == NULL)
ds@1952
  1023
			{
ds@1952
  1024
				continue;
ds@1952
  1025
			}
ds@1952
  1026
			if  (!pTarget->IsConnected())
ds@1952
  1027
			{
ds@1952
  1028
				continue;
ds@1952
  1029
			}
ds@1952
  1030
			if (strcmp(pTarget->GetName(), &info->pattern[1]) == 0)
ds@1952
  1031
			{
ds@1952
  1032
				if ((info->reason = FilterCommandTarget(pAdmin, pTarget, info->flags))
ds@1952
  1033
					== COMMAND_TARGET_VALID)
ds@1952
  1034
				{
ds@1952
  1035
					info->targets[0] = i;
ds@1952
  1036
					info->num_targets = 1;
ds@1952
  1037
					strncopy(info->target_name, pTarget->GetName(), info->target_name_maxlength);
ds@1952
  1038
					info->target_name_style = COMMAND_TARGETNAME_RAW;
ds@1952
  1039
				}
ds@1952
  1040
				else
ds@1952
  1041
				{
ds@1952
  1042
					info->num_targets = 0;
ds@1952
  1043
				}
ds@1952
  1044
				return;
ds@1952
  1045
			}
ds@1952
  1046
		}
ds@1952
  1047
	}
ds@1952
  1048
ds@1952
  1049
	if (strcmp(info->pattern, "@me") == 0 && info->admin != 0)
ds@1952
  1050
	{
ds@1952
  1051
		info->targets[0] = info->admin;
ds@1952
  1052
		info->num_targets = 1;
ds@1952
  1053
		strncopy(info->target_name, pAdmin->GetName(), info->target_name_maxlength);
ds@1952
  1054
		info->target_name_style = COMMAND_TARGETNAME_RAW;
ds@1952
  1055
		return;
ds@1952
  1056
	}
ds@1952
  1057
ds@1952
  1058
	if ((info->flags & COMMAND_FILTER_NO_MULTI) != COMMAND_FILTER_NO_MULTI)
ds@1952
  1059
	{
ds@1952
  1060
		bool is_multi = false;
ds@1952
  1061
		bool bots_only = false;
ds@1952
  1062
		int skip_client = -1;
ds@1952
  1063
ds@1952
  1064
		if (strcmp(info->pattern, "@all") == 0)
ds@1952
  1065
		{
ds@1952
  1066
			is_multi = true;
ds@1952
  1067
			strncopy(info->target_name, "all players", info->target_name_maxlength);
ds@1952
  1068
			info->target_name_style = COMMAND_TARGETNAME_ML;
ds@1952
  1069
		}
ds@1952
  1070
		else if (strcmp(info->pattern, "@dead") == 0)
ds@1952
  1071
		{
ds@1952
  1072
			is_multi = true;
ds@1952
  1073
			if ((info->flags & COMMAND_FILTER_ALIVE) == COMMAND_FILTER_ALIVE)
ds@1952
  1074
			{
ds@1952
  1075
				info->num_targets = 0;
ds@1952
  1076
				info->reason = COMMAND_TARGET_NOT_ALIVE;
ds@1952
  1077
				return;
ds@1952
  1078
			}
ds@1952
  1079
			info->flags |= COMMAND_FILTER_DEAD;
ds@1952
  1080
			strncopy(info->target_name, "all dead players", info->target_name_maxlength);
ds@1952
  1081
			info->target_name_style = COMMAND_TARGETNAME_ML;
ds@1952
  1082
		}
ds@1952
  1083
		else if (strcmp(info->pattern, "@alive") == 0)
ds@1952
  1084
		{
ds@1952
  1085
			is_multi = true;
ds@1952
  1086
			if ((info->flags & COMMAND_FILTER_DEAD) == COMMAND_FILTER_DEAD)
ds@1952
  1087
			{
ds@1952
  1088
				info->num_targets = 0;
ds@1952
  1089
				info->reason = COMMAND_TARGET_NOT_DEAD;
ds@1952
  1090
				return;
ds@1952
  1091
			}
ds@1952
  1092
			strncopy(info->target_name, "all alive players", info->target_name_maxlength);
ds@1952
  1093
			info->target_name_style = COMMAND_TARGETNAME_ML;
ds@1952
  1094
			info->flags |= COMMAND_FILTER_ALIVE;
ds@1952
  1095
		}
ds@1952
  1096
		else if (strcmp(info->pattern, "@bots") == 0)
ds@1952
  1097
		{
ds@1952
  1098
			is_multi = true;
ds@1952
  1099
			if ((info->flags & COMMAND_FILTER_NO_BOTS) == COMMAND_FILTER_NO_BOTS)
ds@1952
  1100
			{
ds@1952
  1101
				info->num_targets = 0;
ds@1952
  1102
				info->reason = COMMAND_FILTER_NO_BOTS;
ds@1952
  1103
				return;
ds@1952
  1104
			}
ds@1952
  1105
			strncopy(info->target_name, "all bots", info->target_name_maxlength);
ds@1952
  1106
			info->target_name_style = COMMAND_TARGETNAME_ML;
ds@1952
  1107
			bots_only = true;
ds@1952
  1108
		}
ds@1952
  1109
		else if (strcmp(info->pattern, "@humans") == 0)
ds@1952
  1110
		{
ds@1952
  1111
			is_multi = true;
ds@1952
  1112
			strncopy(info->target_name, "all humans", info->target_name_maxlength);
ds@1952
  1113
			info->target_name_style = COMMAND_TARGETNAME_ML;
ds@1952
  1114
			info->flags |= COMMAND_FILTER_NO_BOTS;
ds@1952
  1115
		}
ds@1952
  1116
		else if (strcmp(info->pattern, "@!me") == 0)
ds@1952
  1117
		{
ds@1952
  1118
			is_multi = true;
ds@1952
  1119
			strncopy(info->target_name, "all players", info->target_name_maxlength);
ds@1952
  1120
			info->target_name_style = COMMAND_TARGETNAME_ML;
ds@1952
  1121
			skip_client = info->admin;
ds@1952
  1122
		}
ds@1952
  1123
ds@1952
  1124
		if (is_multi)
ds@1952
  1125
		{
ds@1952
  1126
			for (int i = 1; i <= max_clients && total < info->max_targets; i++)
ds@1952
  1127
			{
ds@1952
  1128
				if ((pTarget = GetPlayerByIndex(i)) == NULL)
ds@1952
  1129
				{
ds@1952
  1130
					continue;
ds@1952
  1131
				}
ds@1952
  1132
				if (FilterCommandTarget(pAdmin, pTarget, info->flags) > 0)
ds@1952
  1133
				{
ds@1952
  1134
					if ((!bots_only || pTarget->IsFakeClient())
ds@1952
  1135
						&& skip_client != i)
ds@1952
  1136
					{
ds@1952
  1137
						info->targets[total++] = i;
ds@1952
  1138
					}
ds@1952
  1139
				}
ds@1952
  1140
			}
ds@1952
  1141
ds@1952
  1142
			info->num_targets = total;
ds@1952
  1143
			info->reason = (info->num_targets) ? COMMAND_TARGET_VALID : COMMAND_TARGET_EMPTY_FILTER;
ds@1952
  1144
			return;
ds@1952
  1145
		}
ds@1952
  1146
	}
ds@1952
  1147
ds@1952
  1148
	List<ICommandTargetProcessor *>::iterator iter;
ds@1952
  1149
	for (iter = target_processors.begin(); iter != target_processors.end(); iter++)
ds@1952
  1150
	{
ds@1952
  1151
		ICommandTargetProcessor *pProcessor = (*iter);
ds@1952
  1152
		if (pProcessor->ProcessCommandTarget(info))
ds@1952
  1153
		{
ds@1952
  1154
			return;
ds@1952
  1155
		}
ds@1952
  1156
	}
ds@1952
  1157
ds@1952
  1158
	/* Check partial names */
ds@1952
  1159
	int found_client = 0;
ds@1952
  1160
	CPlayer *pFoundClient = NULL;
ds@1952
  1161
	for (int i = 1; i <= max_clients; i++)
ds@1952
  1162
	{
ds@1952
  1163
		if ((pTarget = GetPlayerByIndex(i)) == NULL)
ds@1952
  1164
		{
ds@1952
  1165
			continue;
ds@1952
  1166
		}
ds@1952
  1167
ds@1952
  1168
		if (stristr(pTarget->GetName(), info->pattern) != NULL)
ds@1952
  1169
		{
ds@1952
  1170
			if (found_client)
ds@1952
  1171
			{
ds@1952
  1172
				info->num_targets = 0;
ds@1952
  1173
				info->reason = COMMAND_TARGET_AMBIGUOUS;
ds@1952
  1174
				return;
ds@1952
  1175
			}
ds@1952
  1176
			else
ds@1952
  1177
			{
ds@1952
  1178
				found_client = i;
ds@1952
  1179
				pFoundClient = pTarget;
ds@1952
  1180
			}
ds@1952
  1181
		}
ds@1952
  1182
	}
ds@1952
  1183
ds@1952
  1184
	if (found_client)
ds@1952
  1185
	{
ds@1952
  1186
		if ((info->reason = FilterCommandTarget(pAdmin, pFoundClient, info->flags)) 
ds@1952
  1187
			== COMMAND_TARGET_VALID)
ds@1952
  1188
		{
ds@1952
  1189
			info->targets[0] = found_client;
ds@1952
  1190
			info->num_targets = 1;
ds@1952
  1191
			strncopy(info->target_name, pFoundClient->GetName(), info->target_name_maxlength);
ds@1952
  1192
			info->target_name_style = COMMAND_TARGETNAME_RAW;
ds@1952
  1193
		}
ds@1952
  1194
		else
ds@1952
  1195
		{
ds@1952
  1196
			info->num_targets = 0;
ds@1952
  1197
		}
ds@1952
  1198
	}
ds@1952
  1199
	else
ds@1952
  1200
	{
ds@1952
  1201
		info->num_targets = 0;
ds@1952
  1202
		info->reason = COMMAND_TARGET_NONE;
ds@1952
  1203
	}
ds@1952
  1204
}
ds@1952
  1205
ds@1952
  1206
/*******************
ds@1952
  1207
 *** PLAYER CODE ***
ds@1952
  1208
 *******************/
ds@1952
  1209
ds@1952
  1210
CPlayer::CPlayer()
ds@1952
  1211
{
ds@1952
  1212
	m_IsConnected = false;
ds@1952
  1213
	m_IsInGame = false;
ds@1952
  1214
	m_IsAuthorized = false;
ds@1952
  1215
	m_pEdict = NULL;
ds@1952
  1216
	m_Admin = INVALID_ADMIN_ID;
ds@1952
  1217
	m_TempAdmin = false;
ds@1952
  1218
	m_Info = NULL;
ds@1952
  1219
	m_bAdminCheckSignalled = false;
ds@1952
  1220
	m_bIsInKickQueue = false;
ds@1952
  1221
	m_LastPassword.clear();
dvander@2042
  1222
	m_LangId = SOURCEMOD_LANGUAGE_ENGLISH;
ds@2237
  1223
	m_bFakeClient = false;
ds@1952
  1224
}
ds@1952
  1225
ds@1952
  1226
void CPlayer::Initialize(const char *name, const char *ip, edict_t *pEntity)
ds@1952
  1227
{
ds@1952
  1228
	m_IsConnected = true;
ds@1952
  1229
	m_Name.assign(name);
ds@1952
  1230
	m_Ip.assign(ip);
ds@1952
  1231
	m_pEdict = pEntity;
ds@1952
  1232
	m_iIndex = engine->IndexOfEdict(pEntity);
ds@1952
  1233
	m_LangId = g_Translator.GetServerLanguage();
ds@1952
  1234
ds@1952
  1235
	char ip2[24], *ptr;
ds@1952
  1236
	strncopy(ip2, ip, sizeof(ip2));
ds@1952
  1237
	if ((ptr = strchr(ip2, ':')) != NULL)
ds@1952
  1238
	{
ds@1952
  1239
		*ptr = '\0';
ds@1952
  1240
	}
ds@1952
  1241
	m_IpNoPort.assign(ip2);
ds@1952
  1242
}
ds@1952
  1243
ds@1952
  1244
void CPlayer::Connect()
ds@1952
  1245
{
ds@1952
  1246
	if (m_IsInGame)
ds@1952
  1247
	{
ds@1952
  1248
		return;
ds@1952
  1249
	}
ds@1952
  1250
ds@1952
  1251
	m_IsInGame = true;
ds@1952
  1252
ds@1952
  1253
	const char *var = g_Players.GetPassInfoVar();
ds@1952
  1254
	int client = engine->IndexOfEdict(m_pEdict);
ds@1952
  1255
	if (var[0] != '\0')
ds@1952
  1256
	{
ds@1952
  1257
		const char *pass = engine->GetClientConVarValue(client, var);
ds@1952
  1258
		m_LastPassword.assign(pass ? pass : "");
ds@1952
  1259
	}
ds@1952
  1260
	else
ds@1952
  1261
	{
ds@1952
  1262
		m_LastPassword.assign("");
ds@1952
  1263
	}
ds@1952
  1264
}
ds@1952
  1265
ds@1952
  1266
void CPlayer::Authorize(const char *steamid)
ds@1952
  1267
{
ds@1952
  1268
	if (m_IsAuthorized)
ds@1952
  1269
	{
ds@1952
  1270
		return;
ds@1952
  1271
	}
ds@1952
  1272
ds@1952
  1273
	m_IsAuthorized = true;
ds@1952
  1274
	m_AuthID.assign(steamid);
ds@1952
  1275
}
ds@1952
  1276
ds@1952
  1277
void CPlayer::Disconnect()
ds@1952
  1278
{
ds@1952
  1279
	DumpAdmin(false);
ds@1952
  1280
	m_IsConnected = false;
ds@1952
  1281
	m_IsInGame = false;
ds@1952
  1282
	m_IsAuthorized = false;
ds@1952
  1283
	m_Name.clear();
ds@1952
  1284
	m_Ip.clear();
ds@1952
  1285
	m_AuthID.clear();
ds@1952
  1286
	m_pEdict = NULL;
ds@1952
  1287
	m_Info = NULL;
ds@1952
  1288
	m_bAdminCheckSignalled = false;
ds@1952
  1289
	m_UserId = -1;
ds@1952
  1290
	m_bIsInKickQueue = false;
ds@2237
  1291
	m_bFakeClient = false;
ds@1952
  1292
}
ds@1952
  1293
ds@1952
  1294
void CPlayer::SetName(const char *name)
ds@1952
  1295
{
ds@1952
  1296
	m_Name.assign(name);
ds@1952
  1297
}
ds@1952
  1298
ds@1952
  1299
const char *CPlayer::GetName()
ds@1952
  1300
{
ds@1952
  1301
	if (m_Info && m_pEdict->GetUnknown())
ds@1952
  1302
	{
ds@1952
  1303
		return m_Info->GetName();
ds@1952
  1304
	}
ds@1952
  1305
	
ds@1952
  1306
	return m_Name.c_str();
ds@1952
  1307
}
ds@1952
  1308
ds@1952
  1309
const char *CPlayer::GetIPAddress()
ds@1952
  1310
{
ds@1952
  1311
	return m_Ip.c_str();
ds@1952
  1312
}
ds@1952
  1313
ds@1952
  1314
const char *CPlayer::GetAuthString()
ds@1952
  1315
{
ds@1952
  1316
	return m_AuthID.c_str();
ds@1952
  1317
}
ds@1952
  1318
ds@1952
  1319
edict_t *CPlayer::GetEdict()
ds@1952
  1320
{
ds@1952
  1321
	return m_pEdict;
ds@1952
  1322
}
ds@1952
  1323
ds@1952
  1324
bool CPlayer::IsInGame()
ds@1952
  1325
{
ds@1952
  1326
	return m_IsInGame && (m_pEdict->GetUnknown() != NULL);
ds@1952
  1327
}
ds@1952
  1328
ds@1952
  1329
bool CPlayer::WasCountedAsInGame()
ds@1952
  1330
{
ds@1952
  1331
	return m_IsInGame;
ds@1952
  1332
}
ds@1952
  1333
ds@1952
  1334
bool CPlayer::IsConnected()
ds@1952
  1335
{
ds@1952
  1336
	return m_IsConnected;
ds@1952
  1337
}
ds@1952
  1338
ds@1952
  1339
bool CPlayer::IsAuthorized()
ds@1952
  1340
{
ds@1952
  1341
	return m_IsAuthorized;
ds@1952
  1342
}
ds@1952
  1343
ds@1952
  1344
IPlayerInfo *CPlayer::GetPlayerInfo()
ds@1952
  1345
{
ds@1952
  1346
	if (m_pEdict->GetUnknown())
ds@1952
  1347
	{
ds@1952
  1348
		return m_Info;
ds@1952
  1349
	}
ds@1952
  1350
ds@1952
  1351
	return NULL;
ds@1952
  1352
}
ds@1952
  1353
ds@1952
  1354
bool CPlayer::IsFakeClient()
ds@1952
  1355
{
ds@2237
  1356
	return m_bFakeClient;
ds@1952
  1357
}
ds@1952
  1358
ds@1952
  1359
void CPlayer::SetAdminId(AdminId id, bool temporary)
ds@1952
  1360
{
ds@1952
  1361
	if (!m_IsConnected)
ds@1952
  1362
	{
ds@1952
  1363
		return;
ds@1952
  1364
	}
ds@1952
  1365
ds@1952
  1366
	DumpAdmin(false);
ds@1952
  1367
ds@1952
  1368
	m_Admin = id;
ds@1952
  1369
	m_TempAdmin = temporary;
ds@1952
  1370
}
ds@1952
  1371
ds@1952
  1372
AdminId CPlayer::GetAdminId()
ds@1952
  1373
{
ds@1952
  1374
	return m_Admin;
ds@1952
  1375
}
ds@1952
  1376
ds@1952
  1377
void CPlayer::DumpAdmin(bool deleting)
ds@1952
  1378
{
ds@1952
  1379
	if (m_Admin != INVALID_ADMIN_ID)
ds@1952
  1380
	{
ds@1952
  1381
		if (m_TempAdmin && !deleting)
ds@1952
  1382
		{
ds@1952
  1383
			g_Admins.InvalidateAdmin(m_Admin);
ds@1952
  1384
		}
ds@1952
  1385
		m_Admin = INVALID_ADMIN_ID;
ds@1952
  1386
		m_TempAdmin = false;
ds@1952
  1387
	}
ds@1952
  1388
}
ds@1952
  1389
ds@1952
  1390
void CPlayer::Kick(const char *str)
ds@1952
  1391
{
ds@1952
  1392
	MarkAsBeingKicked();
ds@1952
  1393
	INetChannel *pNetChan = static_cast<INetChannel *>(engine->GetPlayerNetInfo(m_iIndex));
ds@1952
  1394
	IClient *pClient = static_cast<IClient *>(pNetChan->GetMsgHandler());
ds@1952
  1395
	pClient->Disconnect("%s", str);
ds@1952
  1396
}
ds@1952
  1397
ds@1952
  1398
void CPlayer::Authorize_Post()
ds@1952
  1399
{
ds@1952
  1400
	if (m_IsInGame)
ds@1952
  1401
	{
ds@1952
  1402
		DoPostConnectAuthorization();
ds@1952
  1403
	}
ds@1952
  1404
}
ds@1952
  1405
ds@1952
  1406
void CPlayer::DoPostConnectAuthorization()
ds@1952
  1407
{
ds@1952
  1408
	bool delay = false;
ds@1952
  1409
ds@1952
  1410
	List<IClientListener *>::iterator iter;
ds@1952
  1411
	for (iter = g_Players.m_hooks.begin();
ds@1952
  1412
		 iter != g_Players.m_hooks.end();
ds@1952
  1413
		 iter++)
ds@1952
  1414
	{
ds@1952
  1415
		IClientListener *pListener = (*iter);
ds@1952
  1416
#if defined MIN_API_FOR_ADMINCALLS
ds@1952
  1417
		if (pListener->GetClientListenerVersion() < MIN_API_FOR_ADMINCALLS)
ds@1952
  1418
		{
ds@1952
  1419
			continue;
ds@1952
  1420
		}
ds@1952
  1421
#endif
ds@1952
  1422
		if (!pListener->OnClientPreAdminCheck(m_iIndex))
ds@1952
  1423
		{
ds@1952
  1424
			delay = true;
ds@1952
  1425
		}
ds@1952
  1426
	}
ds@1952
  1427
	
ds@1952
  1428
	cell_t result = 0;
ds@1952
  1429
	PreAdminCheck->PushCell(m_iIndex);
ds@1952
  1430
	PreAdminCheck->Execute(&result);
ds@1952
  1431
ds@1952
  1432
	/* Defer, for better or worse */
ds@1952
  1433
	if (delay || (ResultType)result >= Pl_Handled)
ds@1952
  1434
	{
ds@1952
  1435
		return;
ds@1952
  1436
	}
ds@1952
  1437
ds@1952
  1438
	/* Sanity check */
ds@1952
  1439
	if (!IsConnected())
ds@1952
  1440
	{
ds@1952
  1441
		return;
ds@1952
  1442
	}
ds@1952
  1443
ds@1952
  1444
	/* Otherwise, go ahead and do admin checks */
ds@1952
  1445
	DoBasicAdminChecks();
ds@1952
  1446
ds@1952
  1447
	/* Send the notification out */
ds@1952
  1448
	NotifyPostAdminChecks();
ds@1952
  1449
}
ds@1952
  1450
ds@1952
  1451
bool CPlayer::RunAdminCacheChecks()
ds@1952
  1452
{
ds@1952
  1453
	AdminId old_id = GetAdminId();
ds@1952
  1454
ds@1952
  1455
	DoBasicAdminChecks();
ds@1952
  1456
ds@1952
  1457
	return (GetAdminId() != old_id);
ds@1952
  1458
}
ds@1952
  1459
ds@1952
  1460
void CPlayer::NotifyPostAdminChecks()
ds@1952
  1461
{
ds@1952
  1462
	if (m_bAdminCheckSignalled)
ds@1952
  1463
	{
ds@1952
  1464
		return;
ds@1952
  1465
	}
ds@1952
  1466
ds@1952
  1467
	/* Block beforehand so they can't double-call */
ds@1952
  1468
	m_bAdminCheckSignalled = true;
ds@1952
  1469
ds@1952
  1470
	List<IClientListener *>::iterator iter;
ds@1952
  1471
	for (iter = g_Players.m_hooks.begin();
ds@1952
  1472
		iter != g_Players.m_hooks.end();
ds@1952
  1473
		iter++)
ds@1952
  1474
	{
ds@1952
  1475
		IClientListener *pListener = (*iter);
ds@1952
  1476
#if defined MIN_API_FOR_ADMINCALLS
ds@1952
  1477
		if (pListener->GetClientListenerVersion() < MIN_API_FOR_ADMINCALLS)
ds@1952
  1478
		{
ds@1952
  1479
			continue;
ds@1952
  1480
		}
ds@1952
  1481
#endif
ds@1952
  1482
		pListener->OnClientPostAdminCheck(m_iIndex);
ds@1952
  1483
	}
ds@1952
  1484
ds@1952
  1485
	PostAdminFilter->PushCell(m_iIndex);
ds@1952
  1486
	PostAdminFilter->Execute(NULL);
ds@1952
  1487
ds@1952
  1488
	PostAdminCheck->PushCell(m_iIndex);
ds@1952
  1489
	PostAdminCheck->Execute(NULL);
ds@1952
  1490
}
ds@1952
  1491
ds@1952
  1492
void CPlayer::DoBasicAdminChecks()
ds@1952
  1493
{
ds@1952
  1494
	if (GetAdminId() != INVALID_ADMIN_ID)
ds@1952
  1495
	{
ds@1952
  1496
		return;
ds@1952
  1497
	}
ds@1952
  1498
ds@1952
  1499
	/* First check the name */
ds@1952
  1500
	AdminId id;
ds@1952
  1501
	int client = engine->IndexOfEdict(m_pEdict);
ds@1952
  1502
ds@1952
  1503
	if ((id = g_Admins.FindAdminByIdentity("name", GetName())) != INVALID_ADMIN_ID)
ds@1952
  1504
	{
ds@1952
  1505
		if (!g_Players.CheckSetAdminName(client, this, id))
ds@1952
  1506
		{
ds@1952
  1507
			int userid = engine->GetPlayerUserId(m_pEdict);
ds@1952
  1508
			g_Timers.CreateTimer(&s_KickPlayerTimer, 0.1f, (void *)userid, 0);
ds@1952
  1509
		}
ds@1952
  1510
		return;
ds@1952
  1511
	}
ds@1952
  1512
	
ds@1952
  1513
	/* Check IP */
ds@1952
  1514
	if ((id = g_Admins.FindAdminByIdentity("ip", m_IpNoPort.c_str())) != INVALID_ADMIN_ID)
ds@1952
  1515
	{
ds@1952
  1516
		if (g_Players.CheckSetAdmin(client, this, id))
ds@1952
  1517
		{
ds@1952
  1518
			return;
ds@1952
  1519
		}
ds@1952
  1520
	}
ds@1952
  1521
ds@1952
  1522
	/* Check IP address */
ds@1952
  1523
	if ((id = g_Admins.FindAdminByIdentity("steam", m_AuthID.c_str())) != INVALID_ADMIN_ID)
ds@1952
  1524
	{
ds@1952
  1525
		if (g_Players.CheckSetAdmin(client, this, id))
ds@1952
  1526
		{
ds@1952
  1527
			return;
ds@1952
  1528
		}
ds@1952
  1529
	}
ds@1952
  1530
}
ds@1952
  1531
ds@1952
  1532
unsigned int CPlayer::GetLanguageId()
ds@1952
  1533
{
ds@1952
  1534
	return m_LangId;
ds@1952
  1535
}
ds@1952
  1536
ds@1952
  1537
int CPlayer::GetUserId()
ds@1952
  1538
{
ds@1952
  1539
	if (m_UserId == -1)
ds@1952
  1540
	{
ds@1952
  1541
		m_UserId = engine->GetPlayerUserId(GetEdict());
ds@1952
  1542
	}
ds@1952
  1543
ds@1952
  1544
	return m_UserId;
ds@1952
  1545
}
ds@1952
  1546
ds@1952
  1547
bool CPlayer::IsInKickQueue()
ds@1952
  1548
{
ds@1952
  1549
	return m_bIsInKickQueue;
ds@1952
  1550
}
ds@1952
  1551
ds@1952
  1552
void CPlayer::MarkAsBeingKicked()
ds@1952
  1553
{
ds@1952
  1554
	m_bIsInKickQueue = true;
ds@1952
  1555
}
ds@1952
  1556
ds@1952
  1557
int CPlayer::GetLifeState()
ds@1952
  1558
{
ds@1952
  1559
	if (lifestate_offset == -1)
ds@1952
  1560
	{
ds@1952
  1561
		if (!g_pGameConf->GetOffset("m_lifeState", &lifestate_offset))
ds@1952
  1562
		{
ds@1952
  1563
			lifestate_offset = -2;
ds@1952
  1564
		}
ds@1952
  1565
	}
ds@1952
  1566
ds@1952
  1567
	if (lifestate_offset < 0)
ds@1952
  1568
	{
ds@1952
  1569
		IPlayerInfo *info = GetPlayerInfo();
ds@1952
  1570
		if (info == NULL)
ds@1952
  1571
		{
ds@1952
  1572
			return PLAYER_LIFE_UNKNOWN;
ds@1952
  1573
		}
ds@1952
  1574
		return info->IsDead() ? PLAYER_LIFE_DEAD : PLAYER_LIFE_ALIVE;
ds@1952
  1575
	}
ds@1952
  1576
ds@1952
  1577
	if (m_pEdict == NULL)
ds@1952
  1578
	{
ds@1952
  1579
		return PLAYER_LIFE_UNKNOWN;
ds@1952
  1580
	}
ds@1952
  1581
ds@1952
  1582
	CBaseEntity *pEntity;
ds@1952
  1583
	IServerUnknown *pUnknown = m_pEdict->GetUnknown();
ds@1952
  1584
	if (pUnknown == NULL || (pEntity = pUnknown->GetBaseEntity()) == NULL)
ds@1952
  1585
	{
ds@1952
  1586
		return PLAYER_LIFE_UNKNOWN;
ds@1952
  1587
	}
ds@1952
  1588
ds@1952
  1589
	if (*((uint8_t *)pEntity + lifestate_offset) == LIFE_ALIVE)
ds@1952
  1590
	{
ds@1952
  1591
		return PLAYER_LIFE_ALIVE;
ds@1952
  1592
	}
ds@1952
  1593
	else
ds@1952
  1594
	{
ds@1952
  1595
		return PLAYER_LIFE_DEAD;
ds@1952
  1596
	}
ds@1952
  1597
}