-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinit.lua
283 lines (236 loc) · 6.8 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
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
279
280
281
282
283
-- chat3/init.lua
chat3 = {}
chat3.settings = {}
chat3.storage = minetest.get_mod_storage()
local modpath = minetest.get_modpath("chat3")
local prot = {} -- Table of protocol versions - to be used later
---
--- Handle Settings
---
-- [function] Get float setting
function chat3.settings.get_int(key)
local res = minetest.settings:get(key)
if res then
return tonumber(res)
end
end
-- [function] Get boolean setting
function chat3.settings.get_bool(key, default)
local retval = minetest.settings:get_bool(key)
if default and retval == nil then
retval = default
end
return retval
end
local bell = chat3.settings.get_bool("chat3.bell")
local shout = chat3.settings.get_bool("chat3.shout")
local prefix = minetest.settings:get("chat3.shout_prefix") or "!"
local near = chat3.settings.get_int("chat3.near") or 12
local ignore = chat3.settings.get_bool("chat3.ignore", true)
local alt = chat3.settings.get_bool("chat3.alt_support", true)
if prefix:len() > 1 then
prefix = "!"
end
---
--- Load Features
---
if ignore then dofile(modpath.."/ignore.lua") end -- Load ignore
if alt then dofile(modpath.."/alt.lua") end -- Load alt
---
--- Helpers
---
local function cache_protocal_version(name)
local info = minetest.get_player_information(name)
-- if info is not nil, cache the player's protocol version
if info ~= nil then
prot[name] = info.protocol_version
-- else, if info is nil, the player doesn't exist yet for some reason and
-- caching will occur the first time the information is requested.
end
end
local function get_protocal_version(name)
-- if version is cached, return it
if prot[name] then
return prot[name]
end
-- otherwise, retrive and cache it
cache_protocal_version(name)
end
---
--- Exposed Functions (API)
---
-- [function] Colorize
function chat3.colorize(name, colour, msg)
local vers = get_protocal_version(name)
if vers and vers >= 27 then
return minetest.colorize(colour, msg)
else
return msg
end
end
-- [function] Check if mentioned (should highlight or not)
function chat3.is_mentioned(name, msg)
name, msg = name:lower(), msg:lower()
-- Direct mentions
local direct_mention = msg:find(name, 1, true)
-- Alt mentions
local alt_mention
if alt then
local list = chat3.alt.get(name)
for alt, i in pairs(list) do
alt_mention = msg:find(alt, 1, true) or alt_mention
end
end
return direct_mention or alt_mention
end
-- [function] Process
function chat3.send(name, msg, prefix, source)
if not minetest.check_player_privs(name, "shout")
or minetest.get_modpath("ranks") and source ~= "ranks" then
return
end
local sender = minetest.get_player_by_name(name)
for _, player in pairs(minetest.get_connected_players()) do
local rname = player:get_player_name()
local colour = "#ffffff"
local vers = prot[rname]
if (not vers or (vers and (vers >= 29 or (vers < 29 and name ~= rname))))
and (not ignore or not chat3.ignore.is(rname, name)) then
-- Check for near
if near ~= 0 then -- and name ~= rname then
if vector.distance(sender:getpos(), player:getpos()) <= near then
colour = "#88ffff"
end
end
-- Check for mentions
if chat3.is_mentioned(rname, msg) then
colour = "#00ff00"
-- Chat bell
if bell and name ~= rname then
local pbell = player:get_attribute("chat3:bell")
if pbell ~= "false" then
minetest.sound_play("chat3_bell", {
gain = 4,
to_player = rname,
})
end
end
end
-- Check for shout
if shout and msg:sub(1, 1) == prefix then
colour = "#ff0000"
-- Chat bell
if bell and name ~= rname then
local pbell = player:get_attribute("chat3:bell")
if pbell ~= "false" then
minetest.sound_play("chat3_bell", {
gain = 4,
to_player = rname,
})
end
end
end
-- if same player, set to white
if name == rname then
colour = "#ffffff"
end
-- Send message
local send = chat3.colorize(rname, colour, "<"..name.."> "..msg)
if prefix then
send = prefix..send
end
minetest.chat_send_player(rname, send)
end
end
-- Log message
minetest.log("action", "CHAT: ".."<"..name.."> "..msg)
-- Prevent from sending normally
return true
end
---
--- Events/Definitions
---
-- [event] On join player
minetest.register_on_joinplayer(function(player)
local name = player:get_player_name()
cache_protocal_version(name)
end)
-- [event] On chat message
minetest.register_on_chat_message(function(name, msg)
return chat3.send(name, msg)
end)
-- [redefine] /msg
if minetest.chatcommands["msg"] then
local old_command = minetest.chatcommands["msg"].func
minetest.override_chatcommand("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
if ignore and chat3.ignore.is(sendto, name) then
return false, chat3.colorize(name, "red",
"Could not send message, you are on "..sendto.."'s ignore list.")
else
minetest.log("action", "PM from " .. name .. " to " .. sendto
.. ": " .. message)
minetest.chat_send_player(sendto, chat3.colorize(sendto, '#00ff00',
"PM from " .. name .. ": ".. message))
if bell then
local player = minetest.get_player_by_name(sendto)
local pbell = player:get_attribute("chat3:bell")
if pbell ~= "false" then
minetest.sound_play("chat3_bell", {
gain = 4,
to_player = sendto,
})
end
end
if ignore and chat3.ignore.is(name, sendto) then
return true, "Message sent.\n"..chat3.colorize(name, "red",
"Warning: "..sendto.." will not be able to respond to this"
.." message unless you remove them from your ignore list.")
else
return true, "Message sent."
end
end
end,
})
end
-- [redefine] /me
if minetest.chatcommands["me"] then
local old_command = minetest.chatcommands["me"]
minetest.override_chatcommand("me", {
func = function(name, param)
for _, player in pairs(minetest.get_connected_players()) do
local rname = player:get_player_name()
if not ignore or not chat3.ignore.is(rname, name) then
minetest.chat_send_player(rname, "* "..name.." "..param)
end
end
end,
})
end
-- [chatcommand] Chatbell
if bell then
minetest.register_chatcommand("chatbell", {
description = "Enable/disable chatbell when you are mentioned in the chat",
func = function(name)
local player = minetest.get_player_by_name(name)
if player then
local bell = player:get_attribute("chat3:bell")
if not bell or bell == "" or bell == "true" then
player:set_attribute("chat3:bell", "false")
return true, "Disabled Chatbell"
else
player:set_attribute("chat3:bell", "true")
return true, "Enabled Chatbell"
end
end
end,
})
end