3 * Reads the admin_groups.cfg file. Do not compile this directly.
4 * This file is part of SourceMod, Copyright (C) 2004-2007 AlliedModders LLC
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #define GROUP_STATE_NONE 0
24 #define GROUP_STATE_GROUPS 1
25 #define GROUP_STATE_INGROUP 2
26 #define GROUP_STATE_OVERRIDES 3
27 #define GROUP_PASS_FIRST 1
28 #define GROUP_PASS_SECOND 2
30 static Handle:g_hGroupParser = INVALID_HANDLE;
31 static GroupId:g_CurGrp = INVALID_GROUP_ID;
32 static g_GroupState = GROUP_STATE_NONE;
33 static g_GroupPass = 0;
34 static bool:g_NeedReparse = false;
36 public SMCResult:ReadGroups_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
41 return SMCParse_Continue;
44 if (g_GroupState == GROUP_STATE_NONE)
46 if (StrEqual(name, "Groups"))
48 g_GroupState = GROUP_STATE_GROUPS;
52 } else if (g_GroupState == GROUP_STATE_GROUPS) {
53 if ((g_CurGrp = CreateAdmGroup(name)) == INVALID_GROUP_ID)
55 g_CurGrp = FindAdmGroup(name);
57 g_GroupState = GROUP_STATE_INGROUP;
58 } else if (g_GroupState == GROUP_STATE_INGROUP) {
59 if (StrEqual(name, "Overrides"))
61 g_GroupState = GROUP_STATE_OVERRIDES;
69 return SMCParse_Continue;
72 public SMCResult:ReadGroups_KeyValue(Handle:smc,
78 if (g_CurGrp == INVALID_GROUP_ID
81 return SMCParse_Continue;
86 if (g_GroupPass == GROUP_PASS_FIRST)
88 if (g_GroupState == GROUP_STATE_INGROUP)
90 if (StrEqual(key, "flags"))
92 new len = strlen(value);
93 for (new i=0; i<len; i++)
95 if (!FindFlagByChar(value[i], flag))
99 SetAdmGroupAddFlag(g_CurGrp, flag, true);
101 } else if (StrEqual(key, "immunity")) {
102 /* If it's a value we know about, use it */
103 if (StrEqual(value, "*"))
105 SetAdmGroupImmunity(g_CurGrp, Immunity_Global, true);
106 } else if (StrEqual(value, "$")) {
107 SetAdmGroupImmunity(g_CurGrp, Immunity_Default, true);
109 /* If we can't find the group, we'll need to schedule a reparse */
110 new GroupId:id = FindAdmGroup(value);
111 if (id != INVALID_GROUP_ID)
113 SetAdmGroupImmuneFrom(g_CurGrp, id);
115 g_NeedReparse = true;
119 } else if (g_GroupState == GROUP_STATE_OVERRIDES) {
120 new OverrideRule:rule = Command_Deny;
122 if (StrEqual(value, "allow", false))
124 rule = Command_Allow;
129 AddAdmGroupCmdOverride(g_CurGrp, key[1], Override_CommandGroup, rule);
131 AddAdmGroupCmdOverride(g_CurGrp, key, Override_Command, rule);
134 } else if (g_GroupPass == GROUP_PASS_SECOND
135 && g_GroupState == GROUP_STATE_INGROUP) {
136 /* Check for immunity again, core should handle double inserts */
137 if (StrEqual(key, "immunity"))
139 if (StrEqual(value, "$"))
141 SetAdmGroupImmunity(g_CurGrp, Immunity_Default, true);
142 } else if (StrEqual(value, "*")) {
143 SetAdmGroupImmunity(g_CurGrp, Immunity_Global, true);
145 new GroupId:id = FindAdmGroup(value);
146 if (id != INVALID_GROUP_ID)
148 SetAdmGroupImmuneFrom(g_CurGrp, id);
150 ParseError("Unable to find group: \"%s\"", value);
156 return SMCParse_Continue;
159 public SMCResult:ReadGroups_EndSection(Handle:smc)
161 /* If we're ignoring, skip out */
165 return SMCParse_Continue;
168 if (g_GroupState == GROUP_STATE_OVERRIDES)
170 g_GroupState = GROUP_STATE_INGROUP;
171 } else if (g_GroupState == GROUP_STATE_INGROUP) {
172 g_GroupState = GROUP_STATE_GROUPS;
173 g_CurGrp = INVALID_GROUP_ID;
174 } else if (g_GroupState == GROUP_STATE_GROUPS) {
175 g_GroupState = GROUP_STATE_NONE;
178 return SMCParse_Continue;
181 static InitializeGroupParser()
183 if (g_hGroupParser == INVALID_HANDLE)
185 g_hGroupParser = SMC_CreateParser();
186 SMC_SetReaders(g_hGroupParser,
187 ReadGroups_NewSection,
189 ReadGroups_EndSection);
193 static InternalReadGroups(const String:path[], pass)
197 g_GroupState = GROUP_STATE_NONE;
198 g_CurGrp = INVALID_GROUP_ID;
200 g_NeedReparse = false;
202 new SMCError:err = SMC_ParseFile(g_hGroupParser, path);
203 if (err != SMCError_Okay)
205 decl String:buffer[64];
206 if (SMC_GetErrorString(err, buffer, sizeof(buffer)))
208 ParseError("%s", buffer);
210 ParseError("Fatal parse error");
217 InitializeGroupParser();
219 BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admin_groups.cfg");
221 InternalReadGroups(g_Filename, GROUP_PASS_FIRST);
224 InternalReadGroups(g_Filename, GROUP_PASS_SECOND);