plugins/admin-flatfile/admin-simple.sp
author David Anderson <dvander@alliedmods.net>
Sun Apr 06 08:20:02 2008 +0000 (2008-04-06)
changeset 1970 316eff8d8164
parent 1952 b7aa2b626807
child 2052 138936f11ab5
permissions -rw-r--r--
added regex versioning to windows
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
ds@1952
    95
ReadAdminLine(const String:line[])
ds@1952
    96
{
ds@1952
    97
	new String:auth[64];
ds@1952
    98
	new cur_idx = BreakString(line, auth, sizeof(auth));
ds@1952
    99
	new idx = cur_idx;
ds@1952
   100
	
ds@1952
   101
	if (cur_idx == -1)
ds@1952
   102
	{
ds@1952
   103
		/* This line is bad... we need at least two parameters */
ds@1952
   104
		return;
ds@1952
   105
	}
ds@1952
   106
	
ds@1952
   107
	/* Create the admin */
ds@1952
   108
	new AdminId:admin = CreateAdmin();
ds@1952
   109
	
ds@1952
   110
	/* Read flags */
ds@1952
   111
	new String:flags[64];	
ds@1952
   112
	cur_idx = BreakString(line[idx], flags, sizeof(flags));
ds@1952
   113
	idx += cur_idx;
ds@1952
   114
ds@1952
   115
	/* Read immunity level, if any */
ds@1952
   116
	new level, flag_idx;
ds@1952
   117
ds@1952
   118
	if ((flag_idx = StringToIntEx(flags, level)) > 0)
ds@1952
   119
	{
ds@1952
   120
		SetAdminImmunityLevel(admin, level);
ds@1952
   121
		if (flags[flag_idx] == ':')
ds@1952
   122
		{
ds@1952
   123
			flag_idx++;
ds@1952
   124
		}
ds@1952
   125
	}
ds@1952
   126
ds@1952
   127
	if (flags[flag_idx] == '@')
ds@1952
   128
	{
ds@1952
   129
		new GroupId:gid = FindAdmGroup(flags[flag_idx + 1]);
ds@1952
   130
		if (gid == INVALID_GROUP_ID)
ds@1952
   131
		{
ds@1952
   132
			ParseError("Invalid group detected: %s", flags[flag_idx + 1]);
ds@1952
   133
			return;
ds@1952
   134
		}
ds@1952
   135
		AdminInheritGroup(admin, gid);
ds@1952
   136
	} else {
ds@1952
   137
		new len = strlen(flags[flag_idx]);
ds@1952
   138
		new bool:is_default = false;
ds@1952
   139
		for (new i=0; i<len; i++)
ds@1952
   140
		{
ds@1952
   141
			if (!level && flags[flag_idx + i] == '$')
ds@1952
   142
			{
ds@1952
   143
				SetAdminImmunityLevel(admin, 1);
ds@1952
   144
			} else {
ds@1952
   145
				new AdminFlag:flag;
ds@1952
   146
				
ds@1952
   147
				if (!FindFlagByChar(flags[flag_idx + i], flag))
ds@1952
   148
				{
ds@1952
   149
					ParseError("Invalid flag detected: %c", flags[flag_idx + i]);
ds@1952
   150
					continue;
ds@1952
   151
				}
ds@1952
   152
				SetAdminFlag(admin, flag, true);
ds@1952
   153
			}
ds@1952
   154
		}
ds@1952
   155
		
ds@1952
   156
		if (is_default)
ds@1952
   157
		{
ds@1952
   158
			new GroupId:gid = FindAdmGroup("Default");
ds@1952
   159
			if (gid != INVALID_GROUP_ID)
ds@1952
   160
			{
ds@1952
   161
				AdminInheritGroup(admin, gid);
ds@1952
   162
			}
ds@1952
   163
		}
ds@1952
   164
	}
ds@1952
   165
	
ds@1952
   166
	/* Lastly, is there a password? */
ds@1952
   167
	if (cur_idx != -1)
ds@1952
   168
	{
ds@1952
   169
		decl String:password[64];
ds@1952
   170
		BreakString(line[idx], password, sizeof(password));
ds@1952
   171
		SetAdminPassword(admin, password);
ds@1952
   172
	}
ds@1952
   173
	
ds@1952
   174
	/* Now, bind the identity to something */
ds@1952
   175
	if (StrContains(auth, "STEAM_") == 0)
ds@1952
   176
	{
ds@1952
   177
		BindAdminIdentity(admin, AUTHMETHOD_STEAM, auth);
ds@1952
   178
	} else {
ds@1952
   179
		if (auth[0] == '!')
ds@1952
   180
		{
ds@1952
   181
			BindAdminIdentity(admin, AUTHMETHOD_IP, auth[1]);
ds@1952
   182
		} else {
ds@1952
   183
			BindAdminIdentity(admin, AUTHMETHOD_NAME, auth);
ds@1952
   184
		}
ds@1952
   185
	}
ds@1952
   186
}
ds@1952
   187