lua_xmpp_privacy_bot/main.lua

80 lines
3.1 KiB
Lua
Raw Permalink Normal View History

2024-06-01 16:46:19 -05:00
-- Get the verse lib
2024-06-01 17:26:57 -05:00
verse = require("verse")
2024-06-01 16:46:19 -05:00
-- Setup logging and config
require("utils")
local log = setup_log(string.format("%s_main", config.name))
log("info", "Log initialized")
-- Get the client lib
local client_lib = verse.init("client")
-- Make the client
local client = client_lib.new()
-- Load client plugins
client:add_plugin("groupchat")
client:add_plugin("version")
-- Client hooks
client:hook("disconnected", function()
log("error", "XMPP connetion lost. Quitting...")
os.exit(1)
end)
client:hook("authentication-failure", function()
log("error", "Failed to authenticate with XMPP Server. Quitting...")
os.exit(1)
end)
client:hook("authentication-success", function()
log("info", "XMPP authentication sucessful!")
end)
client:hook("ready", function()
log("info", "Client ready")
-- Main code goes here
for _, room in pairs(config.rooms_to_join) do
local room, err = client:join_room(room.jid, config.name, {}, config.password)
if room then
-- Run on message events
room:hook("message", function(event)
2024-06-01 18:18:11 -05:00
if event.stanza.attr.type == "groupchat"
and not string.find(event.stanza.attr.from, "/" .. config.name)
2024-06-24 17:03:12 -05:00
and not event.stanza:get_child("delay", "urn:xmpp:delay")
and not string.find(event.stanza.attr.from, "Bot") -- This is here because of the Thumbnail bot incident
then
2024-06-01 16:46:19 -05:00
local body = event.stanza:get_child_text("body")
if body then
2024-06-24 17:03:12 -05:00
if event.stanza:get_child("reply", 'urn:xmpp:reply:0') then
local fallback = event.stanza:get_child("fallback", 'urn:xmpp:fallback:0')
if fallback then
local fallback_body = fallback:child_with_name("body")
if fallback_body then
body = string.sub(body, fallback_body.attr["end"])
end
end
else
body = string.gsub(body, ">[^\n\r]-[\n\r]+", "")
end
2024-06-01 16:46:19 -05:00
for site, services in pairs(config.sites) do
2024-06-01 18:43:32 -05:00
local instance = choose_instance(services)
2024-06-01 16:46:19 -05:00
for match in string.gmatch(body, string.format("%%s(%s/%%S+)", site)) do
2024-06-01 17:26:57 -05:00
send_reply_link(room, match, site, instance, event)
2024-06-01 16:46:19 -05:00
end
for match in string.gmatch(body, string.format("(https?://%s/%%S+)", site)) do
2024-06-01 17:26:57 -05:00
send_reply_link(room, match, site, instance, event)
2024-06-01 16:46:19 -05:00
end
end
end
end
end)
2024-06-01 18:18:11 -05:00
log("info", "Joined room \"%s\"", room.jid)
2024-06-01 16:46:19 -05:00
else
log("error", "Error joining room \"%s\": %s", room.jid, err)
end
end
end)
log("info", "Connecting to server...")
client:connect_client(config.jid, config.password)
verse.loop()
log("info", "Verse loop exited. Quitting...")