-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
157 lines (145 loc) · 4.6 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
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
-- Advanced Ban [advancedban] by srifqi
advancedban = {}
-- parameter
local FILE_NAME = "bannedplayerlist.txt"
local BAN_MESSAGE = "Your username is banned."
function advancedban.is_banned(name)
if not file_exists(minetest.get_worldpath() .. DIR_DELIM .. FILE_NAME) then
return false
end
local list = io.open(minetest.get_worldpath() .. DIR_DELIM .. FILE_NAME, "r")
for username in list:lines() do
if name == username then
list:close()
return true
end
end
list:close()
return false
end
function advancedban.ban(name)
if advancedban.is_banned(name) then
-- minetest.log("info", name .. " is already in advancedban list.") -- print debug
return false
else
local list = io.open(minetest.get_worldpath() .. DIR_DELIM .. FILE_NAME, "a")
list:write(name .. "\n")
list:close()
minetest.log("action", name .. " has been added to advancedban list.") -- print debug
return true
end
end
function advancedban.unban(name)
local found = false
if file_exists(minetest.get_worldpath() .. DIR_DELIM .. FILE_NAME) then
local list = io.open(minetest.get_worldpath() .. DIR_DELIM .. FILE_NAME, "r")
local text = ""
for username in list:lines() do
if name == username then
found = true
else
text = text .. username .. "\n"
end
end
list:close()
local list = io.open(minetest.get_worldpath() .. DIR_DELIM .. FILE_NAME, "w")
list:write(text)
list:close()
end
if found then
minetest.log("action", name .. " has been removed from advancedban list.") -- print debug
end
return found
end
minetest.register_chatcommand("abancheck", {
params = "<player name>",
description = "Check ban for a player with given name",
privs = {},
func = function(name, param)
if advancedban.is_banned(param) then
minetest.chat_send_player(name, param .. " is in advancedban list.")
else
minetest.chat_send_player(name, param .. " is not in advancedban list.")
end
end
})
minetest.register_chatcommand("aban", {
params = "<player name>",
description = "Ban a player with given name",
privs = {ban = true},
func = function(name, param)
if advancedban.ban(param) then
minetest.chat_send_player(name, param .. " has been added to advancedban list.")
else
minetest.chat_send_player(name, param .. " is already in advancedban list.")
end
end
})
minetest.register_chatcommand("abankick", {
params = "<player name>",
description = "Ban and kick a player with given name",
privs = {ban = true, kick = true},
func = function(name, param)
local kick = ", but failed to kick the player."
if minetest.kick_player(param) then
kick = " and has been kicked."
end
if advancedban.ban(param) then
minetest.chat_send_player(name, param .. " has been added to advancedban list" .. kick)
else
minetest.chat_send_player(name, param .. " is already in advancedban list" .. kick)
end
end
})
minetest.register_chatcommand("aban+", {
params = "<player name>",
description = "Ban a player and its IP with given name",
privs = {ban = true},
func = function(name, param)
local IP = ", but failed to ban IP of the player."
if minetest.ban_player(param) then
IP = " and IP of the player is banned."
end
if advancedban.ban(param) then
minetest.chat_send_player(name, param .. " has been added to advancedban list" .. IP)
else
minetest.chat_send_player(name, param .. " is already in advancedban list" .. IP)
end
end
})
minetest.register_chatcommand("unaban", {
params = "<player name>",
description = "Remove player ban with given name",
privs = {ban = true},
func = function(name, param)
if advancedban.unban(param) then
minetest.chat_send_player(name, param .. " has been removed from advancedban list.")
else
minetest.chat_send_player(name, param .. " is not found in advancedban list.")
end
end
})
minetest.register_chatcommand("unaban+", {
params = "<player name>",
description = "Remove player ban and its IP with given name",
privs = {ban = true},
func = function(name, param)
local IP = ", but failed to unban IP of the player"
if minetest.unban_player_or_ip(param) then
IP = " and IP of the player is unbanned."
end
if advancedban.unban(param) then
minetest.chat_send_player(name, param .. " has been removed from advancedban list" .. IP)
else
minetest.chat_send_player(name, param .. " is not found in advancedban list" .. IP)
end
end
})
-- prevent advancedbanned player to join
minetest.register_on_prejoinplayer(function(name)
if advancedban.is_banned(name) then
return BAN_MESSAGE
end
end)
-- Minetest library - misc_helpers.lua
function file_exists(filename)local f=io.open(filename,"r");if f==nil then return false else f:close() return true end end