mirror of
https://github.com/processone/ejabberd.git
synced 2024-12-02 16:37:52 +01:00
4731 lines
166 KiB
Erlang
4731 lines
166 KiB
Erlang
%%%----------------------------------------------------------------------
|
|
%%% File : mod_muc_room.erl
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
|
%%% Purpose : MUC room stuff
|
|
%%% Created : 19 Mar 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
%%%
|
|
%%%
|
|
%%% ejabberd, Copyright (C) 2002-2019 ProcessOne
|
|
%%%
|
|
%%% 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.
|
|
%%%
|
|
%%% 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.
|
|
%%%
|
|
%%%----------------------------------------------------------------------
|
|
|
|
-module(mod_muc_room).
|
|
|
|
-author('alexey@process-one.net').
|
|
|
|
-behaviour(p1_fsm).
|
|
|
|
%% External exports
|
|
-export([start_link/10,
|
|
start_link/8,
|
|
start/10,
|
|
start/8,
|
|
supervisor/1,
|
|
get_role/2,
|
|
get_affiliation/2,
|
|
is_occupant_or_admin/2,
|
|
route/2,
|
|
expand_opts/1,
|
|
config_fields/0,
|
|
destroy/1,
|
|
destroy/2,
|
|
shutdown/1,
|
|
get_config/1,
|
|
set_config/2,
|
|
get_state/1,
|
|
change_item/5,
|
|
config_reloaded/1,
|
|
subscribe/4,
|
|
unsubscribe/2,
|
|
is_subscribed/2,
|
|
get_subscribers/1,
|
|
service_message/2,
|
|
get_disco_item/4]).
|
|
|
|
%% gen_fsm callbacks
|
|
-export([init/1,
|
|
normal_state/2,
|
|
handle_event/3,
|
|
handle_sync_event/4,
|
|
handle_info/3,
|
|
terminate/3,
|
|
code_change/4]).
|
|
|
|
-include("logger.hrl").
|
|
-include("xmpp.hrl").
|
|
-include("translate.hrl").
|
|
-include("mod_muc_room.hrl").
|
|
-include("ejabberd_stacktrace.hrl").
|
|
|
|
-define(MAX_USERS_DEFAULT_LIST,
|
|
[5, 10, 20, 30, 50, 100, 200, 500, 1000, 2000, 5000]).
|
|
|
|
-define(DEFAULT_MAX_USERS_PRESENCE,1000).
|
|
|
|
%-define(DBGFSM, true).
|
|
|
|
-ifdef(DBGFSM).
|
|
|
|
-define(FSMOPTS, [{debug, [trace]}]).
|
|
|
|
-else.
|
|
|
|
-define(FSMOPTS, []).
|
|
|
|
-endif.
|
|
|
|
-type state() :: #state{}.
|
|
-type fsm_stop() :: {stop, normal, state()}.
|
|
-type fsm_next() :: {next_state, normal_state, state()}.
|
|
-type fsm_transition() :: fsm_stop() | fsm_next().
|
|
-type disco_item_filter() :: only_non_empty | all | non_neg_integer().
|
|
-type admin_action() :: {jid(), affiliation | role, affiliation() | role(), binary()}.
|
|
-export_type([state/0, disco_item_filter/0]).
|
|
|
|
-callback set_affiliation(binary(), binary(), binary(), jid(), affiliation(),
|
|
binary()) -> ok | {error, any()}.
|
|
-callback set_affiliations(binary(), binary(), binary(),
|
|
affiliations()) -> ok | {error, any()}.
|
|
-callback get_affiliation(binary(), binary(), binary(),
|
|
binary(), binary()) -> {ok, affiliation()} | {error, any()}.
|
|
-callback get_affiliations(binary(), binary(), binary()) -> {ok, affiliations()} | {error, any()}.
|
|
-callback search_affiliation(binary(), binary(), binary(), affiliation()) ->
|
|
{ok, [{ljid(), {affiliation(), binary()}}]} | {error, any()}.
|
|
|
|
%%%----------------------------------------------------------------------
|
|
%%% API
|
|
%%%----------------------------------------------------------------------
|
|
-spec start(binary(), binary(), mod_muc:access(), binary(), non_neg_integer(),
|
|
atom(), jid(), binary(), [{atom(), term()}], ram | file) ->
|
|
{ok, pid()} | {error, any()}.
|
|
start(Host, ServerHost, Access, Room, HistorySize, RoomShaper,
|
|
Creator, Nick, DefRoomOpts, QueueType) ->
|
|
supervisor:start_child(
|
|
supervisor(ServerHost),
|
|
[Host, ServerHost, Access, Room, HistorySize,
|
|
RoomShaper, Creator, Nick, DefRoomOpts, QueueType]).
|
|
|
|
-spec start(binary(), binary(), mod_muc:access(), binary(), non_neg_integer(),
|
|
atom(), [{atom(), term()}], ram | file) ->
|
|
{ok, pid()} | {error, any()}.
|
|
start(Host, ServerHost, Access, Room, HistorySize, RoomShaper, Opts, QueueType) ->
|
|
supervisor:start_child(
|
|
supervisor(ServerHost),
|
|
[Host, ServerHost, Access, Room, HistorySize,
|
|
RoomShaper, Opts, QueueType]).
|
|
|
|
-spec start_link(binary(), binary(), mod_muc:access(), binary(), non_neg_integer(),
|
|
atom(), jid(), binary(), [{atom(), term()}], ram | file) ->
|
|
{ok, pid()} | {error, any()}.
|
|
start_link(Host, ServerHost, Access, Room, HistorySize, RoomShaper,
|
|
Creator, Nick, DefRoomOpts, QueueType) ->
|
|
p1_fsm:start_link(?MODULE, [Host, ServerHost, Access, Room, HistorySize,
|
|
RoomShaper, Creator, Nick, DefRoomOpts, QueueType],
|
|
?FSMOPTS).
|
|
|
|
-spec start_link(binary(), binary(), mod_muc:access(), binary(), non_neg_integer(),
|
|
atom(), [{atom(), term()}], ram | file) ->
|
|
{ok, pid()} | {error, any()}.
|
|
start_link(Host, ServerHost, Access, Room, HistorySize, RoomShaper, Opts, QueueType) ->
|
|
p1_fsm:start_link(?MODULE, [Host, ServerHost, Access, Room, HistorySize,
|
|
RoomShaper, Opts, QueueType],
|
|
?FSMOPTS).
|
|
|
|
-spec supervisor(binary()) -> atom().
|
|
supervisor(Host) ->
|
|
gen_mod:get_module_proc(Host, mod_muc_room_sup).
|
|
|
|
-spec destroy(pid()) -> ok.
|
|
destroy(Pid) ->
|
|
p1_fsm:send_all_state_event(Pid, destroy).
|
|
|
|
-spec destroy(pid(), binary()) -> ok.
|
|
destroy(Pid, Reason) ->
|
|
p1_fsm:send_all_state_event(Pid, {destroy, Reason}).
|
|
|
|
-spec shutdown(pid()) -> boolean().
|
|
shutdown(Pid) ->
|
|
ejabberd_cluster:send(Pid, shutdown).
|
|
|
|
-spec config_reloaded(pid()) -> boolean().
|
|
config_reloaded(Pid) ->
|
|
ejabberd_cluster:send(Pid, config_reloaded).
|
|
|
|
-spec get_config(pid()) -> {ok, config()} | {error, notfound | timeout}.
|
|
get_config(Pid) ->
|
|
try p1_fsm:sync_send_all_state_event(Pid, get_config)
|
|
catch _:{timeout, {p1_fsm, _, _}} ->
|
|
{error, timeout};
|
|
_:{_, {p1_fsm, _, _}} ->
|
|
{error, notfound}
|
|
end.
|
|
|
|
-spec set_config(pid(), config()) -> {ok, config()} | {error, notfound | timeout}.
|
|
set_config(Pid, Config) ->
|
|
try p1_fsm:sync_send_all_state_event(Pid, {change_config, Config})
|
|
catch _:{timeout, {p1_fsm, _, _}} ->
|
|
{error, timeout};
|
|
_:{_, {p1_fsm, _, _}} ->
|
|
{error, notfound}
|
|
end.
|
|
|
|
-spec change_item(pid(), jid(), affiliation | role, affiliation() | role(), binary()) ->
|
|
{ok, state()} | {error, notfound | timeout}.
|
|
change_item(Pid, JID, Type, AffiliationOrRole, Reason) ->
|
|
try p1_fsm:sync_send_all_state_event(
|
|
Pid, {process_item_change, {JID, Type, AffiliationOrRole, Reason}, undefined})
|
|
catch _:{timeout, {p1_fsm, _, _}} ->
|
|
{error, timeout};
|
|
_:{_, {p1_fsm, _, _}} ->
|
|
{error, notfound}
|
|
end.
|
|
|
|
-spec get_state(pid()) -> {ok, state()} | {error, notfound | timeout}.
|
|
get_state(Pid) ->
|
|
try p1_fsm:sync_send_all_state_event(Pid, get_state)
|
|
catch _:{timeout, {p1_fsm, _, _}} ->
|
|
{error, timeout};
|
|
_:{_, {p1_fsm, _, _}} ->
|
|
{error, notfound}
|
|
end.
|
|
|
|
-spec subscribe(pid(), jid(), binary(), [binary()]) -> {ok, [binary()]} | {error, binary()}.
|
|
subscribe(Pid, JID, Nick, Nodes) ->
|
|
try p1_fsm:sync_send_all_state_event(Pid, {muc_subscribe, JID, Nick, Nodes})
|
|
catch _:{timeout, {p1_fsm, _, _}} ->
|
|
{error, ?T("Request has timed out")};
|
|
_:{_, {p1_fsm, _, _}} ->
|
|
{error, ?T("Conference room does not exist")}
|
|
end.
|
|
|
|
-spec unsubscribe(pid(), jid()) -> ok | {error, binary()}.
|
|
unsubscribe(Pid, JID) ->
|
|
try p1_fsm:sync_send_all_state_event(Pid, {muc_unsubscribe, JID})
|
|
catch _:{timeout, {p1_fsm, _, _}} ->
|
|
{error, ?T("Request has timed out")};
|
|
_:{_, {p1_fsm, _, _}} ->
|
|
{error, ?T("Conference room does not exist")}
|
|
end.
|
|
|
|
-spec is_subscribed(pid(), jid()) -> {true, [binary()]} | false.
|
|
is_subscribed(Pid, JID) ->
|
|
try p1_fsm:sync_send_all_state_event(Pid, {is_subscribed, JID})
|
|
catch _:{_, {p1_fsm, _, _}} -> false
|
|
end.
|
|
|
|
-spec get_subscribers(pid()) -> {ok, [jid()]} | {error, notfound | timeout}.
|
|
get_subscribers(Pid) ->
|
|
try p1_fsm:sync_send_all_state_event(Pid, get_subscribers)
|
|
catch _:{timeout, {p1_fsm, _, _}} ->
|
|
{error, timeout};
|
|
_:{_, {p1_fsm, _, _}} ->
|
|
{error, notfound}
|
|
end.
|
|
|
|
-spec service_message(pid(), binary()) -> ok.
|
|
service_message(Pid, Text) ->
|
|
p1_fsm:send_all_state_event(Pid, {service_message, Text}).
|
|
|
|
-spec get_disco_item(pid(), disco_item_filter(), jid(), binary()) ->
|
|
{ok, binary()} | {error, notfound | timeout}.
|
|
get_disco_item(Pid, Filter, JID, Lang) ->
|
|
Timeout = 100,
|
|
Time = erlang:system_time(millisecond),
|
|
Query = {get_disco_item, Filter, JID, Lang, Time+Timeout},
|
|
try p1_fsm:sync_send_all_state_event(Pid, Query, Timeout) of
|
|
{item, Desc} ->
|
|
{ok, Desc};
|
|
false ->
|
|
{error, notfound}
|
|
catch _:{timeout, {p1_fsm, _, _}} ->
|
|
{error, timeout};
|
|
_:{_, {p1_fsm, _, _}} ->
|
|
{error, notfound}
|
|
end.
|
|
|
|
%%%----------------------------------------------------------------------
|
|
%%% Callback functions from gen_fsm
|
|
%%%----------------------------------------------------------------------
|
|
|
|
init([Host, ServerHost, Access, Room, HistorySize,
|
|
RoomShaper, Creator, _Nick, DefRoomOpts, QueueType]) ->
|
|
process_flag(trap_exit, true),
|
|
Shaper = ejabberd_shaper:new(RoomShaper),
|
|
RoomQueue = room_queue_new(ServerHost, Shaper, QueueType),
|
|
State = set_affiliation(Creator, owner,
|
|
#state{host = Host, server_host = ServerHost,
|
|
access = Access, room = Room,
|
|
history = lqueue_new(HistorySize, QueueType),
|
|
jid = jid:make(Room, Host),
|
|
just_created = true,
|
|
room_queue = RoomQueue,
|
|
room_shaper = Shaper}),
|
|
State1 = set_opts(DefRoomOpts, State),
|
|
store_room(State1),
|
|
?INFO_MSG("Created MUC room ~s@~s by ~s",
|
|
[Room, Host, jid:encode(Creator)]),
|
|
add_to_log(room_existence, created, State1),
|
|
add_to_log(room_existence, started, State1),
|
|
{ok, normal_state, reset_hibernate_timer(State1)};
|
|
init([Host, ServerHost, Access, Room, HistorySize, RoomShaper, Opts, QueueType]) ->
|
|
process_flag(trap_exit, true),
|
|
Shaper = ejabberd_shaper:new(RoomShaper),
|
|
RoomQueue = room_queue_new(ServerHost, Shaper, QueueType),
|
|
State = set_opts(Opts, #state{host = Host,
|
|
server_host = ServerHost,
|
|
access = Access,
|
|
room = Room,
|
|
history = lqueue_new(HistorySize, QueueType),
|
|
jid = jid:make(Room, Host),
|
|
room_queue = RoomQueue,
|
|
room_shaper = Shaper}),
|
|
add_to_log(room_existence, started, State),
|
|
{ok, normal_state, reset_hibernate_timer(State)}.
|
|
|
|
normal_state({route, <<"">>,
|
|
#message{from = From, type = Type, lang = Lang} = Packet},
|
|
StateData) ->
|
|
case is_user_online(From, StateData) orelse
|
|
is_subscriber(From, StateData) orelse
|
|
is_user_allowed_message_nonparticipant(From, StateData) of
|
|
true when Type == groupchat ->
|
|
Activity = get_user_activity(From, StateData),
|
|
Now = erlang:system_time(microsecond),
|
|
MinMessageInterval = trunc(mod_muc_opt:min_message_interval(StateData#state.server_host) * 1000000),
|
|
Size = element_size(Packet),
|
|
{MessageShaper, MessageShaperInterval} =
|
|
ejabberd_shaper:update(Activity#activity.message_shaper, Size),
|
|
if Activity#activity.message /= undefined ->
|
|
ErrText = ?T("Traffic rate limit is exceeded"),
|
|
Err = xmpp:err_resource_constraint(ErrText, Lang),
|
|
ejabberd_router:route_error(Packet, Err),
|
|
{next_state, normal_state, StateData};
|
|
Now >= Activity#activity.message_time + MinMessageInterval,
|
|
MessageShaperInterval == 0 ->
|
|
{RoomShaper, RoomShaperInterval} =
|
|
ejabberd_shaper:update(StateData#state.room_shaper, Size),
|
|
RoomQueueEmpty = case StateData#state.room_queue of
|
|
undefined -> true;
|
|
RQ -> p1_queue:is_empty(RQ)
|
|
end,
|
|
if RoomShaperInterval == 0, RoomQueueEmpty ->
|
|
NewActivity = Activity#activity{
|
|
message_time = Now,
|
|
message_shaper = MessageShaper},
|
|
StateData1 = store_user_activity(From,
|
|
NewActivity,
|
|
StateData),
|
|
StateData2 = StateData1#state{room_shaper =
|
|
RoomShaper},
|
|
process_groupchat_message(Packet,
|
|
StateData2);
|
|
true ->
|
|
StateData1 = if RoomQueueEmpty ->
|
|
erlang:send_after(RoomShaperInterval,
|
|
self(),
|
|
process_room_queue),
|
|
StateData#state{room_shaper =
|
|
RoomShaper};
|
|
true -> StateData
|
|
end,
|
|
NewActivity = Activity#activity{
|
|
message_time = Now,
|
|
message_shaper = MessageShaper,
|
|
message = Packet},
|
|
RoomQueue = p1_queue:in({message, From},
|
|
StateData#state.room_queue),
|
|
StateData2 = store_user_activity(From,
|
|
NewActivity,
|
|
StateData1),
|
|
StateData3 = StateData2#state{room_queue = RoomQueue},
|
|
{next_state, normal_state, StateData3}
|
|
end;
|
|
true ->
|
|
MessageInterval = (Activity#activity.message_time +
|
|
MinMessageInterval - Now) div 1000,
|
|
Interval = lists:max([MessageInterval,
|
|
MessageShaperInterval]),
|
|
erlang:send_after(Interval, self(),
|
|
{process_user_message, From}),
|
|
NewActivity = Activity#activity{
|
|
message = Packet,
|
|
message_shaper = MessageShaper},
|
|
StateData1 = store_user_activity(From, NewActivity, StateData),
|
|
{next_state, normal_state, StateData1}
|
|
end;
|
|
true when Type == error ->
|
|
case is_user_online(From, StateData) of
|
|
true ->
|
|
ErrorText = ?T("It is not allowed to send error messages to the"
|
|
" room. The participant (~s) has sent an error "
|
|
"message (~s) and got kicked from the room"),
|
|
NewState = expulse_participant(Packet, From, StateData,
|
|
translate:translate(Lang,
|
|
ErrorText)),
|
|
close_room_if_temporary_and_empty(NewState);
|
|
_ ->
|
|
{next_state, normal_state, StateData}
|
|
end;
|
|
true when Type == chat ->
|
|
ErrText = ?T("It is not allowed to send private messages "
|
|
"to the conference"),
|
|
Err = xmpp:err_not_acceptable(ErrText, Lang),
|
|
ejabberd_router:route_error(Packet, Err),
|
|
{next_state, normal_state, StateData};
|
|
true when Type == normal ->
|
|
{next_state, normal_state,
|
|
try xmpp:decode_els(Packet) of
|
|
Pkt -> process_normal_message(From, Pkt, StateData)
|
|
catch _:{xmpp_codec, Why} ->
|
|
Txt = xmpp:io_format_error(Why),
|
|
Err = xmpp:err_bad_request(Txt, Lang),
|
|
ejabberd_router:route_error(Packet, Err),
|
|
StateData
|
|
end};
|
|
true ->
|
|
ErrText = ?T("Improper message type"),
|
|
Err = xmpp:err_not_acceptable(ErrText, Lang),
|
|
ejabberd_router:route_error(Packet, Err),
|
|
{next_state, normal_state, StateData};
|
|
false when Type /= error ->
|
|
handle_roommessage_from_nonparticipant(Packet, StateData, From),
|
|
{next_state, normal_state, StateData};
|
|
false ->
|
|
{next_state, normal_state, StateData}
|
|
end;
|
|
normal_state({route, <<"">>,
|
|
#iq{from = From, type = Type, lang = Lang, sub_els = [_]} = IQ0},
|
|
StateData) when Type == get; Type == set ->
|
|
try
|
|
case ejabberd_hooks:run_fold(
|
|
muc_process_iq,
|
|
StateData#state.server_host,
|
|
xmpp:set_from_to(xmpp:decode_els(IQ0),
|
|
From, StateData#state.jid),
|
|
[StateData]) of
|
|
ignore ->
|
|
{next_state, normal_state, StateData};
|
|
#iq{type = T} = IQRes when T == error; T == result ->
|
|
ejabberd_router:route(IQRes),
|
|
{next_state, normal_state, StateData};
|
|
#iq{sub_els = [SubEl]} = IQ ->
|
|
Res1 = case SubEl of
|
|
#muc_admin{} ->
|
|
process_iq_admin(From, IQ, StateData);
|
|
#muc_owner{} ->
|
|
process_iq_owner(From, IQ, StateData);
|
|
#disco_info{} ->
|
|
process_iq_disco_info(From, IQ, StateData);
|
|
#disco_items{} ->
|
|
process_iq_disco_items(From, IQ, StateData);
|
|
#vcard_temp{} ->
|
|
process_iq_vcard(From, IQ, StateData);
|
|
#muc_subscribe{} ->
|
|
process_iq_mucsub(From, IQ, StateData);
|
|
#muc_unsubscribe{} ->
|
|
process_iq_mucsub(From, IQ, StateData);
|
|
#muc_subscriptions{} ->
|
|
process_iq_mucsub(From, IQ, StateData);
|
|
#xcaptcha{} ->
|
|
process_iq_captcha(From, IQ, StateData);
|
|
_ ->
|
|
Txt = ?T("The feature requested is not "
|
|
"supported by the conference"),
|
|
{error, xmpp:err_service_unavailable(Txt, Lang)}
|
|
end,
|
|
{IQRes, NewStateData} =
|
|
case Res1 of
|
|
{result, Res, SD} ->
|
|
{xmpp:make_iq_result(IQ, Res), SD};
|
|
{result, Res} ->
|
|
{xmpp:make_iq_result(IQ, Res), StateData};
|
|
{ignore, SD} ->
|
|
{ignore, SD};
|
|
{error, Error} ->
|
|
{xmpp:make_error(IQ0, Error), StateData}
|
|
end,
|
|
if IQRes /= ignore ->
|
|
ejabberd_router:route(IQRes);
|
|
true ->
|
|
ok
|
|
end,
|
|
case NewStateData of
|
|
stop ->
|
|
Conf = StateData#state.config,
|
|
{stop, normal, StateData#state{config = Conf#config{persistent = false}}};
|
|
_ when NewStateData#state.just_created ->
|
|
close_room_if_temporary_and_empty(NewStateData);
|
|
_ ->
|
|
{next_state, normal_state, NewStateData}
|
|
end
|
|
end
|
|
catch _:{xmpp_codec, Why} ->
|
|
ErrTxt = xmpp:io_format_error(Why),
|
|
Err = xmpp:err_bad_request(ErrTxt, Lang),
|
|
ejabberd_router:route_error(IQ0, Err),
|
|
{next_state, normal_state, StateData}
|
|
end;
|
|
normal_state({route, <<"">>, #iq{} = IQ}, StateData) ->
|
|
Err = xmpp:err_bad_request(),
|
|
ejabberd_router:route_error(IQ, Err),
|
|
case StateData#state.just_created of
|
|
true -> {stop, normal, StateData};
|
|
_ -> {next_state, normal_state, StateData}
|
|
end;
|
|
normal_state({route, Nick, #presence{from = From} = Packet}, StateData) ->
|
|
Activity = get_user_activity(From, StateData),
|
|
Now = erlang:system_time(microsecond),
|
|
MinPresenceInterval =
|
|
trunc(mod_muc_opt:min_presence_interval(StateData#state.server_host) * 1000000),
|
|
if (Now >= Activity#activity.presence_time + MinPresenceInterval)
|
|
and (Activity#activity.presence == undefined) ->
|
|
NewActivity = Activity#activity{presence_time = Now},
|
|
StateData1 = store_user_activity(From, NewActivity,
|
|
StateData),
|
|
process_presence(Nick, Packet, StateData1);
|
|
true ->
|
|
if Activity#activity.presence == undefined ->
|
|
Interval = (Activity#activity.presence_time +
|
|
MinPresenceInterval - Now) div 1000,
|
|
erlang:send_after(Interval, self(),
|
|
{process_user_presence, From});
|
|
true -> ok
|
|
end,
|
|
NewActivity = Activity#activity{presence = {Nick, Packet}},
|
|
StateData1 = store_user_activity(From, NewActivity,
|
|
StateData),
|
|
{next_state, normal_state, StateData1}
|
|
end;
|
|
normal_state({route, ToNick,
|
|
#message{from = From, type = Type, lang = Lang} = Packet},
|
|
StateData) ->
|
|
case decide_fate_message(Packet, From, StateData) of
|
|
{expulse_sender, Reason} ->
|
|
?DEBUG(Reason, []),
|
|
ErrorText = ?T("It is not allowed to send error messages to the"
|
|
" room. The participant (~s) has sent an error "
|
|
"message (~s) and got kicked from the room"),
|
|
NewState = expulse_participant(Packet, From, StateData,
|
|
translate:translate(Lang, ErrorText)),
|
|
{next_state, normal_state, NewState};
|
|
forget_message ->
|
|
{next_state, normal_state, StateData};
|
|
continue_delivery ->
|
|
case {(StateData#state.config)#config.allow_private_messages,
|
|
is_user_online(From, StateData) orelse
|
|
is_subscriber(From, StateData)} of
|
|
{true, true} when Type == groupchat ->
|
|
ErrText = ?T("It is not allowed to send private messages "
|
|
"of type \"groupchat\""),
|
|
Err = xmpp:err_bad_request(ErrText, Lang),
|
|
ejabberd_router:route_error(Packet, Err);
|
|
{true, true} ->
|
|
case find_jids_by_nick(ToNick, StateData) of
|
|
[] ->
|
|
ErrText = ?T("Recipient is not in the conference room"),
|
|
Err = xmpp:err_item_not_found(ErrText, Lang),
|
|
ejabberd_router:route_error(Packet, Err);
|
|
ToJIDs ->
|
|
SrcIsVisitor = is_visitor(From, StateData),
|
|
DstIsModerator = is_moderator(hd(ToJIDs), StateData),
|
|
PmFromVisitors =
|
|
(StateData#state.config)#config.allow_private_messages_from_visitors,
|
|
if SrcIsVisitor == false;
|
|
PmFromVisitors == anyone;
|
|
(PmFromVisitors == moderators) and
|
|
DstIsModerator ->
|
|
{FromNick, _} = get_participant_data(From, StateData),
|
|
FromNickJID =
|
|
jid:replace_resource(StateData#state.jid,
|
|
FromNick),
|
|
X = #muc_user{},
|
|
PrivMsg = xmpp:set_from(
|
|
xmpp:set_subtag(Packet, X),
|
|
FromNickJID),
|
|
lists:foreach(
|
|
fun(ToJID) ->
|
|
ejabberd_router:route(xmpp:set_to(PrivMsg, ToJID))
|
|
end, ToJIDs);
|
|
true ->
|
|
ErrText = ?T("It is not allowed to send private messages"),
|
|
Err = xmpp:err_forbidden(ErrText, Lang),
|
|
ejabberd_router:route_error(Packet, Err)
|
|
end
|
|
end;
|
|
{true, false} ->
|
|
ErrText = ?T("Only occupants are allowed to send messages "
|
|
"to the conference"),
|
|
Err = xmpp:err_not_acceptable(ErrText, Lang),
|
|
ejabberd_router:route_error(Packet, Err);
|
|
{false, _} ->
|
|
ErrText = ?T("It is not allowed to send private messages"),
|
|
Err = xmpp:err_forbidden(ErrText, Lang),
|
|
ejabberd_router:route_error(Packet, Err)
|
|
end,
|
|
{next_state, normal_state, StateData}
|
|
end;
|
|
normal_state({route, ToNick,
|
|
#iq{from = From, lang = Lang} = Packet},
|
|
#state{config = #config{allow_query_users = AllowQuery}} = StateData) ->
|
|
try maps:get(jid:tolower(From), StateData#state.users) of
|
|
#user{nick = FromNick} when AllowQuery orelse ToNick == FromNick ->
|
|
case find_jid_by_nick(ToNick, StateData) of
|
|
false ->
|
|
ErrText = ?T("Recipient is not in the conference room"),
|
|
Err = xmpp:err_item_not_found(ErrText, Lang),
|
|
ejabberd_router:route_error(Packet, Err);
|
|
To ->
|
|
FromJID = jid:replace_resource(StateData#state.jid, FromNick),
|
|
case direct_iq_type(Packet) of
|
|
vcard ->
|
|
ejabberd_router:route_iq(
|
|
xmpp:set_from_to(Packet, FromJID, jid:remove_resource(To)),
|
|
Packet, self());
|
|
ping when ToNick == FromNick ->
|
|
%% Self-ping optimization from XEP-0410
|
|
ejabberd_router:route(xmpp:make_iq_result(Packet));
|
|
response ->
|
|
ejabberd_router:route(xmpp:set_from_to(Packet, FromJID, To));
|
|
#stanza_error{} = Err ->
|
|
ejabberd_router:route_error(Packet, Err);
|
|
_OtherRequest ->
|
|
ejabberd_router:route_iq(
|
|
xmpp:set_from_to(Packet, FromJID, To), Packet, self())
|
|
end
|
|
end;
|
|
_ ->
|
|
ErrText = ?T("Queries to the conference members are "
|
|
"not allowed in this room"),
|
|
Err = xmpp:err_not_allowed(ErrText, Lang),
|
|
ejabberd_router:route_error(Packet, Err)
|
|
catch _:{badkey, _} ->
|
|
ErrText = ?T("Only occupants are allowed to send queries "
|
|
"to the conference"),
|
|
Err = xmpp:err_not_acceptable(ErrText, Lang),
|
|
ejabberd_router:route_error(Packet, Err)
|
|
end,
|
|
{next_state, normal_state, StateData};
|
|
normal_state(hibernate, StateData) ->
|
|
case maps:size(StateData#state.users) of
|
|
0 ->
|
|
store_room_no_checks(StateData, []),
|
|
?INFO_MSG("Hibernating room ~s@~s", [StateData#state.room, StateData#state.host]),
|
|
{stop, normal, StateData#state{hibernate_timer = hibernating}};
|
|
_ ->
|
|
{next_state, normal_state, StateData}
|
|
end;
|
|
normal_state(_Event, StateData) ->
|
|
{next_state, normal_state, StateData}.
|
|
|
|
handle_event({service_message, Msg}, _StateName,
|
|
StateData) ->
|
|
MessagePkt = #message{type = groupchat, body = xmpp:mk_text(Msg)},
|
|
send_wrapped_multiple(
|
|
StateData#state.jid,
|
|
get_users_and_subscribers(StateData),
|
|
MessagePkt,
|
|
?NS_MUCSUB_NODES_MESSAGES,
|
|
StateData),
|
|
NSD = add_message_to_history(<<"">>,
|
|
StateData#state.jid, MessagePkt, StateData),
|
|
{next_state, normal_state, NSD};
|
|
handle_event({destroy, Reason}, _StateName,
|
|
StateData) ->
|
|
_ = destroy_room(#muc_destroy{xmlns = ?NS_MUC_OWNER, reason = Reason}, StateData),
|
|
?INFO_MSG("Destroyed MUC room ~s with reason: ~p",
|
|
[jid:encode(StateData#state.jid), Reason]),
|
|
add_to_log(room_existence, destroyed, StateData),
|
|
Conf = StateData#state.config,
|
|
{stop, shutdown, StateData#state{config = Conf#config{persistent = false}}};
|
|
handle_event(destroy, StateName, StateData) ->
|
|
?INFO_MSG("Destroyed MUC room ~s",
|
|
[jid:encode(StateData#state.jid)]),
|
|
handle_event({destroy, <<"">>}, StateName, StateData);
|
|
handle_event({set_affiliations, Affiliations},
|
|
StateName, StateData) ->
|
|
NewStateData = set_affiliations(Affiliations, StateData),
|
|
{next_state, StateName, NewStateData};
|
|
handle_event(_Event, StateName, StateData) ->
|
|
{next_state, StateName, StateData}.
|
|
|
|
handle_sync_event({get_disco_item, Filter, JID, Lang, Time}, _From, StateName, StateData) ->
|
|
Len = maps:size(StateData#state.nicks),
|
|
Reply = case (Filter == all) or (Filter == Len) or ((Filter /= 0) and (Len /= 0)) of
|
|
true ->
|
|
get_roomdesc_reply(JID, StateData,
|
|
get_roomdesc_tail(StateData, Lang));
|
|
false ->
|
|
false
|
|
end,
|
|
CurrentTime = erlang:system_time(millisecond),
|
|
if CurrentTime < Time ->
|
|
{reply, Reply, StateName, StateData};
|
|
true ->
|
|
{next_state, StateName, StateData}
|
|
end;
|
|
%% These two clauses are only for backward compatibility with nodes running old code
|
|
handle_sync_event({get_disco_item, JID, Lang}, From, StateName, StateData) ->
|
|
handle_sync_event({get_disco_item, any, JID, Lang}, From, StateName, StateData);
|
|
handle_sync_event({get_disco_item, Filter, JID, Lang}, From, StateName, StateData) ->
|
|
handle_sync_event({get_disco_item, Filter, JID, Lang, infinity}, From, StateName, StateData);
|
|
handle_sync_event(get_config, _From, StateName,
|
|
StateData) ->
|
|
{reply, {ok, StateData#state.config}, StateName,
|
|
StateData};
|
|
handle_sync_event(get_state, _From, StateName,
|
|
StateData) ->
|
|
{reply, {ok, StateData}, StateName, StateData};
|
|
handle_sync_event({change_config, Config}, _From,
|
|
StateName, StateData) ->
|
|
{result, undefined, NSD} = change_config(Config, StateData),
|
|
{reply, {ok, NSD#state.config}, StateName, NSD};
|
|
handle_sync_event({change_state, NewStateData}, _From,
|
|
StateName, _StateData) ->
|
|
Mod = gen_mod:db_mod(NewStateData#state.server_host, mod_muc),
|
|
case erlang:function_exported(Mod, get_subscribed_rooms, 3) of
|
|
true ->
|
|
ok;
|
|
_ ->
|
|
erlang:put(muc_subscribers, NewStateData#state.subscribers)
|
|
end,
|
|
{reply, {ok, NewStateData}, StateName, NewStateData};
|
|
handle_sync_event({process_item_change, Item, UJID}, _From, StateName, StateData) ->
|
|
case process_item_change(Item, StateData, UJID) of
|
|
{error, _} = Err ->
|
|
{reply, Err, StateName, StateData};
|
|
NSD ->
|
|
{reply, {ok, NSD}, StateName, NSD}
|
|
end;
|
|
handle_sync_event(get_subscribers, _From, StateName, StateData) ->
|
|
JIDs = lists:map(fun jid:make/1,
|
|
maps:keys(StateData#state.subscribers)),
|
|
{reply, {ok, JIDs}, StateName, StateData};
|
|
handle_sync_event({muc_subscribe, From, Nick, Nodes}, _From,
|
|
StateName, StateData) ->
|
|
IQ = #iq{type = set, id = p1_rand:get_string(),
|
|
from = From, sub_els = [#muc_subscribe{nick = Nick,
|
|
events = Nodes}]},
|
|
Config = StateData#state.config,
|
|
CaptchaRequired = Config#config.captcha_protected,
|
|
PasswordProtected = Config#config.password_protected,
|
|
TmpConfig = Config#config{captcha_protected = false,
|
|
password_protected = false},
|
|
TmpState = StateData#state{config = TmpConfig},
|
|
case process_iq_mucsub(From, IQ, TmpState) of
|
|
{result, #muc_subscribe{events = NewNodes}, NewState} ->
|
|
NewConfig = (NewState#state.config)#config{
|
|
captcha_protected = CaptchaRequired,
|
|
password_protected = PasswordProtected},
|
|
{reply, {ok, NewNodes}, StateName,
|
|
NewState#state{config = NewConfig}};
|
|
{ignore, NewState} ->
|
|
NewConfig = (NewState#state.config)#config{
|
|
captcha_protected = CaptchaRequired,
|
|
password_protected = PasswordProtected},
|
|
{reply, {error, ?T("Request is ignored")},
|
|
NewState#state{config = NewConfig}};
|
|
{error, Err} ->
|
|
{reply, {error, get_error_text(Err)}, StateName, StateData}
|
|
end;
|
|
handle_sync_event({muc_unsubscribe, From}, _From, StateName, StateData) ->
|
|
IQ = #iq{type = set, id = p1_rand:get_string(),
|
|
from = From, sub_els = [#muc_unsubscribe{}]},
|
|
case process_iq_mucsub(From, IQ, StateData) of
|
|
{result, _, NewState} ->
|
|
{reply, ok, StateName, NewState};
|
|
{ignore, NewState} ->
|
|
{reply, {error, ?T("Request is ignored")}, NewState};
|
|
{error, Err} ->
|
|
{reply, {error, get_error_text(Err)}, StateName, StateData}
|
|
end;
|
|
handle_sync_event({is_subscribed, From}, _From, StateName, StateData) ->
|
|
IsSubs = try maps:get(jid:split(From), StateData#state.subscribers) of
|
|
#subscriber{nodes = Nodes} -> {true, Nodes}
|
|
catch _:{badkey, _} -> false
|
|
end,
|
|
{reply, IsSubs, StateName, StateData};
|
|
handle_sync_event(_Event, _From, StateName,
|
|
StateData) ->
|
|
Reply = ok, {reply, Reply, StateName, StateData}.
|
|
|
|
code_change(_OldVsn, StateName, StateData, _Extra) ->
|
|
{ok, StateName, StateData}.
|
|
|
|
handle_info({process_user_presence, From}, normal_state = _StateName, StateData) ->
|
|
RoomQueueEmpty = p1_queue:is_empty(StateData#state.room_queue),
|
|
RoomQueue = p1_queue:in({presence, From}, StateData#state.room_queue),
|
|
StateData1 = StateData#state{room_queue = RoomQueue},
|
|
if RoomQueueEmpty ->
|
|
StateData2 = prepare_room_queue(StateData1),
|
|
{next_state, normal_state, StateData2};
|
|
true -> {next_state, normal_state, StateData1}
|
|
end;
|
|
handle_info({process_user_message, From},
|
|
normal_state = _StateName, StateData) ->
|
|
RoomQueueEmpty =
|
|
p1_queue:is_empty(StateData#state.room_queue),
|
|
RoomQueue = p1_queue:in({message, From},
|
|
StateData#state.room_queue),
|
|
StateData1 = StateData#state{room_queue = RoomQueue},
|
|
if RoomQueueEmpty ->
|
|
StateData2 = prepare_room_queue(StateData1),
|
|
{next_state, normal_state, StateData2};
|
|
true -> {next_state, normal_state, StateData1}
|
|
end;
|
|
handle_info(process_room_queue,
|
|
normal_state = StateName, StateData) ->
|
|
case p1_queue:out(StateData#state.room_queue) of
|
|
{{value, {message, From}}, RoomQueue} ->
|
|
Activity = get_user_activity(From, StateData),
|
|
Packet = Activity#activity.message,
|
|
NewActivity = Activity#activity{message = undefined},
|
|
StateData1 = store_user_activity(From, NewActivity,
|
|
StateData),
|
|
StateData2 = StateData1#state{room_queue = RoomQueue},
|
|
StateData3 = prepare_room_queue(StateData2),
|
|
process_groupchat_message(Packet, StateData3);
|
|
{{value, {presence, From}}, RoomQueue} ->
|
|
Activity = get_user_activity(From, StateData),
|
|
{Nick, Packet} = Activity#activity.presence,
|
|
NewActivity = Activity#activity{presence = undefined},
|
|
StateData1 = store_user_activity(From, NewActivity,
|
|
StateData),
|
|
StateData2 = StateData1#state{room_queue = RoomQueue},
|
|
StateData3 = prepare_room_queue(StateData2),
|
|
process_presence(Nick, Packet, StateData3);
|
|
{empty, _} -> {next_state, StateName, StateData}
|
|
end;
|
|
handle_info({captcha_succeed, From}, normal_state,
|
|
StateData) ->
|
|
NewState = case maps:get(From, StateData#state.robots, passed) of
|
|
{Nick, Packet} ->
|
|
Robots = maps:put(From, passed, StateData#state.robots),
|
|
add_new_user(From, Nick, Packet,
|
|
StateData#state{robots = Robots});
|
|
passed ->
|
|
StateData
|
|
end,
|
|
{next_state, normal_state, NewState};
|
|
handle_info({captcha_failed, From}, normal_state,
|
|
StateData) ->
|
|
NewState = case maps:get(From, StateData#state.robots, passed) of
|
|
{_Nick, Packet} ->
|
|
Robots = maps:remove(From, StateData#state.robots),
|
|
Txt = ?T("The CAPTCHA verification has failed"),
|
|
Lang = xmpp:get_lang(Packet),
|
|
Err = xmpp:err_not_authorized(Txt, Lang),
|
|
ejabberd_router:route_error(Packet, Err),
|
|
StateData#state{robots = Robots};
|
|
passed ->
|
|
StateData
|
|
end,
|
|
{next_state, normal_state, NewState};
|
|
handle_info(shutdown, _StateName, StateData) ->
|
|
{stop, shutdown, StateData};
|
|
handle_info({iq_reply, #iq{type = Type, sub_els = Els},
|
|
#iq{from = From, to = To} = IQ}, StateName, StateData) ->
|
|
ejabberd_router:route(
|
|
xmpp:set_from_to(
|
|
IQ#iq{type = Type, sub_els = Els},
|
|
To, From)),
|
|
{next_state, StateName, StateData};
|
|
handle_info({iq_reply, timeout, IQ}, StateName, StateData) ->
|
|
Txt = ?T("Request has timed out"),
|
|
Err = xmpp:err_recipient_unavailable(Txt, IQ#iq.lang),
|
|
ejabberd_router:route_error(IQ, Err),
|
|
{next_state, StateName, StateData};
|
|
handle_info(config_reloaded, StateName, StateData) ->
|
|
Max = mod_muc_opt:history_size(StateData#state.server_host),
|
|
History1 = StateData#state.history,
|
|
Q1 = History1#lqueue.queue,
|
|
Q2 = case p1_queue:len(Q1) of
|
|
Len when Len > Max ->
|
|
lqueue_cut(Q1, Len-Max);
|
|
_ ->
|
|
Q1
|
|
end,
|
|
History2 = History1#lqueue{queue = Q2, max = Max},
|
|
{next_state, StateName, StateData#state{history = History2}};
|
|
handle_info(_Info, StateName, StateData) ->
|
|
{next_state, StateName, StateData}.
|
|
|
|
terminate(Reason, _StateName,
|
|
#state{server_host = LServer, host = Host, room = Room} = StateData) ->
|
|
try
|
|
?INFO_MSG("Stopping MUC room ~s@~s", [Room, Host]),
|
|
ReasonT = case Reason of
|
|
shutdown ->
|
|
?T("You are being removed from the room "
|
|
"because of a system shutdown");
|
|
_ -> ?T("Room terminates")
|
|
end,
|
|
Packet = #presence{
|
|
type = unavailable,
|
|
sub_els = [#muc_user{items = [#muc_item{affiliation = none,
|
|
reason = ReasonT,
|
|
role = none}],
|
|
status_codes = [332,110]}]},
|
|
maps:fold(
|
|
fun(_, #user{nick = Nick, jid = JID}, _) ->
|
|
case Reason of
|
|
shutdown ->
|
|
send_wrapped(jid:replace_resource(StateData#state.jid, Nick),
|
|
JID, Packet,
|
|
?NS_MUCSUB_NODES_PARTICIPANTS,
|
|
StateData);
|
|
_ -> ok
|
|
end,
|
|
tab_remove_online_user(JID, StateData)
|
|
end, [], get_users_and_subscribers(StateData)),
|
|
|
|
disable_hibernate_timer(StateData),
|
|
case StateData#state.hibernate_timer of
|
|
hibernating ->
|
|
ok;
|
|
_ ->
|
|
add_to_log(room_existence, stopped, StateData),
|
|
case (StateData#state.config)#config.persistent of
|
|
false ->
|
|
ejabberd_hooks:run(room_destroyed, LServer, [LServer, Room, Host]);
|
|
_ ->
|
|
ok
|
|
end
|
|
end,
|
|
mod_muc:room_destroyed(Host, Room, self(), LServer)
|
|
catch ?EX_RULE(E, R, St) ->
|
|
StackTrace = ?EX_STACK(St),
|
|
mod_muc:room_destroyed(Host, Room, self(), LServer),
|
|
?ERROR_MSG("Got exception on room termination:~n** ~s",
|
|
[misc:format_exception(2, E, R, StackTrace)])
|
|
end.
|
|
|
|
%%%----------------------------------------------------------------------
|
|
%%% Internal functions
|
|
%%%----------------------------------------------------------------------
|
|
-spec route(pid(), stanza()) -> ok.
|
|
route(Pid, Packet) ->
|
|
?DEBUG("Routing to MUC room ~p:~n~s", [Pid, xmpp:pp(Packet)]),
|
|
#jid{lresource = Nick} = xmpp:get_to(Packet),
|
|
p1_fsm:send_event(Pid, {route, Nick, Packet}).
|
|
|
|
-spec process_groupchat_message(message(), state()) -> fsm_next().
|
|
process_groupchat_message(#message{from = From, lang = Lang} = Packet, StateData) ->
|
|
IsSubscriber = is_subscriber(From, StateData),
|
|
case is_user_online(From, StateData) orelse IsSubscriber orelse
|
|
is_user_allowed_message_nonparticipant(From, StateData)
|
|
of
|
|
true ->
|
|
{FromNick, Role} = get_participant_data(From, StateData),
|
|
if (Role == moderator) or (Role == participant) or IsSubscriber or
|
|
((StateData#state.config)#config.moderated == false) ->
|
|
Subject = check_subject(Packet),
|
|
{NewStateData1, IsAllowed} = case Subject of
|
|
[] -> {StateData, true};
|
|
_ ->
|
|
case
|
|
can_change_subject(Role,
|
|
IsSubscriber,
|
|
StateData)
|
|
of
|
|
true ->
|
|
NSD =
|
|
StateData#state{subject
|
|
=
|
|
Subject,
|
|
subject_author
|
|
=
|
|
FromNick},
|
|
store_room(NSD),
|
|
{NSD, true};
|
|
_ -> {StateData, false}
|
|
end
|
|
end,
|
|
case IsAllowed of
|
|
true ->
|
|
case
|
|
ejabberd_hooks:run_fold(muc_filter_message,
|
|
StateData#state.server_host,
|
|
Packet,
|
|
[StateData, FromNick])
|
|
of
|
|
drop ->
|
|
{next_state, normal_state, StateData};
|
|
NewPacket1 ->
|
|
NewPacket = xmpp:put_meta(xmpp:remove_subtag(NewPacket1, #nick{}),
|
|
muc_sender_real_jid, From),
|
|
Node = if Subject == [] -> ?NS_MUCSUB_NODES_MESSAGES;
|
|
true -> ?NS_MUCSUB_NODES_SUBJECT
|
|
end,
|
|
send_wrapped_multiple(
|
|
jid:replace_resource(StateData#state.jid, FromNick),
|
|
get_users_and_subscribers(StateData),
|
|
NewPacket, Node, NewStateData1),
|
|
NewStateData2 = case has_body_or_subject(NewPacket) of
|
|
true ->
|
|
add_message_to_history(FromNick, From,
|
|
NewPacket,
|
|
NewStateData1);
|
|
false ->
|
|
NewStateData1
|
|
end,
|
|
{next_state, normal_state, NewStateData2}
|
|
end;
|
|
_ ->
|
|
Err = case (StateData#state.config)#config.allow_change_subj of
|
|
true ->
|
|
xmpp:err_forbidden(
|
|
?T("Only moderators and participants are "
|
|
"allowed to change the subject in this "
|
|
"room"), Lang);
|
|
_ ->
|
|
xmpp:err_forbidden(
|
|
?T("Only moderators are allowed to change "
|
|
"the subject in this room"), Lang)
|
|
end,
|
|
ejabberd_router:route_error(Packet, Err),
|
|
{next_state, normal_state, StateData}
|
|
end;
|
|
true ->
|
|
ErrText = ?T("Visitors are not allowed to send messages "
|
|
"to all occupants"),
|
|
Err = xmpp:err_forbidden(ErrText, Lang),
|
|
ejabberd_router:route_error(Packet, Err),
|
|
{next_state, normal_state, StateData}
|
|
end;
|
|
false ->
|
|
ErrText = ?T("Only occupants are allowed to send messages "
|
|
"to the conference"),
|
|
Err = xmpp:err_not_acceptable(ErrText, Lang),
|
|
ejabberd_router:route_error(Packet, Err),
|
|
{next_state, normal_state, StateData}
|
|
end.
|
|
|
|
-spec process_normal_message(jid(), message(), state()) -> state().
|
|
process_normal_message(From, #message{lang = Lang} = Pkt, StateData) ->
|
|
Action = lists:foldl(
|
|
fun(_, {error, _} = Err) ->
|
|
Err;
|
|
(_, {ok, _} = Result) ->
|
|
Result;
|
|
(#muc_user{invites = [_|_] = Invites}, _) ->
|
|
case check_invitation(From, Invites, Lang, StateData) of
|
|
ok ->
|
|
{ok, Invites};
|
|
{error, _} = Err ->
|
|
Err
|
|
end;
|
|
(#xdata{type = submit, fields = Fs}, _) ->
|
|
try {ok, muc_request:decode(Fs)}
|
|
catch _:{muc_request, Why} ->
|
|
Txt = muc_request:format_error(Why),
|
|
{error, xmpp:err_bad_request(Txt, Lang)}
|
|
end;
|
|
(_, Acc) ->
|
|
Acc
|
|
end, ok, xmpp:get_els(Pkt)),
|
|
case Action of
|
|
{ok, [#muc_invite{}|_] = Invitations} ->
|
|
lists:foldl(
|
|
fun(Invitation, AccState) ->
|
|
process_invitation(From, Pkt, Invitation, Lang, AccState)
|
|
end, StateData, Invitations);
|
|
{ok, [{role, participant}]} ->
|
|
process_voice_request(From, Pkt, StateData);
|
|
{ok, VoiceApproval} ->
|
|
process_voice_approval(From, Pkt, VoiceApproval, StateData);
|
|
{error, Err} ->
|
|
ejabberd_router:route_error(Pkt, Err),
|
|
StateData;
|
|
ok ->
|
|
StateData
|
|
end.
|
|
|
|
-spec process_invitation(jid(), message(), muc_invite(), binary(), state()) -> state().
|
|
process_invitation(From, Pkt, Invitation, Lang, StateData) ->
|
|
IJID = route_invitation(From, Pkt, Invitation, Lang, StateData),
|
|
Config = StateData#state.config,
|
|
case Config#config.members_only of
|
|
true ->
|
|
case get_affiliation(IJID, StateData) of
|
|
none ->
|
|
NSD = set_affiliation(IJID, member, StateData),
|
|
send_affiliation(IJID, member, StateData),
|
|
store_room(NSD),
|
|
NSD;
|
|
_ ->
|
|
StateData
|
|
end;
|
|
false ->
|
|
StateData
|
|
end.
|
|
|
|
-spec process_voice_request(jid(), message(), state()) -> state().
|
|
process_voice_request(From, Pkt, StateData) ->
|
|
Lang = xmpp:get_lang(Pkt),
|
|
case (StateData#state.config)#config.allow_voice_requests of
|
|
true ->
|
|
MinInterval = (StateData#state.config)#config.voice_request_min_interval,
|
|
BareFrom = jid:remove_resource(jid:tolower(From)),
|
|
NowPriority = -erlang:system_time(microsecond),
|
|
CleanPriority = NowPriority + MinInterval * 1000000,
|
|
Times = clean_treap(StateData#state.last_voice_request_time,
|
|
CleanPriority),
|
|
case treap:lookup(BareFrom, Times) of
|
|
error ->
|
|
Times1 = treap:insert(BareFrom,
|
|
NowPriority,
|
|
true, Times),
|
|
NSD = StateData#state{last_voice_request_time = Times1},
|
|
send_voice_request(From, Lang, NSD),
|
|
NSD;
|
|
{ok, _, _} ->
|
|
ErrText = ?T("Please, wait for a while before sending "
|
|
"new voice request"),
|
|
Err = xmpp:err_resource_constraint(ErrText, Lang),
|
|
ejabberd_router:route_error(Pkt, Err),
|
|
StateData#state{last_voice_request_time = Times}
|
|
end;
|
|
false ->
|
|
ErrText = ?T("Voice requests are disabled in this conference"),
|
|
Err = xmpp:err_forbidden(ErrText, Lang),
|
|
ejabberd_router:route_error(Pkt, Err),
|
|
StateData
|
|
end.
|
|
|
|
-spec process_voice_approval(jid(), message(), [muc_request:property()], state()) -> state().
|
|
process_voice_approval(From, Pkt, VoiceApproval, StateData) ->
|
|
Lang = xmpp:get_lang(Pkt),
|
|
case is_moderator(From, StateData) of
|
|
true ->
|
|
case lists:keyfind(jid, 1, VoiceApproval) of
|
|
{_, TargetJid} ->
|
|
Allow = proplists:get_bool(request_allow, VoiceApproval),
|
|
case is_visitor(TargetJid, StateData) of
|
|
true when Allow ->
|
|
Reason = <<>>,
|
|
NSD = set_role(TargetJid, participant, StateData),
|
|
catch send_new_presence(
|
|
TargetJid, Reason, NSD, StateData),
|
|
NSD;
|
|
_ ->
|
|
StateData
|
|
end;
|
|
false ->
|
|
ErrText = ?T("Failed to extract JID from your voice "
|
|
"request approval"),
|
|
Err = xmpp:err_bad_request(ErrText, Lang),
|
|
ejabberd_router:route_error(Pkt, Err),
|
|
StateData
|
|
end;
|
|
false ->
|
|
ErrText = ?T("Only moderators can approve voice requests"),
|
|
Err = xmpp:err_not_allowed(ErrText, Lang),
|
|
ejabberd_router:route_error(Pkt, Err),
|
|
StateData
|
|
end.
|
|
|
|
-spec direct_iq_type(iq()) -> vcard | ping | request | response | stanza_error().
|
|
direct_iq_type(#iq{type = T, sub_els = SubEls, lang = Lang}) when T == get; T == set ->
|
|
case SubEls of
|
|
[El] ->
|
|
case xmpp:get_ns(El) of
|
|
?NS_VCARD when T == get -> vcard;
|
|
?NS_PING when T == get -> ping;
|
|
_ -> request
|
|
end;
|
|
[] ->
|
|
xmpp:err_bad_request(?T("No child elements found"), Lang);
|
|
[_|_] ->
|
|
xmpp:err_bad_request(?T("Too many child elements"), Lang)
|
|
end;
|
|
direct_iq_type(#iq{}) ->
|
|
response.
|
|
|
|
%% @doc Check if this non participant can send message to room.
|
|
%%
|
|
%% XEP-0045 v1.23:
|
|
%% 7.9 Sending a Message to All Occupants
|
|
%% an implementation MAY allow users with certain privileges
|
|
%% (e.g., a room owner, room admin, or service-level admin)
|
|
%% to send messages to the room even if those users are not occupants.
|
|
-spec is_user_allowed_message_nonparticipant(jid(), state()) -> boolean().
|
|
is_user_allowed_message_nonparticipant(JID,
|
|
StateData) ->
|
|
case get_service_affiliation(JID, StateData) of
|
|
owner -> true;
|
|
_ -> false
|
|
end.
|
|
|
|
%% @doc Get information of this participant, or default values.
|
|
%% If the JID is not a participant, return values for a service message.
|
|
-spec get_participant_data(jid(), state()) -> {binary(), role()}.
|
|
get_participant_data(From, StateData) ->
|
|
try maps:get(jid:tolower(From), StateData#state.users) of
|
|
#user{nick = FromNick, role = Role} ->
|
|
{FromNick, Role}
|
|
catch _:{badkey, _} ->
|
|
try maps:get(jid:tolower(jid:remove_resource(From)),
|
|
StateData#state.subscribers) of
|
|
#subscriber{nick = FromNick} ->
|
|
{FromNick, none}
|
|
catch _:{badkey, _} ->
|
|
{<<"">>, moderator}
|
|
end
|
|
end.
|
|
|
|
-spec process_presence(binary(), presence(), state()) -> fsm_transition().
|
|
process_presence(Nick, #presence{from = From, type = Type0} = Packet0, StateData) ->
|
|
IsOnline = is_user_online(From, StateData),
|
|
if Type0 == available;
|
|
IsOnline and ((Type0 == unavailable) or (Type0 == error)) ->
|
|
case ejabberd_hooks:run_fold(muc_filter_presence,
|
|
StateData#state.server_host,
|
|
Packet0,
|
|
[StateData, Nick]) of
|
|
drop ->
|
|
{next_state, normal_state, StateData};
|
|
#presence{} = Packet ->
|
|
close_room_if_temporary_and_empty(
|
|
do_process_presence(Nick, Packet, StateData))
|
|
end;
|
|
true ->
|
|
{next_state, normal_state, StateData}
|
|
end.
|
|
|
|
-spec do_process_presence(binary(), presence(), state()) -> state().
|
|
do_process_presence(Nick, #presence{from = From, type = available, lang = Lang} = Packet,
|
|
StateData) ->
|
|
case is_user_online(From, StateData) of
|
|
false ->
|
|
add_new_user(From, Nick, Packet, StateData);
|
|
true ->
|
|
case is_nick_change(From, Nick, StateData) of
|
|
true ->
|
|
case {nick_collision(From, Nick, StateData),
|
|
mod_muc:can_use_nick(StateData#state.server_host,
|
|
StateData#state.host,
|
|
From, Nick),
|
|
{(StateData#state.config)#config.allow_visitor_nickchange,
|
|
is_visitor(From, StateData)}} of
|
|
{_, _, {false, true}} ->
|
|
Packet1 = Packet#presence{sub_els = [#muc{}]},
|
|
ErrText = ?T("Visitors are not allowed to change their "
|
|
"nicknames in this room"),
|
|
Err = xmpp:err_not_allowed(ErrText, Lang),
|
|
ejabberd_router:route_error(Packet1, Err),
|
|
StateData;
|
|
{true, _, _} ->
|
|
Packet1 = Packet#presence{sub_els = [#muc{}]},
|
|
ErrText = ?T("That nickname is already in use by another "
|
|
"occupant"),
|
|
Err = xmpp:err_conflict(ErrText, Lang),
|
|
ejabberd_router:route_error(Packet1, Err),
|
|
StateData;
|
|
{_, false, _} ->
|
|
Packet1 = Packet#presence{sub_els = [#muc{}]},
|
|
Err = case Nick of
|
|
<<>> ->
|
|
xmpp:err_jid_malformed(?T("Nickname can't be empty"),
|
|
Lang);
|
|
_ ->
|
|
xmpp:err_conflict(?T("That nickname is registered"
|
|
" by another person"), Lang)
|
|
end,
|
|
ejabberd_router:route_error(Packet1, Err),
|
|
StateData;
|
|
_ ->
|
|
change_nick(From, Nick, StateData)
|
|
end;
|
|
false ->
|
|
Stanza = maybe_strip_status_from_presence(
|
|
From, Packet, StateData),
|
|
NewState = add_user_presence(From, Stanza,
|
|
StateData),
|
|
case xmpp:has_subtag(Packet, #muc{}) of
|
|
true ->
|
|
send_initial_presences_and_messages(
|
|
From, Nick, Packet, NewState, StateData);
|
|
false ->
|
|
send_new_presence(From, NewState, StateData)
|
|
end,
|
|
NewState
|
|
end
|
|
end;
|
|
do_process_presence(Nick, #presence{from = From, type = unavailable} = Packet,
|
|
StateData) ->
|
|
NewPacket = case {(StateData#state.config)#config.allow_visitor_status,
|
|
is_visitor(From, StateData)} of
|
|
{false, true} ->
|
|
strip_status(Packet);
|
|
_ -> Packet
|
|
end,
|
|
NewState = add_user_presence_un(From, NewPacket, StateData),
|
|
case maps:get(Nick, StateData#state.nicks, []) of
|
|
[_, _ | _] ->
|
|
Aff = get_affiliation(From, StateData),
|
|
Item = #muc_item{affiliation = Aff, role = none, jid = From},
|
|
Pres = xmpp:set_subtag(
|
|
Packet, #muc_user{items = [Item],
|
|
status_codes = [110]}),
|
|
send_wrapped(jid:replace_resource(StateData#state.jid, Nick),
|
|
From, Pres, ?NS_MUCSUB_NODES_PRESENCE, StateData);
|
|
_ ->
|
|
send_new_presence(From, NewState, StateData)
|
|
end,
|
|
Reason = xmpp:get_text(NewPacket#presence.status),
|
|
remove_online_user(From, NewState, Reason);
|
|
do_process_presence(_Nick, #presence{from = From, type = error, lang = Lang} = Packet,
|
|
StateData) ->
|
|
ErrorText = ?T("It is not allowed to send error messages to the"
|
|
" room. The participant (~s) has sent an error "
|
|
"message (~s) and got kicked from the room"),
|
|
expulse_participant(Packet, From, StateData,
|
|
translate:translate(Lang, ErrorText)).
|
|
|
|
-spec maybe_strip_status_from_presence(jid(), presence(),
|
|
state()) -> presence().
|
|
maybe_strip_status_from_presence(From, Packet, StateData) ->
|
|
case {(StateData#state.config)#config.allow_visitor_status,
|
|
is_visitor(From, StateData)} of
|
|
{false, true} ->
|
|
strip_status(Packet);
|
|
_Allowed -> Packet
|
|
end.
|
|
|
|
-spec close_room_if_temporary_and_empty(state()) -> fsm_transition().
|
|
close_room_if_temporary_and_empty(StateData1) ->
|
|
case not (StateData1#state.config)#config.persistent
|
|
andalso maps:size(StateData1#state.users) == 0
|
|
andalso maps:size(StateData1#state.subscribers) == 0 of
|
|
true ->
|
|
?INFO_MSG("Destroyed MUC room ~s because it's temporary "
|
|
"and empty",
|
|
[jid:encode(StateData1#state.jid)]),
|
|
add_to_log(room_existence, destroyed, StateData1),
|
|
forget_room(StateData1),
|
|
{stop, normal, StateData1};
|
|
_ -> {next_state, normal_state, StateData1}
|
|
end.
|
|
|
|
-spec get_users_and_subscribers(state()) -> users().
|
|
get_users_and_subscribers(StateData) ->
|
|
OnlineSubscribers = maps:fold(
|
|
fun(LJID, _, Acc) ->
|
|
LBareJID = jid:remove_resource(LJID),
|
|
case is_subscriber(LBareJID, StateData) of
|
|
true ->
|
|
?SETS:add_element(LBareJID, Acc);
|
|
false ->
|
|
Acc
|
|
end
|
|
end, ?SETS:new(), StateData#state.users),
|
|
maps:fold(
|
|
fun(LBareJID, #subscriber{nick = Nick}, Acc) ->
|
|
case ?SETS:is_element(LBareJID, OnlineSubscribers) of
|
|
false ->
|
|
maps:put(LBareJID,
|
|
#user{jid = jid:make(LBareJID),
|
|
nick = Nick,
|
|
role = none,
|
|
last_presence = undefined},
|
|
Acc);
|
|
true ->
|
|
Acc
|
|
end
|
|
end, StateData#state.users, StateData#state.subscribers).
|
|
|
|
-spec is_user_online(jid(), state()) -> boolean().
|
|
is_user_online(JID, StateData) ->
|
|
LJID = jid:tolower(JID),
|
|
maps:is_key(LJID, StateData#state.users).
|
|
|
|
-spec is_subscriber(jid(), state()) -> boolean().
|
|
is_subscriber(JID, StateData) ->
|
|
LJID = jid:tolower(jid:remove_resource(JID)),
|
|
maps:is_key(LJID, StateData#state.subscribers).
|
|
|
|
%% Check if the user is occupant of the room, or at least is an admin or owner.
|
|
-spec is_occupant_or_admin(jid(), state()) -> boolean().
|
|
is_occupant_or_admin(JID, StateData) ->
|
|
FAffiliation = get_affiliation(JID, StateData),
|
|
FRole = get_role(JID, StateData),
|
|
case FRole /= none orelse
|
|
FAffiliation == member orelse
|
|
FAffiliation == admin orelse FAffiliation == owner
|
|
of
|
|
true -> true;
|
|
_ -> false
|
|
end.
|
|
|
|
%% Decide the fate of the message and its sender
|
|
%% Returns: continue_delivery | forget_message | {expulse_sender, Reason}
|
|
-spec decide_fate_message(message(), jid(), state()) ->
|
|
continue_delivery | forget_message |
|
|
{expulse_sender, binary()}.
|
|
decide_fate_message(#message{type = error} = Msg,
|
|
From, StateData) ->
|
|
Err = xmpp:get_error(Msg),
|
|
PD = case check_error_kick(Err) of
|
|
%% If this is an error stanza and its condition matches a criteria
|
|
true ->
|
|
Reason = str:format("This participant is considered a ghost "
|
|
"and is expulsed: ~s",
|
|
[jid:encode(From)]),
|
|
{expulse_sender, Reason};
|
|
false -> continue_delivery
|
|
end,
|
|
case PD of
|
|
{expulse_sender, R} ->
|
|
case is_user_online(From, StateData) of
|
|
true -> {expulse_sender, R};
|
|
false -> forget_message
|
|
end;
|
|
Other -> Other
|
|
end;
|
|
decide_fate_message(_, _, _) -> continue_delivery.
|
|
|
|
%% Check if the elements of this error stanza indicate
|
|
%% that the sender is a dead participant.
|
|
%% If so, return true to kick the participant.
|
|
-spec check_error_kick(stanza_error()) -> boolean().
|
|
check_error_kick(#stanza_error{reason = Reason}) ->
|
|
case Reason of
|
|
#gone{} -> true;
|
|
'internal-server-error' -> true;
|
|
'item-not-found' -> true;
|
|
'jid-malformed' -> true;
|
|
'recipient-unavailable' -> true;
|
|
#redirect{} -> true;
|
|
'remote-server-not-found' -> true;
|
|
'remote-server-timeout' -> true;
|
|
'service-unavailable' -> true;
|
|
_ -> false
|
|
end;
|
|
check_error_kick(undefined) ->
|
|
false.
|
|
|
|
-spec get_error_condition(stanza_error()) -> string().
|
|
get_error_condition(#stanza_error{reason = Reason}) ->
|
|
case Reason of
|
|
#gone{} -> "gone";
|
|
#redirect{} -> "redirect";
|
|
Atom -> atom_to_list(Atom)
|
|
end;
|
|
get_error_condition(undefined) ->
|
|
"undefined".
|
|
|
|
-spec get_error_text(stanza_error()) -> binary().
|
|
get_error_text(#stanza_error{text = Txt}) ->
|
|
xmpp:get_text(Txt).
|
|
|
|
-spec make_reason(stanza(), jid(), state(), binary()) -> binary().
|
|
make_reason(Packet, From, StateData, Reason1) ->
|
|
#user{nick = FromNick} = maps:get(jid:tolower(From), StateData#state.users),
|
|
Condition = get_error_condition(xmpp:get_error(Packet)),
|
|
str:format(Reason1, [FromNick, Condition]).
|
|
|
|
-spec expulse_participant(stanza(), jid(), state(), binary()) ->
|
|
state().
|
|
expulse_participant(Packet, From, StateData, Reason1) ->
|
|
Reason2 = make_reason(Packet, From, StateData, Reason1),
|
|
NewState = add_user_presence_un(From,
|
|
#presence{type = unavailable,
|
|
status = xmpp:mk_text(Reason2)},
|
|
StateData),
|
|
LJID = jid:tolower(From),
|
|
#user{nick = Nick} = maps:get(LJID, StateData#state.users),
|
|
case maps:get(Nick, StateData#state.nicks, []) of
|
|
[_, _ | _] ->
|
|
Aff = get_affiliation(From, StateData),
|
|
Item = #muc_item{affiliation = Aff, role = none, jid = From},
|
|
Pres = xmpp:set_subtag(
|
|
Packet, #muc_user{items = [Item],
|
|
status_codes = [110]}),
|
|
send_wrapped(jid:replace_resource(StateData#state.jid, Nick),
|
|
From, Pres, ?NS_MUCSUB_NODES_PRESENCE, StateData);
|
|
_ ->
|
|
send_new_presence(From, NewState, StateData)
|
|
end,
|
|
remove_online_user(From, NewState).
|
|
|
|
-spec get_owners(state()) -> [jid:jid()].
|
|
get_owners(StateData) ->
|
|
maps:fold(
|
|
fun(LJID, owner, Acc) ->
|
|
[jid:make(LJID)|Acc];
|
|
(LJID, {owner, _}, Acc) ->
|
|
[jid:make(LJID)|Acc];
|
|
(_, _, Acc) ->
|
|
Acc
|
|
end, [], StateData#state.affiliations).
|
|
|
|
-spec set_affiliation(jid(), affiliation(), state()) -> state().
|
|
set_affiliation(JID, Affiliation, StateData) ->
|
|
set_affiliation(JID, Affiliation, StateData, <<"">>).
|
|
|
|
-spec set_affiliation(jid(), affiliation(), state(), binary()) -> state().
|
|
set_affiliation(JID, Affiliation,
|
|
#state{config = #config{persistent = false}} = StateData,
|
|
Reason) ->
|
|
set_affiliation_fallback(JID, Affiliation, StateData, Reason);
|
|
set_affiliation(JID, Affiliation, StateData, Reason) ->
|
|
ServerHost = StateData#state.server_host,
|
|
Room = StateData#state.room,
|
|
Host = StateData#state.host,
|
|
Mod = gen_mod:db_mod(ServerHost, mod_muc),
|
|
case Mod:set_affiliation(ServerHost, Room, Host, JID, Affiliation, Reason) of
|
|
ok ->
|
|
StateData;
|
|
{error, _} ->
|
|
set_affiliation_fallback(JID, Affiliation, StateData, Reason)
|
|
end.
|
|
|
|
-spec set_affiliation_fallback(jid(), affiliation(), state(), binary()) -> state().
|
|
set_affiliation_fallback(JID, Affiliation, StateData, Reason) ->
|
|
LJID = jid:remove_resource(jid:tolower(JID)),
|
|
Affiliations = case Affiliation of
|
|
none ->
|
|
maps:remove(LJID, StateData#state.affiliations);
|
|
_ ->
|
|
maps:put(LJID, {Affiliation, Reason},
|
|
StateData#state.affiliations)
|
|
end,
|
|
StateData#state{affiliations = Affiliations}.
|
|
|
|
-spec set_affiliations(affiliations(), state()) -> state().
|
|
set_affiliations(Affiliations,
|
|
#state{config = #config{persistent = false}} = StateData) ->
|
|
set_affiliations_fallback(Affiliations, StateData);
|
|
set_affiliations(Affiliations, StateData) ->
|
|
Room = StateData#state.room,
|
|
Host = StateData#state.host,
|
|
ServerHost = StateData#state.server_host,
|
|
Mod = gen_mod:db_mod(ServerHost, mod_muc),
|
|
case Mod:set_affiliations(ServerHost, Room, Host, Affiliations) of
|
|
ok ->
|
|
StateData;
|
|
{error, _} ->
|
|
set_affiliations_fallback(Affiliations, StateData)
|
|
end.
|
|
|
|
-spec set_affiliations_fallback(affiliations(), state()) -> state().
|
|
set_affiliations_fallback(Affiliations, StateData) ->
|
|
StateData#state{affiliations = Affiliations}.
|
|
|
|
-spec get_affiliation(ljid() | jid(), state()) -> affiliation().
|
|
get_affiliation(#jid{} = JID, StateData) ->
|
|
case get_service_affiliation(JID, StateData) of
|
|
owner ->
|
|
owner;
|
|
none ->
|
|
case do_get_affiliation(JID, StateData) of
|
|
{Affiliation, _Reason} -> Affiliation;
|
|
Affiliation -> Affiliation
|
|
end
|
|
end;
|
|
get_affiliation(LJID, StateData) ->
|
|
get_affiliation(jid:make(LJID), StateData).
|
|
|
|
-spec do_get_affiliation(jid(), state()) -> affiliation() | {affiliation(), binary()}.
|
|
do_get_affiliation(JID, #state{config = #config{persistent = false}} = StateData) ->
|
|
do_get_affiliation_fallback(JID, StateData);
|
|
do_get_affiliation(JID, StateData) ->
|
|
Room = StateData#state.room,
|
|
Host = StateData#state.host,
|
|
LServer = JID#jid.lserver,
|
|
LUser = JID#jid.luser,
|
|
ServerHost = StateData#state.server_host,
|
|
Mod = gen_mod:db_mod(ServerHost, mod_muc),
|
|
case Mod:get_affiliation(ServerHost, Room, Host, LUser, LServer) of
|
|
{error, _} ->
|
|
do_get_affiliation_fallback(JID, StateData);
|
|
{ok, Affiliation} ->
|
|
Affiliation
|
|
end.
|
|
|
|
-spec do_get_affiliation_fallback(jid(), state()) -> affiliation() | {affiliation(), binary()}.
|
|
do_get_affiliation_fallback(JID, StateData) ->
|
|
LJID = jid:tolower(JID),
|
|
try maps:get(LJID, StateData#state.affiliations)
|
|
catch _:{badkey, _} ->
|
|
BareLJID = jid:remove_resource(LJID),
|
|
try maps:get(BareLJID, StateData#state.affiliations)
|
|
catch _:{badkey, _} ->
|
|
DomainLJID = setelement(1, LJID, <<"">>),
|
|
try maps:get(DomainLJID, StateData#state.affiliations)
|
|
catch _:{badkey, _} ->
|
|
DomainBareLJID = jid:remove_resource(DomainLJID),
|
|
try maps:get(DomainBareLJID, StateData#state.affiliations)
|
|
catch _:{badkey, _} -> none
|
|
end
|
|
end
|
|
end
|
|
end.
|
|
|
|
-spec get_affiliations(state()) -> affiliations().
|
|
get_affiliations(#state{config = #config{persistent = false}} = StateData) ->
|
|
get_affiliations_callback(StateData);
|
|
get_affiliations(StateData) ->
|
|
Room = StateData#state.room,
|
|
Host = StateData#state.host,
|
|
ServerHost = StateData#state.server_host,
|
|
Mod = gen_mod:db_mod(ServerHost, mod_muc),
|
|
case Mod:get_affiliations(ServerHost, Room, Host) of
|
|
{error, _} ->
|
|
get_affiliations_callback(StateData);
|
|
{ok, Affiliations} ->
|
|
Affiliations
|
|
end.
|
|
|
|
-spec get_affiliations_callback(state()) -> affiliations().
|
|
get_affiliations_callback(StateData) ->
|
|
StateData#state.affiliations.
|
|
|
|
-spec get_service_affiliation(jid(), state()) -> owner | none.
|
|
get_service_affiliation(JID, StateData) ->
|
|
{_AccessRoute, _AccessCreate, AccessAdmin,
|
|
_AccessPersistent, _AccessMam} =
|
|
StateData#state.access,
|
|
case acl:match_rule(StateData#state.server_host,
|
|
AccessAdmin, JID)
|
|
of
|
|
allow -> owner;
|
|
_ -> none
|
|
end.
|
|
|
|
-spec set_role(jid(), role(), state()) -> state().
|
|
set_role(JID, Role, StateData) ->
|
|
LJID = jid:tolower(JID),
|
|
LJIDs = case LJID of
|
|
{U, S, <<"">>} ->
|
|
maps:fold(fun (J, _, Js) ->
|
|
case J of
|
|
{U, S, _} -> [J | Js];
|
|
_ -> Js
|
|
end
|
|
end, [], StateData#state.users);
|
|
_ ->
|
|
case maps:is_key(LJID, StateData#state.users) of
|
|
true -> [LJID];
|
|
_ -> []
|
|
end
|
|
end,
|
|
{Users, Nicks} =
|
|
case Role of
|
|
none ->
|
|
lists:foldl(
|
|
fun (J, {Us, Ns}) ->
|
|
NewNs = try maps:get(J, Us) of
|
|
#user{nick = Nick} ->
|
|
maps:remove(Nick, Ns)
|
|
catch _:{badkey, _} ->
|
|
Ns
|
|
end,
|
|
{maps:remove(J, Us), NewNs}
|
|
end,
|
|
{StateData#state.users, StateData#state.nicks}, LJIDs);
|
|
_ ->
|
|
{lists:foldl(
|
|
fun (J, Us) ->
|
|
User = maps:get(J, Us),
|
|
if User#user.last_presence == undefined ->
|
|
Us;
|
|
true ->
|
|
maps:put(J, User#user{role = Role}, Us)
|
|
end
|
|
end, StateData#state.users, LJIDs),
|
|
StateData#state.nicks}
|
|
end,
|
|
StateData#state{users = Users, nicks = Nicks}.
|
|
|
|
-spec get_role(jid(), state()) -> role().
|
|
get_role(JID, StateData) ->
|
|
LJID = jid:tolower(JID),
|
|
try maps:get(LJID, StateData#state.users) of
|
|
#user{role = Role} -> Role
|
|
catch _:{badkey, _} -> none
|
|
end.
|
|
|
|
-spec get_default_role(affiliation(), state()) -> role().
|
|
get_default_role(Affiliation, StateData) ->
|
|
case Affiliation of
|
|
owner -> moderator;
|
|
admin -> moderator;
|
|
member -> participant;
|
|
outcast -> none;
|
|
none ->
|
|
case (StateData#state.config)#config.members_only of
|
|
true -> none;
|
|
_ ->
|
|
case (StateData#state.config)#config.members_by_default
|
|
of
|
|
true -> participant;
|
|
_ -> visitor
|
|
end
|
|
end
|
|
end.
|
|
|
|
-spec is_visitor(jid(), state()) -> boolean().
|
|
is_visitor(Jid, StateData) ->
|
|
get_role(Jid, StateData) =:= visitor.
|
|
|
|
-spec is_moderator(jid(), state()) -> boolean().
|
|
is_moderator(Jid, StateData) ->
|
|
get_role(Jid, StateData) =:= moderator.
|
|
|
|
-spec get_max_users(state()) -> non_neg_integer().
|
|
get_max_users(StateData) ->
|
|
MaxUsers = (StateData#state.config)#config.max_users,
|
|
ServiceMaxUsers = get_service_max_users(StateData),
|
|
if MaxUsers =< ServiceMaxUsers -> MaxUsers;
|
|
true -> ServiceMaxUsers
|
|
end.
|
|
|
|
-spec get_service_max_users(state()) -> pos_integer().
|
|
get_service_max_users(StateData) ->
|
|
mod_muc_opt:max_users(StateData#state.server_host).
|
|
|
|
-spec get_max_users_admin_threshold(state()) -> pos_integer().
|
|
get_max_users_admin_threshold(StateData) ->
|
|
mod_muc_opt:max_users_admin_threshold(StateData#state.server_host).
|
|
|
|
-spec room_queue_new(binary(), ejabberd_shaper:shaper(), _) -> p1_queue:queue({message | presence, jid()}) | undefined.
|
|
room_queue_new(ServerHost, Shaper, QueueType) ->
|
|
HaveRoomShaper = Shaper /= none,
|
|
HaveMessageShaper = mod_muc_opt:user_message_shaper(ServerHost) /= none,
|
|
HavePresenceShaper = mod_muc_opt:user_presence_shaper(ServerHost) /= none,
|
|
HaveMinMessageInterval = mod_muc_opt:min_message_interval(ServerHost) /= 0,
|
|
HaveMinPresenceInterval = mod_muc_opt:min_presence_interval(ServerHost) /= 0,
|
|
if HaveRoomShaper or HaveMessageShaper or HavePresenceShaper
|
|
or HaveMinMessageInterval or HaveMinPresenceInterval ->
|
|
p1_queue:new(QueueType);
|
|
true ->
|
|
undefined
|
|
end.
|
|
|
|
-spec get_user_activity(jid(), state()) -> #activity{}.
|
|
get_user_activity(JID, StateData) ->
|
|
case treap:lookup(jid:tolower(JID),
|
|
StateData#state.activity)
|
|
of
|
|
{ok, _P, A} -> A;
|
|
error ->
|
|
MessageShaper =
|
|
ejabberd_shaper:new(mod_muc_opt:user_message_shaper(StateData#state.server_host)),
|
|
PresenceShaper =
|
|
ejabberd_shaper:new(mod_muc_opt:user_presence_shaper(StateData#state.server_host)),
|
|
#activity{message_shaper = MessageShaper,
|
|
presence_shaper = PresenceShaper}
|
|
end.
|
|
|
|
-spec store_user_activity(jid(), #activity{}, state()) -> state().
|
|
store_user_activity(JID, UserActivity, StateData) ->
|
|
MinMessageInterval =
|
|
trunc(mod_muc_opt:min_message_interval(StateData#state.server_host) * 1000),
|
|
MinPresenceInterval =
|
|
trunc(mod_muc_opt:min_presence_interval(StateData#state.server_host) * 1000),
|
|
Key = jid:tolower(JID),
|
|
Now = erlang:system_time(microsecond),
|
|
Activity1 = clean_treap(StateData#state.activity,
|
|
{1, -Now}),
|
|
Activity = case treap:lookup(Key, Activity1) of
|
|
{ok, _P, _A} -> treap:delete(Key, Activity1);
|
|
error -> Activity1
|
|
end,
|
|
StateData1 = case MinMessageInterval == 0 andalso
|
|
MinPresenceInterval == 0 andalso
|
|
UserActivity#activity.message_shaper == none andalso
|
|
UserActivity#activity.presence_shaper == none
|
|
andalso
|
|
UserActivity#activity.message == undefined andalso
|
|
UserActivity#activity.presence == undefined
|
|
of
|
|
true -> StateData#state{activity = Activity};
|
|
false ->
|
|
case UserActivity#activity.message == undefined andalso
|
|
UserActivity#activity.presence == undefined
|
|
of
|
|
true ->
|
|
{_, MessageShaperInterval} =
|
|
ejabberd_shaper:update(UserActivity#activity.message_shaper,
|
|
100000),
|
|
{_, PresenceShaperInterval} =
|
|
ejabberd_shaper:update(UserActivity#activity.presence_shaper,
|
|
100000),
|
|
Delay = lists:max([MessageShaperInterval,
|
|
PresenceShaperInterval,
|
|
MinMessageInterval,
|
|
MinPresenceInterval])
|
|
* 1000,
|
|
Priority = {1, -(Now + Delay)},
|
|
StateData#state{activity =
|
|
treap:insert(Key, Priority,
|
|
UserActivity,
|
|
Activity)};
|
|
false ->
|
|
Priority = {0, 0},
|
|
StateData#state{activity =
|
|
treap:insert(Key, Priority,
|
|
UserActivity,
|
|
Activity)}
|
|
end
|
|
end,
|
|
reset_hibernate_timer(StateData1).
|
|
|
|
-spec clean_treap(treap:treap(), integer() | {1, integer()}) -> treap:treap().
|
|
clean_treap(Treap, CleanPriority) ->
|
|
case treap:is_empty(Treap) of
|
|
true -> Treap;
|
|
false ->
|
|
{_Key, Priority, _Value} = treap:get_root(Treap),
|
|
if Priority > CleanPriority ->
|
|
clean_treap(treap:delete_root(Treap), CleanPriority);
|
|
true -> Treap
|
|
end
|
|
end.
|
|
|
|
-spec prepare_room_queue(state()) -> state().
|
|
prepare_room_queue(StateData) ->
|
|
case p1_queue:out(StateData#state.room_queue) of
|
|
{{value, {message, From}}, _RoomQueue} ->
|
|
Activity = get_user_activity(From, StateData),
|
|
Packet = Activity#activity.message,
|
|
Size = element_size(Packet),
|
|
{RoomShaper, RoomShaperInterval} =
|
|
ejabberd_shaper:update(StateData#state.room_shaper, Size),
|
|
erlang:send_after(RoomShaperInterval, self(),
|
|
process_room_queue),
|
|
StateData#state{room_shaper = RoomShaper};
|
|
{{value, {presence, From}}, _RoomQueue} ->
|
|
Activity = get_user_activity(From, StateData),
|
|
{_Nick, Packet} = Activity#activity.presence,
|
|
Size = element_size(Packet),
|
|
{RoomShaper, RoomShaperInterval} =
|
|
ejabberd_shaper:update(StateData#state.room_shaper, Size),
|
|
erlang:send_after(RoomShaperInterval, self(),
|
|
process_room_queue),
|
|
StateData#state{room_shaper = RoomShaper};
|
|
{empty, _} -> StateData
|
|
end.
|
|
|
|
-spec update_online_user(jid(), #user{}, state()) -> state().
|
|
update_online_user(JID, #user{nick = Nick} = User, StateData) ->
|
|
LJID = jid:tolower(JID),
|
|
add_to_log(join, Nick, StateData),
|
|
Nicks1 = try maps:get(LJID, StateData#state.users) of
|
|
#user{nick = OldNick} ->
|
|
case lists:delete(
|
|
LJID, maps:get(OldNick, StateData#state.nicks)) of
|
|
[] ->
|
|
maps:remove(OldNick, StateData#state.nicks);
|
|
LJIDs ->
|
|
maps:put(OldNick, LJIDs, StateData#state.nicks)
|
|
end
|
|
catch _:{badkey, _} ->
|
|
StateData#state.nicks
|
|
end,
|
|
Nicks = maps:update_with(Nick,
|
|
fun (LJIDs) -> [LJID|LJIDs -- [LJID]] end,
|
|
[LJID], Nicks1),
|
|
Users = maps:update_with(LJID,
|
|
fun(U) ->
|
|
U#user{nick = Nick}
|
|
end, User, StateData#state.users),
|
|
NewStateData = StateData#state{users = Users, nicks = Nicks},
|
|
case {maps:get(LJID, StateData#state.users, error),
|
|
maps:get(LJID, NewStateData#state.users, error)} of
|
|
{#user{nick = Old}, #user{nick = New}} when Old /= New ->
|
|
send_nick_changing(JID, Old, NewStateData, true, true);
|
|
_ ->
|
|
ok
|
|
end,
|
|
NewStateData.
|
|
|
|
-spec set_subscriber(jid(), binary(), [binary()], state()) -> state().
|
|
set_subscriber(JID, Nick, Nodes,
|
|
#state{room = Room, host = Host, server_host = ServerHost} = StateData) ->
|
|
BareJID = jid:remove_resource(JID),
|
|
LBareJID = jid:tolower(BareJID),
|
|
Subscribers = maps:put(LBareJID,
|
|
#subscriber{jid = BareJID,
|
|
nick = Nick,
|
|
nodes = Nodes},
|
|
StateData#state.subscribers),
|
|
Nicks = maps:put(Nick, [LBareJID], StateData#state.subscriber_nicks),
|
|
NewStateData = StateData#state{subscribers = Subscribers,
|
|
subscriber_nicks = Nicks},
|
|
store_room(NewStateData, [{add_subscription, BareJID, Nick, Nodes}]),
|
|
case not maps:is_key(LBareJID, StateData#state.subscribers) of
|
|
true ->
|
|
send_subscriptions_change_notifications(BareJID, Nick, subscribe, NewStateData),
|
|
ejabberd_hooks:run(muc_subscribed, ServerHost, [ServerHost, Room, Host, BareJID]);
|
|
_ ->
|
|
ok
|
|
end,
|
|
NewStateData.
|
|
|
|
-spec add_online_user(jid(), binary(), role(), state()) -> state().
|
|
add_online_user(JID, Nick, Role, StateData) ->
|
|
tab_add_online_user(JID, StateData),
|
|
User = #user{jid = JID, nick = Nick, role = Role},
|
|
reset_hibernate_timer(update_online_user(JID, User, StateData)).
|
|
|
|
-spec remove_online_user(jid(), state()) -> state().
|
|
remove_online_user(JID, StateData) ->
|
|
remove_online_user(JID, StateData, <<"">>).
|
|
|
|
-spec remove_online_user(jid(), state(), binary()) -> state().
|
|
remove_online_user(JID, StateData, Reason) ->
|
|
LJID = jid:tolower(JID),
|
|
#user{nick = Nick} = maps:get(LJID, StateData#state.users),
|
|
add_to_log(leave, {Nick, Reason}, StateData),
|
|
tab_remove_online_user(JID, StateData),
|
|
Users = maps:remove(LJID, StateData#state.users),
|
|
Nicks = try maps:get(Nick, StateData#state.nicks) of
|
|
[LJID] ->
|
|
maps:remove(Nick, StateData#state.nicks);
|
|
U ->
|
|
maps:put(Nick, U -- [LJID], StateData#state.nicks)
|
|
catch _:{badkey, _} ->
|
|
StateData#state.nicks
|
|
end,
|
|
reset_hibernate_timer(StateData#state{users = Users, nicks = Nicks}).
|
|
|
|
-spec filter_presence(presence()) -> presence().
|
|
filter_presence(Presence) ->
|
|
Els = lists:filter(
|
|
fun(El) ->
|
|
XMLNS = xmpp:get_ns(El),
|
|
case catch binary:part(XMLNS, 0, size(?NS_MUC)) of
|
|
?NS_MUC -> false;
|
|
_ -> true
|
|
end
|
|
end, xmpp:get_els(Presence)),
|
|
xmpp:set_els(Presence, Els).
|
|
|
|
-spec strip_status(presence()) -> presence().
|
|
strip_status(Presence) ->
|
|
Presence#presence{status = []}.
|
|
|
|
-spec add_user_presence(jid(), presence(), state()) -> state().
|
|
add_user_presence(JID, Presence, StateData) ->
|
|
LJID = jid:tolower(JID),
|
|
FPresence = filter_presence(Presence),
|
|
Users = maps:update_with(LJID,
|
|
fun (#user{} = User) ->
|
|
User#user{last_presence = FPresence}
|
|
end, StateData#state.users),
|
|
StateData#state{users = Users}.
|
|
|
|
-spec add_user_presence_un(jid(), presence(), state()) -> state().
|
|
add_user_presence_un(JID, Presence, StateData) ->
|
|
LJID = jid:tolower(JID),
|
|
FPresence = filter_presence(Presence),
|
|
Users = maps:update_with(LJID,
|
|
fun (#user{} = User) ->
|
|
User#user{last_presence = FPresence,
|
|
role = none}
|
|
end, StateData#state.users),
|
|
StateData#state{users = Users}.
|
|
|
|
%% Find and return a list of the full JIDs of the users of Nick.
|
|
%% Return jid record.
|
|
-spec find_jids_by_nick(binary(), state()) -> [jid()].
|
|
find_jids_by_nick(Nick, StateData) ->
|
|
Users = case maps:get(Nick, StateData#state.nicks, []) of
|
|
[] -> maps:get(Nick, StateData#state.subscriber_nicks, []);
|
|
Us -> Us
|
|
end,
|
|
[jid:make(LJID) || LJID <- Users].
|
|
|
|
%% Find and return the full JID of the user of Nick with
|
|
%% highest-priority presence. Return jid record.
|
|
-spec find_jid_by_nick(binary(), state()) -> jid() | false.
|
|
find_jid_by_nick(Nick, StateData) ->
|
|
try maps:get(Nick, StateData#state.nicks) of
|
|
[User] -> jid:make(User);
|
|
[FirstUser | Users] ->
|
|
#user{last_presence = FirstPresence} =
|
|
maps:get(FirstUser, StateData#state.users),
|
|
{LJID, _} = lists:foldl(
|
|
fun(Compare, {HighestUser, HighestPresence}) ->
|
|
#user{last_presence = P1} =
|
|
maps:get(Compare, StateData#state.users),
|
|
case higher_presence(P1, HighestPresence) of
|
|
true -> {Compare, P1};
|
|
false -> {HighestUser, HighestPresence}
|
|
end
|
|
end, {FirstUser, FirstPresence}, Users),
|
|
jid:make(LJID)
|
|
catch _:{badkey, _} ->
|
|
false
|
|
end.
|
|
|
|
-spec higher_presence(undefined | presence(),
|
|
undefined | presence()) -> boolean().
|
|
higher_presence(Pres1, Pres2) when Pres1 /= undefined, Pres2 /= undefined ->
|
|
Pri1 = get_priority_from_presence(Pres1),
|
|
Pri2 = get_priority_from_presence(Pres2),
|
|
Pri1 > Pri2;
|
|
higher_presence(Pres1, Pres2) ->
|
|
Pres1 > Pres2.
|
|
|
|
-spec get_priority_from_presence(presence()) -> integer().
|
|
get_priority_from_presence(#presence{priority = Prio}) ->
|
|
case Prio of
|
|
undefined -> 0;
|
|
_ -> Prio
|
|
end.
|
|
|
|
-spec find_nick_by_jid(jid(), state()) -> binary().
|
|
find_nick_by_jid(JID, StateData) ->
|
|
LJID = jid:tolower(JID),
|
|
#user{nick = Nick} = maps:get(LJID, StateData#state.users),
|
|
Nick.
|
|
|
|
-spec is_nick_change(jid(), binary(), state()) -> boolean().
|
|
is_nick_change(JID, Nick, StateData) ->
|
|
LJID = jid:tolower(JID),
|
|
case Nick of
|
|
<<"">> -> false;
|
|
_ ->
|
|
#user{nick = OldNick} = maps:get(LJID, StateData#state.users),
|
|
Nick /= OldNick
|
|
end.
|
|
|
|
-spec nick_collision(jid(), binary(), state()) -> boolean().
|
|
nick_collision(User, Nick, StateData) ->
|
|
UserOfNick = case find_jid_by_nick(Nick, StateData) of
|
|
false ->
|
|
try maps:get(Nick, StateData#state.subscriber_nicks) of
|
|
[J] -> J
|
|
catch _:{badkey, _} -> false
|
|
end;
|
|
J -> J
|
|
end,
|
|
(UserOfNick /= false andalso
|
|
jid:remove_resource(jid:tolower(UserOfNick))
|
|
/= jid:remove_resource(jid:tolower(User))).
|
|
|
|
-spec add_new_user(jid(), binary(), presence(), state()) -> state();
|
|
(jid(), binary(), iq(), state()) -> {error, stanza_error()} |
|
|
{ignore, state()} |
|
|
{result, muc_subscribe(), state()}.
|
|
add_new_user(From, Nick, Packet, StateData) ->
|
|
Lang = xmpp:get_lang(Packet),
|
|
MaxUsers = get_max_users(StateData),
|
|
MaxAdminUsers = MaxUsers +
|
|
get_max_users_admin_threshold(StateData),
|
|
NUsers = maps:size(StateData#state.users),
|
|
Affiliation = get_affiliation(From, StateData),
|
|
ServiceAffiliation = get_service_affiliation(From,
|
|
StateData),
|
|
NConferences = tab_count_user(From, StateData),
|
|
MaxConferences =
|
|
mod_muc_opt:max_user_conferences(StateData#state.server_host),
|
|
Collision = nick_collision(From, Nick, StateData),
|
|
IsSubscribeRequest = not is_record(Packet, presence),
|
|
case {(ServiceAffiliation == owner orelse
|
|
((Affiliation == admin orelse Affiliation == owner)
|
|
andalso NUsers < MaxAdminUsers)
|
|
orelse NUsers < MaxUsers)
|
|
andalso NConferences < MaxConferences,
|
|
Collision,
|
|
mod_muc:can_use_nick(StateData#state.server_host,
|
|
StateData#state.host, From, Nick),
|
|
get_default_role(Affiliation, StateData)}
|
|
of
|
|
{false, _, _, _} when NUsers >= MaxUsers orelse NUsers >= MaxAdminUsers ->
|
|
Txt = ?T("Too many users in this conference"),
|
|
Err = xmpp:err_resource_constraint(Txt, Lang),
|
|
if not IsSubscribeRequest ->
|
|
ejabberd_router:route_error(Packet, Err),
|
|
StateData;
|
|
true ->
|
|
{error, Err}
|
|
end;
|
|
{false, _, _, _} when NConferences >= MaxConferences ->
|
|
Txt = ?T("You have joined too many conferences"),
|
|
Err = xmpp:err_resource_constraint(Txt, Lang),
|
|
if not IsSubscribeRequest ->
|
|
ejabberd_router:route_error(Packet, Err),
|
|
StateData;
|
|
true ->
|
|
{error, Err}
|
|
end;
|
|
{false, _, _, _} ->
|
|
Err = xmpp:err_service_unavailable(),
|
|
if not IsSubscribeRequest ->
|
|
ejabberd_router:route_error(Packet, Err),
|
|
StateData;
|
|
true ->
|
|
{error, Err}
|
|
end;
|
|
{_, _, _, none} ->
|
|
Err = case Affiliation of
|
|
outcast ->
|
|
ErrText = ?T("You have been banned from this room"),
|
|
xmpp:err_forbidden(ErrText, Lang);
|
|
_ ->
|
|
ErrText = ?T("Membership is required to enter this room"),
|
|
xmpp:err_registration_required(ErrText, Lang)
|
|
end,
|
|
if not IsSubscribeRequest ->
|
|
ejabberd_router:route_error(Packet, Err),
|
|
StateData;
|
|
true ->
|
|
{error, Err}
|
|
end;
|
|
{_, true, _, _} ->
|
|
ErrText = ?T("That nickname is already in use by another occupant"),
|
|
Err = xmpp:err_conflict(ErrText, Lang),
|
|
if not IsSubscribeRequest ->
|
|
ejabberd_router:route_error(Packet, Err),
|
|
StateData;
|
|
true ->
|
|
{error, Err}
|
|
end;
|
|
{_, _, false, _} ->
|
|
Err = case Nick of
|
|
<<>> ->
|
|
xmpp:err_jid_malformed(?T("Nickname can't be empty"),
|
|
Lang);
|
|
_ ->
|
|
xmpp:err_conflict(?T("That nickname is registered"
|
|
" by another person"), Lang)
|
|
end,
|
|
if not IsSubscribeRequest ->
|
|
ejabberd_router:route_error(Packet, Err),
|
|
StateData;
|
|
true ->
|
|
{error, Err}
|
|
end;
|
|
{_, _, _, Role} ->
|
|
case check_password(ServiceAffiliation, Affiliation,
|
|
Packet, From, StateData)
|
|
of
|
|
true ->
|
|
Nodes = get_subscription_nodes(Packet),
|
|
NewStateData =
|
|
if not IsSubscribeRequest ->
|
|
NewState = add_user_presence(
|
|
From, Packet,
|
|
add_online_user(From, Nick, Role,
|
|
StateData)),
|
|
send_initial_presences_and_messages(
|
|
From, Nick, Packet, NewState, StateData),
|
|
NewState;
|
|
true ->
|
|
set_subscriber(From, Nick, Nodes, StateData)
|
|
end,
|
|
ResultState =
|
|
case NewStateData#state.just_created of
|
|
true ->
|
|
NewStateData#state{just_created = erlang:system_time(microsecond)};
|
|
_ ->
|
|
Robots = maps:remove(From, StateData#state.robots),
|
|
NewStateData#state{robots = Robots}
|
|
end,
|
|
if not IsSubscribeRequest -> ResultState;
|
|
true -> {result, subscribe_result(Packet), ResultState}
|
|
end;
|
|
need_password ->
|
|
ErrText = ?T("A password is required to enter this room"),
|
|
Err = xmpp:err_not_authorized(ErrText, Lang),
|
|
if not IsSubscribeRequest ->
|
|
ejabberd_router:route_error(Packet, Err),
|
|
StateData;
|
|
true ->
|
|
{error, Err}
|
|
end;
|
|
captcha_required ->
|
|
SID = xmpp:get_id(Packet),
|
|
RoomJID = StateData#state.jid,
|
|
To = jid:replace_resource(RoomJID, Nick),
|
|
Limiter = {From#jid.luser, From#jid.lserver},
|
|
case ejabberd_captcha:create_captcha(SID, RoomJID, To,
|
|
Lang, Limiter, From)
|
|
of
|
|
{ok, ID, Body, CaptchaEls} ->
|
|
MsgPkt = #message{from = RoomJID,
|
|
to = From,
|
|
id = ID, body = Body,
|
|
sub_els = CaptchaEls},
|
|
Robots = maps:put(From, {Nick, Packet},
|
|
StateData#state.robots),
|
|
ejabberd_router:route(MsgPkt),
|
|
NewState = StateData#state{robots = Robots},
|
|
if not IsSubscribeRequest ->
|
|
NewState;
|
|
true ->
|
|
{ignore, NewState}
|
|
end;
|
|
{error, limit} ->
|
|
ErrText = ?T("Too many CAPTCHA requests"),
|
|
Err = xmpp:err_resource_constraint(ErrText, Lang),
|
|
if not IsSubscribeRequest ->
|
|
ejabberd_router:route_error(Packet, Err),
|
|
StateData;
|
|
true ->
|
|
{error, Err}
|
|
end;
|
|
_ ->
|
|
ErrText = ?T("Unable to generate a CAPTCHA"),
|
|
Err = xmpp:err_internal_server_error(ErrText, Lang),
|
|
if not IsSubscribeRequest ->
|
|
ejabberd_router:route_error(Packet, Err),
|
|
StateData;
|
|
true ->
|
|
{error, Err}
|
|
end
|
|
end;
|
|
_ ->
|
|
ErrText = ?T("Incorrect password"),
|
|
Err = xmpp:err_not_authorized(ErrText, Lang),
|
|
if not IsSubscribeRequest ->
|
|
ejabberd_router:route_error(Packet, Err),
|
|
StateData;
|
|
true ->
|
|
{error, Err}
|
|
end
|
|
end
|
|
end.
|
|
|
|
-spec check_password(affiliation(), affiliation(),
|
|
presence() | iq(), jid(), state()) ->
|
|
boolean() | need_password | captcha_required.
|
|
check_password(owner, _Affiliation, _Packet, _From,
|
|
_StateData) ->
|
|
%% Don't check pass if user is owner in MUC service (access_admin option)
|
|
true;
|
|
check_password(_ServiceAffiliation, Affiliation, Packet,
|
|
From, StateData) ->
|
|
case (StateData#state.config)#config.password_protected
|
|
of
|
|
false -> check_captcha(Affiliation, From, StateData);
|
|
true ->
|
|
Pass = extract_password(Packet),
|
|
case Pass of
|
|
false -> need_password;
|
|
_ ->
|
|
case (StateData#state.config)#config.password of
|
|
Pass -> true;
|
|
_ -> false
|
|
end
|
|
end
|
|
end.
|
|
|
|
-spec check_captcha(affiliation(), jid(), state()) -> true | captcha_required.
|
|
check_captcha(Affiliation, From, StateData) ->
|
|
case (StateData#state.config)#config.captcha_protected
|
|
andalso ejabberd_captcha:is_feature_available()
|
|
of
|
|
true when Affiliation == none ->
|
|
case maps:get(From, StateData#state.robots, error) of
|
|
passed -> true;
|
|
_ ->
|
|
WList =
|
|
(StateData#state.config)#config.captcha_whitelist,
|
|
#jid{luser = U, lserver = S, lresource = R} = From,
|
|
case (?SETS):is_element({U, S, R}, WList) of
|
|
true -> true;
|
|
false ->
|
|
case (?SETS):is_element({U, S, <<"">>}, WList) of
|
|
true -> true;
|
|
false ->
|
|
case (?SETS):is_element({<<"">>, S, <<"">>}, WList)
|
|
of
|
|
true -> true;
|
|
false -> captcha_required
|
|
end
|
|
end
|
|
end
|
|
end;
|
|
_ -> true
|
|
end.
|
|
|
|
-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.
|
|
|
|
-spec get_history(binary(), stanza(), state()) -> [lqueue_elem()].
|
|
get_history(Nick, Packet, #state{history = History}) ->
|
|
case xmpp:get_subtag(Packet, #muc{}) of
|
|
#muc{history = #muc_history{} = MUCHistory} ->
|
|
Now = erlang:timestamp(),
|
|
Q = History#lqueue.queue,
|
|
filter_history(Q, Now, Nick, MUCHistory);
|
|
_ ->
|
|
p1_queue:to_list(History#lqueue.queue)
|
|
end.
|
|
|
|
-spec filter_history(p1_queue:queue(lqueue_elem()), erlang:timestamp(),
|
|
binary(), muc_history()) -> [lqueue_elem()].
|
|
filter_history(Queue, Now, Nick,
|
|
#muc_history{since = Since,
|
|
seconds = Seconds,
|
|
maxstanzas = MaxStanzas,
|
|
maxchars = MaxChars}) ->
|
|
{History, _, _} =
|
|
lists:foldr(
|
|
fun({_, _, _, TimeStamp, Size} = Elem,
|
|
{Elems, NumStanzas, NumChars} = Acc) ->
|
|
NowDiff = timer:now_diff(Now, TimeStamp) div 1000000,
|
|
Chars = Size + byte_size(Nick) + 1,
|
|
if (NumStanzas < MaxStanzas) andalso
|
|
(TimeStamp > Since) andalso
|
|
(NowDiff =< Seconds) andalso
|
|
(NumChars + Chars =< MaxChars) ->
|
|
{[Elem|Elems], NumStanzas + 1, NumChars + Chars};
|
|
true ->
|
|
Acc
|
|
end
|
|
end, {[], 0, 0}, p1_queue:to_list(Queue)),
|
|
History.
|
|
|
|
-spec is_room_overcrowded(state()) -> boolean().
|
|
is_room_overcrowded(StateData) ->
|
|
MaxUsersPresence = mod_muc_opt:max_users_presence(StateData#state.server_host),
|
|
maps:size(StateData#state.users) > MaxUsersPresence.
|
|
|
|
-spec presence_broadcast_allowed(jid(), state()) -> boolean().
|
|
presence_broadcast_allowed(JID, StateData) ->
|
|
Role = get_role(JID, StateData),
|
|
lists:member(Role, (StateData#state.config)#config.presence_broadcast).
|
|
|
|
-spec send_initial_presences_and_messages(
|
|
jid(), binary(), presence(), state(), state()) -> ok.
|
|
send_initial_presences_and_messages(From, Nick, Presence, NewState, OldState) ->
|
|
advertise_entity_capabilities(From, NewState),
|
|
send_existing_presences(From, NewState),
|
|
send_self_presence(From, NewState, OldState),
|
|
History = get_history(Nick, Presence, NewState),
|
|
send_history(From, History, NewState),
|
|
send_subject(From, OldState).
|
|
|
|
-spec advertise_entity_capabilities(jid(), state()) -> ok.
|
|
advertise_entity_capabilities(JID, State) ->
|
|
AvatarHash = (State#state.config)#config.vcard_xupdate,
|
|
DiscoInfo = make_disco_info(JID, State),
|
|
Extras = iq_disco_info_extras(<<"en">>, State, true),
|
|
DiscoInfo1 = DiscoInfo#disco_info{xdata = [Extras]},
|
|
DiscoHash = mod_caps:compute_disco_hash(DiscoInfo1, sha),
|
|
Els1 = [#caps{hash = <<"sha-1">>,
|
|
node = ejabberd_config:get_uri(),
|
|
version = DiscoHash}],
|
|
Els2 = if is_binary(AvatarHash) ->
|
|
[#vcard_xupdate{hash = AvatarHash}|Els1];
|
|
true ->
|
|
Els1
|
|
end,
|
|
ejabberd_router:route(#presence{from = State#state.jid, to = JID,
|
|
id = p1_rand:get_string(),
|
|
sub_els = Els2}).
|
|
|
|
-spec send_self_presence(jid(), state(), state()) -> ok.
|
|
send_self_presence(NJID, StateData, OldStateData) ->
|
|
send_new_presence(NJID, <<"">>, true, StateData, OldStateData).
|
|
|
|
-spec send_update_presence(jid(), state(), state()) -> ok.
|
|
send_update_presence(JID, StateData, OldStateData) ->
|
|
send_update_presence(JID, <<"">>, StateData, OldStateData).
|
|
|
|
-spec send_update_presence(jid(), binary(), sta |