core/ConCmdManager.cpp
author David Anderson <dvander@alliedmods.net>
Mon Oct 14 11:56:43 2013 -0700 (2013-10-14)
changeset 4147 ee4b042d33db
parent 4113 12e7f3b54c35
child 4217 4c87212c5817
permissions -rw-r--r--
Update linux dep script (no bug, r=NPOTB).
     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 #if SOURCE_ENGINE != SE_DOTA
   417 	edict_t *pEdict = PEntityOfEntIndex(client);
   418 #endif
   419 	
   420 	/* If we got here, the command failed... */
   421 	char buffer[128];
   422 	if (!logicore.CoreTranslate(buffer, sizeof(buffer), "%T", 2, NULL, "No Access", &client))
   423 	{
   424 		UTIL_Format(buffer, sizeof(buffer), "You do not have access to this command");
   425 	}
   426 
   427 	unsigned int replyto = g_ChatTriggers.GetReplyTo();
   428 	if (replyto == SM_REPLY_CONSOLE)
   429 	{
   430 		char fullbuffer[192];
   431 		UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s.\n", buffer);
   432 #if SOURCE_ENGINE == SE_DOTA
   433 		engine->ClientPrintf(client, fullbuffer);
   434 #else
   435 		engine->ClientPrintf(pEdict, fullbuffer);
   436 #endif
   437 	}
   438 	else if (replyto == SM_REPLY_CHAT)
   439 	{
   440 		char fullbuffer[192];
   441 		UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s.", buffer);
   442 		g_HL2.TextMsg(client, HUD_PRINTTALK, fullbuffer);
   443 	}
   444 	
   445 	return false;
   446 }
   447 
   448 bool ConCmdManager::AddAdminCommand(IPluginFunction *pFunction, 
   449 									 const char *name, 
   450 									 const char *group,
   451 									 int adminflags,
   452 									 const char *description, 
   453 									 int flags)
   454 {
   455 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   456 
   457 	if (!pInfo)
   458 		return false;
   459 
   460 	GroupMap::Insert i = m_CmdGrps.findForAdd(group);
   461 	if (!i.found())
   462 	{
   463 		if (!m_CmdGrps.add(i, group))
   464 			return false;
   465 		i->value = NoAddRef(new CommandGroup());
   466 	}
   467 	Ref<CommandGroup> cmdgroup = i->value;
   468 
   469 	CmdHook *pHook = new CmdHook(CmdHook::Client, pInfo, pFunction, description);
   470 	pHook->admin = new AdminCmdInfo(cmdgroup, adminflags);
   471 
   472 	/* First get the command group override, if any */
   473 	bool override = g_Admins.GetCommandOverride(group, 
   474 		Override_CommandGroup,
   475 		&(pHook->admin->eflags));
   476 
   477 	/* Next get the command override, if any */
   478 	if (g_Admins.GetCommandOverride(name,
   479 		Override_Command,
   480 		&(pHook->admin->eflags)))
   481 	{
   482 		override = true;
   483 	}
   484 
   485 	/* Assign normal flags if there were no overrides */
   486 	if (!override)
   487 		pHook->admin->eflags = pHook->admin->flags;
   488 	pInfo->eflags = pHook->admin->eflags;
   489 
   490 	cmdgroup->hooks.append(pHook);
   491 	pInfo->hooks.append(pHook);
   492 	RegisterInPlugin(pHook);
   493 	return true;
   494 }
   495 
   496 bool ConCmdManager::AddServerCommand(IPluginFunction *pFunction, 
   497 									  const char *name, 
   498 									  const char *description, 
   499 									  int flags)
   500 
   501 {
   502 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   503 
   504 	if (!pInfo)
   505 		return false;
   506 
   507 	CmdHook *pHook = new CmdHook(CmdHook::Server, pInfo, pFunction, description);
   508 
   509 	pInfo->hooks.append(pHook);
   510 	RegisterInPlugin(pHook);
   511 	return true;
   512 }
   513 
   514 void RegisterInPlugin(CmdHook *hook)
   515 {
   516 	PluginHookList *pList;
   517 
   518 	IPlugin *pPlugin = scripts->FindPluginByContext(hook->pf->GetParentContext());
   519 	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   520 	{
   521 		pList = new PluginHookList();
   522 		pPlugin->SetProperty("CommandList", pList);
   523 	}
   524 
   525 	const char *orig = hook->info->pCmd->GetName();
   526 
   527 	/* Insert this into the help list, SORTED alphabetically. */
   528 	PluginHookList::iterator iter = pList->begin();
   529 	while (iter != pList->end())
   530 	{
   531 		const char *cmd = (*iter)->info->pCmd->GetName();
   532 		if (strcmp(orig, cmd) < 0)
   533 		{
   534 			pList->insertBefore(iter, hook);
   535 			return;
   536 		}
   537 		iter++;
   538 	}
   539 
   540 	pList->append(hook);
   541 }
   542 
   543 void ConCmdManager::AddToCmdList(ConCmdInfo *info)
   544 {
   545 	List<ConCmdInfo *>::iterator iter = m_CmdList.begin();
   546 	ConCmdInfo *pInfo;
   547 	bool inserted = false;
   548 	const char *orig = NULL;
   549 
   550 	orig = info->pCmd->GetName();
   551 
   552 	/* Insert this into the help list, SORTED alphabetically. */
   553 	while (iter != m_CmdList.end())
   554 	{
   555 		const char *cmd = NULL;
   556 		pInfo = (*iter);
   557 		cmd = pInfo->pCmd->GetName();
   558 		if (strcmp(orig, cmd) < 0)
   559 		{
   560 			m_CmdList.insert(iter, info);
   561 			inserted = true;
   562 			break;
   563 		}
   564 		iter++;
   565 	}
   566 
   567 	if (!inserted)
   568 	{
   569 		m_CmdList.push_back(info);
   570 	}
   571 }
   572 
   573 void ConCmdManager::UpdateAdminCmdFlags(const char *cmd, OverrideType type, FlagBits bits, bool remove)
   574 {
   575 	if (type == Override_Command)
   576 	{
   577 		ConCmdInfo *pInfo;
   578 		if (!m_Cmds.retrieve(cmd, &pInfo))
   579 			return;
   580 
   581 		for (CmdHookList::iterator iter = pInfo->hooks.begin(); iter != pInfo->hooks.end(); iter++)
   582 		{
   583 			if (!iter->admin)
   584 				continue;
   585 
   586 			if (!remove)
   587 				iter->admin->eflags = bits;
   588 			else
   589 				iter->admin->eflags = iter->admin->flags;
   590 			pInfo->eflags = iter->admin->eflags;
   591 		}
   592 	}
   593 	else if (type == Override_CommandGroup)
   594 	{
   595 		GroupMap::Result r = m_CmdGrps.find(cmd);
   596 		if (!r.found())
   597 			return;
   598 
   599 		Ref<CommandGroup> group(r->value);
   600 
   601 		for (PluginHookList::iterator iter = group->hooks.begin(); iter != group->hooks.end(); iter++)
   602 		{
   603 			CmdHook *hook = *iter;
   604 			if (remove)
   605 				hook->admin->eflags = bits;
   606 			else
   607 				hook->admin->eflags = hook->admin->flags;
   608 			hook->info->eflags = hook->admin->eflags;
   609 		}
   610 	}
   611 }
   612 
   613 void ConCmdManager::RemoveConCmd(ConCmdInfo *info, const char *name, bool is_read_safe, bool untrack)
   614 {
   615 	/* Remove from the trie */
   616 	m_Cmds.remove(name);
   617 
   618 	/* Remove console-specific information
   619 	 * This should always be true as of right now
   620 	 */
   621 	if (info->pCmd)
   622 	{
   623 		if (info->sourceMod)
   624 		{
   625 			/* Unlink from SourceMM */
   626 			g_SMAPI->UnregisterConCommandBase(g_PLAPI, info->pCmd);
   627 			/* Delete the command's memory */
   628 			char *new_help = const_cast<char *>(info->pCmd->GetHelpText());
   629 			char *new_name = const_cast<char *>(info->pCmd->GetName());
   630 			delete [] new_help;
   631 			delete [] new_name;
   632 			delete info->pCmd;
   633 		}
   634 		else
   635 		{
   636 			if (is_read_safe)
   637 			{
   638 				/* Remove the external hook */
   639 				SH_REMOVE_HOOK(ConCommand, Dispatch, info->pCmd, SH_STATIC(CommandCallback), false);
   640 			}
   641 			if (untrack)
   642 			{
   643 				UntrackConCommandBase(info->pCmd, this);
   644 			}
   645 		}
   646 	}
   647 	
   648 	/* Remove from list */
   649 	m_CmdList.remove(info);
   650 
   651 	delete info;
   652 }
   653 
   654 bool ConCmdManager::LookForSourceModCommand(const char *cmd)
   655 {
   656 	ConCmdInfo *pInfo;
   657 	if (!m_Cmds.retrieve(cmd, &pInfo))
   658 		return false;
   659 
   660 	return pInfo->sourceMod && !pInfo->hooks.empty();
   661 }
   662 
   663 bool ConCmdManager::LookForCommandAdminFlags(const char *cmd, FlagBits *pFlags)
   664 {
   665 	ConCmdInfo *pInfo;
   666 	if (!m_Cmds.retrieve(cmd, &pInfo))
   667 		return false;
   668 
   669 	*pFlags = pInfo->eflags;
   670 	return true;
   671 }
   672 
   673 ConCmdInfo *ConCmdManager::AddOrFindCommand(const char *name, const char *description, int flags)
   674 {
   675 	ConCmdInfo *pInfo;
   676 	if (!m_Cmds.retrieve(name, &pInfo))
   677 	{
   678 		ConCmdList::iterator item = FindInList(name);
   679 		if (item != m_CmdList.end())
   680 			return *item;
   681 
   682 		pInfo = new ConCmdInfo();
   683 		/* Find the commandopan */
   684 		ConCommand *pCmd = FindCommand(name);
   685 
   686 		if (!pCmd)
   687 		{
   688 			/* Note that we have to duplicate because the source might not be 
   689 			 * a static string, and these expect static memory.
   690 			 */
   691 			if (!description)
   692 			{
   693 				description = "";
   694 			}
   695 			char *new_name = sm_strdup(name);
   696 			char *new_help = sm_strdup(description);
   697 			pCmd = new ConCommand(new_name, CommandCallback, new_help, flags);
   698 			pInfo->sourceMod = true;
   699 		}
   700 		else
   701 		{
   702 			TrackConCommandBase(pCmd, this);
   703 			SH_ADD_HOOK(ConCommand, Dispatch, pCmd, SH_STATIC(CommandCallback), false);
   704 		}
   705 
   706 		pInfo->pCmd = pCmd;
   707 
   708 		m_Cmds.insert(name, pInfo);
   709 		AddToCmdList(pInfo);
   710 	}
   711 
   712 	return pInfo;
   713 }
   714 
   715 void ConCmdManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
   716 {
   717 	if (command.ArgC() >= 3)
   718 	{
   719 		const char *text = command.Arg(2);
   720 
   721 		IPlugin *pPlugin = scripts->FindPluginByConsoleArg(text);
   722 
   723 		if (!pPlugin)
   724 		{
   725 			g_RootMenu.ConsolePrint("[SM] Plugin \"%s\" was not found.", text);
   726 			return;
   727 		}
   728 
   729 		const sm_plugininfo_t *plinfo = pPlugin->GetPublicInfo();
   730 		const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : pPlugin->GetFilename();
   731 
   732 		PluginHookList *pList;
   733 		if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   734 		{
   735 			g_RootMenu.ConsolePrint("[SM] No commands found for: %s", plname);
   736 			return;
   737 		}
   738 		if (pList->empty())
   739 		{
   740 			g_RootMenu.ConsolePrint("[SM] No commands found for: %s", plname);
   741 			return;
   742 		}
   743 
   744 		const char *type = NULL;
   745 		const char *name;
   746 		const char *help;
   747 		g_RootMenu.ConsolePrint("[SM] Listing commands for: %s", plname);
   748 		g_RootMenu.ConsolePrint("  %-17.16s %-8.7s %s", "[Name]", "[Type]", "[Help]");
   749 		for (PluginHookList::iterator iter = pList->begin(); iter != pList->end(); iter++)
   750 		{
   751 			CmdHook *hook = *iter;
   752 			if (hook->type == CmdHook::Server)
   753 				type = "server";
   754 			else 
   755 				type = hook->info->eflags == 0 ? "console" : "admin";
   756 
   757 			name = hook->info->pCmd->GetName();
   758 			if (hook->helptext.length())
   759 				help = hook->helptext.chars();
   760 			else
   761 				help = hook->info->pCmd->GetHelpText();
   762 			g_RootMenu.ConsolePrint("  %-17.16s %-12.11s %s", name, type, help);		
   763 		}
   764 
   765 		return;
   766 	}
   767 
   768 	g_RootMenu.ConsolePrint("[SM] Usage: sm cmds <plugin #>");
   769 }