core/ConCmdManager.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Mon Feb 11 15:11:04 2013 -0500 (2013-02-11)
changeset 3786 cbbc2354ef1b
parent 3687 d0b1262dbc03
child 3819 d17aed3d46bf
permissions -rw-r--r--
More, hopefully final CS:GO/protobuf usermessage fixups (bug 5588).
     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_ORANGEBOX
    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 SOURCE_ENGINE >= SE_ORANGEBOX
   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 ConCmdInfo *ConCmdManager::FindInTrie(const char *name)
   230 {
   231 	ConCmdInfo *pInfo;
   232 	if (!sm_trie_retrieve(m_pCmds, name, (void **)&pInfo))
   233 		return NULL;
   234 	return pInfo;
   235 }
   236 
   237 ConCmdList::iterator ConCmdManager::FindInList(const char *cmd)
   238 {
   239 	List<ConCmdInfo *>::iterator iter = m_CmdList.begin();
   240 
   241 	while (iter != m_CmdList.end())
   242 	{
   243 		if (strcasecmp((*iter)->pCmd->GetName(), cmd) == 0)
   244 			break;
   245 		iter++;
   246 	}
   247 
   248 	return iter;
   249 }
   250 
   251 ResultType ConCmdManager::DispatchClientCommand(int client, const char *cmd, int args, ResultType type)
   252 {
   253 	ConCmdInfo *pInfo;
   254 
   255 	if ((pInfo = FindInTrie(cmd)) == NULL)
   256 	{
   257 		ConCmdList::iterator item = FindInList(cmd);
   258 		if (item == m_CmdList.end())
   259 			return type;
   260 		pInfo = *item;
   261 	}
   262 
   263 	cell_t result = type;
   264 	cell_t tempres = result;
   265 	List<CmdHook *>::iterator iter;
   266 	CmdHook *pHook;
   267 	for (iter=pInfo->conhooks.begin();
   268 		 iter!=pInfo->conhooks.end();
   269 		 iter++)
   270 	{
   271 		pHook = (*iter);
   272 		if (!pHook->pf->IsRunnable())
   273 		{
   274 			continue;
   275 		}
   276 		if (pHook->pAdmin && !CheckAccess(client, cmd, pHook->pAdmin))
   277 		{
   278 			if (result < Pl_Handled)
   279 			{
   280 				result = Pl_Handled;
   281 			}
   282 			continue;
   283 		}
   284 		pHook->pf->PushCell(client);
   285 		pHook->pf->PushCell(args);
   286 		if (pHook->pf->Execute(&tempres) == SP_ERROR_NONE)
   287 		{
   288 			if (tempres > result)
   289 			{
   290 				result = tempres;
   291 			}
   292 			if (result == Pl_Stop)
   293 			{
   294 				break;
   295 			}
   296 		}
   297 	}
   298 
   299 	type = (ResultType)result;
   300 
   301 	return type;
   302 }
   303 
   304 void ConCmdManager::InternalDispatch(const CCommand &command)
   305 {
   306 	int client = m_CmdClient;
   307 
   308 	if (client)
   309 	{
   310 		CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   311 		if (!pPlayer || !pPlayer->IsConnected())
   312 		{
   313 			return;
   314 		}
   315 	}
   316 
   317 	/**
   318 	 * Note: Console commands will EITHER go through IServerGameDLL::ClientCommand,
   319 	 * OR this dispatch.  They will NEVER go through both.
   320 	 * -- 
   321 	 * Whether or not it goes through the callback is determined by FCVAR_GAMEDLL
   322 	 */
   323 	const char *cmd = g_HL2.CurrentCommandName();
   324 
   325 	ConCmdInfo *pInfo = FindInTrie(cmd);
   326 	if (pInfo == NULL)
   327 	{
   328         /* Unfortunately, we now have to do a slow lookup because Valve made client commands 
   329          * case-insensitive.  We can't even use our sortedness.
   330          */
   331         if (client == 0 && !engine->IsDedicatedServer())
   332             return;
   333 
   334 		ConCmdList::iterator item = FindInList(cmd);
   335 		if (item == m_CmdList.end())
   336 			return;
   337 
   338 		pInfo = *item;
   339 	}
   340 
   341 	/* This is a hack to prevent say triggers from firing on messages that were 
   342 	 * blocked because of flooding.  We won't remove this, but the hack will get 
   343 	 * "nicer" when we expose explicit say hooks.
   344 	 */
   345 	if (g_ChatTriggers.WasFloodedMessage())
   346 	{
   347 		return;
   348 	}
   349 
   350 	cell_t result = Pl_Continue;
   351 	int args = command.ArgC() - 1;
   352 
   353 	List<CmdHook *>::iterator iter;
   354 	CmdHook *pHook;
   355 
   356 	/* Execute server-only commands if viable */
   357 	if (client == 0 && pInfo->srvhooks.size())
   358 	{
   359 		cell_t tempres = result;
   360 		for (iter=pInfo->srvhooks.begin();
   361 			 iter!=pInfo->srvhooks.end();
   362 			 iter++)
   363 		{
   364 			pHook = (*iter);
   365 			if (!pHook->pf->IsRunnable())
   366 			{
   367 				continue;
   368 			}
   369 			pHook->pf->PushCell(args);
   370 			if (pHook->pf->Execute(&tempres) == SP_ERROR_NONE)
   371 			{
   372 				if (tempres > result)
   373 				{
   374 					result = tempres;
   375 				}
   376 				if (result == Pl_Stop)
   377 				{
   378 					break;
   379 				}
   380 			}
   381 		}
   382 	
   383 		/* Check if there's an early stop */
   384 		if (result >= Pl_Stop)
   385 		{
   386 			if (!pInfo->sourceMod)
   387 			{
   388 				RETURN_META(MRES_SUPERCEDE);
   389 			}
   390 			return;
   391 		}
   392 	}
   393 
   394 	/* Execute console commands */
   395 	if (pInfo->conhooks.size())
   396 	{
   397 		cell_t tempres = result;
   398 		for (iter=pInfo->conhooks.begin();
   399 			iter!=pInfo->conhooks.end();
   400 			iter++)
   401 		{
   402 			pHook = (*iter);
   403 			if (!pHook->pf->IsRunnable())
   404 			{
   405 				continue;
   406 			}
   407 			if (client 
   408 				&& pHook->pAdmin
   409 				&& !CheckAccess(client, cmd, pHook->pAdmin))
   410 			{
   411 				if (result < Pl_Handled)
   412 				{
   413 					result = Pl_Handled;
   414 				}
   415 				continue;
   416 			}
   417 
   418 			/* On a listen server, sometimes the server host's client index can be set as 0.
   419 			 * So index 1 is passed to the command callback to correct this potential problem.
   420 			 */
   421 			if (!engine->IsDedicatedServer())
   422 			{
   423 				client = g_Players.ListenClient();
   424 			}
   425 
   426 			pHook->pf->PushCell(client);
   427 			pHook->pf->PushCell(args);
   428 
   429 			if (pHook->pf->Execute(&tempres) == SP_ERROR_NONE)
   430 			{
   431 				if (tempres > result)
   432 				{
   433 					result = tempres;
   434 				}
   435 				if (result == Pl_Stop)
   436 				{
   437 					break;
   438 				}
   439 			}
   440 		}
   441 	}
   442 
   443 	if (result >= Pl_Handled)
   444 	{
   445 		if (!pInfo->sourceMod)
   446 		{
   447 			RETURN_META(MRES_SUPERCEDE);
   448 		}
   449 		return;
   450 	}
   451 }
   452 
   453 bool ConCmdManager::CheckClientCommandAccess(int client, const char *cmd, FlagBits cmdflags)
   454 {
   455 	if (cmdflags == 0 || client == 0)
   456 	{
   457 		return true;
   458 	}
   459 
   460 	/* If running listen server, then client 1 is the server host and should have 'root' access */
   461 	if (client == 1 && !engine->IsDedicatedServer())
   462 	{
   463 		return true;
   464 	}
   465 
   466 	CPlayer *player = g_Players.GetPlayerByIndex(client);
   467 	if (!player 
   468 		|| player->GetEdict() == NULL
   469 		|| player->IsFakeClient())
   470 	{
   471 		return false;
   472 	}
   473 
   474 	return CheckAdminCommandAccess(player->GetAdminId(), cmd, cmdflags);
   475 }
   476 
   477 bool ConCmdManager::CheckAdminCommandAccess(AdminId adm, const char *cmd, FlagBits cmdflags)
   478 {
   479 	if (adm != INVALID_ADMIN_ID)
   480 	{
   481 		FlagBits bits = g_Admins.GetAdminFlags(adm, Access_Effective);
   482 
   483 		/* root knows all, WHOA */
   484 		if ((bits & ADMFLAG_ROOT) == ADMFLAG_ROOT)
   485 		{
   486 			return true;
   487 		}
   488 
   489 		/* Check for overrides
   490 		 * :TODO: is it worth optimizing this?
   491 		 */
   492 		unsigned int groups = g_Admins.GetAdminGroupCount(adm);
   493 		GroupId gid;
   494 		OverrideRule rule;
   495 		bool override = false;
   496 		for (unsigned int i=0; i<groups; i++)
   497 		{
   498 			gid = g_Admins.GetAdminGroup(adm, i, NULL);
   499 			/* First get group-level override */
   500 			override = g_Admins.GetGroupCommandOverride(gid, cmd, Override_CommandGroup, &rule);
   501 			/* Now get the specific command override */
   502 			if (g_Admins.GetGroupCommandOverride(gid, cmd, Override_Command, &rule))
   503 			{
   504 				override = true;
   505 			}
   506 			if (override)
   507 			{
   508 				if (rule == Command_Allow)
   509 				{
   510 					return true;
   511 				}
   512 				else if (rule == Command_Deny)
   513 				{
   514 					return false;
   515 				}
   516 			}
   517 		}
   518 
   519 		/* See if our other flags match */
   520 		if ((bits & cmdflags) == cmdflags)
   521 		{
   522 			return true;
   523 		}
   524 	}
   525 
   526 	return false;
   527 }
   528 
   529 bool ConCmdManager::CheckAccess(int client, const char *cmd, AdminCmdInfo *pAdmin)
   530 {
   531 	if (CheckClientCommandAccess(client, cmd, pAdmin->eflags))
   532 	{
   533 		return true;
   534 	}
   535 
   536 	edict_t *pEdict = PEntityOfEntIndex(client);
   537 	
   538 	/* If we got here, the command failed... */
   539 	char buffer[128];
   540 	if (!logicore.CoreTranslate(buffer, sizeof(buffer), "%T", 2, NULL, "No Access", &client))
   541 	{
   542 		UTIL_Format(buffer, sizeof(buffer), "You do not have access to this command");
   543 	}
   544 
   545 	unsigned int replyto = g_ChatTriggers.GetReplyTo();
   546 	if (replyto == SM_REPLY_CONSOLE)
   547 	{
   548 		char fullbuffer[192];
   549 		UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s.\n", buffer);
   550 		engine->ClientPrintf(pEdict, fullbuffer);
   551 	}
   552 	else if (replyto == SM_REPLY_CHAT)
   553 	{
   554 		char fullbuffer[192];
   555 		UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s.", buffer);
   556 		g_HL2.TextMsg(client, HUD_PRINTTALK, fullbuffer);
   557 	}
   558 	
   559 	return false;
   560 }
   561 
   562 bool ConCmdManager::AddAdminCommand(IPluginFunction *pFunction, 
   563 									 const char *name, 
   564 									 const char *group,
   565 									 int adminflags,
   566 									 const char *description, 
   567 									 int flags)
   568 {
   569 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   570 
   571 	if (!pInfo)
   572 	{
   573 		return false;
   574 	}
   575 
   576 	CmdHook *pHook = new CmdHook();
   577 	AdminCmdInfo *pAdmin = new AdminCmdInfo();
   578 
   579 	pHook->pf = pFunction;
   580 	if (description && description[0])
   581 	{
   582 		pHook->helptext.assign(description);
   583 	}
   584 	pHook->pAdmin = pAdmin;
   585 
   586 	void *object;
   587 	int grpid;
   588 	if (!sm_trie_retrieve(m_pCmdGrps, group, (void **)&object))
   589 	{
   590 		grpid = m_Strings.AddString(group);
   591 		sm_trie_insert(m_pCmdGrps, group, (void *)grpid);
   592 	}
   593 	else
   594 	{
   595 		grpid = (int)object;
   596 	}
   597 
   598 	pAdmin->cmdGrpId = grpid;
   599 	pAdmin->flags = adminflags;
   600 
   601 	/* First get the command group override, if any */
   602 	bool override = g_Admins.GetCommandOverride(group, 
   603 		Override_CommandGroup,
   604 		&(pAdmin->eflags));
   605 
   606 	/* Next get the command override, if any */
   607 	if (g_Admins.GetCommandOverride(name,
   608 		Override_Command,
   609 		&(pAdmin->eflags)))
   610 	{
   611 		override = true;
   612 	}
   613 
   614 	/* Assign normal flags if there were no overrides */
   615 	if (!override)
   616 	{
   617 		pAdmin->eflags = pAdmin->flags;
   618 	}
   619 
   620 	/* Finally, add the hook */
   621 	pInfo->conhooks.push_back(pHook);
   622 	pInfo->admin = *(pHook->pAdmin);
   623 
   624 	/* Now add to the plugin */
   625 	CmdList *pList;
   626 	IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
   627 	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   628 	{
   629 		pList = new CmdList();
   630 		pPlugin->SetProperty("CommandList", pList);
   631 	}
   632 	PlCmdInfo info;
   633 	info.pInfo = pInfo;
   634 	info.type = Cmd_Admin;
   635 	info.pHook = pHook;
   636 	AddToPlCmdList(pList, info);
   637 
   638 	return true;
   639 }
   640 
   641 bool ConCmdManager::AddServerCommand(IPluginFunction *pFunction, 
   642 									  const char *name, 
   643 									  const char *description, 
   644 									  int flags)
   645 
   646 {
   647 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   648 
   649 	if (!pInfo)
   650 	{
   651 		return false;
   652 	}
   653 
   654 	CmdHook *pHook = new CmdHook();
   655 
   656 	pHook->pf = pFunction;
   657 	if (description && description[0])
   658 	{
   659 		pHook->helptext.assign(description);
   660 	}
   661 
   662 	pInfo->srvhooks.push_back(pHook);
   663 
   664 	/* Add to the plugin */
   665 	CmdList *pList;
   666 	IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
   667 	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   668 	{
   669 		pList = new CmdList();
   670 		pPlugin->SetProperty("CommandList", pList);
   671 	}
   672 	PlCmdInfo info;
   673 	info.pInfo = pInfo;
   674 	info.type = Cmd_Server;
   675 	info.pHook = pHook;
   676 	AddToPlCmdList(pList, info);
   677 
   678 	return true;
   679 }
   680 
   681 void AddToPlCmdList(CmdList *pList, const PlCmdInfo &info)
   682 {
   683 	CmdList::iterator iter = pList->begin();
   684 	bool inserted = false;
   685 	const char *orig = NULL;
   686 
   687 	orig = info.pInfo->pCmd->GetName();
   688 
   689 	/* Insert this into the help list, SORTED alphabetically. */
   690 	while (iter != pList->end())
   691 	{
   692 		PlCmdInfo &obj = (*iter);
   693 		const char *cmd = obj.pInfo->pCmd->GetName();
   694 		if (strcmp(orig, cmd) < 0)
   695 		{
   696 			pList->insert(iter, info);
   697 			inserted = true;
   698 			break;
   699 		}
   700 		iter++;
   701 	}
   702 
   703 	if (!inserted)
   704 	{
   705 		pList->push_back(info);
   706 	}
   707 }
   708 
   709 void ConCmdManager::AddToCmdList(ConCmdInfo *info)
   710 {
   711 	List<ConCmdInfo *>::iterator iter = m_CmdList.begin();
   712 	ConCmdInfo *pInfo;
   713 	bool inserted = false;
   714 	const char *orig = NULL;
   715 
   716 	orig = info->pCmd->GetName();
   717 
   718 	/* Insert this into the help list, SORTED alphabetically. */
   719 	while (iter != m_CmdList.end())
   720 	{
   721 		const char *cmd = NULL;
   722 		pInfo = (*iter);
   723 		cmd = pInfo->pCmd->GetName();
   724 		if (strcmp(orig, cmd) < 0)
   725 		{
   726 			m_CmdList.insert(iter, info);
   727 			inserted = true;
   728 			break;
   729 		}
   730 		iter++;
   731 	}
   732 
   733 	if (!inserted)
   734 	{
   735 		m_CmdList.push_back(info);
   736 	}
   737 }
   738 
   739 void ConCmdManager::UpdateAdminCmdFlags(const char *cmd, OverrideType type, FlagBits bits, bool remove)
   740 {
   741 	ConCmdInfo *pInfo;
   742 
   743 	if (type == Override_Command)
   744 	{
   745 		if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   746 		{
   747 			return;
   748 		}
   749 
   750 		List<CmdHook *>::iterator iter;
   751 		CmdHook *pHook;
   752 
   753 		for (iter=pInfo->conhooks.begin(); iter!=pInfo->conhooks.end(); iter++)
   754 		{
   755 			pHook = (*iter);
   756 			if (pHook->pAdmin)
   757 			{
   758 				if (!remove)
   759 				{
   760 					pHook->pAdmin->eflags = bits;
   761 				} else {
   762 					pHook->pAdmin->eflags = pHook->pAdmin->flags;
   763 				}
   764 				pInfo->admin = *(pHook->pAdmin);
   765 			}
   766 		}
   767 	}
   768 	else if (type == Override_CommandGroup)
   769 	{
   770 		void *object;
   771 		if (!sm_trie_retrieve(m_pCmdGrps, cmd, &object))
   772 		{
   773 			return;
   774 		}
   775 		int grpid = (int)object;
   776 
   777 		/* This is bad :( loop through all commands */
   778 		List<ConCmdInfo *>::iterator iter;
   779 		CmdHook *pHook;
   780 		for (iter=m_CmdList.begin(); iter!=m_CmdList.end(); iter++)
   781 		{
   782 			pInfo = (*iter);
   783 			for (List<CmdHook *>::iterator citer=pInfo->conhooks.begin();
   784 				 citer!=pInfo->conhooks.end();
   785 				 citer++)
   786 			{
   787 				pHook = (*citer);
   788 				if (pHook->pAdmin && pHook->pAdmin->cmdGrpId == grpid)
   789 				{
   790 					if (remove)
   791 					{
   792 						pHook->pAdmin->eflags = bits;
   793 					} else {
   794 						pHook->pAdmin->eflags = pHook->pAdmin->flags;
   795 					}
   796 					pInfo->admin = *(pHook->pAdmin);
   797 				}
   798 			}
   799 		}
   800 	}
   801 }
   802 
   803 void ConCmdManager::RemoveConCmd(ConCmdInfo *info, const char *name, bool is_read_safe, bool untrack)
   804 {
   805 	/* Remove from the trie */
   806 	sm_trie_delete(m_pCmds, name);
   807 
   808 	/* Remove console-specific information
   809 	 * This should always be true as of right now
   810 	 */
   811 	if (info->pCmd)
   812 	{
   813 		if (info->sourceMod)
   814 		{
   815 			/* Unlink from SourceMM */
   816 			g_SMAPI->UnregisterConCommandBase(g_PLAPI, info->pCmd);
   817 			/* Delete the command's memory */
   818 			char *new_help = const_cast<char *>(info->pCmd->GetHelpText());
   819 			char *new_name = const_cast<char *>(info->pCmd->GetName());
   820 			delete [] new_help;
   821 			delete [] new_name;
   822 			delete info->pCmd;
   823 		}
   824 		else
   825 		{
   826 			if (is_read_safe)
   827 			{
   828 				/* Remove the external hook */
   829 				SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, info->pCmd, CommandCallback, false);
   830 			}
   831 			if (untrack)
   832 			{
   833 				UntrackConCommandBase(info->pCmd, this);
   834 			}
   835 		}
   836 	}
   837 	
   838 	/* Remove from list */
   839 	m_CmdList.remove(info);
   840 
   841 	delete info;
   842 }
   843 
   844 bool ConCmdManager::LookForSourceModCommand(const char *cmd)
   845 {
   846 	ConCmdInfo *pInfo;
   847 
   848 	if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   849 	{
   850 		return false;
   851 	}
   852 
   853 	return pInfo->sourceMod && (pInfo->conhooks.size() > 0);
   854 }
   855 
   856 bool ConCmdManager::LookForCommandAdminFlags(const char *cmd, FlagBits *pFlags)
   857 {
   858 	ConCmdInfo *pInfo;
   859 
   860 	if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   861 	{
   862 		return false;
   863 	}
   864 
   865 	*pFlags = pInfo->admin.eflags;
   866 
   867 	return true;
   868 }
   869 
   870 ConCmdInfo *ConCmdManager::AddOrFindCommand(const char *name, const char *description, int flags)
   871 {
   872 	ConCmdInfo *pInfo;
   873 	if (!sm_trie_retrieve(m_pCmds, name, (void **)&pInfo))
   874 	{
   875 		ConCmdList::iterator item = FindInList(name);
   876 		if (item != m_CmdList.end())
   877 			return *item;
   878 
   879 		pInfo = new ConCmdInfo();
   880 		/* Find the commandopan */
   881 		ConCommand *pCmd = FindCommand(name);
   882 
   883 		if (!pCmd)
   884 		{
   885 			/* Note that we have to duplicate because the source might not be 
   886 			 * a static string, and these expect static memory.
   887 			 */
   888 			if (!description)
   889 			{
   890 				description = "";
   891 			}
   892 			char *new_name = sm_strdup(name);
   893 			char *new_help = sm_strdup(description);
   894 			pCmd = new ConCommand(new_name, CommandCallback, new_help, flags);
   895 			pInfo->sourceMod = true;
   896 		}
   897 		else
   898 		{
   899 			TrackConCommandBase(pCmd, this);
   900 			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, pCmd, CommandCallback, false);
   901 		}
   902 
   903 		pInfo->pCmd = pCmd;
   904 
   905 		sm_trie_insert(m_pCmds, name, pInfo);
   906 		AddToCmdList(pInfo);
   907 	}
   908 
   909 	return pInfo;
   910 }
   911 
   912 void ConCmdManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
   913 {
   914 	if (command.ArgC() >= 3)
   915 	{
   916 		const char *text = command.Arg(2);
   917 
   918 		CPlugin *pPlugin = g_PluginSys.FindPluginByConsoleArg(text);
   919 
   920 		if (!pPlugin)
   921 		{
   922 			g_RootMenu.ConsolePrint("[SM] Plugin \"%s\" was not found.", text);
   923 			return;
   924 		}
   925 
   926 		const sm_plugininfo_t *plinfo = pPlugin->GetPublicInfo();
   927 		const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : pPlugin->GetFilename();
   928 
   929 		CmdList *pList;
   930 		if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   931 		{
   932 			g_RootMenu.ConsolePrint("[SM] No commands found for: %s", plname);
   933 			return;
   934 		}
   935 		if (!pList->size())
   936 		{
   937 			g_RootMenu.ConsolePrint("[SM] No commands found for: %s", plname);
   938 			return;
   939 		}
   940 
   941 		CmdList::iterator iter;
   942 		const char *type = NULL;
   943 		const char *name;
   944 		const char *help;
   945 		g_RootMenu.ConsolePrint("[SM] Listing %d commands for: %s", pList->size(), plname);
   946 		g_RootMenu.ConsolePrint("  %-17.16s %-8.7s %s", "[Name]", "[Type]", "[Help]");
   947 		for (iter=pList->begin();
   948 			 iter!=pList->end();
   949 			 iter++)
   950 		{
   951 			PlCmdInfo &cmd = (*iter);
   952 			if (cmd.type == Cmd_Server)
   953 			{
   954 				type = "server";
   955 			}
   956 			else if (cmd.type == Cmd_Admin)
   957 			{
   958 				type = (cmd.pInfo->admin.eflags == 0)?"console":"admin";
   959 			}
   960 			name = cmd.pInfo->pCmd->GetName();
   961 			if (cmd.pHook->helptext.size())
   962 			{
   963 				help = cmd.pHook->helptext.c_str();
   964 			}
   965 			else
   966 			{
   967 				help = cmd.pInfo->pCmd->GetHelpText();
   968 			}
   969 			g_RootMenu.ConsolePrint("  %-17.16s %-12.11s %s", name, type, help);		
   970 		}
   971 
   972 		return;
   973 	}
   974 
   975 	g_RootMenu.ConsolePrint("[SM] Usage: sm cmds <plugin #>");
   976 }