comparison mod_server_info/mod_server_info.lua @ 5842:ed82916e5796

mod_server_info: Rewrite/backport from Prosody 1ce18cb3e6cc
author Matthew Wild <mwild1@gmail.com>
date Fri, 23 Feb 2024 22:47:05 +0000
parents 174c77da03f5
children f408b8e603af
comparison
equal deleted inserted replaced
5841:904b226fddf1 5842:ed82916e5796
1 -- XEP-0128: Service Discovery Extensions (manual config) 1 -- mod_server_info imported from Prosody commit 1ce18cb3e6cc for the benefit
2 -- 2 -- of 0.12 deployments. This community version of the module will not load in
3 -- Copyright (C) 2023 Matthew Wild 3 -- newer Prosody versions, which include their own copy of the module.
4 -- 4 --% conflicts: mod_server_info
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8 5
9 local dataforms = require "util.dataforms"; 6 local dataforms = require "prosody.util.dataforms";
10 7
11 local config = module:get_option("server_info"); 8 local server_info_config = module:get_option("server_info", {});
9 local server_info_custom_fields = module:get_option_array("server_info_extensions");
12 10
13 if not config or next(config) == nil then return; end -- Nothing to do 11 -- Source: http://xmpp.org/registrar/formtypes.html#http:--jabber.org-network-serverinfo
12 local form_layout = dataforms.new({
13 { var = "FORM_TYPE"; type = "hidden"; value = "http://jabber.org/network/serverinfo" };
14 });
14 15
15 for _, form in ipairs(config) do 16 if server_info_custom_fields then
16 module:add_extension(dataforms.new(form):form({}, "result")); 17 for _, field in ipairs(server_info_custom_fields) do
18 table.insert(form_layout, field);
19 end
17 end 20 end
18 21
22 local generated_form;
23
24 function update_form()
25 local new_form = form_layout:form(server_info_config, "result");
26 if generated_form then
27 module:remove_item("extension", generated_form);
28 end
29 generated_form = new_form;
30 module:add_item("extension", generated_form);
31 end
32
33 function add_fields(event)
34 local fields = event.item;
35 for _, field in ipairs(fields) do
36 table.insert(form_layout, field);
37 end
38 update_form();
39 end
40
41 function remove_fields(event)
42 local removed_fields = event.item;
43 for _, removed_field in ipairs(removed_fields) do
44 local removed_var = removed_field.var or removed_field.name;
45 for i, field in ipairs(form_layout) do
46 local var = field.var or field.name
47 if var == removed_var then
48 table.remove(form_layout, i);
49 break;
50 end
51 end
52 end
53 update_form();
54 end
55
56 module:handle_items("server-info-fields", add_fields, remove_fields);
57
58 function module.load()
59 update_form();
60 end