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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2019-01-08 22:53:27 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2019 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).
|
2007-12-24 14:57:53 +01:00
|
|
|
-author('alexey@process-one.net').
|
2015-05-21 17:02:36 +02:00
|
|
|
-protocol({xep, 45, '1.25'}).
|
2019-07-05 09:35:31 +02:00
|
|
|
-ifndef(GEN_SERVER).
|
|
|
|
-define(GEN_SERVER, gen_server).
|
|
|
|
-endif.
|
|
|
|
-behaviour(?GEN_SERVER).
|
2003-03-23 21:08:44 +01:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2006-02-02 06:00:27 +01:00
|
|
|
%% API
|
2017-02-14 10:39:26 +01:00
|
|
|
-export([start/2,
|
2005-06-20 05:18:13 +02:00
|
|
|
stop/1,
|
2019-08-04 20:46:18 +02:00
|
|
|
start_link/2,
|
2017-02-22 17:46:47 +01:00
|
|
|
reload/3,
|
2007-01-19 05:46:44 +01:00
|
|
|
room_destroyed/4,
|
2012-03-31 13:41:54 +02:00
|
|
|
store_room/4,
|
2017-10-31 14:00:41 +01:00
|
|
|
store_room/5,
|
2012-03-31 13:41:54 +02:00
|
|
|
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,
|
2017-01-13 10:03:39 +01:00
|
|
|
broadcast_service_message/3,
|
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,
|
2017-01-13 10:03:39 +01:00
|
|
|
find_online_room/2,
|
|
|
|
register_online_room/3,
|
|
|
|
get_online_rooms/1,
|
|
|
|
count_online_rooms/1,
|
|
|
|
register_online_user/4,
|
|
|
|
unregister_online_user/4,
|
2017-08-24 10:40:24 +02:00
|
|
|
iq_set_register_info/5,
|
2017-01-13 10:03:39 +01:00
|
|
|
count_online_rooms_by_user/3,
|
|
|
|
get_online_rooms_by_user/3,
|
2019-03-28 17:42:25 +01:00
|
|
|
can_use_nick/4,
|
2019-07-05 09:35:31 +02:00
|
|
|
get_subscribed_rooms/2,
|
|
|
|
procname/2,
|
|
|
|
route/1]).
|
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,
|
2018-01-23 08:54:52 +01:00
|
|
|
mod_opt_type/1, mod_options/1, depends/2]).
|
2006-02-02 06:00:27 +01:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2016-07-25 12:50:30 +02:00
|
|
|
-include("xmpp.hrl").
|
2016-01-15 13:34:48 +01:00
|
|
|
-include("mod_muc.hrl").
|
2019-07-16 17:34:05 +02:00
|
|
|
-include("mod_muc_room.hrl").
|
2018-01-08 09:29:17 +01:00
|
|
|
-include("translate.hrl").
|
2019-07-05 09:35:31 +02:00
|
|
|
-include("ejabberd_stacktrace.hrl").
|
2003-04-17 21:47:43 +02:00
|
|
|
|
2019-07-16 17:34:05 +02:00
|
|
|
-type state() :: #{hosts := [binary()],
|
|
|
|
server_host := binary(),
|
|
|
|
worker := pos_integer()}.
|
2019-07-05 09:35:31 +02:00
|
|
|
-type access() :: {acl:acl(), acl:acl(), acl:acl(), acl:acl(), acl:acl()}.
|
2016-04-13 20:07:32 +02:00
|
|
|
-type muc_room_opts() :: [{atom(), any()}].
|
2019-07-05 09:35:31 +02:00
|
|
|
-export_type([access/0]).
|
2016-04-13 20:07:32 +02:00
|
|
|
-callback init(binary(), gen_mod:opts()) -> any().
|
2016-11-22 14:48:01 +01:00
|
|
|
-callback import(binary(), binary(), [binary()]) -> ok.
|
2017-10-31 14:00:41 +01:00
|
|
|
-callback store_room(binary(), binary(), binary(), list(), list()|undefined) -> {atomic, any()}.
|
2016-04-13 20:07:32 +02:00
|
|
|
-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}.
|
2017-03-29 11:58:01 +02:00
|
|
|
-callback register_online_room(binary(), binary(), binary(), pid()) -> any().
|
|
|
|
-callback unregister_online_room(binary(), binary(), binary(), pid()) -> any().
|
|
|
|
-callback find_online_room(binary(), binary(), binary()) -> {ok, pid()} | error.
|
|
|
|
-callback get_online_rooms(binary(), binary(), undefined | rsm_set()) -> [{binary(), binary(), pid()}].
|
|
|
|
-callback count_online_rooms(binary(), binary()) -> non_neg_integer().
|
2017-01-13 10:03:39 +01:00
|
|
|
-callback rsm_supported() -> boolean().
|
2017-03-29 11:58:01 +02:00
|
|
|
-callback register_online_user(binary(), ljid(), binary(), binary()) -> any().
|
|
|
|
-callback unregister_online_user(binary(), ljid(), binary(), binary()) -> any().
|
|
|
|
-callback count_online_rooms_by_user(binary(), binary(), binary()) -> non_neg_integer().
|
|
|
|
-callback get_online_rooms_by_user(binary(), binary(), binary()) -> [{binary(), binary()}].
|
2019-04-03 13:20:37 +02:00
|
|
|
-callback get_subscribed_rooms(binary(), binary(), jid()) ->
|
|
|
|
{ok, [{jid(), [binary()]}]} | {error, db_failure}.
|
|
|
|
|
|
|
|
-optional_callbacks([get_subscribed_rooms/3]).
|
2016-04-13 20:07:32 +02:00
|
|
|
|
2006-02-02 06:00:27 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% API
|
|
|
|
%%====================================================================
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2019-08-04 20:46:18 +02:00
|
|
|
case mod_muc_sup:start(Host) of
|
2019-07-05 09:35:31 +02:00
|
|
|
{ok, _} ->
|
|
|
|
MyHosts = gen_mod:get_opt_hosts(Opts),
|
|
|
|
Mod = gen_mod:db_mod(Opts, ?MODULE),
|
|
|
|
RMod = gen_mod:ram_db_mod(Opts, ?MODULE),
|
|
|
|
Mod:init(Host, gen_mod:set_opt(hosts, MyHosts, Opts)),
|
|
|
|
RMod:init(Host, gen_mod:set_opt(hosts, MyHosts, Opts)),
|
|
|
|
load_permanent_rooms(MyHosts, Host, Opts);
|
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end.
|
2006-02-02 06:00:27 +01:00
|
|
|
|
|
|
|
stop(Host) ->
|
2019-07-05 09:35:31 +02:00
|
|
|
Proc = mod_muc_sup:procname(Host),
|
|
|
|
supervisor:terminate_child(ejabberd_gen_mod_sup, Proc),
|
2019-07-15 11:43:05 +02:00
|
|
|
supervisor:delete_child(ejabberd_gen_mod_sup, Proc).
|
2013-06-19 13:30:31 +02:00
|
|
|
|
2019-07-05 09:35:31 +02:00
|
|
|
-spec reload(binary(), gen_mod:opts(), gen_mod:opts()) -> ok.
|
|
|
|
reload(ServerHost, NewOpts, OldOpts) ->
|
|
|
|
NewMod = gen_mod:db_mod(NewOpts, ?MODULE),
|
|
|
|
NewRMod = gen_mod:ram_db_mod(NewOpts, ?MODULE),
|
|
|
|
OldMod = gen_mod:db_mod(OldOpts, ?MODULE),
|
|
|
|
OldRMod = gen_mod:ram_db_mod(OldOpts, ?MODULE),
|
|
|
|
NewHosts = gen_mod:get_opt_hosts(NewOpts),
|
|
|
|
OldHosts = gen_mod:get_opt_hosts(OldOpts),
|
|
|
|
AddHosts = NewHosts -- OldHosts,
|
|
|
|
DelHosts = OldHosts -- NewHosts,
|
|
|
|
if NewMod /= OldMod ->
|
|
|
|
NewMod:init(ServerHost, gen_mod:set_opt(hosts, NewHosts, NewOpts));
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
if NewRMod /= OldRMod ->
|
|
|
|
NewRMod:init(ServerHost, gen_mod:set_opt(hosts, NewHosts, NewOpts));
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
lists:foreach(
|
|
|
|
fun(I) ->
|
|
|
|
?GEN_SERVER:cast(procname(ServerHost, I),
|
|
|
|
{reload, AddHosts, DelHosts, NewHosts})
|
|
|
|
end, lists:seq(1, erlang:system_info(logical_processors))),
|
|
|
|
load_permanent_rooms(AddHosts, ServerHost, NewOpts),
|
|
|
|
shutdown_rooms(ServerHost, DelHosts, OldRMod),
|
|
|
|
lists:foreach(
|
|
|
|
fun(Host) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun({_, _, Pid}) when node(Pid) == node() ->
|
2019-07-08 23:47:54 +02:00
|
|
|
mod_muc_room:config_reloaded(Pid);
|
2019-07-05 09:35:31 +02:00
|
|
|
(_) ->
|
|
|
|
ok
|
|
|
|
end, get_online_rooms(ServerHost, Host))
|
|
|
|
end, misc:intersection(NewHosts, OldHosts)).
|
2017-02-22 17:46:47 +01:00
|
|
|
|
2016-07-06 13:58:48 +02:00
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[{mod_mam, soft}].
|
|
|
|
|
2019-08-04 20:46:18 +02:00
|
|
|
start_link(Host, I) ->
|
2019-07-05 09:35:31 +02:00
|
|
|
Proc = procname(Host, I),
|
2019-08-04 20:46:18 +02:00
|
|
|
?GEN_SERVER:start_link({local, Proc}, ?MODULE, [Host, I],
|
2019-07-05 09:35:31 +02:00
|
|
|
ejabberd_config:fsm_limit_opts([])).
|
|
|
|
|
|
|
|
-spec procname(binary(), pos_integer() | {binary(), binary()}) -> atom().
|
|
|
|
procname(Host, I) when is_integer(I) ->
|
|
|
|
binary_to_atom(
|
|
|
|
<<(atom_to_binary(?MODULE, latin1))/binary, "_", Host/binary,
|
|
|
|
"_", (integer_to_binary(I))/binary>>, utf8);
|
|
|
|
procname(Host, RoomHost) ->
|
|
|
|
Cores = erlang:system_info(logical_processors),
|
|
|
|
I = erlang:phash2(RoomHost, Cores) + 1,
|
|
|
|
procname(Host, I).
|
|
|
|
|
|
|
|
-spec route(stanza()) -> ok.
|
|
|
|
route(Pkt) ->
|
|
|
|
To = xmpp:get_to(Pkt),
|
|
|
|
ServerHost = ejabberd_router:host_of_route(To#jid.lserver),
|
|
|
|
route(Pkt, ServerHost).
|
|
|
|
|
|
|
|
-spec route(stanza(), binary()) -> ok.
|
|
|
|
route(Pkt, ServerHost) ->
|
|
|
|
From = xmpp:get_from(Pkt),
|
|
|
|
To = xmpp:get_to(Pkt),
|
|
|
|
Host = To#jid.lserver,
|
|
|
|
Access = mod_muc_opt:access(ServerHost),
|
|
|
|
case acl:match_rule(ServerHost, Access, From) of
|
|
|
|
allow ->
|
|
|
|
route(Pkt, Host, ServerHost);
|
|
|
|
deny ->
|
|
|
|
Lang = xmpp:get_lang(Pkt),
|
|
|
|
ErrText = ?T("Access denied by service policy"),
|
|
|
|
Err = xmpp:err_forbidden(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(Pkt, Err)
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec route(stanza(), binary(), binary()) -> ok.
|
|
|
|
route(#iq{to = #jid{luser = <<"">>, lresource = <<"">>}} = IQ, _, _) ->
|
|
|
|
ejabberd_router:process_iq(IQ);
|
|
|
|
route(#message{lang = Lang, body = Body, type = Type, from = From,
|
|
|
|
to = #jid{luser = <<"">>, lresource = <<"">>}} = Pkt,
|
|
|
|
Host, ServerHost) ->
|
|
|
|
if Type == error ->
|
|
|
|
ok;
|
|
|
|
true ->
|
|
|
|
AccessAdmin = mod_muc_opt:access_admin(ServerHost),
|
|
|
|
case acl:match_rule(ServerHost, AccessAdmin, From) of
|
|
|
|
allow ->
|
|
|
|
Msg = xmpp:get_text(Body),
|
|
|
|
broadcast_service_message(ServerHost, Host, Msg);
|
|
|
|
deny ->
|
|
|
|
ErrText = ?T("Only service administrators are allowed "
|
|
|
|
"to send service messages"),
|
|
|
|
Err = xmpp:err_forbidden(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(Pkt, Err)
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
route(Pkt, Host, ServerHost) ->
|
|
|
|
{Room, _, _} = jid:tolower(xmpp:get_to(Pkt)),
|
|
|
|
case Room of
|
|
|
|
<<"">> ->
|
|
|
|
Txt = ?T("No module is handling this query"),
|
|
|
|
Err = xmpp:err_service_unavailable(Txt, xmpp:get_lang(Pkt)),
|
|
|
|
ejabberd_router:route_error(Pkt, Err);
|
|
|
|
_ ->
|
|
|
|
RMod = gen_mod:ram_db_mod(ServerHost, ?MODULE),
|
|
|
|
case RMod:find_online_room(ServerHost, Room, Host) of
|
|
|
|
error ->
|
|
|
|
Proc = procname(ServerHost, {Room, Host}),
|
|
|
|
case whereis(Proc) of
|
|
|
|
Pid when Pid == self() ->
|
|
|
|
route_to_room(Pkt, ServerHost);
|
|
|
|
Pid when is_pid(Pid) ->
|
|
|
|
?DEBUG("Routing to MUC worker ~p:~n~s", [Proc, xmpp:pp(Pkt)]),
|
|
|
|
?GEN_SERVER:cast(Pid, {route_to_room, Pkt});
|
|
|
|
undefined ->
|
|
|
|
?DEBUG("MUC worker ~p is dead", [Proc]),
|
|
|
|
Err = xmpp:err_internal_server_error(),
|
|
|
|
ejabberd_router:route_error(Pkt, Err)
|
|
|
|
end;
|
|
|
|
{ok, Pid} ->
|
|
|
|
mod_muc_room:route(Pid, Pkt)
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec shutdown_rooms(binary()) -> [pid()].
|
2019-06-14 11:33:26 +02:00
|
|
|
shutdown_rooms(ServerHost) ->
|
|
|
|
RMod = gen_mod:ram_db_mod(ServerHost, ?MODULE),
|
|
|
|
Hosts = gen_mod:get_module_opt_hosts(ServerHost, mod_muc),
|
2019-07-05 09:35:31 +02:00
|
|
|
shutdown_rooms(ServerHost, Hosts, RMod).
|
|
|
|
|
|
|
|
-spec shutdown_rooms(binary(), [binary()], module()) -> [pid()].
|
|
|
|
shutdown_rooms(ServerHost, Hosts, RMod) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Rooms = [RMod:get_online_rooms(ServerHost, Host, undefined)
|
|
|
|
|| Host <- Hosts],
|
2017-01-23 11:51:05 +01:00
|
|
|
lists:flatmap(
|
2017-01-13 10:03:39 +01:00
|
|
|
fun({_, _, Pid}) when node(Pid) == node() ->
|
2019-07-08 23:47:54 +02:00
|
|
|
mod_muc_room:shutdown(Pid),
|
2017-01-23 11:51:05 +01:00
|
|
|
[Pid];
|
2017-01-13 10:03:39 +01:00
|
|
|
(_) ->
|
2017-01-23 11:51:05 +01:00
|
|
|
[]
|
2019-06-14 11:33:26 +02:00
|
|
|
end, lists:flatten(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
|
2019-07-16 21:07:39 +02:00
|
|
|
%% So the message sending must be caught
|
2019-07-05 09:35:31 +02:00
|
|
|
-spec room_destroyed(binary(), binary(), pid(), binary()) -> ok.
|
2007-01-19 05:46:44 +01:00
|
|
|
room_destroyed(Host, Room, Pid, ServerHost) ->
|
2019-07-05 09:35:31 +02:00
|
|
|
Proc = procname(ServerHost, {Room, Host}),
|
|
|
|
?GEN_SERVER:cast(Proc, {room_destroyed, {Room, Host}, Pid}).
|
2006-02-02 06:00:27 +01:00
|
|
|
|
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) ->
|
2017-08-08 16:46:26 +02:00
|
|
|
ServerHost = ejabberd_router:host_of_route(Host),
|
2019-07-05 09:35:31 +02:00
|
|
|
Proc = procname(ServerHost, {Name, Host}),
|
|
|
|
?GEN_SERVER:call(Proc, {create, Name, Host, From, Nick, Opts}).
|
2009-02-16 16:24:40 +01:00
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
store_room(ServerHost, Host, Name, Opts) ->
|
2017-10-31 14:00:41 +01:00
|
|
|
store_room(ServerHost, Host, Name, Opts, undefined).
|
|
|
|
|
|
|
|
store_room(ServerHost, Host, Name, Opts, ChangesHints) ->
|
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),
|
2017-10-31 14:00:41 +01:00
|
|
|
Mod:store_room(LServer, Host, Name, Opts, ChangesHints).
|
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),
|
2017-08-10 13:49:05 +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
|
|
|
|
2017-01-13 10:03:39 +01:00
|
|
|
-spec find_online_room(binary(), binary()) -> {ok, pid()} | error.
|
|
|
|
find_online_room(Room, Host) ->
|
|
|
|
ServerHost = ejabberd_router:host_of_route(Host),
|
|
|
|
RMod = gen_mod:ram_db_mod(ServerHost, ?MODULE),
|
2017-03-29 11:58:01 +02:00
|
|
|
RMod:find_online_room(ServerHost, Room, Host).
|
2017-01-13 10:03:39 +01:00
|
|
|
|
|
|
|
-spec register_online_room(binary(), binary(), pid()) -> any().
|
|
|
|
register_online_room(Room, Host, Pid) ->
|
|
|
|
ServerHost = ejabberd_router:host_of_route(Host),
|
|
|
|
RMod = gen_mod:ram_db_mod(ServerHost, ?MODULE),
|
2017-03-29 11:58:01 +02:00
|
|
|
RMod:register_online_room(ServerHost, Room, Host, Pid).
|
2017-01-13 10:03:39 +01:00
|
|
|
|
|
|
|
-spec get_online_rooms(binary()) -> [{binary(), binary(), pid()}].
|
|
|
|
get_online_rooms(Host) ->
|
|
|
|
ServerHost = ejabberd_router:host_of_route(Host),
|
|
|
|
get_online_rooms(ServerHost, Host).
|
|
|
|
|
|
|
|
-spec count_online_rooms(binary()) -> non_neg_integer().
|
|
|
|
count_online_rooms(Host) ->
|
|
|
|
ServerHost = ejabberd_router:host_of_route(Host),
|
|
|
|
count_online_rooms(ServerHost, Host).
|
|
|
|
|
|
|
|
-spec register_online_user(binary(), ljid(), binary(), binary()) -> any().
|
|
|
|
register_online_user(ServerHost, LJID, Name, Host) ->
|
|
|
|
RMod = gen_mod:ram_db_mod(ServerHost, ?MODULE),
|
2017-03-29 11:58:01 +02:00
|
|
|
RMod:register_online_user(ServerHost, LJID, Name, Host).
|
2017-01-13 10:03:39 +01:00
|
|
|
|
|
|
|
-spec unregister_online_user(binary(), ljid(), binary(), binary()) -> any().
|
|
|
|
unregister_online_user(ServerHost, LJID, Name, Host) ->
|
|
|
|
RMod = gen_mod:ram_db_mod(ServerHost, ?MODULE),
|
2017-03-29 11:58:01 +02:00
|
|
|
RMod:unregister_online_user(ServerHost, LJID, Name, Host).
|
2017-01-13 10:03:39 +01:00
|
|
|
|
|
|
|
-spec count_online_rooms_by_user(binary(), binary(), binary()) -> non_neg_integer().
|
|
|
|
count_online_rooms_by_user(ServerHost, LUser, LServer) ->
|
|
|
|
RMod = gen_mod:ram_db_mod(ServerHost, ?MODULE),
|
2017-03-29 11:58:01 +02:00
|
|
|
RMod:count_online_rooms_by_user(ServerHost, LUser, LServer).
|
2017-01-13 10:03:39 +01:00
|
|
|
|
|
|
|
-spec get_online_rooms_by_user(binary(), binary(), binary()) -> [{binary(), binary()}].
|
|
|
|
get_online_rooms_by_user(ServerHost, LUser, LServer) ->
|
|
|
|
RMod = gen_mod:ram_db_mod(ServerHost, ?MODULE),
|
2017-03-29 11:58:01 +02:00
|
|
|
RMod:get_online_rooms_by_user(ServerHost, LUser, LServer).
|
2017-01-13 10:03:39 +01:00
|
|
|
|
2006-02-02 06:00:27 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
2019-07-16 17:34:05 +02:00
|
|
|
-spec init(list()) -> {ok, state()}.
|
2019-08-04 20:46:18 +02:00
|
|
|
init([Host, Worker]) ->
|
2017-02-14 08:25:08 +01:00
|
|
|
process_flag(trap_exit, true),
|
2019-08-04 20:46:18 +02:00
|
|
|
Opts = gen_mod:get_module_opts(Host, ?MODULE),
|
2019-07-05 09:35:31 +02:00
|
|
|
MyHosts = gen_mod:get_opt_hosts(Opts),
|
|
|
|
register_routes(Host, MyHosts, Worker),
|
|
|
|
register_iq_handlers(MyHosts, Worker),
|
2019-07-16 17:34:05 +02:00
|
|
|
{ok, #{server_host => Host, hosts => MyHosts, worker => Worker}}.
|
2017-02-22 17:46:47 +01:00
|
|
|
|
2019-07-16 17:34:05 +02:00
|
|
|
-spec handle_call(term(), {pid(), term()}, state()) ->
|
|
|
|
{reply, ok | {ok, pid()} | {error, any()}, state()} |
|
|
|
|
{stop, normal, ok, state()}.
|
2017-02-22 17:46:47 +01:00
|
|
|
handle_call(stop, _From, State) ->
|
|
|
|
{stop, normal, ok, State};
|
2017-08-08 16:46:26 +02:00
|
|
|
handle_call({create, Room, Host, From, Nick, Opts}, _From,
|
2019-07-16 17:34:05 +02:00
|
|
|
#{server_host := ServerHost} = State) ->
|
2017-02-22 17:46:47 +01:00
|
|
|
?DEBUG("MUC: create new room '~s'~n", [Room]),
|
|
|
|
NewOpts = case Opts of
|
2019-07-05 09:35:31 +02:00
|
|
|
default -> mod_muc_opt:default_room_options(ServerHost);
|
|
|
|
_ -> Opts
|
2017-02-22 17:46:47 +01:00
|
|
|
end,
|
|
|
|
RMod = gen_mod:ram_db_mod(ServerHost, ?MODULE),
|
2019-07-05 09:35:31 +02:00
|
|
|
case start_room(RMod, Host, ServerHost, Room, NewOpts, From, Nick) of
|
|
|
|
{ok, _} ->
|
|
|
|
ejabberd_hooks:run(create_room, ServerHost, [ServerHost, Room, Host]),
|
|
|
|
{reply, ok, State};
|
|
|
|
Err ->
|
|
|
|
{reply, Err, State}
|
|
|
|
end.
|
2017-02-22 17:46:47 +01:00
|
|
|
|
2019-07-16 17:34:05 +02:00
|
|
|
-spec handle_cast(term(), state()) -> {noreply, state()}.
|
|
|
|
handle_cast({route_to_room, Packet}, #{server_host := ServerHost} = State) ->
|
2019-07-05 09:35:31 +02:00
|
|
|
try route_to_room(Packet, ServerHost)
|
2019-07-06 11:30:57 +02:00
|
|
|
catch ?EX_RULE(Class, Reason, St) ->
|
2019-07-05 09:35:31 +02:00
|
|
|
StackTrace = ?EX_STACK(St),
|
2019-07-06 11:30:57 +02:00
|
|
|
?ERROR_MSG("Failed to route packet:~n~s~n** ~s",
|
|
|
|
[xmpp:pp(Packet),
|
|
|
|
misc:format_exception(2, Class, Reason, StackTrace)])
|
2017-02-22 17:46:47 +01:00
|
|
|
end,
|
2019-07-05 09:35:31 +02:00
|
|
|
{noreply, State};
|
2019-07-16 17:34:05 +02:00
|
|
|
handle_cast({room_destroyed, {Room, Host}, Pid},
|
|
|
|
#{server_host := ServerHost} = State) ->
|
2019-07-05 09:35:31 +02:00
|
|
|
RMod = gen_mod:ram_db_mod(ServerHost, ?MODULE),
|
|
|
|
RMod:unregister_online_room(ServerHost, Room, Host, Pid),
|
|
|
|
{noreply, State};
|
|
|
|
handle_cast({reload, AddHosts, DelHosts, NewHosts},
|
2019-07-16 17:34:05 +02:00
|
|
|
#{server_host := ServerHost, worker := Worker} = State) ->
|
2019-07-05 09:35:31 +02:00
|
|
|
register_routes(ServerHost, AddHosts, Worker),
|
|
|
|
register_iq_handlers(AddHosts, Worker),
|
|
|
|
unregister_routes(DelHosts, Worker),
|
|
|
|
unregister_iq_handlers(DelHosts, Worker),
|
2019-07-16 17:34:05 +02:00
|
|
|
{noreply, State#{hosts => NewHosts}};
|
2017-02-22 17:46:47 +01:00
|
|
|
handle_cast(Msg, State) ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?WARNING_MSG("Unexpected cast: ~p", [Msg]),
|
2017-02-22 17:46:47 +01:00
|
|
|
{noreply, State}.
|
|
|
|
|
2019-07-16 17:34:05 +02:00
|
|
|
-spec handle_info(term(), state()) -> {noreply, state()}.
|
|
|
|
handle_info({route, Packet}, #{server_host := ServerHost} = State) ->
|
2019-07-05 09:35:31 +02:00
|
|
|
%% We can only receive the packet here from other nodes
|
|
|
|
%% where mod_muc is not loaded. Such configuration
|
|
|
|
%% is *highly* discouraged
|
2019-07-16 17:34:05 +02:00
|
|
|
try route(Packet, ServerHost)
|
2019-07-06 11:30:57 +02:00
|
|
|
catch ?EX_RULE(Class, Reason, St) ->
|
2019-07-05 09:35:31 +02:00
|
|
|
StackTrace = ?EX_STACK(St),
|
2019-07-06 11:30:57 +02:00
|
|
|
?ERROR_MSG("Failed to route packet:~n~s~n** ~s",
|
|
|
|
[xmpp:pp(Packet),
|
|
|
|
misc:format_exception(2, Class, Reason, StackTrace)])
|
2017-02-22 17:46:47 +01:00
|
|
|
end,
|
|
|
|
{noreply, State};
|
|
|
|
handle_info({room_destroyed, {Room, Host}, Pid}, State) ->
|
2019-07-05 09:35:31 +02:00
|
|
|
%% For backward compat
|
|
|
|
handle_cast({room_destroyed, {Room, Host}, Pid}, State);
|
2017-02-22 17:46:47 +01:00
|
|
|
handle_info(Info, State) ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?ERROR_MSG("Unexpected info: ~p", [Info]),
|
2017-02-22 17:46:47 +01:00
|
|
|
{noreply, State}.
|
|
|
|
|
2019-07-16 17:34:05 +02:00
|
|
|
-spec terminate(term(), state()) -> any().
|
|
|
|
terminate(_Reason, #{hosts := Hosts, worker := Worker}) ->
|
2019-07-05 09:35:31 +02:00
|
|
|
unregister_routes(Hosts, Worker),
|
|
|
|
unregister_iq_handlers(Hosts, Worker).
|
2017-02-22 17:46:47 +01:00
|
|
|
|
2019-07-16 17:34:05 +02:00
|
|
|
-spec code_change(term(), state(), term()) -> {ok, state()}.
|
2017-02-22 17:46:47 +01:00
|
|
|
code_change(_OldVsn, State, _Extra) -> {ok, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%--------------------------------------------------------------------
|
2019-07-05 09:35:31 +02:00
|
|
|
-spec register_iq_handlers([binary()], pos_integer()) -> ok.
|
|
|
|
register_iq_handlers(Hosts, 1) ->
|
|
|
|
%% Only register handlers on first worker
|
|
|
|
lists:foreach(
|
|
|
|
fun(Host) ->
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_REGISTER,
|
|
|
|
?MODULE, process_register),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_VCARD,
|
|
|
|
?MODULE, process_vcard),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_MUCSUB,
|
|
|
|
?MODULE, process_mucsub),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_MUC_UNIQUE,
|
|
|
|
?MODULE, process_muc_unique),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_DISCO_INFO,
|
|
|
|
?MODULE, process_disco_info),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_DISCO_ITEMS,
|
|
|
|
?MODULE, process_disco_items)
|
|
|
|
end, Hosts);
|
|
|
|
register_iq_handlers(_, _) ->
|
|
|
|
ok.
|
2004-05-17 22:36:41 +02:00
|
|
|
|
2019-07-05 09:35:31 +02:00
|
|
|
-spec unregister_iq_handlers([binary()], pos_integer()) -> ok.
|
|
|
|
unregister_iq_handlers(Hosts, 1) ->
|
|
|
|
%% Only unregister handlers on first worker
|
|
|
|
lists:foreach(
|
|
|
|
fun(Host) ->
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_REGISTER),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_VCARD),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_MUCSUB),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_MUC_UNIQUE),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_DISCO_INFO),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_DISCO_ITEMS)
|
|
|
|
end, Hosts);
|
|
|
|
unregister_iq_handlers(_, _) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
-spec register_routes(binary(), [binary()], pos_integer()) -> ok.
|
|
|
|
register_routes(ServerHost, Hosts, 1) ->
|
|
|
|
%% Only register routes on first worker
|
|
|
|
lists:foreach(
|
|
|
|
fun(Host) ->
|
|
|
|
ejabberd_router:register_route(
|
|
|
|
Host, ServerHost, {apply, ?MODULE, route})
|
|
|
|
end, Hosts);
|
|
|
|
register_routes(_, _, _) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
-spec unregister_routes([binary()], pos_integer()) -> ok.
|
|
|
|
unregister_routes(Hosts, 1) ->
|
|
|
|
%% Only unregister routes on first worker
|
|
|
|
lists:foreach(
|
|
|
|
fun(Host) ->
|
|
|
|
ejabberd_router:unregister_route(Host)
|
|
|
|
end, Hosts);
|
|
|
|
unregister_routes(_, _) ->
|
|
|
|
ok.
|
|
|
|
|
2019-08-13 11:37:18 +02:00
|
|
|
%% Function copied from mod_muc_room.erl
|
|
|
|
-spec extract_password(presence() | iq()) -> binary() | false.
|
|
|
|
extract_password(#presence{} = Pres) ->
|
|
|
|
case xmpp:get_subtag(Pres, #muc{}) of
|
|
|
|
#muc{password = Password} when is_binary(Password) ->
|
|
|
|
Password;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
extract_password(#iq{} = IQ) ->
|
|
|
|
case xmpp:get_subtag(IQ, #muc_subscribe{}) of
|
|
|
|
#muc_subscribe{password = Password} when Password /= <<"">> ->
|
|
|
|
Password;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2019-07-05 09:35:31 +02:00
|
|
|
-spec route_to_room(stanza(), binary()) -> ok.
|
|
|
|
route_to_room(Packet, ServerHost) ->
|
|
|
|
From = xmpp:get_from(Packet),
|
|
|
|
To = xmpp:get_to(Packet),
|
|
|
|
{Room, Host, Nick} = jid:tolower(To),
|
2017-01-13 10:03:39 +01:00
|
|
|
RMod = gen_mod:ram_db_mod(ServerHost, ?MODULE),
|
2018-09-17 12:28:39 +02:00
|
|
|
case RMod:find_online_room(ServerHost, Room, Host) of
|
2017-01-13 10:03:39 +01:00
|
|
|
error ->
|
2019-07-05 13:46:48 +02:00
|
|
|
case should_start_room(Packet) of
|
2016-09-07 10:15:19 +02:00
|
|
|
false ->
|
2016-07-25 12:50:30 +02:00
|
|
|
Lang = xmpp:get_lang(Packet),
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Conference room does not exist"),
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_item_not_found(ErrText, Lang),
|
2019-07-05 13:46:48 +02:00
|
|
|
ejabberd_router:route_error(Packet, Err);
|
|
|
|
StartType ->
|
|
|
|
case load_room(RMod, Host, ServerHost, Room) of
|
2019-07-15 11:43:05 +02:00
|
|
|
{error, notfound} when StartType == start ->
|
2019-07-05 13:46:48 +02:00
|
|
|
case check_create_room(ServerHost, Host, Room, From) of
|
|
|
|
true ->
|
2019-08-13 11:37:18 +02:00
|
|
|
Pass = extract_password(Packet),
|
|
|
|
case start_new_room(RMod, Host, ServerHost, Room, Pass, From, Nick) of
|
2019-07-05 13:46:48 +02:00
|
|
|
{ok, Pid} ->
|
|
|
|
mod_muc_room:route(Pid, Packet);
|
|
|
|
_Err ->
|
|
|
|
Err = xmpp:err_internal_server_error(),
|
|
|
|
ejabberd_router:route_error(Packet, Err)
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
Lang = xmpp:get_lang(Packet),
|
|
|
|
ErrText = ?T("Room creation is denied by service policy"),
|
|
|
|
Err = xmpp:err_forbidden(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(Packet, Err)
|
|
|
|
end;
|
2019-07-15 11:43:05 +02:00
|
|
|
{error, notfound} ->
|
2019-07-05 13:46:48 +02:00
|
|
|
Lang = xmpp:get_lang(Packet),
|
|
|
|
ErrText = ?T("Conference room does not exist"),
|
|
|
|
Err = xmpp:err_item_not_found(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(Packet, Err);
|
2019-07-15 11:43:05 +02:00
|
|
|
{error, _} ->
|
|
|
|
Err = xmpp:err_internal_server_error(),
|
|
|
|
ejabberd_router:route_error(Packet, Err);
|
2019-07-05 13:46:48 +02:00
|
|
|
{ok, Pid2} ->
|
|
|
|
mod_muc_room:route(Pid2, Packet)
|
|
|
|
end
|
2016-07-25 12:50:30 +02:00
|
|
|
end;
|
2017-01-13 10:03:39 +01:00
|
|
|
{ok, Pid} ->
|
2019-07-05 09:35:31 +02:00
|
|
|
mod_muc_room:route(Pid, Packet)
|
2016-07-25 12:50:30 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
-spec process_vcard(iq()) -> iq().
|
2019-08-02 12:55:48 +02:00
|
|
|
process_vcard(#iq{type = get, to = To, lang = Lang, sub_els = [#vcard_temp{}]} = IQ) ->
|
|
|
|
ServerHost = ejabberd_router:host_of_route(To#jid.lserver),
|
|
|
|
VCard = case mod_muc_opt:vcard(ServerHost) of
|
|
|
|
undefined ->
|
|
|
|
#vcard_temp{fn = <<"ejabberd/mod_muc">>,
|
|
|
|
url = ejabberd_config:get_uri(),
|
|
|
|
desc = misc:get_descr(Lang, ?T("ejabberd MUC module"))};
|
|
|
|
V ->
|
|
|
|
V
|
|
|
|
end,
|
|
|
|
xmpp:make_iq_result(IQ, VCard);
|
2016-07-25 12:50:30 +02:00
|
|
|
process_vcard(#iq{type = set, lang = Lang} = IQ) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("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) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("No module is handling this query"),
|
2016-10-17 12:37:23 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_service_unavailable(Txt, Lang)).
|
2016-07-25 12:50:30 +02:00
|
|
|
|
|
|
|
-spec process_register(iq()) -> iq().
|
2018-04-24 11:29:59 +02:00
|
|
|
process_register(#iq{type = Type, from = From, to = To, 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),
|
2019-06-14 11:33:26 +02:00
|
|
|
AccessRegister = mod_muc_opt:access_register(ServerHost),
|
2018-04-24 11:29:59 +02:00
|
|
|
case acl:match_rule(ServerHost, AccessRegister, From) of
|
|
|
|
allow ->
|
|
|
|
case Type of
|
|
|
|
get ->
|
|
|
|
xmpp:make_iq_result(
|
|
|
|
IQ, iq_get_register_info(ServerHost, Host, From, Lang));
|
|
|
|
set ->
|
|
|
|
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)
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
deny ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Access denied by service policy"),
|
2018-04-24 11:29:59 +02:00
|
|
|
Err = xmpp:err_forbidden(ErrText, Lang),
|
2016-07-25 12:50:30 +02:00
|
|
|
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) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Value 'set' of 'type' attribute is not allowed"),
|
2016-07-25 12:50:30 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang));
|
2018-04-24 11:29:59 +02:00
|
|
|
process_disco_info(#iq{type = get, from = From, 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),
|
2017-01-13 10:03:39 +01:00
|
|
|
RMod = gen_mod:ram_db_mod(ServerHost, ?MODULE),
|
2019-06-14 11:33:26 +02:00
|
|
|
AccessRegister = mod_muc_opt:access_register(ServerHost),
|
2016-07-25 12:50:30 +02:00
|
|
|
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
|
2017-11-08 13:15:39 +01:00
|
|
|
true -> [?NS_MAM_TMP, ?NS_MAM_0, ?NS_MAM_1, ?NS_MAM_2];
|
2016-07-25 12:50:30 +02:00
|
|
|
false -> []
|
|
|
|
end,
|
2017-01-13 10:03:39 +01:00
|
|
|
RSMFeatures = case RMod:rsm_supported() of
|
|
|
|
true -> [?NS_RSM];
|
|
|
|
false -> []
|
|
|
|
end,
|
2018-04-24 11:29:59 +02:00
|
|
|
RegisterFeatures = case acl:match_rule(ServerHost, AccessRegister, From) of
|
|
|
|
allow -> [?NS_REGISTER];
|
|
|
|
deny -> []
|
|
|
|
end,
|
2016-07-25 12:50:30 +02:00
|
|
|
Features = [?NS_DISCO_INFO, ?NS_DISCO_ITEMS,
|
2018-04-24 11:29:59 +02:00
|
|
|
?NS_MUC, ?NS_VCARD, ?NS_MUCSUB, ?NS_MUC_UNIQUE
|
|
|
|
| RegisterFeatures ++ RSMFeatures ++ MAMFeatures],
|
2019-06-14 11:33:26 +02:00
|
|
|
Name = mod_muc_opt:name(ServerHost),
|
2016-07-25 12:50:30 +02:00
|
|
|
Identity = #identity{category = <<"conference">>,
|
|
|
|
type = <<"text">>,
|
2018-01-08 09:29:17 +01:00
|
|
|
name = translate:translate(Lang, Name)},
|
2016-07-25 12:50:30 +02:00
|
|
|
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) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_item_not_found(?T("Node not found"), Lang));
|
2016-10-17 12:37:23 +02:00
|
|
|
process_disco_info(#iq{lang = Lang} = IQ) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("No module is handling this query"),
|
2016-10-17 12:37:23 +02:00
|
|
|
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) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Value 'set' of 'type' attribute is not allowed"),
|
2016-07-25 12:50:30 +02:00
|
|
|
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),
|
2019-06-14 11:33:26 +02:00
|
|
|
MaxRoomsDiscoItems = mod_muc_opt:max_rooms_discoitems(ServerHost),
|
2017-01-13 10:03:39 +01:00
|
|
|
case iq_disco_items(ServerHost, Host, From, Lang,
|
|
|
|
MaxRoomsDiscoItems, Node, RSM) of
|
2016-11-20 16:08:49 +01:00
|
|
|
{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) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("No module is handling this query"),
|
2016-10-17 12:37:23 +02:00
|
|
|
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) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Value 'set' of 'type' attribute is not allowed"),
|
2016-07-25 12:50:30 +02:00
|
|
|
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) ->
|
2019-02-27 09:56:20 +01:00
|
|
|
Name = str:sha(term_to_binary([From, erlang:timestamp(),
|
2018-07-05 10:51:49 +02:00
|
|
|
p1_rand:get_string()])),
|
2016-07-25 12:50:30 +02:00
|
|
|
xmpp:make_iq_result(IQ, #muc_unique{name = Name}).
|
|
|
|
|
|
|
|
-spec process_mucsub(iq()) -> iq().
|
|
|
|
process_mucsub(#iq{type = set, lang = Lang} = IQ) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Value 'set' of 'type' attribute is not allowed"),
|
2016-07-25 12:50:30 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang));
|
2019-04-03 13:20:37 +02:00
|
|
|
process_mucsub(#iq{type = get, from = From, to = To, lang = Lang,
|
2016-10-17 12:37:23 +02:00
|
|
|
sub_els = [#muc_subscriptions{}]} = IQ) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
Host = To#jid.lserver,
|
|
|
|
ServerHost = ejabberd_router:host_of_route(Host),
|
2019-04-03 13:20:37 +02:00
|
|
|
case get_subscribed_rooms(ServerHost, Host, From) of
|
|
|
|
{ok, Subs} ->
|
|
|
|
List = [#muc_subscription{jid = JID, events = Nodes}
|
|
|
|
|| {JID, Nodes} <- Subs],
|
|
|
|
xmpp:make_iq_result(IQ, #muc_subscriptions{list = List});
|
|
|
|
{error, _} ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Database failure"),
|
2019-04-03 13:20:37 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_internal_server_error(Txt, Lang))
|
|
|
|
end;
|
2016-10-17 12:37:23 +02:00
|
|
|
process_mucsub(#iq{lang = Lang} = IQ) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("No module is handling this query"),
|
2016-10-17 12:37:23 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_service_unavailable(Txt, Lang)).
|
2016-07-25 12:50:30 +02:00
|
|
|
|
2019-07-05 13:46:48 +02:00
|
|
|
-spec should_start_room(stanza()) -> start | load | false.
|
|
|
|
should_start_room(#presence{type = available}) ->
|
|
|
|
start;
|
|
|
|
should_start_room(#iq{type = T} = IQ) when T == get; T == set ->
|
|
|
|
case xmpp:has_subtag(IQ, #muc_subscribe{}) orelse
|
|
|
|
xmpp:has_subtag(IQ, #muc_owner{}) of
|
|
|
|
true ->
|
|
|
|
start;
|
|
|
|
_ ->
|
|
|
|
load
|
|
|
|
end;
|
|
|
|
should_start_room(#message{type = T, to = #jid{lresource = <<>>}})
|
|
|
|
when T == groupchat; T == normal->
|
|
|
|
load;
|
|
|
|
should_start_room(#message{type = T, to = #jid{lresource = Res}})
|
|
|
|
when Res /= <<>> andalso T /= groupchat andalso T /= error ->
|
|
|
|
load;
|
|
|
|
should_start_room(_) ->
|
2016-09-07 10:15:19 +02:00
|
|
|
false.
|
|
|
|
|
2019-07-05 09:35:31 +02:00
|
|
|
-spec check_create_room(binary(), binary(), binary(), jid()) -> boolean().
|
|
|
|
check_create_room(ServerHost, Host, Room, From) ->
|
|
|
|
AccessCreate = mod_muc_opt:access_create(ServerHost),
|
2009-04-22 14:05:10 +02:00
|
|
|
case acl:match_rule(ServerHost, AccessCreate, From) of
|
2019-03-12 00:05:59 +01:00
|
|
|
allow ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case mod_muc_opt:max_room_id(ServerHost) of
|
2019-03-12 00:05:59 +01:00
|
|
|
Max when byte_size(Room) =< Max ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Regexp = mod_muc_opt:regexp_room_id(ServerHost),
|
2019-07-15 14:07:56 +02:00
|
|
|
case re:run(Room, Regexp, [{capture, none}]) of
|
2019-03-12 00:05:59 +01:00
|
|
|
match ->
|
2019-07-05 09:35:31 +02:00
|
|
|
AccessAdmin = mod_muc_opt:access_admin(ServerHost),
|
|
|
|
case acl:match_rule(ServerHost, AccessAdmin, From) of
|
2019-03-12 00:05:59 +01:00
|
|
|
allow ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
ejabberd_hooks:run_fold(
|
|
|
|
check_create_room, ServerHost, true,
|
|
|
|
[ServerHost, Room, Host])
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
false
|
2009-04-22 14:05:10 +02:00
|
|
|
end.
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2019-07-05 09:35:31 +02:00
|
|
|
-spec get_access(binary() | gen_mod:opts()) -> access().
|
|
|
|
get_access(ServerHost) ->
|
|
|
|
Access = mod_muc_opt:access(ServerHost),
|
|
|
|
AccessCreate = mod_muc_opt:access_create(ServerHost),
|
|
|
|
AccessAdmin = mod_muc_opt:access_admin(ServerHost),
|
|
|
|
AccessPersistent = mod_muc_opt:access_persistent(ServerHost),
|
|
|
|
AccessMam = mod_muc_opt:access_mam(ServerHost),
|
|
|
|
{Access, AccessCreate, AccessAdmin, AccessPersistent, AccessMam}.
|
|
|
|
|
|
|
|
-spec get_rooms(binary(), binary()) -> [#muc_room{}].
|
2012-04-27 11:52:05 +02:00
|
|
|
get_rooms(ServerHost, Host) ->
|
2019-07-05 09:35:31 +02:00
|
|
|
Mod = gen_mod:db_mod(ServerHost, ?MODULE),
|
|
|
|
Mod:get_rooms(ServerHost, Host).
|
2003-03-25 22:03:35 +01:00
|
|
|
|
2019-07-05 09:35:31 +02:00
|
|
|
-spec load_permanent_rooms([binary()], binary(), gen_mod:opts()) -> ok.
|
|
|
|
load_permanent_rooms(Hosts, ServerHost, Opts) ->
|
|
|
|
case mod_muc_opt:preload_rooms(Opts) of
|
|
|
|
true ->
|
|
|
|
Access = get_access(Opts),
|
|
|
|
HistorySize = mod_muc_opt:history_size(Opts),
|
|
|
|
QueueType = mod_muc_opt:queue_type(Opts),
|
|
|
|
RoomShaper = mod_muc_opt:room_shaper(Opts),
|
|
|
|
RMod = gen_mod:ram_db_mod(Opts, ?MODULE),
|
|
|
|
lists:foreach(
|
|
|
|
fun(Host) ->
|
|
|
|
?DEBUG("Loading rooms at ~s", [Host]),
|
|
|
|
lists:foreach(
|
|
|
|
fun(R) ->
|
|
|
|
{Room, _} = R#muc_room.name_host,
|
|
|
|
case proplists:get_bool(persistent, R#muc_room.opts) of
|
|
|
|
true ->
|
|
|
|
case RMod:find_online_room(ServerHost, Room, Host) of
|
|
|
|
error ->
|
|
|
|
start_room(RMod, Host, ServerHost, Access,
|
|
|
|
Room, HistorySize, RoomShaper,
|
|
|
|
R#muc_room.opts, QueueType);
|
|
|
|
{ok, _} ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
forget_room(ServerHost, Host, Room)
|
|
|
|
end
|
|
|
|
end, get_rooms(ServerHost, Host))
|
|
|
|
end, Hosts);
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end.
|
2012-04-27 11:52:05 +02:00
|
|
|
|
2019-07-15 11:43:05 +02:00
|
|
|
-spec load_room(module(), binary(), binary(), binary()) -> {ok, pid()} |
|
|
|
|
{error, notfound | term()}.
|
2019-07-05 13:46:48 +02:00
|
|
|
load_room(RMod, Host, ServerHost, Room) ->
|
|
|
|
case restore_room(ServerHost, Host, Room) of
|
2015-10-07 00:06:58 +02:00
|
|
|
error ->
|
2019-07-15 11:43:05 +02:00
|
|
|
{error, notfound};
|
2019-07-05 13:46:48 +02:00
|
|
|
Opts0 ->
|
|
|
|
case proplists:get_bool(persistent, Opts0) of
|
|
|
|
true ->
|
|
|
|
?DEBUG("Restore room: ~s", [Room]),
|
|
|
|
start_room(RMod, Host, ServerHost, Room, Opts0);
|
|
|
|
_ ->
|
2019-07-16 13:57:48 +02:00
|
|
|
?DEBUG("Restore hibernated non-persistent room: ~s", [Room]),
|
|
|
|
Res = start_room(RMod, Host, ServerHost, Room, Opts0),
|
|
|
|
Mod = gen_mod:db_mod(ServerHost, mod_muc),
|
|
|
|
case erlang:function_exported(Mod, get_subscribed_rooms, 3) of
|
|
|
|
true ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
forget_room(ServerHost, Host, Room)
|
|
|
|
end,
|
|
|
|
Res
|
2019-07-05 13:46:48 +02:00
|
|
|
end
|
2019-07-05 09:35:31 +02:00
|
|
|
end.
|
|
|
|
|
2019-08-13 11:37:18 +02:00
|
|
|
start_new_room(RMod, Host, ServerHost, Room, Pass, From, Nick) ->
|
2019-07-05 13:46:48 +02:00
|
|
|
?DEBUG("Open new room: ~s", [Room]),
|
|
|
|
DefRoomOpts = mod_muc_opt:default_room_options(ServerHost),
|
2019-08-13 11:37:18 +02:00
|
|
|
DefRoomOpts2 = add_password_options(Pass, DefRoomOpts),
|
|
|
|
start_room(RMod, Host, ServerHost, Room, DefRoomOpts2, From, Nick).
|
|
|
|
|
|
|
|
add_password_options(false, DefRoomOpts) ->
|
|
|
|
DefRoomOpts;
|
|
|
|
add_password_options(<<>>, DefRoomOpts) ->
|
|
|
|
DefRoomOpts;
|
|
|
|
add_password_options(Pass, DefRoomOpts) when is_binary(Pass) ->
|
|
|
|
O2 = lists:keystore(password, 1, DefRoomOpts, {password, Pass}),
|
|
|
|
lists:keystore(password_protected, 1, O2, {password_protected, true}).
|
2019-07-05 13:46:48 +02:00
|
|
|
|
2019-07-05 09:35:31 +02:00
|
|
|
start_room(Mod, Host, ServerHost, Room, DefOpts) ->
|
|
|
|
Access = get_access(ServerHost),
|
|
|
|
HistorySize = mod_muc_opt:history_size(ServerHost),
|
|
|
|
QueueType = mod_muc_opt:queue_type(ServerHost),
|
|
|
|
RoomShaper = mod_muc_opt:room_shaper(ServerHost),
|
|
|
|
start_room(Mod, Host, ServerHost, Access, Room, HistorySize,
|
|
|
|
RoomShaper, DefOpts, QueueType).
|
|
|
|
|
|
|
|
start_room(Mod, Host, ServerHost, Room, DefOpts, Creator, Nick) ->
|
|
|
|
Access = get_access(ServerHost),
|
|
|
|
HistorySize = mod_muc_opt:history_size(ServerHost),
|
|
|
|
QueueType = mod_muc_opt:queue_type(ServerHost),
|
|
|
|
RoomShaper = mod_muc_opt:room_shaper(ServerHost),
|
|
|
|
start_room(Mod, Host, ServerHost, Access, Room,
|
|
|
|
HistorySize, RoomShaper,
|
|
|
|
Creator, Nick, DefOpts, QueueType).
|
|
|
|
|
|
|
|
start_room(Mod, Host, ServerHost, Access, Room,
|
|
|
|
HistorySize, RoomShaper, DefOpts, QueueType) ->
|
|
|
|
case mod_muc_room:start(Host, ServerHost, Access, Room,
|
|
|
|
HistorySize, RoomShaper, DefOpts, QueueType) of
|
|
|
|
{ok, Pid} ->
|
|
|
|
Mod:register_online_room(ServerHost, Room, Host, Pid),
|
|
|
|
{ok, Pid};
|
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end.
|
|
|
|
|
|
|
|
start_room(Mod, Host, ServerHost, Access, Room, HistorySize,
|
|
|
|
RoomShaper, Creator, Nick, DefOpts, QueueType) ->
|
|
|
|
case mod_muc_room:start(Host, ServerHost, Access, Room,
|
|
|
|
HistorySize, RoomShaper,
|
|
|
|
Creator, Nick, DefOpts, QueueType) of
|
|
|
|
{ok, Pid} ->
|
|
|
|
Mod:register_online_room(ServerHost, Room, Host, Pid),
|
|
|
|
{ok, Pid};
|
|
|
|
Err ->
|
|
|
|
Err
|
2009-08-26 06:14:54 +02:00
|
|
|
end.
|
|
|
|
|
2017-01-13 10:03:39 +01:00
|
|
|
-spec iq_disco_items(binary(), binary(), jid(), binary(), integer(), binary(),
|
2016-11-20 16:08:49 +01:00
|
|
|
rsm_set() | undefined) ->
|
|
|
|
{result, disco_items()} | {error, stanza_error()}.
|
2017-01-13 10:03:39 +01:00
|
|
|
iq_disco_items(ServerHost, Host, From, Lang, MaxRoomsDiscoItems, Node, RSM)
|
2016-11-20 16:08:49 +01:00
|
|
|
when Node == <<"">>; Node == <<"nonemptyrooms">>; Node == <<"emptyrooms">> ->
|
2017-01-13 10:03:39 +01:00
|
|
|
Count = count_online_rooms(ServerHost, Host),
|
2016-11-20 16:08:49 +01:00
|
|
|
Query = if Node == <<"">>, RSM == undefined, Count > MaxRoomsDiscoItems ->
|
2019-07-08 23:47:54 +02:00
|
|
|
{only_non_empty, From, Lang};
|
2016-11-20 16:08:49 +01:00
|
|
|
Node == <<"nonemptyrooms">> ->
|
2019-07-08 23:47:54 +02:00
|
|
|
{only_non_empty, From, Lang};
|
2016-11-20 16:08:49 +01:00
|
|
|
Node == <<"emptyrooms">> ->
|
2019-07-08 23:47:54 +02:00
|
|
|
{0, From, Lang};
|
2016-11-20 16:08:49 +01:00
|
|
|
true ->
|
2019-07-08 23:47:54 +02:00
|
|
|
{all, From, Lang}
|
2016-11-20 16:08:49 +01:00
|
|
|
end,
|
2019-07-05 11:08:32 +02:00
|
|
|
MaxItems = case RSM of
|
|
|
|
undefined ->
|
|
|
|
MaxRoomsDiscoItems;
|
|
|
|
#rsm_set{max = undefined} ->
|
|
|
|
MaxRoomsDiscoItems;
|
|
|
|
#rsm_set{max = Max} when Max > MaxRoomsDiscoItems ->
|
|
|
|
MaxRoomsDiscoItems;
|
|
|
|
#rsm_set{max = Max} ->
|
|
|
|
Max
|
|
|
|
end,
|
|
|
|
{Items, HitMax} = lists:foldr(
|
|
|
|
fun(_, {Acc, _}) when length(Acc) >= MaxItems ->
|
|
|
|
{Acc, true};
|
|
|
|
(R, {Acc, _}) ->
|
|
|
|
case get_room_disco_item(R, Query) of
|
|
|
|
{ok, Item} -> {[Item | Acc], false};
|
|
|
|
{error, _} -> {Acc, false}
|
|
|
|
end
|
|
|
|
end, {[], false}, get_online_rooms(ServerHost, Host, RSM)),
|
2016-11-20 16:08:49 +01:00
|
|
|
ResRSM = case Items of
|
2019-07-05 11:08:32 +02:00
|
|
|
[_|_] when RSM /= undefined; HitMax ->
|
2016-11-20 16:08:49 +01:00
|
|
|
#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}};
|
2017-01-13 10:03:39 +01:00
|
|
|
iq_disco_items(_ServerHost, _Host, _From, Lang, _MaxRoomsDiscoItems, _Node, _RSM) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
{error, xmpp:err_item_not_found(?T("Node not found"), Lang)}.
|
2016-11-20 16:08:49 +01:00
|
|
|
|
2017-01-13 10:03:39 +01:00
|
|
|
-spec get_room_disco_item({binary(), binary(), pid()},
|
2019-07-08 23:47:54 +02:00
|
|
|
{mod_muc_room:disco_item_filter(),
|
|
|
|
jid(), binary()}) -> {ok, disco_item()} |
|
|
|
|
{error, timeout | notfound}.
|
|
|
|
get_room_disco_item({Name, Host, Pid}, {Filter, JID, Lang}) ->
|
|
|
|
case mod_muc_room:get_disco_item(Pid, Filter, JID, Lang) of
|
|
|
|
{ok, Desc} ->
|
|
|
|
RoomJID = jid:make(Name, Host),
|
2019-05-11 18:27:56 +02:00
|
|
|
{ok, #disco_item{jid = RoomJID, name = Desc}};
|
2019-07-08 23:47:54 +02:00
|
|
|
{error, _} = Err ->
|
|
|
|
Err
|
2016-11-20 16:08:49 +01:00
|
|
|
end.
|
2008-12-23 14:04:42 +01:00
|
|
|
|
2019-04-03 13:20:37 +02:00
|
|
|
-spec get_subscribed_rooms(binary(), jid()) -> {ok, [{jid(), [binary()]}]} | {error, any()}.
|
2019-03-28 17:42:25 +01:00
|
|
|
get_subscribed_rooms(Host, User) ->
|
|
|
|
ServerHost = ejabberd_router:host_of_route(Host),
|
|
|
|
get_subscribed_rooms(ServerHost, Host, User).
|
|
|
|
|
2019-04-03 13:20:37 +02:00
|
|
|
-spec get_subscribed_rooms(binary(), binary(), jid()) ->
|
|
|
|
{ok, [{jid(), [binary()]}]} | {error, any()}.
|
2017-01-13 10:03:39 +01:00
|
|
|
get_subscribed_rooms(ServerHost, Host, From) ->
|
2017-10-31 14:00:41 +01:00
|
|
|
LServer = jid:nameprep(ServerHost),
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
2016-09-09 12:04:47 +02:00
|
|
|
BareFrom = jid:remove_resource(From),
|
2019-04-03 13:20:37 +02:00
|
|
|
case erlang:function_exported(Mod, get_subscribed_rooms, 3) of
|
|
|
|
false ->
|
2017-10-31 14:00:41 +01:00
|
|
|
Rooms = get_online_rooms(ServerHost, Host),
|
2019-04-03 13:20:37 +02:00
|
|
|
{ok, lists:flatmap(
|
2019-05-06 19:15:48 +02:00
|
|
|
fun({Name, _, Pid}) when Pid == self() ->
|
|
|
|
USR = jid:split(BareFrom),
|
|
|
|
case erlang:get(muc_subscribers) of
|
|
|
|
#{USR := #subscriber{nodes = Nodes}} ->
|
|
|
|
[{jid:make(Name, Host), Nodes}];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end;
|
|
|
|
({Name, _, Pid}) ->
|
2019-07-08 23:47:54 +02:00
|
|
|
case mod_muc_room:is_subscribed(Pid, BareFrom) of
|
2019-04-03 13:20:37 +02:00
|
|
|
{true, Nodes} ->
|
|
|
|
[{jid:make(Name, Host), Nodes}];
|
|
|
|
false -> []
|
|
|
|
end;
|
|
|
|
(_) ->
|
|
|
|
[]
|
|
|
|
end, Rooms)};
|
|
|
|
true ->
|
|
|
|
Mod:get_subscribed_rooms(LServer, Host, BareFrom)
|
2017-10-31 14:00:41 +01:00
|
|
|
end.
|
2016-07-07 13:53:15 +02: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) ->
|
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(
|
2019-06-22 16:08:45 +02:00
|
|
|
Lang, ?T("Nickname Registration at ")))/binary, Host/binary>>,
|
|
|
|
Inst = translate:translate(Lang, ?T("Enter nickname you want to register")),
|
2017-03-20 07:57:11 +01:00
|
|
|
Fields = muc_register:encode([{roomnick, Nick}], Lang),
|
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,
|
2018-02-12 15:12:46 +01:00
|
|
|
instructions =
|
2016-07-25 12:50:30 +02:00
|
|
|
translate:translate(
|
2019-06-22 16:08:45 +02:00
|
|
|
Lang, ?T("You need a client that supports x:data "
|
|
|
|
"to register the nickname")),
|
2016-07-25 12:50:30 +02:00
|
|
|
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} ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("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
|
|
|
_ ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("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{} ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Incorrect data form"),
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, xmpp:err_bad_request(Txt, Lang)};
|
|
|
|
_ when is_binary(Nick), Nick /= <<"">> ->
|
|
|
|
iq_set_register_info(ServerHost, Host, From, Nick, Lang);
|
|
|
|
_ ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("You must fill in field \"Nickname\" in the form"),
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, xmpp:err_not_acceptable(ErrText, Lang)}
|
2003-04-17 21:47:43 +02:00
|
|
|
end.
|
|
|
|
|
2017-02-18 07:36:27 +01:00
|
|
|
-spec broadcast_service_message(binary(), binary(), binary()) -> ok.
|
2017-01-13 10:03:39 +01:00
|
|
|
broadcast_service_message(ServerHost, Host, Msg) ->
|
2003-05-18 18:41:15 +02:00
|
|
|
lists:foreach(
|
2017-01-13 10:03:39 +01:00
|
|
|
fun({_, _, Pid}) ->
|
2019-07-08 23:47:54 +02:00
|
|
|
mod_muc_room:service_message(Pid, Msg)
|
2017-01-13 10:03:39 +01:00
|
|
|
end, get_online_rooms(ServerHost, Host)).
|
|
|
|
|
|
|
|
-spec get_online_rooms(binary(), binary()) -> [{binary(), binary(), pid()}].
|
|
|
|
get_online_rooms(ServerHost, Host) ->
|
|
|
|
get_online_rooms(ServerHost, Host, undefined).
|
|
|
|
|
|
|
|
-spec get_online_rooms(binary(), binary(), undefined | rsm_set()) ->
|
|
|
|
[{binary(), binary(), pid()}].
|
|
|
|
get_online_rooms(ServerHost, Host, RSM) ->
|
|
|
|
RMod = gen_mod:ram_db_mod(ServerHost, ?MODULE),
|
2017-03-29 11:58:01 +02:00
|
|
|
RMod:get_online_rooms(ServerHost, Host, RSM).
|
2017-01-13 10:03:39 +01:00
|
|
|
|
|
|
|
-spec count_online_rooms(binary(), binary()) -> non_neg_integer().
|
|
|
|
count_online_rooms(ServerHost, Host) ->
|
|
|
|
RMod = gen_mod:ram_db_mod(ServerHost, ?MODULE),
|
2017-03-29 11:58:01 +02:00
|
|
|
RMod:count_online_rooms(ServerHost, Host).
|
2016-11-20 16:08:49 +01: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)};
|
2017-11-24 07:45:22 +01:00
|
|
|
({subject, [C|_] = Subj}) when is_integer(C), C >= 0, C =< 255 ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{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) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:acl();
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(access_admin) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:acl();
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(access_create) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:acl();
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(access_persistent) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:acl();
|
2019-01-02 17:35:01 +01:00
|
|
|
mod_opt_type(access_mam) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:acl();
|
2018-04-24 11:29:59 +02:00
|
|
|
mod_opt_type(access_register) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:acl();
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(history_size) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:non_neg_int();
|
|
|
|
mod_opt_type(name) ->
|
|
|
|
econf:binary();
|
2015-06-03 14:18:09 +02:00
|
|
|
mod_opt_type(max_room_desc) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:pos_int(infinity);
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(max_room_id) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:pos_int(infinity);
|
2016-10-18 22:59:34 +02:00
|
|
|
mod_opt_type(max_rooms_discoitems) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:non_neg_int();
|
2016-02-05 12:08:40 +01:00
|
|
|
mod_opt_type(regexp_room_id) ->
|
2019-07-15 14:26:47 +02:00
|
|
|
econf:re([unicode]);
|
2015-06-03 14:18:09 +02:00
|
|
|
mod_opt_type(max_room_name) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:pos_int(infinity);
|
2015-06-03 14:18:09 +02:00
|
|
|
mod_opt_type(max_user_conferences) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:pos_int();
|
2015-06-03 14:18:09 +02:00
|
|
|
mod_opt_type(max_users) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:pos_int();
|
2015-06-03 14:18:09 +02:00
|
|
|
mod_opt_type(max_users_admin_threshold) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:pos_int();
|
2015-06-03 14:18:09 +02:00
|
|
|
mod_opt_type(max_users_presence) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:int();
|
2015-06-03 14:18:09 +02:00
|
|
|
mod_opt_type(min_message_interval) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:number(0);
|
2015-06-03 14:18:09 +02:00
|
|
|
mod_opt_type(min_presence_interval) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:number(0);
|
2019-07-05 09:35:31 +02:00
|
|
|
mod_opt_type(preload_rooms) ->
|
|
|
|
econf:bool();
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(room_shaper) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:atom();
|
2015-06-03 14:18:09 +02:00
|
|
|
mod_opt_type(user_message_shaper) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:atom();
|
2015-06-03 14:18:09 +02:00
|
|
|
mod_opt_type(user_presence_shaper) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:atom();
|
|
|
|
mod_opt_type(default_room_options) ->
|
|
|
|
econf:options(
|
|
|
|
#{allow_change_subj => econf:bool(),
|
|
|
|
allow_private_messages => econf:bool(),
|
|
|
|
allow_private_messages_from_visitors =>
|
|
|
|
econf:enum([anyone, moderators, nobody]),
|
|
|
|
allow_query_users => econf:bool(),
|
|
|
|
allow_subscription => econf:bool(),
|
|
|
|
allow_user_invites => econf:bool(),
|
|
|
|
allow_visitor_nickchange => econf:bool(),
|
|
|
|
allow_visitor_status => econf:bool(),
|
|
|
|
anonymous => econf:bool(),
|
|
|
|
captcha_protected => econf:bool(),
|
|
|
|
lang => econf:lang(),
|
|
|
|
logging => econf:bool(),
|
|
|
|
mam => econf:bool(),
|
|
|
|
max_users => econf:pos_int(),
|
|
|
|
members_by_default => econf:bool(),
|
|
|
|
members_only => econf:bool(),
|
|
|
|
moderated => econf:bool(),
|
|
|
|
password => econf:binary(),
|
|
|
|
password_protected => econf:bool(),
|
|
|
|
persistent => econf:bool(),
|
|
|
|
presence_broadcast =>
|
|
|
|
econf:list(
|
|
|
|
econf:enum([moderator, participant, visitor])),
|
|
|
|
public => econf:bool(),
|
|
|
|
public_list => econf:bool(),
|
|
|
|
title => econf:binary()});
|
|
|
|
mod_opt_type(db_type) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:db_type(?MODULE);
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(ram_db_type) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:db_type(?MODULE);
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(host) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:host();
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(hosts) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:hosts();
|
2017-03-10 13:12:43 +01:00
|
|
|
mod_opt_type(queue_type) ->
|
2019-07-16 13:57:48 +02:00
|
|
|
econf:queue_type();
|
|
|
|
mod_opt_type(hibernation_timeout) ->
|
2019-08-02 12:55:48 +02:00
|
|
|
econf:timeout(second, infinity);
|
|
|
|
mod_opt_type(vcard) ->
|
|
|
|
econf:vcard_temp().
|
2018-01-23 08:54:52 +01:00
|
|
|
|
|
|
|
mod_options(Host) ->
|
|
|
|
[{access, all},
|
|
|
|
{access_admin, none},
|
|
|
|
{access_create, all},
|
|
|
|
{access_persistent, all},
|
2019-01-02 17:35:01 +01:00
|
|
|
{access_mam, all},
|
2018-04-24 11:29:59 +02:00
|
|
|
{access_register, all},
|
2018-01-23 08:54:52 +01:00
|
|
|
{db_type, ejabberd_config:default_db(Host, ?MODULE)},
|
|
|
|
{ram_db_type, ejabberd_config:default_ram_db(Host, ?MODULE)},
|
|
|
|
{history_size, 20},
|
2019-06-14 11:33:26 +02:00
|
|
|
{host, <<"conference.", Host/binary>>},
|
2018-01-23 08:54:52 +01:00
|
|
|
{hosts, []},
|
|
|
|
{name, ?T("Chatrooms")},
|
|
|
|
{max_room_desc, infinity},
|
|
|
|
{max_room_id, infinity},
|
|
|
|
{max_room_name, infinity},
|
|
|
|
{max_rooms_discoitems, 100},
|
2018-07-10 01:00:06 +02:00
|
|
|
{max_user_conferences, 100},
|
2018-01-23 08:54:52 +01:00
|
|
|
{max_users, 200},
|
|
|
|
{max_users_admin_threshold, 5},
|
|
|
|
{max_users_presence, 1000},
|
|
|
|
{min_message_interval, 0},
|
|
|
|
{min_presence_interval, 0},
|
2019-06-14 11:33:26 +02:00
|
|
|
{queue_type, ejabberd_option:queue_type(Host)},
|
2018-01-23 08:54:52 +01:00
|
|
|
{regexp_room_id, <<"">>},
|
|
|
|
{room_shaper, none},
|
|
|
|
{user_message_shaper, none},
|
|
|
|
{user_presence_shaper, none},
|
2019-07-05 09:35:31 +02:00
|
|
|
{preload_rooms, true},
|
2019-07-16 13:57:48 +02:00
|
|
|
{hibernation_timeout, infinity},
|
2019-08-02 12:55:48 +02:00
|
|
|
{vcard, undefined},
|
2018-01-23 08:54:52 +01:00
|
|
|
{default_room_options,
|
|
|
|
[{allow_change_subj,true},
|
|
|
|
{allow_private_messages,true},
|
|
|
|
{allow_query_users,true},
|
|
|
|
{allow_user_invites,false},
|
|
|
|
{allow_visitor_nickchange,true},
|
|
|
|
{allow_visitor_status,true},
|
|
|
|
{anonymous,true},
|
|
|
|
{captcha_protected,false},
|
2018-07-16 15:15:36 +02:00
|
|
|
{lang,<<>>},
|
2018-01-23 08:54:52 +01:00
|
|
|
{logging,false},
|
|
|
|
{members_by_default,true},
|
|
|
|
{members_only,false},
|
|
|
|
{moderated,true},
|
|
|
|
{password_protected,false},
|
|
|
|
{persistent,false},
|
|
|
|
{public,true},
|
|
|
|
{public_list,true},
|
|
|
|
{mam,false},
|
|
|
|
{allow_subscription,false},
|
|
|
|
{password,<<>>},
|
|
|
|
{title,<<>>},
|
|
|
|
{allow_private_messages_from_visitors,anyone},
|
|
|
|
{max_users,200},
|
|
|
|
{presence_broadcast,[moderator,participant,visitor]}]}].
|