mail/api.lua
2019-02-27 08:20:09 +01:00

44 lines
1.2 KiB
Lua

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"
function mail.send(src, dst, subject, body)
minetest.log("action", "[mail] '" .. src .. "' sends mail to '" .. dst ..
"' with subject '" .. subject .. "' and body: '" .. body .. "'")
local messages = mail.getMessages(dst)
table.insert(messages, 1, {
unread = true,
sender = src,
subject = subject,
body = body,
time = os.time(),
})
mail.setMessages(dst, messages)
for _, player in ipairs(minetest.get_connected_players()) do
local name = player:get_player_name()
if name == dst then
if subject == "" then subject = "(No subject)" end
if string.len(subject) > 30 then
subject = string.sub(subject,1,27) .. "..."
end
minetest.chat_send_player(dst,
string.format(mail.receive_mail_message, src, subject))
end
end
mail.save()
for i=1, #mail.registered_on_receives do
if mail.registered_on_receives[i](src, dst, subject, body) then
break
end
end
end