3 * Reads the admins.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.
21 #define USER_STATE_NONE 0
22 #define USER_STATE_ADMINS 1
23 #define USER_STATE_INADMIN 2
25 static Handle:g_hUserParser = INVALID_HANDLE;
26 static AdminId:g_CurUser = INVALID_ADMIN_ID;
27 static g_UserState = USER_STATE_NONE;
28 static String:g_CurAuth[64];
29 static String:g_CurIdent[64];
31 public SMCResult:ReadUsers_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
36 return SMCParse_Continue;
39 if (g_UserState == USER_STATE_NONE)
41 if (StrEqual(name, "Admins"))
43 g_UserState = USER_STATE_ADMINS;
44 g_CurUser = INVALID_ADMIN_ID;
48 } else if (g_UserState == USER_STATE_ADMINS) {
49 g_UserState = USER_STATE_INADMIN;
50 g_CurUser = CreateAdmin(name);
57 return SMCParse_Continue;
60 public SMCResult:ReadUsers_KeyValue(Handle:smc,
66 if (g_UserState != USER_STATE_INADMIN
68 || g_CurUser == INVALID_ADMIN_ID)
70 return SMCParse_Continue;
73 new bool:auth = false;
75 if (StrEqual(key, "auth"))
78 StrCopy(g_CurAuth, sizeof(g_CurAuth), value);
79 } else if (StrEqual(key, "identity")) {
81 StrCopy(g_CurIdent, sizeof(g_CurIdent), value);
82 } else if (StrEqual(key, "password")) {
83 SetAdminPassword(g_CurUser, value);
84 } else if (StrEqual(key, "group")) {
85 new GroupId:id = FindAdmGroup(value);
86 if (id == INVALID_GROUP_ID)
88 ParseError("Unknown group \"%s\"", value);
89 } else if (!AdminInheritGroup(g_CurUser, id)) {
90 ParseError("Unable to inherit group \"%s\"", value);
92 } else if (StrEqual(key, "flags")) {
93 new len = strlen(value);
95 for (new i=0; i<len; i++)
97 if (value[i] < 'a' || value[i] > 'z')
99 ParseError("Invalid flag detected: %c", value[i]);
102 new val = value[i] - 'a';
103 if (!g_FlagsSet[val])
105 ParseError("Invalid flag detected: %c", value[i]);
108 SetAdminFlag(g_CurUser, g_FlagLetters[val], true);
112 if (auth && g_CurIdent[0] && g_CurAuth[0])
114 if (BindAdminIdentity(g_CurUser, g_CurAuth, g_CurIdent))
117 g_CurIdent[0] = '\0';
119 ParseError("Failed to bind auth \"%s\" to identity \"%s\"", g_CurAuth, g_CurIdent);
123 return SMCParse_Continue;
126 public SMCResult:ReadUsers_EndSection(Handle:smc)
131 return SMCParse_Continue;
134 if (g_UserState == USER_STATE_INADMIN)
136 g_UserState = USER_STATE_ADMINS;
137 g_CurUser = INVALID_ADMIN_ID;
138 } else if (g_UserState == USER_STATE_ADMINS) {
139 g_UserState = USER_STATE_NONE;
142 return SMCParse_Continue;
145 static InitializeUserParser()
147 if (g_hUserParser == INVALID_HANDLE)
149 g_hUserParser = SMC_CreateParser();
150 SMC_SetReaders(g_hUserParser,
151 ReadUsers_NewSection,
153 ReadUsers_EndSection);
159 InitializeUserParser();
161 BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admins.cfg");
165 g_UserState = USER_STATE_NONE;
167 new SMCError:err = SMC_ParseFile(g_hUserParser, g_Filename);
168 if (err != SMCError_Okay)
170 decl String:buffer[64];
171 if (SMC_GetErrorString(err, buffer, sizeof(buffer)))
173 ParseError("%s", buffer);
175 ParseError("Fatal parse error");