core/ConCmdManager.cpp
author Asher Baker <asherkin@gmail.com>
Sun Aug 14 21:42:32 2011 +0100 (2011-08-14)
changeset 3367 18eb63085ca4
parent 3302 767c92704500
child 3389 68d3e56f4bb7
permissions -rw-r--r--
Added a reset argument to 'sm cvars' (bug 5043, r=psychonic).
     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::CheckCommandAccess(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 	AdminId adm = player->GetAdminId();
   475 	if (adm != INVALID_ADMIN_ID)
   476 	{
   477 		FlagBits bits = g_Admins.GetAdminFlags(adm, Access_Effective);
   478 
   479 		/* root knows all, WHOA */
   480 		if ((bits & ADMFLAG_ROOT) == ADMFLAG_ROOT)
   481 		{
   482 			return true;
   483 		}
   484 
   485 		/* Check for overrides
   486 		 * :TODO: is it worth optimizing this?
   487 		 */
   488 		unsigned int groups = g_Admins.GetAdminGroupCount(adm);
   489 		GroupId gid;
   490 		OverrideRule rule;
   491 		bool override = false;
   492 		for (unsigned int i=0; i<groups; i++)
   493 		{
   494 			gid = g_Admins.GetAdminGroup(adm, i, NULL);
   495 			/* First get group-level override */
   496 			override = g_Admins.GetGroupCommandOverride(gid, cmd, Override_CommandGroup, &rule);
   497 			/* Now get the specific command override */
   498 			if (g_Admins.GetGroupCommandOverride(gid, cmd, Override_Command, &rule))
   499 			{
   500 				override = true;
   501 			}
   502 			if (override)
   503 			{
   504 				if (rule == Command_Allow)
   505 				{
   506 					return true;
   507 				}
   508 				else if (rule == Command_Deny)
   509 				{
   510 					return false;
   511 				}
   512 			}
   513 		}
   514 
   515 		/* See if our other flags match */
   516 		if ((bits & cmdflags) == cmdflags)
   517 		{
   518 			return true;
   519 		}
   520 	}
   521 
   522 	return false;
   523 }
   524 
   525 bool ConCmdManager::CheckAccess(int client, const char *cmd, AdminCmdInfo *pAdmin)
   526 {
   527 	if (CheckCommandAccess(client, cmd, pAdmin->eflags))
   528 	{
   529 		return true;
   530 	}
   531 
   532 	edict_t *pEdict = PEntityOfEntIndex(client);
   533 	
   534 	/* If we got here, the command failed... */
   535 	char buffer[128];
   536 	if (!logicore.CoreTranslate(buffer, sizeof(buffer), "%T", 2, NULL, "No Access", &client))
   537 	{
   538 		UTIL_Format(buffer, sizeof(buffer), "You do not have access to this command");
   539 	}
   540 
   541 	unsigned int replyto = g_ChatTriggers.GetReplyTo();
   542 	if (replyto == SM_REPLY_CONSOLE)
   543 	{
   544 		char fullbuffer[192];
   545 		UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s.\n", buffer);
   546 		engine->ClientPrintf(pEdict, fullbuffer);
   547 	}
   548 	else if (replyto == SM_REPLY_CHAT)
   549 	{
   550 		char fullbuffer[192];
   551 		UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s.", buffer);
   552 		g_HL2.TextMsg(client, HUD_PRINTTALK, fullbuffer);
   553 	}
   554 	
   555 	return false;
   556 }
   557 
   558 bool ConCmdManager::AddConsoleCommand(IPluginFunction *pFunction, 
   559 									   const char *name, 
   560 									   const char *description, 
   561 									   int flags)
   562 {
   563 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   564 
   565 	if (!pInfo)
   566 	{
   567 		return false;
   568 	}
   569 
   570 	CmdHook *pHook = new CmdHook();
   571 
   572 	pHook->pf = pFunction;
   573 	if (description && description[0])
   574 	{
   575 		pHook->helptext.assign(description);
   576 	}
   577 	pInfo->conhooks.push_back(pHook);
   578 
   579 	/* Add to the plugin */
   580 	CmdList *pList;
   581 	IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
   582 	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   583 	{
   584 		pList = new CmdList();
   585 		pPlugin->SetProperty("CommandList", pList);
   586 	}
   587 	PlCmdInfo info;
   588 	info.pInfo = pInfo;
   589 	info.type = Cmd_Console;
   590 	info.pHook = pHook;
   591 	AddToPlCmdList(pList, info);
   592 
   593 	return true;
   594 }
   595 
   596 bool ConCmdManager::AddAdminCommand(IPluginFunction *pFunction, 
   597 									 const char *name, 
   598 									 const char *group,
   599 									 int adminflags,
   600 									 const char *description, 
   601 									 int flags)
   602 {
   603 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   604 
   605 	if (!pInfo)
   606 	{
   607 		return false;
   608 	}
   609 
   610 	CmdHook *pHook = new CmdHook();
   611 	AdminCmdInfo *pAdmin = new AdminCmdInfo();
   612 
   613 	pHook->pf = pFunction;
   614 	if (description && description[0])
   615 	{
   616 		pHook->helptext.assign(description);
   617 	}
   618 	pHook->pAdmin = pAdmin;
   619 
   620 	void *object;
   621 	int grpid;
   622 	if (!sm_trie_retrieve(m_pCmdGrps, group, (void **)&object))
   623 	{
   624 		grpid = m_Strings.AddString(group);
   625 		sm_trie_insert(m_pCmdGrps, group, (void *)grpid);
   626 	}
   627 	else
   628 	{
   629 		grpid = (int)object;
   630 	}
   631 
   632 	pAdmin->cmdGrpId = grpid;
   633 	pAdmin->flags = adminflags;
   634 
   635 	/* First get the command group override, if any */
   636 	bool override = g_Admins.GetCommandOverride(group, 
   637 		Override_CommandGroup,
   638 		&(pAdmin->eflags));
   639 
   640 	/* Next get the command override, if any */
   641 	if (g_Admins.GetCommandOverride(name,
   642 		Override_Command,
   643 		&(pAdmin->eflags)))
   644 	{
   645 		override = true;
   646 	}
   647 
   648 	/* Assign normal flags if there were no overrides */
   649 	if (!override)
   650 	{
   651 		pAdmin->eflags = pAdmin->flags;
   652 	}
   653 
   654 	/* Finally, add the hook */
   655 	pInfo->conhooks.push_back(pHook);
   656 	pInfo->admin = *(pHook->pAdmin);
   657 	pInfo->is_admin_set = true;
   658 
   659 	/* Now add to the plugin */
   660 	CmdList *pList;
   661 	IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
   662 	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   663 	{
   664 		pList = new CmdList();
   665 		pPlugin->SetProperty("CommandList", pList);
   666 	}
   667 	PlCmdInfo info;
   668 	info.pInfo = pInfo;
   669 	info.type = Cmd_Admin;
   670 	info.pHook = pHook;
   671 	AddToPlCmdList(pList, info);
   672 
   673 	return true;
   674 }
   675 
   676 bool ConCmdManager::AddServerCommand(IPluginFunction *pFunction, 
   677 									  const char *name, 
   678 									  const char *description, 
   679 									  int flags)
   680 
   681 {
   682 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   683 
   684 	if (!pInfo)
   685 	{
   686 		return false;
   687 	}
   688 
   689 	CmdHook *pHook = new CmdHook();
   690 
   691 	pHook->pf = pFunction;
   692 	if (description && description[0])
   693 	{
   694 		pHook->helptext.assign(description);
   695 	}
   696 
   697 	pInfo->srvhooks.push_back(pHook);
   698 
   699 	/* Add to the plugin */
   700 	CmdList *pList;
   701 	IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
   702 	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   703 	{
   704 		pList = new CmdList();
   705 		pPlugin->SetProperty("CommandList", pList);
   706 	}
   707 	PlCmdInfo info;
   708 	info.pInfo = pInfo;
   709 	info.type = Cmd_Server;
   710 	info.pHook = pHook;
   711 	AddToPlCmdList(pList, info);
   712 
   713 	return true;
   714 }
   715 
   716 void AddToPlCmdList(CmdList *pList, const PlCmdInfo &info)
   717 {
   718 	CmdList::iterator iter = pList->begin();
   719 	bool inserted = false;
   720 	const char *orig = NULL;
   721 
   722 	orig = info.pInfo->pCmd->GetName();
   723 
   724 	/* Insert this into the help list, SORTED alphabetically. */
   725 	while (iter != pList->end())
   726 	{
   727 		PlCmdInfo &obj = (*iter);
   728 		const char *cmd = obj.pInfo->pCmd->GetName();
   729 		if (strcmp(orig, cmd) < 0)
   730 		{
   731 			pList->insert(iter, info);
   732 			inserted = true;
   733 			break;
   734 		}
   735 		iter++;
   736 	}
   737 
   738 	if (!inserted)
   739 	{
   740 		pList->push_back(info);
   741 	}
   742 }
   743 
   744 void ConCmdManager::AddToCmdList(ConCmdInfo *info)
   745 {
   746 	List<ConCmdInfo *>::iterator iter = m_CmdList.begin();
   747 	ConCmdInfo *pInfo;
   748 	bool inserted = false;
   749 	const char *orig = NULL;
   750 
   751 	orig = info->pCmd->GetName();
   752 
   753 	/* Insert this into the help list, SORTED alphabetically. */
   754 	while (iter != m_CmdList.end())
   755 	{
   756 		const char *cmd = NULL;
   757 		pInfo = (*iter);
   758 		cmd = pInfo->pCmd->GetName();
   759 		if (strcmp(orig, cmd) < 0)
   760 		{
   761 			m_CmdList.insert(iter, info);
   762 			inserted = true;
   763 			break;
   764 		}
   765 		iter++;
   766 	}
   767 
   768 	if (!inserted)
   769 	{
   770 		m_CmdList.push_back(info);
   771 	}
   772 }
   773 
   774 void ConCmdManager::UpdateAdminCmdFlags(const char *cmd, OverrideType type, FlagBits bits, bool remove)
   775 {
   776 	ConCmdInfo *pInfo;
   777 
   778 	if (type == Override_Command)
   779 	{
   780 		if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   781 		{
   782 			return;
   783 		}
   784 
   785 		List<CmdHook *>::iterator iter;
   786 		CmdHook *pHook;
   787 
   788 		for (iter=pInfo->conhooks.begin(); iter!=pInfo->conhooks.end(); iter++)
   789 		{
   790 			pHook = (*iter);
   791 			if (pHook->pAdmin)
   792 			{
   793 				if (!remove)
   794 				{
   795 					pHook->pAdmin->eflags = bits;
   796 				} else {
   797 					pHook->pAdmin->eflags = pHook->pAdmin->flags;
   798 				}
   799 				pInfo->admin = *(pHook->pAdmin);
   800 			}
   801 		}
   802 		pInfo->is_admin_set = true;
   803 	}
   804 	else if (type == Override_CommandGroup)
   805 	{
   806 		void *object;
   807 		if (!sm_trie_retrieve(m_pCmdGrps, cmd, &object))
   808 		{
   809 			return;
   810 		}
   811 		int grpid = (int)object;
   812 
   813 		/* This is bad :( loop through all commands */
   814 		List<ConCmdInfo *>::iterator iter;
   815 		CmdHook *pHook;
   816 		for (iter=m_CmdList.begin(); iter!=m_CmdList.end(); iter++)
   817 		{
   818 			pInfo = (*iter);
   819 			for (List<CmdHook *>::iterator citer=pInfo->conhooks.begin();
   820 				 citer!=pInfo->conhooks.end();
   821 				 citer++)
   822 			{
   823 				pHook = (*citer);
   824 				if (pHook->pAdmin && pHook->pAdmin->cmdGrpId == grpid)
   825 				{
   826 					if (remove)
   827 					{
   828 						pHook->pAdmin->eflags = bits;
   829 					} else {
   830 						pHook->pAdmin->eflags = pHook->pAdmin->flags;
   831 					}
   832 					pInfo->admin = *(pHook->pAdmin);
   833 				}
   834 			}
   835 		}
   836 		pInfo->is_admin_set = true;
   837 	}
   838 }
   839 
   840 void ConCmdManager::RemoveConCmd(ConCmdInfo *info, const char *name, bool is_read_safe, bool untrack)
   841 {
   842 	/* Remove from the trie */
   843 	sm_trie_delete(m_pCmds, name);
   844 
   845 	/* Remove console-specific information
   846 	 * This should always be true as of right now
   847 	 */
   848 	if (info->pCmd)
   849 	{
   850 		if (info->sourceMod)
   851 		{
   852 			/* Unlink from SourceMM */
   853 			g_SMAPI->UnregisterConCommandBase(g_PLAPI, info->pCmd);
   854 			/* Delete the command's memory */
   855 			char *new_help = const_cast<char *>(info->pCmd->GetHelpText());
   856 			char *new_name = const_cast<char *>(info->pCmd->GetName());
   857 			delete [] new_help;
   858 			delete [] new_name;
   859 			delete info->pCmd;
   860 		}
   861 		else
   862 		{
   863 			if (is_read_safe)
   864 			{
   865 				/* Remove the external hook */
   866 				SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, info->pCmd, CommandCallback, false);
   867 			}
   868 			if (untrack)
   869 			{
   870 				UntrackConCommandBase(info->pCmd, this);
   871 			}
   872 		}
   873 	}
   874 	
   875 	/* Remove from list */
   876 	m_CmdList.remove(info);
   877 
   878 	delete info;
   879 }
   880 
   881 bool ConCmdManager::LookForSourceModCommand(const char *cmd)
   882 {
   883 	ConCmdInfo *pInfo;
   884 
   885 	if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   886 	{
   887 		return false;
   888 	}
   889 
   890 	return pInfo->sourceMod && (pInfo->conhooks.size() > 0);
   891 }
   892 
   893 bool ConCmdManager::LookForCommandAdminFlags(const char *cmd, FlagBits *pFlags)
   894 {
   895 	ConCmdInfo *pInfo;
   896 
   897 	if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   898 	{
   899 		return false;
   900 	}
   901 
   902 	*pFlags = pInfo->admin.eflags;
   903 
   904 	return pInfo->is_admin_set;
   905 }
   906 
   907 ConCmdInfo *ConCmdManager::AddOrFindCommand(const char *name, const char *description, int flags)
   908 {
   909 	ConCmdInfo *pInfo;
   910 	if (!sm_trie_retrieve(m_pCmds, name, (void **)&pInfo))
   911 	{
   912 		ConCmdList::iterator item = FindInList(name);
   913 		if (item != m_CmdList.end())
   914 			return *item;
   915 
   916 		pInfo = new ConCmdInfo();
   917 		/* Find the commandopan */
   918 		ConCommand *pCmd = FindCommand(name);
   919 
   920 		if (!pCmd)
   921 		{
   922 			/* Note that we have to duplicate because the source might not be 
   923 			 * a static string, and these expect static memory.
   924 			 */
   925 			if (!description)
   926 			{
   927 				description = "";
   928 			}
   929 			char *new_name = sm_strdup(name);
   930 			char *new_help = sm_strdup(description);
   931 			pCmd = new ConCommand(new_name, CommandCallback, new_help, flags);
   932 			pInfo->sourceMod = true;
   933 		}
   934 		else
   935 		{
   936 			TrackConCommandBase(pCmd, this);
   937 			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, pCmd, CommandCallback, false);
   938 		}
   939 
   940 		pInfo->pCmd = pCmd;
   941 		pInfo->is_admin_set = false;
   942 
   943 		sm_trie_insert(m_pCmds, name, pInfo);
   944 		AddToCmdList(pInfo);
   945 	}
   946 
   947 	return pInfo;
   948 }
   949 
   950 void ConCmdManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
   951 {
   952 	if (command.ArgC() >= 3)
   953 	{
   954 		const char *text = command.Arg(2);
   955 
   956 		CPlugin *pPlugin = g_PluginSys.FindPluginByConsoleArg(text);
   957 
   958 		if (!pPlugin)
   959 		{
   960 			g_RootMenu.ConsolePrint("[SM] Plugin \"%s\" was not found.", text);
   961 			return;
   962 		}
   963 
   964 		const sm_plugininfo_t *plinfo = pPlugin->GetPublicInfo();
   965 		const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : pPlugin->GetFilename();
   966 
   967 		CmdList *pList;
   968 		if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   969 		{
   970 			g_RootMenu.ConsolePrint("[SM] No commands found for: %s", plname);
   971 			return;
   972 		}
   973 		if (!pList->size())
   974 		{
   975 			g_RootMenu.ConsolePrint("[SM] No commands found for: %s", plname);
   976 			return;
   977 		}
   978 
   979 		CmdList::iterator iter;
   980 		const char *type = NULL;
   981 		const char *name;
   982 		const char *help;
   983 		g_RootMenu.ConsolePrint("[SM] Listing %d commands for: %s", pList->size(), plname);
   984 		g_RootMenu.ConsolePrint("  %-17.16s %-8.7s %s", "[Name]", "[Type]", "[Help]");
   985 		for (iter=pList->begin();
   986 			 iter!=pList->end();
   987 			 iter++)
   988 		{
   989 			PlCmdInfo &cmd = (*iter);
   990 			if (cmd.type == Cmd_Server)
   991 			{
   992 				type = "server";
   993 			}
   994 			else if (cmd.type == Cmd_Console)
   995 			{
   996 				type = "console";
   997 			}
   998 			else if (cmd.type == Cmd_Admin)
   999 			{
  1000 				type = "admin";
  1001 			}
  1002 			name = cmd.pInfo->pCmd->GetName();
  1003 			if (cmd.pHook->helptext.size())
  1004 			{
  1005 				help = cmd.pHook->helptext.c_str();
  1006 			}
  1007 			else
  1008 			{
  1009 				help = cmd.pInfo->pCmd->GetHelpText();
  1010 			}
  1011 			g_RootMenu.ConsolePrint("  %-17.16s %-12.11s %s", name, type, help);		
  1012 		}
  1013 
  1014 		return;
  1015 	}
  1016 
  1017 	g_RootMenu.ConsolePrint("[SM] Usage: sm cmds <plugin #>");
  1018 }