plugins/adminhelp.sp
author Scott Ehlert <ds@alliedmods.net>
Sun Sep 14 03:01:05 2008 -0500 (2008-09-14)
changeset 2289 44045ead61cd
parent 1952 b7aa2b626807
parent 1947 e73a1e51833b
permissions -rw-r--r--
Eliminated head: da55a80522cf, a remnant of sourcemod-1.1.0 branch
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Admin Help Plugin
     5  * Displays and searches SourceMod commands and descriptions.
     6  *
     7  * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
     8  * =============================================================================
     9  *
    10  * This program is free software; you can redistribute it and/or modify it under
    11  * the terms of the GNU General Public License, version 3.0, as published by the
    12  * Free Software Foundation.
    13  * 
    14  * This program is distributed in the hope that it will be useful, but WITHOUT
    15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    17  * details.
    18  *
    19  * You should have received a copy of the GNU General Public License along with
    20  * this program.  If not, see <http://www.gnu.org/licenses/>.
    21  *
    22  * As a special exception, AlliedModders LLC gives you permission to link the
    23  * code of this program (as well as its derivative works) to "Half-Life 2," the
    24  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    25  * by the Valve Corporation.  You must obey the GNU General Public License in
    26  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    27  * this exception to all derivative works.  AlliedModders LLC defines further
    28  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    29  * or <http://www.sourcemod.net/license.php>.
    30  *
    31  * Version: $Id$
    32  */
    33 
    34 #pragma semicolon 1
    35 
    36 #include <sourcemod>
    37 
    38 #define COMMANDS_PER_PAGE	10
    39 
    40 public Plugin:myinfo = 
    41 {
    42 	name = "Admin Help",
    43 	author = "AlliedModders LLC",
    44 	description = "Display command information",
    45 	version = SOURCEMOD_VERSION,
    46 	url = "http://www.sourcemod.net/"
    47 };
    48 
    49 public OnPluginStart()
    50 {
    51 	LoadTranslations("common.phrases");
    52 	LoadTranslations("adminhelp.phrases");
    53 	RegConsoleCmd("sm_help", HelpCmd, "Displays SourceMod commands and descriptions");
    54 	RegConsoleCmd("sm_searchcmd", HelpCmd, "Searches SourceMod commands");
    55 }
    56 
    57 public Action:HelpCmd(client, args)
    58 {
    59 	decl String:arg[64], String:CmdName[20];
    60 	new PageNum = 1;
    61 	new bool:DoSearch;
    62 
    63 	GetCmdArg(0, CmdName, sizeof(CmdName));
    64 
    65 	if (GetCmdArgs() >= 1)
    66 	{
    67 		GetCmdArg(1, arg, sizeof(arg));
    68 		StringToIntEx(arg, PageNum);
    69 		PageNum = (PageNum <= 0) ? 1 : PageNum;
    70 	}
    71 
    72 	DoSearch = (strcmp("sm_help", CmdName) == 0) ? false : true;
    73 
    74 	if (GetCmdReplySource() == SM_REPLY_TO_CHAT)
    75 	{
    76 		ReplyToCommand(client, "[SM] %t", "See console for output");
    77 	}
    78 
    79 	decl String:Name[64];
    80 	decl String:Desc[255];
    81 	decl String:NoDesc[128];
    82 	new Flags;
    83 	new Handle:CmdIter = GetCommandIterator();
    84 
    85 	FormatEx(NoDesc, sizeof(NoDesc), "%t", "No description available");
    86 
    87 	if (DoSearch)
    88 	{
    89 		new i = 1;
    90 		while (ReadCommandIterator(CmdIter, Name, sizeof(Name), Flags, Desc, sizeof(Desc)))
    91 		{
    92 			if ((StrContains(Name, arg, false) != -1) && CheckCommandAccess(client, Name, Flags))
    93 			{
    94 				PrintToConsole(client, "[%03d] %s - %s", i++, Name, (Desc[0] == '\0') ? NoDesc : Desc);
    95 			}
    96 		}
    97 
    98 		if (i == 1)
    99 		{
   100 			PrintToConsole(client, "%t", "No matching results found");
   101 		}
   102 	} else {
   103 		PrintToConsole(client, "%t", "SM help commands");		
   104 
   105 		/* Skip the first N commands if we need to */
   106 		if (PageNum > 1)
   107 		{
   108 			new i;
   109 			new EndCmd = (PageNum-1) * COMMANDS_PER_PAGE - 1;
   110 			for (i=0; ReadCommandIterator(CmdIter, Name, sizeof(Name), Flags, Desc, sizeof(Desc)) && i<EndCmd; )
   111 			{
   112 				if (CheckCommandAccess(client, Name, Flags))
   113 				{
   114 					i++;
   115 				}
   116 			}
   117 
   118 			if (i == 0)
   119 			{
   120 				PrintToConsole(client, "%t", "No commands available");
   121 				CloseHandle(CmdIter);
   122 				return Plugin_Handled;
   123 			}
   124 		}
   125 
   126 		/* Start printing the commands to the client */
   127 		new i;
   128 		new StartCmd = (PageNum-1) * COMMANDS_PER_PAGE;
   129 		for (i=0; ReadCommandIterator(CmdIter, Name, sizeof(Name), Flags, Desc, sizeof(Desc)) && i<COMMANDS_PER_PAGE; )
   130 		{
   131 			if (CheckCommandAccess(client, Name, Flags))
   132 			{
   133 				i++;
   134 				PrintToConsole(client, "[%03d] %s - %s", i+StartCmd, Name, (Desc[0] == '\0') ? NoDesc : Desc);
   135 			}
   136 		}
   137 
   138 		if (i == 0)
   139 		{
   140 			PrintToConsole(client, "%t", "No commands available");
   141 		} else {
   142 			PrintToConsole(client, "%t", "Entries n - m in page k", StartCmd+1, i+StartCmd, PageNum);
   143 		}
   144 
   145 		/* Test if there are more commands available */
   146 		if (ReadCommandIterator(CmdIter, Name, sizeof(Name), Flags, Desc, sizeof(Desc)) && CheckCommandAccess(client, Name, Flags))
   147 		{
   148 			PrintToConsole(client, "%t", "Type sm_help to see more", PageNum+1);
   149 		}
   150 	}
   151 
   152 	CloseHandle(CmdIter);
   153 
   154 	return Plugin_Handled;
   155 }