api.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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, find a mailbox."
  7. mail.read_later_message = "You can read your messages later by visiting a mail box."
  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. TODO: refactor this garbage code!
  13. --]]
  14. function mail.send(src, dst, subject, body)
  15. -- figure out format
  16. local m
  17. if dst == nil and subject == nil and body == nil then
  18. -- new format (one object param)
  19. m = src
  20. else
  21. -- old format
  22. m = {}
  23. m.from = src
  24. m.to = dst
  25. m.subject = subject
  26. m.body = body
  27. end
  28. if m.dst and not m.to then
  29. -- populate "to" field
  30. m.to = m.dst
  31. end
  32. if m.src and not m.from then
  33. -- populate "from" field
  34. m.from = m.src
  35. end
  36. -- sane default values
  37. m.subject = m.subject or ""
  38. m.body = m.body or ""
  39. local cc
  40. local bcc
  41. local extra
  42. -- log mail send action
  43. if m.cc or m.bcc then
  44. if m.cc then
  45. cc = "CC: " .. m.cc
  46. if m.bcc then
  47. cc = cc .. " - "
  48. end
  49. else
  50. cc = ""
  51. end
  52. if m.bcc then
  53. bcc = "BCC: " .. m.bcc
  54. else
  55. bcc = ""
  56. end
  57. extra = " (" .. cc .. bcc .. ")"
  58. else
  59. extra = ""
  60. end
  61. minetest.log("action", "[mail] '" .. m.from .. "' sends mail to '" .. m.to .. "'" ..
  62. extra .. "' with subject '" .. m.subject .. "' and body: '" .. m.body .. "'")
  63. -- normalize to, cc and bcc while compiling a list of all recipients
  64. local recipients = {}
  65. m.to = mail.normalize_players_and_add_recipients(m.to, recipients)
  66. if m.cc then
  67. m.cc = mail.normalize_players_and_add_recipients(m.cc, recipients)
  68. end
  69. if m.bcc then
  70. m.bcc = mail.normalize_players_and_add_recipients(m.bcc, recipients)
  71. end
  72. -- form the actual mail
  73. local msg = {
  74. unread = true,
  75. sender = m.from,
  76. to = m.to,
  77. subject = m.subject,
  78. body = m.body,
  79. time = os.time(),
  80. }
  81. if m.cc then
  82. msg.cc = m.cc
  83. end
  84. -- send the mail to all recipients
  85. for _, recipient in pairs(recipients) do
  86. local messages = mail.getMessages(recipient)
  87. table.insert(messages, 1, msg)
  88. mail.setMessages(recipient, messages)
  89. end
  90. -- notify recipients that happen to be online
  91. for _, player in ipairs(minetest.get_connected_players()) do
  92. local name = player:get_player_name()
  93. if recipients[string.lower(name)] ~= nil then
  94. if m.subject == "" then m.subject = "(No subject)" end
  95. if string.len(m.subject) > 30 then
  96. m.subject = string.sub(m.subject,1,27) .. "..."
  97. end
  98. minetest.chat_send_player(name,
  99. string.format(mail.receive_mail_message, m.from, m.subject))
  100. end
  101. end
  102. for i=1, #mail.registered_on_receives do
  103. if mail.registered_on_receives[i](m) then
  104. break
  105. end
  106. end
  107. end