Software /
code /
verse
Changeset
181:c61ba3d1b39a
Merge with Zash
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 31 Dec 2010 03:40:34 +0000 |
parents | 180:58537eb98506 (diff) 174:1c8d48120e21 (current diff) |
children | 182:51c0baa2bd19 |
files | plugins/legacy.lua |
diffstat | 13 files changed, 149 insertions(+), 53 deletions(-) [+] |
line wrap: on
line diff
--- a/bosh.lua Mon Nov 29 16:12:55 2010 +0100 +++ b/bosh.lua Fri Dec 31 03:40:34 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
--- a/client.lua Mon Nov 29 16:12:55 2010 +0100 +++ b/client.lua Fri Dec 31 03:40:34 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,10 +66,10 @@ 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"); + self:debug("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " ")); + self:close("xml-not-well-formed"); end self:hook("incoming-raw", function (data) return self.data(self.conn, data); end); @@ -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
--- a/component.lua Mon Nov 29 16:12:55 2010 +0100 +++ b/component.lua Fri Dec 31 03:40:34 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");
--- a/doc/example_pep.lua Mon Nov 29 16:12:55 2010 +0100 +++ b/doc/example_pep.lua Fri Dec 31 03:40:34 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...")
--- a/init.lua Mon Nov 29 16:12:55 2010 +0100 +++ b/init.lua Fri Dec 31 03:40:34 2010 +0000 @@ -22,6 +22,7 @@ t.id = tostring(t):match("%x*$"); t:set_logger(logger, true); t.events = events.new(); + t.plugins = {}; return t; end @@ -89,8 +90,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 @@ -169,10 +174,12 @@ end function stream:add_plugin(name) + if self.plugins[name] then return true; end 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); + self.plugins[name] = true; else self:warn("Failed to load %s plugin: %s", name, err); end @@ -186,7 +193,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
--- a/plugins/bind.lua Mon Nov 29 16:12:55 2010 +0100 +++ b/plugins/bind.lua Fri Dec 31 03:40:34 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();
--- a/plugins/compression.lua Mon Nov 29 16:12:55 2010 +0100 +++ b/plugins/compression.lua Fri Dec 31 03:40:34 2010 +0000 @@ -47,7 +47,7 @@ session:close({ condition = "undefined-condition"; text = compressed; - extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed"); + extra = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("processing-failed"); }); session:warn("Compressed send failed: %s", tostring(compressed)); return; @@ -66,7 +66,7 @@ session:close({ condition = "undefined-condition"; text = decompressed; - extra = st.stanza("failure", {xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed"); + extra = st.stanza("failure", {xmlns=xmlns_compression_protocol}):tag("processing-failed"); }); stream:warn("%s", tostring(decompressed)); return;
--- a/plugins/disco.lua Mon Nov 29 16:12:55 2010 +0100 +++ b/plugins/disco.lua Fri Dec 31 03:40:34 2010 +0000 @@ -10,19 +10,21 @@ local b64 = require("mime").b64 local sha1 = require("util.sha1").sha1 +local xmlns_caps = "http://jabber.org/protocol/caps"; local xmlns_disco = "http://jabber.org/protocol/disco"; local xmlns_disco_info = xmlns_disco.."#info"; local xmlns_disco_items = xmlns_disco.."#items"; function verse.plugins.disco(stream) + stream:add_plugin("presence"); stream.disco = { cache = {}, info = {} } stream.disco.info.identities = { {category = 'client', type='pc', name='Verse'}, } stream.disco.info.features = { - {var = 'http://jabber.org/protocol/caps'}, - {var = 'http://jabber.org/protocol/disco#info'}, - {var = 'http://jabber.org/protocol/disco#items'}, + {var = xmlns_caps}, + {var = xmlns_disco_info}, + {var = xmlns_disco_items}, } stream.disco.items = {} stream.disco.nodes = {} @@ -78,7 +80,7 @@ -- presence stanza local hash = calculate_hash() return st.stanza('c', { - xmlns = 'http://jabber.org/protocol/caps', + xmlns = xmlns_caps, hash = 'sha-1', node = stream.caps.node, ver = hash @@ -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}); + stream: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); + stream:resend_presence(); return true; end end @@ -156,6 +160,9 @@ function stream:disco_local_services(callback) self:disco_items(self.host, nil, function (items) + if not items then + return callback({}); + end local n_items = 0; local function item_callback() n_items = n_items - 1; @@ -247,7 +254,7 @@ end); end - stream:hook("iq/http://jabber.org/protocol/disco#info", function (stanza) + stream:hook("iq/"..xmlns_disco_info, function (stanza) if stanza.attr.type == 'get' then local query = stanza:child_with_name('query') if not query then return; end @@ -272,7 +279,7 @@ id = stanza.attr.id, type = 'error' }) - response:tag('query',{xmlns = 'http://jabber.org/protocol/disco#info'}):reset() + response:tag('query',{xmlns = xmlns_disco_info}):reset() response:tag('error',{type = 'cancel'}):tag( 'item-not-found',{xmlns = 'urn:ietf:params:xml:ns:xmpp-stanzas'} ) @@ -285,7 +292,7 @@ end -- construct the response local result = st.stanza('query',{ - xmlns = 'http://jabber.org/protocol/disco#info', + xmlns = xmlns_disco_info, node = query.attr.node }) for key,identity in pairs(identities) do @@ -304,7 +311,7 @@ end end); - stream:hook("iq/http://jabber.org/protocol/disco#items", function (stanza) + stream:hook("iq/"..xmlns_disco_items, function (stanza) if stanza.attr.type == 'get' then local query = stanza:child_with_name('query') if not query then return; end @@ -322,7 +329,7 @@ id = stanza.attr.id, type = 'error' }) - response:tag('query',{xmlns = 'http://jabber.org/protocol/disco#items'}):reset() + response:tag('query',{xmlns = xmlns_disco_items}):reset() response:tag('error',{type = 'cancel'}):tag( 'item-not-found',{xmlns = 'urn:ietf:params:xml:ns:xmpp-stanzas'} ) @@ -334,7 +341,7 @@ end -- construct the response local result = st.stanza('query',{ - xmlns = 'http://jabber.org/protocol/disco#items', + xmlns = xmlns_disco_items, node = query.attr.node }) for key,item in pairs(items) do @@ -370,6 +377,12 @@ end); return true; end, 5); + + stream:hook("presence-out", function (presence) + if not presence:get_child("c", xmlns_caps) then + presence:reset():add_child(stream:caps()):reset(); + end + end, 10); end -- end of disco.lua
--- a/plugins/legacy.lua Mon Nov 29 16:12:55 2010 +0100 +++ b/plugins/legacy.lua Fri Dec 31 03:40:34 2010 +0000 @@ -7,9 +7,9 @@ local query = result:get_child("query", xmlns_auth); if result.attr.type ~= "result" or not query then local type, cond, text = result:get_error(); - stream:debug("warn", "%s %s: %s", type, cond, text); - --stream:event("authentication-failure", { condition = cond }); - -- COMPAT continue anyways + stream:debug("warn", "%s %s: %s", type, cond, text); + --stream:event("authentication-failure", { condition = cond }); + -- COMPAT continue anyways end local auth_data = { username = stream.username; @@ -19,7 +19,7 @@ }; local request = verse.iq({ to = stream.host, type = "set" }) :tag("query", { xmlns = xmlns_auth }); - if #query > 0 then + if #query > 0 then for tag in query:childtags() do local field = tag.name; local value = auth_data[field]; @@ -31,13 +31,13 @@ return false; end end - else -- COMPAT for servers not following XEP 78 - for field, value in pairs(auth_data) do - if value then - request:tag(field):text(value):up(); - end - end - end + else -- COMPAT for servers not following XEP 78 + for field, value in pairs(auth_data) do + if value then + request:tag(field):text(value):up(); + end + end + end stream:send_iq(request, function (response) if response.attr.type == "result" then stream.resource = auth_data.resource;
--- a/plugins/pep.lua Mon Nov 29 16:12:55 2010 +0100 +++ b/plugins/pep.lua Fri Dec 31 03:40:34 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)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/presence.lua Fri Dec 31 03:40:34 2010 +0000 @@ -0,0 +1,38 @@ +local st = require "util.stanza" +function verse.plugins.presence(stream) + stream.last_presence = nil; + + stream:hook("presence-out", function (presence) + if not presence.attr.to then + stream.last_presence = presence; -- Cache non-directed presence + end + end, 1); + + function stream:resend_presence() + if last_presence then + stream:send(last_presence); + end + end + + -- Becase I didn't find util.stanza in the client code. + -- And, then the name fits better :) + -- // Zash + function stream:set_status(opts) + local p = st.presence(); + if type(opts) == "table" then + if opts.show then + p:tag("show"):text(opts.show):up(); + end + if opts.prio then + p:tag("priority"):text(opts.priority):up(); + end + if opts.msg then + p:tag("status"):text(opts.msg):up(); + end + end + -- TODO maybe use opts as prio if it's a int, + -- or as show or status if it's a string? + + stream:send(p); + end +end
--- a/plugins/pubsub.lua Mon Nov 29 16:12:55 2010 +0100 +++ b/plugins/pubsub.lua Fri Dec 31 03:40:34 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)
--- a/squishy Mon Nov 29 16:12:55 2010 +0100 +++ b/squishy Fri Dec 31 03:40:34 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" @@ -38,6 +41,7 @@ Module "verse.plugins.jingle" "plugins/jingle.lua" Module "verse.plugins.jingle_ft" "plugins/jingle_ft.lua" Module "verse.plugins.jingle_s5b" "plugins/jingle_s5b.lua" +Module "verse.plugins.presence" "plugins/presence.lua" Module "verse.plugins.disco" "plugins/disco.lua" Module "verse.plugins.pep" "plugins/pep.lua" Module "verse.plugins.adhoc" "plugins/adhoc.lua"