comparison mod_server_contact_info/mod_server_contact_info.lua @ 942:4fdcb5c35021

mod_server_contact_info: Remove config-reloaded hook
author Kim Alvefur <zash@zash.se>
date Thu, 28 Mar 2013 08:22:06 +0100
parents 074237d7820b
children 465b34fdeb60
comparison
equal deleted inserted replaced
941:a6c2345bcf87 942:4fdcb5c35021
1 -- This plugin implements http://xmpp.org/extensions/xep-0157.html 1 -- This plugin implements http://xmpp.org/extensions/xep-0157.html
2 local t_insert = table.insert; 2 local t_insert = table.insert;
3 local df_new = require "util.dataforms".new; 3 local df_new = require "util.dataforms".new;
4 4
5 local x_contact_info;
6 -- Source: http://xmpp.org/registrar/formtypes.html#http:--jabber.org-network-serverinfo 5 -- Source: http://xmpp.org/registrar/formtypes.html#http:--jabber.org-network-serverinfo
7 local valid_types = { 6 local valid_types = {
8 abuse = true; 7 abuse = true;
9 admin = true; 8 admin = true;
10 feedback = true; 9 feedback = true;
11 sales = true; 10 sales = true;
12 security = true; 11 security = true;
13 support = true; 12 support = true;
14 } 13 }
15 14
16 local function update_form_data() 15 local contact_config = module:get_option("contact_info");
17 if x_contact_info then 16 if not contact_config then -- we'll use admins from the config as default
18 module:remove_item("extension", x_contact_info); 17 contact_config = { admin = {}; };
18 local admins = module:get_option("admins");
19 if not admins or #admins == 0 then
20 module:log("debug", "No contact_info or admins in config");
21 return -- Nothing to attach, so we'll just skip it.
19 end 22 end
20 x_contact_info = nil; 23 module:log("debug", "No contact_info in config, using admins as fallback");
24 --TODO fetch global admins too?
25 for i = 1,#admins do
26 t_insert(contact_config.admin, "xmpp:" .. admins[i])
27 module:log("debug", "Added %s to admin-addresses", admins[i]);
28 end
29 end
30 if not next(contact_config) then
31 module:log("debug", "No contacts, skipping");
32 return -- No use in serving an empty form.
33 end
34 local form_layout = {
35 { value = "http://jabber.org/network/serverinfo"; type = "hidden"; name = "FORM_TYPE"; };
36 };
37 local form_values = {};
21 38
22 local contact_config = module:get_option("contact_info"); 39 for t,a in pairs(contact_config) do
23 if not contact_config then -- we'll use admins from the config as default 40 if valid_types[t] and a then
24 contact_config = { admin = {}; }; 41 t_insert(form_layout, { name = t .. "-addresses", type = "list-multi" });
25 local admins = module:get_option("admins"); 42 form_values[t .. "-addresses"] = type(a) == "table" and a or {a};
26 if not admins or #admins == 0 then
27 module:log("debug", "No contact_info or admins in config");
28 return -- Nothing to attach, so we'll just skip it.
29 end
30 module:log("debug", "No contact_info in config, using admins as fallback");
31 --TODO fetch global admins too?
32 for i = 1,#admins do
33 t_insert(contact_config.admin, "xmpp:" .. admins[i])
34 module:log("debug", "Added %s to admin-addresses", admins[i]);
35 end
36 end 43 end
37 if not next(contact_config) then
38 module:log("debug", "No contacts, skipping");
39 return -- No use in serving an empty form.
40 end
41 local form_layout = {
42 { value = "http://jabber.org/network/serverinfo"; type = "hidden"; name = "FORM_TYPE"; };
43 };
44 local form_values = {};
45
46 for t,a in pairs(contact_config) do
47 if valid_types[t] and a then
48 t_insert(form_layout, { name = t .. "-addresses", type = "list-multi" });
49 form_values[t .. "-addresses"] = type(a) == "table" and a or {a};
50 end
51 end
52
53 x_contact_info = df_new(form_layout):form(form_values, "result");
54 module:add_extension(x_contact_info);
55 end 44 end
56 45
57 module:hook_global("config-reloaded", update_form_data); 46 module:add_extension(df_new(form_layout):form(form_values, "result"));
58 update_form_data();