Comparison

plugins/version.lua @ 27:a4a6a33a34c1

Add 'version' plugin to handle and generate version requests
author Matthew Wild <mwild1@gmail.com>
date Wed, 09 Dec 2009 13:55:58 +0000
child 29:0d275519eff4
comparison
equal deleted inserted replaced
26:6c5fab6c11cf 27:a4a6a33a34c1
1 local xmlns_version = "jabber:iq:version";
2
3 local function set_version(version_info)
4 self.name = version_info.name;
5 self.version = version_info.version;
6 self.platform = version_info.platform;
7 end
8
9 function verse.plugins.version(stream)
10 stream.version = { set = set_version };
11 stream:hook("iq/"..xmlns_version, function (event)
12 if event.stanza.attr.type ~= "get" then return; end
13 local reply = verse.reply(event.stanza)
14 :tag("query", { xmlns = xmlns_version });
15 if stream.version.name then
16 reply:tag("name"):text(tostring(stream.version.name)):up();
17 end
18 if stream.version.version then
19 reply:tag("version"):text(tostring(stream.version.version)):up()
20 end
21 if stream.version.platform then
22 reply:tag("os"):text(stream.version.platform);
23 end
24 end);
25
26 function stream:query_version(target_jid, callback)
27 callback = callback or function (version) return stream:event("version/response", version); end
28 stream:send_iq(verse.iq({ type = "get", to = target_jid })
29 :tag("query", { xmlns = xmlns_version }),
30 function (reply)
31 local query = reply:get_child("query", xmlns_version);
32 if query then
33 local name = query:get_child("name");
34 local version = query:get_child("version");
35 local os = query:get_child("os");
36 callback({
37 name = name and name:get_text() or nil;
38 version = version and version:get_text() or nil;
39 platform = os and os:get_text() or nil;
40 });
41 else
42 local type, condition, text = reply:get_error();
43 callback({
44 error = true;
45 condition = condition;
46 text = text;
47 type = type;
48 });
49 end
50 end);
51 end
52
53 end