From 643e4202f4c5a5ee7a87779ffb69928e685db451 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Mon, 26 Aug 2024 16:11:02 +0100 Subject: ooo --- xml.lua | 24 +++++++++++++++++++---- xmpp.lua | 67 +++++++++++++++++++++++++--------------------------------------- 2 files changed, 46 insertions(+), 45 deletions(-) diff --git a/xml.lua b/xml.lua index 7a0758a..7153831 100644 --- a/xml.lua +++ b/xml.lua @@ -1,3 +1,6 @@ +-- originally from http://lua-users.org/wiki/LuaXml +-- modified by me a bit + local function parseargs(s) local arg = {} string.gsub(s, "([%-%w]+)=([\"'])(.-)%2", function (w, _, a) @@ -6,6 +9,18 @@ local function parseargs(s) return arg end +local function tag(x) + return setmetatable(x, {__call=function(x, label) + -- search for child with given label + for _,c in ipairs(x) do + if c.label == label then + return c + end + end + return nil + end}) +end + local psingle local function pmulti(s, i, parent) @@ -41,11 +56,11 @@ psingle = function(s, i) end if empty == "/" then - return nexti, {label=label, xarg=parseargs(xarg), empty=true} + return nexti, tag{label=label, xarg=parseargs(xarg), empty=true} elseif c == "" then -- start tag - return multi(s, nexti, {label=label, xarg=parseargs(xarg)}) + return pmulti(s, nexti, tag{label=label, xarg=parseargs(xarg)}) else -- end tag - return nexti, {label=label, close=true} + return nexti, tag{label=label, close=true} end end @@ -63,7 +78,8 @@ local function stanzae(getmore) coroutine.yield(el) buf = buf:sub(ni) else - buf = buf..getmore() + local more = getmore() + buf = buf .. more end end end) diff --git a/xmpp.lua b/xmpp.lua index a2ea6cb..aebbee9 100644 --- a/xmpp.lua +++ b/xmpp.lua @@ -8,17 +8,17 @@ local cqueues = require'cqueues' local socket = require'cqueues.socket' -local function connect(...) - local sock = assert(socket.connect(... or server, 5222)) +local function connect() + local sock = assert(socket.connect(server, 5222)) sock:setmode('bn','bn') local start = ([[ ]]):format(jid, server) + -- state of the art xml parser local function check_and_send(test, text) local x = sock:read('-2048') print(x) - assert(x:find(test)) if text then sock:write(text) end end @@ -38,50 +38,35 @@ local function connect(...) return sock end --- sock:write[[ --- --- --- --- --- --- --- --- --- --- --- ]] - -local sock = connect(...) local mucs = { - 'ja@conference.ubq323.website' + 'ja@conference.ubq323.website', + 'a@conference.ubq323.website', } -for _, muc in ipairs(mucs) do - sock:write(([[ - - - ]]):format(muc .. '/wilson')) -end +local xml = require'xml' +local pprint=require'pprint' +local function run_xmpp() + local sock = connect() --- sock:write[[ --- --- --- --- --- --- ]] + for _, muc in ipairs(mucs) do + sock:write(([[ + + + + + ]]):format(muc .. '/wilson')) + end -local xml=require'xml' -for x in xml.stanzae(function() return sock:read('-2048') end) do - print(x) + for x in xml.stanzae(function() return sock:read('-2048') end) do + -- pprint(x) + local body = x'body' and x'body'[1] + if x.label == 'message' then pprint('M', x.xarg.from, body) end + end end +local cq = cqueues.new() +cq:wrap(run_xmpp) +pprint(cq:loop()) + -- cgit v1.2.3