plugins/admin-flatfile/admin-users.sp
author David Anderson <dvander@alliedmods.net>
Sun Nov 25 18:50:13 2007 +0000 (2007-11-25)
changeset 1725 ea875bcbc286
parent 1432 d5001413aba7
child 1947 e73a1e51833b
permissions -rw-r--r--
small clarification to param2 in MenuAction_End
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Admin File Reader Plugin
     5  * Reads the admins.cfg file.  Do not compile this directly.
     6  *
     7  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     8  * =============================================================================
     9  *
    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.
    13  * 
    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
    17  * details.
    18  *
    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/>.
    21  *
    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>.
    30  *
    31  * Version: $Id$
    32  */
    33 
    34 #define USER_STATE_NONE			0
    35 #define USER_STATE_ADMINS		1
    36 #define USER_STATE_INADMIN		2
    37 
    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];
    43 
    44 public SMCResult:ReadUsers_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
    45 {
    46 	if (g_IgnoreLevel)
    47 	{
    48 		g_IgnoreLevel++;
    49 		return SMCParse_Continue;
    50 	}
    51 	
    52 	if (g_UserState == USER_STATE_NONE)
    53 	{
    54 		if (StrEqual(name, "Admins"))
    55 		{
    56 			g_UserState = USER_STATE_ADMINS;
    57 			g_CurUser = INVALID_ADMIN_ID;
    58 		} else {
    59 			g_IgnoreLevel++;
    60 		}
    61 	} else if (g_UserState == USER_STATE_ADMINS) {
    62 		g_UserState = USER_STATE_INADMIN;
    63 		g_CurUser = CreateAdmin(name);
    64 		g_CurAuth[0] = '\0';
    65 		g_CurIdent[0] = '\0';
    66 	} else {
    67 		g_IgnoreLevel++;
    68 	}
    69 	
    70 	return SMCParse_Continue;
    71 }
    72 
    73 public SMCResult:ReadUsers_KeyValue(Handle:smc, 
    74 									const String:key[], 
    75 									const String:value[], 
    76 									bool:key_quotes, 
    77 									bool:value_quotes)
    78 {
    79 	if (g_UserState != USER_STATE_INADMIN
    80 		|| g_IgnoreLevel
    81 		|| g_CurUser == INVALID_ADMIN_ID)
    82 	{
    83 		return SMCParse_Continue;
    84 	}
    85 	
    86 	new bool:auth = false;
    87 	
    88 	if (StrEqual(key, "auth"))
    89 	{
    90 		auth = true;
    91 		strcopy(g_CurAuth, sizeof(g_CurAuth), value);
    92 	}
    93 	else if (StrEqual(key, "identity"))
    94 	{
    95 		auth = true;
    96 		strcopy(g_CurIdent, sizeof(g_CurIdent), value);
    97 	}
    98 	else if (StrEqual(key, "password")) 
    99 	{
   100 		SetAdminPassword(g_CurUser, value);
   101 	} 
   102 	else if (StrEqual(key, "group")) 
   103 	{
   104 		new GroupId:id = FindAdmGroup(value);
   105 		if (id == INVALID_GROUP_ID)
   106 		{
   107 			ParseError("Unknown group \"%s\"", value);
   108 		} else if (!AdminInheritGroup(g_CurUser, id)) {
   109 			ParseError("Unable to inherit group \"%s\"", value);
   110 		}
   111 	} 
   112 	else if (StrEqual(key, "flags")) 
   113 	{
   114 		new len = strlen(value);
   115 		new AdminFlag:flag;
   116 		
   117 		for (new i=0; i<len; i++)
   118 		{
   119 			if (!FindFlagByChar(value[i], flag))
   120 			{
   121 				ParseError("Invalid flag detected: %c", value[i]);
   122 			}
   123 			SetAdminFlag(g_CurUser, flag, true);
   124 		}
   125 	} 
   126 	else if (StrEqual(key, "immunity")) 
   127 	{
   128 		new level = StringToInt(value);
   129 		SetAdminImmunityLevel(g_CurUser, level);
   130 	}
   131 
   132 	if (auth && g_CurIdent[0] && g_CurAuth[0])
   133 	{
   134 		if (BindAdminIdentity(g_CurUser, g_CurAuth, g_CurIdent))
   135 		{
   136 			g_CurAuth[0] = '\0';
   137 			g_CurIdent[0] = '\0';
   138 		} else {
   139 			ParseError("Failed to bind auth \"%s\" to identity \"%s\"", g_CurAuth, g_CurIdent);
   140 		}
   141 	}
   142 	
   143 	return SMCParse_Continue;
   144 }
   145 
   146 public SMCResult:ReadUsers_EndSection(Handle:smc)
   147 {
   148 	if (g_IgnoreLevel)
   149 	{
   150 		g_IgnoreLevel--;
   151 		return SMCParse_Continue;
   152 	}
   153 	
   154 	if (g_UserState == USER_STATE_INADMIN)
   155 	{
   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;
   160 	}
   161 	
   162 	return SMCParse_Continue;
   163 }
   164 
   165 public SMCResult:ReadUsers_CurrentLine(Handle:smc, const String:line[], lineno)
   166 {
   167 	g_CurrentLine = lineno;
   168 	
   169 	return SMCParse_Continue;
   170 }
   171 
   172 static InitializeUserParser()
   173 {
   174 	if (g_hUserParser == INVALID_HANDLE)
   175 	{
   176 		g_hUserParser = SMC_CreateParser();
   177 		SMC_SetReaders(g_hUserParser,
   178 					   ReadUsers_NewSection,
   179 					   ReadUsers_KeyValue,
   180 					   ReadUsers_EndSection);
   181 		SMC_SetRawLine(g_hUserParser, ReadUsers_CurrentLine);
   182 	}
   183 }
   184 
   185 ReadUsers()
   186 {
   187 	InitializeUserParser();
   188 	
   189 	BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admins.cfg");
   190 
   191 	/* Set states */
   192 	InitGlobalStates();
   193 	g_UserState = USER_STATE_NONE;
   194 		
   195 	new SMCError:err = SMC_ParseFile(g_hUserParser, g_Filename);
   196 	if (err != SMCError_Okay)
   197 	{
   198 		decl String:buffer[64];
   199 		if (SMC_GetErrorString(err, buffer, sizeof(buffer)))
   200 		{
   201 			ParseError("%s", buffer);
   202 		} else {
   203 			ParseError("Fatal parse error");
   204 		}
   205 	}
   206 }