diff options
author | ubq323 <ubq323@ubq323.website> | 2025-02-27 23:23:57 +0000 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2025-02-27 23:23:57 +0000 |
commit | 5dea96ad915692e9abbc0620930756c51c256179 (patch) | |
tree | 881d7680a521474f262bac262f382e778c1fc191 | |
parent | ec4053dea479c3c85d57c447e262a90addcce5c3 (diff) |
restructure, refactor, a bit
-rw-r--r-- | .luacheckrc | 2 | ||||
-rw-r--r-- | channel.lua (renamed from util/channel.lua) | 0 | ||||
-rw-r--r-- | config.lua (renamed from util/config.lua) | 0 | ||||
-rw-r--r-- | discord/pylon.lua | 37 | ||||
-rw-r--r-- | irc/pylon.lua | 30 | ||||
-rw-r--r-- | main.lua | 4 | ||||
-rw-r--r-- | pylon.lua | 55 | ||||
-rw-r--r-- | queue.lua (renamed from util/queue.lua) | 6 | ||||
-rw-r--r-- | wilson.ini | 1 | ||||
-rw-r--r-- | xmpp/pylon.lua | 53 | ||||
-rw-r--r-- | xmpp/xml.lua | 9 |
11 files changed, 100 insertions, 97 deletions
diff --git a/.luacheckrc b/.luacheckrc new file mode 100644 index 0000000..8686178 --- /dev/null +++ b/.luacheckrc @@ -0,0 +1,2 @@ +codes = true +ignore = { "6..", "21.", "23.", "31.", "4.."} diff --git a/util/channel.lua b/channel.lua index e908069..e908069 100644 --- a/util/channel.lua +++ b/channel.lua diff --git a/util/config.lua b/config.lua index c19a087..c19a087 100644 --- a/util/config.lua +++ b/config.lua diff --git a/discord/pylon.lua b/discord/pylon.lua index 1ecc042..f84db64 100644 --- a/discord/pylon.lua +++ b/discord/pylon.lua @@ -1,23 +1,15 @@ local consts = require'discord.consts' local websocket = require'http.websocket' local json = require 'dkjson' -local Queue = require 'util.queue' +local Queue = require 'queue' local exec_webhook = require'discord.the' local cqueues = require 'cqueues' +local pylon = require 'pylon' -local Discord = {} +local Discord = pylon.subclass "discord" -function Discord.make(wilson, conf) - local self = setmetatable(conf, {__index=Discord}) - - for k,v in pairs { - wilson = wilson, - inbox = Queue.make(), - } do self[k] = v end - - assert(self.token) - - return self +function Discord.init(self) + self:check_config "token" end local function identify_payload(token) @@ -44,14 +36,6 @@ function Discord._connect(self) -- end end -function Discord.run(self) - local cq = cqueues.new() - self:_connect() - cq:wrap(self.recving, self) - cq:wrap(self.sending, self) - print('discord', cq:loop()) -end - function Discord.recving(self) for event in self.ws:each() do print(event) @@ -59,11 +43,12 @@ function Discord.recving(self) end function Discord.sending(self) - local i = 1 - while true do - exec_webhook(self.temp_wh, { username = "god", content = "h"..("i"):rep(i) }) - i = i + 1 - cqueues.sleep(1) + for dest_channel, message in self.inbox:iter() do + print('DDD',message.sender,message.body) + exec_webhook(self.temp_wh, { + username = message.sender, + content = message.body, + }) end end diff --git a/irc/pylon.lua b/irc/pylon.lua index 0c898dd..8c43b13 100644 --- a/irc/pylon.lua +++ b/irc/pylon.lua @@ -2,10 +2,11 @@ local cqueues = require'cqueues' local socket = require'cqueues.socket' local pprint = require'pprint' local rirc = require'irc.rirc' -local Queue = require'util.queue' -local Channel = require'util.channel' +local Queue = require'queue' +local Channel = require'channel' +local pylon = require'pylon' -local Irc = {} +local Irc = pylon.subclass 'irc' function Irc._send(self, args) args.source = args.source or self.nodename @@ -13,21 +14,8 @@ function Irc._send(self, args) print('>', sent) end -function Irc.check_config(self, vars) - for x in vars:gmatch("%S+") do - assert(self[x], "missing conf field "..x) end end - -function Irc.make(wilson, conf) - local self = setmetatable(conf, {__index=Irc}) - - for k,v in pairs { - wilson = wilson, - inbox = Queue.make(), - } do self[k] = v end - +function Irc.init(self) self:check_config "host port password nodename" - - return self end function Irc._connect(self) @@ -36,14 +24,6 @@ function Irc._connect(self) self:_send{'SERVER', self.nodename, '1', 'i am wilson'} end -function Irc.run(self) - local cq = cqueues.new() - self:_connect() - cq:wrap(self.recving, self) - cq:wrap(self.sending, self) - print('irc', cq:loop()) -end - function Irc.recving(self) for line in self.sock:lines "*l" do print('<', line) @@ -1,6 +1,6 @@ local cqueues = require'cqueues' -local config = require'util.config' -local Channel = require'util.channel' +local config = require'config' +local Channel = require'channel' local pylon_classes = { irc = require'irc.pylon', diff --git a/pylon.lua b/pylon.lua new file mode 100644 index 0000000..12b3fdb --- /dev/null +++ b/pylon.lua @@ -0,0 +1,55 @@ +local cqueues = require 'cqueues' +local Queue = require 'queue' + +-- commonality between the different pylon classes +-- they can "inherit" from this + +local BasePylon = {} + +function BasePylon.check_config(self, vars) + for x in vars:gmatch"%S+" do + assert(self[x], "missing conf field "..x) + end +end + +function BasePylon.run(self) + local cq = cqueues.new() + self:_connect() + cq:wrap(self.recving, self) + cq:wrap(self.sending, self) + print(self.pylon_type, cq:loop()) +end + +function BasePylon.post(self, dest_channel, message) + self.inbox:enqueue(dest_channel, message) +end + +function BasePylon.log(self, ...) + if self.debug then + print(self.pylon_type, self.name, ...) + end +end + +local function subclass(pylon_type) + local Subclass = {} + setmetatable(Subclass, {__index=BasePylon}) + Subclass.pylon_type = pylon_type + + Subclass.make = function(wilson, conf) + local self = setmetatable(conf, {__index=Subclass}) + for k,v in pairs { + wilson = wilson, + inbox = Queue.make(), + } do self[k] = v end + + self:init(wilson, conf) + return self + end + + return Subclass +end + +return { + BasePylon = BasePylon, + subclass = subclass, +} diff --git a/util/queue.lua b/queue.lua index 8c9c373..5a30646 100644 --- a/util/queue.lua +++ b/queue.lua @@ -13,6 +13,7 @@ end 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') @@ -27,7 +28,10 @@ function Queue.iter(self) local items = self.items self.items = {} -- the old switcheroo for _, item in ipairs(items) do - coroutine.yield(table.unpack(item, 1, item.n)) + (function(...) + print('p',...) + coroutine.yield(...) + end)(table.unpack(item, 1, item.n)) end end self.cv:wait() @@ -19,3 +19,4 @@ temp_wh=https://discord.com/api/v8/webhooks/1277689699254800436/gzYU3voeunQsEdC7 [bus test] ubq-xmpp d@conference.ubq323.website local-irc #test +discord 868268751433375805 diff --git a/xmpp/pylon.lua b/xmpp/pylon.lua index ba9c252..2476fc9 100644 --- a/xmpp/pylon.lua +++ b/xmpp/pylon.lua @@ -1,42 +1,24 @@ -local cqueues = require'cqueues' -local cqaux = require'cqueues.auxlib' local socket = require'cqueues.socket' local xml = require'xmpp.xml' local X = xml.X local xmlify = xml.xmlify -local pprint=require'pprint' -local Queue = require'util.queue' local base64 = require'xmpp.base64' local sha1 = require'xmpp.sha1' -local Channel = require'util.channel' +local Channel = require'channel' +local pylon = require 'pylon' -local Xmpp = {} +local Xmpp = pylon.subclass "xmpp" local function make_auth(authz, authn, password) -- sasl plain (RFC4616) return base64.encode(authz..'\0'..authn..'\0'..password) end -function Xmpp.make(wilson, conf) - local self = { - name = conf.name, - wilson = wilson, - inbox = Queue.make(), - cq = wilson.cq, -- todo - nicks_inuse = {}, -- todo - } - local function conf_var(name) - assert(conf[name] ~= nil, 'missing conf field '..name) - self[name] = conf[name] - end - -- conf_var 'jid' - conf_var 'server' - -- conf_var 'resource' - conf_var 'component' - conf_var 'component_secret' - - setmetatable(self, {__index=Xmpp}) - return self +function Xmpp.init(self) + self:check_config "server component component_secret" + + self.cq = self.wilson.cq -- todo + self.nicks_inuse = {} -- todo end function Xmpp._connect_c2s(self) @@ -80,7 +62,7 @@ function Xmpp._connect_component(self) -- yes, our component name goes in the 'to' field. don't ask me why local start = ([[<stream:stream to='%s' xmlns='jabber:component:accept' xmlns:stream='http://etherx.jabber.org/streams'>]]):format(self.component) - print(start) + -- print(start) -- state of the art xml parser local function check_and_send(test, text) @@ -91,7 +73,7 @@ function Xmpp._connect_component(self) sock:write(start) local streamhead = sock:read('-2048') - print('streamhead', streamhead) + -- print('streamhead', streamhead) assert(streamhead:find'accept') local streamid = streamhead:match"id='(.-)'" sock:write(xmlify(X.handshake{sha1.sha1(streamid..self.component_secret)})) @@ -99,26 +81,21 @@ function Xmpp._connect_component(self) return sock end +Xmpp._connect = Xmpp._connect_component local THE_MUC = 'd@conference.ubq323.website' -function Xmpp.run(self) - self:_connect_component() - self.cq:wrap(self.recving, self) - self.cq:wrap(self.sending, self) -end - function Xmpp.recving(self) local function getmore() local function t(...) - pprint(...) + -- pprint(...) return ... end return t(self.sock:read'-2048') end for x in xml.stanzae(getmore) do - pprint(x) - print(xmlify(x)) + -- pprint(x) + -- print(xmlify(x)) local body = x'body' and x'body'[1] if x.label == 'message' then local fr = x.xarg.from @@ -152,7 +129,7 @@ function Xmpp.sending(self) end ensure_joined(THE_MUC, 'wilson', 'wilson') for dest_channel, message in self.inbox:iter() do - pprint(dest_channel, message) + -- pprint(dest_channel, message) ensure_joined(THE_MUC, message.sender, message.sender) local user = message.sender:gsub("[^a-zA-Z0-9%.]","."):match("^%.*(.-)%.*$") local jid = user..'@'..self.component diff --git a/xmpp/xml.lua b/xmpp/xml.lua index 68fa741..e54f27f 100644 --- a/xmpp/xml.lua +++ b/xmpp/xml.lua @@ -1,21 +1,20 @@ -- originally from http://lua-users.org/wiki/LuaXml -- modified by me a bit - -entity_escapes = { +local entity_escapes = { ["<"]="<", [">"]=">", ["&"]="&", ['"']=""", ["'"]="'" } -entity_unescapes = {} +local entity_unescapes = {} for k,v in pairs(entity_escapes) do entity_unescapes[v]=k end -function escape(s) +local function escape(s) return s:gsub("[<>&'\"]",entity_escapes) end -function unescape(s) +local function unescape(s) return s:gsub("&[a-z]+;",entity_unescapes) end |