Changeset

226:efd66bcc62c1

doc: Add Carbons example
author Kim Alvefur <zash@zash.se>
date Wed, 02 Nov 2011 01:18:53 +0100
parents 225:04bfa5c3a776
children 227:31019cb93d59
files doc/example_carbons.lua
diffstat 1 files changed, 45 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/doc/example_carbons.lua	Wed Nov 02 01:18:53 2011 +0100
@@ -0,0 +1,45 @@
+local xmlns_carbons = "urn:xmpp:carbons:1";
+local xmlns_forward = "urn:xmpp:forward:0";
+
+-- This line squishes verse each time you run,
+-- handy if you're hacking on Verse itself
+--os.execute("squish --minify-level=none verse");
+
+require "verse" -- Verse main library
+require "verse.client" -- XMPP client library
+
+c = verse.new();--verse.logger());
+c:add_plugin "carbons"
+
+c:hook("disconnected", verse.quit);
+local jid, password = unpack(arg);
+assert(jid and password, "You need to supply JID and password as arguments");
+c:connect_client(jid, password);
+
+-- Print a message after authentication
+c:hook("authentication-success", function () c:debug("Logged in!"); end);
+c:hook("authentication-failure", function (err)
+	c:error("Failed to log in! Error: "..tostring(err.condition));
+	c:close();
+end);
+
+c:hook("carbon", function(carbon)
+	local dir, ts, st = carbon.dir, carbon.timestamp, carbon.stanza;
+	print("", ts, dir:upper());
+	print(st);
+end);
+
+-- Catch the "ready" event to know when the stream is ready to use
+c:hook("ready", function ()
+	c:debug("Connected");
+	c.carbons:enable(function(ok)
+		if ok then
+			c:debug("Carbons enabled")
+		else
+			c:error("Could not enable carbons, aborting");
+			c:close();
+		end
+	end);
+end);
+
+verse.loop()