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