mod_delegation/mod_delegation.lua
author Matthew Wild <mwild1@gmail.com>
Thu, 13 Sep 2018 16:28:10 +0100
changeset 3321 36a9170352b5
parent 3275 7504f765e767
child 3391 c5e8042b174c
permissions -rw-r--r--
mod_delegation: Update to generate own iq ids
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)
2071
107147ca77f7 mod_delegation: date update
Goffi <goffi@goffi.org>
parents: 1995
diff changeset
     2
-- Copyright (C) 2015-2016 Jérôme Poisson
1713
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
1725
f49359330493 mod_delegation: handling of error replies from managing entities after forward.
Goffi <goffi@goffi.org>
parents: 1724
diff changeset
    10
-- TODO: client mode
1713
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    11
1995
a1d5214bd369 mod_delegation: fixed imports, using "/" instead of "." was causing caching issues
Goffi <goffi@goffi.org>
parents: 1992
diff changeset
    12
local jid = require("util.jid")
a1d5214bd369 mod_delegation: fixed imports, using "/" instead of "." was causing caching issues
Goffi <goffi@goffi.org>
parents: 1992
diff changeset
    13
local st = require("util.stanza")
a1d5214bd369 mod_delegation: fixed imports, using "/" instead of "." was causing caching issues
Goffi <goffi@goffi.org>
parents: 1992
diff changeset
    14
local set = require("util.set")
1713
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    15
3321
36a9170352b5 mod_delegation: Update to generate own iq ids
Matthew Wild <mwild1@gmail.com>
parents: 3275
diff changeset
    16
local new_id = require("util.id").short;
36a9170352b5 mod_delegation: Update to generate own iq ids
Matthew Wild <mwild1@gmail.com>
parents: 3275
diff changeset
    17
1713
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    18
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
    19
2756
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
    20
-- FIXME: temporarily needed for disco_items_hook, to be removed when clean implementation is done
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
    21
local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
    22
local jid_split = require "util.jid".split;
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
    23
local jid_bare = require "util.jid".bare;
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
    24
1713
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    25
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
    26
	-- 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
    27
	-- 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
    28
	-- other hosts
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    29
	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
    30
end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    31
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
    32
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    33
local _DELEGATION_NS = 'urn:xmpp:delegation:1'
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
    34
local _FORWARDED_NS = 'urn:xmpp:forward:0'
2756
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
    35
local _DISCO_INFO_NS = 'http://jabber.org/protocol/disco#info'
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
    36
local _DISCO_ITEMS_NS = 'http://jabber.org/protocol/disco#items'
1720
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
    37
local _DATA_NS = 'jabber:x:data'
1713
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    38
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
    39
local _MAIN_SEP = '::'
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
    40
local _BARE_SEP = ':bare:'
2756
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
    41
local _REMAINING = ':*'
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
    42
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
    43
local _BARE_PREFIX = _DELEGATION_NS.._BARE_SEP
2756
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
    44
local _DISCO_REMAINING = _DISCO_ITEMS_NS.._REMAINING
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
    45
local _PREFIXES = {_MAIN_PREFIX, _BARE_PREFIX}
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
    46
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
    47
local disco_nest
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
    48
2756
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
    49
module:log("debug", "Loading namespace delegation module ")
1713
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    50
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    51
--> Configuration management <--
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    52
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    53
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
    54
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    55
local jid2ns = {}
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
    56
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
    57
	-- "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
    58
	ns_data.connected = nil
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
    59
	if ns_data.jid then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
    60
		if jid2ns[ns_data.jid] == nil then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
    61
			jid2ns[ns_data.jid] = {}
1713
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    62
		end
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
    63
		jid2ns[ns_data.jid][namespace] = ns_data
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
    64
		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
    65
	else
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    66
		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
    67
		ns_delegations[namespace] = nil
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    68
	end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    69
end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    70
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    71
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    72
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
    73
	-- 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
    74
	-- as expained in § 4.2
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    75
	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
    76
					  :tag("delegation", {xmlns=_DELEGATION_NS})
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    77
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    78
	-- 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
    79
	-- 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
    80
	-- 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
    81
	local have_delegation = false
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    82
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
    83
	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
    84
		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
    85
			have_delegation = true
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    86
			message:tag("delegated", {namespace=namespace})
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
    87
			if type(ns_data.filtering) == "table" then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
    88
				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
    89
					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
    90
				end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    91
			end
