Changeset

13:c3d83b98fb4f

verse.client: Add stream:send_iq() and stream:new_id() for sending iqs with response handlers, and for generating stream-unique ids respectively
author Matthew Wild <mwild1@gmail.com>
date Sat, 28 Nov 2009 22:30:25 +0000
parents 12:73f466054ead
children 14:53246b2b933b
files client.lua
diffstat 1 files changed, 23 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/client.lua	Sat Nov 28 22:29:29 2009 +0000
+++ b/client.lua	Sat Nov 28 22:30:25 2009 +0000
@@ -54,6 +54,18 @@
 	
 	self:hook("incoming-raw", function (data) return self.data(self.conn, data); end);
 	
+	self.curr_id = 0;
+	
+	self.tracked_iqs = {};
+	self:hook("stanza", function (stanza)
+		local id, type = stanza.attr.id, stanza.attr.type;
+		if id and stanza.name == "iq" and (type == "result" or type == "error") and self.tracked_iqs[id] then
+			self.tracked_iqs[id](stanza);
+			self.tracked_iqs[id] = nil;
+			return true;
+		end
+	end);
+	
 	-- Initialise connection
 	self:connect(self.connect_host or self.host, self.connect_port or 5222);
 	--reset_stream(self);	
@@ -72,3 +84,14 @@
 	self.conn:close();
 end
 
+function stream:send_iq(iq, callback)
+	local id = self:new_id();
+	self.tracked_iqs[id] = callback;
+	iq.attr.id = id;
+	self:send(iq);
+end
+
+function stream:new_id()
+	self.curr_id = self.curr_id + 1;
+	return tostring(self.curr_id);
+end