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