plugins/include/admin.inc
author Scott Ehlert <ds@alliedmods.net>
Tue Mar 06 04:50:30 2007 +0000 (2007-03-06)
changeset 577 39a746736a0f
parent 540 27a380082863
child 619 6934bf39171e
permissions -rw-r--r--
Creating/Firing/Canceling events now use CStack to avoid the need to use new when possible
     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 _admin_included
    17  #endinput
    18 #endif
    19 #define _admin_included
    20 
    21 /**
    22  * Access levels (flags) for admins.
    23  */
    24 enum AdminFlag
    25 {
    26 	Admin_Reservation = 0,	/**< Reserved slot */
    27 	Admin_Generic,			/**< Generic admin abilities */
    28 	Admin_Kick,				/**< Kick another user */
    29 	Admin_Ban,				/**< Ban another user */
    30 	Admin_Unban,			/**< Unban another user */
    31 	Admin_Slay,				/**< Slay/kill/damage another user */
    32 	Admin_Changemap,		/**< Change the map */
    33 	Admin_Convars,			/**< Change basic convars */
    34 	Admin_Config,			/**< Change configuration */
    35 	Admin_Chat,				/**< Special chat privileges */
    36 	Admin_Vote,				/**< Special vote privileges */
    37 	Admin_Password,			/**< Set a server password */
    38 	Admin_RCON,				/**< Use RCON */
    39 	Admin_Cheats,			/**< Change sv_cheats and use its commands */
    40 	Admin_Root,				/**< All access by default */
    41 	Admin_Custom1,			/**< First custom flag type */
    42 	Admin_Custom2,			/**< Second custom flag type */
    43 	Admin_Custom3,			/**< Third custom flag type */
    44 	Admin_Custom4,			/**< Fourth custom flag type */
    45 	Admin_Custom5,			/**< Fifth custom flag type */
    46 	Admin_Custom6,			/**< Sixth custom flag type */
    47 	/* --- */
    48 };
    49 
    50 #define AdminFlags_TOTAL	21
    51 
    52 /**
    53  * These define bitwise values for bitstrings (numbers containing bitwise flags).
    54  */
    55 #define ADMFLAG_RESERVATION			(1<<0)		/**< Convenience macro for Admin_Reservation as a FlagBit */
    56 #define ADMFLAG_GENERIC				(1<<1)		/**< Convenience macro for Admin_Generic as a FlagBit */
    57 #define ADMFLAG_KICK				(1<<2)		/**< Convenience macro for Admin_Kick as a FlagBit */
    58 #define ADMFLAG_BAN					(1<<3)		/**< Convenience macro for Admin_Ban as a FlagBit */
    59 #define ADMFLAG_UNBAN				(1<<4)		/**< Convenience macro for Admin_Unban as a FlagBit */
    60 #define ADMFLAG_SLAY				(1<<5)		/**< Convenience macro for Admin_Slay as a FlagBit */
    61 #define ADMFLAG_CHANGEMAP			(1<<6)		/**< Convenience macro for Admin_Changemap as a FlagBit */
    62 #define ADMFLAG_CONVARS				(1<<7)		/**< Convenience macro for Admin_Convars as a FlagBit */
    63 #define ADMFLAG_CONFIG				(1<<8)		/**< Convenience macro for Admin_Config as a FlagBit */
    64 #define ADMFLAG_CHAT				(1<<9)		/**< Convenience macro for Admin_Chat as a FlagBit */
    65 #define ADMFLAG_VOTE				(1<<10)		/**< Convenience macro for Admin_Vote as a FlagBit */
    66 #define ADMFLAG_PASSWORD			(1<<11)		/**< Convenience macro for Admin_Password as a FlagBit */
    67 #define ADMFLAG_RCON				(1<<12)		/**< Convenience macro for Admin_RCON as a FlagBit */
    68 #define ADMFLAG_CHEATS				(1<<13)		/**< Convenience macro for Admin_Cheats as a FlagBit */
    69 #define ADMFLAG_ROOT				(1<<14)		/**< Convenience macro for Admin_Root as a FlagBit */
    70 #define ADMFLAG_CUSTOM1				(1<<15)		/**< Convenience macro for Admin_Custom1 as a FlagBit */
    71 #define ADMFLAG_CUSTOM2				(1<<16)		/**< Convenience macro for Admin_Custom2 as a FlagBit */
    72 #define ADMFLAG_CUSTOM3				(1<<17)		/**< Convenience macro for Admin_Custom3 as a FlagBit */
    73 #define ADMFLAG_CUSTOM4				(1<<18)		/**< Convenience macro for Admin_Custom4 as a FlagBit */
    74 #define ADMFLAG_CUSTOM5				(1<<19)		/**< Convenience macro for Admin_Custom5 as a FlagBit */
    75 #define ADMFLAG_CUSTOM6				(1<<20)		/**< Convenience macro for Admin_Custom6 as a FlagBit */
    76 
    77 #define AUTHMETHOD_STEAM			"steam"
    78 #define AUTHMETHOD_IP				"ip"
    79 #define AUTHMETHOD_NAME				"name"
    80 
    81 enum OverrideType
    82 {
    83 	Override_Command = 1,	/* Command */
    84 	Override_CommandGroup,	/* Command group */
    85 };
    86 
    87 enum OverrideRule
    88 {
    89 	Command_Deny = 0,
    90 	Command_Allow = 1,
    91 };
    92 
    93 enum ImmunityType
    94 {
    95 	Immunity_Default = 1,	/* Immune from everyone with no immunity */
    96 	Immunity_Global,		/* Immune from everyone (except root admins) */
    97 };
    98 
    99 /** Identifies a unique entry in the group permissions cache.  These are not Handles. */
   100 enum GroupId
   101 {
   102 	INVALID_GROUP_ID = -1,
   103 };
   104 
   105 /** Identifies a unique entry in the admin permissions cache.  These are not Handles. */
   106 enum AdminId
   107 {
   108 	INVALID_ADMIN_ID = -1,
   109 };
   110 
   111 /** Defines user access modes. */
   112 enum AdmAccessMode
   113 {
   114 	Access_Real,		/**< Access the user has inherently */
   115 	Access_Effective,	/**< Access the user has from their groups */
   116 };
   117 
   118 /**
   119  * @brief Represents the various cache regions.
   120  */
   121 enum AdminCachePart
   122 {
   123 	AdminCache_Overrides = 0,		/**< Global overrides */
   124 	AdminCache_Groups = 1,			/**< All groups (automatically invalidates admins too) */
   125 	AdminCache_Admins = 2,			/**< All admins */
   126 };
   127 
   128 /**
   129  * Called when part of the cache which needs to be rebuilt.
   130  *
   131  * @param part			Part of the admin cache to rebuild.
   132  */
   133 forward OnRebuildAdminCache(AdminCachePart:part);
   134 
   135 /**
   136  * Tells the admin system to dump a portion of the cache.
   137  *
   138  * @param part			Part of the cache to dump.  Specifying groups also dumps admins.
   139  * @param rebuild		If true, the rebuild forwards will fire.
   140  * @noreturn
   141  */
   142 native DumpAdminCache(AdminCachePart:part, bool:rebuild);
   143 
   144 /**
   145  * Adds a global command flag override.  Any command registered with this name
   146  * will assume the new flag.  This is applied retroactively as well.
   147  *
   148  * @param cmd			String containing command name (case sensitive).
   149  * @param type			Override type (specific command or group).
   150  * @param flags			New admin flag.
   151  * @noreturn
   152  */
   153 native AddCommandOverride(const String:cmd[], OverrideType:type, flags);
   154 
   155 /**
   156  * Returns a command override.
   157  *
   158  * @param cmd			String containing command name (case sensitive).
   159  * @param type			Override type (specific command or group).
   160  * @param flags			By-reference cell to store the flag (undefined if not found).
   161  * @return				True if there is an override, false otherwise.
   162  */
   163 native bool:GetCommandOverride(const String:cmd[], OverrideType:type, &flags);
   164 
   165 /**
   166  * Unsets a command override.
   167  *
   168  * @param cmd			String containing command name (case sensitive).
   169  * @param type			Override type (specific command or group).
   170  * @noreturn
   171  */
   172 native UnsetCommandOverride(const String:cmd[], OverrideType:type);
   173 
   174 /**
   175  * Adds a new group.  Name must be unique.
   176  *
   177  * @param group_name	String containing the group name.
   178  * @return				A new group id, INVALID_GROUP_ID if it already exists.
   179  */
   180 native GroupId:CreateAdmGroup(const String:group_name[]);
   181 
   182 /**
   183  * @brief Finds a group by name.
   184  *
   185  * @param group_name	String containing the group name.
   186  * @return				A group id, or INVALID_GROUP_ID if not found.
   187  */
   188 native GroupId:FindAdmGroup(const String:group_name[]);
   189 
   190 /**
   191  * Adds or removes a flag from a group's flag set.
   192  * @note These are called "add flags" because they add to a user's flags.
   193  *
   194  * @param id			Group id.
   195  * @param flag			Admin flag to toggle.
   196  * @param enabled		True to set the flag, false to unset/disable.
   197  * @noreturn
   198  */
   199 native SetAdmGroupAddFlag(GroupId:id, AdminFlag:flag, bool:enabled);
   200 
   201 /**
   202  * Gets the set value of an add flag on a group's flag set.
   203  * @note These are called "add flags" because they add to a user's flags.
   204  *
   205  * @param id			Group id.
   206  * @param flag			Admin flag to retrieve.
   207  * @return				True if enabled, false otherwise,
   208  */
   209 native bool:GetAdmGroupAddFlag(GroupId:id, AdminFlag:flag);
   210 
   211 /**
   212  * Returns the flag set that is added to a user from their group.
   213  * @note These are called "add flags" because they add to a user's flags.
   214  *
   215  * @param id			GroupId of the group.
   216  * @return				Bitstring containing the flags enabled.
   217  */
   218 native GetAdmGroupAddFlags(GroupId:id);
   219 
   220 /**
   221  * Toggles a generic immunity type.
   222  *
   223  * @param id			Group id.
   224  * @param type			Generic immunity type.
   225  * @param enabled		True to enable, false otherwise.
   226  * @noreturn
   227  */
   228 native SetAdmGroupImmunity(GroupId:id, ImmunityType:type, bool:enabled);
   229 
   230 /**
   231  * Returns whether or not a group has global immunity.
   232  *
   233  * @param id			Group id.
   234  * @param type			Generic immunity type.
   235  * @return				True if the group has this immunity, false otherwise.
   236  */
   237 native bool:GetAdmGroupImmunity(GroupId:id, ImmunityType:type);
   238 
   239 /**
   240  * Adds immunity to a specific group.
   241  *
   242  * @param id			Group id.
   243  * @param other_id		Group id to receive immunity to.
   244  * @noreturn
   245  */
   246 native SetAdmGroupImmuneFrom(GroupId:id, GroupId:other_id);
   247 
   248 /**
   249  * Returns the number of specific group immunities.
   250  *
   251  * @param id			Group id.
   252  * @return				Number of group immunities.
   253  */
   254 native GetAdmGroupImmuneCount(GroupId:id);
   255 
   256 /**
   257  * Returns a group that this group is immune to given an index.
   258  *
   259  * @param id			Group id.
   260  * @param number		Index from 0 to N-1, from GetAdmGroupImmuneCount().
   261  * @return				GroupId that this group is immune to, or INVALID_GROUP_ID on failure.
   262  */
   263 native GroupId:GetAdmGroupImmuneFrom(GroupId:id, number); 
   264 
   265 /**
   266  * Adds a group-specific override type.
   267  *
   268  * @param id			Group id.
   269  * @param name			String containing command name (case sensitive).
   270  * @param type			Override type (specific command or group).
   271  * @param rule			Override allow/deny setting.
   272  * @noreturn
   273  */
   274 native AddAdmGroupCmdOverride(GroupId:id, const String:name[], OverrideType:type, OverrideRule:rule);
   275 
   276 /**
   277  * Retrieves a group-specific command override.
   278  *
   279  * @param id			Group id.
   280  * @param name			String containing command name (case sensitive).
   281  * @param type			Override type (specific command or group).
   282  * @param rule			Optional pointer to store allow/deny setting.
   283  * @return				True if an override exists, false otherwise.
   284  */
   285 native bool:GetAdmGroupCmdOverride(GroupId:id, const String:name[], OverrideType:type, &OverrideRule:rule);
   286 
   287 /**
   288  * Registers an authentication identity type.  You normally never need to call this except for 
   289  * very specific systems.
   290  *
   291  * @param codename		Codename to use for your authentication type.
   292  * @noreturn
   293  */
   294 native RegisterAuthIdentType(const String:name[]);
   295 
   296 /**
   297  * Creates a new admin entry in the permissions cache.
   298  *
   299  * @param name			Name for this entry (does not have to be unique).
   300  * 						Specify an empty string for an anonymous admin.
   301  */
   302 native AdminId:CreateAdmin(const String:name[]="");
   303 
   304 /**
   305  * Retrieves an admin's user name as made with CreateAdmin().
   306  *
   307  * NOTE: This function can return UTF-8 strings, and will safely chop UTF-8 strings.
   308  *
   309  * @param id			AdminId of the admin.
   310  * @param name			String buffer to store name.
   311  * @param maxlength		Maximum size of string buffer.
   312  * @return				Number of bytes written.
   313  */
   314 native GetAdminUsername(AdminId:id, const String:name[], maxlength);
   315 
   316 /**
   317  * Binds an admin to an identity for fast lookup later on.  The bind must be unique.
   318  *
   319  * @param id			AdminId of the admin.
   320  * @param auth			Auth method to use, predefined or from RegisterAuthIdentType().
   321  * @param ident			String containing the arbitrary, unique identity.
   322  * @return				True on success, false if the auth method was not found,
   323  *						or ident was already taken.
   324  */
   325 native bool:BindAdminIdentity(AdminId:id, const String:auth[], const String:ident[]);
   326 
   327 /**
   328  * Sets whether or not a flag is enabled on an admin.
   329  *
   330  * @param id			AdminId index of the admin.
   331  * @param flag			Admin flag to use.
   332  * @param enabled		True to enable, false to disable.
   333  * @noreturn
   334  */
   335 native SetAdminFlag(AdminId:id, AdminFlag:flag, bool:enabled);
   336 
   337 /**
   338  * Returns whether or not a flag is enabled on an admin.
   339  *
   340  * @param id			AdminId index of the admin.
   341  * @param flag			Admin flag to use.
   342  * @param mode			Access mode to check.
   343  * @return				True if enabled, false otherwise.
   344  */
   345 native bool:GetAdminFlag(AdminId:id, AdminFlag:flag, AdmAccessMode:mode);
   346 
   347 /**
   348  * Returns the bitstring of access flags on an admin.
   349  *
   350  * @param id			AdminId index of the admin.
   351  * @param mode			Access mode to use.
   352  * @return				A bitstring containing which flags are enabled.
   353  */
   354 native GetAdminFlags(AdminId:id, AdmAccessMode:mode);
   355 
   356 /**
   357  * Adds a group to an admin's inherited group list.  Any flags the group has 
   358  * will be added to the admin's effective flags.
   359  *
   360  * @param id			AdminId index of the admin.
   361  * @param gid			GroupId index of the group.
   362  * @return				True on success, false on invalid input or duplicate membership.
   363  */
   364 native bool:AdminInheritGroup(AdminId:id, GroupId:gid);
   365 
   366 /**
   367  * Returns the number of groups this admin is a member of.
   368  * 
   369  * @param id			AdminId index of the admin.
   370  * @return				Number of groups this admin is a member of.
   371  */
   372 native GetAdminGroupCount(AdminId:id);
   373 
   374 /**
   375  * Returns group information from an admin.
   376  *
   377  * @param id        	AdminId index of the admin.
   378  * @param index			Group number to retrieve, from 0 to N-1, where N
   379  *						is the value of GetAdminGroupCount(id).
   380  * @param name			Buffer to store the group's name.
   381  *						Note: This will safely chop UTF-8 strings.
   382  * @param maxlength		Maximum size of the output name buffer.
   383  * @return				A GroupId index and a name pointer, or
   384  *						INVALID_GROUP_ID and NULL if an error occurred.
   385 */
   386 native GroupId:GetAdminGroup(AdminId:id, index, const String:name[], maxlength) =0;
   387 
   388 /**
   389  * Sets a password on an admin.
   390  *
   391  * @param id			AdminId index of the admin.
   392  * @param passwd		String containing the password.
   393  * @noreturn
   394  */
   395 native SetAdminPassword(AdminId:id, const String:password[]);
   396 
   397 /**
   398  * Gets an admin's password.
   399  *
   400  * @param id			AdminId index of the admin.
   401  * @param name			Optional buffer to store the admin's password.
   402  * @param maxlength		Maximum size of the output name buffer.
   403  *						Note: This will safely chop UTF-8 strings.
   404  * @return				True if there was a password set, false otherwise.
   405  */
   406 native bool:GetAdminPassword(AdminId:id, const String:buffer[]="", maxlength=0);
   407 
   408 /**
   409  * Attempts to find an admin by an auth method and an identity.
   410  *
   411  * @param auth			Auth method to try.
   412  * @param identity		Identity string to look up.
   413  * @return				An AdminId index if found, INVALID_ADMIN_ID otherwise.
   414  */
   415 native AdminId:FindAdminByIdentity(const String:auth[], const String:identity[]);
   416 
   417 /**
   418  * Removes an admin entry from the cache.
   419  *
   420  * Note: This will remove any bindings to a specific user.
   421  *
   422  * @param id			AdminId index to remove/invalidate.
   423  * @return				True on success, false otherwise.
   424  */
   425 native bool:RemoveAdmin(AdminId:id);
   426 
   427 /**
   428  * Converts a flag bit string to a bit array.
   429  *
   430  * @param bits			Bit string containing the flags.
   431  * @param array			Array to write the flags to.  Enabled flags will be 'true'.
   432  * @param maxSize		Maximum number of flags the array can store.
   433  * @return				Number of flags written.
   434  */
   435 native FlagBitsToBitArray(bits, bool:array[], maxSize);
   436 
   437 /**
   438  * Converts a flag array to a bit string.
   439  *
   440  * @param array			Array containing true or false for each AdminFlag.
   441  * @param maxSize		Maximum size of the flag array.
   442  * @return				A bit string composed of the array bits.
   443  */
   444 native FlagBitArrayToBits(const bool:array[], maxSize);
   445 
   446 /**
   447  * Converts an array of flags to bits.
   448  *
   449  * @param array			Array containing flags that are enabled.
   450  * @param numFlags		Number of flags in the array.
   451  * @return				A bit string composed of the array flags.
   452  */
   453 native FlagArrayToBits(const AdminFlag:array[], numFlags) =0;
   454 
   455 /**
   456  * Converts a bit string to an array of flags.
   457  *
   458  * @param bits			Bit string containing the flags.
   459  * @param array			Output array to write flags.
   460  * @param maxSize		Maximum size of the flag array.
   461  * @return				Number of flags written.
   462  */
   463 native FlagBitsToArray(bits, AdminFlag:array[], maxSize) =0;
   464 
   465 /**
   466  * Tests whether one admin can target another.
   467  *
   468  * @param admin			Admin doing the targetting (may be INVALID_ADMIN_ID).
   469  * @param target		Target admin (may be INVALID_ADMIN_ID).
   470  * @return				True if targetable, false if immune.
   471  */
   472 native CanAdminTarget(AdminId:admin, AdminId:target);
   473 
   474 /**
   475  * Converts a flag to its single bit.
   476  * 
   477  * @param flag			Flag to convert.
   478  * @return				Bit representation of the flag.
   479  */
   480 stock FlagToBit(AdminFlag:flag)
   481 {
   482 	return (1<<_:flag);
   483 }
   484 
   485 /**
   486  * Converts a bit to an AdminFlag.
   487  *
   488  * @param bit			Bit to convert.
   489  * @param flag			Stores the converted flag by reference.
   490  * @return				True on success, false otherwise.
   491  */
   492 stock bool:BitToFlag(bit, &AdminFlag:flag)
   493 {
   494 	new AdminFlag:array[1];
   495 	
   496 	if (FlagBitsToArray(bit, array, 1))
   497 	{
   498 		flag = array[0];
   499 		return true;
   500 	}
   501 	
   502 	return false;
   503 }