-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfunctions.lua
117 lines (103 loc) · 2.83 KB
/
functions.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
-- WARNING: ipnames.command_* are reserved for chat commands!
function ipnames.command_list(name)
local names = {} -- faster than string concat
for k, v in pairs(ipnames.whitelist) do
if v then
names[#names + 1] = k
end
end
return true, "All exceptions: " .. table.concat(names, ", ")
end
function ipnames.command_whois(name, param)
if not ipnames.data[param] then
return false, "The player '" .. param .. "' did not join yet."
end
local ip = ipnames.data[param][1]
local names = ""
for k, v in pairs(ipnames.data) do
if v[1] == ip then
names = names .. " " .. k
end
end
return true, "Following players share an IP: " .. names
end
function ipnames.command_ignore(name, param)
if not ipnames.data[param] then
return false, "The player '" .. param .. "' did not join yet."
end
ipnames.whitelist[param] = true
ipnames.save_whitelist()
return true, "Added '" .. param .. "' to the name whitelist."
end
function ipnames.command_unignore(name, param)
if not ipnames.whitelist[param] then
return false, "The player '" .. param .. "' is not on the whitelist."
end
ipnames.whitelist[param] = nil
ipnames.save_whitelist()
return true, "Removed '" .. param .. "' from the name whitelist."
end
ipnames.is_registered = minetest.player_exists
or function(name)
-- 0.4.x support: If you get a nil error here -> update Minetest
return minetest.auth_table[name]
end
-- TODO: Use mod storage
function ipnames.load_data()
local file = io.open(ipnames.file, "r")
if not file then
return
end
local t = os.time()
for line in file:lines() do
local data = line:split("|")
if #data >= 2 then
-- Ignore players which were removed (according to auth)
local player_exists = ipnames.is_registered(data[1])
if player_exists then
data[3] = tonumber(data[3]) or 0
-- Remove IP after 2 weeks: Expired
if data[3] > 0 and t - data[3] > (3600 * 24 * 14) then
player_exists = false
end
end
if player_exists then
ipnames.data[data[1]] = {data[2], data[3]}
end
end
end
io.close(file)
end
function ipnames.save_data()
if not ipnames.changes then
return
end
ipnames.changes = false
local contents = {} -- faster than string concat
for k, v in pairs(ipnames.data) do
v[2] = v[2] or os.time()
contents[#contents + 1] = k.."|"..v[1].."|"..v[2]
end
minetest.safe_file_write(ipnames.file, table.concat(contents, "\n"))
end
function ipnames.load_whitelist()
local file = io.open(ipnames.whitelist_file, "r")
if not file then
return
end
for line in file:lines() do
if line ~= "" then
ipnames.whitelist[line] = true
end
end
io.close(file)
end
function ipnames.save_whitelist()
local names = {} -- faster than string concat
for k, v in pairs(ipnames.whitelist) do
if v then
names[#names + 1] = k
end
end
minetest.safe_file_write(ipnames.whitelist_file, table.concat(names, "\n"))
end