core/ConCmdManager.cpp
author Borja Ferrer <faluco@alliedmods.net>
Sun May 04 15:52:34 2008 +0000 (2008-05-04)
changeset 2038 a317edffcfa7
parent 1996 3f0cd604812b
child 2042 4a2faafca477
permissions -rw-r--r--
removed these functions in tf2 for now to avoid more 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 "ConCmdManager.h"
ds@1952
    33
#include "sm_srvcmds.h"
ds@1952
    34
#include "AdminCache.h"
ds@1952
    35
#include "sm_stringutil.h"
ds@1952
    36
#include "PlayerManager.h"
ds@1952
    37
#include "Translator.h"
ds@1952
    38
#include "HalfLife2.h"
ds@1952
    39
#include "ChatTriggers.h"
ds@1952
    40
ds@1952
    41
ConCmdManager g_ConCmds;
ds@1952
    42
ds@1952
    43
#if defined ORANGEBOX_BUILD
ds@1952
    44
	SH_DECL_HOOK1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
ds@1952
    45
#else
ds@1952
    46
	SH_DECL_HOOK0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
ds@1952
    47
#endif
ds@1952
    48
ds@1952
    49
SH_DECL_HOOK1_void(IServerGameClients, SetCommandClient, SH_NOATTRIB, false, int);
ds@1952
    50
ds@1952
    51
struct PlCmdInfo
ds@1952
    52
{
ds@1952
    53
	ConCmdInfo *pInfo;
ds@1952
    54
	CmdHook *pHook;
ds@1952
    55
	CmdType type;
ds@1952
    56
};
ds@1952
    57
typedef List<PlCmdInfo> CmdList;
ds@1952
    58
ds@1952
    59
void AddToPlCmdList(CmdList *pList, const PlCmdInfo &info);
ds@1952
    60
ds@1952
    61
ConCmdManager::ConCmdManager() : m_Strings(1024)
ds@1952
    62
{
ds@1952
    63
	m_pCmds = sm_trie_create();
ds@1952
    64
	m_pCmdGrps = sm_trie_create();
ds@1952
    65
	m_CmdClient = 0;
ds@1952
    66
}
ds@1952
    67
ds@1952
    68
ConCmdManager::~ConCmdManager()
ds@1952
    69
{
ds@1952
    70
	sm_trie_destroy(m_pCmds);
ds@1952
    71
	sm_trie_destroy(m_pCmdGrps);
ds@1952
    72
}
ds@1952
    73
ds@1952
    74
void ConCmdManager::OnSourceModAllInitialized()
ds@1952
    75
{
ds@1952
    76
	g_PluginSys.AddPluginsListener(this);
ds@1952
    77
	g_RootMenu.AddRootConsoleCommand("cmds", "List console commands", this);
ds@1952
    78
	SH_ADD_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, serverClients, this, &ConCmdManager::SetCommandClient, false);
ds@1952
    79
}
ds@1952
    80
ds@1952
    81
void ConCmdManager::OnSourceModShutdown()
ds@1952
    82
{
ds@1952
    83
	/* All commands should already be removed by the time we're done */
ds@1952
    84
	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, serverClients, this, &ConCmdManager::SetCommandClient, false);
ds@1952
    85
	g_RootMenu.RemoveRootConsoleCommand("cmds", this);
ds@1952
    86
}
ds@1952
    87
ds@1952
    88
void ConCmdManager::OnUnlinkConCommandBase(ConCommandBase *pBase, const char *name, bool is_read_safe)
ds@1952
    89
{
ds@1952
    90
	/* Whoa, first get its information struct */
ds@1952
    91
	ConCmdInfo *pInfo;
ds@1952
    92
ds@1952
    93
	if (!sm_trie_retrieve(m_pCmds, name, (void **)&pInfo))
ds@1952
    94
	{
ds@1952
    95
		return;
ds@1952
    96
	}
ds@1952
    97
ds@1952
    98
	RemoveConCmds(pInfo->srvhooks);
ds@1952
    99
	RemoveConCmds(pInfo->conhooks);
ds@1952
   100
ds@1952
   101
	RemoveConCmd(pInfo, name, is_read_safe, false);
ds@1952
   102
}
ds@1952
   103
ds@1952
   104
void ConCmdManager::RemoveConCmds(List<CmdHook *> &cmdlist)
ds@1952
   105
{
ds@1952
   106
	List<CmdHook *>::iterator iter = cmdlist.begin();
ds@1952
   107
ds@1952
   108
	while (iter != cmdlist.end())
ds@1952
   109
	{
ds@1952
   110
		CmdHook *pHook = (*iter);
ds@1952
   111
		IPluginContext *pContext = pHook->pf->GetParentContext();
ds@1952
   112
		IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
ds@1952
   113
		CmdList *pList = NULL;
ds@1952
   114
		
ds@1952
   115
		//gaben
ds@1952
   116
		if (!pPlugin->GetProperty("CommandList", (void **)&pList, false) || !pList)
ds@1952
   117
		{
ds@1952
   118
			continue;
ds@1952
   119
		}
ds@1952
   120
ds@1952
   121
		CmdList::iterator p_iter = pList->begin();
ds@1952
   122
		while (p_iter != pList->end())
ds@1952
   123
		{
ds@1952
   124
			PlCmdInfo &cmd = (*p_iter);
ds@1952
   125
			if (cmd.pHook == pHook)
ds@1952
   126
			{
ds@1952
   127
				p_iter = pList->erase(p_iter);
ds@1952
   128
			}
ds@1952
   129
			else
ds@1952
   130
			{
ds@1952
   131
				p_iter++;
ds@1952
   132
			}
ds@1952
   133
		}
ds@1952
   134
ds@1952
   135
		delete pHook->pAdmin;
ds@1952
   136
		delete pHook;
ds@1952
   137
		
ds@1952
   138
		iter = cmdlist.erase(iter);
ds@1952
   139
	}
ds@1952
   140
}
ds@1952
   141
