diff options
| author | rebecca <ubq323@ubq323.website> | 2026-09-13 16:25:56 +0100 |
|---|---|---|
| committer | rebecca <ubq323@ubq323.website> | 2026-09-13 16:25:56 +0100 |
| commit | c9b43b5bb9d85f68edded36428fb14f05debf190 (patch) | |
| tree | 5c0c296c6b56fa66ceb874366e8b25e10e23baed | |
| parent | 57b7cec33e32f02fe67de91aec66662b6ae5c6ab (diff) | |
use log more pervasively, changes to discord and xmpp:
discord: fix some logic errors, correctly ignore messages in channels
that we don't care about
xmpp: reformat some of the recving logic, but it's still borken somehow,
but olive says she wants to fix it not me
| -rw-r--r-- | discord/pylon.lua | 20 | ||||
| -rw-r--r-- | log.lua | 12 | ||||
| -rw-r--r-- | pylon.lua | 1 | ||||
| -rw-r--r-- | queue.lua | 2 | ||||
| -rw-r--r-- | xmpp/pylon.lua | 31 |
5 files changed, 38 insertions, 28 deletions
diff --git a/discord/pylon.lua b/discord/pylon.lua index 0583e6b..fa2638d 100644 --- a/discord/pylon.lua +++ b/discord/pylon.lua @@ -2,7 +2,6 @@ local cqueues = require 'cqueues' local websocket = require'http.websocket' local request = require'http.request' local json = require 'dkjson' -local pprint = require 'pprint' local class = require 'r.class' local BasePylon = require 'pylon' @@ -109,12 +108,17 @@ end function Discord.handle_dispatch(self, event) local d = event.d if event.t == 'MESSAGE_CREATE' then - if d.author.id == self.channel_to_webhook[d.channel_id].id then return end - if d.author.id == self.bot_id then return end - self.wilson:deliver(Channel(self, d.channel_id), { - body = d.content, - sender = '[d]' .. d.author.username, - }) + self.log:proto('<',d.channel_id,d.author.username,d.content) + if + self.channel_to_webhook[d.channel_id] + and d.author.id ~= self.channel_to_webhook[d.channel_id].id + and d.author.id ~= self.bot_id + then + self.wilson:deliver(Channel(self, d.channel_id), { + body = d.content, + sender = '[d]' .. d.author.username, + }) + end elseif event.t == 'READY' then self.log:debug(event.d) end @@ -122,7 +126,7 @@ end function Discord.sending(self) for dest_channel, message in self.inbox:iter() do - print('DDD',dest_channel,message.sender,message.body) + self.log:proto('>',dest_channel,message.sender,message.body) exec_webhook(self.channel_to_webhook[dest_channel.descriptor].url, { username = message.sender, content = message.body, @@ -2,6 +2,8 @@ local pprint = require 'pprint' local class = require 'r.class' local qw = require 'r.qw' +local levels = qw"debug proto info warn error" + local Log = class() function Log.make(cls, name, level) return setmetatable({ @@ -9,12 +11,14 @@ function Log.make(cls, name, level) level = level or 'info', },cls) end -local levels = qw"debug info warn error" +local colours = {debug="\x1b[38;5;8m",error="\x1b[31m"} function Log.format(self, level, ...) local vals = {} for i=1,select('#',...) do - vals[i] = tostring(select(i,...)) end - return (level == 'error' and '\x1b[31m' or '') - ..os.date"%Y-%m-%d %H:%M:%S%z" + local v = (select(i,...)) + vals[i] = type(v) == 'string' and v or pprint.pformat(v) + end + return (colours[level] or '') + .. os.date"%Y-%m-%d %H:%M:%S%z" .. ' ' .. level .. (" "):rep(5-#level) .. ' [' .. self.name .. '] ' .. table.concat(vals,' ') .. '\x1b[0m' .. '\n' @@ -23,6 +23,7 @@ function BasePylon.run(self) self.cq = cqueues.new() self.cq:wrap(function() self:_connect() + self.log"connected!" self.cq:wrap(self.recving, self) self.cq:wrap(self.sending, self) end) @@ -13,7 +13,6 @@ function Queue.make(cls) function Queue.enqueue(self, ...) local item = table.pack(...) - print('q',...) table.insert(self.items, item) if #self.items > 128 then print('warning: queue is quite big') @@ -29,7 +28,6 @@ function Queue.iter(self) self.items = {} -- the old switcheroo for _, item in ipairs(items) do (function(...) - print('p',...) coroutine.yield(...) end)(table.unpack(item, 1, item.n)) end diff --git a/xmpp/pylon.lua b/xmpp/pylon.lua index 302b787..9568999 100644 --- a/xmpp/pylon.lua +++ b/xmpp/pylon.lua @@ -12,7 +12,6 @@ local Xmpp = class.extend(BasePylon) function Xmpp.init(self) self:_check_fields"server component component_secret" - self.nicks_inuse = {} end @@ -42,8 +41,6 @@ function Xmpp._connect_component(self) end Xmpp._connect = Xmpp._connect_component -local THE_MUC = 'd@conference.ubq323.website' - function Xmpp.recving(self) local function getmore() local function t(...) @@ -53,15 +50,20 @@ function Xmpp.recving(self) return t(self.sock:read'-2048') end for x in xml.stanzae(getmore) do - -- pprint(x) - -- print(xmlify(x)) + self.log:debug(xmlify(x)) local body = x'body' and x'body'[1] if x.label == 'message' then - local fr = x.xarg.from - local t = x.xarg.to - local from_nick = fr:match("/(.*)") - if not self.nicks_inuse[from_nick] and body and fr:match"/" and t == 'wilson@'..self.component then - self.wilson:deliver(Channel(self, THE_MUC), { + local from = x.xarg.from + local to = x.xarg.to + local from_addr, from_nick = from and from:match"(.*)/(.*)" + if + from_nick + and not self.nicks_inuse[from_nick] + and body + and to == 'wilson@'..self.component + then + self.log:proto('<',from_addr,from_nick,body) + self.wilson:deliver(Channel(self, from_addr), { body = body, sender = '[x]'..from_nick }) @@ -86,14 +88,15 @@ function Xmpp.sending(self) X.x{xmlns='http://jabber.org/protocol/muc', X.history{maxstanzas='0'}}})) end - ensure_joined(THE_MUC, 'wilson', 'wilson') + ensure_joined('d@conference.ubq323.website', 'wilson', 'wilson') for dest_channel, message in self.inbox:iter() do - -- pprint(dest_channel, message) - ensure_joined(THE_MUC, message.sender, message.sender) + self.log:proto('>',dest_channel,message.sender,message.body) + local muc = dest_channel.descriptor + ensure_joined(muc, message.sender, message.sender) local user = message.sender:gsub("[^a-zA-Z0-9%.]","."):match("^%.*(.-)%.*$") local jid = user..'@'..self.component self.sock:write(xmlify( - X.message{to=THE_MUC, type='groupchat', from=jid, + X.message{to=muc, type='groupchat', from=jid, X.body{message.body}})) end end |
