diff --git a/init.lua b/init.lua index 8a0195803aada62459c9934747342edb54d72f34..8d27c95a0370682c4a23d3bb6c4f1e55ca1d4a95 100644 --- a/init.lua +++ b/init.lua @@ -7,27 +7,27 @@ local modpath = minetest.get_modpath(minetest.get_current_modname()) local ie, req_ie = _G, minetest.request_insecure_environment if req_ie then ie = req_ie() end if not ie then - error("The Matrix mod requires access to insecure functions in order ".. - "to work. Please add the matrix mod to your secure.trusted_mods ".. - "setting or disable the matrix mod.") + error("The Matrix mod requires access to insecure functions in order ".. + "to work. Please add the matrix mod to your secure.trusted_mods ".. + "setting or disable the matrix mod.") end ie.package.path = - modpath.."/lua-matrix/?.lua;" - ..ie.package.path + modpath.."/lua-matrix/?.lua;" + ..ie.package.path matrix = { - version = "0.0.1", - joined_players = {}, - connected = false, - modpath = modpath, - lib = lib, + version = "0.0.1", + joined_players = {}, + connected = false, + modpath = modpath, + lib = lib, } dofile(modpath.."/config.lua") local function eprintf(fmt, ...) - minetest.log("info", fmt:format(...)) + minetest.log("info", fmt:format(...)) end local client = require("matrix").client("https://"..matrix.config.server..":"..matrix.config.port) @@ -41,11 +41,11 @@ client:hook("invite", function (client, room) client:join_room(room) end end):hook("logged-in", function (client) - matrix.connected = true + matrix.connected = true eprintf("Logged in successfully\n") end):hook("logged-out", function (client) - eprintf("Logged out... bye!\n") - matrix.connected = false + eprintf("Logged out... bye!\n") + matrix.connected = false end):hook("left", function (client, room) eprintf("Left room %s, active rooms:\n", room) for room_id, room in pairs(client.rooms) do @@ -61,7 +61,7 @@ end):hook("joined", function (client, room) --room:send_text("Type “!bot quit” to make the bot exit") - room:hook("message", function (room, sender, message, event) + 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) return @@ -77,9 +77,9 @@ end):hook("joined", function (client, room) 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)") + room:send_text("(gracefully shutting down)") end client:logout() matrix.connected = false @@ -92,52 +92,54 @@ end) dofile(modpath.."/callback.lua") -local stepnum = 0 - minetest.register_globalstep(function(dtime) return matrix.step(dtime) end) -function matrix.step() - if stepnum == 3 then - matrix.connect() - end - stepnum = stepnum + 1 - - if not matrix.connected then return end +local stepnum = 0 +local interval = 1 +local counter = 0 - -- Hooks will manage incoming messages and errors - local good, err = xpcall(function() client:_sync() end, debug.traceback) - if not good then - print(err) - return - end +function matrix.step(dtime) + if stepnum == 3 then + matrix.connect() + end + stepnum = stepnum + 1 + counter = counter + dtime + if counter >= interval and matrix.connected then + counter = counter - interval + local good, err = xpcall(function() client:_sync() end, debug.traceback) + if not good then + print(err) + return + end + end end function matrix.connect() - if matrix.connected then - minetest.log("error", "Matrix: already connected") - return - end - 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!") + if matrix.connected then + minetest.log("error", "Matrix: already connected") + return + end + 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!") end function matrix.disconnect(message) - if matrix.connected then - --The OnDisconnect hook will clear matrix.connected and print a disconnect message - client:logout() - end + if matrix.connected then + --The OnDisconnect hook will clear matrix.connected and print a disconnect message + client:logout() + end end function matrix.say(to, message) - if not message then - message = to - to = matrix.config.channel - end - to = to or matrix.config.channel - for room_id, room in pairs(client.rooms) do - room:send_text(message) - end + if not message then + message = to + to = matrix.config.channel + end + to = to or matrix.config.channel + for room_id, room in pairs(client.rooms) do + room:send_text(message) + end end