1local log = module._log;
2local params = module:get_option("default_mucs", {});
3local jid_bare = require "util.jid".bare;
4
5
6local function set_affiliations(room, affiliations)
7	for affiliation, jids in pairs(affiliations) do
8		for i, jid in pairs(jids) do
9			module:log("debug", "Setting affiliation %s for jid %s", affiliation, jid);
10			room:set_affiliation(true, jid_bare(jid), affiliation);
11		end
12	end
13end
14
15
16local function configure_room(room, config)
17	local should_save = false;
18	if config.name ~= nil then
19		should_save = room:set_name(config.name) or should_save;
20	end
21	if config.description ~= nil then
22		should_save = room:set_description(config.description) or should_save;
23	end
24	if config.allow_member_invites ~= nil then
25		should_save =
26			room:set_allow_member_invites(config.allow_member_invites)
27			or should_save;
28	end
29	if config.change_subject ~= nil then
30		should_save =
31			room:set_changesubject(config.change_subject)
32			or should_save;
33	end
34	if config.history_length ~= nil then
35		should_save =
36			room:set_historylength(config.history_length)
37			or should_save;
38	end
39	if config.lang ~= nil then
40		should_save = room:set_language(config.lang) or should_save;
41	end
42	if config.members_only ~= nil then
43		should_save =
44			room:set_members_only(config.members_only)
45			or should_save;
46	end
47	if config.moderated ~= nil then
48		should_save = room:set_moderated(config.moderated) or should_save;
49	end
50	if config.persistent ~= nil then
51		should_save = room:set_persistent(config.persistent) or should_save;
52	end
53	if config.presence_broadcast ~= nil then
54		should_save = room:set_presence_broadcast(config.presence_broadcast) or should_save;
55	end
56	if config.public ~= nil then
57		should_save = room:set_hidden(not config.public) or should_save;
58	end
59	if config.public_jids ~= nil then
60		should_save =
61			room:set_whois(config.public_jids and "anyone" or "moderators")
62			or should_save;
63	end
64	if config.logging ~= room._data.logging then
65		room._data.logging = config.logging;
66		should_save = true;
67	end
68	if should_save then
69		room:save(true);
70	end
71end
72
73
74local i, room_data;
75for i, room_data in pairs(params) do
76	local host = module.host;
77	local room_jid = room_data.jid_node.."@"..host;
78	local mod_muc = prosody.hosts[host].modules.muc;
79	local room = mod_muc.get_room_from_jid(room_jid);
80	if not room then
81		module:log("debug", "Creating new room %s", room_jid);
82		-- We don't pass in the config, so that the default config is set first.
83		room = mod_muc.create_room(room_jid);
84	else
85		module:log("debug", "Configuring already existing room %s", room_jid);
86	end
87	configure_room(room, room_data.config);
88	if room_data.affiliations then
89		set_affiliations(room, room_data.affiliations);
90	end
91end
92