First commit

master
googol 2019-03-12 14:59:38 +02:00
parent 175dd45390
commit faa51c3f90
7 changed files with 185 additions and 2 deletions

View File

@ -1,2 +1,13 @@
# lp_api
LongPoll APi mod for minetest
# LongPoll APi mod for minetest
#### Add this mods to the trusted_mods:
- Open : minetest.confg
- Add : secure.http_mods = lp_api
#### Edit parameters if needed in to minetest.confg
- lp_api.url (LongPoll server url)
- lp_api.channel_id (Channel ID in Discord)
- lp_api.timeout (Timeout connection)
- lp_api.header (Header if needed)
- lp_api.subscriber.timeout (Timeout for subscribe)
- lp_api.router.cmd (Support commands or not)

1
description.txt Normal file
View File

@ -0,0 +1 @@
lp_api

25
init.lua Normal file
View File

@ -0,0 +1,25 @@
lp_api = {}
lp_api.http = minetest.request_http_api()
lp_api.url = minetest.settings:get("lp_api.url") or "http://mtdbot.pp.ua:8003"
lp_api.channel_id = minetest.settings:get("lp_api.channel_id") or "minetest"
lp_api.timeout = minetest.settings:get("lp_api.timeout") or 30
-- For secure connect set header value (Authorization: Basic bG9naW46cGFzc3dvcmQ=)
-- where bG9naW46cGFzc3dvcmQ= is the base64(login:password)
lp_api.header = minetest.settings:get("lp_api.header") or nil
function lp_api.request_http(ext_url,pd,response_handler)
local req = {url = lp_api.url..ext_url, post_data = pd, extra_headers = {lp_api.header}, timeout = lp_api.timeout}
lp_api.http.fetch(req, function(result)
pcall(response_handler, result)
end)
end
if lp_api.http then
local default_path = minetest.get_modpath("lp_api")
dofile(default_path.."/router.lua")
dofile(default_path.."/publisher.lua")
dofile(default_path.."/subscriber.lua")
minetest.log("[lp_api] Loading... [OK]")
else
minetest.log("[lp_api] Please setup (secure.http_mod = lp_api) in minetest.conf... [ERROR]")
end

1
mod.conf Normal file
View File

@ -0,0 +1 @@
name = lp_api

19
publisher.lua Normal file
View File

@ -0,0 +1,19 @@
--
-- publisher
--
lp_api.publisher = {}
function lp_api.publisher.pub_msg(name, type, title, message)
if name == "minetest" and minetest.get_player_privs(name).shout or message:sub(1, 1) ~= "/" then
local json_msg = minetest.write_json({player = name, type = type, title = title, message = message})
if json_msg then
lp_api.request_http("/pub?category="..lp_api.channel_id, json_msg, lp_api.publisher.resp_handler)
end
end
end
function lp_api.publisher.resp_handler(result)
if not result.succeeded and result.code ~= 200 then
minetest.log("[lp_api] Pub request... [ERROR]")
end
end

101
router.lua Normal file
View File

@ -0,0 +1,101 @@
--
-- router
--
lp_api.router = {}
lp_api.router.cmd = minetest.settings:get("lp_api.router.cmd") or false
function lp_api.router.to_string(tab, from_key)
local res
for k = #tab, from_key, -1 do
if res then
res = tab[k].." "..res
else
res = tab[k]
end
end
return res
end
function lp_api.msg_router(data)
print(dump(data))
if data.type == "chat" then
minetest.chat_send_all(core.colorize("#debd21", "<"..data.player.."> "..data.message))
end
if data.type == "cmd" and lp_api.router.cmd then
if data.command == "msg" then
minetest.chat_send_player(data.args[1],
core.colorize("#de6821", "<"..data.player.."> "..lp_api.router.to_string(data.args, 2)))
end
if data.command == "ban" then
minetest.ban_player(data.args[1])
lp_api.publisher.pub_msg("minetest", "system", "Player {"..data.args[1].."} Banned!", "")
end
if data.command == "unban" then
minetest.unban_player_or_ip(data.args[1])
lp_api.publisher.pub_msg("minetest", "system", "Player {"..data.args[1].."} UnBanned!", "")
end
if data.command == "kick" then
local result = minetest.kick_player(data.args[1], lp_api.router.to_string(data.args, 2))
if result then
if data.args[2] then
lp_api.publisher.pub_msg("minetest", "system",
"Player {"..data.args[1].."} kicked!", lp_api.router.to_string(data.args, 2))
else
lp_api.publisher.pub_msg("minetest", "system",
"Player {"..data.args[1].."} kicked!", "")
end
end
end
if data.command == "grant" then
if data.args[2] then
local sp = lp_api.router.to_string(data.args, 2)
local add_privs = minetest.string_to_privs(sp)
local privs = minetest.get_player_privs(data.args[1])
for p, _ in pairs(add_privs) do
privs[p] = true
end
minetest.set_player_privs(data.args[1], privs)
lp_api.publisher.pub_msg("minetest", "system", "The {"..data.args[1].."} was given the", sp)
end
end
if data.command == "revoke" then
if data.args[2] then
local sp = lp_api.router.to_string(data.args, 2)
local add_privs = minetest.string_to_privs(sp)
local privs = minetest.get_player_privs(data.args[1])
for p, _ in pairs(add_privs) do
privs[p] = nil
end
minetest.set_player_privs(data.args[1], privs)
lp_api.publisher.pub_msg("minetest", "system", "The {"..data.args[1].."} was taken away the", sp)
end
end
if data.command == "privs" then
local privs = minetest.privs_to_string(minetest.get_player_privs(data.args[1]), ' ')
lp_api.publisher.pub_msg("minetest", "system", "Privileges of {"..data.args[1].."}:", privs)
end
if data.command == "status" then
lp_api.publisher.pub_msg("minetest", "system", "Status server.", minetest.get_server_status())
end
end
end
minetest.register_on_chat_message(function(name, message)
lp_api.publisher.pub_msg(name, "chat", "", message)
end)
minetest.register_on_joinplayer(function(player)
lp_api.publisher.pub_msg("minetest", "chat", "", "player {"..player:get_player_name().."} joined the game")
end)
minetest.register_on_leaveplayer(function(player)
lp_api.publisher.pub_msg("minetest", "chat", "", "player {"..player:get_player_name().."} left the game")
end)

25
subscriber.lua Normal file
View File

@ -0,0 +1,25 @@
--
-- subscriber
--
lp_api.subscriber = {}
lp_api.subscriber.timeout = minetest.settings:get("lp_api.subscriber.timeout") or 26
function lp_api.subscriber.sub_msg()
lp_api.request_http("/sub?timeout="..lp_api.subscriber.timeout.."&category="..lp_api.channel_id, "",
lp_api.subscriber.resp_handler)
end
function lp_api.subscriber.resp_handler(result)
if result.succeeded and result.code == 200 then
lp_api.subscriber.sub_msg()
local data = minetest.parse_json(string.sub(result.data, string.find(result.data, "data")+6, -4))
if data then
lp_api.msg_router(data)
end
else
minetest.after(lp_api.timeout, lp_api.subscriber.sub_msg)
minetest.log("[lp_api] Sub request... [ERROR]")
end
end
minetest.after(3, lp_api.subscriber.sub_msg)