summaryrefslogtreecommitdiffstats
blob: 250573602b62c47e12be8a7b5c45ba8cfe3af1b1 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
--[[
	Copyright (c) 2013 Remko Tronçon
	Licensed under the GNU General Public License.
	See the COPYING file for more information.
--]]

local Client = {}
local PubSub = {}
local PubSubNode = {}

--------------------------------------------------------------------------------
-- Utility methods
--------------------------------------------------------------------------------

local function merge_tables(...)
	local result = {}
	for _, table in ipairs({...}) do
		for k, v in pairs(table) do
			result[k] = v
		end
	end
	return result
end

local function clone_table(table) 
 return merge_tables(table)
end

local function parse_options(unnamed_parameters, arg1, arg2)
	local options = {}
	if type(arg1) == 'table' then
		options = arg1
		f = arg2
	elseif type(arg1) == 'function' then
		f = arg1
	end
	options.f = f or options.f
	return clone_table(options)
end


local function table_value_tostring(value)
	local result = tostring(value)
	if type(value) == 'number' then return result
	elseif type(value) == 'boolean' then return result
	elseif type(value) == 'string' then return "'" .. result .. "'"
	else return '<' .. result .. '>'
	end
end

local function table_tostring(table, indent, accumulator)
	local INDENT = '  '
	local accumulator = accumulator or ''
	local indent = indent or ''
	accumulator = accumulator .. '{'
	local is_first = true
	for key, value in pairs(table) do
		if type(value) ~= 'function' then
			if not is_first then
				accumulator = accumulator .. ','
			end
			is_first = false
			accumulator = accumulator .. '\n' .. indent .. INDENT .. '[' .. table_value_tostring(key) .. '] = '
			if type(value) == 'table' then
				accumulator = table_tostring(value, indent .. INDENT, accumulator)
			else
				accumulator = accumulator .. table_value_tostring(value)
			end
		end
	end
	if not is_first then
		accumulator = accumulator .. '\n' .. indent
	end
	accumulator = accumulator .. '}'
	return accumulator
end

local function tprint(table)
	print(table_tostring(table))
end

local function register_table_tostring(table)
	if type(table) == 'table' then
		local metatable = getmetatable(table)
		if not metatable then
			metatable = {}
			setmetatable(table, metatable)
		end
		metatable.__tostring = table_tostring
	end
	return table
end

local function get_by_type(table, typ)
	for _, v in ipairs(table) do
		if v['_type'] == typ then
			return v
		end
 end
end

local function register_get_by_type_index(table)
	if type(table) == 'table' then
		local metatable = getmetatable(table)
		if not metatable then
			metatable = {}
			setmetatable(table, metatable)
		end
		metatable.__index = get_by_type
	end
	return table
end

--------------------------------------------------------------------------------
-- Client
--------------------------------------------------------------------------------

function Client.connect (client, ...)
	local options = parse_options({}, ...)
	local f = options.f
	client:async_connect(options)
	client:wait_connected()
	if f then
		local result = { xpcall(function() return f(client) end, debug.traceback) }
		client:disconnect()
		if result[1] then
			table.remove(result, 1)
			return unpack(result)
		else
			error(result[2])
		end
	end
	return true
end

function Client.events (client, options)
	local function client_events_iterator(s)
		return s['client']:get_next_event(s['options'])
	end
	return client_events_iterator, {client = client, options = options}
end

function Client.for_each_event (client, ...)
	local options = parse_options({}, ...)
	if not type(options.f) == 'function' then error('Expected function') end
	for event in client:events(options) do
		local result = options.f(event)
		if result then
			return result
		end
	end
end

for method, event_type in pairs({message = 'message', presence = 'presence', pubsub_event = 'pubsub'}) do
	Client['for_each_' .. method] = function (client, ...)
		local options = parse_options({}, ...)
		options['type'] = event_type
		return client:for_each_event (options)
	end

	Client['get_next_' .. method] = function (client, ...)
		local options = parse_options({}, ...)
		options['type'] = event_type
		return client:get_next_event(options)
	end
end

for method, event_type in pairs({messages = 'message', pubsub_events = 'pubsub'}) do
	Client[method] = function (client, ...)
		local options = parse_options({}, ...)
		options['type'] = event_type
		return client:events (options)
	end
end

