core/ConCmdManager.cpp
author David Anderson <dvander@alliedmods.net>
Sun Oct 14 02:30:16 2007 +0000 (2007-10-14)
changeset 1544 feff70182207
parent 1541 e46ad208b9b4
child 1584 8158e9eafff5
permissions -rw-r--r--
part 1 of tf2 support is now done
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "ConCmdManager.h"
    33 #include "sm_srvcmds.h"
    34 #include "AdminCache.h"
    35 #include "sm_stringutil.h"
    36 #include "PlayerManager.h"
    37 #include "Translator.h"
    38 #include "HalfLife2.h"
    39 #include "ChatTriggers.h"
    40 
    41 ConCmdManager g_ConCmds;
    42 
    43 #if defined ORANGEBOX_BUILD
    44 	SH_DECL_HOOK1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
    45 #else
    46 	SH_DECL_HOOK0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
    47 #endif
    48 
    49 SH_DECL_HOOK1_void(IServerGameClients, SetCommandClient, SH_NOATTRIB, false, int);
    50 
    51 struct PlCmdInfo
    52 {
    53 	ConCmdInfo *pInfo;
    54 	CmdHook *pHook;
    55 	CmdType type;
    56 };
    57 typedef List<PlCmdInfo> CmdList;
    58 
    59 void AddToPlCmdList(CmdList *pList, const PlCmdInfo &info);
    60 
    61 ConCmdManager::ConCmdManager() : m_Strings(1024)
    62 {
    63 	m_pCmds = sm_trie_create();
    64 	m_pCmdGrps = sm_trie_create();
    65 	m_CmdClient = 0;
    66 }
    67 
    68 ConCmdManager::~ConCmdManager()
    69 {
    70 	sm_trie_destroy(m_pCmds);
    71 	sm_trie_destroy(m_pCmdGrps);
    72 }
    73 
    74 void ConCmdManager::OnSourceModAllInitialized()
    75 {
    76 	g_PluginSys.AddPluginsListener(this);
    77 	g_RootMenu.AddRootConsoleCommand("cmds", "List console commands", this);
    78 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, serverClients, this, &ConCmdManager::SetCommandClient, false);
    79 }
    80 
    81 void ConCmdManager::OnSourceModShutdown()
    82 {
    83 	/* All commands should already be removed by the time we're done */
    84 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, serverClients, this, &ConCmdManager::SetCommandClient, false);
    85 	g_RootMenu.RemoveRootConsoleCommand("cmds", this);
    86 }
    87 
    88 void ConCmdManager::RemoveConCmds(List<CmdHook *> &cmdlist, IPluginContext *pContext)
    89 {
    90 	List<CmdHook *>::iterator iter = cmdlist.begin();
    91 	CmdHook *pHook;
    92 
    93 	while (iter != cmdlist.end())
    94 	{
    95 		pHook = (*iter);
    96 		if (pHook->pf->GetParentContext() == pContext)
    97 		{
    98 			delete pHook->pAdmin;
    99 			delete pHook;
   100 			iter = cmdlist.erase(iter);
   101 		} else {
   102 			iter++;
   103 		}
   104 	}
   105 }
   106 
   107 void ConCmdManager::OnPluginDestroyed(IPlugin *plugin)
   108 {
   109 	CmdList *pList;
   110 	List<ConCmdInfo *> removed;
   111 	if (plugin->GetProperty("CommandList", (void **)&pList, true))
   112 	{
   113 		IPluginContext *pContext = plugin->GetBaseContext();
   114 		CmdList::iterator iter;
   115 		/* First pass!
   116 	 	 * Only bother if there's an actual command list on this plugin... 
   117 	 	 */
   118 		for (iter=pList->begin();
   119 			iter!=pList->end();
   120 			iter++)
   121 		{
   122 			PlCmdInfo &cmd = (*iter);
   123 			ConCmdInfo *pInfo = cmd.pInfo;
   124 
   125 			/* Has this chain already been fully cleaned/removed? */
   126 			if (removed.find(pInfo) != removed.end())
   127 			{
   128 				continue;
   129 			}
   130 
   131 			/* Remove any hooks from us on this command */
   132 			RemoveConCmds(pInfo->conhooks, pContext);
   133 			RemoveConCmds(pInfo->srvhooks, pContext);
   134 
   135 			/* See if there are still hooks */
   136 			if (pInfo->srvhooks.size())
   137 			{
   138 				continue;
   139 			}
   140 			if (pInfo->conhooks.size())
   141 			{
   142 				continue;
   143 			}
   144 
   145 			/* Remove the command, it should be safe now */
   146 			RemoveConCmd(pInfo);
   147 			removed.push_back(pInfo);
   148 		}
   149 		delete pList;
   150 	}
   151 }
   152 #if defined ORANGEBOX_BUILD
   153 void CommandCallback(const CCommand &command)
   154 {
   155 #else
   156 void CommandCallback()
   157 {
   158 	CCommand command;
   159 #endif
   160 
   161 	g_HL2.PushCommandStack(&command);
   162 
   163 	g_ConCmds.InternalDispatch(command);
   164 
   165 	g_HL2.PopCommandStack();
   166 }
   167 
   168 void ConCmdManager::SetCommandClient(int client)
   169 {
   170 	m_CmdClient = client + 1;
   171 }
   172 
   173 ResultType ConCmdManager::DispatchClientCommand(int client, const char *cmd, int args, ResultType type)
   174 {
   175 	ConCmdInfo *pInfo;
   176 	if (sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   177 	{
   178 		cell_t result = type;
   179 		cell_t tempres = result;
   180 		List<CmdHook *>::iterator iter;
   181 		CmdHook *pHook;
   182 		for (iter=pInfo->conhooks.begin();
   183 			 iter!=pInfo->conhooks.end();
   184 			 iter++)
   185 		{
   186 			pHook = (*iter);
   187 			if (!pHook->pf->IsRunnable())
   188 			{
   189 				continue;
   190 			}
   191 			if (pHook->pAdmin && !CheckAccess(client, cmd, pHook->pAdmin))
   192 			{
   193 				if (result < Pl_Handled)
   194 				{
   195 					result = Pl_Handled;
   196 				}
   197 				continue;
   198 			}
   199 			pHook->pf->PushCell(client);
   200 			pHook->pf->PushCell(args);
   201 			if (pHook->pf->Execute(&tempres) == SP_ERROR_NONE)
   202 			{
   203 				if (tempres > result)
   204 				{
   205 					result = tempres;
   206 				}
   207 				if (result == Pl_Stop)
   208 				{
   209 					break;
   210 				}
   211 			}
   212 		}
   213 		type = (ResultType)result;
   214 	}
   215 
   216 	return type;
   217 }
   218 
   219 void ConCmdManager::InternalDispatch(const CCommand &command)
   220 {
   221 	int client = m_CmdClient;
   222 
   223 	if (client)
   224 	{
   225 		CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   226 		if (!pPlayer || !pPlayer->IsConnected())
   227 		{
   228 			return;
   229 		}
   230 	}
   231 
   232 	/**
   233 	 * Note: Console commands will EITHER go through IServerGameDLL::ClientCommand,
   234 	 * OR this dispatch.  They will NEVER go through both.
   235 	 * -- 
   236 	 * Whether or not it goes through the callback is determined by FCVAR_GAMEDLL
   237 	 */
   238 	const char *cmd = g_HL2.CurrentCommandName();
   239 
   240 	ConCmdInfo *pInfo;
   241 	if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   242 	{
   243 		return;
   244 	}
   245 
   246 	cell_t result = Pl_Continue;
   247 	int args = command.ArgC() - 1;
   248 
   249 	List<CmdHook *>::iterator iter;
   250 	CmdHook *pHook;
   251 
   252 	/* Execute server-only commands if viable */
   253 	if (client == 0 && pInfo->srvhooks.size())
   254 	{
   255 		cell_t tempres = result;
   256 		for (iter=pInfo->srvhooks.begin();
   257 			 iter!=pInfo->srvhooks.end();
   258 			 iter++)
   259 		{
   260 			pHook = (*iter);
   261 			if (!pHook->pf->IsRunnable())
   262 			{
   263 				continue;
   264 			}
   265 			pHook->pf->PushCell(args);
   266 			if (pHook->pf->Execute(&tempres) == SP_ERROR_NONE)
   267 			{
   268 				if (tempres > result)
   269 				{
   270 					result = tempres;
   271 				}
   272 				if (result == Pl_Stop)
   273 				{
   274 					break;
   275 				}
   276 			}
   277 		}
   278 	
   279 		/* Check if there's an early stop */
   280 		if (result >= Pl_Stop)
   281 		{
   282 			if (!pInfo->sourceMod)
   283 			{
   284 				RETURN_META(MRES_SUPERCEDE);
   285 			}
   286 			return;
   287 		}
   288 	}
   289 
   290 	/* Execute console commands */
   291 	if (pInfo->conhooks.size())
   292 	{
   293 		cell_t tempres = result;
   294 		for (iter=pInfo->conhooks.begin();
   295 			iter!=pInfo->conhooks.end();
   296 			iter++)
   297 		{
   298 			pHook = (*iter);
   299 			if (!pHook->pf->IsRunnable())
   300 			{
   301 				continue;
   302 			}
   303 			if (client 
   304 				&& pHook->pAdmin
   305 				&& !CheckAccess(client, cmd, pHook->pAdmin))
   306 			{
   307 				if (result < Pl_Handled)
   308 				{
   309 					result = Pl_Handled;
   310 				}
   311 				continue;
   312 			}
   313 
   314 			/* On a listen server, sometimes the server host's client index can be set as 0.
   315 			 * So index 1 is passed to the command callback to correct this potential problem.
   316 			 */
   317 			if (client == 0 && !engine->IsDedicatedServer())
   318 			{
   319 				pHook->pf->PushCell(1);
   320 			} else {
   321 				pHook->pf->PushCell(client);
   322 			}
   323 
   324 			pHook->pf->PushCell(args);
   325 			if (pHook->pf->Execute(&tempres) == SP_ERROR_NONE)
   326 			{
   327 				if (tempres > result)
   328 				{
   329 					result = tempres;
   330 				}
   331 				if (result == Pl_Stop)
   332 				{
   333 					break;
   334 				}
   335 			}
   336 		}
   337 	}
   338 
   339 	if (result >= Pl_Handled)
   340 	{
   341 		if (!pInfo->sourceMod)
   342 		{
   343 			RETURN_META(MRES_SUPERCEDE);
   344 		}
   345 		return;
   346 	}
   347 }
   348 
   349 bool ConCmdManager::CheckCommandAccess(int client, const char *cmd, FlagBits cmdflags)
   350 {
   351 	if (cmdflags == 0 || client == 0)
   352 	{
   353 		return true;
   354 	}
   355 
   356 	/* If running listen server, then client 1 is the server host and should have 'root' access */
   357 	if (client == 1 && !engine->IsDedicatedServer())
   358 	{
   359 		return true;
   360 	}
   361 
   362 	CPlayer *player = g_Players.GetPlayerByIndex(client);
   363 	if (!player 
   364 		|| player->GetEdict() == NULL
   365 		|| player->IsFakeClient())
   366 	{
   367 		return false;
   368 	}
   369 
   370 	AdminId adm = player->GetAdminId();
   371 	if (adm != INVALID_ADMIN_ID)
   372 	{
   373 		FlagBits bits = g_Admins.GetAdminFlags(adm, Access_Effective);
   374 
   375 		/* root knows all, WHOA */
   376 		if ((bits & ADMFLAG_ROOT) == ADMFLAG_ROOT)
   377 		{
   378 			return true;
   379 		}
   380 
   381 		/* Check for overrides
   382 		 * :TODO: is it worth optimizing this?
   383 		 */
   384 		unsigned int groups = g_Admins.GetAdminGroupCount(adm);
   385 		GroupId gid;
   386 		OverrideRule rule;
   387 		bool override = false;
   388 		for (unsigned int i=0; i<groups; i++)
   389 		{
   390 			gid = g_Admins.GetAdminGroup(adm, i, NULL);
   391 			/* First get group-level override */
   392 			override = g_Admins.GetGroupCommandOverride(gid, cmd, Override_CommandGroup, &rule);
   393 			/* Now get the specific command override */
   394 			if (g_Admins.GetGroupCommandOverride(gid, cmd, Override_Command, &rule))
   395 			{
   396 				override = true;
   397 			}
   398 			if (override)
   399 			{
   400 				if (rule == Command_Allow)
   401 				{
   402 					return true;
   403 				} else if (rule == Command_Deny) {
   404 					return false;
   405 				}
   406 			}
   407 		}
   408 
   409 		/* See if our other flags match */
   410 		if ((bits & cmdflags) == cmdflags)
   411 		{
   412 			return true;
   413 		}
   414 	}
   415 
   416 	return false;
   417 }
   418 
   419 bool ConCmdManager::CheckAccess(int client, const char *cmd, AdminCmdInfo *pAdmin)
   420 {
   421 	if (CheckCommandAccess(client, cmd, pAdmin->eflags))
   422 	{
   423 		return true;
   424 	}
   425 
   426 	edict_t *pEdict = engine->PEntityOfEntIndex(client);
   427 	
   428 	/* If we got here, the command failed... */
   429 	char buffer[128];
   430 	if (g_Translator.CoreTrans(client, buffer, sizeof(buffer), "No Access", NULL, NULL)
   431 		!= Trans_Okay)
   432 	{
   433 		snprintf(buffer, sizeof(buffer), "You do not have access to this command");
   434 	}
   435 
   436 	unsigned int replyto = g_ChatTriggers.GetReplyTo();
   437 	if (replyto == SM_REPLY_CONSOLE)
   438 	{
   439 		char fullbuffer[192];
   440 		snprintf(fullbuffer, sizeof(fullbuffer), "[SM] %s.\n", buffer);
   441 		engine->ClientPrintf(pEdict, fullbuffer);
   442 	} else if (replyto == SM_REPLY_CHAT) {
   443 		char fullbuffer[192];
   444 		snprintf(fullbuffer, sizeof(fullbuffer), "[SM] %s.", buffer);
   445 		g_HL2.TextMsg(client, HUD_PRINTTALK, fullbuffer);
   446 	}
   447 	
   448 	return false;
   449 }
   450 
   451 bool ConCmdManager::AddConsoleCommand(IPluginFunction *pFunction, 
   452 									   const char *name, 
   453 									   const char *description, 
   454 									   int flags)
   455 {
   456 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   457 
   458 	if (!pInfo)
   459 	{
   460 		return false;
   461 	}
   462 
   463 	CmdHook *pHook = new CmdHook();
   464 
   465 	pHook->pf = pFunction;
   466 	if (description && description[0])
   467 	{
   468 		pHook->helptext.assign(description);
   469 	}
   470 	pInfo->conhooks.push_back(pHook);
   471 
   472 	/* Add to the plugin */
   473 	CmdList *pList;
   474 	IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
   475 	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   476 	{
   477 		pList = new CmdList();
   478 		pPlugin->SetProperty("CommandList", pList);
   479 	}
   480 	PlCmdInfo info;
   481 	info.pInfo = pInfo;
   482 	info.type = Cmd_Console;
   483 	info.pHook = pHook;
   484 	AddToPlCmdList(pList, info);
   485 
   486 	return true;
   487 }
   488 
   489 bool ConCmdManager::AddAdminCommand(IPluginFunction *pFunction, 
   490 									 const char *name, 
   491 									 const char *group,
   492 									 int adminflags,
   493 									 const char *description, 
   494 									 int flags)
   495 {
   496 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   497 
   498 	if (!pInfo)
   499 	{
   500 		return false;
   501 	}
   502 
   503 	CmdHook *pHook = new CmdHook();
   504 	AdminCmdInfo *pAdmin = new AdminCmdInfo();
   505 
   506 	pHook->pf = pFunction;
   507 	if (description && description[0])
   508 	{
   509 		pHook->helptext.assign(description);
   510 	}
   511 	pHook->pAdmin = pAdmin;
   512 
   513 	void *object;
   514 	int grpid;
   515 	if (!sm_trie_retrieve(m_pCmdGrps, group, (void **)&object))
   516 	{
   517 		grpid = m_Strings.AddString(group);
   518 		sm_trie_insert(m_pCmdGrps, group, (void *)grpid);
   519 	} else {
   520 		grpid = (int)object;
   521 	}
   522 
   523 	pAdmin->cmdGrpId = grpid;
   524 	pAdmin->flags = adminflags;
   525 
   526 	/* First get the command group override, if any */
   527 	bool override = g_Admins.GetCommandOverride(group, 
   528 		Override_CommandGroup,
   529 		&(pAdmin->eflags));
   530 
   531 	/* Next get the command override, if any */
   532 	if (g_Admins.GetCommandOverride(name,
   533 		Override_Command,
   534 		&(pAdmin->eflags)))
   535 	{
   536 		override = true;
   537 	}
   538 
   539 	/* Assign normal flags if there were no overrides */
   540 	if (!override)
   541 	{
   542 		pAdmin->eflags = pAdmin->flags;
   543 	}
   544 
   545 	/* Finally, add the hook */
   546 	pInfo->conhooks.push_back(pHook);
   547 	pInfo->admin = *(pHook->pAdmin);
   548 	pInfo->is_admin_set = true;
   549 
   550 	/* Now add to the plugin */
   551 	CmdList *pList;
   552 	IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
   553 	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   554 	{
   555 		pList = new CmdList();
   556 		pPlugin->SetProperty("CommandList", pList);
   557 	}
   558 	PlCmdInfo info;
   559 	info.pInfo = pInfo;
   560 	info.type = Cmd_Admin;
   561 	info.pHook = pHook;
   562 	AddToPlCmdList(pList, info);
   563 
   564 	return true;
   565 }
   566 
   567 bool ConCmdManager::AddServerCommand(IPluginFunction *pFunction, 
   568 									  const char *name, 
   569 									  const char *description, 
   570 									  int flags)
   571 
   572 {
   573 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   574 
   575 	if (!pInfo)
   576 	{
   577 		return false;
   578 	}
   579 
   580 	CmdHook *pHook = new CmdHook();
   581 
   582 	pHook->pf = pFunction;
   583 	if (description && description[0])
   584 	{
   585 		pHook->helptext.assign(description);
   586 	}
   587 
   588 	pInfo->srvhooks.push_back(pHook);
   589 
   590 	/* Add to the plugin */
   591 	CmdList *pList;
   592 	IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
   593 	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   594 	{
   595 		pList = new CmdList();
   596 		pPlugin->SetProperty("CommandList", pList);
   597 	}
   598 	PlCmdInfo info;
   599 	info.pInfo = pInfo;
   600 	info.type = Cmd_Server;
   601 	info.pHook = pHook;
   602 	AddToPlCmdList(pList, info);
   603 
   604 	return true;
   605 }
   606 
   607 void AddToPlCmdList(CmdList *pList, const PlCmdInfo &info)
   608 {
   609 	CmdList::iterator iter = pList->begin();
   610 	bool inserted = false;
   611 	const char *orig = NULL;
   612 
   613 	orig = info.pInfo->pCmd->GetName();
   614 
   615 	/* Insert this into the help list, SORTED alphabetically. */
   616 	while (iter != pList->end())
   617 	{
   618 		PlCmdInfo &obj = (*iter);
   619 		const char *cmd = obj.pInfo->pCmd->GetName();
   620 		if (strcmp(orig, cmd) < 0)
   621 		{
   622 			pList->insert(iter, info);
   623 			inserted = true;
   624 			break;
   625 		}
   626 		iter++;
   627 	}
   628 
   629 	if (!inserted)
   630 	{
   631 		pList->push_back(info);
   632 	}
   633 }
   634 
   635 void ConCmdManager::AddToCmdList(ConCmdInfo *info)
   636 {
   637 	List<ConCmdInfo *>::iterator iter = m_CmdList.begin();
   638 	ConCmdInfo *pInfo;
   639 	bool inserted = false;
   640 	const char *orig = NULL;
   641 
   642 	orig = info->pCmd->GetName();
   643 
   644 	/* Insert this into the help list, SORTED alphabetically. */
   645 	while (iter != m_CmdList.end())
   646 	{
   647 		const char *cmd = NULL;
   648 		pInfo = (*iter);
   649 		cmd = pInfo->pCmd->GetName();
   650 		if (strcmp(orig, cmd) < 0)
   651 		{
   652 			m_CmdList.insert(iter, info);
   653 			inserted = true;
   654 			break;
   655 		}
   656 		iter++;
   657 	}
   658 
   659 	if (!inserted)
   660 	{
   661 		m_CmdList.push_back(info);
   662 	}
   663 }
   664 
   665 void ConCmdManager::UpdateAdminCmdFlags(const char *cmd, OverrideType type, FlagBits bits, bool remove)
   666 {
   667 	ConCmdInfo *pInfo;
   668 
   669 	if (type == Override_Command)
   670 	{
   671 		if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   672 		{
   673 			return;
   674 		}
   675 
   676 		List<CmdHook *>::iterator iter;
   677 		CmdHook *pHook;
   678 
   679 		for (iter=pInfo->conhooks.begin(); iter!=pInfo->conhooks.end(); iter++)
   680 		{
   681 			pHook = (*iter);
   682 			if (pHook->pAdmin)
   683 			{
   684 				if (!remove)
   685 				{
   686 					pHook->pAdmin->eflags = bits;
   687 				} else {
   688 					pHook->pAdmin->eflags = pHook->pAdmin->flags;
   689 				}
   690 				pInfo->admin = *(pHook->pAdmin);
   691 			}
   692 		}
   693 		pInfo->is_admin_set = true;
   694 	} else if (type == Override_CommandGroup) {
   695 		void *object;
   696 		if (!sm_trie_retrieve(m_pCmdGrps, cmd, &object))
   697 		{
   698 			return;
   699 		}
   700 		int grpid = (int)object;
   701 
   702 		/* This is bad :( loop through all commands */
   703 		List<ConCmdInfo *>::iterator iter;
   704 		CmdHook *pHook;
   705 		for (iter=m_CmdList.begin(); iter!=m_CmdList.end(); iter++)
   706 		{
   707 			pInfo = (*iter);
   708 			for (List<CmdHook *>::iterator citer=pInfo->conhooks.begin();
   709 				 citer!=pInfo->conhooks.end();
   710 				 citer++)
   711 			{
   712 				pHook = (*citer);
   713 				if (pHook->pAdmin && pHook->pAdmin->cmdGrpId == grpid)
   714 				{
   715 					if (remove)
   716 					{
   717 						pHook->pAdmin->eflags = bits;
   718 					} else {
   719 						pHook->pAdmin->eflags = pHook->pAdmin->flags;
   720 					}
   721 					pInfo->admin = *(pHook->pAdmin);
   722 				}
   723 			}
   724 		}
   725 		pInfo->is_admin_set = true;
   726 	}
   727 }
   728 
   729 void ConCmdManager::RemoveConCmd(ConCmdInfo *info)
   730 {
   731 	/* Remove console-specific information
   732 	 * This should always be true as of right now
   733 	 */
   734 	if (info->pCmd)
   735 	{
   736 		/* Remove from the trie */
   737 		sm_trie_delete(m_pCmds, info->pCmd->GetName());
   738 
   739 		if (info->sourceMod)
   740 		{
   741 			/* Unlink from SourceMM */
   742 			g_SMAPI->UnregisterConCommandBase(g_PLAPI, info->pCmd);
   743 			/* Delete the command's memory */
   744 			char *new_help = const_cast<char *>(info->pCmd->GetHelpText());
   745 			char *new_name = const_cast<char *>(info->pCmd->GetName());
   746 			delete [] new_help;
   747 			delete [] new_name;
   748 			delete info->pCmd;
   749 		} else {
   750 			/* Remove the external hook */
   751 			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, info->pCmd, CommandCallback, false);
   752 		}
   753 	}
   754 	
   755 	/* Remove from list */
   756 	m_CmdList.remove(info);
   757 
   758 	delete info;
   759 }
   760 
   761 bool ConCmdManager::LookForSourceModCommand(const char *cmd)
   762 {
   763 	ConCmdInfo *pInfo;
   764 
   765 	if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   766 	{
   767 		return false;
   768 	}
   769 
   770 	return pInfo->sourceMod && (pInfo->conhooks.size() > 0);
   771 }
   772 
   773 bool ConCmdManager::LookForCommandAdminFlags(const char *cmd, FlagBits *pFlags)
   774 {
   775 	ConCmdInfo *pInfo;
   776 
   777 	if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   778 	{
   779 		return false;
   780 	}
   781 
   782 	*pFlags = pInfo->admin.eflags;
   783 
   784 	return pInfo->is_admin_set;
   785 }
   786 
   787 ConCmdInfo *ConCmdManager::AddOrFindCommand(const char *name, const char *description, int flags)
   788 {
   789 	ConCmdInfo *pInfo;
   790 	if (!sm_trie_retrieve(m_pCmds, name, (void **)&pInfo))
   791 	{
   792 		pInfo = new ConCmdInfo();
   793 		/* Find the commandopan */
   794 		ConCommandBase *pBase = icvar->GetCommands();
   795 		ConCommand *pCmd = NULL;
   796 		while (pBase)
   797 		{
   798 			if (strcmp(pBase->GetName(), name) == 0)
   799 			{
   800 				/* Don't want to return convar with same name */
   801 				if (!pBase->IsCommand())
   802 				{
   803 					return NULL;
   804 				}
   805 
   806 				pCmd = (ConCommand *)pBase;
   807 				break;
   808 			}
   809 			pBase = const_cast<ConCommandBase *>(pBase->GetNext());
   810 		}
   811 
   812 		if (!pCmd)
   813 		{
   814 			/* Note that we have to duplicate because the source might not be 
   815 			 * a static string, and these expect static memory.
   816 			 */
   817 			if (!description)
   818 			{
   819 				description = "";
   820 			}
   821 			char *new_name = sm_strdup(name);
   822 			char *new_help = sm_strdup(description);
   823 			pCmd = new ConCommand(new_name, CommandCallback, new_help, flags);
   824 			pInfo->sourceMod = true;
   825 		} else {
   826 			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, pCmd, CommandCallback, false);
   827 		}
   828 
   829 		pInfo->pCmd = pCmd;
   830 		pInfo->is_admin_set = false;
   831 
   832 		sm_trie_insert(m_pCmds, name, pInfo);
   833 		AddToCmdList(pInfo);
   834 	}
   835 
   836 	return pInfo;
   837 }
   838 
   839 void ConCmdManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
   840 {
   841 	if (command.ArgC() >= 3)
   842 	{
   843 		const char *text = command.Arg(2);
   844 
   845 		int id = atoi(text);
   846 		CPlugin *pPlugin = g_PluginSys.GetPluginByOrder(id);
   847 
   848 		if (!pPlugin)
   849 		{
   850 			g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", id);
   851 			return;
   852 		}
   853 
   854 		const sm_plugininfo_t *plinfo = pPlugin->GetPublicInfo();
   855 		const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : pPlugin->GetFilename();
   856 
   857 		CmdList *pList;
   858 		if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   859 		{
   860 			g_RootMenu.ConsolePrint("[SM] No commands found for: %s", plname);
   861 			return;
   862 		}
   863 		if (!pList->size())
   864 		{
   865 			g_RootMenu.ConsolePrint("[SM] No commands found for: %s", plname);
   866 			return;
   867 		}
   868 
   869 		CmdList::iterator iter;
   870 		const char *type = NULL;
   871 		const char *name;
   872 		const char *help;
   873 		g_RootMenu.ConsolePrint("[SM] Listing %d commands for: %s", pList->size(), plname);
   874 		g_RootMenu.ConsolePrint("  %-17.16s %-8.7s %s", "[Name]", "[Type]", "[Help]");
   875 		for (iter=pList->begin();
   876 			 iter!=pList->end();
   877 			 iter++, id++)
   878 		{
   879 			PlCmdInfo &cmd = (*iter);
   880 			if (cmd.type == Cmd_Server)
   881 			{
   882 				type = "server";
   883 			} else if (cmd.type == Cmd_Console) {
   884 				type = "console";
   885 			} else if (cmd.type == Cmd_Admin) {
   886 				type = "admin";
   887 			}
   888 			name = cmd.pInfo->pCmd->GetName();
   889 			if (cmd.pHook->helptext.size())
   890 			{
   891 				help = cmd.pHook->helptext.c_str();
   892 			} else {
   893 				help = cmd.pInfo->pCmd->GetHelpText();
   894 			}
   895 			g_RootMenu.ConsolePrint("  %-17.16s %-12.11s %s", name, type, help);		
   896 		}
   897 
   898 		return;
   899 	}
   900 
   901 	g_RootMenu.ConsolePrint("[SM] Usage: sm cmds <plugin #>");
   902 }
   903 
   904 static int g_yam_state = 0;
   905 void _YamState(int state)
   906 {
   907 	if (state == 0)
   908 	{
   909 		g_RootMenu.ConsolePrint("Welcome to the SourceMod Text Adventure.");
   910 		g_RootMenu.ConsolePrint("Type sm_text to see the last message.");
   911 		g_RootMenu.ConsolePrint("Type sm_text <word> to follow a capital word.");
   912 		g_RootMenu.ConsolePrint("-------------------------------");
   913 		g_RootMenu.ConsolePrint("You are at VALVE HEADQUARTERS.");
   914 		g_RootMenu.ConsolePrint("To your LEFT is BAILOPAN, rearranging the letters to spell");
   915 		g_RootMenu.ConsolePrint("\"A HARD VEAL QUEST\".  FORWARD is the entrance to the building.");
   916 		g_RootMenu.ConsolePrint("To your RIGHT is your last chance to flee in terror.");
   917 	} else if (state == 1) {
   918 		g_RootMenu.ConsolePrint("BAILOPAN tells you that his name his pronounced");
   919 		g_RootMenu.ConsolePrint("\"bye low pahn,\" not \"bay low pan.\"  Do you ");
   920 		g_RootMenu.ConsolePrint("MOCK him, or NOD quietly?");
   921 	} else if (state == 2) {
   922 		g_RootMenu.ConsolePrint("You enter the Valve building.  You hear screams coming from within.");
   923 		g_RootMenu.ConsolePrint("A grotesque figure lumbers up to greet you; it is Gabe Newell.");
   924 		g_RootMenu.ConsolePrint("\"Welcome,\" he belches, \"to my lair.\"");
   925 		g_RootMenu.ConsolePrint("Do you SHAKE Gaben's hand, WALK past him, or OFFER a donut?");
   926 	} else if (state == 3) {
   927 		g_RootMenu.ConsolePrint("You walk into the break room.  Alfred \"Alf is from Melmac\" Reynolds and");
   928 		g_RootMenu.ConsolePrint("Yahn \"Yeti\" Bernier are discussing something (you overhear ");
   929 		g_RootMenu.ConsolePrint("the phrase \"and next Steam update, here's what we should break\").");
   930 		g_RootMenu.ConsolePrint("Should you DIE in a fire, REPORT a bug, REQUEST a feature, or ");
   931 		g_RootMenu.ConsolePrint("SPRAY them with butter?");
   932 	}
   933 	g_yam_state = state;
   934 }
   935 
   936 #if defined ORANGEBOX_BUILD
   937 void _IntExt_CallYams(const CCommand &cmd)
   938 {
   939 #else
   940 void _IntExt_CallYams()
   941 {
   942 	CCommand cmd;
   943 #endif
   944 	const char *arg = cmd.Arg(1);
   945 
   946 	/* should be impossible */
   947 	if (!arg || arg[0] == '\0')
   948 	{
   949 		_YamState(g_yam_state);
   950 	}
   951 
   952 	if (g_yam_state == 1)
   953 	{
   954 		if (strcasecmp(arg, "mock") == 0)
   955 		{
   956 			g_RootMenu.ConsolePrint("You mock BAILOPAN's pronunciation.  In a fit of rage, ");
   957 			g_RootMenu.ConsolePrint("he sticks an INT 3 call into your chest, rendering you broken.");
   958 			g_RootMenu.ConsolePrint("YOU HAVE DIED.  GAME OVER.");
   959 			g_yam_state = 0;
   960 		} else if (strcasecmp(arg, "nod") == 0) {
   961 			g_RootMenu.ConsolePrint("You nod quietly, and then slowly back away into the Valve headquarters.");
   962 			_YamState(2);
   963 		} else {
   964 			g_RootMenu.ConsolePrint("Commands are MOCK and NOD.");
   965 		}
   966 	} else if (g_yam_state == 3) {
   967 		if (strcasecmp(arg, "report") == 0)
   968 		{
   969 			g_RootMenu.ConsolePrint("You report a bug to Alfred and Yeti.  Immediately, both fall asleep.");
   970 			g_RootMenu.ConsolePrint("You decay in the break room for two years while they sleep.");
   971 			g_RootMenu.ConsolePrint("YOU HAVE DIED.  GAME OVER.");
   972 			g_yam_state = 0;
   973 		} else if (strcasecmp(arg, "request") == 0) {
   974 			g_RootMenu.ConsolePrint("You request a feature to Alfred and Yeti.  They both mutter something");
   975 			g_RootMenu.ConsolePrint("about it being implemented \"soon.\"  Then, by accident, someone sends");
   976 			g_RootMenu.ConsolePrint("a message over \"Friends.\"  The entire building catches fire.");
   977 			g_RootMenu.ConsolePrint("YOU HAVE DIED.  GAME OVER.");
   978 			g_yam_state = 0;
   979 		} else if (strcasecmp(arg, "die") == 0) {
   980 			g_RootMenu.ConsolePrint("For no reason, you suddenly catch fire.  Alfred and Yeti find this");
   981 			g_RootMenu.ConsolePrint("deeply disturbing, and cover your flaming corpse with Episode 2");
   982 			g_RootMenu.ConsolePrint("advertisements.  Coming soon, with Team Fortress 2, and Portal!");
   983 			g_RootMenu.ConsolePrint("YOU HAVE DIED.  GAME OVER.");
   984 			g_yam_state = 0;
   985 		} else if (strcasecmp(arg, "spray") == 0) {
   986 			g_RootMenu.ConsolePrint("You spray Alfred and Yeti with butter.  Like Jack Thompson to an");
   987 			g_RootMenu.ConsolePrint("ambulance, Gabe Newell instantly appears and devours them both.");
   988 			g_RootMenu.ConsolePrint("You run away, just in time, as Gabe Newell explodes, registering ");
   989 			g_RootMenu.ConsolePrint("a 5.6 earthquake.  Outside, world peace is achieved.");
   990 			g_RootMenu.ConsolePrint("YOU HAVE WON.");
   991 			g_yam_state = 0;
   992 		}
   993 	} else if (g_yam_state == 2) {
   994 		if (strcasecmp(arg, "shake") == 0)
   995 		{
   996 			g_RootMenu.ConsolePrint("You shake Gaben's hand.  It is a terrifying and disgusting experience.");
   997 			g_RootMenu.ConsolePrint("However, you survive, and continue on.");
   998 			_YamState(3);
   999 		} else if (strcasecmp(arg, "offer") == 0) {
  1000 			g_RootMenu.ConsolePrint("You offer Gabe Newell one (1) donut.  With a gleam in his eyes, ");
  1001 			g_RootMenu.ConsolePrint("he picks you up and devours you whole.");
  1002 			g_RootMenu.ConsolePrint("YOU HAVE DIED.  GAME OVER.");
  1003 			g_yam_state = 0;
  1004 		} else if (strcasecmp(arg, "walk") == 0) {
  1005 			g_RootMenu.ConsolePrint("You walk past Gabe Newell.  He can't keep up with your pace!");
  1006 			_YamState(3);
  1007 		} else {
  1008 			g_RootMenu.ConsolePrint("Commands are SHAKE, OFFER, and WALK.");
  1009 		}
  1010 	} else if (g_yam_state == 0) {
  1011 		if (strcasecmp(arg, "left") == 0)
  1012 		{
  1013 			_YamState(1);
  1014 		} else if (strcasecmp(arg, "right") == 0) {
  1015 			g_RootMenu.ConsolePrint("You run away from the Valve headquarters in sheer terror.");
  1016 			g_RootMenu.ConsolePrint("While running, you smash into an unknown person, who turns out to be your soul mate.");
  1017 			g_RootMenu.ConsolePrint("You marry and raise a family of 3 kids.");
  1018 			g_RootMenu.ConsolePrint("Many years later, you look back, and realize this was your best choice.");
  1019 			g_RootMenu.ConsolePrint("YOU HAVE WON.");
  1020 			g_yam_state = 0;
  1021 		} else if (strcasecmp(arg, "forward") == 0) {
  1022 			_YamState(2);
  1023 		} else if (arg[0] != '\0') {
  1024 			g_RootMenu.ConsolePrint("Commands are FORWARD, LEFT, and RIGHT.");
  1025 		}
  1026 	}
  1027 }
  1028 
  1029 void _IntExt_EnableYams()
  1030 {
  1031 	static ConCommand *pCmd = NULL;
  1032 	if (!pCmd)
  1033 	{
  1034 		pCmd = new ConCommand("sm_text", _IntExt_CallYams, "Fountain of Yams Adventure Game", FCVAR_GAMEDLL);
  1035 		g_RootMenu.ConsolePrint("Something is now enabled...");
  1036 	} else {
  1037 		g_RootMenu.ConsolePrint("Something is already enabled...");
  1038 	}
  1039 }