doc/example_pep.lua

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();
11
c:add_plugin("version");
12
c:add_plugin("pep");
13
 
14
-- Add some hooks for debugging
15
c:hook("opened", function () print("Stream opened!") end);
16
c:hook("closed", function () print("Stream closed!") end);
17
c:hook("stanza", function (stanza) print("Stanza:", stanza) end);
18
 
19
-- This one prints all received data
20
c:hook("incoming-raw", print, 1000);
21
 
22
-- Print a message after authentication
23
c:hook("authentication-success", function () print("Logged in!"); end);
24
c:hook("authentication-failure", function (err) print("Failed to log in! Error: "..tostring(err.condition)); end);
25
 
26
-- Print a message and exit when disconnected
27
c:hook("disconnected", function () print("Disconnected!"); os.exit(); end);
28
 
29
-- Now, actually start the connection:
30
c:connect_client(jid, password);
31
 
32
-- Catch the "ready" event to know when the stream is ready to use
33
c:hook("ready", function ()
34
	print("Stream ready!");
35
	c.version:set{ name = "verse example client" };
36
	c:hook_pep("http://jabber.org/protocol/mood", function (event)
37
		print(event.from.." is "..event.item.tags[1].name);
38
	end);
39
	
40
	c:hook_pep("http://jabber.org/protocol/tune", function (event)
41
		print(event.from.." is listening to "..event.item:get_child_text("title"));
42
	end);
43
 
44
	c:send(verse.presence());
45
 
46
	c:publish_pep(verse.stanza("tune", { xmlns = "http://jabber.org/protocol/tune" })
47
		:tag("title"):text("Beautiful Cedars"):up()
48
		:tag("artist"):text("The Spinners"):up()
49
		:tag("source"):text("Not Quite Folk"):up()
50
		:tag("track"):text("4"):up()
51
	);
52
	
53
end);
54
 
55
print("Starting loop...")
56
verse.loop()