forked from ShadowNinja/whitelist
-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.lua
126 lines (99 loc) · 2.91 KB
/
init.lua
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
--[[
Whiter List mod by Jordan Irwin (AntumDeluge)
License: MIT
Original Whitelist mod by ShadowNinja
License: WTFPL
--]]
local world_path = core.get_worldpath()
local admin = core.settings:get("name")
local whitelist = {}
local S = core.get_translator(core.get_current_modname())
-- Enabled by default
local enabled = core.settings:get_bool("whitelist.enable", true)
local deny_message = core.settings:get("whitelist.message") or "Server access is restricted to whitelisted players only."
deny_message = S(deny_message)
--- Loads whitelisted names into memory.
local function load_whitelist()
local file, err = io.open(world_path .. "/whitelist.txt", "r")
if err then return end
-- FIXME: error if "whitelist.txt" is a directory
-- reset for session in case names have been manually removed
whitelist = {}
for line in file:lines() do
-- ignore lines with whitespace only
if line:gsub("%s+", "") ~= "" then
whitelist[line] = true
end
end
file:close()
end
--- Writes whitelisted names to file.
local function save_whitelist()
local file, err = io.open(world_path .. "/whitelist.txt", "w")
if err then
core.log("error", S("Could not open whitelist file for writing: @1", err))
return
end
for item in pairs(whitelist) do
file:write(item .. "\n")
end
file:close()
end
if enabled then
core.register_on_prejoinplayer(function(name, ip)
load_whitelist()
if core.is_singleplayer() or name == admin or whitelist[name] then
return
end
return deny_message
end)
end
core.register_chatcommand("whitelist", {
params = "[{add|remove} <" .. S("name") .. ">]",
description = S("Manage the whitelist."),
privs = {ban=true},
func = function(name, param)
local action, whitename
if param:find(" ") then
action, whitename = param:match("^([^ ]+) ([^ ]+)$")
else
action = param
end
-- update whitelist from file before performing actions
load_whitelist()
if action == "" then
local names = {}
for k, v in pairs(whitelist) do
table.insert(names, k)
end
return true, S("Whitelisted names: @1", table.concat(names, ", "))
end
if not whitename then
return false, S("Must supply name parameter.")
end
if action == "query" then
local msg
if whitelist[whitename] then
msg = S("@1 is already on the whitelist.", whitename)
else
msg = S("@1 is not on the whitelist.", whitename)
end
return true, msg
elseif action == "add" then
if whitelist[whitename] then
return false, S("@1 is already on the whitelist.", whitename)
end
whitelist[whitename] = true
save_whitelist()
return true, S("Added @1 to the whitelist.", whitename)
elseif action == "remove" then
if not whitelist[whitename] then
return false, S("@1 is not on the whitelist.", whitename)
end
whitelist[whitename] = nil
save_whitelist()
return true, S("Removed @1 from the whitelist.", whitename)
end
return false, S("Invalid action: @1", action)
end,
})