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