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