summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEdwin Mons <edwin.mons@isode.com>2014-05-23 09:01:23 (GMT)
committerSwift Review <review@swift.im>2014-06-22 12:35:26 (GMT)
commitbd7f30aec53fc776be678577dbe4f9afec5898a6 (patch)
tree66afad4382dc16f7405a856dd0b5abc38db51653 /Sluift/Examples
parent1eb14b6bde145ca54ac9b981df339fb8c56d3930 (diff)
downloadswift-bd7f30aec53fc776be678577dbe4f9afec5898a6.zip
swift-bd7f30aec53fc776be678577dbe4f9afec5898a6.tar.bz2
Sluift component support
Change-Id: Ib8af01c04c866e198c04d35236dea4da464c9116
Diffstat (limited to 'Sluift/Examples')
-rw-r--r--Sluift/Examples/Component.lua55
1 files changed, 55 insertions, 0 deletions
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()
+