| 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".init("client"); |
| 9 | |
| 10 | c = verse.new(verse.logger()); |
| 11 | c:add_plugin("version"); |
| 12 | c:add_plugin("disco"); |
| 13 | c:add_plugin("adhoc"); |
| 14 | |
| 15 | -- Add some hooks for debugging |
| 16 | c:hook("opened", function () print("Stream opened!") end); |
| 17 | c:hook("closed", function () print("Stream closed!") end); |
| 18 | c:hook("stanza", function (stanza) print("Stanza:", stanza) end); |
| 19 | |
| 20 | -- This one prints all received data |
| 21 | c:hook("incoming-raw", print, 1000); |
| 22 | |
| 23 | -- Print a message after authentication |
| 24 | c:hook("authentication-success", function () print("Logged in!"); end); |
| 25 | c:hook("authentication-failure", function (err) print("Failed to log in! Error: "..tostring(err.condition)); end); |
| 26 | |
| 27 | -- Print a message and exit when disconnected |
| 28 | c:hook("disconnected", function () print("Disconnected!"); os.exit(); end); |
| 29 | |
| 30 | -- Now, actually start the connection: |
| 31 | c:connect_client(jid, password); |
| 32 | |
| 33 | -- Catch the "ready" event to know when the stream is ready to use |
| 34 | c:hook("ready", function () |
| 35 | print("Stream ready!"); |
| 36 | c.version:set{ name = "verse example client" }; |
| 37 | |
| 38 | local function random_handler() |
| 39 | return { info = tostring(math.random(1,100)), status = "completed" }; |
| 40 | end |
| 41 | |
| 42 | c:add_adhoc_command("Get random number", "random", random_handler); |
| 43 | |
| 44 | c:send(verse.presence():add_child(c:caps())); |
| 45 | end); |
| 46 | |
| 47 | print("Starting loop...") |
| 48 | verse.loop() |