plugins/include/helpers.inc
author Borja Ferrer <faluco@alliedmods.net>
Sat Aug 04 00:09:10 2007 +0000 (2007-08-04)
changeset 1262 843e9ed0ff29
parent 1177 90c066adfa6b
child 1330 ab94316bb52b
permissions -rw-r--r--
fixed amb728, precache slap sounds
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     7  *  This file is part of the SourceMod/SourcePawn SDK.  This file may only be used 
     8  * or modified under the Terms and Conditions of its License Agreement, which is found 
     9  * in LICENSE.txt.  The Terms and Conditions for making SourceMod extensions/plugins 
    10  * may change at any time.  To view the latest information, see:
    11  *   http://www.sourcemod.net/license.php
    12  *
    13  * Version: $Id$
    14  */
    15  
    16 #if defined _helpers_included
    17  #endinput
    18 #endif
    19 #define _helpers_included
    20 
    21 /**
    22  * Provided by basecommands.sp when sm_ban is called.
    23  *
    24  * @param admin			Admin client index (0 for server).
    25  * @param client		Client index which will be banned.
    26  * @param time			Minutes banned for (0 is permanent).
    27  * @param reason		Ban reason (may be empty if none exists).
    28  * @return				Pl_Handled to block output, Pl_Stop to block output and action.
    29  */
    30 forward Action:OnClientBanned(admin, client, time, const String:reason[]);
    31 
    32 /**
    33  * Provided by basecommands.sp when sm_addban or sm_banip is called.
    34  *
    35  * @param admin			Admin client index (0 for server).
    36  * @param info			User info (either steamid or ip).
    37  * @param time			Minutes banned for (0 is permanent).
    38  * @param reason		Ban reason (may be empty if none exists).
    39  * @return				Pl_Handled to block output, Pl_Stop to block output and action.
    40  */
    41 forward Action:OnBanAdded(admin, const String:info[], time, const String:reason[]);
    42 
    43 /**
    44  * Provided by basecommands.sp when sm_unban or sm_unbanip is called.
    45  *
    46  * @param admin			Admin client index (0 for server).
    47  * @param info 			User info (either steamid or ip).
    48  * @return				Pl_Handled to block output, Pl_Stop to block output and action.
    49  */
    50 forward Action:OnBanRemoved(admin, const String:info[]);
    51 
    52 /**
    53  * Formats a user's info as log text.  This is usually not needed because
    54  * %L can be used to auto-format client information into a string.
    55  *
    56  * @param client		Client index.
    57  * @param buffer		Buffer for text.
    58  * @param maxlength		Maximum length of text.
    59  */
    60 stock FormatUserLogText(client, String:buffer[], maxlength)
    61 {
    62 	decl String:auth[32];
    63 	decl String:name[40];
    64 	
    65 	new userid = GetClientUserId(client);
    66 	if (!GetClientAuthString(client, auth, sizeof(auth)))
    67 	{
    68 		strcopy(auth, sizeof(auth), "UNKNOWN");
    69 	}
    70 	if (!GetClientName(client, name, sizeof(name)))
    71 	{
    72 		strcopy(name, sizeof(name), "UNKNOWN");
    73 	}
    74 	
    75 	/** Currently, no team stuff ... */
    76 	
    77 	Format(buffer, maxlength, "\"%s<%d><%s><>\"", name, userid, auth);
    78 }
    79 
    80 /**
    81  * Returns plugin handle from plugin filename.
    82  *
    83  * @param filename		Filename of the plugin to search for.
    84  * @return				Handle to plugin if found, INVALID_HANDLE otherwise.
    85  */
    86 stock Handle:FindPluginByFile(const String:filename[])
    87 {
    88 	decl String:buffer[256];
    89 	
    90 	new Handle:iter = GetPluginIterator();
    91 	new Handle:pl;
    92 	
    93 	while (MorePlugins(iter))
    94 	{
    95 		pl = ReadPlugin(iter);
    96 		
    97 		GetPluginFilename(pl, buffer, sizeof(buffer));
    98 		if (strcmp(buffer, filename, false) == 0)
    99 		{
   100 			CloseHandle(iter);
   101 			return pl;
   102 		}
   103 	}
   104 	
   105 	CloseHandle(iter);
   106 	
   107 	return INVALID_HANDLE;
   108 }
   109 
   110 /**
   111  * Searches for clients that match an input string.
   112  *
   113  * Allowed patterns: 
   114  *  1) #<userid> or #<exact name>
   115  *  2) <partial or full name>
   116  *
   117  * @param pattern		Pattern to search for.
   118  * @param clients		Array to store matching clients in.
   119  * @param maxClients	Maximum clients in the array.
   120  * @return				Number of clients found.
   121  */
   122 stock SearchForClients(const String:pattern[], clients[], maxClients)
   123 {
   124 	new maxclients = GetMaxClients();
   125 	new total = 0;
   126 	
   127 	if (maxClients == 0)
   128 	{
   129 		return 0;
   130 	}
   131 	
   132 	if (pattern[0] == '#')
   133 	{
   134 		new input = StringToInt(pattern[1]);
   135 		if (!input)
   136 		{
   137 			decl String:name[65]
   138 			for (new i=1; i<=maxclients; i++)
   139 			{
   140 				if (!IsClientInGame(i))
   141 				{
   142 					continue;
   143 				}
   144 				GetClientName(i, name, sizeof(name));
   145 				if (strcmp(name, pattern, false) == 0)
   146 				{
   147 					clients[0] = i;
   148 					return 1;
   149 				}
   150 			}
   151 		} else {
   152 			new client = GetClientOfUserId(input);
   153 			if (client)
   154 			{
   155 				clients[0] = client;
   156 				return 1;
   157 			}
   158 		}
   159 	}
   160 	
   161 	decl String:name[65]
   162 	for (new i=1; i<=maxclients; i++)
   163 	{
   164 		if (!IsClientInGame(i))
   165 		{
   166 			continue;
   167 		}
   168 		GetClientName(i, name, sizeof(name));
   169 		if (StrContains(name, pattern, false) != -1)
   170 		{
   171 			clients[total++] = i;
   172 			if (total >= maxClients)
   173 			{
   174 				break;
   175 			}
   176 		}
   177 	}
   178 	
   179 	return total;
   180 }
   181 
   182 /**
   183  * Wraps SearchForClients and handles producing error messages for
   184  * bad targets.
   185  *
   186  * @param client	Client who issued command
   187  * @param target	Client's target argument
   188  * @param nobots	Optional. Set to true if bots should NOT be targetted
   189  * @param immunity	Optional. Set to false to ignore target immunity.
   190  * @return		Index of target client, or -1 on error.
   191  */
   192 stock FindTarget(client, const String:target[], bool:nobots = false, bool:immunity = true)
   193 {
   194 	new clients[2];
   195 	new numClients = SearchForClients(target, clients, 2);
   196 	
   197 	if (numClients == 0)
   198 	{
   199 		ReplyToCommand(client, "[SM] %t", "No matching client");
   200 		return -1;
   201 	}
   202 	else if (numClients > 1)
   203 	{
   204 		ReplyToCommand(client, "[SM] %t", "More than one client matches", target);
   205 		return -1;
   206 	}
   207 	else if (immunity && !CanUserTarget(client, clients[0]))
   208 	{
   209 		ReplyToCommand(client, "[SM] %t", "Unable to target");
   210 		return -1;
   211 	}
   212 	else if (nobots && IsFakeClient(clients[0]))
   213 	{
   214 		ReplyToCommand(client, "[SM] %t", "Cannot target bot");
   215 		return -1;
   216 	}
   217 	
   218 	return clients[0];
   219 }