diff --git a/callback.lua b/callback.lua index af25e390612a6ecca472df6caf7a40c6c221d326..9ece159a7d15cd5ef3ceb53ea06b69a1954a4a71 100644 --- a/callback.lua +++ b/callback.lua @@ -1,6 +1,7 @@ -- This file is licensed under the terms of the BSD 2-clause license. -- See LICENSE.txt for details. +--[[ minetest.register_on_joinplayer(function(player) local name = player:get_player_name() if matrix.connected then --and matrix.config.send_join_part then @@ -15,6 +16,7 @@ minetest.register_on_leaveplayer(function(player, timed_out) (timed_out and " (Timed out)" or "")) end end) +]]-- minetest.register_on_chat_message(function(name, message) if not matrix.connected @@ -24,11 +26,18 @@ minetest.register_on_chat_message(function(name, message) 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) + + + 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) + if nl then + message = message:sub(1, nl - 1) + end + matrix.say("<"..name.."> "..string.sub(message, prefixlen+1)) end - matrix.say("<"..name.."> "..message) end) minetest.register_on_shutdown(function() diff --git a/config.lua b/config.lua index 6af12c3235226d3d232af6eb2da03111774c2213..fef01004a9aad6572d28e44a6f6f461a23bab11d 100644 --- a/config.lua +++ b/config.lua @@ -26,7 +26,8 @@ end -- BASIC USER SETTINGS -- ------------------------- -setting("string", "user", nil, true) -- User name, fe @digbot:matrix.org -setting("string", "server", nil, true) -- Server address to connect to -setting("string", "room_id", nil, true) -- Channel to join (not needed?) -setting("string", "password", nil, true) -- Server password +setting("string", "user", nil, true) -- User name, fe @digbot:matrix.org +setting("string", "server", nil, true) -- Server address to connect to +setting("string", "room_id", nil, true) -- Channel to join (not needed?) +setting("string", "password", nil, true) -- Server password +setting("string", "callphrase", "@matrix", true) -- Prefix that triggers transporting messages to the matrix side. diff --git a/init.lua b/init.lua index bd536fc46808abe4093b518b34b773cc75c2a63f..9849e81b7bce974cf8de60bd126ec7237913382a 100644 --- a/init.lua +++ b/init.lua @@ -27,6 +27,10 @@ matrix = { dofile(modpath.."/config.lua") dofile(modpath.."/debug.lua") +function trim(s) + return s:match("^%s*(.-)%s*$") +end + -- Temporarily set require so that LuaIRC can access it local old_require = require require = ie.require @@ -67,21 +71,21 @@ end):hook("joined", function (client, room) room:hook("message", function (room, sender, message, event) 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 end 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 end 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 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 room:send_text("(gracefully shutting down)") end @@ -89,8 +93,15 @@ end):hook("joined", function (client, room) matrix.connected = false elseif room.room_id == matrix.config.room_id then 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) @@ -126,7 +137,7 @@ function matrix.connect() client:login_with_password(matrix.config.user, matrix.config.password, true) matrix.connected = true minetest.log("action", "Matrix: Connected!") - minetest.chat_send_all("Matrix: Connected!") + --minetest.chat_send_all("Matrix: Connected!") end