public/IPlayerHelpers.h
author Matt Woodrow <pred@alliedmods.net>
Sun Jul 06 03:30:06 2008 +0000 (2008-07-06)
changeset 2165 ca3757e676f6
parent 1952 b7aa2b626807
child 2289 44045ead61cd
permissions -rw-r--r--
Fixed requiring dependencies at the wrong time.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #ifndef _INCLUDE_SOURCEMOD_INTERFACE_IPLAYERHELPERS_H_
    33 #define _INCLUDE_SOURCEMOD_INTERFACE_IPLAYERHELPERS_H_
    34 
    35 /**
    36  * @file IPlayerHelpers.h
    37  * @brief Defines basic helper functions for Half-Life 2 clients
    38  */
    39 
    40 #include <IShareSys.h>
    41 #include <IAdminSystem.h>
    42 
    43 #define SMINTERFACE_PLAYERMANAGER_NAME		"IPlayerManager"
    44 #define SMINTERFACE_PLAYERMANAGER_VERSION	7
    45 
    46 struct edict_t;
    47 class IPlayerInfo;
    48 
    49 #define SM_REPLY_CONSOLE	0			/**< Reply to console. */
    50 #define SM_REPLY_CHAT		1			/**< Reply to chat. */
    51 
    52 namespace SourceMod
    53 {
    54 	/**
    55 	 * @brief Abstracts some Half-Life 2 and SourceMod properties about clients.
    56 	 */
    57 	class IGamePlayer
    58 	{
    59 	public:
    60 		/**
    61 		 * @brief Returns the player's name.
    62 		 *
    63 		 * @return		String containing the player's name,
    64 		 *				or NULL if unavailable.
    65 		 */
    66 		virtual const char *GetName() =0;
    67 
    68 		/**
    69 		 * @brief Returns the player's IP address.
    70 		 *
    71 		 * @return		String containing the player's IP address,
    72 		 *				or NULL if unavailable.
    73 		 */
    74 		virtual const char *GetIPAddress() =0;
    75 
    76 		/**
    77 		 * @brief Returns the player's authentication string.
    78 		 *
    79 		 * @return		String containing the player's auth string.
    80 		 *				May be NULL if unavailable.
    81 		 */
    82 		virtual const char *GetAuthString() =0;
    83 
    84 		/**
    85 		 * @brief Returns the player's edict_t structure.
    86 		 *
    87 		 * @return		edict_t pointer, or NULL if unavailable.
    88 		 */
    89 		virtual edict_t *GetEdict() =0;
    90 
    91 		/**
    92 		 * @brief Returns whether the player is in game (putinserver).
    93 		 *
    94 		 * @return		True if in game, false otherwise.
    95 		 */
    96 		virtual bool IsInGame() =0;
    97 
    98 		/**
    99 		 * @brief Returns whether the player is connected.
   100 		 *
   101 		 * Note: If this returns true, all above functions except for
   102 		 * GetAuthString() should return non-NULL results.
   103 		 *
   104 		 * @return		True if connected, false otherwise.
   105 		 */
   106 		virtual bool IsConnected() =0;
   107 
   108 		/**
   109 		 * @brief Returns whether the player is a fake client.
   110 		 *
   111 		 * @return		True if a fake client, false otherwise.
   112 		 */
   113 		virtual bool IsFakeClient() =0;
   114 
   115 		/**
   116 		 * @brief Returns the client's AdminId, if any.
   117 		 *
   118 		 * @return		AdminId, or INVALID_ADMIN_ID if none.
   119 		 */
   120 		virtual AdminId GetAdminId() =0;
   121 
   122 		/**
   123 		 * @brief Sets the client's AdminId.
   124 		 *
   125 		 * @param id	AdminId to set.
   126 		 * @param temp	If true, the id will be invalidated on disconnect.
   127 		 */
   128 		virtual void SetAdminId(AdminId id, bool temp) =0;
   129 
   130 		/**
   131 		 * @brief Returns the client's userid.
   132 		 *
   133 		 * @return		Userid.
   134 		 */
   135 		virtual int GetUserId() =0;
   136 
   137 		/**
   138 		 * @brief Returns the client's language id.
   139 		 *
   140 		 * @return		Language id.
   141 		 */
   142 		virtual unsigned int GetLanguageId() =0;
   143 
   144 		/**
   145 		 * @brief Returns a player's IPlayerInfo object, if any.
   146 		 *
   147 		 * @return		IPlayerInfo pointer, or NULL if none.
   148 		 */
   149 		virtual IPlayerInfo *GetPlayerInfo() =0;
   150 
   151 		/**
   152 		 * @brief Runs through Core's admin authorization checks.  If the 
   153 		 * client is already an admin, no checks are performed.  
   154 		 *
   155 		 * Note that this function operates solely against the in-memory admin 
   156 		 * cache.  It will check steamids, IPs, names, and verify a password 
   157 		 * if one exists.  To implement other authentication schemes, simply 
   158 		 * don't call this function and use IGamePlayer::SetAdminId() instead.
   159 		 *
   160 		 * @return				True if access changed, false otherwise.
   161 		 */
   162 		virtual bool RunAdminCacheChecks() =0;
   163 
   164 		/**
   165 		 * @brief Notifies all listeners that the client has completed 
   166 		 * all of your post-connection (in-game, auth, admin) checks.  
   167 		 *
   168 		 * If you returned "false" from OnClientPreAdminCheck(), you must 
   169 		 * ALWAYS manually invoke this function, even if RunAdminCacheChecks() 
   170 		 * failed or you did not assign an AdminId.  Failure to call this 
   171 		 * function could result in plugins (such as reservedslots) not 
   172 		 * working properly.
   173 		 *
   174 		 * If you are implementing asynchronous fetches, and the client 
   175 		 * disconnects during your fetching process, you should make sure to 
   176 		 * recognize that case and not call this function.  That is, do not 
   177 		 * call this function on mismatched PreCheck calls, or on disconnected 
   178 		 * clients.  A good way to check this is to pass userids around, which 
   179 		 * are unique per client connection.
   180 		 *
   181 		 * Calling this has no effect if it has already been called on the 
   182 		 * given client (thus it is safe for multiple asynchronous plugins to 
   183 		 * call it at various times).
   184 		 */
   185 		virtual void NotifyPostAdminChecks() =0;
   186 	};
   187 
   188 	/**
   189 	 * @brief Provides callbacks for important client events.
   190 	 */
   191 	class IClientListener
   192 	{
   193 	public:
   194 	   /**
   195 		* @brief Returns the current client listener version.
   196 		*
   197 		* @return		Client listener version.
   198 		*/
   199 		virtual unsigned int GetClientListenerVersion()
   200 		{
   201 			return SMINTERFACE_PLAYERMANAGER_VERSION;
   202 		}
   203 	public:
   204 		/**
   205 		 * @brief Called when a client requests connection.
   206 		 *
   207 		 * @param client		Index of the client.
   208 		 * @param error			Error buffer for a disconnect reason.
   209 		 * @param maxlength		Maximum length of error buffer.
   210 		 * @return				True to allow client, false to reject.
   211 		 */
   212 		virtual bool InterceptClientConnect(int client, char *error, size_t maxlength)
   213 		{
   214 			return true;
   215 		}
   216 
   217 		/**
   218 		 * @brief Called when a client has connected.
   219 		 *
   220 		 * @param client		Index of the client.
   221 		 */
   222 		virtual void OnClientConnected(int client)
   223 		{
   224 		}
   225 
   226 		/**
   227 		 * @brief Called when a client is put in server.
   228 		 *
   229 		 * @param client		Index of the client.
   230 		 */
   231 		virtual void OnClientPutInServer(int client)
   232 		{
   233 		}
   234 
   235 		/**
   236 		 * @brief Called when a client is disconnecting (not fully disconnected yet).
   237 		 *
   238 		 * @param client		Index of the client.
   239 		 */
   240 		virtual void OnClientDisconnecting(int client)
   241 		{
   242 		}
   243 
   244 		/**
   245 		 * @brief Called when a client has fully disconnected.
   246 		 *
   247 		 * @param client		Index of the client.
   248 		 */
   249 		virtual void OnClientDisconnected(int client)
   250 		{
   251 		}
   252 
   253 		/**
   254 		 * @brief Called when a client has received authorization.
   255 		 *
   256 		 * @param client		Index of the client.
   257 		 * @param authstring	Authorization string.
   258 		 */
   259 		virtual void OnClientAuthorized(int client, const char *authstring)
   260 		{
   261 		}
   262 
   263 		/**
   264 		 * @brief Called when the server is activated.
   265 		 */
   266 		virtual void OnServerActivated(int max_clients)
   267 		{
   268 		}
   269 
   270 		/**
   271 		 * @brief Called once a client is authorized and fully in-game, but 
   272 		 * before admin checks are done.  This can be used to override the 
   273 		 * default admin checks for a client.
   274 		 *
   275 		 * By default, this function allows the authentication process to 
   276 		 * continue as normal.  If you need to delay the cache searching 
   277 		 * process in order to get asynchronous data, then return false here. 
   278 		 *
   279 		 * If you return false, you must call IPlayerManager::NotifyPostAdminCheck 
   280 		 * for the same client, or else the OnClientPostAdminCheck callback will 
   281 		 * never be called.
   282 		 *
   283 		 * @param client		Client index.
   284 		 * @return				True to continue normally, false to override 
   285 		 *						the authentication process.
   286 		 */
   287 		virtual bool OnClientPreAdminCheck(int client)
   288 		{
   289 			return true;
   290 		}
   291 
   292 		/**
   293 		 * @brief Called once a client is authorized and fully in-game, and 
   294 		 * after all post-connection authorizations have been passed.  If the 
   295 		 * client does not have an AdminId by this stage, it means that no 
   296 		 * admin entry was in the cache that matched, and the user could not 
   297 		 * be authenticated as an admin.
   298 		 *
   299 		 * @param client		Client index.
   300 		 */
   301 		virtual void OnClientPostAdminCheck(int client)
   302 		{
   303 		}
   304 	};
   305 
   306 	#define COMMAND_FILTER_ALIVE		(1<<0)		/**< Only allow alive players */
   307 	#define COMMAND_FILTER_DEAD			(1<<1)		/**< Only filter dead players */
   308 	#define COMMAND_FILTER_CONNECTED	(1<<2)		/**< Allow players not fully in-game */
   309 	#define COMMAND_FILTER_NO_IMMUNITY	(1<<3)		/**< Ignore immunity rules */
   310 	#define COMMAND_FILTER_NO_MULTI		(1<<4)		/**< Do not allow multiple target patterns */
   311 	#define COMMAND_FILTER_NO_BOTS		(1<<5)		/**< Do not allow bots to be targetted */
   312 
   313 	#define COMMAND_TARGET_VALID		1			/**< Client passed the filter */
   314 	#define COMMAND_TARGET_NONE			0			/**< No target was found */
   315 	#define COMMAND_TARGET_NOT_ALIVE	-1			/**< Single client is not alive */
   316 	#define COMMAND_TARGET_NOT_DEAD		-2			/**< Single client is not dead */
   317 	#define COMMAND_TARGET_NOT_IN_GAME	-3			/**< Single client is not in game */
   318 	#define COMMAND_TARGET_IMMUNE		-4			/**< Single client is immune */
   319 	#define COMMAND_TARGET_EMPTY_FILTER	-5			/**< A multi-filter (such as @all) had no targets */
   320 	#define COMMAND_TARGET_NOT_HUMAN	-6			/**< Target was not human */
   321 	#define COMMAND_TARGET_AMBIGUOUS	-7			/**< Partial name had too many targets */
   322 
   323 	#define COMMAND_TARGETNAME_RAW		0			/**< Target name is a raw string */
   324 	#define COMMAND_TARGETNAME_ML		1			/**< Target name is a multi-lingual phrase */
   325 
   326 	/**
   327 	 * @brief Holds the many command target info parameters.
   328 	 */
   329 	struct cmd_target_info_t
   330 	{
   331 		const char *pattern;			/**< IN: Target pattern string. */
   332 		int admin;						/**< IN: Client admin index, or 0 if server .*/
   333 		cell_t *targets;				/**< IN: Array to store targets. */
   334 		cell_t max_targets;				/**< IN: Max targets (always >= 1) */
   335 		int flags;						/**< IN: COMMAND_FILTER flags. */
   336 		char *target_name;				/**< OUT: Buffer to store target name. */
   337 		size_t target_name_maxlength;	/**< IN: Maximum length of the target name buffer. */
   338 		int target_name_style;			/**< OUT: Target name style (COMMAND_TARGETNAME) */
   339 		int reason;						/**< OUT: COMMAND_TARGET reason. */
   340 		unsigned int num_targets;		/**< OUT: Number of targets. */
   341 	};
   342 
   343 	/**
   344 	 * @brief Intercepts a command target operation.
   345 	 */
   346 	class ICommandTargetProcessor
   347 	{
   348 	public:
   349 		/**
   350 		 * @brief Must process the command target and return a COMMAND_TARGET value.
   351 		 *
   352 		 * @param info			Struct containing command target information.
   353 		 *						Any members labelled OUT must be filled if processing 
   354 		 *						is to be completed (i.e. true returned).
   355 		 * @return				True to end processing, false to let Core continue.
   356 		 */
   357 		virtual bool ProcessCommandTarget(cmd_target_info_t *info) =0;
   358 	};
   359 
   360 	class IPlayerManager : public SMInterface
   361 	{
   362 	public:
   363 		const char *GetInterfaceName()
   364 		{
   365 			return SMINTERFACE_PLAYERMANAGER_NAME;
   366 		}
   367 		unsigned int GetInterfaceVersion()
   368 		{
   369 			return SMINTERFACE_PLAYERMANAGER_VERSION;
   370 		}
   371 	public:
   372 		/**
   373 		 * @brief Adds a client listener.
   374 		 *
   375 		 * @param listener		Pointer to an IClientListener.
   376 		 */
   377 		virtual void AddClientListener(IClientListener *listener) =0;
   378 
   379 		/**
   380 		 * @brief Removes a client listener.
   381 		 *
   382 		 * @param listener		Pointer to an IClientListener.
   383 		 */
   384 		virtual void RemoveClientListener(IClientListener *listener) =0;
   385 
   386 		/**
   387 		 * @brief Retrieves an IGamePlayer object by its client index.
   388 		 *
   389 		 * Note: This will return a valid object for any player, connected or not.
   390 		 * Note: Client indexes start at 1, not 0.
   391 		 *
   392 		 * @param client		Index of the client.
   393 		 * @return				An IGamePlayer pointer, or NULL if out of range.
   394 		 */
   395 		virtual IGamePlayer *GetGamePlayer(int client) =0;
   396 
   397 		/**
   398 		 * @brief Retrieves an IGamePlayer object by its edict_t pointer.
   399 		 *
   400 		 * @param pEdict		Index of the client
   401 		 * @return				An IGamePlayer pointer, or NULL if out of range.
   402 		 */
   403 		virtual IGamePlayer *GetGamePlayer(edict_t *pEdict) =0;
   404 
   405 		/**
   406 		 * @brief Returns the maximum number of clients.
   407 		 *
   408 		 * Note: this will not work until the server is activated.
   409 		 *
   410 		 * @return				Maximum number of clients.
   411 		 */
   412 		virtual int GetMaxClients() =0;
   413 
   414 		/**
   415 		 * @brief Returns the number of players currently connected.
   416 		 *
   417 		 * @return				Current number of connected clients.
   418 		 */
   419 		virtual int GetNumPlayers() =0;
   420 
   421 		/**
   422 		 * @brief Returns the client index by its userid.
   423 		 *
   424 		 * @param userid		Userid of the client.
   425 		 * @return				Client index, or 0 if invalid userid passed.
   426 		 */
   427 		virtual int GetClientOfUserId(int userid) =0;
   428 
   429 		/**
   430 		 * @brief Returns whether or not the server is activated.
   431 		 *
   432 		 * @return				True if ServerActivate() has been called
   433 		 *						at least once, false otherwise.
   434 		 */
   435 		virtual bool IsServerActivated() =0;
   436 
   437 		/**
   438 		 * @brief Gets SourceMod's reply source.
   439 		 *
   440 		 * @return				ReplyTo source.
   441 		 */
   442 		virtual unsigned int GetReplyTo() =0;
   443 
   444 		/**
   445 		 * @brief Sets SourceMod's reply source.
   446 		 *
   447 		 * @param reply			Reply source.
   448 		 * @return				Old reply source.
   449 		 */
   450 		virtual unsigned int SetReplyTo(unsigned int reply) =0;
   451 
   452 		/**
   453 		 * @brief Tests if a player meets command filtering rules.
   454 		 *
   455 		 * @param pAdmin		IGamePlayer of the admin, or NULL if the server. 
   456 		 * @param pTarget		IGamePlayer of the player being targeted.
   457 		 * @param flags			COMMAND_FILTER flags.
   458 		 * @return				COMMAND_TARGET value.
   459 		 */
   460 		virtual int FilterCommandTarget(IGamePlayer *pAdmin, IGamePlayer *pTarget, int flags) =0;
   461 
   462 		/**
   463 		 * @brief Registers a command target processor.
   464 		 *
   465 		 * @param pHandler		Pointer to an ICommandTargetProcessor instance.
   466 		 */
   467 		virtual void RegisterCommandTargetProcessor(ICommandTargetProcessor *pHandler) =0;
   468 
   469 		/**
   470 		 * @brief Removes a command target processor.
   471 		 *
   472 		 * @param pHandler		Pointer to an ICommandTargetProcessor instance.
   473 		 */
   474 		virtual void UnregisterCommandTargetProcessor(ICommandTargetProcessor *pHandler) =0;
   475 	};
   476 }
   477 
   478 #endif //_INCLUDE_SOURCEMOD_INTERFACE_IPLAYERHELPERS_H_