core/ConCmdManager.cpp
author David Anderson <dvander@alliedmods.net>
Fri Aug 30 10:16:28 2013 -0700 (2013-08-30)
changeset 4109 10eb68d668af
parent 4076 a1aeeac165e7
child 4112 31f38e6aae4e
permissions -rw-r--r--
Use Refcounted to manage to NativeEntry (bug 5852 part 4, r=ds).
     1 /**
     2  * vim: set ts=4 sw=4 tw=99 noet :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "ConCmdManager.h"
    33 #include "sm_srvcmds.h"
    34 #include "AdminCache.h"
    35 #include "sm_stringutil.h"
    36 #include "PlayerManager.h"
    37 #include "HalfLife2.h"
    38 #include "ChatTriggers.h"
    39 #include "logic_bridge.h"
    40 
    41 ConCmdManager g_ConCmds;
    42 
    43 #if SOURCE_ENGINE == SE_DOTA
    44 	SH_DECL_HOOK2_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommandContext &, const CCommand &);
    45 #elif SOURCE_ENGINE >= SE_ORANGEBOX
    46 	SH_DECL_HOOK1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
    47 #else
    48 	SH_DECL_HOOK0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
    49 #endif
    50 
    51 SH_DECL_HOOK1_void(IServerGameClients, SetCommandClient, SH_NOATTRIB, false, int);
    52 
    53 struct PlCmdInfo
    54 {
    55 	ConCmdInfo *pInfo;
    56 	CmdHook *pHook;
    57 	CmdType type;
    58 };
    59 typedef List<PlCmdInfo> CmdList;
    60 
    61 void AddToPlCmdList(CmdList *pList, const PlCmdInfo &info);
    62 
    63 ConCmdManager::ConCmdManager() : m_Strings(1024)
    64 {
    65 	m_CmdClient = 0;
    66 }
    67 
    68 ConCmdManager::~ConCmdManager()
    69 {
    70 }
    71 
    72 void ConCmdManager::OnSourceModAllInitialized()
    73 {
    74 	scripts->AddPluginsListener(this);
    75 	g_RootMenu.AddRootConsoleCommand("cmds", "List console commands", this);
    76 	SH_ADD_HOOK(IServerGameClients, SetCommandClient, serverClients, SH_MEMBER(this, &ConCmdManager::SetCommandClient), false);
    77 }
    78 
    79 void ConCmdManager::OnSourceModShutdown()
    80 {
    81 	scripts->RemovePluginsListener(this);
    82 	/* All commands should already be removed by the time we're done */
    83 	SH_REMOVE_HOOK(IServerGameClients, SetCommandClient, serverClients, SH_MEMBER(this, &ConCmdManager::SetCommandClient), false);
    84 	g_RootMenu.RemoveRootConsoleCommand("cmds", this);
    85 }
    86 
    87 void ConCmdManager::OnUnlinkConCommandBase(ConCommandBase *pBase, const char *name, bool is_read_safe)
    88 {
    89 	/* Whoa, first get its information struct */
    90 	ConCmdInfo *pInfo;
    91 
    92 	if (!m_Cmds.retrieve(name, &pInfo))
    93 		return;
    94 
    95 	RemoveConCmds(pInfo->srvhooks);
    96 	RemoveConCmds(pInfo->conhooks);
    97 
    98 	RemoveConCmd(pInfo, name, is_read_safe, false);
    99 }
   100 
   101 void ConCmdManager::RemoveConCmds(List<CmdHook *> &cmdlist)
   102 {
   103 	List<CmdHook *>::iterator iter = cmdlist.begin();
   104 
   105 	while (iter != cmdlist.end())
   106 	{
   107 		CmdHook *pHook = (*iter);
   108 		IPluginContext *pContext = pHook->pf->GetParentContext();
   109 		IPlugin *pPlugin = scripts->FindPluginByContext(pContext->GetContext());
   110 		CmdList *pList = NULL;
   111 		
   112 		//gaben
   113 		if (!pPlugin->GetProperty("CommandList", (void **)&pList, false) || !pList)
   114 		{
   115 			continue;
   116 		}
   117 
   118 		CmdList::iterator p_iter = pList->begin();
   119 		while (p_iter != pList->end())
   120 		{
   121 			PlCmdInfo &cmd = (*p_iter);
   122 			if (cmd.pHook == pHook)
   123 			{
   124 				p_iter = pList->erase(p_iter);
   125 			}
   126 			else
   127 			{
   128 				p_iter++;
   129 			}
   130 		}
   131 
   132 		delete pHook->pAdmin;
   133 		delete pHook;
   134 		
   135 		iter = cmdlist.erase(iter);
   136 	}
   137 }
   138 
   139 void ConCmdManager::RemoveConCmds(List<CmdHook *> &cmdlist, IPluginContext *pContext)
   140 {
   141 	List<CmdHook *>::iterator iter = cmdlist.begin();
   142 	CmdHook *pHook;
   143 
   144 	while (iter != cmdlist.end())
   145 	{
   146 		pHook = (*iter);
   147 		if (pHook->pf->GetParentContext() == pContext)
   148 		{
   149 			delete pHook->pAdmin;
   150 			delete pHook;
   151 			iter = cmdlist.erase(iter);
   152 		}
   153 		else
   154 		{
   155 			iter++;
   156 		}
   157 	}
   158 }
   159 
   160 void ConCmdManager::OnPluginDestroyed(IPlugin *plugin)
   161 {
   162 	CmdList *pList;
   163 	List<ConCmdInfo *> removed;
   164 	if (plugin->GetProperty("CommandList", (void **)&pList, true))
   165 	{
   166 		IPluginContext *pContext = plugin->GetBaseContext();
   167 		CmdList::iterator iter;
   168 		/* First pass!
   169 	 	 * Only bother if there's an actual command list on this plugin... 
   170 	 	 */
   171 		for (iter=pList->begin();
   172 			iter!=pList->end();
   173 			iter++)
   174 		{
   175 			PlCmdInfo &cmd = (*iter);
   176 			ConCmdInfo *pInfo = cmd.pInfo;
   177 
   178 			/* Has this chain already been fully cleaned/removed? */
   179 			if (removed.find(pInfo) != removed.end())
   180 			{
   181 				continue;
   182 			}
   183 
   184 			/* Remove any hooks from us on this command */
   185 			RemoveConCmds(pInfo->conhooks, pContext);
   186 			RemoveConCmds(pInfo->srvhooks, pContext);
   187 
   188 			/* See if there are still hooks */
   189 			if (pInfo->srvhooks.size())
   190 			{
   191 				continue;
   192 			}
   193 			if (pInfo->conhooks.size())
   194 			{
   195 				continue;
   196 			}
   197 
   198 			/* Remove the command, it should be safe now */
   199 			RemoveConCmd(pInfo, pInfo->pCmd->GetName(), true, true);
   200 			removed.push_back(pInfo);
   201 		}
   202 		delete pList;
   203 	}
   204 }
   205 #if SOURCE_ENGINE == SE_DOTA
   206 void CommandCallback(const CCommandContext &context, const CCommand &command)
   207 {
   208 #elif 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 (!m_Cmds.retrieve(name, &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 #if SOURCE_ENGINE != SE_DOTA
   537 	edict_t *pEdict = PEntityOfEntIndex(client);
   538 #endif
   539 	
   540 	/* If we got here, the command failed... */
   541 	char buffer[128];
   542 	if (!logicore.CoreTranslate(buffer, sizeof(buffer), "%T", 2, NULL, "No Access", &client))
   543 	{
   544 		UTIL_Format(buffer, sizeof(buffer), "You do not have access to this command");
   545 	}
   546 
   547 	unsigned int replyto = g_ChatTriggers.GetReplyTo();
   548 	if (replyto == SM_REPLY_CONSOLE)
   549 	{
   550 		char fullbuffer[192];
   551 		UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s.\n", buffer);
   552 #if SOURCE_ENGINE == SE_DOTA
   553 		engine->ClientPrintf(client, fullbuffer);
   554 #else
   555 		engine->ClientPrintf(pEdict, fullbuffer);
   556 #endif
   557 	}
   558 	else if (replyto == SM_REPLY_CHAT)
   559 	{
   560 		char fullbuffer[192];
   561 		UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s.", buffer);
   562 		g_HL2.TextMsg(client, HUD_PRINTTALK, fullbuffer);
   563 	}
   564 	
   565 	return false;
   566 }
   567 
   568 bool ConCmdManager::AddAdminCommand(IPluginFunction *pFunction, 
   569 									 const char *name, 
   570 									 const char *group,
   571 									 int adminflags,
   572 									 const char *description, 
   573 									 int flags)
   574 {
   575 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   576 
   577 	if (!pInfo)
   578 	{
   579 		return false;
   580 	}
   581 
   582 	CmdHook *pHook = new CmdHook();
   583 	AdminCmdInfo *pAdmin = new AdminCmdInfo();
   584 
   585 	pHook->pf = pFunction;
   586 	if (description && description[0])
   587 	{
   588 		pHook->helptext.assign(description);
   589 	}
   590 	pHook->pAdmin = pAdmin;
   591 
   592 	int grpid;
   593 	if (!m_CmdGrps.retrieve(group, &grpid))
   594 	{
   595 		grpid = m_Strings.AddString(group);
   596 		m_CmdGrps.insert(group, grpid);
   597 	}
   598 
   599 	pAdmin->cmdGrpId = grpid;
   600 	pAdmin->flags = adminflags;
   601 
   602 	/* First get the command group override, if any */
   603 	bool override = g_Admins.GetCommandOverride(group, 
   604 		Override_CommandGroup,
   605 		&(pAdmin->eflags));
   606 
   607 	/* Next get the command override, if any */
   608 	if (g_Admins.GetCommandOverride(name,
   609 		Override_Command,
   610 		&(pAdmin->eflags)))
   611 	{
   612 		override = true;
   613 	}
   614 
   615 	/* Assign normal flags if there were no overrides */
   616 	if (!override)
   617 	{
   618 		pAdmin->eflags = pAdmin->flags;
   619 	}
   620 
   621 	/* Finally, add the hook */
   622 	pInfo->conhooks.push_back(pHook);
   623 	pInfo->admin = *(pHook->pAdmin);
   624 
   625 	/* Now add to the plugin */
   626 	CmdList *pList;
   627 	IPlugin *pPlugin = scripts->FindPluginByContext(pFunction->GetParentContext()->GetContext());
   628 	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   629 	{
   630 		pList = new CmdList();
   631 		pPlugin->SetProperty("CommandList", pList);
   632 	}
   633 	PlCmdInfo info;
   634 	info.pInfo = pInfo;
   635 	info.type = Cmd_Admin;
   636 	info.pHook = pHook;
   637 	AddToPlCmdList(pList, info);
   638 
   639 	return true;
   640 }
   641 
   642 bool ConCmdManager::AddServerCommand(IPluginFunction *pFunction, 
   643 									  const char *name, 
   644 									  const char *description, 
   645 									  int flags)
   646 
   647 {
   648 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   649 
   650 	if (!pInfo)
   651 	{
   652 		return false;
   653 	}
   654 
   655 	CmdHook *pHook = new CmdHook();
   656 
   657 	pHook->pf = pFunction;
   658 	if (description && description[0])
   659 	{
   660 		pHook->helptext.assign(description);
   661 	}
   662 
   663 	pInfo->srvhooks.push_back(pHook);
   664 
   665 	/* Add to the plugin */
   666 	CmdList *pList;
   667 	IPlugin *pPlugin = scripts->FindPluginByContext(pFunction->GetParentContext()->GetContext());
   668 	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   669 	{
   670 		pList = new CmdList();
   671 		pPlugin->SetProperty("CommandList", pList);
   672 	}
   673 	PlCmdInfo info;
   674 	info.pInfo = pInfo;
   675 	info.type = Cmd_Server;
   676 	info.pHook = pHook;
   677 	AddToPlCmdList(pList, info);
   678 
   679 	return true;
   680 }
   681 
   682 void AddToPlCmdList(CmdList *pList, const PlCmdInfo &info)
   683 {
   684 	CmdList::iterator iter = pList->begin();
   685 	bool inserted = false;
   686 	const char *orig = NULL;
   687 
   688 	orig = info.pInfo->pCmd->GetName();
   689 
   690 	/* Insert this into the help list, SORTED alphabetically. */
   691 	while (iter != pList->end())
   692 	{
   693 		PlCmdInfo &obj = (*iter);
   694 		const char *cmd = obj.pInfo->pCmd->GetName();
   695 		if (strcmp(orig, cmd) < 0)
   696 		{
   697 			pList->insert(iter, info);
   698 			inserted = true;
   699 			break;
   700 		}
   701 		iter++;
   702 	}
   703 
   704 	if (!inserted)
   705 	{
   706 		pList->push_back(info);
   707 	}
   708 }
   709 
   710 void ConCmdManager::AddToCmdList(ConCmdInfo *info)
   711 {
   712 	List<ConCmdInfo *>::iterator iter = m_CmdList.begin();
   713 	ConCmdInfo *pInfo;
   714 	bool inserted = false;
   715 	const char *orig = NULL;
   716 
   717 	orig = info->pCmd->GetName();
   718 
   719 	/* Insert this into the help list, SORTED alphabetically. */
   720 	while (iter != m_CmdList.end())
   721 	{
   722 		const char *cmd = NULL;
   723 		pInfo = (*iter);
   724 		cmd = pInfo->pCmd->GetName();
   725 		if (strcmp(orig, cmd) < 0)
   726 		{
   727 			m_CmdList.insert(iter, info);
   728 			inserted = true;
   729 			break;
   730 		}
   731 		iter++;
   732 	}
   733 
   734 	if (!inserted)
   735 	{
   736 		m_CmdList.push_back(info);
   737 	}
   738 }
   739 
   740 void ConCmdManager::UpdateAdminCmdFlags(const char *cmd, OverrideType type, FlagBits bits, bool remove)
   741 {
   742 	ConCmdInfo *pInfo;
   743 
   744 	if (type == Override_Command)
   745 	{
   746 		if (!m_Cmds.retrieve(cmd, &pInfo))
   747 			return;
   748 
   749 		List<CmdHook *>::iterator iter;
   750 		CmdHook *pHook;
   751 
   752 		for (iter=pInfo->conhooks.begin(); iter!=pInfo->conhooks.end(); iter++)
   753 		{
   754 			pHook = (*iter);
   755 			if (pHook->pAdmin)
   756 			{
   757 				if (!remove)
   758 				{
   759 					pHook->pAdmin->eflags = bits;
   760 				} else {
   761 					pHook->pAdmin->eflags = pHook->pAdmin->flags;
   762 				}
   763 				pInfo->admin = *(pHook->pAdmin);
   764 			}
   765 		}
   766 	}
   767 	else if (type == Override_CommandGroup)
   768 	{
   769 		int grpid;
   770 		if (!m_CmdGrps.retrieve(cmd, &grpid))
   771 			return;
   772 
   773 		/* This is bad :( loop through all commands */
   774 		List<ConCmdInfo *>::iterator iter;
   775 		CmdHook *pHook;
   776 		for (iter=m_CmdList.begin(); iter!=m_CmdList.end(); iter++)
   777 		{
   778 			pInfo = (*iter);
   779 			for (List<CmdHook *>::iterator citer=pInfo->conhooks.begin();
   780 				 citer!=pInfo->conhooks.end();
   781 				 citer++)
   782 			{
   783 				pHook = (*citer);
   784 				if (pHook->pAdmin && pHook->pAdmin->cmdGrpId == grpid)
   785 				{
   786 					if (remove)
   787 					{
   788 						pHook->pAdmin->eflags = bits;
   789 					} else {
   790 						pHook->pAdmin->eflags = pHook->pAdmin->flags;
   791 					}
   792 					pInfo->admin = *(pHook->pAdmin);
   793 				}
   794 			}
   795 		}
   796 	}
   797 }
   798 
   799 void ConCmdManager::RemoveConCmd(ConCmdInfo *info, const char *name, bool is_read_safe, bool untrack)
   800 {
   801 	/* Remove from the trie */
   802 	m_Cmds.remove(name);
   803 
   804 	/* Remove console-specific information
   805 	 * This should always be true as of right now
   806 	 */
   807 	if (info->pCmd)
   808 	{
   809 		if (info->sourceMod)
   810 		{
   811 			/* Unlink from SourceMM */
   812 			g_SMAPI->UnregisterConCommandBase(g_PLAPI, info->pCmd);
   813 			/* Delete the command's memory */
   814 			char *new_help = const_cast<char *>(info->pCmd->GetHelpText());
   815 			char *new_name = const_cast<char *>(info->pCmd->GetName());
   816 			delete [] new_help;
   817 			delete [] new_name;
   818 			delete info->pCmd;
   819 		}
   820 		else
   821 		{
   822 			if (is_read_safe)
   823 			{
   824 				/* Remove the external hook */
   825 				SH_REMOVE_HOOK(ConCommand, Dispatch, info->pCmd, SH_STATIC(CommandCallback), false);
   826 			}
   827 			if (untrack)
   828 			{
   829 				UntrackConCommandBase(info->pCmd, this);
   830 			}
   831 		}
   832 	}
   833 	
   834 	/* Remove from list */
   835 	m_CmdList.remove(info);
   836 
   837 	delete info;
   838 }
   839 
   840 bool ConCmdManager::LookForSourceModCommand(const char *cmd)
   841 {
   842 	ConCmdInfo *pInfo;
   843 	if (!m_Cmds.retrieve(cmd, &pInfo))
   844 		return false;
   845 
   846 	return pInfo->sourceMod && (pInfo->conhooks.size() > 0);
   847 }
   848 
   849 bool ConCmdManager::LookForCommandAdminFlags(const char *cmd, FlagBits *pFlags)
   850 {
   851 	ConCmdInfo *pInfo;
   852 	if (!m_Cmds.retrieve(cmd, &pInfo))
   853 		return false;
   854 
   855 	*pFlags = pInfo->admin.eflags;
   856 	return true;
   857 }
   858 
   859 ConCmdInfo *ConCmdManager::AddOrFindCommand(const char *name, const char *description, int flags)
   860 {
   861 	ConCmdInfo *pInfo;
   862 	if (!m_Cmds.retrieve(name, &pInfo))
   863 	{
   864 		ConCmdList::iterator item = FindInList(name);
   865 		if (item != m_CmdList.end())
   866 			return *item;
   867 
   868 		pInfo = new ConCmdInfo();
   869 		/* Find the commandopan */
   870 		ConCommand *pCmd = FindCommand(name);
   871 
   872 		if (!pCmd)
   873 		{
   874 			/* Note that we have to duplicate because the source might not be 
   875 			 * a static string, and these expect static memory.
   876 			 */
   877 			if (!description)
   878 			{
   879 				description = "";
   880 			}
   881 			char *new_name = sm_strdup(name);
   882 			char *new_help = sm_strdup(description);
   883 			pCmd = new ConCommand(new_name, CommandCallback, new_help, flags);
   884 			pInfo->sourceMod = true;
   885 		}
   886 		else
   887 		{
   888 			TrackConCommandBase(pCmd, this);
   889 			SH_ADD_HOOK(ConCommand, Dispatch, pCmd, SH_STATIC(CommandCallback), false);
   890 		}
   891 
   892 		pInfo->pCmd = pCmd;
   893 
   894 		m_Cmds.insert(name, pInfo);
   895 		AddToCmdList(pInfo);
   896 	}
   897 
   898 	return pInfo;
   899 }
   900 
   901 void ConCmdManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
   902 {
   903 	if (command.ArgC() >= 3)
   904 	{
   905 		const char *text = command.Arg(2);
   906 
   907 		IPlugin *pPlugin = scripts->FindPluginByConsoleArg(text);
   908 
   909 		if (!pPlugin)
   910 		{
   911 			g_RootMenu.ConsolePrint("[SM] Plugin \"%s\" was not found.", text);
   912 			return;
   913 		}
   914 
   915 		const sm_plugininfo_t *plinfo = pPlugin->GetPublicInfo();
   916 		const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : pPlugin->GetFilename();
   917 
   918 		CmdList *pList;
   919 		if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   920 		{
   921 			g_RootMenu.ConsolePrint("[SM] No commands found for: %s", plname);
   922 			return;
   923 		}
   924 		if (!pList->size())
   925 		{
   926 			g_RootMenu.ConsolePrint("[SM] No commands found for: %s", plname);
   927 			return;
   928 		}
   929 
   930 		CmdList::iterator iter;
   931 		const char *type = NULL;
   932 		const char *name;
   933 		const char *help;
   934 		g_RootMenu.ConsolePrint("[SM] Listing %d commands for: %s", pList->size(), plname);
   935 		g_RootMenu.ConsolePrint("  %-17.16s %-8.7s %s", "[Name]", "[Type]", "[Help]");
   936 		for (iter=pList->begin();
   937 			 iter!=pList->end();
   938 			 iter++)
   939 		{
   940 			PlCmdInfo &cmd = (*iter);
   941 			if (cmd.type == Cmd_Server)
   942 			{
   943 				type = "server";
   944 			}
   945 			else if (cmd.type == Cmd_Admin)
   946 			{
   947 				type = (cmd.pInfo->admin.eflags == 0)?"console":"admin";
   948 			}
   949 			name = cmd.pInfo->pCmd->GetName();
   950 			if (cmd.pHook->helptext.size())
   951 			{
   952 				help = cmd.pHook->helptext.c_str();
   953 			}
   954 			else
   955 			{
   956 				help = cmd.pInfo->pCmd->GetHelpText();
   957 			}
   958 			g_RootMenu.ConsolePrint("  %-17.16s %-12.11s %s", name, type, help);		
   959 		}
   960 
   961 		return;
   962 	}
   963 
   964 	g_RootMenu.ConsolePrint("[SM] Usage: sm cmds <plugin #>");
   965 }