doc/example.lua

Fri, 07 May 2010 09:57:48 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Fri, 07 May 2010 09:57:48 +0100
changeset 58
f9d025fde8dc
child 77
7af3e57501c2
permissions
-rw-r--r--

doc/example.lua: Example XMPP client script

58
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Change these:
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local jid, password = "user@example.com", "secret";
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 -- This line squishes verse each time you run,
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 -- handy if you're hacking on Verse itself
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 --os.execute("squish --minify-level=none verse");
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 require "verse" -- Verse main library
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 require "verse.client" -- XMPP client library
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 c = verse.new()
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 c:add_plugin("sasl");
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 c:add_plugin("version");
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 -- Add some hooks for debugging
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 c:hook("opened", function () print("Stream opened!") end);
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 c:hook("closed", function () print("Stream closed!") end);
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 c:hook("stanza", function (stanza) print("Stanza:", stanza) end);
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 -- This one prints all received data
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 c:hook("incoming-raw", print, 1000);
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 -- Print a message after authentication
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 c:hook("authentication-success", function () print("Logged in!"); end);
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 c:hook("authentication-failure", function (err) print("Failed to log in! Error: "..tostring(err.condition)); end);
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 -- Print a message and exit when disconnected
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 c:hook("disconnected", function () print("Disconnected!"); os.exit(); end);
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 -- Now, actually start the connection:
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 c:connect_client(jid, password);
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 -- Catch binding-success which is (currently) how you know when a stream is ready
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 c:hook("binding-success", function ()
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 print("Stream ready!");
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 c.version:set{ name = "verse example client" };
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 c:query_version(c.jid, function (v) print("I am using "..(v.name or "<unknown>")); end);
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 end);
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 print("Starting loop...")
f9d025fde8dc doc/example.lua: Example XMPP client script
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 verse.loop()

mercurial