core/ConCmdManager.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Fri Jul 12 02:31:41 2013 -0400 (2013-07-12)
changeset 3942 7aaa00382627
parent 3913 a73cf1f26984
child 4076 a1aeeac165e7
permissions -rw-r--r--
Updated for latest hl2sdk-dota changes.
     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, const CCommandContext &, 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(const CCommandContext &context, 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 #if SOURCE_ENGINE != SE_DOTA
   543 	edict_t *pEdict = PEntityOfEntIndex(client);
   544 #endif
   545 	
   546 	/* If we got here, the command failed... */
   547 	char buffer[128];
   548 	if (!logicore.CoreTranslate(buffer, sizeof(buffer), "%T", 2, NULL, "No Access", &client))
   549 	{
   550 		UTIL_Format(buffer, sizeof(buffer), "You do not have access to this command");
   551 	}
   552 
   553 	unsigned int replyto = g_ChatTriggers.GetReplyTo();
   554 	if (replyto == SM_REPLY_CONSOLE)
   555 	{
   556 		char fullbuffer[192];
   557 		UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s.\n", buffer);
   558 #if SOURCE_ENGINE == SE_DOTA
   559 		engine->ClientPrintf(client, fullbuffer);
   560 #else
   561 		engine->ClientPrintf(pEdict, fullbuffer);
   562 #endif
   563 	}
   564 	else if (replyto == SM_REPLY_CHAT)
   565 	{
   566 		char fullbuffer[192];
   567 		UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s.", buffer);
   568 		g_HL2.TextMsg(client, HUD_PRINTTALK, fullbuffer);
   569 	}
   570 	
   571 	return false;
   572 }
   573 
   574 bool ConCmdManager::AddAdminCommand(IPluginFunction *pFunction, 
   575 									 const char *name, 
   576 									 const char *group,
   577 									 int adminflags,
   578 									 const char *description, 
   579 									 int flags)
   580 {
   581 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   582 
   583 	if (!pInfo)
   584 	{
   585 		return false;
   586 	}
   587 
   588 	CmdHook *pHook = new CmdHook();
   589 	AdminCmdInfo *pAdmin = new AdminCmdInfo();
   590 
   591 	pHook->pf = pFunction;
   592 	if (description && description[0])
   593 	{
   594 		pHook->helptext.assign(description);
   595 	}
   596 	pHook->pAdmin = pAdmin;
   597 
   598 	void *object;
   599 	int grpid;
   600 	if (!sm_trie_retrieve(m_pCmdGrps, group, (void **)&object))
   601 	{
   602 		grpid = m_Strings.AddString(group);
   603 		sm_trie_insert(m_pCmdGrps, group, (void *)grpid);
   604 	}
   605 	else
   606 	{
   607 		grpid = (int)object;
   608 	}
   609 
   610 	pAdmin->cmdGrpId = grpid;
   611 	pAdmin->flags = adminflags;
   612 
   613 	/* First get the command group override, if any */
   614 	bool override = g_Admins.GetCommandOverride(group, 
   615 		Override_CommandGroup,
   616 		&(pAdmin->eflags));
   617 
   618 	/* Next get the command override, if any */
   619 	if (g_Admins.GetCommandOverride(name,
   620 		Override_Command,
   621 		&(pAdmin->eflags)))
   622 	{
   623 		override = true;
   624 	}
   625 
   626 	/* Assign normal flags if there were no overrides */
   627 	if (!override)
   628 	{
   629 		pAdmin->eflags = pAdmin->flags;
   630 	}
   631 
   632 	/* Finally, add the hook */
   633 	pInfo->conhooks.push_back(pHook);
   634 	pInfo->admin = *(pHook->pAdmin);
   635 
   636 	/* Now add to the plugin */
   637 	CmdList *pList;
   638 	IPlugin *pPlugin = scripts->FindPluginByContext(pFunction->GetParentContext()->GetContext());
   639 	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   640 	{
   641 		pList = new CmdList();
   642 		pPlugin->SetProperty("CommandList", pList);
   643 	}
   644 	PlCmdInfo info;
   645 	info.pInfo = pInfo;
   646 	info.type = Cmd_Admin;
   647 	info.pHook = pHook;
   648 	AddToPlCmdList(pList, info);
   649 
   650 	return true;
   651 }
   652 
   653 bool ConCmdManager::AddServerCommand(IPluginFunction *pFunction, 
   654 									  const char *name, 
   655 									  const char *description, 
   656 									  int flags)
   657 
   658 {
   659 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   660 
   661 	if (!pInfo)
   662 	{
   663 		return false;
   664 	}
   665 
   666 	CmdHook *pHook = new CmdHook();
   667 
   668 	pHook->pf = pFunction;
   669 	if (description && description[0])
   670 	{
   671 		pHook->helptext.assign(description);
   672 	}
   673 
   674 	pInfo->srvhooks.push_back(pHook);
   675 
   676 	/* Add to the plugin */
   677 	CmdList *pList;
   678 	IPlugin *pPlugin = scripts->FindPluginByContext(pFunction->GetParentContext()->GetContext());
   679 	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   680 	{
   681 		pList = new CmdList();
   682 		pPlugin->SetProperty("CommandList", pList);
   683 	}
   684 	PlCmdInfo info;
   685 	info.pInfo = pInfo;
   686 	info.type = Cmd_Server;
   687 	info.pHook = pHook;
   688 	AddToPlCmdList(pList, info);
   689 
   690 	return true;
   691 }
   692 
   693 void AddToPlCmdList(CmdList *pList, const PlCmdInfo &info)
   694 {
   695 	CmdList::iterator iter = pList->begin();
   696 	bool inserted = false;
   697 	const char *orig = NULL;
   698 
   699 	orig = info.pInfo->pCmd->GetName();
   700 
   701 	/* Insert this into the help list, SORTED alphabetically. */
   702 	while (iter != pList->end())
   703 	{
   704 		PlCmdInfo &obj = (*iter);
   705 		const char *cmd = obj.pInfo->pCmd->GetName();
   706 		if (strcmp(orig, cmd) < 0)
   707 		{
   708 			pList->insert(iter, info);
   709 			inserted = true;
   710 			break;
   711 		}
   712 		iter++;
   713 	}
   714 
   715 	if (!inserted)
   716 	{
   717 		pList->push_back(info);
   718 	}
   719 }
   720 
   721 void ConCmdManager::AddToCmdList(ConCmdInfo *info)
   722 {
   723 	List<ConCmdInfo *>::iterator iter = m_CmdList.begin();
   724 	ConCmdInfo *pInfo;
   725 	bool inserted = false;
   726 	const char *orig = NULL;
   727 
   728 	orig = info->pCmd->GetName();
   729 
   730 	/* Insert this into the help list, SORTED alphabetically. */
   731 	while (iter != m_CmdList.end())
   732 	{
   733 		const char *cmd = NULL;
   734 		pInfo = (*iter);
   735 		cmd = pInfo->pCmd->GetName();
   736 		if (strcmp(orig, cmd) < 0)
   737 		{
   738 			m_CmdList.insert(iter, info);
   739 			inserted = true;
   740 			break;
   741 		}
   742 		iter++;
   743 	}
   744 
   745 	if (!inserted)
   746 	{
   747 		m_CmdList.push_back(info);
   748 	}
   749 }
   750 
   751 void ConCmdManager::UpdateAdminCmdFlags(const char *cmd, OverrideType type, FlagBits bits, bool remove)
   752 {
   753 	ConCmdInfo *pInfo;
   754 
   755 	if (type == Override_Command)
   756 	{
   757 		if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   758 		{
   759 			return;
   760 		}
   761 
   762 		List<CmdHook *>::iterator iter;
   763 		CmdHook *pHook;
   764 
   765 		for (iter=pInfo->conhooks.begin(); iter!=pInfo->conhooks.end(); iter++)
   766 		{
   767 			pHook = (*iter);
   768 			if (pHook->pAdmin)
   769 			{
   770 				if (!remove)
   771 				{
   772 					pHook->pAdmin->eflags = bits;
   773 				} else {
   774 					pHook->pAdmin->eflags = pHook->pAdmin->flags;
   775 				}
   776 				pInfo->admin = *(pHook->pAdmin);
   777 			}
   778 		}
   779 	}
   780 	else if (type == Override_CommandGroup)
   781 	{
   782 		void *object;
   783 		if (!sm_trie_retrieve(m_pCmdGrps, cmd, &object))
   784 		{
   785 			return;
   786 		}
   787 		int grpid = (int)object;
   788 
   789 		/* This is bad :( loop through all commands */
   790 		List<ConCmdInfo *>::iterator iter;
   791 		CmdHook *pHook;
   792 		for (iter=m_CmdList.begin(); iter!=m_CmdList.end(); iter++)
   793 		{
   794 			pInfo = (*iter);
   795 			for (List<CmdHook *>::iterator citer=pInfo->conhooks.begin();
   796 				 citer!=pInfo->conhooks.end();
   797 				 citer++)
   798 			{
   799 				pHook = (*citer);
   800 				if (pHook->pAdmin && pHook->pAdmin->cmdGrpId == grpid)
   801 				{
   802 					if (remove)
   803 					{
   804 						pHook->pAdmin->eflags = bits;
   805 					} else {
   806 						pHook->pAdmin->eflags = pHook->pAdmin->flags;
   807 					}
   808 					pInfo->admin = *(pHook->pAdmin);
   809 				}
   810 			}
   811 		}
   812 	}
   813 }
   814 
   815 void ConCmdManager::RemoveConCmd(ConCmdInfo *info, const char *name, bool is_read_safe, bool untrack)
   816 {
   817 	/* Remove from the trie */
   818 	sm_trie_delete(m_pCmds, name);
   819 
   820 	/* Remove console-specific information
   821 	 * This should always be true as of right now
   822 	 */
   823 	if (info->pCmd)
   824 	{
   825 		if (info->sourceMod)
   826 		{
   827 			/* Unlink from SourceMM */
   828 			g_SMAPI->UnregisterConCommandBase(g_PLAPI, info->pCmd);
   829 			/* Delete the command's memory */
   830 			char *new_help = const_cast<char *>(info->pCmd->GetHelpText());
   831 			char *new_name = const_cast<char *>(info->pCmd->GetName());
   832 			delete [] new_help;
   833 			delete [] new_name;
   834 			delete info->pCmd;
   835 		}
   836 		else
   837 		{
   838 			if (is_read_safe)
   839 			{
   840 				/* Remove the external hook */
   841 				SH_REMOVE_HOOK(ConCommand, Dispatch, info->pCmd, SH_STATIC(CommandCallback), false);
   842 			}
   843 			if (untrack)
   844 			{
   845 				UntrackConCommandBase(info->pCmd, this);
   846 			}
   847 		}
   848 	}
   849 	
   850 	/* Remove from list */
   851 	m_CmdList.remove(info);
   852 
   853 	delete info;
   854 }
   855 
   856 bool ConCmdManager::LookForSourceModCommand(const char *cmd)
   857 {
   858 	ConCmdInfo *pInfo;
   859 
   860 	if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   861 	{
   862 		return false;
   863 	}
   864 
   865 	return pInfo->sourceMod && (pInfo->conhooks.size() > 0);
   866 }
   867 
   868 bool ConCmdManager::LookForCommandAdminFlags(const char *cmd, FlagBits *pFlags)
   869 {
   870 	ConCmdInfo *pInfo;
   871 
   872 	if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   873 	{
   874 		return false;
   875 	}
   876 
   877 	*pFlags = pInfo->admin.eflags;
   878 
   879 	return true;
   880 }
   881 
   882 ConCmdInfo *ConCmdManager::AddOrFindCommand(const char *name, const char *description, int flags)
   883 {
   884 	ConCmdInfo *pInfo;
   885 	if (!sm_trie_retrieve(m_pCmds, name, (void **)&pInfo))
   886 	{
   887 		ConCmdList::iterator item = FindInList(name);
   888 		if (item != m_CmdList.end())
   889 			return *item;
   890 
   891 		pInfo = new ConCmdInfo();
   892 		/* Find the commandopan */
   893 		ConCommand *pCmd = FindCommand(name);
   894 
   895 		if (!pCmd)
   896 		{
   897 			/* Note that we have to duplicate because the source might not be 
   898 			 * a static string, and these expect static memory.
   899 			 */
   900 			if (!description)
   901 			{
   902 				description = "";
   903 			}
   904 			char *new_name = sm_strdup(name);
   905 			char *new_help = sm_strdup(description);
   906 			pCmd = new ConCommand(new_name, CommandCallback, new_help, flags);
   907 			pInfo->sourceMod = true;
   908 		}
   909 		else
   910 		{
   911 			TrackConCommandBase(pCmd, this);
   912 			SH_ADD_HOOK(ConCommand, Dispatch, pCmd, SH_STATIC(CommandCallback), false);
   913 		}
   914 
   915 		pInfo->pCmd = pCmd;
   916 
   917 		sm_trie_insert(m_pCmds, name, pInfo);
   918 		AddToCmdList(pInfo);
   919 	}
   920 
   921 	return pInfo;
   922 }
   923 
   924 void ConCmdManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
   925 {
   926 	if (command.ArgC() >= 3)
   927 	{
   928 		const char *text = command.Arg(2);
   929 
   930 		IPlugin *pPlugin = scripts->FindPluginByConsoleArg(text);
   931 
   932 		if (!pPlugin)
   933 		{
   934 			g_RootMenu.ConsolePrint("[SM] Plugin \"%s\" was not found.", text);
   935 			return;
   936 		}
   937 
   938 		const sm_plugininfo_t *plinfo = pPlugin->GetPublicInfo();
   939 		const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : pPlugin->GetFilename();
   940 
   941 		CmdList *pList;
   942 		if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   943 		{
   944 			g_RootMenu.ConsolePrint("[SM] No commands found for: %s", plname);
   945 			return;
   946 		}
   947 		if (!pList->size())
   948 		{
   949 			g_RootMenu.ConsolePrint("[SM] No commands found for: %s", plname);
   950 			return;
   951 		}
   952 
   953 		CmdList::iterator iter;
   954 		const char *type = NULL;
   955 		const char *name;
   956 		const char *help;
   957 		g_RootMenu.ConsolePrint("[SM] Listing %d commands for: %s", pList->size(), plname);
   958 		g_RootMenu.ConsolePrint("  %-17.16s %-8.7s %s", "[Name]", "[Type]", "[Help]");
   959 		for (iter=pList->begin();
   960 			 iter!=pList->end();
   961 			 iter++)
   962 		{
   963 			PlCmdInfo &cmd = (*iter);
   964 			if (cmd.type == Cmd_Server)
   965 			{
   966 				type = "server";
   967 			}
   968 			else if (cmd.type == Cmd_Admin)
   969 			{
   970 				type = (cmd.pInfo->admin.eflags == 0)?"console":"admin";
   971 			}
   972 			name = cmd.pInfo->pCmd->GetName();
   973 			if (cmd.pHook->helptext.size())
   974 			{
   975 				help = cmd.pHook->helptext.c_str();
   976 			}
   977 			else
   978 			{
   979 				help = cmd.pInfo->pCmd->GetHelpText();
   980 			}
   981 			g_RootMenu.ConsolePrint("  %-17.16s %-12.11s %s", name, type, help);		
   982 		}
   983 
   984 		return;
   985 	}
   986 
   987 	g_RootMenu.ConsolePrint("[SM] Usage: sm cmds <plugin #>");
   988 }