core/CConCmdManager.cpp
author David Anderson <dvander@alliedmods.net>
Sat Feb 17 19:03:18 2007 +0000 (2007-02-17)
changeset 520 7676d61cde2d
parent 514 4c02b38c26ed
child 525 d36c45088e36
permissions -rw-r--r--
added helper a core translation helper to the translator
regadmincmd is now officially done and tested, as are overrides
printtoplayers now prints to the server on id==0 as a convenience
fixed a crash bug in the admin system
fixed up console.inc a bit
     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->pAdmin && !CheckAccess(client, cmd, pHook->pAdmin))
   155 			{
   156 				if (result < Pl_Handled)
   157 				{
   158 					result = Pl_Handled;
   159 				}
   160 				continue;
   161 			}
   162 			pHook->pf->PushCell(client);
   163 			pHook->pf->PushCell(args);
   164 			if (pHook->pf->Execute(&tempres) == SP_ERROR_NONE)
   165 			{
   166 				if (tempres > result)
   167 				{
   168 					result = tempres;
   169 				}
   170 				if (result == Pl_Stop)
   171 				{
   172 					break;
   173 				}
   174 			}
   175 		}
   176 		type = (ResultType)result;
   177 	}
   178 
   179 	return type;
   180 }
   181 
   182 void CConCmdManager::InternalDispatch()
   183 {
   184 	/**
   185 	 * Note: Console commands will EITHER go through IServerGameDLL::ClientCommand,
   186 	 * OR this dispatch.  They will NEVER go through both.
   187 	 * -- 
   188 	 * Whether or not it goes through the callback is determined by FCVAR_GAMEDLL
   189 	 */
   190 
   191 	const char *cmd = engine->Cmd_Argv(0);
   192 	ConCmdInfo *pInfo;
   193 	if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   194 	{
   195 		return;
   196 	}
   197 
   198 	cell_t result = Pl_Continue;
   199 	int args = engine->Cmd_Argc() - 1;
   200 
   201 	List<CmdHook *>::iterator iter;
   202 	CmdHook *pHook;
   203 
   204 	/* Execute server-only commands if viable */
   205 	if (m_CmdClient == 0 && pInfo->srvhooks.size())
   206 	{
   207 		cell_t tempres = result;
   208 		for (iter=pInfo->srvhooks.begin();
   209 			 iter!=pInfo->srvhooks.end();
   210 			 iter++)
   211 		{
   212 			pHook = (*iter);
   213 			pHook->pf->PushCell(args);
   214 			if (pHook->pf->Execute(&tempres) == SP_ERROR_NONE)
   215 			{
   216 				if (tempres > result)
   217 				{
   218 					result = tempres;
   219 				}
   220 				if (result == Pl_Stop)
   221 				{
   222 					break;
   223 				}
   224 			}
   225 		}
   226 	
   227 		/* Check if there's an early stop */
   228 		if (result >= Pl_Stop)
   229 		{
   230 			if (!pInfo->sourceMod)
   231 			{
   232 				RETURN_META(MRES_SUPERCEDE);
   233 			}
   234 			return;
   235 		}
   236 	}
   237 
   238 	/* Execute console commands */
   239 	if (pInfo->conhooks.size())
   240 	{
   241 		cell_t tempres = result;
   242 		for (iter=pInfo->conhooks.begin();
   243 			iter!=pInfo->conhooks.end();
   244 			iter++)
   245 		{
   246 			pHook = (*iter);
   247 			if (m_CmdClient 
   248 				&& pHook->pAdmin
   249 				&& !CheckAccess(m_CmdClient, cmd, pHook->pAdmin))
   250 			{
   251 				if (result < Pl_Handled)
   252 				{
   253 					result = Pl_Handled;
   254 				}
   255 				continue;
   256 			}
   257 			pHook->pf->PushCell(m_CmdClient);
   258 			pHook->pf->PushCell(args);
   259 			if (pHook->pf->Execute(&tempres) != SP_ERROR_NONE)
   260 			{
   261 				if (tempres > result)
   262 				{
   263 					result = tempres;
   264 				}
   265 				if (result == Pl_Stop)
   266 				{
   267 					break;
   268 				}
   269 			}
   270 		}
   271 	}
   272 
   273 	if (result >= Pl_Handled)
   274 	{
   275 		if (!pInfo->sourceMod)
   276 		{
   277 			RETURN_META(MRES_SUPERCEDE);
   278 		}
   279 		return;
   280 	}
   281 }
   282 
   283 bool CConCmdManager::CheckAccess(int client, const char *cmd, AdminCmdInfo *pAdmin)
   284 {
   285 	FlagBits cmdflags = pAdmin->eflags;
   286 	if (cmdflags == 0)
   287 	{
   288 		return true;
   289 	}
   290 
   291 	CPlayer *player = g_Players.GetPlayerByIndex(client);
   292 	if (!player)
   293 	{
   294 		return false;
   295 	}
   296 
   297 	AdminId adm = player->GetAdminId();
   298 	if (adm != INVALID_ADMIN_ID)
   299 	{
   300 		FlagBits bits = g_Admins.GetAdminFlags(adm, Access_Effective);
   301 
   302 		/* root knows all, WHOA */
   303 		if ((bits & ADMFLAG_ROOT) == ADMFLAG_ROOT)
   304 		{
   305 			return true;
   306 		}
   307 
   308 		/* See if our other flags match */
   309 		if ((bits & cmdflags) == cmdflags)
   310 		{
   311 			return true;
   312 		}
   313 
   314 		/* Check for overrides */
   315 		unsigned int groups = g_Admins.GetAdminGroupCount(adm);
   316 		GroupId gid;
   317 		OverrideRule rule;
   318 		bool override = false;
   319 		for (unsigned int i=0; i<groups; i++)
   320 		{
   321 			gid = g_Admins.GetAdminGroup(adm, i, NULL);
   322 			/* First get group-level override */
   323 			override = g_Admins.GetGroupCommandOverride(gid, cmd, Override_CommandGroup, &rule);
   324 			/* Now get the specific command override */
   325 			if (g_Admins.GetGroupCommandOverride(gid, cmd, Override_Command, &rule))
   326 			{
   327 				override = true;
   328 			}
   329 			if (override)
   330 			{
   331 				if (rule == Command_Allow)
   332 				{
   333 					return true;
   334 				} else if (rule == Command_Deny) {
   335 					break;
   336 				}
   337 			}
   338 		}
   339 	}
   340 
   341 	if (player->IsFakeClient()
   342 		|| player->GetEdict() == NULL)
   343 	{
   344 		return false;
   345 	}
   346 	
   347 	/* If we got here, the command failed... */
   348 	char buffer[128];
   349 	if (g_Translator.CoreTrans(client, buffer, sizeof(buffer), "No Access", NULL, NULL)
   350 		!= Trans_Okay)
   351 	{
   352 		snprintf(buffer, sizeof(buffer), "You do not have access to this command");
   353 	}
   354 
   355 	char fullbuffer[192];
   356 	snprintf(fullbuffer, sizeof(fullbuffer), "[SM] %s.\n", buffer);
   357 
   358 	engine->ClientPrintf(player->GetEdict(), fullbuffer);
   359 	
   360 	return false;
   361 }
   362 
   363 void CConCmdManager::AddConsoleCommand(IPluginFunction *pFunction, 
   364 									   const char *name, 
   365 									   const char *description, 
   366 									   int flags)
   367 {
   368 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   369 	CmdHook *pHook = new CmdHook();
   370 
   371 	pHook->pf = pFunction;
   372 	if (description && description[0])
   373 	{
   374 		pHook->helptext.assign(description);
   375 	}
   376 	pInfo->conhooks.push_back(pHook);
   377 
   378 	/* Add to the plugin */
   379 	CmdList *pList;
   380 	IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
   381 	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   382 	{
   383 		pList = new CmdList();
   384 		pPlugin->SetProperty("CommandList", pList);
   385 	}
   386 	PlCmdInfo info;
   387 	info.pInfo = pInfo;
   388 	info.type = Cmd_Console;
   389 	info.pHook = pHook;
   390 	AddToPlCmdList(pList, info);
   391 }
   392 
   393 bool CConCmdManager::AddAdminCommand(IPluginFunction *pFunction, 
   394 									 const char *name, 
   395 									 const char *group,
   396 									 int adminflags,
   397 									 const char *description, 
   398 									 int flags)
   399 {
   400 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   401 
   402 	CmdHook *pHook = new CmdHook();
   403 	AdminCmdInfo *pAdmin = new AdminCmdInfo();
   404 
   405 	pHook->pf = pFunction;
   406 	if (description && description[0])
   407 	{
   408 		pHook->helptext.assign(description);
   409 	}
   410 	pHook->pAdmin = pAdmin;
   411 
   412 	void *object;
   413 	int grpid;
   414 	if (!sm_trie_retrieve(m_pCmdGrps, group, (void **)&object))
   415 	{
   416 		grpid = m_Strings.AddString(group);
   417 		sm_trie_insert(m_pCmdGrps, group, (void *)grpid);
   418 	} else {
   419 		grpid = (int)object;
   420 	}
   421 
   422 	pAdmin->cmdGrpId = grpid;
   423 	pAdmin->flags = adminflags;
   424 
   425 	/* First get the command group override, if any */
   426 	bool override = g_Admins.GetCommandOverride(group, 
   427 		Override_CommandGroup,
   428 		&(pAdmin->eflags));
   429 
   430 	/* Next get the command override, if any */
   431 	if (g_Admins.GetCommandOverride(name,
   432 		Override_Command,
   433 		&(pAdmin->eflags)))
   434 	{
   435 		override = true;
   436 	}
   437 
   438 	/* Assign normal flags if there were no overrides */
   439 	if (!override)
   440 	{
   441 		pAdmin->eflags = pAdmin->flags;
   442 	}
   443 
   444 	/* Finally, add the hook */
   445 	pInfo->conhooks.push_back(pHook);
   446 
   447 	/* Now add to the plugin */
   448 	CmdList *pList;
   449 	IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
   450 	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   451 	{
   452 		pList = new CmdList();
   453 		pPlugin->SetProperty("CommandList", pList);
   454 	}
   455 	PlCmdInfo info;
   456 	info.pInfo = pInfo;
   457 	info.type = Cmd_Admin;
   458 	info.pHook = pHook;
   459 	AddToPlCmdList(pList, info);
   460 
   461 	return true;
   462 }
   463 
   464 void CConCmdManager::AddServerCommand(IPluginFunction *pFunction, 
   465 									  const char *name, 
   466 									  const char *description, 
   467 									  int flags)
   468 
   469 {
   470 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   471 	CmdHook *pHook = new CmdHook();
   472 
   473 	pHook->pf = pFunction;
   474 	if (description && description[0])
   475 	{
   476 		pHook->helptext.assign(description);
   477 	}
   478 
   479 	pInfo->srvhooks.push_back(pHook);
   480 
   481 	/* Add to the plugin */
   482 	CmdList *pList;
   483 	IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
   484 	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   485 	{
   486 		pList = new CmdList();
   487 		pPlugin->SetProperty("CommandList", pList);
   488 	}
   489 	PlCmdInfo info;
   490 	info.pInfo = pInfo;
   491 	info.type = Cmd_Server;
   492 	info.pHook = pHook;
   493 	AddToPlCmdList(pList, info);
   494 }
   495 
   496 void AddToPlCmdList(CmdList *pList, const PlCmdInfo &info)
   497 {
   498 	CmdList::iterator iter = pList->begin();
   499 	bool inserted = false;
   500 	const char *orig = NULL;
   501 
   502 	orig = info.pInfo->pCmd->GetName();
   503 
   504 	/* Insert this into the help list, SORTED alphabetically. */
   505 	while (iter != pList->end())
   506 	{
   507 		PlCmdInfo &obj = (*iter);
   508 		const char *cmd = obj.pInfo->pCmd->GetName();
   509 		if (strcmp(orig, cmd) < 0)
   510 		{
   511 			pList->insert(iter, info);
   512 			inserted = true;
   513 			break;
   514 		}
   515 		iter++;
   516 	}
   517 
   518 	if (!inserted)
   519 	{
   520 		pList->push_back(info);
   521 	}
   522 }
   523 
   524 void CConCmdManager::AddToCmdList(ConCmdInfo *info)
   525 {
   526 	List<ConCmdInfo *>::iterator iter = m_CmdList.begin();
   527 	ConCmdInfo *pInfo;
   528 	bool inserted = false;
   529 	const char *orig = NULL;
   530 
   531 	orig = info->pCmd->GetName();
   532 
   533 	/* Insert this into the help list, SORTED alphabetically. */
   534 	while (iter != m_CmdList.end())
   535 	{
   536 		const char *cmd = NULL;
   537 		pInfo = (*iter);
   538 		cmd = pInfo->pCmd->GetName();
   539 		if (strcmp(orig, cmd) < 0)
   540 		{
   541 			m_CmdList.insert(iter, info);
   542 			inserted = true;
   543 			break;
   544 		}
   545 		iter++;
   546 	}
   547 
   548 	if (!inserted)
   549 	{
   550 		m_CmdList.push_back(info);
   551 	}
   552 }
   553 
   554 void CConCmdManager::UpdateAdminCmdFlags(const char *cmd, OverrideType type, FlagBits bits)
   555 {
   556 	ConCmdInfo *pInfo;
   557 
   558 	if (type == Override_Command)
   559 	{
   560 		if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   561 		{
   562 			return;
   563 		}
   564 
   565 		List<CmdHook *>::iterator iter;
   566 		CmdHook *pHook;
   567 
   568 		for (iter=pInfo->conhooks.begin(); iter!=pInfo->conhooks.end(); iter++)
   569 		{
   570 			pHook = (*iter);
   571 			if (pHook->pAdmin)
   572 			{
   573 				if (bits)
   574 				{
   575 					pHook->pAdmin->eflags = bits;
   576 				} else {
   577 					pHook->pAdmin->eflags = pHook->pAdmin->flags;
   578 				}
   579 			}
   580 		}
   581 	} else if (type == Override_CommandGroup) {
   582 		void *object;
   583 		if (!sm_trie_retrieve(m_pCmdGrps, cmd, &object))
   584 		{
   585 			return;
   586 		}
   587 		int grpid = (int)object;
   588 
   589 		/* This is bad :( loop through all commands */
   590 		List<ConCmdInfo *>::iterator iter;
   591 		CmdHook *pHook;
   592 		for (iter=m_CmdList.begin(); iter!=m_CmdList.end(); iter++)
   593 		{
   594 			pInfo = (*iter);
   595 			for (List<CmdHook *>::iterator citer=pInfo->conhooks.begin();
   596 				 citer!=pInfo->conhooks.end();
   597 				 citer++)
   598 			{
   599 				pHook = (*citer);
   600 				if (pHook->pAdmin && pHook->pAdmin->cmdGrpId == grpid)
   601 				{
   602 					if (bits)
   603 					{
   604 						pHook->pAdmin->eflags = bits;
   605 					} else {
   606 						pHook->pAdmin->eflags = pHook->pAdmin->flags;
   607 					}
   608 				}
   609 			}
   610 		}
   611 	}
   612 }
   613 
   614 void CConCmdManager::RemoveConCmd(ConCmdInfo *info)
   615 {
   616 	/* Remove console-specific information
   617 	 * This should always be true as of right now
   618 	 */
   619 	if (info->pCmd)
   620 	{
   621 		/* Remove from the trie */
   622 		sm_trie_delete(m_pCmds, info->pCmd->GetName());
   623 
   624 		if (info->sourceMod)
   625 		{
   626 			/* Unlink from SourceMM */
   627 			g_SMAPI->UnregisterConCmdBase(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 		} else {
   635 			/* Remove the external hook */
   636 			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, info->pCmd, CommandCallback, false);
   637 		}
   638 	}
   639 	
   640 	/* Remove from list */
   641 	m_CmdList.remove(info);
   642 
   643 	delete info;
   644 }
   645 
   646 ConCmdInfo *CConCmdManager::AddOrFindCommand(const char *name, const char *description, int flags)
   647 {
   648 	ConCmdInfo *pInfo;
   649 	if (!sm_trie_retrieve(m_pCmds, name, (void **)&pInfo))
   650 	{
   651 		pInfo = new ConCmdInfo();
   652 		/* Find the commandopan */
   653 		ConCommandBase *pBase = icvar->GetCommands();
   654 		ConCommand *pCmd = NULL;
   655 		while (pBase)
   656 		{
   657 			if (pBase->IsCommand()
   658 				&& (strcmp(pBase->GetName(), name) == 0))
   659 			{
   660 				pCmd = (ConCommand *)pBase;
   661 				break;
   662 			}
   663 			pBase = const_cast<ConCommandBase *>(pBase->GetNext());
   664 		}
   665 
   666 		if (!pCmd)
   667 		{
   668 			/* Note that we have to duplicate because the source might not be 
   669 			 * a static string, and these expect static memory.
   670 			 */
   671 			if (!description)
   672 			{
   673 				description = "";
   674 			}
   675 			char *new_name = sm_strdup(name);
   676 			char *new_help = sm_strdup(description);
   677 			pCmd = new ConCommand(new_name, CommandCallback, new_help, flags);
   678 			pInfo->sourceMod = true;
   679 		} else {
   680 			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, pCmd, CommandCallback, false);
   681 		}
   682 
   683 		pInfo->pCmd = pCmd;
   684 
   685 		sm_trie_insert(m_pCmds, name, pInfo);
   686 		AddToCmdList(pInfo);
   687 	}
   688 
   689 	return pInfo;
   690 }
   691 
   692 void CConCmdManager::OnRootConsoleCommand(const char *command, unsigned int argcount)
   693 {
   694 	if (argcount >= 3)
   695 	{
   696 		const char *text = engine->Cmd_Argv(2);
   697 		int id = atoi(text);
   698 		CPlugin *pPlugin = g_PluginSys.GetPluginByOrder(id);
   699 
   700 		if (!pPlugin)
   701 		{
   702 			g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", id);
   703 			return;
   704 		}
   705 
   706 		CmdList *pList;
   707 		if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   708 		{
   709 			g_RootMenu.ConsolePrint("[SM] No commands found for %s", pPlugin->GetFilename());
   710 			return;
   711 		}
   712 		if (!pList->size())
   713 		{
   714 			g_RootMenu.ConsolePrint("[SM] No commands found for %s", pPlugin->GetFilename());
   715 			return;
   716 		}
   717 
   718 		CmdList::iterator iter;
   719 		const char *type = NULL;
   720 		const char *name;
   721 		const char *help;
   722 		g_RootMenu.ConsolePrint(" %-17.16s %-8.7s %s", "[Name]", "[Type]", "[Help]");
   723 		for (iter=pList->begin();
   724 			 iter!=pList->end();
   725 			 iter++)
   726 		{
   727 			PlCmdInfo &cmd = (*iter);
   728 			if (cmd.type == Cmd_Server)
   729 			{
   730 				type = "server";
   731 			} else if (cmd.type == Cmd_Console) {
   732 				type = "console";
   733 			} else if (cmd.type == Cmd_Admin) {
   734 				type = "admin";
   735 			}
   736 			name = cmd.pInfo->pCmd->GetName();
   737 			if (cmd.pHook->helptext.size())
   738 			{
   739 				help = cmd.pHook->helptext.c_str();
   740 			} else {
   741 				help = cmd.pInfo->pCmd->GetHelpText();
   742 			}
   743 			g_RootMenu.ConsolePrint(" %-17.16s %-12.11s %s", name, type, help);		
   744 		}
   745 
   746 		return;
   747 	}
   748 
   749 	g_RootMenu.ConsolePrint("[SM] Usage: sm cmds <plugin #>");
   750 }