summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrebecca <ubq323@ubq323.website>2026-09-12 18:30:08 +0100
committerrebecca <ubq323@ubq323.website>2026-09-12 18:33:50 +0100
commit085040f49cc1386b294857aa68651329b0044420 (patch)
tree5d9a5e7d011e384ae1ca36a64be4f4eaca9f8ca0
parentfa9da79c39f62439ece735ed0e7dfe4c4c9692b2 (diff)
refactor classes and inheritance
-rw-r--r--discord/pylon.lua40
-rw-r--r--pylon.lua65
-rw-r--r--xmpp/pylon.lua82
3 files changed, 45 insertions, 142 deletions
diff --git a/discord/pylon.lua b/discord/pylon.lua
index d38b8a7..0b50b84 100644
--- a/discord/pylon.lua
+++ b/discord/pylon.lua
@@ -1,38 +1,32 @@
-local consts = require'discord.consts'
-local opcodes = consts.opcodes
+local cqueues = require 'cqueues'
local websocket = require'http.websocket'
+local request = require'http.request'
local json = require 'dkjson'
-local Queue = require 'queue'
-local exec_webhook = require'discord.the'
-local http_request = require'http.request'
-local cqueues = require 'cqueues'
-local pylon = require 'pylon'
local pprint = require 'pprint'
+local class = require 'r.class'
+
+local BasePylon = require 'pylon'
local Channel = require 'channel'
+local consts = require 'discord.consts'
+local opcodes = consts.opcodes
+local exec_webhook = require'discord.the'
-local Discord = pylon.subclass "discord"
local API_BASE = "https://discord.com/api/v10/"
+local Discord = class.extend(BasePylon)
function Discord.init(self)
self:check_config "token"
end
local function identify_payload(token)
- local I = consts.intents
- return json.encode{
- op = opcodes.identify,
- d = {
- properties = {
- os = "wilson", browser = "wilson", device = "wilson",
- },
+ local I = consts.intents return json.encode{
+ op = opcodes.identify, d = {
+ properties = {os="wilson",browser="wilson",device="wilson"},
intents = I.guilds + I.guild_messages + I.message_content,
- token = token,
- }
- }
-end
+ token = token } } end
function Discord._req_get(self, url)
- local req = http_request.new_from_uri(API_BASE..url)
+ local req = request.new_from_uri(API_BASE..url)
req.headers:upsert('content-type','application/json')
req.headers:upsert(':method','GET')
req.headers:upsert('authorization','Bot '..self.token)
@@ -40,7 +34,7 @@ function Discord._req_get(self, url)
local head,body = assert(req:go())
local status = head:get':status'
assert(status:sub(1,1)=='2','status was '..status)
- return body:get_body_as_string()
+ return assert(json.decode(body:get_body_as_string()))
end
function Discord._connect(self)
@@ -53,7 +47,7 @@ function Discord._connect(self)
pprint('I AM ',me)
end
-function Discord._heartbeat(self, interval_ms)
+function Discord._heartbeating(self, interval_ms)
local interval = interval_ms / 1000
cqueues.sleep(interval * math.random())
while true do
@@ -76,7 +70,7 @@ function Discord.recving(self)
if event.s then self.sequence_number = event.s end
if event.op == opcodes.hello then
- self.cq:wrap(self._heartbeat, self, event.d.heartbeat_interval)
+ self.cq:wrap(self._heartbeating, self, event.d.heartbeat_interval)
elseif event.op == opcodes.dispatch then
self:handle_dispatch(event)
end
diff --git a/pylon.lua b/pylon.lua
index 774395f..02c9b2d 100644
--- a/pylon.lua
+++ b/pylon.lua
@@ -1,55 +1,28 @@
-local cqueues = require 'cqueues'
-local Queue = require 'queue'
-
--- commonality between the different pylon classes
--- they can "inherit" from this
+local cqueues = require'cqueues'
-local BasePylon = {}
+local class = require 'r.class'
+local qw = require 'r.qw'
+local Queue = require 'queue'
-function BasePylon.check_config(self, vars)
- for x in vars:gmatch"%S+" do
- assert(self[x], "missing conf field "..x)
- end
-end
+local BasePylon = class()
+function BasePylon.make(cls, wilson, conf)
+ local self = setmetatable(conf, cls)
+ self.wilson = wilson
+ self.inbox = Queue()
+ self:init()
+ return self end
+function BasePylon._check_fields(self, fields) for k in qw.i(fields) do
+ assert(self[k]~=nil, self.name..": missing field "..k) end 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.name, ...) end end
function BasePylon.run(self)
self.cq = cqueues.new()
self:_connect()
self.cq:wrap(self.recving, self)
self.cq:wrap(self.sending, self)
- print(self.pylon_type, self.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.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
+ print(self.name, self.cq:loop()) end
+return BasePylon
-return {
- BasePylon = BasePylon,
- subclass = subclass,
-}
diff --git a/xmpp/pylon.lua b/xmpp/pylon.lua
index 2476fc9..b3b6bc7 100644
--- a/xmpp/pylon.lua
+++ b/xmpp/pylon.lua
@@ -1,60 +1,22 @@
local socket = require'cqueues.socket'
+local class = require'r.class'
+
+local Channel = require'channel'
+local BasePylon = require 'pylon'
local xml = require'xmpp.xml'
-local X = xml.X
-local xmlify = xml.xmlify
+local X,xmlify = xml.X, xml.xmlify
local base64 = require'xmpp.base64'
local sha1 = require'xmpp.sha1'
-local Channel = require'channel'
-local pylon = require 'pylon'
-local Xmpp = pylon.subclass "xmpp"
-
-local function make_auth(authz, authn, password)
- -- sasl plain (RFC4616)
- return base64.encode(authz..'\0'..authn..'\0'..password)
-end
+local Xmpp = class.extend(BasePylon)
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)
- local sock = assert(socket.connect(self.server, 5222))
- self.sock = sock
- sock:setmode('bn','bn')
-
- local start = ([[
-<?xml version='1.0'?><stream:stream from='%s' to='%s' version='1.0' xml:lang='en' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>]]):format(self.jid, self.server)
-
- -- state of the art xml parser
- local function check_and_send(test, text)
- local x = sock:read('-2048')
- assert(x:find(test))
- if text then sock:write(text) end
- end
- local function ietf_urn(v) return 'urn:ietf:params:xml:ns:xmpp-'..v end
-
- sock:write(start)
- check_and_send('starttls', xmlify(X.starttls{xmlns=ietf_urn"tls"}))
- check_and_send('proceed', nil)
- sock:starttls()
- sock:write(start)
- local auth = make_auth('', self.jid:match"(.*)@", self.password)
- check_and_send('PLAIN',
- xmlify(X.auth{xmlns=ietf_urn"sasl", mechanism='PLAIN', auth}))
- check_and_send('success',start)
- check_and_send('bind',
- xmlify(X.iq{type='set', id='aaaa',
- X.bind{xmlns=ietf_urn"bind", X.resource{self.resource}}}))
- check_and_send('jid',X.presence{X.show{'chat'}})
+ self:_check_fields"server component component_secret"
- return sock
+ self.nicks_inuse = {}
end
--- this sucks! no tls! no security! only use on local connections!
+-- only use on local connections!
function Xmpp._connect_component(self)
local sock = assert(socket.connect(self.server, 5347))
self.sock = sock
@@ -62,8 +24,6 @@ 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)
-
-- state of the art xml parser
local function check_and_send(test, text)
local x = sock:read('-2048')
@@ -73,7 +33,6 @@ function Xmpp._connect_component(self)
sock:write(start)
local streamhead = sock:read('-2048')
- -- print('streamhead', streamhead)
assert(streamhead:find'accept')
local streamid = streamhead:match"id='(.-)'"
sock:write(xmlify(X.handshake{sha1.sha1(streamid..self.component_secret)}))
@@ -81,7 +40,6 @@ function Xmpp._connect_component(self)
return sock
end
-Xmpp._connect = Xmpp._connect_component
local THE_MUC = 'd@conference.ubq323.website'
@@ -139,26 +97,4 @@ function Xmpp.sending(self)
end
end
-function Xmpp.post(self, dest_channel, message)
- self.inbox:enqueue(dest_channel, message)
-end
-
return Xmpp
-
--- local cq = cqueues.new()
--- local conf = {
--- jid='wilson@ubq323.website',
--- server='ubq323.website',
--- password='gregory<3',
--- resource='cheese',
--- }
--- local dummy_network = {
--- post = function(self, pylonname, channel, message)
--- pprint(pylonname, channel, message)
--- end
--- }
--- local pylon = Xmpp.makepylon('xmpptest',conf, cq, dummy_network)
--- pylon:run()
-
--- pprint('peas', cq:loop())
-