summaryrefslogtreecommitdiffstats
blob: b70517272dc60e67564e7dfde7bed2a50abb1101 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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)