add reply support and fix some bugs

This commit is contained in:
mcneb10 2024-06-01 17:26:57 -05:00
parent 7bf1a4d3fe
commit 3bc78e3991
3 changed files with 25 additions and 8 deletions

View file

@ -1,6 +1,6 @@
-- Main privacy bot configuration file
config = {
-- Log verbosity
-- Log verbosity, 1 will print debug 0 will not. TODO: give more control over log output
verbosity = 1,
-- Bot nickname
name = "Privacy Link Bot",
@ -15,6 +15,8 @@ config = {
prefered_website_medium = "clearnet",
-- Choose random frontend instead of fallback one, will force clearnet
random_frontend = true,
-- Reply using XEP-0461 instead of just quoting
use_reply_xep = true,
-- List of desired frontends to extract from `services.json`
sites = {
-- Key is domain pattern

View file

@ -1,5 +1,5 @@
-- Get the verse lib
local verse = require("verse")
verse = require("verse")
-- Setup logging and config
require("utils")
local log = setup_log(string.format("%s_main", config.name))
@ -39,12 +39,11 @@ client:hook("ready", function()
if body then
for site, services in pairs(config.sites) do
local instance = choose_instance(services.frontends)
-- TODO: make it reply using XEP-0461
for match in string.gmatch(body, string.format("%%s(%s/%%S+)", site)) do
room:send_message(string.format("> %s\nPrivate frontend: %s", match, string.gsub(match, site, instance)))
send_reply_link(room, match, site, instance, event)
end
for match in string.gmatch(body, string.format("(https?://%s/%%S+)", site)) do
room:send_message(string.format("> %s\nPrivate frontend: %s", match, string.gsub(match, site, instance)))
send_reply_link(room, match, site, instance, event)
end
end
end

View file

@ -18,8 +18,7 @@ function log_callback(source, level, message, ...)
end
--[[
Make a new logger with `name` and setup a handler based on the log level `v`
TODO: `v` needs to be figured out, for now it's 1 to print debug messages 0 to not print them or any other value to print nothing
Make a new logger with `name`
]]--
function setup_log(name)
local log_module = require("util.logger")
@ -42,12 +41,29 @@ function read_all_text(file)
return text
end
function send_reply_link(room, match, site, instance, event)
local msg = string.format("> %s\nPrivate frontend: %s", match, string.gsub(match, site, instance))
if config.use_reply_xep then
room:send(verse.message()
-- Set message text
:body(msg)
-- Set reply block
:tag("reply", {
xmlns = 'urn:xmpp:reply:0',
to = event.stanza.attr.from,
id = event.stanza.attr.id,
}))
else
room:send_message(msg)
end
end
-- Choose instance from available services
function choose_instance(services)
-- TODO: make it try all available services before falling back
-- Choose a random service
local service = services[math.random(#services)]
-- Get list of instances for service
local service_instances
for _, service_instance_list in pairs(config.instances) do
if service_instance_list.type == service then
-- Based on config choose instance