2762
82109d8eca41 mod_delegation: fixed misplaced up() in <message> stanza generation
Goffi <goffi@goffi.org>
parents: 2756
diff changeset
    92
			message:up()
1713
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    93
		end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    94
	end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    95
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    96
	if have_delegation then
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    97
		session.send(message)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    98
	end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
    99
end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   100
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   101
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
   102
	-- 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
   103
	-- 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
   104
	local function set_config(jid_)
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   105
		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
   106
			if ns_data.connected == nil then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   107
				ns_data.connected = entity_jid
2756
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   108
				-- disco remaining is a special namespace
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   109
				-- there is no disco nesting for it
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   110
				if namespace ~= _DISCO_REMAINING then
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   111
					disco_nest(namespace, entity_jid)
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   112
				end
1713
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   113
			end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   114
		end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   115
	end
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(entity_jid)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   117
	set_config(bare_jid)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   118
	-- 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
   119
	-- 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
   120
	-- 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
   121
	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
   122
		set_config(entity_jid)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   123
		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
   124
	end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   125
end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   126
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   127
local function on_presence(event)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   128
	local session = event.origin
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   129
	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
   130
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   131
	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
   132
		set_connected(session.full_jid)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   133
		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
   134
	end
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
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   137
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
   138
	-- 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
   139
	-- /!\ 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
   140
	-- 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
   141
	local session = event.session
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   142
	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
   143
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   144
	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
   145
	if jid_delegations ~= nil then
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   146
		set_connected(bare_jid)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   147
		advertise_delegations(session, bare_jid)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   148
	end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   149
end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   150
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   151
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
   152
	-- 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
   153
	-- 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
   154
	-- 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
   155
	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
   156
		callback(event)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   157
	end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   158
end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   159
1780
e7b5ab44339c mod_delegation: fixed bad calling of on_auth for components
Goffi <goffi@goffi.org>
parents: 1778
diff changeset
   160
if module:get_host_type() ~= "component" then
2756
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   161
	connected_cb:add(on_component_connected)
1780
e7b5ab44339c mod_delegation: fixed bad calling of on_auth for components
Goffi <goffi@goffi.org>
parents: 1778
diff changeset
   162
end
1713
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
   163
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
   164
module:hook('presence/initial', on_presence)
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   165
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   166
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   167
--> delegated namespaces hook <--
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   168
1725
f49359330493 mod_delegation: handling of error replies from managing entities after forward.
Goffi <goffi@goffi.org>
parents: 1724
diff changeset
   169
local managing_ent_error
1724
48b7e8021afa mod_delegation: original stanza is now cached outside of ns_data, so it can be accessed without knowing the namespace.
Goffi <goffi@goffi.org>
parents: 1723
diff changeset
   170
local stanza_cache = {} -- we cache original stanza to build reply
1725
f49359330493 mod_delegation: handling of error replies from managing entities after forward.
Goffi <goffi@goffi.org>
parents: 1724
diff changeset
   171
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   172
local function managing_ent_result(event)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   173
	-- 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
   174
	-- 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
   175
	-- result to the managed entity
1723
3938496cd4f8 mod_delegation: removed invalid error replies to iq result.
Goffi <goffi@goffi.org>
parents: 1722
diff changeset
   176
	local stanza = event.stanza
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   177
	if stanza.attr.to ~= module.host then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   178
		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
   179
		return
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   180
	end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   181
	module:unhook("iq-result/host/"..stanza.attr.id, managing_ent_result)
1725
f49359330493 mod_delegation: handling of error replies from managing entities after forward.
Goffi <goffi@goffi.org>
parents: 1724
diff changeset
   182
	module:unhook("iq-error/host/"..stanza.attr.id, managing_ent_error)
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   183
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   184
	-- lot of checks to do...
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   185
	local delegation = stanza.tags[1]
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   186
	if #stanza ~= 1 or delegation.name ~= "delegation" or
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   187
		delegation.attr.xmlns ~= _DELEGATION_NS then