ds@1952
   142
void ConCmdManager::RemoveConCmds(List<CmdHook *> &cmdlist, IPluginContext *pContext)
ds@1952
   143
{
ds@1952
   144
	List<CmdHook *>::iterator iter = cmdlist.begin();
ds@1952
   145
	CmdHook *pHook;
ds@1952
   146
ds@1952
   147
	while (iter != cmdlist.end())
ds@1952
   148
	{
ds@1952
   149
		pHook = (*iter);
ds@1952
   150
		if (pHook->pf->GetParentContext() == pContext)
ds@1952
   151
		{
ds@1952
   152
			delete pHook->pAdmin;
ds@1952
   153
			delete pHook;
ds@1952
   154
			iter = cmdlist.erase(iter);
ds@1952
   155
		}
ds@1952
   156
		else
ds@1952
   157
		{
ds@1952
   158
			iter++;
ds@1952
   159
		}
ds@1952
   160
	}
ds@1952
   161
}
ds@1952
   162
ds@1952
   163
void ConCmdManager::OnPluginDestroyed(IPlugin *plugin)
ds@1952
   164
{
ds@1952
   165
	CmdList *pList;
ds@1952
   166
	List<ConCmdInfo *> removed;
ds@1952
   167
	if (plugin->GetProperty("CommandList", (void **)&pList, true))
ds@1952
   168
	{
ds@1952
   169
		IPluginContext *pContext = plugin->GetBaseContext();
ds@1952
   170
		CmdList::iterator iter;
ds@1952
   171
		/* First pass!
ds@1952
   172
	 	 * Only bother if there's an actual command list on this plugin... 
ds@1952
   173
	 	 */
ds@1952
   174
		for (iter=pList->begin();
ds@1952
   175
			iter!=pList->end();
ds@1952
   176
			iter++)
ds@1952
   177
		{
ds@1952
   178
			PlCmdInfo &cmd = (*iter);
ds@1952
   179
			ConCmdInfo *pInfo = cmd.pInfo;
ds@1952
   180
ds@1952
   181
			/* Has this chain already been fully cleaned/removed? */
ds@1952
   182
			if (removed.find(pInfo) != removed.end())
ds@1952
   183
			{
ds@1952
   184
				continue;
ds@1952
   185
			}
ds@1952
   186
ds@1952
   187
			/* Remove any hooks from us on this command */
ds@1952
   188
			RemoveConCmds(pInfo->conhooks, pContext);
ds@1952
   189
			RemoveConCmds(pInfo->srvhooks, pContext);
ds@1952
   190
ds@1952
   191
			/* See if there are still hooks */
ds@1952
   192
			if (pInfo->srvhooks.size())
ds@1952
   193
			{
ds@1952
   194
				continue;
ds@1952
   195
			}
ds@1952
   196
			if (pInfo->conhooks.size())
ds@1952
   197
			{
ds@1952
   198
				continue;
ds@1952
   199
			}
ds@1952
   200
ds@1952
   201
			/* Remove the command, it should be safe now */
ds@1952
   202
			RemoveConCmd(pInfo, pInfo->pCmd->GetName(), true, true);
ds@1952
   203
			removed.push_back(pInfo);
ds@1952
   204
		}
ds@1952
   205
		delete pList;
ds@1952
   206
	}
ds@1952
   207
}
ds@1952
   208
#if defined ORANGEBOX_BUILD
ds@1952
   209
