mail/api.lua

63 lines
1.6 KiB
Lua
Raw Normal View History

2019-09-16 09:06:54 +03:00
-- see: mail.md
mail.registered_on_receives = {}
function mail.register_on_receive(func)
mail.registered_on_receives[#mail.registered_on_receives + 1] = func
end
mail.receive_mail_message = "You have a new message from %s! Subject: %s\nTo view it, type /mail"
mail.read_later_message = "You can read your messages later by using the /mail command"
--[[
mail sending function, can be invoked with one object argument (new api) or
all 4 parameters (old compat version)
see: "Mail format" api.md
--]]
2019-09-16 09:15:43 +03:00
function mail.send(src, dst, subject, body)
2019-09-16 09:06:54 +03:00
local m
2019-09-16 09:15:43 +03:00
if dst == nil and subject == nil and body == nil then
2019-09-16 09:06:54 +03:00
-- new format (one object param)
2019-09-16 09:15:43 +03:00
m = src
2019-09-16 09:06:54 +03:00
else
-- old format
m = {}
2019-09-16 09:15:43 +03:00
m.src = src
m.dst = dst
2019-09-16 09:06:54 +03:00
m.subject = subject
m.body = body
end
2019-09-16 09:15:43 +03:00
minetest.log("action", "[mail] '" .. m.src .. "' sends mail to '" .. m.dst ..
2019-09-16 09:06:54 +03:00
"' with subject '" .. m.subject .. "' and body: '" .. m.body .. "'")
2019-09-16 09:15:43 +03:00
local messages = mail.getMessages(m.dst)
2019-09-16 09:06:54 +03:00
2019-09-16 09:15:43 +03:00
table.insert(messages, 1, {
unread = true,
sender = m.src,
subject = m.subject,
body = m.body,
time = os.time(),
})
mail.setMessages(m.dst, messages)
2019-09-16 09:06:54 +03:00
for _, player in ipairs(minetest.get_connected_players()) do
local name = player:get_player_name()
2019-09-16 09:15:43 +03:00
if name == m.dst then
2019-09-16 09:06:54 +03:00
if m.subject == "" then m.subject = "(No subject)" end
if string.len(m.subject) > 30 then
m.subject = string.sub(m.subject,1,27) .. "..."
end
2019-09-16 09:15:43 +03:00
minetest.chat_send_player(m.dst,
string.format(mail.receive_mail_message, m.src, m.subject))
2019-09-16 09:06:54 +03:00
end
end
for i=1, #mail.registered_on_receives do
if mail.registered_on_receives[i](m) then
break
end
end
end