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