107 lines
3.2 KiB
Lua
107 lines
3.2 KiB
Lua
-- Version 0.0.1
|
|
-- Author AliasAlreadyTaken
|
|
-- License MIT
|
|
|
|
-- Changelog
|
|
|
|
local mod_start_time = core.get_us_time()
|
|
core.log("action", "[MOD] yl_matterbridge loading")
|
|
|
|
yl_matterbridge = {}
|
|
yl_matterbridge.error = {}
|
|
yl_matterbridge.modstorage = core.get_mod_storage()
|
|
yl_matterbridge.modpath = core.get_modpath("yl_matterbridge") .. DIR_DELIM
|
|
yl_matterbridge.worldpath = core.get_worldpath() .. DIR_DELIM
|
|
|
|
yl_matterbridge.information = {}
|
|
yl_matterbridge.information.version = "0.0.1"
|
|
yl_matterbridge.information.author = "AliasAlreadyTaken"
|
|
yl_matterbridge.information.license = "MIT"
|
|
yl_matterbridge.information.name = "yl_matterbridge"
|
|
yl_matterbridge.information.additional = "Additional information"
|
|
|
|
local address = minetest.settings:get("yl_matterbridge.address") or false
|
|
local port = minetest.settings:get("yl_matterbridge.port") or false
|
|
local gateway = minetest.settings:get("yl_matterbridge.gateway") or false
|
|
local token = minetest.settings:get("yl_matterbridge.token") or ""
|
|
|
|
local http = minetest.request_http_api()
|
|
|
|
function send(user_name, message_text)
|
|
local url = "http://" .. address .. ":" .. port .. "/api/message"
|
|
local post_headers = {"Content-Type: application/json"}
|
|
local timeout = 10
|
|
local data = {
|
|
text = message_text,
|
|
username = user_name,
|
|
gateway = gateway
|
|
}
|
|
|
|
http.fetch(
|
|
{
|
|
url = url,
|
|
extra_headers = post_headers,
|
|
timeout = timeout,
|
|
post_data = minetest.write_json(data)
|
|
},
|
|
function(result)
|
|
if result.succeeded then
|
|
local data = minetest.parse_json(result.data)
|
|
core.log("action", "[MOD] yl_matterbridge : Posted " .. dump(data))
|
|
else
|
|
minetest.log("error", "[yl_matterbridge] " .. dump(result))
|
|
return false
|
|
end
|
|
end
|
|
)
|
|
return true
|
|
end
|
|
|
|
local function receive()
|
|
local url = "http://" .. address .. ":" .. port .. "/api/messages"
|
|
local timeout = 0
|
|
|
|
http.fetch(
|
|
{
|
|
url = url,
|
|
timeout = timeout
|
|
},
|
|
function(result)
|
|
if result.succeeded then
|
|
local data = minetest.parse_json(result.data)
|
|
for _,v in ipairs(data) do
|
|
if v.username and v.text then
|
|
yl_matterbridge.publish_to_chat(v.username, v.text)
|
|
end
|
|
end
|
|
else
|
|
minetest.log("error", "[yl_matterbridge] " .. dump(result))
|
|
return false
|
|
end
|
|
end
|
|
)
|
|
end
|
|
|
|
-- API
|
|
-- Overwrite these function in your chat mod, if you want to govern sending and receveiving yourself
|
|
-- Don't forget to add an optional dependency to yl_matterbridge
|
|
|
|
function yl_matterbridge.publish_to_chat(user_name,message_text)
|
|
core.chat_send_all("<"..user_name.."@irc> "..message_text)
|
|
end
|
|
|
|
function yl_matterbridge.send_to_bridge(user_name,message_text)
|
|
send(user_name, message_text)
|
|
end
|
|
|
|
-- connect to Minetest
|
|
|
|
minetest.register_on_chat_message(
|
|
yl_matterbridge.send_to_bridge
|
|
)
|
|
|
|
minetest.register_globalstep(receive)
|
|
|
|
local mod_end_time = (core.get_us_time() - mod_start_time) / 1000000
|
|
core.log("action", "[MOD] yl_matterbridge loaded in [" .. mod_end_time .. "s]")
|