plugins/admin-flatfile/admin-simple.sp
author Matt Woodrow <pred@alliedmods.net>
Tue Jul 01 23:42:19 2008 +0000 (2008-07-01)
changeset 2143 ad12a83e4acd
parent 2052 138936f11ab5
child 2289 44045ead61cd
permissions -rw-r--r--
Added a null check on the vtable symbol
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod Admin File Reader Plugin
ds@1952
     5
 * Reads the admins.cfg file.  Do not compile this directly.
ds@1952
     6
 *
ds@1952
     7
 * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     8
 * =============================================================================
ds@1952
     9
 *
ds@1952
    10
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
    11
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    12
 * Free Software Foundation.
ds@1952
    13
 * 
ds@1952
    14
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    16
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    17
 * details.
ds@1952
    18
 *
ds@1952
    19
 * You should have received a copy of the GNU General Public License along with
ds@1952
    20
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    21
 *
ds@1952
    22
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    23
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    24
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    25
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    26
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    27
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    28
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    29
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    30
 *
ds@1952
    31
 * Version: $Id$
ds@1952
    32
 */
ds@1952
    33
ds@1952
    34
public ReadSimpleUsers()
ds@1952
    35
{
ds@1952
    36
	BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admins_simple.ini");
ds@1952
    37
	
ds@1952
    38
	new Handle:file = OpenFile(g_Filename, "rt");
ds@1952
    39
	if (file == INVALID_HANDLE)
ds@1952
    40
	{
ds@1952
    41
		ParseError("Could not open file!");
ds@1952
    42
		return;
ds@1952
    43
	}
ds@1952
    44
	
ds@1952
    45
	while (!IsEndOfFile(file))
ds@1952
    46
	{
ds@1952
    47
		decl String:line[255];
ds@1952
    48
		if (!ReadFileLine(file, line, sizeof(line)))
ds@1952
    49
		{
ds@1952
    50
			break;
ds@1952
    51
		}
ds@1952
    52
		
ds@1952
    53
		/* Trim comments */
ds@1952
    54
		new len = strlen(line);
ds@1952
    55
		new bool:ignoring = false;
ds@1952
    56
		for (new i=0; i<len; i++)
ds@1952
    57
		{
ds@1952
    58
			if (ignoring)
ds@1952
    59
			{
ds@1952
    60
				if (line[i] == '"')
ds@1952
    61
				{
ds@1952
    62
					ignoring = false;
ds@1952
    63
				}
ds@1952
    64
			} else {
ds@1952
    65
				if (line[i] == '"')
ds@1952
    66
				{
ds@1952
    67
					ignoring = true;
ds@1952
    68
				} else if (line[i] == ';') {
ds@1952
    69
					line[i] = '\0';
ds@1952
    70
					break;
ds@1952
    71
				} else if (line[i] == '/'
ds@1952
    72
							&& i != len - 1
ds@1952
    73
							&& line[i+1] == '/')
ds@1952
    74
				{
ds@1952
    75
					line[i] = '\0';
ds@1952
    76
					break;
ds@1952
    77
				}
ds@1952
    78
			}
ds@1952
    79
		}
ds@1952
    80
		
ds@1952
    81
		TrimString(line);
ds@1952
    82
		
ds@1952
    83
		if ((line[0] == '/' && line[1] == '/')
ds@1952
    84
			|| (line[0] == ';' || line[0] == '\0'))
ds@1952
    85
		{
ds@1952
    86
			continue;
ds@1952
    87
		}
ds@1952
    88
	
ds@1952
    89
		ReadAdminLine(line);
ds@1952
    90
	}
ds@1952
    91
	
ds@1952
    92
	CloseHandle(file);
ds@1952
    93
}
ds@1952
    94
dvander@2052
    95
dvander@2052
    96
dvander@2052
    97
DecodeAuthMethod(const String:auth[], String:method[32], &offset)
dvander@2052
    98
{
dvander@2052
    99
	if (StrContains(auth, "STEAM_") == 0)
dvander@2052
   100
	{
dvander@2052
   101
		strcopy(method, sizeof(method), AUTHMETHOD_STEAM);
dvander@2052
   102
		offset = 0;
dvander@2052
   103
	}
dvander@2052
   104
	else
dvander@2052
   105
	{
dvander@2052
   106
		if (auth[0] == '!')
dvander@2052
   107
		{
dvander@2052
   108
			strcopy(method, sizeof(method), AUTHMETHOD_IP);
dvander@2052
   109
			offset = 1;
dvander@2052
   110
		}
dvander@2052
   111
		else
dvander@2052
   112
		{
dvander@2052
   113
			strcopy(method, sizeof(method), AUTHMETHOD_NAME);
dvander@2052
   114
			offset = 0;
dvander@2052
   115
		}
dvander@2052
   116
	}
dvander@2052
   117
}
dvander@2052
   118
ds@1952
   119
