mod_delegation/mod_delegation.lua
author Goffi <goffi@goffi.org>
Sat, 18 Apr 2015 00:13:09 +0200
changeset 1720 29dfdfc767b4
parent 1719 241c061bb953
child 1721 e22cd2205fc1
permissions -rw-r--r--
mod_delegation: service discovery extensions (xep-0128) management
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1713
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
     1
-- XEP-0355 (Namespace Delegation)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
     2
-- Copyright (C) 2015 Jérôme Poisson
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
     3
--
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
     4
-- This module is MIT/X11 licensed. Please see the
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
     5
-- COPYING file in the source package for more information.
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
     6
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
     7
-- This module manage namespace delegation, a way to delegate server features
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
     8
-- to an external entity/component. Only the admin mode is implemented so far
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
     9
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
    10
-- TODO: client mode, managing entity error handling, disco extensions (XEP-0128)
1713
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    11
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    12
local jid = require("util/jid")
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    13
local st = require("util/stanza")
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    14
local set = require("util/set")
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    15
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    16
local delegation_session = module:shared("/*/delegation/session")
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    17
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    18
if delegation_session.connected_cb == nil then
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    19
	-- set used to have connected event listeners
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    20
	-- which allow a host to react on events from
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    21
	-- other hosts
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    22
	delegation_session.connected_cb = set.new()
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    23
end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    24
local connected_cb = delegation_session.connected_cb
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    25
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    26
local _DELEGATION_NS = 'urn:xmpp:delegation:1'
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
    27
local _FORWARDED_NS = 'urn:xmpp:forward:0'
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
    28
local _DISCO_NS = 'http://jabber.org/protocol/disco#info'
1720
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
    29
local _DATA_NS = 'jabber:x:data'
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
    30
local _ORI_ID_PREFIX = "IQ_RESULT_"
1713
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    31
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
    32
local _MAIN_SEP = '::'
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
    33
local _BARE_SEP = ':bare:'
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
    34
local _MAIN_PREFIX = _DELEGATION_NS.._MAIN_SEP
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
    35
local _BARE_PREFIX = _DELEGATION_NS.._BARE_SEP
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
    36
local _PREFIXES = {_MAIN_PREFIX, _BARE_PREFIX}
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
    37
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
    38
local disco_nest
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
    39
1713
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    40
module:log("debug", "Loading namespace delegation module ");
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    41
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    42
--> Configuration management <--
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    43
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    44
local ns_delegations = module:get_option("delegations", {})
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    45
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    46
local jid2ns = {}
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
    47
for namespace, ns_data in pairs(ns_delegations) do
1713
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    48
	-- "connected" contain the full jid of connected managing entity
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
    49
	ns_data.connected = nil
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
    50
	if ns_data.jid then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
    51
		if jid2ns[ns_data.jid] == nil then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
    52
			jid2ns[ns_data.jid] = {}
1713
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    53
		end
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
    54
		jid2ns[ns_data.jid][namespace] = ns_data
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
    55
		module:log("debug", "Namespace %s is delegated%s to %s", namespace, ns_data.filtering and " (with filtering)" or "", ns_data.jid)
1713
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    56
	else
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    57
		module:log("warn", "Ignoring delegation for %s: no jid specified", tostring(namespace))
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    58
		ns_delegations[namespace] = nil
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    59
	end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    60
end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    61
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    62
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    63
local function advertise_delegations(session, to_jid)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    64
	-- send <message/> stanza to advertise delegations
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    65
	-- as expained in § 4.2
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    66
	local message = st.message({from=module.host, to=to_jid})
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    67
					  :tag("delegation", {xmlns=_DELEGATION_NS})
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    68
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    69
	-- we need to check if a delegation is granted because the configuration
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    70
	-- can be complicated if some delegations are granted to bare jid
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    71
	-- and other to full jids, and several resources are connected.
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    72
	local have_delegation = false
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    73
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
    74
	for namespace, ns_data  in pairs(jid2ns[to_jid]) do
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
    75
		if ns_data.connected == to_jid then
1713
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    76
			have_delegation = true
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    77
			message:tag("delegated", {namespace=namespace})
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
    78
			if type(ns_data.filtering) == "table" then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
    79
				for _, attribute in pairs(ns_data.filtering) do
1713
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    80
					message:tag("attribute", {name=attribute}):up()
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    81
				end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    82
				message:up()
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    83
			end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    84
		end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    85
	end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    86
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    87
	if have_delegation then
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    88
		session.send(message)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    89
	end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    90
