2014-08-04 08:36:20 +00:00
|
|
|
// vim: set ts=4 sw=4 tw=99 noet:
|
|
|
|
//
|
|
|
|
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
|
|
|
|
// Copyright (C) The AMX Mod X Development Team.
|
|
|
|
//
|
|
|
|
// This software is licensed under the GNU General Public License, version 3 or higher.
|
|
|
|
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
|
|
|
|
// https://alliedmods.net/amxmodx-license
|
|
|
|
|
2007-03-13 00:42:16 +00:00
|
|
|
#ifndef CFLAGMANAGER_H
|
|
|
|
#define CFLAGMANAGER_H
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include "sh_list.h"
|
|
|
|
#include "amxmodx.h"
|
2015-07-11 08:37:36 +00:00
|
|
|
#include "CLibrarySys.h"
|
2007-03-13 00:42:16 +00:00
|
|
|
|
|
|
|
class CFlagEntry
|
|
|
|
{
|
|
|
|
private:
|
2015-07-11 08:37:36 +00:00
|
|
|
ke::AString m_strName; // command name ("amx_slap")
|
|
|
|
ke::AString m_strFlags; // string flags ("a","b")
|
|
|
|
ke::AString m_strComment; // comment to write ("; admincmd.amxx")
|
2007-03-13 00:42:16 +00:00
|
|
|
int m_iFlags; // bitmask flags
|
|
|
|
int m_iNeedWritten; // write this command on map change?
|
|
|
|
int m_iHidden; // set to 1 when the command is set to "!" access in
|
|
|
|
// the .ini file: this means do not process this command
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
CFlagEntry()
|
|
|
|
{
|
|
|
|
m_iNeedWritten=0;
|
|
|
|
m_iFlags=0;
|
|
|
|
m_iHidden=0;
|
|
|
|
};
|
|
|
|
const int NeedWritten(void) const
|
|
|
|
{
|
|
|
|
return m_iNeedWritten;
|
|
|
|
};
|
|
|
|
|
|
|
|
void SetNeedWritten(const int i=1)
|
|
|
|
{
|
|
|
|
m_iNeedWritten=i;
|
|
|
|
};
|
|
|
|
|
2015-07-11 08:37:36 +00:00
|
|
|
const ke::AString *GetName(void) const
|
2007-03-13 00:42:16 +00:00
|
|
|
{
|
|
|
|
return &m_strName;
|
|
|
|
};
|
|
|
|
|
2015-07-11 08:37:36 +00:00
|
|
|
const ke::AString *GetFlags(void) const
|
2007-03-13 00:42:16 +00:00
|
|
|
{
|
|
|
|
return &m_strFlags;
|
|
|
|
};
|
2015-07-11 08:37:36 +00:00
|
|
|
|
|
|
|
const ke::AString *GetComment(void) const
|
2007-03-13 00:42:16 +00:00
|
|
|
{
|
|
|
|
return &m_strComment;
|
|
|
|
};
|
|
|
|
|
|
|
|
const int Flags(void) const
|
|
|
|
{
|
|
|
|
return m_iFlags;
|
|
|
|
};
|
|
|
|
|
|
|
|
void SetName(const char *data)
|
|
|
|
{
|
2015-07-11 08:37:36 +00:00
|
|
|
m_strName = data;
|
2007-03-13 00:42:16 +00:00
|
|
|
};
|
|
|
|
void SetFlags(const char *flags)
|
|
|
|
{
|
|
|
|
// If this is a "!" entry then stop
|
|
|
|
if (flags && flags[0]=='!')
|
|
|
|
{
|
|
|
|
SetHidden(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-11 08:37:36 +00:00
|
|
|
m_strFlags = flags;
|
2007-03-13 00:42:16 +00:00
|
|
|
m_iFlags=UTIL_ReadFlags(flags);
|
|
|
|
};
|
|
|
|
void SetFlags(const int flags)
|
|
|
|
{
|
|
|
|
m_iFlags=flags;
|
|
|
|
|
2007-03-09 03:04:40 +00:00
|
|
|
char FlagsString[32];
|
2007-03-13 00:42:16 +00:00
|
|
|
UTIL_GetFlags(FlagsString, flags);
|
|
|
|
|
2015-07-11 08:37:36 +00:00
|
|
|
m_strFlags = FlagsString;
|
2007-03-13 00:42:16 +00:00
|
|
|
};
|
|
|
|
void SetComment(const char *comment)
|
|
|
|
{
|
2015-07-11 08:37:36 +00:00
|
|
|
m_strComment = comment;
|
2007-03-13 00:42:16 +00:00
|
|
|
};
|
|
|
|
void SetHidden(int i=1)
|
|
|
|
{
|
|
|
|
m_iHidden=i;
|
|
|
|
};
|
|
|
|
int IsHidden(void) const
|
|
|
|
{
|
|
|
|
return m_iHidden;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
class CFlagManager
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
List<CFlagEntry *> m_FlagList;
|
2015-07-11 08:37:36 +00:00
|
|
|
ke::AString m_strConfigFile;
|
2007-03-13 00:42:16 +00:00
|
|
|
struct stat m_Stat;
|
|
|
|
int m_iForceRead;
|
|
|
|
int m_iDisabled;
|
|
|
|
|
|
|
|
|
|
|
|
void CreateIfNotExist(void) const
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
2015-07-11 08:37:36 +00:00
|
|
|
fp = fopen(GetFile(), "r");
|
2007-03-13 00:42:16 +00:00
|
|
|
|
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
// File does not exist, create the header
|
2015-07-11 08:37:36 +00:00
|
|
|
fp = fopen(GetFile(), "a");
|
2007-03-13 00:42:16 +00:00
|
|
|
|
|
|
|
if (fp)
|
|
|
|
{
|
|
|
|
fprintf(fp,"; This file will store the commands used by plugins, and their access level\n");
|
|
|
|
fprintf(fp,"; To change the access of a command, edit the flags beside it and then\n");
|
|
|
|
fprintf(fp,"; change the server's map.\n;\n");
|
|
|
|
fprintf(fp,"; Example: If I wanted to change the amx_slap access to require\n");
|
|
|
|
fprintf(fp,"; RCON access (flag \"l\") I would change this:\n");
|
|
|
|
fprintf(fp,"; \"amx_slap\" \"e\" ; admincmd.amxx\n");
|
|
|
|
fprintf(fp,"; To this:\n");
|
|
|
|
fprintf(fp,"; \"amx_slap\" \"l\" ; admincmd.amxx\n;\n");
|
|
|
|
fprintf(fp,"; To disable a specific command from being used with the command manager\n");
|
|
|
|
fprintf(fp,"; and to only use the plugin-specified access set the flag to \"!\"\n;\n");
|
|
|
|
fprintf(fp,"; NOTE: The plugin name at the end is just for reference to what plugin\n");
|
|
|
|
fprintf(fp,"; uses what commands. It is ignored.\n\n");
|
|
|
|
fclose(fp);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* Returns 1 if the timestamp for the file is different than the one we have loaded
|
|
|
|
* 0 otherwise
|
|
|
|
*/
|
|
|
|
inline int NeedToLoad(void)
|
|
|
|
{
|
|
|
|
struct stat TempStat;
|
|
|
|
|
2015-07-11 08:37:36 +00:00
|
|
|
stat(GetFile(), &TempStat);
|
2007-03-13 00:42:16 +00:00
|
|
|
|
|
|
|
// If the modified timestamp does not match the stored
|
|
|
|
// timestamp than we need to re-read this file.
|
|
|
|
// Otherwise, ignore the file.
|
|
|
|
if (TempStat.st_mtime != m_Stat.st_mtime)
|
|
|
|
{
|
|
|
|
// Save down the modified timestamp
|
|
|
|
m_Stat.st_mtime=TempStat.st_mtime;
|
|
|
|
return 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
};
|
|
|
|
public:
|
|
|
|
|
|
|
|
CFlagManager()
|
|
|
|
{
|
|
|
|
memset(&m_Stat,0x0,sizeof(struct stat));
|
|
|
|
m_iDisabled=0;
|
|
|
|
m_iForceRead=0;
|
|
|
|
};
|
|
|
|
~CFlagManager()
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the filename in relation to amxmodx/configs
|
|
|
|
*/
|
|
|
|
void SetFile(const char *Filename="cmdaccess.ini");
|
|
|
|
|
2015-07-11 08:37:36 +00:00
|
|
|
const char *GetFile(void) const { return m_strConfigFile.chars(); };
|
2007-03-13 00:42:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the file, and load all entries
|
|
|
|
* Returns 1 on success, 0 on refusal (no need to), and -1 on error
|
|
|
|
*/
|
|
|
|
const int LoadFile(const int force=0);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the command exists in the list
|
|
|
|
* If it does, it byrefs the flags for it
|
|
|
|
* If it does not, it adds it to the list
|
|
|
|
* These are added from register_*cmd calls
|
|
|
|
*/
|
|
|
|
void LookupOrAdd(const char *Command, int &Flags, AMX *Plugin);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write the commands back to the file
|
|
|
|
*/
|
|
|
|
void WriteCommands(void);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add this straight from the cmdaccess.ini file
|
|
|
|
*/
|
|
|
|
void AddFromFile(const char *Command, const char *Flags);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if this command should be added to flagman or not
|
|
|
|
* This is only checked when adding commands from the register_* natives
|
|
|
|
* If an admin manually adds a command to cmdaccess.ini it will be used
|
|
|
|
* regardless of whatever this function would say should be done with it
|
|
|
|
*/
|
|
|
|
int ShouldIAddThisCommand(const AMX *amx, const cell *params, const char *cmdname) const;
|
|
|
|
|
|
|
|
void Clear(void);
|
|
|
|
|
|
|
|
void CheckIfDisabled(void);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // CFLAGMANAGER_H
|