--
-- Register get_* and set_* convenience methods for some type of queries
--
-- Example usages:
--	client:get_software_version{to = 'alice@wonderland.lit'}
--	client:set_command{to = 'alice@wonderland.lit', command = { type = 'execute', node = 'uptime' }}
--
local get_set_shortcuts = {
	get = {'software_version', 'disco_items', 'xml', 'dom', 'vcard'},
	set = {'command'}
}
for query_action, query_types in pairs(get_set_shortcuts) do
	for _, query_type in ipairs(query_types) do
		Client[query_action .. '_' .. query_type] = function (client, options)
			options = options or {}
			if type(options) ~= 'table' then error('Invalid options: ' .. options) end 
			options['query'] = merge_tables({_type = query_type}, options[query_type] or {})
			return client[query_action](client, options)
		end
	end
end

function Client.pubsub (client, jid)
	local result = { client = client, jid = jid }
	setmetatable(result, PubSub)
	return result
end

--------------------------------------------------------------------------------
-- PubSub
--------------------------------------------------------------------------------

PubSub.__index = PubSub

local function process_pubsub_event (event)
	if event._type == 'pubsub_event_items' then
		-- Add 'item' shortcut to payload of first item
		event.item = event.items and event.items[1] and 
			event.items[1].data and event.items[1].data[1]
	end
end

function PubSub.list_nodes (service, options)
	return service.client:get_disco_items(merge_tables({to = service.jid}, options))
end

function PubSub.node (service, node)
	local result = { client = service.client, jid = service.jid, node = node }
	setmetatable(result, PubSubNode)
	return result
end

local simple_pubsub_queries = {
	get_default_configuration = 'pubsub_owner_default',
	get_subscriptions = 'pubsub_subscriptions',
	get_affiliations = 'pubsub_affiliations',
	get_default_subscription_options = 'pubsub_default',
}
for method, query_type in pairs(simple_pubsub_queries) do
	PubSub[method] = function (service, options)
		options = options or {}
		return service.client:query_pubsub(merge_tables(
			{ type = 'get', to = service.jid, query = { _type = query_type } },
			options))
	end
end

for _, method in ipairs({'events', 'get_next_event', 'for_each_event'}) do
	PubSub[method] = function (node, ...)
		local options = parse_options({}, ...)
		options['if'] = function (event) 
			return event.type == 'pubsub' and event.from == node.jid and event.node == node
		end
		return node.client[method](node.client, options)
 end
end

--------------------------------------------------------------------------------
-- PubSubNode
--------------------------------------------------------------------------------

PubSubNode.__index = PubSubNode

