Software /
code /
verse
Comparison
doc/example_adhoc.lua @ 117:f523516196ce
doc/example_adhoc.lua: Example of using ad-hoc commands
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 26 Aug 2010 17:53:05 +0100 |
child | 260:7f6df45a3d1f |
comparison
equal
deleted
inserted
replaced
116:151c8cc776df | 117:f523516196ce |
---|---|
1 -- Change these: | |
2 local jid, password = "user@example.com", "secret"; | |
3 | |
4 -- This line squishes verse each time you run, | |
5 -- handy if you're hacking on Verse itself | |
6 --os.execute("squish --minify-level=none"); | |
7 | |
8 require "verse" -- Verse main library | |
9 require "verse.client" -- XMPP client library | |
10 | |
11 c = verse.new(verse.logger()); | |
12 c:add_plugin("version"); | |
13 c:add_plugin("disco"); | |
14 c:add_plugin("adhoc"); | |
15 | |
16 -- Add some hooks for debugging | |
17 c:hook("opened", function () print("Stream opened!") end); | |
18 c:hook("closed", function () print("Stream closed!") end); | |
19 c:hook("stanza", function (stanza) print("Stanza:", stanza) end); | |
20 | |
21 -- This one prints all received data | |
22 c:hook("incoming-raw", print, 1000); | |
23 | |
24 -- Print a message after authentication | |
25 c:hook("authentication-success", function () print("Logged in!"); end); | |
26 c:hook("authentication-failure", function (err) print("Failed to log in! Error: "..tostring(err.condition)); end); | |
27 | |
28 -- Print a message and exit when disconnected | |
29 c:hook("disconnected", function () print("Disconnected!"); os.exit(); end); | |
30 | |
31 -- Now, actually start the connection: | |
32 c:connect_client(jid, password); | |
33 | |
34 -- Catch the "ready" event to know when the stream is ready to use | |
35 c:hook("ready", function () | |
36 print("Stream ready!"); | |
37 c.version:set{ name = "verse example client" }; | |
38 | |
39 local function random_handler() | |
40 return { info = tostring(math.random(1,100)), status = "completed" }; | |
41 end | |
42 | |
43 c:add_adhoc_command("Get random number", "random", random_handler); | |
44 | |
45 c:send(verse.presence():add_child(c:caps())); | |
46 end); | |
47 | |
48 print("Starting loop...") | |
49 verse.loop() |