Annotate

mod_pubsub_serverinfo/mod_pubsub_serverinfo.lua @ 5941:5c4e102e2563 default tip

mod_pubsub_serverinfo: fix bool logic when reading config
author Guus der Kinderen <guus.der.kinderen@gmail.com>
date Mon, 03 Jun 2024 12:52:26 +0200
parent 5910:2c85397d7241
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5840
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
1 local http = require "net.http";
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
2 local json = require "util.json";
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
3 local st = require "util.stanza";
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
4 local new_id = require"util.id".medium;
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
5
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
6 local local_domain = module:get_host();
5908
49b0873932ca mod_pubsub_serverinfo: Don't default to non-local pubsub servers (thanks roughnecks)
Matthew Wild <mwild1@gmail.com>
parents: 5867
diff changeset
7 local service = module:get_option_string(module.name .. "_service");
5910
2c85397d7241 mod_pubsub_serverinfo: node is a string, not a number
Matthew Wild <mwild1@gmail.com>
parents: 5909
diff changeset
8 local node = module:get_option_string(module.name .. "_node", "serverinfo");
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
9 local actor = module.host .. "/modules/" .. module.name;
5909
54b451c3790c mod_pubsub_serverinfo: Fix for compatibility with 0.12 option getters
Matthew Wild <mwild1@gmail.com>
parents: 5908
diff changeset
10 local publication_interval = module:get_option_number(module.name .. "_publication_interval", 300);
54b451c3790c mod_pubsub_serverinfo: Fix for compatibility with 0.12 option getters
Matthew Wild <mwild1@gmail.com>
parents: 5908
diff changeset
11 local cache_ttl = module:get_option_number(module.name .. "_cache_ttl", 3600);
5840
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
12 local public_providers_url = module:get_option_string(module.name.."_public_providers_url", "https://data.xmpp.net/providers/v2/providers-Ds.json");
5839
45d0802d0787 mod_pubsub_serverinfo: Allow configuration of node persistence/deletion
Matthew Wild <mwild1@gmail.com>
parents: 5838
diff changeset
13 local delete_node_on_unload = module:get_option_boolean(module.name.."_delete_node_on_unload", false);
45d0802d0787 mod_pubsub_serverinfo: Allow configuration of node persistence/deletion
Matthew Wild <mwild1@gmail.com>
parents: 5838
diff changeset
14 local persist_items = module:get_option_boolean(module.name.."_persist_items", true);
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
15
5908
49b0873932ca mod_pubsub_serverinfo: Don't default to non-local pubsub servers (thanks roughnecks)
Matthew Wild <mwild1@gmail.com>
parents: 5867
diff changeset
16 if not service and prosody.hosts["pubsub."..module.host] then
49b0873932ca mod_pubsub_serverinfo: Don't default to non-local pubsub servers (thanks roughnecks)
Matthew Wild <mwild1@gmail.com>
parents: 5867
diff changeset
17 service = "pubsub."..module.host;
5941
5c4e102e2563 mod_pubsub_serverinfo: fix bool logic when reading config
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5910
diff changeset
18 end
5c4e102e2563 mod_pubsub_serverinfo: fix bool logic when reading config
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5910
diff changeset
19 if not service
5908
49b0873932ca mod_pubsub_serverinfo: Don't default to non-local pubsub servers (thanks roughnecks)
Matthew Wild <mwild1@gmail.com>
parents: 5867
diff changeset
20 module:log_status("warn", "No pubsub service specified - module not activated");
49b0873932ca mod_pubsub_serverinfo: Don't default to non-local pubsub servers (thanks roughnecks)
Matthew Wild <mwild1@gmail.com>
parents: 5867
diff changeset
21 return;
49b0873932ca mod_pubsub_serverinfo: Don't default to non-local pubsub servers (thanks roughnecks)
Matthew Wild <mwild1@gmail.com>
parents: 5867
diff changeset
22 end
49b0873932ca mod_pubsub_serverinfo: Don't default to non-local pubsub servers (thanks roughnecks)
Matthew Wild <mwild1@gmail.com>
parents: 5867
diff changeset
23
5838
77c5709bd57a mod_pubsub_serverinfo: Add explicit xmlns to all pubsub tags
Matthew Wild <mwild1@gmail.com>
parents: 5837
diff changeset
24 local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
25
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
26 function module.load()
5827
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
27 discover_node():next(
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
28 function(exists)
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
29 if not exists then create_node() end
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
30 end
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
31 ):catch(
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
32 function(error)
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
33 module:log("warn", "Error prevented discovery or creation of pub/sub node at %s: %s", service, error)
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
34 end
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
35 )
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
36
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
37 module:add_feature("urn:xmpp:serverinfo:0");
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
38
5867
79ae71f52c81 mod_pubsub_serverinfo: Update to use mod_server_info (fixes #1841)
Matthew Wild <mwild1@gmail.com>
parents: 5840
diff changeset
39 module:add_item("server-info-fields", {
79ae71f52c81 mod_pubsub_serverinfo: Update to use mod_server_info (fixes #1841)
Matthew Wild <mwild1@gmail.com>
parents: 5840
diff changeset
40 { name = "serverinfo-pubsub-node", type = "text-single", value = ("xmpp:%s?;node=%s"):format(service, node) };
79ae71f52c81 mod_pubsub_serverinfo: Update to use mod_server_info (fixes #1841)
Matthew Wild <mwild1@gmail.com>
parents: 5840
diff changeset
41 });
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
42
5836
d38772479891 mod_pubsub_serverinfo: Some logging improvements
Matthew Wild <mwild1@gmail.com>
parents: 5835
diff changeset
43 if cache_ttl < publication_interval then
d38772479891 mod_pubsub_serverinfo: Some logging improvements
Matthew Wild <mwild1@gmail.com>
parents: 5835
diff changeset
44 module:log("warn", "It is recommended to have a cache interval higher than the publication interval");
d38772479891 mod_pubsub_serverinfo: Some logging improvements
Matthew Wild <mwild1@gmail.com>
parents: 5835
diff changeset
45 end
d38772479891 mod_pubsub_serverinfo: Some logging improvements
Matthew Wild <mwild1@gmail.com>
parents: 5835
diff changeset
46
5833
76b57bcfe1b2 mod_pubsub_serverinfo: Warm-up opt-in cache
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5832
diff changeset
47 cache_warm_up()
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
48 module:add_timer(10, publish_serverinfo);
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
49 end
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
50
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
51 function module.unload()
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
52 -- This removes all subscribers, which may or may not be desirable, depending on the reason for the unload.
5839
45d0802d0787 mod_pubsub_serverinfo: Allow configuration of node persistence/deletion
Matthew Wild <mwild1@gmail.com>
parents: 5838
diff changeset
53 if delete_node_on_unload then
45d0802d0787 mod_pubsub_serverinfo: Allow configuration of node persistence/deletion
Matthew Wild <mwild1@gmail.com>
parents: 5838
diff changeset
54 delete_node();
45d0802d0787 mod_pubsub_serverinfo: Allow configuration of node persistence/deletion
Matthew Wild <mwild1@gmail.com>
parents: 5838
diff changeset
55 end
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
56 end
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
57
5827
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
58 -- Returns a promise of a boolean
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
59 function discover_node()
5833
76b57bcfe1b2 mod_pubsub_serverinfo: Warm-up opt-in cache
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5832
diff changeset
60 local request = st.iq({ type = "get", to = service, from = actor, id = new_id() })
76b57bcfe1b2 mod_pubsub_serverinfo: Warm-up opt-in cache
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5832
diff changeset
61 :tag("query", { xmlns = "http://jabber.org/protocol/disco#items" })
5827
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
62
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
63 module:log("debug", "Sending request to discover existence of pub/sub node '%s' at %s", node, service)
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
64 return module:send_iq(request):next(
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
65 function(response)
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
66 if response.stanza == nil or response.stanza.attr.type ~= "result" then
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
67 module:log("warn", "Unexpected response to service discovery items request at %s: %s", service, response.stanza)
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
68 return false
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
69 end
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
70
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
71 local query = response.stanza:get_child("query", "http://jabber.org/protocol/disco#items")
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
72 if query ~= nil then
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
73 for item in query:childtags("item") do
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
74 if item.attr.jid == service and item.attr.node == node then
5836
d38772479891 mod_pubsub_serverinfo: Some logging improvements
Matthew Wild <mwild1@gmail.com>
parents: 5835
diff changeset
75 module:log("debug", "pub/sub node '%s' at %s does exist.", node, service)
5827
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
76 return true
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
77 end
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
78 end
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
79 end
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
80 module:log("debug", "pub/sub node '%s' at %s does not exist.", node, service)
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
81 return false;
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
82 end
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
83 );
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
84 end
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
85
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
86 -- Returns a promise of a boolean
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
87 function create_node()
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
88 local request = st.iq({ type = "set", to = service, from = actor, id = new_id() })
5838
77c5709bd57a mod_pubsub_serverinfo: Add explicit xmlns to all pubsub tags
Matthew Wild <mwild1@gmail.com>
parents: 5837
diff changeset
89 :tag("pubsub", { xmlns = xmlns_pubsub })
77c5709bd57a mod_pubsub_serverinfo: Add explicit xmlns to all pubsub tags
Matthew Wild <mwild1@gmail.com>
parents: 5837
diff changeset
90 :tag("create", { node = node, xmlns = xmlns_pubsub }):up()
77c5709bd57a mod_pubsub_serverinfo: Add explicit xmlns to all pubsub tags
Matthew Wild <mwild1@gmail.com>
parents: 5837
diff changeset
91 :tag("configure", { xmlns = xmlns_pubsub })
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
92 :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: 5824
diff changeset
93 :tag("field", { var = "FORM_TYPE", type = "hidden"})
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
94 :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: 5824
diff changeset
95 :up()
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
96 :tag("field", { var = "pubsub#max_items" })
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
97 :text_tag("value", "1")
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
98 :up()
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
99 :tag("field", { var = "pubsub#persist_items" })
5839
45d0802d0787 mod_pubsub_serverinfo: Allow configuration of node persistence/deletion
Matthew Wild <mwild1@gmail.com>
parents: 5838
diff changeset
100 :text_tag("value", persist_items and "1" or "0")
5827
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
101
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
102 module:log("debug", "Sending request to create pub/sub node '%s' at %s", node, service)
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
103 return module:send_iq(request):next(
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
104 function(response)
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
105 if response.stanza == nil or response.stanza.attr.type ~= "result" then
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
106 module:log("warn", "Unexpected response to pub/sub node '%s' creation request at %s: %s", node, service, response.stanza)
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
107 return false
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
108 else
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
109 module:log("debug", "Successfully created pub/sub node '%s' at %s", node, service)
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
110 return true
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
111 end
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
112 end
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
113 )
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
114 end
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
115
5827
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
116 -- Returns a promise of a boolean
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
117 function delete_node()
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
118 local request = st.iq({ type = "set", to = service, from = actor, id = new_id() })
5838
77c5709bd57a mod_pubsub_serverinfo: Add explicit xmlns to all pubsub tags
Matthew Wild <mwild1@gmail.com>
parents: 5837
diff changeset
119 :tag("pubsub", { xmlns = xmlns_pubsub })
77c5709bd57a mod_pubsub_serverinfo: Add explicit xmlns to all pubsub tags
Matthew Wild <mwild1@gmail.com>
parents: 5837
diff changeset
120 :tag("delete", { node = node, xmlns = xmlns_pubsub });
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
121
5827
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
122 module:log("debug", "Sending request to delete pub/sub node '%s' at %s", node, service)
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
123 return module:send_iq(request):next(
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
124 function(response)
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
125 if response.stanza == nil or response.stanza.attr.type ~= "result" then
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
126 module:log("warn", "Unexpected response to pub/sub node '%s' deletion request at %s: %s", node, service, response.stanza)
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
127 return false
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
128 else
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
129 module:log("debug", "Successfully deleted pub/sub node '%s' at %s", node, service)
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
130 return true
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
131 end
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
132 end
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
133 )
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
134 end
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
135
5833
76b57bcfe1b2 mod_pubsub_serverinfo: Warm-up opt-in cache
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5832
diff changeset
136 function get_remote_domain_names()
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
137 -- 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: 5824
diff changeset
138 -- 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: 5824
diff changeset
139 -- them as an index in a table.
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
140 local domains_by_host = {}
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
141 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: 5824
diff changeset
142 if session ~= nil and session.from_host ~= nil and local_domain == session.to_host then
5831
2465d5191ae9 mod_pubsub_serverinfo: Consider sibling vhosts 'connected'
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5828
diff changeset
143 module:log("debug", "Local host '%s' has remote '%s' (inbound)", session.to_host, session.from_host);
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
144 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: 5824
diff changeset
145 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: 5824
diff changeset
146 sessions[session.from_host] = true
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
147 domains_by_host[session.to_host] = sessions
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
148 end
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
149 end
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
150
5831
2465d5191ae9 mod_pubsub_serverinfo: Consider sibling vhosts 'connected'
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5828
diff changeset
151 -- At an earlier stage, the code iterated over all prosody.hosts, trying to generate one pubsub item for all local hosts. That turned out to be
2465d5191ae9 mod_pubsub_serverinfo: Consider sibling vhosts 'connected'
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5828
diff changeset
152 -- to noisy. Instead, this code now creates an item that includes the local vhost only. It is assumed that this module will also be loaded for
2465d5191ae9 mod_pubsub_serverinfo: Consider sibling vhosts 'connected'
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5828
diff changeset
153 -- other vhosts. Their data should then be published to distinct pub/sub services and nodes.
2465d5191ae9 mod_pubsub_serverinfo: Consider sibling vhosts 'connected'
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5828
diff changeset
154
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
155 -- 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: 5824
diff changeset
156 local host = local_domain
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
157 local data = prosody.hosts[host]
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
158 if data ~= nil then
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
159 local sessions = domains_by_host[host]
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
160 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: 5824
diff changeset
161 if data.s2sout ~= nil then
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
162 for _, session in pairs(data.s2sout) do
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
163 if session.to_host ~= nil then
5831
2465d5191ae9 mod_pubsub_serverinfo: Consider sibling vhosts 'connected'
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5828
diff changeset
164 module:log("debug", "Local host '%s' has remote '%s' (outbound)", host, session.to_host);
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
165 sessions[session.to_host] = true
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
166 domains_by_host[host] = sessions
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
167 end
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
168 end
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
169 end
5831
2465d5191ae9 mod_pubsub_serverinfo: Consider sibling vhosts 'connected'
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5828
diff changeset
170
2465d5191ae9 mod_pubsub_serverinfo: Consider sibling vhosts 'connected'
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5828
diff changeset
171 -- When the instance of Prosody hosts more than one host, the other hosts can be thought of as having a 'permanent' s2s connection.
2465d5191ae9 mod_pubsub_serverinfo: Consider sibling vhosts 'connected'
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5828
diff changeset
172 for host_name, host_info in pairs(prosody.hosts) do
2465d5191ae9 mod_pubsub_serverinfo: Consider sibling vhosts 'connected'
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5828
diff changeset
173 if host ~= host_name and host_info.type ~= "component" then
2465d5191ae9 mod_pubsub_serverinfo: Consider sibling vhosts 'connected'
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5828
diff changeset
174 module:log("debug", "Local host '%s' has remote '%s' (vhost)", host, host_name);
2465d5191ae9 mod_pubsub_serverinfo: Consider sibling vhosts 'connected'
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5828
diff changeset
175 sessions[host_name] = true;
2465d5191ae9 mod_pubsub_serverinfo: Consider sibling vhosts 'connected'
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5828
diff changeset
176 domains_by_host[host] = sessions
2465d5191ae9 mod_pubsub_serverinfo: Consider sibling vhosts 'connected'
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5828
diff changeset
177 end
2465d5191ae9 mod_pubsub_serverinfo: Consider sibling vhosts 'connected'
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5828
diff changeset
178 end
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
179 end
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
180
5833
76b57bcfe1b2 mod_pubsub_serverinfo: Warm-up opt-in cache
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5832
diff changeset
181 return domains_by_host
76b57bcfe1b2 mod_pubsub_serverinfo: Warm-up opt-in cache
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5832
diff changeset
182 end
76b57bcfe1b2 mod_pubsub_serverinfo: Warm-up opt-in cache
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5832
diff changeset
183
76b57bcfe1b2 mod_pubsub_serverinfo: Warm-up opt-in cache
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5832
diff changeset
184 function publish_serverinfo()
5836
d38772479891 mod_pubsub_serverinfo: Some logging improvements
Matthew Wild <mwild1@gmail.com>
parents: 5835
diff changeset
185 module:log("debug", "Publishing server info...");
5833
76b57bcfe1b2 mod_pubsub_serverinfo: Warm-up opt-in cache
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5832
diff changeset
186 local domains_by_host = get_remote_domain_names()
76b57bcfe1b2 mod_pubsub_serverinfo: Warm-up opt-in cache
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5832
diff changeset
187
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
188 -- Build the publication stanza.
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
189 local request = st.iq({ type = "set", to = service, from = actor, id = new_id() })
5838
77c5709bd57a mod_pubsub_serverinfo: Add explicit xmlns to all pubsub tags
Matthew Wild <mwild1@gmail.com>
parents: 5837
diff changeset
190 :tag("pubsub", { xmlns = xmlns_pubsub })
77c5709bd57a mod_pubsub_serverinfo: Add explicit xmlns to all pubsub tags
Matthew Wild <mwild1@gmail.com>
parents: 5837
diff changeset
191 :tag("publish", { node = node, xmlns = xmlns_pubsub })
77c5709bd57a mod_pubsub_serverinfo: Add explicit xmlns to all pubsub tags
Matthew Wild <mwild1@gmail.com>
parents: 5837
diff changeset
192 :tag("item", { id = "current", xmlns = xmlns_pubsub })
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
193 :tag("serverinfo", { xmlns = "urn:xmpp:serverinfo:0" })
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
194
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
195 request:tag("domain", { name = local_domain })
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
196 :tag("federation")
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
197
5833
76b57bcfe1b2 mod_pubsub_serverinfo: Warm-up opt-in cache
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5832
diff changeset
198 local remotes = domains_by_host[local_domain]
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
199
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
200 if remotes ~= nil then
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
201 for remote, _ in pairs(remotes) do
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
202 -- 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: 5824
diff changeset
203 if does_opt_in(remote) then
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
204 request:tag("remote-domain", { name = remote }):up()
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
205 else
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
206 request:tag("remote-domain"):up()
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
207 end
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
208 end
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
209 end
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
210
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
211 request:up():up()
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
212
5827
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
213 module:send_iq(request):next(
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
214 function(response)
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
215 if response.stanza == nil or response.stanza.attr.type ~= "result" then
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
216 module:log("warn", "Unexpected response to item publication at pub/sub node '%s' on %s: %s", node, service, response.stanza)
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
217 return false
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
218 else
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
219 module:log("debug", "Successfully published item on pub/sub node '%s' at %s", node, service)
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
220 return true
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
221 end
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
222 end,
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
223 function(error)
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
224 module:log("warn", "Error prevented publication of item on pub/sub node at %s: %s", service, error)
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
225 end
c3eeeb968403 mod_pubsub_serverinfo: Detect existence of pub/sub node
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5826
diff changeset
226 )
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
227
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
228 return publication_interval;
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
229 end
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
230
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
231 local opt_in_cache = {}
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
232
5840
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
233 -- Public providers are already public, so we fetch the list of providers
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
234 -- registered on providers.xmpp.net so we don't have to disco them individually
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
235 local function update_public_providers()
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
236 return http.request(public_providers_url)
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
237 :next(function (response)
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
238 assert(
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
239 response.headers["content-type"] == "application/json",
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
240 "invalid mimetype: "..tostring(response.headers["content-type"])
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
241 );
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
242 return json.decode(response.body);
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
243 end)
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
244 :next(function (public_server_domains)
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
245 module:log("debug", "Retrieved list of %d public providers", #public_server_domains);
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
246 for _, domain in ipairs(public_server_domains) do
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
247 opt_in_cache[domain] = {
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
248 opt_in = true;
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
249 expires = os.time() + (86400 * 1.5);
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
250 };
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
251 end
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
252 end, function (err)
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
253 module:log("warn", "Failed to fetch/decode provider list: %s", err);
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
254 end);
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
255 end
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
256
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
257 module:daily("update public provider list", update_public_providers);
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
258
5833
76b57bcfe1b2 mod_pubsub_serverinfo: Warm-up opt-in cache
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5832
diff changeset
259 function cache_warm_up()
76b57bcfe1b2 mod_pubsub_serverinfo: Warm-up opt-in cache
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5832
diff changeset
260 module:log("debug", "Warming up opt-in cache")
5840
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
261
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
262 update_public_providers():finally(function ()
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
263 module:log("debug", "Querying known domains for opt-in cache...");
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
264 local domains_by_host = get_remote_domain_names()
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
265 local remotes = domains_by_host[local_domain]
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
266 if remotes ~= nil then
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
267 for remote in pairs(remotes) do
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
268 does_opt_in(remote)
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
269 end
5833
76b57bcfe1b2 mod_pubsub_serverinfo: Warm-up opt-in cache
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5832
diff changeset
270 end
5840
7905766d01f6 mod_pubsub_serverinfo: Treat public providers as public
Matthew Wild <mwild1@gmail.com>
parents: 5839
diff changeset
271 end);
5833
76b57bcfe1b2 mod_pubsub_serverinfo: Warm-up opt-in cache
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5832
diff changeset
272 end
76b57bcfe1b2 mod_pubsub_serverinfo: Warm-up opt-in cache
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5832
diff changeset
273
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
274 function does_opt_in(remoteDomain)
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
275
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
276 -- try to read answer from cache.
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
277 local cached_value = opt_in_cache[remoteDomain]
5835
cf5f77491323 mod_pubsub_serverinfo: Refresh cache entries if they will expire before next run
Matthew Wild <mwild1@gmail.com>
parents: 5833
diff changeset
278 local ttl = cached_value and os.difftime(cached_value.expires, os.time());
cf5f77491323 mod_pubsub_serverinfo: Refresh cache entries if they will expire before next run
Matthew Wild <mwild1@gmail.com>
parents: 5833
diff changeset
279 if cached_value and ttl > (publication_interval + 60) then
5831
2465d5191ae9 mod_pubsub_serverinfo: Consider sibling vhosts 'connected'
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5828
diff changeset
280 module:log("debug", "Opt-in status (from cache) for '%s': %s", remoteDomain, cached_value.opt_in)
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
281 return cached_value.opt_in;
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
282 end
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
283
5835
cf5f77491323 mod_pubsub_serverinfo: Refresh cache entries if they will expire before next run
Matthew Wild <mwild1@gmail.com>
parents: 5833
diff changeset
284 -- We don't have a cached value, or it is nearing expiration - refresh it now
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
285 -- 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: 5824
diff changeset
286
5836
d38772479891 mod_pubsub_serverinfo: Some logging improvements
Matthew Wild <mwild1@gmail.com>
parents: 5835
diff changeset
287 module:log("debug", "%s: performing disco/info to determine opt-in", remoteDomain)
5833
76b57bcfe1b2 mod_pubsub_serverinfo: Warm-up opt-in cache
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5832
diff changeset
288 local discoRequest = st.iq({ type = "get", to = remoteDomain, from = actor, id = new_id() })
76b57bcfe1b2 mod_pubsub_serverinfo: Warm-up opt-in cache
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5832
diff changeset
289 :tag("query", { xmlns = "http://jabber.org/protocol/disco#info" })
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
290
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
291 module:send_iq(discoRequest):next(
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
292 function(response)
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
293 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: 5824
diff changeset
294 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: 5824
diff changeset
295 if query ~= nil then
5832
a8cae8322b7c mod_pubsub_serverinfo: Fix namespace parsing issue with disco/info
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5831
diff changeset
296 for feature in query:childtags("feature") do
5836
d38772479891 mod_pubsub_serverinfo: Some logging improvements
Matthew Wild <mwild1@gmail.com>
parents: 5835
diff changeset
297 --module:log("debug", "Disco/info feature for '%s': %s", remoteDomain, feature)
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
298 if feature.attr.var == 'urn:xmpp:serverinfo:0' then
5831
2465d5191ae9 mod_pubsub_serverinfo: Consider sibling vhosts 'connected'
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5828
diff changeset
299 module:log("debug", "Disco/info response included opt-in for '%s'", remoteDomain)
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
300 opt_in_cache[remoteDomain] = {
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
301 opt_in = true;
5828
55b99f593c3a mod_pubsub_serverinfo: Disco/info cache TTL should be configurable
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5827
diff changeset
302 expires = os.time() + cache_ttl;
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
303 }
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
304 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: 5824
diff changeset
305 end
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
306 end
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
307 end
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
308 end
5831
2465d5191ae9 mod_pubsub_serverinfo: Consider sibling vhosts 'connected'
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5828
diff changeset
309 module:log("debug", "Disco/info response did not include opt-in for '%s'", remoteDomain)
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
310 opt_in_cache[remoteDomain] = {
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
311 opt_in = false;
5828
55b99f593c3a mod_pubsub_serverinfo: Disco/info cache TTL should be configurable
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5827
diff changeset
312 expires = os.time() + cache_ttl;
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
313 }
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
314 end,
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
315 function(response)
5831
2465d5191ae9 mod_pubsub_serverinfo: Consider sibling vhosts 'connected'
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5828
diff changeset
316 module:log("debug", "An error occurred while performing a disco/info request to determine opt-in for '%s'", remoteDomain, response)
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
317 opt_in_cache[remoteDomain] = {
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
318 opt_in = false;
5828
55b99f593c3a mod_pubsub_serverinfo: Disco/info cache TTL should be configurable
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5827
diff changeset
319 expires = os.time() + cache_ttl;
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
320 }
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
321 end
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
322 );
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
323
5835
cf5f77491323 mod_pubsub_serverinfo: Refresh cache entries if they will expire before next run
Matthew Wild <mwild1@gmail.com>
parents: 5833
diff changeset
324 if ttl and ttl <= 0 then
cf5f77491323 mod_pubsub_serverinfo: Refresh cache entries if they will expire before next run
Matthew Wild <mwild1@gmail.com>
parents: 5833
diff changeset
325 -- Cache entry expired, remove it and assume not opted in
cf5f77491323 mod_pubsub_serverinfo: Refresh cache entries if they will expire before next run
Matthew Wild <mwild1@gmail.com>
parents: 5833
diff changeset
326 opt_in_cache[remoteDomain] = nil;
cf5f77491323 mod_pubsub_serverinfo: Refresh cache entries if they will expire before next run
Matthew Wild <mwild1@gmail.com>
parents: 5833
diff changeset
327 return false;
cf5f77491323 mod_pubsub_serverinfo: Refresh cache entries if they will expire before next run
Matthew Wild <mwild1@gmail.com>
parents: 5833
diff changeset
328 end
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
329
5835
cf5f77491323 mod_pubsub_serverinfo: Refresh cache entries if they will expire before next run
Matthew Wild <mwild1@gmail.com>
parents: 5833
diff changeset
330 return cached_value and cached_value.opt_in;
5826
f55e65315ba0 mod_pubsub_serverinfo: implemented all basic features
Guus der Kinderen <guus.der.kinderen@gmail.com>
parents: 5824
diff changeset
331 end