plugins/disco.lua

Sun, 05 Dec 2021 18:35:39 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Sun, 05 Dec 2021 18:35:39 +0000
changeset 165
ec0567256b11
parent 16
ae69cea97598
permissions
-rw-r--r--

Add rtbl_admin plugin

6
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
1 -- disco.lua
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
2
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
3 -- Responds to service discovery queries (XEP-0030), and calculates the entity
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
4 -- capabilities hash (XEP-0115).
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
5
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
6 -- Fill the bot.disco.info.identities, bot.disco.info.features, and
16
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
7 -- bot.disco.items tables with the relevant disco data. It comes pre-populated
6
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
8 -- to advertise support for disco#info, disco#items, and entity capabilities,
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
9 -- and to identify itself as Riddim.
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
10
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
11 -- If you want to advertise a node, add entries to the bot.disco.nodes table
16
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
12 -- with the relevant data. The bot.disco.nodes table should have the same
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
13 -- format as bot.disco (without the nodes element). The nodes are NOT
6
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
14 -- automatically added to the base disco items, so you will need to add them
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
15 -- yourself.
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
16
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
17 -- To property implement Entity Capabilities, you should make sure that you
16
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
18 -- send a "c" element within presence stanzas that are sent. The correct "c"
6
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
19 -- element can be obtained by calling bot.caps() (or bot:caps()).
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
20
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
21 -- Hubert Chathi <hubert@uhoreg.ca>
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
22
16
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
23 -- This file is hereby placed in the public domain. Feel free to modify and
6
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
24 -- redistribute it at will
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
25
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
26 local st = require "util.stanza"
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
27 local b64 = require("mime").b64
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
28 local sha1 = require("util.hashes").sha1
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
29
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
30 function riddim.plugins.disco(bot)
16
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
31 bot.disco = {}
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
32 bot.disco.info = {}
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
33 bot.disco.info.identities = {
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
34 {category = 'client', type='bot', name='Riddim'},
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
35 }
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
36 bot.disco.info.features = {
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
37 {var = 'http://jabber.org/protocol/caps'},
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
38 {var = 'http://jabber.org/protocol/disco#info'},
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
39 {var = 'http://jabber.org/protocol/disco#items'},
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
40 }
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
41 bot.disco.items = {}
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
42 bot.disco.nodes = {}
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
43
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
44 bot.caps = {}
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
45 bot.caps.node = 'http://code.matthewwild.co.uk/riddim/'
6
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
46
16
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
47 local function cmp_identity(item1, item2)
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
48 if item1.category < item2.category then
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
49 return true;
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
50 elseif item2.category < item1.category then
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
51 return false;
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
52 end
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
53 if item1.type < item2.type then
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
54 return true;
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
55 elseif item2.type < item1.type then
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
56 return false;
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
57 end
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
58 if (not item1['xml:lang'] and item2['xml:lang']) or
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
59 (item2['xml:lang'] and item1['xml:lang'] < item2['xml:lang']) then
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
60 return true
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
61 end
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
62 return false
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
63 end
6
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
64
16
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
65 local function cmp_feature(item1, item2)
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
66 return item1.var < item2.var
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
67 end
6
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
68
16
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
69 local function calculate_hash()
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
70 table.sort(bot.disco.info.identities, cmp_identity)
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
71 table.sort(bot.disco.info.features, cmp_feature)
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
72 local S = ''
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
73 for key,identity in pairs(bot.disco.info.identities) do
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
74 S = S .. string.format(
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
75 '%s/%s/%s/%s', identity.category, identity.type,
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
76 identity['xml:lang'] or '', identity.name or ''
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
77 ) .. '<'
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
78 end
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
79 for key,feature in pairs(bot.disco.info.features) do
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
80 S = S .. feature.var .. '<'
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
81 end
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
82 -- FIXME: make sure S is utf8-encoded
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
83 return (b64(sha1(S)))
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
84 end
6
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
85
16
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
86 setmetatable(bot.caps, {
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
87 __call = function (...) -- vararg: allow calling as function or member
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
88 -- retrieve the c stanza to insert into the
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
89 -- presence stanza
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
90 local hash = calculate_hash()
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
91 return st.stanza('c', {
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
92 xmlns = 'http://jabber.org/protocol/caps',
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
93 hash = 'sha-1',
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
94 node = bot.caps.node,
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
95 ver = hash
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
96 })
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
97 end
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
98 })
6
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
99
16
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
100 bot:hook("iq/http://jabber.org/protocol/disco#info", function (event)
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
101 local stanza = event.stanza
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
102 if stanza.attr.type == 'get' then
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
103 local query = stanza:child_with_name('query')
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
104 if not query then return; end
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
105 -- figure out what identities/features to send
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
106 local identities
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
107 local features
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
108 if query.attr.node then
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
109 local hash = calculate_hash()
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
110 local node = bot.disco.nodes[query.attr.node]
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
111 if node and node.info then
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
112 identities = node.info.identities or {}
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
113 features = node.info.identities or {}
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
114 elseif query.attr.node == bot.caps.node..'#'..hash then
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
115 -- matches caps hash, so use the main info
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
116 identities = bot.disco.info.identities
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
117 features = bot.disco.info.features
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
118 else
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
119 -- unknown node: give an error
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
120 local response = st.stanza('iq',{
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
121 to = stanza.attr.from,
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
122 from = stanza.attr.to,
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
123 id = stanza.attr.id,
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
124 type = 'error'
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
125 })
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
126 response:tag('query',{xmlns = 'http://jabber.org/protocol/disco#info'}):reset()
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
127 response:tag('error',{type = 'cancel'}):tag(
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
128 'item-not-found',{xmlns = 'urn:ietf:params:xml:ns:xmpp-stanzas'}
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
129 )
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
130 bot:send(response)
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
131 return true
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
132 end
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
133 else
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
134 identities = bot.disco.info.identities
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
135 features = bot.disco.info.features
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
136 end
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
137 -- construct the response
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
138 local result = st.stanza('query',{
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
139 xmlns = 'http://jabber.org/protocol/disco#info',
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
140 node = query.attr.node
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
141 })
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
142 for key,identity in pairs(identities) do
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
143 result:tag('identity', identity):reset()
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
144 end
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
145 for key,feature in pairs(features) do
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
146 result:tag('feature', feature):reset()
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
147 end
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
148 bot:send(st.stanza('iq',{
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
149 to = stanza.attr.from,
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
150 from = stanza.attr.to,
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
151 id = stanza.attr.id,
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
152 type = 'result'
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
153 }):add_child(result))
6
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
154 return true
16
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
155 end
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
156 end);
6
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
157
16
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
158 bot:hook("iq/http://jabber.org/protocol/disco#items", function (event)
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
159 local stanza = event.stanza
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
160 if stanza.attr.type == 'get' then
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
161 local query = stanza:child_with_name('query')
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
162 if not query then return; end
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
163 -- figure out what items to send
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
164 local items
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
165 if query.attr.node then
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
166 local node = bot.disco.nodes[query.attr.node]
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
167 if node then
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
168 items = node.items or {}
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
169 else
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
170 -- unknown node: give an error
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
171 local response = st.stanza('iq',{
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
172 to = stanza.attr.from,
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
173 from = stanza.attr.to,
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
174 id = stanza.attr.id,
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
175 type = 'error'
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
176 })
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
177 response:tag('query',{xmlns = 'http://jabber.org/protocol/disco#items'}):reset()
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
178 response:tag('error',{type = 'cancel'}):tag(
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
179 'item-not-found',{xmlns = 'urn:ietf:params:xml:ns:xmpp-stanzas'}
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
180 )
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
181 bot:send(response)
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
182 return true
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
183 end
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
184 else
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
185 items = bot.disco.items
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
186 end
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
187 -- construct the response
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
188 local result = st.stanza('query',{
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
189 xmlns = 'http://jabber.org/protocol/disco#items',
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
190 node = query.attr.node
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
191 })
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
192 for key,item in pairs(items) do
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
193 result:tag('item', item):reset()
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
194 end
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
195 bot:send(st.stanza('iq',{
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
196 to = stanza.attr.from,
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
197 from = stanza.attr.to,
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
198 id = stanza.attr.id,
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
199 type = 'result'
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
200 }):add_child(result))
6
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
201 return true
16
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
202 end
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 6
diff changeset
203 end);
6
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
204 end
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
205
b0fec41e695b initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
diff changeset
206 -- end of disco.lua

mercurial