plugins/include/helpers.inc
author David Anderson <dvander@alliedmods.net>
Fri Jun 15 20:23:40 2007 +0000 (2007-06-15)
changeset 938 8e95112b2501
parent 927 a60cedac2a1a
child 982 c4fdf408ccee
permissions -rw-r--r--
added a hacky way to get NULL references
     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  */
    29 forward OnClientBanned(admin, client, time, const String:reason[]);
    30 
    31 /**
    32  * Provided by basecommands.sp when sm_addban or sm_banip is called.
    33  *
    34  * @param admin			Admin client index (0 for server).
    35  * @param info			User info (either steamid or ip).
    36  * @param time			Minutes banned for (0 is permanent).
    37  * @param reason		Ban reason (may be empty if none exists).
    38  */
    39 forward OnBanAdded(admin, const String:info[], time, const String:reason[]);
    40 
    41 /**
    42  * Provided by basecommands.sp when sm_unban or sm_unbanip is called.
    43  *
    44  * @param admin			Admin client index (0 for server).
    45  * @param info 			User info (either steamid or ip).
    46  */
    47 forward OnBanRemoved(admin, const String:info[]);
    48 
    49 /**
    50  * Formats a user's info as log text.
    51  *
    52  * @param client		Client index.
    53  * @param buffer		Buffer for text.
    54  * @param maxlength		Maximum length of text.
    55  */
    56 stock FormatUserLogText(client, String:buffer[], maxlength)
    57 {
    58 	decl String:auth[32];
    59 	decl String:name[40];
    60 	
    61 	new userid = GetClientUserId(client);
    62 	if (!GetClientAuthString(client, auth, sizeof(auth)))
    63 	{
    64 		StrCopy(auth, sizeof(auth), "UNKNOWN");
    65 	}
    66 	if (!GetClientName(client, name, sizeof(name)))
    67 	{
    68 		StrCopy(name, sizeof(name), "UNKNOWN");
    69 	}
    70 	
    71 	/** Currently, no team stuff ... */
    72 	
    73 	Format(buffer, maxlength, "\"%s<%d><%s><>\"", name, userid, auth);
    74 }
    75 
    76 /**
    77  * Returns plugin handle from plugin filename.
    78  *
    79  * @param filename		Filename of the plugin to search for.
    80  * @return				Handle to plugin if found, INVALID_HANDLE otherwise.
    81  */
    82 stock Handle:FindPluginByFile(const String:filename[])
    83 {
    84 	decl String:buffer[256];
    85 	
    86 	new Handle:iter = GetPluginIterator();
    87 	new Handle:pl;
    88 	
    89 	while (MorePlugins(iter))
    90 	{
    91 		pl = ReadPlugin(iter);
    92 		
    93 		GetPluginFilename(pl, buffer, sizeof(buffer));
    94 		if (StrCompare(buffer, filename) == 0)
    95 		{
    96 			return pl;
    97 		}
    98 	}
    99 	
   100 	CloseHandle(iter);
   101 	
   102 	return INVALID_HANDLE;
   103 }
   104 
   105 /**
   106  * Searches for clients that match an input string.
   107  *
   108  * Allowed patterns: 
   109  *  1) #<userid> or #<exact name>
   110  *  2) <partial or full name>
   111  *
   112  * @param pattern		Pattern to search for.
   113  * @param clients		Array to store matching clients in.
   114  * @param maxClients	Maximum clients in the array.
   115  * @return				Number of clients found.
   116  */
   117 stock SearchForClients(const String:pattern[], clients[], maxClients)
   118 {
   119 	new maxclients = GetMaxClients();
   120 	new total = 0;
   121 	
   122 	if (maxClients == 0)
   123 	{
   124 		return 0;
   125 	}
   126 	
   127 	if (pattern[0] == '#')
   128 	{
   129 		new input = StringToInt(pattern[1]);
   130 		if (!input)
   131 		{
   132 			decl String:name[65]
   133 			for (new i=1; i<=maxclients; i++)
   134 			{
   135 				if (!IsClientInGame(i))
   136 				{
   137 					continue;
   138 				}
   139 				GetClientName(i, name, sizeof(name));
   140 				if (StrEqual(name, pattern, false))
   141 				{
   142 					clients[0] = i;
   143 					return 1;
   144 				}
   145 			}
   146 		} else {
   147 			new client = GetClientOfUserId(input);
   148 			if (client)
   149 			{
   150 				clients[0] = client;
   151 				return 1;
   152 			}
   153 		}
   154 	}
   155 	
   156 	decl String:name[65]
   157 	for (new i=1; i<=maxclients; i++)
   158 	{
   159 		if (!IsClientInGame(i))
   160 		{
   161 			continue;
   162 		}
   163 		GetClientName(i, name, sizeof(name));
   164 		if (StrContains(name, pattern, false) != -1)
   165 		{
   166 			clients[total++] = i;
   167 			if (total >= maxClients)
   168 			{
   169 				break;
   170 			}
   171 		}
   172 	}
   173 	
   174 	return total;
   175 }