core/AdminCache.h
author Ruben Gonzalez <drifter01620@gmail.com>
Thu May 22 09:49:27 2014 -0400 (2014-05-22)
changeset 4378 568f4497ef4a
parent 4081 241d291414e9
permissions -rw-r--r--
Update CS:GO team score gamedata.
     1 /**
     2  * vim: set ts=4 sw=4 tw=99 noet :
     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_ADMINCACHE_H_
    33 #define _INCLUDE_SOURCEMOD_ADMINCACHE_H_
    34 
    35 #include "sm_memtable.h"
    36 #include <sm_trie.h>
    37 #include <sh_list.h>
    38 #include <sh_string.h>
    39 #include <IAdminSystem.h>
    40 #include "sm_globals.h"
    41 #include <IForwardSys.h>
    42 #include <sm_stringhashmap.h>
    43 #include <sm_namehashset.h>
    44 
    45 using namespace SourceHook;
    46 
    47 #define GRP_MAGIC_SET		0xDEADFADE
    48 #define GRP_MAGIC_UNSET		0xFACEFACE
    49 #define USR_MAGIC_SET		0xDEADFACE
    50 #define USR_MAGIC_UNSET		0xFADEDEAD
    51 
    52 typedef StringHashMap<OverrideRule> OverrideMap;
    53 
    54 struct AdminGroup
    55 {
    56 	uint32_t magic;					/* Magic flag, for memory validation (ugh) */
    57 	unsigned int immunity_level;	/* Immunity level */
    58 	/* Immune from target table (-1 = nonexistent)
    59 	 * [0] = number of entries
    60 	 * [1...N] = immune targets
    61 	 */
    62 	int immune_table;			
    63 	OverrideMap *pCmdTable;			/* Command override table (can be NULL) */
    64 	OverrideMap *pCmdGrpTable;		/* Command group override table (can be NULL) */
    65 	int next_grp;					/* Next group in the chain */
    66 	int prev_grp;					/* Previous group in the chain */
    67 	int nameidx;					/* Name */
    68 	FlagBits addflags;				/* Additive flags */
    69 };
    70 
    71 struct AuthMethod
    72 {
    73 	String name;
    74 	StringHashMap<AdminId> identities;
    75 
    76 	AuthMethod(const char *name)
    77 		: name(name)
    78 	{
    79 	}
    80 
    81 	static inline bool matches(const char *name, const AuthMethod *method)
    82 	{
    83 		return strcmp(name, method->name.c_str()) == 0;
    84 	}
    85 };
    86 
    87 struct UserAuth
    88 {
    89 	unsigned int index;				/* Index into auth table */
    90 	int identidx;					/* Index into the string table */
    91 };
    92 
    93 struct AdminUser
    94 {
    95 	uint32_t magic;					/* Magic flag, for memory validation */
    96 	FlagBits flags;					/* Flags */
    97 	FlagBits eflags;				/* Effective flags */
    98 	int nameidx;					/* Name index */
    99 	int password;					/* Password index */
   100 	unsigned int grp_count;			/* Number of groups */
   101 	unsigned int grp_size;			/* Size of groups table */
   102 	int grp_table;					/* Group table itself */
   103 	int next_user;					/* Next user in the list */
   104 	int prev_user;					/* Previous user in the list */
   105 	UserAuth auth;					/* Auth method for this user */
   106 	unsigned int immunity_level;	/* Immunity level */
   107 	unsigned int serialchange;		/* Serial # for changes */
   108 };
   109 
   110 class AdminCache : 
   111 	public IAdminSystem,
   112 	public SMGlobalClass
   113 {
   114 public:
   115 	AdminCache();
   116 	~AdminCache();
   117 public: //SMGlobalClass
   118 	void OnSourceModStartup(bool late);
   119 	void OnSourceModAllInitialized();
   120 	void OnSourceModLevelChange(const char *mapName);
   121 	void OnSourceModShutdown();
   122 	void OnSourceModPluginsLoaded();
   123 public: //IAdminSystem
   124 	/** Command cache stuff */
   125 	void AddCommandOverride(const char *cmd, OverrideType type, FlagBits flags);
   126 	bool GetCommandOverride(const char *cmd, OverrideType type, FlagBits *flags);
   127 	void UnsetCommandOverride(const char *cmd, OverrideType type);
   128 	/** Group cache stuff */
   129 	GroupId AddGroup(const char *group_name);
   130 	GroupId FindGroupByName(const char *group_name);
   131 	void SetGroupAddFlag(GroupId id, AdminFlag flag, bool enabled);
   132 	bool GetGroupAddFlag(GroupId id, AdminFlag flag);
   133 	FlagBits GetGroupAddFlags(GroupId id);
   134 	void SetGroupGenericImmunity(GroupId id, ImmunityType type, bool enabled);
   135 	bool GetGroupGenericImmunity(GroupId id, ImmunityType type);
   136 	void InvalidateGroup(GroupId id);
   137 	void AddGroupImmunity(GroupId id, GroupId other_id);
   138 	unsigned int GetGroupImmunityCount(GroupId id);
   139 	GroupId GetGroupImmunity(GroupId id, unsigned int number);
   140 	void AddGroupCommandOverride(GroupId id, const char *name, OverrideType type, OverrideRule rule);
   141 	bool GetGroupCommandOverride(GroupId id, const char *name, OverrideType type, OverrideRule *pRule);
   142 	void DumpAdminCache(AdminCachePart part, bool rebuild);
   143 	void AddAdminListener(IAdminListener *pListener);
   144 	void RemoveAdminListener(IAdminListener *pListener);
   145 	/** User stuff */
   146 	void RegisterAuthIdentType(const char *name);
   147 	AdminId CreateAdmin(const char *name);
   148 	const char *GetAdminName(AdminId id);
   149 	bool BindAdminIdentity(AdminId id, const char *auth, const char *ident);
   150 	virtual void SetAdminFlag(AdminId id, AdminFlag flag, bool enabled);
   151 	bool GetAdminFlag(AdminId id, AdminFlag flag, AccessMode mode);
   152 	FlagBits GetAdminFlags(AdminId id, AccessMode mode);
   153 	bool AdminInheritGroup(AdminId id, GroupId gid);
   154 	unsigned int GetAdminGroupCount(AdminId id);
   155 	GroupId GetAdminGroup(AdminId id, unsigned int index, const char **name);
   156 	void SetAdminPassword(AdminId id, const char *password);
   157 	const char *GetAdminPassword(AdminId id);
   158 	AdminId FindAdminByIdentity(const char *auth, const char *identity);
   159 	bool InvalidateAdmin(AdminId id);
   160 	unsigned int FlagBitsToBitArray(FlagBits bits, bool array[], unsigned int maxSize);
   161 	FlagBits FlagBitArrayToBits(const bool array[], unsigned int maxSize);
   162 	FlagBits FlagArrayToBits(const AdminFlag array[], unsigned int numFlags);
   163 	unsigned int FlagBitsToArray(FlagBits bits, AdminFlag array[], unsigned int maxSize);
   164 	bool CheckAdminFlags(AdminId id, FlagBits bits);
   165 	bool CanAdminTarget(AdminId id, AdminId target);
   166 	void SetAdminFlags(AdminId id, AccessMode mode, FlagBits bits);
   167 	bool FindFlag(const char *str, AdminFlag *pFlag);
   168 	bool FindFlag(char c, AdminFlag *pAdmFlag);
   169 	FlagBits ReadFlagString(const char *flags, const char **end);
   170 	size_t FillFlagString(FlagBits bits, char *buffer, size_t maxlen);
   171 	unsigned int GetAdminSerialChange(AdminId id);
   172 	bool CanAdminUseCommand(int client, const char *cmd);
   173 	const char *GetGroupName(GroupId gid);
   174 	unsigned int SetGroupImmunityLevel(GroupId gid, unsigned int level);
   175 	unsigned int GetGroupImmunityLevel(GroupId gid);
   176 	unsigned int SetAdminImmunityLevel(AdminId id, unsigned int level);
   177 	unsigned int GetAdminImmunityLevel(AdminId id);
   178 	bool CheckAccess(int client, 
   179 		const char *cmd, 
   180 		FlagBits flags, 
   181 		bool override_only);
   182 	bool FindFlagChar(AdminFlag flag, char *c);
   183 public:
   184 	bool IsValidAdmin(AdminId id);
   185 	void DumpCache(FILE *fp);
   186 	AdminGroup *GetGroup(GroupId gid);
   187 	AdminUser *GetUser(AdminId id);
   188 	const char *GetString(int idx);
   189 private:
   190 	void _UnsetCommandOverride(const char *cmd);
   191 	void _UnsetCommandGroupOverride(const char *group);
   192 	void InvalidateGroupCache();
   193 	void InvalidateAdminCache(bool unlink_admins);
   194 	void DumpCommandOverrideCache(OverrideType type);
   195 	AuthMethod *GetMethodByIndex(unsigned int index);
   196 	bool GetMethodIndex(const char *name, unsigned int *_index);
   197 	const char *GetMethodName(unsigned int index);
   198 	void NameFlag(const char *str, AdminFlag flag);
   199 public:
   200 	typedef StringHashMap<FlagBits> FlagMap;
   201 
   202 	BaseStringTable *m_pStrings;
   203 	BaseMemTable *m_pMemory;
   204 	FlagMap m_CmdOverrides;
   205 	FlagMap m_CmdGrpOverrides;
   206 	int m_FirstGroup;
   207 	int m_LastGroup;
   208 	int m_FreeGroupList;
   209 	StringHashMap<GroupId> m_Groups;
   210 	List<IAdminListener *> m_hooks;
   211 	List<AuthMethod *> m_AuthMethods;
   212 	NameHashSet<AuthMethod *> m_AuthTables;
   213 	IForward *m_pCacheFwd;
   214 	int m_FirstUser;
   215 	int m_LastUser;
   216 	int m_FreeUserList;
   217 	bool m_InvalidatingAdmins;
   218 	bool m_destroying;
   219 	StringHashMap<AdminFlag> m_LevelNames;
   220 };
   221 
   222 extern AdminCache g_Admins;
   223 
   224 #endif //_INCLUDE_SOURCEMOD_ADMINCACHE_H_