-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathwhitelist.sp
More file actions
167 lines (142 loc) · 3.88 KB
/
whitelist.sp
File metadata and controls
167 lines (142 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <sourcemod>
#pragma semicolon 1
#define PLUGIN_VERSION "1.1"
public Plugin:myinfo =
{
name = "Player Whitelist",
author = "Stevo.TVR",
description = "Restricts server to SteamIDs listed in the whitelist",
version = PLUGIN_VERSION,
url = "http://www.theville.org/"
}
// maximum SteamIDs the plugin can handle; increase value as needed
#define WHITELIST_MAX 255
new Handle:sm_whitelist_enable = INVALID_HANDLE;
new Handle:sm_whitelist_immunity = INVALID_HANDLE;
new String:whitelist[WHITELIST_MAX][64];
new listlen;
public OnPluginStart()
{
CreateConVar("sm_whitelist_version", PLUGIN_VERSION, "Server Whitelist plugin version", FCVAR_PLUGIN|FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY|FCVAR_DONTRECORD);
sm_whitelist_enable = CreateConVar("sm_whitelist_enable", "1", "Enable server whitelist", _, true, 0.0, true, 1.0);
sm_whitelist_immunity = CreateConVar("sm_whitelist_immunity", "1", "Automatically grant admins access", _, true, 0.0, true, 1.0);
AutoExecConfig(true, "whitelist");
RegAdminCmd("sm_whitelist_reload", CommandReload, ADMFLAG_GENERIC, "Reloads server whitelist");
RegAdminCmd("sm_whitelist_list", CommandList, ADMFLAG_GENERIC, "List all SteamIDs in whitelist");
RegAdminCmd("sm_whitelist_add", CommandAdd, ADMFLAG_CONVARS, "Adds a SteamID to the whitelist");
HookConVarChange(sm_whitelist_enable, OnEnableChange);
LoadList();
}
public OnClientPostAdminCheck(client)
{
if(GetConVarBool(sm_whitelist_enable) && !IsFakeClient(client) && !IsImmune(client))
{
new String:auth[64];
GetClientAuthString(client, auth, sizeof(auth));
new bool:allow = false;
for(new i; i < listlen; i++)
{
if(strcmp(auth, whitelist[i]) == 0)
{
allow = true;
break;
}
}
if(!allow)
{
KickClient(client, "%s is not listed on the server whitelist", auth);
}
}
}
public OnEnableChange(Handle:cvar, const String:oldVal[], const String:newVal[])
{
if(StringToInt(newVal) > 0)
{
LoadList();
}
}
public Action:CommandReload(client, args)
{
LoadList();
ReplyToCommand(client, "[Whitelist] %d SteamIDs loaded from whitelist", listlen);
return Plugin_Handled;
}
public Action:CommandList(client, args)
{
PrintToConsole(client, "[Whitelist] Listing current whitelist (%d items):", listlen);
for(new i; i < listlen; i++)
{
PrintToConsole(client, "%s", whitelist[i]);
}
return Plugin_Handled;
}
public Action:CommandAdd(client, args)
{
if(args < 1)
{
ReplyToCommand(client, "[SM] Usage: sm_whitelist_add <steamid>");
return Plugin_Handled;
}
new String:steamid[64];
GetCmdArg(1, steamid, sizeof(steamid));
TrimString(steamid);
new String:path[PLATFORM_MAX_PATH];
BuildPath(PathType:Path_SM, path, sizeof(path), "configs/whitelist.txt");
new Handle:file = OpenFile(path, "a");
if(file != INVALID_HANDLE)
{
WriteFileLine(file, steamid);
whitelist[listlen] = steamid;
listlen++;
ReplyToCommand(client, "[SM] %s successfully added to whitelist", steamid);
}
else
{
ReplyToCommand(client, "[SM] Failed to open %s for writing", path);
}
CloseHandle(file);
return Plugin_Handled;
}
public LoadList()
{
new String:path[PLATFORM_MAX_PATH];
BuildPath(PathType:Path_SM, path, sizeof(path), "configs/whitelist.txt");
new Handle:file = OpenFile(path, "r");
if(file == INVALID_HANDLE)
{
SetFailState("[Whitelist] Unable to read file %s", path);
}
listlen = 0;
new String:steamid[64];
while(!IsEndOfFile(file) && ReadFileLine(file, steamid, sizeof(steamid)))
{
if (steamid[0] == ';' || !IsCharAlpha(steamid[0]))
{
continue;
}
new len = strlen(steamid);
for (new i; i < len; i++)
{
if (IsCharSpace(steamid[i]) || steamid[i] == ';')
{
steamid[i] = '\0';
break;
}
}
whitelist[listlen] = steamid;
listlen++;
}
CloseHandle(file);
}
public IsImmune(client)
{
new bool:immune = false;
if(GetConVarBool(sm_whitelist_immunity))
{
if(GetUserAdmin(client) != INVALID_ADMIN_ID)
{
immune = true;
}
}
return immune;
}