lua_xmpp_sed_bot/utils.lua
2024-06-24 17:00:30 -05:00

74 lines
No EOL
1.9 KiB
Lua

-- Various utility functions used by the bot
sed_expresion_regex = [[
((?:>[^\r\n]*\R+)+)
s\/
((?:[^\r\n\[\/\\]|\\.|\[(?:[^\r\n\]\\]|\\.)*\])+)
\/
((?:[^\r\n\[\/\\]|\\.|\[(?:[^\r\n\]\\]|\\.)*\])*)
\/(.*?)(\s+|$)
]]
function log_callback(source, level, message, ...)
local output = string.format(
"%s %s [%s]: %s",
os.date(),
source,
level,
string.format(message, ...)
)
if config.verbosity == 0 then
if level ~= "debug" then
print(output)
end
elseif config.verbosity == 1 then
print(output)
end
end
--[[
Make a new logger with `name`
]]--
function setup_log(name)
local log_module = require("util.logger")
log_module.add_level_sink("debug", log_callback)
log_module.add_level_sink("info", log_callback)
log_module.add_level_sink("warn", log_callback)
log_module.add_level_sink("error", log_callback)
return log_module.init(name)
end
-- Read a file in it's entirety, returning an empty string on failure
function read_all_text(file)
local file = io.open(file, "rb")
if not file then
print(string.format("Failed to open file \"%s\"!", file))
return ""
end
local text = file:read("*all")
file:close()
return text
end
function send_sed_output(room, result, event)
--local msg = string.format("> s/%s/%s/%s\n%s", pattern, replacement, flags, result)
local msg = result
if config.use_reply_xep then
room:send(verse.message()
-- Set message text
:body(msg):up()
-- Set reply block
:tag("reply", {
xmlns = 'urn:xmpp:reply:0',
to = event.stanza.attr.from,
id = event.stanza:get_child("stanza-id", "urn:xmpp:sid:0").attr.id,
}):up()
:tag("markable", {
xmlns = "urn:xmpp:chat-markers:0"
}));
else
room:send_message(msg)
end
end
dofile("config.lua")