end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    91
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    92
local function set_connected(entity_jid)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    93
	-- set the "connected" key for all namespace managed by entity_jid
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    94
	-- if the namespace has already a connected entity, ignore the new one
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    95
	local function set_config(jid_)
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
    96
		for namespace, ns_data in pairs(jid2ns[jid_]) do
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
    97
			if ns_data.connected == nil then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
    98
				ns_data.connected = entity_jid
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
    99
				disco_nest(namespace, entity_jid)
1713
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   100
			end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   101
		end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   102
	end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   103
	local bare_jid = jid.bare(entity_jid)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   104
	set_config(bare_jid)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   105
	-- We can have a bare jid of a full jid specified in configuration
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   106
	-- so we try our luck with both (first connected resource will
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   107
	-- manage the namespaces in case of bare jid)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   108
	if bare_jid ~= entity_jid then
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   109
		set_config(entity_jid)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   110
		jid2ns[entity_jid] = jid2ns[bare_jid]
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   111
	end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   112
end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   113
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   114
local function on_presence(event)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   115
	local session = event.origin
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   116
	local bare_jid = jid.bare(session.full_jid)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   117
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   118
	if jid2ns[bare_jid] or jid2ns[session.full_jid] then
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   119
		set_connected(session.full_jid)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   120
		advertise_delegations(session, session.full_jid)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   121
	end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   122
end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   123
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   124
local function on_component_connected(event)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   125
	-- method called by the module loaded by the component
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   126
	-- /!\ the event come from the component host,
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   127
	-- not from the host of this module
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   128
	local session = event.session
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   129
	local bare_jid = jid.join(session.username, session.host)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   130
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   131
	local jid_delegations = jid2ns[bare_jid]
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   132
	if jid_delegations ~= nil then
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   133
		set_connected(bare_jid)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   134
		advertise_delegations(session, bare_jid)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   135
	end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   136
end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   137
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   138
local function on_component_auth(event)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   139
	-- react to component-authenticated event from this host
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   140
	-- and call the on_connected methods from all other hosts
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   141
	-- needed for the component to get delegations advertising
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   142
	for callback in connected_cb:items() do
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   143
		callback(event)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   144
	end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   145
end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   146
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   147
connected_cb:add(on_component_connected)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   148
module:hook('component-authenticated', on_component_auth)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   149
module:hook('presence/initial', on_presence)
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   150
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   151
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   152
--> delegated namespaces hook <--
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   153
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   154
local function managing_ent_result(event)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   155
	-- this function manage iq results from the managing entity
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   156
	-- it do a couple of security check before sending the
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   157
	-- result to the managed entity
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   158
	local session, stanza = event.origin, event.stanza
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   159
	if stanza.attr.to ~= module.host then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   160
		module:log("warn", 'forwarded stanza result has "to" attribute not addressed to current host, id conflict ?')
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   161
		return
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   162
	end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   163
	module:unhook("iq-result/host/"..stanza.attr.id, managing_ent_result)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   164
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   165
	-- lot of checks to do...
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   166
	local delegation = stanza.tags[1]
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   167
	if #stanza ~= 1 or delegation.name ~= "delegation" or
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   168
		delegation.attr.xmlns ~= _DELEGATION_NS then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   169
		session.send(st.error_reply(stanza, 'modify', 'not-acceptable'))
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   170
		return true
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   171
	end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   172
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   173
	local forwarded = delegation.tags[1]
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   174
	if #delegation ~= 1 or forwarded.name ~= "forwarded" or
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   175
		forwarded.attr.xmlns ~= _FORWARDED_NS then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   176
		session.send(st.error_reply(stanza, 'modify', 'not-acceptable'))
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   177
		return true
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   178
	end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   179
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   180
	local iq = forwarded.tags[1]
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   181
	if #forwarded ~= 1 or iq.name ~= "iq" or #iq ~= 1 then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   182
		session.send(st.error_reply(stanza, 'modify', 'not-acceptable'))
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   183
		return true
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   184
	end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   185
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   186
	local namespace = iq.tags[1].xmlns
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   187
	local ns_data = ns_delegations[namespace]
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   188
	local original = ns_data[_ORI_ID_PREFIX..stanza.attr.id]
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   189
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   190
	if stanza.attr.from ~= ns_data.connected or iq.attr.type ~= "result" or
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   191
		iq.attr.id ~= original.attr.id or iq.attr.to ~= original.attr.from then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   192
		session.send(st.error_reply(stanza, 'auth', 'forbidden'))
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   193
		module:send(st.error_reply(original, 'cancel', 'service-unavailable'))
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   194
		return true
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   195
	end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   196
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   197
	-- at this point eveything is checked,
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   198
	-- and we (hopefully) can send the the result safely
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   199
	module:send(iq)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   200
