Skip to content

Instantly share code, notes, and snippets.

@rubenwardy
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubenwardy/2cbf8ea1867c86835a9d to your computer and use it in GitHub Desktop.
Save rubenwardy/2cbf8ea1867c86835a9d to your computer and use it in GitHub Desktop.
local function run_command(name, args)
if args == "" then
return false, "You need a command."
end
local found, _, commandname, params = args:find("^([^%s]+)%s(.+)$")
if not found then
commandname = args
end
local command = minetest.chatcommands[commandname]
if not command then
return false, "Not a valid command."
end
if not minetest.check_player_privs(name, command.privs) then
return false, "Your privileges are insufficient."
end
minetest.log("action", name.."@IRC runs "..args)
return command.func(name, (params or ""))
end
local http = nil
local ltn12 = nil
local server_id = nil
local auth_key = nil
local function init()
-- Include modules
local ie = minetest.request_insecure_environment()
if not ie then
error("Insecure environment required!")
end
http = ie.require("socket.http")
ltn12 = ie.require("ltn12")
http.TIMEOUT = 0.1
-- Get startup data from webpanel
local f = io.open(minetest.get_worldpath() .. "/webpanel.txt", "r")
local data = minetest.deserialize(f:read("*all"))
f.close()
server_id = data.server_id
auth_key = data.auth_key
end
init()
local function process_frompanel(input)
local data = minetest.deserialize(input)
for i = 1, #data do
local item = data[i]
print("Recieved mode " .. item.mode .. " from server.")
end
end
local function sync()
local host = "http://localhost:5000/"
local method = "get"
local path = "api/" .. server_id .. "/" .. auth_key .. "/server_update/"
local params = ""
local resp = {}
-- Compose URL
local url = ""
if params:trim() ~= "" then
url = host .. path .. "?" .. params
else
url = host .. path
end
-- Make Request
local client, code, headers, status = http.request({url=url, sink=ltn12.sink.table(resp),
method=method })
-- Not used:
-- headers=args.headers, source=args.source,
-- step=args.step, proxy=args.proxy, redirect=args.redirect, create=args.create
return
if code ~= 200 then
if code == 404 then
minetest.log("warning", "The webpanel reports that this server does not exist!")
else
minetest.log("warning", "The webpanel gave an unknown HTTP response code!")
end
return
end
if resp and resp[1] then
resp = resp[1]:trim()
else
resp = nil
end
if resp == "auth" then
minetest.log("warning", "Authentication error when requesting commands from webpanel")
return
end
if resp == "offline" then
minetest.log("warning", "The webpanel reports that this server should be offline!")
return
end
if string.find(resp, "return", 1) == nil then
minetest.log("warning", "The webpanel gave an invalid response!")
print(dump(resp))
return
end
process_frompanel(resp)
end
local function updatetick()
sync()
minetest.after(1, updatetick)
end
minetest.after(1, updatetick)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment