plugins/muc/mod_muc.lua
author Waqas Hussain <waqas20@gmail.com>
Wed, 01 Aug 2012 01:36:22 +0500
changeset 5062 88e198d65905
parent 5058 433cc9a4c7e9
child 5064 7a1eb302c562
permissions -rw-r--r--
MUC: Send unavailable presence when the component or server is shutting down.
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
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     4
-- 
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
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     9
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    10
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
    11
	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
    12
end
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    13
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    14
local muc_host = module:get_host();
2028
4f33100195a0 MUC: Added 'name' config option, for specifying the component's name in disco responses.
Waqas Hussain <waqas20@gmail.com>
parents: 1781
diff changeset
    15
local muc_name = module:get_option("name");
4f33100195a0 MUC: Added 'name' config option, for specifying the component's name in disco responses.
Waqas Hussain <waqas20@gmail.com>
parents: 1781
diff changeset
    16
if type(muc_name) ~= "string" then muc_name = "Prosody Chatrooms"; end
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
bc3dfc00da5d MUC: Allow restricting room creation to local JIDs (thanks thomas.mangin).
Waqas Hussain <waqas20@gmail.com>
parents: 3560
diff changeset
    19
	if restrict_room_creation == true then 
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
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    25
local muc_new_room = module:require "muc".new_room;
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    26
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
    27
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
    28
local st = require "util.stanza";
1741
2919f3b985fc MUC: Added support for generating unique room names
Waqas Hussain <waqas20@gmail.com>
parents: 1738
diff changeset
    29
local uuid_gen = require "util.uuid".generate;
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    30
local datamanager = require "util.datamanager";
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 um_is_admin = require "core.usermanager".is_admin;
5016
56a0b13a3d42 mod_muc: Remove unused variable and pull hosts into a local
Kim Alvefur <zash@zash.se>
parents: 5015
diff changeset
    32
local hosts = hosts;
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    33
3576
8d8ce17b83ca MUC: Expose the rooms table as a global 'rooms'.
Waqas Hussain <waqas20@gmail.com>
parents: 3575
diff changeset
    34
rooms = {};
8d8ce17b83ca MUC: Expose the rooms table as a global 'rooms'.
Waqas Hussain <waqas20@gmail.com>
parents: 3575
diff changeset
    35
local rooms = rooms;
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    36
local persistent_rooms = datamanager.load(nil, muc_host, "persistent") or {};
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    37
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
    38
-- Configurable options
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
    39
local max_history_messages = module:get_option_number("max_history_messages");
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
    40
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
    41
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
    42
	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
    43
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
    44
