plugins/include/admin.inc
author David Anderson <dvander@alliedmods.net>
Wed Aug 08 02:27:10 2007 +0000 (2007-08-08)
changeset 1269 27397584df9c
parent 814 b2815f4c1731
child 1298 9cddbed74581
permissions -rw-r--r--
- massive overhaul of the voting subsystem
- NOTE: menu API compatibility was COMPLETELY broken for C++!
- cleaned up various aspects of the menu API
- implemented new extended vote results callback
- extended the debug reporter a tiny bit
     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		/**< Total number of admin flags */
    51 
    52 /**
    53  * @section Bitwise values definitions for admin 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 /**
    78  * @endsection
    79  */
    80 
    81 /**
    82  * @section Hardcoded authentication methods 
    83  */
    84 #define AUTHMETHOD_STEAM			"steam"		/**< SteamID based authentication */
    85 #define AUTHMETHOD_IP				"ip"		/**< IP based authentication */
    86 #define AUTHMETHOD_NAME				"name"		/**< Name based authentication */
    87 
    88 /**
    89  * @endsection
    90  */
    91 
    92 /**
    93  * Access override types.
    94  */
    95 enum OverrideType
    96 {
    97 	Override_Command = 1,	/**< Command */
    98 	Override_CommandGroup,	/**< Command group */
    99 };
   100 
   101 /**
   102  * Access override rules.
   103  */
   104 enum OverrideRule
   105 {
   106 	Command_Deny = 0,
   107 	Command_Allow = 1,
   108 };
   109 
   110 /**
   111  * Immunity types.
   112  */
   113 enum ImmunityType
   114 {
   115 	Immunity_Default = 1,	/**< Immune from everyone with no immunity */
   116 	Immunity_Global,		/**< Immune from everyone (except root admins) */
   117 };
   118 
   119 /** 
   120  * Identifies a unique entry in the group permissions cache.  These are not Handles.
   121  */
   122 enum GroupId
   123 {
   124 	INVALID_GROUP_ID = -1,	/**< An invalid/nonexistant group */
   125 };
   126 
   127 /** 
   128  * Identifies a unique entry in the admin permissions cache.  These are not Handles.
   129  */
   130 enum AdminId
   131 {
   132 	INVALID_ADMIN_ID = -1,	/**< An invalid/nonexistant admin */
   133 };
   134 
   135 /**
   136  * Methods of computing access permissions.
   137  */
   138 enum AdmAccessMode
   139 {
   140 	Access_Real,		/**< Access the user has inherently */
   141 	Access_Effective,	/**< Access the user has from their groups */
   142 };
   143 
   144 /**
   145  * Represents the various cache regions.
   146  */
   147 enum AdminCachePart
   148 {
   149 	AdminCache_Overrides = 0,		/**< Global overrides */
   150 	AdminCache_Groups = 1,			/**< All groups (automatically invalidates admins too) */
   151 	AdminCache_Admins = 2,			/**< All admins */
   152 };
   153 
   154 /**
   155  * Called when part of the cache which needs to be rebuilt.
   156  *
   157  * @param part			Part of the admin cache to rebuild.
   158  */
   159 forward OnRebuildAdminCache(AdminCachePart:part);
   160 
   161 /**
   162  * Tells the admin system to dump a portion of the cache.
   163  *
   164  * @param part			Part of the cache to dump.  Specifying groups also dumps admins.
   165  * @param rebuild		If true, the rebuild forwards will fire.
   166  * @noreturn
   167  */
   168 native DumpAdminCache(AdminCachePart:part, bool:rebuild);
   169 
   170 /**
   171  * Adds a global command flag override.  Any command registered with this name
   172  * will assume the new flag.  This is applied retroactively as well.
   173  *
   174  * @param cmd			String containing command name (case sensitive).
   175  * @param type			Override type (specific command or group).
   176  * @param flags			New admin flag.
   177  * @noreturn
   178  */
   179 native AddCommandOverride(const String:cmd[], OverrideType:type, flags);
   180 
   181 /**
   182  * Returns a command override.
   183  *
   184  * @param cmd			String containing command name (case sensitive).
   185  * @param type			Override type (specific command or group).
   186  * @param flags			By-reference cell to store the flag (undefined if not found).
   187  * @return				True if there is an override, false otherwise.
   188  */
   189 native bool:GetCommandOverride(const String:cmd[], OverrideType:type, &flags);
   190 
   191 /**
   192  * Unsets a command override.
   193  *
   194  * @param cmd			String containing command name (case sensitive).
   195  * @param type			Override type (specific command or group).
   196  * @noreturn
   197  */
   198 native UnsetCommandOverride(const String:cmd[], OverrideType:type);
   199 
   200 /**
   201  * Adds a new group.  Name must be unique.
   202  *
   203  * @param group_name	String containing the group name.
   204  * @return				A new group id, INVALID_GROUP_ID if it already exists.
   205  */
   206 native GroupId:CreateAdmGroup(const String:group_name[]);
   207 
   208 /**
   209  * Finds a group by name.
   210  *
   211  * @param group_name	String containing the group name.
   212  * @return				A group id, or INVALID_GROUP_ID if not found.
   213  */
   214 native GroupId:FindAdmGroup(const String:group_name[]);
   215 
   216 /**
   217  * Adds or removes a flag from a group's flag set.
   218  * @note These are called "add flags" because they add to a user's flags.
   219  *
   220  * @param id			Group id.
   221  * @param flag			Admin flag to toggle.
   222  * @param enabled		True to set the flag, false to unset/disable.
   223  * @noreturn
   224  */
   225 native SetAdmGroupAddFlag(GroupId:id, AdminFlag:flag, bool:enabled);
   226 
   227 /**
   228  * Gets the set value of an add flag on a group's flag set.
   229  * @note These are called "add flags" because they add to a user's flags.
   230  *
   231  * @param id			Group id.
   232  * @param flag			Admin flag to retrieve.
   233  * @return				True if enabled, false otherwise,
   234  */
   235 native bool:GetAdmGroupAddFlag(GroupId:id, AdminFlag:flag);
   236 
   237 /**
   238  * Returns the flag set that is added to a user from their group.
   239  * @note These are called "add flags" because they add to a user's flags.
   240  *
   241  * @param id			GroupId of the group.
   242  * @return				Bitstring containing the flags enabled.
   243  */
   244 native GetAdmGroupAddFlags(GroupId:id);
   245 
   246 /**
   247  * Toggles a generic immunity type.
   248  *
   249  * @param id			Group id.
   250  * @param type			Generic immunity type.
   251  * @param enabled		True to enable, false otherwise.
   252  * @noreturn
   253  */
   254 native SetAdmGroupImmunity(GroupId:id, ImmunityType:type, bool:enabled);
   255 
   256 /**
   257  * Returns whether or not a group has global immunity.
   258  *
   259  * @param id			Group id.
   260  * @param type			Generic immunity type.
   261  * @return				True if the group has this immunity, false otherwise.
   262  */
   263 native bool:GetAdmGroupImmunity(GroupId:id, ImmunityType:type);
   264 
   265 /**
   266  * Adds immunity to a specific group.
   267  *
   268  * @param id			Group id.
   269  * @param other_id		Group id to receive immunity to.
   270  * @noreturn
   271  */
   272 native SetAdmGroupImmuneFrom(GroupId:id, GroupId:other_id);
   273 
   274 /**
   275  * Returns the number of specific group immunities.
   276  *
   277  * @param id			Group id.
   278  * @return				Number of group immunities.
   279  */
   280 native GetAdmGroupImmuneCount(GroupId:id);
   281 
   282 /**
   283  * Returns a group that this group is immune to given an index.
   284  *
   285  * @param id			Group id.
   286  * @param number		Index from 0 to N-1, from GetAdmGroupImmuneCount().
   287  * @return				GroupId that this group is immune to, or INVALID_GROUP_ID on failure.
   288  */
   289 native GroupId:GetAdmGroupImmuneFrom(GroupId:id, number); 
   290 
   291 /**
   292  * Adds a group-specific override type.
   293  *
   294  * @param id			Group id.
   295  * @param name			String containing command name (case sensitive).
   296  * @param type			Override type (specific command or group).
   297  * @param rule			Override allow/deny setting.
   298  * @noreturn
   299  */
   300 native AddAdmGroupCmdOverride(GroupId:id, const String:name[], OverrideType:type, OverrideRule:rule);
   301 
   302 /**
   303  * Retrieves a group-specific command override.
   304  *
   305  * @param id			Group id.
   306  * @param name			String containing command name (case sensitive).
   307  * @param type			Override type (specific command or group).
   308  * @param rule			Optional pointer to store allow/deny setting.
   309  * @return				True if an override exists, false otherwise.
   310  */
   311 native bool:GetAdmGroupCmdOverride(GroupId:id, const String:name[], OverrideType:type, &OverrideRule:rule);
   312 
   313 /**
   314  * Registers an authentication identity type.  You normally never need to call this except for 
   315  * very specific systems.
   316  *
   317  * @param codename		Codename to use for your authentication type.
   318  * @noreturn
   319  */
   320 native RegisterAuthIdentType(const String:name[]);
   321 
   322 /**
   323  * Creates a new admin entry in the permissions cache.
   324  *
   325  * @param name			Name for this entry (does not have to be unique).
   326  * 						Specify an empty string for an anonymous admin.
   327  */
   328 native AdminId:CreateAdmin(const String:name[]="");
   329 
   330 /**
   331  * Retrieves an admin's user name as made with CreateAdmin().
   332  *
   333  * @note This function can return UTF-8 strings, and will safely chop UTF-8 strings.
   334  *
   335  * @param id			AdminId of the admin.
   336  * @param name			String buffer to store name.
   337  * @param maxlength		Maximum size of string buffer.
   338  * @return				Number of bytes written.
   339  */
   340 native GetAdminUsername(AdminId:id, const String:name[], maxlength);
   341 
   342 /**
   343  * Binds an admin to an identity for fast lookup later on.  The bind must be unique.
   344  *
   345  * @param id			AdminId of the admin.
   346  * @param auth			Auth method to use, predefined or from RegisterAuthIdentType().
   347  * @param ident			String containing the arbitrary, unique identity.
   348  * @return				True on success, false if the auth method was not found,
   349  *						or ident was already taken.
   350  */
   351 native bool:BindAdminIdentity(AdminId:id, const String:auth[], const String:ident[]);
   352 
   353 /**
   354  * Sets whether or not a flag is enabled on an admin.
   355  *
   356  * @param id			AdminId index of the admin.
   357  * @param flag			Admin flag to use.
   358  * @param enabled		True to enable, false to disable.
   359  * @noreturn
   360  */
   361 native SetAdminFlag(AdminId:id, AdminFlag:flag, bool:enabled);
   362 
   363 /**
   364  * Returns whether or not a flag is enabled on an admin.
   365  *
   366  * @param id			AdminId index of the admin.
   367  * @param flag			Admin flag to use.
   368  * @param mode			Access mode to check.
   369  * @return				True if enabled, false otherwise.
   370  */
   371 native bool:GetAdminFlag(AdminId:id, AdminFlag:flag, AdmAccessMode:mode);
   372 
   373 /**
   374  * Returns the bitstring of access flags on an admin.
   375  *
   376  * @param id			AdminId index of the admin.
   377  * @param mode			Access mode to use.
   378  * @return				A bitstring containing which flags are enabled.
   379  */
   380 native GetAdminFlags(AdminId:id, AdmAccessMode:mode);
   381 
   382 /**
   383  * Adds a group to an admin's inherited group list.  Any flags the group has 
   384  * will be added to the admin's effective flags.
   385  *
   386  * @param id			AdminId index of the admin.
   387  * @param gid			GroupId index of the group.
   388  * @return				True on success, false on invalid input or duplicate membership.
   389  */
   390 native bool:AdminInheritGroup(AdminId:id, GroupId:gid);
   391 
   392 /**
   393  * Returns the number of groups this admin is a member of.
   394  * 
   395  * @param id			AdminId index of the admin.
   396  * @return				Number of groups this admin is a member of.
   397  */
   398 native GetAdminGroupCount(AdminId:id);
   399 
   400 /**
   401  * Returns group information from an admin.
   402  *
   403  * @param id        	AdminId index of the admin.
   404  * @param index			Group number to retrieve, from 0 to N-1, where N
   405  *						is the value of GetAdminGroupCount(id).
   406  * @param name			Buffer to store the group's name.
   407  *						Note: This will safely chop UTF-8 strings.
   408  * @param maxlength		Maximum size of the output name buffer.
   409  * @return				A GroupId index and a name pointer, or
   410  *						INVALID_GROUP_ID and NULL if an error occurred.
   411 */
   412 native GroupId:GetAdminGroup(AdminId:id, index, const String:name[], maxlength);
   413 
   414 /**
   415  * Sets a password on an admin.
   416  *
   417  * @param id			AdminId index of the admin.
   418  * @param passwd		String containing the password.
   419  * @noreturn
   420  */
   421 native SetAdminPassword(AdminId:id, const String:password[]);
   422 
   423 /**
   424  * Gets an admin's password.
   425  *
   426  * @param id			AdminId index of the admin.
   427  * @param name			Optional buffer to store the admin's password.
   428  * @param maxlength		Maximum size of the output name buffer.
   429  *						Note: This will safely chop UTF-8 strings.
   430  * @return				True if there was a password set, false otherwise.
   431  */
   432 native bool:GetAdminPassword(AdminId:id, const String:buffer[]="", maxlength=0);
   433 
   434 /**
   435  * Attempts to find an admin by an auth method and an identity.
   436  *
   437  * @param auth			Auth method to try.
   438  * @param identity		Identity string to look up.
   439  * @return				An AdminId index if found, INVALID_ADMIN_ID otherwise.
   440  */
   441 native AdminId:FindAdminByIdentity(const String:auth[], const String:identity[]);
   442 
   443 /**
   444  * Removes an admin entry from the cache.
   445  *
   446  * @note This will remove any bindings to a specific user.
   447  *
   448  * @param id			AdminId index to remove/invalidate.
   449  * @return				True on success, false otherwise.
   450  */
   451 native bool:RemoveAdmin(AdminId:id);
   452 
   453 /**
   454  * Converts a flag bit string to a bit array.
   455  *
   456  * @param bits			Bit string containing the flags.
   457  * @param array			Array to write the flags to.  Enabled flags will be 'true'.
   458  * @param maxSize		Maximum number of flags the array can store.
   459  * @return				Number of flags written.
   460  */
   461 native FlagBitsToBitArray(bits, bool:array[], maxSize);
   462 
   463 /**
   464  * Converts a flag array to a bit string.
   465  *
   466  * @param array			Array containing true or false for each AdminFlag.
   467  * @param maxSize		Maximum size of the flag array.
   468  * @return				A bit string composed of the array bits.
   469  */
   470 native FlagBitArrayToBits(const bool:array[], maxSize);
   471 
   472 /**
   473  * Converts an array of flags to bits.
   474  *
   475  * @param array			Array containing flags that are enabled.
   476  * @param numFlags		Number of flags in the array.
   477  * @return				A bit string composed of the array flags.
   478  */
   479 native FlagArrayToBits(const AdminFlag:array[], numFlags);
   480 
   481 /**
   482  * Converts a bit string to an array of flags.
   483  *
   484  * @param bits			Bit string containing the flags.
   485  * @param array			Output array to write flags.
   486  * @param maxSize		Maximum size of the flag array.
   487  * @return				Number of flags written.
   488  */
   489 native FlagBitsToArray(bits, AdminFlag:array[], maxSize);
   490 
   491 /**
   492  * Tests whether one admin can target another.
   493  *
   494  * @param admin			Admin doing the targetting (may be INVALID_ADMIN_ID).
   495  * @param target		Target admin (may be INVALID_ADMIN_ID).
   496  * @return				True if targetable, false if immune.
   497  */
   498 native CanAdminTarget(AdminId:admin, AdminId:target);
   499 
   500 /**
   501  * Creates an admin auth method.  This does not need to be called more than once
   502  * per method, ever.
   503  *
   504  * @param method		Name of the authentication method.
   505  * @return				True on success, false on failure.
   506  */
   507 native bool:CreateAuthMethod(const String:method[]);
   508 
   509 /**
   510  * Converts a flag to its single bit.
   511  * 
   512  * @param flag			Flag to convert.
   513  * @return				Bit representation of the flag.
   514  */
   515 stock FlagToBit(AdminFlag:flag)
   516 {
   517 	return (1<<_:flag);
   518 }
   519 
   520 /**
   521  * Converts a bit to an AdminFlag.
   522  *
   523  * @param bit			Bit to convert.
   524  * @param flag			Stores the converted flag by reference.
   525  * @return				True on success, false otherwise.
   526  */
   527 stock bool:BitToFlag(bit, &AdminFlag:flag)
   528 {
   529 	new AdminFlag:array[1];
   530 	
   531 	if (FlagBitsToArray(bit, array, 1))
   532 	{
   533 		flag = array[0];
   534 		return true;
   535 	}
   536 	
   537 	return false;
   538 }
   539