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