Annotate

mod_delegation/mod_delegation.lua @ 1713:01e9465f8f80

mod_delegation: delegated features/identities removal for disco info requests on bare jid
author Goffi <goffi@goffi.org>
date Fri, 17 Apr 2015 21:06:51 +0200
parent 1712:c1973605d096
child 1714:3d83f5337a73
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1709
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
1 -- XEP-0355 (Namespace Delegation)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
2 -- Copyright (C) 2015 Jérôme Poisson
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
3 --
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
4 -- This module is MIT/X11 licensed. Please see the
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
5 -- COPYING file in the source package for more information.
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
6
1710
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
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: 1709
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: 1709
diff changeset
9
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
10 -- TODO: client mode, managing entity error handling
1709
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
11
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
12 local jid = require("util/jid")
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
13 local st = require("util/stanza")
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
14 local set = require("util/set")
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
15
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
16 local delegation_session = module:shared("/*/delegation/session")
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
17
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
18 if delegation_session.connected_cb == nil then
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
19 -- set used to have connected event listeners
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
20 -- which allow a host to react on events from
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
21 -- other hosts
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
22 delegation_session.connected_cb = set.new()
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
23 end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
24 local connected_cb = delegation_session.connected_cb
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
25
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
26 local _DELEGATION_NS = 'urn:xmpp:delegation:1'
1710
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
27 local _FORWARDED_NS = 'urn:xmpp:forward:0'
1712
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
28 local _DISCO_NS = 'http://jabber.org/protocol/disco#info'
1710
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
29 local _ORI_ID_PREFIX = "IQ_RESULT_"
1709
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
30
1712
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
31 local _MAIN_SEP = '::'
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
32 --local _BARE_SEP = ':bare:'
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
33
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
34 local disco_nest
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
35
1709
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
36 module:log("debug", "Loading namespace delegation module ");
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
37
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
38
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
39 --> Configuration management <--
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
40
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
41 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
42
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
43 local jid2ns = {}
1710
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
44 for namespace, ns_data in pairs(ns_delegations) do
1709
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
45 -- "connected" contain the full jid of connected managing entity
1710
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
46 ns_data.connected = nil
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
47 if ns_data.jid then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
48 if jid2ns[ns_data.jid] == nil then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
49 jid2ns[ns_data.jid] = {}
1709
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
50 end
1710
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
51 jid2ns[ns_data.jid][namespace] = ns_data
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
52 module:log("debug", "Namespace %s is delegated%s to %s", namespace, ns_data.filtering and " (with filtering)" or "", ns_data.jid)
1709
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
53 else
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
54 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
55 ns_delegations[namespace] = nil
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
56 end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
57 end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
58
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
59
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
60 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
61 -- 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
62 -- as expained in § 4.2
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
63 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
64 :tag("delegation", {xmlns=_DELEGATION_NS})
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
65
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
66 -- 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
67 -- 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
68 -- 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
69 local have_delegation = false
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
70
1710
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
71 for namespace, ns_data in pairs(jid2ns[to_jid]) do
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
72 if ns_data.connected == to_jid then
1709
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
73 have_delegation = true
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
74 message:tag("delegated", {namespace=namespace})
1710
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
75 if type(ns_data.filtering) == "table" then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
76 for _, attribute in pairs(ns_data.filtering) do
1709
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
77 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
78 end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
79 message:up()
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
80 end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
81 end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
82 end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
83
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
84 if have_delegation then
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
85 session.send(message)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
86 end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
87 end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
88
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
89 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
90 -- 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
91 -- 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
92 local function set_config(jid_)
1712
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
93 for namespace, ns_data in pairs(jid2ns[jid_]) do
1710
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
94 if ns_data.connected == nil then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
95 ns_data.connected = entity_jid
1712
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
96 disco_nest(namespace, entity_jid)
1709
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
97 end
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 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
101 set_config(bare_jid)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
102 -- 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
103 -- 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
104 -- 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
105 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
106 set_config(entity_jid)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
107 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
108 end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
109 end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
110
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
111 local function on_presence(event)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
112 local session = event.origin
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
113 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
114
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
115 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
116 set_connected(session.full_jid)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
117 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
118 end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
119 end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
120
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
121 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
122 -- 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
123 -- /!\ 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
124 -- 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
125 local session = event.session
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
126 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
127
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
128 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
129 if jid_delegations ~= nil then
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
130 set_connected(bare_jid)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
131 advertise_delegations(session, bare_jid)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
132 end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
133 end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
134
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
135 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
136 -- 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
137 -- 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
138 -- 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
139 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
140 callback(event)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
141 end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
142 end
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 connected_cb:add(on_component_connected)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
145 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
146 module:hook('presence/initial', on_presence)
1710
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
147
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
148
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
149 --> delegated namespaces hook <--
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
150
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
151 local function managing_ent_result(event)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
152 -- this function manage iq results from the managing entity
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
153 -- it do a couple of security check before sending the
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
154 -- result to the managed entity
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
155 local session, stanza = event.origin, event.stanza
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
156 if stanza.attr.to ~= module.host then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
157 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: 1709
diff changeset
158 return
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
159 end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
160 module:unhook("iq-result/host/"..stanza.attr.id, managing_ent_result)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
161
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
162 -- lot of checks to do...
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
163 local delegation = stanza.tags[1]
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
164 if #stanza ~= 1 or delegation.name ~= "delegation" or
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
165 delegation.attr.xmlns ~= _DELEGATION_NS then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
166 session.send(st.error_reply(stanza, 'modify', 'not-acceptable'))
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
167 return true
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
168 end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
169
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
170 local forwarded = delegation.tags[1]
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
171 if #delegation ~= 1 or forwarded.name ~= "forwarded" or
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
172 forwarded.attr.xmlns ~= _FORWARDED_NS then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
173 session.send(st.error_reply(stanza, 'modify', 'not-acceptable'))
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
174 return true
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
175 end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
176
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
177 local iq = forwarded.tags[1]
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
178 if #forwarded ~= 1 or iq.name ~= "iq" or #iq ~= 1 then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
179 session.send(st.error_reply(stanza, 'modify', 'not-acceptable'))
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
180 return true
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
181 end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
182
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
183 local namespace = iq.tags[1].xmlns
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
184 local ns_data = ns_delegations[namespace]
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
185 local original = ns_data[_ORI_ID_PREFIX..stanza.attr.id]
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
186
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
187 if stanza.attr.from ~= ns_data.connected or iq.attr.type ~= "result" or
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
188 iq.attr.id ~= original.attr.id or iq.attr.to ~= original.attr.from then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
189 session.send(st.error_reply(stanza, 'auth', 'forbidden'))
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
190 module:send(st.error_reply(original, 'cancel', 'service-unavailable'))
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
191 return true
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
192 end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
193
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
194 -- at this point eveything is checked,
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
195 -- and we (hopefully) can send the the result safely
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
196 module:send(iq)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
197 end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
198
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
199 local function forward_iq(stanza, ns_data)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
200 local to_jid = ns_data.connected
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
201 local iq_stanza = st.iq({ from=module.host, to=to_jid, type="set" })
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
202 :tag("delegation", { xmlns=_DELEGATION_NS })
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
203 :tag("forwarded", { xmlns=_FORWARDED_NS })
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
204 :add_child(stanza)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
205 local iq_id = iq_stanza.attr.id
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
206 -- we save the original stanza to check the managing entity result
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
207 ns_data[_ORI_ID_PREFIX..iq_id] = stanza
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
208 module:hook("iq-result/host/"..iq_id, managing_ent_result)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
209 module:send(iq_stanza)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
210 end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
211
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
212 local function iq_hook(event)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
213 -- general hook for all the iq which forward delegated ones
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
214 -- and continue normal behaviour else. If a namespace is
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
215 -- delegated but managing entity is offline, a service-unavailable
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
216 -- error will be sent, as requested by the XEP
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
217 local session, stanza = event.origin, event.stanza
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
218 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: 1709
diff changeset
219 local namespace = stanza.tags[1].attr.xmlns
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
220 local ns_data = ns_delegations[namespace]
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
221
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
222 if ns_data then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
223 if ns_data.filtering then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
224 local first_child = stanza.tags[1]
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
225 for _, attribute in ns_data.filtering do
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
226 -- if any filtered attribute if not present,
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
227 -- we must continue the normal bahaviour
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
228 if not first_child.attr[attribute] then
1711
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
229 -- Filtered attribute is not present, we do normal workflow
1710
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
230 return;
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
231 end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
232 end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
233 end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
234 if not ns_data.connected then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
235 module:log("warn", "No connected entity to manage "..namespace)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
236 session.send(st.error_reply(stanza, 'cancel', 'service-unavailable'))
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
237 else
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
238 forward_iq(stanza, ns_data)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
239 end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
240 return true
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
241 else
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
242 -- we have no delegation, we continue normal behaviour
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
243 return
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
244 end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
245 end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
246 end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
247
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
248 module:hook("iq/self", iq_hook, 2^32)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
249 module:hook("iq/host", iq_hook, 2^32)
1711
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
250
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
251
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
252 --> discovery nesting <--
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
253
1712
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
254 -- disabling internal features/identities
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
255
1711
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
256 -- modules whose features/identities are managed by delegation
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
257 local disabled_modules = set.new()
1713
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1712
diff changeset
258 local disabled_identities = set.new()
1711
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
259
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
260 local function identity_added(event)
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
261 local source = event.source
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
262 if disabled_modules:contains(source) then
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
263 local item = event.item
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
264 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: 1710
diff changeset
265 module:log("debug", "Removing (%s/%s%s) identity because of delegation", category, type_, name and "/"..name or "")
1713
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1712
diff changeset
266 disabled_identities:add(item)
1711
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
267 source:remove_item("identity", item)
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
268 end
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
269 end
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
270
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
271 local function feature_added(event)
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
272 local source, item = event.source, event.item
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
273 for namespace, _ in pairs(ns_delegations) do
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
274 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: 1710
diff changeset
275 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: 1710
diff changeset
276 source:remove_item("feature", item)
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
277 disabled_modules:add(source)
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
278 if source.items and source.items.identity then
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
279 -- we remove all identities added by the source module
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
280 -- 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: 1710
diff changeset
281 -- 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: 1710
diff changeset
282 -- FIXME: any better way ?
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
283 for _, identity in pairs(source.items.identity) do
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
284 identity_added({source=source, item=identity})
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
285 end
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
286 end
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
287 end
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
288 end
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
289 end
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
290
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
291 -- for disco nesting (see § 7.2) we need to remove internal features
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
292 -- we use handle_items as it allow to remove already added features
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
293 -- and catch the ones which can come later
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
294 module:handle_items("feature", feature_added, function(_) end)
55b9ac807ac9 mod_delegation: delegated features/identities are removed from disco.
Goffi <goffi@goffi.org>
parents: 1710
diff changeset
295 module:handle_items("identity", identity_added, function(_) end, false)
1712
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
296
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
297
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
298 -- managing entity features/identities collection
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
299
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
300 local disco_main_error
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
301
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
302 local function disco_main_result(event)
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
303 local session, stanza = event.origin, event.stanza
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
304 if stanza.attr.to ~= module.host then
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
305 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: 1711
diff changeset
306 return
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
307 end
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
308 module:unhook("iq-result/host/"..stanza.attr.id, disco_main_result)
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
309 module:unhook("iq-error/host/"..stanza.attr.id, disco_main_error)
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
310 local query = stanza:get_child("query", _DISCO_NS)
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
311 if not query or not query.attr.node then
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
312 session.send(st.error_reply(stanza, 'modify', 'not-acceptable'))
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
313 return true
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
314 end
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
315 -- local node = query.attr.node
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
316 for feature in query:childtags("feature") do
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
317 local namespace = feature.attr.var
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
318 if not module:has_feature(namespace) then -- we avoid doubling features in case of disconnection/reconnexion
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
319 module:add_feature(namespace)
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
320 end
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
321 end
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
322 for identity in query:childtags("identity") do
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
323 local category, type_, name = identity.attr.category, identity.attr.type, identity.attr.name
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
324 if not module:has_identity(category, type_, name) then
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
325 module:add_identity(category, type_, name)
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
326 end
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
327 end
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
328 end
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
329
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
330 function disco_main_error(event)
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
331 local stanza = event.stanza
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
332 if stanza.attr.to ~= module.host then
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
333 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: 1711
diff changeset
334 return
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
335 end
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
336 module:unhook("iq-result/host/"..stanza.attr.id, disco_main_result)
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
337 module:unhook("iq-error/host/"..stanza.attr.id, disco_main_error)
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
338 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: 1711
diff changeset
339 module:log("warn", "Ignoring disco nesting")
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
340 end
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
341
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
342 function disco_nest(namespace, entity_jid)
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
343 local main_node = _DELEGATION_NS.._MAIN_SEP..namespace
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
344 -- local bare_node = _DELEGATION_NS.._BARE_SEP..namespace
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
345
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
346 local iq = st.iq({from=module.host, to=entity_jid, type='get'})
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
347 :tag('query', {xmlns=_DISCO_NS, node=main_node})
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
348
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
349 local iq_id = iq.attr.id
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
350
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
351 module:hook("iq-result/host/"..iq_id, disco_main_result)
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
352 module:hook("iq-error/host/"..iq_id, disco_main_error)
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
353 module:send(iq)
c1973605d096 mod_delegation: disco nesting for host
Goffi <goffi@goffi.org>
parents: 1711
diff changeset
354 end
1713
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1712
diff changeset
355
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1712
diff changeset
356 -- disco to bare jids special case
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1712
diff changeset
357
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1712
diff changeset
358 module:hook("account-disco-info", function(event)
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1712
diff changeset
359 -- this event is called when a disco info request is done on a bare jid
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1712
diff changeset
360 -- we get the final reply and filter delegated features/identities
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1712
diff changeset
361 local reply = event.reply;
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1712
diff changeset
362 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: 1712
diff changeset
363 if child.name == 'feature' then
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1712
diff changeset
364 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: 1712
diff changeset
365 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: 1712
diff changeset
366 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: 1712
diff changeset
367 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: 1712
diff changeset
368 return nil
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1712
diff changeset
369 end
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1712
diff changeset
370 end
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1712
diff changeset
371 elseif child.name == 'identity' then
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1712
diff changeset
372 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: 1712
diff changeset
373 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: 1712
diff changeset
374 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: 1712
diff changeset
375 -- 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: 1712
diff changeset
376 -- 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: 1712
diff changeset
377 then
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1712
diff changeset
378 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: 1712
diff changeset
379 return nil
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1712
diff changeset
380 end
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1712
diff changeset
381 end
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1712
diff changeset
382 end
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1712
diff changeset
383 return child
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1712
diff changeset
384 end)
01e9465f8f80 mod_delegation: delegated features/identities removal for disco info requests on bare jid
Goffi <goffi@goffi.org>
parents: 1712
diff changeset
385 end, -2^32);