Base Votes import.
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.
23 #define USER_STATE_NONE 0
24 #define USER_STATE_ADMINS 1
25 #define USER_STATE_INADMIN 2
27 static Handle:g_hUserParser = INVALID_HANDLE;
28 static AdminId:g_CurUser = INVALID_ADMIN_ID;
29 static g_UserState = USER_STATE_NONE;
30 static String:g_CurAuth[64];
31 static String:g_CurIdent[64];
33 public SMCResult:ReadUsers_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
38 return SMCParse_Continue;
41 if (g_UserState == USER_STATE_NONE)
43 if (StrEqual(name, "Admins"))
45 g_UserState = USER_STATE_ADMINS;
46 g_CurUser = INVALID_ADMIN_ID;
50 } else if (g_UserState == USER_STATE_ADMINS) {
51 g_UserState = USER_STATE_INADMIN;
52 g_CurUser = CreateAdmin(name);
59 return SMCParse_Continue;
62 public SMCResult:ReadUsers_KeyValue(Handle:smc,
68 if (g_UserState != USER_STATE_INADMIN
70 || g_CurUser == INVALID_ADMIN_ID)
72 return SMCParse_Continue;
75 new bool:auth = false;
77 if (StrEqual(key, "auth"))
80 strcopy(g_CurAuth, sizeof(g_CurAuth), value);
81 } else if (StrEqual(key, "identity")) {
83 strcopy(g_CurIdent, sizeof(g_CurIdent), value);
84 } else if (StrEqual(key, "password")) {
85 SetAdminPassword(g_CurUser, value);
86 } else if (StrEqual(key, "group")) {
87 new GroupId:id = FindAdmGroup(value);
88 if (id == INVALID_GROUP_ID)
90 ParseError("Unknown group \"%s\"", value);
91 } else if (!AdminInheritGroup(g_CurUser, id)) {
92 ParseError("Unable to inherit group \"%s\"", value);
94 } else if (StrEqual(key, "flags")) {
95 new len = strlen(value);
97 for (new i=0; i<len; i++)
99 if (value[i] < 'a' || value[i] > 'z')
101 ParseError("Invalid flag detected: %c", value[i]);
104 new val = value[i] - 'a';
105 if (!g_FlagsSet[val])
107 ParseError("Invalid flag detected: %c", value[i]);
110 SetAdminFlag(g_CurUser, g_FlagLetters[val], true);
114 if (auth && g_CurIdent[0] && g_CurAuth[0])
116 if (BindAdminIdentity(g_CurUser, g_CurAuth, g_CurIdent))
119 g_CurIdent[0] = '\0';
121 ParseError("Failed to bind auth \"%s\" to identity \"%s\"", g_CurAuth, g_CurIdent);
125 return SMCParse_Continue;
128 public SMCResult:ReadUsers_EndSection(Handle:smc)
133 return SMCParse_Continue;
136 if (g_UserState == USER_STATE_INADMIN)
138 g_UserState = USER_STATE_ADMINS;
139 g_CurUser = INVALID_ADMIN_ID;
140 } else if (g_UserState == USER_STATE_ADMINS) {
141 g_UserState = USER_STATE_NONE;
144 return SMCParse_Continue;
147 static InitializeUserParser()
149 if (g_hUserParser == INVALID_HANDLE)
151 g_hUserParser = SMC_CreateParser();
152 SMC_SetReaders(g_hUserParser,
153 ReadUsers_NewSection,
155 ReadUsers_EndSection);
161 InitializeUserParser();
163 BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admins.cfg");
167 g_UserState = USER_STATE_NONE;
169 new SMCError:err = SMC_ParseFile(g_hUserParser, g_Filename);
170 if (err != SMCError_Okay)
172 decl String:buffer[64];
173 if (SMC_GetErrorString(err, buffer, sizeof(buffer)))
175 ParseError("%s", buffer);
177 ParseError("Fatal parse error");