1723
3938496cd4f8 mod_delegation: removed invalid error replies to iq result.
Goffi <goffi@goffi.org>
parents: 1722
diff changeset
   188
		module:log("warn", "ignoring invalid iq result from managing entity %s", stanza.attr.from)
1724
48b7e8021afa mod_delegation: original stanza is now cached outside of ns_data, so it can be accessed without knowing the namespace.
Goffi <goffi@goffi.org>
parents: 1723
diff changeset
   189
		stanza_cache[stanza.attr.from][stanza.attr.id] = nil
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   190
		return true
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   191
	end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   192
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   193
	local forwarded = delegation.tags[1]
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   194
	if #delegation ~= 1 or forwarded.name ~= "forwarded" or
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   195
		forwarded.attr.xmlns ~= _FORWARDED_NS then
1723
3938496cd4f8 mod_delegation: removed invalid error replies to iq result.
Goffi <goffi@goffi.org>
parents: 1722
diff changeset
   196
		module:log("warn", "ignoring invalid iq result from managing entity %s", stanza.attr.from)
1724
48b7e8021afa mod_delegation: original stanza is now cached outside of ns_data, so it can be accessed without knowing the namespace.
Goffi <goffi@goffi.org>
parents: 1723
diff changeset
   197
		stanza_cache[stanza.attr.from][stanza.attr.id] = nil
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   198
		return true
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   199
	end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   200
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   201
	local iq = forwarded.tags[1]
1727
ef85c42ad977 mod_delegation: fixed bad handling of error replies from managing entity
Goffi <goffi@goffi.org>
parents: 1726
diff changeset
   202
	if #forwarded ~= 1 or iq.name ~= "iq" or
2756
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   203
		iq.attr.xmlns ~= 'jabber:client' or
