forked from MinetestForFun/ignore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lists.lua
141 lines (117 loc) · 3.76 KB
/
lists.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
-- Part of the ignore mod
-- Last Modification : 01/18/16 @ 9:00PM UTC+1
-- This file contains all methods/namespaces for loading/saving/managing lists
--
ignore.lists = {}
ignore.lists.data = {}
function ignore.get_list(name)
return ignore.lists.data[name]
end
function ignore.get_ignore_names(name)
if not ignore.lists.data[name] then
return {}
end
local tab = {}
for n, _ in pairs(ignore.lists.data[name]) do
table.insert(tab, n)
end
return tab
end
function ignore.set_list(name, list)
ignore.lists.data[name] = list
minetest.log("action", "[Ignore] Set list of player " .. name)
return true
end
function ignore.del_list(name)
ignore.lists.data[name] = nil
minetest.log("action", "[Ignore] Deleted list of player " .. name)
return true
end
function ignore.init_list(name)
ignore.lists.data[name] = {}
minetest.log("action", "[Ignore] Init on list for player " .. name)
return true
end
function ignore.get_ignore(ignored, name)
if not ignore.lists.data[name] then
local res, code = ignore.load(name)
if not res and code then
return false, code
else
return ignore.get_ignore(ignored, name)
end
end
return ignore.lists.data[name][ignored]
end
function ignore.add(ignored, name)
if not ignore.lists.data[name] then
ignore.init_list(name)
return ignore.add(ignored, name)
-- ^ Crooked
end
if ignore.get_ignore(ignored, name) then
minetest.log("action", "[Ignore] Will not add " .. ignored .. " in list of player " .. name .. " : already present")
return false, "dejavu"
elseif minetest.get_player_privs(ignored).ignore_protection then
minetest.log("action", "[Ignore] Will not add " .. ignored .. " in list of player " .. name .. " : player protected")
return false, "protected"
end
ignore.lists.data[name][ignored] = os.time()
minetest.log("action", "[Ignore] Adding " .. ignored .. " in " .. name .. "'s list")
return true
end
function ignore.del(ignored, name)
if not ignore.lists.data[name] then
minetest.log("action", "[Ignore] Will not remove " .. ignored .. " from " .. name .. "'s list : no ignore list")
return false, "nolist"
end
local status = ignore.get_ignore(ignored, name)
if not status then
minetest.log("action", "[Ignore] Couldn't remove " .. ignored .. " from " .. name .. "'s list : not currently ignored")
return false, "notignored"
else
minetest.log("action", "[Ignore] Successfully removed " .. ignored .. " from " .. name .. "'s list")
ignore.lists.data[name][ignored] = nil
return true
end
end
function ignore.save(name)
if not ignore.lists.data[name] then
minetest.log("action", "[Ignore] Saving list of " .. name .. " : inexistant list")
ignore.init_list(name)
end
local f, err = io.open(ignore.config.save_dir .. "/" .. name, 'w')
if not f then
minetest.log("warning", "[Ignore] Failed to save " .. name .. "'s list : " .. err)
return false, err
end
for ignored, timestamp in pairs(ignore.lists.data[name]) do
f:write(("%s %s\n"):format(ignored, timestamp))
end
f:close()
minetest.log("action", "[Ignore] Ignore list saved for " .. name)
return true
end
function ignore.load(name)
ignore.init_list(name)
local f, err = io.open(ignore.config.save_dir .. "/" .. name)
if not f then
minetest.log("warning", "[Ignore] Failed to load " .. name .. "'s list : " .. err)
return false, err
end
for line in f:lines() do
local ignored, timestamp
ignored = line:split(" ")[1]
timestamp = line:split(" ")[2]
if not ignored or not timestamp then
f:close()
minetest.log("error", "[Ignore] Error reading " .. name .. "'s list : corrupted file")
minetest.chat_send_player(name, "Error: Your file might be corrupted")
return false, line
end
ignore.lists.data[name][ignored] = timestamp
end
minetest.log("action", "Successfully logged " .. name .. "'s file")
f:close()
return true
end