mail/api.lua

63 lines
1.6 KiB
Lua
Raw Normal View History

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