First commit

master
Gabriel Pérez-Cerezo 2019-04-17 09:15:32 +02:00
commit 51c61834e3
4 changed files with 183 additions and 0 deletions

21
README.md Normal file
View File

@ -0,0 +1,21 @@
# Auth_export mod for minetest
This is an ad-hoc mod written to interface Minetest with Mediawiki
(see [the mediawiki plugin](/AuthMinetest.git/)). It is intended only
for usage with the http server bundled with the mediawiki plugin.
# Installation
To set up your minetest installation to communicate with the minetest server, edit your "minetest.conf":
```
# enable curl/http on that mod
secure.http_mods = mail
secure.http_mods = auth_export
```
# credits
This mod is essentially scavenged from
[the webmail mod](https://github.com/thomasrudin-mt/mail) by Thomas
Rudin.

93
channel.lua Normal file
View File

@ -0,0 +1,93 @@
-- bi-directional http-channel
-- with long-poll GET and POST on the same URL
local debug = true
local function Channel(http, url, cfg)
cfg = cfg or {}
local extra_headers = cfg.extra_headers or {}
local timeout = cfg.timeout or 1
local long_poll_timeout = cfg.long_poll_timeout or 30
local error_retry = cfg.error_retry or 10
-- assemble post-header with json content
local post_headers = { "Content-Type: application/json" }
for _,header in pairs(cfg.extra_headers) do
table.insert(post_headers, header)
end
local recv_listeners = {}
local run = true
local recv_loop
recv_loop = function()
assert(run)
-- long-poll GET
http.fetch({
url = url,
extra_headers = extra_headers,
timeout = long_poll_timeout
}, function(res)
if res.succeeded and res.code == 200 then
local data = minetest.parse_json(res.data)
if debug then
minetest.log("action", "[webmail-rx] " .. dump(data))
end
if data then
for _,listener in pairs(recv_listeners) do
listener(data)
end
end
-- reschedule immediately
minetest.after(0, recv_loop)
else
-- error, retry after some time
minetest.after(error_retry, recv_loop)
end
end)
end
local send = function(data)
assert(run)
-- POST
if debug then
minetest.log("action", "[webmail-tx] " .. dump(data))
end
http.fetch({
url = url,
extra_headers = post_headers,
timeout = timeout,
post_data = minetest.write_json(data)
}, function(res)
-- TODO: error-handling
end)
end
local receive = function(listener)
table.insert(recv_listeners, listener)
end
local close = function()
run = false
end
recv_loop();
return {
send = send,
receive = receive,
close = close
}
end
return Channel

16
init.lua Normal file
View File

@ -0,0 +1,16 @@
local http = minetest.request_http_api()
mail = {}
local MP = minetest.get_modpath(minetest.get_current_modname())
mail.webmail = {}
mail.webmail.disallow_banned_players = true
if http then
local webmail_url = "http://127.0.0.1:8080"
local webmail_key = "foo bar baz" -- not used in the mod but might get used in the future.
if not webmail_url then error("webmail.url is not defined") end
if not webmail_key then error("webmail.key is not defined") end
print("[mail] loading webmail-component with endpoint: " .. webmail_url)
dofile(MP .. "/webmail.lua")
mail.webmail_init(http, webmail_url, webmail_key)
end

53
webmail.lua Normal file
View File

@ -0,0 +1,53 @@
-- false per default
local has_xban2_mod = minetest.get_modpath("xban2")
local MP = minetest.get_modpath(minetest.get_current_modname())
local Channel = dofile(MP .. "/channel.lua")
local channel
-- auth request from webmail
local function auth_handler(auth)
local handler = minetest.get_auth_handler()
minetest.log("action", "[webmail] auth: " .. auth.name)
local success = false
local banned = false
local message = ""
if mail.webmail.disallow_banned_players and has_xban2_mod then
-- check xban db
local xbanentry = xban.find_entry(auth.name)
if xbanentry and xbanentry.banned then
banned = true
message = "Banned!"
end
end
if not banned then
local entry = handler.get_auth(auth.name)
if entry and minetest.check_password_entry(auth.name, entry.password, auth.password) then
success = true
end
end
channel.send({
type = "auth",
data = {
name = auth.name,
success = success,
message = message
}
})
end
function mail.webmail_init(http, url, key)
channel = Channel(http, url .. "/api/minetest/channel", {
extra_headers = { "webmailkey: " .. key }
})
channel.receive(function(data)
if data.type == "auth" then
auth_handler(data.data)
end
end)
end