3 * Implements basic admin commands.
4 * This file is part of SourceMod, Copyright (C) 2004-2007 AlliedModders LLC
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.
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.
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.
27 public Plugin:myinfo =
29 name = "Basic Commands",
30 author = "AlliedModders LLC",
31 description = "Basic Admin Commands",
32 version = SOURCEMOD_VERSION,
33 url = "http://www.sourcemod.net/"
36 new Handle:hBanForward = INVALID_HANDLE;
38 public OnPluginStart()
40 LoadTranslations("common.cfg");
41 LoadTranslations("plugin.basecommands.cfg");
42 RegAdminCmd("sm_kick", Command_Kick, ADMFLAG_KICK, "sm_kick <#userid|name> [reason]");
43 RegAdminCmd("sm_map", Command_Map, ADMFLAG_CHANGEMAP, "sm_map <map>");
44 RegAdminCmd("sm_rcon", Command_Rcon, ADMFLAG_RCON, "sm_rcon <args>");
45 RegAdminCmd("sm_cvar", Command_Cvar, ADMFLAG_CONVARS, "sm_cvar <cvar> [value]");
46 RegAdminCmd("sm_execcfg", Command_ExecCfg, ADMFLAG_CONFIG, "sm_execcfg <filename>");
47 RegAdminCmd("sm_who", Command_Who, ADMFLAG_GENERIC, "sm_who [#userid|name]");
48 RegAdminCmd("sm_ban", Command_Ban, ADMFLAG_BAN, "sm_ban <#userid|name> <minutes|0> [reason]");
49 RegAdminCmd("sm_unban", Command_Unban, ADMFLAG_UNBAN, "sm_unban <steamid>");
51 hBanForward = CreateGlobalForward("OnClientBanned", ET_Ignore, Param_Cell, Param_Cell, Param_String);
54 public Action:Command_Unban(client, args)
58 ReplyToCommand(client, "[SM] Usage: sm_unban <steamid>");
59 return Plugin_Handled;
62 new String:arg[50], start=0;
63 GetCmdArgString(arg, sizeof(arg));
65 if (strncmp(arg, "STEAM_0:", 8) == 0)
68 } else if (strncmp(arg, "0:1:", 4) == 0 || strncmp(arg, "0:0:", 4) == 0)
73 LogMessage("\"%L\" removed ban (filter \"%s\")", arg[start]);
74 ServerCommand("removeid \"%s\"", arg[start]);
75 ReplyToCommand(client, "[SM] %t", "Removed bans matching", arg);
77 return Plugin_Handled;
80 public Action:Command_Ban(client, args)
84 ReplyToCommand(client, "[SM] Usage: sm_ban <#userid|name> <minutes|0> [reason]");
85 return Plugin_Handled;
89 GetCmdArg(1, arg, sizeof(arg));
92 new numClients = SearchForClients(arg, clients, 2);
96 ReplyToCommand(client, "[SM] %t", "No matching client");
97 return Plugin_Handled;
98 } else if (numClients > 1) {
99 ReplyToCommand(client, "[SM] %t", "More than one client matches", arg);
100 return Plugin_Handled;
101 } else if (!CanUserTarget(client, clients[0])) {
102 ReplyToCommand(client, "[SM] %t", "Unable to target");
103 return Plugin_Handled;
104 } else if (IsFakeClient(clients[0])) {
105 ReplyToCommand(client, "[SM] %t", "Cannot target bot");
106 return Plugin_Handled;
109 new String:s_time[12];
110 GetCmdArg(2, s_time, sizeof(s_time));
112 new time = StringToInt(s_time);
114 decl String:reason[128];
117 GetCmdArg(3, reason, sizeof(reason));
122 new userid = GetClientUserId(clients[0]);
123 GetClientName(clients[0], arg, sizeof(arg));
127 if (reason[0] == '\0')
129 ShowActivity(client, "%t", "Permabanned player", arg);
131 ShowActivity(client, "%t", "Permabanned player reason", arg, reason);
134 if (reason[0] == '\0')
136 ShowActivity(client, "%t", "Banned player", arg, time);
138 ShowActivity(client, "%t", "Banned player reason", arg, time, reason);
142 LogMessage("\"%L\" banned \"%L\" (minutes \"%d\") (reason \"%s\")", client, clients[0], time, reason);
144 /* Fire the ban forward */
145 Call_StartForward(hBanForward);
146 Call_PushCell(client);
147 Call_PushCell(clients[0]);
148 Call_PushString(reason);
151 if (reason[0] == '\0')
153 strcopy(reason, sizeof(reason), "Banned");
156 ServerCommand("banid %d %d", time, userid);
157 ServerCommand("kickid %d \"%s\"", userid, reason);
161 ServerCommand("writeid");
164 return Plugin_Handled;
167 #define FLAG_STRINGS 14
168 new String:g_FlagNames[FLAG_STRINGS][20] =
186 FlagsToString(String:buffer[], maxlength, flags)
188 new String:joins[FLAG_STRINGS][20];
191 for (new i=0; i<FLAG_STRINGS; i++)
195 strcopy(joins[total++], 20, g_FlagNames[i]);
199 ImplodeStrings(joins, total, ", ", buffer, maxlength);
202 public Action:Command_Who(client, args)
207 new String:t_access[16], String:t_name[16];
208 Format(t_access, sizeof(t_access), "%t", "Access", client);
209 Format(t_name, sizeof(t_name), "%t", "Name", client);
211 PrintToConsole(client, "%-24.23s %s", t_name, t_access);
213 /* List all players */
214 new maxClients = GetMaxClients();
215 new String:flagstring[255];
217 for (new i=1; i<=maxClients; i++)
219 if (!IsClientInGame(i))
223 new flags = GetUserFlagBits(i);
226 strcopy(flagstring, sizeof(flagstring), "none");
227 } else if (flags & ADMFLAG_ROOT) {
228 strcopy(flagstring, sizeof(flagstring), "root");
230 FlagsToString(flagstring, sizeof(flagstring), flags);
232 decl String:name[65];
233 GetClientName(i, name, sizeof(name));
234 PrintToConsole(client, "%d. %-24.23s %s", i, name, flagstring);
237 if (GetCmdReplySource() == SM_REPLY_TO_CHAT)
239 ReplyToCommand(client, "[SM] %t", "See console for output");
242 return Plugin_Handled;
246 GetCmdArg(1, arg, sizeof(arg));
249 new numClients = SearchForClients(arg, clients, 2);
253 ReplyToCommand(client, "[SM] %t", "No matching client");
254 return Plugin_Handled;
255 } else if (numClients > 1) {
256 ReplyToCommand(client, "[SM] %t", "More than one client matches", arg);
257 return Plugin_Handled;
260 new flags = GetUserFlagBits(clients[0]);
261 new String:flagstring[255];
264 strcopy(flagstring, sizeof(flagstring), "none");
265 } else if (flags & ADMFLAG_ROOT) {
266 strcopy(flagstring, sizeof(flagstring), "root");
268 FlagsToString(flagstring, sizeof(flagstring), flags);
271 ReplyToCommand(client, "[SM] %t: %s", "Access", flagstring);
273 return Plugin_Handled;
276 public Action:Command_ExecCfg(client, args)
280 ReplyToCommand(client, "[SM] Usage: sm_execcfg <filename>");
281 return Plugin_Handled;
284 new String:path[64] = "cfg/";
285 GetCmdArg(1, path[4], sizeof(path)-4);
287 if (!FileExists(path))
289 ReplyToCommand(client, "[SM] %t", "Config not found", path[4]);
290 return Plugin_Handled;
293 ShowActivity(client, "%t", "Executed config", path[4]);
295 LogMessage("\"%L\" executed config (file \"%s\")", client, path[4]);
297 ServerCommand("exec \"%s\"", path[4]);
299 return Plugin_Handled;
302 public Action:Command_Cvar(client, args)
306 ReplyToCommand(client, "[SM] Usage: sm_cvar <cvar> [value]");
307 return Plugin_Handled;
310 new String:cvarname[33];
311 GetCmdArg(1, cvarname, sizeof(cvarname));
313 new Handle:hndl = FindConVar(cvarname);
314 if (hndl == INVALID_HANDLE)
316 ReplyToCommand(client, "[SM] %t", "Unable to find cvar", cvarname);
317 return Plugin_Handled;
320 new bool:allowed = false;
321 if (GetConVarFlags(hndl) & FCVAR_PROTECTED)
323 /* If they're root, allow anything */
324 if ((GetUserFlagBits(client) & ADMFLAG_ROOT) == ADMFLAG_ROOT)
328 /* If they're not root, and getting sv_password, see if they have ADMFLAG_PASSWORD */
329 else if (StrEqual(cvarname, "sv_password") && (GetUserFlagBits(client) & ADMFLAG_PASSWORD))
334 /* Do a check for the cheat cvar */
335 else if (StrEqual(cvarname, "sv_cheats"))
337 if (GetUserFlagBits(client) & ADMFLAG_CHEATS)
342 /* If we drop down to here, it was a normal cvar. */
350 ReplyToCommand(client, "[SM] %t", "No access to cvar");
351 return Plugin_Handled;
354 decl String:value[255];
357 GetConVarString(hndl, value, sizeof(value));
359 ReplyToCommand(client, "[SM] %t", "Value of cvar", cvarname, value);
360 return Plugin_Handled;
363 GetCmdArg(2, value, sizeof(value));
365 if ((GetConVarFlags(hndl) & FCVAR_PROTECTED) != FCVAR_PROTECTED)
367 ShowActivity(client, "%t", "Cvar changed", cvarname, value);
369 ReplyToCommand(client, "[SM] %t", "Cvar changed", cvarname, value);
372 LogMessage("\"%L\" changed cvar (cvar \"%s\") (value \"%s\")", client, cvarname, value);
374 SetConVarString(hndl, value);
376 return Plugin_Handled;
379 public Action:Command_Rcon(client, args)
383 ReplyToCommand(client, "[SM] Usage: sm_rcon <args>");
384 return Plugin_Handled;
387 decl String:argstring[255];
388 GetCmdArgString(argstring, sizeof(argstring));
390 LogMessage("\"%L\" console command (cmdline \"%s\")", client, argstring);
392 ServerCommand("%s", argstring);
394 return Plugin_Handled;
397 public Action:Command_Map(client, args)
401 ReplyToCommand(client, "[SM] Usage: sm_map <map>");
402 return Plugin_Handled;
406 GetCmdArg(1, map, sizeof(map));
408 if (!IsMapValid(map))
410 ReplyToCommand(client, "[SM] %t", "Map was not found", map);
411 return Plugin_Handled;
414 ShowActivity(client, "%t", "Changing map", map);
416 LogMessage("\"%L\" changed map to \"%s\"", client, map);
419 CreateDataTimer(3.0, Timer_ChangeMap, dp);
420 WritePackString(dp, map);
422 return Plugin_Handled;
425 public Action:Timer_ChangeMap(Handle:timer, Handle:dp)
430 ReadPackString(dp, map, sizeof(map));
432 ServerCommand("changelevel \"%s\"", map);
437 public Action:Command_Kick(client, args)
441 ReplyToCommand(client, "[SM] Usage: sm_kick <#userid|name> [reason]");
442 return Plugin_Handled;
446 GetCmdArg(1, arg, sizeof(arg));
449 new numClients = SearchForClients(arg, clients, 2);
453 ReplyToCommand(client, "[SM] %t", "No matching client");
454 return Plugin_Handled;
455 } else if (numClients > 1) {
456 ReplyToCommand(client, "[SM] %t", "More than one client matches", arg);
457 return Plugin_Handled;
458 } else if (!CanUserTarget(client, clients[0])) {
459 ReplyToCommand(client, "[SM] %t", "Unable to target");
460 return Plugin_Handled;
463 new userid = GetClientUserId(clients[0]);
466 GetClientName(clients[0], name, sizeof(name));
468 decl String:reason[256];
471 /* Safely null terminate */
474 GetCmdArg(2, reason, sizeof(reason));
477 ShowActivity(client, "%t", "Kicked player", name);
479 LogMessage("\"%L\" kicked \"%L\" (reason \"%s\")", client, clients[0], reason);
483 ServerCommand("kickid %d", userid);
485 ServerCommand("kickid %d \"%s\"", userid, reason);
488 return Plugin_Handled;