void CommandCallback(const CCommand &command)
ds@1952
   210
{
ds@1952
   211
#else
ds@1952
   212
void CommandCallback()
ds@1952
   213
{
ds@1952
   214
	CCommand command;
ds@1952
   215
#endif
ds@1952
   216
ds@1952
   217
	g_HL2.PushCommandStack(&command);
ds@1952
   218
ds@1952
   219
	g_ConCmds.InternalDispatch(command);
ds@1952
   220
ds@1952
   221
	g_HL2.PopCommandStack();
ds@1952
   222
}
ds@1952
   223
ds@1952
   224
void ConCmdManager::SetCommandClient(int client)
ds@1952
   225
{
ds@1952
   226
	m_CmdClient = client + 1;
ds@1952
   227
}
ds@1952
   228
ds@1952
   229
ResultType ConCmdManager::DispatchClientCommand(int client, const char *cmd, int args, ResultType type)
ds@1952
   230
{
ds@1952
   231
	ConCmdInfo *pInfo;
ds@1952
   232
	if (sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
ds@1952
   233
	{
ds@1952
   234
		cell_t result = type;
ds@1952
   235
		cell_t tempres = result;
ds@1952
   236
		List<CmdHook *>::iterator iter;
ds@1952
   237
		CmdHook *pHook;
ds@1952
   238
		for (iter=pInfo->conhooks.begin();
ds@1952
   239
			 iter!=pInfo->conhooks.end();
ds@1952
   240
			 iter++)
ds@1952
   241
		{
ds@1952
   242
			pHook = (*iter);
ds@1952
   243
			if (!pHook->pf->IsRunnable())
ds@1952
   244
			{
ds@1952
   245
				continue;
ds@1952
   246
			}
ds@1952
   247
			if (pHook->pAdmin && !CheckAccess(client, cmd, pHook->pAdmin))
ds@1952
   248
			{
ds@1952
   249
				if (result < Pl_Handled)
ds@1952
   250
				{
ds@1952
   251
					result = Pl_Handled;
ds@1952
   252
				}
ds@1952
   253
				continue;
ds@1952
   254
			}
ds@1952
   255
			pHook->pf->PushCell(client);
ds@1952
   256
			pHook->pf->PushCell(args);
ds@1952
   257
			if (pHook->pf->Execute(&tempres) == SP_ERROR_NONE)
ds@1952
   258
			{
ds@1952
   259
				if (tempres > result)
ds@1952
   260
				{
ds@1952
   261
					result = tempres;
ds@1952
   262
				}
ds@1952
   263
				if (result == Pl_Stop)
ds@1952
   264
				{
ds@1952
   265
					break;
ds@1952
   266
				}
ds@1952
   267
			}
ds@1952
   268
		}
ds@1952
   269
		type = (ResultType)result;
ds@1952
   270
	}
ds@1952
   271
ds@1952
   272
	return type;
ds@1952
   273
}
ds@1952
   274
ds@1952
   275
void ConCmdManager::InternalDispatch(const CCommand &command)
ds@1952
   276
{
ds@1952
   277
	int client = m_CmdClient;
ds@1952
   278
ds@1952
   279
	if (client)
ds@1952
   280
	{
ds@1952
   281
		CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
ds@1952
   282
		if (!pPlayer || !pPlayer->IsConnected())
ds@1952
   283
		{
ds@1952
   284
			return;
ds@1952
   285
		}
ds@1952
   286
	}
ds@1952
   287
ds@1952
   288
	/**
ds@1952
   289
	 * Note: Console commands will EITHER go through IServerGameDLL::ClientCommand,
ds@1952
   290
	 * OR this dispatch.  They will NEVER go through both.
ds@1952
   291
	 * -- 
ds@1952
   292
	 * Whether or not it goes through the callback is determined by FCVAR_GAMEDLL
ds@1952
   293
	 */
ds@1952
   294
	const char *cmd = g_HL2.CurrentCommandName();
ds@1952
   295
ds@1952
   296
	ConCmdInfo *pInfo;
ds@1952
   297
	if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
ds@1952
   298
	{
ds@1952
   299
		return;
ds@1952
   300
	}
ds@1952
   301
ds@1952
   302
	/* This is a hack to prevent say triggers from firing on messages that were 
ds@1952
   303
	 * blocked because of flooding.  We won't remove this, but the hack will get 
ds@1952
   304
	 * "nicer" when we expose explicit say hooks.
ds@1952
   305
	 */
ds@1952
   306
	if (g_ChatTriggers.WasFloodedMessage())
ds@1952
   307
	{
ds@1952
   308
		return;
ds@1952
   309
	}
ds@1952
   310
ds@1952
   311
	cell_t result = Pl_Continue;
ds@1952
   312
	int args = command.ArgC() - 1;
ds@1952
   313
ds@1952
   314
	List<CmdHook *>::iterator iter;
ds@1952
   315
	CmdHook *pHook;
ds@1952
   316
ds@1952
   317
	/* Execute server-only commands if viable */
ds@1952
   318
	if (client == 0 && pInfo->srvhooks.size())
ds@1952
   319
	{
ds@1952
   320
		cell_t tempres = result;
ds@1952
   321
		for (iter=pInfo->srvhooks.begin();
ds@1952
   322
			 iter!=pInfo->srvhooks.end();
ds@1952
   323
			 iter++)
ds@1952
   324
		{
ds@1952
   325
			pHook = (*iter);
ds@1952
   326
			if (!pHook->pf->IsRunnable())
ds@1952
   327
			{
ds@1952
   328
				continue;
ds@1952
   329
			}
ds@1952
   330
			pHook->pf->PushCell(args);
ds@1952
   331
			if (pHook->pf->Execute(&tempres) == SP_ERROR_NONE)
ds@1952
   332
			{
ds@1952
   333
				if (tempres > result)
ds@1952
   334
				{
ds@1952
   335
					result = tempres;
ds@1952
   336
				}
ds@1952
   337
				if (result == Pl_Stop)
ds@1952
   338
				{
ds@1952
   339
					break;
ds@1952
   340
				}
ds@1952
   341
			}
ds@1952
   342
		}
ds@1952
   343
	
ds@1952
   344
		/* Check if there's an early stop */
ds@1952
   345
		if (result >= Pl_Stop)
ds@1952
   346
		{
ds@1952
   347
			if (!pInfo->sourceMod)
ds@1952
   348
			{
ds@1952
   349
				RETURN_META(MRES_SUPERCEDE);
ds@1952
   350
			}
ds@1952
   351
			return;
ds@1952
   352
		}
ds@1952
   353
	}
ds@1952
   354
ds@1952
   355
	/* Execute console commands */
ds@1952
   356
	if (pInfo->conhooks.size())
ds@1952
   357
	{
ds@1952
   358
		cell_t tempres = result;
ds@1952
   359
		for (iter=pInfo->conhooks.begin();
ds@1952
   360
			iter!=pInfo->conhooks.end();
ds@1952
   361
			iter++)
ds@1952
   362
		{
ds@1952
   363
			pHook = (*iter);
ds@1952
   364
			if (!pHook->pf->IsRunnable())
ds@1952
   365
			{
ds@1952
   366
				continue;
ds@1952
   367
			}
ds@1952
   368
			if (client 
ds@1952
   369
				&& pHook->pAdmin
ds@1952
   370
				&& !CheckAccess(client, cmd, pHook->pAdmin))
ds@1952
   371
			{
ds@1952
   372
				if (result < Pl_Handled)
ds@1952
   373
				{
ds@1952
   374
					result = Pl_Handled;
ds@1952
   375
				}
ds@1952
   376
				continue;
ds@1952
   377
			}
ds@1952
   378
ds@1952
   379
			/* On a listen server, sometimes the server host's client index can be set as 0.
ds@1952
   380
			 * So index 1 is passed to the command callback to correct this potential problem.
ds@1952
   381
			 */
dvander@1996
   382
			if (!engine->IsDedicatedServer())
ds@1952
   383
			{
dvander@1996
   384
				client = g_Players.ListenClient();
ds@1952
   385
			}
ds@1952
   386
dvander@1996
   387
			pHook->pf->PushCell(client);
ds@1952
   388
			pHook->pf->PushCell(args);
dvander@1996
   389
ds@1952
   390
			if (pHook->pf->Execute(&tempres) == SP_ERROR_NONE)
ds@1952
   391
			{
ds@1952
   392
				if (tempres > result)
ds@1952
   393
				{
ds@1952
   394
					result = tempres;
ds@1952
   395
				}
ds@1952
   396
				if (result == Pl_Stop)
ds@1952
   397
				{
ds@1952
   398
					break;
ds@1952
   399
				}
ds@1952
   400
			}
ds@1952
   401
		}
ds@1952
   402
	}
ds@1952
   403
ds@1952
   404
	if (result >= Pl_Handled)
ds@1952
   405
	{
ds@1952
   406
		if (!pInfo->sourceMod)
ds@1952
   407
		{
ds@1952
   408
			RETURN_META(MRES_SUPERCEDE);
ds@1952
   409
		}
ds@1952
   410
		return;
ds@1952
   411
	}
ds@1952
   412
}
ds@1952
   413
ds@1952
   414
bool ConCmdManager::CheckCommandAccess(int client, const char *cmd, FlagBits cmdflags)
ds@1952
   415
{
ds@1952
   416
	if (cmdflags == 0 || client == 0)
ds@1952
   417
	{
ds@1952
   418
		return true;
ds@1952
   419
	}
ds@1952
   420
ds@1952
   421
	/* If running listen server, then client 1 is the server host and should have 'root' access */
ds@1952
   422
	if (client == 1 && !engine->IsDedicatedServer())
ds@1952
   423
	{
ds@1952
   424
		return true;
ds@1952
   425
	}
ds@1952
   426
ds@1952
   427
	CPlayer *player = g_Players.GetPlayerByIndex(client);
ds@1952
   428
	if (!player 
ds@1952
   429
		|| player->GetEdict() == NULL
ds@1952
   430
		|| player->IsFakeClient())
ds@1952
   431
	{
ds@1952
   432
		return false;
ds@1952
   433
	}
ds@1952
   434
ds@1952
   435
	AdminId adm = player->GetAdminId();
ds@1952
   436
	if (adm != INVALID_ADMIN_ID)
ds@1952
   437
	{
ds@1952
   438
		FlagBits bits = g_Admins.GetAdminFlags(adm, Access_Effective);
ds@1952
   439
ds@1952
   440
		/* root knows all, WHOA */
ds@1952
   441
		if ((bits & ADMFLAG_ROOT) == ADMFLAG_ROOT)
ds@1952
   442
		{
ds@1952
   443
			return true;
ds@1952
   444
		}
ds@1952
   445
ds@1952
   446
		/* Check for overrides
ds@1952
   447
		 * :TODO: is it worth optimizing this?
ds@1952
   448
		 */
ds@1952
   449
		unsigned int groups = g_Admins.GetAdminGroupCount(adm);
ds@1952
   450
		GroupId gid;
ds@1952
   451
		OverrideRule rule;
ds@1952
   452
		bool override = false;
ds@1952
   453
		for (unsigned int i=0; i<groups; i++)
ds@1952
   454
		{
ds@1952
   455
			gid = g_Admins.GetAdminGroup(adm, i, NULL);
ds@1952
   456
			/* First get group-level override */
ds@1952
   457
			override = g_Admins.GetGroupCommandOverride(gid, cmd, Override_CommandGroup, &rule);
ds@1952
   458
			/* Now get the specific command override */
ds@1952
   459
			if (g_Admins.GetGroupCommandOverride(gid, cmd, Override_Command, &rule))
ds@1952
   460
			{
ds@1952
   461
				override = true;
ds@1952
   462
			}
ds@1952
   463
			if (override)
ds@1952
   464
			{
ds@1952
   465
				if (rule == Command_Allow)
ds@1952
   466
				{
ds@1952
   467
					return true;
ds@1952
   468
				}
ds@1952
   469
				else if (rule == Command_Deny)
ds@1952
   470
				{
ds@1952
   471
					return false;
ds@1952
   472
				}
ds@1952
   473
			}
ds@1952
   474
		}
ds@1952
   475
ds@1952
   476
		/* See if our other flags match */
ds@1952
   477
		if ((bits & cmdflags) == cmdflags)
ds@1952
   478
		{
ds@1952
   479
			return true;
ds@1952
   480
		}
ds@1952
   481
	}
ds@1952
   482
ds@1952
   483
	return false;
ds@1952
   484
}
ds@1952
   485
ds@1952
   486
bool ConCmdManager::CheckAccess(int client, const char *cmd, AdminCmdInfo *pAdmin)
ds@1952
   487
{
ds@1952
   488
	if (CheckCommandAccess(client, cmd, pAdmin->eflags))
ds@1952
   489
	{
ds@1952
   490
		return true;
ds@1952
   491
	}
ds@1952
   492
ds@1952
   493
	edict_t *pEdict = engine->PEntityOfEntIndex(client);
ds@1952
   494
	
ds@1952
   495
	/* If we got here, the command failed... */
ds@1952
   496
	char buffer[128];
ds@1952
   497
	if (g_Translator.CoreTrans(client, buffer, sizeof(buffer), "No Access", NULL, NULL)
ds@1952
   498
		!= Trans_Okay)
ds@1952
   499
	{
ds@1952
   500
		UTIL_Format(buffer, sizeof(buffer), "You do not have access to this command");
ds@1952
   501
	}
ds@1952
   502
ds@1952
   503
	unsigned int replyto = g_ChatTriggers.GetReplyTo();
ds@1952
   504
	if (replyto == SM_REPLY_CONSOLE)
ds@1952
   505
	{
ds@1952
   506
		char fullbuffer[192];
ds@1952
   507
		UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s.\n", buffer);
ds@1952
   508
		engine->ClientPrintf(pEdict, fullbuffer);
ds@1952
   509
	}
ds@1952
   510
	else if (replyto == SM_REPLY_CHAT)
ds@1952
   511
	{
ds@1952
   512
		char fullbuffer[192];
ds@1952
   513
		UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s.", buffer);
ds@1952
   514
		g_HL2.TextMsg(client, HUD_PRINTTALK, fullbuffer);
ds@1952
   515
	}
ds@1952
   516
	
ds@1952
   517
	return false;
ds@1952
   518
}
ds@1952
   519
ds@1952
   520
bool ConCmdManager::AddConsoleCommand(IPluginFunction *pFunction, 
ds@1952
   521
									   const char *name, 
ds@1952
   522
									   const char *description, 
ds@1952
   523
									   int flags)
ds@1952
   524
{
ds@1952
   525
	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
ds@1952
   526
ds@1952
   527
	if (!pInfo)
ds@1952
   528
	{
ds@1952
   529
		return false;
ds@1952
   530
	}
ds@1952
   531
ds@1952
   532
	CmdHook *pHook = new CmdHook();
ds@1952
   533
ds@1952
   534
	pHook->pf = pFunction;
ds@1952
   535
	if (description && description[0])
ds@1952
   536
	{
ds@1952
   537
		pHook->helptext.assign(description);
ds@1952
   538
	}
ds@1952
   539
	pInfo->conhooks.push_back(pHook);
ds@1952
   540
ds@1952
   541
	/* Add to the plugin */
ds@1952
   542
	CmdList *pList;
ds@1952
   543
	IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
ds@1952
   544
	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
ds@1952
   545
	{
ds@1952
   546
		pList = new CmdList();
ds@1952
   547
		pPlugin->SetProperty("CommandList", pList);
ds@1952
   548
	}
ds@1952
   549
	PlCmdInfo info;
ds@1952
   550
	info.pInfo = pInfo;
ds@1952
   551
	info.type = Cmd_Console;
ds@1952
   552
	info.pHook = pHook;
ds@1952
   553
	AddToPlCmdList(pList, info);
ds@1952
   554
ds@1952
   555
	return true;
ds@1952
   556
}
ds@1952
   557
ds@1952
   558
bool ConCmdManager::AddAdminCommand(IPluginFunction *pFunction, 
ds@1952
   559
									 const char *name, 
ds@1952
   560
									 const char *group,
ds@1952
   561
									 int adminflags,
ds@1952
   562
									 const char *description, 
ds@1952
   563
									 int flags)
ds@1952
   564
{
ds@1952
   565
	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
ds@1952
   566
ds@1952
   567
	if (!pInfo)
ds@1952
   568
	{
ds@1952
   569
		return false;
ds@1952
   570
	}
ds@1952
   571
ds@1952
   572
	CmdHook *pHook = new CmdHook();
ds@1952
   573
	AdminCmdInfo *pAdmin = new AdminCmdInfo();
ds@1952
   574
ds@1952
   575
	pHook->pf = pFunction;
ds@1952
   576
	if (description && description[0])
ds@1952
   577
	{
ds@1952
   578
		pHook->helptext.assign(description);
ds@1952
   579
	}
ds@1952
   580
	pHook->pAdmin = pAdmin;
ds@1952
   581
ds@1952
   582
	void *object;
ds@1952
   583
	int grpid;
ds@1952
   584
	if (!sm_trie_retrieve(m_pCmdGrps, group, (void **)&object))
ds@1952
   585
	{
ds@1952
   586
		grpid = m_Strings.AddString(group);
ds@1952
   587
		sm_trie_insert(m_pCmdGrps, group, (void *)grpid);
ds@1952
   588
	}
ds@1952
   589
	else
ds@1952
   590
	{
ds@1952
   591
		grpid = (int)object;
ds@1952
   592
	}
ds@1952
   593
ds@1952
   594
	pAdmin->cmdGrpId = grpid;
ds@1952
   595
	pAdmin->flags = adminflags;
ds@1952
   596
ds@1952
   597
	/* First get the command group override, if any */
ds@1952
   598
	bool override = g_Admins.GetCommandOverride(group, 
ds@1952
   599
		Override_CommandGroup,
ds@1952
   600
		&(pAdmin->eflags));
ds@1952
   601
ds@1952
   602
	/* Next get the command override, if any */
ds@1952
   603
	if (g_Admins.GetCommandOverride(name,
ds@1952
   604
		Override_Command,
ds@1952
   605
		&(pAdmin->eflags)))
ds@1952
   606
	{
ds@1952
   607
		override = true;
ds@1952
   608
	}
ds@1952
   609
ds@1952
   610
	/* Assign normal flags if there were no overrides */
ds@1952
   611
	if (!override)
ds@1952
   612
	{
ds@1952
   613
		pAdmin->eflags = pAdmin->flags;
ds@1952
   614
	}
ds@1952
   615
ds@1952
   616
	/* Finally, add the hook */
ds@1952
   617
	pInfo->conhooks.push_back(pHook);
ds@1952
   618
	pInfo->admin = *(pHook->pAdmin);
ds@1952
   619
	pInfo->is_admin_set = true;
ds@1952
   620
ds@1952
   621
	/* Now add to the plugin */
ds@1952
   622
	CmdList *pList;
ds@1952
   623
	IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
ds@1952
   624
	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
ds@1952
   625
	{
ds@1952
   626
		pList = new CmdList();
ds@1952
   627
		pPlugin->SetProperty("CommandList", pList);
ds@1952
   628
	}
ds@1952
   629
	PlCmdInfo info;
ds@1952
   630
	info.pInfo = pInfo;
ds@1952
   631
	info.type = Cmd_Admin;
ds@1952
   632
	info.pHook = pHook;
ds@1952
   633
	AddToPlCmdList(pList, info);
ds@1952
   634
ds@1952
   635
	return true;
ds@1952
   636
}
ds@1952
   637
ds@1952
   638
bool ConCmdManager::AddServerCommand(IPluginFunction *pFunction, 
ds@1952
   639
									  const char *name, 
ds@1952
   640
									  const char *description, 
ds@1952
   641
									  int flags)
ds@1952
   642
ds@1952
   643
{
ds@1952
   644
	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
ds@1952
   645
ds@1952
   646
	if (!pInfo)
ds@1952
   647
	{
ds@1952
   648
		return false;
ds@1952
   649
	}
ds@1952
   650
ds@1952
   651
	CmdHook *pHook = new CmdHook();
ds@1952
   652
ds@1952
   653
	pHook->pf = pFunction;
ds@1952
   654
	if (description && description[0])
ds@1952
   655
	{
ds@1952
   656
		pHook->helptext.assign(description);
ds@1952
   657
	}
ds@1952
   658
ds@1952
   659
	pInfo->srvhooks.push_back(pHook);
ds@1952
   660
ds@1952
   661
	/* Add to the plugin */
ds@1952
   662
	CmdList *pList;
ds@1952
   663
	IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
ds@1952
   664
	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
ds@1952
   665
	{
ds@1952
   666
		pList = new CmdList();
ds@1952
   667
		pPlugin->SetProperty("CommandList", pList);
ds@1952
   668
	}
ds@1952
   669
	PlCmdInfo info;
ds@1952
   670
	info.pInfo = pInfo;
ds@1952
   671
	info.type = Cmd_Server;
ds@1952
   672
	info.pHook = pHook;
ds@1952
   673
	AddToPlCmdList(pList, info);
ds@1952
   674
ds@1952
   675
	return true;
ds@1952
   676
}
ds@1952
   677
ds@1952
   678
void AddToPlCmdList(CmdList *pList, const PlCmdInfo &info)
ds@1952
   679
{
ds@1952
   680
	CmdList::iterator iter = pList->begin();
ds@1952
   681
	bool inserted = false;
ds@1952
   682
	const char *orig = NULL;
ds@1952
   683
ds@1952
   684
	orig = info.pInfo->pCmd->GetName();
ds@1952
   685
ds@1952
   686
	/* Insert this into the help list, SORTED alphabetically. */
ds@1952
   687
	while (iter != pList->end())
ds@1952
   688
	{
ds@1952
   689
		PlCmdInfo &obj = (*iter);
ds@1952
   690
		const char *cmd = obj.pInfo->pCmd->GetName();
ds@1952
   691
		if (strcmp(orig, cmd) < 0)
ds@1952
   692
		{
ds@1952
   693
			pList->insert(iter, info);
ds@1952
   694
			inserted = true;
ds@1952
   695
			break;
ds@1952
   696
		}
ds@1952
   697
		iter++;
ds@1952
   698
	}
ds@1952
   699
ds@1952
   700
	if (!inserted)
ds@1952
   701
	{
ds@1952
   702
		pList->push_back(info);
ds@1952
   703
	}
ds@1952
   704
}
ds@1952
   705
ds@1952
   706
void ConCmdManager::AddToCmdList(ConCmdInfo *info)
ds@1952
   707
{
ds@1952
   708
	List<ConCmdInfo *>::iterator iter = m_CmdList.begin();
ds@1952
   709
	ConCmdInfo *pInfo;
ds@1952
   710
	bool inserted = false;
ds@1952
   711
	const char *orig = NULL;
ds@1952
   712
ds@1952
   713
	orig = info->pCmd->GetName();
ds@1952
   714
ds@1952
   715
	/* Insert this into the help list, SORTED alphabetically. */
ds@1952
   716
	while (iter != m_CmdList.end())
ds@1952
   717
	{
ds@1952
   718
		const char *cmd = NULL;
ds@1952
   719
		pInfo = (*iter);
ds@1952
   720
		cmd = pInfo->pCmd->GetName();
ds@1952
   721
		if (strcmp(orig, cmd) < 0)
ds@1952
   722
		{
ds@1952
   723
			m_CmdList.insert(iter, info);
ds@1952
   724
			inserted = true;
ds@1952
   725
			break;
ds@1952
   726
		}
ds@1952
   727
		iter++;
ds@1952
   728
	}
ds@1952
   729
ds@1952
   730
	if (!inserted)
ds@1952
   731
	{
ds@1952
   732
		m_CmdList.push_back(info);
ds@1952
   733
	}
ds@1952
   734
}
ds@1952
   735
ds@1952
   736
void ConCmdManager::UpdateAdminCmdFlags(const char *cmd, OverrideType type, FlagBits bits, bool remove)
ds@1952
   737
{
ds@1952
   738
	ConCmdInfo *pInfo;
ds@1952
   739
ds@1952
   740
	if (type == Override_Command)
ds@1952
   741
	{
ds@1952
   742
		if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
ds@1952
   743
		{
ds@1952
   744
			return;
ds@1952
   745
		}
ds@1952
   746
ds@1952
   747
		List<CmdHook *>::iterator iter;
ds@1952
   748
		CmdHook *pHook;
ds@1952
   749
ds@1952
   750
		for (iter=pInfo->conhooks.begin(); iter!=pInfo->conhooks.end(); iter++)
ds@1952
   751
		{
ds@1952
   752
			pHook = (*iter);
ds@1952
   753
			if (pHook->pAdmin)
ds@1952
   754
			{
ds@1952
   755
				if (!remove)
ds@1952
   756
				{
ds@1952
   757
					pHook->pAdmin->eflags = bits;
ds@1952
   758
				} else {
ds@1952
   759
					pHook->pAdmin->eflags = pHook->pAdmin->flags;
ds@1952
   760
				}
ds@1952
   761
				pInfo->admin = *(pHook->pAdmin);
ds@1952
   762
			}
ds@1952
   763
		}
ds@1952
   764
		pInfo->is_admin_set = true;
ds@1952
   765
	}
ds@1952
   766
	else if (type == Override_CommandGroup)
ds@1952
   767
	{
ds@1952
   768
		void *object;
ds@1952
   769
		if (!sm_trie_retrieve(m_pCmdGrps, cmd, &object))
ds@1952
   770
		{
ds@1952
   771
			return;
ds@1952
   772
		}
ds@1952
   773
		int grpid = (int)object;
ds@1952
   774
ds@1952
   775
		/* This is bad :( loop through all commands */
ds@1952
   776
		List<ConCmdInfo *>::iterator iter;
ds@1952
   777
		CmdHook *pHook;
ds@1952
   778
		for (iter=m_CmdList.begin(); iter!=m_CmdList.end(); iter++)
ds@1952
   779
		{
ds@1952
   780
			pInfo = (*iter);
ds@1952
   781
			for (List<CmdHook *>::iterator citer=pInfo->conhooks.begin();
ds@1952
   782
				 citer!=pInfo->conhooks.end();
ds@1952
   783
				 citer++)
ds@1952
   784
			{
ds@1952
   785
				pHook = (*citer);
ds@1952
   786
				if (pHook->pAdmin && pHook->pAdmin->cmdGrpId == grpid)
ds@1952
   787
				{
ds@1952
   788
					if (remove)
ds@1952
   789
					{
ds@1952
   790
						pHook->pAdmin->eflags = bits;
ds@1952
   791
					} else {
ds@1952
   792
						pHook->pAdmin->eflags = pHook->pAdmin->flags;
ds@1952
   793
					}
ds@1952
   794
					pInfo->admin = *(pHook->pAdmin);
ds@1952
   795
				}
ds@1952
   796
			}
ds@1952
   797
		}
ds@1952
   798
		pInfo->is_admin_set = true;
ds@1952
   799
	}
ds@1952
   800
}
ds@1952
   801
ds@1952
   802
void ConCmdManager::RemoveConCmd(ConCmdInfo *info, const char *name, bool is_read_safe, bool untrack)
ds@1952
   803
{
ds@1952
   804
	/* Remove from the trie */
ds@1952
   805
	sm_trie_delete(m_pCmds, name);
ds@1952
   806
ds@1952
   807
	/* Remove console-specific information
ds@1952
   808
	 * This should always be true as of right now
ds@1952
   809
	 */
ds@1952
   810
	if (info->pCmd)
ds@1952
   811
	{
ds@1952
   812
		if (info->sourceMod)
ds@1952
   813
		{
ds@1952
   814
			/* Unlink from SourceMM */
ds@1952
   815
			g_SMAPI->UnregisterConCommandBase(g_PLAPI, info->pCmd);
ds@1952
   816
			/* Delete the command's memory */
ds@1952
   817
			char *new_help = const_cast<char *>(info->pCmd->GetHelpText());
ds@1952
   818
			char *new_name = const_cast<char *>(info->pCmd->GetName());
ds@1952
   819
			delete [] new_help;
ds@1952
   820
			delete [] new_name;
ds@1952
   821
			delete info->pCmd;
ds@1952
   822
		}
ds@1952
   823
		else
ds@1952
   824
		{
ds@1952
   825
			if (is_read_safe)
ds@1952
   826
			{
ds@1952
   827
				/* Remove the external hook */
ds@1952
   828
				SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, info->pCmd, CommandCallback, false);
ds@1952
   829
			}
ds@1952
   830
			if (untrack)
ds@1952
   831
			{
ds@1952
   832
				UntrackConCommandBase(info->pCmd, this);
ds@1952
   833
			}
ds@1952
   834
		}
ds@1952
   835
	}
ds@1952
   836
	
ds@1952
   837
	/* Remove from list */
ds@1952
   838
	m_CmdList.remove(info);
ds@1952
   839
ds@1952
   840
	delete info;
ds@1952
   841
}
ds@1952
   842
ds@1952
   843
bool ConCmdManager::LookForSourceModCommand(const char *cmd)
ds@1952
   844
{
ds@1952
   845
	ConCmdInfo *pInfo;
ds@1952
   846
ds@1952
   847
	if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
ds@1952
   848
	{
ds@1952
   849
		return false;
ds@1952
   850
	}
ds@1952
   851
ds@1952
   852
	return pInfo->sourceMod && (pInfo->conhooks.size() > 0);
ds@1952
   853
}
ds@1952
   854
ds@1952
   855
bool ConCmdManager::LookForCommandAdminFlags(const char *cmd, FlagBits *pFlags)
ds@1952
   856
{
ds@1952
   857
	ConCmdInfo *pInfo;
ds@1952
   858
ds@1952
   859
	if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
ds@1952
   860
	{
ds@1952
   861
		return false;
ds@1952
   862
	}
ds@1952
   863
ds@1952
   864
	*pFlags = pInfo->admin.eflags;
ds@1952
   865
ds@1952
   866
	return pInfo->is_admin_set;
ds@1952
   867
}
ds@1952
   868
ds@1952
   869
ConCmdInfo *ConCmdManager::AddOrFindCommand(const char *name, const char *description, int flags)
ds@1952
   870
{
ds@1952
   871
	ConCmdInfo *pInfo;
ds@1952
   872
	if (!sm_trie_retrieve(m_pCmds, name, (void **)&pInfo))
ds@1952
   873
	{
ds@1952
   874
		pInfo = new ConCmdInfo();
ds@1952
   875
		/* Find the commandopan */
ds@1952
   876
		ConCommandBase *pBase = icvar->GetCommands();
ds@1952
   877
		ConCommand *pCmd = NULL;
ds@1952
   878
		while (pBase)
ds@1952
   879
		{
ds@1952
   880
			if (strcmp(pBase->GetName(), name) == 0)
ds@1952
   881
			{
ds@1952
   882
				/* Don't want to return convar with same name */
ds@1952
   883
				if (!pBase->IsCommand())
ds@1952
   884
				{
ds@1952
   885
					return NULL;
ds@1952
   886
				}
ds@1952
   887
ds@1952
   888
				pCmd = (ConCommand *)pBase;
ds@1952
   889
				break;
ds@1952
   890
			}
ds@1952
   891
			pBase = const_cast<ConCommandBase *>(pBase->GetNext());
ds@1952
   892
		}
ds@1952
   893
ds@1952
   894
		if (!pCmd)
ds@1952
   895
		{
ds@1952
   896
			/* Note that we have to duplicate because the source might not be 
ds@1952
   897
			 * a static string, and these expect static memory.
ds@1952
   898
			 */
ds@1952
   899
			if (!description)
ds@1952
   900
			{
ds@1952
   901
				description = "";
ds@1952
   902
			}
ds@1952
   903
			char *new_name = sm_strdup(name);
ds@1952
   904
			char *new_help = sm_strdup(description);
ds@1952
   905
			pCmd = new ConCommand(new_name, CommandCallback, new_help, flags);
ds@1952
   906
			pInfo->sourceMod = true;
ds@1952
   907
		}
ds@1952
   908
		else
ds@1952
   909
		{
ds@1952
   910
			TrackConCommandBase(pCmd, this);
ds@1952
   911
			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, pCmd, CommandCallback, false);
ds@1952
   912
		}
ds@1952
   913
ds@1952
   914
		pInfo->pCmd = pCmd;
ds@1952
   915
		pInfo->is_admin_set = false;
ds@1952
   916
ds@1952
   917
		sm_trie_insert(m_pCmds, name, pInfo);
ds@1952
   918
		AddToCmdList(pInfo);
ds@1952
   919
	}
ds@1952
   920
ds@1952
   921
	return pInfo;
ds@1952
   922
}
ds@1952
   923
