changeset 3586:796b29911747

mod_muc_defaults: Create MUCs from config
author JC Brand <jc@opkode.com>
date Mon, 06 May 2019 16:05:00 +0200
parents ddf109d58eff
children c24d43ababc6
files mod_muc_defaults/README.markdown mod_muc_defaults/mod_muc_defaults.lua
diffstat 2 files changed, 117 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_muc_defaults/README.markdown	Mon May 06 16:05:00 2019 +0200
@@ -0,0 +1,37 @@
+# mod_muc_defaults
+
+Creates MUCs with default configuration settings upon Prosody startup.
+
+## Configuration
+
+Under your MUC component, add a `default_mucs` option with the relevant settings.
+
+```
+Component "conference.example.org" "muc"
+   modules_enabled = {
+            "muc_defaults";
+   }
+
+   default_mucs = {
+      {
+         jid_node = "trollbox",
+         affiliations = {
+                  admin = { "admin@example.org", "superuser@example.org" },
+                  owner = { "owner@example.org" },
+                  visitors = { "visitor@example.org" }
+         },
+         config = {
+                  allow_member_invites = false,
+                  change_subject = false,
+                  history_length = 40,
+                  language = "en",
+                  logging = true,
+                  members_only = false,
+                  moderated = false,
+                  persistent = true,
+                  public = true,
+                  public_jids = true
+         }
+      }
+   };
+```
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_muc_defaults/mod_muc_defaults.lua	Mon May 06 16:05:00 2019 +0200
@@ -0,0 +1,80 @@
+local log = module._log;
+local params = module:get_option("default_mucs", {});
+local jid_bare = require "util.jid".bare;
+
+
+local function set_affiliations(room, affiliations)
+	for affiliation, jids in pairs(affiliations) do
+		for i, jid in pairs(jids) do
+			module:log("debug", "Setting affiliation %s for jid %s", affiliation, jid);
+			room:set_affiliation(true, jid_bare(jid), affiliation);
+		end
+	end
+end
+
+
+local function configure_room(room, config)
+	local should_save = false;
+	if config.allow_member_invites ~= nil then
+		should_save =
+			room:set_allow_member_invites(config.allow_member_invites)
+			or should_save;
+	end
+	if config.change_subject ~= nil then
+		should_save =
+			room:set_changesubject(config.change_subject)
+			or should_save;
+	end
+	if config.history_length ~= nil then
+		should_save =
+			room:set_historylength(config.history_length)
+			or should_save;
+	end
+	if config.lang ~= nil then
+		should_save = room:set_language(config.language) or should_save;
+	end
+	if config.members_only ~= nil then
+		should_save =
+			room:set_members_only(config.members_only)
+			or should_save;
+	end
+	if config.moderated ~= nil then
+		should_save = room:set_moderated(config.moderated) or should_save;
+	end
+	if config.persistent ~= nil then
+		should_save = room:set_persistent(config.persistent) or should_save;
+	end
+	if config.public ~= nil then
+		should_save = room:set_hidden(not config.public) or should_save;
+	end
+	if config.public_jids ~= nil then
+		should_save =
+			room:set_whois(config.public_jids and "anyone" or "moderators")
+			or should_save;
+	end
+	if config.logging ~= room._data.logging then
+		room._data.logging = config.logging;
+		should_save = true;
+	end
+	if should_save then
+		room:save(true);
+	end
+end
+
+
+local i, room_data;
+for i, room_data in pairs(params) do
+	local host = module.host;
+	local room_jid = room_data.jid_node.."@"..host;
+	local mod_muc = prosody.hosts[host].modules.muc;
+	local room = mod_muc.get_room_from_jid(room_jid);
+	if not room then
+		module:log("debug", "Creating new room %s", room_jid);
+		-- We don't pass in the config, so that the default config is set first.
+		room = mod_muc.create_room(room_jid);
+	else
+		module:log("debug", "Configuring already existing room %s", room_jid);
+	end
+	configure_room(room, room_data.config);
+	set_affiliations(room, room_data.affiliations);
+end