-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
278 lines (234 loc) · 6.83 KB
/
Copy pathinit.lua
File metadata and controls
278 lines (234 loc) · 6.83 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
-- privall/init.lua
local storage = minetest.get_mod_storage()
local privall = minetest.deserialize(storage:get_string("data"))
if privall == nil then
privall = {
grant = {},
revoke = {}
}
end
-- Constants
local green = "#009933"
local red = "#ff3300"
local plus = minetest.colorize(green, " +")
local minus = minetest.colorize(red, " -")
local changedPrefix = "[privall] Your privileges have changed:"
-- Utility functions
local formatOutput = function(prefix, changes)
local rope = {prefix}
local changed = false
for _,priv in ipairs(changes.grant) do
table.insert(rope, plus)
table.insert(rope, minetest.colorize(green, priv))
table.insert(rope, ",")
changed = true
end
for _,priv in ipairs(changes.revoke) do
table.insert(rope, minus)
table.insert(rope, minetest.colorize(red, priv))
table.insert(rope, ",")
changed = true
end
return table.concat(rope), changed
end
local save = function()
storage:set_string("data", minetest.serialize(privall))
end
local isTracked = function(priv)
return (privall.grant[priv] or privall.revoke[priv])
end
local grantPriv = function(name, priv, changes)
local privs = minetest.get_player_privs(name)
if not privs[priv] then
privs[priv] = true
minetest.set_player_privs(name, privs)
table.insert(changes.grant, priv)
minetest.log("action", "[privall] Granted priv \""..priv.."\" to player \""..name.."\".")
return true
end
return false
end
local revokePriv = function(name, priv, changes)
local privs = minetest.get_player_privs(name)
if privs[priv] then
privs[priv] = nil
minetest.set_player_privs(name, privs)
table.insert(changes.revoke, priv)
minetest.log("action", "[privall] Revoked priv \""..priv.."\" from player \""..name.."\".")
return true
end
return false
end
local grantAll = function(privs)
for _,player in ipairs(minetest.get_connected_players()) do
local name = player:get_player_name()
local changes = {grant = {}, revoke = {}}
local changed = false
for _,priv in ipairs(privs) do
if grantPriv(name, priv, changes) then
changed = true
end
end
if changed then
local text = formatOutput(changedPrefix, changes)
minetest.chat_send_player(name, text)
end
end
end
local revokeAll = function(privs)
for _,player in ipairs(minetest.get_connected_players()) do
local name = player:get_player_name()
local changes = {grant = {}, revoke = {}}
local changed = false
for _,priv in ipairs(privs) do
if revokePriv(name, priv, changes) then
changed = true
end
end
if changed then
local text = formatOutput(changedPrefix, changes)
minetest.chat_send_player(name, text)
end
end
end
-- Subcommands
local commands = {
grant = {
run = function(name, tokens)
for _,priv in ipairs(tokens) do
privall.revoke[priv] = nil
privall.grant[priv] = true
minetest.log("action", "[privall] Privilege \""..priv.."\" is now being granted to all players.")
end
grantAll(tokens)
save()
return true, "[privall] Privileges\""..table.concat(tokens, ", ").."\" are now being granted to all players."
end,
checkParams = function(tokens)
if #tokens == 0 then
return false, "[privall] Subcommand \"grant\" requires at least 1 argument."
end
for _,priv in ipairs(tokens) do
if not minetest.registered_privileges[priv] then
return false, "[privall] Unknown privilege \""..priv.."\"."
end
if privall.grant[priv] then
return false, "[privall] Privilege \""..priv.."\" is already being granted to all players."
end
end
return true
end,
},
revoke = {
run = function(name, tokens)
for _,priv in ipairs(tokens) do
privall.grant[priv] = nil
privall.revoke[priv] = true
minetest.log("action", "[privall] Privilege \""..priv.."\" is now being revoked from all players.")
end
revokeAll(tokens)
save()
return true, "[privall] Privileges \""..table.concat(tokens, ", ").."\" are now being revoked from all players."
end,
checkParams = function(tokens)
if #tokens == 0 then
return false, "[privall] Subcommand \"revoke\" requires at least 1 argument."
end
for _,priv in ipairs(tokens) do
if privall.revoke[priv] then
return false, "[privall] Privilege \""..priv.." is already being revoked from all players."
end
end
return true
end,
},
reset = {
run = function(name, tokens)
for _,priv in ipairs(tokens) do
privall.grant[priv] = nil
privall.revoke[priv] = nil
minetest.log("action", "[privall] Cleared assignment for privilege \""..priv.."\".")
end
save()
return true, "[privall] Cleared assignments for privileges \""..table.concat(tokens, ", ").."\"."
end,
checkParams = function(tokens)
if #tokens == 0 then
return false, "[privall] Subcommand \"reset\" requires at least 1 argument."
end
for _,priv in ipairs(tokens) do
if not isTracked(priv) then
return false, "[privall] Privilege \""..priv.."\" is not currently tracked by privall."
end
end
return true
end,
},
list = {
run = function(name, tokens)
local rope = {"Current privall configuration: "}
for priv,_ in pairs(privall.grant) do
table.insert(rope, " +")
table.insert(rope, priv)
table.insert(rope, ",")
end
for priv,_ in pairs(privall.revoke) do
table.insert(rope, " -")
table.insert(rope, priv)
table.insert(rope, ",")
end
if(#rope == 1) then
table.insert(rope, "None")
end
minetest.chat_send_player(name, table.concat(rope))
end,
checkParams = function(tokens)
if #tokens ~= 0 then
return false, "[privall] Subcommand \"list\" takes no arguments."
end
return true
end,
},
}
-- Hooks
minetest.register_on_shutdown(save)
minetest.register_chatcommand("privall", {
params = "(grant|revoke|reset|list) <privilege> ...",
description = "Edit or list the automated granting/revoking of privileges.",
privs = {privs = true},
func = function(name, param)
-- Split params at spaces
local tokens = {}
for token in param:gmatch("[^ ]+") do
table.insert(tokens, token)
end
-- Sanitize input
if #tokens == 0 then
return false, "[privall] Please specify a subcommand. See \"/help privall\" for more details."
end
local mode = table.remove(tokens, 1)
if commands[mode] == nil then
return false, "[privall] Unknown subcommand \""..mode.."\"."
end
-- Check subcommand parameters
local result, msg = commands[mode].checkParams(tokens)
if not result then
return false, msg
end
return commands[mode].run(name, tokens)
end,
})
minetest.register_on_joinplayer(function(player)
local name = player:get_player_name()
local changes = {grant = {}, revoke = {}}
for priv,_ in pairs(privall.grant) do
grantPriv(name, priv, changes)
end
for priv,_ in pairs(privall.revoke) do
revokePriv(name, priv, changes)
end
local text, changed = formatOutput(changedPrefix, changes)
if changed then
minetest.chat_send_player(name, text)
end
end)