2003-03-23 21:08:44 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_muc_room.erl
|
2007-12-24 14:57:53 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2003-03-23 21:08:44 +01:00
|
|
|
%%% Purpose : MUC room stuff
|
2007-12-24 14:57:53 +01:00
|
|
|
%%% Created : 19 Mar 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2016-01-13 12:29:14 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2016 ProcessOne
|
2007-12-24 14:57:53 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-12-24 14:57:53 +01:00
|
|
|
%%%
|
2003-03-23 21:08:44 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_muc_room).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2007-12-24 14:57:53 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-03-23 21:08:44 +01:00
|
|
|
|
|
|
|
-behaviour(gen_fsm).
|
|
|
|
|
|
|
|
%% External exports
|
2012-04-27 11:52:05 +02:00
|
|
|
-export([start_link/9,
|
|
|
|
start_link/7,
|
|
|
|
start/9,
|
|
|
|
start/7,
|
2015-08-06 12:33:39 +02:00
|
|
|
get_role/2,
|
2015-12-24 13:01:33 +01:00
|
|
|
get_affiliation/2,
|
|
|
|
is_occupant_or_admin/2,
|
2003-03-23 21:08:44 +01:00
|
|
|
route/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("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-include("xmpp.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2008-12-02 20:23:12 +01:00
|
|
|
-include("mod_muc_room.hrl").
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2007-08-29 19:54:45 +02:00
|
|
|
-define(MAX_USERS_DEFAULT_LIST,
|
|
|
|
[5, 10, 20, 30, 50, 100, 200, 500, 1000, 2000, 5000]).
|
2007-08-28 16:35:50 +02:00
|
|
|
|
2015-05-08 13:09:13 +02:00
|
|
|
-define(DEFAULT_MAX_USERS_PRESENCE,1000).
|
|
|
|
|
2003-09-19 20:22:44 +02:00
|
|
|
%-define(DBGFSM, true).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
|
|
|
-ifdef(DBGFSM).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-03-23 21:08:44 +01:00
|
|
|
-define(FSMOPTS, [{debug, [trace]}]).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-03-23 21:08:44 +01:00
|
|
|
-else.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-03-23 21:08:44 +01:00
|
|
|
-define(FSMOPTS, []).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-03-23 21:08:44 +01:00
|
|
|
-endif.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-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 history_element() :: {binary(), %% nick
|
|
|
|
message(), %% message itself
|
|
|
|
boolean(), %% have subject
|
|
|
|
erlang:timestamp(),
|
|
|
|
non_neg_integer()}.
|
|
|
|
|
|
|
|
-export_type([state/0]).
|
|
|
|
|
2003-03-23 21:08:44 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2007-09-01 23:05:04 +02:00
|
|
|
start(Host, ServerHost, Access, Room, HistorySize, RoomShaper,
|
2012-04-27 11:52:05 +02:00
|
|
|
Creator, Nick, DefRoomOpts) ->
|
2015-11-04 16:24:35 +01:00
|
|
|
gen_fsm:start(?MODULE, [Host, ServerHost, Access, Room, HistorySize,
|
|
|
|
RoomShaper, Creator, Nick, DefRoomOpts],
|
|
|
|
?FSMOPTS).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
start(Host, ServerHost, Access, Room, HistorySize, RoomShaper, Opts) ->
|
2015-11-04 16:24:35 +01:00
|
|
|
gen_fsm:start(?MODULE, [Host, ServerHost, Access, Room, HistorySize,
|
|
|
|
RoomShaper, Opts],
|
|
|
|
?FSMOPTS).
|
2006-02-02 06:00:27 +01:00
|
|
|
|
2007-09-01 23:05:04 +02:00
|
|
|
start_link(Host, ServerHost, Access, Room, HistorySize, RoomShaper,
|
2012-04-27 11:52:05 +02:00
|
|
|
Creator, Nick, DefRoomOpts) ->
|
2007-09-01 23:05:04 +02:00
|
|
|
gen_fsm:start_link(?MODULE, [Host, ServerHost, Access, Room, HistorySize,
|
2012-04-27 11:52:05 +02:00
|
|
|
RoomShaper, Creator, Nick, DefRoomOpts],
|
2006-02-03 04:28:15 +01:00
|
|
|
?FSMOPTS).
|
2006-02-02 06:00:27 +01:00
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
start_link(Host, ServerHost, Access, Room, HistorySize, RoomShaper, Opts) ->
|
2007-09-01 23:05:04 +02:00
|
|
|
gen_fsm:start_link(?MODULE, [Host, ServerHost, Access, Room, HistorySize,
|
2012-04-27 11:52:05 +02:00
|
|
|
RoomShaper, Opts],
|
2006-02-03 04:28:15 +01:00
|
|
|
?FSMOPTS).
|
2003-03-25 22:03:35 +01:00
|
|
|
|
2003-03-23 21:08:44 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Callback functions from gen_fsm
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2015-10-07 00:06:58 +02:00
|
|
|
init([Host, ServerHost, Access, Room, HistorySize,
|
|
|
|
RoomShaper, Creator, _Nick, DefRoomOpts]) ->
|
2008-02-28 01:30:23 +01:00
|
|
|
process_flag(trap_exit, true),
|
2007-09-01 23:05:04 +02:00
|
|
|
Shaper = shaper:new(RoomShaper),
|
2003-03-23 21:08:44 +01:00
|
|
|
State = set_affiliation(Creator, owner,
|
2015-10-07 00:06:58 +02:00
|
|
|
#state{host = Host, server_host = ServerHost,
|
|
|
|
access = Access, room = Room,
|
|
|
|
history = lqueue_new(HistorySize),
|
2015-11-24 16:44:13 +01:00
|
|
|
jid = jid:make(Room, Host, <<"">>),
|
2015-10-07 00:06:58 +02:00
|
|
|
just_created = true,
|
|
|
|
room_shaper = Shaper}),
|
2007-08-02 04:30:25 +02:00
|
|
|
State1 = set_opts(DefRoomOpts, State),
|
2016-06-26 08:08:37 +02:00
|
|
|
store_room(State1),
|
2015-10-07 00:06:58 +02:00
|
|
|
?INFO_MSG("Created MUC room ~s@~s by ~s",
|
2015-11-24 16:44:13 +01:00
|
|
|
[Room, Host, jid:to_string(Creator)]),
|
2009-12-29 15:43:24 +01:00
|
|
|
add_to_log(room_existence, created, State1),
|
|
|
|
add_to_log(room_existence, started, State1),
|
2007-08-02 04:30:25 +02:00
|
|
|
{ok, normal_state, State1};
|
2012-04-27 11:52:05 +02:00
|
|
|
init([Host, ServerHost, Access, Room, HistorySize, RoomShaper, Opts]) ->
|
2008-02-28 01:30:23 +01:00
|
|
|
process_flag(trap_exit, true),
|
2007-09-01 23:05:04 +02:00
|
|
|
Shaper = shaper:new(RoomShaper),
|
2003-03-25 22:03:35 +01:00
|
|
|
State = set_opts(Opts, #state{host = Host,
|
2005-06-20 05:18:13 +02:00
|
|
|
server_host = ServerHost,
|
2004-05-17 22:36:41 +02:00
|
|
|
access = Access,
|
2003-10-07 22:31:44 +02:00
|
|
|
room = Room,
|
2006-09-05 17:53:54 +02:00
|
|
|
history = lqueue_new(HistorySize),
|
2015-11-24 16:44:13 +01:00
|
|
|
jid = jid:make(Room, Host, <<"">>),
|
2007-09-01 23:05:04 +02:00
|
|
|
room_shaper = Shaper}),
|
2009-12-29 15:43:24 +01:00
|
|
|
add_to_log(room_existence, started, State),
|
2003-03-23 21:08:44 +01:00
|
|
|
{ok, normal_state, State}.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
normal_state({route, From, <<"">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
#message{type = Type, lang = Lang} = Packet}, StateData) ->
|
2008-06-13 20:55:26 +02:00
|
|
|
case is_user_online(From, StateData) orelse
|
2016-07-25 12:50:30 +02:00
|
|
|
is_user_allowed_message_nonparticipant(From, StateData) of
|
|
|
|
true when Type == groupchat ->
|
|
|
|
Activity = get_user_activity(From, StateData),
|
|
|
|
Now = p1_time_compat:system_time(micro_seconds),
|
|
|
|
MinMessageInterval = trunc(gen_mod:get_module_opt(
|
|
|
|
StateData#state.server_host,
|
|
|
|
mod_muc, min_message_interval,
|
|
|
|
fun(MMI) when is_number(MMI) -> MMI end, 0)
|
|
|
|
* 1000000),
|
|
|
|
Size = element_size(Packet),
|
|
|
|
{MessageShaper, MessageShaperInterval} =
|
|
|
|
shaper:update(Activity#activity.message_shaper, Size),
|
|
|
|
if Activity#activity.message /= undefined ->
|
|
|
|
ErrText = <<"Traffic rate limit is exceeded">>,
|
|
|
|
Err = xmpp:make_error(
|
|
|
|
Packet,
|
|
|
|
xmpp:err_resource_constraint(ErrText, Lang)),
|
|
|
|
ejabberd_router:route(StateData#state.jid, From, Err),
|
2015-08-06 12:33:39 +02:00
|
|
|
{next_state, normal_state, StateData};
|
2016-07-25 12:50:30 +02:00
|
|
|
Now >= Activity#activity.message_time + MinMessageInterval,
|
|
|
|
MessageShaperInterval == 0 ->
|
|
|
|
{RoomShaper, RoomShaperInterval} =
|
|
|
|
shaper:update(StateData#state.room_shaper, Size),
|
|
|
|
RoomQueueEmpty = queue:is_empty(StateData#state.room_queue),
|
|
|
|
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(From, Packet,
|
|
|
|
StateData2);
|
2016-06-26 08:08:37 +02:00
|
|
|
true ->
|
2016-07-25 12:50:30 +02:00
|
|
|
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 = 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}
|
2015-08-06 12:33:39 +02:00
|
|
|
end;
|
2016-07-25 12:50:30 +02:00
|
|
|
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 = <<"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);
|
2015-08-06 12:33:39 +02:00
|
|
|
_ ->
|
|
|
|
{next_state, normal_state, StateData}
|
2016-07-25 12:50:30 +02:00
|
|
|
end;
|
|
|
|
true when Type == chat ->
|
|
|
|
ErrText = <<"It is not allowed to send private messages "
|
|
|
|
"to the conference">>,
|
|
|
|
Err = xmpp:err_not_acceptable(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(StateData#state.jid, From, 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:format_error(Why),
|
|
|
|
Err = xmpp:err_bad_request(Txt, Lang),
|
|
|
|
ejabberd_router:route_error(
|
|
|
|
StateData#state.jid, From, Packet, Err),
|
|
|
|
StateData
|
|
|
|
end};
|
|
|
|
true ->
|
|
|
|
ErrText = <<"Improper message type">>,
|
|
|
|
Err = xmpp:err_not_acceptable(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(StateData#state.jid, From, Packet, Err),
|
|
|
|
{next_state, normal_state, StateData};
|
|
|
|
false when Type /= error ->
|
|
|
|
handle_roommessage_from_nonparticipant(Packet, StateData, From);
|
|
|
|
false ->
|
|
|
|
{next_state, normal_state, StateData}
|
2003-03-23 21:08:44 +01:00
|
|
|
end;
|
2016-07-25 12:50:30 +02:00
|
|
|
normal_state({route, From, <<"">>,
|
|
|
|
#iq{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(StateData#state.jid, From, IQRes),
|
|
|
|
{next_state, normal_state, StateData};
|
|
|
|
#iq{sub_els = [SubEl]} = IQ ->
|
|
|
|
Res1 = case xmpp:get_ns(SubEl) of
|
|
|
|
?NS_MUC_ADMIN ->
|
|
|
|
process_iq_admin(From, IQ, StateData);
|
|
|
|
?NS_MUC_OWNER ->
|
|
|
|
process_iq_owner(From, IQ, StateData);
|
|
|
|
?NS_DISCO_INFO when SubEl#disco_info.node == undefined ->
|
|
|
|
process_iq_disco_info(From, IQ, StateData);
|
|
|
|
?NS_DISCO_INFO ->
|
|
|
|
Txt = <<"Disco info is not available for this node">>,
|
|
|
|
{error, xmpp:err_service_unavailable(Txt, Lang)};
|
|
|
|
?NS_DISCO_ITEMS ->
|
|
|
|
process_iq_disco_items(From, IQ, StateData);
|
|
|
|
?NS_VCARD ->
|
|
|
|
process_iq_vcard(From, IQ, StateData);
|
|
|
|
?NS_MUCSUB ->
|
|
|
|
process_iq_mucsub(From, IQ, StateData);
|
|
|
|
?NS_CAPTCHA ->
|
|
|
|
process_iq_captcha(From, IQ, StateData);
|
|
|
|
_ ->
|
|
|
|
{error, xmpp:err_feature_not_implemented()}
|
|
|
|
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, ResStateData} ->
|
|
|
|
{xmpp:make_error(IQ0, Error), ResStateData};
|
|
|
|
{error, Error} ->
|
|
|
|
{xmpp:make_error(IQ0, Error), StateData}
|
|
|
|
end,
|
|
|
|
if IQRes /= ignore ->
|
|
|
|
ejabberd_router:route(StateData#state.jid, From, IQRes);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
case NewStateData of
|
|
|
|
stop -> {stop, normal, StateData};
|
|
|
|
_ -> {next_state, normal_state, NewStateData}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
catch _:{xmpp_codec, Why} ->
|
|
|
|
ErrTxt = xmpp:format_error(Why),
|
|
|
|
Err = xmpp:make_error(IQ0, xmpp:err_bad_request(ErrTxt, Lang)),
|
|
|
|
ejabberd_router:route(StateData#state.jid, From, Err)
|
|
|
|
end;
|
|
|
|
normal_state({route, From, <<"">>, #iq{} = IQ}, StateData) ->
|
|
|
|
Err = xmpp:err_bad_request(),
|
|
|
|
ejabberd_router:route_error(StateData#state.jid, From, IQ, Err),
|
|
|
|
{next_state, normal_state, StateData};
|
|
|
|
normal_state({route, From, Nick, #presence{} = Packet}, StateData) ->
|
2007-09-01 23:05:04 +02:00
|
|
|
Activity = get_user_activity(From, StateData),
|
2015-12-04 15:08:43 +01:00
|
|
|
Now = p1_time_compat:system_time(micro_seconds),
|
2007-06-25 18:43:42 +02:00
|
|
|
MinPresenceInterval =
|
2013-03-14 10:33:02 +01:00
|
|
|
trunc(gen_mod:get_module_opt(StateData#state.server_host,
|
|
|
|
mod_muc, min_presence_interval,
|
|
|
|
fun(I) when is_number(I), I>=0 ->
|
|
|
|
I
|
|
|
|
end, 0)
|
|
|
|
* 1000000),
|
2016-07-25 12:50:30 +02:00
|
|
|
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(From, Nick, Packet, StateData1);
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
2016-07-25 12:50:30 +02:00
|
|
|
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}
|
2003-06-30 14:24:35 +02:00
|
|
|
end;
|
2003-03-23 21:08:44 +01:00
|
|
|
normal_state({route, From, ToNick,
|
2016-07-25 12:50:30 +02:00
|
|
|
#message{type = Type, lang = Lang} = Packet},
|
2003-03-23 21:08:44 +01:00
|
|
|
StateData) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
case decide_fate_message(Packet, From, StateData) of
|
|
|
|
{expulse_sender, Reason} ->
|
|
|
|
?DEBUG(Reason, []),
|
|
|
|
ErrorText = <<"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)} of
|
|
|
|
{true, true} when Type == groupchat ->
|
|
|
|
ErrText = <<"It is not allowed to send private messages "
|
|
|
|
"of type \"groupchat\"">>,
|
|
|
|
Err = xmpp:err_bad_request(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(
|
|
|
|
jid:replace_resource(StateData#state.jid, ToNick),
|
|
|
|
From, Packet, Err);
|
|
|
|
{true, true} ->
|
|
|
|
case find_jids_by_nick(ToNick, StateData) of
|
|
|
|
[] ->
|
|
|
|
ErrText = <<"Recipient is not in the conference room">>,
|
|
|
|
Err = xmpp:err_item_not_found(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(
|
|
|
|
jid:replace_resource(StateData#state.jid, ToNick),
|
|
|
|
From, Packet, Err);
|
2013-03-14 10:33:02 +01:00
|
|
|
ToJIDs ->
|
|
|
|
SrcIsVisitor = is_visitor(From, StateData),
|
2016-07-25 12:50:30 +02:00
|
|
|
DstIsModerator = is_moderator(hd(ToJIDs), StateData),
|
2013-03-14 10:33:02 +01:00
|
|
|
PmFromVisitors =
|
|
|
|
(StateData#state.config)#config.allow_private_messages_from_visitors,
|
|
|
|
if SrcIsVisitor == false;
|
|
|
|
PmFromVisitors == anyone;
|
|
|
|
(PmFromVisitors == moderators) and
|
2016-07-25 12:50:30 +02:00
|
|
|
DstIsModerator ->
|
|
|
|
{ok, #user{nick = FromNick}} =
|
|
|
|
(?DICT):find(jid:tolower(From),
|
|
|
|
StateData#state.users),
|
|
|
|
FromNickJID =
|
|
|
|
jid:replace_resource(StateData#state.jid,
|
|
|
|
FromNick),
|
|
|
|
X = #muc_user{},
|
|
|
|
PrivMsg = xmpp:set_subtag(Packet, X),
|
|
|
|
[ejabberd_router:route(FromNickJID, ToJID, PrivMsg)
|
|
|
|
|| ToJID <- ToJIDs];
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
2016-07-25 12:50:30 +02:00
|
|
|
ErrText = <<"It is not allowed to send private messages">>,
|
|
|
|
Err = xmpp:err_forbidden(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(
|
|
|
|
jid:replace_resource(StateData#state.jid, ToNick),
|
|
|
|
From, Packet, Err)
|
2008-01-29 15:49:08 +01:00
|
|
|
end
|
2016-07-25 12:50:30 +02:00
|
|
|
end;
|
|
|
|
{true, false} ->
|
|
|
|
ErrText = <<"Only occupants are allowed to send messages "
|
|
|
|
"to the conference">>,
|
|
|
|
Err = xmpp:err_not_acceptable(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(
|
|
|
|
jid:replace_resource(StateData#state.jid, ToNick),
|
|
|
|
From, Packet, Err);
|
|
|
|
{false, _} ->
|
|
|
|
ErrText = <<"It is not allowed to send private messages">>,
|
|
|
|
Err = xmpp:err_forbidden(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(
|
|
|
|
jid:replace_resource(StateData#state.jid, ToNick),
|
|
|
|
From, Packet, Err)
|
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
{next_state, normal_state, StateData}
|
2008-01-29 15:49:08 +01:00
|
|
|
end;
|
2003-03-23 21:08:44 +01:00
|
|
|
normal_state({route, From, ToNick,
|
2016-07-25 12:50:30 +02:00
|
|
|
#iq{id = StanzaId, lang = Lang} = Packet},
|
2003-03-23 21:08:44 +01:00
|
|
|
StateData) ->
|
2004-03-02 22:16:55 +01:00
|
|
|
case {(StateData#state.config)#config.allow_query_users,
|
2016-07-25 12:50:30 +02:00
|
|
|
is_user_online_iq(StanzaId, From, StateData)} of
|
|
|
|
{true, {true, NewId, FromFull}} ->
|
|
|
|
case find_jid_by_nick(ToNick, StateData) of
|
|
|
|
false ->
|
|
|
|
ErrText = <<"Recipient is not in the conference room">>,
|
|
|
|
Err = xmpp:err_item_not_found(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(
|
|
|
|
jid:replace_resource(StateData#state.jid, ToNick),
|
|
|
|
From, Packet, Err);
|
|
|
|
ToJID ->
|
|
|
|
{ok, #user{nick = FromNick}} =
|
|
|
|
(?DICT):find(jid:tolower(FromFull), StateData#state.users),
|
|
|
|
{ToJID2, Packet2} = handle_iq_vcard(ToJID, NewId, Packet),
|
|
|
|
ejabberd_router:route(
|
|
|
|
jid:replace_resource(StateData#state.jid, FromNick),
|
|
|
|
ToJID2, Packet2)
|
|
|
|
end;
|
|
|
|
{_, {false, _, _}} ->
|
|
|
|
ErrText = <<"Only occupants are allowed to send queries "
|
|
|
|
"to the conference">>,
|
|
|
|
Err = xmpp:err_not_acceptable(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(
|
|
|
|
jid:replace_resource(StateData#state.jid, ToNick),
|
|
|
|
From, Packet, Err);
|
|
|
|
_ ->
|
|
|
|
ErrText = <<"Queries to the conference members are "
|
|
|
|
"not allowed in this room">>,
|
|
|
|
Err = xmpp:err_not_allowed(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(
|
|
|
|
jid:replace_resource(StateData#state.jid, ToNick),
|
|
|
|
From, Packet, Err)
|
2003-03-25 22:03:35 +01:00
|
|
|
end,
|
2003-03-23 21:08:44 +01:00
|
|
|
{next_state, normal_state, StateData};
|
2006-01-19 03:17:31 +01:00
|
|
|
normal_state(_Event, StateData) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
{next_state, normal_state, StateData}.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_event({service_message, Msg}, _StateName,
|
|
|
|
StateData) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
MessagePkt = #message{type = groupchat, body = xmpp:mk_text(Msg)},
|
2016-06-26 08:08:37 +02:00
|
|
|
send_wrapped_multiple(
|
2015-04-08 14:01:16 +02:00
|
|
|
StateData#state.jid,
|
|
|
|
StateData#state.users,
|
2016-06-26 08:08:37 +02:00
|
|
|
MessagePkt,
|
|
|
|
?NS_MUCSUB_NODES_MESSAGES,
|
|
|
|
StateData),
|
2013-03-14 10:33:02 +01:00
|
|
|
NSD = add_message_to_history(<<"">>,
|
|
|
|
StateData#state.jid, MessagePkt, StateData),
|
2003-05-18 18:41:15 +02:00
|
|
|
{next_state, normal_state, NSD};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_event({destroy, Reason}, _StateName,
|
|
|
|
StateData) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
{result, undefined, stop} =
|
|
|
|
destroy_room(#muc_destroy{xmlns = ?NS_MUC_OWNER, reason = Reason},
|
|
|
|
StateData),
|
2013-03-14 10:33:02 +01:00
|
|
|
?INFO_MSG("Destroyed MUC room ~s with reason: ~p",
|
2015-11-24 16:44:13 +01:00
|
|
|
[jid:to_string(StateData#state.jid), Reason]),
|
2009-12-29 15:43:24 +01:00
|
|
|
add_to_log(room_existence, destroyed, StateData),
|
2010-02-02 12:49:49 +01:00
|
|
|
{stop, shutdown, StateData};
|
2007-02-19 10:45:58 +01:00
|
|
|
handle_event(destroy, StateName, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
?INFO_MSG("Destroyed MUC room ~s",
|
2015-11-24 16:44:13 +01:00
|
|
|
[jid:to_string(StateData#state.jid)]),
|
2016-07-25 12:50:30 +02:00
|
|
|
handle_event({destroy, undefined}, StateName, StateData);
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_event({set_affiliations, Affiliations},
|
|
|
|
StateName, StateData) ->
|
|
|
|
{next_state, StateName,
|
|
|
|
StateData#state{affiliations = Affiliations}};
|
2004-05-17 22:36:41 +02:00
|
|
|
handle_event(_Event, StateName, StateData) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
{next_state, StateName, StateData}.
|
|
|
|
|
2015-12-02 01:50:30 +01:00
|
|
|
handle_sync_event({get_disco_item, Filter, JID, Lang}, _From, StateName, StateData) ->
|
|
|
|
Len = ?DICT:fold(fun(_, _, Acc) -> Acc + 1 end, 0,
|
|
|
|
StateData#state.users),
|
|
|
|
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,
|
2010-06-23 09:44:35 +02:00
|
|
|
{reply, Reply, StateName, StateData};
|
2015-12-02 01:50:30 +01:00
|
|
|
%% This clause is only for backwards compatibility
|
|
|
|
handle_sync_event({get_disco_item, JID, Lang}, From, StateName, StateData) ->
|
|
|
|
handle_sync_event({get_disco_item, any, JID, Lang}, From, StateName, StateData);
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_sync_event(get_config, _From, StateName,
|
|
|
|
StateData) ->
|
|
|
|
{reply, {ok, StateData#state.config}, StateName,
|
|
|
|
StateData};
|
|
|
|
handle_sync_event(get_state, _From, StateName,
|
|
|
|
StateData) ->
|
2007-02-19 17:56:06 +01:00
|
|
|
{reply, {ok, StateData}, StateName, StateData};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_sync_event({change_config, Config}, _From,
|
|
|
|
StateName, StateData) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
{result, undefined, NSD} = change_config(Config, StateData),
|
2007-02-19 17:56:06 +01:00
|
|
|
{reply, {ok, NSD#state.config}, StateName, NSD};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_sync_event({change_state, NewStateData}, _From,
|
|
|
|
StateName, _StateData) ->
|
2008-05-16 20:05:03 +02:00
|
|
|
{reply, {ok, NewStateData}, StateName, NewStateData};
|
2015-02-25 16:31:27 +01:00
|
|
|
handle_sync_event({process_item_change, Item, UJID}, _From, StateName, StateData) ->
|
|
|
|
NSD = process_item_change(Item, StateData, UJID),
|
|
|
|
{reply, {ok, NSD}, StateName, NSD};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_sync_event(_Event, _From, StateName,
|
|
|
|
StateData) ->
|
|
|
|
Reply = ok, {reply, Reply, StateName, StateData}.
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2004-05-17 22:36:41 +02:00
|
|
|
code_change(_OldVsn, StateName, StateData, _Extra) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
{ok, StateName, StateData}.
|
|
|
|
|
2007-09-01 23:05:04 +02:00
|
|
|
handle_info({process_user_presence, From}, normal_state = _StateName, StateData) ->
|
2008-04-27 21:06:34 +02:00
|
|
|
RoomQueueEmpty = queue:is_empty(StateData#state.room_queue),
|
|
|
|
RoomQueue = queue:in({presence, From}, StateData#state.room_queue),
|
|
|
|
StateData1 = StateData#state{room_queue = RoomQueue},
|
2013-03-14 10:33:02 +01:00
|
|
|
if RoomQueueEmpty ->
|
|
|
|
StateData2 = prepare_room_queue(StateData1),
|
|
|
|
{next_state, normal_state, StateData2};
|
|
|
|
true -> {next_state, normal_state, StateData1}
|
2008-04-27 21:06:34 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({process_user_message, From},
|
|
|
|
normal_state = _StateName, StateData) ->
|
|
|
|
RoomQueueEmpty =
|
|
|
|
queue:is_empty(StateData#state.room_queue),
|
|
|
|
RoomQueue = queue:in({message, From},
|
|
|
|
StateData#state.room_queue),
|
2008-04-27 21:06:34 +02:00
|
|
|
StateData1 = StateData#state{room_queue = RoomQueue},
|
2013-03-14 10:33:02 +01:00
|
|
|
if RoomQueueEmpty ->
|
|
|
|
StateData2 = prepare_room_queue(StateData1),
|
|
|
|
{next_state, normal_state, StateData2};
|
|
|
|
true -> {next_state, normal_state, StateData1}
|
2008-04-27 21:06:34 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info(process_room_queue,
|
|
|
|
normal_state = StateName, StateData) ->
|
2007-09-01 23:05:04 +02:00
|
|
|
case queue:out(StateData#state.room_queue) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{{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(From, 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(From, Nick, Packet, StateData3);
|
|
|
|
{empty, _} -> {next_state, StateName, StateData}
|
2007-09-01 23:05:04 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({captcha_succeed, From}, normal_state,
|
|
|
|
StateData) ->
|
|
|
|
NewState = case (?DICT):find(From,
|
|
|
|
StateData#state.robots)
|
|
|
|
of
|
|
|
|
{ok, {Nick, Packet}} ->
|
|
|
|
Robots = (?DICT):store(From, passed,
|
|
|
|
StateData#state.robots),
|
|
|
|
add_new_user(From, Nick, Packet,
|
|
|
|
StateData#state{robots = Robots});
|
|
|
|
_ -> StateData
|
2009-03-13 17:01:46 +01:00
|
|
|
end,
|
|
|
|
{next_state, normal_state, NewState};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({captcha_failed, From}, normal_state,
|
|
|
|
StateData) ->
|
|
|
|
NewState = case (?DICT):find(From,
|
|
|
|
StateData#state.robots)
|
|
|
|
of
|
|
|
|
{ok, {Nick, Packet}} ->
|
|
|
|
Robots = (?DICT):erase(From, StateData#state.robots),
|
2016-04-05 12:09:44 +02:00
|
|
|
Txt = <<"The CAPTCHA verification has failed">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
Lang = xmpp:get_lang(Packet),
|
|
|
|
Err = xmpp:err_not_authorized(Txt, Lang),
|
|
|
|
ejabberd_router:route_error(
|
|
|
|
jid:replace_resource(StateData#state.jid, Nick),
|
|
|
|
From, Packet, Err),
|
2013-03-14 10:33:02 +01:00
|
|
|
StateData#state{robots = Robots};
|
|
|
|
_ -> StateData
|
2009-03-13 17:01:46 +01:00
|
|
|
end,
|
|
|
|
{next_state, normal_state, NewState};
|
2013-06-19 13:30:31 +02:00
|
|
|
handle_info(shutdown, _StateName, StateData) ->
|
|
|
|
{stop, shutdown, StateData};
|
2004-05-17 22:36:41 +02:00
|
|
|
handle_info(_Info, StateName, StateData) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
{next_state, StateName, StateData}.
|
|
|
|
|
2009-12-30 02:25:35 +01:00
|
|
|
terminate(Reason, _StateName, StateData) ->
|
2009-12-29 15:43:24 +01:00
|
|
|
?INFO_MSG("Stopping MUC room ~s@~s",
|
|
|
|
[StateData#state.room, StateData#state.host]),
|
2009-12-30 02:25:35 +01:00
|
|
|
ReasonT = case Reason of
|
2013-03-14 10:33:02 +01:00
|
|
|
shutdown ->
|
|
|
|
<<"You are being removed from the room "
|
|
|
|
"because of a system shutdown">>;
|
|
|
|
_ -> <<"Room terminates">>
|
2009-12-30 02:25:35 +01:00
|
|
|
end,
|
2016-07-25 12:50:30 +02:00
|
|
|
Packet = #presence{
|
|
|
|
type = unavailable,
|
|
|
|
sub_els = [#muc_user{items = [#muc_item{affiliation = none,
|
|
|
|
reason = ReasonT,
|
|
|
|
role = none}],
|
|
|
|
status_codes = [332]}]},
|
2013-03-14 10:33:02 +01:00
|
|
|
(?DICT):fold(fun (LJID, Info, _) ->
|
|
|
|
Nick = Info#user.nick,
|
|
|
|
case Reason of
|
|
|
|
shutdown ->
|
2016-06-26 08:08:37 +02:00
|
|
|
send_wrapped(jid:replace_resource(StateData#state.jid,
|
|
|
|
Nick),
|
|
|
|
Info#user.jid, Packet,
|
|
|
|
?NS_MUCSUB_NODES_PARTICIPANTS,
|
|
|
|
StateData);
|
2013-03-14 10:33:02 +01:00
|
|
|
_ -> ok
|
|
|
|
end,
|
|
|
|
tab_remove_online_user(LJID, StateData)
|
|
|
|
end,
|
|
|
|
[], StateData#state.users),
|
2009-12-29 15:43:24 +01:00
|
|
|
add_to_log(room_existence, stopped, StateData),
|
2012-04-27 11:52:05 +02:00
|
|
|
mod_muc:room_destroyed(StateData#state.host, StateData#state.room, self(),
|
|
|
|
StateData#state.server_host),
|
2003-03-23 21:08:44 +01:00
|
|
|
ok.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec route(pid(), jid(), binary(), stanza()) -> ok.
|
2003-03-23 21:08:44 +01:00
|
|
|
route(Pid, From, ToNick, Packet) ->
|
|
|
|
gen_fsm:send_event(Pid, {route, From, ToNick, Packet}).
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec process_groupchat_message(jid(), message(), state()) -> fsm_next().
|
|
|
|
process_groupchat_message(From, #message{lang = Lang} = Packet, StateData) ->
|
2008-06-13 20:55:26 +02:00
|
|
|
case is_user_online(From, StateData) orelse
|
2013-03-14 10:33:02 +01:00
|
|
|
is_user_allowed_message_nonparticipant(From, StateData)
|
|
|
|
of
|
|
|
|
true ->
|
2016-06-26 08:08:37 +02:00
|
|
|
{FromNick, Role, IsSubscriber} = get_participant_data(From, StateData),
|
|
|
|
if (Role == moderator) or (Role == participant) or IsSubscriber or
|
2013-03-14 10:33:02 +01:00
|
|
|
((StateData#state.config)#config.moderated == false) ->
|
2016-06-26 08:08:37 +02:00
|
|
|
Subject = check_subject(Packet),
|
|
|
|
{NewStateData1, IsAllowed} = case Subject of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> {StateData, true};
|
2016-06-26 08:08:37 +02:00
|
|
|
_ ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case
|
|
|
|
can_change_subject(Role,
|
|
|
|
StateData)
|
|
|
|
of
|
|
|
|
true ->
|
|
|
|
NSD =
|
|
|
|
StateData#state{subject
|
|
|
|
=
|
|
|
|
Subject,
|
|
|
|
subject_author
|
|
|
|
=
|
|
|
|
FromNick},
|
2016-06-26 08:08:37 +02:00
|
|
|
store_room(NSD),
|
2013-03-14 10:33:02 +01:00
|
|
|
{NSD, true};
|
|
|
|
_ -> {StateData, false}
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
case IsAllowed of
|
|
|
|
true ->
|
2015-06-26 21:33:32 +02:00
|
|
|
case
|
2015-08-04 21:16:45 +02:00
|
|
|
ejabberd_hooks:run_fold(muc_filter_message,
|
2015-06-26 21:33:32 +02:00
|
|
|
StateData#state.server_host,
|
|
|
|
Packet,
|
|
|
|
[StateData,
|
|
|
|
StateData#state.jid,
|
|
|
|
From, FromNick])
|
|
|
|
of
|
|
|
|
drop ->
|
|
|
|
{next_state, normal_state, StateData};
|
2015-09-25 17:00:00 +02:00
|
|
|
NewPacket1 ->
|
2016-07-25 12:50:30 +02:00
|
|
|
NewPacket = xmpp:remove_subtag(NewPacket1, #nick{}),
|
2016-06-26 08:08:37 +02:00
|
|
|
Node = if Subject == false -> ?NS_MUCSUB_NODES_MESSAGES;
|
|
|
|
true -> ?NS_MUCSUB_NODES_SUBJECT
|
|
|
|
end,
|
|
|
|
send_wrapped_multiple(
|
|
|
|
jid:replace_resource(StateData#state.jid, FromNick),
|
|
|
|
StateData#state.users,
|
|
|
|
NewPacket, Node, NewStateData1),
|
2015-10-07 00:06:58 +02:00
|
|
|
NewStateData2 = case has_body_or_subject(NewPacket) of
|
2015-06-26 21:33:32 +02:00
|
|
|
true ->
|
|
|
|
add_message_to_history(FromNick, From,
|
2015-08-06 12:33:39 +02:00
|
|
|
NewPacket,
|
2015-06-26 21:33:32 +02:00
|
|
|
NewStateData1);
|
|
|
|
false ->
|
|
|
|
NewStateData1
|
|
|
|
end,
|
|
|
|
{next_state, normal_state, NewStateData2}
|
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
_ ->
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = case (StateData#state.config)#config.allow_change_subj of
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
2016-07-25 12:50:30 +02:00
|
|
|
xmpp:err_forbidden(
|
|
|
|
<<"Only moderators and participants are "
|
|
|
|
"allowed to change the subject in this "
|
|
|
|
"room">>, Lang);
|
2013-03-14 10:33:02 +01:00
|
|
|
_ ->
|
2016-07-25 12:50:30 +02:00
|
|
|
xmpp:err_forbidden(
|
|
|
|
<<"Only moderators are allowed to change "
|
|
|
|
"the subject in this room">>, Lang)
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
2016-07-25 12:50:30 +02:00
|
|
|
ejabberd_router:route_error(
|
|
|
|
StateData#state.jid, From, Packet, Err),
|
2013-03-14 10:33:02 +01:00
|
|
|
{next_state, normal_state, StateData}
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
ErrText = <<"Visitors are not allowed to send messages "
|
|
|
|
"to all occupants">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_forbidden(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(
|
|
|
|
StateData#state.jid, From, Packet, Err),
|
2013-03-14 10:33:02 +01:00
|
|
|
{next_state, normal_state, StateData}
|
|
|
|
end;
|
|
|
|
false ->
|
2016-07-25 12:50:30 +02:00
|
|
|
ErrText = <<"Only occupants are allowed to send messages "
|
|
|
|
"to the conference">>,
|
|
|
|
Err = xmpp:err_not_acceptable(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(StateData#state.jid, From, Packet, Err),
|
2013-03-14 10:33:02 +01:00
|
|
|
{next_state, normal_state, StateData}
|
2007-06-25 18:43:42 +02:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec process_normal_message(jid(), message(), state()) -> state().
|
|
|
|
process_normal_message(From, #message{lang = Lang} = Pkt, StateData) ->
|
|
|
|
IsInvitation = is_invitation(Pkt),
|
|
|
|
IsVoiceRequest = is_voice_request(Pkt) and
|
|
|
|
is_visitor(From, StateData),
|
|
|
|
IsVoiceApprovement = is_voice_approvement(Pkt) and
|
|
|
|
not is_visitor(From, StateData),
|
|
|
|
if IsInvitation ->
|
|
|
|
case check_invitation(From, Pkt, StateData) of
|
|
|
|
{error, Error} ->
|
|
|
|
ejabberd_router:route_error(StateData#state.jid, From, Pkt, Error),
|
|
|
|
StateData;
|
|
|
|
IJID ->
|
|
|
|
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
|
|
|
|
end;
|
|
|
|
IsVoiceRequest ->
|
|
|
|
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 = -p1_time_compat:system_time(micro_seconds),
|
|
|
|
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 = <<"Please, wait for a while before sending "
|
|
|
|
"new voice request">>,
|
|
|
|
Err = xmpp:err_not_acceptable(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(
|
|
|
|
StateData#state.jid, From, Pkt, Err),
|
|
|
|
StateData#state{last_voice_request_time = Times}
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
ErrText = <<"Voice requests are disabled in this conference">>,
|
|
|
|
Err = xmpp:err_forbidden(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(
|
|
|
|
StateData#state.jid, From, Pkt, Err),
|
|
|
|
StateData
|
|
|
|
end;
|
|
|
|
IsVoiceApprovement ->
|
|
|
|
case is_moderator(From, StateData) of
|
|
|
|
true ->
|
|
|
|
case extract_jid_from_voice_approvement(Pkt) of
|
|
|
|
error ->
|
|
|
|
ErrText = <<"Failed to extract JID from your voice "
|
|
|
|
"request approval">>,
|
|
|
|
Err = xmpp:err_bad_request(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(
|
|
|
|
StateData#state.jid, From, Pkt, Err),
|
|
|
|
StateData;
|
|
|
|
TargetJid ->
|
|
|
|
case is_visitor(TargetJid, StateData) of
|
|
|
|
true ->
|
|
|
|
Reason = <<>>,
|
|
|
|
NSD = set_role(TargetJid,
|
|
|
|
participant,
|
|
|
|
StateData),
|
|
|
|
catch send_new_presence(TargetJid,
|
|
|
|
Reason,
|
|
|
|
NSD,
|
|
|
|
StateData),
|
|
|
|
NSD;
|
|
|
|
_ ->
|
|
|
|
StateData
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
ErrText = <<"Only moderators can approve voice requests">>,
|
|
|
|
Err = xmpp:err_not_allowed(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(
|
|
|
|
StateData#state.jid, From, Pkt, Err),
|
|
|
|
StateData
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
StateData
|
|
|
|
end.
|
|
|
|
|
2008-06-13 20:55:26 +02:00
|
|
|
%% @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.
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec is_user_allowed_message_nonparticipant(jid(), state()) -> boolean().
|
2013-03-14 10:33:02 +01:00
|
|
|
is_user_allowed_message_nonparticipant(JID,
|
|
|
|
StateData) ->
|
2008-07-17 19:32:11 +02:00
|
|
|
case get_service_affiliation(JID, StateData) of
|
2013-03-14 10:33:02 +01:00
|
|
|
owner -> true;
|
|
|
|
_ -> false
|
2008-06-13 20:55:26 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% @doc Get information of this participant, or default values.
|
|
|
|
%% If the JID is not a participant, return values for a service message.
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec get_participant_data(jid(), state()) -> {binary(), role(), boolean()}.
|
2008-06-13 20:55:26 +02:00
|
|
|
get_participant_data(From, StateData) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
case (?DICT):find(jid:tolower(From),
|
2013-03-14 10:33:02 +01:00
|
|
|
StateData#state.users)
|
|
|
|
of
|
2016-06-26 08:08:37 +02:00
|
|
|
{ok, #user{nick = FromNick, role = Role, is_subscriber = IsSubscriber}} ->
|
|
|
|
{FromNick, Role, IsSubscriber};
|
|
|
|
error -> {<<"">>, moderator, false}
|
2008-06-13 20:55:26 +02:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec process_presence(jid(), binary(), presence(), state()) -> fsm_transition().
|
|
|
|
process_presence(From, Nick, #presence{type = Type0} = Packet0, StateData) ->
|
2015-08-04 20:13:00 +02:00
|
|
|
IsOnline = is_user_online(From, StateData),
|
2016-07-25 12:50:30 +02:00
|
|
|
if Type0 == available;
|
|
|
|
IsOnline and ((Type0 == unavailable) or (Type0 == error)) ->
|
2015-08-04 20:13:00 +02:00
|
|
|
case ejabberd_hooks:run_fold(muc_filter_presence,
|
|
|
|
StateData#state.server_host,
|
|
|
|
Packet0,
|
|
|
|
[StateData,
|
|
|
|
StateData#state.jid,
|
|
|
|
From, Nick]) of
|
|
|
|
drop ->
|
|
|
|
{next_state, normal_state, StateData};
|
2016-07-25 12:50:30 +02:00
|
|
|
#presence{} = Packet ->
|
|
|
|
close_room_if_temporary_and_empty(
|
|
|
|
do_process_presence(From, Nick, Packet, StateData))
|
2015-08-04 20:13:00 +02:00
|
|
|
end;
|
|
|
|
true ->
|
2016-07-25 12:50:30 +02:00
|
|
|
{next_state, normal_state, StateData}
|
2015-08-04 20:13:00 +02:00
|
|
|
end.
|
2014-05-05 13:50:52 +02:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec do_process_presence(jid(), binary(), presence(), state()) ->
|
|
|
|
state().
|
|
|
|
do_process_presence(From, Nick, #presence{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}} ->
|
|
|
|
ErrText = <<"Visitors are not allowed to change their "
|
|
|
|
"nicknames in this room">>,
|
|
|
|
Err = xmpp:err_not_allowed(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(
|
|
|
|
jid:replace_resource(StateData#state.jid, Nick),
|
|
|
|
From, Packet, Err),
|
|
|
|
StateData;
|
|
|
|
{true, _, _} ->
|
|
|
|
ErrText = <<"That nickname is already in use by another "
|
|
|
|
"occupant">>,
|
|
|
|
Err = xmpp:err_conflict(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(
|
|
|
|
jid:replace_resource(StateData#state.jid, Nick),
|
|
|
|
From, Packet, Err),
|
|
|
|
StateData;
|
|
|
|
{_, false, _} ->
|
|
|
|
ErrText = <<"That nickname is registered by another "
|
|
|
|
"person">>,
|
|
|
|
Err = xmpp:err_conflict(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(
|
|
|
|
jid:replace_resource(StateData#state.jid, Nick),
|
|
|
|
From, Packet, Err),
|
|
|
|
StateData;
|
|
|
|
_ ->
|
|
|
|
case is_initial_presence(From, StateData) of
|
|
|
|
true ->
|
|
|
|
subscriber_becomes_available(
|
|
|
|
From, Nick, Packet, StateData);
|
|
|
|
false ->
|
|
|
|
change_nick(From, Nick, StateData)
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
_NotNickChange ->
|
|
|
|
case is_initial_presence(From, StateData) of
|
|
|
|
true ->
|
|
|
|
subscriber_becomes_available(
|
|
|
|
From, Nick, Packet, StateData);
|
|
|
|
false ->
|
|
|
|
Stanza = maybe_strip_status_from_presence(
|
|
|
|
From, Packet, StateData),
|
|
|
|
NewState = add_user_presence(From, Stanza,
|
|
|
|
StateData),
|
|
|
|
send_new_presence(From, NewState, StateData),
|
|
|
|
NewState
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
do_process_presence(From, Nick, #presence{type = unavailable} = Packet,
|
|
|
|
StateData) ->
|
|
|
|
IsSubscriber = is_subscriber(From, 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 (?DICT):find(Nick, StateData#state.nicks) of
|
|
|
|
{ok, [_, _ | _]} -> ok;
|
|
|
|
_ -> send_new_presence(From, NewState, StateData)
|
|
|
|
end,
|
|
|
|
Reason = xmpp:get_text(NewPacket#presence.status),
|
|
|
|
remove_online_user(From, NewState, IsSubscriber, Reason);
|
|
|
|
do_process_presence(From, _Nick, #presence{type = error, lang = Lang} = Packet,
|
|
|
|
StateData) ->
|
|
|
|
ErrorText = <<"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().
|
2016-06-26 08:08:37 +02:00
|
|
|
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.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec subscriber_becomes_available(jid(), binary(), presence(),
|
|
|
|
state()) -> state().
|
2016-06-26 08:08:37 +02:00
|
|
|
subscriber_becomes_available(From, Nick, Packet, StateData) ->
|
|
|
|
Stanza = maybe_strip_status_from_presence(From, Packet, StateData),
|
|
|
|
State1 = add_user_presence(From, Stanza, StateData),
|
|
|
|
Aff = get_affiliation(From, State1),
|
|
|
|
Role = get_default_role(Aff, State1),
|
|
|
|
State2 = set_role(From, Role, State1),
|
|
|
|
State3 = set_nick(From, Nick, State2),
|
|
|
|
send_existing_presences(From, State3),
|
|
|
|
send_initial_presence(From, State3, StateData),
|
|
|
|
State3.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec close_room_if_temporary_and_empty(state()) -> fsm_transition().
|
2014-05-05 13:50:52 +02:00
|
|
|
close_room_if_temporary_and_empty(StateData1) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case not (StateData1#state.config)#config.persistent
|
2016-07-25 12:50:30 +02:00
|
|
|
andalso (?DICT):size(StateData1#state.users) == 0
|
2013-03-14 10:33:02 +01:00
|
|
|
of
|
|
|
|
true ->
|
|
|
|
?INFO_MSG("Destroyed MUC room ~s because it's temporary "
|
|
|
|
"and empty",
|
2015-11-24 16:44:13 +01:00
|
|
|
[jid:to_string(StateData1#state.jid)]),
|
2014-05-05 13:50:52 +02:00
|
|
|
add_to_log(room_existence, destroyed, StateData1),
|
2013-03-14 10:33:02 +01:00
|
|
|
{stop, normal, StateData1};
|
|
|
|
_ -> {next_state, normal_state, StateData1}
|
2007-06-25 18:43:42 +02:00
|
|
|
end.
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec is_user_online(jid(), state()) -> boolean().
|
2003-03-23 21:08:44 +01:00
|
|
|
is_user_online(JID, StateData) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LJID = jid:tolower(JID),
|
2013-03-14 10:33:02 +01:00
|
|
|
(?DICT):is_key(LJID, StateData#state.users).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec is_subscriber(jid(), state()) -> boolean().
|
2016-06-26 08:08:37 +02:00
|
|
|
is_subscriber(JID, StateData) ->
|
|
|
|
LJID = jid:tolower(JID),
|
|
|
|
case (?DICT):find(LJID, StateData#state.users) of
|
|
|
|
{ok, #user{is_subscriber = IsSubscriber}} ->
|
|
|
|
IsSubscriber;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2010-03-29 20:51:04 +02:00
|
|
|
%% Check if the user is occupant of the room, or at least is an admin or owner.
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec is_occupant_or_admin(jid(), state()) -> boolean().
|
2010-03-29 20:51:04 +02:00
|
|
|
is_occupant_or_admin(JID, StateData) ->
|
2010-03-29 20:51:24 +02:00
|
|
|
FAffiliation = get_affiliation(JID, StateData),
|
|
|
|
FRole = get_role(JID, StateData),
|
2013-03-14 10:33:02 +01:00
|
|
|
case FRole /= none orelse
|
2013-06-21 11:43:23 +02:00
|
|
|
FAffiliation == member orelse
|
2013-03-14 10:33:02 +01:00
|
|
|
FAffiliation == admin orelse FAffiliation == owner
|
|
|
|
of
|
|
|
|
true -> true;
|
|
|
|
_ -> false
|
2010-03-29 20:51:24 +02:00
|
|
|
end.
|
2009-12-01 21:00:15 +01:00
|
|
|
|
|
|
|
%%%
|
|
|
|
%%% Handle IQ queries of vCard
|
|
|
|
%%%
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec is_user_online_iq(binary(), jid(), state()) ->
|
|
|
|
{boolean(), binary(), jid()}.
|
2013-03-14 10:33:02 +01:00
|
|
|
is_user_online_iq(StanzaId, JID, StateData)
|
|
|
|
when JID#jid.lresource /= <<"">> ->
|
2009-12-01 21:00:15 +01:00
|
|
|
{is_user_online(JID, StateData), StanzaId, JID};
|
2013-03-14 10:33:02 +01:00
|
|
|
is_user_online_iq(StanzaId, JID, StateData)
|
|
|
|
when JID#jid.lresource == <<"">> ->
|
2009-12-01 21:00:15 +01:00
|
|
|
try stanzaid_unpack(StanzaId) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{OriginalId, Resource} ->
|
2016-07-25 12:50:30 +02:00
|
|
|
JIDWithResource = jid:replace_resource(JID, Resource),
|
2013-03-14 10:33:02 +01:00
|
|
|
{is_user_online(JIDWithResource, StateData), OriginalId,
|
|
|
|
JIDWithResource}
|
2009-12-01 21:00:15 +01:00
|
|
|
catch
|
2013-03-14 10:33:02 +01:00
|
|
|
_:_ -> {is_user_online(JID, StateData), StanzaId, JID}
|
2009-12-01 21:00:15 +01:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec handle_iq_vcard(jid(), binary(), iq()) -> {jid(), iq()}.
|
|
|
|
handle_iq_vcard(ToJID, NewId, #iq{type = Type, sub_els = SubEls} = IQ) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
ToBareJID = jid:remove_resource(ToJID),
|
2016-07-25 12:50:30 +02:00
|
|
|
case SubEls of
|
|
|
|
[SubEl] when Type == get, ToBareJID /= ToJID ->
|
|
|
|
case xmpp:get_ns(SubEl) of
|
|
|
|
?NS_VCARD ->
|
|
|
|
{ToBareJID, change_stanzaid(ToJID, IQ)};
|
|
|
|
_ ->
|
|
|
|
{ToJID, xmpp:set_id(IQ, NewId)}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
{ToJID, xmpp:set_id(IQ, NewId)}
|
|
|
|
end.
|
2009-12-01 21:00:15 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec stanzaid_pack(binary(), binary()) -> binary().
|
2009-12-01 21:00:15 +01:00
|
|
|
stanzaid_pack(OriginalId, Resource) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"berd",
|
|
|
|
(jlib:encode_base64(<<"ejab\000",
|
|
|
|
OriginalId/binary, "\000",
|
|
|
|
Resource/binary>>))/binary>>.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec stanzaid_unpack(binary()) -> {binary(), binary()}.
|
2013-03-14 10:33:02 +01:00
|
|
|
stanzaid_unpack(<<"berd", StanzaIdBase64/binary>>) ->
|
|
|
|
StanzaId = jlib:decode_base64(StanzaIdBase64),
|
|
|
|
[<<"ejab">>, OriginalId, Resource] =
|
|
|
|
str:tokens(StanzaId, <<"\000">>),
|
2009-12-01 21:00:15 +01:00
|
|
|
{OriginalId, Resource}.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec change_stanzaid(jid(), iq()) -> iq().
|
|
|
|
change_stanzaid(ToJID, #iq{id = PreviousId} = Packet) ->
|
2009-12-01 21:00:15 +01:00
|
|
|
NewId = stanzaid_pack(PreviousId, ToJID#jid.lresource),
|
2016-07-25 12:50:30 +02:00
|
|
|
xmpp:set_id(Packet, NewId).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2008-01-29 15:49:08 +01:00
|
|
|
%% Decide the fate of the message and its sender
|
|
|
|
%% Returns: continue_delivery | forget_message | {expulse_sender, Reason}
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec decide_fate_message(message(), jid(), state()) ->
|
|
|
|
continue_delivery | forget_message |
|
|
|
|
{expulse_sender, binary()}.
|
|
|
|
decide_fate_message(#message{type = error, error = Err},
|
|
|
|
From, StateData) ->
|
|
|
|
PD = case check_error_kick(Err) of
|
2013-03-14 10:33:02 +01:00
|
|
|
%% If this is an error stanza and its condition matches a criteria
|
|
|
|
true ->
|
|
|
|
Reason =
|
|
|
|
io_lib:format("This participant is considered a ghost "
|
|
|
|
"and is expulsed: ~s",
|
2015-11-24 16:44:13 +01:00
|
|
|
[jid:to_string(From)]),
|
2013-03-14 10:33:02 +01:00
|
|
|
{expulse_sender, Reason};
|
|
|
|
false -> continue_delivery
|
2008-01-29 19:37:45 +01:00
|
|
|
end,
|
|
|
|
case PD of
|
2013-03-14 10:33:02 +01:00
|
|
|
{expulse_sender, R} ->
|
|
|
|
case is_user_online(From, StateData) of
|
|
|
|
true -> {expulse_sender, R};
|
|
|
|
false -> forget_message
|
|
|
|
end;
|
|
|
|
Other -> Other
|
2008-01-29 15:49:08 +01:00
|
|
|
end;
|
2016-07-25 12:50:30 +02:00
|
|
|
decide_fate_message(_, _, _) -> continue_delivery.
|
2008-01-29 15:49:08 +01:00
|
|
|
|
|
|
|
%% Check if the elements of this error stanza indicate
|
|
|
|
%% that the sender is a dead participant.
|
|
|
|
%% If so, return true to kick the participant.
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec check_error_kick(error()) -> boolean().
|
|
|
|
check_error_kick(#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.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec get_error_condition(error()) -> string().
|
|
|
|
get_error_condition(#error{reason = Reason}) ->
|
|
|
|
case Reason of
|
|
|
|
#gone{} -> "gone";
|
|
|
|
#redirect{} -> "redirect";
|
|
|
|
Atom -> atom_to_list(Atom)
|
|
|
|
end;
|
|
|
|
get_error_condition(undefined) ->
|
|
|
|
"undefined".
|
2008-02-09 11:38:47 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec make_reason(stanza(), jid(), state(), binary()) -> binary().
|
2015-12-23 19:34:23 +01:00
|
|
|
make_reason(Packet, From, StateData, Reason1) ->
|
2015-12-29 11:11:50 +01:00
|
|
|
{ok, #user{nick = FromNick}} = (?DICT):find(jid:tolower(From), StateData#state.users),
|
2016-07-25 12:50:30 +02:00
|
|
|
Condition = get_error_condition(xmpp:get_error(Packet)),
|
2015-12-23 19:34:23 +01:00
|
|
|
iolist_to_binary(io_lib:format(Reason1, [FromNick, Condition])).
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec expulse_participant(stanza(), jid(), state(), binary()) ->
|
|
|
|
state().
|
2008-02-09 11:38:47 +01:00
|
|
|
expulse_participant(Packet, From, StateData, Reason1) ->
|
2016-06-26 08:08:37 +02:00
|
|
|
IsSubscriber = is_subscriber(From, StateData),
|
2015-12-23 19:34:23 +01:00
|
|
|
Reason2 = make_reason(Packet, From, StateData, Reason1),
|
2013-03-14 10:33:02 +01:00
|
|
|
NewState = add_user_presence_un(From,
|
2016-07-25 12:50:30 +02:00
|
|
|
#presence{type = unavailable,
|
|
|
|
status = xmpp:mk_text(Reason2)},
|
2013-03-14 10:33:02 +01:00
|
|
|
StateData),
|
2015-11-12 17:51:20 +01:00
|
|
|
send_new_presence(From, NewState, StateData),
|
2016-06-26 08:08:37 +02:00
|
|
|
remove_online_user(From, NewState, IsSubscriber).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec set_affiliation(jid(), affiliation(), state()) -> state().
|
2003-03-23 21:08:44 +01:00
|
|
|
set_affiliation(JID, Affiliation, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
set_affiliation(JID, Affiliation, StateData, <<"">>).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec set_affiliation(jid(), affiliation(), state(), binary()) -> state().
|
2011-09-05 12:53:01 +02:00
|
|
|
set_affiliation(JID, Affiliation, StateData, Reason) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LJID = jid:remove_resource(jid:tolower(JID)),
|
2005-05-04 01:07:14 +02:00
|
|
|
Affiliations = case Affiliation of
|
2013-03-14 10:33:02 +01:00
|
|
|
none ->
|
|
|
|
(?DICT):erase(LJID, StateData#state.affiliations);
|
|
|
|
_ ->
|
|
|
|
(?DICT):store(LJID, {Affiliation, Reason},
|
2005-05-04 01:07:14 +02:00
|
|
|
StateData#state.affiliations)
|
|
|
|
end,
|
|
|
|
StateData#state{affiliations = Affiliations}.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec get_affiliation(jid(), state()) -> affiliation().
|
2003-03-23 21:08:44 +01:00
|
|
|
get_affiliation(JID, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{_AccessRoute, _AccessCreate, AccessAdmin,
|
|
|
|
_AccessPersistent} =
|
|
|
|
StateData#state.access,
|
|
|
|
Res = case acl:match_rule(StateData#state.server_host,
|
|
|
|
AccessAdmin, JID)
|
|
|
|
of
|
|
|
|
allow -> owner;
|
2005-05-04 01:07:14 +02:00
|
|
|
_ ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LJID = jid:tolower(JID),
|
2013-03-14 10:33:02 +01:00
|
|
|
case (?DICT):find(LJID, StateData#state.affiliations) of
|
|
|
|
{ok, Affiliation} -> Affiliation;
|
|
|
|
_ ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LJID1 = jid:remove_resource(LJID),
|
2013-03-14 10:33:02 +01:00
|
|
|
case (?DICT):find(LJID1, StateData#state.affiliations)
|
|
|
|
of
|
|
|
|
{ok, Affiliation} -> Affiliation;
|
|
|
|
_ ->
|
|
|
|
LJID2 = setelement(1, LJID, <<"">>),
|
|
|
|
case (?DICT):find(LJID2,
|
|
|
|
StateData#state.affiliations)
|
|
|
|
of
|
|
|
|
{ok, Affiliation} -> Affiliation;
|
|
|
|
_ ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LJID3 = jid:remove_resource(LJID2),
|
2013-03-14 10:33:02 +01:00
|
|
|
case (?DICT):find(LJID3,
|
|
|
|
StateData#state.affiliations)
|
|
|
|
of
|
|
|
|
{ok, Affiliation} -> Affiliation;
|
|
|
|
_ -> none
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2005-05-04 01:07:14 +02:00
|
|
|
end
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
2005-05-04 01:07:14 +02:00
|
|
|
case Res of
|
2013-03-14 10:33:02 +01:00
|
|
|
{A, _Reason} -> A;
|
|
|
|
_ -> Res
|
2003-03-23 21:08:44 +01:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec get_service_affiliation(jid(), state()) -> owner | none.
|
2007-08-29 19:54:45 +02:00
|
|
|
get_service_affiliation(JID, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{_AccessRoute, _AccessCreate, AccessAdmin,
|
|
|
|
_AccessPersistent} =
|
2007-08-29 19:54:45 +02:00
|
|
|
StateData#state.access,
|
2013-03-14 10:33:02 +01:00
|
|
|
case acl:match_rule(StateData#state.server_host,
|
|
|
|
AccessAdmin, JID)
|
|
|
|
of
|
|
|
|
allow -> owner;
|
|
|
|
_ -> none
|
2007-08-29 19:54:45 +02:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec set_role(jid(), role(), state()) -> state().
|
2003-03-23 21:08:44 +01:00
|
|
|
set_role(JID, Role, StateData) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LJID = jid:tolower(JID),
|
2003-04-13 21:22:46 +02:00
|
|
|
LJIDs = case LJID of
|
2013-03-14 10:33:02 +01:00
|
|
|
{U, S, <<"">>} ->
|
|
|
|
(?DICT):fold(fun (J, _, Js) ->
|
|
|
|
case J of
|
|
|
|
{U, S, _} -> [J | Js];
|
|
|
|
_ -> Js
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
[], StateData#state.users);
|
2009-08-21 15:22:18 +02:00
|
|
|
_ ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case (?DICT):is_key(LJID, StateData#state.users) of
|
|
|
|
true -> [LJID];
|
|
|
|
_ -> []
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
{Users, Nicks} = case Role of
|
|
|
|
none ->
|
|
|
|
lists:foldl(fun (J, {Us, Ns}) ->
|
|
|
|
NewNs = case (?DICT):find(J, Us)
|
|
|
|
of
|
|
|
|
{ok,
|
|
|
|
#user{nick = Nick}} ->
|
|
|
|
(?DICT):erase(Nick,
|
|
|
|
Ns);
|
|
|
|
_ -> Ns
|
|
|
|
end,
|
|
|
|
{(?DICT):erase(J, Us), NewNs}
|
|
|
|
end,
|
|
|
|
{StateData#state.users,
|
|
|
|
StateData#state.nicks},
|
|
|
|
LJIDs);
|
|
|
|
_ ->
|
2016-06-26 08:08:37 +02:00
|
|
|
{lists:foldl(
|
|
|
|
fun (J, Us) ->
|
|
|
|
{ok, User} = (?DICT):find(J, Us),
|
|
|
|
if User#user.last_presence == undefined ->
|
|
|
|
Us;
|
|
|
|
true ->
|
|
|
|
(?DICT):store(J, User#user{role = Role}, Us)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
StateData#state.users, LJIDs),
|
2013-03-14 10:33:02 +01:00
|
|
|
StateData#state.nicks}
|
|
|
|
end,
|
2009-08-21 15:22:18 +02:00
|
|
|
StateData#state{users = Users, nicks = Nicks}.
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec get_role(jid(), state()) -> role().
|
2003-03-23 21:08:44 +01:00
|
|
|
get_role(JID, StateData) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LJID = jid:tolower(JID),
|
2013-03-14 10:33:02 +01:00
|
|
|
case (?DICT):find(LJID, StateData#state.users) of
|
|
|
|
{ok, #user{role = Role}} -> Role;
|
|
|
|
_ -> none
|
2003-03-23 21:08:44 +01:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec get_default_role(affiliation(), state()) -> role().
|
2003-03-23 21:08:44 +01:00
|
|
|
get_default_role(Affiliation, StateData) ->
|
|
|
|
case Affiliation of
|
2013-03-14 10:33:02 +01:00
|
|
|
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
|
2003-03-23 21:08:44 +01:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec is_visitor(jid(), state()) -> boolean().
|
2008-07-23 14:31:55 +02:00
|
|
|
is_visitor(Jid, StateData) ->
|
2008-08-24 01:17:17 +02:00
|
|
|
get_role(Jid, StateData) =:= visitor.
|
2008-07-23 14:31:55 +02:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec is_moderator(jid(), state()) -> boolean().
|
2011-07-11 17:33:26 +02:00
|
|
|
is_moderator(Jid, StateData) ->
|
|
|
|
get_role(Jid, StateData) =:= moderator.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec get_max_users(state()) -> non_neg_integer().
|
2007-08-29 19:54:45 +02:00
|
|
|
get_max_users(StateData) ->
|
|
|
|
MaxUsers = (StateData#state.config)#config.max_users,
|
|
|
|
ServiceMaxUsers = get_service_max_users(StateData),
|
2013-03-14 10:33:02 +01:00
|
|
|
if MaxUsers =< ServiceMaxUsers -> MaxUsers;
|
|
|
|
true -> ServiceMaxUsers
|
2007-08-29 19:54:45 +02:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec get_service_max_users(state()) -> pos_integer().
|
2007-08-29 19:54:45 +02:00
|
|
|
get_service_max_users(StateData) ->
|
|
|
|
gen_mod:get_module_opt(StateData#state.server_host,
|
2013-03-14 10:33:02 +01:00
|
|
|
mod_muc, max_users,
|
|
|
|
fun(I) when is_integer(I), I>0 -> I end,
|
|
|
|
?MAX_USERS_DEFAULT).
|
2007-08-29 19:54:45 +02:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec get_max_users_admin_threshold(state()) -> pos_integer().
|
2007-08-29 19:54:45 +02:00
|
|
|
get_max_users_admin_threshold(StateData) ->
|
|
|
|
gen_mod:get_module_opt(StateData#state.server_host,
|
2013-03-14 10:33:02 +01:00
|
|
|
mod_muc, max_users_admin_threshold,
|
|
|
|
fun(I) when is_integer(I), I>0 -> I end,
|
|
|
|
5).
|
2007-08-29 19:54:45 +02:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec get_user_activity(jid(), state()) -> #activity{}.
|
2007-09-01 23:05:04 +02:00
|
|
|
get_user_activity(JID, StateData) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
case treap:lookup(jid:tolower(JID),
|
2013-03-14 10:33:02 +01:00
|
|
|
StateData#state.activity)
|
|
|
|
of
|
|
|
|
{ok, _P, A} -> A;
|
|
|
|
error ->
|
|
|
|
MessageShaper =
|
|
|
|
shaper:new(gen_mod:get_module_opt(StateData#state.server_host,
|
|
|
|
mod_muc, user_message_shaper,
|
|
|
|
fun(A) when is_atom(A) -> A end,
|
|
|
|
none)),
|
|
|
|
PresenceShaper =
|
|
|
|
shaper:new(gen_mod:get_module_opt(StateData#state.server_host,
|
|
|
|
mod_muc, user_presence_shaper,
|
|
|
|
fun(A) when is_atom(A) -> A end,
|
|
|
|
none)),
|
|
|
|
#activity{message_shaper = MessageShaper,
|
|
|
|
presence_shaper = PresenceShaper}
|
2007-09-01 23:05:04 +02:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec store_user_activity(jid(), #activity{}, state()) -> state().
|
2008-11-28 17:06:39 +01:00
|
|
|
store_user_activity(JID, UserActivity, StateData) ->
|
|
|
|
MinMessageInterval =
|
2014-11-15 22:35:56 +01:00
|
|
|
trunc(gen_mod:get_module_opt(StateData#state.server_host,
|
|
|
|
mod_muc, min_message_interval,
|
|
|
|
fun(I) when is_number(I), I>=0 -> I end,
|
|
|
|
0)
|
|
|
|
* 1000),
|
2008-11-28 17:06:39 +01:00
|
|
|
MinPresenceInterval =
|
2014-11-15 22:35:56 +01:00
|
|
|
trunc(gen_mod:get_module_opt(StateData#state.server_host,
|
|
|
|
mod_muc, min_presence_interval,
|
|
|
|
fun(I) when is_number(I), I>=0 -> I end,
|
|
|
|
0)
|
|
|
|
* 1000),
|
2015-11-24 16:44:13 +01:00
|
|
|
Key = jid:tolower(JID),
|
2015-12-04 15:08:43 +01:00
|
|
|
Now = p1_time_compat:system_time(micro_seconds),
|
2013-03-14 10:33:02 +01:00
|
|
|
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} =
|
|
|
|
shaper:update(UserActivity#activity.message_shaper,
|
|
|
|
100000),
|
|
|
|
{_, PresenceShaperInterval} =
|
|
|
|
shaper:update(UserActivity#activity.presence_shaper,
|
|
|
|
100000),
|
|
|
|
Delay = lists:max([MessageShaperInterval,
|
|
|
|
PresenceShaperInterval,
|
2014-11-15 22:35:56 +01:00
|
|
|
MinMessageInterval,
|
|
|
|
MinPresenceInterval])
|
2013-03-14 10:33:02 +01:00
|
|
|
* 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,
|
2008-11-28 17:06:39 +01:00
|
|
|
StateData1.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec clean_treap(treap:treap(), integer()) -> treap:treap().
|
2008-11-28 17:06:39 +01:00
|
|
|
clean_treap(Treap, CleanPriority) ->
|
|
|
|
case treap:is_empty(Treap) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true -> Treap;
|
|
|
|
false ->
|
|
|
|
{_Key, Priority, _Value} = treap:get_root(Treap),
|
|
|
|
if Priority > CleanPriority ->
|
|
|
|
clean_treap(treap:delete_root(Treap), CleanPriority);
|
|
|
|
true -> Treap
|
|
|
|
end
|
2008-11-28 17:06:39 +01:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec prepare_room_queue(state()) -> state().
|
2007-09-01 23:05:04 +02:00
|
|
|
prepare_room_queue(StateData) ->
|
|
|
|
case queue:out(StateData#state.room_queue) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{{value, {message, From}}, _RoomQueue} ->
|
|
|
|
Activity = get_user_activity(From, StateData),
|
|
|
|
Packet = Activity#activity.message,
|
|
|
|
Size = element_size(Packet),
|
|
|
|
{RoomShaper, RoomShaperInterval} =
|
|
|
|
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} =
|
|
|
|
shaper:update(StateData#state.room_shaper, Size),
|
|
|
|
erlang:send_after(RoomShaperInterval, self(),
|
|
|
|
process_room_queue),
|
|
|
|
StateData#state{room_shaper = RoomShaper};
|
|
|
|
{empty, _} -> StateData
|
2007-09-01 23:05:04 +02:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec update_online_user(jid(), #user{}, state()) -> state().
|
2016-06-26 08:08:37 +02:00
|
|
|
update_online_user(JID, #user{nick = Nick, subscriptions = Nodes,
|
|
|
|
is_subscriber = IsSubscriber} = User, StateData) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LJID = jid:tolower(JID),
|
2016-06-26 08:08:37 +02:00
|
|
|
Nicks1 = case (?DICT):find(LJID, StateData#state.users) of
|
|
|
|
{ok, #user{nick = OldNick}} ->
|
|
|
|
case lists:delete(
|
|
|
|
LJID, ?DICT:fetch(OldNick, StateData#state.nicks)) of
|
|
|
|
[] ->
|
|
|
|
?DICT:erase(OldNick, StateData#state.nicks);
|
|
|
|
LJIDs ->
|
|
|
|
?DICT:store(OldNick, LJIDs, StateData#state.nicks)
|
|
|
|
end;
|
|
|
|
error ->
|
|
|
|
StateData#state.nicks
|
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
Nicks = (?DICT):update(Nick,
|
2016-06-26 08:08:37 +02:00
|
|
|
fun (LJIDs) -> [LJID|LJIDs -- [LJID]] end,
|
|
|
|
[LJID], Nicks1),
|
|
|
|
Users = (?DICT):update(LJID,
|
|
|
|
fun(U) ->
|
|
|
|
U#user{nick = Nick,
|
|
|
|
subscriptions = Nodes,
|
|
|
|
is_subscriber = IsSubscriber}
|
|
|
|
end, User, StateData#state.users),
|
|
|
|
NewStateData = StateData#state{users = Users, nicks = Nicks},
|
|
|
|
case {?DICT:find(LJID, StateData#state.users),
|
|
|
|
?DICT:find(LJID, NewStateData#state.users)} of
|
|
|
|
{{ok, #user{nick = Old}}, {ok, #user{nick = New}}} when Old /= New ->
|
|
|
|
send_nick_changing(JID, Old, NewStateData, true, true);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
NewStateData.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec add_online_user(jid(), binary(), role(), boolean(), [binary()], state()) -> state().
|
2016-06-26 08:08:37 +02:00
|
|
|
add_online_user(JID, Nick, Role, IsSubscriber, Nodes, StateData) ->
|
2007-12-03 11:47:42 +01:00
|
|
|
tab_add_online_user(JID, StateData),
|
2016-06-26 08:08:37 +02:00
|
|
|
User = #user{jid = JID, nick = Nick, role = Role,
|
|
|
|
is_subscriber = IsSubscriber, subscriptions = Nodes},
|
|
|
|
StateData1 = update_online_user(JID, User, StateData),
|
|
|
|
if IsSubscriber ->
|
|
|
|
store_room(StateData1);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
StateData1.
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec remove_online_user(jid(), state(), boolean()) -> state().
|
2016-06-26 08:08:37 +02:00
|
|
|
remove_online_user(JID, StateData, IsSubscriber) ->
|
|
|
|
remove_online_user(JID, StateData, IsSubscriber, <<"">>).
|
2006-03-14 05:26:15 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec remove_online_user(jid(), state(), boolean(), binary()) -> state().
|
2016-06-26 08:08:37 +02:00
|
|
|
remove_online_user(JID, StateData, _IsSubscriber = true, _Reason) ->
|
|
|
|
LJID = jid:tolower(JID),
|
|
|
|
Users = case (?DICT):find(LJID, StateData#state.users) of
|
|
|
|
{ok, U} ->
|
|
|
|
(?DICT):store(LJID, U#user{last_presence = undefined},
|
|
|
|
StateData#state.users);
|
|
|
|
error ->
|
|
|
|
StateData#state.users
|
|
|
|
end,
|
|
|
|
StateData#state{users = Users};
|
|
|
|
remove_online_user(JID, StateData, _IsSubscriber, Reason) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LJID = jid:tolower(JID),
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, #user{nick = Nick}} = (?DICT):find(LJID,
|
|
|
|
StateData#state.users),
|
2006-03-14 05:26:15 +01:00
|
|
|
add_to_log(leave, {Nick, Reason}, StateData),
|
2007-12-03 11:47:42 +01:00
|
|
|
tab_remove_online_user(JID, StateData),
|
2013-03-14 10:33:02 +01:00
|
|
|
Users = (?DICT):erase(LJID, StateData#state.users),
|
|
|
|
Nicks = case (?DICT):find(Nick, StateData#state.nicks)
|
|
|
|
of
|
|
|
|
{ok, [LJID]} ->
|
|
|
|
(?DICT):erase(Nick, StateData#state.nicks);
|
|
|
|
{ok, U} ->
|
|
|
|
(?DICT):store(Nick, U -- [LJID], StateData#state.nicks);
|
|
|
|
error -> StateData#state.nicks
|
2009-08-21 15:22:18 +02:00
|
|
|
end,
|
2011-09-26 10:13:40 +02:00
|
|
|
StateData#state{users = Users, nicks = Nicks}.
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-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().
|
2003-03-23 21:08:44 +01:00
|
|
|
add_user_presence(JID, Presence, StateData) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LJID = jid:tolower(JID),
|
2003-03-23 21:08:44 +01:00
|
|
|
FPresence = filter_presence(Presence),
|
2013-03-14 10:33:02 +01:00
|
|
|
Users = (?DICT):update(LJID,
|
|
|
|
fun (#user{} = User) ->
|
|
|
|
User#user{last_presence = FPresence}
|
|
|
|
end,
|
|
|
|
StateData#state.users),
|
2003-03-23 21:08:44 +01:00
|
|
|
StateData#state{users = Users}.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec add_user_presence_un(jid(), presence(), state()) -> state().
|
2003-03-23 21:08:44 +01:00
|
|
|
add_user_presence_un(JID, Presence, StateData) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LJID = jid:tolower(JID),
|
2003-03-23 21:08:44 +01:00
|
|
|
FPresence = filter_presence(Presence),
|
2013-03-14 10:33:02 +01:00
|
|
|
Users = (?DICT):update(LJID,
|
|
|
|
fun (#user{} = User) ->
|
|
|
|
User#user{last_presence = FPresence,
|
|
|
|
role = none}
|
|
|
|
end,
|
|
|
|
StateData#state.users),
|
2003-03-23 21:08:44 +01:00
|
|
|
StateData#state{users = Users}.
|
|
|
|
|
2011-08-23 21:53:30 +02:00
|
|
|
%% Find and return a list of the full JIDs of the users of Nick.
|
|
|
|
%% Return jid record.
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec find_jids_by_nick(binary(), state()) -> [jid()].
|
2011-08-23 21:53:30 +02:00
|
|
|
find_jids_by_nick(Nick, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case (?DICT):find(Nick, StateData#state.nicks) of
|
2015-11-24 16:44:13 +01:00
|
|
|
{ok, [User]} -> [jid:make(User)];
|
|
|
|
{ok, Users} -> [jid:make(LJID) || LJID <- Users];
|
2016-07-25 12:50:30 +02:00
|
|
|
error -> []
|
2011-08-23 21:53:30 +02:00
|
|
|
end.
|
|
|
|
|
2009-08-21 15:22:18 +02:00
|
|
|
%% Find and return the full JID of the user of Nick with
|
|
|
|
%% highest-priority presence. Return jid record.
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec find_jid_by_nick(binary(), state()) -> jid() | false.
|
2003-03-23 21:08:44 +01:00
|
|
|
find_jid_by_nick(Nick, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case (?DICT):find(Nick, StateData#state.nicks) of
|
2015-11-24 16:44:13 +01:00
|
|
|
{ok, [User]} -> jid:make(User);
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, [FirstUser | Users]} ->
|
|
|
|
#user{last_presence = FirstPresence} =
|
|
|
|
(?DICT):fetch(FirstUser, StateData#state.users),
|
|
|
|
{LJID, _} = lists:foldl(fun (Compare,
|
|
|
|
{HighestUser, HighestPresence}) ->
|
|
|
|
#user{last_presence = P1} =
|
|
|
|
(?DICT):fetch(Compare,
|
|
|
|
StateData#state.users),
|
|
|
|
case higher_presence(P1,
|
|
|
|
HighestPresence)
|
|
|
|
of
|
|
|
|
true -> {Compare, P1};
|
|
|
|
false ->
|
|
|
|
{HighestUser, HighestPresence}
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
{FirstUser, FirstPresence}, Users),
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:make(LJID);
|
2013-03-14 10:33:02 +01:00
|
|
|
error -> false
|
2009-08-21 15:22:18 +02:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec higher_presence(undefined | presence(),
|
|
|
|
undefined | presence()) -> boolean().
|
2016-06-26 08:08:37 +02:00
|
|
|
higher_presence(Pres1, Pres2) when Pres1 /= undefined, Pres2 /= undefined ->
|
2009-08-21 15:22:18 +02:00
|
|
|
Pri1 = get_priority_from_presence(Pres1),
|
|
|
|
Pri2 = get_priority_from_presence(Pres2),
|
2016-06-26 08:08:37 +02:00
|
|
|
Pri1 > Pri2;
|
|
|
|
higher_presence(Pres1, Pres2) ->
|
|
|
|
Pres1 > Pres2.
|
2009-08-21 15:22:18 +02:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec get_priority_from_presence(presence()) -> integer().
|
|
|
|
get_priority_from_presence(#presence{priority = Prio}) ->
|
|
|
|
case Prio of
|
|
|
|
undefined -> 0;
|
|
|
|
_ -> Prio
|
2009-08-21 15:22:18 +02:00
|
|
|
end.
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec find_nick_by_jid(jid(), state()) -> binary().
|
|
|
|
find_nick_by_jid(JID, StateData) ->
|
|
|
|
LJID = jid:tolower(JID),
|
|
|
|
{ok, #user{nick = Nick}} = (?DICT):find(LJID, StateData#state.users),
|
2013-03-14 10:33:02 +01:00
|
|
|
Nick.
|
2011-07-28 19:40:46 +02:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec is_nick_change(jid(), binary(), state()) -> boolean().
|
2003-03-23 21:08:44 +01:00
|
|
|
is_nick_change(JID, Nick, StateData) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LJID = jid:tolower(JID),
|
2003-03-23 21:08:44 +01:00
|
|
|
case Nick of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"">> -> false;
|
|
|
|
_ ->
|
|
|
|
{ok, #user{nick = OldNick}} = (?DICT):find(LJID,
|
|
|
|
StateData#state.users),
|
|
|
|
Nick /= OldNick
|
2003-03-23 21:08:44 +01:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec nick_collision(jid(), binary(), state()) -> boolean().
|
2009-08-21 15:22:18 +02:00
|
|
|
nick_collision(User, Nick, StateData) ->
|
|
|
|
UserOfNick = find_jid_by_nick(Nick, StateData),
|
2015-10-07 00:06:58 +02:00
|
|
|
(UserOfNick /= false andalso
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:remove_resource(jid:tolower(UserOfNick))
|
|
|
|
/= jid:remove_resource(jid:tolower(User))).
|
2009-08-21 15:22:18 +02:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec add_new_user(jid(), binary(), presence() | iq(), state()) ->
|
|
|
|
state() |
|
|
|
|
{error, error()} |
|
|
|
|
{ignore, state()} |
|
|
|
|
{result, xmpp_element(), state()}.
|
|
|
|
add_new_user(From, Nick, Packet, StateData) ->
|
|
|
|
Lang = xmpp:get_lang(Packet),
|
2016-06-26 08:08:37 +02:00
|
|
|
UserRoomJID = jid:replace_resource(StateData#state.jid, Nick),
|
2007-08-29 19:54:45 +02:00
|
|
|
MaxUsers = get_max_users(StateData),
|
2013-03-14 10:33:02 +01:00
|
|
|
MaxAdminUsers = MaxUsers +
|
|
|
|
get_max_users_admin_threshold(StateData),
|
|
|
|
NUsers = dict:fold(fun (_, _, Acc) -> Acc + 1 end, 0,
|
2007-08-29 19:54:45 +02:00
|
|
|
StateData#state.users),
|
2007-05-16 12:13:04 +02:00
|
|
|
Affiliation = get_affiliation(From, StateData),
|
2013-03-14 10:33:02 +01:00
|
|
|
ServiceAffiliation = get_service_affiliation(From,
|
|
|
|
StateData),
|
2007-12-03 11:47:42 +01:00
|
|
|
NConferences = tab_count_user(From),
|
2013-03-14 10:33:02 +01:00
|
|
|
MaxConferences =
|
|
|
|
gen_mod:get_module_opt(StateData#state.server_host,
|
|
|
|
mod_muc, max_user_conferences,
|
|
|
|
fun(I) when is_integer(I), I>0 -> I end,
|
|
|
|
10),
|
2009-08-21 15:22:18 +02:00
|
|
|
Collision = nick_collision(From, Nick, StateData),
|
2016-07-25 12:50:30 +02:00
|
|
|
IsSubscribeRequest = not is_record(Packet, presence),
|
2007-12-03 11:47:42 +01:00
|
|
|
case {(ServiceAffiliation == owner orelse
|
2016-05-25 12:44:05 +02:00
|
|
|
((Affiliation == admin orelse Affiliation == owner)
|
|
|
|
andalso NUsers < MaxAdminUsers)
|
2013-03-14 10:33:02 +01:00
|
|
|
orelse NUsers < MaxUsers)
|
|
|
|
andalso NConferences < MaxConferences,
|
2009-08-21 15:22:18 +02:00
|
|
|
Collision,
|
2013-03-14 10:33:02 +01:00
|
|
|
mod_muc:can_use_nick(StateData#state.server_host,
|
|
|
|
StateData#state.host, From, Nick),
|
|
|
|
get_default_role(Affiliation, StateData)}
|
|
|
|
of
|
2016-04-05 12:09:44 +02:00
|
|
|
{false, _, _, _} when NUsers >= MaxUsers orelse NUsers >= MaxAdminUsers ->
|
|
|
|
Txt = <<"Too many users in this conference">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_resource_constraint(Txt, Lang),
|
|
|
|
ErrPacket = xmpp:make_error(Packet, Err),
|
2016-06-26 08:08:37 +02:00
|
|
|
if not IsSubscribeRequest ->
|
|
|
|
ejabberd_router:route(UserRoomJID, From, ErrPacket),
|
|
|
|
StateData;
|
|
|
|
true ->
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, Err}
|
2016-06-26 08:08:37 +02:00
|
|
|
end;
|
2016-04-05 12:09:44 +02:00
|
|
|
{false, _, _, _} when NConferences >= MaxConferences ->
|
|
|
|
Txt = <<"You have joined too many conferences">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_resource_constraint(Txt, Lang),
|
|
|
|
ErrPacket = xmpp:make_error(Packet, Err),
|
2016-06-26 08:08:37 +02:00
|
|
|
if not IsSubscribeRequest ->
|
|
|
|
ejabberd_router:route(UserRoomJID, From, ErrPacket),
|
|
|
|
StateData;
|
|
|
|
true ->
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, Err}
|
2016-06-26 08:08:37 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
{false, _, _, _} ->
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_service_unavailable(),
|
|
|
|
ErrPacket = xmpp:make_error(Packet, Err),
|
2016-06-26 08:08:37 +02:00
|
|
|
if not IsSubscribeRequest ->
|
|
|
|
ejabberd_router:route(UserRoomJID, From, ErrPacket),
|
|
|
|
StateData;
|
|
|
|
true ->
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, Err}
|
2016-06-26 08:08:37 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
{_, _, _, none} ->
|
2016-06-26 08:08:37 +02:00
|
|
|
Err = case Affiliation of
|
|
|
|
outcast ->
|
|
|
|
ErrText = <<"You have been banned from this room">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
xmpp:err_forbidden(ErrText, Lang);
|
2016-06-26 08:08:37 +02:00
|
|
|
_ ->
|
|
|
|
ErrText = <<"Membership is required to enter this room">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
xmpp:err_registration_required(ErrText, Lang)
|
2016-06-26 08:08:37 +02:00
|
|
|
end,
|
2016-07-25 12:50:30 +02:00
|
|
|
ErrPacket = xmpp:make_error(Packet, Err),
|
2016-06-26 08:08:37 +02:00
|
|
|
if not IsSubscribeRequest ->
|
|
|
|
ejabberd_router:route(UserRoomJID, From, ErrPacket),
|
|
|
|
StateData;
|
|
|
|
true ->
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, Err}
|
2016-06-26 08:08:37 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
{_, true, _, _} ->
|
|
|
|
ErrText = <<"That nickname is already in use by another occupant">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_conflict(ErrText, Lang),
|
|
|
|
ErrPacket = xmpp:make_error(Packet, Err),
|
2016-06-26 08:08:37 +02:00
|
|
|
if not IsSubscribeRequest ->
|
|
|
|
ejabberd_router:route(UserRoomJID, From, ErrPacket),
|
|
|
|
StateData;
|
|
|
|
true ->
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, Err}
|
2016-06-26 08:08:37 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
{_, _, false, _} ->
|
|
|
|
ErrText = <<"That nickname is registered by another person">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_conflict(ErrText, Lang),
|
|
|
|
ErrPacket = xmpp:make_error(Packet, Err),
|
2016-06-26 08:08:37 +02:00
|
|
|
if not IsSubscribeRequest ->
|
|
|
|
ejabberd_router:route(UserRoomJID, From, ErrPacket),
|
|
|
|
StateData;
|
|
|
|
true ->
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, Err}
|
2016-06-26 08:08:37 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
{_, _, _, Role} ->
|
|
|
|
case check_password(ServiceAffiliation, Affiliation,
|
2016-07-25 12:50:30 +02:00
|
|
|
Packet, From, StateData)
|
2013-03-14 10:33:02 +01:00
|
|
|
of
|
|
|
|
true ->
|
2016-06-26 08:08:37 +02:00
|
|
|
Nodes = get_subscription_nodes(Packet),
|
|
|
|
NewStateData =
|
|
|
|
if not IsSubscribeRequest ->
|
|
|
|
NewState = add_user_presence(
|
|
|
|
From, Packet,
|
|
|
|
add_online_user(From, Nick, Role,
|
|
|
|
IsSubscribeRequest,
|
|
|
|
Nodes, StateData)),
|
|
|
|
send_existing_presences(From, NewState),
|
|
|
|
send_initial_presence(From, NewState, StateData),
|
2016-07-25 12:50:30 +02:00
|
|
|
Shift = count_stanza_shift(Nick, Packet, NewState),
|
2016-06-26 08:08:37 +02:00
|
|
|
case send_history(From, Shift, NewState) of
|
|
|
|
true -> ok;
|
|
|
|
_ -> send_subject(From, StateData)
|
|
|
|
end,
|
|
|
|
NewState;
|
|
|
|
true ->
|
|
|
|
add_online_user(From, Nick, none,
|
|
|
|
IsSubscribeRequest,
|
|
|
|
Nodes, StateData)
|
|
|
|
end,
|
|
|
|
ResultState =
|
|
|
|
case NewStateData#state.just_created of
|
|
|
|
true ->
|
|
|
|
NewStateData#state{just_created = false};
|
|
|
|
false ->
|
|
|
|
Robots = (?DICT):erase(From, StateData#state.robots),
|
|
|
|
NewStateData#state{robots = Robots}
|
|
|
|
end,
|
|
|
|
if not IsSubscribeRequest -> ResultState;
|
2016-07-25 12:50:30 +02:00
|
|
|
true -> {result, subscribe_result(Packet), ResultState}
|
2016-06-26 08:08:37 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
nopass ->
|
|
|
|
ErrText = <<"A password is required to enter this room">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_not_authorized(ErrText, Lang),
|
|
|
|
ErrPacket = xmpp:make_error(Packet, Err),
|
2016-06-26 08:08:37 +02:00
|
|
|
if not IsSubscribeRequest ->
|
|
|
|
ejabberd_router:route(UserRoomJID, From, ErrPacket),
|
|
|
|
StateData;
|
|
|
|
true ->
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, Err}
|
2016-06-26 08:08:37 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
captcha_required ->
|
2016-07-25 12:50:30 +02:00
|
|
|
SID = xmpp:get_id(Packet),
|
2013-03-14 10:33:02 +01:00
|
|
|
RoomJID = StateData#state.jid,
|
2015-11-24 16:44:13 +01:00
|
|
|
To = jid:replace_resource(RoomJID, Nick),
|
2013-03-14 10:33:02 +01:00
|
|
|
Limiter = {From#jid.luser, From#jid.lserver},
|
|
|
|
case ejabberd_captcha:create_captcha(SID, RoomJID, To,
|
|
|
|
Lang, Limiter, From)
|
2016-06-26 08:08:37 +02:00
|
|
|
of
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, ID, CaptchaEls} ->
|
2016-07-25 12:50:30 +02:00
|
|
|
MsgPkt = #message{id = ID, sub_els = CaptchaEls},
|
2013-03-14 10:33:02 +01:00
|
|
|
Robots = (?DICT):store(From, {Nick, Packet},
|
|
|
|
StateData#state.robots),
|
|
|
|
ejabberd_router:route(RoomJID, From, MsgPkt),
|
2016-06-26 08:08:37 +02:00
|
|
|
NewState = StateData#state{robots = Robots},
|
|
|
|
if not IsSubscribeRequest ->
|
|
|
|
NewState;
|
|
|
|
true ->
|
|
|
|
{ignore, NewState}
|
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
{error, limit} ->
|
|
|
|
ErrText = <<"Too many CAPTCHA requests">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_resource_constraint(ErrText, Lang),
|
|
|
|
ErrPacket = xmpp:make_error(Packet, Err),
|
2016-06-26 08:08:37 +02:00
|
|
|
if not IsSubscribeRequest ->
|
|
|
|
ejabberd_router:route(UserRoomJID, From, ErrPacket),
|
|
|
|
StateData;
|
|
|
|
true ->
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, Err}
|
2016-06-26 08:08:37 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
_ ->
|
|
|
|
ErrText = <<"Unable to generate a CAPTCHA">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_internal_server_error(ErrText, Lang),
|
|
|
|
ErrPacket = xmpp:make_error(Packet, Err),
|
2016-06-26 08:08:37 +02:00
|
|
|
if not IsSubscribeRequest ->
|
|
|
|
ejabberd_router:route(UserRoomJID, From, ErrPacket),
|
|
|
|
StateData;
|
|
|
|
true ->
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, Err}
|
2016-06-26 08:08:37 +02:00
|
|
|
end
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
ErrText = <<"Incorrect password">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_not_authorized(ErrText, Lang),
|
|
|
|
ErrPacket = xmpp:make_error(Packet, Err),
|
2016-06-26 08:08:37 +02:00
|
|
|
if not IsSubscribeRequest ->
|
|
|
|
ejabberd_router:route(UserRoomJID, From, ErrPacket),
|
|
|
|
StateData;
|
|
|
|
true ->
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, Err}
|
2016-06-26 08:08:37 +02:00
|
|
|
end
|
2013-03-14 10:33:02 +01:00
|
|
|
end
|
2003-08-15 21:17:12 +02:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec check_password(affiliation(), affiliation(),
|
|
|
|
stanza(), jid(), state()) -> boolean() | nopass.
|
|
|
|
check_password(owner, _Affiliation, _Packet, _From,
|
2013-03-14 10:33:02 +01:00
|
|
|
_StateData) ->
|
2009-02-16 16:57:02 +01:00
|
|
|
%% Don't check pass if user is owner in MUC service (access_admin option)
|
2003-08-15 21:17:12 +02:00
|
|
|
true;
|
2016-07-25 12:50:30 +02:00
|
|
|
check_password(_ServiceAffiliation, Affiliation, Packet,
|
2013-03-14 10:33:02 +01:00
|
|
|
From, StateData) ->
|
|
|
|
case (StateData#state.config)#config.password_protected
|
|
|
|
of
|
|
|
|
false -> check_captcha(Affiliation, From, StateData);
|
|
|
|
true ->
|
2016-07-25 12:50:30 +02:00
|
|
|
Pass = extract_password(Packet),
|
2013-03-14 10:33:02 +01:00
|
|
|
case Pass of
|
|
|
|
false -> nopass;
|
|
|
|
_ ->
|
|
|
|
case (StateData#state.config)#config.password of
|
|
|
|
Pass -> true;
|
|
|
|
_ -> false
|
|
|
|
end
|
|
|
|
end
|
2003-08-15 21:17:12 +02:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec check_captcha(affiliation(), jid(), state()) -> true | captcha_required.
|
2009-03-13 17:01:46 +01:00
|
|
|
check_captcha(Affiliation, From, StateData) ->
|
2009-05-26 13:53:58 +02:00
|
|
|
case (StateData#state.config)#config.captcha_protected
|
2013-03-14 10:33:02 +01:00
|
|
|
andalso ejabberd_captcha:is_feature_available()
|
|
|
|
of
|
|
|
|
true when Affiliation == none ->
|
|
|
|
case (?DICT):find(From, StateData#state.robots) of
|
|
|
|
{ok, 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
|
2009-03-13 17:01:46 +01:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec extract_password(stanza()) -> binary() | false.
|
|
|
|
extract_password(Packet) ->
|
|
|
|
case xmpp:get_subtag(Packet, #muc{}) of
|
|
|
|
#muc{password = Password} when is_binary(Password) ->
|
|
|
|
Password;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
2003-08-15 21:17:12 +02:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec count_stanza_shift(binary(), stanza(), state()) -> non_neg_integer().
|
|
|
|
count_stanza_shift(Nick, Packet, StateData) ->
|
|
|
|
case xmpp:get_subtag(Packet, #muc_history{}) of
|
|
|
|
#muc_history{since = Since,
|
|
|
|
seconds = Seconds,
|
|
|
|
maxstanzas = MaxStanzas,
|
|
|
|
maxchars = MaxChars} ->
|
|
|
|
HL = lqueue_to_list(StateData#state.history),
|
|
|
|
Shift0 = case Since of
|
|
|
|
undefined -> 0;
|
|
|
|
_ ->
|
|
|
|
Sin = calendar:datetime_to_gregorian_seconds(
|
|
|
|
calendar:now_to_datetime(Since)),
|
|
|
|
count_seconds_shift(Sin, HL)
|
|
|
|
end,
|
|
|
|
Shift1 = case Seconds of
|
|
|
|
undefined -> 0;
|
|
|
|
_ ->
|
|
|
|
Sec = calendar:datetime_to_gregorian_seconds(
|
|
|
|
calendar:universal_time()) - Seconds,
|
|
|
|
count_seconds_shift(Sec, HL)
|
|
|
|
end,
|
|
|
|
Shift2 = case MaxStanzas of
|
|
|
|
undefined -> 0;
|
|
|
|
_ -> count_maxstanzas_shift(MaxStanzas, HL)
|
|
|
|
end,
|
|
|
|
Shift3 = case MaxChars of
|
|
|
|
undefined -> 0;
|
|
|
|
_ -> count_maxchars_shift(Nick, MaxChars, HL)
|
|
|
|
end,
|
|
|
|
lists:max([Shift0, Shift1, Shift2, Shift3]);
|
|
|
|
false ->
|
|
|
|
0
|
|
|
|
end.
|
2004-02-15 21:10:40 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec count_seconds_shift(non_neg_integer(),
|
|
|
|
[history_element()]) -> non_neg_integer().
|
2004-02-15 21:10:40 +01:00
|
|
|
count_seconds_shift(Seconds, HistoryList) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:sum(lists:map(fun ({_Nick, _Packet, _HaveSubject,
|
|
|
|
TimeStamp, _Size}) ->
|
|
|
|
T =
|
|
|
|
calendar:datetime_to_gregorian_seconds(TimeStamp),
|
|
|
|
if T < Seconds -> 1;
|
|
|
|
true -> 0
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
HistoryList)).
|
2003-08-15 21:17:12 +02:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec count_maxstanzas_shift(non_neg_integer(),
|
|
|
|
[history_element()]) -> non_neg_integer().
|
2004-02-15 21:10:40 +01:00
|
|
|
count_maxstanzas_shift(MaxStanzas, HistoryList) ->
|
|
|
|
S = length(HistoryList) - MaxStanzas,
|
2013-03-14 10:33:02 +01:00
|
|
|
if S =< 0 -> 0;
|
|
|
|
true -> S
|
2004-02-15 21:10:40 +01:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec count_maxchars_shift(binary(), non_neg_integer(),
|
|
|
|
[history_element()]) -> integer().
|
2004-02-15 21:10:40 +01:00
|
|
|
count_maxchars_shift(Nick, MaxSize, HistoryList) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
NLen = byte_size(Nick) + 1,
|
|
|
|
Sizes = lists:map(fun ({_Nick, _Packet, _HaveSubject,
|
|
|
|
_TimeStamp, Size}) ->
|
|
|
|
Size + NLen
|
|
|
|
end,
|
|
|
|
HistoryList),
|
2004-02-15 21:10:40 +01:00
|
|
|
calc_shift(MaxSize, Sizes).
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec calc_shift(non_neg_integer(), [non_neg_integer()]) -> integer().
|
2004-02-15 21:10:40 +01:00
|
|
|
calc_shift(MaxSize, Sizes) ->
|
|
|
|
Total = lists:sum(Sizes),
|
|
|
|
calc_shift(MaxSize, Total, 0, Sizes).
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec calc_shift(non_neg_integer(), integer(), integer(),
|
|
|
|
[non_neg_integer()]) -> integer().
|
2013-03-14 10:33:02 +01:00
|
|
|
calc_shift(_MaxSize, _Size, Shift, []) -> Shift;
|
2004-02-15 21:10:40 +01:00
|
|
|
calc_shift(MaxSize, Size, Shift, [S | TSizes]) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
if MaxSize >= Size -> Shift;
|
|
|
|
true -> calc_shift(MaxSize, Size - S, Shift + 1, TSizes)
|
2004-02-15 21:10:40 +01:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec is_room_overcrowded(state()) -> boolean().
|
2015-05-08 13:09:13 +02:00
|
|
|
is_room_overcrowded(StateData) ->
|
|
|
|
MaxUsersPresence = gen_mod:get_module_opt(StateData#state.server_host,
|
|
|
|
mod_muc, max_users_presence,
|
|
|
|
fun(MUP) when is_integer(MUP) -> MUP end,
|
|
|
|
?DEFAULT_MAX_USERS_PRESENCE),
|
|
|
|
(?DICT):size(StateData#state.users) > MaxUsersPresence.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec presence_broadcast_allowed(jid(), state()) -> boolean().
|
2015-11-12 17:51:20 +01:00
|
|
|
presence_broadcast_allowed(JID, StateData) ->
|
|
|
|
Role = get_role(JID, StateData),
|
|
|
|
lists:member(Role, (StateData#state.config)#config.presence_broadcast).
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec is_initial_presence(jid(), state()) -> boolean().
|
2016-06-26 08:08:37 +02:00
|
|
|
is_initial_presence(From, StateData) ->
|
|
|
|
LJID = jid:tolower(From),
|
|
|
|
case (?DICT):find(LJID, StateData#state.users) of
|
|
|
|
{ok, #user{last_presence = Pres}} when Pres /= undefined ->
|
|
|
|
false;
|
|
|
|
_ ->
|
|
|
|
true
|
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec send_initial_presence(jid(), state(), state()) -> ok.
|
2016-02-08 20:10:20 +01:00
|
|
|
send_initial_presence(NJID, StateData, OldStateData) ->
|
|
|
|
send_new_presence1(NJID, <<"">>, true, StateData, OldStateData).
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec send_update_presence(jid(), state(), state()) -> ok.
|
2015-12-08 19:24:39 +01:00
|
|
|
send_update_presence(JID, StateData, OldStateData) ->
|
|
|
|
send_update_presence(JID, <<"">>, StateData, OldStateData).
|
2008-07-30 20:24:08 +02:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec send_update_presence(jid(), binary(), state(), state()) -> ok.
|
2015-12-08 19:24:39 +01:00
|
|
|
send_update_presence(JID, Reason, StateData, OldStateData) ->
|
2015-05-08 13:09:13 +02:00
|
|
|
case is_room_overcrowded(StateData) of
|
|
|
|
true -> ok;
|
2015-12-08 19:24:39 +01:00
|
|
|
false -> send_update_presence1(JID, Reason, StateData, OldStateData)
|
2015-05-08 13:09:13 +02:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec send_update_presence1(jid(), binary(), state(), state()) -> ok.
|
2015-12-08 19:24:39 +01:00
|
|
|
send_update_presence1(JID, Reason, StateData, OldStateData) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LJID = jid:tolower(JID),
|
2003-04-13 21:22:46 +02:00
|
|
|
LJIDs = case LJID of
|
2013-03-14 10:33:02 +01:00
|
|
|
{U, S, <<"">>} ->
|
|
|
|
(?DICT):fold(fun (J, _, Js) ->
|
|
|
|
case J of
|
|
|
|
{U, S, _} -> [J | Js];
|
|
|
|
_ -> Js
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
[], StateData#state.users);
|
|
|
|
_ ->
|
|
|
|
case (?DICT):is_key(LJID, StateData#state.users) of
|
|
|
|
true -> [LJID];
|
|
|
|
_ -> []
|
|
|
|
end
|
2003-04-13 21:22:46 +02:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:foreach(fun (J) ->
|
2016-02-08 20:10:20 +01:00
|
|
|
send_new_presence1(J, Reason, false, StateData,
|
|
|
|
OldStateData)
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
|
|
|
LJIDs).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec send_new_presence(jid(), state(), state()) -> ok.
|
2015-11-12 17:51:20 +01:00
|
|
|
send_new_presence(NJID, StateData, OldStateData) ->
|
2016-02-08 20:10:20 +01:00
|
|
|
send_new_presence(NJID, <<"">>, false, StateData, OldStateData).
|
2008-07-30 20:24:08 +02:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec send_new_presence(jid(), binary(), state(), state()) -> ok.
|
2015-11-12 17:51:20 +01:00
|
|
|
send_new_presence(NJID, Reason, StateData, OldStateData) ->
|
2016-02-08 20:10:20 +01:00
|
|
|
send_new_presence(NJID, Reason, false, StateData, OldStateData).
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec send_new_presence(jid(), binary(), boolean(), state(), state()) -> ok.
|
2016-02-08 20:10:20 +01:00
|
|
|
send_new_presence(NJID, Reason, IsInitialPresence, StateData, OldStateData) ->
|
2015-12-08 19:24:39 +01:00
|
|
|
case is_room_overcrowded(StateData) of
|
2015-05-08 13:09:13 +02:00
|
|
|
true -> ok;
|
2016-02-08 20:10:20 +01:00
|
|
|
false -> send_new_presence1(NJID, Reason, IsInitialPresence, StateData,
|
|
|
|
OldStateData)
|
2015-05-08 13:09:13 +02:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec is_ra_changed(jid() | ljid(), boolean(), state(), state()) -> boolean().
|
2016-06-26 08:08:37 +02:00
|
|
|
is_ra_changed(_, _IsInitialPresence = true, _, _) ->
|
|
|
|
false;
|
|
|
|
is_ra_changed(LJID, _IsInitialPresence = false, NewStateData, OldStateData) ->
|
|
|
|
JID = case LJID of
|
|
|
|
#jid{} -> LJID;
|
|
|
|
_ -> jid:make(LJID)
|
|
|
|
end,
|
|
|
|
NewRole = get_role(LJID, NewStateData),
|
|
|
|
NewAff = get_affiliation(JID, NewStateData),
|
|
|
|
OldRole = get_role(LJID, OldStateData),
|
|
|
|
OldAff = get_affiliation(JID, OldStateData),
|
|
|
|
if (NewRole == none) and (NewAff == OldAff) ->
|
|
|
|
%% A user is leaving the room;
|
|
|
|
false;
|
|
|
|
true ->
|
|
|
|
(NewRole /= OldRole) or (NewAff /= OldAff)
|
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec send_new_presence1(jid(), binary(), boolean(), state(), state()) -> ok.
|
2016-02-08 20:10:20 +01:00
|
|
|
send_new_presence1(NJID, Reason, IsInitialPresence, StateData, OldStateData) ->
|
2015-12-08 19:24:39 +01:00
|
|
|
LNJID = jid:tolower(NJID),
|
|
|
|
#user{nick = Nick} = (?DICT):fetch(LNJID, StateData#state.users),
|
2009-08-21 15:22:18 +02:00
|
|
|
LJID = find_jid_by_nick(Nick, StateData),
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok,
|
2015-12-08 19:24:39 +01:00
|
|
|
#user{jid = RealJID, role = Role0,
|
|
|
|
last_presence = Presence0} = UserInfo} =
|
2015-11-24 16:44:13 +01:00
|
|
|
(?DICT):find(jid:tolower(LJID),
|
2013-03-14 10:33:02 +01:00
|
|
|
StateData#state.users),
|
2015-12-08 19:24:39 +01:00
|
|
|
{Role1, Presence1} =
|
|
|
|
case presence_broadcast_allowed(NJID, StateData) of
|
|
|
|
true -> {Role0, Presence0};
|
2016-07-25 12:50:30 +02:00
|
|
|
false -> {none, #presence{type = unavailable}}
|
2015-12-08 19:24:39 +01:00
|
|
|
end,
|
2009-08-21 15:22:18 +02:00
|
|
|
Affiliation = get_affiliation(LJID, StateData),
|
2015-12-08 19:24:39 +01:00
|
|
|
UserList =
|
|
|
|
case not (presence_broadcast_allowed(NJID, StateData) orelse
|
|
|
|
presence_broadcast_allowed(NJID, OldStateData)) of
|
|
|
|
true ->
|
|
|
|
[{LNJID, UserInfo}];
|
|
|
|
false ->
|
|
|
|
(?DICT):to_list(StateData#state.users)
|
|
|
|
end,
|
2016-06-26 08:08:37 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun({LUJID, Info}) ->
|
|
|
|
{Role, Presence} = if LNJID == LUJID -> {Role0, Presence0};
|
|
|
|
true -> {Role1, Presence1}
|
|
|
|
end,
|
2016-07-25 12:50:30 +02:00
|
|
|
Item0 = #muc_item{affiliation = Affiliation,
|
|
|
|
role = Role},
|
|
|
|
Item1 = case Info#user.role == moderator orelse
|
|
|
|
(StateData#state.config)#config.anonymous
|
|
|
|
== false of
|
|
|
|
true -> Item0#muc_item{jid = RealJID};
|
|
|
|
false -> Item0
|
|
|
|
end,
|
|
|
|
Item = if is_binary(Reason), Reason /= <<"">> ->
|
|
|
|
Item1#muc_item{reason = Reason};
|
|
|
|
true ->
|
|
|
|
Item1
|
|
|
|
end,
|
|
|
|
StatusCodes = status_codes(IsInitialPresence, NJID, Info,
|
|
|
|
StateData),
|
|
|
|
Pres = if Presence == undefined -> #presence{};
|
2016-06-26 08:08:37 +02:00
|
|
|
true -> Presence
|
|
|
|
end,
|
2016-07-25 12:50:30 +02:00
|
|
|
Packet = xmpp:set_subtag(
|
|
|
|
Pres, #muc_user{items = [Item],
|
|
|
|
status_codes = StatusCodes}),
|
2016-06-26 08:08:37 +02:00
|
|
|
Node1 = case is_ra_changed(NJID, IsInitialPresence, StateData, OldStateData) of
|
|
|
|
true -> ?NS_MUCSUB_NODES_AFFILIATIONS;
|
2016-07-07 15:42:41 +02:00
|
|
|
false -> ?NS_MUCSUB_NODES_PRESENCE
|
2016-06-26 08:08:37 +02:00
|
|
|
end,
|
|
|
|
send_wrapped(jid:replace_resource(StateData#state.jid, Nick),
|
|
|
|
Info#user.jid, Packet, Node1, StateData),
|
2016-07-25 12:50:30 +02:00
|
|
|
Type = xmpp:get_type(Packet),
|
2016-06-26 08:08:37 +02:00
|
|
|
IsSubscriber = Info#user.is_subscriber,
|
|
|
|
IsOccupant = Info#user.last_presence /= undefined,
|
|
|
|
if (IsSubscriber and not IsOccupant) and
|
2016-07-25 12:50:30 +02:00
|
|
|
(IsInitialPresence or (Type == unavailable)) ->
|
2016-06-26 08:08:37 +02:00
|
|
|
Node2 = ?NS_MUCSUB_NODES_PARTICIPANTS,
|
|
|
|
send_wrapped(jid:replace_resource(StateData#state.jid, Nick),
|
|
|
|
Info#user.jid, Packet, Node2, StateData);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
UserList).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec send_existing_presences(jid(), state()) -> ok.
|
2003-03-23 21:08:44 +01:00
|
|
|
send_existing_presences(ToJID, StateData) ->
|
2015-05-08 13:09:13 +02:00
|
|
|
case is_room_overcrowded(StateData) of
|
|
|
|
true -> ok;
|
|
|
|
false -> send_existing_presences1(ToJID, StateData)
|
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec send_existing_presences1(jid(), state()) -> ok.
|
2015-05-08 13:09:13 +02:00
|
|
|
send_existing_presences1(ToJID, StateData) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LToJID = jid:tolower(ToJID),
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, #user{jid = RealToJID, role = Role}} =
|
|
|
|
(?DICT):find(LToJID, StateData#state.users),
|
2016-06-26 08:08:37 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun({FromNick, _Users}) ->
|
|
|
|
LJID = find_jid_by_nick(FromNick, StateData),
|
|
|
|
#user{jid = FromJID, role = FromRole,
|
|
|
|
last_presence = Presence} =
|
|
|
|
(?DICT):fetch(jid:tolower(LJID),
|
|
|
|
StateData#state.users),
|
|
|
|
PresenceBroadcast =
|
|
|
|
lists:member(
|
|
|
|
FromRole, (StateData#state.config)#config.presence_broadcast),
|
|
|
|
case {RealToJID, PresenceBroadcast} of
|
|
|
|
{FromJID, _} -> ok;
|
|
|
|
{_, false} -> ok;
|
|
|
|
_ ->
|
|
|
|
FromAffiliation = get_affiliation(LJID, StateData),
|
2016-07-25 12:50:30 +02:00
|
|
|
Item0 = #muc_item{affiliation = FromAffiliation,
|
|
|
|
role = FromRole},
|
|
|
|
Item = case Role == moderator orelse
|
|
|
|
(StateData#state.config)#config.anonymous
|
|
|
|
== false of
|
|
|
|
true -> Item0#muc_item{jid = FromJID};
|
|
|
|
false -> Item0
|
|
|
|
end,
|
|
|
|
Packet = xmpp:set_subtag(
|
|
|
|
Presence, #muc_user{items = [Item]}),
|
2016-06-26 08:08:37 +02:00
|
|
|
send_wrapped(jid:replace_resource(StateData#state.jid, FromNick),
|
2016-07-07 15:42:41 +02:00
|
|
|
RealToJID, Packet, ?NS_MUCSUB_NODES_PRESENCE, StateData)
|
2016-06-26 08:08:37 +02:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
(?DICT):to_list(StateData#state.nicks)).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec set_nick(jid(), binary(), state()) -> state().
|
2016-06-26 08:08:37 +02:00
|
|
|
set_nick(JID, Nick, State) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LJID = jid:tolower(JID),
|
2016-06-26 08:08:37 +02:00
|
|
|
{ok, #user{nick = OldNick}} = (?DICT):find(LJID, State#state.users),
|
2013-03-14 10:33:02 +01:00
|
|
|
Users = (?DICT):update(LJID,
|
|
|
|
fun (#user{} = User) -> User#user{nick = Nick} end,
|
2016-06-26 08:08:37 +02:00
|
|
|
State#state.users),
|
|
|
|
OldNickUsers = (?DICT):fetch(OldNick, State#state.nicks),
|
|
|
|
NewNickUsers = case (?DICT):find(Nick, State#state.nicks) of
|
|
|
|
{ok, U} -> U;
|
|
|
|
error -> []
|
2009-08-21 15:22:18 +02:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
Nicks = case OldNickUsers of
|
2016-06-26 08:08:37 +02:00
|
|
|
[LJID] ->
|
|
|
|
(?DICT):store(Nick, [LJID | NewNickUsers -- [LJID]],
|
|
|
|
(?DICT):erase(OldNick, State#state.nicks));
|
|
|
|
[_ | _] ->
|
|
|
|
(?DICT):store(Nick, [LJID | NewNickUsers -- [LJID]],
|
|
|
|
(?DICT):store(OldNick, OldNickUsers -- [LJID],
|
|
|
|
State#state.nicks))
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
2016-06-26 08:08:37 +02:00
|
|
|
State#state{users = Users, nicks = Nicks}.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec change_nick(jid(), binary(), state()) -> state().
|
2016-06-26 08:08:37 +02:00
|
|
|
change_nick(JID, Nick, StateData) ->
|
|
|
|
LJID = jid:tolower(JID),
|
|
|
|
{ok, #user{nick = OldNick}} = (?DICT):find(LJID, StateData#state.users),
|
|
|
|
OldNickUsers = (?DICT):fetch(OldNick, StateData#state.nicks),
|
|
|
|
NewNickUsers = case (?DICT):find(Nick, StateData#state.nicks) of
|
|
|
|
{ok, U} -> U;
|
|
|
|
error -> []
|
|
|
|
end,
|
|
|
|
SendOldUnavailable = length(OldNickUsers) == 1,
|
|
|
|
SendNewAvailable = SendOldUnavailable orelse NewNickUsers == [],
|
|
|
|
NewStateData = set_nick(JID, Nick, StateData),
|
2015-11-12 17:51:20 +01:00
|
|
|
case presence_broadcast_allowed(JID, NewStateData) of
|
|
|
|
true ->
|
|
|
|
send_nick_changing(JID, OldNick, NewStateData,
|
|
|
|
SendOldUnavailable, SendNewAvailable);
|
|
|
|
false -> ok
|
|
|
|
end,
|
2006-03-14 05:26:15 +01:00
|
|
|
add_to_log(nickchange, {OldNick, Nick}, StateData),
|
2003-03-23 21:08:44 +01:00
|
|
|
NewStateData.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec send_nick_changing(jid(), binary(), state(), boolean(), boolean()) -> ok.
|
2009-08-21 15:22:18 +02:00
|
|
|
send_nick_changing(JID, OldNick, StateData,
|
2013-03-14 10:33:02 +01:00
|
|
|
SendOldUnavailable, SendNewAvailable) ->
|
|
|
|
{ok,
|
|
|
|
#user{jid = RealJID, nick = Nick, role = Role,
|
|
|
|
last_presence = Presence}} =
|
2015-11-24 16:44:13 +01:00
|
|
|
(?DICT):find(jid:tolower(JID),
|
2013-03-14 10:33:02 +01:00
|
|
|
StateData#state.users),
|
2003-03-23 21:08:44 +01:00
|
|
|
Affiliation = get_affiliation(JID, StateData),
|
2016-07-25 12:50:30 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun({_LJID, Info}) when Presence /= undefined ->
|
|
|
|
Item0 = #muc_item{affiliation = Affiliation, role = Role},
|
|
|
|
Item1 = case Info#user.role == moderator orelse
|
|
|
|
(StateData#state.config)#config.anonymous
|
|
|
|
== false of
|
|
|
|
true -> Item0#muc_item{jid = RealJID, nick = Nick};
|
|
|
|
false -> Item0#muc_item{nick = Nick}
|
|
|
|
end,
|
|
|
|
Item2 = case Info#user.role == moderator orelse
|
|
|
|
(StateData#state.config)#config.anonymous
|
|
|
|
== false of
|
|
|
|
true -> Item0#muc_item{jid = RealJID};
|
|
|
|
false -> Item0
|
|
|
|
end,
|
|
|
|
Status110 = case JID == Info#user.jid of
|
|
|
|
true -> [110];
|
|
|
|
false -> []
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
2016-07-25 12:50:30 +02:00
|
|
|
Packet1 = #presence{type = unavailable,
|
|
|
|
sub_els = [#muc_user{
|
|
|
|
items = [Item1],
|
|
|
|
status_codes = [303|Status110]}]},
|
|
|
|
Packet2 = xmpp:set_subtag(Presence,
|
|
|
|
#muc_user{items = [Item2],
|
|
|
|
status_codes = Status110}),
|
|
|
|
if SendOldUnavailable ->
|
|
|
|
send_wrapped(
|
|
|
|
jid:replace_resource(StateData#state.jid, OldNick),
|
|
|
|
Info#user.jid, Packet1, ?NS_MUCSUB_NODES_PRESENCE,
|
|
|
|
StateData);
|
|
|
|
true -> ok
|
|
|
|
end,
|
|
|
|
if SendNewAvailable ->
|
|
|
|
send_wrapped(
|
|
|
|
jid:replace_resource(StateData#state.jid, Nick),
|
|
|
|
Info#user.jid, Packet2, ?NS_MUCSUB_NODES_PRESENCE,
|
|
|
|
StateData);
|
|
|
|
true -> ok
|
|
|
|
end;
|
|
|
|
(_) ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
(?DICT):to_list(StateData#state.users)).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec maybe_send_affiliation(jid(), affiliation(), state()) -> ok.
|
2016-05-20 01:28:16 +02:00
|
|
|
maybe_send_affiliation(JID, Affiliation, StateData) ->
|
|
|
|
LJID = jid:tolower(JID),
|
|
|
|
IsOccupant = case LJID of
|
|
|
|
{LUser, LServer, <<"">>} ->
|
|
|
|
not (?DICT):is_empty(
|
|
|
|
(?DICT):filter(fun({U, S, _}, _) ->
|
|
|
|
U == LUser andalso
|
|
|
|
S == LServer
|
|
|
|
end, StateData#state.users));
|
|
|
|
{_LUser, _LServer, _LResource} ->
|
|
|
|
(?DICT):is_key(LJID, StateData#state.users)
|
|
|
|
end,
|
|
|
|
case IsOccupant of
|
|
|
|
true ->
|
|
|
|
ok; % The new affiliation is published via presence.
|
|
|
|
false ->
|
|
|
|
send_affiliation(LJID, Affiliation, StateData)
|
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec send_affiliation(ljid(), affiliation(), state()) -> ok.
|
2016-05-20 01:28:16 +02:00
|
|
|
send_affiliation(LJID, Affiliation, StateData) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
Item = #muc_item{jid = jid:make(LJID),
|
|
|
|
affiliation = Affiliation,
|
|
|
|
role = none},
|
|
|
|
Message = #message{id = randoms:get_string(),
|
|
|
|
sub_els = [#muc_user{items = [Item]}]},
|
2016-05-20 01:28:16 +02:00
|
|
|
Recipients = case (StateData#state.config)#config.anonymous of
|
|
|
|
true ->
|
|
|
|
(?DICT):filter(fun(_, #user{role = moderator}) ->
|
|
|
|
true;
|
|
|
|
(_, _) ->
|
|
|
|
false
|
|
|
|
end, StateData#state.users);
|
|
|
|
false ->
|
|
|
|
StateData#state.users
|
|
|
|
end,
|
|
|
|
send_multiple(StateData#state.jid,
|
|
|
|
StateData#state.server_host,
|
|
|
|
Recipients, Message).
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec status_codes(boolean(), jid(), #user{}, state()) -> [pos_integer()].
|
|
|
|
status_codes(IsInitialPresence, JID, #user{jid = JID}, StateData) ->
|
|
|
|
S0 = [110],
|
|
|
|
case IsInitialPresence of
|
|
|
|
true ->
|
|
|
|
S1 = case StateData#state.just_created of
|
|
|
|
true -> [201|S0];
|
|
|
|
false -> S0
|
|
|
|
end,
|
|
|
|
S2 = case (StateData#state.config)#config.anonymous of
|
|
|
|
true -> S1;
|
|
|
|
false -> [100|S1]
|
|
|
|
end,
|
|
|
|
S3 = case (StateData#state.config)#config.logging of
|
|
|
|
true -> [170|S2];
|
|
|
|
false -> S2
|
|
|
|
end,
|
|
|
|
S3;
|
|
|
|
false -> S0
|
|
|
|
end;
|
|
|
|
status_codes(_IsInitialPresence, _JID, _Info, _StateData) -> [].
|
2016-02-08 20:10:20 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec lqueue_new(non_neg_integer()) -> lqueue().
|
2003-03-23 21:08:44 +01:00
|
|
|
lqueue_new(Max) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
#lqueue{queue = queue:new(), len = 0, max = Max}.
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec lqueue_in(term(), lqueue()) -> lqueue().
|
2006-09-05 17:53:54 +02:00
|
|
|
%% If the message queue limit is set to 0, do not store messages.
|
2013-03-14 10:33:02 +01:00
|
|
|
lqueue_in(_Item, LQ = #lqueue{max = 0}) -> LQ;
|
2006-09-05 17:53:54 +02:00
|
|
|
%% Otherwise, rotate messages in the queue store.
|
2013-03-14 10:33:02 +01:00
|
|
|
lqueue_in(Item,
|
|
|
|
#lqueue{queue = Q1, len = Len, max = Max}) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
Q2 = queue:in(Item, Q1),
|
2013-03-14 10:33:02 +01:00
|
|
|
if Len >= Max ->
|
|
|
|
Q3 = lqueue_cut(Q2, Len - Max + 1),
|
|
|
|
#lqueue{queue = Q3, len = Max, max = Max};
|
|
|
|
true -> #lqueue{queue = Q2, len = Len + 1, max = Max}
|
2003-03-23 21:08:44 +01:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec lqueue_cut(queue:queue(), non_neg_integer()) -> queue:queue().
|
2013-03-14 10:33:02 +01:00
|
|
|
lqueue_cut(Q, 0) -> Q;
|
2003-03-23 21:08:44 +01:00
|
|
|
lqueue_cut(Q, N) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{_, Q1} = queue:out(Q), lqueue_cut(Q1, N - 1).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec lqueue_to_list(lqueue()) -> list().
|
2003-03-23 21:08:44 +01:00
|
|
|
lqueue_to_list(#lqueue{queue = Q1}) ->
|
|
|
|
queue:to_list(Q1).
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec add_message_to_history(binary(), jid(), message(), state()) -> state().
|
2009-06-30 18:51:25 +02:00
|
|
|
add_message_to_history(FromNick, FromJID, Packet, StateData) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
HaveSubject = Packet#message.subject /= [],
|
2015-12-04 15:08:43 +01:00
|
|
|
TimeStamp = p1_time_compat:timestamp(),
|
2015-03-10 00:02:32 +01:00
|
|
|
AddrPacket = case (StateData#state.config)#config.anonymous of
|
|
|
|
true -> Packet;
|
|
|
|
false ->
|
2016-07-25 12:50:30 +02:00
|
|
|
Addresses = #addresses{
|
|
|
|
list = [#address{type = ofrom,
|
|
|
|
jid = FromJID}]},
|
|
|
|
xmpp:set_subtag(Packet, Addresses)
|
2015-03-10 00:02:32 +01:00
|
|
|
end,
|
2016-07-25 12:50:30 +02:00
|
|
|
TSPacket = xmpp_util:add_delay_info(
|
|
|
|
AddrPacket, StateData#state.jid, TimeStamp),
|
|
|
|
SPacket = xmpp:set_from_to(
|
|
|
|
TSPacket,
|
|
|
|
jid:replace_resource(StateData#state.jid, FromNick),
|
|
|
|
StateData#state.jid),
|
2010-07-01 12:54:01 +02:00
|
|
|
Size = element_size(SPacket),
|
2013-03-14 10:33:02 +01:00
|
|
|
Q1 = lqueue_in({FromNick, TSPacket, HaveSubject,
|
2014-12-07 15:55:18 +01:00
|
|
|
calendar:now_to_universal_time(TimeStamp), Size},
|
2004-02-15 21:10:40 +01:00
|
|
|
StateData#state.history),
|
2006-03-14 05:26:15 +01:00
|
|
|
add_to_log(text, {FromNick, Packet}, StateData),
|
2003-03-23 21:08:44 +01:00
|
|
|
StateData#state{history = Q1}.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec send_history(jid(), integer(), state()) -> boolean().
|
2004-02-15 21:10:40 +01:00
|
|
|
send_history(JID, Shift, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:foldl(fun ({Nick, Packet, HaveSubject, _TimeStamp,
|
|
|
|
_Size},
|
|
|
|
B) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
ejabberd_router:route(jid:replace_resource(StateData#state.jid,
|
2013-03-14 10:33:02 +01:00
|
|
|
Nick),
|
|
|
|
JID, Packet),
|
|
|
|
B or HaveSubject
|
|
|
|
end,
|
|
|
|
false,
|
|
|
|
lists:nthtail(Shift,
|
|
|
|
lqueue_to_list(StateData#state.history))).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec send_subject(jid(), state()) -> ok.
|
2015-06-04 09:55:38 +02:00
|
|
|
send_subject(_JID, #state{subject_author = <<"">>}) -> ok;
|
2015-11-25 00:05:24 +01:00
|
|
|
send_subject(JID, #state{subject_author = Nick} = StateData) ->
|
2015-06-04 09:55:38 +02:00
|
|
|
Subject = StateData#state.subject,
|
2016-07-25 12:50:30 +02:00
|
|
|
Packet = #message{type = groupchat, subject = xmpp:mk_text(Subject)},
|
2015-11-25 00:05:24 +01:00
|
|
|
ejabberd_router:route(jid:replace_resource(StateData#state.jid, Nick), JID,
|
|
|
|
Packet).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec check_subject(message()) -> false | binary().
|
|
|
|
check_subject(#message{subject = []}) -> false;
|
|
|
|
check_subject(#message{subject = Subj}) -> xmpp:get_text(Subj).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec can_change_subject(role(), state()) -> boolean().
|
2003-03-23 21:08:44 +01:00
|
|
|
can_change_subject(Role, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case (StateData#state.config)#config.allow_change_subj
|
|
|
|
of
|
|
|
|
true -> Role == moderator orelse Role == participant;
|
|
|
|
_ -> Role == moderator
|
2003-03-26 21:51:18 +01:00
|
|
|
end.
|
2003-03-23 21:08:44 +01:00
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% Admin stuff
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec process_iq_admin(jid(), iq(), #state{}) -> {error, error()} |
|
|
|
|
{result, undefined, #state{}} |
|
|
|
|
{result, muc_admin()}.
|
|
|
|
process_iq_admin(_From, #iq{lang = Lang, sub_els = [#muc_admin{items = []}]},
|
|
|
|
_StateData) ->
|
|
|
|
Txt = <<"No 'item' element found">>,
|
|
|
|
{error, xmpp:err_bad_request(Txt, Lang)};
|
|
|
|
process_iq_admin(From, #iq{type = set, lang = Lang,
|
|
|
|
sub_els = [#muc_admin{items = Items}]},
|
|
|
|
StateData) ->
|
2004-03-02 22:16:55 +01:00
|
|
|
process_admin_items_set(From, Items, Lang, StateData);
|
2016-07-25 12:50:30 +02:00
|
|
|
process_iq_admin(From, #iq{type = get, lang = Lang,
|
|
|
|
sub_els = [#muc_admin{items = [Item]}]},
|
|
|
|
StateData) ->
|
|
|
|
FAffiliation = get_affiliation(From, StateData),
|
|
|
|
FRole = get_role(From, StateData),
|
|
|
|
case Item of
|
|
|
|
#muc_item{role = undefined, affiliation = undefined} ->
|
|
|
|
Txt = <<"Neither 'role' nor 'affiliation' attribute found">>,
|
|
|
|
{error, xmpp:err_bad_request(Txt, Lang)};
|
|
|
|
#muc_item{role = undefined, affiliation = Affiliation} ->
|
|
|
|
if (FAffiliation == owner) or
|
|
|
|
(FAffiliation == admin) or
|
|
|
|
((FAffiliation == member) and
|
|
|
|
not (StateData#state.config)#config.anonymous) ->
|
|
|
|
Items = items_with_affiliation(Affiliation, StateData),
|
|
|
|
{result, #muc_admin{items = Items}};
|
|
|
|
true ->
|
|
|
|
ErrText = <<"Administrator privileges required">>,
|
|
|
|
{error, xmpp:err_forbidden(ErrText, Lang)}
|
|
|
|
end;
|
|
|
|
#muc_item{role = Role} ->
|
|
|
|
if FRole == moderator ->
|
|
|
|
Items = items_with_role(Role, StateData),
|
|
|
|
{result, #muc_admin{items = Items}};
|
|
|
|
true ->
|
|
|
|
ErrText = <<"Moderator privileges required">>,
|
|
|
|
{error, xmpp:err_forbidden(ErrText, Lang)}
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
process_iq_admin(_From, #iq{type = get, lang = Lang}, _StateData) ->
|
|
|
|
ErrText = <<"Too many <item/> elements">>,
|
|
|
|
{error, xmpp:err_bad_request(ErrText, Lang)}.
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec items_with_role(role(), state()) -> [muc_item()].
|
2003-03-23 21:08:44 +01:00
|
|
|
items_with_role(SRole, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:map(fun ({_, U}) -> user_to_item(U, StateData)
|
|
|
|
end,
|
|
|
|
search_role(SRole, StateData)).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec items_with_affiliation(affiliation(), state()) -> [muc_item()].
|
2003-03-23 21:08:44 +01:00
|
|
|
items_with_affiliation(SAffiliation, StateData) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
lists:map(
|
|
|
|
fun({JID, {Affiliation, Reason}}) ->
|
|
|
|
#muc_item{affiliation = Affiliation, jid = JID,
|
|
|
|
reason = if is_binary(Reason), Reason /= <<"">> ->
|
|
|
|
Reason;
|
|
|
|
true ->
|
|
|
|
undefined
|
|
|
|
end};
|
|
|
|
({JID, Affiliation}) ->
|
|
|
|
#muc_item{affiliation = Affiliation, jid = JID}
|
|
|
|
end,
|
|
|
|
search_affiliation(SAffiliation, StateData)).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec user_to_item(#user{}, state()) -> muc_item().
|
2013-03-14 10:33:02 +01:00
|
|
|
user_to_item(#user{role = Role, nick = Nick, jid = JID},
|
|
|
|
StateData) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
Affiliation = get_affiliation(JID, StateData),
|
2016-07-25 12:50:30 +02:00
|
|
|
#muc_item{role = Role,
|
|
|
|
affiliation = Affiliation,
|
|
|
|
nick = Nick,
|
|
|
|
jid = JID}.
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec search_role(role(), state()) -> [{ljid(), #user{}}].
|
2003-03-23 21:08:44 +01:00
|
|
|
search_role(Role, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:filter(fun ({_, #user{role = R}}) -> Role == R
|
|
|
|
end,
|
|
|
|
(?DICT):to_list(StateData#state.users)).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec search_affiliation(affiliation(), state()) ->
|
|
|
|
[{ljid(),
|
|
|
|
affiliation() | {affiliation(), binary()}}].
|
2003-03-23 21:08:44 +01:00
|
|
|
search_affiliation(Affiliation, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:filter(fun ({_, A}) ->
|
|
|
|
case A of
|
|
|
|
{A1, _Reason} -> Affiliation == A1;
|
|
|
|
_ -> Affiliation == A
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
(?DICT):to_list(StateData#state.affiliations)).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec process_admin_items_set(jid(), [muc_item()], binary() | undefined,
|
|
|
|
#state{}) -> {result, undefined, #state{}} |
|
|
|
|
{error, error()}.
|
2004-03-02 22:16:55 +01:00
|
|
|
process_admin_items_set(UJID, Items, Lang, StateData) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
UAffiliation = get_affiliation(UJID, StateData),
|
|
|
|
URole = get_role(UJID, StateData),
|
2016-07-25 12:50:30 +02:00
|
|
|
case catch find_changed_items(UJID, UAffiliation, URole,
|
|
|
|
Items, Lang, StateData, [])
|
2013-03-14 10:33:02 +01:00
|
|
|
of
|
|
|
|
{result, Res} ->
|
|
|
|
?INFO_MSG("Processing MUC admin query from ~s in "
|
|
|
|
"room ~s:~n ~p",
|
2015-11-24 16:44:13 +01:00
|
|
|
[jid:to_string(UJID),
|
|
|
|
jid:to_string(StateData#state.jid), Res]),
|
2015-02-25 16:31:27 +01:00
|
|
|
NSD = lists:foldl(process_item_change(UJID),
|
2013-03-14 10:33:02 +01:00
|
|
|
StateData, lists:flatten(Res)),
|
2016-06-26 08:08:37 +02:00
|
|
|
store_room(NSD),
|
2016-07-25 12:50:30 +02:00
|
|
|
{result, undefined, NSD};
|
|
|
|
{error, Err} -> {error, Err}
|
2003-03-23 21:08:44 +01:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec process_item_change(jid()) -> function().
|
2015-02-25 16:31:27 +01:00
|
|
|
process_item_change(UJID) ->
|
|
|
|
fun(E, SD) ->
|
|
|
|
process_item_change(E, SD, UJID)
|
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-type admin_action() :: {jid(), affiliation | role,
|
|
|
|
affiliation() | role(), binary()}.
|
|
|
|
|
|
|
|
-spec process_item_change(admin_action(), state(), jid()) -> state().
|
|
|
|
process_item_change(Item, SD, UJID) ->
|
|
|
|
try case Item of
|
|
|
|
{JID, affiliation, owner, _} when JID#jid.luser == <<"">> ->
|
|
|
|
%% If the provided JID does not have username,
|
|
|
|
%% forget the affiliation completely
|
|
|
|
SD;
|
|
|
|
{JID, role, none, Reason} ->
|
|
|
|
catch send_kickban_presence(UJID, JID, Reason, 307, SD),
|
|
|
|
set_role(JID, none, SD);
|
|
|
|
{JID, affiliation, none, Reason} ->
|
|
|
|
case (SD#state.config)#config.members_only of
|
|
|
|
true ->
|
|
|
|
catch send_kickban_presence(UJID, JID, Reason, 321, none, SD),
|
|
|
|
maybe_send_affiliation(JID, none, SD),
|
|
|
|
SD1 = set_affiliation(JID, none, SD),
|
|
|
|
set_role(JID, none, SD1);
|
|
|
|
_ ->
|
|
|
|
SD1 = set_affiliation(JID, none, SD),
|
|
|
|
send_update_presence(JID, SD1, SD),
|
|
|
|
maybe_send_affiliation(JID, none, SD1),
|
|
|
|
SD1
|
|
|
|
end;
|
|
|
|
{JID, affiliation, outcast, Reason} ->
|
|
|
|
catch send_kickban_presence(UJID, JID, Reason, 301, outcast, SD),
|
|
|
|
maybe_send_affiliation(JID, outcast, SD),
|
|
|
|
set_affiliation(JID, outcast, set_role(JID, none, SD), Reason);
|
|
|
|
{JID, affiliation, A, Reason} when (A == admin) or (A == owner) ->
|
|
|
|
SD1 = set_affiliation(JID, A, SD, Reason),
|
|
|
|
SD2 = set_role(JID, moderator, SD1),
|
|
|
|
send_update_presence(JID, Reason, SD2, SD),
|
|
|
|
maybe_send_affiliation(JID, A, SD2),
|
|
|
|
SD2;
|
|
|
|
{JID, affiliation, member, Reason} ->
|
|
|
|
SD1 = set_affiliation(JID, member, SD, Reason),
|
|
|
|
SD2 = set_role(JID, participant, SD1),
|
|
|
|
send_update_presence(JID, Reason, SD2, SD),
|
|
|
|
maybe_send_affiliation(JID, member, SD2),
|
|
|
|
SD2;
|
|
|
|
{JID, role, Role, Reason} ->
|
|
|
|
SD1 = set_role(JID, Role, SD),
|
|
|
|
catch send_new_presence(JID, Reason, SD1, SD),
|
|
|
|
SD1;
|
|
|
|
{JID, affiliation, A, _Reason} ->
|
|
|
|
SD1 = set_affiliation(JID, A, SD),
|
|
|
|
send_update_presence(JID, SD1, SD),
|
|
|
|
maybe_send_affiliation(JID, A, SD1),
|
|
|
|
SD1
|
|
|
|
end
|
|
|
|
catch E:R ->
|
|
|
|
?ERROR_MSG("failed to set item ~p from ~s: ~p",
|
|
|
|
[Item, jid:to_string(UJID),
|
|
|
|
{E, {R, erlang:get_stacktrace()}}]),
|
|
|
|
SD
|
2015-02-25 16:31:27 +01:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec find_changed_items(jid(), affiliation(), role(),
|
|
|
|
[muc_item()], binary(), state(), [admin_action()]) ->
|
|
|
|
{result, [admin_action()]}.
|
2013-03-14 10:33:02 +01:00
|
|
|
find_changed_items(_UJID, _UAffiliation, _URole, [],
|
|
|
|
_Lang, _StateData, Res) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
{result, Res};
|
2016-07-25 12:50:30 +02:00
|
|
|
find_changed_items(_UJID, _UAffiliation, _URole,
|
|
|
|
[#muc_item{jid = undefined, nick = undefined}|_],
|
|
|
|
Lang, _StateData, _Res) ->
|
|
|
|
Txt = <<"Neither 'jid' nor 'nick' attribute found">>,
|
|
|
|
throw({error, xmpp:err_bad_request(Txt, Lang)});
|
|
|
|
find_changed_items(_UJID, _UAffiliation, _URole,
|
|
|
|
[#muc_item{role = undefined, affiliation = undefined}|_],
|
|
|
|
Lang, _StateData, _Res) ->
|
|
|
|
Txt = <<"Neither 'role' nor 'affiliation' attribute found">>,
|
|
|
|
throw({error, xmpp:err_bad_request(Txt, Lang)});
|
2003-03-23 21:08:44 +01:00
|
|
|
find_changed_items(UJID, UAffiliation, URole,
|
2016-07-25 12:50:30 +02:00
|
|
|
[#muc_item{jid = J, nick = Nick, reason = Reason0,
|
|
|
|
role = Role, affiliation = Affiliation}|Items],
|
2004-03-02 22:16:55 +01:00
|
|
|
Lang, StateData, Res) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
[JID | _] = JIDs =
|
|
|
|
if J /= undefined ->
|
|
|
|
[J];
|
|
|
|
Nick /= undefined ->
|
|
|
|
case find_jids_by_nick(Nick, StateData) of
|
|
|
|
[] ->
|
|
|
|
ErrText = iolist_to_binary(
|
|
|
|
io_lib:format(
|
|
|
|
translate:translate(
|
|
|
|
Lang,
|
|
|
|
<<"Nickname ~s does not exist in the room">>),
|
|
|
|
[Nick])),
|
|
|
|
throw({error, xmpp:err_not_acceptable(ErrText, Lang)});
|
|
|
|
JIDList ->
|
|
|
|
JIDList
|
2013-03-14 10:33:02 +01:00
|
|
|
end
|
2016-07-25 12:50:30 +02:00
|
|
|
end,
|
|
|
|
{RoleOrAff, RoleOrAffValue} = if Role == undefined ->
|
|
|
|
{affiliation, Affiliation};
|
|
|
|
true ->
|
|
|
|
{role, Role}
|
|
|
|
end,
|
|
|
|
TAffiliation = get_affiliation(JID, StateData),
|
|
|
|
TRole = get_role(JID, StateData),
|
|
|
|
ServiceAf = get_service_affiliation(JID, StateData),
|
|
|
|
CanChangeRA = case can_change_ra(UAffiliation,
|
|
|
|
URole,
|
|
|
|
TAffiliation,
|
|
|
|
TRole, RoleOrAff, RoleOrAffValue,
|
|
|
|
ServiceAf) of
|
|
|
|
nothing -> nothing;
|
|
|
|
true -> true;
|
|
|
|
check_owner ->
|
|
|
|
case search_affiliation(owner, StateData) of
|
|
|
|
[{OJID, _}] ->
|
|
|
|
jid:remove_resource(OJID)
|
|
|
|
/=
|
|
|
|
jid:tolower(jid:remove_resource(UJID));
|
|
|
|
_ -> true
|
|
|
|
end;
|
|
|
|
_ -> false
|
|
|
|
end,
|
|
|
|
case CanChangeRA of
|
|
|
|
nothing ->
|
|
|
|
find_changed_items(UJID, UAffiliation, URole,
|
|
|
|
Items, Lang, StateData,
|
|
|
|
Res);
|
|
|
|
true ->
|
|
|
|
Reason = if is_binary(Reason0) -> Reason0;
|
|
|
|
true -> <<"">>
|
|
|
|
end,
|
|
|
|
MoreRes = [{jid:remove_resource(Jidx),
|
|
|
|
RoleOrAff, RoleOrAffValue, Reason}
|
|
|
|
|| Jidx <- JIDs],
|
|
|
|
find_changed_items(UJID, UAffiliation, URole,
|
|
|
|
Items, Lang, StateData,
|
|
|
|
[MoreRes | Res]);
|
|
|
|
false ->
|
|
|
|
Txt = <<"Changing role/affiliation is not allowed">>,
|
|
|
|
throw({error, xmpp:err_not_allowed(Txt, Lang)})
|
|
|
|
end.
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec can_change_ra(affiliation(), role(), affiliation(), role(),
|
|
|
|
affiliation, affiliation(), affiliation()) -> boolean();
|
|
|
|
(affiliation(), role(), affiliation(), role(),
|
|
|
|
role, role(), affiliation()) -> boolean().
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(_FAffiliation, _FRole, owner, _TRole,
|
2008-07-17 19:32:11 +02:00
|
|
|
affiliation, owner, owner) ->
|
|
|
|
%% A room owner tries to add as persistent owner a
|
|
|
|
%% participant that is already owner because he is MUC admin
|
|
|
|
true;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(_FAffiliation, _FRole, _TAffiliation,
|
|
|
|
_TRole, _RoleorAffiliation, _Value, owner) ->
|
2011-04-17 17:03:07 +02:00
|
|
|
%% Nobody can decrease MUC admin's role/affiliation
|
|
|
|
false;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(_FAffiliation, _FRole, TAffiliation,
|
|
|
|
_TRole, affiliation, Value, _ServiceAf)
|
|
|
|
when TAffiliation == Value ->
|
2003-03-23 21:08:44 +01:00
|
|
|
nothing;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(_FAffiliation, _FRole, _TAffiliation,
|
|
|
|
TRole, role, Value, _ServiceAf)
|
|
|
|
when TRole == Value ->
|
2003-03-23 21:08:44 +01:00
|
|
|
nothing;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(FAffiliation, _FRole, outcast, _TRole,
|
2008-07-17 19:32:11 +02:00
|
|
|
affiliation, none, _ServiceAf)
|
2013-03-14 10:33:02 +01:00
|
|
|
when (FAffiliation == owner) or
|
|
|
|
(FAffiliation == admin) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
true;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(FAffiliation, _FRole, outcast, _TRole,
|
2008-07-17 19:32:11 +02:00
|
|
|
affiliation, member, _ServiceAf)
|
2013-03-14 10:33:02 +01:00
|
|
|
when (FAffiliation == owner) or
|
|
|
|
(FAffiliation == admin) ->
|
2005-05-02 23:59:39 +02:00
|
|
|
true;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(owner, _FRole, outcast, _TRole,
|
2008-07-17 19:32:11 +02:00
|
|
|
affiliation, admin, _ServiceAf) ->
|
2005-05-02 23:59:39 +02:00
|
|
|
true;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(owner, _FRole, outcast, _TRole,
|
2008-07-17 19:32:11 +02:00
|
|
|
affiliation, owner, _ServiceAf) ->
|
2005-05-02 23:59:39 +02:00
|
|
|
true;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(FAffiliation, _FRole, none, _TRole,
|
2008-07-17 19:32:11 +02:00
|
|
|
affiliation, outcast, _ServiceAf)
|
2013-03-14 10:33:02 +01:00
|
|
|
when (FAffiliation == owner) or
|
|
|
|
(FAffiliation == admin) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
true;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(FAffiliation, _FRole, none, _TRole,
|
2008-07-17 19:32:11 +02:00
|
|
|
affiliation, member, _ServiceAf)
|
2013-03-14 10:33:02 +01:00
|
|
|
when (FAffiliation == owner) or
|
|
|
|
(FAffiliation == admin) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
true;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(owner, _FRole, none, _TRole, affiliation,
|
|
|
|
admin, _ServiceAf) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
true;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(owner, _FRole, none, _TRole, affiliation,
|
|
|
|
owner, _ServiceAf) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
true;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(FAffiliation, _FRole, member, _TRole,
|
2008-07-17 19:32:11 +02:00
|
|
|
affiliation, outcast, _ServiceAf)
|
2013-03-14 10:33:02 +01:00
|
|
|
when (FAffiliation == owner) or
|
|
|
|
(FAffiliation == admin) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
true;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(FAffiliation, _FRole, member, _TRole,
|
2008-07-17 19:32:11 +02:00
|
|
|
affiliation, none, _ServiceAf)
|
2013-03-14 10:33:02 +01:00
|
|
|
when (FAffiliation == owner) or
|
|
|
|
(FAffiliation == admin) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
true;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(owner, _FRole, member, _TRole,
|
2008-07-17 19:32:11 +02:00
|
|
|
affiliation, admin, _ServiceAf) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
true;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(owner, _FRole, member, _TRole,
|
2008-07-17 19:32:11 +02:00
|
|
|
affiliation, owner, _ServiceAf) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
true;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(owner, _FRole, admin, _TRole, affiliation,
|
|
|
|
_Affiliation, _ServiceAf) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
true;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(owner, _FRole, owner, _TRole, affiliation,
|
|
|
|
_Affiliation, _ServiceAf) ->
|
2006-03-06 03:30:15 +01:00
|
|
|
check_owner;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(_FAffiliation, _FRole, _TAffiliation,
|
|
|
|
_TRole, affiliation, _Value, _ServiceAf) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
false;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(_FAffiliation, moderator, _TAffiliation,
|
|
|
|
visitor, role, none, _ServiceAf) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
true;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(_FAffiliation, moderator, _TAffiliation,
|
|
|
|
visitor, role, participant, _ServiceAf) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
true;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(FAffiliation, _FRole, _TAffiliation,
|
|
|
|
visitor, role, moderator, _ServiceAf)
|
|
|
|
when (FAffiliation == owner) or
|
|
|
|
(FAffiliation == admin) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
true;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(_FAffiliation, moderator, _TAffiliation,
|
|
|
|
participant, role, none, _ServiceAf) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
true;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(_FAffiliation, moderator, _TAffiliation,
|
|
|
|
participant, role, visitor, _ServiceAf) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
true;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(FAffiliation, _FRole, _TAffiliation,
|
|
|
|
participant, role, moderator, _ServiceAf)
|
|
|
|
when (FAffiliation == owner) or
|
|
|
|
(FAffiliation == admin) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
true;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(_FAffiliation, _FRole, owner, moderator,
|
2008-07-17 19:32:11 +02:00
|
|
|
role, visitor, _ServiceAf) ->
|
2005-05-02 23:59:39 +02:00
|
|
|
false;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(owner, _FRole, _TAffiliation, moderator,
|
2008-07-17 19:32:11 +02:00
|
|
|
role, visitor, _ServiceAf) ->
|
2005-05-02 23:59:39 +02:00
|
|
|
true;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(_FAffiliation, _FRole, admin, moderator,
|
2008-07-17 19:32:11 +02:00
|
|
|
role, visitor, _ServiceAf) ->
|
2005-05-02 23:59:39 +02:00
|
|
|
false;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(admin, _FRole, _TAffiliation, moderator,
|
2008-07-17 19:32:11 +02:00
|
|
|
role, visitor, _ServiceAf) ->
|
2005-05-02 23:59:39 +02:00
|
|
|
true;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(_FAffiliation, _FRole, owner, moderator,
|
2008-07-17 19:32:11 +02:00
|
|
|
role, participant, _ServiceAf) ->
|
2004-01-17 21:26:57 +01:00
|
|
|
false;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(owner, _FRole, _TAffiliation, moderator,
|
2008-07-17 19:32:11 +02:00
|
|
|
role, participant, _ServiceAf) ->
|
2004-01-17 21:26:57 +01:00
|
|
|
true;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(_FAffiliation, _FRole, admin, moderator,
|
2008-07-17 19:32:11 +02:00
|
|
|
role, participant, _ServiceAf) ->
|
2004-01-17 21:26:57 +01:00
|
|
|
false;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(admin, _FRole, _TAffiliation, moderator,
|
2008-07-17 19:32:11 +02:00
|
|
|
role, participant, _ServiceAf) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
true;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_change_ra(_FAffiliation, _FRole, _TAffiliation,
|
|
|
|
_TRole, role, _Value, _ServiceAf) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
false.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec send_kickban_presence(jid(), jid(), binary(),
|
|
|
|
pos_integer(), state()) -> ok.
|
2013-04-24 11:00:04 +02:00
|
|
|
send_kickban_presence(UJID, JID, Reason, Code, StateData) ->
|
2010-11-16 00:03:09 +01:00
|
|
|
NewAffiliation = get_affiliation(JID, StateData),
|
2013-04-24 11:00:04 +02:00
|
|
|
send_kickban_presence(UJID, JID, Reason, Code, NewAffiliation,
|
2013-03-14 10:33:02 +01:00
|
|
|
StateData).
|
2010-11-16 00:03:09 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec send_kickban_presence(jid(), jid(), binary(), pos_integer(),
|
|
|
|
affiliation(), state()) -> ok.
|
2013-04-24 11:00:04 +02:00
|
|
|
send_kickban_presence(UJID, JID, Reason, Code, NewAffiliation,
|
2013-03-14 10:33:02 +01:00
|
|
|
StateData) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LJID = jid:tolower(JID),
|
2003-04-13 21:22:46 +02:00
|
|
|
LJIDs = case LJID of
|
2013-03-14 10:33:02 +01:00
|
|
|
{U, S, <<"">>} ->
|
|
|
|
(?DICT):fold(fun (J, _, Js) ->
|
|
|
|
case J of
|
|
|
|
{U, S, _} -> [J | Js];
|
|
|
|
_ -> Js
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
[], StateData#state.users);
|
|
|
|
_ ->
|
|
|
|
case (?DICT):is_key(LJID, StateData#state.users) of
|
|
|
|
true -> [LJID];
|
|
|
|
_ -> []
|
|
|
|
end
|
2003-04-13 21:22:46 +02:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:foreach(fun (J) ->
|
|
|
|
{ok, #user{nick = Nick}} = (?DICT):find(J,
|
|
|
|
StateData#state.users),
|
2006-03-14 05:26:15 +01:00
|
|
|
add_to_log(kickban, {Nick, Reason, Code}, StateData),
|
2007-12-03 11:47:42 +01:00
|
|
|
tab_remove_online_user(J, StateData),
|
2013-04-24 11:00:04 +02:00
|
|
|
send_kickban_presence1(UJID, J, Reason, Code,
|
2013-03-14 10:33:02 +01:00
|
|
|
NewAffiliation, StateData)
|
|
|
|
end,
|
|
|
|
LJIDs).
|
2003-04-13 21:22:46 +02:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec send_kickban_presence1(jid(), jid(), binary(), pos_integer(),
|
|
|
|
affiliation(), state()) -> ok.
|
2013-04-24 11:00:04 +02:00
|
|
|
send_kickban_presence1(MJID, UJID, Reason, Code, Affiliation,
|
2013-03-14 10:33:02 +01:00
|
|
|
StateData) ->
|
|
|
|
{ok, #user{jid = RealJID, nick = Nick}} =
|
2015-11-24 16:44:13 +01:00
|
|
|
(?DICT):find(jid:tolower(UJID),
|
2013-03-14 10:33:02 +01:00
|
|
|
StateData#state.users),
|
2016-06-03 00:08:58 +02:00
|
|
|
ActorNick = get_actor_nick(MJID, StateData),
|
2016-07-25 12:50:30 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun({_LJID, Info}) ->
|
|
|
|
Item0 = #muc_item{affiliation = Affiliation,
|
|
|
|
role = none},
|
|
|
|
Item1 = case Info#user.role == moderator orelse
|
|
|
|
(StateData#state.config)#config.anonymous
|
|
|
|
== false of
|
|
|
|
true -> Item0#muc_item{jid = RealJID};
|
|
|
|
false -> Item0
|
|
|
|
end,
|
|
|
|
Item2 = if is_binary(Reason), Reason /= <<"">> ->
|
|
|
|
Item1#muc_item{reason = Reason};
|
|
|
|
true ->
|
|
|
|
Item1
|
|
|
|
end,
|
|
|
|
Item = case ActorNick of
|
|
|
|
<<"">> -> Item2;
|
|
|
|
_ -> Item2#muc_item{actor = #muc_actor{nick = ActorNick}}
|
|
|
|
end,
|
|
|
|
Packet = #presence{type = unavailable,
|
|
|
|
sub_els = [#muc_user{items = [Item],
|
|
|
|
status_codes = [Code]}]},
|
|
|
|
RoomJIDNick = jid:replace_resource(StateData#state.jid, Nick),
|
|
|
|
send_wrapped(RoomJIDNick, Info#user.jid, Packet,
|
|
|
|
?NS_MUCSUB_NODES_AFFILIATIONS, StateData),
|
|
|
|
IsSubscriber = Info#user.is_subscriber,
|
|
|
|
IsOccupant = Info#user.last_presence /= undefined,
|
|
|
|
if (IsSubscriber and not IsOccupant) ->
|
|
|
|
send_wrapped(RoomJIDNick, Info#user.jid, Packet,
|
|
|
|
?NS_MUCSUB_NODES_PARTICIPANTS, StateData);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
(?DICT):to_list(StateData#state.users)).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec get_actor_nick(binary() | jid(), state()) -> binary().
|
2016-06-03 12:58:20 +02:00
|
|
|
get_actor_nick(<<"">>, _StateData) ->
|
2016-06-03 00:08:58 +02:00
|
|
|
<<"">>;
|
|
|
|
get_actor_nick(MJID, StateData) ->
|
|
|
|
case (?DICT):find(jid:tolower(MJID), StateData#state.users) of
|
|
|
|
{ok, #user{nick = ActorNick}} -> ActorNick;
|
|
|
|
_ -> <<"">>
|
|
|
|
end.
|
|
|
|
|
2003-03-25 22:03:35 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% Owner stuff
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec process_iq_owner(jid(), iq(), state()) ->
|
|
|
|
{result, undefined | muc_owner()} |
|
|
|
|
{result, undefined | muc_owner(), state() | stop} |
|
|
|
|
{error, error()}.
|
|
|
|
process_iq_owner(From, #iq{type = set, lang = Lang,
|
|
|
|
sub_els = [#muc_owner{destroy = Destroy,
|
|
|
|
config = Config,
|
|
|
|
items = Items}]},
|
|
|
|
StateData) ->
|
2003-03-25 22:03:35 +01:00
|
|
|
FAffiliation = get_affiliation(From, StateData),
|
2016-07-25 12:50:30 +02:00
|
|
|
if FAffiliation /= owner ->
|
|
|
|
ErrText = <<"Owner privileges required">>,
|
|
|
|
{error, xmpp:err_forbidden(ErrText, Lang)};
|
|
|
|
Destroy /= undefined, Config == undefined, Items == [] ->
|
|
|
|
?INFO_MSG("Destroyed MUC room ~s by the owner ~s",
|
|
|
|
[jid:to_string(StateData#state.jid), jid:to_string(From)]),
|
|
|
|
add_to_log(room_existence, destroyed, StateData),
|
|
|
|
destroy_room(Destroy, StateData);
|
|
|
|
Config /= undefined, Destroy == undefined, Items == [] ->
|
|
|
|
case Config of
|
|
|
|
#xdata{type = cancel} ->
|
|
|
|
{result, undefined};
|
|
|
|
#xdata{type = submit} ->
|
|
|
|
case is_allowed_log_change(Config, StateData, From) andalso
|
|
|
|
is_allowed_persistent_change(Config, StateData, From) andalso
|
|
|
|
is_allowed_room_name_desc_limits(Config, StateData) andalso
|
|
|
|
is_password_settings_correct(Config, StateData) of
|
|
|
|
true -> set_config(Config, StateData, Lang);
|
|
|
|
false -> {error, xmpp:err_not_acceptable()}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Txt = <<"Incorrect data form">>,
|
|
|
|
{error, xmpp:err_bad_request(Txt, Lang)}
|
|
|
|
end;
|
|
|
|
Items /= [], Config == undefined, Destroy == undefined ->
|
|
|
|
process_admin_items_set(From, Items, Lang, StateData);
|
|
|
|
true ->
|
|
|
|
{error, xmpp:err_bad_request()}
|
2003-03-25 22:03:35 +01:00
|
|
|
end;
|
2016-07-25 12:50:30 +02:00
|
|
|
process_iq_owner(From, #iq{type = get, lang = Lang,
|
|
|
|
sub_els = [#muc_owner{destroy = Destroy,
|
|
|
|
config = Config,
|
|
|
|
items = Items}]},
|
|
|
|
StateData) ->
|
2003-03-25 22:03:35 +01:00
|
|
|
FAffiliation = get_affiliation(From, StateData),
|
2016-07-25 12:50:30 +02:00
|
|
|
if FAffiliation /= owner ->
|
|
|
|
ErrText = <<"Owner privileges required">>,
|
|
|
|
{error, xmpp:err_forbidden(ErrText, Lang)};
|
|
|
|
Destroy == undefined, Config == undefined ->
|
|
|
|
case Items of
|
|
|
|
[] ->
|
|
|
|
{result,
|
|
|
|
#muc_owner{config = get_config(Lang, StateData, From)}};
|
|
|
|
[#muc_item{affiliation = undefined}] ->
|
|
|
|
Txt = <<"No 'affiliation' attribute found">>,
|
|
|
|
{error, xmpp:err_bad_request(Txt, Lang)};
|
|
|
|
[#muc_item{affiliation = Affiliation}] ->
|
|
|
|
Items = items_with_affiliation(Affiliation, StateData),
|
|
|
|
{result, #muc_owner{items = Items}};
|
|
|
|
[_|_] ->
|
|
|
|
Txt = <<"Too many <item/> elements">>,
|
|
|
|
{error, xmpp:err_bad_request(Txt, Lang)}
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
{error, xmpp:err_bad_request()}
|
2003-03-25 22:03:35 +01:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec is_allowed_log_change(xdata(), state(), jid()) -> boolean().
|
|
|
|
is_allowed_log_change(X, StateData, From) ->
|
|
|
|
case xmpp_util:has_xdata_var(<<"muc#roomconfig_enablelogging">>, X) of
|
|
|
|
false -> true;
|
|
|
|
true ->
|
|
|
|
allow ==
|
|
|
|
mod_muc_log:check_access_log(StateData#state.server_host,
|
|
|
|
From)
|
2006-03-14 05:26:15 +01:00
|
|
|
end.
|
2003-03-25 22:03:35 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec is_allowed_persistent_change(xdata(), state(), jid()) -> boolean().
|
|
|
|
is_allowed_persistent_change(X, StateData, From) ->
|
|
|
|
case xmpp_util:has_xdata_var(<<"muc#roomconfig_persistentroom">>, X) of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> true;
|
|
|
|
true ->
|
|
|
|
{_AccessRoute, _AccessCreate, _AccessAdmin,
|
|
|
|
AccessPersistent} =
|
|
|
|
StateData#state.access,
|
|
|
|
allow ==
|
|
|
|
acl:match_rule(StateData#state.server_host,
|
|
|
|
AccessPersistent, From)
|
2007-06-20 13:25:19 +02:00
|
|
|
end.
|
2003-03-25 22:03:35 +01:00
|
|
|
|
2009-04-22 14:05:10 +02:00
|
|
|
%% Check if the Room Name and Room Description defined in the Data Form
|
|
|
|
%% are conformant to the configured limits
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec is_allowed_room_name_desc_limits(xdata(), state()) -> boolean().
|
|
|
|
is_allowed_room_name_desc_limits(XData, StateData) ->
|
|
|
|
IsNameAccepted = case xmpp_util:get_xdata_values(
|
|
|
|
<<"muc#roomconfig_roomname">>, XData) of
|
|
|
|
[N] ->
|
|
|
|
byte_size(N) =<
|
|
|
|
gen_mod:get_module_opt(
|
|
|
|
StateData#state.server_host,
|
|
|
|
mod_muc, max_room_name,
|
|
|
|
fun(infinity) -> infinity;
|
|
|
|
(I) when is_integer(I),
|
|
|
|
I>0 -> I
|
|
|
|
end, infinity);
|
|
|
|
_ ->
|
|
|
|
true
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
2016-07-25 12:50:30 +02:00
|
|
|
IsDescAccepted = case xmpp_util:get_xdata_values(
|
|
|
|
<<"muc#roomconfig_roomdesc">>, XData) of
|
|
|
|
[D] ->
|
|
|
|
byte_size(D) =<
|
|
|
|
gen_mod:get_module_opt(
|
|
|
|
StateData#state.server_host,
|
|
|
|
mod_muc, max_room_desc,
|
|
|
|
fun(infinity) -> infinity;
|
|
|
|
(I) when is_integer(I),
|
|
|
|
I>0 ->
|
|
|
|
I
|
|
|
|
end, infinity);
|
|
|
|
_ -> true
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
2009-04-22 14:05:10 +02:00
|
|
|
IsNameAccepted and IsDescAccepted.
|
|
|
|
|
2009-08-15 22:09:05 +02:00
|
|
|
%% Return false if:
|
|
|
|
%% "the password for a password-protected room is blank"
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec is_password_settings_correct(xdata(), state()) -> boolean().
|
|
|
|
is_password_settings_correct(XData, StateData) ->
|
2009-08-15 22:09:05 +02:00
|
|
|
Config = StateData#state.config,
|
|
|
|
OldProtected = Config#config.password_protected,
|
|
|
|
OldPassword = Config#config.password,
|
2016-07-25 12:50:30 +02:00
|
|
|
NewProtected = case xmpp_util:get_xdata_values(
|
|
|
|
<<"muc#roomconfig_passwordprotectedroom">>, XData) of
|
|
|
|
[<<"1">>] -> true;
|
|
|
|
[<<"true">>] -> true;
|
|
|
|
[<<"0">>] -> false;
|
|
|
|
[<<"false">>] -> false;
|
|
|
|
_ -> undefined
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
2016-07-25 12:50:30 +02:00
|
|
|
NewPassword = case xmpp_util:get_xdata_values(
|
|
|
|
<<"muc#roomconfig_roomsecret">>, XData) of
|
|
|
|
[P] -> P;
|
|
|
|
_ -> undefined
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
|
|
|
case {OldProtected, NewProtected, OldPassword,
|
|
|
|
NewPassword}
|
|
|
|
of
|
|
|
|
{true, undefined, <<"">>, undefined} -> false;
|
|
|
|
{true, undefined, _, <<"">>} -> false;
|
|
|
|
{_, true, <<"">>, undefined} -> false;
|
|
|
|
{_, true, _, <<"">>} -> false;
|
|
|
|
_ -> true
|
2009-08-15 22:09:05 +02:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-define(XFIELD(Type, Label, Var, Vals),
|
|
|
|
#xdata_field{type = Type,
|
|
|
|
label = translate:translate(Lang, Label),
|
|
|
|
var = Var,
|
|
|
|
values = Vals}).
|
2003-03-25 22:03:35 +01:00
|
|
|
|
|
|
|
-define(BOOLXFIELD(Label, Var, Val),
|
2016-07-25 12:50:30 +02:00
|
|
|
?XFIELD(boolean, Label, Var,
|
2003-03-25 22:03:35 +01:00
|
|
|
case Val of
|
2016-07-25 12:50:30 +02:00
|
|
|
true -> [<<"1">>];
|
|
|
|
_ -> [<<"0">>]
|
2003-03-25 22:03:35 +01:00
|
|
|
end)).
|
|
|
|
|
2003-03-27 21:55:09 +01:00
|
|
|
-define(STRINGXFIELD(Label, Var, Val),
|
2016-07-25 12:50:30 +02:00
|
|
|
?XFIELD('text-single', Label, Var, [Val])).
|
2003-03-27 21:55:09 +01:00
|
|
|
|
2003-08-15 21:17:12 +02:00
|
|
|
-define(PRIVATEXFIELD(Label, Var, Val),
|
2016-07-25 12:50:30 +02:00
|
|
|
?XFIELD('text-private', Label, Var, [Val])).
|
2003-08-15 21:17:12 +02:00
|
|
|
|
2011-02-16 11:14:39 +01:00
|
|
|
-define(JIDMULTIXFIELD(Label, Var, JIDList),
|
2016-07-25 12:50:30 +02:00
|
|
|
?XFIELD('jid-multi', Label, Var,
|
|
|
|
[jid:to_string(JID) || JID <- JIDList])).
|
2011-02-16 11:14:39 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec make_options([{binary(), binary()}], binary()) -> [xdata_option()].
|
|
|
|
make_options(Options, Lang) ->
|
|
|
|
[#xdata_option{label = translate:translate(Lang, Label),
|
|
|
|
value = Value} || {Label, Value} <- Options].
|
|
|
|
|
|
|
|
-spec get_default_room_maxusers(state()) -> non_neg_integer().
|
2008-11-26 16:10:38 +01:00
|
|
|
get_default_room_maxusers(RoomState) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
DefRoomOpts =
|
|
|
|
gen_mod:get_module_opt(RoomState#state.server_host,
|
|
|
|
mod_muc, default_room_options,
|
|
|
|
fun(L) when is_list(L) -> L end,
|
|
|
|
[]),
|
2008-11-26 16:10:38 +01:00
|
|
|
RoomState2 = set_opts(DefRoomOpts, RoomState),
|
|
|
|
(RoomState2#state.config)#config.max_users.
|
2003-03-25 22:03:35 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec get_config(binary(), state(), jid()) -> xdata().
|
2006-03-14 05:26:15 +01:00
|
|
|
get_config(Lang, StateData, From) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
{_AccessRoute, _AccessCreate, _AccessAdmin, AccessPersistent} =
|
2013-03-14 10:33:02 +01:00
|
|
|
StateData#state.access,
|
2007-08-29 19:54:45 +02:00
|
|
|
ServiceMaxUsers = get_service_max_users(StateData),
|
2016-07-25 12:50:30 +02:00
|
|
|
DefaultRoomMaxUsers = get_default_room_maxusers(StateData),
|
2003-03-25 22:03:35 +01:00
|
|
|
Config = StateData#state.config,
|
2016-07-25 12:50:30 +02:00
|
|
|
{MaxUsersRoomInteger, MaxUsersRoomString} =
|
|
|
|
case get_max_users(StateData) of
|
|
|
|
N when is_integer(N) ->
|
|
|
|
{N, integer_to_binary(N)};
|
|
|
|
_ -> {0, <<"none">>}
|
|
|
|
end,
|
|
|
|
Title = iolist_to_binary(
|
|
|
|
io_lib:format(
|
|
|
|
translate:translate(Lang, <<"Configuration of room ~s">>),
|
|
|
|
[jid:to_string(StateData#state.jid)])),
|
|
|
|
Fs = [#xdata_field{type = hidden,
|
|
|
|
var = <<"FORM_TYPE">>,
|
|
|
|
values = [<<"http://jabber.org/protocol/muc#roomconfig">>]},
|
|
|
|
?STRINGXFIELD(<<"Room title">>,
|
|
|
|
<<"muc#roomconfig_roomname">>, (Config#config.title)),
|
|
|
|
?STRINGXFIELD(<<"Room description">>,
|
|
|
|
<<"muc#roomconfig_roomdesc">>,
|
|
|
|
(Config#config.description))] ++
|
|
|
|
case acl:match_rule(StateData#state.server_host, AccessPersistent, From) of
|
|
|
|
allow ->
|
|
|
|
[?BOOLXFIELD(<<"Make room persistent">>,
|
|
|
|
<<"muc#roomconfig_persistentroom">>,
|
|
|
|
(Config#config.persistent))];
|
|
|
|
deny -> []
|
|
|
|
end ++
|
|
|
|
[?BOOLXFIELD(<<"Make room public searchable">>,
|
|
|
|
<<"muc#roomconfig_publicroom">>,
|
|
|
|
(Config#config.public)),
|
|
|
|
?BOOLXFIELD(<<"Make participants list public">>,
|
|
|
|
<<"public_list">>, (Config#config.public_list)),
|
|
|
|
?BOOLXFIELD(<<"Make room password protected">>,
|
|
|
|
<<"muc#roomconfig_passwordprotectedroom">>,
|
|
|
|
(Config#config.password_protected)),
|
|
|
|
?PRIVATEXFIELD(<<"Password">>,
|
|
|
|
<<"muc#roomconfig_roomsecret">>,
|
|
|
|
case Config#config.password_protected of
|
|
|
|
true -> Config#config.password;
|
|
|
|
false -> <<"">>
|
|
|
|
end),
|
|
|
|
#xdata_field{type = 'list-single',
|
|
|
|
label = translate:translate(
|
|
|
|
Lang, <<"Maximum Number of Occupants">>),
|
|
|
|
var = <<"muc#roomconfig_maxusers">>,
|
|
|
|
values = [MaxUsersRoomString],
|
|
|
|
options =
|
|
|
|
if is_integer(ServiceMaxUsers) -> [];
|
|
|
|
true -> make_options(
|
|
|
|
[{<<"No limit">>, <<"none">>}],
|
|
|
|
Lang)
|
|
|
|
end ++
|
|
|
|
make_options(
|
|
|
|
[{integer_to_binary(N), integer_to_binary(N)}
|
|
|
|
|| N <- lists:usort([ServiceMaxUsers,
|
|
|
|
DefaultRoomMaxUsers,
|
|
|
|
MaxUsersRoomInteger
|
|
|
|
| ?MAX_USERS_DEFAULT_LIST]),
|
|
|
|
N =< ServiceMaxUsers],
|
|
|
|
Lang)},
|
|
|
|
#xdata_field{type = 'list-single',
|
|
|
|
label = translate:translate(
|
|
|
|
Lang, <<"Present real Jabber IDs to">>),
|
|
|
|
var = <<"muc#roomconfig_whois">>,
|
|
|
|
values = [if Config#config.anonymous -> <<"moderators">>;
|
|
|
|
true -> <<"anyone">>
|
|
|
|
end],
|
|
|
|
options = make_options(
|
|
|
|
[{<<"moderators only">>, <<"moderators">>},
|
|
|
|
{<<"anyone">>, <<"anyone">>}],
|
|
|
|
Lang)},
|
|
|
|
#xdata_field{type = 'list-multi',
|
|
|
|
label = translate:translate(
|
|
|
|
Lang,
|
|
|
|
<<"Roles for which Presence is Broadcasted">>),
|
|
|
|
var = <<"muc#roomconfig_presencebroadcast">>,
|
|
|
|
values = [atom_to_binary(Role, utf8)
|
|
|
|
|| Role <- Config#config.presence_broadcast],
|
|
|
|
options = make_options(
|
|
|
|
[{<<"Moderator">>, <<"moderator">>},
|
|
|
|
{<<"Participant">>, <<"participant">>},
|
|
|
|
{<<"Visitor">>, <<"visitor">>}],
|
|
|
|
Lang)},
|
|
|
|
?BOOLXFIELD(<<"Make room members-only">>,
|
|
|
|
<<"muc#roomconfig_membersonly">>,
|
|
|
|
(Config#config.members_only)),
|
|
|
|
?BOOLXFIELD(<<"Make room moderated">>,
|
|
|
|
<<"muc#roomconfig_moderatedroom">>,
|
|
|
|
(Config#config.moderated)),
|
|
|
|
?BOOLXFIELD(<<"Default users as participants">>,
|
|
|
|
<<"members_by_default">>,
|
|
|
|
(Config#config.members_by_default)),
|
|
|
|
?BOOLXFIELD(<<"Allow users to change the subject">>,
|
|
|
|
<<"muc#roomconfig_changesubject">>,
|
|
|
|
(Config#config.allow_change_subj)),
|
|
|
|
?BOOLXFIELD(<<"Allow users to send private messages">>,
|
|
|
|
<<"allow_private_messages">>,
|
|
|
|
(Config#config.allow_private_messages)),
|
|
|
|
#xdata_field{type = 'list-single',
|
|
|
|
label = translate:translate(
|
|
|
|
Lang,
|
|
|
|
<<"Allow visitors to send private messages to">>),
|
|
|
|
var = <<"allow_private_messages_from_visitors">>,
|
|
|
|
values = [case Config#config.allow_private_messages_from_visitors of
|
|
|
|
anyone -> <<"anyone">>;
|
|
|
|
moderators -> <<"moderators">>;
|
|
|
|
nobody -> <<"nobody">>
|
|
|
|
end],
|
|
|
|
options = make_options(
|
|
|
|
[{<<"nobody">>, <<"nobody">>},
|
|
|
|
{<<"moderators only">>, <<"moderators">>},
|
|
|
|
{<<"anyone">>, <<"anyone">>}],
|
|
|
|
Lang)},
|
|
|
|
?BOOLXFIELD(<<"Allow users to query other users">>,
|
|
|
|
<<"allow_query_users">>,
|
|
|
|
(Config#config.allow_query_users)),
|
|
|
|
?BOOLXFIELD(<<"Allow users to send invites">>,
|
|
|
|
<<"muc#roomconfig_allowinvites">>,
|
|
|
|
(Config#config.allow_user_invites)),
|
|
|
|
?BOOLXFIELD(<<"Allow visitors to send status text in "
|
|
|
|
"presence updates">>,
|
|
|
|
<<"muc#roomconfig_allowvisitorstatus">>,
|
|
|
|
(Config#config.allow_visitor_status)),
|
|
|
|
?BOOLXFIELD(<<"Allow visitors to change nickname">>,
|
|
|
|
<<"muc#roomconfig_allowvisitornickchange">>,
|
|
|
|
(Config#config.allow_visitor_nickchange)),
|
|
|
|
?BOOLXFIELD(<<"Allow visitors to send voice requests">>,
|
|
|
|
<<"muc#roomconfig_allowvoicerequests">>,
|
|
|
|
(Config#config.allow_voice_requests)),
|
|
|
|
?BOOLXFIELD(<<"Allow subscription">>,
|
|
|
|
<<"muc#roomconfig_allow_subscription">>,
|
|
|
|
(Config#config.allow_subscription)),
|
|
|
|
?STRINGXFIELD(<<"Minimum interval between voice requests "
|
|
|
|
"(in seconds)">>,
|
|
|
|
<<"muc#roomconfig_voicerequestmininterval">>,
|
|
|
|
integer_to_binary(Config#config.voice_request_min_interval))]
|
|
|
|
++
|
|
|
|
case ejabberd_captcha:is_feature_available() of
|
|
|
|
true ->
|
|
|
|
[?BOOLXFIELD(<<"Make room CAPTCHA protected">>,
|
|
|
|
<<"captcha_protected">>,
|
|
|
|
(Config#config.captcha_protected))];
|
|
|
|
false -> []
|
|
|
|
end ++
|
|
|
|
[?JIDMULTIXFIELD(<<"Exclude Jabber IDs from CAPTCHA challenge">>,
|
|
|
|
<<"muc#roomconfig_captcha_whitelist">>,
|
|
|
|
((?SETS):to_list(Config#config.captcha_whitelist)))]
|
|
|
|
++
|
|
|
|
case mod_muc_log:check_access_log(StateData#state.server_host, From) of
|
|
|
|
allow ->
|
|
|
|
[?BOOLXFIELD(<<"Enable logging">>,
|
|
|
|
<<"muc#roomconfig_enablelogging">>,
|
|
|
|
(Config#config.logging))];
|
|
|
|
deny -> []
|
|
|
|
end,
|
|
|
|
Fields = ejabberd_hooks:run_fold(get_room_config,
|
|
|
|
StateData#state.server_host,
|
|
|
|
Fs,
|
|
|
|
[StateData, From, Lang]),
|
|
|
|
#xdata{type = form, title = Title, fields = Fields}.
|
|
|
|
|
|
|
|
-spec set_config(xdata(), state(), binary()) -> {error, error()} |
|
|
|
|
{result, undefined, state()}.
|
|
|
|
set_config(#xdata{fields = Fields}, StateData, Lang) ->
|
|
|
|
Options = [{Var, Vals} || #xdata_field{var = Var, values = Vals} <- Fields],
|
|
|
|
case set_xoption(Options, StateData#state.config,
|
|
|
|
StateData#state.server_host, Lang) of
|
|
|
|
#config{} = Config ->
|
|
|
|
Res = change_config(Config, StateData),
|
|
|
|
{result, _, NSD} = Res,
|
|
|
|
Type = case {(StateData#state.config)#config.logging,
|
|
|
|
Config#config.logging}
|
|
|
|
of
|
|
|
|
{true, false} -> roomconfig_change_disabledlogging;
|
|
|
|
{false, true} -> roomconfig_change_enabledlogging;
|
|
|
|
{_, _} -> roomconfig_change
|
|
|
|
end,
|
|
|
|
Users = [{U#user.jid, U#user.nick, U#user.role}
|
|
|
|
|| {_, U} <- (?DICT):to_list(StateData#state.users)],
|
|
|
|
add_to_log(Type, Users, NSD),
|
|
|
|
Res;
|
|
|
|
Err -> Err
|
2003-03-25 22:03:35 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
-define(SET_BOOL_XOPT(Opt, Val),
|
|
|
|
case Val of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"0">> ->
|
2016-07-09 11:43:01 +02:00
|
|
|
set_xoption(Opts, Config#config{Opt = false}, ServerHost, Lang);
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"false">> ->
|
2016-07-09 11:43:01 +02:00
|
|
|
set_xoption(Opts, Config#config{Opt = false}, ServerHost, Lang);
|
|
|
|
<<"1">> -> set_xoption(Opts, Config#config{Opt = true}, ServerHost, Lang);
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"true">> ->
|
2016-07-09 11:43:01 +02:00
|
|
|
set_xoption(Opts, Config#config{Opt = true}, ServerHost, Lang);
|
2016-04-05 12:09:44 +02:00
|
|
|
_ ->
|
|
|
|
Txt = <<"Value of '~s' should be boolean">>,
|
|
|
|
ErrTxt = iolist_to_binary(io_lib:format(Txt, [Opt])),
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, xmpp:err_bad_request(ErrTxt, Lang)}
|
2003-03-25 22:03:35 +01:00
|
|
|
end).
|
|
|
|
|
2007-08-29 19:54:45 +02:00
|
|
|
-define(SET_NAT_XOPT(Opt, Val),
|
2016-07-25 12:50:30 +02:00
|
|
|
case catch binary_to_integer(Val) of
|
2013-03-14 10:33:02 +01:00
|
|
|
I when is_integer(I), I > 0 ->
|
2016-07-09 11:43:01 +02:00
|
|
|
set_xoption(Opts, Config#config{Opt = I}, ServerHost, Lang);
|
2016-04-05 12:09:44 +02:00
|
|
|
_ ->
|
|
|
|
Txt = <<"Value of '~s' should be integer">>,
|
|
|
|
ErrTxt = iolist_to_binary(io_lib:format(Txt, [Opt])),
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, xmpp:err_bad_request(ErrTxt, Lang)}
|
2007-08-29 19:54:45 +02:00
|
|
|
end).
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-define(SET_STRING_XOPT(Opt, Vals),
|
|
|
|
try
|
|
|
|
V = case Vals of
|
|
|
|
[] -> <<"">>;
|
|
|
|
[Val] -> Val;
|
|
|
|
_ when is_atom(Vals) -> Vals
|
|
|
|
end,
|
|
|
|
set_xoption(Opts, Config#config{Opt = V}, ServerHost, Lang)
|
|
|
|
catch _:_ ->
|
|
|
|
Txt = <<"Incorrect value of option '~s'">>,
|
|
|
|
ErrTxt = iolist_to_binary(io_lib:format(Txt, [Opt])),
|
|
|
|
{error, xmpp:err_bad_request(ErrTxt, Lang)}
|
|
|
|
end).
|
2003-03-27 21:55:09 +01:00
|
|
|
|
2011-02-16 11:14:39 +01:00
|
|
|
-define(SET_JIDMULTI_XOPT(Opt, Vals),
|
2013-03-14 10:33:02 +01:00
|
|
|
begin
|
|
|
|
Set = lists:foldl(fun ({U, S, R}, Set1) ->
|
|
|
|
(?SETS):add_element({U, S, R}, Set1);
|
|
|
|
(#jid{luser = U, lserver = S, lresource = R},
|
|
|
|
Set1) ->
|
|
|
|
(?SETS):add_element({U, S, R}, Set1);
|
|
|
|
(_, Set1) -> Set1
|
|
|
|
end,
|
|
|
|
(?SETS):empty(), Vals),
|
2016-07-09 11:43:01 +02:00
|
|
|
set_xoption(Opts, Config#config{Opt = Set}, ServerHost, Lang)
|
2013-03-14 10:33:02 +01:00
|
|
|
end).
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec set_xoption([{binary(), [binary()]}], #config{},
|
|
|
|
binary(), binary()) -> #config{} | {error, error()}.
|
2016-07-09 11:43:01 +02:00
|
|
|
set_xoption([], Config, _ServerHost, _Lang) -> Config;
|
2016-07-25 12:50:30 +02:00
|
|
|
set_xoption([{<<"muc#roomconfig_roomname">>, Vals}
|
2013-03-14 10:33:02 +01:00
|
|
|
| Opts],
|
2016-07-09 11:43:01 +02:00
|
|
|
Config, ServerHost, Lang) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
?SET_STRING_XOPT(title, Vals);
|
|
|
|
set_xoption([{<<"muc#roomconfig_roomdesc">>, Vals}
|
2013-03-14 10:33:02 +01:00
|
|
|
| Opts],
|
2016-07-09 11:43:01 +02:00
|
|
|
Config, ServerHost, Lang) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
?SET_STRING_XOPT(description, Vals);
|
2013-03-14 10:33:02 +01:00
|
|
|
set_xoption([{<<"muc#roomconfig_changesubject">>, [Val]}
|
|
|
|
| Opts],
|
2016-07-09 11:43:01 +02:00
|
|
|
Config, ServerHost, Lang) ->
|
2003-03-25 22:03:35 +01:00
|
|
|
?SET_BOOL_XOPT(allow_change_subj, Val);
|
2013-03-14 10:33:02 +01:00
|
|
|
set_xoption([{<<"allow_query_users">>, [Val]} | Opts],
|
2016-07-09 11:43:01 +02:00
|
|
|
Config, ServerHost, Lang) ->
|
2003-03-25 22:03:35 +01:00
|
|
|
?SET_BOOL_XOPT(allow_query_users, Val);
|
2013-03-14 10:33:02 +01:00
|
|
|
set_xoption([{<<"allow_private_messages">>, [Val]}
|
|
|
|
| Opts],
|
2016-07-09 11:43:01 +02:00
|
|
|
Config, ServerHost, Lang) ->
|
2003-03-25 22:03:35 +01:00
|
|
|
?SET_BOOL_XOPT(allow_private_messages, Val);
|
2013-03-14 10:33:02 +01:00
|
|
|
set_xoption([{<<"allow_private_messages_from_visitors">>,
|
|
|
|
[Val]}
|
|
|
|
| Opts],
|
2016-07-09 11:43:01 +02:00
|
|
|
Config, ServerHost, Lang) ->
|
2011-07-11 17:33:26 +02:00
|
|
|
case Val of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"anyone">> ->
|
|
|
|
?SET_STRING_XOPT(allow_private_messages_from_visitors,
|
|
|
|
anyone);
|
|
|
|
<<"moderators">> ->
|
|
|
|
?SET_STRING_XOPT(allow_private_messages_from_visitors,
|
|
|
|
moderators);
|
|
|
|
<<"nobody">> ->
|
|
|
|
?SET_STRING_XOPT(allow_private_messages_from_visitors,
|
|
|
|
nobody);
|
2016-04-05 12:09:44 +02:00
|
|
|
_ ->
|
|
|
|
Txt = <<"Value of 'allow_private_messages_from_visitors' "
|
|
|
|
"should be anyone|moderators|nobody">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, xmpp:err_bad_request(Txt, Lang)}
|
2011-07-11 17:33:26 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
set_xoption([{<<"muc#roomconfig_allowvisitorstatus">>,
|
|
|
|
[Val]}
|
|
|
|
| Opts],
|
2016-07-09 11:43:01 +02:00
|
|
|
Config, ServerHost, Lang) ->
|
2008-07-23 18:55:46 +02:00
|
|
|
?SET_BOOL_XOPT(allow_visitor_status, Val);
|
2013-03-14 10:33:02 +01:00
|
|
|
set_xoption([{<<"muc#roomconfig_allowvisitornickchange">>,
|
|
|
|
[Val]}
|
|
|
|
| Opts],
|
2016-07-09 11:43:01 +02:00
|
|
|
Config, ServerHost, Lang) ->
|
2008-07-23 14:31:55 +02:00
|
|
|
?SET_BOOL_XOPT(allow_visitor_nickchange, Val);
|
2013-03-14 10:33:02 +01:00
|
|
|
set_xoption([{<<"muc#roomconfig_publicroom">>, [Val]}
|
|
|
|
| Opts],
|
2016-07-09 11:43:01 +02:00
|
|
|
Config, ServerHost, Lang) ->
|
2003-03-25 22:03:35 +01:00
|
|
|
?SET_BOOL_XOPT(public, Val);
|
2013-03-14 10:33:02 +01:00
|
|
|
set_xoption([{<<"public_list">>, [Val]} | Opts],
|
2016-07-09 11:43:01 +02:00
|
|
|
Config, ServerHost, Lang) ->
|
2003-05-15 20:16:13 +02:00
|
|
|
?SET_BOOL_XOPT(public_list, Val);
|
2013-03-14 10:33:02 +01:00
|
|
|
set_xoption([{<<"muc#roomconfig_persistentroom">>,
|
|
|
|
[Val]}
|
|
|
|
| Opts],
|
2016-07-09 11:43:01 +02:00
|
|
|
Config, ServerHost, Lang) ->
|
2003-03-25 22:03:35 +01:00
|
|
|
?SET_BOOL_XOPT(persistent, Val);
|
2013-03-14 10:33:02 +01:00
|
|
|
set_xoption([{<<"muc#roomconfig_moderatedroom">>, [Val]}
|
|
|
|
| Opts],
|
2016-07-09 11:43:01 +02:00
|
|
|
Config, ServerHost, Lang) ->
|
2003-03-25 22:03:35 +01:00
|
|
|
?SET_BOOL_XOPT(moderated, Val);
|
2013-03-14 10:33:02 +01:00
|
|
|
set_xoption([{<<"members_by_default">>, [Val]} | Opts],
|
2016-07-09 11:43:01 +02:00
|
|
|
Config, ServerHost, Lang) ->
|
2003-03-25 22:03:35 +01:00
|
|
|
?SET_BOOL_XOPT(members_by_default, Val);
|
2013-03-14 10:33:02 +01:00
|
|
|
set_xoption([{<<"muc#roomconfig_membersonly">>, [Val]}
|
|
|
|
| Opts],
|
2016-07-09 11:43:01 +02:00
|
|
|
Config, ServerHost, Lang) ->
|
2003-03-25 22:03:35 +01:00
|
|
|
?SET_BOOL_XOPT(members_only, Val);
|
2013-03-14 10:33:02 +01:00
|
|
|
set_xoption([{<<"captcha_protected">>, [Val]} | Opts],
|
2016-07-09 11:43:01 +02:00
|
|
|
Config, ServerHost, Lang) ->
|
2009-03-13 17:01:46 +01:00
|
|
|
?SET_BOOL_XOPT(captcha_protected, Val);
|
2013-03-14 10:33:02 +01:00
|
|
|
set_xoption([{<<"muc#roomconfig_allowinvites">>, [Val]}
|
|
|
|
| Opts],
|
2016-07-09 11:43:01 +02:00
|
|
|
Config, ServerHost, Lang) ->
|
2003-03-25 22:03:35 +01:00
|
|
|
?SET_BOOL_XOPT(allow_user_invites, Val);
|
2016-06-26 08:08:37 +02:00
|
|
|
set_xoption([{<<"muc#roomconfig_allow_subscription">>, [Val]}
|
|
|
|
| Opts],
|
2016-07-09 11:43:01 +02:00
|
|
|
Config, ServerHost, Lang) ->
|
2016-06-26 08:08:37 +02:00
|
|
|
?SET_BOOL_XOPT(allow_subscription, Val);
|
2013-03-14 10:33:02 +01:00
|
|
|
set_xoption([{<<"muc#roomconfig_passwordprotectedroom">>,
|
|
|
|
[Val]}
|
|
|
|
| Opts],
|
2016-07-09 11:43:01 +02:00
|
|
|
Config, ServerHost, Lang) ->
|
2003-03-25 22:03:35 +01:00
|
|
|
?SET_BOOL_XOPT(password_protected, Val);
|
2016-07-25 12:50:30 +02:00
|
|
|
set_xoption([{<<"muc#roomconfig_roomsecret">>, Vals}
|
2013-03-14 10:33:02 +01:00
|
|
|
| Opts],
|
2016-07-09 11:43:01 +02:00
|
|
|
Config, ServerHost, Lang) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
?SET_STRING_XOPT(password, Vals);
|
2013-03-14 10:33:02 +01:00
|
|
|
set_xoption([{<<"anonymous">>, [Val]} | Opts],
|
2016-07-09 11:43:01 +02:00
|
|
|
Config, ServerHost, Lang) ->
|
2003-03-25 22:03:35 +01:00
|
|
|
?SET_BOOL_XOPT(anonymous, Val);
|
2015-11-12 17:51:20 +01:00
|
|
|
set_xoption([{<<"muc#roomconfig_presencebroadcast">>, Vals} | Opts],
|
2016-07-09 11:43:01 +02:00
|
|
|
Config, ServerHost, Lang) ->
|
2015-11-12 17:51:20 +01:00
|
|
|
Roles =
|
|
|
|
lists:foldl(
|
|
|
|
fun(_S, error) -> error;
|
|
|
|
(S, {M, P, V}) ->
|
|
|
|
case S of
|
|
|
|
<<"moderator">> -> {true, P, V};
|
|
|
|
<<"participant">> -> {M, true, V};
|
|
|
|
<<"visitor">> -> {M, P, true};
|
|
|
|
_ -> error
|
|
|
|
end
|
|
|
|
end, {false, false, false}, Vals),
|
|
|
|
case Roles of
|
2016-04-05 12:09:44 +02:00
|
|
|
error ->
|
|
|
|
Txt = <<"Value of 'muc#roomconfig_presencebroadcast' should "
|
|
|
|
"be moderator|participant|visitor">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, xmpp:err_bad_request(Txt, Lang)};
|
2015-11-12 17:51:20 +01:00
|
|
|
{M, P, V} ->
|
|
|
|
Res =
|
|
|
|
if M -> [moderator]; true -> [] end ++
|
|
|
|
if P -> [participant]; true -> [] end ++
|
|
|
|
if V -> [visitor]; true -> [] end,
|
2016-07-09 11:43:01 +02:00
|
|
|
set_xoption(Opts, Config#config{presence_broadcast = Res},
|
|
|
|
ServerHost, Lang)
|
2015-11-12 17:51:20 +01:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
set_xoption([{<<"muc#roomconfig_allowvoicerequests">>,
|
|
|
|
[Val]}
|
|
|
|
| Opts],
|
2016-07-09 11:43:01 +02:00
|
|
|
Config, ServerHost, Lang) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
?SET_BOOL_XOPT(allow_voice_requests, Val);
|
|
|
|
set_xoption([{<<"muc#roomconfig_voicerequestmininterval">>,
|
|
|
|
[Val]}
|
|
|
|
| Opts],
|
2016-07-09 11:43:01 +02:00
|
|
|
Config, ServerHost, Lang) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
?SET_NAT_XOPT(voice_request_min_interval, Val);
|
|
|
|
set_xoption([{<<"muc#roomconfig_whois">>, [Val]}
|
|
|
|
| Opts],
|
2016-07-09 11:43:01 +02:00
|
|
|
Config, ServerHost, Lang) ->
|
2006-08-04 03:57:51 +02:00
|
|
|
case Val of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"moderators">> ->
|
|
|
|
?SET_BOOL_XOPT(anonymous,
|
|
|
|
(iolist_to_binary(integer_to_list(1))));
|
|
|
|
<<"anyone">> ->
|
|
|
|
?SET_BOOL_XOPT(anonymous,
|
|
|
|
(iolist_to_binary(integer_to_list(0))));
|
2016-04-05 12:09:44 +02:00
|
|
|
_ ->
|
|
|
|
Txt = <<"Value of 'muc#roomconfig_whois' should be "
|
|
|
|
"moderators|anyone">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, xmpp:err_bad_request(Txt, Lang)}
|
2006-08-04 03:57:51 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
set_xoption([{<<"muc#roomconfig_maxusers">>, [Val]}
|
|
|
|
| Opts],
|
2016-07-09 11:43:01 +02:00
|
|
|
Config, ServerHost, Lang) ->
|
2007-08-28 16:35:50 +02:00
|
|
|
case Val of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"none">> -> ?SET_STRING_XOPT(max_users, none);
|
|
|
|
_ -> ?SET_NAT_XOPT(max_users, Val)
|
2007-08-28 16:35:50 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
set_xoption([{<<"muc#roomconfig_enablelogging">>, [Val]}
|
|
|
|
| Opts],
|
2016-07-09 11:43:01 +02:00
|
|
|
Config, ServerHost, Lang) ->
|
2003-03-25 22:03:35 +01:00
|
|
|
?SET_BOOL_XOPT(logging, Val);
|
2013-03-14 10:33:02 +01:00
|
|
|
set_xoption([{<<"muc#roomconfig_captcha_whitelist">>,
|
|
|
|
Vals}
|
|
|
|
| Opts],
|
2016-07-09 11:43:01 +02:00
|
|
|
Config, ServerHost, Lang) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
JIDs = [jid:from_string(Val) || Val <- Vals],
|
2011-02-16 11:14:39 +01:00
|
|
|
?SET_JIDMULTI_XOPT(captcha_whitelist, JIDs);
|
2016-07-09 11:43:01 +02:00
|
|
|
set_xoption([{<<"FORM_TYPE">>, _} | Opts], Config, ServerHost, Lang) ->
|
|
|
|
set_xoption(Opts, Config, ServerHost, Lang);
|
|
|
|
set_xoption([{Opt, Vals} | Opts], Config, ServerHost, Lang) ->
|
2016-04-05 12:09:44 +02:00
|
|
|
Txt = <<"Unknown option '~s'">>,
|
|
|
|
ErrTxt = iolist_to_binary(io_lib:format(Txt, [Opt])),
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = {error, xmpp:err_bad_request(ErrTxt, Lang)},
|
2016-07-09 11:43:01 +02:00
|
|
|
case ejabberd_hooks:run_fold(set_room_option,
|
|
|
|
ServerHost,
|
|
|
|
Err,
|
|
|
|
[Opt, Vals, Lang]) of
|
|
|
|
{error, Reason} ->
|
|
|
|
{error, Reason};
|
|
|
|
{Pos, Val} ->
|
|
|
|
set_xoption(Opts, setelement(Pos, Config, Val), ServerHost, Lang)
|
|
|
|
end.
|
2003-03-25 22:03:35 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec change_config(#config{}, state()) -> {result, undefined, state()}.
|
2003-03-25 22:03:35 +01:00
|
|
|
change_config(Config, StateData) ->
|
2015-07-02 00:36:16 +02:00
|
|
|
send_config_change_info(Config, StateData),
|
2016-06-26 08:08:37 +02:00
|
|
|
NSD = remove_subscriptions(StateData#state{config = Config}),
|
2003-03-25 22:03:35 +01:00
|
|
|
case {(StateData#state.config)#config.persistent,
|
2013-03-14 10:33:02 +01:00
|
|
|
Config#config.persistent}
|
|
|
|
of
|
|
|
|
{_, true} ->
|
|
|
|
mod_muc:store_room(NSD#state.server_host,
|
|
|
|
NSD#state.host, NSD#state.room, make_opts(NSD));
|
|
|
|
{true, false} ->
|
|
|
|
mod_muc:forget_room(NSD#state.server_host,
|
|
|
|
NSD#state.host, NSD#state.room);
|
|
|
|
{false, false} -> ok
|
2006-02-06 06:12:54 +01:00
|
|
|
end,
|
|
|
|
case {(StateData#state.config)#config.members_only,
|
2013-03-14 10:33:02 +01:00
|
|
|
Config#config.members_only}
|
|
|
|
of
|
|
|
|
{false, true} ->
|
2016-07-25 12:50:30 +02:00
|
|
|
NSD1 = remove_nonmembers(NSD), {result, undefined, NSD1};
|
|
|
|
_ -> {result, undefined, NSD}
|
2006-02-06 06:12:54 +01:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec send_config_change_info(#config{}, state()) -> ok.
|
2015-07-02 00:36:16 +02:00
|
|
|
send_config_change_info(Config, #state{config = Config}) -> ok;
|
|
|
|
send_config_change_info(New, #state{config = Old} = StateData) ->
|
|
|
|
Codes = case {Old#config.logging, New#config.logging} of
|
2016-07-25 12:50:30 +02:00
|
|
|
{false, true} -> [170];
|
|
|
|
{true, false} -> [171];
|
2015-07-02 00:36:16 +02:00
|
|
|
_ -> []
|
|
|
|
end
|
|
|
|
++
|
|
|
|
case {Old#config.anonymous, New#config.anonymous} of
|
2016-07-25 12:50:30 +02:00
|
|
|
{true, false} -> [172];
|
|
|
|
{false, true} -> [173];
|
2015-07-02 00:36:16 +02:00
|
|
|
_ -> []
|
|
|
|
end
|
|
|
|
++
|
|
|
|
case Old#config{anonymous = New#config.anonymous,
|
|
|
|
logging = New#config.logging} of
|
|
|
|
New -> [];
|
2016-07-25 12:50:30 +02:00
|
|
|
_ -> [104]
|
2015-07-02 00:36:16 +02:00
|
|
|
end,
|
2016-07-25 12:50:30 +02:00
|
|
|
Message = #message{type = groupchat,
|
|
|
|
id = randoms:get_string(),
|
|
|
|
sub_els = [#muc_user{status_codes = Codes}]},
|
2016-06-26 08:08:37 +02:00
|
|
|
send_wrapped_multiple(StateData#state.jid,
|
|
|
|
StateData#state.users,
|
|
|
|
Message,
|
|
|
|
?NS_MUCSUB_NODES_CONFIG,
|
|
|
|
StateData).
|
2015-07-02 00:36:16 +02:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec remove_nonmembers(state()) -> state().
|
2006-02-06 06:12:54 +01:00
|
|
|
remove_nonmembers(StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:foldl(fun ({_LJID, #user{jid = JID}}, SD) ->
|
|
|
|
Affiliation = get_affiliation(JID, SD),
|
|
|
|
case Affiliation of
|
|
|
|
none ->
|
2013-04-24 11:00:04 +02:00
|
|
|
catch send_kickban_presence(<<"">>, JID, <<"">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
322, SD),
|
2013-03-14 10:33:02 +01:00
|
|
|
set_role(JID, none, SD);
|
|
|
|
_ -> SD
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
StateData, (?DICT):to_list(StateData#state.users)).
|
2003-03-25 22:03:35 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec set_opts([{atom(), any()}], state()) -> state().
|
2013-03-14 10:33:02 +01:00
|
|
|
set_opts([], StateData) -> StateData;
|
2003-03-25 22:03:35 +01:00
|
|
|
set_opts([{Opt, Val} | Opts], StateData) ->
|
|
|
|
NSD = case Opt of
|
2013-03-14 10:33:02 +01:00
|
|
|
title ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{title =
|
|
|
|
Val}};
|
|
|
|
description ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{description
|
|
|
|
= Val}};
|
|
|
|
allow_change_subj ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{allow_change_subj
|
|
|
|
= Val}};
|
|
|
|
allow_query_users ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{allow_query_users
|
|
|
|
= Val}};
|
|
|
|
allow_private_messages ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{allow_private_messages
|
|
|
|
= Val}};
|
|
|
|
allow_private_messages_from_visitors ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{allow_private_messages_from_visitors
|
|
|
|
= Val}};
|
|
|
|
allow_visitor_nickchange ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{allow_visitor_nickchange
|
|
|
|
= Val}};
|
|
|
|
allow_visitor_status ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{allow_visitor_status
|
|
|
|
= Val}};
|
|
|
|
public ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{public =
|
|
|
|
Val}};
|
|
|
|
public_list ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{public_list
|
|
|
|
= Val}};
|
|
|
|
persistent ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{persistent =
|
|
|
|
Val}};
|
|
|
|
moderated ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{moderated =
|
|
|
|
Val}};
|
|
|
|
members_by_default ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{members_by_default
|
|
|
|
= Val}};
|
|
|
|
members_only ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{members_only
|
|
|
|
= Val}};
|
|
|
|
allow_user_invites ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{allow_user_invites
|
|
|
|
= Val}};
|
|
|
|
password_protected ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{password_protected
|
|
|
|
= Val}};
|
|
|
|
captcha_protected ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{captcha_protected
|
|
|
|
= Val}};
|
|
|
|
password ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{password =
|
|
|
|
Val}};
|
|
|
|
anonymous ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{anonymous =
|
|
|
|
Val}};
|
2015-11-12 17:51:20 +01:00
|
|
|
presence_broadcast ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{presence_broadcast =
|
|
|
|
Val}};
|
2013-03-14 10:33:02 +01:00
|
|
|
logging ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{logging =
|
|
|
|
Val}};
|
2015-08-06 12:33:39 +02:00
|
|
|
mam ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{mam = Val}};
|
2013-03-14 10:33:02 +01:00
|
|
|
captcha_whitelist ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{captcha_whitelist
|
|
|
|
=
|
|
|
|
(?SETS):from_list(Val)}};
|
|
|
|
allow_voice_requests ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{allow_voice_requests
|
|
|
|
= Val}};
|
|
|
|
voice_request_min_interval ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{voice_request_min_interval
|
|
|
|
= Val}};
|
|
|
|
max_users ->
|
|
|
|
ServiceMaxUsers = get_service_max_users(StateData),
|
|
|
|
MaxUsers = if Val =< ServiceMaxUsers -> Val;
|
|
|
|
true -> ServiceMaxUsers
|
|
|
|
end,
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{max_users =
|
|
|
|
MaxUsers}};
|
2014-05-04 21:11:05 +02:00
|
|
|
vcard ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{vcard =
|
|
|
|
Val}};
|
2016-06-26 08:08:37 +02:00
|
|
|
allow_subscription ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{allow_subscription = Val}};
|
|
|
|
subscribers ->
|
|
|
|
lists:foldl(
|
|
|
|
fun({JID, Nick, Nodes}, State) ->
|
|
|
|
User = #user{jid = JID, nick = Nick,
|
|
|
|
subscriptions = Nodes,
|
|
|
|
is_subscriber = true,
|
|
|
|
role = none},
|
|
|
|
update_online_user(JID, User, State)
|
|
|
|
end, StateData, Val);
|
2013-03-14 10:33:02 +01:00
|
|
|
affiliations ->
|
|
|
|
StateData#state{affiliations = (?DICT):from_list(Val)};
|
|
|
|
subject -> StateData#state{subject = Val};
|
|
|
|
subject_author -> StateData#state{subject_author = Val};
|
|
|
|
_ -> StateData
|
2003-03-25 22:03:35 +01:00
|
|
|
end,
|
|
|
|
set_opts(Opts, NSD).
|
|
|
|
|
|
|
|
-define(MAKE_CONFIG_OPT(Opt), {Opt, Config#config.Opt}).
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec make_opts(state()) -> [{atom(), any()}].
|
2003-03-25 22:03:35 +01:00
|
|
|
make_opts(StateData) ->
|
|
|
|
Config = StateData#state.config,
|
2016-06-26 08:08:37 +02:00
|
|
|
Subscribers = (?DICT):fold(
|
|
|
|
fun(_LJID, #user{is_subscriber = true} = User, Acc) ->
|
|
|
|
[{User#user.jid, User#user.nick,
|
|
|
|
User#user.subscriptions}|Acc];
|
|
|
|
(_, _, Acc) ->
|
|
|
|
Acc
|
|
|
|
end, [], StateData#state.users),
|
2013-03-14 10:33:02 +01:00
|
|
|
[?MAKE_CONFIG_OPT(title), ?MAKE_CONFIG_OPT(description),
|
2003-03-25 22:03:35 +01:00
|
|
|
?MAKE_CONFIG_OPT(allow_change_subj),
|
|
|
|
?MAKE_CONFIG_OPT(allow_query_users),
|
|
|
|
?MAKE_CONFIG_OPT(allow_private_messages),
|
2011-07-11 17:33:26 +02:00
|
|
|
?MAKE_CONFIG_OPT(allow_private_messages_from_visitors),
|
2008-07-23 18:55:46 +02:00
|
|
|
?MAKE_CONFIG_OPT(allow_visitor_status),
|
2008-07-23 14:31:55 +02:00
|
|
|
?MAKE_CONFIG_OPT(allow_visitor_nickchange),
|
2013-03-14 10:33:02 +01:00
|
|
|
?MAKE_CONFIG_OPT(public), ?MAKE_CONFIG_OPT(public_list),
|
2003-03-25 22:03:35 +01:00
|
|
|
?MAKE_CONFIG_OPT(persistent),
|
|
|
|
?MAKE_CONFIG_OPT(moderated),
|
|
|
|
?MAKE_CONFIG_OPT(members_by_default),
|
|
|
|
?MAKE_CONFIG_OPT(members_only),
|
|
|
|
?MAKE_CONFIG_OPT(allow_user_invites),
|
|
|
|
?MAKE_CONFIG_OPT(password_protected),
|
2009-03-13 17:01:46 +01:00
|
|
|
?MAKE_CONFIG_OPT(captcha_protected),
|
2013-03-14 10:33:02 +01:00
|
|
|
?MAKE_CONFIG_OPT(password), ?MAKE_CONFIG_OPT(anonymous),
|
|
|
|
?MAKE_CONFIG_OPT(logging), ?MAKE_CONFIG_OPT(max_users),
|
2011-07-29 20:22:06 +02:00
|
|
|
?MAKE_CONFIG_OPT(allow_voice_requests),
|
2015-08-12 20:23:28 +02:00
|
|
|
?MAKE_CONFIG_OPT(mam),
|
2011-07-29 20:22:06 +02:00
|
|
|
?MAKE_CONFIG_OPT(voice_request_min_interval),
|
2014-05-04 21:11:05 +02:00
|
|
|
?MAKE_CONFIG_OPT(vcard),
|
2011-02-16 11:14:39 +01:00
|
|
|
{captcha_whitelist,
|
2013-03-14 10:33:02 +01:00
|
|
|
(?SETS):to_list((StateData#state.config)#config.captcha_whitelist)},
|
|
|
|
{affiliations,
|
|
|
|
(?DICT):to_list(StateData#state.affiliations)},
|
2003-05-15 20:16:13 +02:00
|
|
|
{subject, StateData#state.subject},
|
2016-06-26 08:08:37 +02:00
|
|
|
{subject_author, StateData#state.subject_author},
|
|
|
|
{subscribers, Subscribers}].
|
2003-03-27 21:55:09 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec destroy_room(muc_destroy(), state()) -> {result, undefined, stop}.
|
2006-01-19 03:17:31 +01:00
|
|
|
destroy_room(DEl, StateData) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
Destroy = DEl#muc_destroy{xmlns = ?NS_MUC_USER},
|
|
|
|
lists:foreach(
|
|
|
|
fun({_LJID, Info}) ->
|
|
|
|
Nick = Info#user.nick,
|
|
|
|
Item = #muc_item{affiliation = none,
|
|
|
|
role = none},
|
|
|
|
Packet = #presence{
|
|
|
|
type = unavailable,
|
|
|
|
sub_els = [#muc_user{items = [Item],
|
|
|
|
destroy = Destroy}]},
|
|
|
|
send_wrapped(jid:replace_resource(StateData#state.jid, Nick),
|
|
|
|
Info#user.jid, Packet,
|
|
|
|
?NS_MUCSUB_NODES_CONFIG, StateData)
|
|
|
|
end,
|
|
|
|
(?DICT):to_list(StateData#state.users)),
|
2004-10-08 22:40:29 +02:00
|
|
|
case (StateData#state.config)#config.persistent of
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
|
|
|
mod_muc:forget_room(StateData#state.server_host,
|
|
|
|
StateData#state.host, StateData#state.room);
|
|
|
|
false -> ok
|
|
|
|
end,
|
2016-07-25 12:50:30 +02:00
|
|
|
{result, undefined, stop}.
|
2003-03-27 21:55:09 +01:00
|
|
|
|
2003-03-27 15:06:17 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% Disco
|
|
|
|
|
2004-01-17 21:26:57 +01:00
|
|
|
-define(CONFIG_OPT_TO_FEATURE(Opt, Fiftrue, Fiffalse),
|
2013-03-14 10:33:02 +01:00
|
|
|
case Opt of
|
2016-07-25 12:50:30 +02:00
|
|
|
true -> Fiftrue;
|
|
|
|
false -> Fiffalse
|
2013-03-14 10:33:02 +01:00
|
|
|
end).
|
2004-01-17 21:26:57 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec process_iq_disco_info(jid(), iq(), state()) ->
|
|
|
|
{result, disco_info()} | {error, error()}.
|
|
|
|
process_iq_disco_info(_From, #iq{type = set, lang = Lang}, _StateData) ->
|
2016-04-05 12:09:44 +02:00
|
|
|
Txt = <<"Value 'set' of 'type' attribute is not allowed">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, xmpp:err_not_allowed(Txt, Lang)};
|
|
|
|
process_iq_disco_info(_From, #iq{type = get, lang = Lang}, StateData) ->
|
2004-01-17 21:26:57 +01:00
|
|
|
Config = StateData#state.config,
|
2016-07-25 12:50:30 +02:00
|
|
|
Feats = [?NS_VCARD, ?NS_MUC,
|
|
|
|
?CONFIG_OPT_TO_FEATURE((Config#config.public),
|
|
|
|
<<"muc_public">>, <<"muc_hidden">>),
|
|
|
|
?CONFIG_OPT_TO_FEATURE((Config#config.persistent),
|
|
|
|
<<"muc_persistent">>, <<"muc_temporary">>),
|
|
|
|
?CONFIG_OPT_TO_FEATURE((Config#config.members_only),
|
|
|
|
<<"muc_membersonly">>, <<"muc_open">>),
|
|
|
|
?CONFIG_OPT_TO_FEATURE((Config#config.anonymous),
|
|
|
|
<<"muc_semianonymous">>, <<"muc_nonanonymous">>),
|
|
|
|
?CONFIG_OPT_TO_FEATURE((Config#config.moderated),
|
|
|
|
<<"muc_moderated">>, <<"muc_unmoderated">>),
|
|
|
|
?CONFIG_OPT_TO_FEATURE((Config#config.password_protected),
|
|
|
|
<<"muc_passwordprotected">>, <<"muc_unsecured">>)]
|
|
|
|
++ case Config#config.allow_subscription of
|
|
|
|
true -> [?NS_MUCSUB];
|
|
|
|
false -> []
|
|
|
|
end
|
|
|
|
++ case {gen_mod:is_loaded(StateData#state.server_host, mod_mam),
|
|
|
|
Config#config.mam} of
|
|
|
|
{true, true} ->
|
|
|
|
[?NS_MAM_TMP, ?NS_MAM_0, ?NS_MAM_1];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
{result, #disco_info{xdata = [iq_disco_info_extras(Lang, StateData)],
|
|
|
|
identities = [#identity{category = <<"conference">>,
|
|
|
|
type = <<"text">>,
|
|
|
|
name = get_title(StateData)}],
|
|
|
|
features = Feats}}.
|
|
|
|
|
|
|
|
-spec mk_rfieldt('boolean' | 'fixed' | 'hidden' |
|
|
|
|
'jid-multi' | 'jid-single' | 'list-multi' |
|
|
|
|
'list-single' | 'text-multi' | 'text-private' |
|
|
|
|
'text-single', binary(), binary()) -> xdata_field().
|
|
|
|
mk_rfieldt(Type, Var, Val) ->
|
|
|
|
#xdata_field{type = Type, var = Var, values = [Val]}.
|
|
|
|
|
|
|
|
-spec mk_rfield(binary(), binary(), binary(), binary()) -> xdata_field().
|
|
|
|
mk_rfield(Label, Var, Val, Lang) ->
|
|
|
|
#xdata_field{type = 'text-single',
|
|
|
|
label = translate:translate(Lang, Label),
|
|
|
|
var = Var,
|
|
|
|
values = [Val]}.
|
|
|
|
|
|
|
|
-spec iq_disco_info_extras(binary(), state()) -> xdata().
|
2006-01-19 03:17:31 +01:00
|
|
|
iq_disco_info_extras(Lang, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Len = (?DICT):size(StateData#state.users),
|
2016-07-25 12:50:30 +02:00
|
|
|
RoomDescription = (StateData#state.config)#config.description,
|
|
|
|
#xdata{type = result,
|
|
|
|
fields = [mk_rfieldt(hidden, <<"FORM_TYPE">>,
|
|
|
|
"http://jabber.org/protocol/muc#roominfo"),
|
|
|
|
mk_rfield(<<"Room description">>,
|
|
|
|
<<"muc#roominfo_description">>,
|
|
|
|
RoomDescription, Lang),
|
|
|
|
mk_rfield(<<"Number of occupants">>,
|
|
|
|
<<"muc#roominfo_occupants">>,
|
|
|
|
integer_to_binary(Len), Lang)]}.
|
|
|
|
|
|
|
|
-spec process_iq_disco_items(jid(), iq(), state()) ->
|
|
|
|
{error, error()} | {result, disco_items()}.
|
|
|
|
process_iq_disco_items(_From, #iq{type = set, lang = Lang}, _StateData) ->
|
2016-04-05 12:09:44 +02:00
|
|
|
Txt = <<"Value 'set' of 'type' attribute is not allowed">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, xmpp:err_not_allowed(Txt, Lang)};
|
|
|
|
process_iq_disco_items(From, #iq{type = get, lang = Lang}, StateData) ->
|
2010-03-29 20:51:24 +02:00
|
|
|
case (StateData#state.config)#config.public_list of
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
2016-07-25 12:50:30 +02:00
|
|
|
{result, get_mucroom_disco_items(StateData)};
|
2013-03-14 10:33:02 +01:00
|
|
|
_ ->
|
|
|
|
case is_occupant_or_admin(From, StateData) of
|
|
|
|
true ->
|
2016-07-25 12:50:30 +02:00
|
|
|
{result, get_mucroom_disco_items(StateData)};
|
2016-04-05 12:09:44 +02:00
|
|
|
_ ->
|
|
|
|
Txt = <<"Only occupants or administrators can perform this query">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, xmpp:err_forbidden(Txt, Lang)}
|
2013-03-14 10:33:02 +01:00
|
|
|
end
|
2010-03-29 20:51:24 +02:00
|
|
|
end.
|
2003-03-27 15:06:17 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec process_iq_captcha(jid(), iq(), state()) -> {error, error()} |
|
|
|
|
{result, undefined}.
|
|
|
|
process_iq_captcha(_From, #iq{type = get, lang = Lang}, _StateData) ->
|
2016-04-05 12:09:44 +02:00
|
|
|
Txt = <<"Value 'get' of 'type' attribute is not allowed">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, xmpp:err_not_allowed(Txt, Lang)};
|
|
|
|
process_iq_captcha(_From, #iq{type = set, lang = Lang, sub_els = [SubEl]},
|
|
|
|
_StateData) ->
|
2009-03-13 17:01:46 +01:00
|
|
|
case ejabberd_captcha:process_reply(SubEl) of
|
2016-07-25 12:50:30 +02:00
|
|
|
ok -> {result, undefined};
|
2016-04-05 12:09:44 +02:00
|
|
|
{error, malformed} ->
|
|
|
|
Txt = <<"Incorrect CAPTCHA submit">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, xmpp:err_bad_request(Txt, Lang)};
|
2016-04-05 12:09:44 +02:00
|
|
|
_ ->
|
|
|
|
Txt = <<"The CAPTCHA verification has failed">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, xmpp:err_not_allowed(Txt, Lang)}
|
2009-03-13 17:01:46 +01:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec process_iq_vcard(jid(), iq(), state()) ->
|
|
|
|
{result, vcard_temp() | xmlel()} |
|
|
|
|
{result, undefined, state()} |
|
|
|
|
{error, error()}.
|
|
|
|
process_iq_vcard(_From, #iq{type = get}, StateData) ->
|
2014-05-04 21:11:05 +02:00
|
|
|
#state{config = #config{vcard = VCardRaw}} = StateData,
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml_stream:parse_element(VCardRaw) of
|
2016-07-25 12:50:30 +02:00
|
|
|
#xmlel{} = VCard ->
|
|
|
|
{result, VCard};
|
2014-05-04 21:11:05 +02:00
|
|
|
{error, _} ->
|
2016-07-25 12:50:30 +02:00
|
|
|
{result, #vcard_temp{}}
|
2014-05-04 21:11:05 +02:00
|
|
|
end;
|
2016-07-25 12:50:30 +02:00
|
|
|
process_iq_vcard(From, #iq{type = set, lang = Lang, sub_els = [SubEl]},
|
|
|
|
StateData) ->
|
2014-05-04 21:11:05 +02:00
|
|
|
case get_affiliation(From, StateData) of
|
|
|
|
owner ->
|
2016-07-25 12:50:30 +02:00
|
|
|
VCardRaw = fxml:element_to_binary(xmpp:encode(SubEl)),
|
2014-05-04 21:11:05 +02:00
|
|
|
Config = StateData#state.config,
|
|
|
|
NewConfig = Config#config{vcard = VCardRaw},
|
|
|
|
change_config(NewConfig, StateData);
|
|
|
|
_ ->
|
|
|
|
ErrText = <<"Owner privileges required">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, xmpp:err_forbidden(ErrText, Lang)}
|
2014-05-04 21:11:05 +02:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec process_iq_mucsub(jid(), iq(), state()) ->
|
|
|
|
{error, error()} |
|
|
|
|
{result, undefined | muc_subscribe(), state()} |
|
|
|
|
{ignore, state()}.
|
|
|
|
process_iq_mucsub(_From, #iq{type = set, lang = Lang,
|
|
|
|
sub_els = [#muc_subscribe{}]},
|
|
|
|
#state{config = #config{allow_subscription = false}}) ->
|
|
|
|
{error, xmpp:err_not_allowed(<<"Subscriptions are not allowed">>, Lang)};
|
|
|
|
process_iq_mucsub(From,
|
2016-06-26 08:08:37 +02:00
|
|
|
#iq{type = set, lang = Lang,
|
2016-07-25 12:50:30 +02:00
|
|
|
sub_els = [#muc_subscribe{nick = Nick}]} = Packet,
|
|
|
|
StateData) ->
|
|
|
|
LJID = jid:tolower(From),
|
|
|
|
case (?DICT):find(LJID, StateData#state.users) of
|
|
|
|
{ok, #user{role = Role, nick = Nick1}} when Nick1 /= Nick ->
|
|
|
|
Nodes = get_subscription_nodes(Packet),
|
|
|
|
case {nick_collision(From, Nick, StateData),
|
|
|
|
mod_muc:can_use_nick(StateData#state.server_host,
|
|
|
|
StateData#state.host,
|
|
|
|
From, Nick)} of
|
|
|
|
{true, _} ->
|
|
|
|
ErrText = <<"That nickname is already in use by another occupant">>,
|
|
|
|
{error, xmpp:err_conflict(ErrText, Lang)};
|
|
|
|
{_, false} ->
|
|
|
|
ErrText = <<"That nickname is registered by another person">>,
|
|
|
|
{error, xmpp:err_conflict(ErrText, Lang)};
|
|
|
|
_ ->
|
2016-06-26 08:08:37 +02:00
|
|
|
NewStateData = add_online_user(
|
|
|
|
From, Nick, Role, true, Nodes, StateData),
|
2016-07-25 12:50:30 +02:00
|
|
|
{result, subscribe_result(Packet), NewStateData}
|
2016-06-26 08:08:37 +02:00
|
|
|
end;
|
2016-07-25 12:50:30 +02:00
|
|
|
{ok, #user{role = Role}} ->
|
|
|
|
Nodes = get_subscription_nodes(Packet),
|
|
|
|
NewStateData = add_online_user(
|
|
|
|
From, Nick, Role, true, Nodes, StateData),
|
|
|
|
{result, subscribe_result(Packet), NewStateData};
|
|
|
|
error ->
|
|
|
|
add_new_user(From, Nick, Packet, StateData)
|
2016-06-26 08:08:37 +02:00
|
|
|
end;
|
2016-07-25 12:50:30 +02:00
|
|
|
process_iq_mucsub(From, #iq{type = set, sub_els = [#muc_unsubscribe{}]},
|
2016-06-26 08:08:37 +02:00
|
|
|
StateData) ->
|
|
|
|
LJID = jid:tolower(From),
|
|
|
|
case ?DICT:find(LJID, StateData#state.users) of
|
|
|
|
{ok, #user{is_subscriber = true} = User} ->
|
|
|
|
NewStateData = remove_subscription(From, User, StateData),
|
|
|
|
store_room(NewStateData),
|
2016-07-25 12:50:30 +02:00
|
|
|
{result, undefined, NewStateData};
|
2016-06-26 08:08:37 +02:00
|
|
|
_ ->
|
2016-07-25 12:50:30 +02:00
|
|
|
{result, undefined, StateData}
|
2016-06-26 08:08:37 +02:00
|
|
|
end;
|
2016-07-25 12:50:30 +02:00
|
|
|
process_iq_mucsub(_From, #iq{type = get, lang = Lang}, _StateData) ->
|
2016-06-26 08:08:37 +02:00
|
|
|
Txt = <<"Value 'get' of 'type' attribute is not allowed">>,
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, xmpp:err_bad_request(Txt, Lang)}.
|
2016-06-26 08:08:37 +02:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec remove_subscription(jid(), #user{}, state()) -> state().
|
2016-06-26 08:08:37 +02:00
|
|
|
remove_subscription(JID, #user{is_subscriber = true} = User, StateData) ->
|
|
|
|
case User#user.last_presence of
|
|
|
|
undefined ->
|
|
|
|
remove_online_user(JID, StateData, false);
|
|
|
|
_ ->
|
|
|
|
LJID = jid:tolower(JID),
|
|
|
|
Users = ?DICT:store(LJID, User#user{is_subscriber = false},
|
|
|
|
StateData#state.users),
|
|
|
|
StateData#state{users = Users}
|
|
|
|
end;
|
|
|
|
remove_subscription(_JID, #user{}, StateData) ->
|
|
|
|
StateData.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec remove_subscriptions(state()) -> state().
|
2016-06-26 08:08:37 +02:00
|
|
|
remove_subscriptions(StateData) ->
|
|
|
|
if not (StateData#state.config)#config.allow_subscription ->
|
|
|
|
dict:fold(
|
|
|
|
fun(_LJID, User, State) ->
|
|
|
|
remove_subscription(User#user.jid, User, State)
|
|
|
|
end, StateData, StateData#state.users);
|
|
|
|
true ->
|
|
|
|
StateData
|
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec get_subscription_nodes(iq()) -> [binary()].
|
|
|
|
get_subscription_nodes(#iq{sub_els = [#muc_subscribe{events = Nodes}]}) ->
|
|
|
|
lists:filter(
|
|
|
|
fun(Node) ->
|
|
|
|
lists:member(Node, [?NS_MUCSUB_NODES_PRESENCE,
|
|
|
|
?NS_MUCSUB_NODES_MESSAGES,
|
|
|
|
?NS_MUCSUB_NODES_AFFILIATIONS,
|
|
|
|
?NS_MUCSUB_NODES_SUBJECT,
|
|
|
|
?NS_MUCSUB_NODES_CONFIG,
|
|
|
|
?NS_MUCSUB_NODES_PARTICIPANTS])
|
|
|
|
end, Nodes);
|
2016-06-26 08:08:37 +02:00
|
|
|
get_subscription_nodes(_) ->
|
|
|
|
[].
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec subscribe_result(iq()) -> muc_subscribe().
|
|
|
|
subscribe_result(#iq{sub_els = [#muc_subscribe{nick = Nick}]} = Packet) ->
|
|
|
|
#muc_subscribe{nick = Nick, events = get_subscription_nodes(Packet)}.
|
2016-06-26 08:08:37 +02:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec get_title(state()) -> binary().
|
2003-03-27 21:55:09 +01:00
|
|
|
get_title(StateData) ->
|
|
|
|
case (StateData#state.config)#config.title of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"">> -> StateData#state.room;
|
|
|
|
Name -> Name
|
2003-03-27 21:55:09 +01:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec get_roomdesc_reply(jid(), state(), binary()) -> {item, binary()} | false.
|
2010-06-23 09:44:35 +02:00
|
|
|
get_roomdesc_reply(JID, StateData, Tail) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
IsOccupantOrAdmin = is_occupant_or_admin(JID,
|
|
|
|
StateData),
|
|
|
|
if (StateData#state.config)#config.public or
|
|
|
|
IsOccupantOrAdmin ->
|
|
|
|
if (StateData#state.config)#config.public_list or
|
|
|
|
IsOccupantOrAdmin ->
|
|
|
|
{item, <<(get_title(StateData))/binary,Tail/binary>>};
|
|
|
|
true -> {item, get_title(StateData)}
|
|
|
|
end;
|
|
|
|
true -> false
|
2010-03-29 20:51:24 +02:00
|
|
|
end.
|
2010-03-29 20:49:52 +02:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec get_roomdesc_tail(state(), binary()) -> binary().
|
2010-03-29 20:49:52 +02:00
|
|
|
get_roomdesc_tail(StateData, Lang) ->
|
2010-03-29 20:51:24 +02:00
|
|
|
Desc = case (StateData#state.config)#config.public of
|
2013-03-14 10:33:02 +01:00
|
|
|
true -> <<"">>;
|
|
|
|
_ -> translate:translate(Lang, <<"private, ">>)
|
2010-03-29 20:51:24 +02:00
|
|
|
end,
|
2016-07-25 12:50:30 +02:00
|
|
|
Len = (?DICT):size(StateData#state.users),
|
2013-03-14 10:33:02 +01:00
|
|
|
<<" (", Desc/binary,
|
|
|
|
(iolist_to_binary(integer_to_list(Len)))/binary, ")">>.
|
2010-03-29 20:49:52 +02:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec get_mucroom_disco_items(state()) -> disco_items().
|
2010-03-29 20:49:52 +02:00
|
|
|
get_mucroom_disco_items(StateData) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
Items = lists:map(
|
|
|
|
fun({_LJID, Info}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Nick = Info#user.nick,
|
2016-07-25 12:50:30 +02:00
|
|
|
#disco_item{jid = jid:make(StateData#state.room,
|
|
|
|
StateData#state.host,
|
|
|
|
Nick),
|
|
|
|
name = Nick}
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
2016-07-25 12:50:30 +02:00
|
|
|
(?DICT):to_list(StateData#state.users)),
|
|
|
|
#disco_items{items = Items}.
|
2003-03-27 15:06:17 +01:00
|
|
|
|
2011-07-23 16:13:27 +02:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% Voice request support
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec is_voice_request(message()) -> boolean().
|
|
|
|
is_voice_request(Packet) ->
|
|
|
|
Els = xmpp:get_els(Packet),
|
|
|
|
lists:any(
|
|
|
|
fun(#xdata{} = X) ->
|
|
|
|
case {xmpp_util:get_xdata_values(<<"FORM_TYPE">>, X),
|
|
|
|
xmpp_util:get_xdata_values(<<"muc#role">>, X)} of
|
|
|
|
{[<<"http://jabber.org/protocol/muc#request">>],
|
|
|
|
[<<"participant">>]} ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
(_) ->
|
|
|
|
false
|
|
|
|
end, Els).
|
2011-07-23 16:13:27 +02:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec prepare_request_form(jid(), binary(), binary()) -> message().
|
2011-07-24 11:46:47 +02:00
|
|
|
prepare_request_form(Requester, Nick, Lang) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
Title = translate:translate(Lang, <<"Voice request">>),
|
|
|
|
Instruction = translate:translate(
|
|
|
|
Lang, <<"Either approve or decline the voice request.">>),
|
|
|
|
Fs = [#xdata_field{var = <<"FORM_TYPE">>,
|
|
|
|
type = hidden,
|
|
|
|
values = [<<"http://jabber.org/protocol/muc#request">>]},
|
|
|
|
#xdata_field{var = <<"muc#role">>,
|
|
|
|
type = hidden,
|
|
|
|
values = [<<"participant">>]},
|
|
|
|
?STRINGXFIELD(<<"User JID">>, <<"muc#jid">>,
|
|
|
|
jid:to_string(Requester)),
|
|
|
|
?STRINGXFIELD(<<"Nickname">>, <<"muc#roomnick">>, Nick),
|
|
|
|
?BOOLXFIELD(<<"Grant voice to this person?">>,
|
|
|
|
<<"muc#request_allow">>, false)],
|
|
|
|
#message{type = normal,
|
|
|
|
sub_els = [#xdata{type = form,
|
|
|
|
title = Title,
|
|
|
|
instructions = [Instruction],
|
|
|
|
fields = Fs}]}.
|
|
|
|
|
|
|
|
-spec send_voice_request(jid(), binary(), state()) -> ok.
|
|
|
|
send_voice_request(From, Lang, StateData) ->
|
2011-09-26 08:55:07 +02:00
|
|
|
Moderators = search_role(moderator, StateData),
|
|
|
|
FromNick = find_nick_by_jid(From, StateData),
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:foreach(fun ({_, User}) ->
|
2016-06-26 08:08:37 +02:00
|
|
|
ejabberd_router:route(
|
|
|
|
StateData#state.jid, User#user.jid,
|
2016-07-25 12:50:30 +02:00
|
|
|
prepare_request_form(From, FromNick, Lang))
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
|
|
|
Moderators).
|
2011-09-26 08:55:07 +02:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec is_voice_approvement(message()) -> boolean().
|
|
|
|
is_voice_approvement(Packet) ->
|
|
|
|
Els = xmpp:get_els(Packet),
|
|
|
|
lists:any(
|
|
|
|
fun(#xdata{} = X) ->
|
|
|
|
case {xmpp_util:get_xdata_values(<<"FORM_TYPE">>, X),
|
|
|
|
xmpp_util:get_xdata_values(<<"muc#role">>, X),
|
|
|
|
xmpp_util:get_xdata_values(<<"muc#request_allow">>, X)} of
|
|
|
|
{[<<"http://jabber.org/protocol/muc#request">>],
|
|
|
|
[<<"participant">>], [Flag]} when Flag == <<"true">>;
|
|
|
|
Flag == <<"1">> ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
(_) ->
|
|
|
|
false
|
|
|
|
end, Els).
|
|
|
|
|
|
|
|
-spec extract_jid_from_voice_approvement(message()) -> jid() | error.
|
|
|
|
extract_jid_from_voice_approvement(Packet) ->
|
|
|
|
Els = xmpp:get_els(Packet),
|
|
|
|
lists:foldl(
|
|
|
|
fun(#xdata{} = X, error) ->
|
|
|
|
case {xmpp_util:get_xdata_values(<<"FORM_TYPE">>, X),
|
|
|
|
xmpp_util:get_xdata_values(<<"muc#role">>, X),
|
|
|
|
xmpp_util:get_xdata_values(<<"muc#request_allow">>, X),
|
|
|
|
xmpp_util:get_xdata_values(<<"muc#jid">>, X)} of
|
|
|
|
{[<<"http://jabber.org/protocol/muc#request">>],
|
|
|
|
[<<"participant">>], [Flag], [J]} when Flag == <<"true">>;
|
|
|
|
Flag == <<"1">> ->
|
|
|
|
jid:from_string(J);
|
|
|
|
_ ->
|
|
|
|
error
|
|
|
|
end;
|
|
|
|
(_, Acc) ->
|
|
|
|
Acc
|
|
|
|
end, error, Els).
|
2011-07-25 21:46:59 +02:00
|
|
|
|
2003-04-13 21:22:46 +02:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% Invitation support
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec is_invitation(message()) -> boolean().
|
|
|
|
is_invitation(Packet) ->
|
|
|
|
Els = xmpp:get_els(Packet),
|
|
|
|
lists:any(
|
|
|
|
fun(#muc_user{invites = [_|_]}) -> true;
|
|
|
|
(_) -> false
|
|
|
|
end, Els).
|
|
|
|
|
|
|
|
-spec check_invitation(jid(), message(), state()) -> {error, error()} | jid().
|
|
|
|
check_invitation(From, Packet, StateData) ->
|
|
|
|
Lang = xmpp:get_lang(Packet),
|
2003-04-13 21:22:46 +02:00
|
|
|
FAffiliation = get_affiliation(From, StateData),
|
2016-07-25 12:50:30 +02:00
|
|
|
CanInvite = (StateData#state.config)#config.allow_user_invites
|
|
|
|
orelse
|
|
|
|
FAffiliation == admin orelse FAffiliation == owner,
|
2007-05-03 06:07:29 +02:00
|
|
|
case CanInvite of
|
2016-07-25 12:50:30 +02:00
|
|
|
false ->
|
|
|
|
Txt = <<"Invitations are not allowed in this conference">>,
|
|
|
|
{error, xmpp:err_not_allowed(Txt, Lang)};
|
|
|
|
true ->
|
|
|
|
case xmpp:get_subtag(Packet, #muc_user{}) of
|
|
|
|
#muc_user{invites = [#muc_invite{to = undefined}]} ->
|
|
|
|
Txt = <<"No 'to' attribute found">>,
|
|
|
|
{error, xmpp:err_bad_request(Txt, Lang)};
|
|
|
|
#muc_user{invites = [#muc_invite{to = JID, reason = Reason} = I]} ->
|
|
|
|
Invite = I#muc_invite{to = undefined, from = From},
|
|
|
|
Password = case (StateData#state.config)#config.password_protected of
|
|
|
|
true ->
|
|
|
|
(StateData#state.config)#config.password;
|
|
|
|
false ->
|
|
|
|
undefined
|
|
|
|
end,
|
|
|
|
XUser = #muc_user{password = Password, invites = [Invite]},
|
|
|
|
XConference = #x_conference{jid = jid:make(StateData#state.room,
|
|
|
|
StateData#state.host),
|
|
|
|
reason = Reason},
|
|
|
|
Body = iolist_to_binary(
|
|
|
|
[io_lib:format(
|
|
|
|
translate:translate(
|
|
|
|
Lang,
|
|
|
|
<<"~s invites you to the room ~s">>),
|
|
|
|
[jid:to_string(From),
|
|
|
|
jid:to_string({StateData#state.room,
|
|
|
|
StateData#state.host,
|
|
|
|
<<"">>})]),
|
|
|
|
case (StateData#state.config)#config.password_protected of
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
|
|
|
<<", ",
|
2016-07-25 12:50:30 +02:00
|
|
|
(translate:translate(
|
|
|
|
Lang, <<"the password is">>))/binary,
|
2013-03-14 10:33:02 +01:00
|
|
|
" '",
|
|
|
|
((StateData#state.config)#config.password)/binary,
|
|
|
|
"'">>;
|
|
|
|
_ -> <<"">>
|
2016-07-25 12:50:30 +02:00
|
|
|
end,
|
|
|
|
case Reason of
|
|
|
|
<<"">> -> <<"">>;
|
|
|
|
_ -> <<" (", Reason/binary, ") ">>
|
|
|
|
end]),
|
|
|
|
Msg = #message{type = normal,
|
|
|
|
body = xmpp:mk_text(Body),
|
|
|
|
sub_els = [XUser, XConference]},
|
|
|
|
ejabberd_router:route(StateData#state.jid, JID, Msg),
|
|
|
|
JID;
|
|
|
|
#muc_user{invites = [_|_]} ->
|
|
|
|
Txt = <<"Multiple <invite/> elements are not allowed">>,
|
|
|
|
{error, xmpp:err_forbidden(Txt, Lang)};
|
|
|
|
_ ->
|
|
|
|
Txt = <<"No <invite/> element found">>,
|
|
|
|
{error, xmpp:err_bad_request(Txt, Lang)}
|
|
|
|
end
|
2003-04-13 21:22:46 +02:00
|
|
|
end.
|
|
|
|
|
2008-02-06 21:30:58 +01:00
|
|
|
%% Handle a message sent to the room by a non-participant.
|
|
|
|
%% If it is a decline, send to the inviter.
|
|
|
|
%% Otherwise, an error message is sent to the sender.
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec handle_roommessage_from_nonparticipant(message(), state(), jid()) -> ok.
|
|
|
|
handle_roommessage_from_nonparticipant(Packet, StateData, From) ->
|
|
|
|
case xmpp:get_subtag(Packet, #muc_user{}) of
|
|
|
|
#muc_user{decline = #muc_decline{to = #jid{} = To} = Decline} = XUser ->
|
|
|
|
NewDecline = Decline#muc_decline{to = undefined, from = From},
|
|
|
|
NewXUser = XUser#muc_user{decline = NewDecline},
|
|
|
|
NewPacket = xmpp:set_subtag(Packet, NewXUser),
|
|
|
|
ejabberd_router:route(StateData#state.jid, To, NewPacket);
|
|
|
|
_ ->
|
|
|
|
ErrText = <<"Only occupants are allowed to send messages "
|
|
|
|
"to the conference">>,
|
|
|
|
Err = xmpp:err_not_acceptable(ErrText, xmpp:get_lang(Packet)),
|
|
|
|
ejabberd_router:route_error(StateData#state.jid, From, Packet, Err)
|
2008-02-06 21:30:58 +01:00
|
|
|
end.
|
|
|
|
|
2006-03-14 05:26:15 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% Logging
|
|
|
|
|
2009-07-21 20:33:56 +02:00
|
|
|
add_to_log(Type, Data, StateData)
|
2013-03-14 10:33:02 +01:00
|
|
|
when Type == roomconfig_change_disabledlogging ->
|
|
|
|
mod_muc_log:add_to_log(StateData#state.server_host,
|
|
|
|
roomconfig_change, Data, StateData#state.jid,
|
|
|
|
make_opts(StateData));
|
2006-03-14 05:26:15 +01:00
|
|
|
add_to_log(Type, Data, StateData) ->
|
|
|
|
case (StateData#state.config)#config.logging of
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
|
|
|
mod_muc_log:add_to_log(StateData#state.server_host,
|
|
|
|
Type, Data, StateData#state.jid,
|
|
|
|
make_opts(StateData));
|
|
|
|
false -> ok
|
2006-03-14 05:26:15 +01:00
|
|
|
end.
|
2007-12-03 11:47:42 +01:00
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
%% Users number checking
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec tab_add_online_user(jid(), state()) -> ok.
|
2007-12-03 11:47:42 +01:00
|
|
|
tab_add_online_user(JID, StateData) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
{LUser, LServer, LResource} = jid:tolower(JID),
|
2007-12-03 11:47:42 +01:00
|
|
|
US = {LUser, LServer},
|
|
|
|
Room = StateData#state.room,
|
|
|
|
Host = StateData#state.host,
|
2013-03-14 10:33:02 +01:00
|
|
|
catch ets:insert(muc_online_users,
|
|
|
|
#muc_online_users{us = US, resource = LResource,
|
2016-07-25 12:50:30 +02:00
|
|
|
room = Room, host = Host}),
|
|
|
|
ok.
|
2007-12-03 11:47:42 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec tab_remove_online_user(jid(), state()) -> ok.
|
2007-12-03 11:47:42 +01:00
|
|
|
tab_remove_online_user(JID, StateData) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
{LUser, LServer, LResource} = jid:tolower(JID),
|
2007-12-03 11:47:42 +01:00
|
|
|
US = {LUser, LServer},
|
|
|
|
Room = StateData#state.room,
|
|
|
|
Host = StateData#state.host,
|
2013-03-14 10:33:02 +01:00
|
|
|
catch ets:delete_object(muc_online_users,
|
|
|
|
#muc_online_users{us = US, resource = LResource,
|
2016-07-25 12:50:30 +02:00
|
|
|
room = Room, host = Host}),
|
|
|
|
ok.
|
2007-12-03 11:47:42 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec tab_count_user(jid()) -> non_neg_integer().
|
2007-12-03 11:47:42 +01:00
|
|
|
tab_count_user(JID) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
{LUser, LServer, _} = jid:tolower(JID),
|
2007-12-03 11:47:42 +01:00
|
|
|
US = {LUser, LServer},
|
2013-03-14 10:33:02 +01:00
|
|
|
case catch ets:select(muc_online_users,
|
|
|
|
[{#muc_online_users{us = US, _ = '_'}, [], [[]]}])
|
|
|
|
of
|
|
|
|
Res when is_list(Res) -> length(Res);
|
|
|
|
_ -> 0
|
2007-12-03 11:47:42 +01:00
|
|
|
end.
|
2010-07-01 12:54:01 +02:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec element_size(stanza()) -> non_neg_integer().
|
2010-07-01 12:54:01 +02:00
|
|
|
element_size(El) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
byte_size(fxml:element_to_binary(xmpp:encode(El))).
|
2013-06-27 11:50:54 +02:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec store_room(state()) -> ok.
|
2016-06-26 08:08:37 +02:00
|
|
|
store_room(StateData) ->
|
|
|
|
if (StateData#state.config)#config.persistent ->
|
|
|
|
mod_muc:store_room(StateData#state.server_host,
|
|
|
|
StateData#state.host, StateData#state.room,
|
|
|
|
make_opts(StateData));
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec send_wrapped(jid(), jid(), stanza(), binary(), state()) -> ok.
|
2016-06-26 08:08:37 +02:00
|
|
|
send_wrapped(From, To, Packet, Node, State) ->
|
|
|
|
LTo = jid:tolower(To),
|
|
|
|
case ?DICT:find(LTo, State#state.users) of
|
|
|
|
{ok, #user{is_subscriber = true,
|
|
|
|
subscriptions = Nodes,
|
|
|
|
last_presence = undefined}} ->
|
|
|
|
case lists:member(Node, Nodes) of
|
|
|
|
true ->
|
|
|
|
NewPacket = wrap(From, To, Packet, Node),
|
|
|
|
ejabberd_router:route(State#state.jid, To, NewPacket);
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
ejabberd_router:route(From, To, Packet)
|
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec wrap(jid(), jid(), stanza(), binary()) -> message().
|
2016-06-26 08:08:37 +02:00
|
|
|
wrap(From, To, Packet, Node) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
El = xmpp:encode(xmpp:set_from_to(Packet, From, To)),
|
|
|
|
#message{
|
|
|
|
sub_els = [#pubsub_event{
|
|
|
|
items = [#pubsub_event_items{
|
|
|
|
node = Node,
|
|
|
|
items = [#pubsub_event_item{
|
|
|
|
id = randoms:get_string(),
|
|
|
|
xml_els = [El]}]}]}]}.
|
2016-06-26 08:08:37 +02:00
|
|
|
|
2015-10-07 00:06:58 +02:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
%% Multicast
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec send_multiple(jid(), binary(), [#user{}], stanza()) -> ok.
|
2015-10-07 00:06:58 +02:00
|
|
|
send_multiple(From, Server, Users, Packet) ->
|
|
|
|
JIDs = [ User#user.jid || {_, User} <- ?DICT:to_list(Users)],
|
|
|
|
ejabberd_router_multicast:route_multicast(From, Server, JIDs, Packet).
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec send_wrapped_multiple(jid(), [#user{}], stanza(), binary(), state()) -> ok.
|
2016-06-26 08:08:37 +02:00
|
|
|
send_wrapped_multiple(From, Users, Packet, Node, State) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun({_, #user{jid = To}}) ->
|
|
|
|
send_wrapped(From, To, Packet, Node, State)
|
|
|
|
end, ?DICT:to_list(Users)).
|
|
|
|
|
2013-06-27 11:50:54 +02:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
%% Detect messange stanzas that don't have meaninful content
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec has_body_or_subject(message()) -> boolean().
|
|
|
|
has_body_or_subject(#message{body = Body, subject = Subj}) ->
|
|
|
|
Body /= [] orelse Subj /= [].
|