2018-12-05 12:04:39 +01:00
|
|
|
|
|
|
|
local url, key, http
|
|
|
|
|
|
|
|
local webmail = {}
|
|
|
|
|
|
|
|
-- polls the webmail server and processes the logins made there
|
|
|
|
webmail.auth_collector = function()
|
|
|
|
http.fetch({
|
2018-12-05 13:15:28 +01:00
|
|
|
url=url .. "/api/minetest/auth_collector",
|
2018-12-05 12:04:39 +01:00
|
|
|
extra_headers = { "webmailkey: " .. key },
|
2018-12-05 13:56:19 +01:00
|
|
|
timeout=15
|
2018-12-05 12:04:39 +01:00
|
|
|
}, function(res)
|
|
|
|
|
|
|
|
if res.code == 403 then
|
|
|
|
-- unauthorized, abort
|
|
|
|
minetest.log("error", "[webmail] invalid key specified!")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if res.succeeded and res.code == 200 then
|
2018-12-05 15:14:58 +01:00
|
|
|
local auth = minetest.parse_json(res.data)
|
|
|
|
if auth then
|
2018-12-05 12:04:39 +01:00
|
|
|
local auth_response = {}
|
|
|
|
local handler = minetest.get_auth_handler()
|
|
|
|
|
2018-12-05 15:14:58 +01:00
|
|
|
local success = false
|
|
|
|
local entry = handler.get_auth(auth.name)
|
|
|
|
if entry and minetest.check_password_entry(auth.name, entry.password, auth.password) then
|
|
|
|
success = true
|
|
|
|
end
|
2018-12-05 12:04:39 +01:00
|
|
|
|
2018-12-05 15:14:58 +01:00
|
|
|
-- send back auth response data
|
|
|
|
http.fetch({
|
|
|
|
url=url .. "/api/minetest/auth_collector",
|
|
|
|
extra_headers = { "Content-Type: application/json", "webmailkey: " .. key },
|
|
|
|
post_data = minetest.write_json({
|
2018-12-05 12:04:39 +01:00
|
|
|
name = auth.name,
|
|
|
|
success = success
|
|
|
|
})
|
2018-12-05 15:14:58 +01:00
|
|
|
}, function(res)
|
|
|
|
-- stub
|
|
|
|
end)
|
2018-12-05 12:04:39 +01:00
|
|
|
|
|
|
|
end
|
2018-12-05 13:56:19 +01:00
|
|
|
-- execute again
|
|
|
|
minetest.after(1, webmail.auth_collector)
|
|
|
|
else
|
|
|
|
-- execute again (error case)
|
|
|
|
minetest.after(10, webmail.auth_collector)
|
2018-12-05 12:04:39 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2018-12-05 13:56:19 +01:00
|
|
|
-- called on mail saving to disk (every change)
|
|
|
|
mail.webmail_save_hook = function()
|
|
|
|
http.fetch({
|
|
|
|
url=url .. "/api/minetest/messages",
|
|
|
|
extra_headers = { "Content-Type: application/json", "webmailkey: " .. key },
|
|
|
|
post_data = minetest.write_json(mail.messages)
|
|
|
|
}, function(res)
|
|
|
|
if not res.succeeded then
|
|
|
|
minetest.log("error", "[webmail] message sync to web failed")
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- polls the message endpoint for commands from the webclient
|
|
|
|
webmail.message_command_loop = function()
|
|
|
|
http.fetch({
|
|
|
|
url=url .. "/api/minetest/messages",
|
|
|
|
extra_headers = { "webmailkey: " .. key },
|
|
|
|
timeout=15
|
|
|
|
}, function(res)
|
|
|
|
|
|
|
|
if res.code == 403 then
|
|
|
|
-- unauthorized, abort
|
|
|
|
minetest.log("error", "[webmail] invalid key specified!")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if res.succeeded and res.code == 200 then
|
|
|
|
local data = minetest.parse_json(res.data)
|
|
|
|
if data then
|
|
|
|
for _,cmd in pairs(data) do
|
|
|
|
if cmd.type == "send" and cmd.mail and cmd.mail.src and cmd.mail.dst then
|
|
|
|
-- send mail from webclient
|
|
|
|
local sendmail = cmd.mail
|
|
|
|
minetest.log("action", "[webmail] sending mail from webclient: " .. sendmail.src .. " -> " .. sendmail.dst)
|
|
|
|
mail.send(sendmail.src, sendmail.dst, sendmail.subject, sendmail.body)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- execute again
|
|
|
|
minetest.after(1, webmail.message_command_loop)
|
|
|
|
else
|
|
|
|
-- execute again (error case)
|
|
|
|
minetest.after(10, webmail.message_command_loop)
|
|
|
|
end
|
|
|
|
|
|
|
|
end)
|
|
|
|
end
|
2018-12-05 12:04:39 +01:00
|
|
|
|
|
|
|
mail.webmail_init = function(_http, webmail_url, webmail_key)
|
|
|
|
url = webmail_url
|
|
|
|
key = webmail_key
|
|
|
|
http = _http
|
|
|
|
|
2018-12-05 13:56:19 +01:00
|
|
|
minetest.after(4, function()
|
|
|
|
-- start auth collector loop
|
|
|
|
webmail.auth_collector()
|
|
|
|
|
|
|
|
-- start message command loop
|
|
|
|
webmail.message_command_loop()
|
|
|
|
|
|
|
|
-- sync messages after server start
|
|
|
|
if #mail.messages > 0 then
|
|
|
|
-- only if mails available
|
|
|
|
mail.webmail_save_hook()
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|