plugins/muc/mod_muc.lua
author Kim Alvefur <zash@zash.se>
Mon, 12 Mar 2018 16:05:08 +0100
changeset 8707 c0e812dd9dfc
parent 8706 416b8ae3857d
child 8708 1d66f66a13c9
permissions -rw-r--r--
MUC: Say something about storage failure before aborting (see #1091)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     1
-- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2033
diff changeset
     2
-- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2033
diff changeset
     3
-- Copyright (C) 2008-2010 Waqas Hussain
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5692
diff changeset
     4
--
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     5
-- This project is MIT/X11 licensed. Please see the
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     6
-- COPYING file in the source package for more information.
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     7
--
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     8
5807
d7212bd61b60 mod_muc: Import util.array
Matthew Wild <mwild1@gmail.com>
parents: 5776
diff changeset
     9
local array = require "util.array";
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    10
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    11
if module:get_host_type() ~= "component" then
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    12
	error("MUC should be loaded as a component, please see http://prosody.im/doc/components", 0);
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    13
end
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    14
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    15
local muc_host = module:get_host();
7269
f0a2a305b788 MUC: Use type-specific config API for the 'name' option
Kim Alvefur <zash@zash.se>
parents: 6747
diff changeset
    16
local muc_name = module:get_option_string("name", "Prosody Chatrooms");
2033
38d32c154cec MUC: Added config option 'restrict_room_creation' to allow restricting room creation to admins.
Waqas Hussain <waqas20@gmail.com>
parents: 2028
diff changeset
    17
local restrict_room_creation = module:get_option("restrict_room_creation");
3575
bc3dfc00da5d MUC: Allow restricting room creation to local JIDs (thanks thomas.mangin).
Waqas Hussain <waqas20@gmail.com>
parents: 3560
diff changeset
    18
if restrict_room_creation then
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5692
diff changeset
    19
	if restrict_room_creation == true then
3575
bc3dfc00da5d MUC: Allow restricting room creation to local JIDs (thanks thomas.mangin).
Waqas Hussain <waqas20@gmail.com>
parents: 3560
diff changeset
    20
		restrict_room_creation = "admin";
bc3dfc00da5d MUC: Allow restricting room creation to local JIDs (thanks thomas.mangin).
Waqas Hussain <waqas20@gmail.com>
parents: 3560
diff changeset
    21
	elseif restrict_room_creation ~= "admin" and restrict_room_creation ~= "local" then
bc3dfc00da5d MUC: Allow restricting room creation to local JIDs (thanks thomas.mangin).
Waqas Hussain <waqas20@gmail.com>
parents: 3560
diff changeset
    22
		restrict_room_creation = nil;
bc3dfc00da5d MUC: Allow restricting room creation to local JIDs (thanks thomas.mangin).
Waqas Hussain <waqas20@gmail.com>
parents: 3560
diff changeset
    23
	end
bc3dfc00da5d MUC: Allow restricting room creation to local JIDs (thanks thomas.mangin).
Waqas Hussain <waqas20@gmail.com>
parents: 3560
diff changeset
    24
end
5808
026367992a0f mod_muc: Support for locking newly-created rooms until they are configured (enabled with muc_room_locking = true)
Matthew Wild <mwild1@gmail.com>
parents: 5807
diff changeset
    25
local lock_rooms = module:get_option_boolean("muc_room_locking", false);
026367992a0f mod_muc: Support for locking newly-created rooms until they are configured (enabled with muc_room_locking = true)
Matthew Wild <mwild1@gmail.com>
parents: 5807
diff changeset
    26
local lock_room_timeout = module:get_option_number("muc_room_lock_timeout", 300);
026367992a0f mod_muc: Support for locking newly-created rooms until they are configured (enabled with muc_room_locking = true)
Matthew Wild <mwild1@gmail.com>
parents: 5807
diff changeset
    27
5064
7a1eb302c562 MUC: Give host and server admins "owner" affiliation in all rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 5062
diff changeset
    28
local muclib = module:require "muc";
7a1eb302c562 MUC: Give host and server admins "owner" affiliation in all rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 5062
diff changeset
    29
local muc_new_room = muclib.new_room;
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    30
local jid_split = require "util.jid".split;
2033
38d32c154cec MUC: Added config option 'restrict_room_creation' to allow restricting room creation to admins.
Waqas Hussain <waqas20@gmail.com>
parents: 2028
diff changeset
    31
local jid_bare = require "util.jid".bare;
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    32
local st = require "util.stanza";
1741
2919f3b985fc MUC: Added support for generating unique room names
Waqas Hussain <waqas20@gmail.com>
parents: 1738
diff changeset
    33
local uuid_gen = require "util.uuid".generate;
2033
38d32c154cec MUC: Added config option 'restrict_room_creation' to allow restricting room creation to admins.
Waqas Hussain <waqas20@gmail.com>
parents: 2028
diff changeset
    34
local um_is_admin = require "core.usermanager".is_admin;
5376
ba9be0be4bbb MUC: Access prosody.hosts instead of the old global hosts
Kim Alvefur <zash@zash.se>
parents: 5335
diff changeset
    35
local hosts = prosody.hosts;
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    36
3576
8d8ce17b83ca MUC: Expose the rooms table as a global 'rooms'.
Waqas Hussain <waqas20@gmail.com>
parents: 3575
diff changeset
    37
rooms = {};
8d8ce17b83ca MUC: Expose the rooms table as a global 'rooms'.
Waqas Hussain <waqas20@gmail.com>
parents: 3575
diff changeset
    38
local rooms = rooms;
5500
eeea0eb2602a mod_auth_internal_hashed, mod_auth_internal_plain, mod_privacy, mod_private, mod_register, mod_vcard, mod_muc: Use module:open_store()
Kim Alvefur <zash@zash.se>
parents: 5376
diff changeset
    39
local persistent_rooms_storage = module:open_store("persistent");
8705
7e7aa0f770c7 MUC: Abort module loading if unable to get list of persistent rooms from storage (fixes #1091)
Kim Alvefur <zash@zash.se>
parents: 8028
diff changeset
    40
local persistent_rooms, err = persistent_rooms_storage:get();
7e7aa0f770c7 MUC: Abort module loading if unable to get list of persistent rooms from storage (fixes #1091)
Kim Alvefur <zash@zash.se>
parents: 8028
diff changeset
    41
if not persistent_rooms then
8707
c0e812dd9dfc MUC: Say something about storage failure before aborting (see #1091)
Kim Alvefur <zash@zash.se>
parents: 8706
diff changeset
    42
	module:log("error", "Error loading list of persistent rooms from storage. Reload mod_muc or restart to recover.");
8705
7e7aa0f770c7 MUC: Abort module loading if unable to get list of persistent rooms from storage (fixes #1091)
Kim Alvefur <zash@zash.se>
parents: 8028
diff changeset
    43
	assert(not err, err);
7e7aa0f770c7 MUC: Abort module loading if unable to get list of persistent rooms from storage (fixes #1091)
Kim Alvefur <zash@zash.se>
parents: 8028
diff changeset
    44
	persistent_rooms = {};
7e7aa0f770c7 MUC: Abort module loading if unable to get list of persistent rooms from storage (fixes #1091)
Kim Alvefur <zash@zash.se>
parents: 8028
diff changeset
    45
end
5500
eeea0eb2602a mod_auth_internal_hashed, mod_auth_internal_plain, mod_privacy, mod_private, mod_register, mod_vcard, mod_muc: Use module:open_store()
Kim Alvefur <zash@zash.se>
parents: 5376
diff changeset
    46
local room_configs = module:open_store("config");
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    47
3330
bdc325ce9fbc MUC: Make number of stored history messages configurable with option max_history_messages (thanks michal and others who requested)
Matthew Wild <mwild1@gmail.com>
parents: 3262
diff changeset
    48
-- Configurable options
5195
ce5d7538ac48 muc: Make max_history_messages simply a service-wide config option, and don't store it per-room (rooms still have their own history_message, but this is a global limit)
Matthew Wild <mwild1@gmail.com>
parents: 5074
diff changeset
    49
muclib.set_max_history_length(module:get_option_number("max_history_messages"));
3330
bdc325ce9fbc MUC: Make number of stored history messages configurable with option max_history_messages (thanks michal and others who requested)
Matthew Wild <mwild1@gmail.com>
parents: 3262
diff changeset
    50
5691
18a115beeebe mod_muc: Utilize mod_disco, instead of reimplementing disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5659
diff changeset
    51
module:depends("disco");
18a115beeebe mod_muc: Utilize mod_disco, instead of reimplementing disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5659
diff changeset
    52
module:add_identity("conference", "text", muc_name);
18a115beeebe mod_muc: Utilize mod_disco, instead of reimplementing disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5659
diff changeset
    53
module:add_feature("http://jabber.org/protocol/muc");
18a115beeebe mod_muc: Utilize mod_disco, instead of reimplementing disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5659
diff changeset
    54
2033
38d32c154cec MUC: Added config option 'restrict_room_creation' to allow restricting room creation to admins.
Waqas Hussain <waqas20@gmail.com>
parents: 2028
diff changeset
    55
local function is_admin(jid)
3388
02e668d64e05 MUC: No need to call is_admin twice now, global admins are admins on hosts
Matthew Wild <mwild1@gmail.com>
parents: 3330
diff changeset
    56
	return um_is_admin(jid, module.host);
2033
38d32c154cec MUC: Added config option 'restrict_room_creation' to allow restricting room creation to admins.
Waqas Hussain <waqas20@gmail.com>
parents: 2028
diff changeset
    57
end
38d32c154cec MUC: Added config option 'restrict_room_creation' to allow restricting room creation to admins.
Waqas Hussain <waqas20@gmail.com>
parents: 2028
diff changeset
    58
5984
c68e2b11d691 MUC: Expose room metatable on module
Kim Alvefur <zash@zash.se>
parents: 5945
diff changeset
    59
room_mt = muclib.room_mt; -- Yes, global.
c68e2b11d691 MUC: Expose room metatable on module
Kim Alvefur <zash@zash.se>
parents: 5945
diff changeset
    60
local _set_affiliation = room_mt.set_affiliation;
c68e2b11d691 MUC: Expose room metatable on module
Kim Alvefur <zash@zash.se>
parents: 5945
diff changeset
    61
local _get_affiliation = room_mt.get_affiliation;
5064
7a1eb302c562 MUC: Give host and server admins "owner" affiliation in all rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 5062
diff changeset
    62
function muclib.room_mt:get_affiliation(jid)
7a1eb302c562 MUC: Give host and server admins "owner" affiliation in all rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 5062
diff changeset
    63
	if is_admin(jid) then return "owner"; end
7a1eb302c562 MUC: Give host and server admins "owner" affiliation in all rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 5062
diff changeset
    64
	return _get_affiliation(self, jid);
7a1eb302c562 MUC: Give host and server admins "owner" affiliation in all rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 5062
diff changeset
    65
end
6745
6efeb801d62f Backed out changeset bea3862b6bde in favor of a different approach
Kim Alvefur <zash@zash.se>
parents: 6744
diff changeset
    66
function muclib.room_mt:set_affiliation(actor, jid, affiliation, callback, reason)
6746
adf2fdf1264a MUC: Prevent admins from being given affiliatons other than owner
Kim Alvefur <zash@zash.se>
parents: 6745
diff changeset
    67
	if affiliation ~= "owner" and is_admin(jid) then return nil, "modify", "not-acceptable"; end
6745
6efeb801d62f Backed out changeset bea3862b6bde in favor of a different approach
Kim Alvefur <zash@zash.se>
parents: 6744
diff changeset
    68
	return _set_affiliation(self, actor, jid, affiliation, callback, reason);
6efeb801d62f Backed out changeset bea3862b6bde in favor of a different approach
Kim Alvefur <zash@zash.se>
parents: 6744
diff changeset
    69
end
5064
7a1eb302c562 MUC: Give host and server admins "owner" affiliation in all rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 5062
diff changeset
    70
5015
f19b38bfa015 mod_muc: Use module:send() instead of core_*_stanza()
Kim Alvefur <zash@zash.se>
parents: 4924
diff changeset
    71
local function room_route_stanza(room, stanza) module:send(stanza); end
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    72
local function room_save(room, forced)
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    73
	local node = jid_split(room.jid);
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    74
	persistent_rooms[room.jid] = room._data.persistent;
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    75
	if room._data.persistent then
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    76
		local history = room._data.history;
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    77
		room._data.history = nil;
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    78
		local data = {
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    79
			jid = room.jid;
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    80
			_data = room._data;
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    81
			_affiliations = room._affiliations;
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    82
		};
5500
eeea0eb2602a mod_auth_internal_hashed, mod_auth_internal_plain, mod_privacy, mod_private, mod_register, mod_vcard, mod_muc: Use module:open_store()
Kim Alvefur <zash@zash.se>
parents: 5376
diff changeset
    83
		room_configs:set(node, data);
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    84
		room._data.history = history;
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    85
	elseif forced then
5500
eeea0eb2602a mod_auth_internal_hashed, mod_auth_internal_plain, mod_privacy, mod_private, mod_register, mod_vcard, mod_muc: Use module:open_store()
Kim Alvefur <zash@zash.se>
parents: 5376
diff changeset
    86
		room_configs:set(node, nil);
4260
403aba5e49d5 mod_muc: Remove room from memory when it is made non-persistent and is empty
Matthew Wild <mwild1@gmail.com>
parents: 3675
diff changeset
    87
		if not next(room._occupants) then -- Room empty
403aba5e49d5 mod_muc: Remove room from memory when it is made non-persistent and is empty
Matthew Wild <mwild1@gmail.com>
parents: 3675
diff changeset
    88
			rooms[room.jid] = nil;
403aba5e49d5 mod_muc: Remove room from memory when it is made non-persistent and is empty
Matthew Wild <mwild1@gmail.com>
parents: 3675
diff changeset
    89
		end
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    90
	end
5500
eeea0eb2602a mod_auth_internal_hashed, mod_auth_internal_plain, mod_privacy, mod_private, mod_register, mod_vcard, mod_muc: Use module:open_store()
Kim Alvefur <zash@zash.se>
parents: 5376
diff changeset
    91
	if forced then persistent_rooms_storage:set(nil, persistent_rooms); end
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    92
end
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    93
8028
8a7c4497569a MUC: Only create rooms in a locked state when they are created by someone joining (fixes timed deletion of all rooms on startup)
Kim Alvefur <zash@zash.se>
parents: 7749
diff changeset
    94
function create_room(jid, locked)
5210
862a6fae05e7 MUC: Expose create_room(jid).
Waqas Hussain <waqas20@gmail.com>
parents: 5195
diff changeset
    95
	local room = muc_new_room(jid);
862a6fae05e7 MUC: Expose create_room(jid).
Waqas Hussain <waqas20@gmail.com>
parents: 5195
diff changeset
    96
	room.route_stanza = room_route_stanza;
862a6fae05e7 MUC: Expose create_room(jid).
Waqas Hussain <waqas20@gmail.com>
parents: 5195
diff changeset
    97
	room.save = room_save;
862a6fae05e7 MUC: Expose create_room(jid).
Waqas Hussain <waqas20@gmail.com>
parents: 5195
diff changeset
    98
	rooms[jid] = room;
8028
8a7c4497569a MUC: Only create rooms in a locked state when they are created by someone joining (fixes timed deletion of all rooms on startup)
Kim Alvefur <zash@zash.se>
parents: 7749
diff changeset
    99
	if locked then
5808
026367992a0f mod_muc: Support for locking newly-created rooms until they are configured (enabled with muc_room_locking = true)
Matthew Wild <mwild1@gmail.com>
parents: 5807
diff changeset
   100
		room.locked = true;
026367992a0f mod_muc: Support for locking newly-created rooms until they are configured (enabled with muc_room_locking = true)
Matthew Wild <mwild1@gmail.com>
parents: 5807
diff changeset
   101
		if lock_room_timeout and lock_room_timeout > 0 then
026367992a0f mod_muc: Support for locking newly-created rooms until they are configured (enabled with muc_room_locking = true)
Matthew Wild <mwild1@gmail.com>
parents: 5807
diff changeset
   102
			module:add_timer(lock_room_timeout, function ()
026367992a0f mod_muc: Support for locking newly-created rooms until they are configured (enabled with muc_room_locking = true)
Matthew Wild <mwild1@gmail.com>
parents: 5807
diff changeset
   103
				if room.locked then
026367992a0f mod_muc: Support for locking newly-created rooms until they are configured (enabled with muc_room_locking = true)
Matthew Wild <mwild1@gmail.com>
parents: 5807
diff changeset
   104
					room:destroy(); -- Not unlocked in time
026367992a0f mod_muc: Support for locking newly-created rooms until they are configured (enabled with muc_room_locking = true)
Matthew Wild <mwild1@gmail.com>
parents: 5807
diff changeset
   105
				end
026367992a0f mod_muc: Support for locking newly-created rooms until they are configured (enabled with muc_room_locking = true)
Matthew Wild <mwild1@gmail.com>
parents: 5807
diff changeset
   106
			end);
026367992a0f mod_muc: Support for locking newly-created rooms until they are configured (enabled with muc_room_locking = true)
Matthew Wild <mwild1@gmail.com>
parents: 5807
diff changeset
   107
		end
026367992a0f mod_muc: Support for locking newly-created rooms until they are configured (enabled with muc_room_locking = true)
Matthew Wild <mwild1@gmail.com>
parents: 5807
diff changeset
   108
	end
5577
8b09b0d068d4 mod_muc: Fire muc-room-created and muc-room-destroyed events (thanks nik)
Matthew Wild <mwild1@gmail.com>
parents: 5500
diff changeset
   109
	module:fire_event("muc-room-created", { room = room });
5210
862a6fae05e7 MUC: Expose create_room(jid).
Waqas Hussain <waqas20@gmail.com>
parents: 5195
diff changeset
   110
	return room;
862a6fae05e7 MUC: Expose create_room(jid).
Waqas Hussain <waqas20@gmail.com>
parents: 5195
diff changeset
   111
end
862a6fae05e7 MUC: Expose create_room(jid).
Waqas Hussain <waqas20@gmail.com>
parents: 5195
diff changeset
   112
4924
d8b9fe5900a2 MUC: Handle missing persistent room data.
Waqas Hussain <waqas20@gmail.com>
parents: 4528
diff changeset
   113
local persistent_errors = false;
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
   114
for jid in pairs(persistent_rooms) do
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
   115
	local node = jid_split(jid);
8706
416b8ae3857d MUC: Prevent creation of room that could not be loaded from storage (see #1091)
Kim Alvefur <zash@zash.se>
parents: 8705
diff changeset
   116
	local data, err = room_configs:get(node);
4924
d8b9fe5900a2 MUC: Handle missing persistent room data.
Waqas Hussain <waqas20@gmail.com>
parents: 4528
diff changeset
   117
	if data then
5210
862a6fae05e7 MUC: Expose create_room(jid).
Waqas Hussain <waqas20@gmail.com>
parents: 5195
diff changeset
   118
		local room = create_room(jid);
4924
d8b9fe5900a2 MUC: Handle missing persistent room data.
Waqas Hussain <waqas20@gmail.com>
parents: 4528
diff changeset
   119
		room._data = data._data;
d8b9fe5900a2 MUC: Handle missing persistent room data.
Waqas Hussain <waqas20@gmail.com>
parents: 4528
diff changeset
   120
		room._affiliations = data._affiliations;
8706
416b8ae3857d MUC: Prevent creation of room that could not be loaded from storage (see #1091)
Kim Alvefur <zash@zash.se>
parents: 8705
diff changeset
   121
	elseif not err then -- missing room data
4924
d8b9fe5900a2 MUC: Handle missing persistent room data.
Waqas Hussain <waqas20@gmail.com>
parents: 4528
diff changeset
   122
		persistent_rooms[jid] = nil;
d8b9fe5900a2 MUC: Handle missing persistent room data.
Waqas Hussain <waqas20@gmail.com>
parents: 4528
diff changeset
   123
		module:log("error", "Missing data for room '%s', removing from persistent room list", jid);
d8b9fe5900a2 MUC: Handle missing persistent room data.
Waqas Hussain <waqas20@gmail.com>
parents: 4528
diff changeset
   124
		persistent_errors = true;
8706
416b8ae3857d MUC: Prevent creation of room that could not be loaded from storage (see #1091)
Kim Alvefur <zash@zash.se>
parents: 8705
diff changeset
   125
	else -- error
416b8ae3857d MUC: Prevent creation of room that could not be loaded from storage (see #1091)
Kim Alvefur <zash@zash.se>
parents: 8705
diff changeset
   126
		module:log("error", "Error loading data for room '%s', locking it until service restart. Error was: %s", jid, err);
416b8ae3857d MUC: Prevent creation of room that could not be loaded from storage (see #1091)
Kim Alvefur <zash@zash.se>
parents: 8705
diff changeset
   127
		local room = muc_new_room(jid);
416b8ae3857d MUC: Prevent creation of room that could not be loaded from storage (see #1091)
Kim Alvefur <zash@zash.se>
parents: 8705
diff changeset
   128
		room.locked = true;
416b8ae3857d MUC: Prevent creation of room that could not be loaded from storage (see #1091)
Kim Alvefur <zash@zash.se>
parents: 8705
diff changeset
   129
		room._affiliations = { [muc_host] = "owner" }; -- To prevent unlocking
416b8ae3857d MUC: Prevent creation of room that could not be loaded from storage (see #1091)
Kim Alvefur <zash@zash.se>
parents: 8705
diff changeset
   130
		rooms[jid] = room;
4924
d8b9fe5900a2 MUC: Handle missing persistent room data.
Waqas Hussain <waqas20@gmail.com>
parents: 4528
diff changeset
   131
	end
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
   132
end
5500
eeea0eb2602a mod_auth_internal_hashed, mod_auth_internal_plain, mod_privacy, mod_private, mod_register, mod_vcard, mod_muc: Use module:open_store()
Kim Alvefur <zash@zash.se>
parents: 5376
diff changeset
   133
if persistent_errors then persistent_rooms_storage:set(nil, persistent_rooms); end
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
   134
5195
ce5d7538ac48 muc: Make max_history_messages simply a service-wide config option, and don't store it per-room (rooms still have their own history_message, but this is a global limit)
Matthew Wild <mwild1@gmail.com>
parents: 5074
diff changeset
   135
local host_room = muc_new_room(muc_host);
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
   136
host_room.route_stanza = room_route_stanza;
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
   137
host_room.save = room_save;
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   138
5691
18a115beeebe mod_muc: Utilize mod_disco, instead of reimplementing disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5659
diff changeset
   139
module:hook("host-disco-items", function(event)
18a115beeebe mod_muc: Utilize mod_disco, instead of reimplementing disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5659
diff changeset
   140
	local reply = event.reply;
18a115beeebe mod_muc: Utilize mod_disco, instead of reimplementing disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5659
diff changeset
   141
	module:log("debug", "host-disco-items called");
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   142
	for jid, room in pairs(rooms) do
5580
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   143
		if not room:get_hidden() then
3510
711eb5bf94b4 MUC: Make the room node be the default room name (thanks Zash).
Waqas Hussain <waqas20@gmail.com>
parents: 3388
diff changeset
   144
			reply:tag("item", {jid=jid, name=room:get_name()}):up();
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
   145
		end
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   146
	end
5691
18a115beeebe mod_muc: Utilize mod_disco, instead of reimplementing disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5659
diff changeset
   147
end);
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   148
4370
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   149
local function handle_to_domain(event)
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   150
	local origin, stanza = event.origin, event.stanza;
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   151
	local type = stanza.attr.type;
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   152
	if type == "error" or type == "result" then return; end
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   153
	if stanza.name == "iq" and type == "get" then
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   154
		local xmlns = stanza.tags[1].attr.xmlns;
5335
bb81c13d2c6f MUC: Always return <service-unavailable/> when a node is present in service discovery requests.
Waqas Hussain <waqas20@gmail.com>
parents: 5210
diff changeset
   155
		local node = stanza.tags[1].attr.node;
5691
18a115beeebe mod_muc: Utilize mod_disco, instead of reimplementing disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5659
diff changeset
   156
		if xmlns == "http://jabber.org/protocol/muc#unique" then
1741
2919f3b985fc MUC: Added support for generating unique room names
Waqas Hussain <waqas20@gmail.com>
parents: 1738
diff changeset
   157
			origin.send(st.reply(stanza):tag("unique", {xmlns = xmlns}):text(uuid_gen())); -- FIXME Random UUIDs can theoretically have collisions
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   158
		else
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   159
			origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   160
		end
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   161
	else
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   162
		host_room:handle_stanza(origin, stanza);
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   163
		--origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it"));
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   164
	end
4370
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   165
	return true;
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   166
end
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   167
3560
fb49b63e3fe2 MUC: Use events for hooking stanzas instead of the component stanza handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3510
diff changeset
   168
function stanza_handler(event)
fb49b63e3fe2 MUC: Use events for hooking stanzas instead of the component stanza handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3510
diff changeset
   169
	local origin, stanza = event.origin, event.stanza;
4370
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   170
	local bare = jid_bare(stanza.attr.to);
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   171
	local room = rooms[bare];
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   172
	if not room then
7749
3dff38ffdcd0 MUC: Don't create room in response to unavailable presence
Kim Alvefur <zash@zash.se>
parents: 7269
diff changeset
   173
		if stanza.name ~= "presence" or stanza.attr.type ~= nil then
5058
433cc9a4c7e9 MUC: Return <item-not-found/> on message and iq to non-existent rooms (thanks Maranda).
Waqas Hussain <waqas20@gmail.com>
parents: 5016
diff changeset
   174
			origin.send(st.error_reply(stanza, "cancel", "item-not-found"));
433cc9a4c7e9 MUC: Return <item-not-found/> on message and iq to non-existent rooms (thanks Maranda).
Waqas Hussain <waqas20@gmail.com>
parents: 5016
diff changeset
   175
			return true;
433cc9a4c7e9 MUC: Return <item-not-found/> on message and iq to non-existent rooms (thanks Maranda).
Waqas Hussain <waqas20@gmail.com>
parents: 5016
diff changeset
   176
		end
4370
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   177
		if not(restrict_room_creation) or
5943
355f8b59bf1b mod_muc: Remove extra parenthesis (thanks janhouse)
Kim Alvefur <zash@zash.se>
parents: 5938
diff changeset
   178
		  is_admin(stanza.attr.from) or
4370
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   179
		  (restrict_room_creation == "local" and select(2, jid_split(stanza.attr.from)) == module.host:gsub("^[^%.]+%.", "")) then
8028
8a7c4497569a MUC: Only create rooms in a locked state when they are created by someone joining (fixes timed deletion of all rooms on startup)
Kim Alvefur <zash@zash.se>
parents: 7749
diff changeset
   180
			room = create_room(bare, lock_rooms);
4370
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   181
		end
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   182
	end
4370
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   183
	if room then
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   184
		room:handle_stanza(origin, stanza);
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   185
		if not next(room._occupants) and not persistent_rooms[room.jid] then -- empty, non-persistent room
5997
2d652afa57e4 MUC: Fire muc-room-destroyed event when the last participant leaves a non-persistent room
Kim Alvefur <zash@zash.se>
parents: 5943
diff changeset
   186
			module:fire_event("muc-room-destroyed", { room = room });
4370
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   187
			rooms[bare] = nil; -- discard room
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   188
		end
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   189
	else
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   190
		origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   191
	end
3589
1792610e169e MUC: Return true from the stanza handler to suppress error responses.
Waqas Hussain <waqas20@gmail.com>
parents: 3577
diff changeset
   192
	return true;
3560
fb49b63e3fe2 MUC: Use events for hooking stanzas instead of the component stanza handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3510
diff changeset
   193
end
3675
cd3a1ae596b4 MUC: Give stanza handlers a negative priority, to allow mod_iq to process them first.
Waqas Hussain <waqas20@gmail.com>
parents: 3628
diff changeset
   194
module:hook("iq/bare", stanza_handler, -1);
cd3a1ae596b4 MUC: Give stanza handlers a negative priority, to allow mod_iq to process them first.
Waqas Hussain <waqas20@gmail.com>
parents: 3628
diff changeset
   195
module:hook("message/bare", stanza_handler, -1);
cd3a1ae596b4 MUC: Give stanza handlers a negative priority, to allow mod_iq to process them first.
Waqas Hussain <waqas20@gmail.com>
parents: 3628
diff changeset
   196
module:hook("presence/bare", stanza_handler, -1);
cd3a1ae596b4 MUC: Give stanza handlers a negative priority, to allow mod_iq to process them first.
Waqas Hussain <waqas20@gmail.com>
parents: 3628
diff changeset
   197
module:hook("iq/full", stanza_handler, -1);
cd3a1ae596b4 MUC: Give stanza handlers a negative priority, to allow mod_iq to process them first.
Waqas Hussain <waqas20@gmail.com>
parents: 3628
diff changeset
   198
module:hook("message/full", stanza_handler, -1);
cd3a1ae596b4 MUC: Give stanza handlers a negative priority, to allow mod_iq to process them first.
Waqas Hussain <waqas20@gmail.com>
parents: 3628
diff changeset
   199
module:hook("presence/full", stanza_handler, -1);
4370
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   200
module:hook("iq/host", handle_to_domain, -1);
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   201
module:hook("message/host", handle_to_domain, -1);
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   202
module:hook("presence/host", handle_to_domain, -1);
3560
fb49b63e3fe2 MUC: Use events for hooking stanzas instead of the component stanza handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3510
diff changeset
   203
3604
3e89f0509967 prosody: Removed all references to componentmanager from Prosody, except the main componentmanager file.
Waqas Hussain <waqas20@gmail.com>
parents: 3589
diff changeset
   204
hosts[module.host].send = function(stanza) -- FIXME do a generic fix
1780
668ce0a2050d MUC: Added a send() method to the component. Fixes issues with local mod_vcard.
Waqas Hussain <waqas20@gmail.com>
parents: 1767
diff changeset
   205
	if stanza.attr.type == "result" or stanza.attr.type == "error" then
5015
f19b38bfa015 mod_muc: Use module:send() instead of core_*_stanza()
Kim Alvefur <zash@zash.se>
parents: 4924
diff changeset
   206
		module:send(stanza);
1780
668ce0a2050d MUC: Added a send() method to the component. Fixes issues with local mod_vcard.
Waqas Hussain <waqas20@gmail.com>
parents: 1767
diff changeset
   207
	else error("component.send only supports result and error stanzas at the moment"); end
668ce0a2050d MUC: Added a send() method to the component. Fixes issues with local mod_vcard.
Waqas Hussain <waqas20@gmail.com>
parents: 1767
diff changeset
   208
end
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   209
5016
56a0b13a3d42 mod_muc: Remove unused variable and pull hosts into a local
Kim Alvefur <zash@zash.se>
parents: 5015
diff changeset
   210
hosts[module:get_host()].muc = { rooms = rooms };
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   211
5062
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   212
local saved = false;
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   213
module.save = function()
5062
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   214
	saved = true;
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   215
	return {rooms = rooms};
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   216
end
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   217
module.restore = function(data)
1748
f4c50c75af6f MUC: Fixed stanza routing for reloaded rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1747
diff changeset
   218
	for jid, oldroom in pairs(data.rooms or {}) do
5210
862a6fae05e7 MUC: Expose create_room(jid).
Waqas Hussain <waqas20@gmail.com>
parents: 5195
diff changeset
   219
		local room = create_room(jid);
1747
28e5f6b535a8 MUC: Added support for reloading MUC library code.
Waqas Hussain <waqas20@gmail.com>
parents: 1741
diff changeset
   220
		room._jid_nick = oldroom._jid_nick;
28e5f6b535a8 MUC: Added support for reloading MUC library code.
Waqas Hussain <waqas20@gmail.com>
parents: 1741
diff changeset
   221
		room._occupants = oldroom._occupants;
28e5f6b535a8 MUC: Added support for reloading MUC library code.
Waqas Hussain <waqas20@gmail.com>
parents: 1741
diff changeset
   222
		room._data = oldroom._data;
28e5f6b535a8 MUC: Added support for reloading MUC library code.
Waqas Hussain <waqas20@gmail.com>
parents: 1741
diff changeset
   223
		room._affiliations = oldroom._affiliations;
28e5f6b535a8 MUC: Added support for reloading MUC library code.
Waqas Hussain <waqas20@gmail.com>
parents: 1741
diff changeset
   224
	end
5016
56a0b13a3d42 mod_muc: Remove unused variable and pull hosts into a local
Kim Alvefur <zash@zash.se>
parents: 5015
diff changeset
   225
	hosts[module:get_host()].muc = { rooms = rooms };
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   226
end
5062
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   227
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   228
function shutdown_room(room, stanza)
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   229
	for nick, occupant in pairs(room._occupants) do
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   230
		stanza.attr.from = nick;
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   231
		for jid in pairs(occupant.sessions) do
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   232
			stanza.attr.to = jid;
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   233
			room:_route_stanza(stanza);
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   234
			room._jid_nick[jid] = nil;
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   235
		end
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   236
		room._occupants[nick] = nil;
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   237
	end
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   238
end
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   239
function shutdown_component()
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   240
	if not saved then
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   241
		local stanza = st.presence({type = "unavailable"})
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   242
			:tag("x", {xmlns = "http://jabber.org/protocol/muc#user"})
5659
9f9de8078164 mod_muc: Include status code 332 on service shutdown (thanks mathieui)
Matthew Wild <mwild1@gmail.com>
parents: 5580
diff changeset
   243
				:tag("item", { affiliation='none', role='none' }):up()
9f9de8078164 mod_muc: Include status code 332 on service shutdown (thanks mathieui)
Matthew Wild <mwild1@gmail.com>
parents: 5580
diff changeset
   244
				:tag("status", { code = "332"}):up();
5062
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   245
		for roomjid, room in pairs(rooms) do
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   246
			shutdown_room(room, stanza);
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   247
		end
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   248
		shutdown_room(host_room, stanza);
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   249
	end
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   250
end
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   251
module.unload = shutdown_component;
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   252
module:hook_global("server-stopping", shutdown_component);
5692
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   253
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   254
-- Ad-hoc commands
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   255
module:depends("adhoc")
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   256
local t_concat = table.concat;
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   257
local keys = require "util.iterators".keys;
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   258
local adhoc_new = module:require "adhoc".new;
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   259
local adhoc_initial = require "util.adhoc".new_initial_data_form;
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   260
local dataforms_new = require "util.dataforms".new;
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   261
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   262
local destroy_rooms_layout = dataforms_new {
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   263
	title = "Destroy rooms";
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   264
	instructions = "Select the rooms to destroy";
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   265
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   266
	{ name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/muc#destroy" };
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   267
	{ name = "rooms", type = "list-multi", required = true, label = "Rooms to destroy:"};
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   268
};
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   269
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   270
local destroy_rooms_handler = adhoc_initial(destroy_rooms_layout, function()
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   271
	return { rooms = array.collect(keys(rooms)):sort() };
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   272
end, function(fields, errors)
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   273
	if errors then
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   274
		local errmsg = {};
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   275
		for name, err in pairs(errors) do
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   276
			errmsg[#errmsg + 1] = name .. ": " .. err;
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   277
		end
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   278
		return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   279
	end
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   280
	for _, room in ipairs(fields.rooms) do
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   281
		rooms[room]:destroy();
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   282
		rooms[room] = nil;
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   283
	end
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   284
	return { status = "completed", info = "The following rooms were destroyed:\n"..t_concat(fields.rooms, "\n") };
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   285
end);
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   286
local destroy_rooms_desc = adhoc_new("Destroy Rooms", "http://prosody.im/protocol/muc#destroy", destroy_rooms_handler, "admin");
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   287
24e7e58155d8 mod_muc: Add Ad-Hoc command to destroy MUC rooms
Florian Zeitz <florob@babelmonkeys.de>
parents: 5691
diff changeset
   288
module:provides("adhoc", destroy_rooms_desc);