jit refactoring branch

--HG--
branch : refac-jit
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/branches/refac-jit%402369
This commit is contained in:
David Anderson
2008-07-06 04:49:55 +00:00
commit 2e7b6b5ba5
879 changed files with 297155 additions and 0 deletions
+96
View File
@@ -0,0 +1,96 @@
/**
* vim: set ts=4 :
* =============================================================================
* SourceMod Admin File Reader Plugin
* Manages the standard flat files for admins. This is the file to compile.
*
* SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
/* We like semicolons */
#pragma semicolon 1
#include <sourcemod>
public Plugin:myinfo =
{
name = "Admin File Reader",
author = "AlliedModders LLC",
description = "Reads admin files",
version = SOURCEMOD_VERSION,
url = "http://www.sourcemod.net/"
};
/** Various parsing globals */
new bool:g_LoggedFileName = false; /* Whether or not the file name has been logged */
new g_ErrorCount = 0; /* Current error count */
new g_IgnoreLevel = 0; /* Nested ignored section count, so users can screw up files safely */
new g_CurrentLine = 0; /* Current line we're on */
new String:g_Filename[PLATFORM_MAX_PATH]; /* Used for error messages */
#include "admin-overrides.sp"
#include "admin-groups.sp"
#include "admin-users.sp"
#include "admin-simple.sp"
public OnRebuildAdminCache(AdminCachePart:part)
{
if (part == AdminCache_Overrides)
{
ReadOverrides();
} else if (part == AdminCache_Groups) {
ReadGroups();
} else if (part == AdminCache_Admins) {
ReadUsers();
ReadSimpleUsers();
}
}
ParseError(const String:format[], {Handle,String,Float,_}:...)
{
decl String:buffer[512];
if (!g_LoggedFileName)
{
LogError("Error(s) detected parsing %s", g_Filename);
g_LoggedFileName = true;
}
VFormat(buffer, sizeof(buffer), format, 2);
LogError(" (line %d) %s", g_CurrentLine, buffer);
g_ErrorCount++;
}
InitGlobalStates()
{
g_ErrorCount = 0;
g_IgnoreLevel = 0;
g_CurrentLine = 0;
g_LoggedFileName = false;
}
+244
View File
@@ -0,0 +1,244 @@
/**
* vim: set ts=4 :
* =============================================================================
* SourceMod Admin File Reader Plugin
* Reads the admin_groups.cfg file. Do not compile this directly.
*
* SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#define GROUP_STATE_NONE 0
#define GROUP_STATE_GROUPS 1
#define GROUP_STATE_INGROUP 2
#define GROUP_STATE_OVERRIDES 3
#define GROUP_PASS_FIRST 1
#define GROUP_PASS_SECOND 2
static Handle:g_hGroupParser = INVALID_HANDLE;
static GroupId:g_CurGrp = INVALID_GROUP_ID;
static g_GroupState = GROUP_STATE_NONE;
static g_GroupPass = 0;
static bool:g_NeedReparse = false;
public SMCResult:ReadGroups_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
{
if (g_IgnoreLevel)
{
g_IgnoreLevel++;
return SMCParse_Continue;
}
if (g_GroupState == GROUP_STATE_NONE)
{
if (StrEqual(name, "Groups"))
{
g_GroupState = GROUP_STATE_GROUPS;
} else {
g_IgnoreLevel++;
}
} else if (g_GroupState == GROUP_STATE_GROUPS) {
if ((g_CurGrp = CreateAdmGroup(name)) == INVALID_GROUP_ID)
{
g_CurGrp = FindAdmGroup(name);
}
g_GroupState = GROUP_STATE_INGROUP;
} else if (g_GroupState == GROUP_STATE_INGROUP) {
if (StrEqual(name, "Overrides"))
{
g_GroupState = GROUP_STATE_OVERRIDES;
} else {
g_IgnoreLevel++;
}
} else {
g_IgnoreLevel++;
}
return SMCParse_Continue;
}
public SMCResult:ReadGroups_KeyValue(Handle:smc,
const String:key[],
const String:value[],
bool:key_quotes,
bool:value_quotes)
{
if (g_CurGrp == INVALID_GROUP_ID
|| g_IgnoreLevel)
{
return SMCParse_Continue;
}
new AdminFlag:flag;
if (g_GroupPass == GROUP_PASS_FIRST)
{
if (g_GroupState == GROUP_STATE_INGROUP)
{
if (StrEqual(key, "flags"))
{
new len = strlen(value);
for (new i=0; i<len; i++)
{
if (!FindFlagByChar(value[i], flag))
{
continue;
}
SetAdmGroupAddFlag(g_CurGrp, flag, true);
}
} else if (StrEqual(key, "immunity")) {
g_NeedReparse = true;
}
} else if (g_GroupState == GROUP_STATE_OVERRIDES) {
new OverrideRule:rule = Command_Deny;
if (StrEqual(value, "allow", false))
{
rule = Command_Allow;
}
if (key[0] == '@')
{
AddAdmGroupCmdOverride(g_CurGrp, key[1], Override_CommandGroup, rule);
} else {
AddAdmGroupCmdOverride(g_CurGrp, key, Override_Command, rule);
}
}
} else if (g_GroupPass == GROUP_PASS_SECOND
&& g_GroupState == GROUP_STATE_INGROUP) {
/* Check for immunity again, core should handle double inserts */
if (StrEqual(key, "immunity"))
{
/* If it's a value we know about, use it */
if (StrEqual(value, "*"))
{
SetAdmGroupImmunityLevel(g_CurGrp, 2);
} else if (StrEqual(value, "$")) {
SetAdmGroupImmunityLevel(g_CurGrp, 1);
} else {
new level;
if (StringToIntEx(value, level))
{
SetAdmGroupImmunityLevel(g_CurGrp, level);
} else {
new GroupId:id;
if (value[0] == '@')
{
id = FindAdmGroup(value[1]);
} else {
id = FindAdmGroup(value);
}
if (id != INVALID_GROUP_ID)
{
SetAdmGroupImmuneFrom(g_CurGrp, id);
} else {
ParseError("Unable to find group: \"%s\"", value);
}
}
}
}
}
return SMCParse_Continue;
}
public SMCResult:ReadGroups_EndSection(Handle:smc)
{
/* If we're ignoring, skip out */
if (g_IgnoreLevel)
{
g_IgnoreLevel--;
return SMCParse_Continue;
}
if (g_GroupState == GROUP_STATE_OVERRIDES)
{
g_GroupState = GROUP_STATE_INGROUP;
} else if (g_GroupState == GROUP_STATE_INGROUP) {
g_GroupState = GROUP_STATE_GROUPS;
g_CurGrp = INVALID_GROUP_ID;
} else if (g_GroupState == GROUP_STATE_GROUPS) {
g_GroupState = GROUP_STATE_NONE;
}
return SMCParse_Continue;
}
public SMCResult:ReadGroups_CurrentLine(Handle:smc, const String:line[], lineno)
{
g_CurrentLine = lineno;
return SMCParse_Continue;
}
static InitializeGroupParser()
{
if (g_hGroupParser == INVALID_HANDLE)
{
g_hGroupParser = SMC_CreateParser();
SMC_SetReaders(g_hGroupParser,
ReadGroups_NewSection,
ReadGroups_KeyValue,
ReadGroups_EndSection);
SMC_SetRawLine(g_hGroupParser, ReadGroups_CurrentLine);
}
}
static InternalReadGroups(const String:path[], pass)
{
/* Set states */
InitGlobalStates();
g_GroupState = GROUP_STATE_NONE;
g_CurGrp = INVALID_GROUP_ID;
g_GroupPass = pass;
g_NeedReparse = false;
new SMCError:err = SMC_ParseFile(g_hGroupParser, path);
if (err != SMCError_Okay)
{
decl String:buffer[64];
if (SMC_GetErrorString(err, buffer, sizeof(buffer)))
{
ParseError("%s", buffer);
} else {
ParseError("Fatal parse error");
}
}
}
ReadGroups()
{
InitializeGroupParser();
BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admin_groups.cfg");
InternalReadGroups(g_Filename, GROUP_PASS_FIRST);
if (g_NeedReparse)
{
InternalReadGroups(g_Filename, GROUP_PASS_SECOND);
}
}
+213
View File
@@ -0,0 +1,213 @@
/**
* vim: set ts=4 :
* =============================================================================
* SourceMod Admin File Reader Plugin
* Reads overrides from the admin_levels.cfg file. Do not compile
* this directly.
*
* SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#define OVERRIDE_STATE_NONE 0
#define OVERRIDE_STATE_LEVELS 1
#define OVERRIDE_STATE_OVERRIDES 2
static Handle:g_hOldOverrideParser = INVALID_HANDLE;
static Handle:g_hNewOverrideParser = INVALID_HANDLE;
static g_OverrideState = OVERRIDE_STATE_NONE;
public SMCResult:ReadOldOverrides_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
{
if (g_IgnoreLevel)
{
g_IgnoreLevel++;
return SMCParse_Continue;
}
if (g_OverrideState == OVERRIDE_STATE_NONE)
{
if (StrEqual(name, "Levels"))
{
g_OverrideState = OVERRIDE_STATE_LEVELS;
} else {
g_IgnoreLevel++;
}
} else if (g_OverrideState == OVERRIDE_STATE_LEVELS) {
if (StrEqual(name, "Overrides"))
{
g_OverrideState = OVERRIDE_STATE_OVERRIDES;
} else {
g_IgnoreLevel++;
}
} else {
g_IgnoreLevel++;
}
return SMCParse_Continue;
}
public SMCResult:ReadNewOverrides_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
{
if (g_IgnoreLevel)
{
g_IgnoreLevel++;
return SMCParse_Continue;
}
if (g_OverrideState == OVERRIDE_STATE_NONE)
{
if (StrEqual(name, "Overrides"))
{
g_OverrideState = OVERRIDE_STATE_OVERRIDES;
} else {
g_IgnoreLevel++;
}
} else {
g_IgnoreLevel++;
}
return SMCParse_Continue;
}
public SMCResult:ReadOverrides_KeyValue(Handle:smc,
const String:key[],
const String:value[],
bool:key_quotes,
bool:value_quotes)
{
if (g_OverrideState != OVERRIDE_STATE_OVERRIDES
|| g_IgnoreLevel)
{
return SMCParse_Continue;
}
new flags = ReadFlagString(value);
if (key[0] == '@')
{
AddCommandOverride(key[1], Override_CommandGroup, flags);
} else {
AddCommandOverride(key, Override_Command, flags);
}
return SMCParse_Continue;
}
public SMCResult:ReadOldOverrides_EndSection(Handle:smc)
{
/* If we're ignoring, skip out */
if (g_IgnoreLevel)
{
g_IgnoreLevel--;
return SMCParse_Continue;
}
if (g_OverrideState == OVERRIDE_STATE_LEVELS)
{
g_OverrideState = OVERRIDE_STATE_NONE;
} else if (g_OverrideState == OVERRIDE_STATE_OVERRIDES) {
/* We're totally done parsing */
g_OverrideState = OVERRIDE_STATE_LEVELS;
return SMCParse_Halt;
}
return SMCParse_Continue;
}
public SMCResult:ReadNewOverrides_EndSection(Handle:smc)
{
/* If we're ignoring, skip out */
if (g_IgnoreLevel)
{
g_IgnoreLevel--;
return SMCParse_Continue;
}
if (g_OverrideState == OVERRIDE_STATE_OVERRIDES)
{
g_OverrideState = OVERRIDE_STATE_NONE;
}
return SMCParse_Continue;
}
public SMCResult:ReadOverrides_CurrentLine(Handle:smc, const String:line[], lineno)
{
g_CurrentLine = lineno;
return SMCParse_Continue;
}
static InitializeOverrideParsers()
{
if (g_hOldOverrideParser == INVALID_HANDLE)
{
g_hOldOverrideParser = SMC_CreateParser();
SMC_SetReaders(g_hOldOverrideParser,
ReadOldOverrides_NewSection,
ReadOverrides_KeyValue,
ReadOldOverrides_EndSection);
SMC_SetRawLine(g_hOldOverrideParser, ReadOverrides_CurrentLine);
}
if (g_hNewOverrideParser == INVALID_HANDLE)
{
g_hNewOverrideParser = SMC_CreateParser();
SMC_SetReaders(g_hNewOverrideParser,
ReadNewOverrides_NewSection,
ReadOverrides_KeyValue,
ReadNewOverrides_EndSection);
SMC_SetRawLine(g_hNewOverrideParser, ReadOverrides_CurrentLine);
}
}
InternalReadOverrides(Handle:parser, const String:file[])
{
BuildPath(Path_SM, g_Filename, sizeof(g_Filename), file);
/* Set states */
InitGlobalStates();
g_OverrideState = OVERRIDE_STATE_NONE;
new SMCError:err = SMC_ParseFile(parser, g_Filename);
if (err != SMCError_Okay)
{
decl String:buffer[64];
if (SMC_GetErrorString(err, buffer, sizeof(buffer)))
{
ParseError("%s", buffer);
} else {
ParseError("Fatal parse error");
}
}
}
ReadOverrides()
{
InitializeOverrideParsers();
InternalReadOverrides(g_hOldOverrideParser, "configs/admin_levels.cfg");
InternalReadOverrides(g_hNewOverrideParser, "configs/admin_overrides.cfg");
}
+223
View File
@@ -0,0 +1,223 @@
/**
* vim: set ts=4 :
* =============================================================================
* SourceMod Admin File Reader Plugin
* Reads the admins.cfg file. Do not compile this directly.
*
* SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
public ReadSimpleUsers()
{
BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admins_simple.ini");
new Handle:file = OpenFile(g_Filename, "rt");
if (file == INVALID_HANDLE)
{
ParseError("Could not open file!");
return;
}
while (!IsEndOfFile(file))
{
decl String:line[255];
if (!ReadFileLine(file, line, sizeof(line)))
{
break;
}
/* Trim comments */
new len = strlen(line);
new bool:ignoring = false;
for (new i=0; i<len; i++)
{
if (ignoring)
{
if (line[i] == '"')
{
ignoring = false;
}
} else {
if (line[i] == '"')
{
ignoring = true;
} else if (line[i] == ';') {
line[i] = '\0';
break;
} else if (line[i] == '/'
&& i != len - 1
&& line[i+1] == '/')
{
line[i] = '\0';
break;
}
}
}
TrimString(line);
if ((line[0] == '/' && line[1] == '/')
|| (line[0] == ';' || line[0] == '\0'))
{
continue;
}
ReadAdminLine(line);
}
CloseHandle(file);
}
DecodeAuthMethod(const String:auth[], String:method[32], &offset)
{
if (StrContains(auth, "STEAM_") == 0)
{
strcopy(method, sizeof(method), AUTHMETHOD_STEAM);
offset = 0;
}
else
{
if (auth[0] == '!')
{
strcopy(method, sizeof(method), AUTHMETHOD_IP);
offset = 1;
}
else
{
strcopy(method, sizeof(method), AUTHMETHOD_NAME);
offset = 0;
}
}
}
ReadAdminLine(const String:line[])
{
new bool:is_bound;
new AdminId:admin;
new String:auth[64];
decl String:auth_method[32];
new idx, cur_idx, auth_offset;
if ((cur_idx = BreakString(line, auth, sizeof(auth))) == -1)
{
/* This line is bad... we need at least two parameters */
return;
}
idx = cur_idx;
/* Check if we can bind beforehand */
DecodeAuthMethod(auth, auth_method, auth_offset);
if ((admin = FindAdminByIdentity(auth_method, auth[auth_offset])) == INVALID_ADMIN_ID)
{
/* There is no binding, create the admin */
admin = CreateAdmin();
}
else
{
is_bound = true;
}
/* Read flags */
new String:flags[64];
cur_idx = BreakString(line[idx], flags, sizeof(flags));
idx += cur_idx;
/* Read immunity level, if any */
new level, flag_idx;
if ((flag_idx = StringToIntEx(flags, level)) > 0)
{
SetAdminImmunityLevel(admin, level);
if (flags[flag_idx] == ':')
{
flag_idx++;
}
}
if (flags[flag_idx] == '@')
{
new GroupId:gid = FindAdmGroup(flags[flag_idx + 1]);
if (gid == INVALID_GROUP_ID)
{
ParseError("Invalid group detected: %s", flags[flag_idx + 1]);
return;
}
AdminInheritGroup(admin, gid);
}
else
{
new len = strlen(flags[flag_idx]);
new bool:is_default = false;
for (new i=0; i<len; i++)
{
if (!level && flags[flag_idx + i] == '$')
{
SetAdminImmunityLevel(admin, 1);
} else {
new AdminFlag:flag;
if (!FindFlagByChar(flags[flag_idx + i], flag))
{
ParseError("Invalid flag detected: %c", flags[flag_idx + i]);
continue;
}
SetAdminFlag(admin, flag, true);
}
}
if (is_default)
{
new GroupId:gid = FindAdmGroup("Default");
if (gid != INVALID_GROUP_ID)
{
AdminInheritGroup(admin, gid);
}
}
}
/* Lastly, is there a password? */
if (cur_idx != -1)
{
decl String:password[64];
BreakString(line[idx], password, sizeof(password));
SetAdminPassword(admin, password);
}
/* Now, bind the identity to something */
if (!is_bound)
{
if (!BindAdminIdentity(admin, auth_method, auth[auth_offset]))
{
/* We should never reach here */
ParseError("Failed to bind identity %s (method %s)", auth[auth_offset], auth_method);
}
}
}
+247
View File
@@ -0,0 +1,247 @@
/**
* vim: set ts=4 :
* =============================================================================
* SourceMod Admin File Reader Plugin
* Reads the admins.cfg file. Do not compile this directly.
*
* SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#define USER_STATE_NONE 0
#define USER_STATE_ADMINS 1
#define USER_STATE_INADMIN 2
static Handle:g_hUserParser = INVALID_HANDLE;
static g_UserState = USER_STATE_NONE;
static String:g_CurAuth[64];
static String:g_CurIdent[64];
static String:g_CurName[64];
static String:g_CurPass[64];
static Handle:g_GroupArray;
static g_CurFlags;
static g_CurImmunity;
public SMCResult:ReadUsers_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
{
if (g_IgnoreLevel)
{
g_IgnoreLevel++;
return SMCParse_Continue;
}
if (g_UserState == USER_STATE_NONE)
{
if (StrEqual(name, "Admins"))
{
g_UserState = USER_STATE_ADMINS;
}
else
{
g_IgnoreLevel++;
}
}
else if (g_UserState == USER_STATE_ADMINS)
{
g_UserState = USER_STATE_INADMIN;
strcopy(g_CurName, sizeof(g_CurName), name);
g_CurAuth[0] = '\0';
g_CurIdent[0] = '\0';
g_CurPass[0] = '\0';
ClearArray(g_GroupArray);
g_CurFlags = 0;
g_CurImmunity = 0;
}
else
{
g_IgnoreLevel++;
}
return SMCParse_Continue;
}
public SMCResult:ReadUsers_KeyValue(Handle:smc,
const String:key[],
const String:value[],
bool:key_quotes,
bool:value_quotes)
{
if (g_UserState != USER_STATE_INADMIN || g_IgnoreLevel)
{
return SMCParse_Continue;
}
if (StrEqual(key, "auth"))
{
strcopy(g_CurAuth, sizeof(g_CurAuth), value);
}
else if (StrEqual(key, "identity"))
{
strcopy(g_CurIdent, sizeof(g_CurIdent), value);
}
else if (StrEqual(key, "password"))
{
strcopy(g_CurPass, sizeof(g_CurPass), value);
}
else if (StrEqual(key, "group"))
{
new GroupId:id = FindAdmGroup(value);
if (id == INVALID_GROUP_ID)
{
ParseError("Unknown group \"%s\"", value);
}
PushArrayCell(g_GroupArray, id);
}
else if (StrEqual(key, "flags"))
{
new len = strlen(value);
new AdminFlag:flag;
for (new i = 0; i < len; i++)
{
if (!FindFlagByChar(value[i], flag))
{
ParseError("Invalid flag detected: %c", value[i]);
}
else
{
g_CurFlags |= FlagToBit(flag);
}
}
}
else if (StrEqual(key, "immunity"))
{
g_CurImmunity = StringToInt(value);
}
return SMCParse_Continue;
}
public SMCResult:ReadUsers_EndSection(Handle:smc)
{
if (g_IgnoreLevel)
{
g_IgnoreLevel--;
return SMCParse_Continue;
}
if (g_UserState == USER_STATE_INADMIN)
{
/* Dump this user to memory */
if (g_CurIdent[0] != '\0' && g_CurAuth[0] != '\0')
{
decl AdminFlag:flags[26];
new AdminId:id, i, num_groups, num_flags;
if ((id = FindAdminByIdentity(g_CurAuth, g_CurIdent)) == INVALID_ADMIN_ID)
{
id = CreateAdmin(g_CurName);
if (!BindAdminIdentity(id, g_CurAuth, g_CurIdent))
{
ParseError("Failed to bind auth \"%s\" to identity \"%s\"", g_CurAuth, g_CurIdent);
}
}
num_groups = GetArraySize(g_GroupArray);
for (i = 0; i < num_groups; i++)
{
AdminInheritGroup(id, GetArrayCell(g_GroupArray, i));
}
SetAdminPassword(id, g_CurPass);
if (GetAdminImmunityLevel(id) < g_CurImmunity)
{
SetAdminImmunityLevel(id, g_CurImmunity);
}
num_flags = FlagBitsToArray(g_CurFlags, flags, sizeof(flags));
for (i = 0; i < num_flags; i++)
{
SetAdminFlag(id, flags[i], true);
}
}
else
{
ParseError("Failed to create admin: did you forget either the auth or identity properties?");
}
g_UserState = USER_STATE_ADMINS;
}
else if (g_UserState == USER_STATE_ADMINS)
{
g_UserState = USER_STATE_NONE;
}
return SMCParse_Continue;
}
public SMCResult:ReadUsers_CurrentLine(Handle:smc, const String:line[], lineno)
{
g_CurrentLine = lineno;
return SMCParse_Continue;
}
static InitializeUserParser()
{
if (g_hUserParser == INVALID_HANDLE)
{
g_hUserParser = SMC_CreateParser();
SMC_SetReaders(g_hUserParser,
ReadUsers_NewSection,
ReadUsers_KeyValue,
ReadUsers_EndSection);
SMC_SetRawLine(g_hUserParser, ReadUsers_CurrentLine);
g_GroupArray = CreateArray();
}
}
ReadUsers()
{
InitializeUserParser();
BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admins.cfg");
/* Set states */
InitGlobalStates();
g_UserState = USER_STATE_NONE;
new SMCError:err = SMC_ParseFile(g_hUserParser, g_Filename);
if (err != SMCError_Okay)
{
decl String:buffer[64];
if (SMC_GetErrorString(err, buffer, sizeof(buffer)))
{
ParseError("%s", buffer);
}
else
{
ParseError("Fatal parse error");
}
}
}