summaryrefslogtreecommitdiff
path: root/xmpp
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2025-02-27 23:23:57 +0000
committerubq323 <ubq323@ubq323.website>2025-02-27 23:23:57 +0000
commit5dea96ad915692e9abbc0620930756c51c256179 (patch)
tree881d7680a521474f262bac262f382e778c1fc191 /xmpp
parentec4053dea479c3c85d57c447e262a90addcce5c3 (diff)
restructure, refactor, a bit
Diffstat (limited to 'xmpp')
-rw-r--r--xmpp/pylon.lua53
-rw-r--r--xmpp/xml.lua9
2 files changed, 19 insertions, 43 deletions
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 = {
["<"]="&lt;",
[">"]="&gt;",
["&"]="&amp;",
['"']="&quot;",
["'"]="&apos;"
}
-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