# HG changeset patch # User Matthew Wild # Date 1260366958 0 # Node ID a4a6a33a34c109451ca7713bcd0c8e3ab18746e8 # Parent 6c5fab6c11cfd1abc058e09fdb648b0175ab3d6a Add 'version' plugin to handle and generate version requests diff -r 6c5fab6c11cf -r a4a6a33a34c1 plugins/version.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/version.lua Wed Dec 09 13:55:58 2009 +0000 @@ -0,0 +1,53 @@ +local xmlns_version = "jabber:iq:version"; + +local function set_version(version_info) + self.name = version_info.name; + self.version = version_info.version; + self.platform = version_info.platform; +end + +function verse.plugins.version(stream) + stream.version = { set = set_version }; + stream:hook("iq/"..xmlns_version, function (event) + if event.stanza.attr.type ~= "get" then return; end + local reply = verse.reply(event.stanza) + :tag("query", { xmlns = xmlns_version }); + if stream.version.name then + reply:tag("name"):text(tostring(stream.version.name)):up(); + end + if stream.version.version then + reply:tag("version"):text(tostring(stream.version.version)):up() + end + if stream.version.platform then + reply:tag("os"):text(stream.version.platform); + end + end); + + function stream:query_version(target_jid, callback) + callback = callback or function (version) return stream:event("version/response", version); end + stream:send_iq(verse.iq({ type = "get", to = target_jid }) + :tag("query", { xmlns = xmlns_version }), + function (reply) + local query = reply:get_child("query", xmlns_version); + if query then + local name = query:get_child("name"); + local version = query:get_child("version"); + local os = query:get_child("os"); + callback({ + name = name and name:get_text() or nil; + version = version and version:get_text() or nil; + platform = os and os:get_text() or nil; + }); + else + local type, condition, text = reply:get_error(); + callback({ + error = true; + condition = condition; + text = text; + type = type; + }); + end + end); + end + +end