end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   201
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   202
local function forward_iq(stanza, ns_data)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   203
	local to_jid = ns_data.connected
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   204
	local iq_stanza  = st.iq({ from=module.host, to=to_jid, type="set" })
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   205
		:tag("delegation", { xmlns=_DELEGATION_NS })
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   206
		:tag("forwarded", { xmlns=_FORWARDED_NS })
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   207
		:add_child(stanza)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   208
	local iq_id = iq_stanza.attr.id
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   209
	-- we save the original stanza to check the managing entity result
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   210
	ns_data[_ORI_ID_PREFIX..iq_id] = stanza
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   211
	module:hook("iq-result/host/"..iq_id, managing_ent_result)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   212
	module:send(iq_stanza)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   213
end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   214
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   215
local function iq_hook(event)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   216
	-- general hook for all the iq which forward delegated ones
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   217
	-- and continue normal behaviour else. If a namespace is
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   218
	-- delegated but managing entity is offline, a service-unavailable
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   219
	-- error will be sent, as requested by the XEP
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   220
	local session, stanza = event.origin, event.stanza
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   221
	if #stanza == 1 and stanza.attr.type == 'get' or stanza.attr.type == 'set' then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   222
		local namespace = stanza.tags[1].attr.xmlns
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   223
		local ns_data = ns_delegations[namespace]
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   224
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   225
		if ns_data then
1719
241c061bb953 mod_delegation: we don't forward stanzas from managing entity itself
Goffi <goffi@goffi.org>
parents: 1718
diff changeset
   226
			if stanza.attr.from == ns_data.connected then
241c061bb953 mod_delegation: we don't forward stanzas from managing entity itself
Goffi <goffi@goffi.org>
parents: 1718
diff changeset
   227
				-- we don't forward stanzas from managing entity itself
241c061bb953 mod_delegation: we don't forward stanzas from managing entity itself
Goffi <goffi@goffi.org>
parents: 1718
diff changeset
   228
				return
241c061bb953 mod_delegation: we don't forward stanzas from managing entity itself
Goffi <goffi@goffi.org>
parents: 1718
diff changeset
   229
			end
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   230
			if ns_data.filtering then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   231
				local first_child = stanza.tags[1]
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   232
				for _, attribute in ns_data.filtering do
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   233
					-- if any filtered attribute if not present,
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   234
					-- we must continue the normal bahaviour
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   235
					if not first_child.attr[attribute] then
1715
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   236
						-- Filtered attribute is not present, we do normal workflow
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   237
						return;
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   238
					end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   239
				end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   240
			end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   241
			if not ns_data.connected then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   242
				module:log("warn", "No connected entity to manage "..namespace)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   243
				session.send(st.error_reply(stanza, 'cancel', 'service-unavailable'))
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   244
			else
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   245
				forward_iq(stanza, ns_data)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   246
			end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   247
			return true
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   248
		else
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   249
			-- we have no delegation, we continue normal behaviour
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   250
			return
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   251
		end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   252
	end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   253
end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   254
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   255
module:hook("iq/self", iq_hook, 2^32)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   256
module:hook("iq/host", iq_hook, 2^32)
1715
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   257
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   258
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   259
--> discovery nesting <--
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   260
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   261
-- disabling internal features/identities
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   262
1720
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   263
local function find_form_type(stanza)
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   264
	local form_type = nil
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   265
	for field in stanza.childtags('field', 'jabber:x:data') do
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   266
		if field.attr.var=='FORM_TYPE' and field.attr.type=='hidden' then
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   267
			local value = field:get_child('value')
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   268
			if not value then
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   269
				module:log("warn", "No value found in FORM_TYPE field: "..tostring(stanza))
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   270
			else
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   271
				form_type=value.get_text()
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   272
			end
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   273
		end
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   274
	end
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   275
	return form_type
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   276
end
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   277
1715
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   278
-- modules whose features/identities are managed by delegation
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   279
local disabled_modules = set.new()
1717
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   280
local disabled_identities = set.new()
1715
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   281
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   282
local function identity_added(event)
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   283
	local source = event.source
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   284
	if disabled_modules:contains(source) then
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   285
		local item = event.item
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   286
		local category, type_, name = item.category, item.type, item.name
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   287
		module:log("debug", "Removing (%s/%s%s) identity because of delegation", category, type_, name and "/"..name or "")
