3 * =============================================================================
4 * SourceMod Basic Commands Plugin
5 * Implements basic admin commands.
7 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
8 * =============================================================================
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.
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
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/>.
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>.
38 public Plugin:myinfo =
40 name = "Basic Commands",
41 author = "AlliedModders LLC",
42 description = "Basic Admin Commands",
43 version = SOURCEMOD_VERSION,
44 url = "http://www.sourcemod.net/"
47 public OnPluginStart()
49 LoadTranslations("common.phrases");
51 RegAdminCmd("sm_kick", Command_Kick, ADMFLAG_KICK, "sm_kick <#userid|name> [reason]");
52 RegAdminCmd("sm_map", Command_Map, ADMFLAG_CHANGEMAP, "sm_map <map>");
53 RegAdminCmd("sm_rcon", Command_Rcon, ADMFLAG_RCON, "sm_rcon <args>");
54 RegAdminCmd("sm_cvar", Command_Cvar, ADMFLAG_CONVARS, "sm_cvar <cvar> [value]");
55 RegAdminCmd("sm_execcfg", Command_ExecCfg, ADMFLAG_CONFIG, "sm_execcfg <filename>");
56 RegAdminCmd("sm_who", Command_Who, ADMFLAG_GENERIC, "sm_who [#userid|name]");
57 RegAdminCmd("sm_reloadadmins", Command_ReloadAdmins, ADMFLAG_BAN, "sm_reloadadmins");
58 RegAdminCmd("sm_cancelvote", Command_CancelVote, ADMFLAG_VOTE, "sm_cancelvote");
61 public Action:Command_ReloadAdmins(client, args)
64 DumpAdminCache(AdminCache_Groups, true);
65 DumpAdminCache(AdminCache_Overrides, true);
67 LogAction(client, -1, "\"%L\" refreshed the admin cache.", client);
68 ReplyToCommand(client, "[SM] %t", "Admin cache refreshed");
70 return Plugin_Handled;
73 #define FLAG_STRINGS 14
74 new String:g_FlagNames[FLAG_STRINGS][20] =
92 CustomFlagsToString(String:buffer[], maxlength, flags)
94 decl String:joins[6][6];
97 for (new i=_:Admin_Custom1; i<=_:Admin_Custom6; i++)
101 IntToString(i - _:Admin_Custom1 + 1, joins[total++], 6);
105 ImplodeStrings(joins, total, ",", buffer, maxlength);
110 FlagsToString(String:buffer[], maxlength, flags)
112 decl String:joins[FLAG_STRINGS+1][32];
115 for (new i=0; i<FLAG_STRINGS; i++)
119 strcopy(joins[total++], 32, g_FlagNames[i]);
123 decl String:custom_flags[32];
124 if (CustomFlagsToString(custom_flags, sizeof(custom_flags), flags))
126 Format(joins[total++], 32, "custom(%s)", custom_flags);
129 ImplodeStrings(joins, total, ", ", buffer, maxlength);
132 public Action:Command_Who(client, args)
137 decl String:t_access[16], String:t_name[16];
138 Format(t_access, sizeof(t_access), "%t", "Access", client);
139 Format(t_name, sizeof(t_name), "%t", "Name", client);
141 PrintToConsole(client, "%-24.23s %s", t_name, t_access);
143 /* List all players */
144 new maxClients = GetMaxClients();
145 decl String:flagstring[255];
147 for (new i=1; i<=maxClients; i++)
149 if (!IsClientInGame(i))
153 new flags = GetUserFlagBits(i);
156 strcopy(flagstring, sizeof(flagstring), "none");
157 } else if (flags & ADMFLAG_ROOT) {
158 strcopy(flagstring, sizeof(flagstring), "root");
160 FlagsToString(flagstring, sizeof(flagstring), flags);
162 decl String:name[65];
163 GetClientName(i, name, sizeof(name));
164 PrintToConsole(client, "%d. %-24.23s %s", i, name, flagstring);
167 if (GetCmdReplySource() == SM_REPLY_TO_CHAT)
169 ReplyToCommand(client, "[SM] %t", "See console for output");
172 return Plugin_Handled;
176 GetCmdArg(1, arg, sizeof(arg));
179 new numClients = SearchForClients(arg, clients, 2);
183 ReplyToCommand(client, "[SM] %t", "No matching client");
184 return Plugin_Handled;
185 } else if (numClients > 1) {
186 ReplyToCommand(client, "[SM] %t", "More than one client matches", arg);
187 return Plugin_Handled;
190 new flags = GetUserFlagBits(clients[0]);
191 decl String:flagstring[255];
194 strcopy(flagstring, sizeof(flagstring), "none");
195 } else if (flags & ADMFLAG_ROOT) {
196 strcopy(flagstring, sizeof(flagstring), "root");
198 FlagsToString(flagstring, sizeof(flagstring), flags);
201 ReplyToCommand(client, "[SM] %t: %s", "Access", flagstring);
203 return Plugin_Handled;
206 public Action:Command_ExecCfg(client, args)
210 ReplyToCommand(client, "[SM] Usage: sm_execcfg <filename>");
211 return Plugin_Handled;
214 new String:path[64] = "cfg/";
215 GetCmdArg(1, path[4], sizeof(path)-4);
217 if (!FileExists(path))
219 ReplyToCommand(client, "[SM] %t", "Config not found", path[4]);
220 return Plugin_Handled;
223 ShowActivity(client, "%t", "Executed config", path[4]);
225 LogAction(client, -1, "\"%L\" executed config (file \"%s\")", client, path[4]);
227 ServerCommand("exec \"%s\"", path[4]);
229 return Plugin_Handled;
232 public Action:Command_Cvar(client, args)
236 ReplyToCommand(client, "[SM] Usage: sm_cvar <cvar> [value]");
237 return Plugin_Handled;
240 decl String:cvarname[33];
241 GetCmdArg(1, cvarname, sizeof(cvarname));
243 new Handle:hndl = FindConVar(cvarname);
244 if (hndl == INVALID_HANDLE)
246 ReplyToCommand(client, "[SM] %t", "Unable to find cvar", cvarname);
247 return Plugin_Handled;
250 new bool:allowed = false;
251 if (GetConVarFlags(hndl) & FCVAR_PROTECTED)
253 /* If they're root, allow anything */
254 if ((GetUserFlagBits(client) & ADMFLAG_ROOT) == ADMFLAG_ROOT)
258 /* If they're not root, and getting sv_password, see if they have ADMFLAG_PASSWORD */
259 else if (StrEqual(cvarname, "sv_password") && (GetUserFlagBits(client) & ADMFLAG_PASSWORD))
264 /* Do a check for the cheat cvar */
265 else if (StrEqual(cvarname, "sv_cheats"))
267 if (GetUserFlagBits(client) & ADMFLAG_CHEATS
268 || GetUserFlagBits(client) & ADMFLAG_ROOT)
273 /* If we drop down to here, it was a normal cvar. */
281 ReplyToCommand(client, "[SM] %t", "No access to cvar");
282 return Plugin_Handled;
285 decl String:value[255];
288 GetConVarString(hndl, value, sizeof(value));
290 ReplyToCommand(client, "[SM] %t", "Value of cvar", cvarname, value);
291 return Plugin_Handled;
294 GetCmdArg(2, value, sizeof(value));
296 if ((GetConVarFlags(hndl) & FCVAR_PROTECTED) != FCVAR_PROTECTED)
298 ShowActivity(client, "%t", "Cvar changed", cvarname, value);
300 ReplyToCommand(client, "[SM] %t", "Cvar changed", cvarname, value);
303 LogAction(client, -1, "\"%L\" changed cvar (cvar \"%s\") (value \"%s\")", client, cvarname, value);
305 SetConVarString(hndl, value, true);
307 return Plugin_Handled;
310 public Action:Command_Rcon(client, args)
314 ReplyToCommand(client, "[SM] Usage: sm_rcon <args>");
315 return Plugin_Handled;
318 decl String:argstring[255];
319 GetCmdArgString(argstring, sizeof(argstring));
321 LogAction(client, -1, "\"%L\" console command (cmdline \"%s\")", client, argstring);
323 ServerCommand("%s", argstring);
325 return Plugin_Handled;
328 public Action:Command_Map(client, args)
332 ReplyToCommand(client, "[SM] Usage: sm_map <map>");
333 return Plugin_Handled;
337 GetCmdArg(1, map, sizeof(map));
339 if (!IsMapValid(map))
341 ReplyToCommand(client, "[SM] %t", "Map was not found", map);
342 return Plugin_Handled;
345 ShowActivity(client, "%t", "Changing map", map);
347 LogAction(client, -1, "\"%L\" changed map to \"%s\"", client, map);
350 CreateDataTimer(3.0, Timer_ChangeMap, dp);
351 WritePackString(dp, map);
353 return Plugin_Handled;
356 public Action:Timer_ChangeMap(Handle:timer, Handle:dp)
361 ReadPackString(dp, map, sizeof(map));
363 ServerCommand("changelevel \"%s\"", map);
368 public Action:Command_Kick(client, args)
372 ReplyToCommand(client, "[SM] Usage: sm_kick <#userid|name> [reason]");
373 return Plugin_Handled;
377 decl String:Arguments[256];
378 GetCmdArgString(Arguments, sizeof(Arguments));
381 new len = BreakString(Arguments, arg, sizeof(arg));
383 new target = FindTarget(client, arg);
386 return Plugin_Handled;
389 GetClientName(target, arg, sizeof(arg));
393 /* Safely null terminate */
398 ShowActivity(client, "%t", "Kicked player", arg);
399 LogAction(client, target, "\"%L\" kicked \"%L\" (reason \"%s\")", client, target, Arguments[len]);
401 if (Arguments[0] == '\0')
403 KickClient(target, "%t", "Kicked by admin");
407 KickClient(target, "%s", Arguments[len]);
410 return Plugin_Handled;
413 public Action:Command_CancelVote(client, args)
415 if (!IsVoteInProgress())
417 ReplyToCommand(client, "[SM] %t", "Vote Not In Progress");
418 return Plugin_Handled;
421 ShowActivity(client, "%t", "Cancelled Vote");
425 return Plugin_Handled;