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