Changeset

194:651c696e0b21

Added uptime plugin, included also an entry for it into the squishy file.
author Marco Cirillo <maranda@lightwitch.org>
date Mon, 14 Mar 2011 14:21:30 +0000
parents 193:fa6e1e65cb3c
children 195:dc61684e8dbf
files plugins/uptime.lua squishy
diffstat 2 files changed, 41 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/uptime.lua	Mon Mar 14 14:21:30 2011 +0000
@@ -0,0 +1,40 @@
+local xmlns_last = "jabber:iq:last";
+
+local function set_uptime(self, uptime_info)
+	self.starttime = uptime_info.starttime;
+end
+
+function verse.plugins.uptime(stream)
+	stream.uptime = { set = set_uptime };
+	stream:hook("iq/"..xmlns_last, function (stanza)
+		if stanza.attr.type ~= "get" then return; end
+		local reply = verse.reply(stanza)
+			:tag("query", { seconds = tostring(os.difftime(os.time(), stream.uptime.starttime)), xmlns = xmlns_last });
+		stream:send(reply);
+		return true;
+	end);
+
+	function stream:query_uptime(target_jid, callback)
+		callback = callback or function (uptime) return stream:event("uptime/response", uptime); end
+		stream:send_iq(verse.iq({ type = "get", to = target_jid })
+			:tag("query", { xmlns = xmlns_last }),
+			function (reply)
+				local query = reply:get_child("query", xmlns_last);
+				if reply.attr.type == "result" then
+					local seconds = query.attr.seconds;
+					callback({
+						seconds = seconds or nil;
+						});
+				else
+					local type, condition, text = reply:get_error();
+					callback({
+						error = true;
+						condition = condition;
+						text = text;
+						type = type;
+						});
+				end
+			end);
+	end
+	return true;
+end
--- a/squishy	Mon Mar 14 15:17:32 2011 +0000
+++ b/squishy	Mon Mar 14 14:21:30 2011 +0000
@@ -47,6 +47,7 @@
 Module "verse.plugins.adhoc"       "plugins/adhoc.lua"
 Module "verse.plugins.private"     "plugins/private.lua"
 Module "verse.plugins.groupchat"   "plugins/groupchat.lua"
+Module "verse.plugins.uptime"      "plugins/uptime.lua"
 
 Module "net.httpclient_listener" "net/httpclient_listener.lua"
 Module "net.connlisteners" "net/connlisteners.lua"