plugins/admin-flatfile/admin-simple.sp
author Matt Woodrow <pred@alliedmods.net>
Mon Jul 28 07:47:28 2008 +0000 (2008-07-28)
changeset 2215 bcbfee080d08
parent 2052 138936f11ab5
child 2289 44045ead61cd
permissions -rw-r--r--
Pending Map should no longer be shown when mapvotes are disabled.
Removed tf2 include from mapchooser.
Moved sm_setnextmap to mapchooser and made it block the end of map vote.
     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-2008 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 public ReadSimpleUsers()
    35 {
    36 	BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admins_simple.ini");
    37 	
    38 	new Handle:file = OpenFile(g_Filename, "rt");
    39 	if (file == INVALID_HANDLE)
    40 	{
    41 		ParseError("Could not open file!");
    42 		return;
    43 	}
    44 	
    45 	while (!IsEndOfFile(file))
    46 	{
    47 		decl String:line[255];
    48 		if (!ReadFileLine(file, line, sizeof(line)))
    49 		{
    50 			break;
    51 		}
    52 		
    53 		/* Trim comments */
    54 		new len = strlen(line);
    55 		new bool:ignoring = false;
    56 		for (new i=0; i<len; i++)
    57 		{
    58 			if (ignoring)
    59 			{
    60 				if (line[i] == '"')
    61 				{
    62 					ignoring = false;
    63 				}
    64 			} else {
    65 				if (line[i] == '"')
    66 				{
    67 					ignoring = true;
    68 				} else if (line[i] == ';') {
    69 					line[i] = '\0';
    70 					break;
    71 				} else if (line[i] == '/'
    72 							&& i != len - 1
    73 							&& line[i+1] == '/')
    74 				{
    75 					line[i] = '\0';
    76 					break;
    77 				}
    78 			}
    79 		}
    80 		
    81 		TrimString(line);
    82 		
    83 		if ((line[0] == '/' && line[1] == '/')
    84 			|| (line[0] == ';' || line[0] == '\0'))
    85 		{
    86 			continue;
    87 		}
    88 	
    89 		ReadAdminLine(line);
    90 	}
    91 	
    92 	CloseHandle(file);
    93 }
    94 
    95 
    96 
    97 DecodeAuthMethod(const String:auth[], String:method[32], &offset)
    98 {
    99 	if (StrContains(auth, "STEAM_") == 0)
   100 	{
   101 		strcopy(method, sizeof(method), AUTHMETHOD_STEAM);
   102 		offset = 0;
   103 	}
   104 	else
   105 	{
   106 		if (auth[0] == '!')
   107 		{
   108 			strcopy(method, sizeof(method), AUTHMETHOD_IP);
   109 			offset = 1;
   110 		}
   111 		else
   112 		{
   113 			strcopy(method, sizeof(method), AUTHMETHOD_NAME);
   114 			offset = 0;
   115 		}
   116 	}
   117 }
   118 
   119 ReadAdminLine(const String:line[])
   120 {
   121 	new bool:is_bound;
   122 	new AdminId:admin;
   123 	new String:auth[64];
   124 	decl String:auth_method[32];
   125 	new idx, cur_idx, auth_offset;
   126 	
   127 	if ((cur_idx = BreakString(line, auth, sizeof(auth))) == -1)
   128 	{
   129 		/* This line is bad... we need at least two parameters */
   130 		return;
   131 	}
   132 	
   133 	idx = cur_idx;
   134 	
   135 	/* Check if we can bind beforehand */
   136 	DecodeAuthMethod(auth, auth_method, auth_offset);
   137 	if ((admin = FindAdminByIdentity(auth_method, auth[auth_offset])) == INVALID_ADMIN_ID)
   138 	{
   139 		/* There is no binding, create the admin */
   140 		admin = CreateAdmin();
   141 	}
   142 	else
   143 	{
   144 		is_bound = true;
   145 	}
   146 	
   147 	/* Read flags */
   148 	new String:flags[64];	
   149 	cur_idx = BreakString(line[idx], flags, sizeof(flags));
   150 	idx += cur_idx;
   151 
   152 	/* Read immunity level, if any */
   153 	new level, flag_idx;
   154 
   155 	if ((flag_idx = StringToIntEx(flags, level)) > 0)
   156 	{
   157 		SetAdminImmunityLevel(admin, level);
   158 		if (flags[flag_idx] == ':')
   159 		{
   160 			flag_idx++;
   161 		}
   162 	}
   163 
   164 	if (flags[flag_idx] == '@')
   165 	{
   166 		new GroupId:gid = FindAdmGroup(flags[flag_idx + 1]);
   167 		if (gid == INVALID_GROUP_ID)
   168 		{
   169 			ParseError("Invalid group detected: %s", flags[flag_idx + 1]);
   170 			return;
   171 		}
   172 		AdminInheritGroup(admin, gid);
   173 	}
   174 	else
   175 	{
   176 		new len = strlen(flags[flag_idx]);
   177 		new bool:is_default = false;
   178 		for (new i=0; i<len; i++)
   179 		{
   180 			if (!level && flags[flag_idx + i] == '$')
   181 			{
   182 				SetAdminImmunityLevel(admin, 1);
   183 			} else {
   184 				new AdminFlag:flag;
   185 				
   186 				if (!FindFlagByChar(flags[flag_idx + i], flag))
   187 				{
   188 					ParseError("Invalid flag detected: %c", flags[flag_idx + i]);
   189 					continue;
   190 				}
   191 				SetAdminFlag(admin, flag, true);
   192 			}
   193 		}
   194 		
   195 		if (is_default)
   196 		{
   197 			new GroupId:gid = FindAdmGroup("Default");
   198 			if (gid != INVALID_GROUP_ID)
   199 			{
   200 				AdminInheritGroup(admin, gid);
   201 			}
   202 		}
   203 	}
   204 	
   205 	/* Lastly, is there a password? */
   206 	if (cur_idx != -1)
   207 	{
   208 		decl String:password[64];
   209 		BreakString(line[idx], password, sizeof(password));
   210 		SetAdminPassword(admin, password);
   211 	}
   212 	
   213 	/* Now, bind the identity to something */
   214 	if (!is_bound)
   215 	{
   216 		if (!BindAdminIdentity(admin, auth_method, auth[auth_offset]))
   217 		{
   218 			/* We should never reach here */
   219 			ParseError("Failed to bind identity %s (method %s)", auth[auth_offset], auth_method);
   220 		}
   221 	}
   222 }
   223