mailall.lua 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. mailall = mailall or {}
  2. mailall.modpath = minetest.get_modpath("hb4")
  3. mailall.worldpath = minetest.get_worldpath()
  4. function mailall.get_registered_players()
  5. if mailall.registered_players then
  6. return mailall.registered_players
  7. end
  8. -- Read data, if it hasn't been loaded for this session yet.
  9. local file = io.open(mailall.worldpath .. "/registered-players.txt", "r")
  10. if not file then
  11. return {}
  12. end
  13. local data = file:read("*all")
  14. file:close()
  15. local players = {}
  16. if data and data ~= "" then
  17. local lines = string.split(data, "\n")
  18. for _, line in pairs(lines) do
  19. local name = string.trim(line)
  20. players[#players+1] = name
  21. end
  22. end
  23. mailall.registered_players = players
  24. return players
  25. end
  26. function mailall.send_mail(name, subj, msg)
  27. local players = mailall.get_registered_players()
  28. local from = string.trim(name)
  29. local subject = string.trim(subj)
  30. local message = string.trim(msg)
  31. -- Insert line breaks.
  32. message = string.gsub(message, "%%[nN]", "\n")
  33. --minetest.chat_send_player(name, "# Server: Would send: \"" .. message .. "\".")
  34. --do return 0, 0 end
  35. local good, bad = email.send_mail_multi(from, players, subject, message)
  36. minetest.chat_send_player(name, "# Server: Email sent to " .. #good ..
  37. " of " .. #players .. " total registered players. " ..
  38. "Failed to send message to " .. #bad .. " players.")
  39. return #good, #bad
  40. end
  41. function mailall.run_command(name, param)
  42. mailall.send_mail(name, "Public Announcement", param)
  43. end
  44. if not mailall.registered then
  45. minetest.register_chatcommand("mailall", {
  46. params = "<message>",
  47. description = "Send an in-game email to all registered players on the server.",
  48. privs = {server=true},
  49. func = function(...)
  50. return mailall.run_command(...)
  51. end
  52. })
  53. mailall.registered = true
  54. end