-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
176 lines (150 loc) · 4.34 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
mute = {}
mute.shadow_mute_list = {}
mute.ignore_lists = {}
local ignore_lists_path = minetest.get_worldpath() .. "/mute/"
local shadow_mute_path = minetest.get_worldpath() .. "/shadow_mute"
minetest.mkdir(ignore_lists_path) -- Make sure the directory exist
local shadow_mute = mute.shadow_mute_list
local ignore = mute.ignore_lists
core.register_privilege("mute", "Can mute players")
local function send_message(sender, target, message)
if target == sender then
minetest.chat_send_player(target, message)
return
end
for _,muted in ipairs(shadow_mute) do
-- Check if the player is muted by an admin/mod
if muted == sender then
return
end
end
for _,muted_player in ipairs(ignore[target]) do
-- Check if the player is ignored by the player
if muted_player == sender then
return
end
end
minetest.chat_send_player(target, message)
end
mute.send_message = send_message
local function broadcast_message(sender, message)
for _,player in ipairs(minetest.get_connected_players()) do
local name = player:get_player_name()
send_message(sender, name, message)
end
end
mute.broadcast_message = broadcast_message
local function load_list(path)
local f = io.open(path, "r")
if f == nil then
return {}
else
local output = minetest.deserialize(f:read("*all"))
if output == nil then
minetest.log("error", "Failed to load " .. path .. " file is corrupted")
minetest.log("error", "Dumping contents to log file")
minetest.debug(f:read("*all"))
output = {}
end
f:close()
return output
end
end
minetest.register_on_joinplayer(function(player)
local name = player:get_player_name()
ignore[name] = load_list(ignore_lists_path .. name)
end)
local function save_ignore_list(player)
local name = player:get_player_name()
if #ignore[name] == 0 then return end
local f = io.open(ignore_lists_path .. name, "w")
f:write(minetest.serialize(ignore[name]))
f:close()
end
minetest.register_on_leaveplayer(save_ignore_list)
minetest.register_on_shutdown(function()
for _,player in ipairs(minetest.get_connected_players()) do
save_ignore_list(player)
end
local f = io.open(shadow_mute_path, "w")
f:write(minetest.serialize(shadow_mute))
f:close()
end)
minetest.register_on_chat_message(function(username, message)
for _,player in ipairs(minetest.get_connected_players()) do
local name = player:get_player_name()
if name ~= username then
send_message(username, name, "<" .. username .. "> " .. message)
end
end
return true
end)
if minetest.chatcommands["me"] then
minetest.chatcommands["me"].func = function(name, param)
broadcast_message(name, "* " .. name .. " " .. param)
end
end
if minetest.chatcommands["msg"] then
minetest.chatcommands["msg"].func = function(name, param)
local sendto, message = param:match("^(%S+)%s(.+)$")
if not sendto then
return false, "Invalid usage, see /help msg."
end
if not minetest.get_player_by_name(sendto) then
return false, "The player " .. sendto
.. " is not online."
end
minetest.log("action", "PM from " .. name .. " to " .. sendto
.. ": " .. message)
send_message(sendto, name, "PM from " .. name .. ": "
.. message)
return true, "Message sent."
end
end
minetest.register_chatcommand("ignore", {
params = "<player name>",
description = "Stop messages from this player from appearing in your chat",
func = function(name, param)
table.insert(ignore[name],param)
return true, "Ignored player " .. param
end,
})
minetest.register_chatcommand("unignore", {
params = "<player name>",
description = "Unignore a player",
func = function(name, param)
local t = ignore[name]
for i=#t,1,-1 do
local v = t[i]
if v == param then
table.remove(t, i)
end
end
return true, "Unignored player " .. param
end,
})
minetest.register_chatcommand("mute", {
params = "<player name>",
description = "Stop a players messages from being displayed",
privs = {mute = true},
func = function(name, param)
table.insert(shadow_mute, param)
return true, "Muted player " .. param
end,
})
minetest.register_chatcommand("unmute", {
params = "<player name>",
description = "Undo /mute",
privs = {mute = true},
func = function(name, param)
for i=#shadow_mute,1,-1 do
local v = shadow_mute[i]
print(i, v)
if v == param then
table.remove(shadow_mute, i)
end
end
return true, "Unmuted player " .. param
end,
})
shadow_mute = load_list(shadow_mute_path)