plugins/admin-flatfile/admin-users.sp
author David Anderson <dvander@alliedmods.net>
Sat Jun 30 16:43:11 2007 +0000 (2007-06-30)
changeset 1028 ec73814041b4
parent 908 cd411e8827c4
child 1268 9360f5ea2289
permissions -rw-r--r--
- added IADTFactory for sdktools
- updated sample extension and sdktools sdks
     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  * Version: $Id$
    21  */
    22 
    23 #define USER_STATE_NONE			0
    24 #define USER_STATE_ADMINS		1
    25 #define USER_STATE_INADMIN		2
    26 
    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];
    32 
    33 public SMCResult:ReadUsers_NewSection(Handle:smc, const String:name[], bool:opt_quotes)
    34 {
    35 	if (g_IgnoreLevel)
    36 	{
    37 		g_IgnoreLevel++;
    38 		return SMCParse_Continue;
    39 	}
    40 	
    41 	if (g_UserState == USER_STATE_NONE)
    42 	{
    43 		if (StrEqual(name, "Admins"))
    44 		{
    45 			g_UserState = USER_STATE_ADMINS;
    46 			g_CurUser = INVALID_ADMIN_ID;
    47 		} else {
    48 			g_IgnoreLevel++;
    49 		}
    50 	} else if (g_UserState == USER_STATE_ADMINS) {
    51 		g_UserState = USER_STATE_INADMIN;
    52 		g_CurUser = CreateAdmin(name);
    53 		g_CurAuth[0] = '\0';
    54 		g_CurIdent[0] = '\0';
    55 	} else {
    56 		g_IgnoreLevel++;
    57 	}
    58 	
    59 	return SMCParse_Continue;
    60 }
    61 
    62 public SMCResult:ReadUsers_KeyValue(Handle:smc, 
    63 									const String:key[], 
    64 									const String:value[], 
    65 									bool:key_quotes, 
    66 									bool:value_quotes)
    67 {
    68 	if (g_UserState != USER_STATE_INADMIN
    69 		|| g_IgnoreLevel
    70 		|| g_CurUser == INVALID_ADMIN_ID)
    71 	{
    72 		return SMCParse_Continue;
    73 	}
    74 	
    75 	new bool:auth = false;
    76 	
    77 	if (StrEqual(key, "auth"))
    78 	{
    79 		auth = true;
    80 		StrCopy(g_CurAuth, sizeof(g_CurAuth), value);
    81 	} else if (StrEqual(key, "identity")) {
    82 		auth = true;
    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)
    89 		{
    90 			ParseError("Unknown group \"%s\"", value);
    91 		} else if (!AdminInheritGroup(g_CurUser, id)) {
    92 			ParseError("Unable to inherit group \"%s\"", value);
    93 		}
    94 	} else if (StrEqual(key, "flags")) {
    95 		new len = strlen(value);
    96 		
    97 		for (new i=0; i<len; i++)
    98 		{
    99 			if (value[i] < 'a' || value[i] > 'z')
   100 			{
   101 				ParseError("Invalid flag detected: %c", value[i]);
   102 				continue;
   103 			}
   104 			new val = value[i] - 'a';
   105 			if (!g_FlagsSet[val])
   106 			{
   107 				ParseError("Invalid flag detected: %c", value[i]);
   108 				continue;
   109 			}
   110 			SetAdminFlag(g_CurUser, g_FlagLetters[val], true);
   111 		}
   112 	}
   113 
   114 	if (auth && g_CurIdent[0] && g_CurAuth[0])
   115 	{
   116 		if (BindAdminIdentity(g_CurUser, g_CurAuth, g_CurIdent))
   117 		{
   118 			g_CurAuth[0] = '\0';
   119 			g_CurIdent[0] = '\0';
   120 		} else {
   121 			ParseError("Failed to bind auth \"%s\" to identity \"%s\"", g_CurAuth, g_CurIdent);
   122 		}
   123 	}
   124 	
   125 	return SMCParse_Continue;
   126 }
   127 
   128 public SMCResult:ReadUsers_EndSection(Handle:smc)
   129 {
   130 	if (g_IgnoreLevel)
   131 	{
   132 		g_IgnoreLevel--;
   133 		return SMCParse_Continue;
   134 	}
   135 	
   136 	if (g_UserState == USER_STATE_INADMIN)
   137 	{
   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;
   142 	}
   143 	
   144 	return SMCParse_Continue;
   145 }
   146 
   147 static InitializeUserParser()
   148 {
   149 	if (g_hUserParser == INVALID_HANDLE)
   150 	{
   151 		g_hUserParser = SMC_CreateParser();
   152 		SMC_SetReaders(g_hUserParser,
   153 					   ReadUsers_NewSection,
   154 					   ReadUsers_KeyValue,
   155 					   ReadUsers_EndSection);
   156 	}
   157 }
   158 
   159 ReadUsers()
   160 {
   161 	InitializeUserParser();
   162 	
   163 	BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admins.cfg");
   164 
   165 	/* Set states */
   166 	InitGlobalStates();
   167 	g_UserState = USER_STATE_NONE;
   168 		
   169 	new SMCError:err = SMC_ParseFile(g_hUserParser, g_Filename);
   170 	if (err != SMCError_Okay)
   171 	{
   172 		decl String:buffer[64];
   173 		if (SMC_GetErrorString(err, buffer, sizeof(buffer)))
   174 		{
   175 			ParseError("%s", buffer);
   176 		} else {
   177 			ParseError("Fatal parse error");
   178 		}
   179 	}
   180 }