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