core/ConCmdManager.cpp
author David Anderson <dvander@alliedmods.net>
Sat Aug 31 11:51:23 2013 -0700 (2013-08-31)
changeset 4112 31f38e6aae4e
parent 4076 a1aeeac165e7
child 4113 12e7f3b54c35
permissions -rw-r--r--
Clean up ConCmdManager (bug 5900, r=fyren).
     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 typedef ke::LinkedList<CmdHook *> PluginHookList;
    54 void RegisterInPlugin(CmdHook *hook);
    55 
    56 ConCmdManager::ConCmdManager() : m_Strings(1024)
    57 {
    58 	m_CmdClient = 0;
    59 }
    60 
    61 ConCmdManager::~ConCmdManager()
    62 {
    63 }
    64 
    65 void ConCmdManager::OnSourceModAllInitialized()
    66 {
    67 	scripts->AddPluginsListener(this);
    68 	g_RootMenu.AddRootConsoleCommand("cmds", "List console commands", this);
    69 	SH_ADD_HOOK(IServerGameClients, SetCommandClient, serverClients, SH_MEMBER(this, &ConCmdManager::SetCommandClient), false);
    70 }
    71 
    72 void ConCmdManager::OnSourceModShutdown()
    73 {
    74 	scripts->RemovePluginsListener(this);
    75 	/* All commands should already be removed by the time we're done */
    76 	SH_REMOVE_HOOK(IServerGameClients, SetCommandClient, serverClients, SH_MEMBER(this, &ConCmdManager::SetCommandClient), false);
    77 	g_RootMenu.RemoveRootConsoleCommand("cmds", this);
    78 }
    79 
    80 void ConCmdManager::OnUnlinkConCommandBase(ConCommandBase *pBase, const char *name, bool is_read_safe)
    81 {
    82 	/* Whoa, first get its information struct */
    83 	ConCmdInfo *pInfo;
    84 
    85 	if (!m_Cmds.retrieve(name, &pInfo))
    86 		return;
    87 
    88 	CmdHookList::iterator iter = pInfo->hooks.begin();
    89 	while (iter != pInfo->hooks.end())
    90 	{
    91 		CmdHook *hook = *iter;
    92 
    93 		IPluginContext *pContext = hook->pf->GetParentContext();
    94 		IPlugin *pPlugin = scripts->FindPluginByContext(pContext->GetContext());
    95 
    96 		// The list is guaranteed to exist.
    97 		PluginHookList *list;
    98 		pPlugin->GetProperty("CommandList", (void **)&list, false);
    99 		for (PluginHookList::iterator hiter = list->begin(); hiter != list->end(); hiter++)
   100 		{
   101 			if (*hiter == hook)
   102 			{
   103 				list->erase(hiter);
   104 				break;
   105 			}
   106 		}
   107 
   108 		iter = pInfo->hooks.erase(iter);
   109 		delete hook;
   110 	}
   111 
   112 	RemoveConCmd(pInfo, name, is_read_safe, false);
   113 }
   114 
   115 void ConCmdManager::OnPluginDestroyed(IPlugin *plugin)
   116 {
   117 	PluginHookList *pList;
   118 	if (!plugin->GetProperty("CommandList", (void **)&pList, true))
   119 		return;
   120 
   121 	PluginHookList::iterator iter = pList->begin();
   122 	while (iter != pList->end())
   123 	{
   124 		CmdHook *hook = *iter;
   125 
   126 		hook->info->hooks.remove(hook);
   127 
   128 		if (hook->info->hooks.empty())
   129 			RemoveConCmd(hook->info, hook->info->pCmd->GetName(), true, true);
   130 
   131 		iter = pList->erase(iter);
   132 		delete hook;
   133 	}
   134 
   135 	delete pList;
   136 }
   137 
   138 #if SOURCE_ENGINE == SE_DOTA
   139 void CommandCallback(const CCommandContext &context, const CCommand &command)
   140 {
   141 #elif SOURCE_ENGINE >= SE_ORANGEBOX
   142 void CommandCallback(const CCommand &command)
   143 {
   144 #else
   145 void CommandCallback()
   146 {
   147 	CCommand command;
   148 #endif
   149 
   150 	g_HL2.PushCommandStack(&command);
   151 
   152 	g_ConCmds.InternalDispatch(command);
   153 
   154 	g_HL2.PopCommandStack();
   155 }
   156 
   157 void ConCmdManager::SetCommandClient(int client)
   158 {
   159 	m_CmdClient = client + 1;
   160 }
   161 
   162 ConCmdInfo *ConCmdManager::FindInTrie(const char *name)
   163 {
   164 	ConCmdInfo *pInfo;
   165 	if (!m_Cmds.retrieve(name, &pInfo))
   166 		return NULL;
   167 	return pInfo;
   168 }
   169 
   170 ConCmdList::iterator ConCmdManager::FindInList(const char *cmd)
   171 {
   172 	List<ConCmdInfo *>::iterator iter = m_CmdList.begin();
   173 
   174 	while (iter != m_CmdList.end())
   175 	{
   176 		if (strcasecmp((*iter)->pCmd->GetName(), cmd) == 0)
   177 			break;
   178 		iter++;
   179 	}
   180 
   181 	return iter;
   182 }
   183 
   184 ResultType ConCmdManager::DispatchClientCommand(int client, const char *cmd, int args, ResultType type)
   185 {
   186 	ConCmdInfo *pInfo;
   187 
   188 	if ((pInfo = FindInTrie(cmd)) == NULL)
   189 	{
   190 		ConCmdList::iterator item = FindInList(cmd);
   191 		if (item == m_CmdList.end())
   192 			return type;
   193 		pInfo = *item;
   194 	}
   195 
   196 	cell_t result = type;
   197 	for (CmdHookList::iterator iter = pInfo->hooks.begin(); iter != pInfo->hooks.end(); iter++)
   198 	{
   199 		CmdHook *hook = *iter;
   200 
   201 		if (hook->type == CmdHook::Server || !hook->pf->IsRunnable())
   202 			continue;
   203 
   204 		if (hook->admin && !CheckAccess(client, cmd, hook->admin))
   205 		{
   206 			if (result < Pl_Handled)
   207 				result = Pl_Handled;
   208 			continue;
   209 		}
   210 
   211 		hook->pf->PushCell(client);
   212 		hook->pf->PushCell(args);
   213 
   214 		cell_t tempres = result;
   215 		if (hook->pf->Execute(&tempres) == SP_ERROR_NONE)
   216 		{
   217 			if (tempres > result)
   218 				result = tempres;
   219 			if (result == Pl_Stop)
   220 				break;
   221 		}
   222 	}
   223 
   224 	return (ResultType)result;
   225 }
   226 
   227 void ConCmdManager::InternalDispatch(const CCommand &command)
   228 {
   229 	int client = m_CmdClient;
   230 
   231 	if (client)
   232 	{
   233 		CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   234 		if (!pPlayer || !pPlayer->IsConnected())
   235 			return;
   236 	}
   237 
   238 	/**
   239 	 * Note: Console commands will EITHER go through IServerGameDLL::ClientCommand,
   240 	 * OR this dispatch.  They will NEVER go through both.
   241 	 * -- 
   242 	 * Whether or not it goes through the callback is determined by FCVAR_GAMEDLL
   243 	 */
   244 	const char *cmd = g_HL2.CurrentCommandName();
   245 
   246 	ConCmdInfo *pInfo = FindInTrie(cmd);
   247 	if (pInfo == NULL)
   248 	{
   249         /* Unfortunately, we now have to do a slow lookup because Valve made client commands 
   250          * case-insensitive.  We can't even use our sortedness.
   251          */
   252         if (client == 0 && !engine->IsDedicatedServer())
   253             return;
   254 
   255 		ConCmdList::iterator item = FindInList(cmd);
   256 		if (item == m_CmdList.end())
   257 			return;
   258 
   259 		pInfo = *item;
   260 	}
   261 
   262 	/* This is a hack to prevent say triggers from firing on messages that were 
   263 	 * blocked because of flooding.  We won't remove this, but the hack will get 
   264 	 * "nicer" when we expose explicit say hooks.
   265 	 */
   266 	if (g_ChatTriggers.WasFloodedMessage())
   267 		return;
   268 
   269 	cell_t result = Pl_Continue;
   270 	int args = command.ArgC() - 1;
   271 
   272 	// On a listen server, sometimes the server host's client index can be set
   273 	// as 0. So index 1 is passed to the command callback to correct this
   274 	// potential problem.
   275 	int realClient = (!engine->IsDedicatedServer() && client == 0)
   276 	                 ? g_Players.ListenClient()
   277 	                 : client;
   278 	int dedicatedClient = engine->IsDedicatedServer() ? 0 : g_Players.ListenClient();
   279 
   280 	for (CmdHookList::iterator iter = pInfo->hooks.begin(); iter != pInfo->hooks.end(); iter++)
   281 	{
   282 		CmdHook *hook = *iter;
   283 
   284 		if (!hook->pf->IsRunnable())
   285 			continue;
   286 
   287 		if (hook->type == CmdHook::Server)
   288 		{
   289 			// Don't execute unless we're in the server console.
   290 			if (realClient != dedicatedClient)
   291 				continue;
   292 		} else {
   293 			// Check admin rights if needed. realClient isn't needed since we
   294 			// should bypass admin checks if client == 0 anyway.
   295 			if (client && hook->admin && !CheckAccess(client, cmd, hook->admin))
   296 			{
   297 				if (result < Pl_Handled)
   298 					result = Pl_Handled;
   299 				continue;
   300 			}
   301 
   302 			// Client hooks get a client argument.
   303 			hook->pf->PushCell(realClient);
   304 		}
   305 
   306 		hook->pf->PushCell(args);
   307 
   308 		cell_t tempres = result;
   309 		if (hook->pf->Execute(&tempres) == SP_ERROR_NONE)
   310 		{
   311 			if (tempres > result)
   312 				result = tempres;
   313 			if (result == Pl_Stop)
   314 				break;
   315 		}
   316 	}
   317 
   318 	if (result >= Pl_Handled)
   319 	{
   320 		if (!pInfo->sourceMod)
   321 			RETURN_META(MRES_SUPERCEDE);
   322 		return;
   323 	}
   324 }
   325 
   326 bool ConCmdManager::CheckClientCommandAccess(int client, const char *cmd, FlagBits cmdflags)
   327 {
   328 	if (cmdflags == 0 || client == 0)
   329 	{
   330 		return true;
   331 	}
   332 
   333 	/* If running listen server, then client 1 is the server host and should have 'root' access */
   334 	if (client == 1 && !engine->IsDedicatedServer())
   335 	{
   336 		return true;
   337 	}
   338 
   339 	CPlayer *player = g_Players.GetPlayerByIndex(client);
   340 	if (!player 
   341 		|| player->GetEdict() == NULL
   342 		|| player->IsFakeClient())
   343 	{
   344 		return false;
   345 	}
   346 
   347 	return CheckAdminCommandAccess(player->GetAdminId(), cmd, cmdflags);
   348 }
   349 
   350 bool ConCmdManager::CheckAdminCommandAccess(AdminId adm, const char *cmd, FlagBits cmdflags)
   351 {
   352 	if (adm != INVALID_ADMIN_ID)
   353 	{
   354 		FlagBits bits = g_Admins.GetAdminFlags(adm, Access_Effective);
   355 
   356 		/* root knows all, WHOA */
   357 		if ((bits & ADMFLAG_ROOT) == ADMFLAG_ROOT)
   358 		{
   359 			return true;
   360 		}
   361 
   362 		/* Check for overrides
   363 		 * :TODO: is it worth optimizing this?
   364 		 */
   365 		unsigned int groups = g_Admins.GetAdminGroupCount(adm);
   366 		GroupId gid;
   367 		OverrideRule rule;
   368 		bool override = false;
   369 		for (unsigned int i=0; i<groups; i++)
   370 		{
   371 			gid = g_Admins.GetAdminGroup(adm, i, NULL);
   372 			/* First get group-level override */
   373 			override = g_Admins.GetGroupCommandOverride(gid, cmd, Override_CommandGroup, &rule);
   374 			/* Now get the specific command override */
   375 			if (g_Admins.GetGroupCommandOverride(gid, cmd, Override_Command, &rule))
   376 			{
   377 				override = true;
   378 			}
   379 			if (override)
   380 			{
   381 				if (rule == Command_Allow)
   382 				{
   383 					return true;
   384 				}
   385 				else if (rule == Command_Deny)
   386 				{
   387 					return false;
   388 				}
   389 			}
   390 		}
   391 
   392 		/* See if our other flags match */
   393 		if ((bits & cmdflags) == cmdflags)
   394 		{
   395 			return true;
   396 		}
   397 	}
   398 
   399 	return false;
   400 }
   401 
   402 bool ConCmdManager::CheckAccess(int client, const char *cmd, AdminCmdInfo *pAdmin)
   403 {
   404 	if (CheckClientCommandAccess(client, cmd, pAdmin->eflags))
   405 	{
   406 		return true;
   407 	}
   408 
   409 #if SOURCE_ENGINE != SE_DOTA
   410 	edict_t *pEdict = PEntityOfEntIndex(client);
   411 #endif
   412 	
   413 	/* If we got here, the command failed... */
   414 	char buffer[128];
   415 	if (!logicore.CoreTranslate(buffer, sizeof(buffer), "%T", 2, NULL, "No Access", &client))
   416 	{
   417 		UTIL_Format(buffer, sizeof(buffer), "You do not have access to this command");
   418 	}
   419 
   420 	unsigned int replyto = g_ChatTriggers.GetReplyTo();
   421 	if (replyto == SM_REPLY_CONSOLE)
   422 	{
   423 		char fullbuffer[192];
   424 		UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s.\n", buffer);
   425 #if SOURCE_ENGINE == SE_DOTA
   426 		engine->ClientPrintf(client, fullbuffer);
   427 #else
   428 		engine->ClientPrintf(pEdict, fullbuffer);
   429 #endif
   430 	}
   431 	else if (replyto == SM_REPLY_CHAT)
   432 	{
   433 		char fullbuffer[192];
   434 		UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s.", buffer);
   435 		g_HL2.TextMsg(client, HUD_PRINTTALK, fullbuffer);
   436 	}
   437 	
   438 	return false;
   439 }
   440 
   441 bool ConCmdManager::AddAdminCommand(IPluginFunction *pFunction, 
   442 									 const char *name, 
   443 									 const char *group,
   444 									 int adminflags,
   445 									 const char *description, 
   446 									 int flags)
   447 {
   448 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   449 
   450 	if (!pInfo)
   451 		return false;
   452 
   453 	CmdHook *pHook = new CmdHook(CmdHook::Client, pInfo, pFunction, description);
   454 
   455 	pHook->admin = new AdminCmdInfo();
   456 
   457 	int grpid;
   458 	if (!m_CmdGrps.retrieve(group, &grpid))
   459 	{
   460 		grpid = m_Strings.AddString(group);
   461 		m_CmdGrps.insert(group, grpid);
   462 	}
   463 
   464 	pHook->admin->cmdGrpId = grpid;
   465 	pHook->admin->flags = adminflags;
   466 
   467 	/* First get the command group override, if any */
   468 	bool override = g_Admins.GetCommandOverride(group, 
   469 		Override_CommandGroup,
   470 		&(pHook->admin->eflags));
   471 
   472 	/* Next get the command override, if any */
   473 	if (g_Admins.GetCommandOverride(name,
   474 		Override_Command,
   475 		&(pHook->admin->eflags)))
   476 	{
   477 		override = true;
   478 	}
   479 
   480 	/* Assign normal flags if there were no overrides */
   481 	if (!override)
   482 		pHook->admin->eflags = pHook->admin->flags;
   483 	pInfo->eflags = pHook->admin->eflags;
   484 
   485 	pInfo->hooks.append(pHook);
   486 	RegisterInPlugin(pHook);
   487 	return true;
   488 }
   489 
   490 bool ConCmdManager::AddServerCommand(IPluginFunction *pFunction, 
   491 									  const char *name, 
   492 									  const char *description, 
   493 									  int flags)
   494 
   495 {
   496 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   497 
   498 	if (!pInfo)
   499 		return false;
   500 
   501 	CmdHook *pHook = new CmdHook(CmdHook::Server, pInfo, pFunction, description);
   502 
   503 	pInfo->hooks.append(pHook);
   504 	RegisterInPlugin(pHook);
   505 	return true;
   506 }
   507 
   508 void RegisterInPlugin(CmdHook *hook)
   509 {
   510 	PluginHookList *pList;
   511 
   512 	IPlugin *pPlugin = scripts->FindPluginByContext(hook->pf->GetParentContext());
   513 	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   514 	{
   515 		pList = new PluginHookList();
   516 		pPlugin->SetProperty("CommandList", pList);
   517 	}
   518 
   519 	const char *orig = hook->info->pCmd->GetName();
   520 
   521 	/* Insert this into the help list, SORTED alphabetically. */
   522 	PluginHookList::iterator iter = pList->begin();
   523 	while (iter != pList->end())
   524 	{
   525 		const char *cmd = (*iter)->info->pCmd->GetName();
   526 		if (strcmp(orig, cmd) < 0)
   527 		{
   528 			pList->insertBefore(iter, hook);
   529 			return;
   530 		}
   531 		iter++;
   532 	}
   533 
   534 	pList->append(hook);
   535 }
   536 
   537 void ConCmdManager::AddToCmdList(ConCmdInfo *info)
   538 {
   539 	List<ConCmdInfo *>::iterator iter = m_CmdList.begin();
   540 	ConCmdInfo *pInfo;
   541 	bool inserted = false;
   542 	const char *orig = NULL;
   543 
   544 	orig = info->pCmd->GetName();
   545 
   546 	/* Insert this into the help list, SORTED alphabetically. */
   547 	while (iter != m_CmdList.end())
   548 	{
   549 		const char *cmd = NULL;
   550 		pInfo = (*iter);
   551 		cmd = pInfo->pCmd->GetName();
   552 		if (strcmp(orig, cmd) < 0)
   553 		{
   554 			m_CmdList.insert(iter, info);
   555 			inserted = true;
   556 			break;
   557 		}
   558 		iter++;
   559 	}
   560 
   561 	if (!inserted)
   562 	{
   563 		m_CmdList.push_back(info);
   564 	}
   565 }
   566 
   567 void ConCmdManager::UpdateAdminCmdFlags(const char *cmd, OverrideType type, FlagBits bits, bool remove)
   568 {
   569 	if (type == Override_Command)
   570 	{
   571 		ConCmdInfo *pInfo;
   572 		if (!m_Cmds.retrieve(cmd, &pInfo))
   573 			return;
   574 
   575 		for (CmdHookList::iterator iter = pInfo->hooks.begin(); iter != pInfo->hooks.end(); iter++)
   576 		{
   577 			if (!iter->admin)
   578 				continue;
   579 
   580 			if (!remove)
   581 				iter->admin->eflags = bits;
   582 			else
   583 				iter->admin->eflags = iter->admin->flags;
   584 			pInfo->eflags = iter->admin->eflags;
   585 		}
   586 	}
   587 	else if (type == Override_CommandGroup)
   588 	{
   589 		int grpid;
   590 		if (!m_CmdGrps.retrieve(cmd, &grpid))
   591 			return;
   592 
   593 		/* This is bad :( loop through all commands */
   594 		List<ConCmdInfo *>::iterator iter;
   595 		for (iter=m_CmdList.begin(); iter!=m_CmdList.end(); iter++)
   596 		{
   597 			ConCmdInfo *pInfo = *iter;
   598 			for (CmdHookList::iterator citer = pInfo->hooks.begin(); citer != pInfo->hooks.end(); citer++)
   599 			{
   600 				CmdHook *hook = (*citer);
   601 				if (!hook->admin || hook->admin->cmdGrpId != grpid)
   602 					continue;
   603 
   604 				if (remove)
   605 					hook->admin->eflags = bits;
   606 				else
   607 					hook->admin->eflags = hook->admin->flags;
   608 				pInfo->eflags = hook->admin->eflags;
   609 			}
   610 		}
   611 	}
   612 }
   613 
   614 void ConCmdManager::RemoveConCmd(ConCmdInfo *info, const char *name, bool is_read_safe, bool untrack)
   615 {
   616 	/* Remove from the trie */
   617 	m_Cmds.remove(name);
   618 
   619 	/* Remove console-specific information
   620 	 * This should always be true as of right now
   621 	 */
   622 	if (info->pCmd)
   623 	{
   624 		if (info->sourceMod)
   625 		{
   626 			/* Unlink from SourceMM */
   627 			g_SMAPI->UnregisterConCommandBase(g_PLAPI, info->pCmd);
   628 			/* Delete the command's memory */
   629 			char *new_help = const_cast<char *>(info->pCmd->GetHelpText());
   630 			char *new_name = const_cast<char *>(info->pCmd->GetName());
   631 			delete [] new_help;
   632 			delete [] new_name;
   633 			delete info->pCmd;
   634 		}
   635 		else
   636 		{
   637 			if (is_read_safe)
   638 			{
   639 				/* Remove the external hook */
   640 				SH_REMOVE_HOOK(ConCommand, Dispatch, info->pCmd, SH_STATIC(CommandCallback), false);
   641 			}
   642 			if (untrack)
   643 			{
   644 				UntrackConCommandBase(info->pCmd, this);
   645 			}
   646 		}
   647 	}
   648 	
   649 	/* Remove from list */
   650 	m_CmdList.remove(info);
   651 
   652 	delete info;
   653 }
   654 
   655 bool ConCmdManager::LookForSourceModCommand(const char *cmd)
   656 {
   657 	ConCmdInfo *pInfo;
   658 	if (!m_Cmds.retrieve(cmd, &pInfo))
   659 		return false;
   660 
   661 	return pInfo->sourceMod && !pInfo->hooks.empty();
   662 }
   663 
   664 bool ConCmdManager::LookForCommandAdminFlags(const char *cmd, FlagBits *pFlags)
   665 {
   666 	ConCmdInfo *pInfo;
   667 	if (!m_Cmds.retrieve(cmd, &pInfo))
   668 		return false;
   669 
   670 	*pFlags = pInfo->eflags;
   671 	return true;
   672 }
   673 
   674 ConCmdInfo *ConCmdManager::AddOrFindCommand(const char *name, const char *description, int flags)
   675 {
   676 	ConCmdInfo *pInfo;
   677 	if (!m_Cmds.retrieve(name, &pInfo))
   678 	{
   679 		ConCmdList::iterator item = FindInList(name);
   680 		if (item != m_CmdList.end())
   681 			return *item;
   682 
   683 		pInfo = new ConCmdInfo();
   684 		/* Find the commandopan */
   685 		ConCommand *pCmd = FindCommand(name);
   686 
   687 		if (!pCmd)
   688 		{
   689 			/* Note that we have to duplicate because the source might not be 
   690 			 * a static string, and these expect static memory.
   691 			 */
   692 			if (!description)
   693 			{
   694 				description = "";
   695 			}
   696 			char *new_name = sm_strdup(name);
   697 			char *new_help = sm_strdup(description);
   698 			pCmd = new ConCommand(new_name, CommandCallback, new_help, flags);
   699 			pInfo->sourceMod = true;
   700 		}
   701 		else
   702 		{
   703 			TrackConCommandBase(pCmd, this);
   704 			SH_ADD_HOOK(ConCommand, Dispatch, pCmd, SH_STATIC(CommandCallback), false);
   705 		}
   706 
   707 		pInfo->pCmd = pCmd;
   708 
   709 		m_Cmds.insert(name, pInfo);
   710 		AddToCmdList(pInfo);
   711 	}
   712 
   713 	return pInfo;
   714 }
   715 
   716 void ConCmdManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
   717 {
   718 	if (command.ArgC() >= 3)
   719 	{
   720 		const char *text = command.Arg(2);
   721 
   722 		IPlugin *pPlugin = scripts->FindPluginByConsoleArg(text);
   723 
   724 		if (!pPlugin)
   725 		{
   726 			g_RootMenu.ConsolePrint("[SM] Plugin \"%s\" was not found.", text);
   727 			return;
   728 		}
   729 
   730 		const sm_plugininfo_t *plinfo = pPlugin->GetPublicInfo();
   731 		const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : pPlugin->GetFilename();
   732 
   733 		PluginHookList *pList;
   734 		if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   735 		{
   736 			g_RootMenu.ConsolePrint("[SM] No commands found for: %s", plname);
   737 			return;
   738 		}
   739 		if (pList->empty())
   740 		{
   741 			g_RootMenu.ConsolePrint("[SM] No commands found for: %s", plname);
   742 			return;
   743 		}
   744 
   745 		const char *type = NULL;
   746 		const char *name;
   747 		const char *help;
   748 		g_RootMenu.ConsolePrint("[SM] Listing commands for: %s", plname);
   749 		g_RootMenu.ConsolePrint("  %-17.16s %-8.7s %s", "[Name]", "[Type]", "[Help]");
   750 		for (PluginHookList::iterator iter = pList->begin(); iter != pList->end(); iter++)
   751 		{
   752 			CmdHook *hook = *iter;
   753 			if (hook->type == CmdHook::Server)
   754 				type = "server";
   755 			else 
   756 				type = hook->info->eflags == 0 ? "console" : "admin";
   757 
   758 			name = hook->info->pCmd->GetName();
   759 			if (hook->helptext.length())
   760 				help = hook->helptext.chars();
   761 			else
   762 				help = hook->info->pCmd->GetHelpText();
   763 			g_RootMenu.ConsolePrint("  %-17.16s %-12.11s %s", name, type, help);		
   764 		}
   765 
   766 		return;
   767 	}
   768 
   769 	g_RootMenu.ConsolePrint("[SM] Usage: sm cmds <plugin #>");
   770 }