2003-03-23 21:08:44 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_muc.erl
|
2007-12-24 14:57:53 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2010-04-14 01:00:14 +02:00
|
|
|
%%% Purpose : MUC support (XEP-0045)
|
2007-12-24 14:57:53 +01:00
|
|
|
%%% Created : 19 Mar 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2016-01-13 12:29:14 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2016 ProcessOne
|
2007-12-24 14:57:53 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-12-24 14:57:53 +01:00
|
|
|
%%%
|
2003-03-23 21:08:44 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_muc).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2007-12-24 14:57:53 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2015-05-21 17:02:36 +02:00
|
|
|
-protocol({xep, 45, '1.25'}).
|
|
|
|
|
2006-02-02 06:00:27 +01:00
|
|
|
-behaviour(gen_server).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-03-23 21:08:44 +01:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2006-02-02 06:00:27 +01:00
|
|
|
%% API
|
|
|
|
-export([start_link/2,
|
|
|
|
start/2,
|
2005-06-20 05:18:13 +02:00
|
|
|
stop/1,
|
2007-01-19 05:46:44 +01:00
|
|
|
room_destroyed/4,
|
2012-03-31 13:41:54 +02:00
|
|
|
store_room/4,
|
|
|
|
restore_room/3,
|
|
|
|
forget_room/3,
|
2009-02-16 16:24:40 +01:00
|
|
|
create_room/5,
|
2015-10-07 00:06:58 +02:00
|
|
|
shutdown_rooms/1,
|
2016-07-25 12:50:30 +02:00
|
|
|
process_disco_info/1,
|
|
|
|
process_disco_items/1,
|
|
|
|
process_vcard/1,
|
|
|
|
process_register/1,
|
|
|
|
process_muc_unique/1,
|
|
|
|
process_mucsub/1,
|
2010-01-03 01:38:00 +01:00
|
|
|
broadcast_service_message/2,
|
2015-10-07 00:06:58 +02:00
|
|
|
export/1,
|
2016-11-22 14:48:01 +01:00
|
|
|
import_info/0,
|
|
|
|
import/5,
|
|
|
|
import_start/2,
|
2016-04-13 20:07:32 +02:00
|
|
|
opts_to_binary/1,
|
2012-03-31 13:41:54 +02:00
|
|
|
can_use_nick/4]).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([init/1, handle_call/3, handle_cast/2,
|
2015-06-01 14:38:27 +02:00
|
|
|
handle_info/2, terminate/2, code_change/3,
|
2016-07-06 13:58:48 +02:00
|
|
|
mod_opt_type/1, depends/2]).
|
2006-02-02 06:00:27 +01:00
|
|
|
|
2003-03-23 21:08:44 +01:00
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2016-11-20 16:08:49 +01:00
|
|
|
-include_lib("stdlib/include/ms_transform.hrl").
|
2016-07-25 12:50:30 +02:00
|
|
|
-include("xmpp.hrl").
|
2016-01-15 13:34:48 +01:00
|
|
|
-include("mod_muc.hrl").
|
2003-04-17 21:47:43 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-record(state,
|
|
|
|
{host = <<"">> :: binary(),
|
|
|
|
server_host = <<"">> :: binary(),
|
|
|
|
access = {none, none, none, none} :: {atom(), atom(), atom(), atom()},
|
|
|
|
history_size = 20 :: non_neg_integer(),
|
2016-10-18 22:59:34 +02:00
|
|
|
max_rooms_discoitems = 100 :: non_neg_integer(),
|
2013-03-14 10:33:02 +01:00
|
|
|
default_room_opts = [] :: list(),
|
|
|
|
room_shaper = none :: shaper:shaper()}).
|
2006-02-02 06:00:27 +01:00
|
|
|
|
2005-05-23 02:30:29 +02:00
|
|
|
-define(PROCNAME, ejabberd_mod_muc).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-04-13 20:07:32 +02:00
|
|
|
-type muc_room_opts() :: [{atom(), any()}].
|
|
|
|
-callback init(binary(), gen_mod:opts()) -> any().
|
2016-11-22 14:48:01 +01:00
|
|
|
-callback import(binary(), binary(), [binary()]) -> ok.
|
2016-04-13 20:07:32 +02:00
|
|
|
-callback store_room(binary(), binary(), binary(), list()) -> {atomic, any()}.
|
|
|
|
-callback restore_room(binary(), binary(), binary()) -> muc_room_opts() | error.
|
|
|
|
-callback forget_room(binary(), binary(), binary()) -> {atomic, any()}.
|
|
|
|
-callback can_use_nick(binary(), binary(), jid(), binary()) -> boolean().
|
|
|
|
-callback get_rooms(binary(), binary()) -> [#muc_room{}].
|
|
|
|
-callback get_nick(binary(), binary(), jid()) -> binary() | error.
|
|
|
|
-callback set_nick(binary(), binary(), jid(), binary()) -> {atomic, ok | false}.
|
|
|
|
|
2006-02-02 06:00:27 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% API
|
|
|
|
%%====================================================================
|
|
|
|
start_link(Host, Opts) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_server:start_link({local, Proc}, ?MODULE,
|
|
|
|
[Host, Opts], []).
|
2006-02-02 06:00:27 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2006-02-02 06:00:27 +01:00
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
2013-03-14 10:33:02 +01:00
|
|
|
ChildSpec = {Proc, {?MODULE, start_link, [Host, Opts]},
|
2016-07-29 11:18:42 +02:00
|
|
|
transient, 1000, worker, [?MODULE]},
|
2006-02-02 06:00:27 +01:00
|
|
|
supervisor:start_child(ejabberd_sup, ChildSpec).
|
|
|
|
|
|
|
|
stop(Host) ->
|
2013-06-19 13:30:31 +02:00
|
|
|
Rooms = shutdown_rooms(Host),
|
2006-02-02 06:00:27 +01:00
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
|
|
|
gen_server:call(Proc, stop),
|
2013-06-19 13:30:31 +02:00
|
|
|
supervisor:delete_child(ejabberd_sup, Proc),
|
|
|
|
{wait, Rooms}.
|
|
|
|
|
2016-07-06 13:58:48 +02:00
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[{mod_mam, soft}].
|
|
|
|
|
2013-06-19 13:30:31 +02:00
|
|
|
shutdown_rooms(Host) ->
|
|
|
|
MyHost = gen_mod:get_module_opt_host(Host, mod_muc,
|
|
|
|
<<"conference.@HOST@">>),
|
|
|
|
Rooms = mnesia:dirty_select(muc_online_room,
|
|
|
|
[{#muc_online_room{name_host = '$1',
|
|
|
|
pid = '$2'},
|
2015-07-08 14:35:26 +02:00
|
|
|
[{'==', {element, 2, '$1'}, MyHost},
|
|
|
|
{'==', {node, '$2'}, node()}],
|
2013-06-19 13:30:31 +02:00
|
|
|
['$2']}]),
|
|
|
|
[Pid ! shutdown || Pid <- Rooms],
|
|
|
|
Rooms.
|
2006-02-02 06:00:27 +01:00
|
|
|
|
2008-02-28 01:30:23 +01:00
|
|
|
%% This function is called by a room in three situations:
|
|
|
|
%% A) The owner of the room destroyed it
|
|
|
|
%% B) The only participant of a temporary room leaves it
|
|
|
|
%% C) mod_muc:stop was called, and each room is being terminated
|
|
|
|
%% In this case, the mod_muc process died before the room processes
|
|
|
|
%% So the message sending must be catched
|
2007-01-19 05:46:44 +01:00
|
|
|
room_destroyed(Host, Room, Pid, ServerHost) ->
|
2008-02-28 01:30:23 +01:00
|
|
|
catch gen_mod:get_module_proc(ServerHost, ?PROCNAME) !
|
2013-03-14 10:33:02 +01:00
|
|
|
{room_destroyed, {Room, Host}, Pid},
|
2006-02-02 06:00:27 +01:00
|
|
|
ok.
|
|
|
|
|
2009-02-16 16:24:40 +01:00
|
|
|
%% @doc Create a room.
|
|
|
|
%% If Opts = default, the default room options are used.
|
|
|
|
%% Else use the passed options as defined in mod_muc_room.
|
|
|
|
create_room(Host, Name, From, Nick, Opts) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
|
|
|
gen_server:call(Proc, {create, Name, From, Nick, Opts}).
|
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
store_room(ServerHost, Host, Name, Opts) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LServer = jid:nameprep(ServerHost),
|
2016-04-13 20:07:32 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:store_room(LServer, Host, Name, Opts).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
|
|
|
restore_room(ServerHost, Host, Name) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LServer = jid:nameprep(ServerHost),
|
2016-04-13 20:07:32 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:restore_room(LServer, Host, Name).
|
2006-02-02 06:00:27 +01:00
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
forget_room(ServerHost, Host, Name) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LServer = jid:nameprep(ServerHost),
|
2016-07-09 11:43:01 +02:00
|
|
|
ejabberd_hooks:run(remove_room, LServer, [LServer, Name, Host]),
|
2016-04-13 20:07:32 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:forget_room(LServer, Host, Name).
|
2006-02-02 06:00:27 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
can_use_nick(_ServerHost, _Host, _JID, <<"">>) -> false;
|
2012-04-27 11:52:05 +02:00
|
|
|
can_use_nick(ServerHost, Host, JID, Nick) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LServer = jid:nameprep(ServerHost),
|
2016-04-13 20:07:32 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:can_use_nick(LServer, Host, JID, Nick).
|
2006-02-02 06:00:27 +01:00
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
|
|
|
|
|
|
|
init([Host, Opts]) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, fun gen_iq_handler:check_type/1,
|
|
|
|
one_queue),
|
2013-03-14 10:33:02 +01:00
|
|
|
MyHost = gen_mod:get_opt_host(Host, Opts,
|
|
|
|
<<"conference.@HOST@">>),
|
2016-04-13 20:07:32 +02:00
|
|
|
Mod = gen_mod:db_mod(Host, Opts, ?MODULE),
|
|
|
|
Mod:init(Host, [{host, MyHost}|Opts]),
|
2016-11-20 16:08:49 +01:00
|
|
|
update_tables(),
|
2016-11-30 11:09:17 +01:00
|
|
|
ejabberd_mnesia:create(?MODULE, muc_online_room,
|
2007-01-19 05:46:44 +01:00
|
|
|
[{ram_copies, [node()]},
|
2016-11-20 16:08:49 +01:00
|
|
|
{type, ordered_set},
|
2007-01-19 05:46:44 +01:00
|
|
|
{attributes, record_info(fields, muc_online_room)}]),
|
|
|
|
mnesia:add_table_copy(muc_online_room, node(), ram_copies),
|
2007-12-06 16:40:21 +01:00
|
|
|
catch ets:new(muc_online_users, [bag, named_table, public, {keypos, 2}]),
|
2007-05-09 16:47:58 +02:00
|
|
|
clean_table_from_bad_node(node(), MyHost),
|
2007-02-13 18:23:36 +01:00
|
|
|
mnesia:subscribe(system),
|
2015-05-28 10:37:27 +02:00
|
|
|
Access = gen_mod:get_opt(access, Opts,
|
2016-06-21 13:18:24 +02:00
|
|
|
fun acl:access_rules_validator/1, all),
|
2015-05-28 10:37:27 +02:00
|
|
|
AccessCreate = gen_mod:get_opt(access_create, Opts,
|
2016-06-21 13:18:24 +02:00
|
|
|
fun acl:access_rules_validator/1, all),
|
2015-05-28 10:37:27 +02:00
|
|
|
AccessAdmin = gen_mod:get_opt(access_admin, Opts,
|
2016-06-21 13:18:24 +02:00
|
|
|
fun acl:access_rules_validator/1,
|
2015-05-28 10:37:27 +02:00
|
|
|
none),
|
|
|
|
AccessPersistent = gen_mod:get_opt(access_persistent, Opts,
|
2016-06-21 13:18:24 +02:00
|
|
|
fun acl:access_rules_validator/1,
|
2015-05-28 10:37:27 +02:00
|
|
|
all),
|
|
|
|
HistorySize = gen_mod:get_opt(history_size, Opts,
|
|
|
|
fun(I) when is_integer(I), I>=0 -> I end,
|
|
|
|
20),
|
2016-10-18 22:59:34 +02:00
|
|
|
MaxRoomsDiscoItems = gen_mod:get_opt(max_rooms_discoitems, Opts,
|
|
|
|
fun(I) when is_integer(I), I>=0 -> I end,
|
|
|
|
100),
|
2015-05-28 10:37:27 +02:00
|
|
|
DefRoomOpts1 = gen_mod:get_opt(default_room_options, Opts,
|
|
|
|
fun(L) when is_list(L) -> L end,
|
|
|
|
[]),
|
|
|
|
DefRoomOpts =
|
2015-05-29 11:08:13 +02:00
|
|
|
lists:flatmap(
|
2015-05-28 10:37:27 +02:00
|
|
|
fun({Opt, Val}) ->
|
2015-05-29 11:08:13 +02:00
|
|
|
Bool = fun(B) when is_boolean(B) -> B end,
|
2015-05-28 10:37:27 +02:00
|
|
|
VFun = case Opt of
|
2015-05-29 11:08:13 +02:00
|
|
|
allow_change_subj -> Bool;
|
|
|
|
allow_private_messages -> Bool;
|
|
|
|
allow_query_users -> Bool;
|
|
|
|
allow_user_invites -> Bool;
|
|
|
|
allow_visitor_nickchange -> Bool;
|
|
|
|
allow_visitor_status -> Bool;
|
|
|
|
anonymous -> Bool;
|
|
|
|
captcha_protected -> Bool;
|
|
|
|
logging -> Bool;
|
|
|
|
members_by_default -> Bool;
|
|
|
|
members_only -> Bool;
|
|
|
|
moderated -> Bool;
|
|
|
|
password_protected -> Bool;
|
|
|
|
persistent -> Bool;
|
|
|
|
public -> Bool;
|
|
|
|
public_list -> Bool;
|
2015-08-06 12:33:39 +02:00
|
|
|
mam -> Bool;
|
2016-07-26 13:37:28 +02:00
|
|
|
allow_subscription -> Bool;
|
2015-05-28 10:37:27 +02:00
|
|
|
password -> fun iolist_to_binary/1;
|
|
|
|
title -> fun iolist_to_binary/1;
|
|
|
|
allow_private_messages_from_visitors ->
|
|
|
|
fun(anyone) -> anyone;
|
|
|
|
(moderators) -> moderators;
|
|
|
|
(nobody) -> nobody
|
|
|
|
end;
|
|
|
|
max_users ->
|
|
|
|
fun(I) when is_integer(I), I > 0 -> I end;
|
2015-12-21 00:23:50 +01:00
|
|
|
presence_broadcast ->
|
|
|
|
fun(L) ->
|
|
|
|
lists:map(
|
|
|
|
fun(moderator) -> moderator;
|
|
|
|
(participant) -> participant;
|
|
|
|
(visitor) -> visitor
|
|
|
|
end, L)
|
|
|
|
end;
|
2015-05-28 10:37:27 +02:00
|
|
|
_ ->
|
|
|
|
?ERROR_MSG("unknown option ~p with value ~p",
|
2015-05-29 11:08:13 +02:00
|
|
|
[Opt, Val]),
|
|
|
|
fun(_) -> undefined end
|
2015-05-28 10:37:27 +02:00
|
|
|
end,
|
2015-05-29 11:08:13 +02:00
|
|
|
case gen_mod:get_opt(Opt, [{Opt, Val}], VFun) of
|
|
|
|
undefined -> [];
|
|
|
|
NewVal -> [{Opt, NewVal}]
|
|
|
|
end
|
2015-05-28 10:37:27 +02:00
|
|
|
end, DefRoomOpts1),
|
|
|
|
RoomShaper = gen_mod:get_opt(room_shaper, Opts,
|
|
|
|
fun(A) when is_atom(A) -> A end,
|
|
|
|
none),
|
2016-07-25 12:50:30 +02:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, MyHost, ?NS_REGISTER,
|
|
|
|
?MODULE, process_register, IQDisc),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, MyHost, ?NS_VCARD,
|
|
|
|
?MODULE, process_vcard, IQDisc),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, MyHost, ?NS_MUCSUB,
|
|
|
|
?MODULE, process_mucsub, IQDisc),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, MyHost, ?NS_MUC_UNIQUE,
|
|
|
|
?MODULE, process_muc_unique, IQDisc),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, MyHost, ?NS_DISCO_INFO,
|
|
|
|
?MODULE, process_disco_info, IQDisc),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, MyHost, ?NS_DISCO_ITEMS,
|
|
|
|
?MODULE, process_disco_items, IQDisc),
|
2016-03-13 09:38:40 +01:00
|
|
|
ejabberd_router:register_route(MyHost, Host),
|
2007-09-01 23:05:04 +02:00
|
|
|
load_permanent_rooms(MyHost, Host,
|
|
|
|
{Access, AccessCreate, AccessAdmin, AccessPersistent},
|
2015-10-07 00:06:58 +02:00
|
|
|
HistorySize, RoomShaper),
|
2006-02-02 06:00:27 +01:00
|
|
|
{ok, #state{host = MyHost,
|
|
|
|
server_host = Host,
|
2007-06-20 13:25:19 +02:00
|
|
|
access = {Access, AccessCreate, AccessAdmin, AccessPersistent},
|
2007-08-02 04:30:25 +02:00
|
|
|
default_room_opts = DefRoomOpts,
|
2007-09-01 23:05:04 +02:00
|
|
|
history_size = HistorySize,
|
2016-10-18 22:59:34 +02:00
|
|
|
max_rooms_discoitems = MaxRoomsDiscoItems,
|
2007-09-01 23:05:04 +02:00
|
|
|
room_shaper = RoomShaper}}.
|
2006-02-02 06:00:27 +01:00
|
|
|
|
|
|
|
handle_call(stop, _From, State) ->
|
2009-02-16 16:24:40 +01:00
|
|
|
{stop, normal, ok, State};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_call({create, Room, From, Nick, Opts}, _From,
|
|
|
|
#state{host = Host, server_host = ServerHost,
|
|
|
|
access = Access, default_room_opts = DefOpts,
|
2009-02-16 16:24:40 +01:00
|
|
|
history_size = HistorySize,
|
|
|
|
room_shaper = RoomShaper} = State) ->
|
|
|
|
?DEBUG("MUC: create new room '~s'~n", [Room]),
|
|
|
|
NewOpts = case Opts of
|
2013-03-14 10:33:02 +01:00
|
|
|
default -> DefOpts;
|
|
|
|
_ -> Opts
|
2009-02-16 16:24:40 +01:00
|
|
|
end,
|
|
|
|
{ok, Pid} = mod_muc_room:start(
|
|
|
|
Host, ServerHost, Access,
|
|
|
|
Room, HistorySize,
|
|
|
|
RoomShaper, From,
|
2012-04-27 11:52:05 +02:00
|
|
|
Nick, NewOpts),
|
2009-02-16 16:24:40 +01:00
|
|
|
register_room(Host, Room, Pid),
|
|
|
|
{reply, ok, State}.
|
2006-02-02 06:00:27 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_cast(_Msg, State) -> {noreply, State}.
|
2006-02-02 06:00:27 +01:00
|
|
|
|
|
|
|
handle_info({route, From, To, Packet},
|
2013-03-14 10:33:02 +01:00
|
|
|
#state{host = Host, server_host = ServerHost,
|
|
|
|
access = Access, default_room_opts = DefRoomOpts,
|
2007-09-01 23:05:04 +02:00
|
|
|
history_size = HistorySize,
|
2016-10-18 22:59:34 +02:00
|
|
|
max_rooms_discoitems = MaxRoomsDiscoItems,
|
2007-09-01 23:05:04 +02:00
|
|
|
room_shaper = RoomShaper} = State) ->
|
|
|
|
case catch do_route(Host, ServerHost, Access, HistorySize, RoomShaper,
|
2016-10-18 22:59:34 +02:00
|
|
|
From, To, Packet, DefRoomOpts, MaxRoomsDiscoItems) of
|
2006-02-02 06:00:27 +01:00
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p", [Reason]);
|
2003-03-23 21:08:44 +01:00
|
|
|
_ ->
|
2006-02-02 06:00:27 +01:00
|
|
|
ok
|
|
|
|
end,
|
|
|
|
{noreply, State};
|
2007-01-19 05:46:44 +01:00
|
|
|
handle_info({room_destroyed, RoomHost, Pid}, State) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () ->
|
|
|
|
mnesia:delete_object(#muc_online_room{name_host =
|
|
|
|
RoomHost,
|
2007-01-19 05:46:44 +01:00
|
|
|
pid = Pid})
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F),
|
2006-02-02 06:00:27 +01:00
|
|
|
{noreply, State};
|
2007-02-13 18:23:36 +01:00
|
|
|
handle_info({mnesia_system_event, {mnesia_down, Node}}, State) ->
|
|
|
|
clean_table_from_bad_node(Node),
|
|
|
|
{noreply, State};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info(_Info, State) -> {noreply, State}.
|
2006-02-02 06:00:27 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
terminate(_Reason, #state{host = MyHost}) ->
|
|
|
|
ejabberd_router:unregister_route(MyHost),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, MyHost, ?NS_REGISTER),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, MyHost, ?NS_VCARD),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, MyHost, ?NS_MUCSUB),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, MyHost, ?NS_MUC_UNIQUE),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, MyHost, ?NS_DISCO_INFO),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, MyHost, ?NS_DISCO_ITEMS),
|
2006-02-02 06:00:27 +01:00
|
|
|
ok.
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
code_change(_OldVsn, State, _Extra) -> {ok, State}.
|
2006-02-02 06:00:27 +01:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%--------------------------------------------------------------------
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2007-09-01 23:05:04 +02:00
|
|
|
do_route(Host, ServerHost, Access, HistorySize, RoomShaper,
|
2016-11-12 11:27:15 +01:00
|
|
|
From, To, Packet, DefRoomOpts, _MaxRoomsDiscoItems) ->
|
2007-08-02 04:30:25 +02:00
|
|
|
{AccessRoute, _AccessCreate, _AccessAdmin, _AccessPersistent} = Access,
|
2005-06-20 05:18:13 +02:00
|
|
|
case acl:match_rule(ServerHost, AccessRoute, From) of
|
2004-05-17 22:36:41 +02:00
|
|
|
allow ->
|
2007-09-01 23:05:04 +02:00
|
|
|
do_route1(Host, ServerHost, Access, HistorySize, RoomShaper,
|
2016-11-12 11:27:15 +01:00
|
|
|
From, To, Packet, DefRoomOpts);
|
2016-07-25 12:50:30 +02:00
|
|
|
deny ->
|
|
|
|
Lang = xmpp:get_lang(Packet),
|
2013-03-14 10:33:02 +01:00
|
|
|
ErrText = <<"Access denied by service policy">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_forbidden(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(To, From, Packet, Err)
|
2004-05-17 22:36:41 +02:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
do_route1(_Host, _ServerHost, _Access, _HistorySize, _RoomShaper,
|
|
|
|
From, #jid{luser = <<"">>, lresource = <<"">>} = To,
|
|
|
|
#iq{} = IQ, _DefRoomOpts) ->
|
|
|
|
ejabberd_local:process_iq(From, To, IQ);
|
|
|
|
do_route1(Host, ServerHost, Access, _HistorySize, _RoomShaper,
|
|
|
|
From, #jid{luser = <<"">>, lresource = <<"">>} = To,
|
|
|
|
#message{lang = Lang, body = Body, type = Type} = Packet, _) ->
|
|
|
|
{_AccessRoute, _AccessCreate, AccessAdmin, _AccessPersistent} = Access,
|
|
|
|
if Type == error ->
|
|
|
|
ok;
|
|
|
|
true ->
|
|
|
|
case acl:match_rule(ServerHost, AccessAdmin, From) of
|
|
|
|
allow ->
|
|
|
|
Msg = xmpp:get_text(Body),
|
|
|
|
broadcast_service_message(Host, Msg);
|
|
|
|
deny ->
|
|
|
|
ErrText = <<"Only service administrators are allowed "
|
|
|
|
"to send service messages">>,
|
2016-11-25 09:41:24 +01:00
|
|
|
Err = xmpp:err_forbidden(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(To, From, Packet, Err)
|
2016-07-25 12:50:30 +02:00
|
|
|
end
|
|
|
|
end;
|
|
|
|
do_route1(_Host, _ServerHost, _Access, _HistorySize, _RoomShaper,
|
|
|
|
From, #jid{luser = <<"">>} = To, Packet, _DefRoomOpts) ->
|
2016-10-17 12:37:23 +02:00
|
|
|
Err = xmpp:err_service_unavailable(),
|
2016-07-25 12:50:30 +02:00
|
|
|
ejabberd_router:route_error(To, From, Packet, Err);
|
2007-09-01 23:05:04 +02:00
|
|
|
do_route1(Host, ServerHost, Access, HistorySize, RoomShaper,
|
|
|
|
From, To, Packet, DefRoomOpts) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
{_AccessRoute, AccessCreate, _AccessAdmin, _AccessPersistent} = Access,
|
2015-11-24 16:44:13 +01:00
|
|
|
{Room, _, Nick} = jid:tolower(To),
|
2016-07-25 12:50:30 +02:00
|
|
|
case mnesia:dirty_read(muc_online_room, {Room, Host}) of
|
|
|
|
[] ->
|
2016-09-07 10:15:19 +02:00
|
|
|
case is_create_request(Packet) of
|
|
|
|
true ->
|
2016-07-25 12:50:30 +02:00
|
|
|
case check_user_can_create_room(
|
|
|
|
ServerHost, AccessCreate, From, Room) and
|
|
|
|
check_create_roomid(ServerHost, Room) of
|
|
|
|
true ->
|
|
|
|
{ok, Pid} = start_new_room(
|
|
|
|
Host, ServerHost, Access,
|
|
|
|
Room, HistorySize,
|
|
|
|
RoomShaper, From, Nick, DefRoomOpts),
|
|
|
|
register_room(Host, Room, Pid),
|
|
|
|
mod_muc_room:route(Pid, From, Nick, Packet),
|
|
|
|
ok;
|
|
|
|
false ->
|
2016-09-07 10:15:19 +02:00
|
|
|
Lang = xmpp:get_lang(Packet),
|
2016-07-25 12:50:30 +02:00
|
|
|
ErrText = <<"Room creation is denied by service policy">>,
|
2016-11-25 09:41:24 +01:00
|
|
|
Err = xmpp:err_forbidden(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(To, From, Packet, Err)
|
2015-10-07 00:06:58 +02:00
|
|
|
end;
|
2016-09-07 10:15:19 +02:00
|
|
|
false ->
|
2016-07-25 12:50:30 +02:00
|
|
|
Lang = xmpp:get_lang(Packet),
|
|
|
|
ErrText = <<"Conference room does not exist">>,
|
|
|
|
Err = xmpp:err_item_not_found(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(To, From, Packet, Err)
|
|
|
|
end;
|
|
|
|
[R] ->
|
|
|
|
Pid = R#muc_online_room.pid,
|
|
|
|
?DEBUG("MUC: send to process ~p~n", [Pid]),
|
|
|
|
mod_muc_room:route(Pid, From, Nick, Packet),
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec process_vcard(iq()) -> iq().
|
2016-10-17 12:37:23 +02:00
|
|
|
process_vcard(#iq{type = get, lang = Lang, sub_els = [#vcard_temp{}]} = IQ) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
Desc = translate:translate(Lang, <<"ejabberd MUC module">>),
|
|
|
|
xmpp:make_iq_result(
|
|
|
|
IQ, #vcard_temp{fn = <<"ejabberd/mod_muc">>,
|
|
|
|
url = ?EJABBERD_URI,
|
2016-11-23 13:51:48 +01:00
|
|
|
desc = <<Desc/binary, $\n, ?COPYRIGHT>>});
|
2016-07-25 12:50:30 +02:00
|
|
|
process_vcard(#iq{type = set, lang = Lang} = IQ) ->
|
|
|
|
Txt = <<"Value 'set' of 'type' attribute is not allowed">>,
|
2016-10-17 12:37:23 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang));
|
|
|
|
process_vcard(#iq{lang = Lang} = IQ) ->
|
|
|
|
Txt = <<"No module is handling this query">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_service_unavailable(Txt, Lang)).
|
2016-07-25 12:50:30 +02:00
|
|
|
|
|
|
|
-spec process_register(iq()) -> iq().
|
2016-10-17 12:37:23 +02:00
|
|
|
process_register(#iq{type = get, from = From, to = To, lang = Lang,
|
|
|
|
sub_els = [#register{}]} = IQ) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
Host = To#jid.lserver,
|
|
|
|
ServerHost = ejabberd_router:host_of_route(Host),
|
|
|
|
xmpp:make_iq_result(IQ, iq_get_register_info(ServerHost, Host, From, Lang));
|
|
|
|
process_register(#iq{type = set, from = From, to = To,
|
2016-10-17 12:37:23 +02:00
|
|
|
lang = Lang, sub_els = [El = #register{}]} = IQ) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
Host = To#jid.lserver,
|
|
|
|
ServerHost = ejabberd_router:host_of_route(Host),
|
|
|
|
case process_iq_register_set(ServerHost, Host, From, El, Lang) of
|
|
|
|
{result, Result} ->
|
|
|
|
xmpp:make_iq_result(IQ, Result);
|
|
|
|
{error, Err} ->
|
|
|
|
xmpp:make_error(IQ, Err)
|
2003-03-23 21:08:44 +01:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec process_disco_info(iq()) -> iq().
|
|
|
|
process_disco_info(#iq{type = set, lang = Lang} = IQ) ->
|
|
|
|
Txt = <<"Value 'set' of 'type' attribute is not allowed">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang));
|
|
|
|
process_disco_info(#iq{type = get, to = To, lang = Lang,
|
2016-08-05 07:41:08 +02:00
|
|
|
sub_els = [#disco_info{node = <<"">>}]} = IQ) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
ServerHost = ejabberd_router:host_of_route(To#jid.lserver),
|
|
|
|
X = ejabberd_hooks:run_fold(disco_info, ServerHost, [],
|
2016-08-05 07:41:08 +02:00
|
|
|
[ServerHost, ?MODULE, <<"">>, Lang]),
|
2016-07-25 12:50:30 +02:00
|
|
|
MAMFeatures = case gen_mod:is_loaded(ServerHost, mod_mam) of
|
|
|
|
true -> [?NS_MAM_TMP, ?NS_MAM_0, ?NS_MAM_1];
|
|
|
|
false -> []
|
|
|
|
end,
|
|
|
|
Features = [?NS_DISCO_INFO, ?NS_DISCO_ITEMS,
|
|
|
|
?NS_REGISTER, ?NS_MUC, ?NS_RSM,
|
|
|
|
?NS_VCARD, ?NS_MUCSUB, ?NS_MUC_UNIQUE | MAMFeatures],
|
|
|
|
Identity = #identity{category = <<"conference">>,
|
|
|
|
type = <<"text">>,
|
|
|
|
name = translate:translate(Lang, <<"Chatrooms">>)},
|
|
|
|
xmpp:make_iq_result(
|
|
|
|
IQ, #disco_info{features = Features,
|
|
|
|
identities = [Identity],
|
|
|
|
xdata = X});
|
2016-10-17 12:37:23 +02:00
|
|
|
process_disco_info(#iq{type = get, lang = Lang,
|
|
|
|
sub_els = [#disco_info{}]} = IQ) ->
|
|
|
|
xmpp:make_error(IQ, xmpp:err_item_not_found(<<"Node not found">>, Lang));
|
|
|
|
process_disco_info(#iq{lang = Lang} = IQ) ->
|
|
|
|
Txt = <<"No module is handling this query">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_service_unavailable(Txt, Lang)).
|
2016-07-25 12:50:30 +02:00
|
|
|
|
|
|
|
-spec process_disco_items(iq()) -> iq().
|
|
|
|
process_disco_items(#iq{type = set, lang = Lang} = IQ) ->
|
|
|
|
Txt = <<"Value 'set' of 'type' attribute is not allowed">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang));
|
|
|
|
process_disco_items(#iq{type = get, from = From, to = To, lang = Lang,
|
|
|
|
sub_els = [#disco_items{node = Node, rsm = RSM}]} = IQ) ->
|
|
|
|
Host = To#jid.lserver,
|
2016-11-12 11:27:15 +01:00
|
|
|
ServerHost = ejabberd_router:host_of_route(Host),
|
|
|
|
MaxRoomsDiscoItems = gen_mod:get_module_opt(
|
|
|
|
ServerHost, ?MODULE, max_rooms_discoitems,
|
|
|
|
fun(I) when is_integer(I), I>=0 -> I end,
|
|
|
|
100),
|
2016-11-20 16:08:49 +01:00
|
|
|
case iq_disco_items(Host, From, Lang, MaxRoomsDiscoItems, Node, RSM) of
|
|
|
|
{error, Err} ->
|
|
|
|
xmpp:make_error(IQ, Err);
|
|
|
|
{result, Result} ->
|
|
|
|
xmpp:make_iq_result(IQ, Result)
|
|
|
|
end;
|
2016-10-17 12:37:23 +02:00
|
|
|
process_disco_items(#iq{lang = Lang} = IQ) ->
|
|
|
|
Txt = <<"No module is handling this query">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_service_unavailable(Txt, Lang)).
|
2016-07-25 12:50:30 +02:00
|
|
|
|
|
|
|
-spec process_muc_unique(iq()) -> iq().
|
|
|
|
process_muc_unique(#iq{type = set, lang = Lang} = IQ) ->
|
|
|
|
Txt = <<"Value 'set' of 'type' attribute is not allowed">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang));
|
2016-10-17 12:37:23 +02:00
|
|
|
process_muc_unique(#iq{from = From, type = get,
|
|
|
|
sub_els = [#muc_unique{}]} = IQ) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
Name = p1_sha:sha(term_to_binary([From, p1_time_compat:timestamp(),
|
|
|
|
randoms:get_string()])),
|
|
|
|
xmpp:make_iq_result(IQ, #muc_unique{name = Name}).
|
|
|
|
|
|
|
|
-spec process_mucsub(iq()) -> iq().
|
|
|
|
process_mucsub(#iq{type = set, lang = Lang} = IQ) ->
|
|
|
|
Txt = <<"Value 'set' of 'type' attribute is not allowed">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang));
|
2016-10-17 12:37:23 +02:00
|
|
|
process_mucsub(#iq{type = get, from = From, to = To,
|
|
|
|
sub_els = [#muc_subscriptions{}]} = IQ) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
Host = To#jid.lserver,
|
|
|
|
ServerHost = ejabberd_router:host_of_route(Host),
|
|
|
|
RoomJIDs = get_subscribed_rooms(ServerHost, Host, From),
|
2016-10-17 12:37:23 +02:00
|
|
|
xmpp:make_iq_result(IQ, #muc_subscriptions{list = RoomJIDs});
|
|
|
|
process_mucsub(#iq{lang = Lang} = IQ) ->
|
|
|
|
Txt = <<"No module is handling this query">>,
|
|
|
|
xmpp:make_error(IQ, xmpp:err_service_unavailable(Txt, Lang)).
|
2016-07-25 12:50:30 +02:00
|
|
|
|
2016-09-07 10:15:19 +02:00
|
|
|
-spec is_create_request(stanza()) -> boolean().
|
|
|
|
is_create_request(#presence{type = available}) ->
|
|
|
|
true;
|
2016-10-17 12:37:23 +02:00
|
|
|
is_create_request(#iq{type = T} = IQ) when T == get; T == set ->
|
|
|
|
xmpp:has_subtag(IQ, #muc_subscribe{}) orelse
|
|
|
|
xmpp:has_subtag(IQ, #muc_owner{});
|
2016-09-07 10:15:19 +02:00
|
|
|
is_create_request(_) ->
|
|
|
|
false.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
check_user_can_create_room(ServerHost, AccessCreate,
|
2016-01-14 15:08:31 +01:00
|
|
|
From, _RoomID) ->
|
2009-04-22 14:05:10 +02:00
|
|
|
case acl:match_rule(ServerHost, AccessCreate, From) of
|
2016-01-14 15:08:31 +01:00
|
|
|
allow -> true;
|
2013-03-14 10:33:02 +01:00
|
|
|
_ -> false
|
2009-04-22 14:05:10 +02:00
|
|
|
end.
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-01-14 15:08:31 +01:00
|
|
|
check_create_roomid(ServerHost, RoomID) ->
|
|
|
|
Max = gen_mod:get_module_opt(ServerHost, ?MODULE, max_room_id,
|
|
|
|
fun(infinity) -> infinity;
|
|
|
|
(I) when is_integer(I), I>0 -> I
|
|
|
|
end, infinity),
|
2016-02-05 12:08:40 +01:00
|
|
|
Regexp = gen_mod:get_module_opt(ServerHost, ?MODULE, regexp_room_id,
|
2016-01-14 15:08:31 +01:00
|
|
|
fun iolist_to_binary/1, ""),
|
|
|
|
(byte_size(RoomID) =< Max) and
|
|
|
|
(re:run(RoomID, Regexp, [unicode, {capture, none}]) == match).
|
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
get_rooms(ServerHost, Host) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LServer = jid:nameprep(ServerHost),
|
2016-04-13 20:07:32 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:get_rooms(LServer, Host).
|
2003-03-25 22:03:35 +01:00
|
|
|
|
2015-10-07 00:06:58 +02:00
|
|
|
load_permanent_rooms(Host, ServerHost, Access,
|
|
|
|
HistorySize, RoomShaper) ->
|
2012-04-27 11:52:05 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(R) ->
|
2015-10-07 00:06:58 +02:00
|
|
|
{Room, Host} = R#muc_room.name_host,
|
|
|
|
case mnesia:dirty_read(muc_online_room, {Room, Host}) of
|
|
|
|
[] ->
|
|
|
|
{ok, Pid} = mod_muc_room:start(Host,
|
|
|
|
ServerHost, Access, Room,
|
|
|
|
HistorySize, RoomShaper,
|
|
|
|
R#muc_room.opts),
|
|
|
|
register_room(Host, Room, Pid);
|
|
|
|
_ -> ok
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
get_rooms(ServerHost, Host)).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
2009-08-26 06:14:54 +02:00
|
|
|
start_new_room(Host, ServerHost, Access, Room,
|
2015-10-07 00:06:58 +02:00
|
|
|
HistorySize, RoomShaper, From,
|
|
|
|
Nick, DefRoomOpts) ->
|
2014-01-23 15:39:52 +01:00
|
|
|
case restore_room(ServerHost, Host, Room) of
|
2015-10-07 00:06:58 +02:00
|
|
|
error ->
|
2009-08-26 06:14:54 +02:00
|
|
|
?DEBUG("MUC: open new room '~s'~n", [Room]),
|
2015-10-07 00:06:58 +02:00
|
|
|
mod_muc_room:start(Host, ServerHost, Access, Room,
|
|
|
|
HistorySize, RoomShaper,
|
|
|
|
From, Nick, DefRoomOpts);
|
|
|
|
Opts ->
|
2009-08-26 06:14:54 +02:00
|
|
|
?DEBUG("MUC: restore room '~s'~n", [Room]),
|
2015-10-07 00:06:58 +02:00
|
|
|
mod_muc_room:start(Host, ServerHost, Access, Room,
|
|
|
|
HistorySize, RoomShaper, Opts)
|
2009-08-26 06:14:54 +02:00
|
|
|
end.
|
|
|
|
|
2007-01-19 05:46:44 +01:00
|
|
|
register_room(Host, Room, Pid) ->
|
|
|
|
F = fun() ->
|
2015-10-07 00:06:58 +02:00
|
|
|
mnesia:write(#muc_online_room{name_host = {Room, Host},
|
|
|
|
pid = Pid})
|
|
|
|
end,
|
2007-01-19 05:46:44 +01:00
|
|
|
mnesia:transaction(F).
|
|
|
|
|
2016-11-20 16:08:49 +01:00
|
|
|
-spec iq_disco_items(binary(), jid(), binary(), integer(), binary(),
|
|
|
|
rsm_set() | undefined) ->
|
|
|
|
{result, disco_items()} | {error, stanza_error()}.
|
|
|
|
iq_disco_items(Host, From, Lang, MaxRoomsDiscoItems, Node, RSM)
|
|
|
|
when Node == <<"">>; Node == <<"nonemptyrooms">>; Node == <<"emptyrooms">> ->
|
|
|
|
Count = get_vh_rooms_count(Host),
|
|
|
|
Query = if Node == <<"">>, RSM == undefined, Count > MaxRoomsDiscoItems ->
|
|
|
|
{get_disco_item, only_non_empty, From, Lang};
|
|
|
|
Node == <<"nonemptyrooms">> ->
|
|
|
|
{get_disco_item, only_non_empty, From, Lang};
|
|
|
|
Node == <<"emptyrooms">> ->
|
|
|
|
{get_disco_item, 0, From, Lang};
|
|
|
|
true ->
|
|
|
|
{get_disco_item, all, From, Lang}
|
|
|
|
end,
|
|
|
|
Items = get_vh_rooms(Host, Query, RSM),
|
|
|
|
ResRSM = case Items of
|
|
|
|
[_|_] when RSM /= undefined ->
|
|
|
|
#disco_item{jid = #jid{luser = First}} = hd(Items),
|
|
|
|
#disco_item{jid = #jid{luser = Last}} = lists:last(Items),
|
|
|
|
#rsm_set{first = #rsm_first{data = First},
|
|
|
|
last = Last,
|
|
|
|
count = Count};
|
|
|
|
[] when RSM /= undefined ->
|
|
|
|
#rsm_set{count = Count};
|
|
|
|
_ ->
|
|
|
|
undefined
|
|
|
|
end,
|
|
|
|
{result, #disco_items{node = Node, items = Items, rsm = ResRSM}};
|
|
|
|
iq_disco_items(_Host, _From, Lang, _MaxRoomsDiscoItems, _Node, _RSM) ->
|
|
|
|
{error, xmpp:err_item_not_found(<<"Node not found">>, Lang)}.
|
|
|
|
|
|
|
|
-spec get_vh_rooms(binary, term(), rsm_set() | undefined) -> [disco_item()].
|
|
|
|
get_vh_rooms(Host, Query,
|
|
|
|
#rsm_set{max = Max, 'after' = After, before = undefined})
|
|
|
|
when is_binary(After), After /= <<"">> ->
|
2016-11-21 10:14:24 +01:00
|
|
|
lists:reverse(get_vh_rooms(next, {After, Host}, Host, Query, 0, Max, []));
|
2016-11-20 16:08:49 +01:00
|
|
|
get_vh_rooms(Host, Query,
|
|
|
|
#rsm_set{max = Max, 'after' = undefined, before = Before})
|
|
|
|
when is_binary(Before), Before /= <<"">> ->
|
2016-11-21 10:14:24 +01:00
|
|
|
get_vh_rooms(prev, {Before, Host}, Host, Query, 0, Max, []);
|
2016-11-20 16:08:49 +01:00
|
|
|
get_vh_rooms(Host, Query,
|
|
|
|
#rsm_set{max = Max, 'after' = undefined, before = <<"">>}) ->
|
2016-11-21 10:14:24 +01:00
|
|
|
get_vh_rooms(last, {<<"">>, Host}, Host, Query, 0, Max, []);
|
2016-11-20 16:08:49 +01:00
|
|
|
get_vh_rooms(Host, Query, #rsm_set{max = Max}) ->
|
2016-11-21 10:14:24 +01:00
|
|
|
lists:reverse(get_vh_rooms(first, {<<"">>, Host}, Host, Query, 0, Max, []));
|
2016-11-20 16:08:49 +01:00
|
|
|
get_vh_rooms(Host, Query, undefined) ->
|
2016-11-21 10:14:24 +01:00
|
|
|
lists:reverse(get_vh_rooms(first, {<<"">>, Host}, Host, Query, 0, undefined, [])).
|
2016-11-20 16:08:49 +01:00
|
|
|
|
|
|
|
-spec get_vh_rooms(prev | next | last | first,
|
2016-11-21 10:14:24 +01:00
|
|
|
{binary(), binary()}, binary(), term(),
|
2016-11-20 16:08:49 +01:00
|
|
|
non_neg_integer(), non_neg_integer() | undefined,
|
|
|
|
[disco_item()]) -> [disco_item()].
|
2016-11-21 10:14:24 +01:00
|
|
|
get_vh_rooms(_Action, _Key, _Host, _Query, Count, Max, Items) when Count >= Max ->
|
2016-11-20 16:08:49 +01:00
|
|
|
Items;
|
2016-11-21 10:14:24 +01:00
|
|
|
get_vh_rooms(Action, Key, Host, Query, Count, Max, Items) ->
|
2016-11-20 16:08:49 +01:00
|
|
|
Call = fun() ->
|
|
|
|
case Action of
|
|
|
|
prev -> mnesia:dirty_prev(muc_online_room, Key);
|
|
|
|
next -> mnesia:dirty_next(muc_online_room, Key);
|
|
|
|
last -> mnesia:dirty_last(muc_online_room);
|
|
|
|
first -> mnesia:dirty_first(muc_online_room)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
NewAction = case Action of
|
|
|
|
last -> prev;
|
|
|
|
first -> next;
|
|
|
|
_ -> Action
|
|
|
|
end,
|
|
|
|
try Call() of
|
|
|
|
'$end_of_table' ->
|
|
|
|
Items;
|
|
|
|
{_, Host} = NewKey ->
|
|
|
|
case get_room_disco_item(NewKey, Query) of
|
|
|
|
{ok, Item} ->
|
2016-11-21 10:14:24 +01:00
|
|
|
get_vh_rooms(NewAction, NewKey, Host, Query,
|
2016-11-20 16:08:49 +01:00
|
|
|
Count + 1, Max, [Item|Items]);
|
|
|
|
{error, _} ->
|
2016-11-21 10:14:24 +01:00
|
|
|
get_vh_rooms(NewAction, NewKey, Host, Query,
|
2016-11-20 16:08:49 +01:00
|
|
|
Count, Max, Items)
|
|
|
|
end;
|
|
|
|
NewKey ->
|
2016-11-21 10:14:24 +01:00
|
|
|
get_vh_rooms(NewAction, NewKey, Host, Query, Count, Max, Items)
|
2016-11-20 16:08:49 +01:00
|
|
|
catch _:{aborted, {badarg, _}} ->
|
|
|
|
Items
|
|
|
|
end.
|
2016-07-25 12:50:30 +02:00
|
|
|
|
2016-11-20 16:08:49 +01:00
|
|
|
-spec get_room_disco_item({binary(), binary()}, term()) -> {ok, disco_item()} |
|
|
|
|
{error, timeout | notfound}.
|
|
|
|
get_room_disco_item({Name, Host}, Query) ->
|
|
|
|
case mnesia:dirty_read(muc_online_room, {Name, Host}) of
|
|
|
|
[#muc_online_room{pid = Pid}|_] ->
|
|
|
|
RoomJID = jid:make(Name, Host),
|
|
|
|
try gen_fsm:sync_send_all_state_event(Pid, Query, 100) of
|
|
|
|
{item, Desc} ->
|
|
|
|
{ok, #disco_item{jid = RoomJID, name = Desc}};
|
|
|
|
false ->
|
|
|
|
{error, notfound}
|
|
|
|
catch _:{timeout, _} ->
|
|
|
|
{error, timeout};
|
|
|
|
_:{noproc, _} ->
|
|
|
|
{error, notfound}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
{error, notfound}
|
|
|
|
end.
|
2008-12-23 14:04:42 +01:00
|
|
|
|
2016-10-17 12:37:23 +02:00
|
|
|
get_subscribed_rooms(_ServerHost, Host, From) ->
|
|
|
|
Rooms = get_vh_rooms(Host),
|
2016-09-09 12:04:47 +02:00
|
|
|
BareFrom = jid:remove_resource(From),
|
2016-07-07 13:53:15 +02:00
|
|
|
lists:flatmap(
|
2016-10-17 12:37:23 +02:00
|
|
|
fun(#muc_online_room{name_host = {Name, _}, pid = Pid}) ->
|
2016-09-19 13:46:01 +02:00
|
|
|
case gen_fsm:sync_send_all_state_event(Pid, {is_subscribed, BareFrom}) of
|
2016-10-17 12:37:23 +02:00
|
|
|
true -> [jid:make(Name, Host)];
|
2016-07-07 13:53:15 +02:00
|
|
|
false -> []
|
|
|
|
end;
|
|
|
|
(_) ->
|
|
|
|
[]
|
|
|
|
end, Rooms).
|
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
get_nick(ServerHost, Host, From) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LServer = jid:nameprep(ServerHost),
|
2016-04-13 20:07:32 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:get_nick(LServer, Host, From).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
|
|
|
iq_get_register_info(ServerHost, Host, From, Lang) ->
|
2016-10-07 09:31:03 +02:00
|
|
|
{Nick, Registered} = case get_nick(ServerHost, Host, From) of
|
|
|
|
error -> {<<"">>, false};
|
|
|
|
N -> {N, true}
|
|
|
|
end,
|
2016-07-25 12:50:30 +02:00
|
|
|
Title = <<(translate:translate(
|
|
|
|
Lang, <<"Nickname Registration at ">>))/binary, Host/binary>>,
|
|
|
|
Inst = translate:translate(Lang, <<"Enter nickname you want to register">>),
|
2016-10-07 09:31:03 +02:00
|
|
|
Fields = muc_register:encode(
|
|
|
|
[{roomnick, Nick}],
|
|
|
|
fun(T) -> translate:translate(Lang, T) end),
|
2016-07-25 12:50:30 +02:00
|
|
|
X = #xdata{type = form, title = Title,
|
2016-10-07 09:31:03 +02:00
|
|
|
instructions = [Inst], fields = Fields},
|
2016-07-25 12:50:30 +02:00
|
|
|
#register{nick = Nick,
|
|
|
|
registered = Registered,
|
|
|
|
instructions =
|
|
|
|
translate:translate(
|
|
|
|
Lang, <<"You need a client that supports x:data "
|
|
|
|
"to register the nickname">>),
|
|
|
|
xdata = X}.
|
2003-04-17 21:47:43 +02:00
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
set_nick(ServerHost, Host, From, Nick) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LServer = jid:nameprep(ServerHost),
|
2016-04-13 20:07:32 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:set_nick(LServer, Host, From, Nick).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
iq_set_register_info(ServerHost, Host, From, Nick,
|
|
|
|
Lang) ->
|
2012-04-27 11:52:05 +02:00
|
|
|
case set_nick(ServerHost, Host, From, Nick) of
|
2016-07-25 12:50:30 +02:00
|
|
|
{atomic, ok} -> {result, undefined};
|
2013-03-14 10:33:02 +01:00
|
|
|
{atomic, false} ->
|
|
|
|
ErrText = <<"That nickname is registered by another "
|
|
|
|
"person">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, xmpp:err_conflict(ErrText, Lang)};
|
2016-03-31 10:00:29 +02:00
|
|
|
_ ->
|
|
|
|
Txt = <<"Database failure">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, xmpp:err_internal_server_error(Txt, Lang)}
|
2003-04-17 21:47:43 +02:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
process_iq_register_set(ServerHost, Host, From,
|
|
|
|
#register{remove = true}, Lang) ->
|
|
|
|
iq_set_register_info(ServerHost, Host, From, <<"">>, Lang);
|
|
|
|
process_iq_register_set(_ServerHost, _Host, _From,
|
|
|
|
#register{xdata = #xdata{type = cancel}}, _Lang) ->
|
|
|
|
{result, undefined};
|
|
|
|
process_iq_register_set(ServerHost, Host, From,
|
|
|
|
#register{nick = Nick, xdata = XData}, Lang) ->
|
|
|
|
case XData of
|
|
|
|
#xdata{type = submit, fields = Fs} ->
|
2016-10-07 09:31:03 +02:00
|
|
|
try
|
|
|
|
Options = muc_register:decode(Fs),
|
|
|
|
N = proplists:get_value(roomnick, Options),
|
|
|
|
iq_set_register_info(ServerHost, Host, From, N, Lang)
|
|
|
|
catch _:{muc_register, Why} ->
|
|
|
|
ErrText = muc_register:format_error(Why),
|
|
|
|
{error, xmpp:err_bad_request(ErrText, Lang)}
|
2016-07-25 12:50:30 +02:00
|
|
|
end;
|
|
|
|
#xdata{} ->
|
|
|
|
Txt = <<"Incorrect data form">>,
|
|
|
|
{error, xmpp:err_bad_request(Txt, Lang)};
|
|
|
|
_ when is_binary(Nick), Nick /= <<"">> ->
|
|
|
|
iq_set_register_info(ServerHost, Host, From, Nick, Lang);
|
|
|
|
_ ->
|
|
|
|
ErrText = <<"You must fill in field \"Nickname\" in the form">>,
|
|
|
|
{error, xmpp:err_not_acceptable(ErrText, Lang)}
|
2003-04-17 21:47:43 +02:00
|
|
|
end.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
broadcast_service_message(Host, Msg) ->
|
2003-05-18 18:41:15 +02:00
|
|
|
lists:foreach(
|
2015-10-07 00:06:58 +02:00
|
|
|
fun(#muc_online_room{pid = Pid}) ->
|
|
|
|
gen_fsm:send_all_state_event(
|
|
|
|
Pid, {service_message, Msg})
|
|
|
|
end, get_vh_rooms(Host)).
|
2003-05-18 18:41:15 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_vh_rooms(Host) ->
|
2007-01-19 05:46:44 +01:00
|
|
|
mnesia:dirty_select(muc_online_room,
|
|
|
|
[{#muc_online_room{name_host = '$1', _ = '_'},
|
|
|
|
[{'==', {element, 2, '$1'}, Host}],
|
|
|
|
['$_']}]).
|
2003-05-18 18:41:15 +02:00
|
|
|
|
2016-11-20 16:08:49 +01:00
|
|
|
-spec get_vh_rooms_count(binary()) -> non_neg_integer().
|
|
|
|
get_vh_rooms_count(Host) ->
|
|
|
|
ets:select_count(muc_online_room,
|
|
|
|
ets:fun2ms(
|
|
|
|
fun(#muc_online_room{name_host = {_, H}}) ->
|
|
|
|
H == Host
|
|
|
|
end)).
|
2003-04-17 21:47:43 +02:00
|
|
|
|
2007-02-13 18:23:36 +01:00
|
|
|
clean_table_from_bad_node(Node) ->
|
|
|
|
F = fun() ->
|
|
|
|
Es = mnesia:select(
|
|
|
|
muc_online_room,
|
|
|
|
[{#muc_online_room{pid = '$1', _ = '_'},
|
|
|
|
[{'==', {node, '$1'}, Node}],
|
|
|
|
['$_']}]),
|
|
|
|
lists:foreach(fun(E) ->
|
|
|
|
mnesia:delete_object(E)
|
|
|
|
end, Es)
|
|
|
|
end,
|
2010-02-19 08:38:18 +01:00
|
|
|
mnesia:async_dirty(F).
|
2007-02-13 18:23:36 +01:00
|
|
|
|
2007-05-09 16:47:58 +02:00
|
|
|
clean_table_from_bad_node(Node, Host) ->
|
|
|
|
F = fun() ->
|
|
|
|
Es = mnesia:select(
|
|
|
|
muc_online_room,
|
|
|
|
[{#muc_online_room{pid = '$1',
|
|
|
|
name_host = {'_', Host},
|
|
|
|
_ = '_'},
|
|
|
|
[{'==', {node, '$1'}, Node}],
|
|
|
|
['$_']}]),
|
|
|
|
lists:foreach(fun(E) ->
|
|
|
|
mnesia:delete_object(E)
|
|
|
|
end, Es)
|
|
|
|
end,
|
2010-02-19 08:38:18 +01:00
|
|
|
mnesia:async_dirty(F).
|
2007-05-09 16:47:58 +02:00
|
|
|
|
2016-11-20 16:08:49 +01:00
|
|
|
update_tables() ->
|
|
|
|
try
|
|
|
|
case mnesia:table_info(muc_online_room, type) of
|
|
|
|
ordered_set -> ok;
|
|
|
|
_ ->
|
|
|
|
case mnesia:delete_table(muc_online_room) of
|
|
|
|
{atomic, ok} -> ok;
|
|
|
|
Err -> erlang:error(Err)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
catch _:{aborted, {no_exists, muc_online_room}} -> ok;
|
|
|
|
_:{aborted, {no_exists, muc_online_room, type}} -> ok;
|
|
|
|
E:R ->
|
|
|
|
?ERROR_MSG("failed to update mnesia table '~s': ~p",
|
|
|
|
[muc_online_room, {E, R}])
|
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
opts_to_binary(Opts) ->
|
|
|
|
lists:map(
|
|
|
|
fun({title, Title}) ->
|
|
|
|
{title, iolist_to_binary(Title)};
|
|
|
|
({description, Desc}) ->
|
|
|
|
{description, iolist_to_binary(Desc)};
|
|
|
|
({password, Pass}) ->
|
|
|
|
{password, iolist_to_binary(Pass)};
|
|
|
|
({subject, Subj}) ->
|
|
|
|
{subject, iolist_to_binary(Subj)};
|
|
|
|
({subject_author, Author}) ->
|
|
|
|
{subject_author, iolist_to_binary(Author)};
|
|
|
|
({affiliations, Affs}) ->
|
|
|
|
{affiliations, lists:map(
|
|
|
|
fun({{U, S, R}, Aff}) ->
|
|
|
|
NewAff =
|
|
|
|
case Aff of
|
|
|
|
{A, Reason} ->
|
|
|
|
{A, iolist_to_binary(Reason)};
|
|
|
|
_ ->
|
|
|
|
Aff
|
|
|
|
end,
|
|
|
|
{{iolist_to_binary(U),
|
|
|
|
iolist_to_binary(S),
|
|
|
|
iolist_to_binary(R)},
|
|
|
|
NewAff}
|
|
|
|
end, Affs)};
|
|
|
|
({captcha_whitelist, CWList}) ->
|
|
|
|
{captcha_whitelist, lists:map(
|
|
|
|
fun({U, S, R}) ->
|
|
|
|
{iolist_to_binary(U),
|
|
|
|
iolist_to_binary(S),
|
|
|
|
iolist_to_binary(R)}
|
|
|
|
end, CWList)};
|
|
|
|
(Opt) ->
|
|
|
|
Opt
|
|
|
|
end, Opts).
|
|
|
|
|
2016-04-13 20:07:32 +02:00
|
|
|
export(LServer) ->
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:export(LServer).
|
2003-03-26 21:51:18 +01:00
|
|
|
|
2016-11-22 14:48:01 +01:00
|
|
|
import_info() ->
|
|
|
|
[{<<"muc_room">>, 4}, {<<"muc_registered">>, 4}].
|
|
|
|
|
|
|
|
import_start(LServer, DBType) ->
|
|
|
|
Mod = gen_mod:db_mod(DBType, ?MODULE),
|
|
|
|
Mod:init(LServer, []).
|
2013-07-21 12:24:36 +02:00
|
|
|
|
2016-11-22 14:48:01 +01:00
|
|
|
import(LServer, {sql, _}, DBType, Tab, L) ->
|
2016-04-13 20:07:32 +02:00
|
|
|
Mod = gen_mod:db_mod(DBType, ?MODULE),
|
2016-11-22 14:48:01 +01:00
|
|
|
Mod:import(LServer, Tab, L).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
|
|
|
mod_opt_type(access) ->
|
2016-06-21 13:18:24 +02:00
|
|
|
fun acl:access_rules_validator/1;
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(access_admin) ->
|
2016-06-21 13:18:24 +02:00
|
|
|
fun acl:access_rules_validator/1;
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(access_create) ->
|
2016-06-21 13:18:24 +02:00
|
|
|
fun acl:access_rules_validator/1;
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(access_persistent) ->
|
2016-06-21 13:18:24 +02:00
|
|
|
fun acl:access_rules_validator/1;
|
2016-04-27 16:10:50 +02:00
|
|
|
mod_opt_type(db_type) -> fun(T) -> ejabberd_config:v_db(?MODULE, T) end;
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(default_room_options) ->
|
|
|
|
fun (L) when is_list(L) -> L end;
|
|
|
|
mod_opt_type(history_size) ->
|
|
|
|
fun (I) when is_integer(I), I >= 0 -> I end;
|
|
|
|
mod_opt_type(host) -> fun iolist_to_binary/1;
|
2015-06-03 14:18:09 +02:00
|
|
|
mod_opt_type(max_room_desc) ->
|
|
|
|
fun (infinity) -> infinity;
|
|
|
|
(I) when is_integer(I), I > 0 -> I
|
|
|
|
end;
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(max_room_id) ->
|
|
|
|
fun (infinity) -> infinity;
|
|
|
|
(I) when is_integer(I), I > 0 -> I
|
|
|
|
end;
|
2016-10-18 22:59:34 +02:00
|
|
|
mod_opt_type(max_rooms_discoitems) ->
|
|
|
|
fun (I) when is_integer(I), I >= 0 -> I end;
|
2016-02-05 12:08:40 +01:00
|
|
|
mod_opt_type(regexp_room_id) ->
|
2016-01-14 15:08:31 +01:00
|
|
|
fun iolist_to_binary/1;
|
2015-06-03 14:18:09 +02:00
|
|
|
mod_opt_type(max_room_name) ->
|
|
|
|
fun (infinity) -> infinity;
|
|
|
|
(I) when is_integer(I), I > 0 -> I
|
|
|
|
end;
|
|
|
|
mod_opt_type(max_user_conferences) ->
|
|
|
|
fun (I) when is_integer(I), I > 0 -> I end;
|
|
|
|
mod_opt_type(max_users) ->
|
|
|
|
fun (I) when is_integer(I), I > 0 -> I end;
|
|
|
|
mod_opt_type(max_users_admin_threshold) ->
|
|
|
|
fun (I) when is_integer(I), I > 0 -> I end;
|
|
|
|
mod_opt_type(max_users_presence) ->
|
|
|
|
fun (MUP) when is_integer(MUP) -> MUP end;
|
|
|
|
mod_opt_type(min_message_interval) ->
|
|
|
|
fun (MMI) when is_number(MMI) -> MMI end;
|
|
|
|
mod_opt_type(min_presence_interval) ->
|
|
|
|
fun (I) when is_number(I), I >= 0 -> I end;
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(room_shaper) ->
|
|
|
|
fun (A) when is_atom(A) -> A end;
|
2015-06-03 14:18:09 +02:00
|
|
|
mod_opt_type(user_message_shaper) ->
|
|
|
|
fun (A) when is_atom(A) -> A end;
|
|
|
|
mod_opt_type(user_presence_shaper) ->
|
|
|
|
fun (A) when is_atom(A) -> A end;
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(_) ->
|
|
|
|
[access, access_admin, access_create, access_persistent,
|
|
|
|
db_type, default_room_options, history_size, host,
|
2016-10-18 22:59:34 +02:00
|
|
|
max_room_desc, max_room_id, max_room_name,
|
|
|
|
max_rooms_discoitems, max_user_conferences, max_users,
|
2015-06-03 14:18:09 +02:00
|
|
|
max_users_admin_threshold, max_users_presence,
|
|
|
|
min_message_interval, min_presence_interval,
|
2016-10-18 22:59:34 +02:00
|
|
|
regexp_room_id, room_shaper, user_message_shaper, user_presence_shaper].
|