plugins/include/helpers.inc
author Scott Ehlert <ds@alliedmods.net>
Thu Jun 21 19:51:34 2007 +0000 (2007-06-21)
changeset 996 378358b53fd5
parent 982 c4fdf408ccee
child 1083 9b3e66a9369b
permissions -rw-r--r--
Not sure how I did this, but fixed KickClient include
     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 (StrCompare(buffer, filename) == 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 (StrEqual(name, pattern, false))
   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 }