Skip to content
Snippets Groups Projects
Commit 180bd41d authored by Peter Nerlich's avatar Peter Nerlich
Browse files

suppress some unneeded messages, only transport messages when triggered by...

suppress some unneeded messages, only transport messages when triggered by username (from matrix side) or callphrase (from minetest side) as prefix
parent cd2c6c94
No related branches found
No related tags found
No related merge requests found
-- This file is licensed under the terms of the BSD 2-clause license. -- This file is licensed under the terms of the BSD 2-clause license.
-- See LICENSE.txt for details. -- See LICENSE.txt for details.
--[[
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
local name = player:get_player_name() local name = player:get_player_name()
if matrix.connected then --and matrix.config.send_join_part then if matrix.connected then --and matrix.config.send_join_part then
...@@ -15,6 +16,7 @@ minetest.register_on_leaveplayer(function(player, timed_out) ...@@ -15,6 +16,7 @@ minetest.register_on_leaveplayer(function(player, timed_out)
(timed_out and " (Timed out)" or "")) (timed_out and " (Timed out)" or ""))
end end
end) end)
]]--
minetest.register_on_chat_message(function(name, message) minetest.register_on_chat_message(function(name, message)
if not matrix.connected if not matrix.connected
...@@ -24,11 +26,18 @@ minetest.register_on_chat_message(function(name, message) ...@@ -24,11 +26,18 @@ minetest.register_on_chat_message(function(name, message)
or (not minetest.check_player_privs(name, {shout=true})) then or (not minetest.check_player_privs(name, {shout=true})) then
return return
end end
message = trim(message)
local prefixlen = string.len(matrix.config.callphrase) + 1
local prefix = string.sub( string.lower(message), 1, prefixlen)
if prefix == string.lower(matrix.config.callphrase.." ") then
local nl = message:find("\n", 1, true) local nl = message:find("\n", 1, true)
if nl then if nl then
message = message:sub(1, nl - 1) message = message:sub(1, nl - 1)
end end
matrix.say("<"..name.."> "..message) matrix.say("<"..name.."> "..string.sub(message, prefixlen+1))
end
end) end)
minetest.register_on_shutdown(function() minetest.register_on_shutdown(function()
......
...@@ -30,3 +30,4 @@ setting("string", "user", nil, true) -- User name, fe @digbot:matrix.org ...@@ -30,3 +30,4 @@ setting("string", "user", nil, true) -- User name, fe @digbot:matrix.org
setting("string", "server", nil, true) -- Server address to connect to setting("string", "server", nil, true) -- Server address to connect to
setting("string", "room_id", nil, true) -- Channel to join (not needed?) setting("string", "room_id", nil, true) -- Channel to join (not needed?)
setting("string", "password", nil, true) -- Server password setting("string", "password", nil, true) -- Server password
setting("string", "callphrase", "@matrix", true) -- Prefix that triggers transporting messages to the matrix side.
...@@ -27,6 +27,10 @@ matrix = { ...@@ -27,6 +27,10 @@ matrix = {
dofile(modpath.."/config.lua") dofile(modpath.."/config.lua")
dofile(modpath.."/debug.lua") dofile(modpath.."/debug.lua")
function trim(s)
return s:match("^%s*(.-)%s*$")
end
-- Temporarily set require so that LuaIRC can access it -- Temporarily set require so that LuaIRC can access it
local old_require = require local old_require = require
require = ie.require require = ie.require
...@@ -67,21 +71,21 @@ end):hook("joined", function (client, room) ...@@ -67,21 +71,21 @@ end):hook("joined", function (client, room)
room:hook("message", function (room, sender, message, event) room:hook("message", function (room, sender, message, event)
if event.origin_server_ts < start_ts then if event.origin_server_ts < start_ts then
eprintf("%s: (Skipping message sent before bot startup)\n", room) --eprintf("%s: (Skipping message sent before bot startup)\n", room)
return return
end end
if sender == room.client.user_id then if sender == room.client.user_id then
eprintf("%s: (Skipping message sent by ourselves)\n", room) --eprintf("%s: (Skipping message sent by ourselves)\n", room)
return return
end end
if message.msgtype ~= "m.text" then if message.msgtype ~= "m.text" then
eprintf("%s: (Message of type %s ignored)\n", room, message.msgtype) --eprintf("%s: (Message of type %s ignored)\n", room, message.msgtype)
return return
end end
eprintf("%s: <%s> %s\n", room, sender, message.body) --eprintf("%s: <%s> %s\n", room, sender, message.body)
if message.body == "!bot quit" then --[[if message.body == "!bot quit" then
for _, room in pairs(client.rooms) do for _, room in pairs(client.rooms) do
room:send_text("(gracefully shutting down)") room:send_text("(gracefully shutting down)")
end end
...@@ -89,6 +93,13 @@ end):hook("joined", function (client, room) ...@@ -89,6 +93,13 @@ end):hook("joined", function (client, room)
matrix.connected = false matrix.connected = false
elseif room.room_id == matrix.config.room_id then elseif room.room_id == matrix.config.room_id then
minetest.chat_send_all("<"..sender.."> "..message.body) minetest.chat_send_all("<"..sender.."> "..message.body)
end]]--
message.body = trim(message.body)
local prefixlen = string.len(matrix.config.user) + 1
local prefix = string.sub( string.lower(message.body), 1, prefixlen)
if prefix == string.lower(matrix.config.user.." ") then
minetest.chat_send_all("<"..sender.."> "..string.sub(message.body, prefixlen+1))
end end
end) end)
end) end)
...@@ -126,7 +137,7 @@ function matrix.connect() ...@@ -126,7 +137,7 @@ function matrix.connect()
client:login_with_password(matrix.config.user, matrix.config.password, true) client:login_with_password(matrix.config.user, matrix.config.password, true)
matrix.connected = true matrix.connected = true
minetest.log("action", "Matrix: Connected!") minetest.log("action", "Matrix: Connected!")
minetest.chat_send_all("Matrix: Connected!") --minetest.chat_send_all("Matrix: Connected!")
end end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment