core/ConCmdManager.cpp
author David Anderson <dvander@alliedmods.net>
Sun Nov 16 16:06:18 2008 -0800 (2008-11-16)
changeset 2411 8dfaa3d730b5
parent 2289 44045ead61cd
child 2400 76b7b4575e21
permissions -rw-r--r--
Removed pointless "systems" folder from core.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 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 #if defined ORANGEBOX_BUILD
    44 	SH_DECL_HOOK1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
    45 #else
    46 	SH_DECL_HOOK0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
    47 #endif
    48 
    49 SH_DECL_HOOK1_void(IServerGameClients, SetCommandClient, SH_NOATTRIB, false, int);
    50 
    51 struct PlCmdInfo
    52 {
    53 	ConCmdInfo *pInfo;
    54 	CmdHook *pHook;
    55 	CmdType type;
    56 };
    57 typedef List<PlCmdInfo> CmdList;
    58 
    59 void AddToPlCmdList(CmdList *pList, const PlCmdInfo &info);
    60 
    61 ConCmdManager::ConCmdManager() : m_Strings(1024)
    62 {
    63 	m_pCmds = sm_trie_create();
    64 	m_pCmdGrps = sm_trie_create();
    65 	m_CmdClient = 0;
    66 }
    67 
    68 ConCmdManager::~ConCmdManager()
    69 {
    70 	sm_trie_destroy(m_pCmds);
    71 	sm_trie_destroy(m_pCmdGrps);
    72 }
    73 
    74 void ConCmdManager::OnSourceModAllInitialized()
    75 {
    76 	g_PluginSys.AddPluginsListener(this);
    77 	g_RootMenu.AddRootConsoleCommand("cmds", "List console commands", this);
    78 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, serverClients, this, &ConCmdManager::SetCommandClient, false);
    79 }
    80 
    81 void ConCmdManager::OnSourceModShutdown()
    82 {
    83 	/* All commands should already be removed by the time we're done */
    84 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, serverClients, this, &ConCmdManager::SetCommandClient, false);
    85 	g_RootMenu.RemoveRootConsoleCommand("cmds", this);
    86 }
    87 
    88 void ConCmdManager::OnUnlinkConCommandBase(ConCommandBase *pBase, const char *name, bool is_read_safe)
    89 {
    90 	/* Whoa, first get its information struct */
    91 	ConCmdInfo *pInfo;
    92 
    93 	if (!sm_trie_retrieve(m_pCmds, name, (void **)&pInfo))
    94 	{
    95 		return;
    96 	}
    97 
    98 	RemoveConCmds(pInfo->srvhooks);
    99 	RemoveConCmds(pInfo->conhooks);
   100 
   101 	RemoveConCmd(pInfo, name, is_read_safe, false);
   102 }
   103 
   104 void ConCmdManager::RemoveConCmds(List<CmdHook *> &cmdlist)
   105 {
   106 	List<CmdHook *>::iterator iter = cmdlist.begin();
   107 
   108 	while (iter != cmdlist.end())
   109 	{
   110 		CmdHook *pHook = (*iter);
   111 		IPluginContext *pContext = pHook->pf->GetParentContext();
   112 		IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
   113 		CmdList *pList = NULL;
   114 		
   115 		//gaben
   116 		if (!pPlugin->GetProperty("CommandList", (void **)&pList, false) || !pList)
   117 		{
   118 			continue;
   119 		}
   120 
   121 		CmdList::iterator p_iter = pList->begin();
   122 		while (p_iter != pList->end())
   123 		{
   124 			PlCmdInfo &cmd = (*p_iter);
   125 			if (cmd.pHook == pHook)
   126 			{
   127 				p_iter = pList->erase(p_iter);
   128 			}
   129 			else
   130 			{
   131 				p_iter++;
   132 			}
   133 		}
   134 
   135 		delete pHook->pAdmin;
   136 		delete pHook;
   137 		
   138 		iter = cmdlist.erase(iter);
   139 	}
   140 }
   141 
   142 void ConCmdManager::RemoveConCmds(List<CmdHook *> &cmdlist, IPluginContext *pContext)
   143 {
   144 	List<CmdHook *>::iterator iter = cmdlist.begin();
   145 	CmdHook *pHook;
   146 
   147 	while (iter != cmdlist.end())
   148 	{
   149 		pHook = (*iter);
   150 		if (pHook->pf->GetParentContext() == pContext)
   151 		{
   152 			delete pHook->pAdmin;
   153 			delete pHook;
   154 			iter = cmdlist.erase(iter);
   155 		}
   156 		else
   157 		{
   158 			iter++;
   159 		}
   160 	}
   161 }
   162 
   163 void ConCmdManager::OnPluginDestroyed(IPlugin *plugin)
   164 {
   165 	CmdList *pList;
   166 	List<ConCmdInfo *> removed;
   167 	if (plugin->GetProperty("CommandList", (void **)&pList, true))
   168 	{
   169 		IPluginContext *pContext = plugin->GetBaseContext();
   170 		CmdList::iterator iter;
   171 		/* First pass!
   172 	 	 * Only bother if there's an actual command list on this plugin... 
   173 	 	 */
   174 		for (iter=pList->begin();
   175 			iter!=pList->end();
   176 			iter++)
   177 		{
   178 			PlCmdInfo &cmd = (*iter);
   179 			ConCmdInfo *pInfo = cmd.pInfo;
   180 
   181 			/* Has this chain already been fully cleaned/removed? */
   182 			if (removed.find(pInfo) != removed.end())
   183 			{
   184 				continue;
   185 			}
   186 
   187 			/* Remove any hooks from us on this command */
   188 			RemoveConCmds(pInfo->conhooks, pContext);
   189 			RemoveConCmds(pInfo->srvhooks, pContext);
   190 
   191 			/* See if there are still hooks */
   192 			if (pInfo->srvhooks.size())
   193 			{
   194 				continue;
   195 			}
   196 			if (pInfo->conhooks.size())
   197 			{
   198 				continue;
   199 			}
   200 
   201 			/* Remove the command, it should be safe now */
   202 			RemoveConCmd(pInfo, pInfo->pCmd->GetName(), true, true);
   203 			removed.push_back(pInfo);
   204 		}
   205 		delete pList;
   206 	}
   207 }
   208 #if defined ORANGEBOX_BUILD
   209 void CommandCallback(const CCommand &command)
   210 {
   211 #else
   212 void CommandCallback()
   213 {
   214 	CCommand command;
   215 #endif
   216 
   217 	g_HL2.PushCommandStack(&command);
   218 
   219 	g_ConCmds.InternalDispatch(command);
   220 
   221 	g_HL2.PopCommandStack();
   222 }
   223 
   224 void ConCmdManager::SetCommandClient(int client)
   225 {
   226 	m_CmdClient = client + 1;
   227 }
   228 
   229 ResultType ConCmdManager::DispatchClientCommand(int client, const char *cmd, int args, ResultType type)
   230 {
   231 	ConCmdInfo *pInfo;
   232 	if (sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   233 	{
   234 		cell_t result = type;
   235 		cell_t tempres = result;
   236 		List<CmdHook *>::iterator iter;
   237 		CmdHook *pHook;
   238 		for (iter=pInfo->conhooks.begin();
   239 			 iter!=pInfo->conhooks.end();
   240 			 iter++)
   241 		{
   242 			pHook = (*iter);
   243 			if (!pHook->pf->IsRunnable())
   244 			{
   245 				continue;
   246 			}
   247 			if (pHook->pAdmin && !CheckAccess(client, cmd, pHook->pAdmin))
   248 			{
   249 				if (result < Pl_Handled)
   250 				{
   251 					result = Pl_Handled;
   252 				}
   253 				continue;
   254 			}
   255 			pHook->pf->PushCell(client);
   256 			pHook->pf->PushCell(args);
   257 			if (pHook->pf->Execute(&tempres) == SP_ERROR_NONE)
   258 			{
   259 				if (tempres > result)
   260 				{
   261 					result = tempres;
   262 				}
   263 				if (result == Pl_Stop)
   264 				{
   265 					break;
   266 				}
   267 			}
   268 		}
   269 		type = (ResultType)result;
   270 	}
   271 
   272 	return type;
   273 }
   274 
   275 void ConCmdManager::InternalDispatch(const CCommand &command)
   276 {
   277 	int client = m_CmdClient;
   278 
   279 	if (client)
   280 	{
   281 		CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   282 		if (!pPlayer || !pPlayer->IsConnected())
   283 		{
   284 			return;
   285 		}
   286 	}
   287 
   288 	/**
   289 	 * Note: Console commands will EITHER go through IServerGameDLL::ClientCommand,
   290 	 * OR this dispatch.  They will NEVER go through both.
   291 	 * -- 
   292 	 * Whether or not it goes through the callback is determined by FCVAR_GAMEDLL
   293 	 */
   294 	const char *cmd = g_HL2.CurrentCommandName();
   295 
   296 	ConCmdInfo *pInfo;
   297 	if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   298 	{
   299         /* Unfortunately, we now have to do a slow lookup because Valve made client commands 
   300          * case-insensitive.  We can't even use our sortedness.
   301          */
   302         if (client == 0 && !engine->IsDedicatedServer())
   303         {
   304             return;
   305         }
   306 
   307         List<ConCmdInfo *>::iterator iter;
   308 
   309         pInfo = NULL;
   310         iter = m_CmdList.begin();
   311         while (iter != m_CmdList.end())
   312         {
   313             if (strcasecmp((*iter)->pCmd->GetName(), cmd) == 0)
   314             {
   315                 pInfo = (*iter);
   316                 break;
   317             }
   318             iter++;
   319         }
   320         
   321 		if (pInfo == NULL)
   322         {
   323             return;
   324         }
   325 	}
   326 
   327 	/* This is a hack to prevent say triggers from firing on messages that were 
   328 	 * blocked because of flooding.  We won't remove this, but the hack will get 
   329 	 * "nicer" when we expose explicit say hooks.
   330 	 */
   331 	if (g_ChatTriggers.WasFloodedMessage())
   332 	{
   333 		return;
   334 	}
   335 
   336 	cell_t result = Pl_Continue;
   337 	int args = command.ArgC() - 1;
   338 
   339 	List<CmdHook *>::iterator iter;
   340 	CmdHook *pHook;
   341 
   342 	/* Execute server-only commands if viable */
   343 	if (client == 0 && pInfo->srvhooks.size())
   344 	{
   345 		cell_t tempres = result;
   346 		for (iter=pInfo->srvhooks.begin();
   347 			 iter!=pInfo->srvhooks.end();
   348 			 iter++)
   349 		{
   350 			pHook = (*iter);
   351 			if (!pHook->pf->IsRunnable())
   352 			{
   353 				continue;
   354 			}
   355 			pHook->pf->PushCell(args);
   356 			if (pHook->pf->Execute(&tempres) == SP_ERROR_NONE)
   357 			{
   358 				if (tempres > result)
   359 				{
   360 					result = tempres;
   361 				}
   362 				if (result == Pl_Stop)
   363 				{
   364 					break;
   365 				}
   366 			}
   367 		}
   368 	
   369 		/* Check if there's an early stop */
   370 		if (result >= Pl_Stop)
   371 		{
   372 			if (!pInfo->sourceMod)
   373 			{
   374 				RETURN_META(MRES_SUPERCEDE);
   375 			}
   376 			return;
   377 		}
   378 	}
   379 
   380 	/* Execute console commands */
   381 	if (pInfo->conhooks.size())
   382 	{
   383 		cell_t tempres = result;
   384 		for (iter=pInfo->conhooks.begin();
   385 			iter!=pInfo->conhooks.end();
   386 			iter++)
   387 		{
   388 			pHook = (*iter);
   389 			if (!pHook->pf->IsRunnable())
   390 			{
   391 				continue;
   392 			}
   393 			if (client 
   394 				&& pHook->pAdmin
   395 				&& !CheckAccess(client, cmd, pHook->pAdmin))
   396 			{
   397 				if (result < Pl_Handled)
   398 				{
   399 					result = Pl_Handled;
   400 				}
   401 				continue;
   402 			}
   403 
   404 			/* On a listen server, sometimes the server host's client index can be set as 0.
   405 			 * So index 1 is passed to the command callback to correct this potential problem.
   406 			 */
   407 			if (!engine->IsDedicatedServer())
   408 			{
   409 				client = g_Players.ListenClient();
   410 			}
   411 
   412 			pHook->pf->PushCell(client);
   413 			pHook->pf->PushCell(args);
   414 
   415 			if (pHook->pf->Execute(&tempres) == SP_ERROR_NONE)
   416 			{
   417 				if (tempres > result)
   418 				{
   419 					result = tempres;
   420 				}
   421 				if (result == Pl_Stop)
   422 				{
   423 					break;
   424 				}
   425 			}
   426 		}
   427 	}
   428 
   429 	if (result >= Pl_Handled)
   430 	{
   431 		if (!pInfo->sourceMod)
   432 		{
   433 			RETURN_META(MRES_SUPERCEDE);
   434 		}
   435 		return;
   436 	}
   437 }
   438 
   439 bool ConCmdManager::CheckCommandAccess(int client, const char *cmd, FlagBits cmdflags)
   440 {
   441 	if (cmdflags == 0 || client == 0)
   442 	{
   443 		return true;
   444 	}
   445 
   446 	/* If running listen server, then client 1 is the server host and should have 'root' access */
   447 	if (client == 1 && !engine->IsDedicatedServer())
   448 	{
   449 		return true;
   450 	}
   451 
   452 	CPlayer *player = g_Players.GetPlayerByIndex(client);
   453 	if (!player 
   454 		|| player->GetEdict() == NULL
   455 		|| player->IsFakeClient())
   456 	{
   457 		return false;
   458 	}
   459 
   460 	AdminId adm = player->GetAdminId();
   461 	if (adm != INVALID_ADMIN_ID)
   462 	{
   463 		FlagBits bits = g_Admins.GetAdminFlags(adm, Access_Effective);
   464 
   465 		/* root knows all, WHOA */
   466 		if ((bits & ADMFLAG_ROOT) == ADMFLAG_ROOT)
   467 		{
   468 			return true;
   469 		}
   470 
   471 		/* Check for overrides
   472 		 * :TODO: is it worth optimizing this?
   473 		 */
   474 		unsigned int groups = g_Admins.GetAdminGroupCount(adm);
   475 		GroupId gid;
   476 		OverrideRule rule;
   477 		bool override = false;
   478 		for (unsigned int i=0; i<groups; i++)
   479 		{
   480 			gid = g_Admins.GetAdminGroup(adm, i, NULL);
   481 			/* First get group-level override */
   482 			override = g_Admins.GetGroupCommandOverride(gid, cmd, Override_CommandGroup, &rule);
   483 			/* Now get the specific command override */
   484 			if (g_Admins.GetGroupCommandOverride(gid, cmd, Override_Command, &rule))
   485 			{
   486 				override = true;
   487 			}
   488 			if (override)
   489 			{
   490 				if (rule == Command_Allow)
   491 				{
   492 					return true;
   493 				}
   494 				else if (rule == Command_Deny)
   495 				{
   496 					return false;
   497 				}
   498 			}
   499 		}
   500 
   501 		/* See if our other flags match */
   502 		if ((bits & cmdflags) == cmdflags)
   503 		{
   504 			return true;
   505 		}
   506 	}
   507 
   508 	return false;
   509 }
   510 
   511 bool ConCmdManager::CheckAccess(int client, const char *cmd, AdminCmdInfo *pAdmin)
   512 {
   513 	if (CheckCommandAccess(client, cmd, pAdmin->eflags))
   514 	{
   515 		return true;
   516 	}
   517 
   518 	edict_t *pEdict = engine->PEntityOfEntIndex(client);
   519 	
   520 	/* If we got here, the command failed... */
   521 	char buffer[128];
   522 	if (!CoreTranslate(buffer, sizeof(buffer), "%T", 2, NULL, "No Access", &client))
   523 	{
   524 		UTIL_Format(buffer, sizeof(buffer), "You do not have access to this command");
   525 	}
   526 
   527 	unsigned int replyto = g_ChatTriggers.GetReplyTo();
   528 	if (replyto == SM_REPLY_CONSOLE)
   529 	{
   530 		char fullbuffer[192];
   531 		UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s.\n", buffer);
   532 		engine->ClientPrintf(pEdict, fullbuffer);
   533 	}
   534 	else if (replyto == SM_REPLY_CHAT)
   535 	{
   536 		char fullbuffer[192];
   537 		UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s.", buffer);
   538 		g_HL2.TextMsg(client, HUD_PRINTTALK, fullbuffer);
   539 	}
   540 	
   541 	return false;
   542 }
   543 
   544 bool ConCmdManager::AddConsoleCommand(IPluginFunction *pFunction, 
   545 									   const char *name, 
   546 									   const char *description, 
   547 									   int flags)
   548 {
   549 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   550 
   551 	if (!pInfo)
   552 	{
   553 		return false;
   554 	}
   555 
   556 	CmdHook *pHook = new CmdHook();
   557 
   558 	pHook->pf = pFunction;
   559 	if (description && description[0])
   560 	{
   561 		pHook->helptext.assign(description);
   562 	}
   563 	pInfo->conhooks.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_Console;
   576 	info.pHook = pHook;
   577 	AddToPlCmdList(pList, info);
   578 
   579 	return true;
   580 }
   581 
   582 bool ConCmdManager::AddAdminCommand(IPluginFunction *pFunction, 
   583 									 const char *name, 
   584 									 const char *group,
   585 									 int adminflags,
   586 									 const char *description, 
   587 									 int flags)
   588 {
   589 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   590 
   591 	if (!pInfo)
   592 	{
   593 		return false;
   594 	}
   595 
   596 	CmdHook *pHook = new CmdHook();
   597 	AdminCmdInfo *pAdmin = new AdminCmdInfo();
   598 
   599 	pHook->pf = pFunction;
   600 	if (description && description[0])
   601 	{
   602 		pHook->helptext.assign(description);
   603 	}
   604 	pHook->pAdmin = pAdmin;
   605 
   606 	void *object;
   607 	int grpid;
   608 	if (!sm_trie_retrieve(m_pCmdGrps, group, (void **)&object))
   609 	{
   610 		grpid = m_Strings.AddString(group);
   611 		sm_trie_insert(m_pCmdGrps, group, (void *)grpid);
   612 	}
   613 	else
   614 	{
   615 		grpid = (int)object;
   616 	}
   617 
   618 	pAdmin->cmdGrpId = grpid;
   619 	pAdmin->flags = adminflags;
   620 
   621 	/* First get the command group override, if any */
   622 	bool override = g_Admins.GetCommandOverride(group, 
   623 		Override_CommandGroup,
   624 		&(pAdmin->eflags));
   625 
   626 	/* Next get the command override, if any */
   627 	if (g_Admins.GetCommandOverride(name,
   628 		Override_Command,
   629 		&(pAdmin->eflags)))
   630 	{
   631 		override = true;
   632 	}
   633 
   634 	/* Assign normal flags if there were no overrides */
   635 	if (!override)
   636 	{
   637 		pAdmin->eflags = pAdmin->flags;
   638 	}
   639 
   640 	/* Finally, add the hook */
   641 	pInfo->conhooks.push_back(pHook);
   642 	pInfo->admin = *(pHook->pAdmin);
   643 	pInfo->is_admin_set = true;
   644 
   645 	/* Now add to the plugin */
   646 	CmdList *pList;
   647 	IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
   648 	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   649 	{
   650 		pList = new CmdList();
   651 		pPlugin->SetProperty("CommandList", pList);
   652 	}
   653 	PlCmdInfo info;
   654 	info.pInfo = pInfo;
   655 	info.type = Cmd_Admin;
   656 	info.pHook = pHook;
   657 	AddToPlCmdList(pList, info);
   658 
   659 	return true;
   660 }
   661 
   662 bool ConCmdManager::AddServerCommand(IPluginFunction *pFunction, 
   663 									  const char *name, 
   664 									  const char *description, 
   665 									  int flags)
   666 
   667 {
   668 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   669 
   670 	if (!pInfo)
   671 	{
   672 		return false;
   673 	}
   674 
   675 	CmdHook *pHook = new CmdHook();
   676 
   677 	pHook->pf = pFunction;
   678 	if (description && description[0])
   679 	{
   680 		pHook->helptext.assign(description);
   681 	}
   682 
   683 	pInfo->srvhooks.push_back(pHook);
   684 
   685 	/* Add to the plugin */
   686 	CmdList *pList;
   687 	IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
   688 	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   689 	{
   690 		pList = new CmdList();
   691 		pPlugin->SetProperty("CommandList", pList);
   692 	}
   693 	PlCmdInfo info;
   694 	info.pInfo = pInfo;
   695 	info.type = Cmd_Server;
   696 	info.pHook = pHook;
   697 	AddToPlCmdList(pList, info);
   698 
   699 	return true;
   700 }
   701 
   702 void AddToPlCmdList(CmdList *pList, const PlCmdInfo &info)
   703 {
   704 	CmdList::iterator iter = pList->begin();
   705 	bool inserted = false;
   706 	const char *orig = NULL;
   707 
   708 	orig = info.pInfo->pCmd->GetName();
   709 
   710 	/* Insert this into the help list, SORTED alphabetically. */
   711 	while (iter != pList->end())
   712 	{
   713 		PlCmdInfo &obj = (*iter);
   714 		const char *cmd = obj.pInfo->pCmd->GetName();
   715 		if (strcmp(orig, cmd) < 0)
   716 		{
   717 			pList->insert(iter, info);
   718 			inserted = true;
   719 			break;
   720 		}
   721 		iter++;
   722 	}
   723 
   724 	if (!inserted)
   725 	{
   726 		pList->push_back(info);
   727 	}
   728 }
   729 
   730 void ConCmdManager::AddToCmdList(ConCmdInfo *info)
   731 {
   732 	List<ConCmdInfo *>::iterator iter = m_CmdList.begin();
   733 	ConCmdInfo *pInfo;
   734 	bool inserted = false;
   735 	const char *orig = NULL;
   736 
   737 	orig = info->pCmd->GetName();
   738 
   739 	/* Insert this into the help list, SORTED alphabetically. */
   740 	while (iter != m_CmdList.end())
   741 	{
   742 		const char *cmd = NULL;
   743 		pInfo = (*iter);
   744 		cmd = pInfo->pCmd->GetName();
   745 		if (strcmp(orig, cmd) < 0)
   746 		{
   747 			m_CmdList.insert(iter, info);
   748 			inserted = true;
   749 			break;
   750 		}
   751 		iter++;
   752 	}
   753 
   754 	if (!inserted)
   755 	{
   756 		m_CmdList.push_back(info);
   757 	}
   758 }
   759 
   760 void ConCmdManager::UpdateAdminCmdFlags(const char *cmd, OverrideType type, FlagBits bits, bool remove)
   761 {
   762 	ConCmdInfo *pInfo;
   763 
   764 	if (type == Override_Command)
   765 	{
   766 		if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   767 		{
   768 			return;
   769 		}
   770 
   771 		List<CmdHook *>::iterator iter;
   772 		CmdHook *pHook;
   773 
   774 		for (iter=pInfo->conhooks.begin(); iter!=pInfo->conhooks.end(); iter++)
   775 		{
   776 			pHook = (*iter);
   777 			if (pHook->pAdmin)
   778 			{
   779 				if (!remove)
   780 				{
   781 					pHook->pAdmin->eflags = bits;
   782 				} else {
   783 					pHook->pAdmin->eflags = pHook->pAdmin->flags;
   784 				}
   785 				pInfo->admin = *(pHook->pAdmin);
   786 			}
   787 		}
   788 		pInfo->is_admin_set = true;
   789 	}
   790 	else if (type == Override_CommandGroup)
   791 	{
   792 		void *object;
   793 		if (!sm_trie_retrieve(m_pCmdGrps, cmd, &object))
   794 		{
   795 			return;
   796 		}
   797 		int grpid = (int)object;
   798 
   799 		/* This is bad :( loop through all commands */
   800 		List<ConCmdInfo *>::iterator iter;
   801 		CmdHook *pHook;
   802 		for (iter=m_CmdList.begin(); iter!=m_CmdList.end(); iter++)
   803 		{
   804 			pInfo = (*iter);
   805 			for (List<CmdHook *>::iterator citer=pInfo->conhooks.begin();
   806 				 citer!=pInfo->conhooks.end();
   807 				 citer++)
   808 			{
   809 				pHook = (*citer);
   810 				if (pHook->pAdmin && pHook->pAdmin->cmdGrpId == grpid)
   811 				{
   812 					if (remove)
   813 					{
   814 						pHook->pAdmin->eflags = bits;
   815 					} else {
   816 						pHook->pAdmin->eflags = pHook->pAdmin->flags;
   817 					}
   818 					pInfo->admin = *(pHook->pAdmin);
   819 				}
   820 			}
   821 		}
   822 		pInfo->is_admin_set = true;
   823 	}
   824 }
   825 
   826 void ConCmdManager::RemoveConCmd(ConCmdInfo *info, const char *name, bool is_read_safe, bool untrack)
   827 {
   828 	/* Remove from the trie */
   829 	sm_trie_delete(m_pCmds, name);
   830 
   831 	/* Remove console-specific information
   832 	 * This should always be true as of right now
   833 	 */
   834 	if (info->pCmd)
   835 	{
   836 		if (info->sourceMod)
   837 		{
   838 			/* Unlink from SourceMM */
   839 			g_SMAPI->UnregisterConCommandBase(g_PLAPI, info->pCmd);
   840 			/* Delete the command's memory */
   841 			char *new_help = const_cast<char *>(info->pCmd->GetHelpText());
   842 			char *new_name = const_cast<char *>(info->pCmd->GetName());
   843 			delete [] new_help;
   844 			delete [] new_name;
   845 			delete info->pCmd;
   846 		}
   847 		else
   848 		{
   849 			if (is_read_safe)
   850 			{
   851 				/* Remove the external hook */
   852 				SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, info->pCmd, CommandCallback, false);
   853 			}
   854 			if (untrack)
   855 			{
   856 				UntrackConCommandBase(info->pCmd, this);
   857 			}
   858 		}
   859 	}
   860 	
   861 	/* Remove from list */
   862 	m_CmdList.remove(info);
   863 
   864 	delete info;
   865 }
   866 
   867 bool ConCmdManager::LookForSourceModCommand(const char *cmd)
   868 {
   869 	ConCmdInfo *pInfo;
   870 
   871 	if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   872 	{
   873 		return false;
   874 	}
   875 
   876 	return pInfo->sourceMod && (pInfo->conhooks.size() > 0);
   877 }
   878 
   879 bool ConCmdManager::LookForCommandAdminFlags(const char *cmd, FlagBits *pFlags)
   880 {
   881 	ConCmdInfo *pInfo;
   882 
   883 	if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   884 	{
   885 		return false;
   886 	}
   887 
   888 	*pFlags = pInfo->admin.eflags;
   889 
   890 	return pInfo->is_admin_set;
   891 }
   892 
   893 ConCmdInfo *ConCmdManager::AddOrFindCommand(const char *name, const char *description, int flags)
   894 {
   895 	ConCmdInfo *pInfo;
   896 	if (!sm_trie_retrieve(m_pCmds, name, (void **)&pInfo))
   897 	{
   898 		pInfo = new ConCmdInfo();
   899 		/* Find the commandopan */
   900 		ConCommandBase *pBase = icvar->GetCommands();
   901 		ConCommand *pCmd = NULL;
   902 		while (pBase)
   903 		{
   904 			if (strcmp(pBase->GetName(), name) == 0)
   905 			{
   906 				/* Don't want to return convar with same name */
   907 				if (!pBase->IsCommand())
   908 				{
   909 					return NULL;
   910 				}
   911 
   912 				pCmd = (ConCommand *)pBase;
   913 				break;
   914 			}
   915 			pBase = const_cast<ConCommandBase *>(pBase->GetNext());
   916 		}
   917 
   918 		if (!pCmd)
   919 		{
   920 			/* Note that we have to duplicate because the source might not be 
   921 			 * a static string, and these expect static memory.
   922 			 */
   923 			if (!description)
   924 			{
   925 				description = "";
   926 			}
   927 			char *new_name = sm_strdup(name);
   928 			char *new_help = sm_strdup(description);
   929 			pCmd = new ConCommand(new_name, CommandCallback, new_help, flags);
   930 			pInfo->sourceMod = true;
   931 		}
   932 		else
   933 		{
   934 			TrackConCommandBase(pCmd, this);
   935 			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, pCmd, CommandCallback, false);
   936 		}
   937 
   938 		pInfo->pCmd = pCmd;
   939 		pInfo->is_admin_set = false;
   940 
   941 		sm_trie_insert(m_pCmds, name, pInfo);
   942 		AddToCmdList(pInfo);
   943 	}
   944 
   945 	return pInfo;
   946 }
   947 
   948 void ConCmdManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
   949 {
   950 	if (command.ArgC() >= 3)
   951 	{
   952 		const char *text = command.Arg(2);
   953 
   954 		CPlugin *pPlugin = g_PluginSys.FindPluginByConsoleArg(text);
   955 
   956 		if (!pPlugin)
   957 		{
   958 			g_RootMenu.ConsolePrint("[SM] Plugin \"%s\" was not found.", text);
   959 			return;
   960 		}
   961 
   962 		const sm_plugininfo_t *plinfo = pPlugin->GetPublicInfo();
   963 		const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : pPlugin->GetFilename();
   964 
   965 		CmdList *pList;
   966 		if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   967 		{
   968 			g_RootMenu.ConsolePrint("[SM] No commands found for: %s", plname);
   969 			return;
   970 		}
   971 		if (!pList->size())
   972 		{
   973 			g_RootMenu.ConsolePrint("[SM] No commands found for: %s", plname);
   974 			return;
   975 		}
   976 
   977 		CmdList::iterator iter;
   978 		const char *type = NULL;
   979 		const char *name;
   980 		const char *help;
   981 		g_RootMenu.ConsolePrint("[SM] Listing %d commands for: %s", pList->size(), plname);
   982 		g_RootMenu.ConsolePrint("  %-17.16s %-8.7s %s", "[Name]", "[Type]", "[Help]");
   983 		for (iter=pList->begin();
   984 			 iter!=pList->end();
   985 			 iter++)
   986 		{
   987 			PlCmdInfo &cmd = (*iter);
   988 			if (cmd.type == Cmd_Server)
   989 			{
   990 				type = "server";
   991 			}
   992 			else if (cmd.type == Cmd_Console)
   993 			{
   994 				type = "console";
   995 			}
   996 			else if (cmd.type == Cmd_Admin)
   997 			{
   998 				type = "admin";
   999 			}
  1000 			name = cmd.pInfo->pCmd->GetName();
  1001 			if (cmd.pHook->helptext.size())
  1002 			{
  1003 				help = cmd.pHook->helptext.c_str();
  1004 			}
  1005 			else
  1006 			{
  1007 				help = cmd.pInfo->pCmd->GetHelpText();
  1008 			}
  1009 			g_RootMenu.ConsolePrint("  %-17.16s %-12.11s %s", name, type, help);		
  1010 		}
  1011 
  1012 		return;
  1013 	}
  1014 
  1015 	g_RootMenu.ConsolePrint("[SM] Usage: sm cmds <plugin #>");
  1016 }