ds@1952
   924
void ConCmdManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
ds@1952
   925
{
ds@1952
   926
	if (command.ArgC() >= 3)
ds@1952
   927
	{
ds@1952
   928
		const char *text = command.Arg(2);
ds@1952
   929
ds@1952
   930
		CPlugin *pPlugin = g_PluginSys.FindPluginByConsoleArg(text);
ds@1952
   931
ds@1952
   932
		if (!pPlugin)
ds@1952
   933
		{
ds@1952
   934
			g_RootMenu.ConsolePrint("[SM] Plugin \"%s\" was not found.", text);
ds@1952
   935
			return;
ds@1952
   936
		}
ds@1952
   937
ds@1952
   938
		const sm_plugininfo_t *plinfo = pPlugin->GetPublicInfo();
ds@1952
   939
		const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : pPlugin->GetFilename();
ds@1952
   940
ds@1952
   941
		CmdList *pList;
ds@1952
   942
		if (!pPlugin->GetProperty("CommandList", (void **)&pList))
ds@1952
   943
		{
ds@1952
   944
			g_RootMenu.ConsolePrint("[SM] No commands found for: %s", plname);
ds@1952
   945
			return;
ds@1952
   946
		}
ds@1952
   947
		if (!pList->size())
ds@1952
   948
		{
ds@1952
   949
			g_RootMenu.ConsolePrint("[SM] No commands found for: %s", plname);
ds@1952
   950
			return;
ds@1952
   951
		}
ds@1952
   952
ds@1952
   953
		CmdList::iterator iter;
ds@1952
   954
		const char *type = NULL;
ds@1952
   955
		const char *name;
ds@1952
   956
		const char *help;
ds@1952
   957
		g_RootMenu.ConsolePrint("[SM] Listing %d commands for: %s", pList->size(), plname);
ds@1952
   958
		g_RootMenu.ConsolePrint("  %-17.16s %-8.7s %s", "[Name]", "[Type]", "[Help]");
ds@1952
   959
		for (iter=pList->begin();
ds@1952
   960
			 iter!=pList->end();
ds@1952
   961
			 iter++)
ds@1952
   962
		{
ds@1952
   963
			PlCmdInfo &cmd = (*iter);
ds@1952
   964
			if (cmd.type == Cmd_Server)
ds@1952
   965
			{
ds@1952
   966
				type = "server";
ds@1952
   967
			}
ds@1952
   968
			else if (cmd.type == Cmd_Console)
ds@1952
   969
			{
ds@1952
   970
				type = "console";
ds@1952
   971
			}
ds@1952
   972
			else if (cmd.type == Cmd_Admin)
ds@1952
   973
			{
ds@1952
   974
				type = "admin";
ds@1952
   975
			}
ds@1952
   976
			name = cmd.pInfo->pCmd->GetName();
ds@1952
   977
			if (cmd.pHook->helptext.size())
ds@1952
   978
			{
ds@1952
   979
				help = cmd.pHook->helptext.c_str();
ds@1952
   980
			}
ds@1952
   981
			else
ds@1952
   982
			{
ds@1952
   983
				help = cmd.pInfo->pCmd->GetHelpText();
ds@1952
   984
			}
ds@1952
   985
			g_RootMenu.ConsolePrint("  %-17.16s %-12.11s %s", name, type, help);		
ds@1952
   986
		}
ds@1952
   987
ds@1952
   988
		return;
ds@1952
   989
	}
ds@1952
   990
ds@1952
   991
	g_RootMenu.ConsolePrint("[SM] Usage: sm cmds <plugin #>");
ds@1952
   992
}