ReadAdminLine(const String:line[])
ds@1952
   120
{
dvander@2052
   121
	new bool:is_bound;
dvander@2052
   122
	new AdminId:admin;
ds@1952
   123
	new String:auth[64];
dvander@2052
   124
	decl String:auth_method[32];
dvander@2052
   125
	new idx, cur_idx, auth_offset;
ds@1952
   126
	
dvander@2052
   127
	if ((cur_idx = BreakString(line, auth, sizeof(auth))) == -1)
ds@1952
   128
	{
ds@1952
   129
		/* This line is bad... we need at least two parameters */
ds@1952
   130
		return;
ds@1952
   131
	}
ds@1952
   132
	
dvander@2052
   133
	idx = cur_idx;
dvander@2052
   134
	
dvander@2052
   135
	/* Check if we can bind beforehand */
dvander@2052
   136
	DecodeAuthMethod(auth, auth_method, auth_offset);
dvander@2052
   137
	if ((admin = FindAdminByIdentity(auth_method, auth[auth_offset])) == INVALID_ADMIN_ID)
dvander@2052
   138
	{
dvander@2052
   139
		/* There is no binding, create the admin */
dvander@2052
   140
		admin = CreateAdmin();
dvander@2052
   141
	}
dvander@2052
   142
	else
dvander@2052
   143
	{
dvander@2052
   144
		is_bound = true;
dvander@2052
   145
	}
ds@1952
   146
	
ds@1952
   147
	/* Read flags */
ds@1952
   148
	new String:flags[64];	
ds@1952
   149
	cur_idx = BreakString(line[idx], flags, sizeof(flags));
ds@1952
   150
	idx += cur_idx;
ds@1952
   151
ds@1952
   152
	/* Read immunity level, if any */
ds@1952
   153
	new level, flag_idx;
ds@1952
   154
ds@1952
   155
	if ((flag_idx = StringToIntEx(flags, level)) > 0)
ds@1952
   156
	{
ds@1952
   157
		SetAdminImmunityLevel(admin, level);
ds@1952
   158
		if (flags[flag_idx] == ':')
ds@1952
   159
		{
ds@1952
   160
			flag_idx++;
ds@1952
   161
		}
ds@1952
   162
	}
ds@1952
   163
ds@1952
   164
	if (flags[flag_idx] == '@')
ds@1952
   165
	{
ds@1952
   166
		new GroupId:gid = FindAdmGroup(flags[flag_idx + 1]);
ds@1952
   167
		if (gid == INVALID_GROUP_ID)
ds@1952
   168
		{
ds@1952
   169
			ParseError("Invalid group detected: %s", flags[flag_idx + 1]);
ds@1952
   170
			return;
ds@1952
   171
		}
ds@1952
   172
		AdminInheritGroup(admin, gid);
dvander@2052
   173
	}
dvander@2052
   174
	else
dvander@2052
   175
	{
ds@1952
   176
		new len = strlen(flags[flag_idx]);
ds@1952
   177
		new bool:is_default = false;
ds@1952
   178
		for (new i=0; i<len; i++)
ds@1952
   179
		{
ds@1952
   180
			if (!level && flags[flag_idx + i] == '$')
ds@1952
   181
			{
ds@1952
   182
				SetAdminImmunityLevel(admin, 1);
ds@1952
   183
			} else {
ds@1952
   184
				new AdminFlag:flag;
ds@1952
   185
				
ds@1952
   186
				if (!FindFlagByChar(flags[flag_idx + i], flag))
ds@1952
   187
				{
ds@1952
   188
					ParseError("Invalid flag detected: %c", flags[flag_idx + i]);
ds@1952
   189
					continue;
ds@1952
   190
				}
ds@1952
   191
				SetAdminFlag(admin, flag, true);
ds@1952
   192
			}
ds@1952
   193
		}
ds@1952
   194
		
ds@1952
   195
		if (is_default)
ds@1952
   196
		{
ds@1952
   197
			new GroupId:gid = FindAdmGroup("Default");
ds@1952
   198
			if (gid != INVALID_GROUP_ID)
ds@1952
   199
			{
ds@1952
   200
				AdminInheritGroup(admin, gid);
ds@1952
   201
			}
ds@1952
   202
		}
ds@1952
   203
	}
ds@1952
   204
	
ds@1952
   205
	/* Lastly, is there a password? */
ds@1952
   206
	if (cur_idx != -1)
ds@1952
   207
	{
ds@1952
   208
		decl String:password[64];
ds@1952
   209
		BreakString(line[idx], password, sizeof(password));
ds@1952
   210
		SetAdminPassword(admin, password);
ds@1952
   211
	}
ds@1952
   212
	
ds@1952
   213
	/* Now, bind the identity to something */
dvander@2052
   214
	if (!is_bound)
ds@1952
   215
	{
dvander@2052
   216
		if (!BindAdminIdentity(admin, auth_method, auth[auth_offset]))
ds@1952
   217
		{
dvander@2052
   218
			/* We should never reach here */
dvander@2052
   219
			ParseError("Failed to bind identity %s (method %s)", auth[auth_offset], auth_method);
ds@1952
   220
		}
ds@1952
   221
	}
ds@1952
   222
}
ds@1952
   223