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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2023-01-09 17:09:06 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2023 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
|
|
|
|
2022-11-07 12:32:38 +01:00
|
|
|
-protocol({xep, 317, '0.1', '21.12', "", "conversejs/prosody compatible"}).
|
|
|
|
-protocol({xep, 410, '1.1.0', '18.12', "", ""}).
|
2021-11-05 11:11:29 +01:00
|
|
|
|
2017-08-05 19:58:21 +02:00
|
|
|
-behaviour(p1_fsm).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
|
|
|
%% External exports
|
2017-03-10 13:12:43 +01:00
|
|
|
-export([start_link/10,
|
|
|
|
start_link/8,
|
|
|
|
start/10,
|
|
|
|
start/8,
|
2019-07-15 11:43:05 +02:00
|
|
|
supervisor/1,
|
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,
|
2018-02-16 06:28:33 +01:00
|
|
|
route/2,
|
|
|
|
expand_opts/1,
|
2019-07-08 23:47:54 +02:00
|
|
|
config_fields/0,
|
|
|
|
destroy/1,
|
|
|
|
destroy/2,
|
|
|
|
shutdown/1,
|
|
|
|
get_config/1,
|
|
|
|
set_config/2,
|
|
|
|
get_state/1,
|
2022-03-07 06:42:42 +01:00
|
|
|
get_info/1,
|
2019-07-08 23:47:54 +02:00
|
|
|
change_item/5,
|
2021-12-20 07:31:01 +01:00
|
|
|
change_item_async/5,
|
2019-07-08 23:47:54 +02:00
|
|
|
config_reloaded/1,
|
|
|
|
subscribe/4,
|
|
|
|
unsubscribe/2,
|
|
|
|
is_subscribed/2,
|
|
|
|
get_subscribers/1,
|
|
|
|
service_message/2,
|
|
|
|
get_disco_item/4]).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
|
|
|
%% gen_fsm callbacks
|
|
|
|
-export([init/1,
|
|
|
|
normal_state/2,
|
|
|
|
handle_event/3,
|
|
|
|
handle_sync_event/4,
|
|
|
|
handle_info/3,
|
|
|
|
terminate/3,
|
|
|
|
code_change/4]).
|
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2020-09-03 13:45:57 +02:00
|
|
|
-include_lib("xmpp/include/xmpp.hrl").
|
2018-11-29 10:16:12 +01:00
|
|
|
-include("translate.hrl").
|
2008-12-02 20:23:12 +01:00
|
|
|
-include("mod_muc_room.hrl").
|
2018-12-13 11:45:45 +01:00
|
|
|
-include("ejabberd_stacktrace.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
|
|
|
|
2021-10-29 04:12:26 +02:00
|
|
|
-define(MUC_HAT_ADD_CMD, <<"http://prosody.im/protocol/hats#add">>).
|
|
|
|
-define(MUC_HAT_REMOVE_CMD, <<"http://prosody.im/protocol/hats#remove">>).
|
|
|
|
-define(MUC_HAT_LIST_CMD, <<"p1:hats#list">>).
|
|
|
|
-define(MAX_HATS_USERS, 100).
|
|
|
|
-define(MAX_HATS_PER_USER, 10).
|
2022-04-21 13:37:01 +02:00
|
|
|
-define(CLEAN_ROOM_TIMEOUT, 30000).
|
2021-10-29 04:12:26 +02:00
|
|
|
|
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().
|
2019-07-08 23:47:54 +02:00
|
|
|
-type disco_item_filter() :: only_non_empty | all | non_neg_integer().
|
2019-07-09 14:21:17 +02:00
|
|
|
-type admin_action() :: {jid(), affiliation | role, affiliation() | role(), binary()}.
|
2019-07-08 23:47:54 +02:00
|
|
|
-export_type([state/0, disco_item_filter/0]).
|
2016-07-25 12:50:30 +02:00
|
|
|
|
2016-11-24 12:44:09 +01:00
|
|
|
-callback set_affiliation(binary(), binary(), binary(), jid(), affiliation(),
|
|
|
|
binary()) -> ok | {error, any()}.
|
|
|
|
-callback set_affiliations(binary(), binary(), binary(),
|
2019-06-27 14:22:27 +02:00
|
|
|
affiliations()) -> ok | {error, any()}.
|
2016-11-24 12:44:09 +01:00
|
|
|
-callback get_affiliation(binary(), binary(), binary(),
|
|
|
|
binary(), binary()) -> {ok, affiliation()} | {error, any()}.
|
2019-06-27 14:22:27 +02:00
|
|
|
-callback get_affiliations(binary(), binary(), binary()) -> {ok, affiliations()} | {error, any()}.
|
2016-11-24 12:44:09 +01:00
|
|
|
-callback search_affiliation(binary(), binary(), binary(), affiliation()) ->
|
|
|
|
{ok, [{ljid(), {affiliation(), binary()}}]} | {error, any()}.
|
|
|
|
|
2003-03-23 21:08:44 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2019-07-05 09:35:31 +02:00
|
|
|
-spec start(binary(), binary(), mod_muc:access(), binary(), non_neg_integer(),
|
|
|
|
atom(), jid(), binary(), [{atom(), term()}], ram | file) ->
|
|
|
|
{ok, pid()} | {error, any()}.
|
2007-09-01 23:05:04 +02:00
|
|
|
start(Host, ServerHost, Access, Room, HistorySize, RoomShaper,
|
2017-03-10 13:12:43 +01:00
|
|
|
Creator, Nick, DefRoomOpts, QueueType) ->
|
2019-07-15 11:43:05 +02:00
|
|
|
supervisor:start_child(
|
|
|
|
supervisor(ServerHost),
|
|
|
|
[Host, ServerHost, Access, Room, HistorySize,
|
|
|
|
RoomShaper, Creator, Nick, DefRoomOpts, QueueType]).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2019-07-05 09:35:31 +02:00
|
|
|
-spec start(binary(), binary(), mod_muc:access(), binary(), non_neg_integer(),
|
|
|
|
atom(), [{atom(), term()}], ram | file) ->
|
|
|
|
{ok, pid()} | {error, any()}.
|
2017-03-10 13:12:43 +01:00
|
|
|
start(Host, ServerHost, Access, Room, HistorySize, RoomShaper, Opts, QueueType) ->
|
2019-07-15 11:43:05 +02:00
|
|
|
supervisor:start_child(
|
|
|
|
supervisor(ServerHost),
|
|
|
|
[Host, ServerHost, Access, Room, HistorySize,
|
|
|
|
RoomShaper, Opts, QueueType]).
|
2006-02-02 06:00:27 +01:00
|
|
|
|
2019-07-05 09:35:31 +02:00
|
|
|
-spec start_link(binary(), binary(), mod_muc:access(), binary(), non_neg_integer(),
|
|
|
|
atom(), jid(), binary(), [{atom(), term()}], ram | file) ->
|
|
|
|
{ok, pid()} | {error, any()}.
|
2007-09-01 23:05:04 +02:00
|
|
|
start_link(Host, ServerHost, Access, Room, HistorySize, RoomShaper,
|
2017-03-10 13:12:43 +01:00
|
|
|
Creator, Nick, DefRoomOpts, QueueType) ->
|
2017-08-05 19:58:21 +02:00
|
|
|
p1_fsm:start_link(?MODULE, [Host, ServerHost, Access, Room, HistorySize,
|
2017-03-10 13:12:43 +01:00
|
|
|
RoomShaper, Creator, Nick, DefRoomOpts, QueueType],
|
2006-02-03 04:28:15 +01:00
|
|
|
?FSMOPTS).
|
2006-02-02 06:00:27 +01:00
|
|
|
|
2019-07-05 09:35:31 +02:00
|
|
|
-spec start_link(binary(), binary(), mod_muc:access(), binary(), non_neg_integer(),
|
|
|
|
atom(), [{atom(), term()}], ram | file) ->
|
|
|
|
{ok, pid()} | {error, any()}.
|
2017-03-10 13:12:43 +01:00
|
|
|
start_link(Host, ServerHost, Access, Room, HistorySize, RoomShaper, Opts, QueueType) ->
|
2017-08-05 19:58:21 +02:00
|
|
|
p1_fsm:start_link(?MODULE, [Host, ServerHost, Access, Room, HistorySize,
|
2017-03-10 13:12:43 +01:00
|
|
|
RoomShaper, Opts, QueueType],
|
2006-02-03 04:28:15 +01:00
|
|
|
?FSMOPTS).
|
2003-03-25 22:03:35 +01:00
|
|
|
|
2019-07-15 11:43:05 +02:00
|
|
|
-spec supervisor(binary()) -> atom().
|
|
|
|
supervisor(Host) ->
|
|
|
|
gen_mod:get_module_proc(Host, mod_muc_room_sup).
|
|
|
|
|
2019-07-08 23:47:54 +02:00
|
|
|
-spec destroy(pid()) -> ok.
|
|
|
|
destroy(Pid) ->
|
|
|
|
p1_fsm:send_all_state_event(Pid, destroy).
|
|
|
|
|
|
|
|
-spec destroy(pid(), binary()) -> ok.
|
|
|
|
destroy(Pid, Reason) ->
|
|
|
|
p1_fsm:send_all_state_event(Pid, {destroy, Reason}).
|
|
|
|
|
|
|
|
-spec shutdown(pid()) -> boolean().
|
|
|
|
shutdown(Pid) ->
|
|
|
|
ejabberd_cluster:send(Pid, shutdown).
|
|
|
|
|
|
|
|
-spec config_reloaded(pid()) -> boolean().
|
|
|
|
config_reloaded(Pid) ->
|
|
|
|
ejabberd_cluster:send(Pid, config_reloaded).
|
|
|
|
|
|
|
|
-spec get_config(pid()) -> {ok, config()} | {error, notfound | timeout}.
|
|
|
|
get_config(Pid) ->
|
|
|
|
try p1_fsm:sync_send_all_state_event(Pid, get_config)
|
|
|
|
catch _:{timeout, {p1_fsm, _, _}} ->
|
|
|
|
{error, timeout};
|
|
|
|
_:{_, {p1_fsm, _, _}} ->
|
|
|
|
{error, notfound}
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec set_config(pid(), config()) -> {ok, config()} | {error, notfound | timeout}.
|
|
|
|
set_config(Pid, Config) ->
|
|
|
|
try p1_fsm:sync_send_all_state_event(Pid, {change_config, Config})
|
|
|
|
catch _:{timeout, {p1_fsm, _, _}} ->
|
|
|
|
{error, timeout};
|
|
|
|
_:{_, {p1_fsm, _, _}} ->
|
|
|
|
{error, notfound}
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec change_item(pid(), jid(), affiliation | role, affiliation() | role(), binary()) ->
|
|
|
|
{ok, state()} | {error, notfound | timeout}.
|
|
|
|
change_item(Pid, JID, Type, AffiliationOrRole, Reason) ->
|
|
|
|
try p1_fsm:sync_send_all_state_event(
|
|
|
|
Pid, {process_item_change, {JID, Type, AffiliationOrRole, Reason}, undefined})
|
|
|
|
catch _:{timeout, {p1_fsm, _, _}} ->
|
|
|
|
{error, timeout};
|
|
|
|
_:{_, {p1_fsm, _, _}} ->
|
|
|
|
{error, notfound}
|
|
|
|
end.
|
|
|
|
|
2021-12-20 07:31:01 +01:00
|
|
|
-spec change_item_async(pid(), jid(), affiliation | role, affiliation() | role(), binary()) -> ok.
|
|
|
|
change_item_async(Pid, JID, Type, AffiliationOrRole, Reason) ->
|
|
|
|
p1_fsm:send_all_state_event(
|
|
|
|
Pid, {process_item_change, {JID, Type, AffiliationOrRole, Reason}, undefined}).
|
|
|
|
|
2019-07-08 23:47:54 +02:00
|
|
|
-spec get_state(pid()) -> {ok, state()} | {error, notfound | timeout}.
|
|
|
|
get_state(Pid) ->
|
|
|
|
try p1_fsm:sync_send_all_state_event(Pid, get_state)
|
|
|
|
catch _:{timeout, {p1_fsm, _, _}} ->
|
|
|
|
{error, timeout};
|
|
|
|
_:{_, {p1_fsm, _, _}} ->
|
|
|
|
{error, notfound}
|
|
|
|
end.
|
|
|
|
|
2022-03-07 06:42:42 +01:00
|
|
|
-spec get_info(pid()) -> {ok, #{occupants_number => integer()}} |
|
|
|
|
{error, notfound | timeout}.
|
|
|
|
get_info(Pid) ->
|
|
|
|
try
|
|
|
|
{ok, p1_fsm:sync_send_all_state_event(Pid, get_info)}
|
|
|
|
catch _:{timeout, {p1_fsm, _, _}} ->
|
|
|
|
{error, timeout};
|
|
|
|
_:{_, {p1_fsm, _, _}} ->
|
|
|
|
{error, notfound}
|
|
|
|
end.
|
|
|
|
|
2019-07-08 23:47:54 +02:00
|
|
|
-spec subscribe(pid(), jid(), binary(), [binary()]) -> {ok, [binary()]} | {error, binary()}.
|
|
|
|
subscribe(Pid, JID, Nick, Nodes) ->
|
|
|
|
try p1_fsm:sync_send_all_state_event(Pid, {muc_subscribe, JID, Nick, Nodes})
|
|
|
|
catch _:{timeout, {p1_fsm, _, _}} ->
|
|
|
|
{error, ?T("Request has timed out")};
|
|
|
|
_:{_, {p1_fsm, _, _}} ->
|
|
|
|
{error, ?T("Conference room does not exist")}
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec unsubscribe(pid(), jid()) -> ok | {error, binary()}.
|
|
|
|
unsubscribe(Pid, JID) ->
|
|
|
|
try p1_fsm:sync_send_all_state_event(Pid, {muc_unsubscribe, JID})
|
|
|
|
catch _:{timeout, {p1_fsm, _, _}} ->
|
|
|
|
{error, ?T("Request has timed out")};
|
2020-03-24 11:44:22 +01:00
|
|
|
exit:{normal, {p1_fsm, _, _}} ->
|
|
|
|
ok;
|
2019-07-08 23:47:54 +02:00
|
|
|
_:{_, {p1_fsm, _, _}} ->
|
|
|
|
{error, ?T("Conference room does not exist")}
|
|
|
|
end.
|
|
|
|
|
2020-04-03 12:34:57 +02:00
|
|
|
-spec is_subscribed(pid(), jid()) -> {true, binary(), [binary()]} | false.
|
2019-07-08 23:47:54 +02:00
|
|
|
is_subscribed(Pid, JID) ->
|
|
|
|
try p1_fsm:sync_send_all_state_event(Pid, {is_subscribed, JID})
|
|
|
|
catch _:{_, {p1_fsm, _, _}} -> false
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec get_subscribers(pid()) -> {ok, [jid()]} | {error, notfound | timeout}.
|
|
|
|
get_subscribers(Pid) ->
|
|
|
|
try p1_fsm:sync_send_all_state_event(Pid, get_subscribers)
|
|
|
|
catch _:{timeout, {p1_fsm, _, _}} ->
|
|
|
|
{error, timeout};
|
|
|
|
_:{_, {p1_fsm, _, _}} ->
|
|
|
|
{error, notfound}
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec service_message(pid(), binary()) -> ok.
|
|
|
|
service_message(Pid, Text) ->
|
|
|
|
p1_fsm:send_all_state_event(Pid, {service_message, Text}).
|
|
|
|
|
|
|
|
-spec get_disco_item(pid(), disco_item_filter(), jid(), binary()) ->
|
|
|
|
{ok, binary()} | {error, notfound | timeout}.
|
|
|
|
get_disco_item(Pid, Filter, JID, Lang) ->
|
|
|
|
Timeout = 100,
|
|
|
|
Time = erlang:system_time(millisecond),
|
|
|
|
Query = {get_disco_item, Filter, JID, Lang, Time+Timeout},
|
|
|
|
try p1_fsm:sync_send_all_state_event(Pid, Query, Timeout) of
|
|
|
|
{item, Desc} ->
|
|
|
|
{ok, Desc};
|
|
|
|
false ->
|
|
|
|
{error, notfound}
|
|
|
|
catch _:{timeout, {p1_fsm, _, _}} ->
|
|
|
|
{error, timeout};
|
|
|
|
_:{_, {p1_fsm, _, _}} ->
|
|
|
|
{error, notfound}
|
|
|
|
end.
|
|
|
|
|
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,
|
2017-03-10 13:12:43 +01:00
|
|
|
RoomShaper, Creator, _Nick, DefRoomOpts, QueueType]) ->
|
2008-02-28 01:30:23 +01:00
|
|
|
process_flag(trap_exit, true),
|
2018-07-05 08:31:55 +02:00
|
|
|
Shaper = ejabberd_shaper:new(RoomShaper),
|
2017-03-10 13:12:43 +01:00
|
|
|
RoomQueue = room_queue_new(ServerHost, Shaper, QueueType),
|
2023-02-20 13:03:46 +01:00
|
|
|
State = set_opts(DefRoomOpts,
|
|
|
|
#state{host = Host, server_host = ServerHost,
|
|
|
|
access = Access, room = Room,
|
|
|
|
history = lqueue_new(HistorySize, QueueType),
|
|
|
|
jid = jid:make(Room, Host),
|
|
|
|
just_created = true,
|
|
|
|
room_queue = RoomQueue,
|
|
|
|
room_shaper = Shaper}),
|
|
|
|
State1 = set_affiliation(Creator, owner, State),
|
2016-06-26 08:08:37 +02:00
|
|
|
store_room(State1),
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Created MUC room ~ts@~ts by ~ts",
|
2017-02-26 08:07:12 +01:00
|
|
|
[Room, Host, jid:encode(Creator)]),
|
2009-12-29 15:43:24 +01:00
|
|
|
add_to_log(room_existence, created, State1),
|
|
|
|
add_to_log(room_existence, started, State1),
|
2020-08-28 14:53:32 +02:00
|
|
|
ejabberd_hooks:run(start_room, ServerHost, [ServerHost, Room, Host]),
|
2022-04-21 13:37:01 +02:00
|
|
|
erlang:send_after(?CLEAN_ROOM_TIMEOUT, self(),
|
|
|
|
close_room_if_temporary_and_empty),
|
2019-07-16 13:57:48 +02:00
|
|
|
{ok, normal_state, reset_hibernate_timer(State1)};
|
2017-03-10 13:12:43 +01:00
|
|
|
init([Host, ServerHost, Access, Room, HistorySize, RoomShaper, Opts, QueueType]) ->
|
2008-02-28 01:30:23 +01:00
|
|
|
process_flag(trap_exit, true),
|
2018-07-05 08:31:55 +02:00
|
|
|
Shaper = ejabberd_shaper:new(RoomShaper),
|
2017-03-10 13:12:43 +01:00
|
|
|
RoomQueue = room_queue_new(ServerHost, Shaper, QueueType),
|
2023-01-20 16:27:03 +01:00
|
|
|
Jid = jid:make(Room, Host),
|
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,
|
2017-03-10 13:12:43 +01:00
|
|
|
history = lqueue_new(HistorySize, QueueType),
|
2023-01-20 16:27:03 +01:00
|
|
|
jid = Jid,
|
2017-03-10 13:12:43 +01:00
|
|
|
room_queue = RoomQueue,
|
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),
|
2020-08-28 14:53:32 +02:00
|
|
|
ejabberd_hooks:run(start_room, ServerHost, [ServerHost, Room, Host]),
|
2021-12-14 07:54:00 +01:00
|
|
|
State1 = cleanup_affiliations(State),
|
2023-01-20 16:27:03 +01:00
|
|
|
State2 =
|
|
|
|
case {lists:keyfind(hibernation_time, 1, Opts),
|
|
|
|
(State1#state.config)#config.mam,
|
|
|
|
(State1#state.history)#lqueue.max} of
|
|
|
|
{{_, V}, true, L} when is_integer(V), L > 0 ->
|
|
|
|
{Msgs, _, _} = mod_mam:select(ServerHost, Jid, Jid, [],
|
|
|
|
#rsm_set{max = L, before = <<"9999999999999999">>},
|
|
|
|
groupchat, only_messages),
|
|
|
|
Hist2 =
|
|
|
|
lists:foldl(
|
|
|
|
fun({_, TS, #forwarded{sub_els = [#message{meta = #{archive_nick := Nick}} = Msg]}}, Hist) ->
|
|
|
|
Pkt = xmpp:set_from_to(Msg, jid:replace_resource(Jid, Nick), Jid),
|
|
|
|
Size = element_size(Pkt),
|
|
|
|
lqueue_in({Nick, Pkt, false, misc:usec_to_now(TS), Size}, Hist)
|
|
|
|
end, State1#state.history, Msgs),
|
|
|
|
State1#state{history = Hist2};
|
2023-01-20 17:12:50 +01:00
|
|
|
_ ->
|
2023-01-20 16:27:03 +01:00
|
|
|
State1
|
|
|
|
end,
|
2022-04-21 13:37:01 +02:00
|
|
|
erlang:send_after(?CLEAN_ROOM_TIMEOUT, self(),
|
|
|
|
close_room_if_temporary_and_empty),
|
2023-01-20 16:27:03 +01:00
|
|
|
{ok, normal_state, reset_hibernate_timer(State2)}.
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
normal_state({route, <<"">>,
|
|
|
|
#message{from = From, type = Type, lang = Lang} = Packet},
|
|
|
|
StateData) ->
|
2008-06-13 20:55:26 +02:00
|
|
|
case is_user_online(From, StateData) orelse
|
2016-09-08 15:39:34 +02:00
|
|
|
is_subscriber(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),
|
2019-02-27 09:56:20 +01:00
|
|
|
Now = erlang:system_time(microsecond),
|
2019-06-14 11:33:26 +02:00
|
|
|
MinMessageInterval = trunc(mod_muc_opt:min_message_interval(StateData#state.server_host) * 1000000),
|
2016-07-25 12:50:30 +02:00
|
|
|
Size = element_size(Packet),
|
|
|
|
{MessageShaper, MessageShaperInterval} =
|
2018-07-05 08:31:55 +02:00
|
|
|
ejabberd_shaper:update(Activity#activity.message_shaper, Size),
|
2016-07-25 12:50:30 +02:00
|
|
|
if Activity#activity.message /= undefined ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Traffic rate limit is exceeded"),
|
2016-11-25 09:41:24 +01:00
|
|
|
Err = xmpp:err_resource_constraint(ErrText, Lang),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, 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} =
|
2018-07-05 08:31:55 +02:00
|
|
|
ejabberd_shaper:update(StateData#state.room_shaper, Size),
|
2017-03-10 13:12:43 +01:00
|
|
|
RoomQueueEmpty = case StateData#state.room_queue of
|
|
|
|
undefined -> true;
|
|
|
|
RQ -> p1_queue:is_empty(RQ)
|
|
|
|
end,
|
2016-07-25 12:50:30 +02:00
|
|
|
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},
|
2017-02-24 19:25:25 +01:00
|
|
|
process_groupchat_message(Packet,
|
2016-07-25 12:50:30 +02:00
|
|
|
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},
|
2017-03-10 13:12:43 +01:00
|
|
|
RoomQueue = p1_queue:in({message, From},
|
|
|
|
StateData#state.room_queue),
|
2016-07-25 12:50:30 +02:00
|
|
|
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 ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrorText = ?T("It is not allowed to send error messages to the"
|
2020-01-22 12:52:30 +01:00
|
|
|
" room. The participant (~s) has sent an error "
|
|
|
|
"message (~s) and got kicked from the room"),
|
2016-07-25 12:50:30 +02:00
|
|
|
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 ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("It is not allowed to send private messages "
|
|
|
|
"to the conference"),
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_not_acceptable(ErrText, Lang),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err),
|
2016-07-25 12:50:30 +02:00
|
|
|
{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} ->
|
2017-11-14 07:02:43 +01:00
|
|
|
Txt = xmpp:io_format_error(Why),
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_bad_request(Txt, Lang),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err),
|
2016-07-25 12:50:30 +02:00
|
|
|
StateData
|
|
|
|
end};
|
|
|
|
true ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Improper message type"),
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_not_acceptable(ErrText, Lang),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err),
|
2016-07-25 12:50:30 +02:00
|
|
|
{next_state, normal_state, StateData};
|
|
|
|
false when Type /= error ->
|
2016-10-17 12:37:23 +02:00
|
|
|
handle_roommessage_from_nonparticipant(Packet, StateData, From),
|
|
|
|
{next_state, normal_state, StateData};
|
2016-07-25 12:50:30 +02:00
|
|
|
false ->
|
|
|
|
{next_state, normal_state, StateData}
|
2003-03-23 21:08:44 +01:00
|
|
|
end;
|
2017-02-16 09:00:26 +01:00
|
|
|
normal_state({route, <<"">>,
|
|
|
|
#iq{from = From, type = Type, lang = Lang, sub_els = [_]} = IQ0},
|
2016-07-25 12:50:30 +02:00
|
|
|
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};
|
2023-04-17 16:57:54 +02:00
|
|
|
{ignore, StateData2} ->
|
|
|
|
{next_state, normal_state, StateData2};
|
2016-07-25 12:50:30 +02:00
|
|
|
#iq{type = T} = IQRes when T == error; T == result ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route(IQRes),
|
2016-07-25 12:50:30 +02:00
|
|
|
{next_state, normal_state, StateData};
|
|
|
|
#iq{sub_els = [SubEl]} = IQ ->
|
2019-07-15 12:59:41 +02:00
|
|
|
Res1 = case SubEl of
|
|
|
|
#muc_admin{} ->
|
2016-07-25 12:50:30 +02:00
|
|
|
process_iq_admin(From, IQ, StateData);
|
2019-07-15 12:59:41 +02:00
|
|
|
#muc_owner{} ->
|
2016-07-25 12:50:30 +02:00
|
|
|
process_iq_owner(From, IQ, StateData);
|
2019-07-15 12:59:41 +02:00
|
|
|
#disco_info{} ->
|
2016-07-25 12:50:30 +02:00
|
|
|
process_iq_disco_info(From, IQ, StateData);
|
2019-07-15 12:59:41 +02:00
|
|
|
#disco_items{} ->
|
2016-07-25 12:50:30 +02:00
|
|
|
process_iq_disco_items(From, IQ, StateData);
|
2019-07-15 12:59:41 +02:00
|
|
|
#vcard_temp{} ->
|
2016-07-25 12:50:30 +02:00
|
|
|
process_iq_vcard(From, IQ, StateData);
|
2019-07-15 12:59:41 +02:00
|
|
|
#muc_subscribe{} ->
|
2016-07-25 12:50:30 +02:00
|
|
|
process_iq_mucsub(From, IQ, StateData);
|
2019-07-15 12:59:41 +02:00
|
|
|
#muc_unsubscribe{} ->
|
|
|
|
process_iq_mucsub(From, IQ, StateData);
|
|
|
|
#muc_subscriptions{} ->
|
|
|
|
process_iq_mucsub(From, IQ, StateData);
|
|
|
|
#xcaptcha{} ->
|
2016-07-25 12:50:30 +02:00
|
|
|
process_iq_captcha(From, IQ, StateData);
|
2021-10-29 04:12:26 +02:00
|
|
|
#adhoc_command{} ->
|
|
|
|
process_iq_adhoc(From, IQ, StateData);
|
2023-03-30 14:37:13 +02:00
|
|
|
#fasten_apply_to{} = ApplyTo ->
|
|
|
|
case xmpp:get_subtag(ApplyTo, #message_moderate{}) of
|
|
|
|
#message_moderate{} = Moderate ->
|
|
|
|
process_iq_moderate(From, IQ, ApplyTo, Moderate, StateData);
|
|
|
|
_ ->
|
|
|
|
Txt = ?T("The feature requested is not "
|
|
|
|
"supported by the conference"),
|
|
|
|
{error, xmpp:err_service_unavailable(Txt, Lang)}
|
|
|
|
end;
|
2016-07-25 12:50:30 +02:00
|
|
|
_ ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("The feature requested is not "
|
|
|
|
"supported by the conference"),
|
2016-10-17 12:37:23 +02:00
|
|
|
{error, xmpp:err_service_unavailable(Txt, Lang)}
|
2016-07-25 12:50:30 +02:00
|
|
|
end,
|
|
|
|
{IQRes, NewStateData} =
|
|
|
|
case Res1 of
|
|
|
|
{result, Res, SD} ->
|
|
|
|
{xmpp:make_iq_result(IQ, Res), SD};
|
|
|
|
{result, Res} ->
|
|
|
|
{xmpp:make_iq_result(IQ, Res), StateData};
|
|
|
|
{ignore, SD} ->
|
|
|
|
{ignore, SD};
|
|
|
|
{error, Error} ->
|
|
|
|
{xmpp:make_error(IQ0, Error), StateData}
|
|
|
|
end,
|
|
|
|
if IQRes /= ignore ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route(IQRes);
|
2016-07-25 12:50:30 +02:00
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
case NewStateData of
|
2016-10-17 12:37:23 +02:00
|
|
|
stop ->
|
2019-04-17 17:23:21 +02:00
|
|
|
Conf = StateData#state.config,
|
|
|
|
{stop, normal, StateData#state{config = Conf#config{persistent = false}}};
|
2016-10-17 12:37:23 +02:00
|
|
|
_ when NewStateData#state.just_created ->
|
|
|
|
close_room_if_temporary_and_empty(NewStateData);
|
|
|
|
_ ->
|
|
|
|
{next_state, normal_state, NewStateData}
|
2016-07-25 12:50:30 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
catch _:{xmpp_codec, Why} ->
|
2017-11-14 07:02:43 +01:00
|
|
|
ErrTxt = xmpp:io_format_error(Why),
|
2016-11-25 09:41:24 +01:00
|
|
|
Err = xmpp:err_bad_request(ErrTxt, Lang),
|
2017-12-19 07:15:35 +01:00
|
|
|
ejabberd_router:route_error(IQ0, Err),
|
|
|
|
{next_state, normal_state, StateData}
|
2016-07-25 12:50:30 +02:00
|
|
|
end;
|
2017-02-16 09:00:26 +01:00
|
|
|
normal_state({route, <<"">>, #iq{} = IQ}, StateData) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_bad_request(),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(IQ, Err),
|
2016-10-17 12:37:23 +02:00
|
|
|
case StateData#state.just_created of
|
|
|
|
true -> {stop, normal, StateData};
|
2019-02-06 16:13:30 +01:00
|
|
|
_ -> {next_state, normal_state, StateData}
|
2016-10-17 12:37:23 +02:00
|
|
|
end;
|
2017-02-16 09:00:26 +01:00
|
|
|
normal_state({route, Nick, #presence{from = From} = Packet}, StateData) ->
|
2007-09-01 23:05:04 +02:00
|
|
|
Activity = get_user_activity(From, StateData),
|
2019-02-27 09:56:20 +01:00
|
|
|
Now = erlang:system_time(microsecond),
|
2007-06-25 18:43:42 +02:00
|
|
|
MinPresenceInterval =
|
2019-06-14 11:33:26 +02:00
|
|
|
trunc(mod_muc_opt:min_presence_interval(StateData#state.server_host) * 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),
|
2017-02-24 19:25:25 +01:00
|
|
|
process_presence(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;
|
2017-02-16 09:00:26 +01:00
|
|
|
normal_state({route, ToNick,
|
|
|
|
#message{from = From, 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, []),
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrorText = ?T("It is not allowed to send error messages to the"
|
2020-01-22 12:52:30 +01:00
|
|
|
" room. The participant (~s) has sent an error "
|
|
|
|
"message (~s) and got kicked from the room"),
|
2016-07-25 12:50:30 +02:00
|
|
|
NewState = expulse_participant(Packet, From, StateData,
|
|
|
|
translate:translate(Lang, ErrorText)),
|
|
|
|
{next_state, normal_state, NewState};
|
|
|
|
forget_message ->
|
|
|
|
{next_state, normal_state, StateData};
|
|
|
|
continue_delivery ->
|
2023-08-08 13:28:02 +02:00
|
|
|
case {is_user_allowed_private_message(From, StateData),
|
2016-11-12 11:27:15 +01:00
|
|
|
is_user_online(From, StateData) orelse
|
2021-01-08 20:30:15 +01:00
|
|
|
is_subscriber(From, StateData) orelse
|
|
|
|
is_user_allowed_message_nonparticipant(From, StateData)} of
|
2016-07-25 12:50:30 +02:00
|
|
|
{true, true} when Type == groupchat ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("It is not allowed to send private messages "
|
|
|
|
"of type \"groupchat\""),
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_bad_request(ErrText, Lang),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err);
|
2016-07-25 12:50:30 +02:00
|
|
|
{true, true} ->
|
|
|
|
case find_jids_by_nick(ToNick, StateData) of
|
|
|
|
[] ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Recipient is not in the conference room"),
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_item_not_found(ErrText, Lang),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(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 ->
|
2016-09-08 15:39:34 +02:00
|
|
|
{FromNick, _} = get_participant_data(From, StateData),
|
2016-07-25 12:50:30 +02:00
|
|
|
FromNickJID =
|
|
|
|
jid:replace_resource(StateData#state.jid,
|
|
|
|
FromNick),
|
|
|
|
X = #muc_user{},
|
2023-08-04 16:07:14 +02:00
|
|
|
Packet2 = xmpp:set_subtag(Packet, X),
|
|
|
|
case ejabberd_hooks:run_fold(muc_filter_message,
|
|
|
|
StateData#state.server_host,
|
2023-08-24 11:54:26 +02:00
|
|
|
xmpp:put_meta(Packet2, mam_ignore, true),
|
2023-08-04 16:07:14 +02:00
|
|
|
[StateData, FromNick]) of
|
|
|
|
drop ->
|
|
|
|
ok;
|
|
|
|
Packet3 ->
|
2023-08-24 11:54:26 +02:00
|
|
|
PrivMsg = xmpp:set_from(xmpp:del_meta(Packet3, mam_ignore), FromNickJID),
|
2023-08-04 16:07:14 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(ToJID) ->
|
|
|
|
ejabberd_router:route(xmpp:set_to(PrivMsg, ToJID))
|
|
|
|
end, ToJIDs)
|
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
2023-08-08 13:28:02 +02:00
|
|
|
ErrText = ?T("You are not allowed to send private messages"),
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_forbidden(ErrText, Lang),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err)
|
2008-01-29 15:49:08 +01:00
|
|
|
end
|
2016-07-25 12:50:30 +02:00
|
|
|
end;
|
|
|
|
{true, false} ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Only occupants are allowed to send messages "
|
|
|
|
"to the conference"),
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_not_acceptable(ErrText, Lang),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err);
|
2016-07-25 12:50:30 +02:00
|
|
|
{false, _} ->
|
2023-08-08 13:28:02 +02:00
|
|
|
ErrText = ?T("You are not allowed to send private messages"),
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_forbidden(ErrText, Lang),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err)
|
2016-07-25 12:50:30 +02:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
{next_state, normal_state, StateData}
|
2008-01-29 15:49:08 +01:00
|
|
|
end;
|
2017-02-16 09:00:26 +01:00
|
|
|
normal_state({route, ToNick,
|
2018-11-29 10:16:12 +01:00
|
|
|
#iq{from = From, lang = Lang} = Packet},
|
|
|
|
#state{config = #config{allow_query_users = AllowQuery}} = StateData) ->
|
|
|
|
try maps:get(jid:tolower(From), StateData#state.users) of
|
|
|
|
#user{nick = FromNick} when AllowQuery orelse ToNick == FromNick ->
|
2016-07-25 12:50:30 +02:00
|
|
|
case find_jid_by_nick(ToNick, StateData) of
|
|
|
|
false ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Recipient is not in the conference room"),
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_item_not_found(ErrText, Lang),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err);
|
2017-11-10 16:02:22 +01:00
|
|
|
To ->
|
|
|
|
FromJID = jid:replace_resource(StateData#state.jid, FromNick),
|
2018-11-29 10:16:12 +01:00
|
|
|
case direct_iq_type(Packet) of
|
|
|
|
vcard ->
|
2017-11-10 16:02:22 +01:00
|
|
|
ejabberd_router:route_iq(
|
2018-11-29 10:16:12 +01:00
|
|
|
xmpp:set_from_to(Packet, FromJID, jid:remove_resource(To)),
|
|
|
|
Packet, self());
|
|
|
|
ping when ToNick == FromNick ->
|
|
|
|
%% Self-ping optimization from XEP-0410
|
|
|
|
ejabberd_router:route(xmpp:make_iq_result(Packet));
|
|
|
|
response ->
|
|
|
|
ejabberd_router:route(xmpp:set_from_to(Packet, FromJID, To));
|
|
|
|
#stanza_error{} = Err ->
|
|
|
|
ejabberd_router:route_error(Packet, Err);
|
|
|
|
_OtherRequest ->
|
|
|
|
ejabberd_router:route_iq(
|
|
|
|
xmpp:set_from_to(Packet, FromJID, To), Packet, self())
|
2017-11-10 16:02:22 +01:00
|
|
|
end
|
2016-07-25 12:50:30 +02:00
|
|
|
end;
|
|
|
|
_ ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Queries to the conference members are "
|
|
|
|
"not allowed in this room"),
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_not_allowed(ErrText, Lang),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err)
|
2018-11-29 10:16:12 +01:00
|
|
|
catch _:{badkey, _} ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Only occupants are allowed to send queries "
|
|
|
|
"to the conference"),
|
2018-11-29 10:16:12 +01:00
|
|
|
Err = xmpp:err_not_acceptable(ErrText, Lang),
|
|
|
|
ejabberd_router:route_error(Packet, Err)
|
2003-03-25 22:03:35 +01:00
|
|
|
end,
|
2003-03-23 21:08:44 +01:00
|
|
|
{next_state, normal_state, StateData};
|
2019-07-16 13:57:48 +02:00
|
|
|
normal_state(hibernate, StateData) ->
|
|
|
|
case maps:size(StateData#state.users) of
|
|
|
|
0 ->
|
2022-02-18 17:21:22 +01:00
|
|
|
store_room_no_checks(StateData, [], true),
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Hibernating room ~ts@~ts", [StateData#state.room, StateData#state.host]),
|
2019-07-16 13:57:48 +02:00
|
|
|
{stop, normal, StateData#state{hibernate_timer = hibernating}};
|
|
|
|
_ ->
|
|
|
|
{next_state, normal_state, StateData}
|
|
|
|
end;
|
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,
|
2021-09-13 07:15:11 +02:00
|
|
|
get_users_and_subscribers_with_node(?NS_MUCSUB_NODES_MESSAGES, StateData),
|
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) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
_ = destroy_room(#muc_destroy{xmlns = ?NS_MUC_OWNER, reason = Reason}, StateData),
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Destroyed MUC room ~ts with reason: ~p",
|
2017-02-26 08:07:12 +01:00
|
|
|
[jid:encode(StateData#state.jid), Reason]),
|
2009-12-29 15:43:24 +01:00
|
|
|
add_to_log(room_existence, destroyed, StateData),
|
2019-04-17 17:23:21 +02:00
|
|
|
Conf = StateData#state.config,
|
|
|
|
{stop, shutdown, StateData#state{config = Conf#config{persistent = false}}};
|
2007-02-19 10:45:58 +01:00
|
|
|
handle_event(destroy, StateName, StateData) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Destroyed MUC room ~ts",
|
2017-02-26 08:07:12 +01:00
|
|
|
[jid:encode(StateData#state.jid)]),
|
2017-08-21 15:57:48 +02:00
|
|
|
handle_event({destroy, <<"">>}, StateName, StateData);
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_event({set_affiliations, Affiliations},
|
|
|
|
StateName, StateData) ->
|
2018-04-23 10:35:43 +02:00
|
|
|
NewStateData = set_affiliations(Affiliations, StateData),
|
|
|
|
{next_state, StateName, NewStateData};
|
2021-12-20 07:31:01 +01:00
|
|
|
handle_event({process_item_change, Item, UJID}, StateName, StateData) ->
|
|
|
|
case process_item_change(Item, StateData, UJID) of
|
|
|
|
{error, _} ->
|
|
|
|
{next_state, StateName, StateData};
|
|
|
|
StateData ->
|
|
|
|
{next_state, StateName, StateData};
|
|
|
|
NSD ->
|
|
|
|
store_room(NSD),
|
|
|
|
{next_state, StateName, NSD}
|
|
|
|
end;
|
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}.
|
|
|
|
|
2019-05-11 18:27:56 +02:00
|
|
|
handle_sync_event({get_disco_item, Filter, JID, Lang, Time}, _From, StateName, StateData) ->
|
2018-11-15 12:13:45 +01:00
|
|
|
Len = maps:size(StateData#state.nicks),
|
2015-12-02 01:50:30 +01:00
|
|
|
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,
|
2019-06-06 20:26:50 +02:00
|
|
|
CurrentTime = erlang:system_time(millisecond),
|
2019-05-11 18:27:56 +02:00
|
|
|
if CurrentTime < Time ->
|
|
|
|
{reply, Reply, StateName, StateData};
|
|
|
|
true ->
|
|
|
|
{next_state, StateName, StateData}
|
|
|
|
end;
|
|
|
|
%% These two clauses are only for backward compatibility with nodes running old code
|
2015-12-02 01:50:30 +01:00
|
|
|
handle_sync_event({get_disco_item, JID, Lang}, From, StateName, StateData) ->
|
|
|
|
handle_sync_event({get_disco_item, any, JID, Lang}, From, StateName, StateData);
|
2019-05-11 18:27:56 +02:00
|
|
|
handle_sync_event({get_disco_item, Filter, JID, Lang}, From, StateName, StateData) ->
|
|
|
|
handle_sync_event({get_disco_item, Filter, JID, Lang, infinity}, From, StateName, StateData);
|
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};
|
2022-03-07 06:42:42 +01:00
|
|
|
handle_sync_event(get_info, _From, StateName,
|
|
|
|
StateData) ->
|
|
|
|
Result = #{occupants_number => maps:size(StateData#state.users)},
|
|
|
|
{reply, Result, 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) ->
|
2019-06-25 16:41:47 +02:00
|
|
|
Mod = gen_mod:db_mod(NewStateData#state.server_host, mod_muc),
|
|
|
|
case erlang:function_exported(Mod, get_subscribed_rooms, 3) of
|
|
|
|
true ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
2021-09-13 07:15:11 +02:00
|
|
|
erlang:put(muc_subscribers, NewStateData#state.muc_subscribers#muc_subscribers.subscribers)
|
2019-06-25 16:41:47 +02:00
|
|
|
end,
|
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) ->
|
2016-10-17 12:37:23 +02:00
|
|
|
case process_item_change(Item, StateData, UJID) of
|
|
|
|
{error, _} = Err ->
|
|
|
|
{reply, Err, StateName, StateData};
|
2021-12-20 07:31:01 +01:00
|
|
|
StateData ->
|
|
|
|
{reply, {ok, StateData}, StateName, StateData};
|
2016-10-17 12:37:23 +02:00
|
|
|
NSD ->
|
2020-12-04 13:06:53 +01:00
|
|
|
store_room(NSD),
|
2016-10-17 12:37:23 +02:00
|
|
|
{reply, {ok, NSD}, StateName, NSD}
|
|
|
|
end;
|
2016-09-06 16:55:18 +02:00
|
|
|
handle_sync_event(get_subscribers, _From, StateName, StateData) ->
|
2021-09-13 07:15:11 +02:00
|
|
|
JIDs = muc_subscribers_fold(
|
|
|
|
fun(_LBareJID, #subscriber{jid = JID}, Acc) ->
|
|
|
|
[JID | Acc]
|
|
|
|
end, [], StateData#state.muc_subscribers),
|
2016-09-06 16:55:18 +02:00
|
|
|
{reply, {ok, JIDs}, StateName, StateData};
|
2016-08-09 12:36:43 +02:00
|
|
|
handle_sync_event({muc_subscribe, From, Nick, Nodes}, _From,
|
|
|
|
StateName, StateData) ->
|
2018-07-05 10:51:49 +02:00
|
|
|
IQ = #iq{type = set, id = p1_rand:get_string(),
|
2016-11-12 11:27:15 +01:00
|
|
|
from = From, sub_els = [#muc_subscribe{nick = Nick,
|
|
|
|
events = Nodes}]},
|
2016-08-09 12:36:43 +02:00
|
|
|
Config = StateData#state.config,
|
|
|
|
CaptchaRequired = Config#config.captcha_protected,
|
|
|
|
PasswordProtected = Config#config.password_protected,
|
2022-12-01 11:52:39 +01:00
|
|
|
MembersOnly = Config#config.members_only,
|
2016-08-09 12:36:43 +02:00
|
|
|
TmpConfig = Config#config{captcha_protected = false,
|
2022-12-01 11:52:39 +01:00
|
|
|
password_protected = false,
|
|
|
|
members_only = false},
|
2016-08-09 12:36:43 +02:00
|
|
|
TmpState = StateData#state{config = TmpConfig},
|
2016-11-12 11:27:15 +01:00
|
|
|
case process_iq_mucsub(From, IQ, TmpState) of
|
|
|
|
{result, #muc_subscribe{events = NewNodes}, NewState} ->
|
2016-08-09 12:36:43 +02:00
|
|
|
NewConfig = (NewState#state.config)#config{
|
|
|
|
captcha_protected = CaptchaRequired,
|
2022-12-01 11:52:39 +01:00
|
|
|
password_protected = PasswordProtected,
|
|
|
|
members_only = MembersOnly},
|
2016-11-12 11:27:15 +01:00
|
|
|
{reply, {ok, NewNodes}, StateName,
|
2016-08-09 12:36:43 +02:00
|
|
|
NewState#state{config = NewConfig}};
|
|
|
|
{ignore, NewState} ->
|
|
|
|
NewConfig = (NewState#state.config)#config{
|
|
|
|
captcha_protected = CaptchaRequired,
|
2022-12-01 11:52:39 +01:00
|
|
|
password_protected = PasswordProtected,
|
|
|
|
members_only = MembersOnly},
|
2019-07-08 23:47:54 +02:00
|
|
|
{reply, {error, ?T("Request is ignored")},
|
2016-08-09 12:36:43 +02:00
|
|
|
NewState#state{config = NewConfig}};
|
|
|
|
{error, Err} ->
|
|
|
|
{reply, {error, get_error_text(Err)}, StateName, StateData}
|
|
|
|
end;
|
2020-03-23 13:16:48 +01:00
|
|
|
handle_sync_event({muc_unsubscribe, From}, _From, StateName,
|
|
|
|
#state{config = Conf} = StateData) ->
|
2018-07-05 10:51:49 +02:00
|
|
|
IQ = #iq{type = set, id = p1_rand:get_string(),
|
2016-11-12 11:27:15 +01:00
|
|
|
from = From, sub_els = [#muc_unsubscribe{}]},
|
|
|
|
case process_iq_mucsub(From, IQ, StateData) of
|
2020-03-23 13:16:48 +01:00
|
|
|
{result, _, stop} ->
|
2020-03-24 11:44:22 +01:00
|
|
|
{stop, normal, StateData#state{config = Conf#config{persistent = false}}};
|
2016-08-09 12:36:43 +02:00
|
|
|
{result, _, NewState} ->
|
|
|
|
{reply, ok, StateName, NewState};
|
|
|
|
{ignore, NewState} ->
|
2019-07-08 23:47:54 +02:00
|
|
|
{reply, {error, ?T("Request is ignored")}, NewState};
|
2016-08-09 12:36:43 +02:00
|
|
|
{error, Err} ->
|
|
|
|
{reply, {error, get_error_text(Err)}, StateName, StateData}
|
|
|
|
end;
|
2016-09-19 13:46:01 +02:00
|
|
|
handle_sync_event({is_subscribed, From}, _From, StateName, StateData) ->
|
2021-09-13 07:15:11 +02:00
|
|
|
IsSubs = try muc_subscribers_get(
|
|
|
|
jid:split(From), StateData#state.muc_subscribers) of
|
2020-04-03 12:34:57 +02:00
|
|
|
#subscriber{nick = Nick, nodes = Nodes} -> {true, Nick, Nodes}
|
2018-11-15 13:07:58 +01:00
|
|
|
catch _:{badkey, _} -> false
|
2018-11-15 12:13:45 +01:00
|
|
|
end,
|
2016-09-19 13:46:01 +02:00
|
|
|
{reply, IsSubs, StateName, StateData};
|
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) ->
|
2017-03-10 13:12:43 +01:00
|
|
|
RoomQueueEmpty = p1_queue:is_empty(StateData#state.room_queue),
|
|
|
|
RoomQueue = p1_queue:in({presence, 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_user_message, From},
|
|
|
|
normal_state = _StateName, StateData) ->
|
|
|
|
RoomQueueEmpty =
|
2017-03-10 13:12:43 +01:00
|
|
|
p1_queue:is_empty(StateData#state.room_queue),
|
|
|
|
RoomQueue = p1_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) ->
|
2017-03-10 13:12:43 +01:00
|
|
|
case p1_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),
|
2017-02-24 19:25:25 +01:00
|
|
|
process_groupchat_message(Packet, StateData3);
|
2013-03-14 10:33:02 +01:00
|
|
|
{{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),
|
2017-02-24 19:25:25 +01:00
|
|
|
process_presence(Nick, Packet, StateData3);
|
2013-03-14 10:33:02 +01:00
|
|
|
{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) ->
|
2018-11-15 12:13:45 +01:00
|
|
|
NewState = case maps:get(From, StateData#state.robots, passed) of
|
|
|
|
{Nick, Packet} ->
|
|
|
|
Robots = maps:put(From, passed, StateData#state.robots),
|
|
|
|
add_new_user(From, Nick, Packet,
|
|
|
|
StateData#state{robots = Robots});
|
|
|
|
passed ->
|
|
|
|
StateData
|
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) ->
|
2018-11-15 12:13:45 +01:00
|
|
|
NewState = case maps:get(From, StateData#state.robots, passed) of
|
|
|
|
{_Nick, Packet} ->
|
|
|
|
Robots = maps:remove(From, StateData#state.robots),
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("The CAPTCHA verification has failed"),
|
2018-11-15 12:13:45 +01:00
|
|
|
Lang = xmpp:get_lang(Packet),
|
|
|
|
Err = xmpp:err_not_authorized(Txt, Lang),
|
|
|
|
ejabberd_router:route_error(Packet, Err),
|
|
|
|
StateData#state{robots = Robots};
|
|
|
|
passed ->
|
|
|
|
StateData
|
2009-03-13 17:01:46 +01:00
|
|
|
end,
|
|
|
|
{next_state, normal_state, NewState};
|
2022-04-21 13:37:01 +02:00
|
|
|
handle_info(close_room_if_temporary_and_empty, _StateName, StateData) ->
|
|
|
|
close_room_if_temporary_and_empty(StateData);
|
2013-06-19 13:30:31 +02:00
|
|
|
handle_info(shutdown, _StateName, StateData) ->
|
|
|
|
{stop, shutdown, StateData};
|
2017-11-10 16:02:22 +01:00
|
|
|
handle_info({iq_reply, #iq{type = Type, sub_els = Els},
|
|
|
|
#iq{from = From, to = To} = IQ}, StateName, StateData) ->
|
|
|
|
ejabberd_router:route(
|
|
|
|
xmpp:set_from_to(
|
|
|
|
IQ#iq{type = Type, sub_els = Els},
|
|
|
|
To, From)),
|
|
|
|
{next_state, StateName, StateData};
|
|
|
|
handle_info({iq_reply, timeout, IQ}, StateName, StateData) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Request has timed out"),
|
2017-11-10 16:02:22 +01:00
|
|
|
Err = xmpp:err_recipient_unavailable(Txt, IQ#iq.lang),
|
|
|
|
ejabberd_router:route_error(IQ, Err),
|
|
|
|
{next_state, StateName, StateData};
|
2018-07-08 13:52:12 +02:00
|
|
|
handle_info(config_reloaded, StateName, StateData) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Max = mod_muc_opt:history_size(StateData#state.server_host),
|
2018-07-08 13:52:12 +02:00
|
|
|
History1 = StateData#state.history,
|
|
|
|
Q1 = History1#lqueue.queue,
|
|
|
|
Q2 = case p1_queue:len(Q1) of
|
|
|
|
Len when Len > Max ->
|
|
|
|
lqueue_cut(Q1, Len-Max);
|
|
|
|
_ ->
|
|
|
|
Q1
|
|
|
|
end,
|
|
|
|
History2 = History1#lqueue{queue = Q2, max = Max},
|
|
|
|
{next_state, StateName, StateData#state{history = History2}};
|
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}.
|
|
|
|
|
2019-04-17 17:23:21 +02:00
|
|
|
terminate(Reason, _StateName,
|
|
|
|
#state{server_host = LServer, host = Host, room = Room} = StateData) ->
|
2019-01-22 14:22:15 +01:00
|
|
|
try
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Stopping MUC room ~ts@~ts", [Room, Host]),
|
2019-01-22 14:22:15 +01:00
|
|
|
ReasonT = case Reason of
|
|
|
|
shutdown ->
|
2019-06-22 16:08:45 +02:00
|
|
|
?T("You are being removed from the room "
|
|
|
|
"because of a system shutdown");
|
|
|
|
_ -> ?T("Room terminates")
|
2019-01-22 14:22:15 +01:00
|
|
|
end,
|
|
|
|
Packet = #presence{
|
|
|
|
type = unavailable,
|
|
|
|
sub_els = [#muc_user{items = [#muc_item{affiliation = none,
|
|
|
|
reason = ReasonT,
|
|
|
|
role = none}],
|
|
|
|
status_codes = [332,110]}]},
|
|
|
|
maps:fold(
|
2019-06-27 14:22:27 +02:00
|
|
|
fun(_, #user{nick = Nick, jid = JID}, _) ->
|
2019-01-22 14:22:15 +01:00
|
|
|
case Reason of
|
|
|
|
shutdown ->
|
|
|
|
send_wrapped(jid:replace_resource(StateData#state.jid, Nick),
|
2019-06-27 14:22:27 +02:00
|
|
|
JID, Packet,
|
2019-01-22 14:22:15 +01:00
|
|
|
?NS_MUCSUB_NODES_PARTICIPANTS,
|
|
|
|
StateData);
|
|
|
|
_ -> ok
|
|
|
|
end,
|
2019-06-27 14:22:27 +02:00
|
|
|
tab_remove_online_user(JID, StateData)
|
2021-09-13 07:15:11 +02:00
|
|
|
end, [], get_users_and_subscribers_with_node(
|
|
|
|
?NS_MUCSUB_NODES_PARTICIPANTS, StateData)),
|
2019-07-16 13:57:48 +02:00
|
|
|
|
|
|
|
disable_hibernate_timer(StateData),
|
|
|
|
case StateData#state.hibernate_timer of
|
|
|
|
hibernating ->
|
|
|
|
ok;
|
2019-04-17 17:23:21 +02:00
|
|
|
_ ->
|
2019-07-16 13:57:48 +02:00
|
|
|
add_to_log(room_existence, stopped, StateData),
|
|
|
|
case (StateData#state.config)#config.persistent of
|
|
|
|
false ->
|
|
|
|
ejabberd_hooks:run(room_destroyed, LServer, [LServer, Room, Host]);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end
|
2021-03-03 11:30:43 +01:00
|
|
|
end
|
2019-01-22 14:40:01 +01:00
|
|
|
catch ?EX_RULE(E, R, St) ->
|
2019-06-25 23:05:41 +02:00
|
|
|
StackTrace = ?EX_STACK(St),
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Got exception on room termination:~n** ~ts",
|
2019-07-07 21:12:14 +02:00
|
|
|
[misc:format_exception(2, E, R, StackTrace)])
|
|
|
|
end.
|
2003-03-23 21:08:44 +01:00
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
2017-02-16 09:00:26 +01:00
|
|
|
-spec route(pid(), stanza()) -> ok.
|
|
|
|
route(Pid, Packet) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Routing to MUC room ~p:~n~ts", [Pid, xmpp:pp(Packet)]),
|
2017-02-16 09:00:26 +01:00
|
|
|
#jid{lresource = Nick} = xmpp:get_to(Packet),
|
2017-08-05 19:58:21 +02:00
|
|
|
p1_fsm:send_event(Pid, {route, Nick, Packet}).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2017-02-24 19:25:25 +01:00
|
|
|
-spec process_groupchat_message(message(), state()) -> fsm_next().
|
|
|
|
process_groupchat_message(#message{from = From, lang = Lang} = Packet, StateData) ->
|
2016-09-07 09:33:37 +02:00
|
|
|
IsSubscriber = is_subscriber(From, StateData),
|
|
|
|
case is_user_online(From, StateData) orelse IsSubscriber orelse
|
2013-03-14 10:33:02 +01:00
|
|
|
is_user_allowed_message_nonparticipant(From, StateData)
|
|
|
|
of
|
|
|
|
true ->
|
2016-09-07 09:33:37 +02:00
|
|
|
{FromNick, Role} = get_participant_data(From, StateData),
|
2021-02-17 13:09:29 +01:00
|
|
|
#config{moderated = Moderated} = StateData#state.config,
|
2021-02-17 10:45:30 +01:00
|
|
|
AllowedByModerationRules =
|
|
|
|
case {Role == moderator orelse Role == participant orelse
|
2021-02-17 13:09:29 +01:00
|
|
|
not Moderated, IsSubscriber} of
|
2021-02-17 10:45:30 +01:00
|
|
|
{true, _} -> true;
|
|
|
|
{_, true} ->
|
2023-02-20 12:55:31 +01:00
|
|
|
% We assume all subscribers are at least members
|
|
|
|
true;
|
2021-02-17 10:45:30 +01:00
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end,
|
|
|
|
if AllowedByModerationRules ->
|
|
|
|
Subject = check_subject(Packet),
|
|
|
|
{NewStateData1, IsAllowed} =
|
|
|
|
case Subject of
|
|
|
|
[] ->
|
|
|
|
{StateData, true};
|
|
|
|
_ ->
|
|
|
|
case
|
|
|
|
can_change_subject(Role,
|
|
|
|
IsSubscriber,
|
|
|
|
StateData)
|
|
|
|
of
|
|
|
|
true ->
|
|
|
|
NSD =
|
|
|
|
StateData#state{subject = Subject,
|
2023-08-15 12:44:41 +02:00
|
|
|
subject_author = {FromNick, From}},
|
2021-02-17 10:45:30 +01:00
|
|
|
store_room(NSD),
|
|
|
|
{NSD, true};
|
|
|
|
_ -> {StateData, false}
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
case IsAllowed of
|
2013-03-14 10:33:02 +01:00
|
|
|
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,
|
2017-02-24 19:25:25 +01:00
|
|
|
[StateData, FromNick])
|
2015-06-26 21:33:32 +02:00
|
|
|
of
|
|
|
|
drop ->
|
|
|
|
{next_state, normal_state, StateData};
|
2015-09-25 17:00:00 +02:00
|
|
|
NewPacket1 ->
|
2023-03-30 14:37:13 +02:00
|
|
|
NewPacket = xmpp:put_meta(xmpp:remove_subtag(
|
|
|
|
add_stanza_id(NewPacket1, StateData), #nick{}),
|
2018-09-06 12:40:05 +02:00
|
|
|
muc_sender_real_jid, From),
|
2017-11-13 09:25:35 +01:00
|
|
|
Node = if Subject == [] -> ?NS_MUCSUB_NODES_MESSAGES;
|
2016-06-26 08:08:37 +02:00
|
|
|
true -> ?NS_MUCSUB_NODES_SUBJECT
|
|
|
|
end,
|
2023-04-14 12:19:38 +02:00
|
|
|
NewStateData2 = check_message_for_retractions(NewPacket1, NewStateData1),
|
2016-06-26 08:08:37 +02:00
|
|
|
send_wrapped_multiple(
|
|
|
|
jid:replace_resource(StateData#state.jid, FromNick),
|
2021-09-13 07:15:11 +02:00
|
|
|
get_users_and_subscribers_with_node(Node, StateData),
|
2023-04-14 12:19:38 +02:00
|
|
|
NewPacket, Node, NewStateData2),
|
|
|
|
NewStateData3 = 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,
|
2023-04-14 12:19:38 +02:00
|
|
|
NewStateData2);
|
2015-06-26 21:33:32 +02:00
|
|
|
false ->
|
2023-04-14 12:19:38 +02:00
|
|
|
NewStateData2
|
2015-06-26 21:33:32 +02:00
|
|
|
end,
|
2023-04-14 12:19:38 +02:00
|
|
|
{next_state, normal_state, NewStateData3}
|
2015-06-26 21:33:32 +02:00
|
|
|
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(
|
2019-06-22 16:08:45 +02:00
|
|
|
?T("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(
|
2019-06-22 16:08:45 +02:00
|
|
|
?T("Only moderators are allowed to change "
|
|
|
|
"the subject in this room"), Lang)
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err),
|
2013-03-14 10:33:02 +01:00
|
|
|
{next_state, normal_state, StateData}
|
|
|
|
end;
|
|
|
|
true ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Visitors are not allowed to send messages "
|
|
|
|
"to all occupants"),
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_forbidden(ErrText, Lang),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err),
|
2013-03-14 10:33:02 +01:00
|
|
|
{next_state, normal_state, StateData}
|
|
|
|
end;
|
|
|
|
false ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Only occupants are allowed to send messages "
|
|
|
|
"to the conference"),
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_not_acceptable(ErrText, Lang),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err),
|
2013-03-14 10:33:02 +01:00
|
|
|
{next_state, normal_state, StateData}
|
2007-06-25 18:43:42 +02:00
|
|
|
end.
|
|
|
|
|
2023-04-14 12:19:38 +02:00
|
|
|
-spec check_message_for_retractions(Packet :: message(), State :: state()) -> state().
|
|
|
|
check_message_for_retractions(Packet,
|
2023-08-07 12:32:36 +02:00
|
|
|
#state{config = Config, room = Room, host = Host,
|
|
|
|
server_host = Server} = State) ->
|
2023-04-14 12:19:38 +02:00
|
|
|
case xmpp:get_subtag(Packet, #fasten_apply_to{}) of
|
|
|
|
#fasten_apply_to{id = ID} = F ->
|
|
|
|
case xmpp:get_subtag(F, #message_retract{}) of
|
|
|
|
#message_retract{} ->
|
|
|
|
#jid{luser = U, lserver = S} = xmpp:get_from(Packet),
|
|
|
|
case remove_from_history({U, S}, ID, State) of
|
|
|
|
{NewState, StanzaId} when is_integer(StanzaId) ->
|
|
|
|
case Config#config.mam of
|
|
|
|
true ->
|
2023-08-07 12:32:36 +02:00
|
|
|
mod_mam:remove_message_from_archive({Room, Host}, Server, StanzaId),
|
2023-04-14 12:19:38 +02:00
|
|
|
NewState;
|
|
|
|
_ ->
|
|
|
|
NewState
|
|
|
|
end;
|
|
|
|
{NewState, _} ->
|
|
|
|
NewState
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
State
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
State
|
|
|
|
end.
|
|
|
|
|
2023-03-30 14:37:13 +02:00
|
|
|
-spec add_stanza_id(Packet :: message(), State :: state()) -> message().
|
|
|
|
add_stanza_id(Packet, #state{jid = JID}) ->
|
|
|
|
{AddId, NewPacket} =
|
|
|
|
case xmpp:get_meta(Packet, stanza_id, false) of
|
|
|
|
false ->
|
|
|
|
GenID = erlang:system_time(microsecond),
|
|
|
|
{true, xmpp:put_meta(Packet, stanza_id, GenID)};
|
|
|
|
_ ->
|
2023-03-30 14:53:51 +02:00
|
|
|
StanzaIds = xmpp:get_subtags(Packet, #stanza_id{by = #jid{}}),
|
2023-03-30 14:37:13 +02:00
|
|
|
HasOurStanzaId = lists:any(
|
|
|
|
fun(#stanza_id{by = JID2}) when JID == JID2 -> true;
|
|
|
|
(_) -> false
|
|
|
|
end, StanzaIds),
|
|
|
|
{not HasOurStanzaId, Packet}
|
|
|
|
end,
|
|
|
|
if
|
|
|
|
AddId ->
|
|
|
|
ID = xmpp:get_meta(NewPacket, stanza_id),
|
|
|
|
IDs = integer_to_binary(ID),
|
|
|
|
xmpp:append_subtags(NewPacket, [#stanza_id{by = JID, id = IDs}]);
|
|
|
|
true ->
|
|
|
|
Packet
|
|
|
|
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) ->
|
2016-10-07 09:31:03 +02:00
|
|
|
Action = lists:foldl(
|
|
|
|
fun(_, {error, _} = Err) ->
|
|
|
|
Err;
|
2017-01-19 15:26:08 +01:00
|
|
|
(_, {ok, _} = Result) ->
|
|
|
|
Result;
|
|
|
|
(#muc_user{invites = [_|_] = Invites}, _) ->
|
|
|
|
case check_invitation(From, Invites, Lang, StateData) of
|
|
|
|
ok ->
|
|
|
|
{ok, Invites};
|
|
|
|
{error, _} = Err ->
|
|
|
|
Err
|
|
|
|
end;
|
2016-10-07 09:31:03 +02:00
|
|
|
(#xdata{type = submit, fields = Fs}, _) ->
|
|
|
|
try {ok, muc_request:decode(Fs)}
|
|
|
|
catch _:{muc_request, Why} ->
|
|
|
|
Txt = muc_request:format_error(Why),
|
|
|
|
{error, xmpp:err_bad_request(Txt, Lang)}
|
|
|
|
end;
|
|
|
|
(_, Acc) ->
|
|
|
|
Acc
|
|
|
|
end, ok, xmpp:get_els(Pkt)),
|
|
|
|
case Action of
|
2017-01-19 15:26:08 +01:00
|
|
|
{ok, [#muc_invite{}|_] = Invitations} ->
|
|
|
|
lists:foldl(
|
|
|
|
fun(Invitation, AccState) ->
|
2018-09-21 16:37:52 +02:00
|
|
|
process_invitation(From, Pkt, Invitation, Lang, AccState)
|
2017-01-19 15:26:08 +01:00
|
|
|
end, StateData, Invitations);
|
2016-10-07 09:31:03 +02:00
|
|
|
{ok, [{role, participant}]} ->
|
|
|
|
process_voice_request(From, Pkt, StateData);
|
|
|
|
{ok, VoiceApproval} ->
|
|
|
|
process_voice_approval(From, Pkt, VoiceApproval, StateData);
|
|
|
|
{error, Err} ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Pkt, Err),
|
2016-10-07 09:31:03 +02:00
|
|
|
StateData;
|
|
|
|
ok ->
|
|
|
|
StateData
|
|
|
|
end.
|
|
|
|
|
2018-09-21 16:37:52 +02:00
|
|
|
-spec process_invitation(jid(), message(), muc_invite(), binary(), state()) -> state().
|
|
|
|
process_invitation(From, Pkt, Invitation, Lang, StateData) ->
|
|
|
|
IJID = route_invitation(From, Pkt, Invitation, Lang, StateData),
|
2017-01-19 15:26:08 +01:00
|
|
|
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;
|
|
|
|
_ ->
|
2016-10-07 09:31:03 +02:00
|
|
|
StateData
|
2017-01-19 15:26:08 +01:00
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
StateData
|
2016-10-07 09:31:03 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
-spec process_voice_request(jid(), message(), state()) -> state().
|
|
|
|
process_voice_request(From, Pkt, StateData) ->
|
|
|
|
Lang = xmpp:get_lang(Pkt),
|
|
|
|
case (StateData#state.config)#config.allow_voice_requests of
|
|
|
|
true ->
|
|
|
|
MinInterval = (StateData#state.config)#config.voice_request_min_interval,
|
|
|
|
BareFrom = jid:remove_resource(jid:tolower(From)),
|
2019-02-27 09:56:20 +01:00
|
|
|
NowPriority = -erlang:system_time(microsecond),
|
2016-10-07 09:31:03 +02:00
|
|
|
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, _, _} ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Please, wait for a while before sending "
|
|
|
|
"new voice request"),
|
2016-10-17 12:37:23 +02:00
|
|
|
Err = xmpp:err_resource_constraint(ErrText, Lang),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Pkt, Err),
|
2016-10-07 09:31:03 +02:00
|
|
|
StateData#state{last_voice_request_time = Times}
|
2016-07-25 12:50:30 +02:00
|
|
|
end;
|
2016-10-07 09:31:03 +02:00
|
|
|
false ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Voice requests are disabled in this conference"),
|
2016-10-07 09:31:03 +02:00
|
|
|
Err = xmpp:err_forbidden(ErrText, Lang),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Pkt, Err),
|
2016-10-07 09:31:03 +02:00
|
|
|
StateData
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec process_voice_approval(jid(), message(), [muc_request:property()], state()) -> state().
|
|
|
|
process_voice_approval(From, Pkt, VoiceApproval, StateData) ->
|
|
|
|
Lang = xmpp:get_lang(Pkt),
|
|
|
|
case is_moderator(From, StateData) of
|
|
|
|
true ->
|
|
|
|
case lists:keyfind(jid, 1, VoiceApproval) of
|
|
|
|
{_, TargetJid} ->
|
|
|
|
Allow = proplists:get_bool(request_allow, VoiceApproval),
|
|
|
|
case is_visitor(TargetJid, StateData) of
|
|
|
|
true when Allow ->
|
|
|
|
Reason = <<>>,
|
|
|
|
NSD = set_role(TargetJid, participant, StateData),
|
|
|
|
catch send_new_presence(
|
|
|
|
TargetJid, Reason, NSD, StateData),
|
|
|
|
NSD;
|
|
|
|
_ ->
|
|
|
|
StateData
|
2016-07-25 12:50:30 +02:00
|
|
|
end;
|
2016-10-07 09:31:03 +02:00
|
|
|
false ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Failed to extract JID from your voice "
|
|
|
|
"request approval"),
|
2016-10-07 09:31:03 +02:00
|
|
|
Err = xmpp:err_bad_request(ErrText, Lang),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Pkt, Err),
|
2016-07-25 12:50:30 +02:00
|
|
|
StateData
|
|
|
|
end;
|
2016-10-07 09:31:03 +02:00
|
|
|
false ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Only moderators can approve voice requests"),
|
2016-10-07 09:31:03 +02:00
|
|
|
Err = xmpp:err_not_allowed(ErrText, Lang),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Pkt, Err),
|
2016-07-25 12:50:30 +02:00
|
|
|
StateData
|
|
|
|
end.
|
|
|
|
|
2018-11-29 10:16:12 +01:00
|
|
|
-spec direct_iq_type(iq()) -> vcard | ping | request | response | stanza_error().
|
|
|
|
direct_iq_type(#iq{type = T, sub_els = SubEls, lang = Lang}) when T == get; T == set ->
|
|
|
|
case SubEls of
|
|
|
|
[El] ->
|
|
|
|
case xmpp:get_ns(El) of
|
|
|
|
?NS_VCARD when T == get -> vcard;
|
|
|
|
?NS_PING when T == get -> ping;
|
|
|
|
_ -> request
|
|
|
|
end;
|
|
|
|
[] ->
|
|
|
|
xmpp:err_bad_request(?T("No child elements found"), Lang);
|
|
|
|
[_|_] ->
|
|
|
|
xmpp:err_bad_request(?T("Too many child elements"), Lang)
|
|
|
|
end;
|
|
|
|
direct_iq_type(#iq{}) ->
|
|
|
|
response.
|
2017-11-10 16:02:22 +01:00
|
|
|
|
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.
|
|
|
|
|
2023-08-08 13:28:02 +02:00
|
|
|
-spec is_user_allowed_private_message(jid(), state()) -> boolean().
|
|
|
|
is_user_allowed_private_message(JID, StateData) ->
|
|
|
|
case {(StateData#state.config)#config.allowpm,
|
|
|
|
get_role(JID, StateData)} of
|
|
|
|
{anyone, _} ->
|
|
|
|
true;
|
|
|
|
{participants, moderator} ->
|
|
|
|
true;
|
|
|
|
{participants, participant} ->
|
|
|
|
true;
|
|
|
|
{moderators, moderator} ->
|
|
|
|
true;
|
|
|
|
{none, _} ->
|
|
|
|
false;
|
|
|
|
{_, _} ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2008-06-13 20:55:26 +02:00
|
|
|
%% @doc Get information of this participant, or default values.
|
|
|
|
%% If the JID is not a participant, return values for a service message.
|
2016-11-12 11:27:15 +01:00
|
|
|
-spec get_participant_data(jid(), state()) -> {binary(), role()}.
|
2008-06-13 20:55:26 +02:00
|
|
|
get_participant_data(From, StateData) ->
|
2018-11-15 12:13:45 +01:00
|
|
|
try maps:get(jid:tolower(From), StateData#state.users) of
|
|
|
|
#user{nick = FromNick, role = Role} ->
|
|
|
|
{FromNick, Role}
|
2018-11-15 13:07:58 +01:00
|
|
|
catch _:{badkey, _} ->
|
2021-09-13 07:15:11 +02:00
|
|
|
try muc_subscribers_get(jid:tolower(jid:remove_resource(From)),
|
|
|
|
StateData#state.muc_subscribers) of
|
2018-11-15 12:13:45 +01:00
|
|
|
#subscriber{nick = FromNick} ->
|
|
|
|
{FromNick, none}
|
2018-11-15 13:07:58 +01:00
|
|
|
catch _:{badkey, _} ->
|
2021-01-08 20:30:15 +01:00
|
|
|
{From#jid.luser, moderator}
|
2016-09-08 15:39:34 +02:00
|
|
|
end
|
2008-06-13 20:55:26 +02:00
|
|
|
end.
|
|
|
|
|
2017-02-24 19:25:25 +01:00
|
|
|
-spec process_presence(binary(), presence(), state()) -> fsm_transition().
|
|
|
|
process_presence(Nick, #presence{from = From, 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,
|
2017-02-24 19:25:25 +01:00
|
|
|
[StateData, Nick]) of
|
2015-08-04 20:13:00 +02:00
|
|
|
drop ->
|
|
|
|
{next_state, normal_state, StateData};
|
2016-07-25 12:50:30 +02:00
|
|
|
#presence{} = Packet ->
|
|
|
|
close_room_if_temporary_and_empty(
|
2017-02-24 19:25:25 +01:00
|
|
|
do_process_presence(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
|
|
|
|
2017-02-24 19:25:25 +01:00
|
|
|
-spec do_process_presence(binary(), presence(), state()) -> state().
|
|
|
|
do_process_presence(Nick, #presence{from = From, type = available, lang = Lang} = Packet,
|
2016-07-25 12:50:30 +02:00
|
|
|
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}} ->
|
2017-12-09 21:13:20 +01:00
|
|
|
Packet1 = Packet#presence{sub_els = [#muc{}]},
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Visitors are not allowed to change their "
|
|
|
|
"nicknames in this room"),
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_not_allowed(ErrText, Lang),
|
2017-12-09 21:13:20 +01:00
|
|
|
ejabberd_router:route_error(Packet1, Err),
|
2016-07-25 12:50:30 +02:00
|
|
|
StateData;
|
|
|
|
{true, _, _} ->
|
2017-12-09 21:13:20 +01:00
|
|
|
Packet1 = Packet#presence{sub_els = [#muc{}]},
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("That nickname is already in use by another "
|
|
|
|
"occupant"),
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_conflict(ErrText, Lang),
|
2017-12-09 21:13:20 +01:00
|
|
|
ejabberd_router:route_error(Packet1, Err),
|
2016-07-25 12:50:30 +02:00
|
|
|
StateData;
|
|
|
|
{_, false, _} ->
|
2017-12-09 21:13:20 +01:00
|
|
|
Packet1 = Packet#presence{sub_els = [#muc{}]},
|
2019-06-13 18:42:02 +02:00
|
|
|
Err = case Nick of
|
|
|
|
<<>> ->
|
2019-06-22 16:08:45 +02:00
|
|
|
xmpp:err_jid_malformed(?T("Nickname can't be empty"),
|
2019-06-13 18:42:02 +02:00
|
|
|
Lang);
|
|
|
|
_ ->
|
2019-06-22 16:08:45 +02:00
|
|
|
xmpp:err_conflict(?T("That nickname is registered"
|
|
|
|
" by another person"), Lang)
|
2019-06-13 18:42:02 +02:00
|
|
|
end,
|
2017-12-09 21:13:20 +01:00
|
|
|
ejabberd_router:route_error(Packet1, Err),
|
2016-07-25 12:50:30 +02:00
|
|
|
StateData;
|
|
|
|
_ ->
|
2019-06-13 18:42:02 +02:00
|
|
|
change_nick(From, Nick, StateData)
|
2016-07-25 12:50:30 +02:00
|
|
|
end;
|
2016-11-12 11:27:15 +01:00
|
|
|
false ->
|
|
|
|
Stanza = maybe_strip_status_from_presence(
|
|
|
|
From, Packet, StateData),
|
|
|
|
NewState = add_user_presence(From, Stanza,
|
|
|
|
StateData),
|
2017-10-09 16:59:22 +02:00
|
|
|
case xmpp:has_subtag(Packet, #muc{}) of
|
|
|
|
true ->
|
|
|
|
send_initial_presences_and_messages(
|
|
|
|
From, Nick, Packet, NewState, StateData);
|
|
|
|
false ->
|
|
|
|
send_new_presence(From, NewState, StateData)
|
|
|
|
end,
|
2016-11-12 11:27:15 +01:00
|
|
|
NewState
|
2016-07-25 12:50:30 +02:00
|
|
|
end
|
|
|
|
end;
|
2017-02-24 19:25:25 +01:00
|
|
|
do_process_presence(Nick, #presence{from = From, type = unavailable} = Packet,
|
2016-07-25 12:50:30 +02:00
|
|
|
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),
|
2018-11-15 12:13:45 +01:00
|
|
|
case maps:get(Nick, StateData#state.nicks, []) of
|
|
|
|
[_, _ | _] ->
|
2017-09-28 10:56:46 +02:00
|
|
|
Aff = get_affiliation(From, StateData),
|
|
|
|
Item = #muc_item{affiliation = Aff, role = none, jid = From},
|
|
|
|
Pres = xmpp:set_subtag(
|
|
|
|
Packet, #muc_user{items = [Item],
|
|
|
|
status_codes = [110]}),
|
|
|
|
send_wrapped(jid:replace_resource(StateData#state.jid, Nick),
|
|
|
|
From, Pres, ?NS_MUCSUB_NODES_PRESENCE, StateData);
|
|
|
|
_ ->
|
|
|
|
send_new_presence(From, NewState, StateData)
|
2016-07-25 12:50:30 +02:00
|
|
|
end,
|
|
|
|
Reason = xmpp:get_text(NewPacket#presence.status),
|
2016-11-12 11:27:15 +01:00
|
|
|
remove_online_user(From, NewState, Reason);
|
2017-02-24 19:25:25 +01:00
|
|
|
do_process_presence(_Nick, #presence{from = From, type = error, lang = Lang} = Packet,
|
2016-07-25 12:50:30 +02:00
|
|
|
StateData) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrorText = ?T("It is not allowed to send error messages to the"
|
2020-01-22 12:52:30 +01:00
|
|
|
" room. The participant (~s) has sent an error "
|
|
|
|
"message (~s) and got kicked from the room"),
|
2016-07-25 12:50:30 +02:00
|
|
|
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 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
|
2018-11-15 12:13:45 +01:00
|
|
|
andalso maps:size(StateData1#state.users) == 0
|
2021-09-13 07:15:11 +02:00
|
|
|
andalso muc_subscribers_size(StateData1#state.muc_subscribers) == 0 of
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Destroyed MUC room ~ts because it's temporary "
|
2013-03-14 10:33:02 +01:00
|
|
|
"and empty",
|
2017-02-26 08:07:12 +01:00
|
|
|
[jid:encode(StateData1#state.jid)]),
|
2014-05-05 13:50:52 +02:00
|
|
|
add_to_log(room_existence, destroyed, StateData1),
|
2019-07-16 13:57:48 +02:00
|
|
|
forget_room(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
|
|
|
|
2019-06-27 14:22:27 +02:00
|
|
|
-spec get_users_and_subscribers(state()) -> users().
|
2016-09-07 09:33:37 +02:00
|
|
|
get_users_and_subscribers(StateData) ->
|
2021-09-13 07:15:11 +02:00
|
|
|
get_users_and_subscribers_aux(
|
|
|
|
StateData#state.muc_subscribers#muc_subscribers.subscribers,
|
|
|
|
StateData).
|
|
|
|
|
|
|
|
-spec get_users_and_subscribers_with_node(binary(), state()) -> users().
|
|
|
|
get_users_and_subscribers_with_node(Node, StateData) ->
|
|
|
|
get_users_and_subscribers_aux(
|
|
|
|
muc_subscribers_get_by_node(Node, StateData#state.muc_subscribers),
|
|
|
|
StateData).
|
|
|
|
|
|
|
|
get_users_and_subscribers_aux(Subscribers, StateData) ->
|
2018-11-15 12:13:45 +01:00
|
|
|
OnlineSubscribers = maps:fold(
|
2016-09-07 09:33:37 +02:00
|
|
|
fun(LJID, _, Acc) ->
|
|
|
|
LBareJID = jid:remove_resource(LJID),
|
|
|
|
case is_subscriber(LBareJID, StateData) of
|
|
|
|
true ->
|
|
|
|
?SETS:add_element(LBareJID, Acc);
|
|
|
|
false ->
|
|
|
|
Acc
|
|
|
|
end
|
|
|
|
end, ?SETS:new(), StateData#state.users),
|
2018-11-15 12:13:45 +01:00
|
|
|
maps:fold(
|
2016-09-07 09:33:37 +02:00
|
|
|
fun(LBareJID, #subscriber{nick = Nick}, Acc) ->
|
|
|
|
case ?SETS:is_element(LBareJID, OnlineSubscribers) of
|
|
|
|
false ->
|
2018-11-15 12:13:45 +01:00
|
|
|
maps:put(LBareJID,
|
|
|
|
#user{jid = jid:make(LBareJID),
|
|
|
|
nick = Nick,
|
|
|
|
role = none,
|
|
|
|
last_presence = undefined},
|
|
|
|
Acc);
|
2016-09-07 09:33:37 +02:00
|
|
|
true ->
|
|
|
|
Acc
|
|
|
|
end
|
2021-09-13 07:15:11 +02:00
|
|
|
end, StateData#state.users, Subscribers).
|
2016-09-07 09:33:37 +02: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),
|
2018-11-15 12:13:45 +01:00
|
|
|
maps: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) ->
|
2016-09-07 09:33:37 +02:00
|
|
|
LJID = jid:tolower(jid:remove_resource(JID)),
|
2021-09-13 07:15:11 +02:00
|
|
|
muc_subscribers_is_key(LJID, StateData#state.muc_subscribers).
|
2016-06-26 08:08:37 +02:00
|
|
|
|
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
|
|
|
|
2021-10-29 04:12:26 +02:00
|
|
|
%% Check if the user is an admin or owner.
|
|
|
|
-spec is_admin(jid(), state()) -> boolean().
|
|
|
|
is_admin(JID, StateData) ->
|
|
|
|
FAffiliation = get_affiliation(JID, StateData),
|
|
|
|
FAffiliation == admin orelse FAffiliation == owner.
|
|
|
|
|
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()}.
|
2016-07-31 07:51:47 +02:00
|
|
|
decide_fate_message(#message{type = error} = Msg,
|
2016-07-25 12:50:30 +02:00
|
|
|
From, StateData) ->
|
2016-07-31 07:51:47 +02:00
|
|
|
Err = xmpp:get_error(Msg),
|
2016-07-25 12:50:30 +02:00
|
|
|
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 ->
|
2016-10-17 12:37:23 +02:00
|
|
|
Reason = str:format("This participant is considered a ghost "
|
2020-01-22 12:52:30 +01:00
|
|
|
"and is expulsed: ~s",
|
2017-02-26 08:07:12 +01:00
|
|
|
[jid:encode(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-09-08 16:08:48 +02:00
|
|
|
-spec check_error_kick(stanza_error()) -> boolean().
|
|
|
|
check_error_kick(#stanza_error{reason = Reason}) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
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-09-08 16:08:48 +02:00
|
|
|
-spec get_error_condition(stanza_error()) -> string().
|
|
|
|
get_error_condition(#stanza_error{reason = Reason}) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
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
|
|
|
|
2017-02-18 07:36:27 +01:00
|
|
|
-spec get_error_text(stanza_error()) -> binary().
|
|
|
|
get_error_text(#stanza_error{text = Txt}) ->
|
2017-10-13 19:48:21 +02:00
|
|
|
xmpp:get_text(Txt).
|
2016-08-09 12:36:43 +02: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) ->
|
2018-11-15 12:13:45 +01:00
|
|
|
#user{nick = FromNick} = maps:get(jid:tolower(From), StateData#state.users),
|
2016-07-25 12:50:30 +02:00
|
|
|
Condition = get_error_condition(xmpp:get_error(Packet)),
|
2020-11-09 12:20:23 +01:00
|
|
|
Reason2 = unicode:characters_to_list(Reason1),
|
|
|
|
str:format(Reason2, [FromNick, Condition]).
|
2015-12-23 19:34:23 +01:00
|
|
|
|
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) ->
|
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),
|
2017-09-26 18:01:54 +02:00
|
|
|
LJID = jid:tolower(From),
|
2018-11-15 12:13:45 +01:00
|
|
|
#user{nick = Nick} = maps:get(LJID, StateData#state.users),
|
|
|
|
case maps:get(Nick, StateData#state.nicks, []) of
|
|
|
|
[_, _ | _] ->
|
2017-10-13 08:17:22 +02:00
|
|
|
Aff = get_affiliation(From, StateData),
|
|
|
|
Item = #muc_item{affiliation = Aff, role = none, jid = From},
|
|
|
|
Pres = xmpp:set_subtag(
|
|
|
|
Packet, #muc_user{items = [Item],
|
|
|
|
status_codes = [110]}),
|
|
|
|
send_wrapped(jid:replace_resource(StateData#state.jid, Nick),
|
|
|
|
From, Pres, ?NS_MUCSUB_NODES_PRESENCE, StateData);
|
|
|
|
_ ->
|
|
|
|
send_new_presence(From, NewState, StateData)
|
2017-09-26 18:01:54 +02:00
|
|
|
end,
|
2016-09-07 09:33:37 +02:00
|
|
|
remove_online_user(From, NewState).
|
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().
|
2018-04-23 10:35:43 +02:00
|
|
|
set_affiliation(JID, Affiliation,
|
|
|
|
#state{config = #config{persistent = false}} = StateData,
|
|
|
|
Reason) ->
|
|
|
|
set_affiliation_fallback(JID, Affiliation, StateData, Reason);
|
2011-09-05 12:53:01 +02:00
|
|
|
set_affiliation(JID, Affiliation, StateData, Reason) ->
|
2018-04-23 10:35:43 +02:00
|
|
|
ServerHost = StateData#state.server_host,
|
|
|
|
Room = StateData#state.room,
|
|
|
|
Host = StateData#state.host,
|
|
|
|
Mod = gen_mod:db_mod(ServerHost, mod_muc),
|
|
|
|
case Mod:set_affiliation(ServerHost, Room, Host, JID, Affiliation, Reason) of
|
|
|
|
ok ->
|
|
|
|
StateData;
|
|
|
|
{error, _} ->
|
|
|
|
set_affiliation_fallback(JID, Affiliation, StateData, Reason)
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec set_affiliation_fallback(jid(), affiliation(), state(), binary()) -> state().
|
|
|
|
set_affiliation_fallback(JID, Affiliation, StateData, Reason) ->
|
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
|
2018-04-23 10:35:43 +02:00
|
|
|
none ->
|
2018-11-15 12:13:45 +01:00
|
|
|
maps:remove(LJID, StateData#state.affiliations);
|
2018-04-23 10:35:43 +02:00
|
|
|
_ ->
|
2018-11-15 12:13:45 +01:00
|
|
|
maps:put(LJID, {Affiliation, Reason},
|
|
|
|
StateData#state.affiliations)
|
2005-05-04 01:07:14 +02:00
|
|
|
end,
|
|
|
|
StateData#state{affiliations = Affiliations}.
|
|
|
|
|
2019-06-27 14:22:27 +02:00
|
|
|
-spec set_affiliations(affiliations(), state()) -> state().
|
2018-04-23 10:35:43 +02:00
|
|
|
set_affiliations(Affiliations,
|
|
|
|
#state{config = #config{persistent = false}} = StateData) ->
|
|
|
|
set_affiliations_fallback(Affiliations, StateData);
|
|
|
|
set_affiliations(Affiliations, StateData) ->
|
|
|
|
Room = StateData#state.room,
|
|
|
|
Host = StateData#state.host,
|
|
|
|
ServerHost = StateData#state.server_host,
|
|
|
|
Mod = gen_mod:db_mod(ServerHost, mod_muc),
|
|
|
|
case Mod:set_affiliations(ServerHost, Room, Host, Affiliations) of
|
|
|
|
ok ->
|
|
|
|
StateData;
|
|
|
|
{error, _} ->
|
|
|
|
set_affiliations_fallback(Affiliations, StateData)
|
|
|
|
end.
|
|
|
|
|
2019-06-27 14:22:27 +02:00
|
|
|
-spec set_affiliations_fallback(affiliations(), state()) -> state().
|
2018-04-23 10:35:43 +02:00
|
|
|
set_affiliations_fallback(Affiliations, StateData) ->
|
|
|
|
StateData#state{affiliations = Affiliations}.
|
|
|
|
|
2018-04-24 11:07:10 +02:00
|
|
|
-spec get_affiliation(ljid() | jid(), state()) -> affiliation().
|
|
|
|
get_affiliation(#jid{} = JID, StateData) ->
|
2018-04-23 10:35:43 +02:00
|
|
|
case get_service_affiliation(JID, StateData) of
|
2023-05-24 11:26:48 +02:00
|
|
|
owner ->
|
|
|
|
owner;
|
|
|
|
none ->
|
|
|
|
Aff = case do_get_affiliation(JID, StateData) of
|
|
|
|
{Affiliation, _Reason} -> Affiliation;
|
|
|
|
Affiliation -> Affiliation
|
|
|
|
end,
|
|
|
|
case {Aff, (StateData#state.config)#config.members_only} of
|
|
|
|
% Subscribers should be have members affiliation in this case
|
|
|
|
{none, true} ->
|
|
|
|
case is_subscriber(JID, StateData) of
|
|
|
|
true -> member;
|
|
|
|
_ -> none
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Aff
|
|
|
|
end
|
2018-04-24 11:07:10 +02:00
|
|
|
end;
|
|
|
|
get_affiliation(LJID, StateData) ->
|
|
|
|
get_affiliation(jid:make(LJID), StateData).
|
2018-04-23 10:35:43 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec do_get_affiliation(jid(), state()) -> affiliation() | {affiliation(), binary()}.
|
2018-04-23 10:35:43 +02:00
|
|
|
do_get_affiliation(JID, #state{config = #config{persistent = false}} = StateData) ->
|
|
|
|
do_get_affiliation_fallback(JID, StateData);
|
|
|
|
do_get_affiliation(JID, StateData) ->
|
|
|
|
Room = StateData#state.room,
|
|
|
|
Host = StateData#state.host,
|
|
|
|
LServer = JID#jid.lserver,
|
|
|
|
LUser = JID#jid.luser,
|
|
|
|
ServerHost = StateData#state.server_host,
|
|
|
|
Mod = gen_mod:db_mod(ServerHost, mod_muc),
|
|
|
|
case Mod:get_affiliation(ServerHost, Room, Host, LUser, LServer) of
|
|
|
|
{error, _} ->
|
|
|
|
do_get_affiliation_fallback(JID, StateData);
|
|
|
|
{ok, Affiliation} ->
|
|
|
|
Affiliation
|
|
|
|
end.
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec do_get_affiliation_fallback(jid(), state()) -> affiliation() | {affiliation(), binary()}.
|
2018-04-23 10:35:43 +02:00
|
|
|
do_get_affiliation_fallback(JID, StateData) ->
|
|
|
|
LJID = jid:tolower(JID),
|
2018-11-15 12:13:45 +01:00
|
|
|
try maps:get(LJID, StateData#state.affiliations)
|
2018-11-15 13:07:58 +01:00
|
|
|
catch _:{badkey, _} ->
|
2018-11-15 12:13:45 +01:00
|
|
|
BareLJID = jid:remove_resource(LJID),
|
|
|
|
try maps:get(BareLJID, StateData#state.affiliations)
|
2018-11-15 13:07:58 +01:00
|
|
|
catch _:{badkey, _} ->
|
2018-11-15 12:13:45 +01:00
|
|
|
DomainLJID = setelement(1, LJID, <<"">>),
|
|
|
|
try maps:get(DomainLJID, StateData#state.affiliations)
|
2018-11-15 13:07:58 +01:00
|
|
|
catch _:{badkey, _} ->
|
2018-11-15 12:13:45 +01:00
|
|
|
DomainBareLJID = jid:remove_resource(DomainLJID),
|
|
|
|
try maps:get(DomainBareLJID, StateData#state.affiliations)
|
2018-11-15 13:07:58 +01:00
|
|
|
catch _:{badkey, _} -> none
|
2018-04-23 10:35:43 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2019-06-27 14:22:27 +02:00
|
|
|
-spec get_affiliations(state()) -> affiliations().
|
2018-04-23 10:35:43 +02:00
|
|
|
get_affiliations(#state{config = #config{persistent = false}} = StateData) ->
|
2021-12-30 21:17:11 +01:00
|
|
|
get_affiliations_fallback(StateData);
|
2018-04-23 10:35:43 +02:00
|
|
|
get_affiliations(StateData) ->
|
|
|
|
Room = StateData#state.room,
|
|
|
|
Host = StateData#state.host,
|
|
|
|
ServerHost = StateData#state.server_host,
|
|
|
|
Mod = gen_mod:db_mod(ServerHost, mod_muc),
|
|
|
|
case Mod:get_affiliations(ServerHost, Room, Host) of
|
|
|
|
{error, _} ->
|
2021-12-30 21:17:11 +01:00
|
|
|
get_affiliations_fallback(StateData);
|
2018-04-23 10:35:43 +02:00
|
|
|
{ok, Affiliations} ->
|
|
|
|
Affiliations
|
2003-03-23 21:08:44 +01:00
|
|
|
end.
|
|
|
|
|
2021-12-30 21:17:11 +01:00
|
|
|
-spec get_affiliations_fallback(state()) -> affiliations().
|
|
|
|
get_affiliations_fallback(StateData) ->
|
2018-04-23 10:35:43 +02:00
|
|
|
StateData#state.affiliations.
|
|
|
|
|
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,
|
2019-01-02 17:35:01 +01:00
|
|
|
_AccessPersistent, _AccessMam} =
|
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, <<"">>} ->
|
2018-11-15 12:13:45 +01:00
|
|
|
maps: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
|
|
|
_ ->
|
2018-11-15 12:13:45 +01:00
|
|
|
case maps:is_key(LJID, StateData#state.users) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true -> [LJID];
|
|
|
|
_ -> []
|
|
|
|
end
|
|
|
|
end,
|
2018-11-15 12:13:45 +01:00
|
|
|
{Users, Nicks} =
|
|
|
|
case Role of
|
|
|
|
none ->
|
|
|
|
lists:foldl(
|
|
|
|
fun (J, {Us, Ns}) ->
|
|
|
|
NewNs = try maps:get(J, Us) of
|
|
|
|
#user{nick = Nick} ->
|
|
|
|
maps:remove(Nick, Ns)
|
2018-11-15 13:07:58 +01:00
|
|
|
catch _:{badkey, _} ->
|
2018-11-15 12:13:45 +01:00
|
|
|
Ns
|
|
|
|
end,
|
|
|
|
{maps:remove(J, Us), NewNs}
|
|
|
|
end,
|
|
|
|
{StateData#state.users, StateData#state.nicks}, LJIDs);
|
|
|
|
_ ->
|
|
|
|
{lists:foldl(
|
|
|
|
fun (J, Us) ->
|
|
|
|
User = maps:get(J, Us),
|
|
|
|
if User#user.last_presence == undefined ->
|
|
|
|
Us;
|
|
|
|
true ->
|
|
|
|
maps:put(J, User#user{role = Role}, Us)
|
|
|
|
end
|
|
|
|
end, StateData#state.users, LJIDs),
|
|
|
|
StateData#state.nicks}
|
|
|
|
end,
|
2023-03-19 06:40:19 +01:00
|
|
|
Affiliation = get_affiliation(JID, StateData),
|
2022-09-07 16:44:50 +02:00
|
|
|
Roles = case Role of
|
|
|
|
%% Don't persist 'none' role: if someone is kicked, they will
|
2023-03-19 06:40:19 +01:00
|
|
|
%% maintain the same role they had *before* they were kicked,
|
|
|
|
%% unless they were banned
|
|
|
|
none when Affiliation /= outcast ->
|
2023-05-24 12:15:20 +02:00
|
|
|
maps:remove(jid:remove_resource(LJID), StateData#state.roles);
|
2022-09-07 16:44:50 +02:00
|
|
|
NewRole ->
|
|
|
|
maps:put(jid:remove_resource(LJID),
|
|
|
|
NewRole,
|
|
|
|
StateData#state.roles)
|
|
|
|
end,
|
2021-10-01 12:11:51 +02:00
|
|
|
StateData#state{users = Users, nicks = Nicks, roles = Roles}.
|
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),
|
2018-11-15 12:13:45 +01:00
|
|
|
try maps:get(LJID, StateData#state.users) of
|
|
|
|
#user{role = Role} -> Role
|
2018-11-15 13:07:58 +01:00
|
|
|
catch _:{badkey, _} -> 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) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_muc_opt:max_users(StateData#state.server_host).
|
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) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_muc_opt:max_users_admin_threshold(StateData#state.server_host).
|
2007-08-29 19:54:45 +02:00
|
|
|
|
2019-06-28 21:16:29 +02:00
|
|
|
-spec room_queue_new(binary(), ejabberd_shaper:shaper(), _) -> p1_queue:queue({message | presence, jid()}) | undefined.
|
2017-03-10 13:12:43 +01:00
|
|
|
room_queue_new(ServerHost, Shaper, QueueType) ->
|
|
|
|
HaveRoomShaper = Shaper /= none,
|
2019-06-14 11:33:26 +02:00
|
|
|
HaveMessageShaper = mod_muc_opt:user_message_shaper(ServerHost) /= none,
|
|
|
|
HavePresenceShaper = mod_muc_opt:user_presence_shaper(ServerHost) /= none,
|
|
|
|
HaveMinMessageInterval = mod_muc_opt:min_message_interval(ServerHost) /= 0,
|
|
|
|
HaveMinPresenceInterval = mod_muc_opt:min_presence_interval(ServerHost) /= 0,
|
2017-03-10 13:12:43 +01:00
|
|
|
if HaveRoomShaper or HaveMessageShaper or HavePresenceShaper
|
|
|
|
or HaveMinMessageInterval or HaveMinPresenceInterval ->
|
|
|
|
p1_queue:new(QueueType);
|
|
|
|
true ->
|
|
|
|
undefined
|
|
|
|
end.
|
|
|
|
|
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 =
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd_shaper:new(mod_muc_opt:user_message_shaper(StateData#state.server_host)),
|
2013-03-14 10:33:02 +01:00
|
|
|
PresenceShaper =
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd_shaper:new(mod_muc_opt:user_presence_shaper(StateData#state.server_host)),
|
2013-03-14 10:33:02 +01:00
|
|
|
#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 =
|
2019-06-14 11:33:26 +02:00
|
|
|
trunc(mod_muc_opt:min_message_interval(StateData#state.server_host) * 1000),
|
2008-11-28 17:06:39 +01:00
|
|
|
MinPresenceInterval =
|
2019-06-14 11:33:26 +02:00
|
|
|
trunc(mod_muc_opt:min_presence_interval(StateData#state.server_host) * 1000),
|
2015-11-24 16:44:13 +01:00
|
|
|
Key = jid:tolower(JID),
|
2019-02-27 09:56:20 +01:00
|
|
|
Now = erlang:system_time(microsecond),
|
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} =
|
2018-07-05 08:31:55 +02:00
|
|
|
ejabberd_shaper:update(UserActivity#activity.message_shaper,
|
2013-03-14 10:33:02 +01:00
|
|
|
100000),
|
|
|
|
{_, PresenceShaperInterval} =
|
2018-07-05 08:31:55 +02:00
|
|
|
ejabberd_shaper:update(UserActivity#activity.presence_shaper,
|
2013-03-14 10:33:02 +01:00
|
|
|
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,
|
2019-07-16 13:57:48 +02:00
|
|
|
reset_hibernate_timer(StateData1).
|
2008-11-28 17:06:39 +01:00
|
|
|
|
2017-02-18 07:36:27 +01:00
|
|
|
-spec clean_treap(treap:treap(), integer() | {1, 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) ->
|
2017-03-10 13:12:43 +01:00
|
|
|
case p1_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} =
|
2018-07-05 08:31:55 +02:00
|
|
|
ejabberd_shaper:update(StateData#state.room_shaper, Size),
|
2013-03-14 10:33:02 +01:00
|
|
|
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} =
|
2018-07-05 08:31:55 +02:00
|
|
|
ejabberd_shaper:update(StateData#state.room_shaper, Size),
|
2013-03-14 10:33:02 +01:00
|
|
|
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-09-07 09:33:37 +02:00
|
|
|
update_online_user(JID, #user{nick = Nick} = User, StateData) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LJID = jid:tolower(JID),
|
2018-11-09 13:21:35 +01:00
|
|
|
add_to_log(join, Nick, StateData),
|
2018-11-15 12:13:45 +01:00
|
|
|
Nicks1 = try maps:get(LJID, StateData#state.users) of
|
|
|
|
#user{nick = OldNick} ->
|
2016-06-26 08:08:37 +02:00
|
|
|
case lists:delete(
|
2018-11-15 12:13:45 +01:00
|
|
|
LJID, maps:get(OldNick, StateData#state.nicks)) of
|
2016-06-26 08:08:37 +02:00
|
|
|
[] ->
|
2018-11-15 12:13:45 +01:00
|
|
|
maps:remove(OldNick, StateData#state.nicks);
|
2016-06-26 08:08:37 +02:00
|
|
|
LJIDs ->
|
2018-11-15 12:13:45 +01:00
|
|
|
maps:put(OldNick, LJIDs, StateData#state.nicks)
|
|
|
|
end
|
2018-11-15 13:07:58 +01:00
|
|
|
catch _:{badkey, _} ->
|
2016-06-26 08:08:37 +02:00
|
|
|
StateData#state.nicks
|
|
|
|
end,
|
2018-11-15 12:13:45 +01:00
|
|
|
Nicks = maps:update_with(Nick,
|
|
|
|
fun (LJIDs) -> [LJID|LJIDs -- [LJID]] end,
|
|
|
|
[LJID], Nicks1),
|
|
|
|
Users = maps:update_with(LJID,
|
|
|
|
fun(U) ->
|
|
|
|
U#user{nick = Nick}
|
|
|
|
end, User, StateData#state.users),
|
2016-06-26 08:08:37 +02:00
|
|
|
NewStateData = StateData#state{users = Users, nicks = Nicks},
|
2018-11-15 12:13:45 +01:00
|
|
|
case {maps:get(LJID, StateData#state.users, error),
|
|
|
|
maps:get(LJID, NewStateData#state.users, error)} of
|
|
|
|
{#user{nick = Old}, #user{nick = New}} when Old /= New ->
|
2016-06-26 08:08:37 +02:00
|
|
|
send_nick_changing(JID, Old, NewStateData, true, true);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
NewStateData.
|
|
|
|
|
2019-07-09 14:21:17 +02:00
|
|
|
-spec set_subscriber(jid(), binary(), [binary()], state()) -> state().
|
2019-04-15 12:02:43 +02:00
|
|
|
set_subscriber(JID, Nick, Nodes,
|
|
|
|
#state{room = Room, host = Host, server_host = ServerHost} = StateData) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
BareJID = jid:remove_resource(JID),
|
2016-09-12 13:41:33 +02:00
|
|
|
LBareJID = jid:tolower(BareJID),
|
2021-09-13 07:15:11 +02:00
|
|
|
MUCSubscribers =
|
|
|
|
muc_subscribers_put(
|
|
|
|
#subscriber{jid = BareJID,
|
|
|
|
nick = Nick,
|
|
|
|
nodes = Nodes},
|
|
|
|
StateData#state.muc_subscribers),
|
|
|
|
NewStateData = StateData#state{muc_subscribers = MUCSubscribers},
|
2017-10-31 14:00:41 +01:00
|
|
|
store_room(NewStateData, [{add_subscription, BareJID, Nick, Nodes}]),
|
2021-09-13 07:15:11 +02:00
|
|
|
case not muc_subscribers_is_key(LBareJID, StateData#state.muc_subscribers) of
|
2017-10-30 12:05:18 +01:00
|
|
|
true ->
|
2022-05-09 16:34:27 +02:00
|
|
|
Packet1a = #message{
|
|
|
|
sub_els = [#ps_event{
|
|
|
|
items = #ps_items{
|
|
|
|
node = ?NS_MUCSUB_NODES_SUBSCRIBERS,
|
|
|
|
items = [#ps_item{
|
|
|
|
id = p1_rand:get_string(),
|
|
|
|
sub_els = [#muc_subscribe{jid = BareJID, nick = Nick}]}]}}]},
|
|
|
|
Packet1b = #message{
|
|
|
|
sub_els = [#ps_event{
|
|
|
|
items = #ps_items{
|
|
|
|
node = ?NS_MUCSUB_NODES_SUBSCRIBERS,
|
|
|
|
items = [#ps_item{
|
|
|
|
id = p1_rand:get_string(),
|
|
|
|
sub_els = [#muc_subscribe{nick = Nick}]}]}}]},
|
|
|
|
{Packet2a, Packet2b} = ejabberd_hooks:run_fold(muc_subscribed, ServerHost, {Packet1a, Packet1b},
|
2022-05-09 17:32:34 +02:00
|
|
|
[ServerHost, Room, Host, BareJID, StateData]),
|
2022-05-09 16:34:27 +02:00
|
|
|
send_subscriptions_change_notifications(Packet2a, Packet2b, NewStateData);
|
2017-10-30 12:05:18 +01:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
2016-09-07 09:33:37 +02:00
|
|
|
NewStateData.
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-11-12 11:27:15 +01:00
|
|
|
-spec add_online_user(jid(), binary(), role(), state()) -> state().
|
2016-09-07 09:33:37 +02:00
|
|
|
add_online_user(JID, Nick, Role, StateData) ->
|
2007-12-03 11:47:42 +01:00
|
|
|
tab_add_online_user(JID, StateData),
|
2016-09-07 09:33:37 +02:00
|
|
|
User = #user{jid = JID, nick = Nick, role = Role},
|
2019-07-16 13:57:48 +02:00
|
|
|
reset_hibernate_timer(update_online_user(JID, User, StateData)).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-11-12 11:27:15 +01:00
|
|
|
-spec remove_online_user(jid(), state()) -> state().
|
2016-09-07 09:33:37 +02:00
|
|
|
remove_online_user(JID, StateData) ->
|
|
|
|
remove_online_user(JID, StateData, <<"">>).
|
2006-03-14 05:26:15 +01:00
|
|
|
|
2016-11-12 11:27:15 +01:00
|
|
|
-spec remove_online_user(jid(), state(), binary()) -> state().
|
2016-09-07 09:33:37 +02:00
|
|
|
remove_online_user(JID, StateData, Reason) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LJID = jid:tolower(JID),
|
2018-11-15 12:13:45 +01:00
|
|
|
#user{nick = Nick} = maps:get(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),
|
2018-11-15 12:13:45 +01:00
|
|
|
Users = maps:remove(LJID, StateData#state.users),
|
|
|
|
Nicks = try maps:get(Nick, StateData#state.nicks) of
|
|
|
|
[LJID] ->
|
|
|
|
maps:remove(Nick, StateData#state.nicks);
|
|
|
|
U ->
|
|
|
|
maps:put(Nick, U -- [LJID], StateData#state.nicks)
|
2018-11-15 13:07:58 +01:00
|
|
|
catch _:{badkey, _} ->
|
2018-11-15 12:13:45 +01:00
|
|
|
StateData#state.nicks
|
2009-08-21 15:22:18 +02:00
|
|
|
end,
|
2019-07-16 13:57:48 +02:00
|
|
|
reset_hibernate_timer(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;
|
2021-10-29 04:12:26 +02:00
|
|
|
_ -> XMLNS /= ?NS_HATS
|
2016-07-25 12:50:30 +02:00
|
|
|
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),
|
2018-11-15 12:13:45 +01:00
|
|
|
Users = maps:update_with(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),
|
2018-11-15 12:13:45 +01:00
|
|
|
Users = maps:update_with(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) ->
|
2018-11-15 12:13:45 +01:00
|
|
|
Users = case maps:get(Nick, StateData#state.nicks, []) of
|
2021-09-13 07:15:11 +02:00
|
|
|
[] -> muc_subscribers_get_by_nick(
|
|
|
|
Nick, StateData#state.muc_subscribers);
|
2018-11-15 12:13:45 +01:00
|
|
|
Us -> Us
|
|
|
|
end,
|
|
|
|
[jid:make(LJID) || LJID <- Users].
|
2011-08-23 21:53:30 +02:00
|
|
|
|
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) ->
|
2018-11-15 12:13:45 +01:00
|
|
|
try maps:get(Nick, StateData#state.nicks) of
|
|
|
|
[User] -> jid:make(User);
|
|
|
|
[FirstUser | Users] ->
|
|
|
|
#user{last_presence = FirstPresence} =
|
|
|
|
maps:get(FirstUser, StateData#state.users),
|
|
|
|
{LJID, _} = lists:foldl(
|
|
|
|
fun(Compare, {HighestUser, HighestPresence}) ->
|
|
|
|
#user{last_presence = P1} =
|
|
|
|
maps:get(Compare, StateData#state.users),
|
|
|
|
case higher_presence(P1, HighestPresence) of
|
|
|
|
true -> {Compare, P1};
|
|
|
|
false -> {HighestUser, HighestPresence}
|
|
|
|
end
|
|
|
|
end, {FirstUser, FirstPresence}, Users),
|
|
|
|
jid:make(LJID)
|
2018-11-15 13:07:58 +01:00
|
|
|
catch _:{badkey, _} ->
|
2018-11-15 12:13:45 +01:00
|
|
|
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
|
|
|
|
2023-04-14 12:05:49 +02:00
|
|
|
-spec find_nick_by_jid(jid() | undefined, state()) -> binary().
|
|
|
|
find_nick_by_jid(undefined, _StateData) ->
|
|
|
|
<<>>;
|
2016-07-25 12:50:30 +02:00
|
|
|
find_nick_by_jid(JID, StateData) ->
|
|
|
|
LJID = jid:tolower(JID),
|
2023-04-14 12:05:49 +02:00
|
|
|
case maps:find(LJID, StateData#state.users) of
|
|
|
|
{ok, #user{nick = Nick}} ->
|
|
|
|
Nick;
|
|
|
|
_ ->
|
|
|
|
case maps:find(LJID, (StateData#state.muc_subscribers)#muc_subscribers.subscribers) of
|
|
|
|
{ok, #subscriber{nick = Nick}} ->
|
|
|
|
Nick;
|
|
|
|
_ ->
|
|
|
|
<<>>
|
|
|
|
end
|
|
|
|
end.
|
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;
|
|
|
|
_ ->
|
2018-11-15 12:13:45 +01:00
|
|
|
#user{nick = OldNick} = maps:get(LJID, StateData#state.users),
|
2013-03-14 10:33:02 +01:00
|
|
|
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) ->
|
2016-09-12 13:41:33 +02:00
|
|
|
UserOfNick = case find_jid_by_nick(Nick, StateData) of
|
|
|
|
false ->
|
2021-09-13 07:15:11 +02:00
|
|
|
case muc_subscribers_get_by_nick(Nick, StateData#state.muc_subscribers) of
|
|
|
|
[J] -> J;
|
|
|
|
[] -> false
|
|
|
|
end;
|
2016-09-12 13:41:33 +02:00
|
|
|
J -> J
|
|
|
|
end,
|
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
|
|
|
|
2017-02-18 07:36:27 +01:00
|
|
|
-spec add_new_user(jid(), binary(), presence(), state()) -> state();
|
|
|
|
(jid(), binary(), iq(), state()) -> {error, stanza_error()} |
|
|
|
|
{ignore, state()} |
|
|
|
|
{result, muc_subscribe(), state()}.
|
2016-07-25 12:50:30 +02:00
|
|
|
add_new_user(From, Nick, Packet, StateData) ->
|
|
|
|
Lang = xmpp:get_lang(Packet),
|
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),
|
2018-11-15 12:13:45 +01:00
|
|
|
NUsers = maps:size(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),
|
2017-01-13 10:03:39 +01:00
|
|
|
NConferences = tab_count_user(From, StateData),
|
2013-03-14 10:33:02 +01:00
|
|
|
MaxConferences =
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_muc_opt:max_user_conferences(StateData#state.server_host),
|
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),
|
2020-12-10 21:59:07 +01:00
|
|
|
case {ServiceAffiliation == owner orelse
|
|
|
|
((((Affiliation == admin orelse Affiliation == owner)
|
2016-05-25 12:44:05 +02:00
|
|
|
andalso NUsers < MaxAdminUsers)
|
2013-03-14 10:33:02 +01:00
|
|
|
orelse NUsers < MaxUsers)
|
2020-12-10 21:59:07 +01:00
|
|
|
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),
|
2021-10-01 12:11:51 +02:00
|
|
|
get_occupant_initial_role(From, Affiliation, StateData)}
|
2013-03-14 10:33:02 +01:00
|
|
|
of
|
2016-04-05 12:09:44 +02:00
|
|
|
{false, _, _, _} when NUsers >= MaxUsers orelse NUsers >= MaxAdminUsers ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Too many users in this conference"),
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_resource_constraint(Txt, Lang),
|
2016-06-26 08:08:37 +02:00
|
|
|
if not IsSubscribeRequest ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err),
|
2016-06-26 08:08:37 +02:00
|
|
|
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 ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("You have joined too many conferences"),
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_resource_constraint(Txt, Lang),
|
2016-06-26 08:08:37 +02:00
|
|
|
if not IsSubscribeRequest ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err),
|
2016-06-26 08:08:37 +02:00
|
|
|
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(),
|
2016-06-26 08:08:37 +02:00
|
|
|
if not IsSubscribeRequest ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err),
|
2016-06-26 08:08:37 +02:00
|
|
|
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 ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("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
|
|
|
_ ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("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,
|
|
|
|
if not IsSubscribeRequest ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err),
|
2016-06-26 08:08:37 +02:00
|
|
|
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, _, _} ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("That nickname is already in use by another occupant"),
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_conflict(ErrText, Lang),
|
2016-06-26 08:08:37 +02:00
|
|
|
if not IsSubscribeRequest ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err),
|
2016-06-26 08:08:37 +02:00
|
|
|
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, _} ->
|
2019-06-13 18:42:02 +02:00
|
|
|
Err = case Nick of
|
|
|
|
<<>> ->
|
2019-06-22 16:08:45 +02:00
|
|
|
xmpp:err_jid_malformed(?T("Nickname can't be empty"),
|
2019-06-13 18:42:02 +02:00
|
|
|
Lang);
|
|
|
|
_ ->
|
2019-06-22 16:08:45 +02:00
|
|
|
xmpp:err_conflict(?T("That nickname is registered"
|
|
|
|
" by another person"), Lang)
|
2019-06-13 18:42:02 +02:00
|
|
|
end,
|
2016-06-26 08:08:37 +02:00
|
|
|
if not IsSubscribeRequest ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err),
|
2016-06-26 08:08:37 +02:00
|
|
|
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,
|
2016-09-07 09:33:37 +02:00
|
|
|
StateData)),
|
2017-10-09 16:59:22 +02:00
|
|
|
send_initial_presences_and_messages(
|
|
|
|
From, Nick, Packet, NewState, StateData),
|
2016-06-26 08:08:37 +02:00
|
|
|
NewState;
|
|
|
|
true ->
|
2016-09-07 09:33:37 +02:00
|
|
|
set_subscriber(From, Nick, Nodes, StateData)
|
2016-06-26 08:08:37 +02:00
|
|
|
end,
|
|
|
|
ResultState =
|
|
|
|
case NewStateData#state.just_created of
|
|
|
|
true ->
|
2019-02-27 09:56:20 +01:00
|
|
|
NewStateData#state{just_created = erlang:system_time(microsecond)};
|
2019-02-06 16:13:30 +01:00
|
|
|
_ ->
|
2018-11-15 12:13:45 +01:00
|
|
|
Robots = maps:remove(From, StateData#state.robots),
|
2016-06-26 08:08:37 +02:00
|
|
|
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;
|
2017-02-18 07:36:27 +01:00
|
|
|
need_password ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("A password is required to enter this room"),
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_not_authorized(ErrText, Lang),
|
2016-06-26 08:08:37 +02:00
|
|
|
if not IsSubscribeRequest ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err),
|
2016-06-26 08:08:37 +02:00
|
|
|
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
|
2016-07-28 14:10:41 +02:00
|
|
|
{ok, ID, Body, CaptchaEls} ->
|
2017-02-16 09:00:26 +01:00
|
|
|
MsgPkt = #message{from = RoomJID,
|
|
|
|
to = From,
|
|
|
|
id = ID, body = Body,
|
2016-07-28 14:10:41 +02:00
|
|
|
sub_els = CaptchaEls},
|
2018-11-15 12:13:45 +01:00
|
|
|
Robots = maps:put(From, {Nick, Packet},
|
|
|
|
StateData#state.robots),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route(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} ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Too many CAPTCHA requests"),
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_resource_constraint(ErrText, Lang),
|
2016-06-26 08:08:37 +02:00
|
|
|
if not IsSubscribeRequest ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err),
|
2016-06-26 08:08:37 +02:00
|
|
|
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
|
|
|
_ ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Unable to generate a CAPTCHA"),
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_internal_server_error(ErrText, Lang),
|
2016-06-26 08:08:37 +02:00
|
|
|
if not IsSubscribeRequest ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err),
|
2016-06-26 08:08:37 +02:00
|
|
|
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;
|
|
|
|
_ ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Incorrect password"),
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_not_authorized(ErrText, Lang),
|
2016-06-26 08:08:37 +02:00
|
|
|
if not IsSubscribeRequest ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err),
|
2016-06-26 08:08:37 +02:00
|
|
|
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(),
|
2017-02-18 07:36:27 +01:00
|
|
|
presence() | iq(), jid(), state()) ->
|
|
|
|
boolean() | need_password | captcha_required.
|
2016-07-25 12:50:30 +02:00
|
|
|
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
|
2017-02-18 07:36:27 +01:00
|
|
|
false -> need_password;
|
2013-03-14 10:33:02 +01:00
|
|
|
_ ->
|
|
|
|
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 ->
|
2018-11-15 12:13:45 +01:00
|
|
|
case maps:get(From, StateData#state.robots, error) of
|
|
|
|
passed -> true;
|
|
|
|
_ ->
|
2013-03-14 10:33:02 +01:00
|
|
|
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.
|
|
|
|
|
2017-02-18 07:36:27 +01:00
|
|
|
-spec extract_password(presence() | iq()) -> binary() | false.
|
|
|
|
extract_password(#presence{} = Pres) ->
|
|
|
|
case xmpp:get_subtag(Pres, #muc{}) of
|
|
|
|
#muc{password = Password} when is_binary(Password) ->
|
2016-12-02 16:18:22 +01:00
|
|
|
Password;
|
2017-02-18 07:36:27 +01:00
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
extract_password(#iq{} = IQ) ->
|
|
|
|
case xmpp:get_subtag(IQ, #muc_subscribe{}) of
|
|
|
|
#muc_subscribe{password = Password} when Password /= <<"">> ->
|
2016-07-25 12:50:30 +02:00
|
|
|
Password;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
2003-08-15 21:17:12 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec get_history(binary(), stanza(), state()) -> [lqueue_elem()].
|
2016-10-17 12:37:23 +02:00
|
|
|
get_history(Nick, Packet, #state{history = History}) ->
|
|
|
|
case xmpp:get_subtag(Packet, #muc{}) of
|
|
|
|
#muc{history = #muc_history{} = MUCHistory} ->
|
2019-02-27 09:56:20 +01:00
|
|
|
Now = erlang:timestamp(),
|
2016-10-17 12:37:23 +02:00
|
|
|
Q = History#lqueue.queue,
|
2017-03-10 13:12:43 +01:00
|
|
|
filter_history(Q, Now, Nick, MUCHistory);
|
2016-10-17 12:37:23 +02:00
|
|
|
_ ->
|
2017-03-10 13:12:43 +01:00
|
|
|
p1_queue:to_list(History#lqueue.queue)
|
2004-02-15 21:10:40 +01:00
|
|
|
end.
|
|
|
|
|
2019-06-28 21:16:29 +02:00
|
|
|
-spec filter_history(p1_queue:queue(lqueue_elem()), erlang:timestamp(),
|
2019-06-14 11:33:26 +02:00
|
|
|
binary(), muc_history()) -> [lqueue_elem()].
|
2017-03-10 13:12:43 +01:00
|
|
|
filter_history(Queue, Now, Nick,
|
|
|
|
#muc_history{since = Since,
|
|
|
|
seconds = Seconds,
|
|
|
|
maxstanzas = MaxStanzas,
|
|
|
|
maxchars = MaxChars}) ->
|
|
|
|
{History, _, _} =
|
|
|
|
lists:foldr(
|
|
|
|
fun({_, _, _, TimeStamp, Size} = Elem,
|
|
|
|
{Elems, NumStanzas, NumChars} = Acc) ->
|
|
|
|
NowDiff = timer:now_diff(Now, TimeStamp) div 1000000,
|
|
|
|
Chars = Size + byte_size(Nick) + 1,
|
|
|
|
if (NumStanzas < MaxStanzas) andalso
|
|
|
|
(TimeStamp > Since) andalso
|
|
|
|
(NowDiff =< Seconds) andalso
|
|
|
|
(NumChars + Chars =< MaxChars) ->
|
|
|
|
{[Elem|Elems], NumStanzas + 1, NumChars + Chars};
|
|
|
|
true ->
|
|
|
|
Acc
|
|
|
|
end
|
|
|
|
end, {[], 0, 0}, p1_queue:to_list(Queue)),
|
|
|
|
History.
|
2004-02-15 21:10:40 +01:00
|
|
|
|
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) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
MaxUsersPresence = mod_muc_opt:max_users_presence(StateData#state.server_host),
|
2018-11-15 12:13:45 +01:00
|
|
|
maps:size(StateData#state.users) > MaxUsersPresence.
|
2015-05-08 13:09:13 +02:00
|
|
|
|
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).
|
|
|
|
|
2017-10-09 16:59:22 +02:00
|
|
|
-spec send_initial_presences_and_messages(
|
|
|
|
jid(), binary(), presence(), state(), state()) -> ok.
|
|
|
|
send_initial_presences_and_messages(From, Nick, Presence, NewState, OldState) ->
|
2018-10-01 22:03:11 +02:00
|
|
|
advertise_entity_capabilities(From, NewState),
|
2017-10-09 16:59:22 +02:00
|
|
|
send_existing_presences(From, NewState),
|
2018-10-01 22:03:11 +02:00
|
|
|
send_self_presence(From, NewState, OldState),
|
2017-10-09 16:59:22 +02:00
|
|
|
History = get_history(Nick, Presence, NewState),
|
|
|
|
send_history(From, History, NewState),
|
|
|
|
send_subject(From, OldState).
|
|
|
|
|
2018-10-01 22:03:11 +02:00
|
|
|
-spec advertise_entity_capabilities(jid(), state()) -> ok.
|
|
|
|
advertise_entity_capabilities(JID, State) ->
|
2018-02-12 15:37:36 +01:00
|
|
|
AvatarHash = (State#state.config)#config.vcard_xupdate,
|
|
|
|
DiscoInfo = make_disco_info(JID, State),
|
2018-09-14 00:18:17 +02:00
|
|
|
Extras = iq_disco_info_extras(<<"en">>, State, true),
|
|
|
|
DiscoInfo1 = DiscoInfo#disco_info{xdata = [Extras]},
|
|
|
|
DiscoHash = mod_caps:compute_disco_hash(DiscoInfo1, sha),
|
2018-02-12 15:37:36 +01:00
|
|
|
Els1 = [#caps{hash = <<"sha-1">>,
|
2018-06-14 13:00:47 +02:00
|
|
|
node = ejabberd_config:get_uri(),
|
2018-02-12 15:37:36 +01:00
|
|
|
version = DiscoHash}],
|
|
|
|
Els2 = if is_binary(AvatarHash) ->
|
|
|
|
[#vcard_xupdate{hash = AvatarHash}|Els1];
|
|
|
|
true ->
|
|
|
|
Els1
|
|
|
|
end,
|
|
|
|
ejabberd_router:route(#presence{from = State#state.jid, to = JID,
|
2018-07-05 10:51:49 +02:00
|
|
|
id = p1_rand:get_string(),
|
2018-02-12 15:37:36 +01:00
|
|
|
sub_els = Els2}).
|
|
|
|
|
2018-10-01 22:03:11 +02:00
|
|
|
-spec send_self_presence(jid(), state(), state()) -> ok.
|
|
|
|
send_self_presence(NJID, StateData, OldStateData) ->
|
2018-11-15 12:13:45 +01:00
|
|
|
send_new_presence(NJID, <<"">>, true, StateData, OldStateData).
|
2016-02-08 20:10:20 +01:00
|
|
|
|
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, <<"">>} ->
|
2018-11-15 12:13:45 +01:00
|
|
|
maps:fold(fun (J, _, Js) ->
|
|
|
|
case J of
|
|
|
|
{U, S, _} -> [J | Js];
|
|
|
|
_ -> Js
|
|
|
|
end
|
|
|
|
end, [], StateData#state.users);
|
2013-03-14 10:33:02 +01:00
|
|
|
_ ->
|
2018-11-15 12:13:45 +01:00
|
|
|
case maps:is_key(LJID, StateData#state.users) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true -> [LJID];
|
|
|
|
_ -> []
|
|
|
|
end
|
2003-04-13 21:22:46 +02:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:foreach(fun (J) ->
|
2018-11-15 12:13:45 +01:00
|
|
|
send_new_presence(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).
|
|
|
|
|
2017-02-18 07:36:27 +01:00
|
|
|
-spec is_ra_changed(jid(), boolean(), state(), state()) -> boolean().
|
2016-06-26 08:08:37 +02:00
|
|
|
is_ra_changed(_, _IsInitialPresence = true, _, _) ->
|
|
|
|
false;
|
2017-02-18 07:36:27 +01:00
|
|
|
is_ra_changed(JID, _IsInitialPresence = false, NewStateData, OldStateData) ->
|
|
|
|
NewRole = get_role(JID, NewStateData),
|
2016-06-26 08:08:37 +02:00
|
|
|
NewAff = get_affiliation(JID, NewStateData),
|
2017-02-18 07:36:27 +01:00
|
|
|
OldRole = get_role(JID, OldStateData),
|
2016-06-26 08:08:37 +02:00
|
|
|
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.
|
|
|
|
|
2018-11-15 12:13:45 +01:00
|
|
|
-spec send_new_presence(jid(), binary(), boolean(), state(), state()) -> ok.
|
|
|
|
send_new_presence(NJID, Reason, IsInitialPresence, StateData, OldStateData) ->
|
2015-12-08 19:24:39 +01:00
|
|
|
LNJID = jid:tolower(NJID),
|
2018-11-15 12:13:45 +01:00
|
|
|
#user{nick = Nick} = maps:get(LNJID, StateData#state.users),
|
2009-08-21 15:22:18 +02:00
|
|
|
LJID = find_jid_by_nick(Nick, StateData),
|
2018-11-15 12:13:45 +01:00
|
|
|
#user{jid = RealJID, role = Role0,
|
|
|
|
last_presence = Presence0} = UserInfo =
|
|
|
|
maps:get(jid:tolower(LJID), StateData#state.users),
|
2015-12-08 19:24:39 +01:00
|
|
|
{Role1, Presence1} =
|
2019-08-23 08:32:58 +02:00
|
|
|
case (presence_broadcast_allowed(NJID, StateData) orelse
|
|
|
|
presence_broadcast_allowed(NJID, OldStateData)) of
|
2015-12-08 19:24:39 +01:00
|
|
|
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),
|
2021-09-13 07:15:11 +02:00
|
|
|
Node1 = case is_ra_changed(NJID, IsInitialPresence, StateData, OldStateData) of
|
|
|
|
true -> ?NS_MUCSUB_NODES_AFFILIATIONS;
|
|
|
|
false -> ?NS_MUCSUB_NODES_PRESENCE
|
|
|
|
end,
|
|
|
|
Node2 = ?NS_MUCSUB_NODES_PARTICIPANTS,
|
2018-11-15 12:13:45 +01:00
|
|
|
UserMap =
|
|
|
|
case is_room_overcrowded(StateData) orelse
|
|
|
|
(not (presence_broadcast_allowed(NJID, StateData) orelse
|
|
|
|
presence_broadcast_allowed(NJID, OldStateData))) of
|
2015-12-08 19:24:39 +01:00
|
|
|
true ->
|
2018-11-15 12:13:45 +01:00
|
|
|
#{LNJID => UserInfo};
|
2015-12-08 19:24:39 +01:00
|
|
|
false ->
|
2021-09-13 07:15:11 +02:00
|
|
|
%% TODO: optimize further
|
|
|
|
UM1 = get_users_and_subscribers_with_node(Node1, StateData),
|
|
|
|
UM2 = get_users_and_subscribers_with_node(Node2, StateData),
|
|
|
|
maps:merge(UM1, UM2)
|
2015-12-08 19:24:39 +01:00
|
|
|
end,
|
2018-11-15 12:13:45 +01:00
|
|
|
maps:fold(
|
|
|
|
fun(LUJID, Info, _) ->
|
2016-10-17 12:37:23 +02:00
|
|
|
IsSelfPresence = LNJID == LUJID,
|
|
|
|
{Role, Presence} = if IsSelfPresence -> {Role0, Presence0};
|
2016-06-26 08:08:37 +02:00
|
|
|
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
|
2016-10-17 12:37:23 +02:00
|
|
|
== false orelse IsSelfPresence of
|
2016-07-25 12:50:30 +02:00
|
|
|
true -> Item0#muc_item{jid = RealJID};
|
|
|
|
false -> Item0
|
|
|
|
end,
|
2016-08-05 07:41:08 +02:00
|
|
|
Item = Item1#muc_item{reason = Reason},
|
2016-10-17 12:37:23 +02:00
|
|
|
StatusCodes = status_codes(IsInitialPresence, IsSelfPresence,
|
2016-07-25 12:50:30 +02:00
|
|
|
StateData),
|
|
|
|
Pres = if Presence == undefined -> #presence{};
|
2016-06-26 08:08:37 +02:00
|
|
|
true -> Presence
|
|
|
|
end,
|
2021-10-29 04:12:26 +02:00
|
|
|
Packet = xmpp:set_subtag(
|
|
|
|
add_presence_hats(NJID, Pres, StateData),
|
|
|
|
#muc_user{items = [Item],
|
|
|
|
status_codes = StatusCodes}),
|
2016-06-26 08:08:37 +02:00
|
|
|
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-09-07 09:33:37 +02:00
|
|
|
IsSubscriber = is_subscriber(Info#user.jid, StateData),
|
2016-06-26 08:08:37 +02:00
|
|
|
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
|
|
|
send_wrapped(jid:replace_resource(StateData#state.jid, Nick),
|
|
|
|
Info#user.jid, Packet, Node2, StateData);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end
|
2018-11-15 12:13:45 +01:00
|
|
|
end, ok, UserMap).
|
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),
|
2018-11-15 12:13:45 +01:00
|
|
|
#user{jid = RealToJID, role = Role} = maps:get(LToJID, StateData#state.users),
|
|
|
|
maps:fold(
|
|
|
|
fun(FromNick, _Users, _) ->
|
2016-06-26 08:08:37 +02:00
|
|
|
LJID = find_jid_by_nick(FromNick, StateData),
|
|
|
|
#user{jid = FromJID, role = FromRole,
|
|
|
|
last_presence = Presence} =
|
2018-11-15 12:13:45 +01:00
|
|
|
maps:get(jid:tolower(LJID), StateData#state.users),
|
2016-06-26 08:08:37 +02:00
|
|
|
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(
|
2021-10-29 04:12:26 +02:00
|
|
|
add_presence_hats(
|
|
|
|
FromJID, Presence, StateData),
|
|
|
|
#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
|
2018-11-15 12:13:45 +01:00
|
|
|
end, ok, 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),
|
2018-11-15 12:13:45 +01:00
|
|
|
#user{nick = OldNick} = maps:get(LJID, State#state.users),
|
|
|
|
Users = maps:update_with(LJID,
|
|
|
|
fun (#user{} = User) -> User#user{nick = Nick} end,
|
|
|
|
State#state.users),
|
|
|
|
OldNickUsers = maps:get(OldNick, State#state.nicks),
|
|
|
|
NewNickUsers = maps:get(Nick, State#state.nicks, []),
|
2013-03-14 10:33:02 +01:00
|
|
|
Nicks = case OldNickUsers of
|
2016-06-26 08:08:37 +02:00
|
|
|
[LJID] ->
|
2018-11-15 12:13:45 +01:00
|
|
|
maps:put(Nick, [LJID | NewNickUsers -- [LJID]],
|
|
|
|
maps:remove(OldNick, State#state.nicks));
|
2016-06-26 08:08:37 +02:00
|
|
|
[_ | _] ->
|
2018-11-15 12:13:45 +01:00
|
|
|
maps:put(Nick, [LJID | NewNickUsers -- [LJID]],
|
|
|
|
maps:put(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),
|
2018-11-15 12:13:45 +01:00
|
|
|
#user{nick = OldNick} = maps:get(LJID, StateData#state.users),
|
|
|
|
OldNickUsers = maps:get(OldNick, StateData#state.nicks),
|
|
|
|
NewNickUsers = maps:get(Nick, StateData#state.nicks, []),
|
2016-06-26 08:08:37 +02:00
|
|
|
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) ->
|
2018-11-15 12:13:45 +01:00
|
|
|
#user{jid = RealJID, nick = Nick, role = Role,
|
|
|
|
last_presence = Presence} =
|
|
|
|
maps:get(jid:tolower(JID), StateData#state.users),
|
2003-03-23 21:08:44 +01:00
|
|
|
Affiliation = get_affiliation(JID, StateData),
|
2018-11-15 12:13:45 +01:00
|
|
|
maps:fold(
|
|
|
|
fun(LJID, Info, _) when Presence /= undefined ->
|
2016-10-17 12:37:23 +02:00
|
|
|
IsSelfPresence = LJID == jid:tolower(JID),
|
2016-07-25 12:50:30 +02:00
|
|
|
Item0 = #muc_item{affiliation = Affiliation, role = Role},
|
2016-10-17 12:37:23 +02:00
|
|
|
Item = case Info#user.role == moderator orelse
|
|
|
|
(StateData#state.config)#config.anonymous
|
|
|
|
== false orelse IsSelfPresence of
|
|
|
|
true -> Item0#muc_item{jid = RealJID};
|
|
|
|
false -> Item0
|
|
|
|
end,
|
|
|
|
Status110 = case IsSelfPresence of
|
2016-07-25 12:50:30 +02:00
|
|
|
true -> [110];
|
|
|
|
false -> []
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
2016-10-17 12:37:23 +02:00
|
|
|
Packet1 = #presence{
|
|
|
|
type = unavailable,
|
|
|
|
sub_els = [#muc_user{
|
|
|
|
items = [Item#muc_item{nick = Nick}],
|
|
|
|
status_codes = [303|Status110]}]},
|
2016-07-25 12:50:30 +02:00
|
|
|
Packet2 = xmpp:set_subtag(Presence,
|
2016-10-17 12:37:23 +02:00
|
|
|
#muc_user{items = [Item],
|
2016-07-25 12:50:30 +02:00
|
|
|
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;
|
2018-11-15 12:13:45 +01:00
|
|
|
(_, _, _) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
ok
|
2021-09-13 07:15:11 +02:00
|
|
|
end, ok, get_users_and_subscribers_with_node(
|
|
|
|
?NS_MUCSUB_NODES_PRESENCE, StateData)).
|
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),
|
2021-09-13 07:15:11 +02:00
|
|
|
%% TODO: there should be a better way to check IsOccupant
|
2016-09-07 09:33:37 +02:00
|
|
|
Users = get_users_and_subscribers(StateData),
|
2016-05-20 01:28:16 +02:00
|
|
|
IsOccupant = case LJID of
|
2018-11-15 12:13:45 +01:00
|
|
|
{LUser, LServer, <<"">>} ->
|
|
|
|
#{} /= maps:filter(
|
|
|
|
fun({U, S, _}, _) ->
|
|
|
|
U == LUser andalso
|
|
|
|
S == LServer
|
|
|
|
end, Users);
|
|
|
|
{_LUser, _LServer, _LResource} ->
|
|
|
|
maps:is_key(LJID, Users)
|
2016-05-20 01:28:16 +02:00
|
|
|
end,
|
|
|
|
case IsOccupant of
|
|
|
|
true ->
|
|
|
|
ok; % The new affiliation is published via presence.
|
|
|
|
false ->
|
2016-10-17 12:37:23 +02:00
|
|
|
send_affiliation(JID, Affiliation, StateData)
|
2016-05-20 01:28:16 +02:00
|
|
|
end.
|
|
|
|
|
2016-10-17 12:37:23 +02:00
|
|
|
-spec send_affiliation(jid(), affiliation(), state()) -> ok.
|
|
|
|
send_affiliation(JID, Affiliation, StateData) ->
|
|
|
|
Item = #muc_item{jid = JID,
|
2016-07-25 12:50:30 +02:00
|
|
|
affiliation = Affiliation,
|
|
|
|
role = none},
|
2018-07-05 10:51:49 +02:00
|
|
|
Message = #message{id = p1_rand:get_string(),
|
2016-07-25 12:50:30 +02:00
|
|
|
sub_els = [#muc_user{items = [Item]}]},
|
2021-09-13 07:15:11 +02:00
|
|
|
Users = get_users_and_subscribers_with_node(
|
|
|
|
?NS_MUCSUB_NODES_AFFILIATIONS, StateData),
|
2016-05-20 01:28:16 +02:00
|
|
|
Recipients = case (StateData#state.config)#config.anonymous of
|
|
|
|
true ->
|
2018-11-15 12:13:45 +01:00
|
|
|
maps:filter(fun(_, #user{role = moderator}) ->
|
|
|
|
true;
|
|
|
|
(_, _) ->
|
|
|
|
false
|
|
|
|
end, Users);
|
2016-05-20 01:28:16 +02:00
|
|
|
false ->
|
2016-09-07 09:33:37 +02:00
|
|
|
Users
|
2016-05-20 01:28:16 +02:00
|
|
|
end,
|
2016-09-07 09:33:37 +02:00
|
|
|
send_wrapped_multiple(StateData#state.jid, Recipients, Message,
|
|
|
|
?NS_MUCSUB_NODES_AFFILIATIONS, StateData).
|
2016-05-20 01:28:16 +02:00
|
|
|
|
2016-10-17 12:37:23 +02:00
|
|
|
-spec status_codes(boolean(), boolean(), state()) -> [pos_integer()].
|
|
|
|
status_codes(IsInitialPresence, _IsSelfPresence = true, StateData) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
S0 = [110],
|
|
|
|
case IsInitialPresence of
|
|
|
|
true ->
|
|
|
|
S1 = case StateData#state.just_created of
|
|
|
|
true -> [201|S0];
|
2019-02-06 16:13:30 +01:00
|
|
|
_ -> S0
|
2016-07-25 12:50:30 +02:00
|
|
|
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;
|
2016-10-17 12:37:23 +02:00
|
|
|
status_codes(_IsInitialPresence, _IsSelfPresence = false, _StateData) -> [].
|
2016-02-08 20:10:20 +01:00
|
|
|
|
2017-03-10 13:12:43 +01:00
|
|
|
-spec lqueue_new(non_neg_integer(), ram | file) -> lqueue().
|
|
|
|
lqueue_new(Max, Type) ->
|
|
|
|
#lqueue{queue = p1_queue:new(Type), max = Max}.
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec lqueue_in(lqueue_elem(), 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.
|
2017-03-10 13:12:43 +01:00
|
|
|
lqueue_in(Item, #lqueue{queue = Q1, max = Max}) ->
|
|
|
|
Len = p1_queue:len(Q1),
|
|
|
|
Q2 = p1_queue:in(Item, Q1),
|
2013-03-14 10:33:02 +01:00
|
|
|
if Len >= Max ->
|
|
|
|
Q3 = lqueue_cut(Q2, Len - Max + 1),
|
2017-03-10 13:12:43 +01:00
|
|
|
#lqueue{queue = Q3, max = Max};
|
|
|
|
true -> #lqueue{queue = Q2, max = Max}
|
2003-03-23 21:08:44 +01:00
|
|
|
end.
|
|
|
|
|
2019-06-28 21:16:29 +02:00
|
|
|
-spec lqueue_cut(p1_queue:queue(lqueue_elem()), non_neg_integer()) -> p1_queue:queue(lqueue_elem()).
|
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) ->
|
2017-03-10 13:12:43 +01:00
|
|
|
{_, Q1} = p1_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 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) ->
|
2006-03-14 05:26:15 +01:00
|
|
|
add_to_log(text, {FromNick, Packet}, StateData),
|
2016-10-17 12:37:23 +02:00
|
|
|
case check_subject(Packet) of
|
2017-11-13 09:25:35 +01:00
|
|
|
[] ->
|
2019-02-27 09:56:20 +01:00
|
|
|
TimeStamp = erlang:timestamp(),
|
2016-10-17 12:37:23 +02:00
|
|
|
AddrPacket = case (StateData#state.config)#config.anonymous of
|
|
|
|
true -> Packet;
|
|
|
|
false ->
|
|
|
|
Addresses = #addresses{
|
|
|
|
list = [#address{type = ofrom,
|
|
|
|
jid = FromJID}]},
|
|
|
|
xmpp:set_subtag(Packet, Addresses)
|
|
|
|
end,
|
2018-10-25 01:05:45 +02:00
|
|
|
TSPacket = misc:add_delay_info(
|
2016-10-17 12:37:23 +02:00
|
|
|
AddrPacket, StateData#state.jid, TimeStamp),
|
|
|
|
SPacket = xmpp:set_from_to(
|
|
|
|
TSPacket,
|
|
|
|
jid:replace_resource(StateData#state.jid, FromNick),
|
|
|
|
StateData#state.jid),
|
|
|
|
Size = element_size(SPacket),
|
|
|
|
Q1 = lqueue_in({FromNick, TSPacket, false,
|
|
|
|
TimeStamp, Size},
|
|
|
|
StateData#state.history),
|
2020-11-18 17:14:28 +01:00
|
|
|
StateData#state{history = Q1, just_created = erlang:system_time(microsecond)};
|
2016-10-17 12:37:23 +02:00
|
|
|
_ ->
|
2020-11-18 17:14:28 +01:00
|
|
|
StateData#state{just_created = erlang:system_time(microsecond)}
|
2016-10-17 12:37:23 +02:00
|
|
|
end.
|
|
|
|
|
2023-03-30 14:37:13 +02:00
|
|
|
remove_from_history(StanzaId, #state{history = #lqueue{queue = Queue} = LQueue} = StateData) ->
|
|
|
|
NewQ = p1_queue:foldl(
|
|
|
|
fun({_, Pkt, _, _, _} = Entry, Acc) ->
|
2023-04-14 12:05:49 +02:00
|
|
|
case xmpp:get_meta(Pkt, stanza_id, missing) of
|
2023-03-30 14:37:13 +02:00
|
|
|
V when V == StanzaId ->
|
|
|
|
Acc;
|
|
|
|
_ ->
|
|
|
|
p1_queue:in(Entry, Acc)
|
|
|
|
end
|
|
|
|
end, p1_queue:new(), Queue),
|
|
|
|
StateData#state{history = LQueue#lqueue{queue = NewQ}}.
|
|
|
|
|
2023-04-14 12:19:38 +02:00
|
|
|
remove_from_history({U1, S1}, OriginId, #state{history = #lqueue{queue = Queue} = LQueue} = StateData) ->
|
|
|
|
{NewQ, StanzaId} = p1_queue:foldl(
|
|
|
|
fun({_, Pkt, _, _, _} = Entry, {Q, none}) ->
|
|
|
|
case jid:tolower(xmpp:get_from(Pkt)) of
|
|
|
|
{U2, S2, _} when U1 == U2, S1 == S2 ->
|
|
|
|
case xmpp:get_subtag(Pkt, #origin_id{}) of
|
|
|
|
#origin_id{id = V} when V == OriginId ->
|
|
|
|
{Q, xmpp:get_meta(Pkt, stanza_id, missing)};
|
|
|
|
_ ->
|
|
|
|
{p1_queue:in(Entry, Q), none}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
{p1_queue:in(Entry, Q), none}
|
|
|
|
end;
|
|
|
|
(Entry, {Q, S}) ->
|
|
|
|
{p1_queue:in(Entry, Q), S}
|
|
|
|
end, {p1_queue:new(), none}, Queue),
|
|
|
|
{StateData#state{history = LQueue#lqueue{queue = NewQ}}, StanzaId}.
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec send_history(jid(), [lqueue_elem()], state()) -> ok.
|
2016-10-17 12:37:23 +02:00
|
|
|
send_history(JID, History, StateData) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun({Nick, Packet, _HaveSubject, _TimeStamp, _Size}) ->
|
|
|
|
ejabberd_router:route(
|
2017-02-16 09:00:26 +01:00
|
|
|
xmpp:set_from_to(
|
|
|
|
Packet,
|
|
|
|
jid:replace_resource(StateData#state.jid, Nick),
|
|
|
|
JID))
|
2017-03-10 13:12:43 +01:00
|
|
|
end, History).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec send_subject(jid(), state()) -> ok.
|
2023-08-15 12:44:41 +02:00
|
|
|
send_subject(JID, #state{subject_author = {Nick, AuthorJID}} = StateData) ->
|
2016-10-17 12:37:23 +02:00
|
|
|
Subject = case StateData#state.subject of
|
2017-11-13 09:25:35 +01:00
|
|
|
[] -> [#text{}];
|
|
|
|
[_|_] = S -> S
|
2016-10-17 12:37:23 +02:00
|
|
|
end,
|
2023-08-15 12:44:41 +02:00
|
|
|
Packet = #message{from = AuthorJID,
|
2017-02-16 09:00:26 +01:00
|
|
|
to = JID, type = groupchat, subject = Subject},
|
2023-08-15 12:44:41 +02:00
|
|
|
case ejabberd_hooks:run_fold(muc_filter_message,
|
|
|
|
StateData#state.server_host,
|
2023-08-24 11:54:26 +02:00
|
|
|
xmpp:put_meta(Packet, mam_ignore, true),
|
2023-08-15 12:44:41 +02:00
|
|
|
[StateData, Nick]) of
|
|
|
|
drop ->
|
|
|
|
ok;
|
|
|
|
NewPacket1 ->
|
|
|
|
FromRoomNick = jid:replace_resource(StateData#state.jid, Nick),
|
|
|
|
NewPacket2 = xmpp:set_from(NewPacket1, FromRoomNick),
|
|
|
|
ejabberd_router:route(NewPacket2)
|
|
|
|
end.
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2017-11-13 09:25:35 +01:00
|
|
|
-spec check_subject(message()) -> [text()].
|
2016-10-17 12:37:23 +02:00
|
|
|
check_subject(#message{subject = [_|_] = Subj, body = [],
|
|
|
|
thread = undefined}) ->
|
2017-11-13 09:25:35 +01:00
|
|
|
Subj;
|
2016-10-17 12:37:23 +02:00
|
|
|
check_subject(_) ->
|
2017-11-13 09:25:35 +01:00
|
|
|
[].
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-11-12 11:27:15 +01:00
|
|
|
-spec can_change_subject(role(), boolean(), state()) -> boolean().
|
2016-10-20 20:34:48 +02:00
|
|
|
can_change_subject(Role, IsSubscriber, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case (StateData#state.config)#config.allow_change_subj
|
|
|
|
of
|
2016-10-20 20:34:48 +02:00
|
|
|
true -> Role == moderator orelse Role == participant orelse IsSubscriber == true;
|
2013-03-14 10:33:02 +01:00
|
|
|
_ -> Role == moderator
|
2003-03-26 21:51:18 +01:00
|
|
|
end.
|
2003-03-23 21:08:44 +01:00
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% Admin stuff
|
|
|
|
|
2016-09-08 16:08:48 +02:00
|
|
|
-spec process_iq_admin(jid(), iq(), #state{}) -> {error, stanza_error()} |
|
2016-07-25 12:50:30 +02:00
|
|
|
{result, undefined, #state{}} |
|
|
|
|
{result, muc_admin()}.
|
|
|
|
process_iq_admin(_From, #iq{lang = Lang, sub_els = [#muc_admin{items = []}]},
|
|
|
|
_StateData) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("No 'item' element found"),
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, xmpp:err_bad_request(Txt, Lang)};
|
2019-07-15 12:59:41 +02:00
|
|
|
process_iq_admin(_From, #iq{type = get, lang = Lang,
|
|
|
|
sub_els = [#muc_admin{items = [_, _|_]}]},
|
|
|
|
_StateData) ->
|
|
|
|
ErrText = ?T("Too many <item/> elements"),
|
|
|
|
{error, xmpp:err_bad_request(ErrText, Lang)};
|
2016-07-25 12:50:30 +02:00
|
|
|
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} ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Neither 'role' nor 'affiliation' attribute found"),
|
2016-07-25 12:50:30 +02:00
|
|
|
{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 ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Administrator privileges required"),
|
2016-07-25 12:50:30 +02:00
|
|
|
{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 ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Moderator privileges required"),
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, xmpp:err_forbidden(ErrText, Lang)}
|
|
|
|
end
|
2019-07-15 12:59:41 +02:00
|
|
|
end.
|
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}}) ->
|
2016-10-17 12:37:23 +02:00
|
|
|
#muc_item{affiliation = Affiliation, jid = jid:make(JID),
|
2016-08-05 07:41:08 +02:00
|
|
|
reason = Reason};
|
2016-07-25 12:50:30 +02:00
|
|
|
({JID, Affiliation}) ->
|
2016-10-17 12:37:23 +02:00
|
|
|
#muc_item{affiliation = Affiliation, jid = jid:make(JID)}
|
2016-07-25 12:50:30 +02:00
|
|
|
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,
|
2018-11-15 12:13:45 +01:00
|
|
|
maps: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()) ->
|
2018-04-23 10:35:43 +02:00
|
|
|
[{ljid(),
|
|
|
|
affiliation() | {affiliation(), binary()}}].
|
|
|
|
search_affiliation(Affiliation,
|
|
|
|
#state{config = #config{persistent = false}} = StateData) ->
|
|
|
|
search_affiliation_fallback(Affiliation, StateData);
|
2003-03-23 21:08:44 +01:00
|
|
|
search_affiliation(Affiliation, StateData) ->
|
2018-04-23 10:35:43 +02:00
|
|
|
Room = StateData#state.room,
|
|
|
|
Host = StateData#state.host,
|
|
|
|
ServerHost = StateData#state.server_host,
|
|
|
|
Mod = gen_mod:db_mod(ServerHost, mod_muc),
|
|
|
|
case Mod:search_affiliation(ServerHost, Room, Host, Affiliation) of
|
|
|
|
{ok, AffiliationList} ->
|
|
|
|
AffiliationList;
|
|
|
|
{error, _} ->
|
|
|
|
search_affiliation_fallback(Affiliation, StateData)
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec search_affiliation_fallback(affiliation(), state()) ->
|
|
|
|
[{ljid(),
|
|
|
|
affiliation() | {affiliation(), binary()}}].
|
|
|
|
search_affiliation_fallback(Affiliation, StateData) ->
|
|
|
|
lists:filter(
|
|
|
|
fun({_, A}) ->
|
|
|
|
case A of
|
|
|
|
{A1, _Reason} -> Affiliation == A1;
|
|
|
|
_ -> Affiliation == A
|
|
|
|
end
|
2018-11-15 12:13:45 +01:00
|
|
|
end, maps:to_list(StateData#state.affiliations)).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2016-08-05 07:41:08 +02:00
|
|
|
-spec process_admin_items_set(jid(), [muc_item()], binary(),
|
2016-07-25 12:50:30 +02:00
|
|
|
#state{}) -> {result, undefined, #state{}} |
|
2016-09-08 16:08:48 +02:00
|
|
|
{error, stanza_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} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Processing MUC admin query from ~ts in "
|
|
|
|
"room ~ts:~n ~p",
|
2017-02-26 08:07:12 +01:00
|
|
|
[jid:encode(UJID),
|
|
|
|
jid:encode(StateData#state.jid), Res]),
|
2016-10-17 12:37:23 +02:00
|
|
|
case lists:foldl(process_item_change(UJID),
|
|
|
|
StateData, lists:flatten(Res)) of
|
|
|
|
{error, _} = Err ->
|
|
|
|
Err;
|
|
|
|
NSD ->
|
|
|
|
store_room(NSD),
|
|
|
|
{result, undefined, NSD}
|
|
|
|
end;
|
|
|
|
{error, Err} -> {error, Err}
|
2003-03-23 21:08:44 +01:00
|
|
|
end.
|
|
|
|
|
2019-07-09 14:21:17 +02:00
|
|
|
-spec process_item_change(jid()) -> fun((admin_action(), state() | {error, stanza_error()}) ->
|
|
|
|
state() | {error, stanza_error()}).
|
2015-02-25 16:31:27 +01:00
|
|
|
process_item_change(UJID) ->
|
2016-10-17 12:37:23 +02:00
|
|
|
fun(_, {error, _} = Err) ->
|
|
|
|
Err;
|
|
|
|
(Item, SD) ->
|
|
|
|
process_item_change(Item, SD, UJID)
|
2015-02-25 16:31:27 +01:00
|
|
|
end.
|
|
|
|
|
2017-06-21 15:44:09 +02:00
|
|
|
-spec process_item_change(admin_action(), state(), undefined | jid()) -> state() | {error, stanza_error()}.
|
2016-07-25 12:50:30 +02:00
|
|
|
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} ->
|
2016-10-17 12:37:23 +02:00
|
|
|
send_kickban_presence(UJID, JID, Reason, 307, SD),
|
2016-07-25 12:50:30 +02:00
|
|
|
set_role(JID, none, SD);
|
|
|
|
{JID, affiliation, none, Reason} ->
|
2022-07-01 08:13:37 +02:00
|
|
|
case get_affiliation(JID, SD) of
|
|
|
|
none -> SD;
|
|
|
|
_ ->
|
|
|
|
case (SD#state.config)#config.members_only of
|
|
|
|
true ->
|
|
|
|
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),
|
|
|
|
SD2 = case (SD1#state.config)#config.moderated of
|
|
|
|
true -> set_role(JID, visitor, SD1);
|
|
|
|
false -> set_role(JID, participant, SD1)
|
|
|
|
end,
|
|
|
|
send_update_presence(JID, Reason, SD2, SD),
|
|
|
|
maybe_send_affiliation(JID, none, SD2),
|
|
|
|
SD2
|
|
|
|
end
|
|
|
|
end;
|
2016-07-25 12:50:30 +02:00
|
|
|
{JID, affiliation, outcast, Reason} ->
|
2016-10-17 12:37:23 +02:00
|
|
|
send_kickban_presence(UJID, JID, Reason, 301, outcast, SD),
|
2016-07-25 12:50:30 +02:00
|
|
|
maybe_send_affiliation(JID, outcast, SD),
|
2021-03-18 10:28:03 +01:00
|
|
|
{result, undefined, SD2} =
|
|
|
|
process_iq_mucsub(JID,
|
|
|
|
#iq{type = set,
|
|
|
|
sub_els = [#muc_unsubscribe{}]}, SD),
|
2023-03-19 06:40:19 +01:00
|
|
|
set_role(JID, none, set_affiliation(JID, outcast, SD2, Reason));
|
2016-07-25 12:50:30 +02:00
|
|
|
{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),
|
2016-10-17 12:37:23 +02:00
|
|
|
send_new_presence(JID, Reason, SD1, SD),
|
2016-07-25 12:50:30 +02:00
|
|
|
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
|
2018-12-13 11:45:45 +01:00
|
|
|
catch ?EX_RULE(E, R, St) ->
|
2019-06-25 23:05:41 +02:00
|
|
|
StackTrace = ?EX_STACK(St),
|
|
|
|
FromSuffix = case UJID of
|
|
|
|
#jid{} ->
|
|
|
|
JidString = jid:encode(UJID),
|
|
|
|
<<" from ", JidString/binary>>;
|
|
|
|
undefined ->
|
|
|
|
<<"">>
|
|
|
|
end,
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Failed to set item ~p~ts:~n** ~ts",
|
2019-07-07 21:12:14 +02:00
|
|
|
[Item, FromSuffix,
|
|
|
|
misc:format_exception(2, E, R, StackTrace)]),
|
2016-10-17 12:37:23 +02:00
|
|
|
{error, xmpp:err_internal_server_error()}
|
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,
|
2017-02-18 07:36:27 +01:00
|
|
|
[#muc_item{jid = undefined, nick = <<"">>}|_],
|
2016-07-25 12:50:30 +02:00
|
|
|
Lang, _StateData, _Res) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Neither 'jid' nor 'nick' attribute found"),
|
2016-07-25 12:50:30 +02:00
|
|
|
throw({error, xmpp:err_bad_request(Txt, Lang)});
|
|
|
|
find_changed_items(_UJID, _UAffiliation, _URole,
|
|
|
|
[#muc_item{role = undefined, affiliation = undefined}|_],
|
|
|
|
Lang, _StateData, _Res) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Neither 'role' nor 'affiliation' attribute found"),
|
2016-07-25 12:50:30 +02:00
|
|
|
throw({error, xmpp:err_bad_request(Txt, Lang)});
|
2003-03-23 21:08:44 +01:00
|
|
|
find_changed_items(UJID, UAffiliation, URole,
|
2016-08-05 07:41:08 +02:00
|
|
|
[#muc_item{jid = J, nick = Nick, reason = Reason,
|
2016-07-25 12:50:30 +02:00
|
|
|
role = Role, affiliation = Affiliation}|Items],
|
2004-03-02 22:16:55 +01:00
|
|
|
Lang, StateData, Res) ->
|
2017-10-30 13:27:37 +01:00
|
|
|
[JID | _] = JIDs =
|
2016-07-25 12:50:30 +02:00
|
|
|
if J /= undefined ->
|
|
|
|
[J];
|
2016-08-05 07:41:08 +02:00
|
|
|
Nick /= <<"">> ->
|
2016-07-25 12:50:30 +02:00
|
|
|
case find_jids_by_nick(Nick, StateData) of
|
|
|
|
[] ->
|
2020-01-22 12:52:30 +01:00
|
|
|
ErrText = {?T("Nickname ~s does not exist in the room"),
|
2017-09-23 23:08:01 +02:00
|
|
|
[Nick]},
|
2016-07-25 12:50:30 +02:00
|
|
|
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),
|
2018-09-20 11:14:11 +02:00
|
|
|
UIsSubscriber = is_subscriber(UJID, StateData),
|
|
|
|
URole1 = case {URole, UIsSubscriber} of
|
|
|
|
{none, true} -> subscriber;
|
|
|
|
{UR, _} -> UR
|
|
|
|
end,
|
2016-07-25 12:50:30 +02:00
|
|
|
CanChangeRA = case can_change_ra(UAffiliation,
|
2018-09-20 11:14:11 +02:00
|
|
|
URole1,
|
2016-07-25 12:50:30 +02:00
|
|
|
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 ->
|
2016-10-17 12:37:23 +02:00
|
|
|
MoreRes = case RoleOrAff of
|
|
|
|
affiliation ->
|
|
|
|
[{jid:remove_resource(Jidx),
|
|
|
|
RoleOrAff, RoleOrAffValue, Reason}
|
|
|
|
|| Jidx <- JIDs];
|
|
|
|
role ->
|
|
|
|
[{Jidx, RoleOrAff, RoleOrAffValue, Reason}
|
|
|
|
|| Jidx <- JIDs]
|
|
|
|
end,
|
2016-07-25 12:50:30 +02:00
|
|
|
find_changed_items(UJID, UAffiliation, URole,
|
|
|
|
Items, Lang, StateData,
|
2017-02-18 07:36:27 +01:00
|
|
|
MoreRes ++ Res);
|
2016-07-25 12:50:30 +02:00
|
|
|
false ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Changing role/affiliation is not allowed"),
|
2016-07-25 12:50:30 +02:00
|
|
|
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(),
|
2017-02-18 07:36:27 +01:00
|
|
|
affiliation, affiliation(), affiliation()) -> boolean() | nothing | check_owner;
|
2016-07-25 12:50:30 +02:00
|
|
|
(affiliation(), role(), affiliation(), role(),
|
2017-02-18 07:36:27 +01:00
|
|
|
role, role(), affiliation()) -> boolean() | nothing | check_owner.
|
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;
|
2018-09-24 11:20:10 +02:00
|
|
|
can_change_ra(FAffiliation, subscriber, _TAffiliation,
|
|
|
|
visitor, role, none, _ServiceAf)
|
|
|
|
when (FAffiliation == owner) or
|
|
|
|
(FAffiliation == admin) ->
|
|
|
|
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;
|
2018-09-20 11:14:11 +02:00
|
|
|
can_change_ra(FAffiliation, subscriber, _TAffiliation,
|
|
|
|
visitor, role, participant, _ServiceAf)
|
|
|
|
when (FAffiliation == owner) or
|
|
|
|
(FAffiliation == admin) ->
|
|
|
|
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;
|
2018-09-24 11:20:10 +02:00
|
|
|
can_change_ra(FAffiliation, subscriber, _TAffiliation,
|
|
|
|
participant, role, none, _ServiceAf)
|
|
|
|
when (FAffiliation == owner) or
|
|
|
|
(FAffiliation == admin) ->
|
|
|
|
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;
|
2018-09-20 11:14:11 +02:00
|
|
|
can_change_ra(FAffiliation, subscriber, _TAffiliation,
|
|
|
|
participant, role, visitor, _ServiceAf)
|
|
|
|
when (FAffiliation == owner) or
|
|
|
|
(FAffiliation == admin) ->
|
|
|
|
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;
|
2018-09-25 10:27:17 +02:00
|
|
|
can_change_ra(owner, moderator, TAffiliation,
|
|
|
|
moderator, role, none, _ServiceAf)
|
|
|
|
when TAffiliation /= owner ->
|
|
|
|
true;
|
2018-09-25 10:43:38 +02:00
|
|
|
can_change_ra(owner, subscriber, TAffiliation,
|
|
|
|
moderator, role, none, _ServiceAf)
|
|
|
|
when TAffiliation /= owner ->
|
|
|
|
true;
|
2018-09-25 10:27:17 +02:00
|
|
|
can_change_ra(admin, moderator, TAffiliation,
|
|
|
|
moderator, role, none, _ServiceAf)
|
|
|
|
when (TAffiliation /= owner) and
|
2018-09-25 10:43:38 +02:00
|
|
|
(TAffiliation /= admin) ->
|
|
|
|
true;
|
|
|
|
can_change_ra(admin, subscriber, TAffiliation,
|
|
|
|
moderator, role, none, _ServiceAf)
|
|
|
|
when (TAffiliation /= owner) and
|
2018-09-25 10:27:17 +02:00
|
|
|
(TAffiliation /= admin) ->
|
|
|
|
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.
|
|
|
|
|
2017-02-18 07:36:27 +01:00
|
|
|
-spec send_kickban_presence(undefined | jid(), jid(), binary(),
|
2016-07-25 12:50:30 +02:00
|
|
|
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
|
|
|
|
2017-02-18 07:36:27 +01:00
|
|
|
-spec send_kickban_presence(undefined | jid(), jid(), binary(), pos_integer(),
|
2016-07-25 12:50:30 +02:00
|
|
|
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
|
2018-11-15 12:13:45 +01:00
|
|
|
{U, S, <<"">>} ->
|
|
|
|
maps:fold(fun (J, _, Js) ->
|
|
|
|
case J of
|
|
|
|
{U, S, _} -> [J | Js];
|
|
|
|
_ -> Js
|
|
|
|
end
|
|
|
|
end, [], StateData#state.users);
|
|
|
|
_ ->
|
|
|
|
case maps:is_key(LJID, StateData#state.users) of
|
|
|
|
true -> [LJID];
|
|
|
|
_ -> []
|
|
|
|
end
|
2003-04-13 21:22:46 +02:00
|
|
|
end,
|
2019-06-27 14:22:27 +02:00
|
|
|
lists:foreach(fun (LJ) ->
|
|
|
|
#user{nick = Nick, jid = J} = maps:get(LJ, 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
|
|
|
|
2017-02-18 07:36:27 +01:00
|
|
|
-spec send_kickban_presence1(undefined | jid(), jid(), binary(), pos_integer(),
|
2016-07-25 12:50:30 +02:00
|
|
|
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) ->
|
2018-11-15 12:13:45 +01:00
|
|
|
#user{jid = RealJID, nick = Nick} = maps:get(jid:tolower(UJID), StateData#state.users),
|
2023-04-14 12:05:49 +02:00
|
|
|
ActorNick = find_nick_by_jid(MJID, StateData),
|
2021-09-13 07:15:11 +02:00
|
|
|
%% TODO: optimize further
|
|
|
|
UserMap =
|
|
|
|
maps:merge(
|
|
|
|
get_users_and_subscribers_with_node(
|
|
|
|
?NS_MUCSUB_NODES_AFFILIATIONS, StateData),
|
|
|
|
get_users_and_subscribers_with_node(
|
|
|
|
?NS_MUCSUB_NODES_PARTICIPANTS, StateData)),
|
2018-11-15 12:13:45 +01:00
|
|
|
maps:fold(
|
|
|
|
fun(LJID, Info, _) ->
|
2016-10-17 12:37:23 +02:00
|
|
|
IsSelfPresence = jid:tolower(UJID) == LJID,
|
2016-07-25 12:50:30 +02:00
|
|
|
Item0 = #muc_item{affiliation = Affiliation,
|
|
|
|
role = none},
|
|
|
|
Item1 = case Info#user.role == moderator orelse
|
|
|
|
(StateData#state.config)#config.anonymous
|
2016-10-17 12:37:23 +02:00
|
|
|
== false orelse IsSelfPresence of
|
2016-07-25 12:50:30 +02:00
|
|
|
true -> Item0#muc_item{jid = RealJID};
|
|
|
|
false -> Item0
|
|
|
|
end,
|
2016-08-05 07:41:08 +02:00
|
|
|
Item2 = Item1#muc_item{reason = Reason},
|
2016-07-25 12:50:30 +02:00
|
|
|
Item = case ActorNick of
|
|
|
|
<<"">> -> Item2;
|
|
|
|
_ -> Item2#muc_item{actor = #muc_actor{nick = ActorNick}}
|
|
|
|
end,
|
2016-10-17 12:37:23 +02:00
|
|
|
Codes = if IsSelfPresence -> [110, Code];
|
|
|
|
true -> [Code]
|
|
|
|
end,
|
2016-07-25 12:50:30 +02:00
|
|
|
Packet = #presence{type = unavailable,
|
|
|
|
sub_els = [#muc_user{items = [Item],
|
2016-10-17 12:37:23 +02:00
|
|
|
status_codes = Codes}]},
|
2016-07-25 12:50:30 +02:00
|
|
|
RoomJIDNick = jid:replace_resource(StateData#state.jid, Nick),
|
|
|
|
send_wrapped(RoomJIDNick, Info#user.jid, Packet,
|
|
|
|
?NS_MUCSUB_NODES_AFFILIATIONS, StateData),
|
2016-09-07 09:33:37 +02:00
|
|
|
IsSubscriber = is_subscriber(Info#user.jid, StateData),
|
2016-07-25 12:50:30 +02:00
|
|
|
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
|
2021-09-13 07:15:11 +02:00
|
|
|
end, ok, UserMap).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec convert_legacy_fields([xdata_field()]) -> [xdata_field()].
|
2017-01-19 09:12:27 +01:00
|
|
|
convert_legacy_fields(Fs) ->
|
|
|
|
lists:map(
|
|
|
|
fun(#xdata_field{var = Var} = F) ->
|
|
|
|
NewVar = case Var of
|
|
|
|
<<"muc#roomconfig_allowvisitorstatus">> ->
|
|
|
|
<<"allow_visitor_status">>;
|
|
|
|
<<"muc#roomconfig_allowvisitornickchange">> ->
|
|
|
|
<<"allow_visitor_nickchange">>;
|
|
|
|
<<"muc#roomconfig_allowvoicerequests">> ->
|
|
|
|
<<"allow_voice_requests">>;
|
|
|
|
<<"muc#roomconfig_allow_subscription">> ->
|
|
|
|
<<"allow_subscription">>;
|
|
|
|
<<"muc#roomconfig_voicerequestmininterval">> ->
|
|
|
|
<<"voice_request_min_interval">>;
|
|
|
|
<<"muc#roomconfig_captcha_whitelist">> ->
|
|
|
|
<<"captcha_whitelist">>;
|
2017-01-19 14:42:04 +01:00
|
|
|
<<"muc#roomconfig_mam">> ->
|
|
|
|
<<"mam">>;
|
2017-01-19 09:12:27 +01:00
|
|
|
_ ->
|
|
|
|
Var
|
|
|
|
end,
|
|
|
|
F#xdata_field{var = NewVar}
|
|
|
|
end, Fs).
|
|
|
|
|
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} |
|
2016-09-08 16:08:48 +02:00
|
|
|
{error, stanza_error()}.
|
2016-07-25 12:50:30 +02:00
|
|
|
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 ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Owner privileges required"),
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, xmpp:err_forbidden(ErrText, Lang)};
|
|
|
|
Destroy /= undefined, Config == undefined, Items == [] ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Destroyed MUC room ~ts by the owner ~ts",
|
2017-02-26 08:07:12 +01:00
|
|
|
[jid:encode(StateData#state.jid), jid:encode(From)]),
|
2016-07-25 12:50:30 +02:00
|
|
|
add_to_log(room_existence, destroyed, StateData),
|
|
|
|
destroy_room(Destroy, StateData);
|
|
|
|
Config /= undefined, Destroy == undefined, Items == [] ->
|
|
|
|
case Config of
|
|
|
|
#xdata{type = cancel} ->
|
|
|
|
{result, undefined};
|
2016-10-17 12:37:23 +02:00
|
|
|
#xdata{type = submit, fields = Fs} ->
|
2017-01-19 09:12:27 +01:00
|
|
|
Fs1 = convert_legacy_fields(Fs),
|
|
|
|
try muc_roomconfig:decode(Fs1) of
|
2016-10-17 12:37:23 +02:00
|
|
|
Options ->
|
|
|
|
case is_allowed_log_change(Options, StateData, From) andalso
|
|
|
|
is_allowed_persistent_change(Options, StateData, From) andalso
|
2019-01-02 17:35:01 +01:00
|
|
|
is_allowed_mam_change(Options, StateData, From) andalso
|
2021-01-21 16:58:05 +01:00
|
|
|
is_allowed_string_limits(Options, StateData) andalso
|
2016-10-17 12:37:23 +02:00
|
|
|
is_password_settings_correct(Options, StateData) of
|
|
|
|
true ->
|
|
|
|
set_config(Options, StateData, Lang);
|
|
|
|
false ->
|
|
|
|
{error, xmpp:err_not_acceptable()}
|
|
|
|
end
|
|
|
|
catch _:{muc_roomconfig, Why} ->
|
|
|
|
Txt = muc_roomconfig:format_error(Why),
|
|
|
|
{error, xmpp:err_bad_request(Txt, Lang)}
|
2016-07-25 12:50:30 +02:00
|
|
|
end;
|
|
|
|
_ ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Incorrect data form"),
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, xmpp:err_bad_request(Txt, Lang)}
|
|
|
|
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 ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Owner privileges required"),
|
2016-07-25 12:50:30 +02:00
|
|
|
{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}] ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("No 'affiliation' attribute found"),
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, xmpp:err_bad_request(Txt, Lang)};
|
|
|
|
[#muc_item{affiliation = Affiliation}] ->
|
|
|
|
Items = items_with_affiliation(Affiliation, StateData),
|
|
|
|
{result, #muc_owner{items = Items}};
|
|
|
|
[_|_] ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Too many <item/> elements"),
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, xmpp:err_bad_request(Txt, Lang)}
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
{error, xmpp:err_bad_request()}
|
2019-07-15 12:59:41 +02:00
|
|
|
end.
|
2003-03-25 22:03:35 +01:00
|
|
|
|
2016-10-17 12:37:23 +02:00
|
|
|
-spec is_allowed_log_change(muc_roomconfig:result(), state(), jid()) -> boolean().
|
|
|
|
is_allowed_log_change(Options, StateData, From) ->
|
|
|
|
case proplists:is_defined(enablelogging, Options) of
|
2016-07-25 12:50:30 +02:00
|
|
|
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-10-17 12:37:23 +02:00
|
|
|
-spec is_allowed_persistent_change(muc_roomconfig:result(), state(), jid()) -> boolean().
|
|
|
|
is_allowed_persistent_change(Options, StateData, From) ->
|
|
|
|
case proplists:is_defined(persistentroom, Options) of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> true;
|
|
|
|
true ->
|
|
|
|
{_AccessRoute, _AccessCreate, _AccessAdmin,
|
2019-01-02 17:35:01 +01:00
|
|
|
AccessPersistent, _AccessMam} =
|
2013-03-14 10:33:02 +01:00
|
|
|
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
|
|
|
|
2019-01-02 17:35:01 +01:00
|
|
|
-spec is_allowed_mam_change(muc_roomconfig:result(), state(), jid()) -> boolean().
|
|
|
|
is_allowed_mam_change(Options, StateData, From) ->
|
|
|
|
case proplists:is_defined(mam, Options) of
|
|
|
|
false -> true;
|
|
|
|
true ->
|
|
|
|
{_AccessRoute, _AccessCreate, _AccessAdmin,
|
|
|
|
_AccessPersistent, AccessMam} =
|
|
|
|
StateData#state.access,
|
|
|
|
allow ==
|
|
|
|
acl:match_rule(StateData#state.server_host,
|
|
|
|
AccessMam, From)
|
|
|
|
end.
|
|
|
|
|
2021-01-21 16:58:05 +01:00
|
|
|
%% Check if the string fields defined in the Data Form
|
2009-04-22 14:05:10 +02:00
|
|
|
%% are conformant to the configured limits
|
2021-01-21 16:58:05 +01:00
|
|
|
-spec is_allowed_string_limits(muc_roomconfig:result(), state()) -> boolean().
|
|
|
|
is_allowed_string_limits(Options, StateData) ->
|
2016-10-17 12:37:23 +02:00
|
|
|
RoomName = proplists:get_value(roomname, Options, <<"">>),
|
|
|
|
RoomDesc = proplists:get_value(roomdesc, Options, <<"">>),
|
2021-01-21 16:58:05 +01:00
|
|
|
Password = proplists:get_value(roomsecret, Options, <<"">>),
|
|
|
|
CaptchaWhitelist = proplists:get_value(captcha_whitelist, Options, []),
|
|
|
|
CaptchaWhitelistSize = lists:foldl(
|
|
|
|
fun(Jid, Sum) -> byte_size(jid:encode(Jid)) + Sum end,
|
|
|
|
0, CaptchaWhitelist),
|
2019-06-14 11:33:26 +02:00
|
|
|
MaxRoomName = mod_muc_opt:max_room_name(StateData#state.server_host),
|
|
|
|
MaxRoomDesc = mod_muc_opt:max_room_desc(StateData#state.server_host),
|
2021-01-21 16:58:05 +01:00
|
|
|
MaxPassword = mod_muc_opt:max_password(StateData#state.server_host),
|
|
|
|
MaxCaptchaWhitelist = mod_muc_opt:max_captcha_whitelist(StateData#state.server_host),
|
2016-10-17 12:37:23 +02:00
|
|
|
(byte_size(RoomName) =< MaxRoomName)
|
2021-01-21 16:58:05 +01:00
|
|
|
andalso (byte_size(RoomDesc) =< MaxRoomDesc)
|
|
|
|
andalso (byte_size(Password) =< MaxPassword)
|
|
|
|
andalso (CaptchaWhitelistSize =< MaxCaptchaWhitelist).
|
2009-04-22 14:05:10 +02:00
|
|
|
|
2009-08-15 22:09:05 +02:00
|
|
|
%% Return false if:
|
|
|
|
%% "the password for a password-protected room is blank"
|
2016-10-17 12:37:23 +02:00
|
|
|
-spec is_password_settings_correct(muc_roomconfig:result(), state()) -> boolean().
|
|
|
|
is_password_settings_correct(Options, StateData) ->
|
2009-08-15 22:09:05 +02:00
|
|
|
Config = StateData#state.config,
|
|
|
|
OldProtected = Config#config.password_protected,
|
|
|
|
OldPassword = Config#config.password,
|
2016-10-17 12:37:23 +02:00
|
|
|
NewProtected = proplists:get_value(passwordprotectedroom, Options),
|
|
|
|
NewPassword = proplists:get_value(roomsecret, Options),
|
|
|
|
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
|
|
|
-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 =
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_muc_opt:default_room_options(RoomState#state.server_host),
|
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) ->
|
2019-01-02 17:35:01 +01:00
|
|
|
{_AccessRoute, _AccessCreate, _AccessAdmin, AccessPersistent, _AccessMam} =
|
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,
|
2017-02-18 07:36:27 +01:00
|
|
|
MaxUsersRoom = get_max_users(StateData),
|
2020-11-09 12:20:23 +01:00
|
|
|
Title = str:translate_and_format(
|
|
|
|
Lang, ?T("Configuration of room ~s"),
|
2017-02-26 08:07:12 +01:00
|
|
|
[jid:encode(StateData#state.jid)]),
|
2016-10-07 09:31:03 +02:00
|
|
|
Fs = [{roomname, Config#config.title},
|
2018-05-30 07:11:58 +02:00
|
|
|
{roomdesc, Config#config.description},
|
|
|
|
{lang, Config#config.lang}] ++
|
2016-07-25 12:50:30 +02:00
|
|
|
case acl:match_rule(StateData#state.server_host, AccessPersistent, From) of
|
2016-10-07 09:31:03 +02:00
|
|
|
allow -> [{persistentroom, Config#config.persistent}];
|
2016-07-25 12:50:30 +02:00
|
|
|
deny -> []
|
|
|
|
end ++
|
2016-10-07 09:31:03 +02:00
|
|
|
[{publicroom, Config#config.public},
|
|
|
|
{public_list, Config#config.public_list},
|
|
|
|
{passwordprotectedroom, Config#config.password_protected},
|
|
|
|
{roomsecret, case Config#config.password_protected of
|
|
|
|
true -> Config#config.password;
|
|
|
|
false -> <<"">>
|
|
|
|
end},
|
2017-02-18 07:36:27 +01:00
|
|
|
{maxusers, MaxUsersRoom,
|
2016-10-07 09:31:03 +02:00
|
|
|
[if is_integer(ServiceMaxUsers) -> [];
|
2019-06-22 16:08:45 +02:00
|
|
|
true -> [{?T("No limit"), <<"none">>}]
|
2016-10-07 09:31:03 +02:00
|
|
|
end] ++ [{integer_to_binary(N), N}
|
|
|
|
|| N <- lists:usort([ServiceMaxUsers,
|
|
|
|
DefaultRoomMaxUsers,
|
2017-02-18 07:36:27 +01:00
|
|
|
MaxUsersRoom
|
2016-10-07 09:31:03 +02:00
|
|
|
| ?MAX_USERS_DEFAULT_LIST]),
|
|
|
|
N =< ServiceMaxUsers]},
|
|
|
|
{whois, if Config#config.anonymous -> moderators;
|
|
|
|
true -> anyone
|
|
|
|
end},
|
|
|
|
{presencebroadcast, Config#config.presence_broadcast},
|
|
|
|
{membersonly, Config#config.members_only},
|
|
|
|
{moderatedroom, Config#config.moderated},
|
|
|
|
{members_by_default, Config#config.members_by_default},
|
|
|
|
{changesubject, Config#config.allow_change_subj},
|
2023-08-08 13:28:02 +02:00
|
|
|
{allowpm, Config#config.allowpm},
|
2016-10-07 09:31:03 +02:00
|
|
|
{allow_private_messages_from_visitors,
|
|
|
|
Config#config.allow_private_messages_from_visitors},
|
|
|
|
{allow_query_users, Config#config.allow_query_users},
|
|
|
|
{allowinvites, Config#config.allow_user_invites},
|
|
|
|
{allow_visitor_status, Config#config.allow_visitor_status},
|
|
|
|
{allow_visitor_nickchange, Config#config.allow_visitor_nickchange},
|
|
|
|
{allow_voice_requests, Config#config.allow_voice_requests},
|
|
|
|
{allow_subscription, Config#config.allow_subscription},
|
2017-11-27 11:07:10 +01:00
|
|
|
{voice_request_min_interval, Config#config.voice_request_min_interval},
|
2021-10-29 04:12:26 +02:00
|
|
|
{pubsub, Config#config.pubsub},
|
|
|
|
{enable_hats, Config#config.enable_hats}]
|
2016-07-25 12:50:30 +02:00
|
|
|
++
|
|
|
|
case ejabberd_captcha:is_feature_available() of
|
2019-07-31 11:35:06 +02:00
|
|
|
true ->
|
|
|
|
[{captcha_protected, Config#config.captcha_protected},
|
|
|
|
{captcha_whitelist,
|
|
|
|
lists:map(
|
|
|
|
fun jid:make/1,
|
|
|
|
?SETS:to_list(Config#config.captcha_whitelist))}];
|
|
|
|
false ->
|
|
|
|
[]
|
|
|
|
end
|
2016-07-25 12:50:30 +02:00
|
|
|
++
|
|
|
|
case mod_muc_log:check_access_log(StateData#state.server_host, From) of
|
2016-10-07 09:31:03 +02:00
|
|
|
allow -> [{enablelogging, Config#config.logging}];
|
2016-07-25 12:50:30 +02:00
|
|
|
deny -> []
|
|
|
|
end,
|
|
|
|
Fields = ejabberd_hooks:run_fold(get_room_config,
|
|
|
|
StateData#state.server_host,
|
|
|
|
Fs,
|
|
|
|
[StateData, From, Lang]),
|
2016-10-07 09:31:03 +02:00
|
|
|
#xdata{type = form, title = Title,
|
2017-03-20 07:57:11 +01:00
|
|
|
fields = muc_roomconfig:encode(Fields, Lang)}.
|
2016-07-25 12:50:30 +02:00
|
|
|
|
2016-10-17 12:37:23 +02:00
|
|
|
-spec set_config(muc_roomconfig:result(), state(), binary()) ->
|
|
|
|
{error, stanza_error()} | {result, undefined, state()}.
|
|
|
|
set_config(Options, StateData, Lang) ->
|
2016-10-07 09:31:03 +02:00
|
|
|
try
|
|
|
|
#config{} = Config = set_config(Options, StateData#state.config,
|
|
|
|
StateData#state.server_host, Lang),
|
|
|
|
{result, _, NSD} = Res = change_config(Config, StateData),
|
|
|
|
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}
|
2018-11-15 12:13:45 +01:00
|
|
|
|| U <- maps:values(StateData#state.users)],
|
2016-10-07 09:31:03 +02:00
|
|
|
add_to_log(Type, Users, NSD),
|
|
|
|
Res
|
2016-10-17 12:37:23 +02:00
|
|
|
catch _:{badmatch, {error, #stanza_error{}} = Err} ->
|
2016-10-07 09:31:03 +02:00
|
|
|
Err
|
2003-03-25 22:03:35 +01:00
|
|
|
end.
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec get_config_opt_name(pos_integer()) -> atom().
|
2016-08-09 09:56:32 +02:00
|
|
|
get_config_opt_name(Pos) ->
|
|
|
|
Fs = [config|record_info(fields, config)],
|
|
|
|
lists:nth(Pos, Fs).
|
|
|
|
|
2016-10-07 09:31:03 +02:00
|
|
|
-spec set_config([muc_roomconfig:property()], #config{},
|
2016-09-08 16:08:48 +02:00
|
|
|
binary(), binary()) -> #config{} | {error, stanza_error()}.
|
2016-10-07 09:31:03 +02:00
|
|
|
set_config(Opts, Config, ServerHost, Lang) ->
|
|
|
|
lists:foldl(
|
|
|
|
fun(_, {error, _} = Err) -> Err;
|
|
|
|
({roomname, Title}, C) -> C#config{title = Title};
|
|
|
|
({roomdesc, Desc}, C) -> C#config{description = Desc};
|
|
|
|
({changesubject, V}, C) -> C#config{allow_change_subj = V};
|
|
|
|
({allow_query_users, V}, C) -> C#config{allow_query_users = V};
|
2023-08-08 13:28:02 +02:00
|
|
|
({allowpm, V}, C) ->
|
|
|
|
C#config{allowpm = V};
|
2016-10-07 09:31:03 +02:00
|
|
|
({allow_private_messages_from_visitors, V}, C) ->
|
|
|
|
C#config{allow_private_messages_from_visitors = V};
|
|
|
|
({allow_visitor_status, V}, C) -> C#config{allow_visitor_status = V};
|
|
|
|
({allow_visitor_nickchange, V}, C) ->
|
|
|
|
C#config{allow_visitor_nickchange = V};
|
|
|
|
({publicroom, V}, C) -> C#config{public = V};
|
|
|
|
({public_list, V}, C) -> C#config{public_list = V};
|
|
|
|
({persistentroom, V}, C) -> C#config{persistent = V};
|
|
|
|
({moderatedroom, V}, C) -> C#config{moderated = V};
|
|
|
|
({members_by_default, V}, C) -> C#config{members_by_default = V};
|
|
|
|
({membersonly, V}, C) -> C#config{members_only = V};
|
|
|
|
({captcha_protected, V}, C) -> C#config{captcha_protected = V};
|
|
|
|
({allowinvites, V}, C) -> C#config{allow_user_invites = V};
|
|
|
|
({allow_subscription, V}, C) -> C#config{allow_subscription = V};
|
|
|
|
({passwordprotectedroom, V}, C) -> C#config{password_protected = V};
|
|
|
|
({roomsecret, V}, C) -> C#config{password = V};
|
|
|
|
({anonymous, V}, C) -> C#config{anonymous = V};
|
|
|
|
({presencebroadcast, V}, C) -> C#config{presence_broadcast = V};
|
|
|
|
({allow_voice_requests, V}, C) -> C#config{allow_voice_requests = V};
|
|
|
|
({voice_request_min_interval, V}, C) ->
|
|
|
|
C#config{voice_request_min_interval = V};
|
|
|
|
({whois, moderators}, C) -> C#config{anonymous = true};
|
|
|
|
({whois, anyone}, C) -> C#config{anonymous = false};
|
|
|
|
({maxusers, V}, C) -> C#config{max_users = V};
|
|
|
|
({enablelogging, V}, C) -> C#config{logging = V};
|
2017-11-27 11:07:10 +01:00
|
|
|
({pubsub, V}, C) -> C#config{pubsub = V};
|
2021-10-29 04:12:26 +02:00
|
|
|
({enable_hats, V}, C) -> C#config{enable_hats = V};
|
2018-05-30 07:11:58 +02:00
|
|
|
({lang, L}, C) -> C#config{lang = L};
|
2016-10-07 09:31:03 +02:00
|
|
|
({captcha_whitelist, Js}, C) ->
|
|
|
|
LJIDs = [jid:tolower(J) || J <- Js],
|
|
|
|
C#config{captcha_whitelist = ?SETS:from_list(LJIDs)};
|
|
|
|
({O, V} = Opt, C) ->
|
|
|
|
case ejabberd_hooks:run_fold(set_room_option,
|
|
|
|
ServerHost,
|
|
|
|
{0, undefined},
|
|
|
|
[Opt, Lang]) of
|
|
|
|
{0, undefined} ->
|
|
|
|
?ERROR_MSG("set_room_option hook failed for "
|
2019-09-23 14:17:20 +02:00
|
|
|
"option '~ts' with value ~p", [O, V]),
|
2020-01-22 12:52:30 +01:00
|
|
|
Txt = {?T("Failed to process option '~s'"), [O]},
|
2016-10-07 09:31:03 +02:00
|
|
|
{error, xmpp:err_internal_server_error(Txt, Lang)};
|
|
|
|
{Pos, Val} ->
|
|
|
|
setelement(Pos, C, Val)
|
|
|
|
end
|
|
|
|
end, Config, Opts).
|
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),
|
2018-04-23 10:35:43 +02:00
|
|
|
StateData0 = StateData#state{config = Config},
|
|
|
|
StateData1 = remove_subscriptions(StateData0),
|
|
|
|
StateData2 =
|
|
|
|
case {(StateData#state.config)#config.persistent,
|
|
|
|
Config#config.persistent} of
|
|
|
|
{WasPersistent, true} ->
|
|
|
|
if not WasPersistent ->
|
|
|
|
set_affiliations(StateData1#state.affiliations,
|
|
|
|
StateData1);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
store_room(StateData1),
|
|
|
|
StateData1;
|
2019-04-30 17:34:49 +02:00
|
|
|
{true, false} ->
|
|
|
|
Affiliations = get_affiliations(StateData),
|
|
|
|
maybe_forget_room(StateData),
|
|
|
|
StateData1#state{affiliations = Affiliations};
|
|
|
|
_ ->
|
|
|
|
StateData1
|
2018-04-23 10:35:43 +02:00
|
|
|
end,
|
2006-02-06 06:12:54 +01:00
|
|
|
case {(StateData#state.config)#config.members_only,
|
2018-04-23 10:35:43 +02:00
|
|
|
Config#config.members_only} of
|
|
|
|
{false, true} ->
|
|
|
|
StateData3 = remove_nonmembers(StateData2),
|
|
|
|
{result, undefined, StateData3};
|
|
|
|
_ ->
|
|
|
|
{result, undefined, StateData2}
|
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-10-17 12:37:23 +02:00
|
|
|
if Codes /= [] ->
|
2018-11-15 12:13:45 +01:00
|
|
|
maps:fold(
|
|
|
|
fun(_LJID, #user{jid = JID}, _) ->
|
2018-10-01 22:03:11 +02:00
|
|
|
advertise_entity_capabilities(JID, StateData#state{config = New})
|
2018-11-15 12:13:45 +01:00
|
|
|
end, ok, StateData#state.users),
|
2016-10-17 12:37:23 +02:00
|
|
|
Message = #message{type = groupchat,
|
2018-07-05 10:51:49 +02:00
|
|
|
id = p1_rand:get_string(),
|
2016-10-17 12:37:23 +02:00
|
|
|
sub_els = [#muc_user{status_codes = Codes}]},
|
|
|
|
send_wrapped_multiple(StateData#state.jid,
|
2021-09-13 07:15:11 +02:00
|
|
|
get_users_and_subscribers_with_node(
|
|
|
|
?NS_MUCSUB_NODES_CONFIG, StateData),
|
2016-10-17 12:37:23 +02:00
|
|
|
Message,
|
|
|
|
?NS_MUCSUB_NODES_CONFIG,
|
|
|
|
StateData);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end.
|
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) ->
|
2018-11-15 12:13:45 +01:00
|
|
|
maps:fold(
|
|
|
|
fun(_LJID, #user{jid = JID}, SD) ->
|
|
|
|
Affiliation = get_affiliation(JID, SD),
|
|
|
|
case Affiliation of
|
|
|
|
none ->
|
|
|
|
catch send_kickban_presence(undefined, JID, <<"">>, 322, SD),
|
|
|
|
set_role(JID, none, SD);
|
|
|
|
_ -> SD
|
|
|
|
end
|
|
|
|
end, StateData, get_users_and_subscribers(StateData)).
|
2003-03-25 22:03:35 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec set_opts([{atom(), any()}], state()) -> state().
|
2023-02-20 13:42:52 +01:00
|
|
|
set_opts(Opts, StateData) ->
|
|
|
|
case lists:keytake(persistent, 1, Opts) of
|
|
|
|
false ->
|
|
|
|
set_opts2(Opts, StateData);
|
|
|
|
{value, Tuple, Rest} ->
|
|
|
|
set_opts2([Tuple | Rest], StateData)
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec set_opts2([{atom(), any()}], state()) -> state().
|
|
|
|
set_opts2([], StateData) ->
|
2018-02-12 15:37:36 +01:00
|
|
|
set_vcard_xupdate(StateData);
|
2023-02-20 13:42:52 +01:00
|
|
|
set_opts2([{vcard, Val} | Opts], StateData)
|
2022-04-29 17:30:14 +02:00
|
|
|
when is_record(Val, vcard_temp) ->
|
|
|
|
%% default_room_options is setting a default room vcard
|
|
|
|
ValRaw = fxml:element_to_binary(xmpp:encode(Val)),
|
2023-02-20 13:42:52 +01:00
|
|
|
set_opts2([{vcard, ValRaw} | Opts], StateData);
|
|
|
|
set_opts2([{Opt, Val} | Opts], StateData) ->
|
2003-03-25 22:03:35 +01:00
|
|
|
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}};
|
2023-08-08 13:28:02 +02:00
|
|
|
allowpm ->
|
2013-03-14 10:33:02 +01:00
|
|
|
StateData#state{config =
|
2023-08-08 13:28:02 +02:00
|
|
|
(StateData#state.config)#config{allowpm
|
2013-03-14 10:33:02 +01:00
|
|
|
= 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}};
|
2018-02-12 15:37:36 +01:00
|
|
|
vcard_xupdate ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{vcard_xupdate =
|
|
|
|
Val}};
|
2017-11-27 11:07:10 +01:00
|
|
|
pubsub ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{pubsub = Val}};
|
2016-06-26 08:08:37 +02:00
|
|
|
allow_subscription ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{allow_subscription = Val}};
|
2021-10-29 04:12:26 +02:00
|
|
|
enable_hats ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{enable_hats = Val}};
|
2018-05-30 07:11:58 +02:00
|
|
|
lang ->
|
|
|
|
StateData#state{config =
|
|
|
|
(StateData#state.config)#config{lang = Val}};
|
2016-06-26 08:08:37 +02:00
|
|
|
subscribers ->
|
2021-09-13 07:15:11 +02:00
|
|
|
MUCSubscribers =
|
|
|
|
lists:foldl(
|
|
|
|
fun({JID, Nick, Nodes}, MUCSubs) ->
|
|
|
|
BareJID =
|
|
|
|
case JID of
|
|
|
|
#jid{} -> jid:remove_resource(JID);
|
|
|
|
_ ->
|
|
|
|
?ERROR_MSG("Invalid subscriber JID in set_opts ~p", [JID]),
|
|
|
|
jid:remove_resource(jid:make(JID))
|
|
|
|
end,
|
|
|
|
muc_subscribers_put(
|
|
|
|
#subscriber{jid = BareJID,
|
|
|
|
nick = Nick,
|
|
|
|
nodes = Nodes},
|
|
|
|
MUCSubs)
|
|
|
|
end, muc_subscribers_new(), Val),
|
|
|
|
StateData#state{muc_subscribers = MUCSubscribers};
|
2013-03-14 10:33:02 +01:00
|
|
|
affiliations ->
|
2023-02-20 13:03:46 +01:00
|
|
|
set_affiliations(maps:from_list(Val), StateData);
|
2021-10-01 12:11:51 +02:00
|
|
|
roles ->
|
|
|
|
StateData#state{roles = maps:from_list(Val)};
|
2017-11-13 09:25:35 +01:00
|
|
|
subject ->
|
|
|
|
Subj = if Val == <<"">> -> [];
|
|
|
|
is_binary(Val) -> [#text{data = Val}];
|
|
|
|
is_list(Val) -> Val
|
|
|
|
end,
|
|
|
|
StateData#state{subject = Subj};
|
2023-09-04 11:17:54 +02:00
|
|
|
subject_author when is_tuple(Val) ->
|
|
|
|
StateData#state{subject_author = Val};
|
|
|
|
subject_author when is_binary(Val) -> % ejabberd 23.04 or older
|
|
|
|
StateData#state{subject_author = {Val, #jid{}}};
|
2021-10-29 04:12:26 +02:00
|
|
|
hats_users ->
|
|
|
|
Hats = maps:from_list(
|
|
|
|
lists:map(fun({U, H}) -> {U, maps:from_list(H)} end,
|
|
|
|
Val)),
|
|
|
|
StateData#state{hats_users = Hats};
|
2022-11-18 12:20:15 +01:00
|
|
|
hibernation_time -> StateData;
|
2021-10-01 12:11:51 +02:00
|
|
|
Other ->
|
|
|
|
?INFO_MSG("Unknown MUC room option, will be discarded: ~p", [Other]),
|
|
|
|
StateData
|
2003-03-25 22:03:35 +01:00
|
|
|
end,
|
2023-02-20 13:42:52 +01:00
|
|
|
set_opts2(Opts, NSD).
|
2003-03-25 22:03:35 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec set_vcard_xupdate(state()) -> state().
|
2018-02-12 15:37:36 +01:00
|
|
|
set_vcard_xupdate(#state{config =
|
|
|
|
#config{vcard = VCardRaw,
|
|
|
|
vcard_xupdate = undefined} = Config} = State)
|
|
|
|
when VCardRaw /= <<"">> ->
|
|
|
|
case fxml_stream:parse_element(VCardRaw) of
|
|
|
|
{error, _} ->
|
|
|
|
State;
|
|
|
|
El ->
|
|
|
|
Hash = mod_vcard_xupdate:compute_hash(El),
|
|
|
|
State#state{config = Config#config{vcard_xupdate = Hash}}
|
|
|
|
end;
|
|
|
|
set_vcard_xupdate(State) ->
|
|
|
|
State.
|
|
|
|
|
2021-10-01 12:11:51 +02:00
|
|
|
get_occupant_initial_role(Jid, Affiliation, #state{roles = Roles} = StateData) ->
|
|
|
|
DefaultRole = get_default_role(Affiliation, StateData),
|
|
|
|
case (StateData#state.config)#config.moderated of
|
|
|
|
true ->
|
|
|
|
get_occupant_stored_role(Jid, Roles, DefaultRole);
|
|
|
|
false ->
|
|
|
|
DefaultRole
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_occupant_stored_role(Jid, Roles, DefaultRole) ->
|
|
|
|
maps:get(jid:split(jid:remove_resource(Jid)), Roles, DefaultRole).
|
|
|
|
|
2016-08-09 09:56:32 +02:00
|
|
|
-define(MAKE_CONFIG_OPT(Opt),
|
|
|
|
{get_config_opt_name(Opt), element(Opt, Config)}).
|
2003-03-25 22:03:35 +01:00
|
|
|
|
2022-02-18 17:21:22 +01:00
|
|
|
-spec make_opts(state(), boolean()) -> [{atom(), any()}].
|
|
|
|
make_opts(StateData, Hibernation) ->
|
2003-03-25 22:03:35 +01:00
|
|
|
Config = StateData#state.config,
|
2021-09-13 07:15:11 +02:00
|
|
|
Subscribers = muc_subscribers_fold(
|
2016-09-07 09:33:37 +02:00
|
|
|
fun(_LJID, Sub, Acc) ->
|
|
|
|
[{Sub#subscriber.jid,
|
|
|
|
Sub#subscriber.nick,
|
|
|
|
Sub#subscriber.nodes}|Acc]
|
2021-09-13 07:15:11 +02:00
|
|
|
end, [], StateData#state.muc_subscribers),
|
2016-08-09 09:56:32 +02:00
|
|
|
[?MAKE_CONFIG_OPT(#config.title), ?MAKE_CONFIG_OPT(#config.description),
|
|
|
|
?MAKE_CONFIG_OPT(#config.allow_change_subj),
|
|
|
|
?MAKE_CONFIG_OPT(#config.allow_query_users),
|
2023-08-08 13:28:02 +02:00
|
|
|
?MAKE_CONFIG_OPT(#config.allowpm),
|
2016-08-09 09:56:32 +02:00
|
|
|
?MAKE_CONFIG_OPT(#config.allow_private_messages_from_visitors),
|
|
|
|
?MAKE_CONFIG_OPT(#config.allow_visitor_status),
|
|
|
|
?MAKE_CONFIG_OPT(#config.allow_visitor_nickchange),
|
|
|
|
?MAKE_CONFIG_OPT(#config.public), ?MAKE_CONFIG_OPT(#config.public_list),
|
|
|
|
?MAKE_CONFIG_OPT(#config.persistent),
|
|
|
|
?MAKE_CONFIG_OPT(#config.moderated),
|
|
|
|
?MAKE_CONFIG_OPT(#config.members_by_default),
|
|
|
|
?MAKE_CONFIG_OPT(#config.members_only),
|
|
|
|
?MAKE_CONFIG_OPT(#config.allow_user_invites),
|
|
|
|
?MAKE_CONFIG_OPT(#config.password_protected),
|
|
|
|
?MAKE_CONFIG_OPT(#config.captcha_protected),
|
|
|
|
?MAKE_CONFIG_OPT(#config.password), ?MAKE_CONFIG_OPT(#config.anonymous),
|
|
|
|
?MAKE_CONFIG_OPT(#config.logging), ?MAKE_CONFIG_OPT(#config.max_users),
|
|
|
|
?MAKE_CONFIG_OPT(#config.allow_voice_requests),
|
2016-11-12 11:27:15 +01:00
|
|
|
?MAKE_CONFIG_OPT(#config.allow_subscription),
|
2016-08-09 09:56:32 +02:00
|
|
|
?MAKE_CONFIG_OPT(#config.mam),
|
2016-11-16 13:35:50 +01:00
|
|
|
?MAKE_CONFIG_OPT(#config.presence_broadcast),
|
2016-08-09 09:56:32 +02:00
|
|
|
?MAKE_CONFIG_OPT(#config.voice_request_min_interval),
|
|
|
|
?MAKE_CONFIG_OPT(#config.vcard),
|
2018-02-12 15:37:36 +01:00
|
|
|
?MAKE_CONFIG_OPT(#config.vcard_xupdate),
|
2017-11-27 11:07:10 +01:00
|
|
|
?MAKE_CONFIG_OPT(#config.pubsub),
|
2021-10-29 04:12:26 +02:00
|
|
|
?MAKE_CONFIG_OPT(#config.enable_hats),
|
2018-05-30 07:11:58 +02:00
|
|
|
?MAKE_CONFIG_OPT(#config.lang),
|
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,
|
2018-11-15 12:13:45 +01:00
|
|
|
maps:to_list(StateData#state.affiliations)},
|
2021-10-01 12:11:51 +02:00
|
|
|
{roles, maps:to_list(StateData#state.roles)},
|
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},
|
2021-10-29 04:12:26 +02:00
|
|
|
{hats_users,
|
|
|
|
lists:map(fun({U, H}) -> {U, maps:to_list(H)} end,
|
|
|
|
maps:to_list(StateData#state.hats_users))},
|
2022-02-18 17:21:22 +01:00
|
|
|
{hibernation_time, if Hibernation -> erlang:system_time(microsecond); true -> undefined end},
|
2016-06-26 08:08:37 +02:00
|
|
|
{subscribers, Subscribers}].
|
2003-03-27 21:55:09 +01:00
|
|
|
|
2018-02-16 06:28:33 +01:00
|
|
|
expand_opts(CompactOpts) ->
|
|
|
|
DefConfig = #config{},
|
|
|
|
Fields = record_info(fields, config),
|
|
|
|
{_, Opts1} =
|
|
|
|
lists:foldl(
|
|
|
|
fun(Field, {Pos, Opts}) ->
|
|
|
|
case lists:keyfind(Field, 1, CompactOpts) of
|
|
|
|
false ->
|
|
|
|
DefV = element(Pos, DefConfig),
|
|
|
|
DefVal = case (?SETS):is_set(DefV) of
|
|
|
|
true -> (?SETS):to_list(DefV);
|
|
|
|
false -> DefV
|
|
|
|
end,
|
|
|
|
{Pos+1, [{Field, DefVal}|Opts]};
|
|
|
|
{_, Val} ->
|
|
|
|
{Pos+1, [{Field, Val}|Opts]}
|
|
|
|
end
|
|
|
|
end, {2, []}, Fields),
|
2023-08-15 12:44:41 +02:00
|
|
|
SubjectAuthor = proplists:get_value(subject_author, CompactOpts, {<<"">>, #jid{}}),
|
2018-02-16 06:28:33 +01:00
|
|
|
Subject = proplists:get_value(subject, CompactOpts, <<"">>),
|
|
|
|
Subscribers = proplists:get_value(subscribers, CompactOpts, []),
|
2020-11-16 15:25:01 +01:00
|
|
|
HibernationTime = proplists:get_value(hibernation_time, CompactOpts, 0),
|
2018-02-16 06:28:33 +01:00
|
|
|
[{subject, Subject},
|
|
|
|
{subject_author, SubjectAuthor},
|
2020-11-16 15:25:01 +01:00
|
|
|
{subscribers, Subscribers},
|
|
|
|
{hibernation_time, HibernationTime}
|
2018-02-16 06:28:33 +01:00
|
|
|
| lists:reverse(Opts1)].
|
|
|
|
|
|
|
|
config_fields() ->
|
2020-11-16 15:25:01 +01:00
|
|
|
[subject, subject_author, subscribers, hibernate_time | record_info(fields, config)].
|
2018-02-16 06:28:33 +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},
|
2018-11-15 12:13:45 +01:00
|
|
|
maps:fold(
|
|
|
|
fun(_LJID, Info, _) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
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)
|
2021-09-13 07:15:11 +02:00
|
|
|
end, ok, get_users_and_subscribers_with_node(
|
|
|
|
?NS_MUCSUB_NODES_CONFIG, StateData)),
|
2019-07-16 13:57:48 +02:00
|
|
|
forget_room(StateData),
|
2016-07-25 12:50:30 +02:00
|
|
|
{result, undefined, stop}.
|
2003-03-27 21:55:09 +01:00
|
|
|
|
2019-07-16 13:57:48 +02:00
|
|
|
-spec forget_room(state()) -> state().
|
|
|
|
forget_room(StateData) ->
|
|
|
|
mod_muc:forget_room(StateData#state.server_host,
|
|
|
|
StateData#state.host,
|
|
|
|
StateData#state.room),
|
|
|
|
StateData.
|
|
|
|
|
2019-07-09 14:21:17 +02:00
|
|
|
-spec maybe_forget_room(state()) -> state().
|
2019-04-30 13:41:03 +02:00
|
|
|
maybe_forget_room(StateData) ->
|
|
|
|
Forget = case (StateData#state.config)#config.persistent of
|
|
|
|
true ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
Mod = gen_mod:db_mod(StateData#state.server_host, mod_muc),
|
|
|
|
erlang:function_exported(Mod, get_subscribed_rooms, 3)
|
|
|
|
end,
|
|
|
|
case Forget of
|
|
|
|
true ->
|
2019-07-16 13:57:48 +02:00
|
|
|
forget_room(StateData);
|
2019-04-30 13:41:03 +02:00
|
|
|
_ ->
|
|
|
|
StateData
|
|
|
|
end.
|
|
|
|
|
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
|
|
|
|
2018-02-12 15:37:36 +01:00
|
|
|
-spec make_disco_info(jid(), state()) -> disco_info().
|
|
|
|
make_disco_info(_From, StateData) ->
|
2004-01-17 21:26:57 +01:00
|
|
|
Config = StateData#state.config,
|
2018-11-29 08:35:03 +01:00
|
|
|
Feats = [?NS_VCARD, ?NS_MUC, ?NS_DISCO_INFO, ?NS_DISCO_ITEMS,
|
2023-04-14 12:19:38 +02:00
|
|
|
?NS_COMMANDS, ?NS_MESSAGE_MODERATE, ?NS_MESSAGE_RETRACT,
|
2016-07-25 12:50:30 +02:00
|
|
|
?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
|
2023-08-02 13:42:09 +02:00
|
|
|
++ case gen_mod:is_loaded(StateData#state.server_host, mod_muc_occupantid) of
|
|
|
|
true ->
|
|
|
|
[?NS_OCCUPANT_ID];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end
|
2016-07-25 12:50:30 +02:00
|
|
|
++ case {gen_mod:is_loaded(StateData#state.server_host, mod_mam),
|
|
|
|
Config#config.mam} of
|
|
|
|
{true, true} ->
|
2017-11-15 00:18:25 +01:00
|
|
|
[?NS_MAM_TMP, ?NS_MAM_0, ?NS_MAM_1, ?NS_MAM_2, ?NS_SID_0];
|
2016-07-25 12:50:30 +02:00
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
2018-02-12 15:37:36 +01:00
|
|
|
#disco_info{identities = [#identity{category = <<"conference">>,
|
|
|
|
type = <<"text">>,
|
2020-08-28 12:01:09 +02:00
|
|
|
name = (StateData#state.config)#config.title}],
|
2018-02-12 15:37:36 +01:00
|
|
|
features = Feats}.
|
|
|
|
|
|
|
|
-spec process_iq_disco_info(jid(), iq(), state()) ->
|
|
|
|
{result, disco_info()} | {error, stanza_error()}.
|
|
|
|
process_iq_disco_info(_From, #iq{type = set, lang = Lang}, _StateData) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Value 'set' of 'type' attribute is not allowed"),
|
2018-02-12 15:37:36 +01:00
|
|
|
{error, xmpp:err_not_allowed(Txt, Lang)};
|
|
|
|
process_iq_disco_info(From, #iq{type = get, lang = Lang,
|
|
|
|
sub_els = [#disco_info{node = <<>>}]},
|
|
|
|
StateData) ->
|
|
|
|
DiscoInfo = make_disco_info(From, StateData),
|
2018-06-01 08:07:16 +02:00
|
|
|
Extras = iq_disco_info_extras(Lang, StateData, false),
|
2018-02-12 15:37:36 +01:00
|
|
|
{result, DiscoInfo#disco_info{xdata = [Extras]}};
|
2021-10-29 04:12:26 +02:00
|
|
|
process_iq_disco_info(From, #iq{type = get, lang = Lang,
|
|
|
|
sub_els = [#disco_info{node = ?NS_COMMANDS}]},
|
|
|
|
StateData) ->
|
|
|
|
case (StateData#state.config)#config.enable_hats andalso
|
|
|
|
is_admin(From, StateData)
|
|
|
|
of
|
|
|
|
true ->
|
|
|
|
{result,
|
|
|
|
#disco_info{
|
|
|
|
identities = [#identity{category = <<"automation">>,
|
|
|
|
type = <<"command-list">>,
|
|
|
|
name = translate:translate(
|
|
|
|
Lang, ?T("Commands"))}]}};
|
|
|
|
false ->
|
|
|
|
Txt = ?T("Node not found"),
|
|
|
|
{error, xmpp:err_item_not_found(Txt, Lang)}
|
|
|
|
end;
|
|
|
|
process_iq_disco_info(From, #iq{type = get, lang = Lang,
|
|
|
|
sub_els = [#disco_info{node = ?MUC_HAT_ADD_CMD}]},
|
|
|
|
StateData) ->
|
|
|
|
case (StateData#state.config)#config.enable_hats andalso
|
|
|
|
is_admin(From, StateData)
|
|
|
|
of
|
|
|
|
true ->
|
|
|
|
{result,
|
|
|
|
#disco_info{
|
|
|
|
identities = [#identity{category = <<"automation">>,
|
|
|
|
type = <<"command-node">>,
|
|
|
|
name = translate:translate(
|
|
|
|
Lang, ?T("Add a hat to a user"))}],
|
|
|
|
features = [?NS_COMMANDS]}};
|
|
|
|
false ->
|
|
|
|
Txt = ?T("Node not found"),
|
|
|
|
{error, xmpp:err_item_not_found(Txt, Lang)}
|
|
|
|
end;
|
|
|
|
process_iq_disco_info(From, #iq{type = get, lang = Lang,
|
|
|
|
sub_els = [#disco_info{node = ?MUC_HAT_REMOVE_CMD}]},
|
|
|
|
StateData) ->
|
|
|
|
case (StateData#state.config)#config.enable_hats andalso
|
|
|
|
is_admin(From, StateData)
|
|
|
|
of
|
|
|
|
true ->
|
|
|
|
{result,
|
|
|
|
#disco_info{
|
|
|
|
identities = [#identity{category = <<"automation">>,
|
|
|
|
type = <<"command-node">>,
|
|
|
|
name = translate:translate(
|
|
|
|
Lang, ?T("Remove a hat from a user"))}],
|
|
|
|
features = [?NS_COMMANDS]}};
|
|
|
|
false ->
|
|
|
|
Txt = ?T("Node not found"),
|
|
|
|
{error, xmpp:err_item_not_found(Txt, Lang)}
|
|
|
|
end;
|
|
|
|
process_iq_disco_info(From, #iq{type = get, lang = Lang,
|
|
|
|
sub_els = [#disco_info{node = ?MUC_HAT_LIST_CMD}]},
|
|
|
|
StateData) ->
|
|
|
|
case (StateData#state.config)#config.enable_hats andalso
|
|
|
|
is_admin(From, StateData)
|
|
|
|
of
|
|
|
|
true ->
|
|
|
|
{result,
|
|
|
|
#disco_info{
|
|
|
|
identities = [#identity{category = <<"automation">>,
|
|
|
|
type = <<"command-node">>,
|
|
|
|
name = translate:translate(
|
|
|
|
Lang, ?T("List users with hats"))}],
|
|
|
|
features = [?NS_COMMANDS]}};
|
|
|
|
false ->
|
|
|
|
Txt = ?T("Node not found"),
|
|
|
|
{error, xmpp:err_item_not_found(Txt, Lang)}
|
|
|
|
end;
|
2018-02-12 15:37:36 +01:00
|
|
|
process_iq_disco_info(From, #iq{type = get, lang = Lang,
|
|
|
|
sub_els = [#disco_info{node = Node}]},
|
|
|
|
StateData) ->
|
|
|
|
try
|
|
|
|
true = mod_caps:is_valid_node(Node),
|
|
|
|
DiscoInfo = make_disco_info(From, StateData),
|
2018-06-01 08:07:16 +02:00
|
|
|
Extras = iq_disco_info_extras(Lang, StateData, true),
|
2018-09-14 00:18:17 +02:00
|
|
|
DiscoInfo1 = DiscoInfo#disco_info{xdata = [Extras]},
|
|
|
|
Hash = mod_caps:compute_disco_hash(DiscoInfo1, sha),
|
|
|
|
Node = <<(ejabberd_config:get_uri())/binary, $#, Hash/binary>>,
|
|
|
|
{result, DiscoInfo1#disco_info{node = Node}}
|
2018-02-12 15:37:36 +01:00
|
|
|
catch _:{badmatch, _} ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Invalid node name"),
|
2018-02-12 15:37:36 +01:00
|
|
|
{error, xmpp:err_item_not_found(Txt, Lang)}
|
|
|
|
end.
|
2016-07-25 12:50:30 +02:00
|
|
|
|
2018-06-01 08:07:16 +02:00
|
|
|
-spec iq_disco_info_extras(binary(), state(), boolean()) -> xdata().
|
|
|
|
iq_disco_info_extras(Lang, StateData, Static) ->
|
2018-06-19 13:02:45 +02:00
|
|
|
Config = StateData#state.config,
|
|
|
|
Fs1 = [{roomname, Config#config.title},
|
|
|
|
{description, Config#config.description},
|
|
|
|
{changesubject, Config#config.allow_change_subj},
|
|
|
|
{allowinvites, Config#config.allow_user_invites},
|
2022-08-04 12:25:53 +02:00
|
|
|
{allow_query_users, Config#config.allow_query_users},
|
2023-08-08 13:28:02 +02:00
|
|
|
{allowpm, Config#config.allowpm},
|
2018-06-19 13:02:45 +02:00
|
|
|
{lang, Config#config.lang}],
|
|
|
|
Fs2 = case Config#config.pubsub of
|
2017-11-27 11:07:10 +01:00
|
|
|
Node when is_binary(Node), Node /= <<"">> ->
|
|
|
|
[{pubsub, Node}|Fs1];
|
|
|
|
_ ->
|
|
|
|
Fs1
|
|
|
|
end,
|
2018-06-01 08:07:16 +02:00
|
|
|
Fs3 = case Static of
|
|
|
|
false ->
|
2018-11-15 12:13:45 +01:00
|
|
|
[{occupants, maps:size(StateData#state.nicks)}|Fs2];
|
2018-06-01 08:07:16 +02:00
|
|
|
true ->
|
|
|
|
Fs2
|
|
|
|
end,
|
2019-09-26 15:53:36 +02:00
|
|
|
Fs4 = case Config#config.logging of
|
|
|
|
true ->
|
|
|
|
case mod_muc_log:get_url(StateData) of
|
|
|
|
{ok, URL} ->
|
|
|
|
[{logs, URL}|Fs3];
|
|
|
|
error ->
|
|
|
|
Fs3
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
Fs3
|
|
|
|
end,
|
2016-07-25 12:50:30 +02:00
|
|
|
#xdata{type = result,
|
2019-09-26 15:53:36 +02:00
|
|
|
fields = muc_roominfo:encode(Fs4, Lang)}.
|
2016-07-25 12:50:30 +02:00
|
|
|
|
|
|
|
-spec process_iq_disco_items(jid(), iq(), state()) ->
|
2016-09-08 16:08:48 +02:00
|
|
|
{error, stanza_error()} | {result, disco_items()}.
|
2016-07-25 12:50:30 +02:00
|
|
|
process_iq_disco_items(_From, #iq{type = set, lang = Lang}, _StateData) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Value 'set' of 'type' attribute is not allowed"),
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, xmpp:err_not_allowed(Txt, Lang)};
|
2019-07-15 13:10:45 +02:00
|
|
|
process_iq_disco_items(From, #iq{type = get, sub_els = [#disco_items{node = <<>>}]},
|
|
|
|
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
|
|
|
_ ->
|
2016-10-17 12:37:23 +02:00
|
|
|
%% If the list of occupants is private,
|
|
|
|
%% the room MUST return an empty <query/> element
|
|
|
|
%% (http://xmpp.org/extensions/xep-0045.html#disco-roomitems)
|
|
|
|
{result, #disco_items{}}
|
2013-03-14 10:33:02 +01:00
|
|
|
end
|
2019-07-15 13:10:45 +02:00
|
|
|
end;
|
2021-10-29 04:12:26 +02:00
|
|
|
process_iq_disco_items(From, #iq{type = get, lang = Lang,
|
|
|
|
sub_els = [#disco_items{node = ?NS_COMMANDS}]},
|
|
|
|
StateData) ->
|
|
|
|
case (StateData#state.config)#config.enable_hats andalso
|
|
|
|
is_admin(From, StateData)
|
|
|
|
of
|
|
|
|
true ->
|
|
|
|
{result,
|
|
|
|
#disco_items{
|
|
|
|
items = [#disco_item{jid = StateData#state.jid,
|
|
|
|
node = ?MUC_HAT_ADD_CMD,
|
|
|
|
name = translate:translate(
|
|
|
|
Lang, ?T("Add a hat to a user"))},
|
|
|
|
#disco_item{jid = StateData#state.jid,
|
|
|
|
node = ?MUC_HAT_REMOVE_CMD,
|
|
|
|
name = translate:translate(
|
|
|
|
Lang, ?T("Remove a hat from a user"))},
|
|
|
|
#disco_item{jid = StateData#state.jid,
|
|
|
|
node = ?MUC_HAT_LIST_CMD,
|
|
|
|
name = translate:translate(
|
|
|
|
Lang, ?T("List users with hats"))}]}};
|
|
|
|
false ->
|
|
|
|
Txt = ?T("Node not found"),
|
|
|
|
{error, xmpp:err_item_not_found(Txt, Lang)}
|
|
|
|
end;
|
|
|
|
process_iq_disco_items(From, #iq{type = get, lang = Lang,
|
|
|
|
sub_els = [#disco_items{node = Node}]},
|
|
|
|
StateData)
|
|
|
|
when Node == ?MUC_HAT_ADD_CMD;
|
|
|
|
Node == ?MUC_HAT_REMOVE_CMD;
|
|
|
|
Node == ?MUC_HAT_LIST_CMD ->
|
|
|
|
case (StateData#state.config)#config.enable_hats andalso
|
|
|
|
is_admin(From, StateData)
|
|
|
|
of
|
|
|
|
true ->
|
|
|
|
{result, #disco_items{}};
|
|
|
|
false ->
|
|
|
|
Txt = ?T("Node not found"),
|
|
|
|
{error, xmpp:err_item_not_found(Txt, Lang)}
|
|
|
|
end;
|
2019-07-15 13:10:45 +02:00
|
|
|
process_iq_disco_items(_From, #iq{lang = Lang}, _StateData) ->
|
|
|
|
Txt = ?T("Node not found"),
|
|
|
|
{error, xmpp:err_item_not_found(Txt, Lang)}.
|
2003-03-27 15:06:17 +01:00
|
|
|
|
2016-09-08 16:08:48 +02:00
|
|
|
-spec process_iq_captcha(jid(), iq(), state()) -> {error, stanza_error()} |
|
2016-07-25 12:50:30 +02:00
|
|
|
{result, undefined}.
|
|
|
|
process_iq_captcha(_From, #iq{type = get, lang = Lang}, _StateData) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("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} ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("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
|
|
|
_ ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("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()} |
|
2016-09-08 16:08:48 +02:00
|
|
|
{error, stanza_error()}.
|
2016-07-25 12:50:30 +02:00
|
|
|
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-10-17 12:37:23 +02:00
|
|
|
{error, xmpp:err_item_not_found()}
|
2014-05-04 21:11:05 +02:00
|
|
|
end;
|
2018-02-12 15:37:36 +01:00
|
|
|
process_iq_vcard(From, #iq{type = set, lang = Lang, sub_els = [Pkt]},
|
2016-07-25 12:50:30 +02:00
|
|
|
StateData) ->
|
2014-05-04 21:11:05 +02:00
|
|
|
case get_affiliation(From, StateData) of
|
|
|
|
owner ->
|
2018-02-12 15:37:36 +01:00
|
|
|
SubEl = xmpp:encode(Pkt),
|
|
|
|
VCardRaw = fxml:element_to_binary(SubEl),
|
|
|
|
Hash = mod_vcard_xupdate:compute_hash(SubEl),
|
2014-05-04 21:11:05 +02:00
|
|
|
Config = StateData#state.config,
|
2018-02-12 15:37:36 +01:00
|
|
|
NewConfig = Config#config{vcard = VCardRaw, vcard_xupdate = Hash},
|
2014-05-04 21:11:05 +02:00
|
|
|
change_config(NewConfig, StateData);
|
|
|
|
_ ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("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()) ->
|
2017-02-18 07:36:27 +01:00
|
|
|
{error, stanza_error()} |
|
2020-03-24 11:44:22 +01:00
|
|
|
{result, undefined | muc_subscribe() | muc_subscriptions(), stop | state()} |
|
2017-02-18 07:36:27 +01:00
|
|
|
{ignore, state()}.
|
2016-07-25 12:50:30 +02:00
|
|
|
process_iq_mucsub(_From, #iq{type = set, lang = Lang,
|
|
|
|
sub_els = [#muc_subscribe{}]},
|
2019-02-06 16:13:30 +01:00
|
|
|
#state{just_created = Just, config = #config{allow_subscription = false}}) when Just /= true ->
|
2019-06-22 16:08:45 +02:00
|
|
|
{error, xmpp:err_not_allowed(?T("Subscriptions are not allowed"), Lang)};
|
2017-04-26 01:30:12 +02:00
|
|
|
process_iq_mucsub(From,
|
|
|
|
#iq{type = set, lang = Lang,
|
|
|
|
sub_els = [#muc_subscribe{jid = #jid{} = SubJid} = Mucsub]},
|
|
|
|
StateData) ->
|
|
|
|
FAffiliation = get_affiliation(From, StateData),
|
|
|
|
FRole = get_role(From, StateData),
|
|
|
|
if FRole == moderator; FAffiliation == owner; FAffiliation == admin ->
|
|
|
|
process_iq_mucsub(SubJid,
|
|
|
|
#iq{type = set, lang = Lang,
|
|
|
|
sub_els = [Mucsub#muc_subscribe{jid = undefined}]},
|
|
|
|
StateData);
|
|
|
|
true ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Moderator privileges required"),
|
2017-04-26 01:30:12 +02:00
|
|
|
{error, xmpp:err_forbidden(Txt, Lang)}
|
|
|
|
end;
|
2016-07-25 12:50:30 +02:00
|
|
|
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) ->
|
2016-11-12 11:27:15 +01:00
|
|
|
LBareJID = jid:tolower(jid:remove_resource(From)),
|
2021-09-13 07:15:11 +02:00
|
|
|
try muc_subscribers_get(LBareJID, StateData#state.muc_subscribers) of
|
2018-11-15 12:13:45 +01:00
|
|
|
#subscriber{nick = Nick1} when Nick1 /= Nick ->
|
2016-07-25 12:50:30 +02:00
|
|
|
Nodes = get_subscription_nodes(Packet),
|
2021-09-13 07:15:11 +02:00
|
|
|
case nick_collision(From, Nick, StateData) of
|
|
|
|
true ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("That nickname is already in use by another occupant"),
|
2016-07-25 12:50:30 +02:00
|
|
|
{error, xmpp:err_conflict(ErrText, Lang)};
|
2021-09-13 07:15:11 +02:00
|
|
|
false ->
|
|
|
|
case mod_muc:can_use_nick(StateData#state.server_host,
|
|
|
|
StateData#state.host,
|
|
|
|
From, Nick) of
|
|
|
|
false ->
|
|
|
|
Err = case Nick of
|
|
|
|
<<>> ->
|
|
|
|
xmpp:err_jid_malformed(
|
|
|
|
?T("Nickname can't be empty"),
|
|
|
|
Lang);
|
|
|
|
_ ->
|
|
|
|
xmpp:err_conflict(
|
|
|
|
?T("That nickname is registered"
|
|
|
|
" by another person"), Lang)
|
|
|
|
end,
|
|
|
|
{error, Err};
|
|
|
|
true ->
|
|
|
|
NewStateData =
|
|
|
|
set_subscriber(From, Nick, Nodes, StateData),
|
|
|
|
{result, subscribe_result(Packet), NewStateData}
|
|
|
|
end
|
|
|
|
end;
|
2018-11-15 12:13:45 +01:00
|
|
|
#subscriber{} ->
|
2016-07-25 12:50:30 +02:00
|
|
|
Nodes = get_subscription_nodes(Packet),
|
2016-11-12 11:27:15 +01:00
|
|
|
NewStateData = set_subscriber(From, Nick, Nodes, StateData),
|
2018-11-15 12:13:45 +01:00
|
|
|
{result, subscribe_result(Packet), NewStateData}
|
2018-11-15 13:07:58 +01:00
|
|
|
catch _:{badkey, _} ->
|
2017-01-09 15:56:25 +01:00
|
|
|
SD2 = StateData#state{config = (StateData#state.config)#config{allow_subscription = true}},
|
2016-12-26 21:15:52 +01:00
|
|
|
add_new_user(From, Nick, Packet, SD2)
|
2016-06-26 08:08:37 +02:00
|
|
|
end;
|
2017-04-13 22:37:39 +02:00
|
|
|
process_iq_mucsub(From, #iq{type = set, lang = Lang,
|
2017-04-19 22:18:23 +02:00
|
|
|
sub_els = [#muc_unsubscribe{jid = #jid{} = UnsubJid}]},
|
|
|
|
StateData) ->
|
2017-04-13 22:37:39 +02:00
|
|
|
FAffiliation = get_affiliation(From, StateData),
|
|
|
|
FRole = get_role(From, StateData),
|
2017-04-19 22:18:23 +02:00
|
|
|
if FRole == moderator; FAffiliation == owner; FAffiliation == admin ->
|
|
|
|
process_iq_mucsub(UnsubJid,
|
2017-04-26 01:30:12 +02:00
|
|
|
#iq{type = set, lang = Lang,
|
2017-04-19 22:18:23 +02:00
|
|
|
sub_els = [#muc_unsubscribe{jid = undefined}]},
|
|
|
|
StateData);
|
|
|
|
true ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Moderator privileges required"),
|
2017-04-13 22:37:39 +02:00
|
|
|
{error, xmpp:err_forbidden(Txt, Lang)}
|
|
|
|
end;
|
2016-07-25 12:50:30 +02:00
|
|
|
process_iq_mucsub(From, #iq{type = set, sub_els = [#muc_unsubscribe{}]},
|
2019-04-15 12:02:43 +02:00
|
|
|
#state{room = Room, host = Host, server_host = ServerHost} = StateData) ->
|
2019-03-14 12:34:15 +01:00
|
|
|
BareJID = jid:remove_resource(From),
|
|
|
|
LBareJID = jid:tolower(BareJID),
|
2021-09-13 07:15:11 +02:00
|
|
|
try muc_subscribers_remove_exn(LBareJID, StateData#state.muc_subscribers) of
|
|
|
|
{MUCSubscribers, #subscriber{nick = Nick}} ->
|
|
|
|
NewStateData = StateData#state{muc_subscribers = MUCSubscribers},
|
2017-10-31 14:00:41 +01:00
|
|
|
store_room(NewStateData, [{del_subscription, LBareJID}]),
|
2022-05-09 16:34:27 +02:00
|
|
|
Packet1a = #message{
|
|
|
|
sub_els = [#ps_event{
|
|
|
|
items = #ps_items{
|
|
|
|
node = ?NS_MUCSUB_NODES_SUBSCRIBERS,
|
|
|
|
items = [#ps_item{
|
|
|
|
id = p1_rand:get_string(),
|
2022-08-24 10:30:02 +02:00
|
|
|
sub_els = [#muc_unsubscribe{jid = BareJID, nick = Nick}]}]}}]},
|
2022-05-09 16:34:27 +02:00
|
|
|
Packet1b = #message{
|
|
|
|
sub_els = [#ps_event{
|
|
|
|
items = #ps_items{
|
|
|
|
node = ?NS_MUCSUB_NODES_SUBSCRIBERS,
|
|
|
|
items = [#ps_item{
|
|
|
|
id = p1_rand:get_string(),
|
2022-08-24 10:30:02 +02:00
|
|
|
sub_els = [#muc_unsubscribe{nick = Nick}]}]}}]},
|
2022-05-09 16:34:27 +02:00
|
|
|
{Packet2a, Packet2b} = ejabberd_hooks:run_fold(muc_unsubscribed, ServerHost, {Packet1a, Packet1b},
|
2022-05-09 17:32:34 +02:00
|
|
|
[ServerHost, Room, Host, BareJID, StateData]),
|
2022-05-09 16:34:27 +02:00
|
|
|
send_subscriptions_change_notifications(Packet2a, Packet2b, StateData),
|
2016-12-01 22:09:57 +01:00
|
|
|
NewStateData2 = case close_room_if_temporary_and_empty(NewStateData) of
|
|
|
|
{stop, normal, _} -> stop;
|
|
|
|
{next_state, normal_state, SD} -> SD
|
|
|
|
end,
|
2018-11-15 12:13:45 +01:00
|
|
|
{result, undefined, NewStateData2}
|
2018-11-15 13:07:58 +01:00
|
|
|
catch _:{badkey, _} ->
|
2016-07-25 12:50:30 +02:00
|
|
|
{result, undefined, StateData}
|
2016-06-26 08:08:37 +02:00
|
|
|
end;
|
2016-11-12 11:27:15 +01:00
|
|
|
process_iq_mucsub(From, #iq{type = get, lang = Lang,
|
|
|
|
sub_els = [#muc_subscriptions{}]},
|
2016-09-06 18:06:02 +02:00
|
|
|
StateData) ->
|
|
|
|
FAffiliation = get_affiliation(From, StateData),
|
|
|
|
FRole = get_role(From, StateData),
|
2019-04-25 14:52:29 +02:00
|
|
|
IsModerator = FRole == moderator orelse FAffiliation == owner orelse
|
|
|
|
FAffiliation == admin,
|
|
|
|
case IsModerator orelse is_subscriber(From, StateData) of
|
|
|
|
true ->
|
|
|
|
ShowJid = IsModerator orelse
|
|
|
|
(StateData#state.config)#config.anonymous == false,
|
2021-09-13 07:15:11 +02:00
|
|
|
Subs = muc_subscribers_fold(
|
2019-04-25 14:52:29 +02:00
|
|
|
fun(_, #subscriber{jid = J, nick = N, nodes = Nodes}, Acc) ->
|
|
|
|
case ShowJid of
|
|
|
|
true ->
|
2020-08-26 13:29:30 +02:00
|
|
|
[#muc_subscription{jid = J, nick = N, events = Nodes}|Acc];
|
2019-04-25 14:52:29 +02:00
|
|
|
_ ->
|
|
|
|
[#muc_subscription{nick = N, events = Nodes}|Acc]
|
|
|
|
end
|
2021-09-13 07:15:11 +02:00
|
|
|
end, [], StateData#state.muc_subscribers),
|
2018-08-10 17:46:47 +02:00
|
|
|
{result, #muc_subscriptions{list = Subs}, StateData};
|
2019-04-25 14:52:29 +02:00
|
|
|
_ ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Moderator privileges required"),
|
2016-11-12 11:27:15 +01:00
|
|
|
{error, xmpp:err_forbidden(Txt, Lang)}
|
2016-09-06 18:06:02 +02:00
|
|
|
end;
|
2016-07-25 12:50:30 +02:00
|
|
|
process_iq_mucsub(_From, #iq{type = get, lang = Lang}, _StateData) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("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
|
|
|
|
2019-07-09 14:21:17 +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 ->
|
2021-09-13 07:15:11 +02:00
|
|
|
StateData#state{muc_subscribers = muc_subscribers_new()};
|
2016-06-26 08:08:37 +02:00
|
|
|
true ->
|
|
|
|
StateData
|
|
|
|
end.
|
|
|
|
|
2017-02-18 07:36:27 +01:00
|
|
|
-spec get_subscription_nodes(stanza()) -> [binary()].
|
2016-07-25 12:50:30 +02:00
|
|
|
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,
|
2017-10-30 12:05:18 +01:00
|
|
|
?NS_MUCSUB_NODES_PARTICIPANTS,
|
|
|
|
?NS_MUCSUB_NODES_SUBSCRIBERS])
|
2016-07-25 12:50:30 +02:00
|
|
|
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 -> <<"">>;
|
2019-06-22 16:08:45 +02:00
|
|
|
_ -> translate:translate(Lang, ?T("private, "))
|
2010-03-29 20:51:24 +02:00
|
|
|
end,
|
2018-11-15 12:13:45 +01:00
|
|
|
Len = maps:size(StateData#state.nicks),
|
2016-10-17 12:37:23 +02:00
|
|
|
<<" (", Desc/binary, (integer_to_binary(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) ->
|
2018-11-15 12:13:45 +01:00
|
|
|
Items = maps:fold(
|
2018-02-21 08:25:15 +01:00
|
|
|
fun(Nick, _, Acc) ->
|
|
|
|
[#disco_item{jid = jid:make(StateData#state.room,
|
|
|
|
StateData#state.host,
|
|
|
|
Nick),
|
|
|
|
name = Nick}|Acc]
|
|
|
|
end, [], StateData#state.nicks),
|
2016-07-25 12:50:30 +02:00
|
|
|
#disco_items{items = Items}.
|
2003-03-27 15:06:17 +01:00
|
|
|
|
2021-10-29 04:12:26 +02:00
|
|
|
-spec process_iq_adhoc(jid(), iq(), state()) ->
|
|
|
|
{result, adhoc_command()} |
|
|
|
|
{result, adhoc_command(), state()} |
|
|
|
|
{error, stanza_error()}.
|
|
|
|
process_iq_adhoc(_From, #iq{type = get}, _StateData) ->
|
|
|
|
{error, xmpp:err_bad_request()};
|
|
|
|
process_iq_adhoc(From, #iq{type = set, lang = Lang1,
|
|
|
|
sub_els = [#adhoc_command{} = Request]},
|
|
|
|
StateData) ->
|
|
|
|
% Ad-Hoc Commands are used only for Hats here
|
|
|
|
case (StateData#state.config)#config.enable_hats andalso
|
|
|
|
is_admin(From, StateData)
|
|
|
|
of
|
|
|
|
true ->
|
|
|
|
#adhoc_command{lang = Lang2, node = Node,
|
|
|
|
action = Action, xdata = XData} = Request,
|
|
|
|
Lang = case Lang2 of
|
|
|
|
<<"">> -> Lang1;
|
|
|
|
_ -> Lang2
|
|
|
|
end,
|
|
|
|
case {Node, Action} of
|
|
|
|
{_, cancel} ->
|
|
|
|
{result,
|
|
|
|
xmpp_util:make_adhoc_response(
|
|
|
|
Request,
|
|
|
|
#adhoc_command{status = canceled, lang = Lang,
|
|
|
|
node = Node})};
|
|
|
|
{?MUC_HAT_ADD_CMD, execute} ->
|
|
|
|
Form =
|
|
|
|
#xdata{
|
|
|
|
title = translate:translate(
|
|
|
|
Lang, ?T("Add a hat to a user")),
|
|
|
|
type = form,
|
|
|
|
fields =
|
|
|
|
[#xdata_field{
|
|
|
|
type = 'jid-single',
|
|
|
|
label = translate:translate(Lang, ?T("Jabber ID")),
|
|
|
|
required = true,
|
|
|
|
var = <<"jid">>},
|
|
|
|
#xdata_field{
|
|
|
|
type = 'text-single',
|
|
|
|
label = translate:translate(Lang, ?T("Hat title")),
|
|
|
|
var = <<"hat_title">>},
|
|
|
|
#xdata_field{
|
|
|
|
type = 'text-single',
|
|
|
|
label = translate:translate(Lang, ?T("Hat URI")),
|
|
|
|
required = true,
|
|
|
|
var = <<"hat_uri">>}
|
|
|
|
]},
|
|
|
|
{result,
|
|
|
|
xmpp_util:make_adhoc_response(
|
|
|
|
Request,
|
|
|
|
#adhoc_command{
|
|
|
|
status = executing,
|
|
|
|
xdata = Form})};
|
|
|
|
{?MUC_HAT_ADD_CMD, complete} when XData /= undefined ->
|
|
|
|
JID = try
|
|
|
|
jid:decode(hd(xmpp_util:get_xdata_values(
|
|
|
|
<<"jid">>, XData)))
|
|
|
|
catch _:_ -> error
|
|
|
|
end,
|
|
|
|
URI = try
|
|
|
|
hd(xmpp_util:get_xdata_values(
|
|
|
|
<<"hat_uri">>, XData))
|
|
|
|
catch _:_ -> error
|
|
|
|
end,
|
|
|
|
Title = case xmpp_util:get_xdata_values(
|
|
|
|
<<"hat_title">>, XData) of
|
|
|
|
[] -> <<"">>;
|
|
|
|
[T] -> T
|
|
|
|
end,
|
|
|
|
if
|
|
|
|
(JID /= error) and (URI /= error) ->
|
|
|
|
case add_hat(JID, URI, Title, StateData) of
|
|
|
|
{ok, NewStateData} ->
|
|
|
|
store_room(NewStateData),
|
|
|
|
send_update_presence(
|
|
|
|
JID, NewStateData, StateData),
|
|
|
|
{result,
|
|
|
|
xmpp_util:make_adhoc_response(
|
|
|
|
Request,
|
|
|
|
#adhoc_command{status = completed}),
|
|
|
|
NewStateData};
|
|
|
|
{error, size_limit} ->
|
|
|
|
Txt = ?T("Hats limit exceeded"),
|
|
|
|
{error, xmpp:err_not_allowed(Txt, Lang)}
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
{error, xmpp:err_bad_request()}
|
|
|
|
end;
|
|
|
|
{?MUC_HAT_ADD_CMD, complete} ->
|
|
|
|
{error, xmpp:err_bad_request()};
|
|
|
|
{?MUC_HAT_ADD_CMD, _} ->
|
|
|
|
Txt = ?T("Incorrect value of 'action' attribute"),
|
|
|
|
{error, xmpp:err_bad_request(Txt, Lang)};
|
|
|
|
{?MUC_HAT_REMOVE_CMD, execute} ->
|
|
|
|
Form =
|
|
|
|
#xdata{
|
|
|
|
title = translate:translate(
|
|
|
|
Lang, ?T("Remove a hat from a user")),
|
|
|
|
type = form,
|
|
|
|
fields =
|
|
|
|
[#xdata_field{
|
|
|
|
type = 'jid-single',
|
|
|
|
label = translate:translate(Lang, ?T("Jabber ID")),
|
|
|
|
required = true,
|
|
|
|
var = <<"jid">>},
|
|
|
|
#xdata_field{
|
|
|
|
type = 'text-single',
|
|
|
|
label = translate:translate(Lang, ?T("Hat URI")),
|
|
|
|
required = true,
|
|
|
|
var = <<"hat_uri">>}
|
|
|
|
]},
|
|
|
|
{result,
|
|
|
|
xmpp_util:make_adhoc_response(
|
|
|
|
Request,
|
|
|
|
#adhoc_command{
|
|
|
|
status = executing,
|
|
|
|
xdata = Form})};
|
|
|
|
{?MUC_HAT_REMOVE_CMD, complete} when XData /= undefined ->
|
|
|
|
JID = try
|
|
|
|
jid:decode(hd(xmpp_util:get_xdata_values(
|
|
|
|
<<"jid">>, XData)))
|
|
|
|
catch _:_ -> error
|
|
|
|
end,
|
|
|
|
URI = try
|
|
|
|
hd(xmpp_util:get_xdata_values(
|
|
|
|
<<"hat_uri">>, XData))
|
|
|
|
catch _:_ -> error
|
|
|
|
end,
|
|
|
|
if
|
|
|
|
(JID /= error) and (URI /= error) ->
|
|
|
|
NewStateData = del_hat(JID, URI, StateData),
|
|
|
|
store_room(NewStateData),
|
|
|
|
send_update_presence(
|
|
|
|
JID, NewStateData, StateData),
|
|
|
|
{result,
|
|
|
|
xmpp_util:make_adhoc_response(
|
|
|
|
Request,
|
|
|
|
#adhoc_command{status = completed}),
|
|
|
|
NewStateData};
|
|
|
|
true ->
|
|
|
|
{error, xmpp:err_bad_request()}
|
|
|
|
end;
|
|
|
|
{?MUC_HAT_REMOVE_CMD, complete} ->
|
|
|
|
{error, xmpp:err_bad_request()};
|
|
|
|
{?MUC_HAT_REMOVE_CMD, _} ->
|
|
|
|
Txt = ?T("Incorrect value of 'action' attribute"),
|
|
|
|
{error, xmpp:err_bad_request(Txt, Lang)};
|
|
|
|
{?MUC_HAT_LIST_CMD, execute} ->
|
|
|
|
Hats = get_all_hats(StateData),
|
|
|
|
Items =
|
|
|
|
lists:map(
|
|
|
|
fun({JID, URI, Title}) ->
|
|
|
|
[#xdata_field{
|
|
|
|
var = <<"jid">>,
|
|
|
|
values = [jid:encode(JID)]},
|
|
|
|
#xdata_field{
|
|
|
|
var = <<"hat_title">>,
|
|
|
|
values = [URI]},
|
|
|
|
#xdata_field{
|
|
|
|
var = <<"hat_uri">>,
|
|
|
|
values = [Title]}]
|
|
|
|
end, Hats),
|
|
|
|
Form =
|
|
|
|
#xdata{
|
|
|
|
title = translate:translate(
|
|
|
|
Lang, ?T("List of users with hats")),
|
|
|
|
type = result,
|
|
|
|
reported =
|
|
|
|
[#xdata_field{
|
|
|
|
label = translate:translate(Lang, ?T("Jabber ID")),
|
|
|
|
var = <<"jid">>},
|
|
|
|
#xdata_field{
|
|
|
|
label = translate:translate(Lang, ?T("Hat title")),
|
|
|
|
var = <<"hat_title">>},
|
|
|
|
#xdata_field{
|
|
|
|
label = translate:translate(Lang, ?T("Hat URI")),
|
|
|
|
var = <<"hat_uri">>}],
|
|
|
|
items = Items},
|
|
|
|
{result,
|
|
|
|
xmpp_util:make_adhoc_response(
|
|
|
|
Request,
|
|
|
|
#adhoc_command{
|
|
|
|
status = completed,
|
|
|
|
xdata = Form})};
|
|
|
|
{?MUC_HAT_LIST_CMD, _} ->
|
|
|
|
Txt = ?T("Incorrect value of 'action' attribute"),
|
|
|
|
{error, xmpp:err_bad_request(Txt, Lang)};
|
|
|
|
_ ->
|
|
|
|
{error, xmpp:err_item_not_found()}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
{error, xmpp:err_forbidden()}
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec add_hat(jid(), binary(), binary(), state()) ->
|
|
|
|
{ok, state()} | {error, size_limit}.
|
|
|
|
add_hat(JID, URI, Title, StateData) ->
|
|
|
|
Hats = StateData#state.hats_users,
|
|
|
|
LJID = jid:remove_resource(jid:tolower(JID)),
|
|
|
|
UserHats = maps:get(LJID, Hats, #{}),
|
|
|
|
UserHats2 = maps:put(URI, Title, UserHats),
|
|
|
|
USize = maps:size(UserHats2),
|
|
|
|
if
|
|
|
|
USize =< ?MAX_HATS_PER_USER ->
|
|
|
|
Hats2 = maps:put(LJID, UserHats2, Hats),
|
|
|
|
Size = maps:size(Hats2),
|
|
|
|
if
|
|
|
|
Size =< ?MAX_HATS_USERS ->
|
|
|
|
{ok, StateData#state{hats_users = Hats2}};
|
|
|
|
true ->
|
|
|
|
{error, size_limit}
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
{error, size_limit}
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec del_hat(jid(), binary(), state()) -> state().
|
|
|
|
del_hat(JID, URI, StateData) ->
|
|
|
|
Hats = StateData#state.hats_users,
|
|
|
|
LJID = jid:remove_resource(jid:tolower(JID)),
|
|
|
|
UserHats = maps:get(LJID, Hats, #{}),
|
|
|
|
UserHats2 = maps:remove(URI, UserHats),
|
|
|
|
Hats2 =
|
|
|
|
case maps:size(UserHats2) of
|
|
|
|
0 ->
|
|
|
|
maps:remove(LJID, Hats);
|
|
|
|
_ ->
|
|
|
|
maps:put(LJID, UserHats2, Hats)
|
|
|
|
end,
|
|
|
|
StateData#state{hats_users = Hats2}.
|
|
|
|
|
|
|
|
-spec get_all_hats(state()) -> list({jid(), binary(), binary()}).
|
|
|
|
get_all_hats(StateData) ->
|
|
|
|
lists:flatmap(
|
|
|
|
fun({LJID, H}) ->
|
|
|
|
JID = jid:make(LJID),
|
|
|
|
lists:map(fun({URI, Title}) -> {JID, URI, Title} end,
|
|
|
|
maps:to_list(H))
|
|
|
|
end,
|
|
|
|
maps:to_list(StateData#state.hats_users)).
|
|
|
|
|
|
|
|
-spec add_presence_hats(jid(), #presence{}, state()) -> #presence{}.
|
|
|
|
add_presence_hats(JID, Pres, StateData) ->
|
|
|
|
case (StateData#state.config)#config.enable_hats of
|
|
|
|
true ->
|
|
|
|
Hats = StateData#state.hats_users,
|
|
|
|
LJID = jid:remove_resource(jid:tolower(JID)),
|
|
|
|
UserHats = maps:get(LJID, Hats, #{}),
|
|
|
|
case maps:size(UserHats) of
|
|
|
|
0 -> Pres;
|
|
|
|
_ ->
|
|
|
|
Items =
|
|
|
|
lists:map(fun({URI, Title}) ->
|
|
|
|
#muc_hat{uri = URI, title = Title}
|
|
|
|
end,
|
|
|
|
maps:to_list(UserHats)),
|
|
|
|
xmpp:set_subtag(Pres,
|
|
|
|
#muc_hats{hats = Items})
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
Pres
|
|
|
|
end.
|
|
|
|
|
2023-03-30 14:37:13 +02:00
|
|
|
-spec process_iq_moderate(jid(), iq(), fasten_apply_to(), message_moderate(), state()) ->
|
|
|
|
{result, undefined, state()} |
|
|
|
|
{error, stanza_error()}.
|
|
|
|
process_iq_moderate(_From, #iq{type = get}, _ApplyTo, _Moderate, _StateData) ->
|
|
|
|
{error, xmpp:err_bad_request()};
|
|
|
|
process_iq_moderate(From, #iq{type = set, lang = Lang},
|
|
|
|
#fasten_apply_to{id = Id},
|
|
|
|
#message_moderate{reason = Reason},
|
2023-08-07 12:32:36 +02:00
|
|
|
#state{config = Config, room = Room, host = Host,
|
|
|
|
jid = JID, server_host = Server} = StateData) ->
|
2023-03-30 14:37:13 +02:00
|
|
|
FAffiliation = get_affiliation(From, StateData),
|
|
|
|
FRole = get_role(From, StateData),
|
|
|
|
IsModerator = FRole == moderator orelse FAffiliation == owner orelse
|
|
|
|
FAffiliation == admin,
|
|
|
|
case IsModerator of
|
|
|
|
false ->
|
|
|
|
{error, xmpp:err_forbidden(
|
|
|
|
?T("Only moderators are allowed to retract messages"), Lang)};
|
|
|
|
_ ->
|
|
|
|
try binary_to_integer(Id) of
|
|
|
|
StanzaId ->
|
|
|
|
case Config#config.mam of
|
|
|
|
true ->
|
2023-08-07 12:32:36 +02:00
|
|
|
mod_mam:remove_message_from_archive({Room, Host}, Server, StanzaId);
|
2023-03-30 14:37:13 +02:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
2023-04-14 12:05:49 +02:00
|
|
|
By = jid:replace_resource(JID, find_nick_by_jid(From, StateData)),
|
2023-08-07 13:05:06 +02:00
|
|
|
Packet0 = #message{type = groupchat,
|
|
|
|
from = From,
|
2023-03-30 14:37:13 +02:00
|
|
|
sub_els = [
|
|
|
|
#fasten_apply_to{id = Id, sub_els = [
|
2023-04-14 12:05:49 +02:00
|
|
|
#message_moderated{by = By, reason = Reason,
|
2023-03-30 14:37:13 +02:00
|
|
|
retract = #message_retract{}}
|
|
|
|
]}]},
|
2023-08-07 13:05:06 +02:00
|
|
|
{FromNick, _Role} = get_participant_data(From, StateData),
|
|
|
|
Packet = ejabberd_hooks:run_fold(muc_filter_message,
|
2023-08-24 11:54:26 +02:00
|
|
|
StateData#state.server_host,
|
|
|
|
xmpp:put_meta(Packet0, mam_ignore, true),
|
|
|
|
[StateData, FromNick]),
|
2023-03-30 14:37:13 +02:00
|
|
|
send_wrapped_multiple(JID,
|
|
|
|
get_users_and_subscribers_with_node(?NS_MUCSUB_NODES_MESSAGES, StateData),
|
|
|
|
Packet, ?NS_MUCSUB_NODES_MESSAGES, StateData),
|
2023-04-14 12:05:49 +02:00
|
|
|
NSD = add_message_to_history(<<"">>,
|
|
|
|
StateData#state.jid, Packet, StateData),
|
|
|
|
{result, undefined, remove_from_history(StanzaId, NSD)}
|
2023-03-30 14:37:13 +02:00
|
|
|
catch _:_ ->
|
|
|
|
{error, xmpp:err_bad_request(
|
|
|
|
?T("Stanza id is not valid"), Lang)}
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2011-07-23 16:13:27 +02:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% Voice request support
|
|
|
|
|
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) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Title = translate:translate(Lang, ?T("Voice request")),
|
2016-07-25 12:50:30 +02:00
|
|
|
Instruction = translate:translate(
|
2019-06-22 16:08:45 +02:00
|
|
|
Lang, ?T("Either approve or decline the voice request.")),
|
2016-10-07 09:31:03 +02:00
|
|
|
Fs = muc_request:encode([{role, participant},
|
|
|
|
{jid, Requester},
|
|
|
|
{roomnick, Nick},
|
|
|
|
{request_allow, false}],
|
2017-03-20 07:57:11 +01:00
|
|
|
Lang),
|
2016-07-25 12:50:30 +02:00
|
|
|
#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),
|
2017-02-16 09:00:26 +01:00
|
|
|
lists:foreach(
|
|
|
|
fun({_, User}) ->
|
|
|
|
ejabberd_router:route(
|
|
|
|
xmpp:set_from_to(
|
|
|
|
prepare_request_form(From, FromNick, Lang),
|
|
|
|
StateData#state.jid, User#user.jid))
|
|
|
|
end, Moderators).
|
2011-09-26 08:55:07 +02:00
|
|
|
|
2003-04-13 21:22:46 +02:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% Invitation support
|
2017-01-19 15:26:08 +01:00
|
|
|
-spec check_invitation(jid(), [muc_invite()], binary(), state()) ->
|
|
|
|
ok | {error, stanza_error()}.
|
|
|
|
check_invitation(From, Invitations, Lang, StateData) ->
|
2003-04-13 21:22:46 +02:00
|
|
|
FAffiliation = get_affiliation(From, StateData),
|
2019-08-13 14:55:37 +02:00
|
|
|
CanInvite = (StateData#state.config)#config.allow_user_invites orelse
|
2017-01-19 15:26:08 +01:00
|
|
|
FAffiliation == admin orelse FAffiliation == owner,
|
2007-05-03 06:07:29 +02:00
|
|
|
case CanInvite of
|
2017-01-19 15:26:08 +01:00
|
|
|
true ->
|
|
|
|
case lists:all(
|
|
|
|
fun(#muc_invite{to = #jid{}}) -> true;
|
|
|
|
(_) -> false
|
|
|
|
end, Invitations) of
|
|
|
|
true ->
|
|
|
|
ok;
|
|
|
|
false ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("No 'to' attribute found in the invitation"),
|
2017-01-19 15:26:08 +01:00
|
|
|
{error, xmpp:err_bad_request(Txt, Lang)}
|
|
|
|
end;
|
2016-07-25 12:50:30 +02:00
|
|
|
false ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Invitations are not allowed in this conference"),
|
2017-01-19 15:26:08 +01:00
|
|
|
{error, xmpp:err_not_allowed(Txt, Lang)}
|
2003-04-13 21:22:46 +02:00
|
|
|
end.
|
|
|
|
|
2018-09-21 16:37:52 +02:00
|
|
|
-spec route_invitation(jid(), message(), muc_invite(), binary(), state()) -> jid().
|
|
|
|
route_invitation(From, Pkt, Invitation, Lang, StateData) ->
|
2017-01-19 15:26:08 +01:00
|
|
|
#muc_invite{to = JID, reason = Reason} = Invitation,
|
|
|
|
Invite = Invitation#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,
|
2020-01-22 12:52:30 +01:00
|
|
|
?T("~s invites you to the room ~s")),
|
2017-02-26 08:07:12 +01:00
|
|
|
[jid:encode(From),
|
|
|
|
jid:encode({StateData#state.room, StateData#state.host, <<"">>})]),
|
2017-01-19 15:26:08 +01:00
|
|
|
case (StateData#state.config)#config.password_protected of
|
|
|
|
true ->
|
|
|
|
<<", ",
|
|
|
|
(translate:translate(
|
2019-06-22 16:08:45 +02:00
|
|
|
Lang, ?T("the password is")))/binary,
|
2017-01-19 15:26:08 +01:00
|
|
|
" '",
|
|
|
|
((StateData#state.config)#config.password)/binary,
|
|
|
|
"'">>;
|
|
|
|
_ -> <<"">>
|
|
|
|
end,
|
|
|
|
case Reason of
|
|
|
|
<<"">> -> <<"">>;
|
|
|
|
_ -> <<" (", Reason/binary, ") ">>
|
|
|
|
end]),
|
2017-02-16 09:00:26 +01:00
|
|
|
Msg = #message{from = StateData#state.jid,
|
|
|
|
to = JID,
|
|
|
|
type = normal,
|
2017-01-19 15:26:08 +01:00
|
|
|
body = xmpp:mk_text(Body),
|
|
|
|
sub_els = [XUser, XConference]},
|
2018-09-21 16:37:52 +02:00
|
|
|
Msg2 = ejabberd_hooks:run_fold(muc_invite,
|
|
|
|
StateData#state.server_host,
|
|
|
|
Msg,
|
|
|
|
[StateData#state.jid, StateData#state.config,
|
|
|
|
From, JID, Reason, Pkt]),
|
|
|
|
ejabberd_router:route(Msg2),
|
2017-01-19 15:26:08 +01:00
|
|
|
JID.
|
|
|
|
|
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) ->
|
2017-12-11 07:46:26 +01:00
|
|
|
try xmpp:try_subtag(Packet, #muc_user{}) of
|
2016-07-25 12:50:30 +02:00
|
|
|
#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),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route(
|
|
|
|
xmpp:set_from_to(NewPacket, StateData#state.jid, To));
|
2016-07-25 12:50:30 +02:00
|
|
|
_ ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Only occupants are allowed to send messages "
|
|
|
|
"to the conference"),
|
2016-07-25 12:50:30 +02:00
|
|
|
Err = xmpp:err_not_acceptable(ErrText, xmpp:get_lang(Packet)),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err)
|
2017-12-11 07:46:26 +01:00
|
|
|
catch _:{xmpp_codec, Why} ->
|
|
|
|
Txt = xmpp:io_format_error(Why),
|
|
|
|
Err = xmpp:err_bad_request(Txt, xmpp:get_lang(Packet)),
|
|
|
|
ejabberd_router:route_error(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,
|
2022-02-18 17:21:22 +01:00
|
|
|
make_opts(StateData, false));
|
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,
|
2022-02-18 17:21:22 +01:00
|
|
|
make_opts(StateData, false));
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> ok
|
2006-03-14 05:26:15 +01:00
|
|
|
end.
|
2007-12-03 11:47:42 +01:00
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
%% Users number checking
|
|
|
|
|
2017-01-13 10:03:39 +01:00
|
|
|
-spec tab_add_online_user(jid(), state()) -> any().
|
2007-12-03 11:47:42 +01:00
|
|
|
tab_add_online_user(JID, StateData) ->
|
|
|
|
Room = StateData#state.room,
|
|
|
|
Host = StateData#state.host,
|
2017-01-13 10:03:39 +01:00
|
|
|
ServerHost = StateData#state.server_host,
|
2017-07-27 17:02:06 +02:00
|
|
|
ejabberd_hooks:run(join_room, ServerHost, [ServerHost, Room, Host, JID]),
|
2017-01-13 10:03:39 +01:00
|
|
|
mod_muc:register_online_user(ServerHost, jid:tolower(JID), Room, Host).
|
2007-12-03 11:47:42 +01:00
|
|
|
|
2017-01-13 10:03:39 +01:00
|
|
|
-spec tab_remove_online_user(jid(), state()) -> any().
|
2007-12-03 11:47:42 +01:00
|
|
|
tab_remove_online_user(JID, StateData) ->
|
|
|
|
Room = StateData#state.room,
|
|
|
|
Host = StateData#state.host,
|
2017-01-13 10:03:39 +01:00
|
|
|
ServerHost = StateData#state.server_host,
|
2017-07-27 17:02:06 +02:00
|
|
|
ejabberd_hooks:run(leave_room, ServerHost, [ServerHost, Room, Host, JID]),
|
2017-01-13 10:03:39 +01:00
|
|
|
mod_muc:unregister_online_user(ServerHost, jid:tolower(JID), Room, Host).
|
2007-12-03 11:47:42 +01:00
|
|
|
|
2017-01-13 10:03:39 +01:00
|
|
|
-spec tab_count_user(jid(), state()) -> non_neg_integer().
|
|
|
|
tab_count_user(JID, StateData) ->
|
|
|
|
ServerHost = StateData#state.server_host,
|
2015-11-24 16:44:13 +01:00
|
|
|
{LUser, LServer, _} = jid:tolower(JID),
|
2017-01-13 10:03:39 +01:00
|
|
|
mod_muc:count_online_rooms_by_user(ServerHost, LUser, LServer).
|
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-09-24 13:17:21 +02:00
|
|
|
byte_size(fxml:element_to_binary(xmpp:encode(El, ?NS_CLIENT))).
|
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) ->
|
2017-10-31 14:00:41 +01:00
|
|
|
store_room(StateData, []).
|
|
|
|
store_room(StateData, ChangesHints) ->
|
2019-04-30 13:41:03 +02:00
|
|
|
% Let store persistent rooms or on those backends that have get_subscribed_rooms
|
2019-06-25 16:41:47 +02:00
|
|
|
Mod = gen_mod:db_mod(StateData#state.server_host, mod_muc),
|
|
|
|
HasGSR = erlang:function_exported(Mod, get_subscribed_rooms, 3),
|
|
|
|
case HasGSR of
|
|
|
|
true ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
2021-09-13 07:15:11 +02:00
|
|
|
erlang:put(muc_subscribers, StateData#state.muc_subscribers#muc_subscribers.subscribers)
|
2019-06-25 16:41:47 +02:00
|
|
|
end,
|
2019-04-30 13:41:03 +02:00
|
|
|
ShouldStore = case (StateData#state.config)#config.persistent of
|
|
|
|
true ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
case ChangesHints of
|
|
|
|
[] ->
|
|
|
|
false;
|
|
|
|
_ ->
|
2019-06-25 16:41:47 +02:00
|
|
|
HasGSR
|
2019-04-30 13:41:03 +02:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
if ShouldStore ->
|
2021-09-13 07:15:11 +02:00
|
|
|
case erlang:function_exported(Mod, store_changes, 4) of
|
|
|
|
true when ChangesHints /= [] ->
|
|
|
|
mod_muc:store_changes(
|
|
|
|
StateData#state.server_host,
|
|
|
|
StateData#state.host, StateData#state.room,
|
|
|
|
ChangesHints);
|
|
|
|
_ ->
|
2022-02-18 17:21:22 +01:00
|
|
|
store_room_no_checks(StateData, ChangesHints, false),
|
|
|
|
ok
|
2021-09-13 07:15:11 +02:00
|
|
|
end;
|
2016-06-26 08:08:37 +02:00
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2022-02-18 17:21:22 +01:00
|
|
|
-spec store_room_no_checks(state(), list(), boolean()) -> {atomic, any()}.
|
|
|
|
store_room_no_checks(StateData, ChangesHints, Hibernation) ->
|
2019-07-16 13:57:48 +02:00
|
|
|
mod_muc:store_room(StateData#state.server_host,
|
|
|
|
StateData#state.host, StateData#state.room,
|
2022-02-18 17:21:22 +01:00
|
|
|
make_opts(StateData, Hibernation),
|
2019-07-16 13:57:48 +02:00
|
|
|
ChangesHints).
|
|
|
|
|
2022-05-09 16:34:27 +02:00
|
|
|
-spec send_subscriptions_change_notifications(stanza(), stanza(), state()) -> ok.
|
|
|
|
send_subscriptions_change_notifications(Packet, PacketWithoutJid, State) ->
|
2021-07-13 16:01:25 +02:00
|
|
|
{WJ, WN} =
|
2021-09-13 07:15:11 +02:00
|
|
|
maps:fold(
|
|
|
|
fun(_, #subscriber{jid = JID}, {WithJid, WithNick}) ->
|
|
|
|
case (State#state.config)#config.anonymous == false orelse
|
|
|
|
get_role(JID, State) == moderator orelse
|
|
|
|
get_default_role(get_affiliation(JID, State), State) == moderator of
|
|
|
|
true ->
|
|
|
|
{[JID | WithJid], WithNick};
|
|
|
|
_ ->
|
|
|
|
{WithJid, [JID | WithNick]}
|
|
|
|
end
|
|
|
|
end, {[], []},
|
|
|
|
muc_subscribers_get_by_node(?NS_MUCSUB_NODES_SUBSCRIBERS,
|
|
|
|
State#state.muc_subscribers)),
|
2021-07-13 16:01:25 +02:00
|
|
|
if WJ /= [] ->
|
|
|
|
ejabberd_router_multicast:route_multicast(State#state.jid, State#state.server_host,
|
2022-05-09 16:34:27 +02:00
|
|
|
WJ, Packet, false);
|
2021-07-13 16:01:25 +02:00
|
|
|
true -> ok
|
|
|
|
end,
|
|
|
|
if WN /= [] ->
|
|
|
|
ejabberd_router_multicast:route_multicast(State#state.jid, State#state.server_host,
|
2022-05-09 16:34:27 +02:00
|
|
|
WN, PacketWithoutJid, false);
|
2021-07-13 16:01:25 +02:00
|
|
|
true -> ok
|
|
|
|
end.
|
2017-10-30 12:05:18 +01:00
|
|
|
|
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),
|
2016-09-07 09:33:37 +02:00
|
|
|
LBareTo = jid:tolower(jid:remove_resource(To)),
|
2018-11-15 12:13:45 +01:00
|
|
|
IsOffline = case maps:get(LTo, State#state.users, error) of
|
|
|
|
#user{last_presence = undefined} -> true;
|
2016-09-07 09:33:37 +02:00
|
|
|
error -> true;
|
|
|
|
_ -> false
|
|
|
|
end,
|
|
|
|
if IsOffline ->
|
2021-09-13 07:15:11 +02:00
|
|
|
try muc_subscribers_get(LBareTo, State#state.muc_subscribers) of
|
2018-11-15 12:13:45 +01:00
|
|
|
#subscriber{nodes = Nodes, jid = JID} ->
|
|
|
|
case lists:member(Node, Nodes) of
|
|
|
|
true ->
|
2019-03-28 17:42:25 +01:00
|
|
|
MamEnabled = (State#state.config)#config.mam,
|
2019-06-14 11:33:26 +02:00
|
|
|
Id = case xmpp:get_subtag(Packet, #stanza_id{by = #jid{}}) of
|
2019-03-28 17:42:25 +01:00
|
|
|
#stanza_id{id = Id2} ->
|
|
|
|
Id2;
|
|
|
|
_ ->
|
|
|
|
p1_rand:get_string()
|
|
|
|
end,
|
|
|
|
NewPacket = wrap(From, JID, Packet, Node, Id),
|
|
|
|
NewPacket2 = xmpp:put_meta(NewPacket, in_muc_mam, MamEnabled),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route(
|
2019-03-28 17:42:25 +01:00
|
|
|
xmpp:set_from_to(NewPacket2, State#state.jid, JID));
|
2018-11-15 12:13:45 +01:00
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end
|
2018-11-15 13:07:58 +01:00
|
|
|
catch _:{badkey, _} ->
|
2016-06-26 08:08:37 +02:00
|
|
|
ok
|
|
|
|
end;
|
2016-09-07 09:33:37 +02:00
|
|
|
true ->
|
2018-02-12 15:37:36 +01:00
|
|
|
case Packet of
|
|
|
|
#presence{type = unavailable} ->
|
|
|
|
case xmpp:get_subtag(Packet, #muc_user{}) of
|
|
|
|
#muc_user{destroy = Destroy,
|
|
|
|
status_codes = Codes} ->
|
|
|
|
case Destroy /= undefined orelse
|
|
|
|
(lists:member(110,Codes) andalso
|
|
|
|
not lists:member(303, Codes)) of
|
|
|
|
true ->
|
|
|
|
ejabberd_router:route(
|
|
|
|
#presence{from = State#state.jid, to = To,
|
2018-07-05 10:51:49 +02:00
|
|
|
id = p1_rand:get_string(),
|
2018-02-12 15:37:36 +01:00
|
|
|
type = unavailable});
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route(xmpp:set_from_to(Packet, From, To))
|
2016-06-26 08:08:37 +02:00
|
|
|
end.
|
|
|
|
|
2021-07-13 20:43:44 +02:00
|
|
|
-spec wrap(jid(), undefined | jid(), stanza(), binary(), binary()) -> message().
|
2019-03-28 17:42:25 +01:00
|
|
|
wrap(From, To, Packet, Node, Id) ->
|
2018-01-25 18:02:47 +01:00
|
|
|
El = xmpp:set_from_to(Packet, From, To),
|
2016-07-25 12:50:30 +02:00
|
|
|
#message{
|
2019-03-28 14:43:22 +01:00
|
|
|
id = Id,
|
|
|
|
sub_els = [#ps_event{
|
|
|
|
items = #ps_items{
|
|
|
|
node = Node,
|
|
|
|
items = [#ps_item{
|
|
|
|
id = Id,
|
|
|
|
sub_els = [El]}]}}]}.
|
2016-06-26 08:08:37 +02:00
|
|
|
|
2019-06-27 14:22:27 +02:00
|
|
|
-spec send_wrapped_multiple(jid(), users(), stanza(), binary(), state()) -> ok.
|
2016-06-26 08:08:37 +02:00
|
|
|
send_wrapped_multiple(From, Users, Packet, Node, State) ->
|
2021-07-13 16:01:25 +02:00
|
|
|
{Dir, Wra} =
|
2018-11-15 12:13:45 +01:00
|
|
|
maps:fold(
|
2021-07-13 16:01:25 +02:00
|
|
|
fun(_, #user{jid = To, last_presence = LP}, {Direct, Wrapped} = Res) ->
|
|
|
|
IsOffline = LP == undefined,
|
|
|
|
if IsOffline ->
|
|
|
|
LBareTo = jid:tolower(jid:remove_resource(To)),
|
2021-09-13 07:15:11 +02:00
|
|
|
case muc_subscribers_find(LBareTo, State#state.muc_subscribers) of
|
2021-07-13 16:01:25 +02:00
|
|
|
{ok, #subscriber{nodes = Nodes}} ->
|
|
|
|
case lists:member(Node, Nodes) of
|
|
|
|
true ->
|
|
|
|
{Direct, [To | Wrapped]};
|
|
|
|
_ ->
|
2021-09-13 07:15:11 +02:00
|
|
|
%% TODO: check that this branch is never called
|
2021-07-13 16:01:25 +02:00
|
|
|
Res
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Res
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
{[To | Direct], Wrapped}
|
|
|
|
end
|
|
|
|
end, {[],[]}, Users),
|
|
|
|
case Dir of
|
|
|
|
[] -> ok;
|
|
|
|
_ ->
|
|
|
|
case Packet of
|
|
|
|
#presence{type = unavailable} ->
|
|
|
|
case xmpp:get_subtag(Packet, #muc_user{}) of
|
|
|
|
#muc_user{destroy = Destroy,
|
|
|
|
status_codes = Codes} ->
|
|
|
|
case Destroy /= undefined orelse
|
|
|
|
(lists:member(110,Codes) andalso
|
|
|
|
not lists:member(303, Codes)) of
|
|
|
|
true ->
|
|
|
|
ejabberd_router_multicast:route_multicast(
|
2021-07-13 17:56:16 +02:00
|
|
|
From, State#state.server_host, Dir,
|
2021-07-13 16:01:25 +02:00
|
|
|
#presence{id = p1_rand:get_string(),
|
|
|
|
type = unavailable}, false);
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
2021-07-13 17:56:16 +02:00
|
|
|
ejabberd_router_multicast:route_multicast(From, State#state.server_host,
|
2021-07-13 16:01:25 +02:00
|
|
|
Dir, Packet, false)
|
|
|
|
end,
|
|
|
|
case Wra of
|
|
|
|
[] -> ok;
|
|
|
|
_ ->
|
|
|
|
MamEnabled = (State#state.config)#config.mam,
|
|
|
|
Id = case xmpp:get_subtag(Packet, #stanza_id{by = #jid{}}) of
|
|
|
|
#stanza_id{id = Id2} ->
|
|
|
|
Id2;
|
|
|
|
_ ->
|
|
|
|
p1_rand:get_string()
|
|
|
|
end,
|
2021-07-13 17:56:16 +02:00
|
|
|
NewPacket = wrap(From, undefined, Packet, Node, Id),
|
2021-07-13 16:01:25 +02:00
|
|
|
NewPacket2 = xmpp:put_meta(NewPacket, in_muc_mam, MamEnabled),
|
|
|
|
ejabberd_router_multicast:route_multicast(State#state.jid, State#state.server_host,
|
|
|
|
Wra, NewPacket2, true)
|
|
|
|
end.
|
2016-06-26 08:08:37 +02:00
|
|
|
|
2021-09-13 07:15:11 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% #muc_subscribers API
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-spec muc_subscribers_new() -> #muc_subscribers{}.
|
|
|
|
muc_subscribers_new() ->
|
|
|
|
#muc_subscribers{}.
|
|
|
|
|
|
|
|
-spec muc_subscribers_get(ljid(), #muc_subscribers{}) -> #subscriber{}.
|
|
|
|
muc_subscribers_get({_, _, _} = LJID, MUCSubscribers) ->
|
|
|
|
maps:get(LJID, MUCSubscribers#muc_subscribers.subscribers).
|
|
|
|
|
|
|
|
-spec muc_subscribers_find(ljid(), #muc_subscribers{}) ->
|
|
|
|
{ok, #subscriber{}} | error.
|
|
|
|
muc_subscribers_find({_, _, _} = LJID, MUCSubscribers) ->
|
|
|
|
maps:find(LJID, MUCSubscribers#muc_subscribers.subscribers).
|
|
|
|
|
|
|
|
-spec muc_subscribers_is_key(ljid(), #muc_subscribers{}) -> boolean().
|
|
|
|
muc_subscribers_is_key({_, _, _} = LJID, MUCSubscribers) ->
|
|
|
|
maps:is_key(LJID, MUCSubscribers#muc_subscribers.subscribers).
|
|
|
|
|
|
|
|
-spec muc_subscribers_size(#muc_subscribers{}) -> integer().
|
|
|
|
muc_subscribers_size(MUCSubscribers) ->
|
|
|
|
maps:size(MUCSubscribers#muc_subscribers.subscribers).
|
|
|
|
|
|
|
|
-spec muc_subscribers_fold(Fun, Acc, #muc_subscribers{}) -> Acc when
|
|
|
|
Fun :: fun((ljid(), #subscriber{}, Acc) -> Acc).
|
|
|
|
muc_subscribers_fold(Fun, Init, MUCSubscribers) ->
|
|
|
|
maps:fold(Fun, Init, MUCSubscribers#muc_subscribers.subscribers).
|
|
|
|
|
|
|
|
-spec muc_subscribers_get_by_nick(binary(), #muc_subscribers{}) -> [#subscriber{}].
|
|
|
|
muc_subscribers_get_by_nick(Nick, MUCSubscribers) ->
|
|
|
|
maps:get(Nick, MUCSubscribers#muc_subscribers.subscriber_nicks, []).
|
|
|
|
|
|
|
|
-spec muc_subscribers_get_by_node(binary(), #muc_subscribers{}) -> subscribers().
|
|
|
|
muc_subscribers_get_by_node(Node, MUCSubscribers) ->
|
|
|
|
maps:get(Node, MUCSubscribers#muc_subscribers.subscriber_nodes, #{}).
|
|
|
|
|
|
|
|
-spec muc_subscribers_remove_exn(ljid(), #muc_subscribers{}) ->
|
|
|
|
{#muc_subscribers{}, #subscriber{}}.
|
|
|
|
muc_subscribers_remove_exn({_, _, _} = LJID, MUCSubscribers) ->
|
|
|
|
#muc_subscribers{subscribers = Subs,
|
|
|
|
subscriber_nicks = SubNicks,
|
|
|
|
subscriber_nodes = SubNodes} = MUCSubscribers,
|
|
|
|
Subscriber = maps:get(LJID, Subs),
|
|
|
|
#subscriber{nick = Nick, nodes = Nodes} = Subscriber,
|
|
|
|
NewSubNicks = maps:remove(Nick, SubNicks),
|
|
|
|
NewSubs = maps:remove(LJID, Subs),
|
|
|
|
NewSubNodes =
|
|
|
|
lists:foldl(
|
|
|
|
fun(Node, Acc) ->
|
|
|
|
NodeSubs = maps:get(Node, Acc, #{}),
|
|
|
|
NodeSubs2 = maps:remove(LJID, NodeSubs),
|
|
|
|
maps:put(Node, NodeSubs2, Acc)
|
|
|
|
end, SubNodes, Nodes),
|
|
|
|
{#muc_subscribers{subscribers = NewSubs,
|
|
|
|
subscriber_nicks = NewSubNicks,
|
|
|
|
subscriber_nodes = NewSubNodes}, Subscriber}.
|
|
|
|
|
|
|
|
-spec muc_subscribers_put(#subscriber{}, #muc_subscribers{}) ->
|
|
|
|
#muc_subscribers{}.
|
|
|
|
muc_subscribers_put(Subscriber, MUCSubscribers) ->
|
|
|
|
#subscriber{jid = JID,
|
|
|
|
nick = Nick,
|
|
|
|
nodes = Nodes} = Subscriber,
|
|
|
|
#muc_subscribers{subscribers = Subs,
|
|
|
|
subscriber_nicks = SubNicks,
|
|
|
|
subscriber_nodes = SubNodes} = MUCSubscribers,
|
|
|
|
LJID = jid:tolower(JID),
|
|
|
|
NewSubs = maps:put(LJID, Subscriber, Subs),
|
|
|
|
NewSubNicks = maps:put(Nick, [LJID], SubNicks),
|
|
|
|
NewSubNodes =
|
|
|
|
lists:foldl(
|
|
|
|
fun(Node, Acc) ->
|
|
|
|
NodeSubs = maps:get(Node, Acc, #{}),
|
|
|
|
NodeSubs2 = maps:put(LJID, Subscriber, NodeSubs),
|
|
|
|
maps:put(Node, NodeSubs2, Acc)
|
|
|
|
end, SubNodes, Nodes),
|
|
|
|
#muc_subscribers{subscribers = NewSubs,
|
|
|
|
subscriber_nicks = NewSubNicks,
|
|
|
|
subscriber_nodes = NewSubNodes}.
|
|
|
|
|
|
|
|
|
2021-12-14 07:54:00 +01:00
|
|
|
cleanup_affiliations(State) ->
|
|
|
|
case mod_muc_opt:cleanup_affiliations_on_start(State#state.server_host) of
|
|
|
|
true ->
|
|
|
|
Affiliations =
|
|
|
|
maps:filter(
|
|
|
|
fun({LUser, LServer, _}, _) ->
|
|
|
|
case ejabberd_router:is_my_host(LServer) of
|
|
|
|
true ->
|
|
|
|
ejabberd_auth:user_exists(LUser, LServer);
|
|
|
|
false ->
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end, State#state.affiliations),
|
|
|
|
State#state{affiliations = Affiliations};
|
|
|
|
false ->
|
|
|
|
State
|
|
|
|
end.
|
2021-09-13 07:15:11 +02:00
|
|
|
|
2013-06-27 11:50:54 +02:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2019-07-16 21:07:39 +02:00
|
|
|
%% Detect messange stanzas that don't have meaningful 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 /= [].
|
2019-07-16 13:57:48 +02:00
|
|
|
|
|
|
|
-spec reset_hibernate_timer(state()) -> state().
|
|
|
|
reset_hibernate_timer(State) ->
|
|
|
|
case State#state.hibernate_timer of
|
|
|
|
hibernating ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
disable_hibernate_timer(State),
|
|
|
|
NewTimer = case {mod_muc_opt:hibernation_timeout(State#state.server_host),
|
|
|
|
maps:size(State#state.users)} of
|
|
|
|
{infinity, _} ->
|
|
|
|
none;
|
|
|
|
{Timeout, 0} ->
|
2019-07-16 17:02:32 +02:00
|
|
|
p1_fsm:send_event_after(Timeout, hibernate);
|
2019-07-16 13:57:48 +02:00
|
|
|
_ ->
|
|
|
|
none
|
|
|
|
end,
|
|
|
|
State#state{hibernate_timer = NewTimer}
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
-spec disable_hibernate_timer(state()) -> ok.
|
|
|
|
disable_hibernate_timer(State) ->
|
|
|
|
case State#state.hibernate_timer of
|
|
|
|
Ref when is_reference(Ref) ->
|
|
|
|
p1_fsm:cancel_timer(Ref),
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|