2015-06-22 15:56:08 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
2017-01-03 15:58:52 +01:00
|
|
|
%%% File : mod_mam.erl
|
|
|
|
%%% Author : Evgeniy Khramtsov <ekhramtsov@process-one.net>
|
|
|
|
%%% Purpose : Message Archive Management (XEP-0313)
|
2015-06-22 15:56:08 +02:00
|
|
|
%%% Created : 4 Jul 2013 by Evgeniy Khramtsov <ekhramtsov@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2019-01-08 22:53:27 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2013-2019 ProcessOne
|
2015-06-22 15:56:08 +02: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.
|
|
|
|
%%%
|
2015-08-05 09:52:54 +02: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.
|
2015-06-22 15:56:08 +02:00
|
|
|
%%%
|
|
|
|
%%%-------------------------------------------------------------------
|
2017-11-10 17:51:22 +01:00
|
|
|
|
2015-06-22 15:56:08 +02:00
|
|
|
-module(mod_mam).
|
|
|
|
|
2017-11-08 13:15:39 +01:00
|
|
|
-protocol({xep, 313, '0.6.1'}).
|
2015-12-09 22:28:44 +01:00
|
|
|
-protocol({xep, 334, '0.2'}).
|
2017-11-15 00:18:25 +01:00
|
|
|
-protocol({xep, 359, '0.5.0'}).
|
2015-06-24 11:58:37 +02:00
|
|
|
|
2015-06-22 15:56:08 +02:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
|
|
|
%% API
|
2017-02-22 17:46:47 +01:00
|
|
|
-export([start/2, stop/1, reload/3, depends/2]).
|
2015-06-22 15:56:08 +02:00
|
|
|
|
2017-11-14 22:02:48 +01:00
|
|
|
-export([sm_receive_packet/1, user_receive_packet/1, user_send_packet/1,
|
|
|
|
user_send_packet_strip_tag/1, process_iq_v0_2/1, process_iq_v0_3/1,
|
|
|
|
disco_sm_features/5, remove_user/2, remove_room/3, mod_opt_type/1,
|
|
|
|
muc_process_iq/2, muc_filter_message/3, message_is_archived/3,
|
|
|
|
delete_old_messages/2, get_commands_spec/0, msg_to_el/4,
|
2018-01-23 08:54:52 +01:00
|
|
|
get_room_config/4, set_room_option/3, offline_message/1, export/1,
|
2019-01-27 15:35:06 +01:00
|
|
|
mod_options/1, remove_mam_for_user_with_peer/3, remove_mam_for_user/2,
|
2019-02-21 10:36:33 +01:00
|
|
|
is_empty_for_user/2, is_empty_for_room/3, check_create_room/4,
|
2019-05-02 11:12:22 +02:00
|
|
|
process_iq/3, store_mam_message/7, make_id/0, wrap_as_mucsub/2, select/7]).
|
2015-06-22 15:56:08 +02:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-include("xmpp.hrl").
|
2015-06-22 15:56:08 +02:00
|
|
|
-include("logger.hrl").
|
2015-08-06 12:33:39 +02:00
|
|
|
-include("mod_muc_room.hrl").
|
2016-01-13 22:46:30 +01:00
|
|
|
-include("ejabberd_commands.hrl").
|
2016-04-15 14:11:31 +02:00
|
|
|
-include("mod_mam.hrl").
|
2019-06-22 16:08:45 +02:00
|
|
|
-include("translate.hrl").
|
2015-06-22 15:56:08 +02:00
|
|
|
|
2016-01-14 02:12:28 +01:00
|
|
|
-define(DEF_PAGE_SIZE, 50).
|
|
|
|
-define(MAX_PAGE_SIZE, 250).
|
|
|
|
|
2017-01-21 09:47:04 +01:00
|
|
|
-type c2s_state() :: ejabberd_c2s:state().
|
2018-12-21 20:29:20 +01:00
|
|
|
-type count() :: non_neg_integer() | undefined.
|
2017-01-21 09:47:04 +01:00
|
|
|
|
2016-04-15 14:11:31 +02:00
|
|
|
-callback init(binary(), gen_mod:opts()) -> any().
|
|
|
|
-callback remove_user(binary(), binary()) -> any().
|
|
|
|
-callback remove_room(binary(), binary(), binary()) -> any().
|
|
|
|
-callback delete_old_messages(binary() | global,
|
|
|
|
erlang:timestamp(),
|
|
|
|
all | chat | groupchat) -> any().
|
2016-11-18 11:38:08 +01:00
|
|
|
-callback extended_fields() -> [mam_query:property() | #xdata_field{}].
|
2016-04-15 14:11:31 +02:00
|
|
|
-callback store(xmlel(), binary(), {binary(), binary()}, chat | groupchat,
|
2017-11-14 22:02:48 +01:00
|
|
|
jid(), binary(), recv | send, integer()) -> ok | any().
|
2016-04-15 14:11:31 +02:00
|
|
|
-callback write_prefs(binary(), binary(), #archive_prefs{}, binary()) -> ok | any().
|
2019-01-02 19:11:22 +01:00
|
|
|
-callback get_prefs(binary(), binary()) -> {ok, #archive_prefs{}} | error | {error, db_failure}.
|
2016-11-18 11:38:08 +01:00
|
|
|
-callback select(binary(), jid(), jid(), mam_query:result(),
|
|
|
|
#rsm_set{} | undefined, chat | groupchat) ->
|
2018-12-21 20:29:20 +01:00
|
|
|
{[{binary(), non_neg_integer(), xmlel()}], boolean(), count()} |
|
|
|
|
{error, db_failure}.
|
2019-05-02 11:12:22 +02:00
|
|
|
-callback select(binary(), jid(), jid(), mam_query:result(),
|
|
|
|
#rsm_set{} | undefined, chat | groupchat,
|
|
|
|
all | only_count | only_messages) ->
|
|
|
|
{[{binary(), non_neg_integer(), xmlel()}], boolean(), count()} |
|
|
|
|
{error, db_failure}.
|
2018-02-13 09:38:41 +01:00
|
|
|
-callback use_cache(binary()) -> boolean().
|
|
|
|
-callback cache_nodes(binary()) -> [node()].
|
2018-04-20 13:27:39 +02:00
|
|
|
-callback remove_from_archive(binary(), binary(), jid() | none) -> ok | {error, any()}.
|
2019-01-27 15:35:06 +01:00
|
|
|
-callback is_empty_for_user(binary(), binary()) -> boolean().
|
|
|
|
-callback is_empty_for_room(binary(), binary(), binary()) -> boolean().
|
2019-03-28 17:42:25 +01:00
|
|
|
-callback select_with_mucsub(binary(), jid(), jid(), mam_query:result(),
|
2019-05-02 11:12:22 +02:00
|
|
|
#rsm_set{} | undefined, all | only_count | only_messages) ->
|
2019-03-28 17:42:25 +01:00
|
|
|
{[{binary(), non_neg_integer(), xmlel()}], boolean(), count()} |
|
|
|
|
{error, db_failure}.
|
2017-04-20 17:52:16 +02:00
|
|
|
|
2019-05-02 11:12:22 +02:00
|
|
|
-optional_callbacks([use_cache/1, cache_nodes/1, select_with_mucsub/6, select/6, select/7]).
|
2015-06-22 15:56:08 +02:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
start(Host, Opts) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case mod_mam_opt:db_type(Opts) of
|
2018-07-01 22:57:27 +02:00
|
|
|
mnesia ->
|
|
|
|
?WARNING_MSG("Mnesia backend for ~s is not recommended: "
|
|
|
|
"it's limited to 2GB and often gets corrupted "
|
|
|
|
"when reaching this limit. SQL backend is "
|
|
|
|
"recommended. Namely, for small servers SQLite "
|
|
|
|
"is a preferred choice because it's very easy "
|
|
|
|
"to configure.", [?MODULE]);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
2019-06-14 11:33:26 +02:00
|
|
|
Mod = gen_mod:db_mod(Opts, ?MODULE),
|
2018-12-21 20:29:20 +01:00
|
|
|
case Mod:init(Host, Opts) of
|
|
|
|
ok ->
|
|
|
|
init_cache(Mod, Host, Opts),
|
|
|
|
register_iq_handlers(Host),
|
|
|
|
ejabberd_hooks:add(sm_receive_packet, Host, ?MODULE,
|
|
|
|
sm_receive_packet, 50),
|
|
|
|
ejabberd_hooks:add(user_receive_packet, Host, ?MODULE,
|
|
|
|
user_receive_packet, 88),
|
|
|
|
ejabberd_hooks:add(user_send_packet, Host, ?MODULE,
|
|
|
|
user_send_packet, 88),
|
|
|
|
ejabberd_hooks:add(user_send_packet, Host, ?MODULE,
|
|
|
|
user_send_packet_strip_tag, 500),
|
|
|
|
ejabberd_hooks:add(offline_message_hook, Host, ?MODULE,
|
2019-04-26 19:59:06 +02:00
|
|
|
offline_message, 49),
|
2018-12-21 20:29:20 +01:00
|
|
|
ejabberd_hooks:add(muc_filter_message, Host, ?MODULE,
|
|
|
|
muc_filter_message, 50),
|
|
|
|
ejabberd_hooks:add(muc_process_iq, Host, ?MODULE,
|
|
|
|
muc_process_iq, 50),
|
|
|
|
ejabberd_hooks:add(disco_sm_features, Host, ?MODULE,
|
|
|
|
disco_sm_features, 50),
|
|
|
|
ejabberd_hooks:add(remove_user, Host, ?MODULE,
|
|
|
|
remove_user, 50),
|
|
|
|
ejabberd_hooks:add(get_room_config, Host, ?MODULE,
|
|
|
|
get_room_config, 50),
|
|
|
|
ejabberd_hooks:add(set_room_option, Host, ?MODULE,
|
|
|
|
set_room_option, 50),
|
2019-02-21 10:36:33 +01:00
|
|
|
ejabberd_hooks:add(store_mam_message, Host, ?MODULE,
|
|
|
|
store_mam_message, 100),
|
2019-06-14 11:33:26 +02:00
|
|
|
case mod_mam_opt:assume_mam_usage(Opts) of
|
2018-12-21 20:29:20 +01:00
|
|
|
true ->
|
|
|
|
ejabberd_hooks:add(message_is_archived, Host, ?MODULE,
|
|
|
|
message_is_archived, 50);
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end,
|
2019-06-14 11:33:26 +02:00
|
|
|
case mod_mam_opt:clear_archive_on_room_destroy(Opts) of
|
2019-01-13 10:35:03 +01:00
|
|
|
true ->
|
|
|
|
ejabberd_hooks:add(remove_room, Host, ?MODULE,
|
|
|
|
remove_room, 50);
|
|
|
|
false ->
|
2019-01-27 17:40:43 +01:00
|
|
|
ejabberd_hooks:add(check_create_room, Host, ?MODULE,
|
|
|
|
check_create_room, 50)
|
2019-01-13 10:35:03 +01:00
|
|
|
end,
|
2019-02-21 10:36:33 +01:00
|
|
|
ejabberd_commands:register_commands(get_commands_spec()),
|
|
|
|
ok;
|
2018-12-21 20:29:20 +01:00
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end.
|
2015-06-22 15:56:08 +02:00
|
|
|
|
2018-02-13 09:38:41 +01:00
|
|
|
use_cache(Mod, Host) ->
|
2017-04-20 17:52:16 +02:00
|
|
|
case erlang:function_exported(Mod, use_cache, 2) of
|
2018-02-13 09:38:41 +01:00
|
|
|
true -> Mod:use_cache(Host);
|
2019-06-14 11:33:26 +02:00
|
|
|
false -> mod_mam_opt:use_cache(Host)
|
2018-02-13 09:38:41 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
cache_nodes(Mod, Host) ->
|
|
|
|
case erlang:function_exported(Mod, cache_nodes, 1) of
|
|
|
|
true -> Mod:cache_nodes(Host);
|
|
|
|
false -> ejabberd_cluster:get_nodes()
|
2017-04-20 17:52:16 +02:00
|
|
|
end.
|
|
|
|
|
2018-02-13 09:38:41 +01:00
|
|
|
init_cache(Mod, Host, Opts) ->
|
|
|
|
case use_cache(Mod, Host) of
|
2017-04-20 17:52:16 +02:00
|
|
|
true ->
|
2018-01-23 08:54:52 +01:00
|
|
|
ets_cache:new(archive_prefs_cache, cache_opts(Opts));
|
2017-04-20 17:52:16 +02:00
|
|
|
false ->
|
2018-02-13 09:38:41 +01:00
|
|
|
ets_cache:delete(archive_prefs_cache)
|
2017-04-20 17:52:16 +02:00
|
|
|
end.
|
|
|
|
|
2018-01-23 08:54:52 +01:00
|
|
|
cache_opts(Opts) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
MaxSize = mod_mam_opt:cache_size(Opts),
|
|
|
|
CacheMissed = mod_mam_opt:cache_missed(Opts),
|
2019-06-15 11:53:16 +02:00
|
|
|
LifeTime = mod_mam_opt:cache_life_time(Opts),
|
2017-04-20 17:52:16 +02:00
|
|
|
[{max_size, MaxSize}, {life_time, LifeTime}, {cache_missed, CacheMissed}].
|
2015-06-22 15:56:08 +02:00
|
|
|
|
|
|
|
stop(Host) ->
|
2017-02-22 17:46:47 +01:00
|
|
|
unregister_iq_handlers(Host),
|
2017-11-09 00:21:40 +01:00
|
|
|
ejabberd_hooks:delete(sm_receive_packet, Host, ?MODULE,
|
|
|
|
sm_receive_packet, 50),
|
2017-11-14 22:02:48 +01:00
|
|
|
ejabberd_hooks:delete(user_receive_packet, Host, ?MODULE,
|
|
|
|
user_receive_packet, 88),
|
2015-06-22 15:56:08 +02:00
|
|
|
ejabberd_hooks:delete(user_send_packet, Host, ?MODULE,
|
2016-04-29 13:11:08 +02:00
|
|
|
user_send_packet, 88),
|
|
|
|
ejabberd_hooks:delete(user_send_packet, Host, ?MODULE,
|
2017-02-22 17:46:47 +01:00
|
|
|
user_send_packet_strip_tag, 500),
|
2017-11-10 18:09:33 +01:00
|
|
|
ejabberd_hooks:delete(offline_message_hook, Host, ?MODULE,
|
2019-04-26 19:59:06 +02:00
|
|
|
offline_message, 49),
|
2015-08-06 12:33:39 +02:00
|
|
|
ejabberd_hooks:delete(muc_filter_message, Host, ?MODULE,
|
|
|
|
muc_filter_message, 50),
|
|
|
|
ejabberd_hooks:delete(muc_process_iq, Host, ?MODULE,
|
|
|
|
muc_process_iq, 50),
|
2015-12-16 00:08:23 +01:00
|
|
|
ejabberd_hooks:delete(disco_sm_features, Host, ?MODULE,
|
|
|
|
disco_sm_features, 50),
|
2015-06-22 15:56:08 +02:00
|
|
|
ejabberd_hooks:delete(remove_user, Host, ?MODULE,
|
|
|
|
remove_user, 50),
|
2016-07-09 11:43:01 +02:00
|
|
|
ejabberd_hooks:delete(get_room_config, Host, ?MODULE,
|
|
|
|
get_room_config, 50),
|
|
|
|
ejabberd_hooks:delete(set_room_option, Host, ?MODULE,
|
|
|
|
set_room_option, 50),
|
2018-12-05 11:14:29 +01:00
|
|
|
ejabberd_hooks:delete(store_mam_message, Host, ?MODULE,
|
|
|
|
store_mam_message, 100),
|
2019-06-14 11:33:26 +02:00
|
|
|
case mod_mam_opt:assume_mam_usage(Host) of
|
2016-08-05 01:57:01 +02:00
|
|
|
true ->
|
2016-01-15 01:08:22 +01:00
|
|
|
ejabberd_hooks:delete(message_is_archived, Host, ?MODULE,
|
2016-08-05 01:57:01 +02:00
|
|
|
message_is_archived, 50);
|
|
|
|
false ->
|
|
|
|
ok
|
2016-01-15 01:08:22 +01:00
|
|
|
end,
|
2019-06-14 11:33:26 +02:00
|
|
|
case mod_mam_opt:clear_archive_on_room_destroy(Host) of
|
2019-01-13 10:35:03 +01:00
|
|
|
true ->
|
|
|
|
ejabberd_hooks:delete(remove_room, Host, ?MODULE,
|
|
|
|
remove_room, 50);
|
|
|
|
false ->
|
2019-01-27 17:40:43 +01:00
|
|
|
ejabberd_hooks:delete(check_create_room, Host, ?MODULE,
|
|
|
|
check_create_room, 50)
|
2019-01-13 10:35:03 +01:00
|
|
|
end,
|
2017-11-06 16:53:49 +01:00
|
|
|
case gen_mod:is_loaded_elsewhere(Host, ?MODULE) of
|
|
|
|
false ->
|
|
|
|
ejabberd_commands:unregister_commands(get_commands_spec());
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end.
|
2015-06-22 15:56:08 +02:00
|
|
|
|
2017-02-22 17:46:47 +01:00
|
|
|
reload(Host, NewOpts, OldOpts) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
NewMod = gen_mod:db_mod(NewOpts, ?MODULE),
|
|
|
|
OldMod = gen_mod:db_mod(OldOpts, ?MODULE),
|
2017-02-22 17:46:47 +01:00
|
|
|
if NewMod /= OldMod ->
|
|
|
|
NewMod:init(Host, NewOpts);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
2018-02-13 09:38:41 +01:00
|
|
|
init_cache(NewMod, Host, NewOpts),
|
2019-06-14 11:33:26 +02:00
|
|
|
case {mod_mam_opt:assume_mam_usage(NewOpts),
|
|
|
|
mod_mam_opt:assume_mam_usage(OldOpts)} of
|
|
|
|
{true, false} ->
|
2017-02-22 17:46:47 +01:00
|
|
|
ejabberd_hooks:add(message_is_archived, Host, ?MODULE,
|
|
|
|
message_is_archived, 50);
|
2019-06-14 11:33:26 +02:00
|
|
|
{false, true} ->
|
2017-02-22 17:46:47 +01:00
|
|
|
ejabberd_hooks:delete(message_is_archived, Host, ?MODULE,
|
|
|
|
message_is_archived, 50);
|
2019-06-14 11:33:26 +02:00
|
|
|
_ ->
|
2017-02-22 17:46:47 +01:00
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2019-05-06 20:03:10 +02:00
|
|
|
depends(_Host, _Opts) ->
|
2019-05-06 12:21:49 +02:00
|
|
|
[].
|
2016-07-06 13:58:48 +02:00
|
|
|
|
2018-02-11 10:54:15 +01:00
|
|
|
-spec register_iq_handlers(binary()) -> ok.
|
|
|
|
register_iq_handlers(Host) ->
|
2017-02-22 17:46:47 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_MAM_TMP,
|
2018-02-11 10:54:15 +01:00
|
|
|
?MODULE, process_iq_v0_2),
|
2017-02-22 17:46:47 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_MAM_TMP,
|
2018-02-11 10:54:15 +01:00
|
|
|
?MODULE, process_iq_v0_2),
|
2017-02-22 17:46:47 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_MAM_0,
|
2018-02-11 10:54:15 +01:00
|
|
|
?MODULE, process_iq_v0_3),
|
2017-02-22 17:46:47 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_MAM_0, ?MODULE,
|
2018-02-11 10:54:15 +01:00
|
|
|
process_iq_v0_3),
|
2017-02-22 17:46:47 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_MAM_1,
|
2018-02-11 10:54:15 +01:00
|
|
|
?MODULE, process_iq_v0_3),
|
2017-02-22 17:46:47 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_MAM_1,
|
2018-02-11 10:54:15 +01:00
|
|
|
?MODULE, process_iq_v0_3),
|
2017-11-08 13:15:39 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_MAM_2,
|
2018-02-11 10:54:15 +01:00
|
|
|
?MODULE, process_iq_v0_3),
|
2017-11-08 13:15:39 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_MAM_2,
|
2018-02-11 10:54:15 +01:00
|
|
|
?MODULE, process_iq_v0_3).
|
2017-02-22 17:46:47 +01:00
|
|
|
|
|
|
|
-spec unregister_iq_handlers(binary()) -> ok.
|
|
|
|
unregister_iq_handlers(Host) ->
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_MAM_TMP),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_MAM_TMP),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_MAM_0),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_MAM_0),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_MAM_1),
|
2017-11-08 13:15:39 +01:00
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_MAM_1),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_MAM_2),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_MAM_2).
|
2017-02-22 17:46:47 +01:00
|
|
|
|
2016-08-09 09:56:32 +02:00
|
|
|
-spec remove_user(binary(), binary()) -> ok.
|
2015-06-22 15:56:08 +02:00
|
|
|
remove_user(User, Server) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2016-04-15 14:11:31 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
2016-08-09 09:56:32 +02:00
|
|
|
Mod:remove_user(LUser, LServer),
|
2018-02-13 09:38:41 +01:00
|
|
|
case use_cache(Mod, LServer) of
|
|
|
|
true ->
|
|
|
|
ets_cache:delete(archive_prefs_cache, {LUser, LServer},
|
|
|
|
cache_nodes(Mod, LServer));
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end.
|
2015-06-22 15:56:08 +02:00
|
|
|
|
2016-08-09 09:56:32 +02:00
|
|
|
-spec remove_room(binary(), binary(), binary()) -> ok.
|
2016-04-15 14:11:31 +02:00
|
|
|
remove_room(LServer, Name, Host) ->
|
|
|
|
LName = jid:nodeprep(Name),
|
|
|
|
LHost = jid:nameprep(Host),
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
2016-08-09 09:56:32 +02:00
|
|
|
Mod:remove_room(LServer, LName, LHost),
|
|
|
|
ok.
|
2015-06-22 15:56:08 +02:00
|
|
|
|
2018-04-20 13:36:54 +02:00
|
|
|
-spec remove_mam_for_user(binary(), binary()) ->
|
|
|
|
{ok, binary()} | {error, binary()}.
|
2018-04-20 13:27:39 +02:00
|
|
|
remove_mam_for_user(User, Server) ->
|
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
case Mod:remove_from_archive(LUser, LServer, none) of
|
|
|
|
ok ->
|
|
|
|
{ok, <<"MAM archive removed">>};
|
|
|
|
{error, Bin} when is_binary(Bin) ->
|
|
|
|
{error, Bin};
|
|
|
|
{error, _} ->
|
|
|
|
{error, <<"Db returned error">>}
|
|
|
|
end.
|
|
|
|
|
2018-04-20 13:36:54 +02:00
|
|
|
-spec remove_mam_for_user_with_peer(binary(), binary(), binary()) ->
|
|
|
|
{ok, binary()} | {error, binary()}.
|
2018-04-20 13:27:39 +02:00
|
|
|
remove_mam_for_user_with_peer(User, Server, Peer) ->
|
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
|
|
|
try jid:decode(Peer) of
|
|
|
|
Jid ->
|
2018-04-20 14:06:23 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
2018-04-20 13:27:39 +02:00
|
|
|
case Mod:remove_from_archive(LUser, LServer, Jid) of
|
|
|
|
ok ->
|
|
|
|
{ok, <<"MAM archive removed">>};
|
|
|
|
{error, Bin} when is_binary(Bin) ->
|
|
|
|
{error, Bin};
|
|
|
|
{error, _} ->
|
|
|
|
{error, <<"Db returned error">>}
|
|
|
|
end
|
|
|
|
catch _:_ ->
|
|
|
|
{error, <<"Invalid peer JID">>}
|
|
|
|
end.
|
|
|
|
|
2016-10-07 09:31:03 +02:00
|
|
|
-spec get_room_config([muc_roomconfig:property()], mod_muc_room:state(),
|
|
|
|
jid(), binary()) -> [muc_roomconfig:property()].
|
|
|
|
get_room_config(Fields, RoomState, _From, _Lang) ->
|
2016-07-09 11:43:01 +02:00
|
|
|
Config = RoomState#state.config,
|
2016-10-07 09:31:03 +02:00
|
|
|
Fields ++ [{mam, Config#config.mam}].
|
|
|
|
|
|
|
|
-spec set_room_option({pos_integer(), _}, muc_roomconfig:property(), binary())
|
2016-08-12 12:17:42 +02:00
|
|
|
-> {pos_integer(), _}.
|
2016-10-07 09:31:03 +02:00
|
|
|
set_room_option(_Acc, {mam, Val}, _Lang) ->
|
|
|
|
{#config.mam, Val};
|
|
|
|
set_room_option(Acc, _Property, _Lang) ->
|
2016-07-09 11:43:01 +02:00
|
|
|
Acc.
|
|
|
|
|
2017-11-09 00:21:40 +01:00
|
|
|
-spec sm_receive_packet(stanza()) -> stanza().
|
2017-11-14 22:02:48 +01:00
|
|
|
sm_receive_packet(#message{to = #jid{lserver = LServer}} = Pkt) ->
|
|
|
|
init_stanza_id(Pkt, LServer);
|
|
|
|
sm_receive_packet(Acc) ->
|
|
|
|
Acc.
|
|
|
|
|
|
|
|
-spec user_receive_packet({stanza(), c2s_state()}) -> {stanza(), c2s_state()}.
|
|
|
|
user_receive_packet({#message{from = Peer} = Pkt, #{jid := JID} = C2SState}) ->
|
2015-06-22 15:56:08 +02:00
|
|
|
LUser = JID#jid.luser,
|
|
|
|
LServer = JID#jid.lserver,
|
2017-11-14 22:02:48 +01:00
|
|
|
Pkt1 = case should_archive(Pkt, LServer) of
|
|
|
|
true ->
|
|
|
|
case store_msg(Pkt, LUser, LServer, Peer, recv) of
|
|
|
|
ok ->
|
|
|
|
mark_stored_msg(Pkt, JID);
|
|
|
|
_ ->
|
|
|
|
Pkt
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Pkt
|
|
|
|
end,
|
|
|
|
{Pkt1, C2SState};
|
|
|
|
user_receive_packet(Acc) ->
|
2017-11-09 00:21:40 +01:00
|
|
|
Acc.
|
2015-06-22 15:56:08 +02:00
|
|
|
|
2017-11-09 00:48:19 +01:00
|
|
|
-spec user_send_packet({stanza(), c2s_state()})
|
|
|
|
-> {stanza(), c2s_state()}.
|
|
|
|
user_send_packet({#message{to = Peer} = Pkt, #{jid := JID} = C2SState}) ->
|
2015-06-22 15:56:08 +02:00
|
|
|
LUser = JID#jid.luser,
|
|
|
|
LServer = JID#jid.lserver,
|
2017-11-14 22:02:48 +01:00
|
|
|
Pkt1 = init_stanza_id(Pkt, LServer),
|
2017-11-09 01:10:24 +01:00
|
|
|
Pkt2 = case should_archive(Pkt1, LServer) of
|
2017-11-09 01:00:15 +01:00
|
|
|
true ->
|
2017-05-17 13:47:35 +02:00
|
|
|
case store_msg(xmpp:set_from_to(Pkt1, JID, Peer),
|
2017-11-09 01:00:15 +01:00
|
|
|
LUser, LServer, Peer, send) of
|
2017-11-14 22:02:48 +01:00
|
|
|
ok ->
|
|
|
|
mark_stored_msg(Pkt1, JID);
|
2017-11-09 01:00:15 +01:00
|
|
|
_ ->
|
2017-01-09 15:02:17 +01:00
|
|
|
Pkt1
|
2017-11-09 01:00:15 +01:00
|
|
|
end;
|
|
|
|
false ->
|
2017-11-09 01:10:24 +01:00
|
|
|
Pkt1
|
2017-01-09 15:02:17 +01:00
|
|
|
end,
|
2017-11-09 00:48:19 +01:00
|
|
|
{Pkt2, C2SState};
|
|
|
|
user_send_packet(Acc) ->
|
|
|
|
Acc.
|
2015-06-22 15:56:08 +02:00
|
|
|
|
2017-11-09 01:00:15 +01:00
|
|
|
-spec user_send_packet_strip_tag({stanza(), c2s_state()})
|
|
|
|
-> {stanza(), c2s_state()}.
|
2017-11-09 00:48:19 +01:00
|
|
|
user_send_packet_strip_tag({#message{} = Pkt, #{jid := JID} = C2SState}) ->
|
2016-04-29 13:11:08 +02:00
|
|
|
LServer = JID#jid.lserver,
|
2018-07-27 00:14:48 +02:00
|
|
|
Pkt1 = xmpp:del_meta(Pkt, stanza_id),
|
|
|
|
Pkt2 = strip_my_stanza_id(Pkt1, LServer),
|
|
|
|
{Pkt2, C2SState};
|
2017-11-09 00:48:19 +01:00
|
|
|
user_send_packet_strip_tag(Acc) ->
|
|
|
|
Acc.
|
2016-04-29 13:11:08 +02:00
|
|
|
|
2017-11-10 18:09:33 +01:00
|
|
|
-spec offline_message({any(), message()}) -> {any(), message()}.
|
2017-11-14 22:02:48 +01:00
|
|
|
offline_message({_Action, #message{from = Peer, to = To} = Pkt} = Acc) ->
|
|
|
|
LUser = To#jid.luser,
|
|
|
|
LServer = To#jid.lserver,
|
|
|
|
case should_archive(Pkt, LServer) of
|
|
|
|
true ->
|
|
|
|
case store_msg(Pkt, LUser, LServer, Peer, recv) of
|
|
|
|
ok ->
|
|
|
|
{archived, mark_stored_msg(Pkt, To)};
|
|
|
|
_ ->
|
|
|
|
Acc
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
Acc
|
|
|
|
end.
|
2017-11-10 18:09:33 +01:00
|
|
|
|
2016-08-12 12:17:42 +02:00
|
|
|
-spec muc_filter_message(message(), mod_muc_room:state(),
|
2017-02-24 19:25:25 +01:00
|
|
|
binary()) -> message().
|
2017-11-09 00:48:19 +01:00
|
|
|
muc_filter_message(#message{from = From} = Pkt,
|
|
|
|
#state{config = Config, jid = RoomJID} = MUCState,
|
2017-02-24 19:25:25 +01:00
|
|
|
FromNick) ->
|
2017-11-09 01:10:24 +01:00
|
|
|
LServer = RoomJID#jid.lserver,
|
2017-11-14 22:36:46 +01:00
|
|
|
Pkt1 = init_stanza_id(Pkt, LServer),
|
2015-08-06 12:33:39 +02:00
|
|
|
if Config#config.mam ->
|
2017-11-14 22:36:46 +01:00
|
|
|
StorePkt = strip_x_jid_tags(Pkt1),
|
2016-01-19 00:39:10 +01:00
|
|
|
case store_muc(MUCState, StorePkt, RoomJID, From, FromNick) of
|
2017-11-14 22:02:48 +01:00
|
|
|
ok ->
|
2017-11-14 22:36:46 +01:00
|
|
|
mark_stored_msg(Pkt1, RoomJID);
|
2015-09-22 20:15:34 +02:00
|
|
|
_ ->
|
2017-11-14 22:36:46 +01:00
|
|
|
Pkt1
|
2015-09-22 20:15:34 +02:00
|
|
|
end;
|
2015-08-06 12:33:39 +02:00
|
|
|
true ->
|
2017-11-14 22:36:46 +01:00
|
|
|
Pkt1
|
2017-11-09 00:48:19 +01:00
|
|
|
end;
|
|
|
|
muc_filter_message(Acc, _MUCState, _FromNick) ->
|
|
|
|
Acc.
|
2015-08-06 12:33:39 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec make_id() -> integer().
|
2018-12-05 11:14:29 +01:00
|
|
|
make_id() ->
|
2019-02-27 09:56:20 +01:00
|
|
|
erlang:system_time(microsecond).
|
2018-12-05 11:14:29 +01:00
|
|
|
|
2017-11-14 22:02:48 +01:00
|
|
|
-spec get_stanza_id(stanza()) -> integer().
|
|
|
|
get_stanza_id(#message{meta = #{stanza_id := ID}}) ->
|
|
|
|
ID.
|
|
|
|
|
|
|
|
-spec init_stanza_id(stanza(), binary()) -> stanza().
|
2018-06-16 00:43:22 +02:00
|
|
|
init_stanza_id(#message{meta = #{stanza_id := _ID}} = Pkt, _LServer) ->
|
|
|
|
Pkt;
|
2018-07-27 00:27:10 +02:00
|
|
|
init_stanza_id(#message{meta = #{from_offline := true}} = Pkt, _LServer) ->
|
|
|
|
Pkt;
|
2017-11-14 22:02:48 +01:00
|
|
|
init_stanza_id(Pkt, LServer) ->
|
2018-12-05 11:14:29 +01:00
|
|
|
ID = make_id(),
|
2017-11-14 22:36:46 +01:00
|
|
|
Pkt1 = strip_my_stanza_id(Pkt, LServer),
|
2017-11-14 22:02:48 +01:00
|
|
|
xmpp:put_meta(Pkt1, stanza_id, ID).
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec set_stanza_id(stanza(), jid(), binary()) -> stanza().
|
2016-11-01 08:47:08 +01:00
|
|
|
set_stanza_id(Pkt, JID, ID) ->
|
2016-11-18 11:38:08 +01:00
|
|
|
BareJID = jid:remove_resource(JID),
|
|
|
|
Archived = #mam_archived{by = BareJID, id = ID},
|
|
|
|
StanzaID = #stanza_id{by = BareJID, id = ID},
|
2016-07-25 12:50:30 +02:00
|
|
|
NewEls = [Archived, StanzaID|xmpp:get_els(Pkt)],
|
|
|
|
xmpp:set_els(Pkt, NewEls).
|
|
|
|
|
2017-11-14 22:02:48 +01:00
|
|
|
-spec mark_stored_msg(message(), jid()) -> message().
|
|
|
|
mark_stored_msg(#message{meta = #{stanza_id := ID}} = Pkt, JID) ->
|
|
|
|
Pkt1 = set_stanza_id(Pkt, JID, integer_to_binary(ID)),
|
|
|
|
xmpp:put_meta(Pkt1, mam_archived, true).
|
|
|
|
|
2015-07-06 15:39:23 +02:00
|
|
|
% Query archive v0.2
|
2016-07-25 12:50:30 +02:00
|
|
|
process_iq_v0_2(#iq{from = #jid{lserver = LServer},
|
|
|
|
to = #jid{lserver = LServer},
|
|
|
|
type = get, sub_els = [#mam_query{}]} = IQ) ->
|
|
|
|
process_iq(LServer, IQ, chat);
|
|
|
|
process_iq_v0_2(IQ) ->
|
|
|
|
process_iq(IQ).
|
2015-07-06 15:39:23 +02:00
|
|
|
|
|
|
|
% Query archive v0.3
|
2016-07-25 12:50:30 +02:00
|
|
|
process_iq_v0_3(#iq{from = #jid{lserver = LServer},
|
|
|
|
to = #jid{lserver = LServer},
|
|
|
|
type = set, sub_els = [#mam_query{}]} = IQ) ->
|
|
|
|
process_iq(LServer, IQ, chat);
|
|
|
|
process_iq_v0_3(#iq{from = #jid{lserver = LServer},
|
|
|
|
to = #jid{lserver = LServer},
|
|
|
|
type = get, sub_els = [#mam_query{}]} = IQ) ->
|
2016-01-13 23:56:05 +01:00
|
|
|
process_iq(LServer, IQ);
|
2016-07-25 12:50:30 +02:00
|
|
|
process_iq_v0_3(IQ) ->
|
|
|
|
process_iq(IQ).
|
|
|
|
|
2016-08-12 12:17:42 +02:00
|
|
|
-spec muc_process_iq(ignore | iq(), mod_muc_room:state()) -> ignore | iq().
|
2016-07-25 12:50:30 +02:00
|
|
|
muc_process_iq(#iq{type = T, lang = Lang,
|
|
|
|
from = From,
|
|
|
|
sub_els = [#mam_query{xmlns = NS}]} = IQ,
|
|
|
|
MUCState)
|
2017-11-08 13:15:39 +01:00
|
|
|
when (T == set andalso (NS /= ?NS_MAM_TMP)) orelse
|
2016-07-25 12:50:30 +02:00
|
|
|
(T == get andalso NS == ?NS_MAM_TMP) ->
|
|
|
|
case may_enter_room(From, MUCState) of
|
|
|
|
true ->
|
2016-01-13 23:56:05 +01:00
|
|
|
LServer = MUCState#state.server_host,
|
2016-07-25 12:50:30 +02:00
|
|
|
Role = mod_muc_room:get_role(From, MUCState),
|
|
|
|
process_iq(LServer, IQ, {groupchat, Role, MUCState});
|
|
|
|
false ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Text = ?T("Only members may query archives of this room"),
|
2016-07-25 12:50:30 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_forbidden(Text, Lang))
|
2016-01-13 23:56:05 +01:00
|
|
|
end;
|
2016-07-25 12:50:30 +02:00
|
|
|
muc_process_iq(#iq{type = get,
|
|
|
|
sub_els = [#mam_query{xmlns = NS}]} = IQ,
|
2017-11-08 13:15:39 +01:00
|
|
|
MUCState) when NS /= ?NS_MAM_TMP ->
|
2016-07-25 12:50:30 +02:00
|
|
|
LServer = MUCState#state.server_host,
|
|
|
|
process_iq(LServer, IQ);
|
|
|
|
muc_process_iq(IQ, _MUCState) ->
|
2015-08-06 12:33:39 +02:00
|
|
|
IQ.
|
|
|
|
|
2016-11-18 11:38:08 +01:00
|
|
|
parse_query(#mam_query{xmlns = ?NS_MAM_TMP,
|
|
|
|
start = Start, 'end' = End,
|
|
|
|
with = With, withtext = Text}, _Lang) ->
|
|
|
|
{ok, [{start, Start}, {'end', End},
|
|
|
|
{with, With}, {withtext, Text}]};
|
|
|
|
parse_query(#mam_query{xdata = #xdata{}} = Query, Lang) ->
|
|
|
|
X = xmpp_util:set_xdata_field(
|
|
|
|
#xdata_field{var = <<"FORM_TYPE">>,
|
|
|
|
type = hidden, values = [?NS_MAM_1]},
|
|
|
|
Query#mam_query.xdata),
|
|
|
|
try mam_query:decode(X#xdata.fields) of
|
|
|
|
Form -> {ok, Form}
|
|
|
|
catch _:{mam_query, Why} ->
|
|
|
|
Txt = mam_query:format_error(Why),
|
|
|
|
{error, xmpp:err_bad_request(Txt, Lang)}
|
2016-07-25 12:50:30 +02:00
|
|
|
end;
|
2016-11-18 11:38:08 +01:00
|
|
|
parse_query(#mam_query{}, _Lang) ->
|
|
|
|
{ok, []}.
|
2015-07-06 15:39:23 +02:00
|
|
|
|
2015-12-16 00:08:23 +01:00
|
|
|
disco_sm_features(empty, From, To, Node, Lang) ->
|
|
|
|
disco_sm_features({result, []}, From, To, Node, Lang);
|
|
|
|
disco_sm_features({result, OtherFeatures},
|
|
|
|
#jid{luser = U, lserver = S},
|
2016-08-05 07:41:08 +02:00
|
|
|
#jid{luser = U, lserver = S}, <<"">>, _Lang) ->
|
2017-11-15 00:18:25 +01:00
|
|
|
{result, [?NS_MAM_TMP, ?NS_MAM_0, ?NS_MAM_1, ?NS_MAM_2, ?NS_SID_0 |
|
|
|
|
OtherFeatures]};
|
2015-12-16 00:08:23 +01:00
|
|
|
disco_sm_features(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc.
|
|
|
|
|
2017-05-17 13:47:35 +02:00
|
|
|
-spec message_is_archived(boolean(), c2s_state(), message()) -> boolean().
|
2017-01-09 15:02:17 +01:00
|
|
|
message_is_archived(true, _C2SState, _Pkt) ->
|
2016-01-15 01:08:22 +01:00
|
|
|
true;
|
2017-11-15 00:36:07 +01:00
|
|
|
message_is_archived(false, #{lserver := LServer}, Pkt) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case mod_mam_opt:assume_mam_usage(LServer) of
|
2016-08-05 01:57:01 +02:00
|
|
|
true ->
|
2017-11-15 00:36:07 +01:00
|
|
|
is_archived(Pkt, LServer);
|
2016-08-05 01:57:01 +02:00
|
|
|
false ->
|
2016-01-15 01:08:22 +01:00
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2016-01-13 22:46:30 +01:00
|
|
|
delete_old_messages(TypeBin, Days) when TypeBin == <<"chat">>;
|
|
|
|
TypeBin == <<"groupchat">>;
|
|
|
|
TypeBin == <<"all">> ->
|
2018-12-05 11:14:29 +01:00
|
|
|
CurrentTime = make_id(),
|
2016-01-13 22:46:30 +01:00
|
|
|
Diff = Days * 24 * 60 * 60 * 1000000,
|
2017-07-20 20:22:50 +02:00
|
|
|
TimeStamp = misc:usec_to_now(CurrentTime - Diff),
|
2017-04-11 12:13:58 +02:00
|
|
|
Type = misc:binary_to_atom(TypeBin),
|
2016-04-15 14:11:31 +02:00
|
|
|
DBTypes = lists:usort(
|
|
|
|
lists:map(
|
|
|
|
fun(Host) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case mod_mam_opt:db_type(Host) of
|
2016-04-20 11:27:32 +02:00
|
|
|
sql -> {sql, Host};
|
2016-04-15 14:11:31 +02:00
|
|
|
Other -> {Other, global}
|
|
|
|
end
|
2019-06-14 11:33:26 +02:00
|
|
|
end, ejabberd_option:hosts())),
|
2016-04-15 14:11:31 +02:00
|
|
|
Results = lists:map(
|
|
|
|
fun({DBType, ServerHost}) ->
|
|
|
|
Mod = gen_mod:db_mod(DBType, ?MODULE),
|
|
|
|
Mod:delete_old_messages(ServerHost, TimeStamp, Type)
|
|
|
|
end, DBTypes),
|
2016-01-13 22:46:30 +01:00
|
|
|
case lists:filter(fun(Res) -> Res /= ok end, Results) of
|
|
|
|
[] -> ok;
|
|
|
|
[NotOk|_] -> NotOk
|
|
|
|
end;
|
|
|
|
delete_old_messages(_TypeBin, _Days) ->
|
|
|
|
unsupported_type.
|
|
|
|
|
2017-05-07 14:58:11 +02:00
|
|
|
export(LServer) ->
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:export(LServer).
|
|
|
|
|
2019-01-27 15:35:06 +01:00
|
|
|
-spec is_empty_for_user(binary(), binary()) -> boolean().
|
|
|
|
is_empty_for_user(User, Server) ->
|
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:is_empty_for_user(LUser, LServer).
|
|
|
|
|
|
|
|
-spec is_empty_for_room(binary(), binary(), binary()) -> boolean().
|
|
|
|
is_empty_for_room(LServer, Name, Host) ->
|
|
|
|
LName = jid:nodeprep(Name),
|
|
|
|
LHost = jid:nameprep(Host),
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:is_empty_for_room(LServer, LName, LHost).
|
|
|
|
|
2019-01-27 17:40:43 +01:00
|
|
|
-spec check_create_room(boolean(), binary(), binary(), binary()) -> boolean().
|
|
|
|
check_create_room(Acc, ServerHost, RoomID, Host) ->
|
|
|
|
Acc and is_empty_for_room(ServerHost, RoomID, Host).
|
|
|
|
|
2015-07-06 15:39:23 +02:00
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
process_iq(LServer, #iq{sub_els = [#mam_query{xmlns = NS}]} = IQ) ->
|
2016-04-15 14:11:31 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
2016-11-18 11:38:08 +01:00
|
|
|
CommonFields = [{with, undefined},
|
|
|
|
{start, undefined},
|
|
|
|
{'end', undefined}],
|
2016-04-15 14:11:31 +02:00
|
|
|
ExtendedFields = Mod:extended_fields(),
|
2016-11-18 11:38:08 +01:00
|
|
|
Fields = mam_query:encode(CommonFields ++ ExtendedFields),
|
|
|
|
X = xmpp_util:set_xdata_field(
|
|
|
|
#xdata_field{var = <<"FORM_TYPE">>, type = hidden, values = [NS]},
|
|
|
|
#xdata{type = form, fields = Fields}),
|
|
|
|
xmpp:make_iq_result(IQ, #mam_query{xmlns = NS, xdata = X}).
|
2016-01-13 23:56:05 +01:00
|
|
|
|
2015-07-06 15:39:23 +02:00
|
|
|
% Preference setting (both v0.2 & v0.3)
|
2016-07-25 12:50:30 +02:00
|
|
|
process_iq(#iq{type = set, lang = Lang,
|
|
|
|
sub_els = [#mam_prefs{default = undefined, xmlns = NS}]} = IQ) ->
|
|
|
|
Why = {missing_attr, <<"default">>, <<"prefs">>, NS},
|
2017-11-14 07:02:43 +01:00
|
|
|
ErrTxt = xmpp:io_format_error(Why),
|
2016-07-25 12:50:30 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_bad_request(ErrTxt, Lang));
|
|
|
|
process_iq(#iq{from = #jid{luser = LUser, lserver = LServer},
|
|
|
|
to = #jid{lserver = LServer},
|
|
|
|
type = set, lang = Lang,
|
|
|
|
sub_els = [#mam_prefs{xmlns = NS,
|
|
|
|
default = Default,
|
|
|
|
always = Always0,
|
|
|
|
never = Never0}]} = IQ) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Access = mod_mam_opt:access_preferences(LServer),
|
2019-01-13 11:37:08 +01:00
|
|
|
case acl:match_rule(LServer, Access, jid:make(LUser, LServer)) of
|
|
|
|
allow ->
|
|
|
|
Always = lists:usort(get_jids(Always0)),
|
|
|
|
Never = lists:usort(get_jids(Never0)),
|
|
|
|
case write_prefs(LUser, LServer, LServer, Default, Always, Never) of
|
|
|
|
ok ->
|
|
|
|
NewPrefs = prefs_el(Default, Always, Never, NS),
|
|
|
|
xmpp:make_iq_result(IQ, NewPrefs);
|
|
|
|
_Err ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Database failure"),
|
2019-01-13 11:37:08 +01:00
|
|
|
xmpp:make_error(IQ, xmpp:err_internal_server_error(Txt, Lang))
|
|
|
|
end;
|
|
|
|
deny ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("MAM preference modification denied by service policy"),
|
2019-01-13 11:37:08 +01:00
|
|
|
xmpp:make_error(IQ, xmpp:err_forbidden(Txt, Lang))
|
2015-06-22 15:56:08 +02:00
|
|
|
end;
|
2016-07-25 12:50:30 +02:00
|
|
|
process_iq(#iq{from = #jid{luser = LUser, lserver = LServer},
|
2019-01-02 19:11:22 +01:00
|
|
|
to = #jid{lserver = LServer}, lang = Lang,
|
2016-07-25 12:50:30 +02:00
|
|
|
type = get, sub_els = [#mam_prefs{xmlns = NS}]} = IQ) ->
|
2019-01-02 19:11:22 +01:00
|
|
|
case get_prefs(LUser, LServer) of
|
|
|
|
{ok, Prefs} ->
|
|
|
|
PrefsEl = prefs_el(Prefs#archive_prefs.default,
|
|
|
|
Prefs#archive_prefs.always,
|
|
|
|
Prefs#archive_prefs.never,
|
|
|
|
NS),
|
|
|
|
xmpp:make_iq_result(IQ, PrefsEl);
|
|
|
|
{error, _} ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Database failure"),
|
2019-01-02 19:11:22 +01:00
|
|
|
xmpp:make_error(IQ, xmpp:err_internal_server_error(Txt, Lang))
|
|
|
|
end;
|
2016-07-25 12:50:30 +02:00
|
|
|
process_iq(IQ) ->
|
|
|
|
xmpp:make_error(IQ, xmpp:err_not_allowed()).
|
2015-06-22 15:56:08 +02:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
process_iq(LServer, #iq{from = #jid{luser = LUser}, lang = Lang,
|
|
|
|
sub_els = [SubEl]} = IQ, MsgType) ->
|
2019-01-02 19:11:22 +01:00
|
|
|
Ret = case MsgType of
|
|
|
|
chat ->
|
|
|
|
maybe_activate_mam(LUser, LServer);
|
2019-02-21 10:36:33 +01:00
|
|
|
_ ->
|
2019-01-02 19:11:22 +01:00
|
|
|
ok
|
|
|
|
end,
|
|
|
|
case Ret of
|
|
|
|
ok ->
|
|
|
|
case SubEl of
|
|
|
|
#mam_query{rsm = #rsm_set{index = I}} when is_integer(I) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Unsupported <index/> element"),
|
2019-01-02 19:11:22 +01:00
|
|
|
xmpp:make_error(IQ, xmpp:err_feature_not_implemented(Txt, Lang));
|
|
|
|
#mam_query{rsm = RSM, xmlns = NS} ->
|
|
|
|
case parse_query(SubEl, Lang) of
|
|
|
|
{ok, Query} ->
|
|
|
|
NewRSM = limit_max(RSM, NS),
|
|
|
|
select_and_send(LServer, Query, NewRSM, IQ, MsgType);
|
|
|
|
{error, Err} ->
|
|
|
|
xmpp:make_error(IQ, Err)
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
{error, _} ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Database failure"),
|
2019-01-02 19:11:22 +01:00
|
|
|
xmpp:make_error(IQ, xmpp:err_internal_server_error(Txt, Lang))
|
2015-07-06 15:39:23 +02:00
|
|
|
end.
|
|
|
|
|
2017-11-14 22:58:16 +01:00
|
|
|
-spec should_archive(message(), binary()) -> boolean().
|
2016-11-12 11:27:15 +01:00
|
|
|
should_archive(#message{type = error}, _LServer) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
false;
|
2017-11-30 22:15:33 +01:00
|
|
|
should_archive(#message{type = groupchat}, _LServer) ->
|
|
|
|
false;
|
2017-01-21 09:47:04 +01:00
|
|
|
should_archive(#message{meta = #{from_offline := true}}, _LServer) ->
|
|
|
|
false;
|
2016-11-12 11:27:15 +01:00
|
|
|
should_archive(#message{body = Body, subject = Subject,
|
|
|
|
type = Type} = Pkt, LServer) ->
|
2017-11-15 00:36:07 +01:00
|
|
|
case is_archived(Pkt, LServer) of
|
2016-01-14 00:58:48 +01:00
|
|
|
true ->
|
2015-10-07 00:06:58 +02:00
|
|
|
false;
|
2016-07-25 12:50:30 +02:00
|
|
|
false ->
|
|
|
|
case check_store_hint(Pkt) of
|
|
|
|
store ->
|
|
|
|
true;
|
|
|
|
no_store ->
|
2015-12-08 00:10:00 +01:00
|
|
|
false;
|
2017-11-30 22:15:33 +01:00
|
|
|
none when Type == headline ->
|
2016-08-12 21:13:10 +02:00
|
|
|
false;
|
2016-07-25 12:50:30 +02:00
|
|
|
none ->
|
2018-10-10 18:02:45 +02:00
|
|
|
case xmpp:get_text(Body) /= <<>> orelse
|
|
|
|
xmpp:get_text(Subject) /= <<>> of
|
|
|
|
true ->
|
|
|
|
true;
|
|
|
|
_ ->
|
2018-10-25 01:22:57 +02:00
|
|
|
case misc:unwrap_mucsub_message(Pkt) of
|
2018-10-10 18:02:45 +02:00
|
|
|
#message{type = groupchat} = Msg ->
|
|
|
|
should_archive(Msg#message{type = chat}, LServer);
|
|
|
|
#message{} = Msg ->
|
|
|
|
should_archive(Msg, LServer);
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
2015-12-08 00:10:00 +01:00
|
|
|
end
|
2015-06-22 15:56:08 +02:00
|
|
|
end;
|
2016-07-25 12:50:30 +02:00
|
|
|
should_archive(_, _LServer) ->
|
2015-06-22 15:56:08 +02:00
|
|
|
false.
|
|
|
|
|
2017-11-14 22:36:46 +01:00
|
|
|
-spec strip_my_stanza_id(stanza(), binary()) -> stanza().
|
|
|
|
strip_my_stanza_id(Pkt, LServer) ->
|
2016-11-18 11:38:08 +01:00
|
|
|
Els = xmpp:get_els(Pkt),
|
|
|
|
NewEls = lists:filter(
|
2016-09-24 13:17:21 +02:00
|
|
|
fun(El) ->
|
2016-11-18 11:38:08 +01:00
|
|
|
Name = xmpp:get_name(El),
|
|
|
|
NS = xmpp:get_ns(El),
|
|
|
|
if (Name == <<"archived">> andalso NS == ?NS_MAM_TMP);
|
|
|
|
(Name == <<"stanza-id">> andalso NS == ?NS_SID_0) ->
|
|
|
|
try xmpp:decode(El) of
|
|
|
|
#mam_archived{by = By} ->
|
|
|
|
By#jid.lserver /= LServer;
|
|
|
|
#stanza_id{by = By} ->
|
|
|
|
By#jid.lserver /= LServer
|
|
|
|
catch _:{xmpp_codec, _} ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
true
|
2016-09-24 13:17:21 +02:00
|
|
|
end
|
2016-11-18 11:38:08 +01:00
|
|
|
end, Els),
|
|
|
|
xmpp:set_els(Pkt, NewEls).
|
2015-06-22 15:56:08 +02:00
|
|
|
|
2016-11-18 11:38:08 +01:00
|
|
|
-spec strip_x_jid_tags(stanza()) -> stanza().
|
2016-01-19 00:39:10 +01:00
|
|
|
strip_x_jid_tags(Pkt) ->
|
2016-11-18 11:38:08 +01:00
|
|
|
Els = xmpp:get_els(Pkt),
|
|
|
|
NewEls = lists:filter(
|
2016-09-24 13:17:21 +02:00
|
|
|
fun(El) ->
|
|
|
|
case xmpp:get_name(El) of
|
|
|
|
<<"x">> ->
|
2016-11-18 11:38:08 +01:00
|
|
|
NS = xmpp:get_ns(El),
|
|
|
|
Items = if NS == ?NS_MUC_USER;
|
|
|
|
NS == ?NS_MUC_ADMIN;
|
|
|
|
NS == ?NS_MUC_OWNER ->
|
|
|
|
try xmpp:decode(El) of
|
|
|
|
#muc_user{items = Is} -> Is;
|
|
|
|
#muc_admin{items = Is} -> Is;
|
|
|
|
#muc_owner{items = Is} -> Is
|
|
|
|
catch _:{xmpp_codec, _} ->
|
|
|
|
[]
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
not lists:any(
|
|
|
|
fun(#muc_item{jid = JID}) ->
|
|
|
|
JID /= undefined
|
|
|
|
end, Items);
|
2016-09-24 13:17:21 +02:00
|
|
|
_ ->
|
2016-11-18 11:38:08 +01:00
|
|
|
true
|
2016-09-24 13:17:21 +02:00
|
|
|
end
|
2016-11-18 11:38:08 +01:00
|
|
|
end, Els),
|
|
|
|
xmpp:set_els(Pkt, NewEls).
|
2016-01-19 00:39:10 +01:00
|
|
|
|
2017-05-17 13:47:35 +02:00
|
|
|
-spec should_archive_peer(binary(), binary(),
|
2017-01-21 09:47:04 +01:00
|
|
|
#archive_prefs{}, jid()) -> boolean().
|
2017-05-17 13:47:35 +02:00
|
|
|
should_archive_peer(LUser, LServer,
|
2015-10-07 00:06:58 +02:00
|
|
|
#archive_prefs{default = Default,
|
|
|
|
always = Always,
|
|
|
|
never = Never},
|
|
|
|
Peer) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
LPeer = jid:remove_resource(jid:tolower(Peer)),
|
2015-06-22 15:56:08 +02:00
|
|
|
case lists:member(LPeer, Always) of
|
2015-10-07 00:06:58 +02:00
|
|
|
true ->
|
|
|
|
true;
|
|
|
|
false ->
|
|
|
|
case lists:member(LPeer, Never) of
|
|
|
|
true ->
|
|
|
|
false;
|
|
|
|
false ->
|
|
|
|
case Default of
|
|
|
|
always -> true;
|
|
|
|
never -> false;
|
|
|
|
roster ->
|
2018-01-26 13:02:06 +01:00
|
|
|
{Sub, _, _} = ejabberd_hooks:run_fold(
|
|
|
|
roster_get_jid_info,
|
|
|
|
LServer, {none, none, []},
|
|
|
|
[LUser, LServer, Peer]),
|
2017-05-17 13:47:35 +02:00
|
|
|
Sub == both orelse Sub == from orelse Sub == to
|
2015-10-07 00:06:58 +02:00
|
|
|
end
|
|
|
|
end
|
2015-06-22 15:56:08 +02:00
|
|
|
end.
|
|
|
|
|
2017-11-14 22:58:16 +01:00
|
|
|
-spec should_archive_muc(message()) -> boolean().
|
2016-07-25 12:50:30 +02:00
|
|
|
should_archive_muc(#message{type = groupchat,
|
|
|
|
body = Body, subject = Subj} = Pkt) ->
|
|
|
|
case check_store_hint(Pkt) of
|
|
|
|
store ->
|
|
|
|
true;
|
|
|
|
no_store ->
|
|
|
|
false;
|
|
|
|
none ->
|
|
|
|
case xmpp:get_text(Body) of
|
|
|
|
<<"">> ->
|
|
|
|
case xmpp:get_text(Subj) of
|
|
|
|
<<"">> ->
|
|
|
|
false;
|
2016-01-14 01:13:16 +01:00
|
|
|
_ ->
|
|
|
|
true
|
2016-07-25 12:50:30 +02:00
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
true
|
2016-08-09 09:56:32 +02:00
|
|
|
end
|
2016-07-25 12:50:30 +02:00
|
|
|
end;
|
|
|
|
should_archive_muc(_) ->
|
|
|
|
false.
|
2015-06-22 15:56:08 +02:00
|
|
|
|
2017-11-14 22:58:16 +01:00
|
|
|
-spec check_store_hint(message()) -> store | no_store | none.
|
2015-12-08 00:10:00 +01:00
|
|
|
check_store_hint(Pkt) ->
|
|
|
|
case has_store_hint(Pkt) of
|
|
|
|
true ->
|
|
|
|
store;
|
|
|
|
false ->
|
|
|
|
case has_no_store_hint(Pkt) of
|
|
|
|
true ->
|
|
|
|
no_store;
|
|
|
|
false ->
|
|
|
|
none
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec has_store_hint(message()) -> boolean().
|
2015-12-08 00:10:00 +01:00
|
|
|
has_store_hint(Message) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
xmpp:has_subtag(Message, #hint{type = 'store'}).
|
2015-12-08 00:10:00 +01:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec has_no_store_hint(message()) -> boolean().
|
2015-12-08 00:10:00 +01:00
|
|
|
has_no_store_hint(Message) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
xmpp:has_subtag(Message, #hint{type = 'no-store'}) orelse
|
|
|
|
xmpp:has_subtag(Message, #hint{type = 'no-storage'}) orelse
|
|
|
|
xmpp:has_subtag(Message, #hint{type = 'no-permanent-store'}) orelse
|
|
|
|
xmpp:has_subtag(Message, #hint{type = 'no-permanent-storage'}).
|
2015-12-08 00:10:00 +01:00
|
|
|
|
2017-11-15 00:36:07 +01:00
|
|
|
-spec is_archived(message(), binary()) -> boolean().
|
|
|
|
is_archived(Pkt, LServer) ->
|
2017-02-18 07:36:27 +01:00
|
|
|
case xmpp:get_subtag(Pkt, #stanza_id{by = #jid{}}) of
|
2016-11-18 11:38:08 +01:00
|
|
|
#stanza_id{by = #jid{lserver = LServer}} ->
|
2016-07-25 12:50:30 +02:00
|
|
|
true;
|
|
|
|
_ ->
|
2015-12-29 09:54:48 +01:00
|
|
|
false
|
2015-12-09 22:44:45 +01:00
|
|
|
end.
|
|
|
|
|
2017-11-14 22:58:16 +01:00
|
|
|
-spec may_enter_room(jid(), mod_muc_room:state()) -> boolean().
|
2016-02-08 00:16:02 +01:00
|
|
|
may_enter_room(From,
|
|
|
|
#state{config = #config{members_only = false}} = MUCState) ->
|
|
|
|
mod_muc_room:get_affiliation(From, MUCState) /= outcast;
|
|
|
|
may_enter_room(From, MUCState) ->
|
|
|
|
mod_muc_room:is_occupant_or_admin(From, MUCState).
|
|
|
|
|
2017-11-14 22:02:48 +01:00
|
|
|
-spec store_msg(message(), binary(), binary(), jid(), send | recv)
|
|
|
|
-> ok | pass | any().
|
2017-05-17 13:47:35 +02:00
|
|
|
store_msg(Pkt, LUser, LServer, Peer, Dir) ->
|
2019-01-02 19:11:22 +01:00
|
|
|
case get_prefs(LUser, LServer) of
|
|
|
|
{ok, Prefs} ->
|
2019-06-14 11:33:26 +02:00
|
|
|
UseMucArchive = mod_mam_opt:user_mucsub_from_muc_archive(LServer),
|
2019-03-28 17:42:25 +01:00
|
|
|
StoredInMucMam = UseMucArchive andalso xmpp:get_meta(Pkt, in_muc_mam, false),
|
|
|
|
case {should_archive_peer(LUser, LServer, Prefs, Peer), Pkt, StoredInMucMam} of
|
|
|
|
{true, #message{meta = #{sm_copy := true}}, _} ->
|
2019-01-02 19:11:22 +01:00
|
|
|
ok; % Already stored.
|
2019-03-28 17:42:25 +01:00
|
|
|
{true, _, true} ->
|
|
|
|
ok; % Stored in muc archive.
|
|
|
|
{true, _, _} ->
|
2019-01-02 19:11:22 +01:00
|
|
|
case ejabberd_hooks:run_fold(store_mam_message, LServer, Pkt,
|
2019-02-21 10:36:33 +01:00
|
|
|
[LUser, LServer, Peer, <<"">>, chat, Dir]) of
|
|
|
|
#message{} -> ok;
|
|
|
|
_ -> pass
|
2019-01-02 19:11:22 +01:00
|
|
|
end;
|
2019-03-28 17:42:25 +01:00
|
|
|
{false, _, _} ->
|
2019-01-02 19:11:22 +01:00
|
|
|
pass
|
2016-08-15 23:28:36 +02:00
|
|
|
end;
|
2019-01-02 19:11:22 +01:00
|
|
|
{error, _} ->
|
2015-10-07 00:06:58 +02:00
|
|
|
pass
|
2015-06-22 15:56:08 +02:00
|
|
|
end.
|
|
|
|
|
2017-11-14 22:58:16 +01:00
|
|
|
-spec store_muc(mod_muc_room:state(), message(), jid(), jid(), binary())
|
|
|
|
-> ok | pass | any().
|
2015-08-06 12:33:39 +02:00
|
|
|
store_muc(MUCState, Pkt, RoomJID, Peer, Nick) ->
|
2016-01-14 01:13:16 +01:00
|
|
|
case should_archive_muc(Pkt) of
|
2015-08-06 12:33:39 +02:00
|
|
|
true ->
|
2015-11-24 16:44:13 +01:00
|
|
|
{U, S, _} = jid:tolower(RoomJID),
|
2016-08-15 23:28:36 +02:00
|
|
|
LServer = MUCState#state.server_host,
|
|
|
|
case ejabberd_hooks:run_fold(store_mam_message, LServer, Pkt,
|
2018-12-05 11:14:29 +01:00
|
|
|
[U, S, Peer, Nick, groupchat, recv]) of
|
|
|
|
#message{} -> ok;
|
|
|
|
_ -> pass
|
2016-08-15 23:28:36 +02:00
|
|
|
end;
|
2015-08-06 12:33:39 +02:00
|
|
|
false ->
|
|
|
|
pass
|
|
|
|
end.
|
|
|
|
|
2018-12-05 11:14:29 +01:00
|
|
|
store_mam_message(Pkt, U, S, Peer, Nick, Type, Dir) ->
|
|
|
|
LServer = ejabberd_router:host_of_route(S),
|
|
|
|
US = {U, S},
|
|
|
|
ID = get_stanza_id(Pkt),
|
|
|
|
El = xmpp:encode(Pkt),
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:store(El, LServer, US, Type, Peer, Nick, Dir, ID),
|
|
|
|
Pkt.
|
|
|
|
|
2015-06-22 15:56:08 +02:00
|
|
|
write_prefs(LUser, LServer, Host, Default, Always, Never) ->
|
|
|
|
Prefs = #archive_prefs{us = {LUser, LServer},
|
2015-10-07 00:06:58 +02:00
|
|
|
default = Default,
|
|
|
|
always = Always,
|
|
|
|
never = Never},
|
2016-04-15 14:11:31 +02:00
|
|
|
Mod = gen_mod:db_mod(Host, ?MODULE),
|
2017-04-20 17:52:16 +02:00
|
|
|
case Mod:write_prefs(LUser, LServer, Prefs, Host) of
|
|
|
|
ok ->
|
2018-02-13 09:38:41 +01:00
|
|
|
case use_cache(Mod, LServer) of
|
|
|
|
true ->
|
|
|
|
ets_cache:delete(archive_prefs_cache, {LUser, LServer},
|
|
|
|
cache_nodes(Mod, LServer));
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end;
|
2017-04-20 17:52:16 +02:00
|
|
|
_Err ->
|
|
|
|
{error, db_failure}
|
|
|
|
end.
|
2015-06-22 15:56:08 +02:00
|
|
|
|
|
|
|
get_prefs(LUser, LServer) ->
|
2016-04-15 14:11:31 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
2018-02-13 09:38:41 +01:00
|
|
|
Res = case use_cache(Mod, LServer) of
|
|
|
|
true ->
|
|
|
|
ets_cache:lookup(archive_prefs_cache, {LUser, LServer},
|
|
|
|
fun() -> Mod:get_prefs(LUser, LServer) end);
|
|
|
|
false ->
|
|
|
|
Mod:get_prefs(LUser, LServer)
|
|
|
|
end,
|
2015-06-22 15:56:08 +02:00
|
|
|
case Res of
|
2015-10-07 00:06:58 +02:00
|
|
|
{ok, Prefs} ->
|
2019-01-02 19:11:22 +01:00
|
|
|
{ok, Prefs};
|
|
|
|
{error, _} ->
|
|
|
|
{error, db_failure};
|
2015-10-07 00:06:58 +02:00
|
|
|
error ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ActivateOpt = mod_mam_opt:request_activates_archiving(LServer),
|
2016-01-13 21:38:08 +01:00
|
|
|
case ActivateOpt of
|
|
|
|
true ->
|
2019-01-02 19:11:22 +01:00
|
|
|
{ok, #archive_prefs{us = {LUser, LServer}, default = never}};
|
2016-01-13 21:38:08 +01:00
|
|
|
false ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Default = mod_mam_opt:default(LServer),
|
2019-01-02 19:11:22 +01:00
|
|
|
{ok, #archive_prefs{us = {LUser, LServer}, default = Default}}
|
2016-01-13 21:38:08 +01:00
|
|
|
end
|
2015-06-22 15:56:08 +02:00
|
|
|
end.
|
|
|
|
|
2016-02-10 23:06:31 +01:00
|
|
|
prefs_el(Default, Always, Never, NS) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
#mam_prefs{default = Default,
|
|
|
|
always = [jid:make(LJ) || LJ <- Always],
|
|
|
|
never = [jid:make(LJ) || LJ <- Never],
|
|
|
|
xmlns = NS}.
|
2016-02-10 23:06:31 +01:00
|
|
|
|
2016-01-13 21:38:08 +01:00
|
|
|
maybe_activate_mam(LUser, LServer) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ActivateOpt = mod_mam_opt:request_activates_archiving(LServer),
|
2016-01-13 21:38:08 +01:00
|
|
|
case ActivateOpt of
|
|
|
|
true ->
|
2016-04-15 14:11:31 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
2018-02-13 09:38:41 +01:00
|
|
|
Res = case use_cache(Mod, LServer) of
|
|
|
|
true ->
|
|
|
|
ets_cache:lookup(archive_prefs_cache,
|
|
|
|
{LUser, LServer},
|
|
|
|
fun() ->
|
|
|
|
Mod:get_prefs(LUser, LServer)
|
|
|
|
end);
|
|
|
|
false ->
|
|
|
|
Mod:get_prefs(LUser, LServer)
|
|
|
|
end,
|
2016-01-13 21:38:08 +01:00
|
|
|
case Res of
|
|
|
|
{ok, _Prefs} ->
|
|
|
|
ok;
|
2019-01-02 19:11:22 +01:00
|
|
|
{error, _} ->
|
|
|
|
{error, db_failure};
|
2016-01-13 21:38:08 +01:00
|
|
|
error ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Default = mod_mam_opt:default(LServer),
|
2016-01-13 21:38:08 +01:00
|
|
|
write_prefs(LUser, LServer, LServer, Default, [], [])
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2016-11-18 11:38:08 +01:00
|
|
|
select_and_send(LServer, Query, RSM, #iq{from = From, to = To} = IQ, MsgType) ->
|
2018-12-21 20:29:20 +01:00
|
|
|
Ret = case MsgType of
|
|
|
|
chat ->
|
|
|
|
select(LServer, From, From, Query, RSM, MsgType);
|
2019-02-21 10:36:33 +01:00
|
|
|
_ ->
|
2018-12-21 20:29:20 +01:00
|
|
|
select(LServer, From, To, Query, RSM, MsgType)
|
|
|
|
end,
|
|
|
|
case Ret of
|
|
|
|
{Msgs, IsComplete, Count} ->
|
|
|
|
SortedMsgs = lists:keysort(2, Msgs),
|
|
|
|
send(SortedMsgs, Count, IsComplete, IQ);
|
|
|
|
{error, _} ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Database failure"),
|
2018-12-21 20:29:20 +01:00
|
|
|
Err = xmpp:err_internal_server_error(Txt, IQ#iq.lang),
|
|
|
|
xmpp:make_error(IQ, Err)
|
|
|
|
end.
|
2015-06-22 15:56:08 +02:00
|
|
|
|
2019-05-02 11:12:22 +02:00
|
|
|
select(LServer, JidRequestor, JidArchive, Query, RSM, MsgType) ->
|
|
|
|
select(LServer, JidRequestor, JidArchive, Query, RSM, MsgType, all).
|
|
|
|
|
2016-11-18 11:38:08 +01:00
|
|
|
select(_LServer, JidRequestor, JidArchive, Query, RSM,
|
2015-09-01 10:37:07 +02:00
|
|
|
{groupchat, _Role, #state{config = #config{mam = false},
|
2019-05-02 11:12:22 +02:00
|
|
|
history = History}} = MsgType, _Flags) ->
|
2016-11-18 11:38:08 +01:00
|
|
|
Start = proplists:get_value(start, Query),
|
|
|
|
End = proplists:get_value('end', Query),
|
2017-03-10 13:12:43 +01:00
|
|
|
#lqueue{queue = Q} = History,
|
|
|
|
L = p1_queue:len(Q),
|
2016-10-17 12:37:23 +02:00
|
|
|
Msgs =
|
|
|
|
lists:flatmap(
|
|
|
|
fun({Nick, Pkt, _HaveSubject, Now, _Size}) ->
|
2017-07-20 20:22:50 +02:00
|
|
|
TS = misc:now_to_usec(Now),
|
2015-09-01 10:37:07 +02:00
|
|
|
case match_interval(Now, Start, End) and
|
|
|
|
match_rsm(Now, RSM) of
|
|
|
|
true ->
|
2017-01-20 21:21:06 +01:00
|
|
|
case msg_to_el(#archive_msg{
|
|
|
|
id = integer_to_binary(TS),
|
|
|
|
type = groupchat,
|
|
|
|
timestamp = Now,
|
|
|
|
peer = undefined,
|
|
|
|
nick = Nick,
|
|
|
|
packet = Pkt},
|
|
|
|
MsgType, JidRequestor, JidArchive) of
|
|
|
|
{ok, Msg} ->
|
|
|
|
[{integer_to_binary(TS), TS, Msg}];
|
|
|
|
{error, _} ->
|
|
|
|
[]
|
|
|
|
end;
|
2015-09-01 10:37:07 +02:00
|
|
|
false ->
|
2016-10-17 12:37:23 +02:00
|
|
|
[]
|
2015-09-01 10:37:07 +02:00
|
|
|
end
|
2017-03-10 13:12:43 +01:00
|
|
|
end, p1_queue:to_list(Q)),
|
2015-09-01 10:37:07 +02:00
|
|
|
case RSM of
|
2016-07-25 12:50:30 +02:00
|
|
|
#rsm_set{max = Max, before = Before} when is_binary(Before) ->
|
2015-09-01 10:37:07 +02:00
|
|
|
{NewMsgs, IsComplete} = filter_by_max(lists:reverse(Msgs), Max),
|
|
|
|
{NewMsgs, IsComplete, L};
|
2016-07-25 12:50:30 +02:00
|
|
|
#rsm_set{max = Max} ->
|
2015-09-01 10:37:07 +02:00
|
|
|
{NewMsgs, IsComplete} = filter_by_max(Msgs, Max),
|
|
|
|
{NewMsgs, IsComplete, L};
|
|
|
|
_ ->
|
|
|
|
{Msgs, true, L}
|
|
|
|
end;
|
2019-05-02 11:12:22 +02:00
|
|
|
select(LServer, JidRequestor, JidArchive, Query, RSM, MsgType, Flags) ->
|
2017-09-24 15:04:09 +02:00
|
|
|
case might_expose_jid(Query, MsgType) of
|
2017-09-24 02:05:50 +02:00
|
|
|
true ->
|
|
|
|
{[], true, 0};
|
|
|
|
false ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case {MsgType, mod_mam_opt:user_mucsub_from_muc_archive(LServer)} of
|
2019-03-28 17:42:25 +01:00
|
|
|
{chat, true} ->
|
2019-05-02 11:12:22 +02:00
|
|
|
select_with_mucsub(LServer, JidRequestor, JidArchive, Query, RSM, Flags);
|
2019-03-28 17:42:25 +01:00
|
|
|
_ ->
|
2019-05-02 11:12:22 +02:00
|
|
|
db_select(LServer, JidRequestor, JidArchive, Query, RSM, MsgType, Flags)
|
2019-03-28 17:42:25 +01:00
|
|
|
end
|
2017-09-24 02:05:50 +02:00
|
|
|
end.
|
2015-06-22 15:56:08 +02:00
|
|
|
|
2019-05-02 11:12:22 +02:00
|
|
|
select_with_mucsub(LServer, JidRequestor, JidArchive, Query, RSM, Flags) ->
|
2019-03-28 17:42:25 +01:00
|
|
|
MucHosts = mod_muc_admin:find_hosts(LServer),
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
case proplists:get_value(with, Query) of
|
|
|
|
#jid{lserver = WithLServer} = MucJid ->
|
|
|
|
case lists:member(WithLServer, MucHosts) of
|
|
|
|
true ->
|
|
|
|
select(LServer, JidRequestor, MucJid, Query, RSM,
|
|
|
|
{groupchat, member, #state{config = #config{mam = true}}});
|
|
|
|
_ ->
|
2019-05-02 11:12:22 +02:00
|
|
|
db_select(LServer, JidRequestor, JidArchive, Query, RSM, chat, Flags)
|
2019-03-28 17:42:25 +01:00
|
|
|
end;
|
|
|
|
_ ->
|
2019-05-02 11:12:22 +02:00
|
|
|
case erlang:function_exported(Mod, select_with_mucsub, 6) of
|
2019-03-28 17:42:25 +01:00
|
|
|
true ->
|
2019-05-02 11:12:22 +02:00
|
|
|
Mod:select_with_mucsub(LServer, JidRequestor, JidArchive, Query, RSM, Flags);
|
2019-03-28 17:42:25 +01:00
|
|
|
false ->
|
2019-05-02 11:12:22 +02:00
|
|
|
select_with_mucsub_fallback(LServer, JidRequestor, JidArchive, Query, RSM, Flags)
|
2019-04-03 13:50:56 +02:00
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2019-05-02 11:12:22 +02:00
|
|
|
select_with_mucsub_fallback(LServer, JidRequestor, JidArchive, Query, RSM, Flags) ->
|
|
|
|
case db_select(LServer, JidRequestor, JidArchive, Query, RSM, chat, Flags) of
|
2019-04-03 13:50:56 +02:00
|
|
|
{error, _} = Err ->
|
|
|
|
Err;
|
|
|
|
{Entries, All, Count} ->
|
|
|
|
{Dir, Max} = case RSM of
|
|
|
|
#rsm_set{max = M, before = V} when is_binary(V) ->
|
|
|
|
{desc, M};
|
|
|
|
#rsm_set{max = M} ->
|
|
|
|
{asc, M};
|
|
|
|
_ ->
|
|
|
|
{asc, undefined}
|
|
|
|
end,
|
|
|
|
SubRooms = case mod_muc_admin:find_hosts(LServer) of
|
|
|
|
[First|_] ->
|
|
|
|
case mod_muc:get_subscribed_rooms(First, JidRequestor) of
|
|
|
|
{ok, L} -> L;
|
|
|
|
{error, _} -> []
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
SubRoomJids = [Jid || {Jid, _} <- SubRooms],
|
|
|
|
{E2, A2, C2} =
|
|
|
|
lists:foldl(
|
|
|
|
fun(MucJid, {E0, A0, C0}) ->
|
|
|
|
case select(LServer, JidRequestor, MucJid, Query, RSM,
|
|
|
|
{groupchat, member, #state{config = #config{mam = true}}}) of
|
|
|
|
{error, _} ->
|
|
|
|
{E0, A0, C0};
|
|
|
|
{E, A, C} ->
|
|
|
|
{lists:keymerge(2, E0, wrap_as_mucsub(E, JidRequestor)),
|
|
|
|
A0 andalso A, C0 + C}
|
|
|
|
end
|
|
|
|
end, {Entries, All, Count}, SubRoomJids),
|
|
|
|
case {Dir, Max} of
|
|
|
|
{_, undefined} ->
|
|
|
|
{E2, A2, C2};
|
|
|
|
{desc, _} ->
|
|
|
|
Start = case length(E2) of
|
|
|
|
Len when Len < Max -> 1;
|
|
|
|
Len -> Len - Max + 1
|
|
|
|
end,
|
|
|
|
Sub = lists:sublist(E2, Start, Max),
|
|
|
|
{Sub, if Sub == E2 -> A2; true -> false end, C2};
|
|
|
|
_ ->
|
|
|
|
Sub = lists:sublist(E2, 1, Max),
|
|
|
|
{Sub, if Sub == E2 -> A2; true -> false end, C2}
|
2019-03-28 17:42:25 +01:00
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2019-05-02 11:12:22 +02:00
|
|
|
db_select(LServer, JidRequestor, JidArchive, Query, RSM, MsgType, Flags) ->
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
case erlang:function_exported(Mod, select, 7) of
|
|
|
|
true ->
|
|
|
|
Mod:select(LServer, JidRequestor, JidArchive, Query, RSM, MsgType, Flags);
|
|
|
|
_ ->
|
|
|
|
Mod:select(LServer, JidRequestor, JidArchive, Query, RSM, MsgType)
|
|
|
|
end.
|
|
|
|
|
2019-03-28 17:42:25 +01:00
|
|
|
wrap_as_mucsub(Messages, #jid{lserver = LServer} = Requester) ->
|
|
|
|
ReqBare = jid:remove_resource(Requester),
|
|
|
|
ReqServer = jid:make(<<>>, LServer, <<>>),
|
|
|
|
[{T1, T2, wrap_as_mucsub(M, ReqBare, ReqServer)} || {T1, T2, M} <- Messages].
|
|
|
|
|
|
|
|
wrap_as_mucsub(Message, Requester, ReqServer) ->
|
|
|
|
case Message of
|
|
|
|
#forwarded{delay = #delay{stamp = Stamp, desc = Desc},
|
2019-04-09 11:16:27 +02:00
|
|
|
sub_els = [#message{from = From, sub_els = SubEls, subject = Subject} = Msg]} ->
|
2019-06-14 11:33:26 +02:00
|
|
|
{L1, SubEls2} = case lists:keytake(mam_archived, 1, SubEls) of
|
2019-03-28 17:42:25 +01:00
|
|
|
{value, Arch, Rest} ->
|
|
|
|
{[Arch#mam_archived{by = Requester}], Rest};
|
|
|
|
_ ->
|
|
|
|
{[], SubEls}
|
|
|
|
end,
|
|
|
|
{Sid, L2, SubEls3} = case lists:keytake(stanza_id, 1, SubEls2) of
|
|
|
|
{value, #stanza_id{id = Sid0} = SID, Rest2} ->
|
|
|
|
{Sid0, [SID#stanza_id{by = Requester} | L1], Rest2};
|
|
|
|
_ ->
|
|
|
|
{p1_rand:get_string(), L1, SubEls2}
|
|
|
|
end,
|
|
|
|
Msg2 = Msg#message{to = Requester, sub_els = SubEls3},
|
2019-04-09 11:16:27 +02:00
|
|
|
Node = case Subject of
|
|
|
|
[] ->
|
|
|
|
?NS_MUCSUB_NODES_MESSAGES;
|
|
|
|
_ ->
|
|
|
|
?NS_MUCSUB_NODES_SUBJECT
|
|
|
|
end,
|
2019-03-28 17:42:25 +01:00
|
|
|
#forwarded{delay = #delay{stamp = Stamp, desc = Desc, from = ReqServer},
|
|
|
|
sub_els = [
|
|
|
|
#message{from = jid:remove_resource(From), to = Requester,
|
|
|
|
id = Sid,
|
|
|
|
sub_els = [#ps_event{
|
|
|
|
items = #ps_items{
|
2019-04-09 11:16:27 +02:00
|
|
|
node = Node,
|
2019-03-28 17:42:25 +01:00
|
|
|
items = [#ps_item{
|
|
|
|
id = Sid,
|
|
|
|
sub_els = [Msg2]
|
|
|
|
}]}} | L2]}]};
|
|
|
|
_ ->
|
|
|
|
Message
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2017-01-20 21:21:06 +01:00
|
|
|
msg_to_el(#archive_msg{timestamp = TS, packet = El, nick = Nick,
|
|
|
|
peer = Peer, id = ID},
|
2016-01-20 00:02:40 +01:00
|
|
|
MsgType, JidRequestor, #jid{lserver = LServer} = JidArchive) ->
|
2019-06-21 20:06:32 +02:00
|
|
|
CodecOpts = ejabberd_config:codec_options(),
|
2018-02-09 16:12:50 +01:00
|
|
|
try xmpp:decode(El, ?NS_CLIENT, CodecOpts) of
|
2017-01-20 21:21:06 +01:00
|
|
|
Pkt1 ->
|
2018-12-05 11:14:29 +01:00
|
|
|
Pkt2 = case MsgType of
|
|
|
|
chat -> set_stanza_id(Pkt1, JidArchive, ID);
|
|
|
|
{groupchat, _, _} -> set_stanza_id(Pkt1, JidArchive, ID);
|
|
|
|
_ -> Pkt1
|
|
|
|
end,
|
2017-01-20 21:21:06 +01:00
|
|
|
Pkt3 = maybe_update_from_to(
|
|
|
|
Pkt2, JidRequestor, JidArchive, Peer, MsgType, Nick),
|
|
|
|
Delay = #delay{stamp = TS, from = jid:make(LServer)},
|
2018-01-25 18:02:47 +01:00
|
|
|
{ok, #forwarded{sub_els = [Pkt3], delay = Delay}}
|
2017-01-20 21:21:06 +01:00
|
|
|
catch _:{xmpp_codec, Why} ->
|
|
|
|
?ERROR_MSG("Failed to decode raw element ~p from message "
|
|
|
|
"archive of user ~s: ~s",
|
2017-02-26 08:07:12 +01:00
|
|
|
[El, jid:encode(JidArchive), xmpp:format_error(Why)]),
|
2017-01-20 21:21:06 +01:00
|
|
|
{error, invalid_xml}
|
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
maybe_update_from_to(#message{sub_els = Els} = Pkt, JidRequestor, JidArchive,
|
2016-01-20 00:02:40 +01:00
|
|
|
Peer, {groupchat, Role,
|
|
|
|
#state{config = #config{anonymous = Anon}}},
|
|
|
|
Nick) ->
|
|
|
|
ExposeJID = case {Peer, JidRequestor} of
|
|
|
|
{undefined, _JidRequestor} ->
|
|
|
|
false;
|
|
|
|
{{U, S, _R}, #jid{luser = U, lserver = S}} ->
|
|
|
|
true;
|
|
|
|
{_Peer, _JidRequestor} when not Anon; Role == moderator ->
|
|
|
|
true;
|
|
|
|
{_Peer, _JidRequestor} ->
|
|
|
|
false
|
|
|
|
end,
|
|
|
|
Items = case ExposeJID of
|
|
|
|
true ->
|
2016-07-25 12:50:30 +02:00
|
|
|
[#muc_user{items = [#muc_item{jid = Peer}]}];
|
2016-01-20 00:02:40 +01:00
|
|
|
false ->
|
2015-08-06 12:33:39 +02:00
|
|
|
[]
|
|
|
|
end,
|
2016-07-25 12:50:30 +02:00
|
|
|
Pkt#message{from = jid:replace_resource(JidArchive, Nick),
|
|
|
|
to = undefined,
|
|
|
|
sub_els = Items ++ Els};
|
2018-12-05 11:14:29 +01:00
|
|
|
maybe_update_from_to(Pkt, _JidRequestor, _JidArchive, _Peer, _MsgType, _Nick) ->
|
2016-01-18 13:27:29 +01:00
|
|
|
Pkt.
|
2015-06-22 15:56:08 +02:00
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec send([{binary(), integer(), xmlel()}],
|
2018-12-21 20:29:20 +01:00
|
|
|
count(), boolean(), iq()) -> iq() | ignore.
|
2016-07-25 12:50:30 +02:00
|
|
|
send(Msgs, Count, IsComplete,
|
|
|
|
#iq{from = From, to = To,
|
|
|
|
sub_els = [#mam_query{id = QID, xmlns = NS}]} = IQ) ->
|
2016-11-12 11:27:15 +01:00
|
|
|
Hint = #hint{type = 'no-store'},
|
2015-06-22 15:56:08 +02:00
|
|
|
Els = lists:map(
|
|
|
|
fun({ID, _IDInt, El}) ->
|
2017-02-16 09:00:26 +01:00
|
|
|
#message{from = To,
|
|
|
|
to = From,
|
|
|
|
sub_els = [#mam_result{xmlns = NS,
|
2016-07-25 12:50:30 +02:00
|
|
|
id = ID,
|
|
|
|
queryid = QID,
|
|
|
|
sub_els = [El]}]}
|
2015-06-22 15:56:08 +02:00
|
|
|
end, Msgs),
|
2016-07-25 12:50:30 +02:00
|
|
|
RSMOut = make_rsm_out(Msgs, Count),
|
|
|
|
Result = if NS == ?NS_MAM_TMP ->
|
|
|
|
#mam_query{xmlns = NS, id = QID, rsm = RSMOut};
|
|
|
|
true ->
|
2016-09-13 11:30:05 +02:00
|
|
|
#mam_fin{xmlns = NS, id = QID, rsm = RSMOut,
|
|
|
|
complete = IsComplete}
|
2016-07-25 12:50:30 +02:00
|
|
|
end,
|
2017-11-08 13:15:39 +01:00
|
|
|
if NS /= ?NS_MAM_0 ->
|
2015-06-22 15:56:08 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(El) ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route(El)
|
2015-06-22 15:56:08 +02:00
|
|
|
end, Els),
|
2016-07-25 12:50:30 +02:00
|
|
|
xmpp:make_iq_result(IQ, Result);
|
2017-11-08 13:15:39 +01:00
|
|
|
true ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route(xmpp:make_iq_result(IQ)),
|
2015-06-22 15:56:08 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(El) ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route(El)
|
2015-06-22 15:56:08 +02:00
|
|
|
end, Els),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route(
|
|
|
|
#message{from = To, to = From, sub_els = [Result, Hint]}),
|
2015-06-22 15:56:08 +02:00
|
|
|
ignore
|
|
|
|
end.
|
|
|
|
|
2018-12-21 20:29:20 +01:00
|
|
|
-spec make_rsm_out([{binary(), integer(), xmlel()}], count()) -> rsm_set().
|
2016-07-25 12:50:30 +02:00
|
|
|
make_rsm_out([], Count) ->
|
|
|
|
#rsm_set{count = Count};
|
|
|
|
make_rsm_out([{FirstID, _, _}|_] = Msgs, Count) ->
|
2015-06-22 15:56:08 +02:00
|
|
|
{LastID, _, _} = lists:last(Msgs),
|
2016-07-25 12:50:30 +02:00
|
|
|
#rsm_set{first = #rsm_first{data = FirstID}, last = LastID, count = Count}.
|
2015-06-22 15:56:08 +02:00
|
|
|
|
|
|
|
filter_by_max(Msgs, undefined) ->
|
2015-07-10 12:02:42 +02:00
|
|
|
{Msgs, true};
|
2015-06-22 15:56:08 +02:00
|
|
|
filter_by_max(Msgs, Len) when is_integer(Len), Len >= 0 ->
|
2015-07-10 12:02:42 +02:00
|
|
|
{lists:sublist(Msgs, Len), length(Msgs) =< Len};
|
2015-06-22 15:56:08 +02:00
|
|
|
filter_by_max(_Msgs, _Junk) ->
|
2015-07-10 12:02:42 +02:00
|
|
|
{[], true}.
|
2015-06-22 15:56:08 +02:00
|
|
|
|
2016-09-13 15:56:34 +02:00
|
|
|
-spec limit_max(rsm_set(), binary()) -> rsm_set() | undefined.
|
2016-01-14 02:12:28 +01:00
|
|
|
limit_max(RSM, ?NS_MAM_TMP) ->
|
|
|
|
RSM; % XEP-0313 v0.2 doesn't require clients to support RSM.
|
2016-11-12 11:27:15 +01:00
|
|
|
limit_max(undefined, _NS) ->
|
|
|
|
#rsm_set{max = ?DEF_PAGE_SIZE};
|
2016-07-25 12:50:30 +02:00
|
|
|
limit_max(#rsm_set{max = Max} = RSM, _NS) when not is_integer(Max) ->
|
|
|
|
RSM#rsm_set{max = ?DEF_PAGE_SIZE};
|
|
|
|
limit_max(#rsm_set{max = Max} = RSM, _NS) when Max > ?MAX_PAGE_SIZE ->
|
|
|
|
RSM#rsm_set{max = ?MAX_PAGE_SIZE};
|
2016-01-14 02:12:28 +01:00
|
|
|
limit_max(RSM, _NS) ->
|
|
|
|
RSM.
|
|
|
|
|
2016-09-13 11:30:05 +02:00
|
|
|
match_interval(Now, Start, undefined) ->
|
|
|
|
Now >= Start;
|
2015-09-01 10:37:07 +02:00
|
|
|
match_interval(Now, Start, End) ->
|
|
|
|
(Now >= Start) and (Now =< End).
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
match_rsm(Now, #rsm_set{'after' = ID}) when is_binary(ID), ID /= <<"">> ->
|
2017-07-20 20:22:50 +02:00
|
|
|
Now1 = (catch misc:usec_to_now(binary_to_integer(ID))),
|
2015-09-01 10:37:07 +02:00
|
|
|
Now > Now1;
|
2016-07-25 12:50:30 +02:00
|
|
|
match_rsm(Now, #rsm_set{before = ID}) when is_binary(ID), ID /= <<"">> ->
|
2017-07-20 20:22:50 +02:00
|
|
|
Now1 = (catch misc:usec_to_now(binary_to_integer(ID))),
|
2015-09-01 10:37:07 +02:00
|
|
|
Now < Now1;
|
|
|
|
match_rsm(_Now, _) ->
|
|
|
|
true.
|
|
|
|
|
2017-09-24 15:04:09 +02:00
|
|
|
might_expose_jid(Query,
|
|
|
|
{groupchat, Role, #state{config = #config{anonymous = true}}})
|
2017-09-24 02:05:50 +02:00
|
|
|
when Role /= moderator ->
|
2017-09-24 15:04:09 +02:00
|
|
|
proplists:is_defined(with, Query);
|
|
|
|
might_expose_jid(_Query, _MsgType) ->
|
2017-09-24 02:05:50 +02:00
|
|
|
false.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
get_jids(undefined) ->
|
|
|
|
[];
|
|
|
|
get_jids(Js) ->
|
|
|
|
[jid:tolower(jid:remove_resource(J)) || J <- Js].
|
2015-06-22 15:56:08 +02:00
|
|
|
|
2016-01-26 10:00:11 +01:00
|
|
|
get_commands_spec() ->
|
2016-01-13 22:46:30 +01:00
|
|
|
[#ejabberd_commands{name = delete_old_mam_messages, tags = [purge],
|
|
|
|
desc = "Delete MAM messages older than DAYS",
|
|
|
|
longdesc = "Valid message TYPEs: "
|
|
|
|
"\"chat\", \"groupchat\", \"all\".",
|
|
|
|
module = ?MODULE, function = delete_old_messages,
|
2017-07-21 11:18:44 +02:00
|
|
|
args_desc = ["Type of messages to delete (chat, groupchat, all)",
|
|
|
|
"Days to keep messages"],
|
|
|
|
args_example = [<<"all">>, 31],
|
2016-01-13 22:46:30 +01:00
|
|
|
args = [{type, binary}, {days, integer}],
|
2018-04-20 13:27:39 +02:00
|
|
|
result = {res, rescode}},
|
|
|
|
#ejabberd_commands{name = remove_mam_for_user, tags = [mam],
|
|
|
|
desc = "Remove mam archive for user",
|
|
|
|
module = ?MODULE, function = remove_mam_for_user,
|
2019-06-19 09:26:28 +02:00
|
|
|
args = [{user, binary}, {host, binary}],
|
|
|
|
args_rename = [{server, host}],
|
2018-04-20 13:27:39 +02:00
|
|
|
args_desc = ["Username", "Server"],
|
|
|
|
args_example = [<<"bob">>, <<"example.com">>],
|
|
|
|
result = {res, restuple},
|
|
|
|
result_desc = "Result tuple",
|
|
|
|
result_example = {ok, <<"MAM archive removed">>}},
|
|
|
|
#ejabberd_commands{name = remove_mam_for_user_with_peer, tags = [mam],
|
|
|
|
desc = "Remove mam archive for user with peer",
|
|
|
|
module = ?MODULE, function = remove_mam_for_user_with_peer,
|
2019-06-19 09:26:28 +02:00
|
|
|
args = [{user, binary}, {host, binary}, {with, binary}],
|
|
|
|
args_rename = [{server, host}],
|
2018-04-20 13:27:39 +02:00
|
|
|
args_desc = ["Username", "Server", "Peer"],
|
|
|
|
args_example = [<<"bob">>, <<"example.com">>, <<"anne@example.com">>],
|
|
|
|
result = {res, restuple},
|
|
|
|
result_desc = "Result tuple",
|
|
|
|
result_example = {ok, <<"MAM archive removed">>}}
|
|
|
|
].
|
2016-01-13 22:46:30 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(compress_xml) ->
|
|
|
|
econf:bool();
|
2016-01-15 01:08:22 +01:00
|
|
|
mod_opt_type(assume_mam_usage) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:bool();
|
2015-06-22 15:56:08 +02:00
|
|
|
mod_opt_type(default) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:enum([always, never, roster]);
|
2016-01-13 21:38:08 +01:00
|
|
|
mod_opt_type(request_activates_archiving) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:bool();
|
2019-01-13 10:35:03 +01:00
|
|
|
mod_opt_type(clear_archive_on_room_destroy) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:bool();
|
2019-03-28 17:42:25 +01:00
|
|
|
mod_opt_type(user_mucsub_from_muc_archive) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:bool();
|
2019-01-13 11:37:08 +01:00
|
|
|
mod_opt_type(access_preferences) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:acl();
|
|
|
|
mod_opt_type(db_type) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:db_type(?MODULE);
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(use_cache) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:bool();
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(cache_size) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:pos_int(infinity);
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(cache_missed) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:bool();
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(cache_life_time) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:timeout(second, infinity).
|
2018-01-23 08:54:52 +01:00
|
|
|
|
|
|
|
mod_options(Host) ->
|
|
|
|
[{assume_mam_usage, false},
|
|
|
|
{default, never},
|
|
|
|
{request_activates_archiving, false},
|
2018-11-28 11:25:04 +01:00
|
|
|
{compress_xml, false},
|
2019-01-13 10:35:03 +01:00
|
|
|
{clear_archive_on_room_destroy, true},
|
2019-01-13 11:37:08 +01:00
|
|
|
{access_preferences, all},
|
2019-03-28 17:42:25 +01:00
|
|
|
{user_mucsub_from_muc_archive, false},
|
2018-01-23 08:54:52 +01:00
|
|
|
{db_type, ejabberd_config:default_db(Host, ?MODULE)},
|
2019-06-14 11:33:26 +02:00
|
|
|
{use_cache, ejabberd_option:use_cache(Host)},
|
|
|
|
{cache_size, ejabberd_option:cache_size(Host)},
|
|
|
|
{cache_missed, ejabberd_option:cache_missed(Host)},
|
|
|
|
{cache_life_time, ejabberd_option:cache_life_time(Host)}].
|