1778
7bfc23b2c038 mod_delegation: fixed handling of <iq> results without children
Goffi <goffi@goffi.org>
parents: 1728
diff changeset
   204
		(iq.attr.type =='result' and #iq > 1) or
1727
ef85c42ad977 mod_delegation: fixed bad handling of error replies from managing entity
Goffi <goffi@goffi.org>
parents: 1726
diff changeset
   205
		(iq.attr.type == 'error' and #iq > 2) then
1723
3938496cd4f8 mod_delegation: removed invalid error replies to iq result.
Goffi <goffi@goffi.org>
parents: 1722
diff changeset
   206
		module:log("warn", "ignoring invalid iq result from managing entity %s", stanza.attr.from)
1724
48b7e8021afa mod_delegation: original stanza is now cached outside of ns_data, so it can be accessed without knowing the namespace.
Goffi <goffi@goffi.org>
parents: 1723
diff changeset
   207
		stanza_cache[stanza.attr.from][stanza.attr.id] = nil
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   208
		return true
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   209
	end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   210
2756
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   211
	iq.attr.xmlns = nil
1728
2440a75e868f mod_delegation: managing_ent_* now stop event propagation + forwarded <iq/> xmlns check
Goffi <goffi@goffi.org>
parents: 1727
diff changeset
   212
1724
48b7e8021afa mod_delegation: original stanza is now cached outside of ns_data, so it can be accessed without knowing the namespace.
Goffi <goffi@goffi.org>
parents: 1723
diff changeset
   213
	local original = stanza_cache[stanza.attr.from][stanza.attr.id]
48b7e8021afa mod_delegation: original stanza is now cached outside of ns_data, so it can be accessed without knowing the namespace.
Goffi <goffi@goffi.org>
parents: 1723
diff changeset
   214
	stanza_cache[stanza.attr.from][stanza.attr.id] = nil
1727
ef85c42ad977 mod_delegation: fixed bad handling of error replies from managing entity
Goffi <goffi@goffi.org>
parents: 1726
diff changeset
   215
	-- we get namespace from original and not iq
ef85c42ad977 mod_delegation: fixed bad handling of error replies from managing entity
Goffi <goffi@goffi.org>
parents: 1726
diff changeset
   216
	-- because the namespace can be lacking in case of error
ef85c42ad977 mod_delegation: fixed bad handling of error replies from managing entity
Goffi <goffi@goffi.org>
parents: 1726
diff changeset
   217
	local namespace = original.tags[1].attr.xmlns
2756
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   218
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   219
	-- small hack for disco remaining feat
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   220
	if namespace == _DISCO_ITEMS_NS then
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   221
		namespace = _DISCO_REMAINING
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   222
	end
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   223
1727
ef85c42ad977 mod_delegation: fixed bad handling of error replies from managing entity
Goffi <goffi@goffi.org>
parents: 1726
diff changeset
   224
	local ns_data = ns_delegations[namespace]
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   225
1727
ef85c42ad977 mod_delegation: fixed bad handling of error replies from managing entity
Goffi <goffi@goffi.org>
parents: 1726
diff changeset
   226
	if stanza.attr.from ~= ns_data.connected or (iq.attr.type ~= "result" and iq.attr.type ~= "error") or
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   227
		iq.attr.id ~= original.attr.id or iq.attr.to ~= original.attr.from then
1723
3938496cd4f8 mod_delegation: removed invalid error replies to iq result.
Goffi <goffi@goffi.org>
parents: 1722
diff changeset
   228
		module:log("warn", "ignoring forbidden iq result from managing entity %s, please check that the component is no trying to do something bad (stanza: %s)", stanza.attr.from, tostring(stanza))
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   229
		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
   230
		return true
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   231
	end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   232
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   233
	-- at this point eveything is checked,
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   234
	-- 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
   235
	module:send(iq)
2756
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   236
	return true
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   237
end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   238
1725
f49359330493 mod_delegation: handling of error replies from managing entities after forward.
Goffi <goffi@goffi.org>
parents: 1724
diff changeset
   239
function managing_ent_error(event)
f49359330493 mod_delegation: handling of error replies from managing entities after forward.
Goffi <goffi@goffi.org>
parents: 1724
diff changeset
   240
	local stanza = event.stanza
f49359330493 mod_delegation: handling of error replies from managing entities after forward.
Goffi <goffi@goffi.org>
parents: 1724
diff changeset
   241
	if stanza.attr.to ~= module.host then
f49359330493 mod_delegation: handling of error replies from managing entities after forward.
Goffi <goffi@goffi.org>
parents: 1724
diff changeset
   242
		module:log("warn", 'Stanza result has "to" attribute not addressed to current host, id conflict ?')
f49359330493 mod_delegation: handling of error replies from managing entities after forward.
Goffi <goffi@goffi.org>
parents: 1724
diff changeset
   243
		return
f49359330493 mod_delegation: handling of error replies from managing entities after forward.
Goffi <goffi@goffi.org>
parents: 1724
diff changeset
   244
	end
f49359330493 mod_delegation: handling of error replies from managing entities after forward.
Goffi <goffi@goffi.org>
parents: 1724
diff changeset
   245
	module:unhook("iq-result/host/"..stanza.attr.id, managing_ent_result)
f49359330493 mod_delegation: handling of error replies from managing entities after forward.
Goffi <goffi@goffi.org>
parents: 1724
diff changeset
   246
	module:unhook("iq-error/host/"..stanza.attr.id, managing_ent_error)
f49359330493 mod_delegation: handling of error replies from managing entities after forward.
Goffi <goffi@goffi.org>
parents: 1724
diff changeset
   247
	local original = stanza_cache[stanza.attr.from][stanza.attr.id]
f49359330493 mod_delegation: handling of error replies from managing entities after forward.
Goffi <goffi@goffi.org>
parents: 1724
diff changeset
   248
	stanza_cache[stanza.attr.from][stanza.attr.id] = nil
f49359330493 mod_delegation: handling of error replies from managing entities after forward.
Goffi <goffi@goffi.org>
parents: 1724
diff changeset
   249
	module:log("warn", "Got an error after forwarding stanza to "..stanza.attr.from)
f49359330493 mod_delegation: handling of error replies from managing entities after forward.
Goffi <goffi@goffi.org>
parents: 1724
diff changeset
   250
	module:send(st.error_reply(original, 'cancel', 'service-unavailable'))
2756
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   251
	return true
1725
f49359330493 mod_delegation: handling of error replies from managing entities after forward.
Goffi <goffi@goffi.org>
parents: 1724
diff changeset
   252
end
f49359330493 mod_delegation: handling of error replies from managing entities after forward.
Goffi <goffi@goffi.org>
parents: 1724
diff changeset
   253
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   254
local function forward_iq(stanza, ns_data)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   255
	local to_jid = ns_data.connected
1726
c48c7f948cfb mod_delegation: fixed namespace of <forwarded/> child element, as specified in XEP-0297
Goffi <goffi@goffi.org>
parents: 1725
diff changeset
   256
	stanza.attr.xmlns = 'jabber:client'
3321
36a9170352b5 mod_delegation: Update to generate own iq ids
Matthew Wild <mwild1@gmail.com>
parents: 3275
diff changeset
   257
	local iq_id = new_id();
36a9170352b5 mod_delegation: Update to generate own iq ids
Matthew Wild <mwild1@gmail.com>
parents: 3275
diff changeset
   258
	local iq_stanza  = st.iq({ from=module.host, to=to_jid, type="set", id = iq_id })
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   259
		:tag("delegation", { xmlns=_DELEGATION_NS })
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   260
		:tag("forwarded", { xmlns=_FORWARDED_NS })
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   261
		:add_child(stanza)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   262
	-- we save the original stanza to check the managing entity result
1724
48b7e8021afa mod_delegation: original stanza is now cached outside of ns_data, so it can be accessed without knowing the namespace.
Goffi <goffi@goffi.org>
parents: 1723
diff changeset
   263
	if not stanza_cache[to_jid] then stanza_cache[to_jid] = {} end
48b7e8021afa mod_delegation: original stanza is now cached outside of ns_data, so it can be accessed without knowing the namespace.
Goffi <goffi@goffi.org>
parents: 1723
diff changeset
   264
	stanza_cache[to_jid][iq_id] = stanza
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   265
	module:hook("iq-result/host/"..iq_id, managing_ent_result)
1725
f49359330493 mod_delegation: handling of error replies from managing entities after forward.
Goffi <goffi@goffi.org>
parents: 1724
diff changeset
   266
	module:hook("iq-error/host/"..iq_id, managing_ent_error)
f49359330493 mod_delegation: handling of error replies from managing entities after forward.
Goffi <goffi@goffi.org>
parents: 1724
diff changeset
   267
	module:log("debug", "stanza forwarded")
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   268
	module:send(iq_stanza)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   269
end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   270
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   271
local function iq_hook(event)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   272
	-- 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
   273
	-- 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
   274
	-- 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
   275
	-- 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
   276
	local session, stanza = event.origin, event.stanza
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   277
	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
   278
		local namespace = stanza.tags[1].attr.xmlns
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   279
		local ns_data = ns_delegations[namespace]
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   280
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   281
		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
   282
			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
   283
				-- 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
   284
				return
241c061bb953 mod_delegation: we don't forward stanzas from managing entity itself
Goffi <goffi@goffi.org>
parents: 1718
diff changeset
   285
			end
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   286
			if ns_data.filtering then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   287
				local first_child = stanza.tags[1]
1992
d7c1daaf2dea mod_delegation: fixed attribute filtering
Goffi <goffi@goffi.org>
parents: 1780
diff changeset
   288
				for _, attribute in pairs(ns_data.filtering) do
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   289
					-- if any filtered attribute if not present,
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   290
					-- we must continue the normal bahaviour
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   291
					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
   292
						-- Filtered attribute is not present, we do normal workflow
2756
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   293
						return
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   294
					end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   295
				end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   296
			end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   297
			if not ns_data.connected then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   298
				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
   299
				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
   300
			else
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   301
				forward_iq(stanza, ns_data)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   302
			end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   303
			return true
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   304
		else
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   305
			-- we have no delegation, we continue normal behaviour
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   306
			return
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   307
		end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   308
	end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   309
end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   310
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   311
module:hook("iq/self", iq_hook, 2^32)
1722
48be6e7efbe8 mod_delegation: added 'iq/bare' to hooked events.
Goffi <goffi@goffi.org>
parents: 1721
diff changeset
   312
module:hook("iq/bare", iq_hook, 2^32)
1714
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1713
diff changeset
   313
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
   314
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   315
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   316
--> discovery nesting <--
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   317
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   318
-- disabling internal features/identities
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   319
1720
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   320
local function find_form_type(stanza)
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   321
	local form_type = nil
2073
cf9cd666ba00 mod_delegation: fixed bad use of period instead of colon
Goffi <goffi@goffi.org>
parents: 2071
diff changeset
   322
	for field in stanza:childtags('field', 'jabber:x:data') do
1720
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   323
		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
   324
			local value = field:get_child('value')
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   325
			if not value then
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   326
				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
   327
			else
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   328
				form_type=value.get_text()
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   329
			end
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   330
		end
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   331
	end
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   332
	return form_type
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   333
end
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   334
1715
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   335
-- 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
   336
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
   337
local disabled_identities = set.new()
1715
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   338
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   339
local function identity_added(event)
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   340
	local source = event.source
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   341
	if disabled_modules:contains(source) then
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   342
		local item = event.item
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   343
		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
   344
		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
   345
		disabled_identities:add(item)
1715
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   346
		source:remove_item("identity", item)
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   347
	end
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   348
end
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   349
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   350
local function feature_added(event)
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   351
	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
   352
	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
   353
		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
   354
			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
   355
			source:remove_item("feature", item)
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   356
			disabled_modules:add(source)
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   357
			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
   358
				-- 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
   359
				-- 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
   360
				-- 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
   361
				-- FIXME: any better way ?
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   362
				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
   363
					identity_added({source=source, item=identity})
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   364
				end
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   365
			end
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   366
		end
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   367
	end
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   368
end
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   369
1720
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   370
local function extension_added(event)
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   371
	local source, stanza = event.source, event.item
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   372
	local form_type = find_form_type(stanza)
2756
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   373
	if not form_type then return end
1720
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   374
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   375
	for namespace, _ in pairs(ns_delegations) do
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   376
		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
   377
			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
   378
			source:remove_item("extension", stanza)
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   379
		end
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   380
	end
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   381
end
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   382
1715
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   383
-- for disco nesting (see § 7.2) we need to remove internal features
2756
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   384
-- we use handle_items as it allows to remove already added features
1715
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1714
diff changeset
   385
-- 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
   386
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
   387
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
   388
module:handle_items("extension", extension_added, function(_) end)
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   389
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   390
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   391
-- managing entity features/identities collection
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   392
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   393
local disco_error
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   394
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
   395
local bare_identities = {}
1720
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   396
local bare_extensions = {}
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   397
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   398
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
   399
	-- 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
   400
	-- 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
   401
	local session, stanza = event.origin, event.stanza
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   402
	if stanza.attr.to ~= module.host then
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   403
		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
   404
		return
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   405
	end
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   406
	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
   407
	module:unhook("iq-error/host/"..stanza.attr.id, disco_error)
2756
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   408
	local query = stanza:get_child("query", _DISCO_INFO_NS)
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   409
	if not query or not query.attr.node then
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   410
		session.send(st.error_reply(stanza, 'modify', 'not-acceptable'))
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   411
		return true
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   412
	end
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   413
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   414
	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
   415
	local main
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   416
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   417
	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
   418
		main=true
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   419
	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
   420
		main=false
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   421
	else
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   422
		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
   423
		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
   424
		return true
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   425
	end
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   426
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   427
	for feature in query:childtags("feature") do
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   428
		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
   429
		if main then
1721
e22cd2205fc1 mod_delegation: reset features/identities/extensions before doing a disco nesting
Goffi <goffi@goffi.org>
parents: 1720
diff changeset
   430
			module:add_feature(namespace)
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   431
		else
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   432
			bare_features:add(namespace)
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   433
		end
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   434
	end
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   435
	for identity in query:childtags("identity") do
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   436
		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
   437
		if main then
1721
e22cd2205fc1 mod_delegation: reset features/identities/extensions before doing a disco nesting
Goffi <goffi@goffi.org>
parents: 1720
diff changeset
   438
			module:add_identity(category, type_, name)
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   439
		else
1721
e22cd2205fc1 mod_delegation: reset features/identities/extensions before doing a disco nesting
Goffi <goffi@goffi.org>
parents: 1720
diff changeset
   440
			table.insert(bare_identities, {category=category, type=type_, name=name})
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   441
		end
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   442
	end
1720
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   443
	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
   444
		if main then
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   445
			module:add_extension(extension)
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   446
		else
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   447
			table.insert(bare_extensions, extension)
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   448
		end
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   449
	end
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   450
end
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   451
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   452
function disco_error(event)
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   453
	local stanza = event.stanza
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   454
	if stanza.attr.to ~= module.host then
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   455
		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
   456
		return
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   457
	end
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   458
	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
   459
	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
   460
	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
   461
	module:log("warn", "Ignoring disco nesting")
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   462
end
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   463
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   464
function disco_nest(namespace, entity_jid)
1721
e22cd2205fc1 mod_delegation: reset features/identities/extensions before doing a disco nesting
Goffi <goffi@goffi.org>
parents: 1720
diff changeset
   465
	-- manage discovery nesting (see § 7.2)
e22cd2205fc1 mod_delegation: reset features/identities/extensions before doing a disco nesting
Goffi <goffi@goffi.org>
parents: 1720
diff changeset
   466
e22cd2205fc1 mod_delegation: reset features/identities/extensions before doing a disco nesting
Goffi <goffi@goffi.org>
parents: 1720
diff changeset
   467
	-- first we reset the current values
e22cd2205fc1 mod_delegation: reset features/identities/extensions before doing a disco nesting
Goffi <goffi@goffi.org>
parents: 1720
diff changeset
   468
	if module.items then
e22cd2205fc1 mod_delegation: reset features/identities/extensions before doing a disco nesting
Goffi <goffi@goffi.org>
parents: 1720
diff changeset
   469
		module.items['feature'] = nil
e22cd2205fc1 mod_delegation: reset features/identities/extensions before doing a disco nesting
Goffi <goffi@goffi.org>
parents: 1720
diff changeset
   470
		module.items['identity'] = nil
e22cd2205fc1 mod_delegation: reset features/identities/extensions before doing a disco nesting
Goffi <goffi@goffi.org>
parents: 1720
diff changeset
   471
		module.items['extension'] = nil
e22cd2205fc1 mod_delegation: reset features/identities/extensions before doing a disco nesting
Goffi <goffi@goffi.org>
parents: 1720
diff changeset
   472
		bare_features = set.new()
e22cd2205fc1 mod_delegation: reset features/identities/extensions before doing a disco nesting
Goffi <goffi@goffi.org>
parents: 1720
diff changeset
   473
		bare_identities = {}
e22cd2205fc1 mod_delegation: reset features/identities/extensions before doing a disco nesting
Goffi <goffi@goffi.org>
parents: 1720
diff changeset
   474
		bare_extensions = {}
e22cd2205fc1 mod_delegation: reset features/identities/extensions before doing a disco nesting
Goffi <goffi@goffi.org>
parents: 1720
diff changeset
   475
	end
e22cd2205fc1 mod_delegation: reset features/identities/extensions before doing a disco nesting
Goffi <goffi@goffi.org>
parents: 1720
diff changeset
   476
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   477
	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
   478
		local node = prefix..namespace
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   479
3321
36a9170352b5 mod_delegation: Update to generate own iq ids
Matthew Wild <mwild1@gmail.com>
parents: 3275
diff changeset
   480
		local iq_id = new_id();
36a9170352b5 mod_delegation: Update to generate own iq ids
Matthew Wild <mwild1@gmail.com>
parents: 3275
diff changeset
   481
		local iq = st.iq({from=module.host, to=entity_jid, type='get', id = iq_id })
2756
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   482
			:tag('query', {xmlns=_DISCO_INFO_NS, node=node})
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   483
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   484
		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
   485
		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
   486
		module:send(iq)
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   487
	end
1716
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1715
diff changeset
   488
end
1717
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   489
2756
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   490
-- disco to bare jids special cases
1717
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   491
2756
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   492
-- disco#info
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   493
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   494
local function disco_hook(event)
1717
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   495
	-- 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
   496
	-- we get the final reply and filter delegated features/identities/extensions
2756
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   497
	local reply = event.reply
1717
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   498
	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
   499
		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
   500
			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
   501
			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
   502
				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
   503
					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
   504
					return nil
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   505
				end
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   506
			end
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   507
		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
   508
			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
   509
				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
   510
					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
   511
					-- 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
   512
					-- 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
   513
				then
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   514
					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
   515
					return nil
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   516
				end
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   517
			end
1720
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   518
		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
   519
			local form_type = find_form_type(child)
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   520
			if form_type then
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   521
				for namespace, _ in pairs(ns_delegations) do
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   522
					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
   523
						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
   524
						return nil
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   525
					end
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   526
				end
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   527
			end
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   528
1717
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   529
		end
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   530
		return child
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1716
diff changeset
   531
	end)
1718
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   532
	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
   533
		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
   534
	end
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   535
	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
   536
		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
   537
	end
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   538
	for _, stanza in ipairs(bare_extensions) do
29dfdfc767b4 mod_delegation: service discovery extensions (xep-0128) management
Goffi <goffi@goffi.org>
parents: 1719
diff changeset
   539
		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
   540
	end
3d83f5337a73 mod_delegation: disco info request on bare jid is now managed
Goffi <goffi@goffi.org>
parents: 1717
diff changeset
   541
2756
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   542
end
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   543
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   544
-- disco#items
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   545
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   546
local function disco_items_node_hook(event)
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   547
	-- check if node is not handled by server
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   548
	-- and forward the disco request to suitable entity
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   549
	if not event.exists then
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   550
		-- this node is not handled by the server
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   551
		local ns_data = ns_delegations[_DISCO_REMAINING]
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   552
		if ns_data ~= nil then
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   553
			-- remaining delegation is requested, we forward
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   554
			forward_iq(event.stanza, ns_data)
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   555
			-- and stop normal event handling
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   556
			return true
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   557
		end
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   558
	end
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   559
end
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   560
module:hook("account-disco-items-node", disco_items_node_hook, -2^32)
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   561
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   562
local function disco_items_hook(event)
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   563
	-- FIXME: we forward all bare-jid disco-items requests (without node) which will replace any Prosody reply
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   564
	--		for now it's OK because Prosody is not returning anything on request on bare jid
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   565
	--		but to be properly done, any Prosody reply should be kept and managing entities items should be added (merged) to it.
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   566
	--		account-disco-items can't be cancelled (return value of hooks are not checked in mod_disco), so corountine needs
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   567
	--		to be used with util.async (to get the IQ result, merge items then return from the event)
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   568
	local origin, stanza = event.origin, event.stanza;
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   569
	local node = stanza.tags[1].attr.node;
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   570
	local username = jid_split(stanza.attr.to) or origin.username;
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   571
	if not stanza.attr.to or is_contact_subscribed(username, module.host, jid_bare(stanza.attr.from)) then
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   572
		if node == nil or node == "" then
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   573
			local ns_data = ns_delegations[_DISCO_REMAINING]
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   574
			if ns_data ~= nil then
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   575
				forward_iq(event.stanza, ns_data)
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   576
				return true
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   577
			end
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   578
		end
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   579
	end
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   580
end
3275
7504f765e767 mod_delegation: Simplify iq handling by hooking on iq-get/ instead of iq/.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2762
diff changeset
   581
module:hook("iq-get/bare/http://jabber.org/protocol/disco#items:query", disco_items_hook, 100)
2756
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   582
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   583
local function disco_items_raw_hook(event)
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   584
	-- this method is called when account-disco-items-* event are not called
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   585
	-- notably when a disco-item is done by an unsubscibed entity
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   586
	-- (i.e. an entity doing a disco#item on an entity without having
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   587
	-- presence subscription)
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   588
	-- we forward the request to managing entity
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   589
	-- it's the responsability of the managing entity to filter the items
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   590
	local ns_data = ns_delegations[_DISCO_REMAINING]
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   591
	if ns_data ~= nil then
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   592
		forward_iq(event.stanza, ns_data)
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   593
		return true
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   594
	end
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   595
end
d0e75bf21d30 mod_delegation: added disco#items support
Goffi <goffi@goffi.org>
parents: 2073
diff changeset
   596
module:hook("iq-get/bare/http://jabber.org/protocol/disco#items:query", disco_items_raw_hook, -2^32)