mail-cd2025/webmail.lua

131 lines
3.1 KiB
Lua
Raw Normal View History

2019-09-16 08:06:54 +02:00
-- false per default
local has_xban2_mod = minetest.get_modpath("xban2")
local MP = minetest.get_modpath(minetest.get_current_modname())
local Channel = dofile(MP .. "/util/channel.lua")
local channel
-- auth request from webmail
2019-09-16 08:15:43 +02:00
local function auth_handler(auth)
2019-09-16 08:06:54 +02:00
local handler = minetest.get_auth_handler()
2019-09-16 08:15:43 +02:00
minetest.log("action", "[webmail] auth: " .. auth.name)
2019-09-16 08:06:54 +02:00
local success = false
local banned = false
local message = ""
if mail.webmail.disallow_banned_players and has_xban2_mod then
-- check xban db
2019-09-16 08:15:43 +02:00
local xbanentry = xban.find_entry(auth.name)
2019-09-16 08:06:54 +02:00
if xbanentry and xbanentry.banned then
banned = true
message = "Banned!"
end
end
if not banned then
-- check tan
2019-09-16 08:15:43 +02:00
local tan = mail.tan[auth.name]
2019-09-16 08:06:54 +02:00
if tan ~= nil then
success = tan == auth.password
end
-- check auth
if not success then
2019-09-16 08:15:43 +02:00
local entry = handler.get_auth(auth.name)
if entry and minetest.check_password_entry(auth.name, entry.password, auth.password) then
2019-09-16 08:06:54 +02:00
success = true
end
end
end
channel.send({
2019-09-16 08:15:43 +02:00
type = "auth",
data = {
name = auth.name,
2019-09-16 08:06:54 +02:00
success = success,
message = message
}
})
end
-- send request from webmail
2019-09-16 08:15:43 +02:00
local function send_handler(sendmail)
2019-09-16 08:06:54 +02:00
-- send mail from webclient
2019-09-16 08:15:43 +02:00
minetest.log("action", "[webmail] sending mail from webclient: " .. sendmail.src .. " -> " .. sendmail.dst)
mail.send(sendmail)
2019-09-16 08:06:54 +02:00
end
-- get player messages request from webmail
2019-09-16 08:15:43 +02:00
local function get_player_messages_handler(playername)
local messages = mail.getMessages(playername)
2019-09-16 08:06:54 +02:00
channel.send({
2019-09-16 08:15:43 +02:00
type = "player-messages",
playername = playername,
data = messages
2019-09-16 08:06:54 +02:00
})
end
-- remove mail
2019-09-16 08:15:43 +02:00
local function delete_mail_handler(playername, index)
2019-09-16 08:06:54 +02:00
local messages = mail.getMessages(playername)
if messages[index] then
table.remove(messages, index)
end
mail.setMessages(playername, messages)
end
-- mark mail as read
2019-09-16 08:15:43 +02:00
local function mark_mail_read_handler(playername, index)
2019-09-16 08:06:54 +02:00
local messages = mail.getMessages(playername)
if messages[index] then
2019-09-16 08:15:43 +02:00
messages[index].unread = false
2019-09-16 08:06:54 +02:00
end
mail.setMessages(playername, messages)
2019-09-16 08:15:43 +02:00
end
2019-09-16 08:06:54 +02:00
2019-09-16 08:15:43 +02:00
-- mark mail as unread
local function mark_mail_unread_handler(playername, index)
local messages = mail.getMessages(playername)
if messages[index] then
messages[index].unread = true
end
mail.setMessages(playername, messages)
2019-09-16 08:06:54 +02:00
end
function mail.webmail_send_hook(m)
channel.send({
type = "new-message",
data = m
})
end
mail.register_on_receive(mail.webmail_send_hook)
function mail.webmail_init(http, url, key)
channel = Channel(http, url .. "/api/minetest/channel", {
extra_headers = { "webmailkey: " .. key }
})
channel.receive(function(data)
2019-09-16 08:15:43 +02:00
if data.type == "auth" then
auth_handler(data.data)
2019-09-16 08:06:54 +02:00
2019-09-16 08:15:43 +02:00
elseif data.type == "send" then
send_handler(data.data) -- { src, dst, subject, body }
2019-09-16 08:06:54 +02:00
2019-09-16 08:15:43 +02:00
elseif data.type == "delete-mail" then
delete_mail_handler(data.playername, data.index) -- index 1-based
2019-09-16 08:06:54 +02:00
2019-09-16 08:15:43 +02:00
elseif data.type == "mark-mail-read" then
mark_mail_read_handler(data.playername, data.index) -- index 1-based
2019-09-16 08:06:54 +02:00
2019-09-16 08:15:43 +02:00
elseif data.type == "mark-mail-unread" then
mark_mail_unread_handler(data.playername, data.index) -- index 1-based
2019-09-16 08:06:54 +02:00
2019-09-16 08:15:43 +02:00
elseif data.type == "player-messages" then
get_player_messages_handler(data.data)
2019-09-16 08:06:54 +02:00
end
end)
end