core/CConCmdManager.cpp
author David Anderson <dvander@alliedmods.net>
Mon Feb 26 09:21:08 2007 +0000 (2007-02-26)
changeset 539 fa7e8a232a18
parent 525 d36c45088e36
child 546 3b5fe88ff68d
permissions -rw-r--r--
fixed stupid export
     1 /**
     2 * ===============================================================
     3 * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     4 * ===============================================================
     5 *
     6 * This file is not open source and may not be copied without explicit
     7 * written permission of AlliedModders LLC.  This file may not be redistributed 
     8 * in whole or significant part.
     9 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    10 *
    11 * Version: $Id$
    12 */
    13 
    14 #include "CConCmdManager.h"
    15 #include "sm_srvcmds.h"
    16 #include "AdminCache.h"
    17 #include "sm_stringutil.h"
    18 #include "CPlayerManager.h"
    19 #include "CTranslator.h"
    20 
    21 CConCmdManager g_ConCmds;
    22 
    23 SH_DECL_HOOK0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
    24 SH_DECL_HOOK1_void(IServerGameClients, SetCommandClient, SH_NOATTRIB, false, int);
    25 
    26 struct PlCmdInfo
    27 {
    28 	ConCmdInfo *pInfo;
    29 	CmdHook *pHook;
    30 	CmdType type;
    31 };
    32 typedef List<PlCmdInfo> CmdList;
    33 void AddToPlCmdList(CmdList *pList, const PlCmdInfo &info);
    34 
    35 CConCmdManager::CConCmdManager() : m_Strings(1024)
    36 {
    37 	m_pCmds = sm_trie_create();
    38 	m_pCmdGrps = sm_trie_create();
    39 }
    40 
    41 CConCmdManager::~CConCmdManager()
    42 {
    43 	sm_trie_destroy(m_pCmds);
    44 	sm_trie_destroy(m_pCmdGrps);
    45 }
    46 
    47 void CConCmdManager::OnSourceModAllInitialized()
    48 {
    49 	g_PluginSys.AddPluginsListener(this);
    50 	g_RootMenu.AddRootConsoleCommand("cmds", "List console commands", this);
    51 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, serverClients, this, &CConCmdManager::SetCommandClient, false);
    52 }
    53 
    54 void CConCmdManager::OnSourceModShutdown()
    55 {
    56 	/* All commands should already be removed by the time we're done */
    57 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, serverClients, this, &CConCmdManager::SetCommandClient, false);
    58 	g_RootMenu.RemoveRootConsoleCommand("cmds", this);
    59 	g_PluginSys.RemovePluginsListener(this);
    60 }
    61 
    62 void CConCmdManager::RemoveConCmds(List<CmdHook *> &cmdlist, IPluginContext *pContext)
    63 {
    64 	List<CmdHook *>::iterator iter = cmdlist.begin();
    65 	CmdHook *pHook;
    66 
    67 	while (iter != cmdlist.end())
    68 	{
    69 		pHook = (*iter);
    70 		if (pHook->pf->GetParentContext() == pContext)
    71 		{
    72 			delete pHook->pAdmin;
    73 			delete pHook;
    74 			iter = cmdlist.erase(iter);
    75 		} else {
    76 			iter++;
    77 		}
    78 	}
    79 }
    80 
    81 void CConCmdManager::OnPluginDestroyed(IPlugin *plugin)
    82 {
    83 	CmdList *pList;
    84 	List<ConCmdInfo *> removed;
    85 	if (plugin->GetProperty("CommandList", (void **)&pList, true))
    86 	{
    87 		IPluginContext *pContext = plugin->GetBaseContext();
    88 		CmdList::iterator iter;
    89 		/* First pass!
    90 	 	 * Only bother if there's an actual command list on this plugin... 
    91 	 	 */
    92 		for (iter=pList->begin();
    93 			iter!=pList->end();
    94 			iter++)
    95 		{
    96 			PlCmdInfo &cmd = (*iter);
    97 			ConCmdInfo *pInfo = cmd.pInfo;
    98 
    99 			/* Has this chain already been fully cleaned/removed? */
   100 			if (removed.find(pInfo) != removed.end())
   101 			{
   102 				continue;
   103 			}
   104 
   105 			/* Remove any hooks from us on this command */
   106 			RemoveConCmds(pInfo->conhooks, pContext);
   107 			RemoveConCmds(pInfo->srvhooks, pContext);
   108 
   109 			/* See if there are still hooks */
   110 			if (pInfo->srvhooks.size())
   111 			{
   112 				continue;
   113 			}
   114 			if (pInfo->conhooks.size())
   115 			{
   116 				continue;
   117 			}
   118 
   119 			/* Remove the command, it should be safe now */
   120 			RemoveConCmd(pInfo);
   121 			removed.push_back(pInfo);
   122 		}
   123 		delete pList;
   124 	}
   125 }
   126 
   127 void CommandCallback()
   128 {
   129 	g_ConCmds.InternalDispatch();
   130 }
   131 
   132 void CConCmdManager::SetCommandClient(int client)
   133 {
   134 	m_CmdClient = client + 1;
   135 }
   136 
   137 ResultType CConCmdManager::DispatchClientCommand(int client, ResultType type)
   138 {
   139 	const char *cmd = engine->Cmd_Argv(0);
   140 	int args = engine->Cmd_Argc() - 1;
   141 
   142 	ConCmdInfo *pInfo;
   143 	if (sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   144 	{
   145 		cell_t result = type;
   146 		cell_t tempres = result;
   147 		List<CmdHook *>::iterator iter;
   148 		CmdHook *pHook;
   149 		for (iter=pInfo->conhooks.begin();
   150 			 iter!=pInfo->conhooks.end();
   151 			 iter++)
   152 		{
   153 			pHook = (*iter);
   154 			if (!pHook->pf->IsRunnable())
   155 			{
   156 				continue;
   157 			}
   158 			if (pHook->pAdmin && !CheckAccess(client, cmd, pHook->pAdmin))
   159 			{
   160 				if (result < Pl_Handled)
   161 				{
   162 					result = Pl_Handled;
   163 				}
   164 				continue;
   165 			}
   166 			pHook->pf->PushCell(client);
   167 			pHook->pf->PushCell(args);
   168 			if (pHook->pf->Execute(&tempres) == SP_ERROR_NONE)
   169 			{
   170 				if (tempres > result)
   171 				{
   172 					result = tempres;
   173 				}
   174 				if (result == Pl_Stop)
   175 				{
   176 					break;
   177 				}
   178 			}
   179 		}
   180 		type = (ResultType)result;
   181 	}
   182 
   183 	return type;
   184 }
   185 
   186 void CConCmdManager::InternalDispatch()
   187 {
   188 	/**
   189 	 * Note: Console commands will EITHER go through IServerGameDLL::ClientCommand,
   190 	 * OR this dispatch.  They will NEVER go through both.
   191 	 * -- 
   192 	 * Whether or not it goes through the callback is determined by FCVAR_GAMEDLL
   193 	 */
   194 
   195 	const char *cmd = engine->Cmd_Argv(0);
   196 	ConCmdInfo *pInfo;
   197 	if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   198 	{
   199 		return;
   200 	}
   201 
   202 	cell_t result = Pl_Continue;
   203 	int args = engine->Cmd_Argc() - 1;
   204 
   205 	List<CmdHook *>::iterator iter;
   206 	CmdHook *pHook;
   207 
   208 	/* Execute server-only commands if viable */
   209 	if (m_CmdClient == 0 && pInfo->srvhooks.size())
   210 	{
   211 		cell_t tempres = result;
   212 		for (iter=pInfo->srvhooks.begin();
   213 			 iter!=pInfo->srvhooks.end();
   214 			 iter++)
   215 		{
   216 			pHook = (*iter);
   217 			if (!pHook->pf->IsRunnable())
   218 			{
   219 				continue;
   220 			}
   221 			pHook->pf->PushCell(args);
   222 			if (pHook->pf->Execute(&tempres) == SP_ERROR_NONE)
   223 			{
   224 				if (tempres > result)
   225 				{
   226 					result = tempres;
   227 				}
   228 				if (result == Pl_Stop)
   229 				{
   230 					break;
   231 				}
   232 			}
   233 		}
   234 	
   235 		/* Check if there's an early stop */
   236 		if (result >= Pl_Stop)
   237 		{
   238 			if (!pInfo->sourceMod)
   239 			{
   240 				RETURN_META(MRES_SUPERCEDE);
   241 			}
   242 			return;
   243 		}
   244 	}
   245 
   246 	/* Execute console commands */
   247 	if (pInfo->conhooks.size())
   248 	{
   249 		cell_t tempres = result;
   250 		for (iter=pInfo->conhooks.begin();
   251 			iter!=pInfo->conhooks.end();
   252 			iter++)
   253 		{
   254 			pHook = (*iter);
   255 			if (!pHook->pf->IsRunnable())
   256 			{
   257 				continue;
   258 			}
   259 			if (m_CmdClient 
   260 				&& pHook->pAdmin
   261 				&& !CheckAccess(m_CmdClient, cmd, pHook->pAdmin))
   262 			{
   263 				if (result < Pl_Handled)
   264 				{
   265 					result = Pl_Handled;
   266 				}
   267 				continue;
   268 			}
   269 			pHook->pf->PushCell(m_CmdClient);
   270 			pHook->pf->PushCell(args);
   271 			if (pHook->pf->Execute(&tempres) != SP_ERROR_NONE)
   272 			{
   273 				if (tempres > result)
   274 				{
   275 					result = tempres;
   276 				}
   277 				if (result == Pl_Stop)
   278 				{
   279 					break;
   280 				}
   281 			}
   282 		}
   283 	}
   284 
   285 	if (result >= Pl_Handled)
   286 	{
   287 		if (!pInfo->sourceMod)
   288 		{
   289 			RETURN_META(MRES_SUPERCEDE);
   290 		}
   291 		return;
   292 	}
   293 }
   294 
   295 bool CConCmdManager::CheckAccess(int client, const char *cmd, AdminCmdInfo *pAdmin)
   296 {
   297 	FlagBits cmdflags = pAdmin->eflags;
   298 	if (cmdflags == 0)
   299 	{
   300 		return true;
   301 	}
   302 
   303 	CPlayer *player = g_Players.GetPlayerByIndex(client);
   304 	if (!player)
   305 	{
   306 		return false;
   307 	}
   308 
   309 	AdminId adm = player->GetAdminId();
   310 	if (adm != INVALID_ADMIN_ID)
   311 	{
   312 		FlagBits bits = g_Admins.GetAdminFlags(adm, Access_Effective);
   313 
   314 		/* root knows all, WHOA */
   315 		if ((bits & ADMFLAG_ROOT) == ADMFLAG_ROOT)
   316 		{
   317 			return true;
   318 		}
   319 
   320 		/* See if our other flags match */
   321 		if ((bits & cmdflags) == cmdflags)
   322 		{
   323 			return true;
   324 		}
   325 
   326 		/* Check for overrides */
   327 		unsigned int groups = g_Admins.GetAdminGroupCount(adm);
   328 		GroupId gid;
   329 		OverrideRule rule;
   330 		bool override = false;
   331 		for (unsigned int i=0; i<groups; i++)
   332 		{
   333 			gid = g_Admins.GetAdminGroup(adm, i, NULL);
   334 			/* First get group-level override */
   335 			override = g_Admins.GetGroupCommandOverride(gid, cmd, Override_CommandGroup, &rule);
   336 			/* Now get the specific command override */
   337 			if (g_Admins.GetGroupCommandOverride(gid, cmd, Override_Command, &rule))
   338 			{
   339 				override = true;
   340 			}
   341 			if (override)
   342 			{
   343 				if (rule == Command_Allow)
   344 				{
   345 					return true;
   346 				} else if (rule == Command_Deny) {
   347 					break;
   348 				}
   349 			}
   350 		}
   351 	}
   352 
   353 	if (player->IsFakeClient()
   354 		|| player->GetEdict() == NULL)
   355 	{
   356 		return false;
   357 	}
   358 	
   359 	/* If we got here, the command failed... */
   360 	char buffer[128];
   361 	if (g_Translator.CoreTrans(client, buffer, sizeof(buffer), "No Access", NULL, NULL)
   362 		!= Trans_Okay)
   363 	{
   364 		snprintf(buffer, sizeof(buffer), "You do not have access to this command");
   365 	}
   366 
   367 	char fullbuffer[192];
   368 	snprintf(fullbuffer, sizeof(fullbuffer), "[SM] %s.\n", buffer);
   369 
   370 	engine->ClientPrintf(player->GetEdict(), fullbuffer);
   371 	
   372 	return false;
   373 }
   374 
   375 void CConCmdManager::AddConsoleCommand(IPluginFunction *pFunction, 
   376 									   const char *name, 
   377 									   const char *description, 
   378 									   int flags)
   379 {
   380 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   381 	CmdHook *pHook = new CmdHook();
   382 
   383 	pHook->pf = pFunction;
   384 	if (description && description[0])
   385 	{
   386 		pHook->helptext.assign(description);
   387 	}
   388 	pInfo->conhooks.push_back(pHook);
   389 
   390 	/* Add to the plugin */
   391 	CmdList *pList;
   392 	IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
   393 	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   394 	{
   395 		pList = new CmdList();
   396 		pPlugin->SetProperty("CommandList", pList);
   397 	}
   398 	PlCmdInfo info;
   399 	info.pInfo = pInfo;
   400 	info.type = Cmd_Console;
   401 	info.pHook = pHook;
   402 	AddToPlCmdList(pList, info);
   403 }
   404 
   405 bool CConCmdManager::AddAdminCommand(IPluginFunction *pFunction, 
   406 									 const char *name, 
   407 									 const char *group,
   408 									 int adminflags,
   409 									 const char *description, 
   410 									 int flags)
   411 {
   412 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   413 
   414 	CmdHook *pHook = new CmdHook();
   415 	AdminCmdInfo *pAdmin = new AdminCmdInfo();
   416 
   417 	pHook->pf = pFunction;
   418 	if (description && description[0])
   419 	{
   420 		pHook->helptext.assign(description);
   421 	}
   422 	pHook->pAdmin = pAdmin;
   423 
   424 	void *object;
   425 	int grpid;
   426 	if (!sm_trie_retrieve(m_pCmdGrps, group, (void **)&object))
   427 	{
   428 		grpid = m_Strings.AddString(group);
   429 		sm_trie_insert(m_pCmdGrps, group, (void *)grpid);
   430 	} else {
   431 		grpid = (int)object;
   432 	}
   433 
   434 	pAdmin->cmdGrpId = grpid;
   435 	pAdmin->flags = adminflags;
   436 
   437 	/* First get the command group override, if any */
   438 	bool override = g_Admins.GetCommandOverride(group, 
   439 		Override_CommandGroup,
   440 		&(pAdmin->eflags));
   441 
   442 	/* Next get the command override, if any */
   443 	if (g_Admins.GetCommandOverride(name,
   444 		Override_Command,
   445 		&(pAdmin->eflags)))
   446 	{
   447 		override = true;
   448 	}
   449 
   450 	/* Assign normal flags if there were no overrides */
   451 	if (!override)
   452 	{
   453 		pAdmin->eflags = pAdmin->flags;
   454 	}
   455 
   456 	/* Finally, add the hook */
   457 	pInfo->conhooks.push_back(pHook);
   458 
   459 	/* Now add to the plugin */
   460 	CmdList *pList;
   461 	IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
   462 	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   463 	{
   464 		pList = new CmdList();
   465 		pPlugin->SetProperty("CommandList", pList);
   466 	}
   467 	PlCmdInfo info;
   468 	info.pInfo = pInfo;
   469 	info.type = Cmd_Admin;
   470 	info.pHook = pHook;
   471 	AddToPlCmdList(pList, info);
   472 
   473 	return true;
   474 }
   475 
   476 void CConCmdManager::AddServerCommand(IPluginFunction *pFunction, 
   477 									  const char *name, 
   478 									  const char *description, 
   479 									  int flags)
   480 
   481 {
   482 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   483 	CmdHook *pHook = new CmdHook();
   484 
   485 	pHook->pf = pFunction;
   486 	if (description && description[0])
   487 	{
   488 		pHook->helptext.assign(description);
   489 	}
   490 
   491 	pInfo->srvhooks.push_back(pHook);
   492 
   493 	/* Add to the plugin */
   494 	CmdList *pList;
   495 	IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
   496 	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   497 	{
   498 		pList = new CmdList();
   499 		pPlugin->SetProperty("CommandList", pList);
   500 	}
   501 	PlCmdInfo info;
   502 	info.pInfo = pInfo;
   503 	info.type = Cmd_Server;
   504 	info.pHook = pHook;
   505 	AddToPlCmdList(pList, info);
   506 }
   507 
   508 void AddToPlCmdList(CmdList *pList, const PlCmdInfo &info)
   509 {
   510 	CmdList::iterator iter = pList->begin();
   511 	bool inserted = false;
   512 	const char *orig = NULL;
   513 
   514 	orig = info.pInfo->pCmd->GetName();
   515 
   516 	/* Insert this into the help list, SORTED alphabetically. */
   517 	while (iter != pList->end())
   518 	{
   519 		PlCmdInfo &obj = (*iter);
   520 		const char *cmd = obj.pInfo->pCmd->GetName();
   521 		if (strcmp(orig, cmd) < 0)
   522 		{
   523 			pList->insert(iter, info);
   524 			inserted = true;
   525 			break;
   526 		}
   527 		iter++;
   528 	}
   529 
   530 	if (!inserted)
   531 	{
   532 		pList->push_back(info);
   533 	}
   534 }
   535 
   536 void CConCmdManager::AddToCmdList(ConCmdInfo *info)
   537 {
   538 	List<ConCmdInfo *>::iterator iter = m_CmdList.begin();
   539 	ConCmdInfo *pInfo;
   540 	bool inserted = false;
   541 	const char *orig = NULL;
   542 
   543 	orig = info->pCmd->GetName();
   544 
   545 	/* Insert this into the help list, SORTED alphabetically. */
   546 	while (iter != m_CmdList.end())
   547 	{
   548 		const char *cmd = NULL;
   549 		pInfo = (*iter);
   550 		cmd = pInfo->pCmd->GetName();
   551 		if (strcmp(orig, cmd) < 0)
   552 		{
   553 			m_CmdList.insert(iter, info);
   554 			inserted = true;
   555 			break;
   556 		}
   557 		iter++;
   558 	}
   559 
   560 	if (!inserted)
   561 	{
   562 		m_CmdList.push_back(info);
   563 	}
   564 }
   565 
   566 void CConCmdManager::UpdateAdminCmdFlags(const char *cmd, OverrideType type, FlagBits bits)
   567 {
   568 	ConCmdInfo *pInfo;
   569 
   570 	if (type == Override_Command)
   571 	{
   572 		if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   573 		{
   574 			return;
   575 		}
   576 
   577 		List<CmdHook *>::iterator iter;
   578 		CmdHook *pHook;
   579 
   580 		for (iter=pInfo->conhooks.begin(); iter!=pInfo->conhooks.end(); iter++)
   581 		{
   582 			pHook = (*iter);
   583 			if (pHook->pAdmin)
   584 			{
   585 				if (bits)
   586 				{
   587 					pHook->pAdmin->eflags = bits;
   588 				} else {
   589 					pHook->pAdmin->eflags = pHook->pAdmin->flags;
   590 				}
   591 			}
   592 		}
   593 	} else if (type == Override_CommandGroup) {
   594 		void *object;
   595 		if (!sm_trie_retrieve(m_pCmdGrps, cmd, &object))
   596 		{
   597 			return;
   598 		}
   599 		int grpid = (int)object;
   600 
   601 		/* This is bad :( loop through all commands */
   602 		List<ConCmdInfo *>::iterator iter;
   603 		CmdHook *pHook;
   604 		for (iter=m_CmdList.begin(); iter!=m_CmdList.end(); iter++)
   605 		{
   606 			pInfo = (*iter);
   607 			for (List<CmdHook *>::iterator citer=pInfo->conhooks.begin();
   608 				 citer!=pInfo->conhooks.end();
   609 				 citer++)
   610 			{
   611 				pHook = (*citer);
   612 				if (pHook->pAdmin && pHook->pAdmin->cmdGrpId == grpid)
   613 				{
   614 					if (bits)
   615 					{
   616 						pHook->pAdmin->eflags = bits;
   617 					} else {
   618 						pHook->pAdmin->eflags = pHook->pAdmin->flags;
   619 					}
   620 				}
   621 			}
   622 		}
   623 	}
   624 }
   625 
   626 void CConCmdManager::RemoveConCmd(ConCmdInfo *info)
   627 {
   628 	/* Remove console-specific information
   629 	 * This should always be true as of right now
   630 	 */
   631 	if (info->pCmd)
   632 	{
   633 		/* Remove from the trie */
   634 		sm_trie_delete(m_pCmds, info->pCmd->GetName());
   635 
   636 		if (info->sourceMod)
   637 		{
   638 			/* Unlink from SourceMM */
   639 			g_SMAPI->UnregisterConCmdBase(g_PLAPI, info->pCmd);
   640 			/* Delete the command's memory */
   641 			char *new_help = const_cast<char *>(info->pCmd->GetHelpText());
   642 			char *new_name = const_cast<char *>(info->pCmd->GetName());
   643 			delete [] new_help;
   644 			delete [] new_name;
   645 			delete info->pCmd;
   646 		} else {
   647 			/* Remove the external hook */
   648 			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, info->pCmd, CommandCallback, false);
   649 		}
   650 	}
   651 	
   652 	/* Remove from list */
   653 	m_CmdList.remove(info);
   654 
   655 	delete info;
   656 }
   657 
   658 ConCmdInfo *CConCmdManager::AddOrFindCommand(const char *name, const char *description, int flags)
   659 {
   660 	ConCmdInfo *pInfo;
   661 	if (!sm_trie_retrieve(m_pCmds, name, (void **)&pInfo))
   662 	{
   663 		pInfo = new ConCmdInfo();
   664 		/* Find the commandopan */
   665 		ConCommandBase *pBase = icvar->GetCommands();
   666 		ConCommand *pCmd = NULL;
   667 		while (pBase)
   668 		{
   669 			if (pBase->IsCommand()
   670 				&& (strcmp(pBase->GetName(), name) == 0))
   671 			{
   672 				pCmd = (ConCommand *)pBase;
   673 				break;
   674 			}
   675 			pBase = const_cast<ConCommandBase *>(pBase->GetNext());
   676 		}
   677 
   678 		if (!pCmd)
   679 		{
   680 			/* Note that we have to duplicate because the source might not be 
   681 			 * a static string, and these expect static memory.
   682 			 */
   683 			if (!description)
   684 			{
   685 				description = "";
   686 			}
   687 			char *new_name = sm_strdup(name);
   688 			char *new_help = sm_strdup(description);
   689 			pCmd = new ConCommand(new_name, CommandCallback, new_help, flags);
   690 			pInfo->sourceMod = true;
   691 		} else {
   692 			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, pCmd, CommandCallback, false);
   693 		}
   694 
   695 		pInfo->pCmd = pCmd;
   696 
   697 		sm_trie_insert(m_pCmds, name, pInfo);
   698 		AddToCmdList(pInfo);
   699 	}
   700 
   701 	return pInfo;
   702 }
   703 
   704 void CConCmdManager::OnRootConsoleCommand(const char *command, unsigned int argcount)
   705 {
   706 	if (argcount >= 3)
   707 	{
   708 		const char *text = engine->Cmd_Argv(2);
   709 		int id = atoi(text);
   710 		CPlugin *pPlugin = g_PluginSys.GetPluginByOrder(id);
   711 
   712 		if (!pPlugin)
   713 		{
   714 			g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", id);
   715 			return;
   716 		}
   717 
   718 		CmdList *pList;
   719 		if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   720 		{
   721 			g_RootMenu.ConsolePrint("[SM] No commands found for %s", pPlugin->GetFilename());
   722 			return;
   723 		}
   724 		if (!pList->size())
   725 		{
   726 			g_RootMenu.ConsolePrint("[SM] No commands found for %s", pPlugin->GetFilename());
   727 			return;
   728 		}
   729 
   730 		CmdList::iterator iter;
   731 		const char *type = NULL;
   732 		const char *name;
   733 		const char *help;
   734 		g_RootMenu.ConsolePrint(" %-17.16s %-8.7s %s", "[Name]", "[Type]", "[Help]");
   735 		for (iter=pList->begin();
   736 			 iter!=pList->end();
   737 			 iter++)
   738 		{
   739 			PlCmdInfo &cmd = (*iter);
   740 			if (cmd.type == Cmd_Server)
   741 			{
   742 				type = "server";
   743 			} else if (cmd.type == Cmd_Console) {
   744 				type = "console";
   745 			} else if (cmd.type == Cmd_Admin) {
   746 				type = "admin";
   747 			}
   748 			name = cmd.pInfo->pCmd->GetName();
   749 			if (cmd.pHook->helptext.size())
   750 			{
   751 				help = cmd.pHook->helptext.c_str();
   752 			} else {
   753 				help = cmd.pInfo->pCmd->GetHelpText();
   754 			}
   755 			g_RootMenu.ConsolePrint(" %-17.16s %-12.11s %s", name, type, help);		
   756 		}
   757 
   758 		return;
   759 	}
   760 
   761 	g_RootMenu.ConsolePrint("[SM] Usage: sm cmds <plugin #>");
   762 }