annotate mod_delegation/mod_delegation.lua @ 1710:b68eed25b880

mod_delegation: iq forwarding to managing entity
author Goffi <goffi@goffi.org>
date Fri, 17 Apr 2015 21:05:08 +0200
parents 59ba224bf145
children 55b9ac807ac9
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'
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
28 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
29
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
30 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
31
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 --> Configuration management <--
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
34
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
35 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
36
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
37 local jid2ns = {}
1710
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
38 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
39 -- "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
40 ns_data.connected = nil
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
41 if ns_data.jid then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
42 if jid2ns[ns_data.jid] == nil then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
43 jid2ns[ns_data.jid] = {}
1709
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
44 end
1710
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
45 jid2ns[ns_data.jid][namespace] = ns_data
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
46 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
47 else
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
48 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
49 ns_delegations[namespace] = nil
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
50 end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
51 end
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
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
54 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
55 -- 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
56 -- as expained in § 4.2
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
57 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
58 :tag("delegation", {xmlns=_DELEGATION_NS})
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 -- 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
61 -- 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
62 -- 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
63 local have_delegation = false
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
64
1710
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
65 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
66 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
67 have_delegation = true
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
68 message:tag("delegated", {namespace=namespace})
1710
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
69 if type(ns_data.filtering) == "table" then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
70 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
71 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
72 end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
73 message:up()
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
74 end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
75 end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
76 end
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 if have_delegation then
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
79 session.send(message)
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
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
83 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
84 -- 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
85 -- 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
86 local function set_config(jid_)
1710
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
87 for _, ns_data in pairs(jid2ns[jid_]) do
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
88 if ns_data.connected == nil then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
89 ns_data.connected = entity_jid
1709
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
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
92 end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
93 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
94 set_config(bare_jid)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
95 -- 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
96 -- 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
97 -- 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
98 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
99 set_config(entity_jid)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
100 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
101 end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
102 end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
103
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
104 local function on_presence(event)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
105 local session = event.origin
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
106 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
107
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
108 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
109 set_connected(session.full_jid)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
110 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
111 end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
112 end
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
113
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
114 local function on_component_connected(event)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
115 -- 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
116 -- /!\ 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
117 -- 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
118 local session = event.session
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
119 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
120
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
121 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
122 if jid_delegations ~= nil then
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
123 set_connected(bare_jid)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
124 advertise_delegations(session, bare_jid)
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 end
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 function on_component_auth(event)
59ba224bf145 mod_delegation: XEP-0355 implementation, first draft (configuration management + delegations advertising)
Goffi <goffi@goffi.org>
parents:
diff changeset
129 -- 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
130 -- 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
131 -- 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
132 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
133 callback(event)
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 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
138 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
139 module:hook('presence/initial', on_presence)
1710
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
140
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
141
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
142 --> delegated namespaces hook <--
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
143
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
144 local function managing_ent_result(event)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
145 -- 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
146 -- 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
147 -- result to the managed entity
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
148 local session, stanza = event.origin, event.stanza
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
149 if stanza.attr.to ~= module.host then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
150 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
151 return
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
152 end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
153 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
154
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
155 -- lot of checks to do...
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
156 local delegation = stanza.tags[1]
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
157 if #stanza ~= 1 or delegation.name ~= "delegation" or
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
158 delegation.attr.xmlns ~= _DELEGATION_NS then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
159 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
160 return true
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
161 end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
162
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
163 local forwarded = delegation.tags[1]
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
164 if #delegation ~= 1 or forwarded.name ~= "forwarded" or
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
165 forwarded.attr.xmlns ~= _FORWARDED_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 iq = forwarded.tags[1]
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
171 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
172 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
173 return true
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
174 end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
175
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
176 local namespace = iq.tags[1].xmlns
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
177 local ns_data = ns_delegations[namespace]
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
178 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
179
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
180 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
181 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
182 session.send(st.error_reply(stanza, 'auth', 'forbidden'))
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
183 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
184 return true
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
185 end
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 -- at this point eveything is checked,
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
188 -- 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
189 module:send(iq)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
190 end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
191
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
192 local function forward_iq(stanza, ns_data)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
193 local to_jid = ns_data.connected
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
194 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
195 :tag("delegation", { xmlns=_DELEGATION_NS })
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
196 :tag("forwarded", { xmlns=_FORWARDED_NS })
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
197 :add_child(stanza)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
198 local iq_id = iq_stanza.attr.id
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
199 -- 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
200 ns_data[_ORI_ID_PREFIX..iq_id] = stanza
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
201 module:log("debug", "stanza forwarded to "..to_jid..": "..tostring(iq_stanza))
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
202 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
203 module:send(iq_stanza)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
204 end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
205
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
206 local function iq_hook(event)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
207 -- 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
208 -- 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
209 -- 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
210 -- 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
211 local session, stanza = event.origin, event.stanza
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
212 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
213 local namespace = stanza.tags[1].attr.xmlns
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
214 local ns_data = ns_delegations[namespace]
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
215
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
216 if ns_data then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
217 module:log("debug", "Namespace %s is delegated", namespace)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
218 if ns_data.filtering then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
219 local first_child = stanza.tags[1]
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
220 for _, attribute in ns_data.filtering do
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
221 -- if any filtered attribute if not present,
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
222 -- we must continue the normal bahaviour
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
223 if not first_child.attr[attribute] then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
224 module:log("debug", "Filtered attribute %s not present, doing normal workflow", attribute)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
225 return;
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
226 end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
227 end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
228 end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
229 if not ns_data.connected then
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
230 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
231 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
232 else
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
233 local managing_entity = ns_data.connected
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
234 module:log("debug", "Entity %s is managing %s", managing_entity, namespace)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
235 forward_iq(stanza, ns_data)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
236 end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
237 return true
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
238 else
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
239 -- we have no delegation, we continue normal behaviour
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
240 return
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
241 end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
242 end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
243 end
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
244
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
245 module:hook("iq/self", iq_hook, 2^32)
b68eed25b880 mod_delegation: iq forwarding to managing entity
Goffi <goffi@goffi.org>
parents: 1709
diff changeset
246 module:hook("iq/host", iq_hook, 2^32)