summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Sluift/Examples/Wonderland.lua')
-rwxr-xr-xSluift/Examples/Wonderland.lua94
1 files changed, 73 insertions, 21 deletions
diff --git a/Sluift/Examples/Wonderland.lua b/Sluift/Examples/Wonderland.lua
index 235d8d0..426ec07 100755
--- a/Sluift/Examples/Wonderland.lua
+++ b/Sluift/Examples/Wonderland.lua
@@ -8,45 +8,97 @@
--
require "sluift"
---sluift.debug = true
+sluift.debug = true
characters = {
- {jid = "alice@wonderland.lit", name = "Alice", groups = {}, presence = "<presence/>"},
- {jid = "hatter@wonderland.lit", name = "Mad Hatter", groups = {}, presence = "<presence><show>away</show><status>At the Tea Party</status></presence>"},
- {jid ="queen@wonderland.lit", name = "Queen of Hearts", groups = {}, presence = "<presence><show>dnd</show><status>Executing</status></presence>"},
- {jid = "rabbit@wonderland.lit", name = "White Rabbit", groups = {"Animals"}, presence = "<presence><status>Oh dear!</status></presence>"},
- {jid = "turtle@wonderland.lit", name = "Mock Turtle", groups = {"Animals"}, presence = "<presence/>"},
+ ["Alice"] = {
+ jid = "alice@wonderland.lit", groups = {}, presence = "<presence/>"
+ },
+ ["Mad Hatter"] = {
+ jid = "hatter@wonderland.lit", groups = {},
+ presence = "<presence><show>away</show><status>At the Tea Party</status></presence>"
+ },
+ ["Queen of Hearts"] = {
+ jid ="queen@wonderland.lit", groups = {},
+ presence = "<presence><show>dnd</show><status>Executing</status></presence>"
+ },
+ ["White Rabbit"] = {
+ jid = "rabbit@wonderland.lit", groups = {"Animals"},
+ presence = "<presence><status>Oh dear!</status></presence>"},
+ ["Mock Turtle"] = {
+ jid = "turtle@wonderland.lit", groups = {"Animals"},
+ presence = "<presence/>"
+ },
}
-clients = {}
-for _, character in ipairs(characters) do
- print("Connecting " .. character["name"] .. "...")
- client = sluift.new_client(character["jid"], os.getenv("SLUIFT_PASS"))
+for name, character in pairs(characters) do
+ print("Connecting " .. name .. "...")
+ local client = sluift.new_client(character.jid, os.getenv("SLUIFT_PASS"))
client:set_options({compress = false, tls = false})
client:connect()
client:get_contacts()
- client:send(character["presence"])
- table.insert(clients, client)
- for _, contact in ipairs(characters) do
- if contact["jid"] ~= character["jid"] then
+ client:send(character.presence)
+ for contact_name, contact in pairs(characters) do
+ if contact.jid ~= character.jid then
client:add_contact(contact)
end
end
+ character.client = client
end
print("Confirming subscriptions")
-for _, client in ipairs(clients) do
- for _, contact in ipairs(characters) do
- client:confirm_subscription(contact["jid"])
+for _, character in pairs(characters) do
+ for _, contact in pairs(characters) do
+ character.client:confirm_subscription(contact.jid)
end
end
+print("Setting up PubSub nodes")
+local hatters_riddles = characters["Mad Hatter"].client:pubsub("pubsub.wonderland.lit"):node("hatters_riddles")
+hatters_riddles:delete()
+assert(hatters_riddles:create())
+
+local queen_quotes = characters["Queen of Hearts"].client:pubsub("pubsub.wonderland.lit"):node("queen_quotes")
+queen_quotes:delete()
+assert(queen_quotes:create())
+queen_quotes:publish{id = 'quote1', item = {_type = 'body', text = 'Off with his head!'}}
+queen_quotes:publish{id = 'quote2', item = {_type = 'body', text = 'Off with her head!'}}
+queen_quotes:publish{id = 'quote3', item = {_type = 'body', text = 'Off with their heads!'}}
+
+characters['Mad Hatter'].client:pubsub():node('http://jabber.org/protocol/geoloc'):publish{
+ item = {_type = 'user_location', latitude = 50.376739, longitude = -4.200709}}
+characters['Queen of Hearts'].client:pubsub():node('http://jabber.org/protocol/geoloc'):publish{
+ item = {_type = 'user_location', latitude = 50.551123, longitude = -4.141654}}
+characters['Mock Turtle'].client:pubsub():node('http://jabber.org/protocol/geoloc'):publish{
+ item = {_type = 'user_location', latitude = 50.366630, longitude = -4.134518}}
+characters['White Rabbit'].client:pubsub():node('http://jabber.org/protocol/geoloc'):publish{
+ item = {_type = 'user_location', latitude = 50.332907, longitude = -4.759194}}
+
+
+
+print("Disconnecting alice")
+characters['Alice'].client:disconnect()
+
print("Done. Waiting ...")
while true do
- for _, client in ipairs(clients) do
- for message in client:messages {timeout = 1000} do
- client:send_message{to = e["from"], body = "Off with their heads!"}
+ for name, character in pairs(characters) do
+ if name == 'Queen of Hearts' then
+ for message in character.client:messages{timeout = 1000} do
+ if message.body == 'publish' then
+ queen_quotes:publish{item = {_type = 'body', text = 'Off with her head!'}}
+ queen_quotes:publish{item = {_type = 'body', text = 'Off with his head!'}}
+ else
+ character.client:send_message{to = e["from"], body = "Off with their heads!"}
+ end
+ end
+ elseif name == "Mad Hatter" then
+ for message in character.client:messages{timeout = 1000} do
+ if message.body == 'publish' then
+ hatters_riddles:publish{item = {_type = 'body', text = 'Why is a raven like a writing desk?'}}
+ end
+ end
+ else
+ for message in character.client:messages{timeout = 100} do end
end
end
- sluift.sleep(1000)
end