forked from AliasAlreadyTaken/yl_matterbridge
128 lines
4.3 KiB
Lua
128 lines
4.3 KiB
Lua
-- Version 0.0.2
|
|
-- Author AliasAlreadyTaken
|
|
-- License MIT
|
|
|
|
-- Changelog
|
|
|
|
-- 0.0.1 First puny attempts
|
|
-- 0.0.2 feature complete including settings
|
|
|
|
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.2"
|
|
yl_matterbridge.information.author = "AliasAlreadyTaken"
|
|
yl_matterbridge.information.license = "MIT"
|
|
yl_matterbridge.information.name = "yl_matterbridge"
|
|
yl_matterbridge.information.source = "https://gitea.your-land.de/AliasAlreadyTaken/yl_matterbridge"
|
|
yl_matterbridge.information.additional =
|
|
"Sends and receives ingame chat to and from matterbridge https://github.com/42wim/matterbridge"
|
|
|
|
local address = core.settings:get("yl_matterbridge.address") or "127.0.0.1"
|
|
local port = core.settings:get("yl_matterbridge.port") or "4242"
|
|
local gateway = core.settings:get("yl_matterbridge.gateway") or "default"
|
|
local token = core.settings:get("yl_matterbridge.token") or ""
|
|
|
|
local http = core.request_http_api()
|
|
|
|
assert(http, "[MOD] yl_matterbridge: Please add yl_matterbridge to secure.http_mods")
|
|
|
|
local post_headers = {"Content-Type: application/json"}
|
|
if token and token ~= "" then
|
|
table.insert(post_headers, "Authorization: Bearer " .. token)
|
|
end
|
|
local url_send = "http://" .. address .. ":" .. port .. "/api/message"
|
|
local url_receive = "http://" .. address .. ":" .. port .. "/api/messages"
|
|
|
|
function send(user_name, message_text)
|
|
local timeout = 10
|
|
local data = {
|
|
text = message_text,
|
|
username = user_name,
|
|
gateway = gateway
|
|
}
|
|
|
|
http.fetch(
|
|
{
|
|
url = url_send,
|
|
extra_headers = post_headers,
|
|
timeout = timeout,
|
|
post_data = core.write_json(data)
|
|
},
|
|
function(result)
|
|
if result.succeeded then
|
|
local data = core.parse_json(result.data)
|
|
core.log("action", "[MOD] yl_matterbridge : Posted " .. dump(data))
|
|
else
|
|
core.log("error", "[MOD] yl_matterbridge: send/http.fetch failed. Result = " .. dump(result))
|
|
return false
|
|
end
|
|
end
|
|
)
|
|
return true
|
|
end
|
|
|
|
local function receive()
|
|
local timeout = 0
|
|
|
|
http.fetch(
|
|
{
|
|
url = url_receive,
|
|
extra_headers = post_headers,
|
|
timeout = timeout
|
|
},
|
|
function(result)
|
|
if result.succeeded then
|
|
local data = core.parse_json(result.data)
|
|
for _, v in ipairs(data) do
|
|
if v.username and v.text and v.account then
|
|
core.log("action", "[MOD] yl_matterbridge : Received " .. dump(data))
|
|
yl_matterbridge.receive_from_bridge(v.username, v.text, v.account)
|
|
end
|
|
end
|
|
else
|
|
core.log("error", "[MOD] yl_matterbridge: receive/http.fetch failed. Result = " .. 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.receive_from_bridge(user_name, message_text, account_name)
|
|
core.chat_send_all("<" .. account_name .. "|" .. user_name .. "> " .. message_text)
|
|
end
|
|
|
|
function yl_matterbridge.send_to_bridge(user_name, message_text)
|
|
send(user_name, message_text)
|
|
end
|
|
|
|
-- Set this function to yl_matterbridge.chat_message = function() end
|
|
-- if you call yl_matterbridge.send_to_bridge yourself
|
|
|
|
function yl_matterbridge.chat_message(user_name, message_text)
|
|
yl_matterbridge.send_to_bridge(user_name, message_text)
|
|
end
|
|
|
|
-- connect to Minetest
|
|
function yl_matterbridge.register_chat()
|
|
core.register_on_chat_message(yl_matterbridge.chat_message)
|
|
end
|
|
|
|
minetest.register_on_mods_loaded(yl_matterbridge.register_chat)
|
|
|
|
core.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]")
|