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