plugins/admin-flatfile/admin-users.sp
author Scott Ehlert <ds@alliedmods.net>
Tue May 01 05:29:37 2007 +0000 (2007-05-01)
changeset 714 93b19876407e
parent 460 0be67c5566c6
child 908 cd411e8827c4
permissions -rw-r--r--
- Removed GetConVarMin/Max() and replaced them with GetConVarBounds()
- Added SetConVarBounds() to set convar constraints after convar has already been created
     1 /**
     2  * admin-users.sp
     3  * Reads the admins.cfg file.  Do not compile this directly.
     4  * This file is part of SourceMod, Copyright (C) 2004-2007 AlliedModders LLC
     5  *
     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.
    10  *
    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.
    15  * 
    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.
    19  */
    20 
    21 #define USER_STATE_NONE			0
    22 #define USER_STATE_ADMINS		1
    23 #define USER_STATE_INADMIN		2
    24 
    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];
    30 
    31 public SMCResult:ReadUsers_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
    32 {
    33 	if (g_IgnoreLevel)
    34 	{
    35 		g_IgnoreLevel++;
    36 		return SMCParse_Continue;
    37 	}
    38 	
    39 	if (g_UserState == USER_STATE_NONE)
    40 	{
    41 		if (StrEqual(name, "Admins"))
    42 		{
    43 			g_UserState = USER_STATE_ADMINS;
    44 			g_CurUser = INVALID_ADMIN_ID;
    45 		} else {
    46 			g_IgnoreLevel++;
    47 		}
    48 	} else if (g_UserState == USER_STATE_ADMINS) {
    49 		g_UserState = USER_STATE_INADMIN;
    50 		g_CurUser = CreateAdmin(name);
    51 		g_CurAuth[0] = '\0';
    52 		g_CurIdent[0] = '\0';
    53 	} else {
    54 		g_IgnoreLevel++;
    55 	}
    56 	
    57 	return SMCParse_Continue;
    58 }
    59 
    60 public SMCResult:ReadUsers_KeyValue(Handle:smc, 
    61 									const String:key[], 
    62 									const String:value[], 
    63 									bool:key_quotes, 
    64 									bool:value_quotes)
    65 {
    66 	if (g_UserState != USER_STATE_INADMIN
    67 		|| g_IgnoreLevel
    68 		|| g_CurUser == INVALID_ADMIN_ID)
    69 	{
    70 		return SMCParse_Continue;
    71 	}
    72 	
    73 	new bool:auth = false;
    74 	
    75 	if (StrEqual(key, "auth"))
    76 	{
    77 		auth = true;
    78 		StrCopy(g_CurAuth, sizeof(g_CurAuth), value);
    79 	} else if (StrEqual(key, "identity")) {
    80 		auth = true;
    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)
    87 		{
    88 			ParseError("Unknown group \"%s\"", value);
    89 		} else if (!AdminInheritGroup(g_CurUser, id)) {
    90 			ParseError("Unable to inherit group \"%s\"", value);
    91 		}
    92 	} else if (StrEqual(key, "flags")) {
    93 		new len = strlen(value);
    94 		
    95 		for (new i=0; i<len; i++)
    96 		{
    97 			if (value[i] < 'a' || value[i] > 'z')
    98 			{
    99 				ParseError("Invalid flag detected: %c", value[i]);
   100 				continue;
   101 			}
   102 			new val = value[i] - 'a';
   103 			if (!g_FlagsSet[val])
   104 			{
   105 				ParseError("Invalid flag detected: %c", value[i]);
   106 				continue;
   107 			}
   108 			SetAdminFlag(g_CurUser, g_FlagLetters[val], true);
   109 		}
   110 	}
   111 
   112 	if (auth && g_CurIdent[0] && g_CurAuth[0])
   113 	{
   114 		if (BindAdminIdentity(g_CurUser, g_CurAuth, g_CurIdent))
   115 		{
   116 			g_CurAuth[0] = '\0';
   117 			g_CurIdent[0] = '\0';
   118 		} else {
   119 			ParseError("Failed to bind auth \"%s\" to identity \"%s\"", g_CurAuth, g_CurIdent);
   120 		}
   121 	}
   122 	
   123 	return SMCParse_Continue;
   124 }
   125 
   126 public SMCResult:ReadUsers_EndSection(Handle:smc)
   127 {
   128 	if (g_IgnoreLevel)
   129 	{
   130 		g_IgnoreLevel--;
   131 		return SMCParse_Continue;
   132 	}
   133 	
   134 	if (g_UserState == USER_STATE_INADMIN)
   135 	{
   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;
   140 	}
   141 	
   142 	return SMCParse_Continue;
   143 }
   144 
   145 static InitializeUserParser()
   146 {
   147 	if (g_hUserParser == INVALID_HANDLE)
   148 	{
   149 		g_hUserParser = SMC_CreateParser();
   150 		SMC_SetReaders(g_hUserParser,
   151 					   ReadUsers_NewSection,
   152 					   ReadUsers_KeyValue,
   153 					   ReadUsers_EndSection);
   154 	}
   155 }
   156 
   157 ReadUsers()
   158 {
   159 	InitializeUserParser();
   160 	
   161 	BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admins.cfg");
   162 
   163 	/* Set states */
   164 	InitGlobalStates();
   165 	g_UserState = USER_STATE_NONE;
   166 		
   167 	new SMCError:err = SMC_ParseFile(g_hUserParser, g_Filename);
   168 	if (err != SMCError_Okay)
   169 	{
   170 		decl String:buffer[64];
   171 		if (SMC_GetErrorString(err, buffer, sizeof(buffer)))
   172 		{
   173 			ParseError("%s", buffer);
   174 		} else {
   175 			ParseError("Fatal parse error");
   176 		}
   177 	}
   178 }