1717
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   288
		disabled_identities:add(item)
1715
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   289
		source:remove_item("identity", item)
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   290
	end
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   291
end
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   292
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   293
local function feature_added(event)
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   294
	local source, item = event.source, event.item
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   295
	for namespace, _ in pairs(ns_delegations) do
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   296
		if source ~= module and string.sub(item, 1, #namespace) == namespace then
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   297
			module:log("debug", "Removing %s feature which is delegated", item)
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   298
			source:remove_item("feature", item)
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   299
			disabled_modules:add(source)
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   300
			if source.items and source.items.identity then
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   301
				-- we remove all identities added by the source module
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   302
				-- that can cause issues if the module manages several features/identities
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   303
				-- but this case is probably rare (or doesn't happen at all)
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   304
				-- FIXME: any better way ?
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   305
				for _, identity in pairs(source.items.identity) do
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   306
					identity_added({source=source, item=identity})
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   307
				end
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   308
			end
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   309
		end
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   310
	end
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   311
end
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   312
1720
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   313
local function extension_added(event)
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   314
	local source, stanza = event.source, event.item
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   315
	local form_type = find_form_type(stanza)
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   316
	if not form_type then return; end
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   317
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   318
	for namespace, _ in pairs(ns_delegations) do
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   319
		if source ~= module and string.sub(form_type, 1, #namespace) == namespace then
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   320
			module:log("debug", "Removing extension which is delegated: %s", tostring(stanza))
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   321
			source:remove_item("extension", stanza)
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   322
		end
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   323
	end
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   324
end
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   325
1715
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   326
-- for disco nesting (see § 7.2) we need to remove internal features
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   327
-- we use handle_items as it allow to remove already added features
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   328
-- and catch the ones which can come later
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   329
module:handle_items("feature", feature_added, function(_) end)
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   330
module:handle_items("identity", identity_added, function(_) end, false)
1720
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   331
module:handle_items("extension", extension_added, function(_) end)
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   332
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   333
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   334
-- managing entity features/identities collection
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   335
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   336
local disco_error
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   337
local bare_features = set.new()
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   338
local bare_identities = {}
1720
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   339
local bare_extensions = {}
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   340
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   341
local function disco_result(event)
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   342
	-- parse result from disco nesting request
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   343
	-- and fill module features/identities and bare_features/bare_identities accordingly
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   344
	local session, stanza = event.origin, event.stanza
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   345
	if stanza.attr.to ~= module.host then
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   346
		module:log("warn", 'Stanza result has "to" attribute not addressed to current host, id conflict ?')
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   347
		return
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   348
	end
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   349
	module:unhook("iq-result/host/"..stanza.attr.id, disco_result)
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   350
	module:unhook("iq-error/host/"..stanza.attr.id, disco_error)
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   351
	local query = stanza:get_child("query", _DISCO_NS)
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   352
	if not query or not query.attr.node then
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   353
		session.send(st.error_reply(stanza, 'modify', 'not-acceptable'))
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   354
		return true
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   355
	end
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   356
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   357
	local node = query.attr.node
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   358
	local main
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   359
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   360
	if string.sub(node, 1, #_MAIN_PREFIX) == _MAIN_PREFIX then
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   361
		main=true
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   362
	elseif string.sub(node, 1, #_BARE_PREFIX) == _BARE_PREFIX then
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   363
		main=false
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   364
	else
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   365
		module:log("warn", "Unexpected node: "..node)
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   366
		session.send(st.error_reply(stanza, 'modify', 'not-acceptable'))
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   367
		return true
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   368
	end
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   369
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   370
	for feature in query:childtags("feature") do
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   371
		local namespace = feature.attr.var
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   372
		if main then
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   373
			if not module:has_feature(namespace) then -- we avoid doubling features in case of disconnection/reconnexion
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   374
				module:add_feature(namespace)
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   375
			end
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   376
		else
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   377
			bare_features:add(namespace)
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   378
		end
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   379
	end
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   380
	for identity in query:childtags("identity") do
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   381
		local category, type_, name = identity.attr.category, identity.attr.type, identity.attr.name
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   382
		if main then
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   383
			if not module:has_identity(category, type_, name) then
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   384
				module:add_identity(category, type_, name)
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   385
			end
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   386
		else
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   387
			local found=false
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   388
			for _, item in ipairs(bare_identities) do
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   389
				if item.category == category and item.type == type_ and item.name == name then
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   390
					found=true
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   391
					break
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   392
				end
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   393
			end
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   394
			if not found then
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   395
				table.insert(bare_identities, {category=category, type=type_, name=name})
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   396
			end
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   397
		end
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   398
	end
1720
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   399
	for extension in query:childtags("x", _DATA_NS) do
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   400
		if main then
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   401
			module:add_extension(extension)
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   402
		else
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   403
			table.insert(bare_extensions, extension)
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   404
		end
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   405
	end
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   406
end
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   407
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   408
function disco_error(event)
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   409
	local stanza = event.stanza
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   410
	if stanza.attr.to ~= module.host then
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   411
		module:log("warn", 'Stanza result has "to" attribute not addressed to current host, id conflict ?')
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   412
		return
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   413
	end
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   414
	module:unhook("iq-result/host/"..stanza.attr.id, disco_result)
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   415
	module:unhook("iq-error/host/"..stanza.attr.id, disco_error)
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   416
	module:log("warn", "Got an error while requesting disco for nesting to "..stanza.attr.from)
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   417
	module:log("warn", "Ignoring disco nesting")
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   418
end
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   419
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   420
function disco_nest(namespace, entity_jid)
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   421
	for _, prefix in ipairs(_PREFIXES) do
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   422
		local node = prefix..namespace
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   423
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   424
		local iq = st.iq({from=module.host, to=entity_jid, type='get'})
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   425
			:tag('query', {xmlns=_DISCO_NS, node=node})
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   426
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   427
		local iq_id = iq.attr.id
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   428
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   429
		module:hook("iq-result/host/"..iq_id, disco_result)
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   430
		module:hook("iq-error/host/"..iq_id, disco_error)
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   431
		module:send(iq)
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   432
	end
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   433
end
1717
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   434
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   435
-- disco to bare jids special case
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   436
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   437
module:hook("account-disco-info", function(event)
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   438
	-- this event is called when a disco info request is done on a bare jid
1720
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   439
	-- we get the final reply and filter delegated features/identities/extensions
1717
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   440
	local reply = event.reply;
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   441
	reply.tags[1]:maptags(function(child)
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   442
		if child.name == 'feature' then
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   443
			local feature_ns = child.attr.var
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   444
			for namespace, _ in pairs(ns_delegations) do
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   445
				if string.sub(feature_ns, 1, #namespace) == namespace then
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   446
					module:log("debug", "Removing feature namespace %s which is delegated", feature_ns)
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   447
					return nil
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   448
				end
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   449
			end
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   450
		elseif child.name == 'identity' then
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   451
			for item in disabled_identities:items() do
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   452
				if item.category == child.attr.category
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   453
					and item.type == child.attr.type
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   454
					-- we don't check name, because mod_pep use a name for main disco, but not in account-disco-info hook
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   455
					-- and item.name == child.attr.name
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   456
				then
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   457
					module:log("debug", "Removing (%s/%s%s) identity because of delegation", item.category, item.type, item.name and "/"..item.name or "")
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   458
					return nil
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   459
				end
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   460
			end
1720
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   461
		elseif child.name == 'x' and child.attr.xmlns == _DATA_NS then
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   462
			local form_type = find_form_type(child)
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   463
			if form_type then
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   464
				for namespace, _ in pairs(ns_delegations) do
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   465
					if string.sub(form_type, 1, #namespace) == namespace then
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   466
						module:log("debug", "Removing extension which is delegated: %s", tostring(child))
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   467
						return nil
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   468
					end
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   469
				end
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   470
			end
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   471
1717
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   472
		end
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   473
		return child
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   474
	end)
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   475
	for feature in bare_features:items() do
1720
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   476
		reply:tag('feature', {var=feature}):up()
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   477
	end
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   478
	for _, item in ipairs(bare_identities) do
1720
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   479
		reply:tag('identity', {category=item.category, type=item.type, name=item.name}):up()
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   480
	end
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   481
	for _, stanza in ipairs(bare_extensions) do
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   482
		reply:add_child(stanza)
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   483
	end
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   484
1717
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   485
end, -2^32);