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,
|
2003-12-17 21:13:21 +01:00
|
|
|
process_iq_disco_items/4,
|
2010-01-03 01:38:00 +01:00
|
|
|
broadcast_service_message/2,
|
2015-10-07 00:06:58 +02:00
|
|
|
export/1,
|
|
|
|
import/1,
|
|
|
|
import/3,
|
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,
|
|
|
|
mod_opt_type/1]).
|
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").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-03-23 21:08:44 +01:00
|
|
|
-include("jlib.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(),
|
|
|
|
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
|
|
|
|
2015-12-02 02:32:14 +01:00
|
|
|
-define(MAX_ROOMS_DISCOITEMS, 100).
|
|
|
|
|
2016-04-13 20:07:32 +02:00
|
|
|
-type muc_room_opts() :: [{atom(), any()}].
|
|
|
|
-callback init(binary(), gen_mod:opts()) -> any().
|
|
|
|
-callback import(binary(), #muc_room{} | #muc_registered{}) -> ok | pass.
|
|
|
|
-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]},
|
|
|
|
temporary, 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}.
|
|
|
|
|
|
|
|
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),
|
2015-08-19 15:15:02 +02:00
|
|
|
remove_room_mam(LServer, Host, Name),
|
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
|
|
|
|
2015-08-19 15:15:02 +02:00
|
|
|
remove_room_mam(LServer, Host, Name) ->
|
|
|
|
case gen_mod:is_loaded(LServer, mod_mam) of
|
|
|
|
true ->
|
2016-04-15 14:11:31 +02:00
|
|
|
mod_mam:remove_room(LServer, Name, Host);
|
2015-08-19 15:15:02 +02:00
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
process_iq_disco_items(Host, From, To,
|
|
|
|
#iq{lang = Lang} = IQ) ->
|
2008-12-23 14:04:42 +01:00
|
|
|
Rsm = jlib:rsm_decode(IQ),
|
2016-02-03 19:03:17 +01:00
|
|
|
DiscoNode = fxml:get_tag_attr_s(<<"node">>, IQ#iq.sub_el),
|
2006-02-02 06:00:27 +01:00
|
|
|
Res = IQ#iq{type = result,
|
2013-03-14 10:33:02 +01:00
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_DISCO_ITEMS}],
|
2015-12-02 01:50:30 +01:00
|
|
|
children = iq_disco_items(Host, From, Lang, DiscoNode, Rsm)}]},
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_router:route(To, From, jlib:iq_to_xml(Res)).
|
|
|
|
|
|
|
|
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]) ->
|
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]),
|
2007-01-19 05:46:44 +01:00
|
|
|
mnesia:create_table(muc_online_room,
|
|
|
|
[{ram_copies, [node()]},
|
|
|
|
{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,
|
|
|
|
fun(A) when is_atom(A) -> A end, all),
|
|
|
|
AccessCreate = gen_mod:get_opt(access_create, Opts,
|
|
|
|
fun(A) when is_atom(A) -> A end, all),
|
|
|
|
AccessAdmin = gen_mod:get_opt(access_admin, Opts,
|
|
|
|
fun(A) when is_atom(A) -> A end,
|
|
|
|
none),
|
|
|
|
AccessPersistent = gen_mod:get_opt(access_persistent, Opts,
|
|
|
|
fun(A) when is_atom(A) -> A end,
|
|
|
|
all),
|
|
|
|
HistorySize = gen_mod:get_opt(history_size, Opts,
|
|
|
|
fun(I) when is_integer(I), I>=0 -> I end,
|
|
|
|
20),
|
|
|
|
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;
|
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-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,
|
|
|
|
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,
|
|
|
|
room_shaper = RoomShaper} = State) ->
|
|
|
|
case catch do_route(Host, ServerHost, Access, HistorySize, RoomShaper,
|
|
|
|
From, To, Packet, DefRoomOpts) 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
|
|
|
|
|
|
|
terminate(_Reason, State) ->
|
|
|
|
ejabberd_router:unregister_route(State#state.host),
|
|
|
|
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,
|
|
|
|
From, To, Packet, DefRoomOpts) ->
|
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,
|
2015-10-07 00:06:58 +02:00
|
|
|
From, To, Packet, DefRoomOpts);
|
2004-05-17 22:36:41 +02:00
|
|
|
_ ->
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{attrs = Attrs} = Packet,
|
2016-02-03 19:03:17 +01:00
|
|
|
Lang = fxml:get_attr_s(<<"xml:lang">>, Attrs),
|
2013-03-14 10:33:02 +01:00
|
|
|
ErrText = <<"Access denied by service policy">>,
|
2004-05-17 22:36:41 +02:00
|
|
|
Err = jlib:make_error_reply(Packet,
|
2015-10-07 00:06:58 +02:00
|
|
|
?ERRT_FORBIDDEN(Lang, ErrText)),
|
2010-04-20 20:31:42 +02:00
|
|
|
ejabberd_router:route_error(To, From, Err, Packet)
|
2004-05-17 22:36:41 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2007-09-01 23:05:04 +02:00
|
|
|
do_route1(Host, ServerHost, Access, HistorySize, RoomShaper,
|
|
|
|
From, To, Packet, DefRoomOpts) ->
|
2007-08-02 04:30:25 +02:00
|
|
|
{_AccessRoute, AccessCreate, AccessAdmin, _AccessPersistent} = Access,
|
2015-11-24 16:44:13 +01:00
|
|
|
{Room, _, Nick} = jid:tolower(To),
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = Name, attrs = Attrs} = Packet,
|
2003-03-23 21:08:44 +01:00
|
|
|
case Room of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"">> ->
|
|
|
|
case Nick of
|
|
|
|
<<"">> ->
|
|
|
|
case Name of
|
|
|
|
<<"iq">> ->
|
|
|
|
case jlib:iq_query_info(Packet) of
|
|
|
|
#iq{type = get, xmlns = (?NS_DISCO_INFO) = XMLNS,
|
|
|
|
sub_el = _SubEl, lang = Lang} =
|
|
|
|
IQ ->
|
|
|
|
Info = ejabberd_hooks:run_fold(disco_info,
|
|
|
|
ServerHost, [],
|
|
|
|
[ServerHost, ?MODULE,
|
|
|
|
<<"">>, <<"">>]),
|
|
|
|
Res = IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>, XMLNS}],
|
|
|
|
children =
|
2015-08-06 12:33:39 +02:00
|
|
|
iq_disco_info(
|
|
|
|
ServerHost, Lang) ++
|
2013-03-14 10:33:02 +01:00
|
|
|
Info}]},
|
|
|
|
ejabberd_router:route(To, From,
|
|
|
|
jlib:iq_to_xml(Res));
|
|
|
|
#iq{type = get, xmlns = ?NS_DISCO_ITEMS} = IQ ->
|
|
|
|
spawn(?MODULE, process_iq_disco_items,
|
|
|
|
[Host, From, To, IQ]);
|
|
|
|
#iq{type = get, xmlns = (?NS_REGISTER) = XMLNS,
|
|
|
|
lang = Lang, sub_el = _SubEl} =
|
|
|
|
IQ ->
|
|
|
|
Res = IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>, XMLNS}],
|
|
|
|
children =
|
|
|
|
iq_get_register_info(ServerHost,
|
|
|
|
Host,
|
|
|
|
From,
|
|
|
|
Lang)}]},
|
|
|
|
ejabberd_router:route(To, From,
|
|
|
|
jlib:iq_to_xml(Res));
|
|
|
|
#iq{type = set, xmlns = (?NS_REGISTER) = XMLNS,
|
|
|
|
lang = Lang, sub_el = SubEl} =
|
|
|
|
IQ ->
|
|
|
|
case process_iq_register_set(ServerHost, Host, From,
|
|
|
|
SubEl, Lang)
|
|
|
|
of
|
|
|
|
{result, IQRes} ->
|
|
|
|
Res = IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>,
|
|
|
|
XMLNS}],
|
|
|
|
children = IQRes}]},
|
|
|
|
ejabberd_router:route(To, From,
|
|
|
|
jlib:iq_to_xml(Res));
|
|
|
|
{error, Error} ->
|
|
|
|
Err = jlib:make_error_reply(Packet, Error),
|
|
|
|
ejabberd_router:route(To, From, Err)
|
2003-05-18 18:41:15 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
#iq{type = get, xmlns = (?NS_VCARD) = XMLNS,
|
|
|
|
lang = Lang, sub_el = _SubEl} =
|
|
|
|
IQ ->
|
|
|
|
Res = IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"vCard">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>, XMLNS}],
|
|
|
|
children =
|
|
|
|
iq_get_vcard(Lang)}]},
|
|
|
|
ejabberd_router:route(To, From,
|
|
|
|
jlib:iq_to_xml(Res));
|
|
|
|
#iq{type = get, xmlns = ?NS_MUC_UNIQUE} = IQ ->
|
|
|
|
Res = IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"unique">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>,
|
|
|
|
?NS_MUC_UNIQUE}],
|
|
|
|
children =
|
|
|
|
[iq_get_unique(From)]}]},
|
|
|
|
ejabberd_router:route(To, From,
|
|
|
|
jlib:iq_to_xml(Res));
|
|
|
|
#iq{} ->
|
|
|
|
Err = jlib:make_error_reply(Packet,
|
|
|
|
?ERR_FEATURE_NOT_IMPLEMENTED),
|
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
_ -> ok
|
|
|
|
end;
|
|
|
|
<<"message">> ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_attr_s(<<"type">>, Attrs) of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"error">> -> ok;
|
2003-03-23 21:08:44 +01:00
|
|
|
_ ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case acl:match_rule(ServerHost, AccessAdmin, From)
|
|
|
|
of
|
|
|
|
allow ->
|
2016-02-03 19:03:17 +01:00
|
|
|
Msg = fxml:get_path_s(Packet,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{elem, <<"body">>},
|
|
|
|
cdata]),
|
|
|
|
broadcast_service_message(Host, Msg);
|
|
|
|
_ ->
|
2016-02-03 19:03:17 +01:00
|
|
|
Lang = fxml:get_attr_s(<<"xml:lang">>, Attrs),
|
2013-03-14 10:33:02 +01:00
|
|
|
ErrText =
|
|
|
|
<<"Only service administrators are allowed "
|
|
|
|
"to send service messages">>,
|
|
|
|
Err = jlib:make_error_reply(Packet,
|
|
|
|
?ERRT_FORBIDDEN(Lang,
|
|
|
|
ErrText)),
|
|
|
|
ejabberd_router:route(To, From, Err)
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
<<"presence">> -> ok
|
|
|
|
end;
|
|
|
|
_ ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_attr_s(<<"type">>, Attrs) of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"error">> -> ok;
|
|
|
|
<<"result">> -> ok;
|
|
|
|
_ ->
|
2015-10-07 00:06:58 +02:00
|
|
|
Err = jlib:make_error_reply(Packet,
|
|
|
|
?ERR_ITEM_NOT_FOUND),
|
|
|
|
ejabberd_router:route(To, From, Err)
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
_ ->
|
2007-01-19 05:46:44 +01:00
|
|
|
case mnesia:dirty_read(muc_online_room, {Room, Host}) of
|
2003-03-23 21:08:44 +01:00
|
|
|
[] ->
|
2016-02-03 19:03:17 +01:00
|
|
|
Type = fxml:get_attr_s(<<"type">>, Attrs),
|
2003-03-23 21:08:44 +01:00
|
|
|
case {Name, Type} of
|
2013-03-14 10:33:02 +01:00
|
|
|
{<<"presence">>, <<"">>} ->
|
2009-04-22 14:05:10 +02:00
|
|
|
case check_user_can_create_room(ServerHost,
|
2016-01-14 15:08:31 +01:00
|
|
|
AccessCreate, From, Room) and
|
|
|
|
check_create_roomid(ServerHost, Room) of
|
2009-04-22 14:05:10 +02:00
|
|
|
true ->
|
2015-10-07 00:06:58 +02:00
|
|
|
{ok, Pid} = start_new_room(Host, ServerHost, Access,
|
|
|
|
Room, HistorySize,
|
|
|
|
RoomShaper, From, Nick, DefRoomOpts),
|
2007-01-19 05:46:44 +01:00
|
|
|
register_room(Host, Room, Pid),
|
2004-05-17 22:36:41 +02:00
|
|
|
mod_muc_room:route(Pid, From, Nick, Packet),
|
|
|
|
ok;
|
2009-04-22 14:05:10 +02:00
|
|
|
false ->
|
2016-02-03 19:03:17 +01:00
|
|
|
Lang = fxml:get_attr_s(<<"xml:lang">>, Attrs),
|
2013-03-14 10:33:02 +01:00
|
|
|
ErrText = <<"Room creation is denied by service policy">>,
|
2004-05-17 22:36:41 +02:00
|
|
|
Err = jlib:make_error_reply(
|
|
|
|
Packet, ?ERRT_FORBIDDEN(Lang, ErrText)),
|
|
|
|
ejabberd_router:route(To, From, Err)
|
|
|
|
end;
|
2003-03-23 21:08:44 +01:00
|
|
|
_ ->
|
2016-02-03 19:03:17 +01:00
|
|
|
Lang = fxml:get_attr_s(<<"xml:lang">>, Attrs),
|
2015-10-07 00:06:58 +02:00
|
|
|
ErrText = <<"Conference room does not exist">>,
|
|
|
|
Err = jlib:make_error_reply(Packet,
|
|
|
|
?ERRT_ITEM_NOT_FOUND(Lang, ErrText)),
|
|
|
|
ejabberd_router:route(To, From, 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
|
2003-03-23 21:08:44 +01:00
|
|
|
end.
|
|
|
|
|
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).
|
|
|
|
|
2003-03-26 21:51:18 +01:00
|
|
|
|
2015-08-06 12:33:39 +02:00
|
|
|
iq_disco_info(ServerHost, Lang) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[#xmlel{name = <<"identity">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"category">>, <<"conference">>},
|
|
|
|
{<<"type">>, <<"text">>},
|
|
|
|
{<<"name">>,
|
|
|
|
translate:translate(Lang, <<"Chatrooms">>)}],
|
|
|
|
children = []},
|
|
|
|
#xmlel{name = <<"feature">>,
|
|
|
|
attrs = [{<<"var">>, ?NS_DISCO_INFO}], children = []},
|
|
|
|
#xmlel{name = <<"feature">>,
|
|
|
|
attrs = [{<<"var">>, ?NS_DISCO_ITEMS}], children = []},
|
|
|
|
#xmlel{name = <<"feature">>,
|
|
|
|
attrs = [{<<"var">>, ?NS_MUC}], children = []},
|
|
|
|
#xmlel{name = <<"feature">>,
|
|
|
|
attrs = [{<<"var">>, ?NS_MUC_UNIQUE}], children = []},
|
|
|
|
#xmlel{name = <<"feature">>,
|
|
|
|
attrs = [{<<"var">>, ?NS_REGISTER}], children = []},
|
|
|
|
#xmlel{name = <<"feature">>,
|
|
|
|
attrs = [{<<"var">>, ?NS_RSM}], children = []},
|
|
|
|
#xmlel{name = <<"feature">>,
|
2015-08-06 12:33:39 +02:00
|
|
|
attrs = [{<<"var">>, ?NS_VCARD}], children = []}] ++
|
|
|
|
case gen_mod:is_loaded(ServerHost, mod_mam) of
|
|
|
|
true ->
|
|
|
|
[#xmlel{name = <<"feature">>,
|
2016-01-14 00:58:48 +01:00
|
|
|
attrs = [{<<"var">>, ?NS_MAM_TMP}]},
|
|
|
|
#xmlel{name = <<"feature">>,
|
2015-10-11 18:42:53 +02:00
|
|
|
attrs = [{<<"var">>, ?NS_MAM_0}]},
|
|
|
|
#xmlel{name = <<"feature">>,
|
|
|
|
attrs = [{<<"var">>, ?NS_MAM_1}]}];
|
2015-08-06 12:33:39 +02:00
|
|
|
false ->
|
|
|
|
[]
|
|
|
|
end.
|
2003-03-26 21:51:18 +01:00
|
|
|
|
2015-12-02 01:50:30 +01:00
|
|
|
iq_disco_items(Host, From, Lang, <<>>, none) ->
|
2015-12-02 02:32:14 +01:00
|
|
|
Rooms = get_vh_rooms(Host),
|
|
|
|
case erlang:length(Rooms) < ?MAX_ROOMS_DISCOITEMS of
|
|
|
|
true ->
|
|
|
|
iq_disco_items_list(Host, Rooms, {get_disco_item, all, From, Lang});
|
|
|
|
false ->
|
|
|
|
iq_disco_items(Host, From, Lang, <<"nonemptyrooms">>, none)
|
|
|
|
end;
|
|
|
|
iq_disco_items(Host, From, Lang, <<"nonemptyrooms">>, none) ->
|
2015-12-02 01:50:30 +01:00
|
|
|
XmlEmpty = #xmlel{name = <<"item">>,
|
2013-03-14 10:33:02 +01:00
|
|
|
attrs =
|
2015-12-02 01:50:30 +01:00
|
|
|
[{<<"jid">>, <<"conference.localhost">>},
|
|
|
|
{<<"node">>, <<"emptyrooms">>},
|
|
|
|
{<<"name">>, translate:translate(Lang, <<"Empty Rooms">>)}],
|
|
|
|
children = []},
|
|
|
|
Query = {get_disco_item, only_non_empty, From, Lang},
|
|
|
|
[XmlEmpty | iq_disco_items_list(Host, get_vh_rooms(Host), Query)];
|
|
|
|
iq_disco_items(Host, From, Lang, <<"emptyrooms">>, none) ->
|
|
|
|
iq_disco_items_list(Host, get_vh_rooms(Host), {get_disco_item, 0, From, Lang});
|
|
|
|
iq_disco_items(Host, From, Lang, _DiscoNode, Rsm) ->
|
2008-12-23 14:04:42 +01:00
|
|
|
{Rooms, RsmO} = get_vh_rooms(Host, Rsm),
|
|
|
|
RsmOut = jlib:rsm_encode(RsmO),
|
2015-12-02 01:50:30 +01:00
|
|
|
iq_disco_items_list(Host, Rooms, {get_disco_item, all, From, Lang}) ++ RsmOut.
|
|
|
|
|
|
|
|
iq_disco_items_list(Host, Rooms, Query) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:zf(fun (#muc_online_room{name_host =
|
|
|
|
{Name, _Host},
|
|
|
|
pid = Pid}) ->
|
|
|
|
case catch gen_fsm:sync_send_all_state_event(Pid,
|
2015-12-02 01:50:30 +01:00
|
|
|
Query,
|
2013-03-14 10:33:02 +01:00
|
|
|
100)
|
|
|
|
of
|
|
|
|
{item, Desc} ->
|
|
|
|
flush(),
|
|
|
|
{true,
|
|
|
|
#xmlel{name = <<"item">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"jid">>,
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:to_string({Name, Host,
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"">>})},
|
|
|
|
{<<"name">>, Desc}],
|
|
|
|
children = []}};
|
|
|
|
_ -> false
|
2008-12-23 14:04:42 +01:00
|
|
|
end
|
2015-12-02 01:50:30 +01:00
|
|
|
end, Rooms).
|
2008-12-23 14:04:42 +01:00
|
|
|
|
|
|
|
get_vh_rooms(Host, #rsm_in{max=M, direction=Direction, id=I, index=Index})->
|
|
|
|
AllRooms = lists:sort(get_vh_rooms(Host)),
|
|
|
|
Count = erlang:length(AllRooms),
|
|
|
|
Guard = case Direction of
|
|
|
|
_ when Index =/= undefined -> [{'==', {element, 2, '$1'}, Host}];
|
|
|
|
aft -> [{'==', {element, 2, '$1'}, Host}, {'>=',{element, 1, '$1'} ,I}];
|
|
|
|
before when I =/= []-> [{'==', {element, 2, '$1'}, Host}, {'=<',{element, 1, '$1'} ,I}];
|
|
|
|
_ -> [{'==', {element, 2, '$1'}, Host}]
|
|
|
|
end,
|
|
|
|
L = lists:sort(
|
|
|
|
mnesia:dirty_select(muc_online_room,
|
|
|
|
[{#muc_online_room{name_host = '$1', _ = '_'},
|
|
|
|
Guard,
|
|
|
|
['$_']}])),
|
|
|
|
L2 = if
|
|
|
|
Index == undefined andalso Direction == before ->
|
|
|
|
lists:reverse(lists:sublist(lists:reverse(L), 1, M));
|
|
|
|
Index == undefined ->
|
|
|
|
lists:sublist(L, 1, M);
|
|
|
|
Index > Count orelse Index < 0 ->
|
|
|
|
[];
|
|
|
|
true ->
|
|
|
|
lists:sublist(L, Index+1, M)
|
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
if L2 == [] -> {L2, #rsm_out{count = Count}};
|
|
|
|
true ->
|
|
|
|
H = hd(L2),
|
|
|
|
NewIndex = get_room_pos(H, AllRooms),
|
|
|
|
T = lists:last(L2),
|
|
|
|
{F, _} = H#muc_online_room.name_host,
|
|
|
|
{Last, _} = T#muc_online_room.name_host,
|
|
|
|
{L2,
|
|
|
|
#rsm_out{first = F, last = Last, count = Count,
|
|
|
|
index = NewIndex}}
|
2008-12-23 14:04:42 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% @doc Return the position of desired room in the list of rooms.
|
|
|
|
%% The room must exist in the list. The count starts in 0.
|
|
|
|
%% @spec (Desired::muc_online_room(), Rooms::[muc_online_room()]) -> integer()
|
|
|
|
get_room_pos(Desired, Rooms) ->
|
|
|
|
get_room_pos(Desired, Rooms, 0).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2008-12-23 14:04:42 +01:00
|
|
|
get_room_pos(Desired, [HeadRoom | _], HeadPosition)
|
2013-03-14 10:33:02 +01:00
|
|
|
when Desired#muc_online_room.name_host ==
|
|
|
|
HeadRoom#muc_online_room.name_host ->
|
2008-12-23 14:04:42 +01:00
|
|
|
HeadPosition;
|
|
|
|
get_room_pos(Desired, [_ | Rooms], HeadPosition) ->
|
|
|
|
get_room_pos(Desired, Rooms, HeadPosition + 1).
|
2003-03-26 21:51:18 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
flush() -> receive _ -> flush() after 0 -> ok end.
|
2003-03-26 21:51:18 +01:00
|
|
|
|
2003-04-17 21:47:43 +02:00
|
|
|
-define(XFIELD(Type, Label, Var, Val),
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"field">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"type">>, Type},
|
|
|
|
{<<"label">>, translate:translate(Lang, Label)},
|
|
|
|
{<<"var">>, Var}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"value">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Val}]}]}).
|
|
|
|
|
2009-12-11 13:30:41 +01:00
|
|
|
iq_get_unique(From) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{xmlcdata,
|
2015-12-07 16:08:57 +01:00
|
|
|
p1_sha:sha(term_to_binary([From, p1_time_compat:timestamp(),
|
2013-03-14 10:33:02 +01:00
|
|
|
randoms:get_string()]))}.
|
2009-12-11 13:30:41 +01:00
|
|
|
|
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) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{Nick, Registered} = case get_nick(ServerHost, Host,
|
|
|
|
From)
|
|
|
|
of
|
|
|
|
error -> {<<"">>, []};
|
|
|
|
N ->
|
|
|
|
{N,
|
|
|
|
[#xmlel{name = <<"registered">>, attrs = [],
|
|
|
|
children = []}]}
|
|
|
|
end,
|
2003-12-06 20:58:49 +01:00
|
|
|
Registered ++
|
2013-03-14 10:33:02 +01:00
|
|
|
[#xmlel{name = <<"instructions">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
translate:translate(Lang,
|
|
|
|
<<"You need a client that supports x:data "
|
|
|
|
"to register the nickname">>)}]},
|
|
|
|
#xmlel{name = <<"x">>,
|
2014-09-03 19:26:12 +02:00
|
|
|
attrs = [{<<"xmlns">>, ?NS_XDATA},
|
|
|
|
{<<"type">>, <<"form">>}],
|
2013-03-14 10:33:02 +01:00
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"title">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
<<(translate:translate(Lang,
|
|
|
|
<<"Nickname Registration at ">>))/binary,
|
|
|
|
Host/binary>>}]},
|
|
|
|
#xmlel{name = <<"instructions">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
translate:translate(Lang,
|
|
|
|
<<"Enter nickname you want to register">>)}]},
|
|
|
|
?XFIELD(<<"text-single">>, <<"Nickname">>, <<"nick">>,
|
|
|
|
Nick)]}].
|
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
|
2013-03-14 10:33:02 +01:00
|
|
|
{atomic, ok} -> {result, []};
|
|
|
|
{atomic, false} ->
|
|
|
|
ErrText = <<"That nickname is registered by another "
|
|
|
|
"person">>,
|
|
|
|
{error, ?ERRT_CONFLICT(Lang, ErrText)};
|
2016-03-31 10:00:29 +02:00
|
|
|
_ ->
|
|
|
|
Txt = <<"Database failure">>,
|
|
|
|
{error, ?ERRT_INTERNAL_SERVER_ERROR(Lang, Txt)}
|
2003-04-17 21:47:43 +02:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
process_iq_register_set(ServerHost, Host, From, SubEl,
|
|
|
|
Lang) ->
|
|
|
|
#xmlel{children = Els} = SubEl,
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_subtag(SubEl, <<"remove">>) of
|
2013-03-14 10:33:02 +01:00
|
|
|
false ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:remove_cdata(Els) of
|
2013-03-14 10:33:02 +01:00
|
|
|
[#xmlel{name = <<"x">>} = XEl] ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case {fxml:get_tag_attr_s(<<"xmlns">>, XEl),
|
|
|
|
fxml:get_tag_attr_s(<<"type">>, XEl)}
|
2013-03-14 10:33:02 +01:00
|
|
|
of
|
|
|
|
{?NS_XDATA, <<"cancel">>} -> {result, []};
|
|
|
|
{?NS_XDATA, <<"submit">>} ->
|
|
|
|
XData = jlib:parse_xdata_submit(XEl),
|
|
|
|
case XData of
|
2016-03-31 10:00:29 +02:00
|
|
|
invalid ->
|
|
|
|
Txt = <<"Incorrect data form">>,
|
|
|
|
{error, ?ERRT_BAD_REQUEST(Lang, Txt)};
|
2003-04-17 21:47:43 +02:00
|
|
|
_ ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case lists:keysearch(<<"nick">>, 1, XData) of
|
|
|
|
{value, {_, [Nick]}} when Nick /= <<"">> ->
|
|
|
|
iq_set_register_info(ServerHost, Host, From,
|
|
|
|
Nick, Lang);
|
|
|
|
_ ->
|
|
|
|
ErrText =
|
|
|
|
<<"You must fill in field \"Nickname\" "
|
|
|
|
"in the form">>,
|
|
|
|
{error, ?ERRT_NOT_ACCEPTABLE(Lang, ErrText)}
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
_ -> {error, ?ERR_BAD_REQUEST}
|
|
|
|
end;
|
|
|
|
_ -> {error, ?ERR_BAD_REQUEST}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
iq_set_register_info(ServerHost, Host, From, <<"">>,
|
|
|
|
Lang)
|
2003-04-17 21:47:43 +02:00
|
|
|
end.
|
|
|
|
|
2003-05-09 21:32:59 +02:00
|
|
|
iq_get_vcard(Lang) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[#xmlel{name = <<"FN">>, attrs = [],
|
|
|
|
children = [{xmlcdata, <<"ejabberd/mod_muc">>}]},
|
|
|
|
#xmlel{name = <<"URL">>, attrs = [],
|
|
|
|
children = [{xmlcdata, ?EJABBERD_URI}]},
|
|
|
|
#xmlel{name = <<"DESC">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
<<(translate:translate(Lang,
|
|
|
|
<<"ejabberd MUC module">>))/binary,
|
2016-01-13 12:29:14 +01:00
|
|
|
"\nCopyright (c) 2003-2016 ProcessOne">>}]}].
|
2003-05-09 21:32:59 +02:00
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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-04-13 20:07:32 +02:00
|
|
|
import(LServer) ->
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:import(LServer).
|
2013-07-21 12:24:36 +02:00
|
|
|
|
2016-04-13 20:07:32 +02:00
|
|
|
import(LServer, DBType, Data) ->
|
|
|
|
Mod = gen_mod:db_mod(DBType, ?MODULE),
|
|
|
|
Mod:import(LServer, Data).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
|
|
|
mod_opt_type(access) ->
|
|
|
|
fun (A) when is_atom(A) -> A end;
|
|
|
|
mod_opt_type(access_admin) ->
|
|
|
|
fun (A) when is_atom(A) -> A end;
|
|
|
|
mod_opt_type(access_create) ->
|
|
|
|
fun (A) when is_atom(A) -> A end;
|
|
|
|
mod_opt_type(access_persistent) ->
|
|
|
|
fun (A) when is_atom(A) -> A end;
|
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-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-02-05 12:08:40 +01:00
|
|
|
max_room_desc, max_room_id, max_room_name, regexp_room_id,
|
2015-06-03 14:18:09 +02:00
|
|
|
max_user_conferences, max_users,
|
|
|
|
max_users_admin_threshold, max_users_presence,
|
|
|
|
min_message_interval, min_presence_interval,
|
|
|
|
room_shaper, user_message_shaper, user_presence_shaper].
|