From cc1d97fc393c4d6fd3c9ecacd35b3683a10de356 Mon Sep 17 00:00:00 2001 From: Edwin Mons Date: Wed, 21 Feb 2018 10:06:42 +0100 Subject: Add a Sluift example of sending a custom reference payload The example will send a message with a data reference as an example of creating and sending custom children of a message. Test-Information: Reference received on macOS 10.12 Change-Id: If670db0c2ded426e8502a4d331026cf56f6df8df diff --git a/Sluift/Examples/SendReference.lua b/Sluift/Examples/SendReference.lua new file mode 100644 index 0000000..b705172 --- /dev/null +++ b/Sluift/Examples/SendReference.lua @@ -0,0 +1,70 @@ +#!/usr/bin/env sluift -- + +-- This is an example of sending a custom payload, a XEP-0372 reference in this +-- case. +-- +-- Running this requires a sluift binary somewhere in the PATH. Alternatively, +-- you can feed this to sluift, or to a Lua that has sluift.so in its library +-- path. + +require("sluift") + +DEFAULT_URI = "http://example.com/kitten.jpg" + +function usage(err) + print("Usage: SendReference.lua jid [tojid] [datauri]") + print() + print("This tool will log in as jid, and send a reference to tojid. If no tojid") + print("is provided, a message to itself is sent. The datauri defaults to") + print(DEFAULT_URI .. ", but can be overridden.") + + if err then + print("") + print("Error: " .. err) + end + os.exit(1) +end + +function get_password(prompt) + if prompt then + io.write(prompt) + end + local stty_ret = os.execute("stty -echo 2>/dev/null") + if stty_ret then + io.write("\027[8m") + end + local ok, pass = pcall(io.read, "*l") + if stty_ret then + io.write("\027[m") + else + os.execute("stty sane") + end + io.write("\n"); + if ok then + return pass + end +end + +if #arg < 1 then + usage("no jid specified") +end +jid = arg[1] +password = os.getenv("XMPP_PASSWORD") or get_password("Password: ") +tojid = #arg > 1 and arg[2] or jid +datauri = #arg > 2 and arg[3] or DEFAULT_URI + +sluift.debug = 1 +sluift.with(sluift.new_client(jid, password), function() + connect(options) + reference = { + ['_type'] = 'dom', + ['tag'] = 'reference', + ['ns'] = 'urn:xmpp:reference:0', + ['attributes'] = { + { ['name'] = 'type', ['value'] = 'data' }, + { ['name'] = 'uri', ['value'] = datauri }, + }, + } + send_message{to=tojid, body="Check out this data!", payloads={reference}} + disconnect() +end) -- cgit v0.10.2-6-g49f6