dvander@370: /**
dvander@407: * vim: set ts=4 :
ds@1330: * =============================================================================
dvander@1231: * SourceMod
ds@1947: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
ds@1330: * =============================================================================
dvander@370: *
ds@1330: * This program is free software; you can redistribute it and/or modify it under
ds@1330: * the terms of the GNU General Public License, version 3.0, as published by the
ds@1330: * Free Software Foundation.
dvander@1231: *
ds@1330: * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1330: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1330: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
ds@1330: * details.
dvander@1231: *
ds@1330: * You should have received a copy of the GNU General Public License along with
ds@1330: * this program. If not, see .
dvander@1231: *
ds@1330: * As a special exception, AlliedModders LLC gives you permission to link the
ds@1330: * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1330: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1330: * by the Valve Corporation. You must obey the GNU General Public License in
ds@1330: * all respects for all other code used. Additionally, AlliedModders LLC grants
ds@1330: * this exception to all derivative works. AlliedModders LLC defines further
ds@1330: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1330: * or .
dvander@370: *
dvander@370: * Version: $Id$
dvander@370: */
dvander@370:
dvander@335: #ifndef _INCLUDE_SOURCEMOD_ADMINCACHE_H_
dvander@335: #define _INCLUDE_SOURCEMOD_ADMINCACHE_H_
dvander@335:
dvander@335: #include "sm_memtable.h"
dvander@335: #include
dvander@336: #include
dvander@404: #include
dvander@335: #include
dvander@335: #include "sm_globals.h"
dvander@336: #include
dvander@336:
dvander@336: using namespace SourceHook;
dvander@335:
dvander@335: #define GRP_MAGIC_SET 0xDEADFADE
dvander@335: #define GRP_MAGIC_UNSET 0xFACEFACE
dvander@404: #define USR_MAGIC_SET 0xDEADFACE
dvander@404: #define USR_MAGIC_UNSET 0xFADEDEAD
dvander@335:
dvander@335: struct AdminGroup
dvander@335: {
dvander@335: uint32_t magic; /* Magic flag, for memory validation (ugh) */
dvander@1403: unsigned int immunity_level; /* Immunity level */
ds@729: /* Immune from target table (-1 = nonexistent)
dvander@335: * [0] = number of entries
dvander@335: * [1...N] = immune targets
dvander@335: */
dvander@335: int immune_table;
dvander@335: Trie *pCmdTable; /* Command override table (can be NULL) */
dvander@335: Trie *pCmdGrpTable; /* Command group override table (can be NULL) */
dvander@335: int next_grp; /* Next group in the chain */
dvander@335: int prev_grp; /* Previous group in the chain */
dvander@335: int nameidx; /* Name */
dvander@433: FlagBits addflags; /* Additive flags */
dvander@335: };
dvander@335:
dvander@404: struct AuthMethod
dvander@404: {
dvander@404: String name;
dvander@404: Trie *table;
dvander@404: };
dvander@404:
dvander@404: struct UserAuth
dvander@404: {
dvander@404: unsigned int index; /* Index into auth table */
dvander@404: int identidx; /* Index into the string table */
dvander@404: };
dvander@404:
dvander@404: struct AdminUser
dvander@404: {
dvander@433: uint32_t magic; /* Magic flag, for memory validation */
dvander@433: FlagBits flags; /* Flags */
dvander@433: FlagBits eflags; /* Effective flags */
dvander@404: int nameidx; /* Name index */
dvander@404: int password; /* Password index */
dvander@404: unsigned int grp_count; /* Number of groups */
dvander@404: unsigned int grp_size; /* Size of groups table */
dvander@404: int grp_table; /* Group table itself */
ds@729: int next_user; /* Next user in the list */
ds@729: int prev_user; /* Previous user in the list */
dvander@405: UserAuth auth; /* Auth method for this user */
dvander@1403: unsigned int immunity_level; /* Immunity level */
dvander@1349: unsigned int serialchange; /* Serial # for changes */
dvander@404: };
dvander@404:
dvander@335: class AdminCache :
dvander@335: public IAdminSystem,
dvander@335: public SMGlobalClass
dvander@335: {
dvander@335: public:
dvander@335: AdminCache();
dvander@335: ~AdminCache();
dvander@335: public: //SMGlobalClass
dvander@404: void OnSourceModStartup(bool late);
dvander@335: void OnSourceModAllInitialized();
dvander@1298: void OnSourceModLevelChange(const char *mapName);
dvander@336: void OnSourceModShutdown();
dvander@673: void OnSourceModPluginsLoaded();
dvander@335: public: //IAdminSystem
dvander@335: /** Command cache stuff */
dvander@433: void AddCommandOverride(const char *cmd, OverrideType type, FlagBits flags);
dvander@433: bool GetCommandOverride(const char *cmd, OverrideType type, FlagBits *flags);
dvander@335: void UnsetCommandOverride(const char *cmd, OverrideType type);
dvander@335: /** Group cache stuff */
dvander@335: GroupId AddGroup(const char *group_name);
dvander@335: GroupId FindGroupByName(const char *group_name);
dvander@335: void SetGroupAddFlag(GroupId id, AdminFlag flag, bool enabled);
dvander@335: bool GetGroupAddFlag(GroupId id, AdminFlag flag);
dvander@433: FlagBits GetGroupAddFlags(GroupId id);
dvander@335: void SetGroupGenericImmunity(GroupId id, ImmunityType type, bool enabled);
dvander@335: bool GetGroupGenericImmunity(GroupId id, ImmunityType type);
dvander@335: void InvalidateGroup(GroupId id);
dvander@335: void AddGroupImmunity(GroupId id, GroupId other_id);
dvander@335: unsigned int GetGroupImmunityCount(GroupId id);
dvander@335: GroupId GetGroupImmunity(GroupId id, unsigned int number);
dvander@335: void AddGroupCommandOverride(GroupId id, const char *name, OverrideType type, OverrideRule rule);
dvander@335: bool GetGroupCommandOverride(GroupId id, const char *name, OverrideType type, OverrideRule *pRule);
dvander@438: void DumpAdminCache(AdminCachePart part, bool rebuild);
dvander@336: void AddAdminListener(IAdminListener *pListener);
dvander@336: void RemoveAdminListener(IAdminListener *pListener);
dvander@404: /** User stuff */
dvander@404: void RegisterAuthIdentType(const char *name);
dvander@404: AdminId CreateAdmin(const char *name);
dvander@404: const char *GetAdminName(AdminId id);
dvander@404: bool BindAdminIdentity(AdminId id, const char *auth, const char *ident);
dvander@404: virtual void SetAdminFlag(AdminId id, AdminFlag flag, bool enabled);
dvander@404: bool GetAdminFlag(AdminId id, AdminFlag flag, AccessMode mode);
dvander@433: FlagBits GetAdminFlags(AdminId id, AccessMode mode);
dvander@404: bool AdminInheritGroup(AdminId id, GroupId gid);
dvander@404: unsigned int GetAdminGroupCount(AdminId id);
dvander@404: GroupId GetAdminGroup(AdminId id, unsigned int index, const char **name);
dvander@404: void SetAdminPassword(AdminId id, const char *password);
dvander@404: const char *GetAdminPassword(AdminId id);
dvander@404: AdminId FindAdminByIdentity(const char *auth, const char *identity);
dvander@404: bool InvalidateAdmin(AdminId id);
dvander@433: unsigned int FlagBitsToBitArray(FlagBits bits, bool array[], unsigned int maxSize);
dvander@433: FlagBits FlagBitArrayToBits(const bool array[], unsigned int maxSize);
dvander@433: FlagBits FlagArrayToBits(const AdminFlag array[], unsigned int numFlags);
dvander@433: unsigned int FlagBitsToArray(FlagBits bits, AdminFlag array[], unsigned int maxSize);
dvander@434: bool CheckAdminFlags(AdminId id, FlagBits bits);
dvander@434: bool CanAdminTarget(AdminId id, AdminId target);
dvander@458: void SetAdminFlags(AdminId id, AccessMode mode, FlagBits bits);
dvander@1298: bool FindFlag(const char *str, AdminFlag *pFlag);
dvander@1298: bool FindFlag(char c, AdminFlag *pAdmFlag);
dvander@1298: FlagBits ReadFlagString(const char *flags, const char **end);
dvander@1965: size_t FillFlagString(FlagBits bits, char *buffer, size_t maxlen);
dvander@1349: unsigned int GetAdminSerialChange(AdminId id);
dvander@1349: bool CanAdminUseCommand(int client, const char *cmd);
dvander@1356: const char *GetGroupName(GroupId gid);
dvander@1473: unsigned int SetGroupImmunityLevel(GroupId gid, unsigned int level);
dvander@1473: unsigned int GetGroupImmunityLevel(GroupId gid);
dvander@1473: unsigned int SetAdminImmunityLevel(AdminId id, unsigned int level);
dvander@1473: unsigned int GetAdminImmunityLevel(AdminId id);
dvander@1473: bool CheckAccess(int client,
dvander@1473: const char *cmd,
dvander@1477: FlagBits flags,
dvander@1473: bool override_only);
dvander@1965: bool FindFlagChar(AdminFlag flag, char *c);
dvander@458: public:
dvander@458: bool IsValidAdmin(AdminId id);
dvander@1965: void DumpCache(FILE *fp);
dvander@1965: AdminGroup *GetGroup(GroupId gid);
dvander@1965: AdminUser *GetUser(AdminId id);
dvander@1965: const char *GetString(int idx);
dvander@335: private:
dvander@335: void _UnsetCommandOverride(const char *cmd);
dvander@335: void _UnsetCommandGroupOverride(const char *group);
dvander@336: void InvalidateGroupCache();
dvander@404: void InvalidateAdminCache(bool unlink_admins);
dvander@336: void DumpCommandOverrideCache(OverrideType type);
dvander@404: Trie *GetMethodByIndex(unsigned int index);
dvander@405: bool GetMethodIndex(const char *name, unsigned int *_index);
dvander@1965: const char *GetMethodName(unsigned int index);
dvander@1298: void NameFlag(const char *str, AdminFlag flag);
dvander@335: public:
dvander@335: BaseStringTable *m_pStrings;
dvander@335: BaseMemTable *m_pMemory;
dvander@335: Trie *m_pCmdOverrides;
dvander@335: Trie *m_pCmdGrpOverrides;
dvander@335: int m_FirstGroup;
dvander@335: int m_LastGroup;
dvander@335: int m_FreeGroupList;
dvander@335: Trie *m_pGroups;
dvander@336: List m_hooks;
dvander@404: List m_AuthMethods;
dvander@404: Trie *m_pAuthTables;
dvander@336: IForward *m_pCacheFwd;
dvander@404: int m_FirstUser;
dvander@404: int m_LastUser;
dvander@404: int m_FreeUserList;
dvander@458: bool m_InvalidatingAdmins;
dvander@520: bool m_destroying;
dvander@1298: Trie *m_pLevelNames;
dvander@335: };
dvander@335:
dvander@335: extern AdminCache g_Admins;
dvander@335:
dvander@335: #endif //_INCLUDE_SOURCEMOD_ADMINCACHE_H_