3 * =============================================================================
4 * SourceMod Admin File Reader Plugin
5 * Reads the admins.cfg file. Do not compile this directly.
7 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
8 * =============================================================================
10 * This program is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU General Public License, version 3.0, as published by the
12 * Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
22 * As a special exception, AlliedModders LLC gives you permission to link the
23 * code of this program (as well as its derivative works) to "Half-Life 2," the
24 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
25 * by the Valve Corporation. You must obey the GNU General Public License in
26 * all respects for all other code used. Additionally, AlliedModders LLC grants
27 * this exception to all derivative works. AlliedModders LLC defines further
28 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
29 * or <http://www.sourcemod.net/license.php>.
34 #define USER_STATE_NONE 0
35 #define USER_STATE_ADMINS 1
36 #define USER_STATE_INADMIN 2
38 static Handle:g_hUserParser = INVALID_HANDLE;
39 static AdminId:g_CurUser = INVALID_ADMIN_ID;
40 static g_UserState = USER_STATE_NONE;
41 static String:g_CurAuth[64];
42 static String:g_CurIdent[64];
44 public SMCResult:ReadUsers_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
49 return SMCParse_Continue;
52 if (g_UserState == USER_STATE_NONE)
54 if (StrEqual(name, "Admins"))
56 g_UserState = USER_STATE_ADMINS;
57 g_CurUser = INVALID_ADMIN_ID;
61 } else if (g_UserState == USER_STATE_ADMINS) {
62 g_UserState = USER_STATE_INADMIN;
63 g_CurUser = CreateAdmin(name);
70 return SMCParse_Continue;
73 public SMCResult:ReadUsers_KeyValue(Handle:smc,
79 if (g_UserState != USER_STATE_INADMIN
81 || g_CurUser == INVALID_ADMIN_ID)
83 return SMCParse_Continue;
86 new bool:auth = false;
88 if (StrEqual(key, "auth"))
91 strcopy(g_CurAuth, sizeof(g_CurAuth), value);
93 else if (StrEqual(key, "identity"))
96 strcopy(g_CurIdent, sizeof(g_CurIdent), value);
98 else if (StrEqual(key, "password"))
100 SetAdminPassword(g_CurUser, value);
102 else if (StrEqual(key, "group"))
104 new GroupId:id = FindAdmGroup(value);
105 if (id == INVALID_GROUP_ID)
107 ParseError("Unknown group \"%s\"", value);
108 } else if (!AdminInheritGroup(g_CurUser, id)) {
109 ParseError("Unable to inherit group \"%s\"", value);
112 else if (StrEqual(key, "flags"))
114 new len = strlen(value);
117 for (new i=0; i<len; i++)
119 if (!FindFlagByChar(value[i], flag))
121 ParseError("Invalid flag detected: %c", value[i]);
123 SetAdminFlag(g_CurUser, flag, true);
126 else if (StrEqual(key, "immunity"))
128 new level = StringToInt(value);
129 SetAdminImmunityLevel(g_CurUser, level);
132 if (auth && g_CurIdent[0] && g_CurAuth[0])
134 if (BindAdminIdentity(g_CurUser, g_CurAuth, g_CurIdent))
137 g_CurIdent[0] = '\0';
139 ParseError("Failed to bind auth \"%s\" to identity \"%s\"", g_CurAuth, g_CurIdent);
143 return SMCParse_Continue;
146 public SMCResult:ReadUsers_EndSection(Handle:smc)
151 return SMCParse_Continue;
154 if (g_UserState == USER_STATE_INADMIN)
156 g_UserState = USER_STATE_ADMINS;
157 g_CurUser = INVALID_ADMIN_ID;
158 } else if (g_UserState == USER_STATE_ADMINS) {
159 g_UserState = USER_STATE_NONE;
162 return SMCParse_Continue;
165 public SMCResult:ReadUsers_CurrentLine(Handle:smc, const String:line[], lineno)
167 g_CurrentLine = lineno;
169 return SMCParse_Continue;
172 static InitializeUserParser()
174 if (g_hUserParser == INVALID_HANDLE)
176 g_hUserParser = SMC_CreateParser();
177 SMC_SetReaders(g_hUserParser,
178 ReadUsers_NewSection,
180 ReadUsers_EndSection);
181 SMC_SetRawLine(g_hUserParser, ReadUsers_CurrentLine);
187 InitializeUserParser();
189 BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admins.cfg");
193 g_UserState = USER_STATE_NONE;
195 new SMCError:err = SMC_ParseFile(g_hUserParser, g_Filename);
196 if (err != SMCError_Okay)
198 decl String:buffer[64];
199 if (SMC_GetErrorString(err, buffer, sizeof(buffer)))
201 ParseError("%s", buffer);
203 ParseError("Fatal parse error");