3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
32 #ifndef _INCLUDE_SOURCEMOD_INTERFACE_IPLAYERHELPERS_H_
33 #define _INCLUDE_SOURCEMOD_INTERFACE_IPLAYERHELPERS_H_
36 * @file IPlayerHelpers.h
37 * @brief Defines basic helper functions for Half-Life 2 clients
40 #include <IShareSys.h>
41 #include <IAdminSystem.h>
43 #define SMINTERFACE_PLAYERMANAGER_NAME "IPlayerManager"
44 #define SMINTERFACE_PLAYERMANAGER_VERSION 14
49 #define SM_REPLY_CONSOLE 0 /**< Reply to console. */
50 #define SM_REPLY_CHAT 1 /**< Reply to chat. */
55 * @brief Abstracts some Half-Life 2 and SourceMod properties about clients.
61 * @brief Returns the player's name.
63 * @return String containing the player's name,
64 * or NULL if unavailable.
66 virtual const char *GetName() =0;
69 * @brief Returns the player's IP address.
71 * @return String containing the player's IP address,
72 * or NULL if unavailable.
74 virtual const char *GetIPAddress() =0;
77 * @brief Returns the player's authentication string.
79 * @return String containing the player's auth string.
80 * May be NULL if unavailable.
82 virtual const char *GetAuthString() =0;
85 * @brief Returns the player's edict_t structure.
87 * @return edict_t pointer, or NULL if unavailable.
89 virtual edict_t *GetEdict() =0;
92 * @brief Returns whether the player is in game (putinserver).
94 * @return True if in game, false otherwise.
96 virtual bool IsInGame() =0;
99 * @brief Returns whether the player is connected.
101 * Note: If this returns true, all above functions except for
102 * GetAuthString() should return non-NULL results.
104 * @return True if connected, false otherwise.
106 virtual bool IsConnected() =0;
109 * @brief Returns whether the player is a fake client.
111 * @return True if a fake client, false otherwise.
113 virtual bool IsFakeClient() =0;
116 * @brief Returns the client's AdminId, if any.
118 * @return AdminId, or INVALID_ADMIN_ID if none.
120 virtual AdminId GetAdminId() =0;
123 * @brief Sets the client's AdminId.
125 * @param id AdminId to set.
126 * @param temp If true, the id will be invalidated on disconnect.
128 virtual void SetAdminId(AdminId id, bool temp) =0;
131 * @brief Returns the client's userid.
135 virtual int GetUserId() =0;
138 * @brief Returns the client's language id.
140 * @return Language id.
142 virtual unsigned int GetLanguageId() =0;
145 * @brief Returns a player's IPlayerInfo object, if any.
147 * @return IPlayerInfo pointer, or NULL if none.
149 virtual IPlayerInfo *GetPlayerInfo() =0;
152 * @brief Runs through Core's admin authorization checks. If the
153 * client is already an admin, no checks are performed.
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.
160 * @return True if access changed, false otherwise.
162 virtual bool RunAdminCacheChecks() =0;
165 * @brief Notifies all listeners that the client has completed
166 * all of your post-connection (in-game, auth, admin) checks.
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
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.
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).
185 virtual void NotifyPostAdminChecks() =0;
188 * @brief Returns the clients unique serial identifier.
190 * @return Serial number.
192 virtual unsigned int GetSerial() =0;
195 * @brief Return whether the client is authorized.
197 * @return True if authorized, false otherwise.
199 virtual bool IsAuthorized() =0;
202 * @brief Kicks the client with a message
204 * @param message The message shown to the client when kicked
206 virtual void Kick(const char *message) =0;
209 * @brief Returns whether the client is marked as being in the kick
210 * queue. The client doesn't necessarily have to be in the actual kick
211 * queue for this function to return true.
213 * @return True if in the kick queue, false otherwise.
215 virtual bool IsInKickQueue() =0;
218 * @brief Marks the client as being in the kick queue. They are not
219 * actually added to the kick queue. Use IGameHelpers::AddDelayedKick()
220 * to actually add them to the queue.
222 virtual void MarkAsBeingKicked() =0;
224 virtual void SetLanguageId(unsigned int id) =0;
228 * @brief Provides callbacks for important client events.
230 class IClientListener
234 * @brief Returns the current client listener version.
236 * @return Client listener version.
238 virtual unsigned int GetClientListenerVersion()
240 return SMINTERFACE_PLAYERMANAGER_VERSION;
244 * @brief Called when a client requests connection.
246 * @param client Index of the client.
247 * @param error Error buffer for a disconnect reason.
248 * @param maxlength Maximum length of error buffer.
249 * @return True to allow client, false to reject.
251 virtual bool InterceptClientConnect(int client, char *error, size_t maxlength)
257 * @brief Called when a client has connected.
259 * @param client Index of the client.
261 virtual void OnClientConnected(int client)
266 * @brief Called when a client is put in server.
268 * @param client Index of the client.
270 virtual void OnClientPutInServer(int client)
275 * @brief Called when a client is disconnecting (not fully disconnected yet).
277 * @param client Index of the client.
279 virtual void OnClientDisconnecting(int client)
284 * @brief Called when a client has fully disconnected.
286 * @param client Index of the client.
288 virtual void OnClientDisconnected(int client)
293 * @brief Called when a client has received authorization.
295 * @param client Index of the client.
296 * @param authstring Authorization string.
298 virtual void OnClientAuthorized(int client, const char *authstring)
303 * @brief Called when the server is activated.
305 virtual void OnServerActivated(int max_clients)
310 * @brief Called once a client is authorized and fully in-game, but
311 * before admin checks are done. This can be used to override the
312 * default admin checks for a client.
314 * By default, this function allows the authentication process to
315 * continue as normal. If you need to delay the cache searching
316 * process in order to get asynchronous data, then return false here.
318 * If you return false, you must call IPlayerManager::NotifyPostAdminCheck
319 * for the same client, or else the OnClientPostAdminCheck callback will
322 * @param client Client index.
323 * @return True to continue normally, false to override
324 * the authentication process.
326 virtual bool OnClientPreAdminCheck(int client)
332 * @brief Called once a client is authorized and fully in-game, and
333 * after all post-connection authorizations have been passed. If the
334 * client does not have an AdminId by this stage, it means that no
335 * admin entry was in the cache that matched, and the user could not
336 * be authenticated as an admin.
338 * @param client Client index.
340 virtual void OnClientPostAdminCheck(int client)
345 * @brief Notifies the extension that the maxplayers value has changed
347 * @param newvalue New maxplayers value.
349 virtual void OnMaxPlayersChanged(int newvalue)
354 * @brief Notifies the extension that a clients settings changed
356 * @param client Client index.
358 virtual void OnClientSettingsChanged(int client)
363 #define COMMAND_FILTER_ALIVE (1<<0) /**< Only allow alive players */
364 #define COMMAND_FILTER_DEAD (1<<1) /**< Only filter dead players */
365 #define COMMAND_FILTER_CONNECTED (1<<2) /**< Allow players not fully in-game */
366 #define COMMAND_FILTER_NO_IMMUNITY (1<<3) /**< Ignore immunity rules */
367 #define COMMAND_FILTER_NO_MULTI (1<<4) /**< Do not allow multiple target patterns */
368 #define COMMAND_FILTER_NO_BOTS (1<<5) /**< Do not allow bots to be targetted */
370 #define COMMAND_TARGET_VALID 1 /**< Client passed the filter */
371 #define COMMAND_TARGET_NONE 0 /**< No target was found */
372 #define COMMAND_TARGET_NOT_ALIVE -1 /**< Single client is not alive */
373 #define COMMAND_TARGET_NOT_DEAD -2 /**< Single client is not dead */
374 #define COMMAND_TARGET_NOT_IN_GAME -3 /**< Single client is not in game */
375 #define COMMAND_TARGET_IMMUNE -4 /**< Single client is immune */
376 #define COMMAND_TARGET_EMPTY_FILTER -5 /**< A multi-filter (such as @all) had no targets */
377 #define COMMAND_TARGET_NOT_HUMAN -6 /**< Target was not human */
378 #define COMMAND_TARGET_AMBIGUOUS -7 /**< Partial name had too many targets */
380 #define COMMAND_TARGETNAME_RAW 0 /**< Target name is a raw string */
381 #define COMMAND_TARGETNAME_ML 1 /**< Target name is a multi-lingual phrase */
384 * @brief Holds the many command target info parameters.
386 struct cmd_target_info_t
388 const char *pattern; /**< IN: Target pattern string. */
389 int admin; /**< IN: Client admin index, or 0 if server .*/
390 cell_t *targets; /**< IN: Array to store targets. */
391 cell_t max_targets; /**< IN: Max targets (always >= 1) */
392 int flags; /**< IN: COMMAND_FILTER flags. */
393 char *target_name; /**< OUT: Buffer to store target name. */
394 size_t target_name_maxlength; /**< IN: Maximum length of the target name buffer. */
395 int target_name_style; /**< OUT: Target name style (COMMAND_TARGETNAME) */
396 int reason; /**< OUT: COMMAND_TARGET reason. */
397 unsigned int num_targets; /**< OUT: Number of targets. */
401 * @brief Intercepts a command target operation.
403 class ICommandTargetProcessor
407 * @brief Must process the command target and return a COMMAND_TARGET value.
409 * @param info Struct containing command target information.
410 * Any members labelled OUT must be filled if processing
411 * is to be completed (i.e. true returned).
412 * @return True to end processing, false to let Core continue.
414 virtual bool ProcessCommandTarget(cmd_target_info_t *info) =0;
417 class IPlayerManager : public SMInterface
420 const char *GetInterfaceName()
422 return SMINTERFACE_PLAYERMANAGER_NAME;
424 unsigned int GetInterfaceVersion()
426 return SMINTERFACE_PLAYERMANAGER_VERSION;
430 * @brief Adds a client listener.
432 * @param listener Pointer to an IClientListener.
434 virtual void AddClientListener(IClientListener *listener) =0;
437 * @brief Removes a client listener.
439 * @param listener Pointer to an IClientListener.
441 virtual void RemoveClientListener(IClientListener *listener) =0;
444 * @brief Retrieves an IGamePlayer object by its client index.
446 * Note: This will return a valid object for any player, connected or not.
447 * Note: Client indexes start at 1, not 0.
449 * @param client Index of the client.
450 * @return An IGamePlayer pointer, or NULL if out of range.
452 virtual IGamePlayer *GetGamePlayer(int client) =0;
455 * @brief Retrieves an IGamePlayer object by its edict_t pointer.
457 * @param pEdict Index of the client
458 * @return An IGamePlayer pointer, or NULL if out of range.
460 virtual IGamePlayer *GetGamePlayer(edict_t *pEdict) =0;
463 * @brief Returns the maximum number of clients.
465 * Note: this will not work until the server is activated.
467 * @return Maximum number of clients.
469 virtual int GetMaxClients() =0;
472 * @brief Returns the number of players currently connected.
474 * @return Current number of connected clients.
476 virtual int GetNumPlayers() =0;
479 * @brief Returns the client index by its userid.
481 * @param userid Userid of the client.
482 * @return Client index, or 0 if invalid userid passed.
484 virtual int GetClientOfUserId(int userid) =0;
487 * @brief Returns whether or not the server is activated.
489 * @return True if ServerActivate() has been called
490 * at least once, false otherwise.
492 virtual bool IsServerActivated() =0;
495 * @brief Gets SourceMod's reply source.
497 * @return ReplyTo source.
499 virtual unsigned int GetReplyTo() =0;
502 * @brief Sets SourceMod's reply source.
504 * @param reply Reply source.
505 * @return Old reply source.
507 virtual unsigned int SetReplyTo(unsigned int reply) =0;
510 * @brief Tests if a player meets command filtering rules.
512 * @param pAdmin IGamePlayer of the admin, or NULL if the server.
513 * @param pTarget IGamePlayer of the player being targeted.
514 * @param flags COMMAND_FILTER flags.
515 * @return COMMAND_TARGET value.
517 virtual int FilterCommandTarget(IGamePlayer *pAdmin, IGamePlayer *pTarget, int flags) =0;
520 * @brief Registers a command target processor.
522 * @param pHandler Pointer to an ICommandTargetProcessor instance.
524 virtual void RegisterCommandTargetProcessor(ICommandTargetProcessor *pHandler) =0;
527 * @brief Removes a command target processor.
529 * @param pHandler Pointer to an ICommandTargetProcessor instance.
531 virtual void UnregisterCommandTargetProcessor(ICommandTargetProcessor *pHandler) =0;
534 * @brief Returns the client index by its serial number.
536 * @return Client index, or 0 for invalid serial.
538 virtual int GetClientFromSerial(unsigned int serial) =0;
541 * @brief Processes the pattern inside a cmd_target_info_t structure
542 * and outputs the clients that match it.
544 * @param info Pointer to the cmd_target_info_t structure to process.
546 virtual void ProcessCommandTarget(cmd_target_info_t *info) =0;
550 #endif //_INCLUDE_SOURCEMOD_INTERFACE_IPLAYERHELPERS_H_