plugins/basecommands.sp
author David Anderson <dvander@alliedmods.net>
Fri Jun 08 01:09:17 2007 +0000 (2007-06-08)
changeset 922 980941c525fa
parent 921 e07dad9724f5
child 923 346e4c60fc32
permissions -rw-r--r--
sm_kick now uses targeting properly
     1 /**
     2  * basecommands.sp
     3  * Implements basic admin commands.
     4  * This file is part of SourceMod, Copyright (C) 2004-2007 AlliedModders LLC
     5  *
     6  * This program is free software; you can redistribute it and/or
     7  * modify it under the terms of the GNU General Public License
     8  * as published by the Free Software Foundation; either version 2
     9  * of the License, or (at your option) any later version.
    10  *
    11  * This program is distributed in the hope that it will be useful,
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  * GNU General Public License for more details.
    15  * 
    16  * You should have received a copy of the GNU General Public License
    17  * along with this program; if not, write to the Free Software
    18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    19  *
    20  * Version: $Id$
    21  */
    22 
    23 #include <sourcemod>
    24 
    25 #pragma semicolon 1
    26 
    27 public Plugin:myinfo = 
    28 {
    29 	name = "Basic Commands",
    30 	author = "AlliedModders LLC",
    31 	description = "Basic Admin Commands",
    32 	version = SOURCEMOD_VERSION,
    33 	url = "http://www.sourcemod.net/"
    34 };
    35 
    36 public OnPluginStart()
    37 {
    38 	LoadTranslations("common.cfg");
    39 	LoadTranslations("plugin.basecommands.cfg");
    40 	RegAdminCmd("sm_kick", Command_Kick, ADMFLAG_KICK, "sm_kick <#userid|name> [reason]");
    41 	RegAdminCmd("sm_map", Command_Map, ADMFLAG_CHANGEMAP, "sm_map <map>");
    42 	RegAdminCmd("sm_rcon", Command_Rcon, ADMFLAG_RCON, "sm_rcon <args>");
    43 	RegAdminCmd("sm_cvar", Command_Cvar, ADMFLAG_CONVARS, "sm_cvar <cvar> [value]");
    44 	RegAdminCmd("sm_execcfg", Command_ExecCfg, ADMFLAG_CONFIG, "sm_execcfg <filename>");
    45 }
    46 
    47 public Action:Command_ExecCfg(client, args)
    48 {
    49 	if (args < 1)
    50 	{
    51 		ReplyToCommand(client, "[SM] Usage: sm_execcfg <filename>");
    52 		return Plugin_Handled;
    53 	}
    54 	
    55 	new String:path[64] = "cfg/";
    56 	GetCmdArg(1, path[4], sizeof(path)-4);
    57 	
    58 	if (!FileExists(path))
    59 	{
    60 		ReplyToCommand(client, "[SM] %t", "Config not found", path[4]);
    61 		return Plugin_Handled;
    62 	}
    63 	
    64 	LogMessage("\"%L\" executed config (file \"%s\")", client, path[4]);
    65 	ShowActivity(client, "%t", "Executed config", path[4]);
    66 		
    67 	ServerCommand("exec \"%s\"", path[4]);
    68 	
    69 	ReplyToCommand(client, "[SM] %t", "Executed config", path[4]);
    70 	
    71 	return Plugin_Handled;
    72 }
    73 
    74 public Action:Command_Cvar(client, args)
    75 {
    76 	if (args < 1)
    77 	{
    78 		ReplyToCommand(client, "[SM] Usage: sm_cvar <cvar> [value]");
    79 		return Plugin_Handled;
    80 	}
    81 	
    82 	new String:cvarname[33];
    83 	GetCmdArg(1, cvarname, sizeof(cvarname));
    84 	
    85 	new Handle:hndl = FindConVar(cvarname);
    86 	if (hndl == INVALID_HANDLE)
    87 	{
    88 		ReplyToCommand(client, "[SM] %t", "Unable to find cvar", cvarname);
    89 		return Plugin_Handled;
    90 	}
    91 	
    92 	new bool:allowed = false;
    93 	if (GetConVarFlags(hndl) & FCVAR_PROTECTED)
    94 	{
    95 		/* If they're root, allow anything */
    96 		if ((GetUserFlagBits(client) & ADMFLAG_ROOT) == ADMFLAG_ROOT)
    97 		{
    98 			allowed = true;
    99 		}
   100 		/* If they're not root, and getting sv_password, see if they have ADMFLAG_PASSWORD */
   101 		else if (StrEqual(cvarname, "sv_password") && (GetUserFlagBits(client) & ADMFLAG_PASSWORD))
   102 		{
   103 			allowed = true;
   104 		}
   105 	} 
   106 	/* Do a check for the cheat cvar */
   107 	else if (StrEqual(cvarname, "sv_cheats"))
   108 	{
   109 		if (GetUserFlagBits(client) & ADMFLAG_CHEATS)
   110 		{
   111 			allowed = true;
   112 		}
   113 	}
   114 	/* If we drop down to here, it was a normal cvar. */
   115 	else
   116 	{
   117 		allowed = true;
   118 	}
   119 	
   120 	if (!allowed)
   121 	{
   122 		ReplyToCommand(client, "[SM] %t", "No access to cvar");
   123 		return Plugin_Handled;
   124 	}
   125 	
   126 	decl String:value[255];
   127 	if (args < 2)
   128 	{
   129 		GetConVarString(hndl, value, sizeof(value));
   130 		
   131 		ReplyToCommand(client, "[SM] %t", "Value of cvar", cvarname, value);
   132 		return Plugin_Handled;
   133 	}
   134 	
   135 	GetCmdArg(2, value, sizeof(value));
   136 	
   137 	LogMessage("\"%L\" changed cvar (cvar \"%s\") (value \"%s\")", client, cvarname, value);
   138 	if ((GetConVarFlags(hndl) & FCVAR_PROTECTED) != FCVAR_PROTECTED)
   139 	{
   140 		ShowActivity(client, "%t", "Cvar changed", cvarname, value);
   141 	}
   142 	
   143 	SetConVarString(hndl, value);
   144 	ReplyToCommand(client, "[SM] %t", "Cvar changed", cvarname, value);
   145 		
   146 	return Plugin_Handled;
   147 }
   148 
   149 public Action:Command_Rcon(client, args)
   150 {
   151 	if (args < 1)
   152 	{
   153 		ReplyToCommand(client, "[SM] Usage: sm_rcon <args>");
   154 		return Plugin_Handled;
   155 	}
   156 	
   157 	decl String:argstring[255];
   158 	GetCmdArgString(argstring, sizeof(argstring));
   159 	
   160 	LogMessage("\"%L\" console command (cmdline \"%s\")", client, argstring);
   161 	ServerCommand("%s", argstring);
   162 	
   163 	return Plugin_Handled;
   164 }
   165 
   166 public Action:Command_Map(client, args)
   167 {
   168 	if (args < 1)
   169 	{
   170 		ReplyToCommand(client, "[SM] Usage: sm_map <map>");
   171 		return Plugin_Handled;
   172 	}
   173 	
   174 	new String:map[64];
   175 	GetCmdArg(1, map, sizeof(map));
   176 	
   177 	if (!IsMapValid(map))
   178 	{
   179 		ReplyToCommand(client, "[SM] %t", "Map was not found", map);
   180 		return Plugin_Handled;
   181 	}
   182 	
   183 	LogMessage("\"%L\" changed map to \"%s\"", client, map);
   184 	ShowActivity(client, "%t", "Changing map", map);
   185 	ReplyToCommand(client, "%t", "Changing map", map);
   186 	
   187 	new Handle:dp;
   188 	CreateDataTimer(3.0, Timer_ChangeMap, dp);
   189 	WritePackString(dp, map);
   190 	
   191 	return Plugin_Handled;
   192 }
   193 
   194 public Action:Timer_ChangeMap(Handle:timer, Handle:dp)
   195 {
   196 	new String:map[65];
   197 	
   198 	ResetPack(dp);
   199 	ReadPackString(dp, map, sizeof(map));
   200 	
   201 	ServerCommand("changelevel \"%s\"", map);
   202 	
   203 	return Plugin_Stop;
   204 }
   205 
   206 public Action:Command_Kick(client, args)
   207 {
   208 	if (args < 1)
   209 	{
   210 		ReplyToCommand(client, "[SM] Usage: sm_kick <#userid|name> [reason]");
   211 		return Plugin_Handled;
   212 	}
   213 	
   214 	new String:arg[65];
   215 	GetCmdArg(1, arg, sizeof(arg));
   216 	
   217 	new clients[2];
   218 	new numClients = SearchForClients(arg, clients, 2);
   219 	
   220 	if (numClients == 0)
   221 	{
   222 		ReplyToCommand(client, "[SM] %t", "No matching client");
   223 		return Plugin_Handled;
   224 	} else if (numClients > 1) {
   225 		ReplyToCommand(client, "[SM] %t", "More than one client matches", arg);
   226 		return Plugin_Handled;
   227 	} else if (!CanUserTarget(client, clients[0])) {
   228 		ReplyToCommand(client, "[SM] %t", "Unable to target");
   229 	}
   230 	
   231 	new userid = GetClientUserId(clients[0]);
   232 	new String:name[65];
   233 	
   234 	GetClientName(clients[0], name, sizeof(name));
   235 	
   236 	decl String:reason[256];
   237 	if (args < 2)
   238 	{
   239 		/* Safely null terminate */
   240 		reason[0] = '\0';
   241 	} else {
   242 		GetCmdArg(2, reason, sizeof(reason));
   243 	}
   244 	
   245 	LogMessage("\"%L\" kicked \"%L\" (reason \"%s\")", client, clients[0], reason);
   246 	ShowActivity(client, "%t", "Kicked player", name);
   247 	
   248 	if (args < 2)
   249 	{
   250 		ServerCommand("kickid %d", userid);
   251 	} else {
   252 		ServerCommand("kickid %d \"%s\"", userid, reason);
   253 	}
   254 	
   255 	ReplyToCommand(client, "[SM] %t", "Client kicked", name);
   256 	
   257 	return Plugin_Handled;
   258 }