Comparison

plugins/muc/mod_muc.lua @ 9626:4d3ab7153153 0.11

MUC: Use defaults from config in create_room() if no config provided
author Matthew Wild <mwild1@gmail.com>
date Thu, 15 Nov 2018 10:25:48 +0000
parent 9240:f9a83aca4421
child 9647:0f43b901c854
child 10646:85585910d468
comparison
equal deleted inserted replaced
9624:cc9dff0212f4 9626:4d3ab7153153
249 end 249 end
250 room_miss(); 250 room_miss();
251 return restore_room(room_jid); 251 return restore_room(room_jid);
252 end 252 end
253 253
254 function create_room(room_jid, config) 254 local function set_room_defaults(room, lang)
255 local exists = get_room_from_jid(room_jid);
256 if exists then
257 return nil, "room-exists";
258 end
259 local room = muclib.new_room(room_jid, config);
260 module:fire_event("muc-room-created", {
261 room = room;
262 });
263 return track_room(room);
264 end
265
266 function all_rooms()
267 return coroutine.wrap(function ()
268 local seen = {}; -- Don't iterate over persistent rooms twice
269 for room in live_rooms() do
270 coroutine.yield(room);
271 seen[room.jid] = true;
272 end
273 local all_persistent_rooms, err = persistent_rooms_storage:get(nil);
274 if not all_persistent_rooms then
275 if err then
276 module:log("error", "Error loading list of persistent rooms, only rooms live in memory were iterated over");
277 module:log("debug", "%s", debug.traceback(err));
278 end
279 return nil;
280 end
281 for room_jid in pairs(all_persistent_rooms) do
282 if not seen[room_jid] then
283 local room = restore_room(room_jid);
284 if room then
285 coroutine.yield(room);
286 else
287 module:log("error", "Missing data for room '%s', omitting from iteration", room_jid);
288 end
289 end
290 end
291 end);
292 end
293
294 function live_rooms()
295 return rooms:values();
296 end
297
298 function each_room(live_only)
299 if live_only then
300 return live_rooms();
301 end
302 return all_rooms();
303 end
304
305 module:hook("host-disco-items", function(event)
306 local reply = event.reply;
307 module:log("debug", "host-disco-items called");
308 if next(room_items_cache) ~= nil then
309 for jid, room_name in pairs(room_items_cache) do
310 reply:tag("item", { jid = jid, name = room_name }):up();
311 end
312 else
313 for room in all_rooms() do
314 if not room:get_hidden() then
315 local jid, room_name = room.jid, room:get_name();
316 room_items_cache[jid] = room_name;
317 reply:tag("item", { jid = jid, name = room_name }):up();
318 end
319 end
320 end
321 end);
322
323 module:hook("muc-room-pre-create", function (event)
324 local room = event.room;
325 room:set_public(module:get_option_boolean("muc_room_default_public", false)); 255 room:set_public(module:get_option_boolean("muc_room_default_public", false));
326 room:set_persistent(module:get_option_boolean("muc_room_default_persistent", room:get_persistent())); 256 room:set_persistent(module:get_option_boolean("muc_room_default_persistent", room:get_persistent()));
327 room:set_members_only(module:get_option_boolean("muc_room_default_members_only", room:get_members_only())); 257 room:set_members_only(module:get_option_boolean("muc_room_default_members_only", room:get_members_only()));
328 room:set_allow_member_invites(module:get_option_boolean("muc_room_default_allow_member_invites", 258 room:set_allow_member_invites(module:get_option_boolean("muc_room_default_allow_member_invites",
329 room:get_allow_member_invites())); 259 room:get_allow_member_invites()));
330 room:set_moderated(module:get_option_boolean("muc_room_default_moderated", room:get_moderated())); 260 room:set_moderated(module:get_option_boolean("muc_room_default_moderated", room:get_moderated()));
331 room:set_whois(module:get_option_boolean("muc_room_default_public_jids", 261 room:set_whois(module:get_option_boolean("muc_room_default_public_jids",
332 room:get_whois() == "anyone") and "anyone" or "moderators"); 262 room:get_whois() == "anyone") and "anyone" or "moderators");
333 room:set_changesubject(module:get_option_boolean("muc_room_default_change_subject", room:get_changesubject())); 263 room:set_changesubject(module:get_option_boolean("muc_room_default_change_subject", room:get_changesubject()));
334 room:set_historylength(module:get_option_number("muc_room_default_history_length", room:get_historylength())); 264 room:set_historylength(module:get_option_number("muc_room_default_history_length", room:get_historylength()));
335 room:set_language(event.stanza.attr["xml:lang"] or module:get_option_string("muc_room_default_language")); 265 room:set_language(lang or module:get_option_string("muc_room_default_language"));
266 end
267
268 function create_room(room_jid, config)
269 local exists = get_room_from_jid(room_jid);
270 if exists then
271 return nil, "room-exists";
272 end
273 local room = muclib.new_room(room_jid, config);
274 if not config then
275 set_room_defaults(room);
276 end
277 module:fire_event("muc-room-created", {
278 room = room;
279 });
280 return track_room(room);
281 end
282
283 function all_rooms()
284 return coroutine.wrap(function ()
285 local seen = {}; -- Don't iterate over persistent rooms twice
286 for room in live_rooms() do
287 coroutine.yield(room);
288 seen[room.jid] = true;
289 end
290 local all_persistent_rooms, err = persistent_rooms_storage:get(nil);
291 if not all_persistent_rooms then
292 if err then
293 module:log("error", "Error loading list of persistent rooms, only rooms live in memory were iterated over");
294 module:log("debug", "%s", debug.traceback(err));
295 end
296 return nil;
297 end
298 for room_jid in pairs(all_persistent_rooms) do
299 if not seen[room_jid] then
300 local room = restore_room(room_jid);
301 if room then
302 coroutine.yield(room);
303 else
304 module:log("error", "Missing data for room '%s', omitting from iteration", room_jid);
305 end
306 end
307 end
308 end);
309 end
310
311 function live_rooms()
312 return rooms:values();
313 end
314
315 function each_room(live_only)
316 if live_only then
317 return live_rooms();
318 end
319 return all_rooms();
320 end
321
322 module:hook("host-disco-items", function(event)
323 local reply = event.reply;
324 module:log("debug", "host-disco-items called");
325 if next(room_items_cache) ~= nil then
326 for jid, room_name in pairs(room_items_cache) do
327 reply:tag("item", { jid = jid, name = room_name }):up();
328 end
329 else
330 for room in all_rooms() do
331 if not room:get_hidden() then
332 local jid, room_name = room.jid, room:get_name();
333 room_items_cache[jid] = room_name;
334 reply:tag("item", { jid = jid, name = room_name }):up();
335 end
336 end
337 end
338 end);
339
340 module:hook("muc-room-pre-create", function (event)
341 set_room_defaults(event.room, event.stanza.attr["xml:lang"]);
336 end, 1); 342 end, 1);
337 343
338 module:hook("muc-room-pre-create", function(event) 344 module:hook("muc-room-pre-create", function(event)
339 local origin, stanza = event.origin, event.stanza; 345 local origin, stanza = event.origin, event.stanza;
340 if not track_room(event.room) then 346 if not track_room(event.room) then