68 lines
1.8 KiB
Lua
68 lines
1.8 KiB
Lua
local modpath = minetest.get_modpath(minetest.get_current_modname())
|
|
|
|
dofile(modpath.."/debug.lua")
|
|
|
|
local http = minetest.request_http_api()
|
|
if http == nil then
|
|
error("Please add matrix_bridge to secure.http_mods")
|
|
end
|
|
|
|
local token = nil
|
|
local txid = 0
|
|
|
|
function send_message(msg)
|
|
txid = txid + 1
|
|
http.fetch({
|
|
url = "https://matrix.txmn.tk:8448/_matrix/client/r0/rooms/!xReNhJbGJeGK4fysJU:matrix.txmn.tk/send/m.room.message/"..txid.."?access_token="..token,
|
|
method = "PUT",
|
|
extra_headers = {"Content-Type: application/json"},
|
|
data = minetest.write_json({msgtype="m.text", body=msg})
|
|
}, function(res)
|
|
end)
|
|
end
|
|
|
|
http.fetch({
|
|
url = "https://matrix.txmn.tk:8448/_matrix/client/r0/login",
|
|
method = "POST",
|
|
extra_headers = {"Content-Type: application/json"},
|
|
data = '{"type":"m.login.password","password":"PUT-PASSWORD-HERE","identifier":{"type":"m.id.user","user":"minebot"}}'
|
|
}, function(res)
|
|
if res.code == 200 then
|
|
minetest.log("action", res.data)
|
|
local data = minetest.parse_json(res.data)
|
|
token = data.access_token
|
|
minetest.log("action", "Matrix authenticated")
|
|
else
|
|
minetest.log("error", to_string(res))
|
|
end
|
|
end)
|
|
|
|
minetest.register_on_joinplayer(function(player)
|
|
local name = player:get_player_name()
|
|
if token then
|
|
send_message("*** "..name.." joined the game")
|
|
end
|
|
end)
|
|
|
|
minetest.register_on_leaveplayer(function(player, timed_out)
|
|
local name = player:get_player_name()
|
|
if token then
|
|
send_message("*** "..name.." left the game"..
|
|
(timed_out and " (Timed out)" or ""))
|
|
end
|
|
end)
|
|
|
|
minetest.register_on_chat_message(function(name, message)
|
|
if token == nil
|
|
or message:sub(1, 1) == "/"
|
|
or message:sub(1, 5) == "[off]"
|
|
or (not minetest.check_player_privs(name, {shout=true})) then
|
|
return
|
|
end
|
|
local nl = message:find("\n", 1, true)
|
|
if nl then
|
|
message = message:sub(1, nl - 1)
|
|
end
|
|
send_message("<"..name.."> "..message)
|
|
end)
|