plugins/basecommands/who.sp
author David Anderson <dvander@alliedmods.net>
Wed Feb 25 20:07:12 2009 -0500 (2009-02-25)
changeset 2607 f3b8bbe17f85
parent 2289 44045ead61cd
child 2830 af1936f4c533
permissions -rw-r--r--
Backed out changeset fa286305bd3f
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Basecommands Plugin
     5  * Provides sm_who functionality
     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 PerformWho(client, target, ReplySource:reply, bool:is_admin)
    35 {
    36 	decl String:name[MAX_NAME_LENGTH];
    37 	GetClientName(target, name, sizeof(name));
    38 	
    39 	new bool:show_name = false;
    40 	new String:admin_name[MAX_NAME_LENGTH];
    41 	new AdminId:id = GetUserAdmin(target);
    42 	if (id != INVALID_ADMIN_ID && GetAdminUsername(id, admin_name, sizeof(admin_name)))
    43 	{
    44 		show_name = true;
    45 	}
    46 	
    47 	new ReplySource:old_reply = SetCmdReplySource(reply);
    48 	
    49 	if (id == INVALID_ADMIN_ID)
    50 	{
    51 		ReplyToCommand(client, "[SM] %t", "Player is not an admin", name);
    52 	}
    53 	else
    54 	{
    55 		if (!is_admin)
    56 		{
    57 			ReplyToCommand(client, "[SM] %t", "Player is an admin", name);
    58 		}
    59 		else
    60 		{
    61 			new flags = GetUserFlagBits(target);
    62 			decl String:flagstring[255];
    63 			if (flags == 0)
    64 			{
    65 				strcopy(flagstring, sizeof(flagstring), "none");
    66 			}
    67 			else if (flags & ADMFLAG_ROOT)
    68 			{
    69 				strcopy(flagstring, sizeof(flagstring), "root");
    70 			}
    71 			else
    72 			{
    73 				FlagsToString(flagstring, sizeof(flagstring), flags);
    74 			}
    75 			
    76 			if (show_name)
    77 			{
    78 				ReplyToCommand(client, "[SM] %t", "Admin logged in as", name, admin_name, flagstring);
    79 			}
    80 			else
    81 			{
    82 				ReplyToCommand(client, "[SM] %t", "Admin logged in anon", name, flagstring);
    83 			}
    84 		}
    85 	}
    86 	
    87 	SetCmdReplySource(old_reply);
    88 }
    89 
    90 DisplayWhoMenu(client)
    91 {
    92 	new Handle:menu = CreateMenu(MenuHandler_Who);
    93 	
    94 	decl String:title[100];
    95 	Format(title, sizeof(title), "%T:", "Identify player", client);
    96 	SetMenuTitle(menu, title);
    97 	SetMenuExitBackButton(menu, true);
    98 	
    99 	AddTargetsToMenu(menu, client, false, false);
   100 	
   101 	DisplayMenu(menu, client, MENU_TIME_FOREVER);
   102 }
   103 
   104 public AdminMenu_Who(Handle:topmenu, 
   105 					  TopMenuAction:action,
   106 					  TopMenuObject:object_id,
   107 					  param,
   108 					  String:buffer[],
   109 					  maxlength)
   110 {
   111 	if (action == TopMenuAction_DisplayOption)
   112 	{
   113 		Format(buffer, maxlength, "%T", "Identify player", param);
   114 	}
   115 	else if (action == TopMenuAction_SelectOption)
   116 	{
   117 		DisplayWhoMenu(param);
   118 	}
   119 }
   120 
   121 public MenuHandler_Who(Handle:menu, MenuAction:action, param1, param2)
   122 {
   123 	if (action == MenuAction_End)
   124 	{
   125 		CloseHandle(menu);
   126 	}
   127 	else if (action == MenuAction_Cancel)
   128 	{
   129 		if (param2 == MenuCancel_ExitBack && hTopMenu != INVALID_HANDLE)
   130 		{
   131 			DisplayTopMenu(hTopMenu, param1, TopMenuPosition_LastCategory);
   132 		}
   133 	}
   134 	else if (action == MenuAction_Select)
   135 	{
   136 		decl String:info[32];
   137 		new userid, target;
   138 		
   139 		GetMenuItem(menu, param2, info, sizeof(info));
   140 		userid = StringToInt(info);
   141 
   142 		if ((target = GetClientOfUserId(userid)) == 0)
   143 		{
   144 			PrintToChat(param1, "[SM] %t", "Player no longer available");
   145 		}
   146 		else if (!CanUserTarget(param1, target))
   147 		{
   148 			PrintToChat(param1, "[SM] %t", "Unable to target");
   149 		}
   150 		else
   151 		{
   152 			PerformWho(param1, target, SM_REPLY_TO_CHAT, (GetUserFlagBits(param1) != 0 ? true : false));
   153 		}
   154 		
   155 		/* Re-draw the menu if they're still valid */
   156 		
   157 		/* - Close the menu? redisplay? jump back up to the category?
   158 		if (IsClientInGame(param1) && !IsClientInKickQueue(param1))
   159 		{
   160 			DisplayWhoMenu(param1);
   161 		}
   162 		*/
   163 	}
   164 }
   165 public Action:Command_Who(client, args)
   166 {
   167 	new bool:is_admin = false;
   168 	
   169 	if (!client || (client && GetUserFlagBits(client) != 0))
   170 	{
   171 		is_admin = true;
   172 	}
   173 	
   174 	if (args < 1)
   175 	{
   176 		/* Display header */
   177 		decl String:t_access[16], String:t_name[16], String:t_username[16];
   178 		Format(t_access, sizeof(t_access), "%t", "Admin access", client);
   179 		Format(t_name, sizeof(t_name), "%t", "Name", client);
   180 		Format(t_username, sizeof(t_username), "%t", "Username", client);
   181 
   182 		if (is_admin)
   183 		{
   184 			PrintToConsole(client, "    %-24.23s %-18.17s %s", t_name, t_username, t_access);
   185 		}
   186 		else
   187 		{
   188 			PrintToConsole(client, "    %-24.23s %s", t_name, t_access);
   189 		}
   190 
   191 		/* List all players */
   192 		new maxClients = GetMaxClients();
   193 		decl String:flagstring[255];
   194 
   195 		for (new i=1; i<=maxClients; i++)
   196 		{
   197 			if (!IsClientInGame(i))
   198 			{
   199 				continue;
   200 			}
   201 			new flags = GetUserFlagBits(i);
   202 			new AdminId:id = GetUserAdmin(i);
   203 			if (flags == 0)
   204 			{
   205 				strcopy(flagstring, sizeof(flagstring), "none");
   206 			}
   207 			else if (flags & ADMFLAG_ROOT)
   208 			{
   209 				strcopy(flagstring, sizeof(flagstring), "root");
   210 			}
   211 			else
   212 			{
   213 				FlagsToString(flagstring, sizeof(flagstring), flags);
   214 			}
   215 			decl String:name[MAX_NAME_LENGTH];
   216 			new String:username[MAX_NAME_LENGTH];
   217 			
   218 			GetClientName(i, name, sizeof(name));
   219 			
   220 			if (id != INVALID_ADMIN_ID)
   221 			{
   222 				GetAdminUsername(id, username, sizeof(username));
   223 			}
   224 			
   225 			if (is_admin)
   226 			{
   227 				PrintToConsole(client, "%2d. %-24.23s %-18.17s %s", i, name, username, flagstring);
   228 			}
   229 			else
   230 			{
   231 				if (flags == 0)
   232 				{
   233 					PrintToConsole(client, "%2d. %-24.23s %t", i, name, "No");
   234 				}
   235 				else
   236 				{
   237 					PrintToConsole(client, "%2d. %-24.23s %t", i, name, "Yes");
   238 				}
   239 			}
   240 		}
   241 
   242 		if (GetCmdReplySource() == SM_REPLY_TO_CHAT)
   243 		{
   244 			ReplyToCommand(client, "[SM] %t", "See console for output");
   245 		}
   246 
   247 		return Plugin_Handled;
   248 	}
   249 
   250 	decl String:arg[65];
   251 	GetCmdArg(1, arg, sizeof(arg));
   252 
   253 	new target = FindTarget(client, arg, false, false);
   254 	if (target == -1)
   255 	{
   256 		return Plugin_Handled;
   257 	}
   258 	
   259 	PerformWho(client, target, GetCmdReplySource(), is_admin);
   260 
   261 	return Plugin_Handled;
   262 }