local function pubsub_node_configuration_to_form(configuration)
	if not configuration then
		return
	end
	local fields = { {name = 'form_type', value = 'http://jabber.org/protocol/pubsub#node_config'} }
	for var, value in pairs(configuration) do
		fields[#fields+1] = { name = var, value = value }
	end
	return { type = "submit", fields = fields }
end

function PubSubNode.list_items (node, options)
	return node.client:get_disco_items(merge_tables({to = node.jid, query = { node = node.node }}, options))
end

local simple_pubsub_node_queries = {
	get_configuration = 'pubsub_owner_configure',
	get_subscriptions = 'pubsub_subscriptions',
	get_affiliations = 'pubsub_affiliations',
	get_default_subscription_options = 'pubsub_default',
}
for method, query_type in pairs(simple_pubsub_node_queries) do
	PubSubNode[method] = function (node, options)
		return node.client:query_pubsub(merge_tables({ 
			type = 'get', to = node.jid, query = {
					_type = query_type, node = node.node 
			}}, options))
	end
end

function PubSubNode.get_items (node, ...)
	local options = parse_options({}, ...)
	local items = options.items or {}
	if options.maximum_items then
		items = merge_tables({maximum_items = options.maximum_items}, items)
	end
	items = merge_tables({_type = 'pubsub_items', node = node.node}, items)
	return node.client:query_pubsub(merge_tables({ 
		type = 'get', to = node.jid, query = items}, options))
end

function PubSubNode.get_item (node, ...)
	local options = parse_options({}, ...)
	if not type(options.id) == 'string' then error('Expected ID') end
	return PubSubNode.get_items(node, {items = {{id = options.id}}})
end

function PubSubNode.create (node, options)
	options = options or {}
	local configure
	if options['configuration'] then
		configure = { data = pubsub_node_configuration_to_form(options['configuration']) }
	end
	return node.client:query_pubsub(merge_tables(
		{ type = 'set', to = node.jid, query = { 
				_type = 'pubsub_create', node = node.node, configure = configure }
		}, options))
end

function PubSubNode.delete (node, options)
	options = options or {}
	local redirect
	if options['redirect'] then
		redirect = {uri = options['redirect']}
	end
	return node.client:query_pubsub(merge_tables({ type = 'set', to = node.jid, query = { 
			_type = 'pubsub_owner_delete', node = node.node, redirect = redirect 
		}}, options))
end

function PubSubNode.set_configuration(node, options)
	options = options or {}
	local configuration = pubsub_node_configuration_to_form(options['configuration'])
	return node.client:query_pubsub(merge_tables(
		{ type = 'set', to = node.jid, query = { 
				_type = 'pubsub_owner_configure', node = node.node, data = configuration }
		}, options))
end

function PubSubNode.subscribe(node, ...)
	local options = parse_options(...)
	local jid = options.jid or sluift.jid.to_bare(node.client:jid())
	return node.client:query_pubsub(merge_tables(
		{ type = 'set', to = node.jid, query = { 
				_type = 'pubsub_subscribe', node = node.node, jid = jid }
		}, options))
end

function PubSubNode.unsubscribe(node, options)
	options = options or {}
	return node.client:query_pubsub(merge_tables(
		{ type = 'set', to = node.jid, query = { 
				_type = 'pubsub_unsubscribe', node = node.node, jid = options['jid'] }
		}, options))
end

function PubSubNode.get_subscription_options (node, options)
	return node.client:query_pubsub(merge_tables(
		{ type = 'get', to = node.jid, query = { 
				_type = 'pubsub_options', node = node.node, jid = options['jid'] }
		}, options))
end

function PubSubNode.publish(node, ...)
	local options = parse_options({}, ...)
	local items = options.items or {}
	if options.item then
		if type(options.item) == 'string' or options.item._type then
			items = {{id = options.id, data = { options.item } }}
			options.id = nil 
		else
			items = { options.item }
		end
		options.item = nil
	end
	return node.client:query_pubsub(merge_tables(
		{ type = 'set', to = node.jid, query = { 
				_type = 'pubsub_publish', node = node.node, items = items }
		}, options))
end

function PubSubNode.retract(node, ...)
	local options = parse_options({}, ...)
	local items = options.items
	if options.id then
		items = {{id = options.id}}
	end
	return node.client:query_pubsub(merge_tables(
		{ type = 'set', to = node.jid, query = { 
				_type = 'pubsub_retract', node = node.node, items = items, notify = options['notify']
		}}, options))
end

function PubSubNode.purge(node, ...)
	local options = parse_options({}, ...)
	return node.client:query_pubsub(merge_tables(
		{ type = 'set', to = node.jid, query = { 
				_type = 'pubsub_owner_purge', node = node.node
		}}, options))
end

-- Iterators over events
for _, method in ipairs({'events', 'get_next_event', 'for_each_event'}) do
	PubSubNode[method] = function (node, ...)
		local options = parse_options({}, ...)
		options['if'] = function (event) 
			return event.type == 'pubsub' and event.from == node.jid and event.node == node.node
		end
		return node.client[method](node.client, options)
	end
end

--------------------------------------------------------------------------------
-- Service discovery
--------------------------------------------------------------------------------

local disco = {
	features = {
		DISCO_INFO = 'http://jabber.org/protocol/disco#info',
		COMMANDS = 'http://jabber.org/protocol/commands',
		USER_LOCATION = 'http://jabber.org/protocol/geoloc',
		USER_TUNE = 'http://jabber.org/protocol/tune',
		USER_AVATAR_METADATA = 'urn:xmpp:avatar:metadata',
		USER_ACTIVITY = 'http://jabber.org/protocol/activity',
		USER_PROFILE = 'urn:xmpp:tmp:profile'
	}
}

--------------------------------------------------------------------------------

return {
	Client = Client,
	register_table_tostring = register_table_tostring,
	register_get_by_type_index = register_get_by_type_index,
	process_pubsub_event = process_pubsub_event,
	tprint = tprint,
	disco = disco,
}