5015
f19b38bfa015 mod_muc: Use module:send() instead of core_*_stanza()
Kim Alvefur <zash@zash.se>
parents: 4924
diff changeset
    45
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
    46
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
    47
	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
    48
	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
    49
	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
    50
		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
    51
		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
    52
		local data = {
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    53
			jid = room.jid;
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    54
			_data = room._data;
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    55
			_affiliations = room._affiliations;
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    56
		};
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    57
		datamanager.store(node, muc_host, "config", data);
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    58
		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
    59
	elseif forced then
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    60
		datamanager.store(node, muc_host, "config", 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
    61
		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
    62
			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
    63
		end
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    64
	end
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    65
	if forced then datamanager.store(nil, muc_host, "persistent", persistent_rooms); end
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    66
end
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    67
4924
d8b9fe5900a2 MUC: Handle missing persistent room data.
Waqas Hussain <waqas20@gmail.com>
parents: 4528
diff changeset
    68
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
    69
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
    70
	local node = jid_split(jid);
4924
d8b9fe5900a2 MUC: Handle missing persistent room data.
Waqas Hussain <waqas20@gmail.com>
parents: 4528
diff changeset
    71
	local data = datamanager.load(node, muc_host, "config");
d8b9fe5900a2 MUC: Handle missing persistent room data.
Waqas Hussain <waqas20@gmail.com>
parents: 4528
diff changeset
    72
	if data then
d8b9fe5900a2 MUC: Handle missing persistent room data.
Waqas Hussain <waqas20@gmail.com>
parents: 4528
diff changeset
    73
		local room = muc_new_room(jid, {
d8b9fe5900a2 MUC: Handle missing persistent room data.
Waqas Hussain <waqas20@gmail.com>
parents: 4528
diff changeset
    74
			max_history_length = max_history_messages;
d8b9fe5900a2 MUC: Handle missing persistent room data.
Waqas Hussain <waqas20@gmail.com>
parents: 4528
diff changeset
    75
		});
d8b9fe5900a2 MUC: Handle missing persistent room data.
Waqas Hussain <waqas20@gmail.com>
parents: 4528
diff changeset
    76
		room._data = data._data;
d8b9fe5900a2 MUC: Handle missing persistent room data.
Waqas Hussain <waqas20@gmail.com>
parents: 4528
diff changeset
    77
		room._data.max_history_length = max_history_messages; -- Overwrite old max_history_length in data with current settings
d8b9fe5900a2 MUC: Handle missing persistent room data.
Waqas Hussain <waqas20@gmail.com>
parents: 4528
diff changeset
    78
		room._affiliations = data._affiliations;
d8b9fe5900a2 MUC: Handle missing persistent room data.
Waqas Hussain <waqas20@gmail.com>
parents: 4528
diff changeset
    79
		room.route_stanza = room_route_stanza;
d8b9fe5900a2 MUC: Handle missing persistent room data.
Waqas Hussain <waqas20@gmail.com>
parents: 4528
diff changeset
    80
		room.save = room_save;
d8b9fe5900a2 MUC: Handle missing persistent room data.
Waqas Hussain <waqas20@gmail.com>
parents: 4528
diff changeset
    81
		rooms[jid] = room;
d8b9fe5900a2 MUC: Handle missing persistent room data.
Waqas Hussain <waqas20@gmail.com>
parents: 4528
diff changeset
    82
	else -- missing room data
d8b9fe5900a2 MUC: Handle missing persistent room data.
Waqas Hussain <waqas20@gmail.com>
parents: 4528
diff changeset
    83
		persistent_rooms[jid] = nil;
d8b9fe5900a2 MUC: Handle missing persistent room data.
Waqas Hussain <waqas20@gmail.com>
parents: 4528
diff changeset
    84
		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
    85
		persistent_errors = true;
d8b9fe5900a2 MUC: Handle missing persistent room data.
Waqas Hussain <waqas20@gmail.com>
parents: 4528
diff changeset
    86
	end
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    87
end
4924
d8b9fe5900a2 MUC: Handle missing persistent room data.
Waqas Hussain <waqas20@gmail.com>
parents: 4528
diff changeset
    88
if persistent_errors then datamanager.store(nil, muc_host, "persistent", 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
    89
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
    90
local host_room = muc_new_room(muc_host, {
4528
875b90d5ce0f muc - implement per channel history limits
Markus Kötter <koetter@rrzn-hiwi.uni-hannover.de>
parents: 4370
diff changeset
    91
	max_history_length = 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
    92
});
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
    93
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
    94
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
    95
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    96
local function get_disco_info(stanza)
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    97
	return st.iq({type='result', id=stanza.attr.id, from=muc_host, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    98
		:tag("identity", {category='conference', type='text', name=muc_name}):up()
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    99
		:tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   100
end
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   101
local function get_disco_items(stanza)
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   102
	local reply = st.iq({type='result', id=stanza.attr.id, from=muc_host, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   103
	for jid, room in pairs(rooms) do
3262
330010ef078f MUC: Updated code to use :set_hidden() and :is_hidden().
Waqas Hussain <waqas20@gmail.com>
parents: 2925
diff changeset
   104
		if not room:is_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
   105
			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
   106
		end
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   107
	end
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   108
	return reply; -- TODO cache disco reply
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   109
end
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   110
4370
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   111
local function handle_to_domain(event)
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   112
	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
   113
	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
   114
	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
   115
	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
   116
		local xmlns = stanza.tags[1].attr.xmlns;
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   117
		if xmlns == "http://jabber.org/protocol/disco#info" then
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   118
			origin.send(get_disco_info(stanza));
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   119
		elseif xmlns == "http://jabber.org/protocol/disco#items" then
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   120
			origin.send(get_disco_items(stanza));
1741
2919f3b985fc MUC: Added support for generating unique room names
Waqas Hussain <waqas20@gmail.com>
parents: 1738
diff changeset
   121
		elseif xmlns == "http://jabber.org/protocol/muc#unique" then
2919f3b985fc MUC: Added support for generating unique room names
Waqas Hussain <waqas20@gmail.com>
parents: 1738
diff changeset
   122
			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
   123
		else
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   124
			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
   125
		end
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   126
	else
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   127
		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
   128
		--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
   129
	end
4370
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   130
	return true;
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   131
end
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   132
3560
fb49b63e3fe2 MUC: Use events for hooking stanzas instead of the component stanza handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3510
diff changeset
   133
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
   134
	local origin, stanza = event.origin, event.stanza;
4370
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   135
	local bare = jid_bare(stanza.attr.to);
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   136
	local room = rooms[bare];
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   137
	if not room 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
   138
		if stanza.name ~= "presence" then
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
   139
			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
   140
			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
   141
		end
4370
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   142
		if not(restrict_room_creation) or
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   143
		  (restrict_room_creation == "admin" and is_admin(stanza.attr.from)) or
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   144
		  (restrict_room_creation == "local" and select(2, jid_split(stanza.attr.from)) == module.host:gsub("^[^%.]+%.", "")) then
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   145
			room = muc_new_room(bare, {
4528
875b90d5ce0f muc - implement per channel history limits
Markus Kötter <koetter@rrzn-hiwi.uni-hannover.de>
parents: 4370
diff changeset
   146
				max_history_length = max_history_messages;
4370
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   147
			});
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   148
			room.route_stanza = room_route_stanza;
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   149
			room.save = room_save;
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   150
			rooms[bare] = room;
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   151
		end
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   152
	end
4370
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   153
	if room then
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   154
		room:handle_stanza(origin, stanza);
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   155
		if not next(room._occupants) and not persistent_rooms[room.jid] then -- empty, non-persistent room
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   156
			rooms[bare] = nil; -- discard room
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   157
		end
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   158
	else
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   159
		origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   160
	end
3589
1792610e169e MUC: Return true from the stanza handler to suppress error responses.
Waqas Hussain <waqas20@gmail.com>
parents: 3577
diff changeset
   161
	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
   162
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
   163
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
   164
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
   165
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
   166
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
   167
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
   168
module:hook("presence/full", stanza_handler, -1);
4370
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   169
module:hook("iq/host", handle_to_domain, -1);
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   170
module:hook("message/host", handle_to_domain, -1);
be14f96290a4 MUC: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4260
diff changeset
   171
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
   172
3604
3e89f0509967 prosody: Removed all references to componentmanager from Prosody, except the main componentmanager file.
Waqas Hussain <waqas20@gmail.com>
parents: 3589
diff changeset
   173
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
   174
	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
   175
		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
   176
	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
   177
end
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   178
5016
56a0b13a3d42 mod_muc: Remove unused variable and pull hosts into a local
Kim Alvefur <zash@zash.se>
parents: 5015
diff changeset
   179
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
   180
5062
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   181
local saved = false;
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   182
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
   183
	saved = true;
1738
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   184
	return {rooms = rooms};
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   185
end
ee4a7151ed07 MUC: New basic mod_muc based on the new MUC library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   186
module.restore = function(data)
1748
f4c50c75af6f MUC: Fixed stanza routing for reloaded rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1747
diff changeset
   187
	for jid, oldroom in pairs(data.rooms or {}) do
1747
28e5f6b535a8 MUC: Added support for reloading MUC library code.
Waqas Hussain <waqas20@gmail.com>
parents: 1741
diff changeset
   188
		local room = muc_new_room(jid);
28e5f6b535a8 MUC: Added support for reloading MUC library code.
Waqas Hussain <waqas20@gmail.com>
parents: 1741
diff changeset
   189
		room._jid_nick = oldroom._jid_nick;
28e5f6b535a8 MUC: Added support for reloading MUC library code.
Waqas Hussain <waqas20@gmail.com>
parents: 1741
diff changeset
   190
		room._occupants = oldroom._occupants;
28e5f6b535a8 MUC: Added support for reloading MUC library code.
Waqas Hussain <waqas20@gmail.com>
parents: 1741
diff changeset
   191
		room._data = oldroom._data;
28e5f6b535a8 MUC: Added support for reloading MUC library code.
Waqas Hussain <waqas20@gmail.com>
parents: 1741
diff changeset
   192
		room._affiliations = oldroom._affiliations;
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1748
diff changeset
   193
		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
   194
		room.save = room_save;
1747
28e5f6b535a8 MUC: Added support for reloading MUC library code.
Waqas Hussain <waqas20@gmail.com>
parents: 1741
diff changeset
   195
		rooms[jid] = room;
28e5f6b535a8 MUC: Added support for reloading MUC library code.
Waqas Hussain <waqas20@gmail.com>
parents: 1741
diff changeset
   196
	end
5016
56a0b13a3d42 mod_muc: Remove unused variable and pull hosts into a local
Kim Alvefur <zash@zash.se>
parents: 5015
diff changeset
   197
	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
   198
end
5062
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   199
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   200
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
   201
	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
   202
		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
   203
		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
   204
			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
   205
			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
   206
			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
   207
		end
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   208
		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
   209
	end
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   210
end
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   211
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
   212
	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
   213
		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
   214
			:tag("x", {xmlns = "http://jabber.org/protocol/muc#user"})
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   215
				:tag("item", { affiliation='none', role='none' }):up();
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   216
		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
   217
			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
   218
		end
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   219
		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
   220
	end
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   221
end
88e198d65905 MUC: Send unavailable presence when the component or server is shutting down.
Waqas Hussain <waqas20@gmail.com>
parents: 5058
diff changeset
   222
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
   223
module:hook_global("server-stopping", shutdown_component);