diff options
Diffstat (limited to 'Sluift/Examples')
-rw-r--r-- | Sluift/Examples/AdHocCommands.lua | 48 | ||||
-rw-r--r-- | Sluift/Examples/CollectVersions.lua | 32 | ||||
-rw-r--r-- | Sluift/Examples/Component.lua | 55 | ||||
-rw-r--r-- | Sluift/Examples/ContactsMap.lua | 120 | ||||
-rw-r--r-- | Sluift/Examples/EchoBot.lua | 42 | ||||
-rw-r--r-- | Sluift/Examples/EchoBot_With.lua | 32 | ||||
-rw-r--r-- | Sluift/Examples/Login.lua | 34 | ||||
-rw-r--r-- | Sluift/Examples/MAMRSM.lua | 23 | ||||
-rw-r--r-- | Sluift/Examples/MAMRSMPage.lua | 36 | ||||
-rw-r--r-- | Sluift/Examples/MAMSimple.lua | 19 | ||||
-rw-r--r-- | Sluift/Examples/MAMSupportedFields.lua | 18 | ||||
-rw-r--r-- | Sluift/Examples/MAMUser.lua | 25 | ||||
-rw-r--r-- | Sluift/Examples/PEPListener.lua | 46 | ||||
-rw-r--r-- | Sluift/Examples/RemoveUnreachableContacts.lua | 55 | ||||
-rw-r--r-- | Sluift/Examples/Tunes.lua | 68 | ||||
-rwxr-xr-x | Sluift/Examples/Wonderland.lua | 96 |
16 files changed, 651 insertions, 98 deletions
diff --git a/Sluift/Examples/AdHocCommands.lua b/Sluift/Examples/AdHocCommands.lua new file mode 100644 index 0000000..9e83f0c --- /dev/null +++ b/Sluift/Examples/AdHocCommands.lua @@ -0,0 +1,48 @@ +--[[ + Copyright (c) 2013 Remko Tronçon + Licensed under the GNU General Public License v3. + See Documentation/Licenses/GPLv3.txt for more information. +--]] + +--[[ + + Ad-hoc command example. + + This script logs into an XMPP server, and executes a + multi-step ad-hoc command. + + The following environment variables are used: + * SLUIFT_JID, SWIFT_PASS: JID and password to log in with + * SLUIFT_DEBUG: Sets whether debugging should be turned on + +--]] + +require "sluift" + +sluift.debug = os.getenv("SLUIFT_DEBUG") or false + +c = sluift.new_client(os.getenv("SLUIFT_JID"), os.getenv("SLUIFT_PASS")) +c:connect(function () + to = sluift.jid.domain(os.getenv("SLUIFT_JID")) + + -- Get the list of commands + commands = assert(c:get_disco_items{ to = to, disco_items = { + node = sluift.disco.features.COMMANDS }}) + print(commands) + + -- Get the initial form + result = assert(c:set_command{ to = to, command = { + action = 'execute', node = 'http://jabber.org/protocol/admin#get-user-roster'}}) + + -- Fill in the form + submission = result.form:create_submission() + submission.accountjid = os.getenv("SLUIFT_JID") + + -- Submit the form + result = assert(c:set_command{ to = to, command = { + action = 'complete', node = 'http://jabber.org/protocol/admin#get-user-roster', + session_id = result.session_id, form = submission}}) + for _, v in ipairs(result.form.roster.values) do + print(v) + end +end) diff --git a/Sluift/Examples/CollectVersions.lua b/Sluift/Examples/CollectVersions.lua index c93c8c8..38bf6ac 100644 --- a/Sluift/Examples/CollectVersions.lua +++ b/Sluift/Examples/CollectVersions.lua @@ -1,22 +1,22 @@ --- --- Copyright (c) 2010 Remko Tronçon --- Licensed under the GNU General Public License v3. --- See Documentation/Licenses/GPLv3.txt for more information. --- +--[[ --- This script logs into an XMPP server, and collects statistics about --- the server software of all contacts in your roster + Copyright (c) 2010-2013 Remko Tronçon + Licensed under the GNU General Public License v3. + See Documentation/Licenses/GPLv3.txt for more information. -require "sluift" + This script logs into an XMPP server, and collects statistics about + the server software of all contacts in your roster -c = sluift.new_client(os.getenv("SLUIFT_JID"), os.getenv("SLUIFT_PASS")) -c:connect() +--]] +require 'sluift' + +c = sluift.new_client(os.getenv('SLUIFT_JID'), os.getenv('SLUIFT_PASS')) +c:connect(function () versions = {} -for jid, _ in pairs(c:get_contacts()) do - v = c:get_version(sluift.jid_domain(jid)) - if v then versions[v["name"]] = (versions[v["name"]] or 0) + 1 end + for jid in pairs(c:get_contacts()) do + local v = c:get_software_version {to = sluift.jid.domain(jid), timeout = 3000} or {name = 'Unknown'} + versions[v['name']] = (versions[v['name']] or 0) + 1 end -for name, count in pairs(versions) do print(name .. ": " .. count) end - -c:disconnect() + for name, count in pairs(versions) do print(name .. ': ' .. count) end +end) diff --git a/Sluift/Examples/Component.lua b/Sluift/Examples/Component.lua new file mode 100644 index 0000000..b5d6539 --- /dev/null +++ b/Sluift/Examples/Component.lua @@ -0,0 +1,55 @@ +--[[ + Copyright (c) 2014 Edwin Mons and Remko Tronçon + Licensed under the GNU General Public License v3. + See Documentation/Licenses/GPLv3.txt for more information. +--]] + +--[[ + + Component example. + + This script connects to an XMPP server as a component, and listens to + messages received. + + The following environment variables are used: + * SLUIFT_COMP_DOMAIN: Component domain name + * SLUIFT_COMP_SECRET: Component secret + * SLUIFT_COMP_HOST: XMPP server host name + * SLUIFT_COMP_PORT: XMPP server component port + * SLUIFT_JID: Recipient of presence and initial message + * SLUIFT_DEBUG: Sets whether debugging should be turned on + +--]] + +require "sluift" + +sluift.debug = os.getenv("SLUIFT_DEBUG") or false + +config = { + domain = os.getenv('SLUIFT_COMP_DOMAIN'), + secret = os.getenv('SLUIFT_COMP_SECRET'), + host = os.getenv('SLUIFT_COMP_HOST'), + port = os.getenv('SLUIFT_COMP_PORT'), + jid = os.getenv('SLUIFT_JID') +} + +-- Create the component, and connect +comp = sluift.new_component(config.domain, config.secret); +comp:connect(config) + +-- Send initial presence and message +-- Assumes the remote client already has this component user on his roster +comp:send_presence{from='root@' .. config.domain, to=config.jid} +comp:send_message{from='root@' .. config.domain, to=config.jid, body='Component active'} + +-- Listen for messages, and reply if one is received +for message in comp:messages() do + print("Received a message from " .. message.from) + comp:send_message{to=message.from, from=message.to, body='I received: ' .. message['body']} + + -- Send out a ping to demonstrate we can do more than just send messages + comp:get{to=message.from, query='<ping xmlns="urn:xmpp:ping"/>'} +end + +comp:disconnect() + diff --git a/Sluift/Examples/ContactsMap.lua b/Sluift/Examples/ContactsMap.lua new file mode 100644 index 0000000..d248dc7 --- /dev/null +++ b/Sluift/Examples/ContactsMap.lua @@ -0,0 +1,120 @@ +--[[ + Copyright (c) 2013 Remko Tronçon + Licensed under the GNU General Public License v3. + See Documentation/Licenses/GPLv3.txt for more information. +--]] + +--[[ + + Contacts map + + Creates an HTML file of a map with all your contacts on it. + + The following environment variables are used: + - SLUIFT_JID, SWIFT_PASS: JID and password to log in with + +--]] + +require "sluift" + +output_dir = arg[1] +if not output_dir then + error("Please specify the directory to write the map to") +end + +-- Collect all data +geolocs = {} +avatars = {} +c = sluift.new_client(os.getenv("SLUIFT_JID"), os.getenv("SLUIFT_PASS")) +c:connect(function () + -- Indicate we're interested in getting user location information, and send initial presence + c:set_caps_node("http://swift.im/ContactsMap") + c:set_disco_info({identities = {{name = 'ContactsMap'}}, features = { + sluift.disco.features.DISCO_INFO, + sluift.disco.features.USER_LOCATION .. '+notify', + }}) + c:send_presence() + + -- Collect geoloc info + for event in c:pubsub_events {timeout = 10000} do + local from = sluift.jid.to_bare(event.from) + if event._type == 'pubsub_event_items' and event.item then + if event.node == sluift.disco.features.USER_LOCATION then + local lat, lon = event.item.latitude, event.item.longitude + if lat and lon then geolocs[from] = {lat = lat, lon = lon} end + end + end + end + + -- Download the necessary avatars + for contact in pairs(geolocs) do + local vcard = c:get_vcard {to = contact} + if vcard and vcard.photo then + local avatar_hash = sluift.hexify(sluift.sha1(vcard.photo)) + local file = io.open(output_dir.."/" .. avatar_hash .. ".png", "wb") + file:write(vcard.photo) + file:close() + avatars[contact] = avatar_hash + end + end +end) + +-- Generate html +min_lat, max_lat = 90, -90 +min_lon, max_lon = 180, -180 +contacts_html = {} +for contact, geoloc in pairs(geolocs) do + if geoloc.lat < min_lat then min_lat = geoloc.lat end + if geoloc.lon < min_lon then min_lon = geoloc.lon end + if geoloc.lat > max_lat then max_lat = geoloc.lat end + if geoloc.lon > max_lon then max_lon = geoloc.lon end + local image = 'null' + if avatars[contact] then + image = "'" .. avatars[contact] .. ".png'" + end + contacts_html[#contacts_html+1] = "['" .. contact .. "'," .. geoloc.lat .. "," .. geoloc.lon .. "," .. image .. "]" +end +center_html = ((min_lat + max_lat) / 2) .. ',' .. ((min_lon + max_lon) / 2) + +map_html = [[ +<html> + <head> + <title>Contacts Map</title> + <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script> + </head> + <body> + <div id="map" style="height: 100%; width: 100%;"/> + <script> + var map = new google.maps.Map(document.getElementById('map'), { + zoom: 2, + center: new google.maps.LatLng(%(CENTER)), + mapTypeId: google.maps.MapTypeId.ROADMAP + }); + var infowindow = new google.maps.InfoWindow(); + var contacts = [%(CONTACTS)]; + for (var i = 0; i < contacts.length; i++) { + var icon = null; + if (contacts[i][3]) { + icon = { url: contacts[i][3], scaledSize: { width: 30, height: 30} }; + } + var marker = new google.maps.Marker({ + position: new google.maps.LatLng(contacts[i][1], contacts[i][2]), + map: map, + icon: icon, + }); + google.maps.event.addListener(marker, 'click', (function(marker, i) { + return function() { + infowindow.setContent(contacts[i][0]); + infowindow.open(map, marker); + } + })(marker, i)); + } + </script> + </body> +</html> +]] +local file = io.open(output_dir .. "/index.html", "w") +file:write(map_html: + gsub('%%%(CONTACTS%)', table.concat(contacts_html, ",")): + gsub('%%%(CENTER%)', center_html)) +file:close() diff --git a/Sluift/Examples/EchoBot.lua b/Sluift/Examples/EchoBot.lua index 09da63b..fc495c4 100644 --- a/Sluift/Examples/EchoBot.lua +++ b/Sluift/Examples/EchoBot.lua @@ -1,18 +1,20 @@ --- --- Copyright (c) 2010 Remko Tronçon --- Licensed under the GNU General Public License v3. --- See Documentation/Licenses/GPLv3.txt for more information. --- - --- --- An XMPP Echoing Bot --- --- This script logs into an XMPP server, sends initial presence, --- and then waits for incoming messages, and echoes them back. --- --- The following environment variables are used: --- * SLUIFT_JID, SWIFT_PASS: JID and password to log in with --- * SLUIFT_DEBUG: Sets whether debugging should be turned on --- +--[[ + Copyright (c) 2010-2013 Remko Tronçon + Licensed under the GNU General Public License v3. + See Documentation/Licenses/GPLv3.txt for more information. +--]] + +--[[ + + An XMPP Echoing Bot + + This script logs into an XMPP server, sends initial presence, + and then waits for incoming messages, and echoes them back. + + The following environment variables are used: + * SLUIFT_JID, SWIFT_PASS: JID and password to log in with + * SLUIFT_DEBUG: Sets whether debugging should be turned on + +--]] require "sluift" @@ -21,7 +23,9 @@ sluift.debug = os.getenv("SLUIFT_DEBUG") or false c = sluift.new_client(os.getenv("SLUIFT_JID"), os.getenv("SLUIFT_PASS")) -c:connect() +c:connect(function () + c:set_version{name = "EchoBot", version = "0.1"} c:send_presence("Send me a message") -c:for_event(function(e) - if e["type"] == "message" then c:send_message(e["from"], e["body"]) end + for message in c:messages() do + c:send_message{to = message["from"], body = message["body"]} + end end) diff --git a/Sluift/Examples/EchoBot_With.lua b/Sluift/Examples/EchoBot_With.lua new file mode 100644 index 0000000..1f7d0bb --- /dev/null +++ b/Sluift/Examples/EchoBot_With.lua @@ -0,0 +1,32 @@ +--[[ + Copyright (c) 2010-2013 Remko Tronçon + Licensed under the GNU General Public License v3. + See Documentation/Licenses/GPLv3.txt for more information. +--]] + +--[[ + + Alternative version of EchoBot that uses with() + + This script logs into an XMPP server, sends initial presence, + and then waits for incoming messages, and echoes them back. + + The following environment variables are used: + * SLUIFT_JID, SWIFT_PASS: JID and password to log in with + * SLUIFT_DEBUG: Sets whether debugging should be turned on + +--]] + +require "sluift" + +sluift.debug = os.getenv("SLUIFT_DEBUG") or false + +client = sluift.new_client(os.getenv("SLUIFT_JID"), os.getenv("SLUIFT_PASS")) +sluift.with(client, function () + connect() + set_version{name = "EchoBot", version = "0.1"} + send_presence("Send me a message") + for message in messages() do + send_message{to = message["from"], body = message["body"]} + end +end) diff --git a/Sluift/Examples/Login.lua b/Sluift/Examples/Login.lua index 1733bb9..fadb651 100644 --- a/Sluift/Examples/Login.lua +++ b/Sluift/Examples/Login.lua @@ -1,15 +1,19 @@ --- --- Copyright (c) 2010 Remko Tronçon --- Licensed under the GNU General Public License v3. --- See Documentation/Licenses/GPLv3.txt for more information. --- +--[[ + Copyright (c) 2010-2014 Remko Tronçon + Licensed under the GNU General Public License v3. + See Documentation/Licenses/GPLv3.txt for more information. +--]] --- This script logs into an XMPP server, and sends initial presence --- Useful as initialization script for an interactive session ('-i'), --- or as a starting point for scripts. --- --- The following environment variables are used: --- * SLUIFT_JID, SWIFT_PASS: JID and password to log in with --- * SLUIFT_DEBUG: Sets whether debugging should be turned on +--[[ + + This script logs into an XMPP server, and sends initial presence + Useful as initialization script for an interactive session ('-i'), + or as a starting point for scripts. + + The following environment variables are used: + * SLUIFT_JID, SWIFT_PASS: JID and password to log in with + * SLUIFT_DEBUG: Sets whether debugging should be turned on + +--]] require "sluift" @@ -18,6 +22,8 @@ sluift.debug = os.getenv("SLUIFT_DEBUG") or false print("Connecting " .. os.getenv("SLUIFT_JID") .. " ...") c = sluift.new_client(os.getenv("SLUIFT_JID"), os.getenv("SLUIFT_PASS")) -c:set_options({compress = false, tls = false}) -c:connect():send_presence("") +c:set_options{compress = false, tls = false} +c:connect() +c:send_presence("") print("Connected ...") +print("Use the 'c' variable to communicate.") diff --git a/Sluift/Examples/MAMRSM.lua b/Sluift/Examples/MAMRSM.lua new file mode 100644 index 0000000..c8a1e85 --- /dev/null +++ b/Sluift/Examples/MAMRSM.lua @@ -0,0 +1,23 @@ +-- A query using Result Set Management +-- Usage: ./sluift MAMRSM.lua <jid> <password> <query_dest> <max_results> + +sluift.debug = true + +c = sluift.new_client(arg[1], arg[2]) + +c:connect(); + +query = { + result_set={max_items=arg[4]} +} + +c:set_mam{mam=query, to=arg[3]} + +c:for_each_message(function(e) + if e.payloads[1].tag == 'fin' then return true end + if e.payloads[1]._type == 'mam_result' then + print(e.payloads[1].payload.stanza.payloads[1].text) + end +end) + +c:disconnect() diff --git a/Sluift/Examples/MAMRSMPage.lua b/Sluift/Examples/MAMRSMPage.lua new file mode 100644 index 0000000..cb3307c --- /dev/null +++ b/Sluift/Examples/MAMRSMPage.lua @@ -0,0 +1,36 @@ +-- A page query using Result Set Management +-- Usage: ./sluift MAMRSMPage.lua <jid> <password> <query_dest> <pages> + +sluift.debug = true + +c = sluift.new_client(arg[1], arg[2]) + +c:set_options{compress = false, tls = false} + +c:connect(); + +query = { + result_set={max_items=5} +} + +done = false +page = 0 +while not done and page < tonumber(arg[4]) do + page = page + 1 + c:set_mam{mam=query, to=arg[3]} + c:for_each_message(function(e) + if e.payloads[1].tag == 'fin' then + if e.payloads[2].last_id then + query.result_set.after = e.payloads[2].last_id + else + done = true + end + return true + end + if e.payloads[1]._type == 'mam_result' then + print(e.payloads[1].payload.stanza.payloads[1].text) + end + end) +end + +c:disconnect() diff --git a/Sluift/Examples/MAMSimple.lua b/Sluift/Examples/MAMSimple.lua new file mode 100644 index 0000000..13ab1a0 --- /dev/null +++ b/Sluift/Examples/MAMSimple.lua @@ -0,0 +1,19 @@ +-- Querying the archive for messages +-- Usage: ./sluift MAMSimple.lua <jid> <password> <query_dest> + +sluift.debug = true + +c = sluift.new_client(arg[1], arg[2]) + +c:connect(); + +c:set_mam{mam={}, to=arg[3]} + +c:for_each_message(function(e) + if e.payloads[1].tag == 'fin' then return true end + if e.payloads[1]._type == 'mam_result' then + print(e.payloads[1].payload.stanza.payloads[1].text) + end +end) + +c:disconnect() diff --git a/Sluift/Examples/MAMSupportedFields.lua b/Sluift/Examples/MAMSupportedFields.lua new file mode 100644 index 0000000..0417924 --- /dev/null +++ b/Sluift/Examples/MAMSupportedFields.lua @@ -0,0 +1,18 @@ +-- Retrieving form fields +-- Usage: ./sluift MAMSupportedFields.lua <jid> <password> + +sluift.debug = true + +c = sluift.new_client(arg[1], arg[2]) + +c:connect(); + +mam_result = c:get_mam{} + +for i=1,#mam_result.form.fields do + if mam_result.form.fields[i].type ~= "hidden" then + print("Server supports: " .. mam_result.form.fields[i].name) + end +end + +c:disconnect() diff --git a/Sluift/Examples/MAMUser.lua b/Sluift/Examples/MAMUser.lua new file mode 100644 index 0000000..e4a7c28 --- /dev/null +++ b/Sluift/Examples/MAMUser.lua @@ -0,0 +1,25 @@ +-- Querying for all messages to/from a particular JID +-- Usage: ./sluift MAMUser.lua <jid> <password> <query_dest> <query_jid> + +sluift.debug = true + +c = sluift.new_client(arg[1], arg[2]) + +c:connect(); + +fields = { + with = arg[4] +} + +query_form = sluift.create_form{fields, form_type="urn:xmpp:mam:0"} + +c:set_mam{mam={form=query_form}, to=arg[3]} + +c:for_each_message(function(e) + if e.payloads[1].tag == 'fin' then return true end + if e.payloads[1]._type == 'mam_result' then + print(e.payloads[1].payload.stanza.payloads[1].text) + end +end) + +c:disconnect() diff --git a/Sluift/Examples/PEPListener.lua b/Sluift/Examples/PEPListener.lua new file mode 100644 index 0000000..f196f84 --- /dev/null +++ b/Sluift/Examples/PEPListener.lua @@ -0,0 +1,46 @@ +--[[ + Copyright (c) 2010-2013 Remko Tronçon + Licensed under the GNU General Public License v3. + See Documentation/Licenses/GPLv3.txt for more information. +--]] + +--[[ + + PEP Listener + + Listens to a series of PEP events of all contacts. + + The following environment variables are used: + - SLUIFT_JID, SWIFT_PASS: JID and password to log in with + - SLUIFT_DEBUG: Sets whether debugging should be turned on + +--]] + +require "sluift" + +sluift.debug = os.getenv("SLUIFT_DEBUG") or false + +pep_protocols = { + [sluift.disco.features.USER_LOCATION] = true, + [sluift.disco.features.USER_TUNE] = true, + [sluift.disco.features.USER_ACTIVITY] = true, + [sluift.disco.features.USER_AVATAR_METADATA] = true, + [sluift.disco.features.USER_PROFILE] = true, +} + +client = sluift.new_client(os.getenv("SLUIFT_JID"), os.getenv("SLUIFT_PASS")) +client:connect(function (c) + features = {sluift.disco.features.DISCO_INFO} + for protocol in pairs(pep_protocols) do + features[#features+1] = protocol .. '+notify' + end + + c:set_caps_node("http://swift.im/PEPListener") + c:set_disco_info({identities = {{name = 'PEPListener'}}, features = features}) + c:send_presence() + for event in c:pubsub_events() do + if event._type == 'pubsub_event_items' and pep_protocols[event.node] then + print("<" .. event.from .. "> " .. tostring(event.item)) + end + end +end) diff --git a/Sluift/Examples/RemoveUnreachableContacts.lua b/Sluift/Examples/RemoveUnreachableContacts.lua index 90122df..a202e62 100644 --- a/Sluift/Examples/RemoveUnreachableContacts.lua +++ b/Sluift/Examples/RemoveUnreachableContacts.lua @@ -1,37 +1,38 @@ --- --- Copyright (c) 2010 Remko Tronçon --- Licensed under the GNU General Public License v3. --- See Documentation/Licenses/GPLv3.txt for more information. --- +--[[ + Copyright (c) 2010-2013 Remko Tronçon + Licensed under the GNU General Public License v3. + See Documentation/Licenses/GPLv3.txt for more information. +--]] --- This script logs into an XMPP server, iterates over all roster items, --- and checks if their server is still alive. If not, the script asks you --- whether it should remove the contact from your contact list. --- --- The following environment variables are used: --- * SLUIFT_JID, SWIFT_PASS: JID and password to log in with --- * SLUIFT_DEBUG: Sets whether debugging should be turned on +--[[ + This script logs into an XMPP server, iterates over all roster items, + and checks if their server is still alive. If not, the script asks you + whether it should remove the contact from your contact list. -require "sluift" -sluift.debug = os.getenv("SLUIFT_DEBUG") + The following environment variables are used: + * SLUIFT_JID, SWIFT_PASS: JID and password to log in with + * SLUIFT_DEBUG: Sets whether debugging should be turned on +--]] -print "Connecting ..." -c = sluift.new_client(os.getenv("SLUIFT_JID"), os.getenv("SLUIFT_PASS")) -c:connect() +require 'sluift' +sluift.debug = os.getenv('SLUIFT_DEBUG') -print "Checking for unreachable contacts ..." -for jid, _ in pairs(c:get_contacts()) do - _, err = c:get_version(sluift.jid_domain(jid), 10000) - if err == "Remote server not found" or err == "Timeout" then - print("Delete " .. jid .. " (" .. err .. ") ? [y/n/q]") +print 'Connecting ...' +c = sluift.new_client(os.getenv('SLUIFT_JID'), os.getenv('SLUIFT_PASS')) +c:connect(function (c) + print 'Checking for unreachable contacts ...' + for jid in pairs(c:get_contacts()) do + _, err = c:get_software_version {to = sluift.jid.domain(jid), timeout = 10000} + print(err) + if err == 'Remote server not found' or err == 'Remote server timeout' then + print('Delete ' .. jid .. ' (' .. err .. ') ? [y/n/q]') answer = io.read() - if answer == "y" then + if answer == 'y' then c:remove_contact(jid) - elseif answer == "q" then + elseif answer == 'q' then break end end end - -print "Done. Exiting ..." -c:disconnect() + print 'Done. Exiting ...' +end) diff --git a/Sluift/Examples/Tunes.lua b/Sluift/Examples/Tunes.lua new file mode 100644 index 0000000..37ad996 --- /dev/null +++ b/Sluift/Examples/Tunes.lua @@ -0,0 +1,68 @@ +--[[ + Copyright (c) 2014 Remko Tronçon + Licensed under the GNU General Public License v3. + See Documentation/Licenses/GPLv3.txt for more information. +--]] + +--[[ + + Tune Publisher/Listener + + Publishes the currently playing tune in iTunes, and prints + the playing tunes of contacts + + The following environment variables are used: + - SLUIFT_JID, SWIFT_PASS: JID and password to log in with + - SLUIFT_DEBUG: Sets whether debugging should be turned on + +--]] + +require 'sluift' + +sluift.debug = os.getenv('SLUIFT_DEBUG') or false + +client = sluift.new_client(os.getenv('SLUIFT_JID'), os.getenv('SLUIFT_PASS')) +client:connect(function (c) + -- Send initial presence (with service discovery information) + c:set_caps_node('http://swift.im/Tunes') + c:set_disco_info{ + identities = {{name = 'Tunes'}}, + features = { + sluift.disco.features.DISCO_INFO, + sluift.disco.features.USER_TUNE .. '+notify' + }} + c:send_presence{priority = -1} + + local pubsub = c:pubsub(sluift.jid.to_bare(c:jid())) + local tunes_node = pubsub:node(sluift.disco.features.USER_TUNE) + local current_track = nil + while true do + -- Publish currently playing tune + if sluift.itunes then + local track = sluift.itunes.get_current_track() + if track ~= current_track then + tunes_node:publish{item = { + _type = 'user_tune', + title = track.name, + artist = track.artist, + track = track.track_number, + source = track.album, + length = track.length, + rating = track.rating and track.rating / 10 or nil + }} + current_track = track + end + end + + -- Print incoming events for a while + for event in c:pubsub_events{timeout = 1000} do + if event._type == 'pubsub_event_items' + and event.node == sluift.disco.features.USER_TUNE + and event.item.title then + print(event.from .. ' is playing "' + .. (event.item.artist or '<Unknown Artist>') .. ' - ' + .. event.item.title .. '"') + end + end + end +end) diff --git a/Sluift/Examples/Wonderland.lua b/Sluift/Examples/Wonderland.lua index 58c00ca..426ec07 100755 --- a/Sluift/Examples/Wonderland.lua +++ b/Sluift/Examples/Wonderland.lua @@ -9,44 +9,96 @@ 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 - client:for_event(function(e) - if e["type"] == "message" then client:send_message(e["from"], "Off with their heads!") end - end, 1000) + 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 |