public/IPlayerHelpers.h
author GoD-Tony <aangione@gmail.com>
Mon Mar 18 19:53:30 2013 -0400 (2013-03-18)
changeset 3848 7d02e32ac0ae
parent 3825 ecd2382ca37e
child 3913 a73cf1f26984
permissions -rw-r--r--
Added logged error when PlayerRunCommand offset lookup fails (bug 5535, r=psychonic).
     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	17
    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 		 * @param		validated	Check backend validation status.
    80 		 * @return		String containing the player's auth string.
    81 		 *				May be NULL if unavailable.
    82 		 */
    83 		virtual const char *GetAuthString(bool validated = true) =0;
    84 
    85 		/**
    86 		 * @brief Returns the player's edict_t structure.
    87 		 *
    88 		 * @return		edict_t pointer, or NULL if unavailable.
    89 		 */
    90 		virtual edict_t *GetEdict() =0;
    91 
    92 		/**
    93 		 * @brief Returns whether the player is in game (putinserver).
    94 		 *
    95 		 * @return		True if in game, false otherwise.
    96 		 */
    97 		virtual bool IsInGame() =0;
    98 
    99 		/**
   100 		 * @brief Returns whether the player is connected.
   101 		 *
   102 		 * Note: If this returns true, all above functions except for
   103 		 * GetAuthString() should return non-NULL results.
   104 		 *
   105 		 * @return		True if connected, false otherwise.
   106 		 */
   107 		virtual bool IsConnected() =0;
   108 
   109 		/**
   110 		 * @brief Returns whether the player is a fake client.
   111 		 *
   112 		 * @return		True if a fake client, false otherwise.
   113 		 */
   114 		virtual bool IsFakeClient() =0;
   115 
   116 		/**
   117 		 * @brief Returns the client's AdminId, if any.
   118 		 *
   119 		 * @return		AdminId, or INVALID_ADMIN_ID if none.
   120 		 */
   121 		virtual AdminId GetAdminId() =0;
   122 
   123 		/**
   124 		 * @brief Sets the client's AdminId.
   125 		 *
   126 		 * @param id	AdminId to set.
   127 		 * @param temp	If true, the id will be invalidated on disconnect.
   128 		 */
   129 		virtual void SetAdminId(AdminId id, bool temp) =0;
   130 
   131 		/**
   132 		 * @brief Returns the client's userid.
   133 		 *
   134 		 * @return		Userid.
   135 		 */
   136 		virtual int GetUserId() =0;
   137 
   138 		/**
   139 		 * @brief Returns the client's language id.
   140 		 *
   141 		 * @return		Language id.
   142 		 */
   143 		virtual unsigned int GetLanguageId() =0;
   144 
   145 		/**
   146 		 * @brief Returns a player's IPlayerInfo object, if any.
   147 		 *
   148 		 * @return		IPlayerInfo pointer, or NULL if none.
   149 		 */
   150 		virtual IPlayerInfo *GetPlayerInfo() =0;
   151 
   152 		/**
   153 		 * @brief Runs through Core's admin authorization checks.  If the 
   154 		 * client is already an admin, no checks are performed.  
   155 		 *
   156 		 * Note that this function operates solely against the in-memory admin 
   157 		 * cache.  It will check steamids, IPs, names, and verify a password 
   158 		 * if one exists.  To implement other authentication schemes, simply 
   159 		 * don't call this function and use IGamePlayer::SetAdminId() instead.
   160 		 *
   161 		 * @return				True if access changed, false otherwise.
   162 		 */
   163 		virtual bool RunAdminCacheChecks() =0;
   164 
   165 		/**
   166 		 * @brief Notifies all listeners that the client has completed 
   167 		 * all of your post-connection (in-game, auth, admin) checks.  
   168 		 *
   169 		 * If you returned "false" from OnClientPreAdminCheck(), you must 
   170 		 * ALWAYS manually invoke this function, even if RunAdminCacheChecks() 
   171 		 * failed or you did not assign an AdminId.  Failure to call this 
   172 		 * function could result in plugins (such as reservedslots) not 
   173 		 * working properly.
   174 		 *
   175 		 * If you are implementing asynchronous fetches, and the client 
   176 		 * disconnects during your fetching process, you should make sure to 
   177 		 * recognize that case and not call this function.  That is, do not 
   178 		 * call this function on mismatched PreCheck calls, or on disconnected 
   179 		 * clients.  A good way to check this is to pass userids around, which 
   180 		 * are unique per client connection.
   181 		 *
   182 		 * Calling this has no effect if it has already been called on the 
   183 		 * given client (thus it is safe for multiple asynchronous plugins to 
   184 		 * call it at various times).
   185 		 */
   186 		virtual void NotifyPostAdminChecks() =0;
   187 
   188 		/**
   189 		 * @brief Returns the clients unique serial identifier.
   190 		 *
   191 		 * @return	Serial number.
   192 		 */
   193 		virtual unsigned int GetSerial() =0;
   194 
   195 		/**
   196 		 * @brief Return whether the client is authorized.
   197 		 *
   198 		 * @return		True if authorized, false otherwise.
   199 		 */
   200 		virtual bool IsAuthorized() =0;
   201 		
   202 		/**
   203 		 * @brief Kicks the client with a message
   204 		 *
   205 		 * @param message   The message shown to the client when kicked
   206 		 */
   207 		virtual void Kick(const char *message) =0;
   208 		
   209 		/**
   210 		 * @brief Returns whether the client is marked as being in the kick
   211 		 * queue. The client doesn't necessarily have to be in the actual kick
   212 		 * queue for this function to return true.
   213 		 *
   214 		 * @return      True if in the kick queue, false otherwise.
   215 		 */
   216 		virtual bool IsInKickQueue() =0;
   217 		
   218 		/**
   219 		 * @brief Marks the client as being in the kick queue. They are not
   220 		 * actually added to the kick queue. Use IGameHelpers::AddDelayedKick()
   221 		 * to actually add them to the queue.
   222 		 */
   223 		virtual void MarkAsBeingKicked() =0;
   224 
   225 		virtual void SetLanguageId(unsigned int id) =0;
   226 
   227 		/**
   228 		 * @brief Returns whether the player is the SourceTV bot.
   229 		 *
   230 		 * @return		True if the SourceTV bot, false otherwise.
   231 		 */
   232 		virtual bool IsSourceTV() const =0;
   233 		
   234 		/**
   235 		 * @brief Returns whether the player is the Replay bot.
   236 		 *
   237 		 * @return		True if the Replay bot, false otherwise.
   238 		 */
   239 		virtual bool IsReplay() const =0;
   240 		
   241 		/**
   242 		 * @brief Returns the client's Steam account ID.
   243 		 *
   244 		 * @param validated		Check backend validation status.
   245 		 * 
   246 		 * @return			Steam account ID or 0 if not available.
   247 		 */
   248 		virtual unsigned int GetSteamAccountID(bool validated = true) =0;
   249 	};
   250 
   251 	/**
   252 	 * @brief Provides callbacks for important client events.
   253 	 */
   254 	class IClientListener
   255 	{
   256 	public:
   257 	   /**
   258 		* @brief Returns the current client listener version.
   259 		*
   260 		* @return		Client listener version.
   261 		*/
   262 		virtual unsigned int GetClientListenerVersion()
   263 		{
   264 			return SMINTERFACE_PLAYERMANAGER_VERSION;
   265 		}
   266 	public:
   267 		/**
   268 		 * @brief Called when a client requests connection.
   269 		 *
   270 		 * @param client		Index of the client.
   271 		 * @param error			Error buffer for a disconnect reason.
   272 		 * @param maxlength		Maximum length of error buffer.
   273 		 * @return				True to allow client, false to reject.
   274 		 */
   275 		virtual bool InterceptClientConnect(int client, char *error, size_t maxlength)
   276 		{
   277 			return true;
   278 		}
   279 
   280 		/**
   281 		 * @brief Called when a client has connected.
   282 		 *
   283 		 * @param client		Index of the client.
   284 		 */
   285 		virtual void OnClientConnected(int client)
   286 		{
   287 		}
   288 
   289 		/**
   290 		 * @brief Called when a client is put in server.
   291 		 *
   292 		 * @param client		Index of the client.
   293 		 */
   294 		virtual void OnClientPutInServer(int client)
   295 		{
   296 		}
   297 
   298 		/**
   299 		 * @brief Called when a client is disconnecting (not fully disconnected yet).
   300 		 *
   301 		 * @param client		Index of the client.
   302 		 */
   303 		virtual void OnClientDisconnecting(int client)
   304 		{
   305 		}
   306 
   307 		/**
   308 		 * @brief Called when a client has fully disconnected.
   309 		 *
   310 		 * @param client		Index of the client.
   311 		 */
   312 		virtual void OnClientDisconnected(int client)
   313 		{
   314 		}
   315 
   316 		/**
   317 		 * @brief Called when a client has received authorization.
   318 		 *
   319 		 * @param client		Index of the client.
   320 		 * @param authstring	Authorization string.
   321 		 */
   322 		virtual void OnClientAuthorized(int client, const char *authstring)
   323 		{
   324 		}
   325 
   326 		/**
   327 		 * @brief Called when the server is activated.
   328 		 */
   329 		virtual void OnServerActivated(int max_clients)
   330 		{
   331 		}
   332 
   333 		/**
   334 		 * @brief Called once a client is authorized and fully in-game, but 
   335 		 * before admin checks are done.  This can be used to override the 
   336 		 * default admin checks for a client.
   337 		 *
   338 		 * By default, this function allows the authentication process to 
   339 		 * continue as normal.  If you need to delay the cache searching 
   340 		 * process in order to get asynchronous data, then return false here. 
   341 		 *
   342 		 * If you return false, you must call IPlayerManager::NotifyPostAdminCheck 
   343 		 * for the same client, or else the OnClientPostAdminCheck callback will 
   344 		 * never be called.
   345 		 *
   346 		 * @param client		Client index.
   347 		 * @return				True to continue normally, false to override 
   348 		 *						the authentication process.
   349 		 */
   350 		virtual bool OnClientPreAdminCheck(int client)
   351 		{
   352 			return true;
   353 		}
   354 
   355 		/**
   356 		 * @brief Called once a client is authorized and fully in-game, and 
   357 		 * after all post-connection authorizations have been passed.  If the 
   358 		 * client does not have an AdminId by this stage, it means that no 
   359 		 * admin entry was in the cache that matched, and the user could not 
   360 		 * be authenticated as an admin.
   361 		 *
   362 		 * @param client		Client index.
   363 		 */
   364 		virtual void OnClientPostAdminCheck(int client)
   365 		{
   366 		}
   367 
   368 		/**
   369 		* @brief Notifies the extension that the maxplayers value has changed
   370 		*
   371 		* @param newvalue			New maxplayers value.
   372 		*/
   373 		virtual void OnMaxPlayersChanged(int newvalue)
   374 		{
   375 		}
   376 
   377 		/**
   378 		* @brief Notifies the extension that a clients settings changed
   379 		*
   380 		* @param client			Client index.
   381 		*/
   382 		virtual void OnClientSettingsChanged(int client)
   383 		{
   384 		}
   385 	};
   386 
   387 	#define COMMAND_FILTER_ALIVE		(1<<0)		/**< Only allow alive players */
   388 	#define COMMAND_FILTER_DEAD			(1<<1)		/**< Only filter dead players */
   389 	#define COMMAND_FILTER_CONNECTED	(1<<2)		/**< Allow players not fully in-game */
   390 	#define COMMAND_FILTER_NO_IMMUNITY	(1<<3)		/**< Ignore immunity rules */
   391 	#define COMMAND_FILTER_NO_MULTI		(1<<4)		/**< Do not allow multiple target patterns */
   392 	#define COMMAND_FILTER_NO_BOTS		(1<<5)		/**< Do not allow bots to be targetted */
   393 
   394 	#define COMMAND_TARGET_VALID		1			/**< Client passed the filter */
   395 	#define COMMAND_TARGET_NONE			0			/**< No target was found */
   396 	#define COMMAND_TARGET_NOT_ALIVE	-1			/**< Single client is not alive */
   397 	#define COMMAND_TARGET_NOT_DEAD		-2			/**< Single client is not dead */
   398 	#define COMMAND_TARGET_NOT_IN_GAME	-3			/**< Single client is not in game */
   399 	#define COMMAND_TARGET_IMMUNE		-4			/**< Single client is immune */
   400 	#define COMMAND_TARGET_EMPTY_FILTER	-5			/**< A multi-filter (such as @all) had no targets */
   401 	#define COMMAND_TARGET_NOT_HUMAN	-6			/**< Target was not human */
   402 	#define COMMAND_TARGET_AMBIGUOUS	-7			/**< Partial name had too many targets */
   403 
   404 	#define COMMAND_TARGETNAME_RAW		0			/**< Target name is a raw string */
   405 	#define COMMAND_TARGETNAME_ML		1			/**< Target name is a multi-lingual phrase */
   406 
   407 	/**
   408 	 * @brief Holds the many command target info parameters.
   409 	 */
   410 	struct cmd_target_info_t
   411 	{
   412 		const char *pattern;			/**< IN: Target pattern string. */
   413 		int admin;						/**< IN: Client admin index, or 0 if server .*/
   414 		cell_t *targets;				/**< IN: Array to store targets. */
   415 		cell_t max_targets;				/**< IN: Max targets (always >= 1) */
   416 		int flags;						/**< IN: COMMAND_FILTER flags. */
   417 		char *target_name;				/**< OUT: Buffer to store target name. */
   418 		size_t target_name_maxlength;	/**< IN: Maximum length of the target name buffer. */
   419 		int target_name_style;			/**< OUT: Target name style (COMMAND_TARGETNAME) */
   420 		int reason;						/**< OUT: COMMAND_TARGET reason. */
   421 		unsigned int num_targets;		/**< OUT: Number of targets. */
   422 	};
   423 
   424 	/**
   425 	 * @brief Intercepts a command target operation.
   426 	 */
   427 	class ICommandTargetProcessor
   428 	{
   429 	public:
   430 		/**
   431 		 * @brief Must process the command target and return a COMMAND_TARGET value.
   432 		 *
   433 		 * @param info			Struct containing command target information.
   434 		 *						Any members labelled OUT must be filled if processing 
   435 		 *						is to be completed (i.e. true returned).
   436 		 * @return				True to end processing, false to let Core continue.
   437 		 */
   438 		virtual bool ProcessCommandTarget(cmd_target_info_t *info) =0;
   439 	};
   440 
   441 	class IPlayerManager : public SMInterface
   442 	{
   443 	public:
   444 		const char *GetInterfaceName()
   445 		{
   446 			return SMINTERFACE_PLAYERMANAGER_NAME;
   447 		}
   448 		unsigned int GetInterfaceVersion()
   449 		{
   450 			return SMINTERFACE_PLAYERMANAGER_VERSION;
   451 		}
   452 	public:
   453 		/**
   454 		 * @brief Adds a client listener.
   455 		 *
   456 		 * @param listener		Pointer to an IClientListener.
   457 		 */
   458 		virtual void AddClientListener(IClientListener *listener) =0;
   459 
   460 		/**
   461 		 * @brief Removes a client listener.
   462 		 *
   463 		 * @param listener		Pointer to an IClientListener.
   464 		 */
   465 		virtual void RemoveClientListener(IClientListener *listener) =0;
   466 
   467 		/**
   468 		 * @brief Retrieves an IGamePlayer object by its client index.
   469 		 *
   470 		 * Note: This will return a valid object for any player, connected or not.
   471 		 * Note: Client indexes start at 1, not 0.
   472 		 *
   473 		 * @param client		Index of the client.
   474 		 * @return				An IGamePlayer pointer, or NULL if out of range.
   475 		 */
   476 		virtual IGamePlayer *GetGamePlayer(int client) =0;
   477 
   478 		/**
   479 		 * @brief Retrieves an IGamePlayer object by its edict_t pointer.
   480 		 *
   481 		 * @param pEdict		Index of the client
   482 		 * @return				An IGamePlayer pointer, or NULL if out of range.
   483 		 */
   484 		virtual IGamePlayer *GetGamePlayer(edict_t *pEdict) =0;
   485 
   486 		/**
   487 		 * @brief Returns the maximum number of clients.
   488 		 *
   489 		 * Note: this will not work until the server is activated.
   490 		 *
   491 		 * @return				Maximum number of clients.
   492 		 */
   493 		virtual int GetMaxClients() =0;
   494 
   495 		/**
   496 		 * @brief Returns the number of players currently connected.
   497 		 *
   498 		 * @return				Current number of connected clients.
   499 		 */
   500 		virtual int GetNumPlayers() =0;
   501 
   502 		/**
   503 		 * @brief Returns the client index by its userid.
   504 		 *
   505 		 * @param userid		Userid of the client.
   506 		 * @return				Client index, or 0 if invalid userid passed.
   507 		 */
   508 		virtual int GetClientOfUserId(int userid) =0;
   509 
   510 		/**
   511 		 * @brief Returns whether or not the server is activated.
   512 		 *
   513 		 * @return				True if ServerActivate() has been called
   514 		 *						at least once, false otherwise.
   515 		 */
   516 		virtual bool IsServerActivated() =0;
   517 
   518 		/**
   519 		 * @brief Gets SourceMod's reply source.
   520 		 *
   521 		 * @return				ReplyTo source.
   522 		 */
   523 		virtual unsigned int GetReplyTo() =0;
   524 
   525 		/**
   526 		 * @brief Sets SourceMod's reply source.
   527 		 *
   528 		 * @param reply			Reply source.
   529 		 * @return				Old reply source.
   530 		 */
   531 		virtual unsigned int SetReplyTo(unsigned int reply) =0;
   532 
   533 		/**
   534 		 * @brief Tests if a player meets command filtering rules.
   535 		 *
   536 		 * @param pAdmin		IGamePlayer of the admin, or NULL if the server. 
   537 		 * @param pTarget		IGamePlayer of the player being targeted.
   538 		 * @param flags			COMMAND_FILTER flags.
   539 		 * @return				COMMAND_TARGET value.
   540 		 */
   541 		virtual int FilterCommandTarget(IGamePlayer *pAdmin, IGamePlayer *pTarget, int flags) =0;
   542 
   543 		/**
   544 		 * @brief Registers a command target processor.
   545 		 *
   546 		 * @param pHandler		Pointer to an ICommandTargetProcessor instance.
   547 		 */
   548 		virtual void RegisterCommandTargetProcessor(ICommandTargetProcessor *pHandler) =0;
   549 
   550 		/**
   551 		 * @brief Removes a command target processor.
   552 		 *
   553 		 * @param pHandler		Pointer to an ICommandTargetProcessor instance.
   554 		 */
   555 		virtual void UnregisterCommandTargetProcessor(ICommandTargetProcessor *pHandler) =0;
   556 
   557 		/**
   558 		 * @brief Returns the client index by its serial number.
   559 		 *
   560 		 * @return				Client index, or 0 for invalid serial.
   561 		 */
   562 		virtual int GetClientFromSerial(unsigned int serial) =0;
   563 
   564 		/**
   565 		 * @brief Processes the pattern inside a cmd_target_info_t structure
   566 		 * and outputs the clients that match it.
   567 		 *
   568 		 * @param info			Pointer to the cmd_target_info_t structure to process.
   569 		 */
   570 		virtual void ProcessCommandTarget(cmd_target_info_t *info) =0;
   571 	};
   572 }
   573 
   574 #endif //_INCLUDE_SOURCEMOD_INTERFACE_IPLAYERHELPERS_H_