core/ConCmdManager.cpp
author David Anderson <dvander@alliedmods.net>
Sun Aug 19 18:34:36 2007 +0000 (2007-08-19)
changeset 1361 591f6d2fd11d
parent 1349 0d84046ec139
child 1369 bd9ccced1b1a
permissions -rw-r--r--
added helper function for panel text
     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 	/**
   207 	 * Note: Console commands will EITHER go through IServerGameDLL::ClientCommand,
   208 	 * OR this dispatch.  They will NEVER go through both.
   209 	 * -- 
   210 	 * Whether or not it goes through the callback is determined by FCVAR_GAMEDLL
   211 	 */
   212 	char cmd[300];
   213 	strncopy(cmd, engine->Cmd_Argv(0), sizeof(cmd));
   214 
   215 	ConCmdInfo *pInfo;
   216 	if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   217 	{
   218 		return;
   219 	}
   220 
   221 	cell_t result = Pl_Continue;
   222 	int args = engine->Cmd_Argc() - 1;
   223 
   224 	List<CmdHook *>::iterator iter;
   225 	CmdHook *pHook;
   226 
   227 	/* Execute server-only commands if viable */
   228 	if (m_CmdClient == 0 && pInfo->srvhooks.size())
   229 	{
   230 		cell_t tempres = result;
   231 		for (iter=pInfo->srvhooks.begin();
   232 			 iter!=pInfo->srvhooks.end();
   233 			 iter++)
   234 		{
   235 			pHook = (*iter);
   236 			if (!pHook->pf->IsRunnable())
   237 			{
   238 				continue;
   239 			}
   240 			pHook->pf->PushCell(args);
   241 			if (pHook->pf->Execute(&tempres) == SP_ERROR_NONE)
   242 			{
   243 				if (tempres > result)
   244 				{
   245 					result = tempres;
   246 				}
   247 				if (result == Pl_Stop)
   248 				{
   249 					break;
   250 				}
   251 			}
   252 		}
   253 	
   254 		/* Check if there's an early stop */
   255 		if (result >= Pl_Stop)
   256 		{
   257 			if (!pInfo->sourceMod)
   258 			{
   259 				RETURN_META(MRES_SUPERCEDE);
   260 			}
   261 			return;
   262 		}
   263 	}
   264 
   265 	/* Execute console commands */
   266 	if (pInfo->conhooks.size())
   267 	{
   268 		cell_t tempres = result;
   269 		for (iter=pInfo->conhooks.begin();
   270 			iter!=pInfo->conhooks.end();
   271 			iter++)
   272 		{
   273 			pHook = (*iter);
   274 			if (!pHook->pf->IsRunnable())
   275 			{
   276 				continue;
   277 			}
   278 			if (m_CmdClient 
   279 				&& pHook->pAdmin
   280 				&& !CheckAccess(m_CmdClient, cmd, pHook->pAdmin))
   281 			{
   282 				if (result < Pl_Handled)
   283 				{
   284 					result = Pl_Handled;
   285 				}
   286 				continue;
   287 			}
   288 
   289 			/* On a listen server, sometimes the server host's client index can be set as 0.
   290 			 * So index 1 is passed to the command callback to correct this potential problem.
   291 			 */
   292 			if (m_CmdClient == 0 && !engine->IsDedicatedServer())
   293 			{
   294 				pHook->pf->PushCell(1);
   295 			} else {
   296 				pHook->pf->PushCell(m_CmdClient);
   297 			}
   298 
   299 			pHook->pf->PushCell(args);
   300 			if (pHook->pf->Execute(&tempres) == SP_ERROR_NONE)
   301 			{
   302 				if (tempres > result)
   303 				{
   304 					result = tempres;
   305 				}
   306 				if (result == Pl_Stop)
   307 				{
   308 					break;
   309 				}
   310 			}
   311 		}
   312 	}
   313 
   314 	if (result >= Pl_Handled)
   315 	{
   316 		if (!pInfo->sourceMod)
   317 		{
   318 			RETURN_META(MRES_SUPERCEDE);
   319 		}
   320 		return;
   321 	}
   322 }
   323 
   324 bool ConCmdManager::CheckCommandAccess(int client, const char *cmd, FlagBits cmdflags)
   325 {
   326 	if (cmdflags == 0 || client == 0)
   327 	{
   328 		return true;
   329 	}
   330 
   331 	/* If running listen server, then client 1 is the server host and should have 'root' access */
   332 	if (client == 1 && !engine->IsDedicatedServer())
   333 	{
   334 		return true;
   335 	}
   336 
   337 	CPlayer *player = g_Players.GetPlayerByIndex(client);
   338 	if (!player 
   339 		|| player->GetEdict() == NULL
   340 		|| player->IsFakeClient())
   341 	{
   342 		return false;
   343 	}
   344 
   345 	AdminId adm = player->GetAdminId();
   346 	if (adm != INVALID_ADMIN_ID)
   347 	{
   348 		FlagBits bits = g_Admins.GetAdminFlags(adm, Access_Effective);
   349 
   350 		/* root knows all, WHOA */
   351 		if ((bits & ADMFLAG_ROOT) == ADMFLAG_ROOT)
   352 		{
   353 			return true;
   354 		}
   355 
   356 		/* Check for overrides
   357 		 * :TODO: is it worth optimizing this?
   358 		 */
   359 		unsigned int groups = g_Admins.GetAdminGroupCount(adm);
   360 		GroupId gid;
   361 		OverrideRule rule;
   362 		bool override = false;
   363 		for (unsigned int i=0; i<groups; i++)
   364 		{
   365 			gid = g_Admins.GetAdminGroup(adm, i, NULL);
   366 			/* First get group-level override */
   367 			override = g_Admins.GetGroupCommandOverride(gid, cmd, Override_CommandGroup, &rule);
   368 			/* Now get the specific command override */
   369 			if (g_Admins.GetGroupCommandOverride(gid, cmd, Override_Command, &rule))
   370 			{
   371 				override = true;
   372 			}
   373 			if (override)
   374 			{
   375 				if (rule == Command_Allow)
   376 				{
   377 					return true;
   378 				} else if (rule == Command_Deny) {
   379 					return false;
   380 				}
   381 			}
   382 		}
   383 
   384 		/* See if our other flags match */
   385 		if ((bits & cmdflags) == cmdflags)
   386 		{
   387 			return true;
   388 		}
   389 	}
   390 
   391 	return false;
   392 }
   393 
   394 bool ConCmdManager::CheckAccess(int client, const char *cmd, AdminCmdInfo *pAdmin)
   395 {
   396 	if (CheckCommandAccess(client, cmd, pAdmin->eflags))
   397 	{
   398 		return true;
   399 	}
   400 
   401 	edict_t *pEdict = engine->PEntityOfEntIndex(client);
   402 	
   403 	/* If we got here, the command failed... */
   404 	char buffer[128];
   405 	if (g_Translator.CoreTrans(client, buffer, sizeof(buffer), "No Access", NULL, NULL)
   406 		!= Trans_Okay)
   407 	{
   408 		snprintf(buffer, sizeof(buffer), "You do not have access to this command");
   409 	}
   410 
   411 	unsigned int replyto = g_ChatTriggers.GetReplyTo();
   412 	if (replyto == SM_REPLY_CONSOLE)
   413 	{
   414 		char fullbuffer[192];
   415 		snprintf(fullbuffer, sizeof(fullbuffer), "[SM] %s.\n", buffer);
   416 		engine->ClientPrintf(pEdict, fullbuffer);
   417 	} else if (replyto == SM_REPLY_CHAT) {
   418 		char fullbuffer[192];
   419 		snprintf(fullbuffer, sizeof(fullbuffer), "[SM] %s.", buffer);
   420 		g_HL2.TextMsg(client, HUD_PRINTTALK, fullbuffer);
   421 	}
   422 	
   423 	return false;
   424 }
   425 
   426 bool ConCmdManager::AddConsoleCommand(IPluginFunction *pFunction, 
   427 									   const char *name, 
   428 									   const char *description, 
   429 									   int flags)
   430 {
   431 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   432 
   433 	if (!pInfo)
   434 	{
   435 		return false;
   436 	}
   437 
   438 	CmdHook *pHook = new CmdHook();
   439 
   440 	pHook->pf = pFunction;
   441 	if (description && description[0])
   442 	{
   443 		pHook->helptext.assign(description);
   444 	}
   445 	pInfo->conhooks.push_back(pHook);
   446 
   447 	/* Add to the plugin */
   448 	CmdList *pList;
   449 	IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
   450 	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   451 	{
   452 		pList = new CmdList();
   453 		pPlugin->SetProperty("CommandList", pList);
   454 	}
   455 	PlCmdInfo info;
   456 	info.pInfo = pInfo;
   457 	info.type = Cmd_Console;
   458 	info.pHook = pHook;
   459 	AddToPlCmdList(pList, info);
   460 
   461 	return true;
   462 }
   463 
   464 bool ConCmdManager::AddAdminCommand(IPluginFunction *pFunction, 
   465 									 const char *name, 
   466 									 const char *group,
   467 									 int adminflags,
   468 									 const char *description, 
   469 									 int flags)
   470 {
   471 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   472 
   473 	if (!pInfo)
   474 	{
   475 		return false;
   476 	}
   477 
   478 	CmdHook *pHook = new CmdHook();
   479 	AdminCmdInfo *pAdmin = new AdminCmdInfo();
   480 
   481 	pHook->pf = pFunction;
   482 	if (description && description[0])
   483 	{
   484 		pHook->helptext.assign(description);
   485 	}
   486 	pHook->pAdmin = pAdmin;
   487 
   488 	void *object;
   489 	int grpid;
   490 	if (!sm_trie_retrieve(m_pCmdGrps, group, (void **)&object))
   491 	{
   492 		grpid = m_Strings.AddString(group);
   493 		sm_trie_insert(m_pCmdGrps, group, (void *)grpid);
   494 	} else {
   495 		grpid = (int)object;
   496 	}
   497 
   498 	pAdmin->cmdGrpId = grpid;
   499 	pAdmin->flags = adminflags;
   500 
   501 	/* First get the command group override, if any */
   502 	bool override = g_Admins.GetCommandOverride(group, 
   503 		Override_CommandGroup,
   504 		&(pAdmin->eflags));
   505 
   506 	/* Next get the command override, if any */
   507 	if (g_Admins.GetCommandOverride(name,
   508 		Override_Command,
   509 		&(pAdmin->eflags)))
   510 	{
   511 		override = true;
   512 	}
   513 
   514 	/* Assign normal flags if there were no overrides */
   515 	if (!override)
   516 	{
   517 		pAdmin->eflags = pAdmin->flags;
   518 	}
   519 
   520 	/* Finally, add the hook */
   521 	pInfo->conhooks.push_back(pHook);
   522 	pInfo->admin = *(pHook->pAdmin);
   523 	pInfo->is_admin_set = true;
   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 		pInfo->is_admin_set = true;
   669 	} else if (type == Override_CommandGroup) {
   670 		void *object;
   671 		if (!sm_trie_retrieve(m_pCmdGrps, cmd, &object))
   672 		{
   673 			return;
   674 		}
   675 		int grpid = (int)object;
   676 
   677 		/* This is bad :( loop through all commands */
   678 		List<ConCmdInfo *>::iterator iter;
   679 		CmdHook *pHook;
   680 		for (iter=m_CmdList.begin(); iter!=m_CmdList.end(); iter++)
   681 		{
   682 			pInfo = (*iter);
   683 			for (List<CmdHook *>::iterator citer=pInfo->conhooks.begin();
   684 				 citer!=pInfo->conhooks.end();
   685 				 citer++)
   686 			{
   687 				pHook = (*citer);
   688 				if (pHook->pAdmin && pHook->pAdmin->cmdGrpId == grpid)
   689 				{
   690 					if (bits)
   691 					{
   692 						pHook->pAdmin->eflags = bits;
   693 					} else {
   694 						pHook->pAdmin->eflags = pHook->pAdmin->flags;
   695 					}
   696 					pInfo->admin = *(pHook->pAdmin);
   697 				}
   698 			}
   699 		}
   700 		pInfo->is_admin_set = true;
   701 	}
   702 }
   703 
   704 void ConCmdManager::RemoveConCmd(ConCmdInfo *info)
   705 {
   706 	/* Remove console-specific information
   707 	 * This should always be true as of right now
   708 	 */
   709 	if (info->pCmd)
   710 	{
   711 		/* Remove from the trie */
   712 		sm_trie_delete(m_pCmds, info->pCmd->GetName());
   713 
   714 		if (info->sourceMod)
   715 		{
   716 			/* Unlink from SourceMM */
   717 			g_SMAPI->UnregisterConCmdBase(g_PLAPI, info->pCmd);
   718 			/* Delete the command's memory */
   719 			char *new_help = const_cast<char *>(info->pCmd->GetHelpText());
   720 			char *new_name = const_cast<char *>(info->pCmd->GetName());
   721 			delete [] new_help;
   722 			delete [] new_name;
   723 			delete info->pCmd;
   724 		} else {
   725 			/* Remove the external hook */
   726 			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, info->pCmd, CommandCallback, false);
   727 		}
   728 	}
   729 	
   730 	/* Remove from list */
   731 	m_CmdList.remove(info);
   732 
   733 	delete info;
   734 }
   735 
   736 bool ConCmdManager::LookForSourceModCommand(const char *cmd)
   737 {
   738 	ConCmdInfo *pInfo;
   739 
   740 	if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   741 	{
   742 		return false;
   743 	}
   744 
   745 	return pInfo->sourceMod && (pInfo->conhooks.size() > 0);
   746 }
   747 
   748 bool ConCmdManager::LookForCommandAdminFlags(const char *cmd, FlagBits *pFlags)
   749 {
   750 	ConCmdInfo *pInfo;
   751 
   752 	if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   753 	{
   754 		return false;
   755 	}
   756 
   757 	*pFlags = pInfo->admin.eflags;
   758 
   759 	return pInfo->is_admin_set;
   760 }
   761 
   762 ConCmdInfo *ConCmdManager::AddOrFindCommand(const char *name, const char *description, int flags)
   763 {
   764 	ConCmdInfo *pInfo;
   765 	if (!sm_trie_retrieve(m_pCmds, name, (void **)&pInfo))
   766 	{
   767 		pInfo = new ConCmdInfo();
   768 		/* Find the commandopan */
   769 		ConCommandBase *pBase = icvar->GetCommands();
   770 		ConCommand *pCmd = NULL;
   771 		while (pBase)
   772 		{
   773 			if (strcmp(pBase->GetName(), name) == 0)
   774 			{
   775 				/* Don't want to return convar with same name */
   776 				if (!pBase->IsCommand())
   777 				{
   778 					return NULL;
   779 				}
   780 
   781 				pCmd = (ConCommand *)pBase;
   782 				break;
   783 			}
   784 			pBase = const_cast<ConCommandBase *>(pBase->GetNext());
   785 		}
   786 
   787 		if (!pCmd)
   788 		{
   789 			/* Note that we have to duplicate because the source might not be 
   790 			 * a static string, and these expect static memory.
   791 			 */
   792 			if (!description)
   793 			{
   794 				description = "";
   795 			}
   796 			char *new_name = sm_strdup(name);
   797 			char *new_help = sm_strdup(description);
   798 			pCmd = new ConCommand(new_name, CommandCallback, new_help, flags);
   799 			pInfo->sourceMod = true;
   800 		} else {
   801 			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, pCmd, CommandCallback, false);
   802 		}
   803 
   804 		pInfo->pCmd = pCmd;
   805 		pInfo->is_admin_set = false;
   806 
   807 		sm_trie_insert(m_pCmds, name, pInfo);
   808 		AddToCmdList(pInfo);
   809 	}
   810 
   811 	return pInfo;
   812 }
   813 
   814 void ConCmdManager::OnRootConsoleCommand(const char *command, unsigned int argcount)
   815 {
   816 	if (argcount >= 3)
   817 	{
   818 		const char *text = engine->Cmd_Argv(2);
   819 
   820 		int id = atoi(text);
   821 		CPlugin *pPlugin = g_PluginSys.GetPluginByOrder(id);
   822 
   823 		if (!pPlugin)
   824 		{
   825 			g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", id);
   826 			return;
   827 		}
   828 
   829 		const sm_plugininfo_t *plinfo = pPlugin->GetPublicInfo();
   830 		const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : pPlugin->GetFilename();
   831 
   832 		CmdList *pList;
   833 		if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   834 		{
   835 			g_RootMenu.ConsolePrint("[SM] No commands found for: %s", plname);
   836 			return;
   837 		}
   838 		if (!pList->size())
   839 		{
   840 			g_RootMenu.ConsolePrint("[SM] No commands found for: %s", plname);
   841 			return;
   842 		}
   843 
   844 		CmdList::iterator iter;
   845 		const char *type = NULL;
   846 		const char *name;
   847 		const char *help;
   848 		g_RootMenu.ConsolePrint("[SM] Listing %d commands for: %s", pList->size(), plname);
   849 		g_RootMenu.ConsolePrint("  %-17.16s %-8.7s %s", "[Name]", "[Type]", "[Help]");
   850 		for (iter=pList->begin();
   851 			 iter!=pList->end();
   852 			 iter++, id++)
   853 		{
   854 			PlCmdInfo &cmd = (*iter);
   855 			if (cmd.type == Cmd_Server)
   856 			{
   857 				type = "server";
   858 			} else if (cmd.type == Cmd_Console) {
   859 				type = "console";
   860 			} else if (cmd.type == Cmd_Admin) {
   861 				type = "admin";
   862 			}
   863 			name = cmd.pInfo->pCmd->GetName();
   864 			if (cmd.pHook->helptext.size())
   865 			{
   866 				help = cmd.pHook->helptext.c_str();
   867 			} else {
   868 				help = cmd.pInfo->pCmd->GetHelpText();
   869 			}
   870 			g_RootMenu.ConsolePrint("  %-17.16s %-12.11s %s", name, type, help);		
   871 		}
   872 
   873 		return;
   874 	}
   875 
   876 	g_RootMenu.ConsolePrint("[SM] Usage: sm cmds <plugin #>");
   877 }
   878 
   879 static int g_yam_state = 0;
   880 void _YamState(int state)
   881 {
   882 	if (state == 0)
   883 	{
   884 		g_RootMenu.ConsolePrint("Welcome to the SourceMod Text Adventure.");
   885 		g_RootMenu.ConsolePrint("Type sm_text to see the last message.");
   886 		g_RootMenu.ConsolePrint("Type sm_text <word> to follow a capital word.");
   887 		g_RootMenu.ConsolePrint("-------------------------------");
   888 		g_RootMenu.ConsolePrint("You are at VALVE HEADQUARTERS.");
   889 		g_RootMenu.ConsolePrint("To your LEFT is BAILOPAN, rearranging the letters to spell");
   890 		g_RootMenu.ConsolePrint("\"A HARD VEAL QUEST\".  FORWARD is the entrance to the building.");
   891 		g_RootMenu.ConsolePrint("To your RIGHT is your last chance to flee in terror.");
   892 	} else if (state == 1) {
   893 		g_RootMenu.ConsolePrint("BAILOPAN tells you that his name his pronounced");
   894 		g_RootMenu.ConsolePrint("\"bye low pahn,\" not \"bay low pan.\"  Do you ");
   895 		g_RootMenu.ConsolePrint("MOCK him, or NOD quietly?");
   896 	} else if (state == 2) {
   897 		g_RootMenu.ConsolePrint("You enter the Valve building.  You hear screams coming from within.");
   898 		g_RootMenu.ConsolePrint("A grotesque figure lumbers up to greet you; it is Gabe Newell.");
   899 		g_RootMenu.ConsolePrint("\"Welcome,\" he belches, \"to my lair.\"");
   900 		g_RootMenu.ConsolePrint("Do you SHAKE Gaben's hand, WALK past him, or OFFER a donut?");
   901 	} else if (state == 3) {
   902 		g_RootMenu.ConsolePrint("You walk into the break room.  Alfred \"Alf is from Melmac\" Reynolds and");
   903 		g_RootMenu.ConsolePrint("Yahn \"Yeti\" Bernier are discussing something (you overhear ");
   904 		g_RootMenu.ConsolePrint("the phrase \"and next Steam update, here's what we should break\").");
   905 		g_RootMenu.ConsolePrint("Should you DIE in a fire, REPORT a bug, REQUEST a feature, or ");
   906 		g_RootMenu.ConsolePrint("SPRAY them with butter?");
   907 	}
   908 	g_yam_state = state;
   909 }
   910 
   911 void _IntExt_CallYams()
   912 {
   913 	const char *arg = engine->Cmd_Argv(1);
   914 
   915 	/* should be impossible */
   916 	if (!arg || arg[0] == '\0')
   917 	{
   918 		_YamState(g_yam_state);
   919 	}
   920 
   921 	if (g_yam_state == 1)
   922 	{
   923 		if (strcasecmp(arg, "mock") == 0)
   924 		{
   925 			g_RootMenu.ConsolePrint("You mock BAILOPAN's pronunciation.  In a fit of rage, ");
   926 			g_RootMenu.ConsolePrint("he sticks an INT 3 call into your chest, rendering you broken.");
   927 			g_RootMenu.ConsolePrint("YOU HAVE DIED.  GAME OVER.");
   928 			g_yam_state = 0;
   929 		} else if (strcasecmp(arg, "nod") == 0) {
   930 			g_RootMenu.ConsolePrint("You nod quietly, and then slowly back away into the Valve headquarters.");
   931 			_YamState(2);
   932 		} else {
   933 			g_RootMenu.ConsolePrint("Commands are MOCK and NOD.");
   934 		}
   935 	} else if (g_yam_state == 3) {
   936 		if (strcasecmp(arg, "report") == 0)
   937 		{
   938 			g_RootMenu.ConsolePrint("You report a bug to Alfred and Yeti.  Immediately, both fall asleep.");
   939 			g_RootMenu.ConsolePrint("You decay in the break room for two years while they sleep.");
   940 			g_RootMenu.ConsolePrint("YOU HAVE DIED.  GAME OVER.");
   941 			g_yam_state = 0;
   942 		} else if (strcasecmp(arg, "request") == 0) {
   943 			g_RootMenu.ConsolePrint("You request a feature to Alfred and Yeti.  They both mutter something");
   944 			g_RootMenu.ConsolePrint("about it being implemented \"soon.\"  Then, by accident, someone sends");
   945 			g_RootMenu.ConsolePrint("a message over \"Friends.\"  The entire building catches fire.");
   946 			g_RootMenu.ConsolePrint("YOU HAVE DIED.  GAME OVER.");
   947 			g_yam_state = 0;
   948 		} else if (strcasecmp(arg, "die") == 0) {
   949 			g_RootMenu.ConsolePrint("For no reason, you suddenly catch fire.  Alfred and Yeti find this");
   950 			g_RootMenu.ConsolePrint("deeply disturbing, and cover your flaming corpse with Episode 2");
   951 			g_RootMenu.ConsolePrint("advertisements.  Coming soon, with Team Fortress 2, and Portal!");
   952 			g_RootMenu.ConsolePrint("YOU HAVE DIED.  GAME OVER.");
   953 			g_yam_state = 0;
   954 		} else if (strcasecmp(arg, "spray") == 0) {
   955 			g_RootMenu.ConsolePrint("You spray Alfred and Yeti with butter.  Like Jack Thompson to an");
   956 			g_RootMenu.ConsolePrint("ambulance, Gabe Newell instantly appears and devours them both.");
   957 			g_RootMenu.ConsolePrint("You run away, just in time, as Gabe Newell explodes, registering ");
   958 			g_RootMenu.ConsolePrint("a 5.6 earthquake.  Outside, world peace is achieved.");
   959 			g_RootMenu.ConsolePrint("YOU HAVE WON.");
   960 			g_yam_state = 0;
   961 		}
   962 	} else if (g_yam_state == 2) {
   963 		if (strcasecmp(arg, "shake") == 0)
   964 		{
   965 			g_RootMenu.ConsolePrint("You shake Gaben's hand.  It is a terrifying and disgusting experience.");
   966 			g_RootMenu.ConsolePrint("However, you survive, and continue on.");
   967 			_YamState(3);
   968 		} else if (strcasecmp(arg, "offer") == 0) {
   969 			g_RootMenu.ConsolePrint("You offer Gabe Newell one (1) donut.  With a gleam in his eyes, ");
   970 			g_RootMenu.ConsolePrint("he picks you up and devours you whole.");
   971 			g_RootMenu.ConsolePrint("YOU HAVE DIED.  GAME OVER.");
   972 			g_yam_state = 0;
   973 		} else if (strcasecmp(arg, "walk") == 0) {
   974 			g_RootMenu.ConsolePrint("You walk past Gabe Newell.  He can't keep up with your pace!");
   975 			_YamState(3);
   976 		} else {
   977 			g_RootMenu.ConsolePrint("Commands are SHAKE, OFFER, and WALK.");
   978 		}
   979 	} else if (g_yam_state == 0) {
   980 		if (strcasecmp(arg, "left") == 0)
   981 		{
   982 			_YamState(1);
   983 		} else if (strcasecmp(arg, "right") == 0) {
   984 			g_RootMenu.ConsolePrint("You run away from the Valve headquarters in sheer terror.");
   985 			g_RootMenu.ConsolePrint("While running, you smash into an unknown person, who turns out to be your soul mate.");
   986 			g_RootMenu.ConsolePrint("You marry and raise a family of 3 kids.");
   987 			g_RootMenu.ConsolePrint("Many years later, you look back, and realize this was your best choice.");
   988 			g_RootMenu.ConsolePrint("YOU HAVE WON.");
   989 			g_yam_state = 0;
   990 		} else if (strcasecmp(arg, "forward") == 0) {
   991 			_YamState(2);
   992 		} else if (arg[0] != '\0') {
   993 			g_RootMenu.ConsolePrint("Commands are FORWARD, LEFT, and RIGHT.");
   994 		}
   995 	}
   996 }
   997 
   998 void _IntExt_EnableYams()
   999 {
  1000 	static ConCommand *pCmd = NULL;
  1001 	if (!pCmd)
  1002 	{
  1003 		pCmd = new ConCommand("sm_text", _IntExt_CallYams, "Fountain of Yams Adventure Game", FCVAR_GAMEDLL);
  1004 		g_RootMenu.ConsolePrint("Something is now enabled...");
  1005 	} else {
  1006 		g_RootMenu.ConsolePrint("Something is already enabled...");
  1007 	}
  1008 }