Mercurial > prosody-modules
annotate mod_pubsub_serverinfo/mod_pubsub_serverinfo.lua @ 5803:f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
This commit replaces the earlier proof-of-concept to a solution that:
- reports on remotely-connected domains
- uses disco/info to detect if those domains opt-in
- publishes domain names for remote domains that do so
- caches the disco/info response
author | Guus der Kinderen <guus.der.kinderen@gmail.com> |
---|---|
date | Wed, 03 Jan 2024 23:05:14 +0100 |
parents | 73887dcb2129 |
children | c3eeeb968403 |
rev | line source |
---|---|
5803
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
1 local st = require "util.stanza"; |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
2 local new_id = require"util.id".medium; |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
3 local dataform = require "util.dataforms".new; |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
4 |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
5 local local_domain = module:get_host(); |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
6 local service = module:get_option(module.name .. "_service") or "pubsub." .. local_domain; |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
7 local node = module:get_option(module.name .. "_node") or "serverinfo"; |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
8 local actor = module.host .. "/modules/" .. module.name; |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
9 local publication_interval = module:get_option(module.name .. "_publication_interval") or 300; |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
10 |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
11 local opt_in_reports |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
12 |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
13 function module.load() |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
14 -- Will error out with a 'conflict' if the node already exists. TODO: create the node only when it's missing. |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
15 create_node():next() |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
16 |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
17 module:add_feature("urn:xmpp:serverinfo:0"); |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
18 |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
19 module:add_extension(dataform { |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
20 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/network/serverinfo" }, |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
21 { name = "serverinfo-pubsub-node", type = "text-single" }, |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
22 }:form({ ["serverinfo-pubsub-node"] = ("xmpp:%s?;node=%s"):format(service, node) }, "result")); |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
23 |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
24 module:add_timer(10, publish_serverinfo); |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
25 end |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
26 |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
27 function module.unload() |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
28 -- This removes all subscribers, which may or may not be desirable, depending on the reason for the unload. |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
29 delete_node(); -- Should this block, to delay unload() until the node is deleted? |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
30 end |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
31 |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
32 -- Returns a promise |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
33 function create_node() |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
34 local request = st.iq({ type = "set", to = service, from = actor, id = new_id() }) |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
35 :tag("pubsub", { xmlns = "http://jabber.org/protocol/pubsub" }) |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
36 :tag("create", { node = node }):up() |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
37 :tag("configure") |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
38 :tag("x", { xmlns = "jabber:x:data", type = "submit" }) |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
39 :tag("field", { var = "FORM_TYPE", type = "hidden"}) |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
40 :text_tag("value", "http://jabber.org/protocol/pubsub#node_config") |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
41 :up() |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
42 :tag("field", { var = "pubsub#max_items" }) |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
43 :text_tag("value", "1") |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
44 :up() |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
45 :tag("field", { var = "pubsub#persist_items" }) |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
46 :text_tag("value", "0") |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
47 return module:send_iq(request); |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
48 end |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
49 |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
50 -- Returns a promise |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
51 function delete_node() |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
52 local request = st.iq({ type = "set", to = service, from = actor, id = new_id() }) |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
53 :tag("pubsub", { xmlns = "http://jabber.org/protocol/pubsub" }) |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
54 :tag("delete", { node = node }); |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
55 |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
56 return module:send_iq(request); |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
57 end |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
58 |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
59 function publish_serverinfo() |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
60 -- Iterate over s2s sessions, adding them to a multimap, where the key is the local domain name, |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
61 -- mapped to a collection of remote domain names. De-duplicate all remote domain names by using |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
62 -- them as an index in a table. |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
63 local domains_by_host = {} |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
64 for session, _ in pairs(prosody.incoming_s2s) do |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
65 if session ~= nil and session.from_host ~= nil and local_domain == session.to_host then |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
66 local sessions = domains_by_host[session.to_host] |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
67 if sessions == nil then sessions = {} end; -- instantiate a new entry if none existed |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
68 sessions[session.from_host] = true |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
69 domains_by_host[session.to_host] = sessions |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
70 end |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
71 end |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
72 |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
73 -- At an earlier stage, the code iterated voer all prosody.hosts - but that turned out to be to noisy. |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
74 -- for host, data in pairs(prosody.hosts) do |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
75 local host = local_domain |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
76 local data = prosody.hosts[host] |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
77 if data ~= nil then |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
78 local sessions = domains_by_host[host] |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
79 if sessions == nil then sessions = {} end; -- instantiate a new entry if none existed |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
80 if data.s2sout ~= nil then |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
81 for _, session in pairs(data.s2sout) do |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
82 if session.to_host ~= nil then |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
83 sessions[session.to_host] = true |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
84 domains_by_host[host] = sessions |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
85 end |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
86 end |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
87 end |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
88 end |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
89 |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
90 -- Build the publication stanza. |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
91 local request = st.iq({ type = "set", to = service, from = actor, id = new_id() }) |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
92 :tag("pubsub", { xmlns = "http://jabber.org/protocol/pubsub" }) |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
93 :tag("publish", { node = node, xmlns = "http://jabber.org/protocol/pubsub" }) |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
94 :tag("item", { id = "current", xmlns = "http://jabber.org/protocol/pubsub" }) |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
95 :tag("serverinfo", { xmlns = "urn:xmpp:serverinfo:0" }) |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
96 |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
97 request:tag("domain", { name = local_domain }) |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
98 :tag("federation") |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
99 |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
100 local remotes = domains_by_host[host] |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
101 |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
102 if remotes ~= nil then |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
103 for remote, _ in pairs(remotes) do |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
104 -- include a domain name for remote domains, but only if they advertise support. |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
105 if does_opt_in(remote) then |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
106 request:tag("remote-domain", { name = remote }):up() |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
107 else |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
108 request:tag("remote-domain"):up() |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
109 end |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
110 end |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
111 end |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
112 |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
113 request:up():up() |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
114 |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
115 module:send_iq(request):next() |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
116 |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
117 return publication_interval; |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
118 end |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
119 |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
120 local opt_in_cache = {} |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
121 |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
122 function does_opt_in(remoteDomain) |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
123 |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
124 -- try to read answer from cache. |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
125 local cached_value = opt_in_cache[remoteDomain] |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
126 if cached_value ~= nil and os.difftime(cached_value.expires, os.time()) > 0 then |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
127 return cached_value.opt_in; |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
128 end |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
129 |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
130 -- TODO worry about not having multiple requests in flight to the same domain.cached_value |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
131 |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
132 -- Cache could not provide an answer. Perform service discovery. |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
133 local discoRequest = st.iq({ type = "get", to = remoteDomain, from = actor, id = new_id() }) |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
134 :tag("query", { xmlns = "http://jabber.org/protocol/disco#info" }) |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
135 |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
136 module:send_iq(discoRequest):next( |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
137 function(response) |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
138 if response.stanza ~= nil and response.stanza.attr.type == "result" then |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
139 local query = response.stanza:get_child("query", "http://jabber.org/protocol/disco#info") |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
140 if query ~= nil then |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
141 for feature in query:childtags("feature", "http://jabber.org/protocol/disco#info") do |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
142 if feature.attr.var == 'urn:xmpp:serverinfo:0' then |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
143 opt_in_cache[remoteDomain] = { |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
144 opt_in = true; |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
145 expires = os.time() + 3600; |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
146 } |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
147 return; -- prevent 'false' to be cached, down below. |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
148 end |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
149 end |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
150 end |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
151 end |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
152 opt_in_cache[remoteDomain] = { |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
153 opt_in = false; |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
154 expires = os.time() + 3600; |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
155 } |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
156 end, |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
157 function(response) |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
158 opt_in_cache[remoteDomain] = { |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
159 opt_in = false; |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
160 expires = os.time() + 3600; |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
161 } |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
162 end |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
163 ); |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
164 |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
165 -- return 'false' for now. Better luck next time... |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
166 return false; |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
167 |
f55e65315ba0
mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents:
5801
diff
changeset
|
168 end |