api.lua 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. -- see: mail.md
  2. mail.registered_on_receives = {}
  3. function mail.register_on_receive(func)
  4. mail.registered_on_receives[#mail.registered_on_receives + 1] = func
  5. end
  6. mail.receive_mail_message = "You have a new message from %s! Subject: %s\nTo view it, type /mail"
  7. mail.read_later_message = "You can read your messages later by using the /mail command"
  8. --[[
  9. mail sending function, can be invoked with one object argument (new api) or
  10. all 4 parameters (old compat version)
  11. see: "Mail format" api.md
  12. --]]
  13. function mail.send(src, dst, subject, body)
  14. local m
  15. if dst == nil and subject == nil and body == nil then
  16. -- new format (one object param)
  17. m = src
  18. else
  19. -- old format
  20. m = {}
  21. m.src = src
  22. m.dst = dst
  23. m.subject = subject
  24. m.body = body
  25. end
  26. minetest.log("action", "[mail] '" .. m.src .. "' sends mail to '" .. m.dst ..
  27. "' with subject '" .. m.subject .. "' and body: '" .. m.body .. "'")
  28. local messages = mail.getMessages(m.dst)
  29. table.insert(messages, 1, {
  30. unread = true,
  31. sender = m.src,
  32. subject = m.subject,
  33. body = m.body,
  34. time = os.time(),
  35. })
  36. mail.setMessages(m.dst, messages)
  37. for _, player in ipairs(minetest.get_connected_players()) do
  38. local name = player:get_player_name()
  39. if name == m.dst then
  40. if m.subject == "" then m.subject = "(No subject)" end
  41. if string.len(m.subject) > 30 then
  42. m.subject = string.sub(m.subject,1,27) .. "..."
  43. end
  44. minetest.chat_send_player(m.dst,
  45. string.format(mail.receive_mail_message, m.src, m.subject))
  46. end
  47. end
  48. for i=1, #mail.registered_on_receives do
  49. if mail.registered_on_receives[i](m) then
  50. break
  51. end
  52. end
  53. end