# HG changeset patch # User Matthew Wild # Date 1292425436 0 # Node ID cb03e8ae2e30ebeac48073ead98c5dbef949e1f6 # Parent 4bb1e9c91fbe97a1d5a8abb42b9bce62dd3e0e56# Parent 7285e04a47975ab4dc480fe5ef44c17a4c019541 Merge with Zash diff -r 4bb1e9c91fbe -r cb03e8ae2e30 bosh.lua --- a/bosh.lua Wed Dec 15 15:03:46 2010 +0000 +++ b/bosh.lua Wed Dec 15 15:03:56 2010 +0000 @@ -1,5 +1,5 @@ -local init_xmlhandlers = require "core.xmlhandlers"; +local new_xmpp_stream = require "util.xmppstream".new; local st = require "util.stanza"; require "net.httpclient_listener"; -- Required for net.http to work local http = require "net.http"; @@ -186,8 +186,8 @@ return; end local session = { notopen = true, log = self.log }; - local parser = lxp.new(init_xmlhandlers(session, stream_callbacks), "\1"); - parser:parse(response); + local stream = new_xmpp_stream(session, stream_callbacks); + stream:feed(response); return session.payload; end diff -r 4bb1e9c91fbe -r cb03e8ae2e30 client.lua --- a/client.lua Wed Dec 15 15:03:46 2010 +0000 +++ b/client.lua Wed Dec 15 15:03:56 2010 +0000 @@ -10,7 +10,7 @@ verse.message, verse.presence, verse.iq, verse.stanza, verse.reply, verse.error_reply = st.message, st.presence, st.iq, st.stanza, st.reply, st.error_reply; -local init_xmlhandlers = require "core.xmlhandlers"; +local new_xmpp_stream = require "util.xmppstream".new; local xmlns_stream = "http://etherx.jabber.org/streams"; @@ -46,12 +46,12 @@ end function stream:reset() - -- Reset stream - local parser = lxp.new(init_xmlhandlers(self, stream_callbacks), "\1"); - self.parser = parser; - + if self.stream then + self.stream:reset(); + else + self.stream = new_xmpp_stream(self, stream_callbacks); + end self.notopen = true; - return true; end @@ -66,7 +66,7 @@ self:add_plugin("session"); function self.data(conn, data) - local ok, err = self.parser:parse(data); + local ok, err = self.stream:feed(data); if ok then return; end stream:debug("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " ")); stream:close("xml-not-well-formed"); @@ -107,6 +107,18 @@ return ret; end, -1); + self:hook("outgoing", function (data) + if data.name then + self:event("stanza-out", data); + end + end); + + self:hook("stanza-out", function (stanza) + if not stanza.attr.xmlns then + self:event(stanza.name.."-out", stanza); + end + end); + local function stream_ready() self:event("ready"); end diff -r 4bb1e9c91fbe -r cb03e8ae2e30 component.lua --- a/component.lua Wed Dec 15 15:03:46 2010 +0000 +++ b/component.lua Wed Dec 15 15:03:56 2010 +0000 @@ -10,7 +10,7 @@ verse.message, verse.presence, verse.iq, verse.stanza, verse.reply, verse.error_reply = st.message, st.presence, st.iq, st.stanza, st.reply, st.error_reply; -local init_xmlhandlers = require "core.xmlhandlers"; +local new_xmpp_stream = require "util.xmppstream".new; local xmlns_stream = "http://etherx.jabber.org/streams"; local xmlns_component = "jabber:component:accept"; @@ -43,12 +43,12 @@ end function stream:reset() - -- Reset stream - local parser = lxp.new(init_xmlhandlers(self, stream_callbacks), "\1"); - self.parser = parser; - + if self.stream then + self.stream:reset(); + else + self.stream = new_xmpp_stream(self, stream_callbacks); + end self.notopen = true; - return true; end @@ -57,7 +57,7 @@ self.username, self.host, self.resource = jid_split(jid); function self.data(conn, data) - local ok, err = self.parser:parse(data); + local ok, err = self.stream:feed(data); if ok then return; end stream:debug("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " ")); stream:close("xml-not-well-formed"); diff -r 4bb1e9c91fbe -r cb03e8ae2e30 doc/example_pep.lua --- a/doc/example_pep.lua Wed Dec 15 15:03:46 2010 +0000 +++ b/doc/example_pep.lua Wed Dec 15 15:03:56 2010 +0000 @@ -34,6 +34,7 @@ -- Catch the "ready" event to know when the stream is ready to use c:hook("ready", function () print("Stream ready!"); + c:send(verse.presence()); c.version:set{ name = "verse example client" }; c:publish_pep(verse.stanza("tune", { xmlns = "http://jabber.org/protocol/tune" }) :tag("title"):text("Beautiful Cedars"):up() @@ -45,7 +46,10 @@ c:hook_pep("http://jabber.org/protocol/mood", function (event) print(event.from.." is "..event.item.tags[1].name); end); - c:send(verse.presence():add_child(c:caps())); + + c:hook_pep("http://jabber.org/protocol/tune", function (event) + print(event.from.." is listening to "..event.item:get_child("title"):get_text()); + end); end); print("Starting loop...") diff -r 4bb1e9c91fbe -r cb03e8ae2e30 init.lua --- a/init.lua Wed Dec 15 15:03:46 2010 +0000 +++ b/init.lua Wed Dec 15 15:03:56 2010 +0000 @@ -89,8 +89,12 @@ end self.conn = conn; - local w, t = conn.write, tostring; - self.send = function (_, data) return w(conn, t(data)); end + self.send = function (stream, data) + self:event("outgoing", data); + data = tostring(data); + self:event("outgoing-raw", data); + return conn:write(data); + end; return true; end @@ -171,7 +175,7 @@ function stream:add_plugin(name) if require("verse.plugins."..name) then local ok, err = verse.plugins[name](self); - if ok then + if ok ~= false then self:debug("Loaded %s plugin", name); else self:warn("Failed to load %s plugin: %s", name, err); @@ -186,7 +190,6 @@ function conn_listener.onconnect(conn) stream.connected = true; - stream.send = function (stream, data) stream:debug("Sending data: "..tostring(data)); return conn:write(tostring(data)); end; stream:event("connected"); end diff -r 4bb1e9c91fbe -r cb03e8ae2e30 plugins/bind.lua --- a/plugins/bind.lua Wed Dec 15 15:03:46 2010 +0000 +++ b/plugins/bind.lua Wed Dec 15 15:03:56 2010 +0000 @@ -14,7 +14,7 @@ :get_text(); stream.username, stream.host, stream.resource = jid.split(result_jid); stream.jid, stream.bound = result_jid, true; - stream:event("bind-success", full_jid); + stream:event("bind-success", { jid = result_jid }); elseif reply.attr.type == "error" then local err = reply:child_with_name("error"); local type, condition, text = reply:get_error(); diff -r 4bb1e9c91fbe -r cb03e8ae2e30 plugins/disco.lua --- a/plugins/disco.lua Wed Dec 15 15:03:46 2010 +0000 +++ b/plugins/disco.lua Wed Dec 15 15:03:56 2010 +0000 @@ -30,6 +30,8 @@ stream.caps = {} stream.caps.node = 'http://code.matthewwild.co.uk/verse/' + local _resend_presence; -- Forward declaration of a function + local function cmp_identity(item1, item2) if item1.category < item2.category then return true; @@ -87,13 +89,15 @@ }) function stream:add_disco_feature(feature) - table.insert(self.disco.info.features, {var=feature}); + table.insert(self.disco.info.features, {var=feature}); + _resend_presence(); end function stream:remove_disco_feature(feature) for idx, disco_feature in ipairs(self.disco.info.features) do if disco_feature.var == feature then table.remove(self.disco.info.features, idx); + _resend_presence(); return true; end end @@ -370,6 +374,30 @@ end); return true; end, 5); + + local last_presence; -- Cache to re-send with updated caps + + stream:hook("presence-out", function (presence) + if not presence:get_child("c", xmlns_caps) then + presence:reset():add_child(stream:caps()):reset(); + end + if not presence.attr.to then + last_presence = presence; -- Cache non-directed presence + end + end); + + local function update_caps(tag) + if tag.name == "c" and tag.attr.xmlns == xmlns_caps then + return stream:caps(); + end + end + + function _resend_presence() -- Local via forward declaration + if last_presence then + last_presence = last_presence:maptags(update_caps); + stream:send(last_presence); + end + end end -- end of disco.lua diff -r 4bb1e9c91fbe -r cb03e8ae2e30 plugins/pep.lua --- a/plugins/pep.lua Wed Dec 15 15:03:46 2010 +0000 +++ b/plugins/pep.lua Wed Dec 15 15:03:56 2010 +0000 @@ -22,8 +22,11 @@ end); function stream:hook_pep(node, callback, priority) + local handlers = stream.events._handlers["pep/"..node]; + if not(handlers) or #handlers == 0 then + stream:add_disco_feature(node.."+notify"); + end stream:hook("pep/"..node, callback, priority); - stream:add_disco_feature(node.."+notify"); end function stream:unhook_pep(node, callback) diff -r 4bb1e9c91fbe -r cb03e8ae2e30 plugins/pubsub.lua --- a/plugins/pubsub.lua Wed Dec 15 15:03:46 2010 +0000 +++ b/plugins/pubsub.lua Wed Dec 15 15:03:56 2010 +0000 @@ -9,6 +9,22 @@ function verse.plugins.pubsub(stream) stream.pubsub = setmetatable({ stream = stream }, pubsub_mt); + stream:hook("message", function (message) + for pubsub_event in message:matching_tags("event", xmlns_pubsub_event) do + local items = pubsub_event:get_child("items"); + if items then + local node = items.attr.node; + for item in items:matching_tags("item") do + stream:event("pubsub/event", { + from = message.attr.from; + node = node; + item = item; + }); + end + end + end + end); + return true; end function pubsub:subscribe(server, node, jid, callback) diff -r 4bb1e9c91fbe -r cb03e8ae2e30 squishy --- a/squishy Wed Dec 15 15:03:46 2010 +0000 +++ b/squishy Wed Dec 15 15:03:56 2010 +0000 @@ -8,7 +8,11 @@ Module "lib.adhoc" "libs/adhoc.lib.lua" -- Prosody libraries -AutoFetchURL "http://prosody.im/tip/?" +if not GetOption("prosody") then + AutoFetchURL "http://prosody.im/tip/?" +else + AutoFetchURL(GetOption("prosody").."/?") +end Module "util.stanza" "util/stanza.lua" Module "util.timer" "util/timer.lua" @@ -17,11 +21,10 @@ Module "net.dns" "net/dns.lua" Module "net.adns" "net/adns.lua" Module "net.server" "net/server_select.lua" -Module "core.xmlhandlers" "core/xmlhandlers.lua" +Module "util.xmppstream" "util/xmppstream.lua" Module "util.jid" "util/jid.lua" Module "util.events" "util/events.lua" Module "util.dataforms" "util/dataforms.lua" -Module "util.ztact" "util/ztact.lua" -- Verse plugins Module "verse.plugins.tls" "plugins/tls.lua"