2003-01-05 21:24:59 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_offline.erl
|
2007-08-07 18:43:02 +02:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2012-04-27 11:52:05 +02:00
|
|
|
%%% Purpose : Store and manage offline messages.
|
2007-08-07 18:43:02 +02:00
|
|
|
%%% Created : 5 Jan 2003 by Alexey Shchepin <alexey@process-one.net>
|
2007-12-24 13:58:05 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2020-01-28 13:34:02 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2020 ProcessOne
|
2007-12-24 13:58:05 +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 13:58:05 +01:00
|
|
|
%%%
|
2003-01-05 21:24:59 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_offline).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2007-08-07 18:43:02 +02:00
|
|
|
-author('alexey@process-one.net').
|
2015-05-21 17:02:36 +02:00
|
|
|
|
2016-02-08 21:35:46 +01:00
|
|
|
-protocol({xep, 13, '1.2'}).
|
2015-05-21 17:02:36 +02:00
|
|
|
-protocol({xep, 22, '1.4'}).
|
|
|
|
-protocol({xep, 23, '1.3'}).
|
|
|
|
-protocol({xep, 160, '1.0'}).
|
2015-12-09 22:28:44 +01:00
|
|
|
-protocol({xep, 334, '0.2'}).
|
2015-05-21 17:02:36 +02:00
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
-export([start/2,
|
|
|
|
stop/1,
|
2017-02-22 17:46:47 +01:00
|
|
|
reload/3,
|
2017-02-23 16:55:35 +01:00
|
|
|
store_packet/1,
|
2017-05-21 22:21:13 +02:00
|
|
|
store_offline_msg/1,
|
2017-01-09 15:02:17 +01:00
|
|
|
c2s_self_presence/1,
|
2009-06-30 18:51:15 +02:00
|
|
|
get_sm_features/5,
|
2016-02-09 15:59:54 +01:00
|
|
|
get_sm_identity/5,
|
|
|
|
get_sm_items/5,
|
|
|
|
get_info/5,
|
2016-07-19 06:56:14 +02:00
|
|
|
handle_offline_query/1,
|
2012-04-27 11:52:05 +02:00
|
|
|
remove_expired_messages/1,
|
|
|
|
remove_old_messages/2,
|
2007-08-23 02:51:54 +02:00
|
|
|
remove_user/2,
|
2016-11-22 14:48:01 +01:00
|
|
|
import_info/0,
|
|
|
|
import_start/2,
|
|
|
|
import/5,
|
2015-10-07 00:06:58 +02:00
|
|
|
export/1,
|
2010-01-12 13:02:50 +01:00
|
|
|
get_queue_length/2,
|
2016-03-04 11:09:14 +01:00
|
|
|
count_offline_messages/2,
|
2015-10-07 00:06:58 +02:00
|
|
|
get_offline_els/2,
|
2016-04-15 12:44:33 +02:00
|
|
|
find_x_expire/2,
|
2016-12-11 13:03:37 +01:00
|
|
|
c2s_handle_info/2,
|
2017-01-09 15:02:17 +01:00
|
|
|
c2s_copy_session/2,
|
2007-08-23 02:51:54 +02:00
|
|
|
webadmin_page/3,
|
2008-10-12 16:16:05 +02:00
|
|
|
webadmin_user/4,
|
2019-05-28 14:32:09 +02:00
|
|
|
webadmin_user_parse_query/5]).
|
2003-01-05 21:24:59 +01:00
|
|
|
|
2020-01-08 10:24:51 +01:00
|
|
|
-export([mod_opt_type/1, mod_options/1, mod_doc/0, depends/2]).
|
2014-07-31 12:25:37 +02:00
|
|
|
|
2016-03-04 11:09:14 +01:00
|
|
|
-deprecated({get_queue_length,2}).
|
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2016-07-19 06:56:14 +02:00
|
|
|
-include("xmpp.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("ejabberd_http.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("ejabberd_web_admin.hrl").
|
2003-01-05 21:24:59 +01:00
|
|
|
|
2015-10-07 00:06:58 +02:00
|
|
|
-include("mod_offline.hrl").
|
2014-07-31 12:25:37 +02:00
|
|
|
|
2019-06-22 16:08:45 +02:00
|
|
|
-include("translate.hrl").
|
|
|
|
|
2004-08-22 23:54:14 +02:00
|
|
|
-define(OFFLINE_TABLE_LOCK_THRESHOLD, 1000).
|
2003-01-05 21:24:59 +01:00
|
|
|
|
2009-06-15 19:43:18 +02:00
|
|
|
%% default value for the maximum number of user messages
|
|
|
|
-define(MAX_USER_MESSAGES, infinity).
|
|
|
|
|
2019-06-30 20:14:37 +02:00
|
|
|
-define(SPOOL_COUNTER_CACHE, offline_msg_counter_cache).
|
2019-05-28 14:32:09 +02:00
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-type c2s_state() :: ejabberd_c2s:state().
|
|
|
|
|
2016-04-15 12:44:33 +02:00
|
|
|
-callback init(binary(), gen_mod:opts()) -> any().
|
2016-11-22 14:48:01 +01:00
|
|
|
-callback import(#offline_msg{}) -> ok.
|
2017-05-21 22:21:13 +02:00
|
|
|
-callback store_message(#offline_msg{}) -> ok | {error, any()}.
|
2016-04-15 12:44:33 +02:00
|
|
|
-callback pop_messages(binary(), binary()) ->
|
2017-02-18 07:36:27 +01:00
|
|
|
{ok, [#offline_msg{}]} | {error, any()}.
|
2016-04-15 12:44:33 +02:00
|
|
|
-callback remove_expired_messages(binary()) -> {atomic, any()}.
|
|
|
|
-callback remove_old_messages(non_neg_integer(), binary()) -> {atomic, any()}.
|
2017-02-18 07:36:27 +01:00
|
|
|
-callback remove_user(binary(), binary()) -> any().
|
2016-11-18 11:38:08 +01:00
|
|
|
-callback read_message_headers(binary(), binary()) ->
|
2019-07-01 13:36:05 +02:00
|
|
|
[{non_neg_integer(), jid(), jid(), undefined | erlang:timestamp(), xmlel()}] | error.
|
2016-04-15 12:44:33 +02:00
|
|
|
-callback read_message(binary(), binary(), non_neg_integer()) ->
|
|
|
|
{ok, #offline_msg{}} | error.
|
2016-11-08 13:15:19 +01:00
|
|
|
-callback remove_message(binary(), binary(), non_neg_integer()) -> ok | {error, any()}.
|
2016-04-15 12:44:33 +02:00
|
|
|
-callback read_all_messages(binary(), binary()) -> [#offline_msg{}].
|
|
|
|
-callback remove_all_messages(binary(), binary()) -> {atomic, any()}.
|
2019-06-30 20:14:37 +02:00
|
|
|
-callback count_messages(binary(), binary()) -> {ets_cache:tag(), non_neg_integer()}.
|
|
|
|
-callback use_cache(binary()) -> boolean().
|
|
|
|
-callback cache_nodes(binary()) -> [node()].
|
2016-04-15 12:44:33 +02:00
|
|
|
|
2019-06-30 20:14:37 +02:00
|
|
|
-optional_callbacks([remove_expired_messages/1, remove_old_messages/2,
|
|
|
|
use_cache/1, cache_nodes/1]).
|
2019-01-08 11:41:16 +01:00
|
|
|
|
2016-07-06 13:58:48 +02:00
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[].
|
2014-07-31 12:25:37 +02:00
|
|
|
|
2017-05-21 22:21:13 +02:00
|
|
|
start(Host, Opts) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Mod = gen_mod:db_mod(Opts, ?MODULE),
|
2016-04-15 12:44:33 +02:00
|
|
|
Mod:init(Host, Opts),
|
2019-06-30 20:14:37 +02:00
|
|
|
init_cache(Mod, Host, Opts),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_hooks:add(offline_message_hook, Host, ?MODULE,
|
|
|
|
store_packet, 50),
|
2017-01-09 15:02:17 +01:00
|
|
|
ejabberd_hooks:add(c2s_self_presence, Host, ?MODULE, c2s_self_presence, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:add(remove_user, Host,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, remove_user, 50),
|
2009-06-30 18:51:15 +02:00
|
|
|
ejabberd_hooks:add(disco_sm_features, Host,
|
|
|
|
?MODULE, get_sm_features, 50),
|
|
|
|
ejabberd_hooks:add(disco_local_features, Host,
|
|
|
|
?MODULE, get_sm_features, 50),
|
2016-02-09 15:59:54 +01:00
|
|
|
ejabberd_hooks:add(disco_sm_identity, Host,
|
|
|
|
?MODULE, get_sm_identity, 50),
|
|
|
|
ejabberd_hooks:add(disco_sm_items, Host,
|
|
|
|
?MODULE, get_sm_items, 50),
|
|
|
|
ejabberd_hooks:add(disco_info, Host, ?MODULE, get_info, 50),
|
2016-12-11 13:03:37 +01:00
|
|
|
ejabberd_hooks:add(c2s_handle_info, Host, ?MODULE, c2s_handle_info, 50),
|
2017-01-09 15:02:17 +01:00
|
|
|
ejabberd_hooks:add(c2s_copy_session, Host, ?MODULE, c2s_copy_session, 50),
|
2007-08-23 02:51:54 +02:00
|
|
|
ejabberd_hooks:add(webadmin_page_host, Host,
|
|
|
|
?MODULE, webadmin_page, 50),
|
|
|
|
ejabberd_hooks:add(webadmin_user, Host,
|
|
|
|
?MODULE, webadmin_user, 50),
|
2008-10-12 16:16:05 +02:00
|
|
|
ejabberd_hooks:add(webadmin_user_parse_query, Host,
|
2015-10-07 00:06:58 +02:00
|
|
|
?MODULE, webadmin_user_parse_query, 50),
|
2016-02-09 15:59:54 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_FLEX_OFFLINE,
|
2018-02-11 10:54:15 +01:00
|
|
|
?MODULE, handle_offline_query).
|
2014-07-31 12:25:37 +02:00
|
|
|
|
2017-05-21 22:21:13 +02:00
|
|
|
stop(Host) ->
|
2014-07-31 12:25:37 +02:00
|
|
|
ejabberd_hooks:delete(offline_message_hook, Host,
|
|
|
|
?MODULE, store_packet, 50),
|
2017-01-09 15:02:17 +01:00
|
|
|
ejabberd_hooks:delete(c2s_self_presence, Host, ?MODULE, c2s_self_presence, 50),
|
2014-07-31 12:25:37 +02:00
|
|
|
ejabberd_hooks:delete(remove_user, Host, ?MODULE,
|
|
|
|
remove_user, 50),
|
|
|
|
ejabberd_hooks:delete(disco_sm_features, Host, ?MODULE, get_sm_features, 50),
|
|
|
|
ejabberd_hooks:delete(disco_local_features, Host, ?MODULE, get_sm_features, 50),
|
2016-02-09 15:59:54 +01:00
|
|
|
ejabberd_hooks:delete(disco_sm_identity, Host, ?MODULE, get_sm_identity, 50),
|
|
|
|
ejabberd_hooks:delete(disco_sm_items, Host, ?MODULE, get_sm_items, 50),
|
|
|
|
ejabberd_hooks:delete(disco_info, Host, ?MODULE, get_info, 50),
|
2016-12-11 13:03:37 +01:00
|
|
|
ejabberd_hooks:delete(c2s_handle_info, Host, ?MODULE, c2s_handle_info, 50),
|
2017-01-09 15:02:17 +01:00
|
|
|
ejabberd_hooks:delete(c2s_copy_session, Host, ?MODULE, c2s_copy_session, 50),
|
2014-07-31 12:25:37 +02:00
|
|
|
ejabberd_hooks:delete(webadmin_page_host, Host,
|
|
|
|
?MODULE, webadmin_page, 50),
|
|
|
|
ejabberd_hooks:delete(webadmin_user, Host,
|
|
|
|
?MODULE, webadmin_user, 50),
|
|
|
|
ejabberd_hooks:delete(webadmin_user_parse_query, Host,
|
|
|
|
?MODULE, webadmin_user_parse_query, 50),
|
2017-05-21 22:21:13 +02:00
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_FLEX_OFFLINE).
|
2003-02-13 20:39:13 +01:00
|
|
|
|
2017-05-21 22:21:13 +02: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),
|
2019-06-30 20:14:37 +02:00
|
|
|
init_cache(NewMod, Host, NewOpts),
|
2017-05-21 22:21:13 +02:00
|
|
|
if NewMod /= OldMod ->
|
|
|
|
NewMod:init(Host, NewOpts);
|
|
|
|
true ->
|
|
|
|
ok
|
2009-06-15 19:43:18 +02:00
|
|
|
end.
|
|
|
|
|
2019-06-30 20:14:37 +02:00
|
|
|
init_cache(Mod, Host, Opts) ->
|
|
|
|
CacheOpts = [{max_size, mod_offline_opt:cache_size(Opts)},
|
|
|
|
{life_time, mod_offline_opt:cache_life_time(Opts)},
|
|
|
|
{cache_missed, false}],
|
|
|
|
case use_cache(Mod, Host) of
|
|
|
|
true ->
|
|
|
|
ets_cache:new(?SPOOL_COUNTER_CACHE, CacheOpts);
|
|
|
|
false ->
|
|
|
|
ets_cache:delete(?SPOOL_COUNTER_CACHE)
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec use_cache(module(), binary()) -> boolean().
|
|
|
|
use_cache(Mod, Host) ->
|
|
|
|
case erlang:function_exported(Mod, use_cache, 1) of
|
|
|
|
true -> Mod:use_cache(Host);
|
|
|
|
false -> mod_offline_opt:use_cache(Host)
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec cache_nodes(module(), binary()) -> [node()].
|
|
|
|
cache_nodes(Mod, Host) ->
|
|
|
|
case erlang:function_exported(Mod, cache_nodes, 1) of
|
|
|
|
true -> Mod:cache_nodes(Host);
|
|
|
|
false -> ejabberd_cluster:get_nodes()
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec flush_cache(module(), binary(), binary()) -> ok.
|
|
|
|
flush_cache(Mod, User, Server) ->
|
|
|
|
case use_cache(Mod, Server) of
|
|
|
|
true ->
|
|
|
|
ets_cache:delete(?SPOOL_COUNTER_CACHE,
|
|
|
|
{User, Server},
|
|
|
|
cache_nodes(Mod, Server));
|
|
|
|
false ->
|
|
|
|
ok
|
2019-05-28 14:32:09 +02:00
|
|
|
end.
|
|
|
|
|
2017-05-21 22:21:13 +02:00
|
|
|
-spec store_offline_msg(#offline_msg{}) -> ok | {error, full | any()}.
|
2019-04-26 19:59:06 +02:00
|
|
|
store_offline_msg(#offline_msg{us = {User, Server}, packet = Pkt} = Msg) ->
|
2019-05-28 14:32:09 +02:00
|
|
|
UseMam = use_mam_for_user(User, Server),
|
2019-06-30 20:14:37 +02:00
|
|
|
Mod = gen_mod:db_mod(Server, ?MODULE),
|
2019-05-28 14:32:09 +02:00
|
|
|
case UseMam andalso xmpp:get_meta(Pkt, mam_archived, false) of
|
2019-04-26 19:59:06 +02:00
|
|
|
true ->
|
2019-07-01 13:36:05 +02:00
|
|
|
case count_offline_messages(User, Server) of
|
|
|
|
0 ->
|
|
|
|
store_message_in_db(Mod, Msg);
|
|
|
|
_ ->
|
|
|
|
case use_cache(Mod, Server) of
|
|
|
|
true ->
|
|
|
|
ets_cache:incr(
|
|
|
|
?SPOOL_COUNTER_CACHE,
|
|
|
|
{User, Server}, 1,
|
|
|
|
cache_nodes(Mod, Server));
|
|
|
|
false ->
|
|
|
|
ok
|
2019-05-28 14:32:09 +02:00
|
|
|
end
|
2019-07-01 13:36:05 +02:00
|
|
|
end;
|
2019-05-02 11:12:47 +02:00
|
|
|
false ->
|
2019-04-26 19:59:06 +02:00
|
|
|
case get_max_user_messages(User, Server) of
|
|
|
|
infinity ->
|
2019-06-30 20:14:37 +02:00
|
|
|
store_message_in_db(Mod, Msg);
|
2019-04-26 19:59:06 +02:00
|
|
|
Limit ->
|
2019-07-01 13:36:05 +02:00
|
|
|
Num = count_offline_messages(User, Server),
|
2019-04-26 19:59:06 +02:00
|
|
|
if Num < Limit ->
|
2019-06-30 20:14:37 +02:00
|
|
|
store_message_in_db(Mod, Msg);
|
|
|
|
true ->
|
2019-04-26 19:59:06 +02:00
|
|
|
{error, full}
|
|
|
|
end
|
2017-05-21 22:21:13 +02:00
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_max_user_messages(User, Server) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Access = mod_offline_opt:access_max_user_messages(Server),
|
|
|
|
case ejabberd_shaper:match(Server, Access, jid:make(User, Server)) of
|
2009-06-15 19:43:18 +02:00
|
|
|
Max when is_integer(Max) -> Max;
|
|
|
|
infinity -> infinity;
|
|
|
|
_ -> ?MAX_USER_MESSAGES
|
2003-02-13 20:39:13 +01:00
|
|
|
end.
|
|
|
|
|
2016-08-05 07:41:08 +02:00
|
|
|
get_sm_features(Acc, _From, _To, <<"">>, _Lang) ->
|
2009-06-30 18:51:15 +02:00
|
|
|
Feats = case Acc of
|
|
|
|
{result, I} -> I;
|
|
|
|
_ -> []
|
|
|
|
end,
|
2016-02-09 15:59:54 +01:00
|
|
|
{result, Feats ++ [?NS_FEATURE_MSGOFFLINE, ?NS_FLEX_OFFLINE]};
|
2009-06-30 18:51:15 +02:00
|
|
|
|
|
|
|
get_sm_features(_Acc, _From, _To, ?NS_FEATURE_MSGOFFLINE, _Lang) ->
|
|
|
|
%% override all lesser features...
|
|
|
|
{result, []};
|
|
|
|
|
2016-02-09 15:59:54 +01:00
|
|
|
get_sm_features(_Acc, #jid{luser = U, lserver = S}, #jid{luser = U, lserver = S},
|
|
|
|
?NS_FLEX_OFFLINE, _Lang) ->
|
|
|
|
{result, [?NS_FLEX_OFFLINE]};
|
|
|
|
|
2009-06-30 18:51:15 +02:00
|
|
|
get_sm_features(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc.
|
|
|
|
|
2016-07-19 06:56:14 +02:00
|
|
|
get_sm_identity(Acc, #jid{luser = U, lserver = S}, #jid{luser = U, lserver = S},
|
2016-02-09 15:59:54 +01:00
|
|
|
?NS_FLEX_OFFLINE, _Lang) ->
|
2016-07-19 06:56:14 +02:00
|
|
|
[#identity{category = <<"automation">>,
|
|
|
|
type = <<"message-list">>}|Acc];
|
2016-02-09 15:59:54 +01:00
|
|
|
get_sm_identity(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc.
|
|
|
|
|
2016-12-11 13:03:37 +01:00
|
|
|
get_sm_items(_Acc, #jid{luser = U, lserver = S} = JID,
|
2016-02-09 15:59:54 +01:00
|
|
|
#jid{luser = U, lserver = S},
|
|
|
|
?NS_FLEX_OFFLINE, _Lang) ->
|
2016-12-11 13:03:37 +01:00
|
|
|
ejabberd_sm:route(JID, {resend_offline, false}),
|
2016-07-19 06:56:14 +02:00
|
|
|
Mod = gen_mod:db_mod(S, ?MODULE),
|
2019-07-01 13:36:05 +02:00
|
|
|
Hdrs = case Mod:read_message_headers(U, S) of
|
|
|
|
L when is_list(L) ->
|
|
|
|
L;
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
2016-07-19 06:56:14 +02:00
|
|
|
BareJID = jid:remove_resource(JID),
|
2016-02-09 15:59:54 +01:00
|
|
|
{result, lists:map(
|
2016-11-13 08:44:53 +01:00
|
|
|
fun({Seq, From, _To, _TS, _El}) ->
|
2016-07-19 06:56:14 +02:00
|
|
|
Node = integer_to_binary(Seq),
|
|
|
|
#disco_item{jid = BareJID,
|
|
|
|
node = Node,
|
2017-02-26 08:07:12 +01:00
|
|
|
name = jid:encode(From)}
|
2016-02-09 15:59:54 +01:00
|
|
|
end, Hdrs)};
|
|
|
|
get_sm_items(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc.
|
|
|
|
|
2016-08-09 09:56:32 +02:00
|
|
|
-spec get_info([xdata()], binary(), module(), binary(), binary()) -> [xdata()];
|
|
|
|
([xdata()], jid(), jid(), binary(), binary()) -> [xdata()].
|
2016-12-11 13:03:37 +01:00
|
|
|
get_info(_Acc, #jid{luser = U, lserver = S} = JID,
|
2016-10-07 09:31:03 +02:00
|
|
|
#jid{luser = U, lserver = S}, ?NS_FLEX_OFFLINE, Lang) ->
|
2016-12-11 13:03:37 +01:00
|
|
|
ejabberd_sm:route(JID, {resend_offline, false}),
|
2016-07-19 06:56:14 +02:00
|
|
|
[#xdata{type = result,
|
2016-10-07 09:31:03 +02:00
|
|
|
fields = flex_offline:encode(
|
|
|
|
[{number_of_messages, count_offline_messages(U, S)}],
|
2017-03-20 07:57:11 +01:00
|
|
|
Lang)}];
|
2016-02-09 15:59:54 +01:00
|
|
|
get_info(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc.
|
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec c2s_handle_info(c2s_state(), term()) -> c2s_state().
|
2016-12-28 07:47:11 +01:00
|
|
|
c2s_handle_info(State, {resend_offline, Flag}) ->
|
|
|
|
{stop, State#{resend_offline => Flag}};
|
|
|
|
c2s_handle_info(State, _) ->
|
|
|
|
State.
|
2016-12-11 13:03:37 +01:00
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec c2s_copy_session(c2s_state(), c2s_state()) -> c2s_state().
|
|
|
|
c2s_copy_session(State, #{resend_offline := Flag}) ->
|
|
|
|
State#{resend_offline => Flag};
|
|
|
|
c2s_copy_session(State, _) ->
|
|
|
|
State.
|
|
|
|
|
2016-07-19 06:56:14 +02:00
|
|
|
-spec handle_offline_query(iq()) -> iq().
|
2016-11-08 13:15:19 +01:00
|
|
|
handle_offline_query(#iq{from = #jid{luser = U1, lserver = S1},
|
|
|
|
to = #jid{luser = U2, lserver = S2},
|
|
|
|
lang = Lang,
|
|
|
|
sub_els = [#offline{}]} = IQ)
|
|
|
|
when {U1, S1} /= {U2, S2} ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Query to another users is forbidden"),
|
2016-11-08 13:15:19 +01:00
|
|
|
xmpp:make_error(IQ, xmpp:err_forbidden(Txt, Lang));
|
2016-07-19 06:56:14 +02:00
|
|
|
handle_offline_query(#iq{from = #jid{luser = U, lserver = S} = From,
|
|
|
|
to = #jid{luser = U, lserver = S} = _To,
|
2016-11-08 13:15:19 +01:00
|
|
|
type = Type, lang = Lang,
|
|
|
|
sub_els = [#offline{} = Offline]} = IQ) ->
|
|
|
|
case {Type, Offline} of
|
|
|
|
{get, #offline{fetch = true, items = [], purge = false}} ->
|
|
|
|
%% TODO: report database errors
|
|
|
|
handle_offline_fetch(From),
|
|
|
|
xmpp:make_iq_result(IQ);
|
|
|
|
{get, #offline{fetch = false, items = [_|_] = Items, purge = false}} ->
|
|
|
|
case handle_offline_items_view(From, Items) of
|
|
|
|
true -> xmpp:make_iq_result(IQ);
|
|
|
|
false -> xmpp:make_error(IQ, xmpp:err_item_not_found())
|
2016-02-09 15:59:54 +01:00
|
|
|
end;
|
2016-11-08 13:15:19 +01:00
|
|
|
{set, #offline{fetch = false, items = [], purge = true}} ->
|
|
|
|
case delete_all_msgs(U, S) of
|
|
|
|
{atomic, ok} ->
|
|
|
|
xmpp:make_iq_result(IQ);
|
|
|
|
_Err ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Database failure"),
|
2016-11-08 13:15:19 +01:00
|
|
|
xmpp:make_error(IQ, xmpp:err_internal_server_error(Txt, Lang))
|
|
|
|
end;
|
|
|
|
{set, #offline{fetch = false, items = [_|_] = Items, purge = false}} ->
|
|
|
|
case handle_offline_items_remove(From, Items) of
|
|
|
|
true -> xmpp:make_iq_result(IQ);
|
|
|
|
false -> xmpp:make_error(IQ, xmpp:err_item_not_found())
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
xmpp:make_error(IQ, xmpp:err_bad_request())
|
|
|
|
end;
|
2016-07-19 06:56:14 +02:00
|
|
|
handle_offline_query(#iq{lang = Lang} = IQ) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("No module is handling this query"),
|
2016-11-08 13:15:19 +01:00
|
|
|
xmpp:make_error(IQ, xmpp:err_service_unavailable(Txt, Lang)).
|
2016-02-09 15:59:54 +01:00
|
|
|
|
2016-11-08 13:15:19 +01:00
|
|
|
-spec handle_offline_items_view(jid(), [offline_item()]) -> boolean().
|
2016-07-19 06:56:14 +02:00
|
|
|
handle_offline_items_view(JID, Items) ->
|
2016-02-09 15:59:54 +01:00
|
|
|
{U, S, R} = jid:tolower(JID),
|
2019-04-26 19:59:06 +02:00
|
|
|
case use_mam_for_user(U, S) of
|
|
|
|
true ->
|
|
|
|
false;
|
|
|
|
_ ->
|
|
|
|
lists:foldl(
|
|
|
|
fun(#offline_item{node = Node, action = view}, Acc) ->
|
|
|
|
case fetch_msg_by_node(JID, Node) of
|
|
|
|
{ok, OfflineMsg} ->
|
|
|
|
case offline_msg_to_route(S, OfflineMsg) of
|
|
|
|
{route, El} ->
|
|
|
|
NewEl = set_offline_tag(El, Node),
|
|
|
|
case ejabberd_sm:get_session_pid(U, S, R) of
|
|
|
|
Pid when is_pid(Pid) ->
|
2019-07-03 08:42:18 +02:00
|
|
|
ejabberd_c2s:route(Pid, {route, NewEl});
|
2019-04-26 19:59:06 +02:00
|
|
|
none ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
Acc or true;
|
|
|
|
error ->
|
|
|
|
Acc or false
|
|
|
|
end;
|
|
|
|
error ->
|
|
|
|
Acc or false
|
|
|
|
end
|
|
|
|
end, false, Items) end.
|
2016-02-09 15:59:54 +01:00
|
|
|
|
2016-11-08 13:15:19 +01:00
|
|
|
-spec handle_offline_items_remove(jid(), [offline_item()]) -> boolean().
|
2016-07-19 06:56:14 +02:00
|
|
|
handle_offline_items_remove(JID, Items) ->
|
2019-04-26 19:59:06 +02:00
|
|
|
{U, S, _R} = jid:tolower(JID),
|
|
|
|
case use_mam_for_user(U, S) of
|
|
|
|
true ->
|
|
|
|
false;
|
|
|
|
_ ->
|
|
|
|
lists:foldl(
|
|
|
|
fun(#offline_item{node = Node, action = remove}, Acc) ->
|
|
|
|
Acc or remove_msg_by_node(JID, Node)
|
|
|
|
end, false, Items)
|
|
|
|
end.
|
2016-02-09 15:59:54 +01:00
|
|
|
|
2016-07-19 06:56:14 +02:00
|
|
|
-spec set_offline_tag(message(), binary()) -> message().
|
|
|
|
set_offline_tag(Msg, Node) ->
|
|
|
|
xmpp:set_subtag(Msg, #offline{items = [#offline_item{node = Node}]}).
|
2016-02-09 15:59:54 +01:00
|
|
|
|
2016-07-19 06:56:14 +02:00
|
|
|
-spec handle_offline_fetch(jid()) -> ok.
|
2016-12-11 13:03:37 +01:00
|
|
|
handle_offline_fetch(#jid{luser = U, lserver = S} = JID) ->
|
|
|
|
ejabberd_sm:route(JID, {resend_offline, false}),
|
2019-04-26 19:59:06 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun({Node, El}) ->
|
|
|
|
El1 = set_offline_tag(El, Node),
|
|
|
|
ejabberd_router:route(El1)
|
|
|
|
end, read_messages(U, S)).
|
2016-02-09 15:59:54 +01:00
|
|
|
|
2016-07-19 06:56:14 +02:00
|
|
|
-spec fetch_msg_by_node(jid(), binary()) -> error | {ok, #offline_msg{}}.
|
2016-04-15 12:44:33 +02:00
|
|
|
fetch_msg_by_node(To, Seq) ->
|
|
|
|
case catch binary_to_integer(Seq) of
|
|
|
|
I when is_integer(I), I >= 0 ->
|
|
|
|
LUser = To#jid.luser,
|
|
|
|
LServer = To#jid.lserver,
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:read_message(LUser, LServer, I);
|
|
|
|
_ ->
|
|
|
|
error
|
2016-02-09 15:59:54 +01:00
|
|
|
end.
|
|
|
|
|
2016-11-08 13:15:19 +01:00
|
|
|
-spec remove_msg_by_node(jid(), binary()) -> boolean().
|
2016-04-15 12:44:33 +02:00
|
|
|
remove_msg_by_node(To, Seq) ->
|
|
|
|
case catch binary_to_integer(Seq) of
|
|
|
|
I when is_integer(I), I>= 0 ->
|
|
|
|
LUser = To#jid.luser,
|
|
|
|
LServer = To#jid.lserver,
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
2016-11-08 13:15:19 +01:00
|
|
|
Mod:remove_message(LUser, LServer, I),
|
2019-06-30 20:14:37 +02:00
|
|
|
flush_cache(Mod, LUser, LServer),
|
2016-11-08 13:15:19 +01:00
|
|
|
true;
|
2016-04-15 12:44:33 +02:00
|
|
|
_ ->
|
2016-11-08 13:15:19 +01:00
|
|
|
false
|
2016-02-09 15:59:54 +01:00
|
|
|
end.
|
|
|
|
|
2016-07-19 06:56:14 +02:00
|
|
|
-spec need_to_store(binary(), message()) -> boolean().
|
|
|
|
need_to_store(_LServer, #message{type = error}) -> false;
|
2016-11-08 13:15:19 +01:00
|
|
|
need_to_store(LServer, #message{type = Type} = Packet) ->
|
2016-07-19 06:56:14 +02:00
|
|
|
case xmpp:has_subtag(Packet, #offline{}) of
|
|
|
|
false ->
|
2019-05-06 19:17:30 +02:00
|
|
|
case misc:unwrap_mucsub_message(Packet) of
|
|
|
|
#message{type = groupchat} = Msg ->
|
|
|
|
need_to_store(LServer, Msg#message{type = chat});
|
|
|
|
#message{} = Msg ->
|
|
|
|
need_to_store(LServer, Msg);
|
|
|
|
_ ->
|
|
|
|
case check_store_hint(Packet) of
|
|
|
|
store ->
|
2016-02-09 15:59:54 +01:00
|
|
|
true;
|
2019-05-06 19:17:30 +02:00
|
|
|
no_store ->
|
|
|
|
false;
|
|
|
|
none ->
|
|
|
|
Store = case Type of
|
|
|
|
groupchat ->
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_offline_opt:store_groupchat(LServer);
|
2019-05-06 19:17:30 +02:00
|
|
|
headline ->
|
|
|
|
false;
|
|
|
|
_ ->
|
|
|
|
true
|
|
|
|
end,
|
2019-06-14 11:33:26 +02:00
|
|
|
case {Store, mod_offline_opt:store_empty_body(LServer)} of
|
2019-05-06 19:17:30 +02:00
|
|
|
{false, _} ->
|
|
|
|
false;
|
|
|
|
{_, true} ->
|
|
|
|
true;
|
|
|
|
{_, false} ->
|
|
|
|
Packet#message.body /= [];
|
|
|
|
{_, unless_chat_state} ->
|
|
|
|
not misc:is_standalone_chat_state(Packet)
|
|
|
|
end
|
2016-07-19 06:56:14 +02:00
|
|
|
end
|
2014-10-27 12:14:52 +01:00
|
|
|
end;
|
2016-07-19 06:56:14 +02:00
|
|
|
true ->
|
2014-10-27 12:14:52 +01:00
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2017-02-23 16:55:35 +01:00
|
|
|
-spec store_packet({any(), message()}) -> {any(), message()}.
|
|
|
|
store_packet({_Action, #message{from = From, to = To} = Packet} = Acc) ->
|
2014-10-27 12:14:52 +01:00
|
|
|
case need_to_store(To#jid.lserver, Packet) of
|
|
|
|
true ->
|
2017-02-16 09:00:26 +01:00
|
|
|
case check_event(Packet) of
|
2015-12-08 21:46:36 +01:00
|
|
|
true ->
|
|
|
|
#jid{luser = LUser, lserver = LServer} = To,
|
2020-01-05 20:08:54 +01:00
|
|
|
TimeStamp = erlang:timestamp(),
|
|
|
|
Expire = find_x_expire(TimeStamp, Packet),
|
|
|
|
OffMsg = #offline_msg{us = {LUser, LServer},
|
|
|
|
timestamp = TimeStamp,
|
|
|
|
expire = Expire,
|
|
|
|
from = From,
|
|
|
|
to = To,
|
|
|
|
packet = Packet},
|
|
|
|
case store_offline_msg(OffMsg) of
|
|
|
|
ok ->
|
|
|
|
{offlined, Packet};
|
|
|
|
{error, Reason} ->
|
|
|
|
discard_warn_sender(Packet, Reason),
|
|
|
|
stop
|
2016-08-15 22:30:08 +02:00
|
|
|
end;
|
2019-07-01 13:36:05 +02:00
|
|
|
_ ->
|
|
|
|
maybe_update_cache(To, Packet),
|
|
|
|
Acc
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
maybe_update_cache(To, Packet),
|
|
|
|
Acc
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec maybe_update_cache(jid(), message()) -> ok.
|
|
|
|
maybe_update_cache(#jid{lserver = Server, luser = User}, Packet) ->
|
|
|
|
case xmpp:get_meta(Packet, mam_archived, false) of
|
|
|
|
true ->
|
|
|
|
Mod = gen_mod:db_mod(Server, ?MODULE),
|
|
|
|
case use_mam_for_user(User, Server) andalso use_cache(Mod, Server) of
|
|
|
|
true ->
|
|
|
|
ets_cache:incr(
|
|
|
|
?SPOOL_COUNTER_CACHE,
|
|
|
|
{User, Server}, 1,
|
|
|
|
cache_nodes(Mod, Server));
|
|
|
|
_ ->
|
|
|
|
ok
|
2015-10-07 00:06:58 +02:00
|
|
|
end;
|
2019-07-01 13:36:05 +02:00
|
|
|
_ ->
|
|
|
|
ok
|
2003-01-05 21:24:59 +01:00
|
|
|
end.
|
|
|
|
|
2016-07-19 06:56:14 +02:00
|
|
|
-spec check_store_hint(message()) -> store | no_store | none.
|
2015-12-08 21:46:36 +01:00
|
|
|
check_store_hint(Packet) ->
|
|
|
|
case has_store_hint(Packet) of
|
|
|
|
true ->
|
|
|
|
store;
|
|
|
|
false ->
|
|
|
|
case has_no_store_hint(Packet) of
|
|
|
|
true ->
|
|
|
|
no_store;
|
|
|
|
false ->
|
|
|
|
none
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2016-07-19 06:56:14 +02:00
|
|
|
-spec has_store_hint(message()) -> boolean().
|
2015-12-08 21:46:36 +01:00
|
|
|
has_store_hint(Packet) ->
|
2016-07-19 06:56:14 +02:00
|
|
|
xmpp:has_subtag(Packet, #hint{type = 'store'}).
|
2015-12-08 21:46:36 +01:00
|
|
|
|
2016-07-19 06:56:14 +02:00
|
|
|
-spec has_no_store_hint(message()) -> boolean().
|
2015-07-28 21:08:33 +02:00
|
|
|
has_no_store_hint(Packet) ->
|
2016-07-19 06:56:14 +02:00
|
|
|
xmpp:has_subtag(Packet, #hint{type = 'no-store'})
|
|
|
|
orelse
|
|
|
|
xmpp:has_subtag(Packet, #hint{type = 'no-storage'}).
|
2016-02-09 15:59:54 +01:00
|
|
|
|
2015-12-08 21:58:54 +01:00
|
|
|
%% Check if the packet has any content about XEP-0022
|
2017-02-16 09:00:26 +01:00
|
|
|
-spec check_event(message()) -> boolean().
|
2017-04-25 16:59:26 +02:00
|
|
|
check_event(#message{from = From, to = To, id = ID, type = Type} = Msg) ->
|
2016-07-19 06:56:14 +02:00
|
|
|
case xmpp:get_subtag(Msg, #xevent{}) of
|
|
|
|
false ->
|
|
|
|
true;
|
|
|
|
#xevent{id = undefined, offline = false} ->
|
|
|
|
true;
|
|
|
|
#xevent{id = undefined, offline = true} ->
|
2017-04-25 16:59:26 +02:00
|
|
|
NewMsg = #message{from = To, to = From, id = ID, type = Type,
|
2017-04-25 16:21:24 +02:00
|
|
|
sub_els = [#xevent{id = ID, offline = true}]},
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route(NewMsg),
|
2016-07-19 06:56:14 +02:00
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
2003-01-05 21:24:59 +01:00
|
|
|
end.
|
|
|
|
|
2016-07-19 06:56:14 +02:00
|
|
|
-spec find_x_expire(erlang:timestamp(), message()) -> erlang:timestamp() | never.
|
|
|
|
find_x_expire(TimeStamp, Msg) ->
|
2017-02-18 07:36:27 +01:00
|
|
|
case xmpp:get_subtag(Msg, #expire{seconds = 0}) of
|
2016-07-19 06:56:14 +02:00
|
|
|
#expire{seconds = Int} ->
|
|
|
|
{MegaSecs, Secs, MicroSecs} = TimeStamp,
|
|
|
|
S = MegaSecs * 1000000 + Secs + Int,
|
|
|
|
MegaSecs1 = S div 1000000,
|
|
|
|
Secs1 = S rem 1000000,
|
|
|
|
{MegaSecs1, Secs1, MicroSecs};
|
|
|
|
false ->
|
|
|
|
never
|
2004-09-10 22:57:00 +02:00
|
|
|
end.
|
|
|
|
|
2017-01-23 11:51:05 +01:00
|
|
|
c2s_self_presence({_Pres, #{resend_offline := false}} = Acc) ->
|
|
|
|
Acc;
|
2017-01-09 15:02:17 +01:00
|
|
|
c2s_self_presence({#presence{type = available} = NewPres, State} = Acc) ->
|
|
|
|
NewPrio = get_priority_from_presence(NewPres),
|
2018-06-20 12:16:01 +02:00
|
|
|
LastPrio = case maps:get(pres_last, State, undefined) of
|
|
|
|
undefined -> -1;
|
2017-01-09 15:02:17 +01:00
|
|
|
LastPres -> get_priority_from_presence(LastPres)
|
|
|
|
end,
|
|
|
|
if LastPrio < 0 andalso NewPrio >= 0 ->
|
|
|
|
route_offline_messages(State);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
Acc;
|
|
|
|
c2s_self_presence(Acc) ->
|
|
|
|
Acc.
|
|
|
|
|
|
|
|
-spec route_offline_messages(c2s_state()) -> ok.
|
|
|
|
route_offline_messages(#{jid := #jid{luser = LUser, lserver = LServer}} = State) ->
|
2016-04-15 12:44:33 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
2019-04-26 19:59:06 +02:00
|
|
|
Msgs = case Mod:pop_messages(LUser, LServer) of
|
|
|
|
{ok, OffMsgs} ->
|
|
|
|
case use_mam_for_user(LUser, LServer) of
|
|
|
|
true ->
|
2019-07-01 14:30:21 +02:00
|
|
|
flush_cache(Mod, LUser, LServer),
|
2019-04-26 19:59:06 +02:00
|
|
|
lists:map(
|
|
|
|
fun({_, #message{from = From, to = To} = Msg}) ->
|
|
|
|
#offline_msg{from = From, to = To,
|
|
|
|
us = {LUser, LServer},
|
|
|
|
packet = Msg}
|
|
|
|
end, read_mam_messages(LUser, LServer, OffMsgs));
|
|
|
|
_ ->
|
2019-06-30 20:14:37 +02:00
|
|
|
flush_cache(Mod, LUser, LServer),
|
2019-04-26 19:59:06 +02:00
|
|
|
OffMsgs
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
lists:foreach(
|
|
|
|
fun(OffMsg) ->
|
|
|
|
route_offline_message(State, OffMsg)
|
|
|
|
end, Msgs).
|
2004-01-18 21:42:09 +01:00
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec route_offline_message(c2s_state(), #offline_msg{}) -> ok.
|
|
|
|
route_offline_message(#{lserver := LServer} = State,
|
|
|
|
#offline_msg{expire = Expire} = OffMsg) ->
|
|
|
|
case offline_msg_to_route(LServer, OffMsg) of
|
|
|
|
error ->
|
|
|
|
ok;
|
2017-02-16 09:00:26 +01:00
|
|
|
{route, Msg} ->
|
2017-01-09 15:02:17 +01:00
|
|
|
case is_message_expired(Expire, Msg) of
|
|
|
|
true ->
|
|
|
|
ok;
|
|
|
|
false ->
|
|
|
|
case privacy_check_packet(State, Msg, in) of
|
2017-02-16 09:00:26 +01:00
|
|
|
allow -> ejabberd_router:route(Msg);
|
2017-02-18 07:36:27 +01:00
|
|
|
deny -> ok
|
2017-01-09 15:02:17 +01:00
|
|
|
end
|
|
|
|
end
|
2017-01-13 12:20:25 +01:00
|
|
|
end.
|
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec is_message_expired(erlang:timestamp() | never, message()) -> boolean().
|
|
|
|
is_message_expired(Expire, Msg) ->
|
2019-02-27 09:56:20 +01:00
|
|
|
TS = erlang:timestamp(),
|
2017-01-09 15:02:17 +01:00
|
|
|
Expire1 = case Expire of
|
|
|
|
undefined -> find_x_expire(TS, Msg);
|
|
|
|
_ -> Expire
|
|
|
|
end,
|
|
|
|
Expire1 /= never andalso Expire1 =< TS.
|
|
|
|
|
|
|
|
-spec privacy_check_packet(c2s_state(), stanza(), in | out) -> allow | deny.
|
|
|
|
privacy_check_packet(#{lserver := LServer} = State, Pkt, Dir) ->
|
|
|
|
ejabberd_hooks:run_fold(privacy_check_packet,
|
|
|
|
LServer, allow, [State, Pkt, Dir]).
|
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
remove_expired_messages(Server) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LServer = jid:nameprep(Server),
|
2016-04-15 12:44:33 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
2019-01-08 11:41:16 +01:00
|
|
|
case erlang:function_exported(Mod, remove_expired_messages, 1) of
|
2019-06-30 20:14:37 +02:00
|
|
|
true ->
|
|
|
|
Ret = Mod:remove_expired_messages(LServer),
|
|
|
|
ets_cache:clear(?SPOOL_COUNTER_CACHE),
|
|
|
|
Ret;
|
|
|
|
false ->
|
|
|
|
erlang:error(not_implemented)
|
2019-01-08 11:41:16 +01:00
|
|
|
end.
|
2004-09-10 22:57:00 +02:00
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
remove_old_messages(Days, Server) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LServer = jid:nameprep(Server),
|
2016-04-15 12:44:33 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
2019-01-08 11:41:16 +01:00
|
|
|
case erlang:function_exported(Mod, remove_old_messages, 2) of
|
2019-06-30 20:14:37 +02:00
|
|
|
true ->
|
|
|
|
Ret = Mod:remove_old_messages(Days, LServer),
|
|
|
|
ets_cache:clear(?SPOOL_COUNTER_CACHE),
|
|
|
|
Ret;
|
|
|
|
false ->
|
|
|
|
erlang:error(not_implemented)
|
2019-01-08 11:41:16 +01:00
|
|
|
end.
|
2003-10-24 21:21:07 +02:00
|
|
|
|
2016-08-09 09:56:32 +02:00
|
|
|
-spec remove_user(binary(), binary()) -> ok.
|
2005-04-17 20:08:34 +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 12:44:33 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
2016-08-09 09:56:32 +02:00
|
|
|
Mod:remove_user(LUser, LServer),
|
2019-06-30 20:14:37 +02:00
|
|
|
flush_cache(Mod, LUser, LServer).
|
2007-08-13 12:27:28 +02:00
|
|
|
|
|
|
|
%% Helper functions:
|
|
|
|
|
2019-03-14 15:12:39 +01:00
|
|
|
-spec check_if_message_should_be_bounced(message()) -> boolean().
|
|
|
|
check_if_message_should_be_bounced(Packet) ->
|
|
|
|
case Packet of
|
|
|
|
#message{type = groupchat, to = #jid{lserver = LServer}} ->
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_offline_opt:bounce_groupchat(LServer);
|
2019-03-14 15:12:39 +01:00
|
|
|
#message{to = #jid{lserver = LServer}} ->
|
|
|
|
case misc:is_mucsub_message(Packet) of
|
|
|
|
true ->
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_offline_opt:bounce_groupchat(LServer);
|
2019-03-14 15:12:39 +01:00
|
|
|
_ ->
|
|
|
|
true
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
true
|
|
|
|
end.
|
|
|
|
|
2007-08-13 12:27:28 +02:00
|
|
|
%% Warn senders that their messages have been discarded:
|
2019-03-14 15:12:39 +01:00
|
|
|
|
2017-05-21 22:21:13 +02:00
|
|
|
-spec discard_warn_sender(message(), full | any()) -> ok.
|
2019-03-14 15:12:39 +01:00
|
|
|
discard_warn_sender(Packet, Reason) ->
|
|
|
|
case check_if_message_should_be_bounced(Packet) of
|
|
|
|
true ->
|
|
|
|
Lang = xmpp:get_lang(Packet),
|
|
|
|
Err = case Reason of
|
|
|
|
full ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Your contact offline message queue is "
|
|
|
|
"full. The message has been discarded."),
|
2019-03-14 15:12:39 +01:00
|
|
|
xmpp:err_resource_constraint(ErrText, Lang);
|
|
|
|
_ ->
|
2019-06-22 16:08:45 +02:00
|
|
|
ErrText = ?T("Database failure"),
|
2019-03-14 15:12:39 +01:00
|
|
|
xmpp:err_internal_server_error(ErrText, Lang)
|
|
|
|
end,
|
|
|
|
ejabberd_router:route_error(Packet, Err);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2007-08-23 02:51:54 +02:00
|
|
|
|
|
|
|
webadmin_page(_, Host,
|
2013-03-14 10:33:02 +01:00
|
|
|
#request{us = _US, path = [<<"user">>, U, <<"queue">>],
|
|
|
|
q = Query, lang = Lang} =
|
|
|
|
_Request) ->
|
|
|
|
Res = user_queue(U, Host, Query, Lang), {stop, Res};
|
2007-08-23 02:51:54 +02:00
|
|
|
webadmin_page(Acc, _, _) -> Acc.
|
|
|
|
|
2013-06-24 12:04:56 +02:00
|
|
|
get_offline_els(LUser, LServer) ->
|
2016-11-13 08:44:53 +01:00
|
|
|
[Packet || {_Seq, Packet} <- read_messages(LUser, LServer)].
|
2013-06-24 12:04:56 +02:00
|
|
|
|
2016-11-13 08:44:53 +01:00
|
|
|
-spec offline_msg_to_route(binary(), #offline_msg{}) ->
|
2017-02-16 09:00:26 +01:00
|
|
|
{route, message()} | error.
|
2017-01-09 15:02:17 +01:00
|
|
|
offline_msg_to_route(LServer, #offline_msg{from = From, to = To} = R) ->
|
2019-06-21 20:06:32 +02:00
|
|
|
CodecOpts = ejabberd_config:codec_options(),
|
2018-02-09 16:12:50 +01:00
|
|
|
try xmpp:decode(R#offline_msg.packet, ?NS_CLIENT, CodecOpts) of
|
2016-11-13 08:44:53 +01:00
|
|
|
Pkt ->
|
2017-01-09 15:02:17 +01:00
|
|
|
Pkt1 = xmpp:set_from_to(Pkt, From, To),
|
|
|
|
Pkt2 = add_delay_info(Pkt1, LServer, R#offline_msg.timestamp),
|
2017-02-16 09:00:26 +01:00
|
|
|
{route, Pkt2}
|
2016-11-13 08:44:53 +01:00
|
|
|
catch _:{xmpp_codec, Why} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Failed to decode packet ~p of user ~ts: ~ts",
|
2017-02-26 08:07:12 +01:00
|
|
|
[R#offline_msg.packet, jid:encode(To),
|
2016-11-13 08:44:53 +01:00
|
|
|
xmpp:format_error(Why)]),
|
|
|
|
error
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec read_messages(binary(), binary()) -> [{binary(), message()}].
|
|
|
|
read_messages(LUser, LServer) ->
|
2019-07-01 13:36:05 +02:00
|
|
|
Res = case read_db_messages(LUser, LServer) of
|
|
|
|
error ->
|
|
|
|
[];
|
|
|
|
L when is_list(L) ->
|
|
|
|
L
|
|
|
|
end,
|
2019-04-26 19:59:06 +02:00
|
|
|
case use_mam_for_user(LUser, LServer) of
|
|
|
|
true ->
|
|
|
|
read_mam_messages(LUser, LServer, Res);
|
|
|
|
_ ->
|
|
|
|
Res
|
|
|
|
end.
|
|
|
|
|
2019-07-01 13:36:05 +02:00
|
|
|
-spec read_db_messages(binary(), binary()) -> [{binary(), message()}] | error.
|
2019-04-26 19:59:06 +02:00
|
|
|
read_db_messages(LUser, LServer) ->
|
2016-04-15 12:44:33 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
2019-06-21 20:06:32 +02:00
|
|
|
CodecOpts = ejabberd_config:codec_options(),
|
2019-07-01 13:36:05 +02:00
|
|
|
case Mod:read_message_headers(LUser, LServer) of
|
|
|
|
error ->
|
|
|
|
error;
|
|
|
|
L ->
|
|
|
|
lists:flatmap(
|
|
|
|
fun({Seq, From, To, TS, El}) ->
|
2019-04-26 19:59:06 +02:00
|
|
|
Node = integer_to_binary(Seq),
|
2019-07-01 13:36:05 +02:00
|
|
|
try xmpp:decode(El, ?NS_CLIENT, CodecOpts) of
|
|
|
|
Pkt ->
|
|
|
|
Node = integer_to_binary(Seq),
|
|
|
|
Pkt1 = add_delay_info(Pkt, LServer, TS),
|
|
|
|
Pkt2 = xmpp:set_from_to(Pkt1, From, To),
|
|
|
|
[{Node, Pkt2}]
|
|
|
|
catch _:{xmpp_codec, Why} ->
|
|
|
|
?ERROR_MSG("Failed to decode packet ~p "
|
2019-09-23 14:17:20 +02:00
|
|
|
"of user ~ts: ~ts",
|
2019-07-01 13:36:05 +02:00
|
|
|
[El, jid:encode(To),
|
|
|
|
xmpp:format_error(Why)]),
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end, L)
|
|
|
|
end.
|
2019-04-26 19:59:06 +02:00
|
|
|
|
2019-05-02 11:12:22 +02:00
|
|
|
-spec parse_marker_messages(binary(), [#offline_msg{} | {any(), message()}]) ->
|
|
|
|
{integer() | none, [message()]}.
|
|
|
|
parse_marker_messages(LServer, ReadMsgs) ->
|
2019-04-26 19:59:06 +02:00
|
|
|
{Timestamp, ExtraMsgs} = lists:foldl(
|
|
|
|
fun({_Node, #message{id = <<"ActivityMarker">>,
|
|
|
|
body = [], type = error} = Msg}, {T, E}) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case xmpp:get_subtag(Msg, #delay{stamp = {0,0,0}}) of
|
2019-04-26 19:59:06 +02:00
|
|
|
#delay{stamp = Time} ->
|
|
|
|
if T == none orelse T > Time ->
|
|
|
|
{Time, E};
|
|
|
|
true ->
|
|
|
|
{T, E}
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
(#offline_msg{from = From, to = To, timestamp = TS, packet = Pkt},
|
|
|
|
{T, E}) ->
|
|
|
|
try xmpp:decode(Pkt) of
|
|
|
|
#message{id = <<"ActivityMarker">>,
|
|
|
|
body = [], type = error} = Msg ->
|
|
|
|
TS2 = case TS of
|
|
|
|
undefined ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case xmpp:get_subtag(Msg, #delay{stamp = {0,0,0}}) of
|
2019-04-26 19:59:06 +02:00
|
|
|
#delay{stamp = TS0} ->
|
|
|
|
TS0;
|
|
|
|
_ ->
|
|
|
|
erlang:timestamp()
|
2019-04-29 17:25:06 +02:00
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
TS
|
2019-04-26 19:59:06 +02:00
|
|
|
end,
|
|
|
|
if T == none orelse T > TS2 ->
|
|
|
|
{TS2, E};
|
|
|
|
true ->
|
|
|
|
{T, E}
|
|
|
|
end;
|
|
|
|
Decoded ->
|
|
|
|
Pkt1 = add_delay_info(Decoded, LServer, TS),
|
|
|
|
{T, [xmpp:set_from_to(Pkt1, From, To) | E]}
|
2019-05-02 11:12:22 +02:00
|
|
|
catch _:{xmpp_codec, _Why} ->
|
|
|
|
{T, E}
|
2019-04-26 19:59:06 +02:00
|
|
|
end;
|
|
|
|
({_Node, Msg}, {T, E}) ->
|
|
|
|
{T, [Msg | E]}
|
|
|
|
end, {none, []}, ReadMsgs),
|
|
|
|
Start = case {Timestamp, ExtraMsgs} of
|
|
|
|
{none, [First|_]} ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case xmpp:get_subtag(First, #delay{stamp = {0,0,0}}) of
|
2019-04-26 19:59:06 +02:00
|
|
|
#delay{stamp = {Mega, Sec, Micro}} ->
|
|
|
|
{Mega, Sec, Micro+1};
|
|
|
|
_ ->
|
|
|
|
none
|
|
|
|
end;
|
|
|
|
{none, _} ->
|
|
|
|
none;
|
|
|
|
_ ->
|
|
|
|
Timestamp
|
|
|
|
end,
|
2019-05-02 11:12:22 +02:00
|
|
|
{Start, ExtraMsgs}.
|
|
|
|
|
|
|
|
-spec read_mam_messages(binary(), binary(), [#offline_msg{} | {any(), message()}]) ->
|
|
|
|
[{integer(), message()}].
|
|
|
|
read_mam_messages(LUser, LServer, ReadMsgs) ->
|
|
|
|
{Start, ExtraMsgs} = parse_marker_messages(LServer, ReadMsgs),
|
2019-04-26 19:59:06 +02:00
|
|
|
AllMsgs = case Start of
|
|
|
|
none ->
|
|
|
|
ExtraMsgs;
|
|
|
|
_ ->
|
|
|
|
MaxOfflineMsgs = case get_max_user_messages(LUser, LServer) of
|
2019-06-14 11:33:26 +02:00
|
|
|
Number when is_integer(Number) ->
|
|
|
|
max(0, Number - length(ExtraMsgs));
|
|
|
|
infinity ->
|
|
|
|
undefined
|
2019-04-26 19:59:06 +02:00
|
|
|
end,
|
|
|
|
JID = jid:make(LUser, LServer, <<>>),
|
|
|
|
{MamMsgs, _, _} = mod_mam:select(LServer, JID, JID,
|
|
|
|
[{start, Start}],
|
|
|
|
#rsm_set{max = MaxOfflineMsgs,
|
|
|
|
before = <<"9999999999999999">>},
|
2019-05-02 11:12:22 +02:00
|
|
|
chat, only_messages),
|
2019-04-26 19:59:06 +02:00
|
|
|
MamMsgs2 = lists:map(
|
|
|
|
fun({_, _, #forwarded{sub_els = [MM | _], delay = #delay{stamp = MMT}}}) ->
|
|
|
|
add_delay_info(MM, LServer, MMT)
|
|
|
|
end, MamMsgs),
|
|
|
|
|
|
|
|
ExtraMsgs ++ MamMsgs2
|
|
|
|
end,
|
|
|
|
AllMsgs2 = lists:sort(
|
|
|
|
fun(A, B) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
DA = case xmpp:get_subtag(A, #stanza_id{by = #jid{}}) of
|
2019-04-29 16:32:19 +02:00
|
|
|
#stanza_id{id = IDA} ->
|
|
|
|
IDA;
|
2019-06-14 11:33:26 +02:00
|
|
|
_ -> case xmpp:get_subtag(A, #delay{stamp = {0,0,0}}) of
|
2019-04-29 16:32:19 +02:00
|
|
|
#delay{stamp = STA} ->
|
|
|
|
integer_to_binary(misc:now_to_usec(STA));
|
|
|
|
_ ->
|
|
|
|
<<"unknown">>
|
|
|
|
end
|
|
|
|
end,
|
2019-06-14 11:33:26 +02:00
|
|
|
DB = case xmpp:get_subtag(B, #stanza_id{by = #jid{}}) of
|
2019-04-29 16:32:19 +02:00
|
|
|
#stanza_id{id = IDB} ->
|
|
|
|
IDB;
|
2019-06-14 11:33:26 +02:00
|
|
|
_ -> case xmpp:get_subtag(B, #delay{stamp = {0,0,0}}) of
|
2019-04-29 16:32:19 +02:00
|
|
|
#delay{stamp = STB} ->
|
|
|
|
integer_to_binary(misc:now_to_usec(STB));
|
|
|
|
_ ->
|
|
|
|
<<"unknown">>
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
DA < DB
|
2019-04-26 19:59:06 +02:00
|
|
|
end, AllMsgs),
|
|
|
|
{AllMsgs3, _} = lists:mapfoldl(
|
|
|
|
fun(Msg, Counter) ->
|
|
|
|
{{Counter, Msg}, Counter + 1}
|
|
|
|
end, 1, AllMsgs2),
|
|
|
|
AllMsgs3.
|
2012-04-27 11:52:05 +02:00
|
|
|
|
2019-07-01 13:36:05 +02:00
|
|
|
-spec count_mam_messages(binary(), binary(), [#offline_msg{} | {any(), message()}] | error) ->
|
|
|
|
{cache, integer()} | {nocache, integer()}.
|
|
|
|
count_mam_messages(_LUser, _LServer, error) ->
|
|
|
|
{nocache, 0};
|
2019-05-02 11:12:22 +02:00
|
|
|
count_mam_messages(LUser, LServer, ReadMsgs) ->
|
|
|
|
{Start, ExtraMsgs} = parse_marker_messages(LServer, ReadMsgs),
|
|
|
|
case Start of
|
|
|
|
none ->
|
2019-07-01 13:36:05 +02:00
|
|
|
{cache, length(ExtraMsgs)};
|
2019-05-02 11:12:22 +02:00
|
|
|
_ ->
|
|
|
|
MaxOfflineMsgs = case get_max_user_messages(LUser, LServer) of
|
|
|
|
Number when is_integer(Number) -> Number - length(ExtraMsgs);
|
2019-06-14 11:33:26 +02:00
|
|
|
infinity -> undefined
|
2019-05-02 11:12:22 +02:00
|
|
|
end,
|
|
|
|
JID = jid:make(LUser, LServer, <<>>),
|
|
|
|
{_, _, Count} = mod_mam:select(LServer, JID, JID,
|
|
|
|
[{start, Start}],
|
|
|
|
#rsm_set{max = MaxOfflineMsgs,
|
|
|
|
before = <<"9999999999999999">>},
|
|
|
|
chat, only_count),
|
2019-07-01 13:36:05 +02:00
|
|
|
{cache, Count + length(ExtraMsgs)}
|
2019-05-02 11:12:22 +02:00
|
|
|
end.
|
|
|
|
|
2016-04-15 12:44:33 +02:00
|
|
|
format_user_queue(Hdrs) ->
|
|
|
|
lists:map(
|
2016-11-13 08:44:53 +01:00
|
|
|
fun({Seq, From, To, TS, El}) ->
|
2016-04-15 12:44:33 +02:00
|
|
|
ID = integer_to_binary(Seq),
|
|
|
|
FPacket = ejabberd_web_admin:pretty_print_xml(El),
|
2017-02-26 08:07:12 +01:00
|
|
|
SFrom = jid:encode(From),
|
|
|
|
STo = jid:encode(To),
|
2016-11-13 08:44:53 +01:00
|
|
|
Time = case TS of
|
|
|
|
undefined ->
|
|
|
|
Stamp = fxml:get_path_s(El, [{elem, <<"delay">>},
|
|
|
|
{attr, <<"stamp">>}]),
|
|
|
|
try xmpp_util:decode_timestamp(Stamp) of
|
|
|
|
{_, _, _} = Now -> format_time(Now)
|
|
|
|
catch _:_ ->
|
|
|
|
<<"">>
|
|
|
|
end;
|
2016-04-15 12:44:33 +02:00
|
|
|
{_, _, _} = Now ->
|
2016-11-13 08:44:53 +01:00
|
|
|
format_time(Now)
|
2016-04-15 12:44:33 +02:00
|
|
|
end,
|
|
|
|
?XE(<<"tr">>,
|
|
|
|
[?XAE(<<"td">>, [{<<"class">>, <<"valign">>}],
|
|
|
|
[?INPUT(<<"checkbox">>, <<"selected">>, ID)]),
|
|
|
|
?XAC(<<"td">>, [{<<"class">>, <<"valign">>}], Time),
|
|
|
|
?XAC(<<"td">>, [{<<"class">>, <<"valign">>}], SFrom),
|
|
|
|
?XAC(<<"td">>, [{<<"class">>, <<"valign">>}], STo),
|
|
|
|
?XAE(<<"td">>, [{<<"class">>, <<"valign">>}],
|
|
|
|
[?XC(<<"pre">>, FPacket)])])
|
|
|
|
end, Hdrs).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
2016-11-13 08:44:53 +01:00
|
|
|
format_time(Now) ->
|
|
|
|
{{Year, Month, Day}, {Hour, Minute, Second}} = calendar:now_to_local_time(Now),
|
|
|
|
str:format("~w-~.2.0w-~.2.0w ~.2.0w:~.2.0w:~.2.0w",
|
|
|
|
[Year, Month, Day, Hour, Minute, Second]).
|
|
|
|
|
2007-08-23 02:51:54 +02:00
|
|
|
user_queue(User, Server, Query, Lang) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2012-04-27 11:52:05 +02:00
|
|
|
US = {LUser, LServer},
|
2016-04-15 12:44:33 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
2019-06-14 11:33:26 +02:00
|
|
|
user_queue_parse_query(LUser, LServer, Query),
|
2019-07-01 13:36:05 +02:00
|
|
|
HdrsAll = case Mod:read_message_headers(LUser, LServer) of
|
|
|
|
error -> [];
|
|
|
|
L -> L
|
|
|
|
end,
|
2017-06-30 14:17:20 +02:00
|
|
|
Hdrs = get_messages_subset(User, Server, HdrsAll),
|
2016-04-15 12:44:33 +02:00
|
|
|
FMsgs = format_user_queue(Hdrs),
|
2019-09-23 14:17:20 +02:00
|
|
|
PageTitle = str:format(translate:translate(Lang, ?T("~ts's Offline Messages Queue")), [us_to_list(US)]),
|
2019-08-27 12:29:05 +02:00
|
|
|
(?H1GL(PageTitle, <<"mod-offline">>, <<"mod_offline">>))
|
2019-06-22 16:08:45 +02:00
|
|
|
++ [?XREST(?T("Submitted"))] ++
|
2013-03-14 10:33:02 +01:00
|
|
|
[?XAE(<<"form">>,
|
|
|
|
[{<<"action">>, <<"">>}, {<<"method">>, <<"post">>}],
|
|
|
|
[?XE(<<"table">>,
|
|
|
|
[?XE(<<"thead">>,
|
|
|
|
[?XE(<<"tr">>,
|
2019-06-22 16:08:45 +02:00
|
|
|
[?X(<<"td">>), ?XCT(<<"td">>, ?T("Time")),
|
|
|
|
?XCT(<<"td">>, ?T("From")),
|
|
|
|
?XCT(<<"td">>, ?T("To")),
|
|
|
|
?XCT(<<"td">>, ?T("Packet"))])]),
|
2013-03-14 10:33:02 +01:00
|
|
|
?XE(<<"tbody">>,
|
|
|
|
if FMsgs == [] ->
|
|
|
|
[?XE(<<"tr">>,
|
|
|
|
[?XAC(<<"td">>, [{<<"colspan">>, <<"4">>}],
|
|
|
|
<<" ">>)])];
|
|
|
|
true -> FMsgs
|
|
|
|
end)]),
|
2007-08-23 02:51:54 +02:00
|
|
|
?BR,
|
2013-03-14 10:33:02 +01:00
|
|
|
?INPUTT(<<"submit">>, <<"delete">>,
|
2019-06-22 16:08:45 +02:00
|
|
|
?T("Delete Selected"))])].
|
2007-08-23 02:51:54 +02:00
|
|
|
|
2016-04-15 12:44:33 +02:00
|
|
|
user_queue_parse_query(LUser, LServer, Query) ->
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
2012-11-13 13:56:27 +01:00
|
|
|
case lists:keysearch(<<"delete">>, 1, Query) of
|
2016-04-15 12:44:33 +02:00
|
|
|
{value, _} ->
|
2019-06-30 20:14:37 +02:00
|
|
|
case user_queue_parse_query(LUser, LServer, Query, Mod, false) of
|
|
|
|
true ->
|
|
|
|
flush_cache(Mod, LUser, LServer);
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end;
|
2016-04-15 12:44:33 +02:00
|
|
|
_ ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ok
|
2007-08-23 02:51:54 +02:00
|
|
|
end.
|
|
|
|
|
2019-06-30 20:14:37 +02:00
|
|
|
user_queue_parse_query(LUser, LServer, Query, Mod, Acc) ->
|
2017-08-25 12:44:53 +02:00
|
|
|
case lists:keytake(<<"selected">>, 1, Query) of
|
|
|
|
{value, {_, Seq}, Query2} ->
|
2019-06-30 20:14:37 +02:00
|
|
|
NewAcc = case catch binary_to_integer(Seq) of
|
|
|
|
I when is_integer(I), I>=0 ->
|
|
|
|
Mod:remove_message(LUser, LServer, I),
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
Acc
|
|
|
|
end,
|
|
|
|
user_queue_parse_query(LUser, LServer, Query2, Mod, NewAcc);
|
2017-08-25 12:44:53 +02:00
|
|
|
false ->
|
2019-06-30 20:14:37 +02:00
|
|
|
Acc
|
2017-08-25 12:44:53 +02:00
|
|
|
end.
|
|
|
|
|
2007-08-23 02:51:54 +02:00
|
|
|
us_to_list({User, Server}) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
jid:encode({User, Server, <<"">>}).
|
2007-08-23 02:51:54 +02:00
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
get_queue_length(LUser, LServer) ->
|
2016-02-12 14:25:09 +01:00
|
|
|
count_offline_messages(LUser, LServer).
|
2010-01-12 13:02:50 +01:00
|
|
|
|
2016-04-15 12:44:33 +02:00
|
|
|
get_messages_subset(User, Host, MsgsAll) ->
|
2017-05-21 22:21:13 +02:00
|
|
|
MaxOfflineMsgs = case get_max_user_messages(User, Host) of
|
2013-03-14 10:33:02 +01:00
|
|
|
Number when is_integer(Number) -> Number;
|
|
|
|
_ -> 100
|
2010-01-13 00:58:22 +01:00
|
|
|
end,
|
|
|
|
Length = length(MsgsAll),
|
2016-04-15 12:44:33 +02:00
|
|
|
get_messages_subset2(MaxOfflineMsgs, Length, MsgsAll).
|
2010-01-13 00:58:22 +01:00
|
|
|
|
2016-04-15 12:44:33 +02:00
|
|
|
get_messages_subset2(Max, Length, MsgsAll) when Length =< Max * 2 ->
|
2010-01-13 00:58:22 +01:00
|
|
|
MsgsAll;
|
2016-04-15 12:44:33 +02:00
|
|
|
get_messages_subset2(Max, Length, MsgsAll) ->
|
2010-01-13 00:58:22 +01:00
|
|
|
FirstN = Max,
|
|
|
|
{MsgsFirstN, Msgs2} = lists:split(FirstN, MsgsAll),
|
2013-03-14 10:33:02 +01:00
|
|
|
MsgsLastN = lists:nthtail(Length - FirstN - FirstN,
|
|
|
|
Msgs2),
|
2017-02-25 08:01:01 +01:00
|
|
|
NoJID = jid:make(<<"...">>, <<"...">>),
|
2016-04-15 12:44:33 +02:00
|
|
|
Seq = <<"0">>,
|
2013-03-14 10:33:02 +01:00
|
|
|
IntermediateMsg = #xmlel{name = <<"...">>, attrs = [],
|
|
|
|
children = []},
|
2016-04-15 12:44:33 +02:00
|
|
|
MsgsFirstN ++ [{Seq, NoJID, NoJID, IntermediateMsg}] ++ MsgsLastN.
|
2010-01-13 00:58:22 +01:00
|
|
|
|
2007-08-23 02:51:54 +02:00
|
|
|
webadmin_user(Acc, User, Server, Lang) ->
|
2016-03-04 11:09:14 +01:00
|
|
|
QueueLen = count_offline_messages(jid:nodeprep(User),
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:nameprep(Server)),
|
2013-03-14 10:33:02 +01:00
|
|
|
FQueueLen = [?AC(<<"queue/">>,
|
2016-11-24 13:06:06 +01:00
|
|
|
(integer_to_binary(QueueLen)))],
|
2013-03-14 10:33:02 +01:00
|
|
|
Acc ++
|
2019-06-22 16:08:45 +02:00
|
|
|
[?XCT(<<"h3">>, ?T("Offline Messages:"))] ++
|
2013-03-14 10:33:02 +01:00
|
|
|
FQueueLen ++
|
|
|
|
[?C(<<" ">>),
|
|
|
|
?INPUTT(<<"submit">>, <<"removealloffline">>,
|
2019-06-22 16:08:45 +02:00
|
|
|
?T("Remove All Offline Messages"))].
|
2008-10-12 16:16:05 +02:00
|
|
|
|
2016-07-19 06:56:14 +02:00
|
|
|
-spec delete_all_msgs(binary(), binary()) -> {atomic, any()}.
|
2012-04-27 11:52:05 +02:00
|
|
|
delete_all_msgs(User, Server) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2016-04-15 12:44:33 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
2019-06-30 20:14:37 +02:00
|
|
|
Ret = Mod:remove_all_messages(LUser, LServer),
|
|
|
|
flush_cache(Mod, LUser, LServer),
|
|
|
|
Ret.
|
2012-04-27 11:52:05 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
webadmin_user_parse_query(_, <<"removealloffline">>,
|
|
|
|
User, Server, _Query) ->
|
2012-04-27 11:52:05 +02:00
|
|
|
case delete_all_msgs(User, Server) of
|
2017-02-18 07:36:27 +01:00
|
|
|
{atomic, ok} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Removed all offline messages for ~ts@~ts",
|
2017-02-18 07:36:27 +01:00
|
|
|
[User, Server]),
|
|
|
|
{stop, ok};
|
|
|
|
Err ->
|
|
|
|
?ERROR_MSG("Failed to remove offline messages: ~p",
|
|
|
|
[Err]),
|
|
|
|
{stop, error}
|
2008-10-12 16:16:05 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
webadmin_user_parse_query(Acc, _Action, _User, _Server,
|
|
|
|
_Query) ->
|
2008-10-12 16:16:05 +02:00
|
|
|
Acc.
|
2012-04-27 11:52:05 +02:00
|
|
|
|
|
|
|
%% Returns as integer the number of offline messages for a given user
|
2016-07-19 06:56:14 +02:00
|
|
|
-spec count_offline_messages(binary(), binary()) -> non_neg_integer().
|
2012-11-13 13:56:27 +01:00
|
|
|
count_offline_messages(User, Server) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2019-07-01 13:36:05 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
2019-04-29 16:31:37 +02:00
|
|
|
case use_mam_for_user(User, Server) of
|
|
|
|
true ->
|
2019-07-01 13:36:05 +02:00
|
|
|
case use_cache(Mod, LServer) of
|
|
|
|
true ->
|
|
|
|
ets_cache:lookup(
|
|
|
|
?SPOOL_COUNTER_CACHE, {LUser, LServer},
|
|
|
|
fun() ->
|
|
|
|
Res = read_db_messages(LUser, LServer),
|
|
|
|
count_mam_messages(LUser, LServer, Res)
|
|
|
|
end);
|
|
|
|
false ->
|
|
|
|
Res = read_db_messages(LUser, LServer),
|
|
|
|
ets_cache:untag(count_mam_messages(LUser, LServer, Res))
|
|
|
|
end;
|
2019-04-29 16:31:37 +02:00
|
|
|
_ ->
|
2019-07-01 13:36:05 +02:00
|
|
|
case use_cache(Mod, LServer) of
|
|
|
|
true ->
|
|
|
|
ets_cache:lookup(
|
|
|
|
?SPOOL_COUNTER_CACHE, {LUser, LServer},
|
|
|
|
fun() ->
|
|
|
|
Mod:count_messages(LUser, LServer)
|
|
|
|
end);
|
|
|
|
false ->
|
|
|
|
ets_cache:untag(Mod:count_messages(LUser, LServer))
|
|
|
|
end
|
2019-06-30 20:14:37 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
-spec store_message_in_db(module(), #offline_msg{}) -> ok | {error, any()}.
|
|
|
|
store_message_in_db(Mod, #offline_msg{us = {User, Server}} = Msg) ->
|
|
|
|
case Mod:store_message(Msg) of
|
|
|
|
ok ->
|
|
|
|
case use_cache(Mod, Server) of
|
|
|
|
true ->
|
|
|
|
ets_cache:incr(
|
|
|
|
?SPOOL_COUNTER_CACHE,
|
|
|
|
{User, Server}, 1,
|
|
|
|
cache_nodes(Mod, Server));
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end.
|
2019-05-07 09:58:14 +02:00
|
|
|
|
2016-11-13 08:44:53 +01:00
|
|
|
-spec add_delay_info(message(), binary(),
|
|
|
|
undefined | erlang:timestamp()) -> message().
|
2017-01-18 12:09:39 +01:00
|
|
|
add_delay_info(Packet, LServer, TS) ->
|
|
|
|
NewTS = case TS of
|
2019-02-27 09:56:20 +01:00
|
|
|
undefined -> erlang:timestamp();
|
2017-01-18 12:09:39 +01:00
|
|
|
_ -> TS
|
|
|
|
end,
|
2017-01-09 15:02:17 +01:00
|
|
|
Packet1 = xmpp:put_meta(Packet, from_offline, true),
|
2018-10-25 01:05:45 +02:00
|
|
|
misc:add_delay_info(Packet1, jid:make(LServer), NewTS,
|
|
|
|
<<"Offline storage">>).
|
2016-11-13 08:44:53 +01:00
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec get_priority_from_presence(presence()) -> integer().
|
|
|
|
get_priority_from_presence(#presence{priority = Prio}) ->
|
|
|
|
case Prio of
|
|
|
|
undefined -> 0;
|
|
|
|
_ -> Prio
|
|
|
|
end.
|
|
|
|
|
2016-04-15 12:44:33 +02:00
|
|
|
export(LServer) ->
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:export(LServer).
|
2013-07-21 12:24:36 +02:00
|
|
|
|
2016-11-22 14:48:01 +01:00
|
|
|
import_info() ->
|
|
|
|
[{<<"spool">>, 4}].
|
2016-04-15 12:44:33 +02:00
|
|
|
|
2016-11-22 14:48:01 +01:00
|
|
|
import_start(LServer, DBType) ->
|
|
|
|
Mod = gen_mod:db_mod(DBType, ?MODULE),
|
|
|
|
Mod:import(LServer, []).
|
|
|
|
|
|
|
|
import(LServer, {sql, _}, DBType, <<"spool">>,
|
|
|
|
[LUser, XML, _Seq, _TimeStamp]) ->
|
|
|
|
El = fxml_stream:parse_element(XML),
|
2017-02-18 07:36:27 +01:00
|
|
|
#message{from = From, to = To} = Msg = xmpp:decode(El, ?NS_CLIENT, [ignore_els]),
|
|
|
|
TS = case xmpp:get_subtag(Msg, #delay{stamp = {0,0,0}}) of
|
|
|
|
#delay{stamp = {MegaSecs, Secs, _}} ->
|
|
|
|
{MegaSecs, Secs, 0};
|
|
|
|
false ->
|
2019-02-27 09:56:20 +01:00
|
|
|
erlang:timestamp()
|
2017-02-18 07:36:27 +01:00
|
|
|
end,
|
2016-11-22 14:48:01 +01:00
|
|
|
US = {LUser, LServer},
|
2017-02-18 07:36:27 +01:00
|
|
|
Expire = find_x_expire(TS, Msg),
|
|
|
|
OffMsg = #offline_msg{us = US, packet = El,
|
|
|
|
from = From, to = To,
|
|
|
|
timestamp = TS, expire = Expire},
|
2016-04-15 12:44:33 +02:00
|
|
|
Mod = gen_mod:db_mod(DBType, ?MODULE),
|
2017-02-18 07:36:27 +01:00
|
|
|
Mod:import(OffMsg).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2019-04-26 19:59:06 +02:00
|
|
|
use_mam_for_user(_User, Server) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_offline_opt:use_mam_for_storage(Server).
|
2019-04-26 19:59:06 +02:00
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(access_max_user_messages) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:shaper();
|
2018-12-20 13:25:53 +01:00
|
|
|
mod_opt_type(store_groupchat) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:bool();
|
2019-03-14 15:12:39 +01:00
|
|
|
mod_opt_type(bounce_groupchat) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:bool();
|
2019-04-26 19:59:06 +02:00
|
|
|
mod_opt_type(use_mam_for_storage) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:bool();
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(store_empty_body) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:either(
|
|
|
|
unless_chat_state,
|
|
|
|
econf:bool());
|
|
|
|
mod_opt_type(db_type) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:db_type(?MODULE);
|
2019-06-30 20:14:37 +02:00
|
|
|
mod_opt_type(use_cache) ->
|
|
|
|
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_life_time) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:timeout(second, infinity).
|
2019-05-28 14:32:09 +02:00
|
|
|
|
2018-01-23 08:54:52 +01:00
|
|
|
mod_options(Host) ->
|
|
|
|
[{db_type, ejabberd_config:default_db(Host, ?MODULE)},
|
|
|
|
{access_max_user_messages, max_user_offline_messages},
|
2018-12-20 13:25:53 +01:00
|
|
|
{store_empty_body, unless_chat_state},
|
2019-04-26 19:59:06 +02:00
|
|
|
{use_mam_for_storage, false},
|
2019-03-14 15:40:34 +01:00
|
|
|
{bounce_groupchat, false},
|
2019-05-28 14:32:09 +02:00
|
|
|
{store_groupchat, false},
|
2019-06-30 20:14:37 +02:00
|
|
|
{use_cache, ejabberd_option:use_cache(Host)},
|
2019-06-14 11:33:26 +02:00
|
|
|
{cache_size, ejabberd_option:cache_size(Host)},
|
|
|
|
{cache_life_time, ejabberd_option:cache_life_time(Host)}].
|
2020-01-08 10:24:51 +01:00
|
|
|
|
|
|
|
mod_doc() ->
|
|
|
|
#{desc =>
|
|
|
|
[?T("This module implements "
|
|
|
|
"https://xmpp.org/extensions/xep-0160.html"
|
|
|
|
"[XEP-0160: Best Practices for Handling Offline Messages] "
|
|
|
|
"and https://xmpp.org/extensions/xep-0013.html"
|
|
|
|
"[XEP-0013: Flexible Offline Message Retrieval]. "
|
|
|
|
"This means that all messages sent to an offline user "
|
|
|
|
"will be stored on the server until that user comes online "
|
|
|
|
"again. Thus it is very similar to how email works. A user "
|
|
|
|
"is considered offline if no session presence priority > 0 "
|
|
|
|
"are currently open."), "",
|
|
|
|
?T("NOTE: 'ejabberdctl' has a command to "
|
|
|
|
"delete expired messages (see chapter"
|
|
|
|
"https://docs.ejabberd.im/admin/guide/managing"
|
|
|
|
"[Managing an ejabberd server] in online documentation.")],
|
|
|
|
opts =>
|
|
|
|
[{access_max_user_messages,
|
|
|
|
#{value => ?T("AccessName"),
|
|
|
|
desc =>
|
|
|
|
?T("This option defines which access rule will be "
|
|
|
|
"enforced to limit the maximum number of offline "
|
|
|
|
"messages that a user can have (quota). When a user "
|
|
|
|
"has too many offline messages, any new messages that "
|
|
|
|
"they receive are discarded, and a <resource-constraint/> "
|
|
|
|
"error is returned to the sender. The default value is "
|
|
|
|
"'max_user_offline_messages'.")}},
|
|
|
|
{store_empty_body,
|
|
|
|
#{value => "true | false | unless_chat_state",
|
|
|
|
desc =>
|
|
|
|
?T("Whether or not to store messages that lack a <body/> "
|
|
|
|
"element. The default value is 'unless_chat_state', "
|
|
|
|
"which tells ejabberd to store messages even if they "
|
|
|
|
"lack the <body/> element, unless they only contain a "
|
|
|
|
"chat state notification (as defined in "
|
|
|
|
"https://xmpp.org/extensions/xep-0085.html"
|
|
|
|
"[XEP-0085: Chat State Notifications].")}},
|
|
|
|
{store_groupchat,
|
|
|
|
#{value => "true | false",
|
|
|
|
desc =>
|
|
|
|
?T("Whether or not to store groupchat messages. "
|
|
|
|
"The default value is 'false'.")}},
|
|
|
|
{use_mam_for_storage,
|
|
|
|
#{value => "true | false",
|
|
|
|
desc =>
|
|
|
|
?T("This is an experimetal option. Enabling this option "
|
|
|
|
"will make 'mod_offline' not use the former spool "
|
|
|
|
"table for storing MucSub offline messages, but will "
|
|
|
|
"use the archive table instead. This use of the archive "
|
|
|
|
"table is cleaner and it makes it possible for clients "
|
|
|
|
"to slowly drop the former offline use case and rely on "
|
|
|
|
"message archive instead. It also further reduces the "
|
|
|
|
"storage required when you enabled MucSub. Enabling this "
|
|
|
|
"option has a known drawback for the moment: most of "
|
|
|
|
"flexible message retrieval queries don't work (those that "
|
|
|
|
"allow retrieval/deletion of messages by id), but this "
|
|
|
|
"specification is not widely used. The default value "
|
|
|
|
"is 'false' to keep former behaviour as default and "
|
|
|
|
"ensure this option is disabled.")}},
|
|
|
|
{bounce_groupchat,
|
|
|
|
#{value => "true | false",
|
|
|
|
desc =>
|
|
|
|
?T("This option is use the disable an optimisation that "
|
|
|
|
"avoids bouncing error messages when groupchat messages "
|
|
|
|
"could not be stored as offline. It will reduce chat "
|
|
|
|
"room load, without any drawback in standard use cases. "
|
|
|
|
"You may change default value only if you have a custom "
|
|
|
|
"module which uses offline hook after 'mod_offline'. This "
|
|
|
|
"option can be useful for both standard MUC and MucSub, "
|
|
|
|
"but the bounce is much more likely to happen in the context "
|
|
|
|
"of MucSub, so it is even more important to have it on "
|
|
|
|
"large MucSub services. The default value is 'false', meaning "
|
|
|
|
"the optimisation is enabled.")}},
|
|
|
|
{db_type,
|
|
|
|
#{value => "mnesia | sql",
|
|
|
|
desc =>
|
|
|
|
?T("Same as top-level 'default_db' option, but applied to this module only.")}},
|
|
|
|
{use_cache,
|
|
|
|
#{value => "true | false",
|
|
|
|
desc =>
|
|
|
|
?T("Same as top-level 'use_cache' option, but applied to this module only.")}},
|
|
|
|
{cache_size,
|
|
|
|
#{value => "pos_integer() | infinity",
|
|
|
|
desc =>
|
|
|
|
?T("Same as top-level 'cache_size' option, but applied to this module only.")}},
|
|
|
|
{cache_life_time,
|
|
|
|
#{value => "timeout()",
|
|
|
|
desc =>
|
|
|
|
?T("Same as top-level 'cache_life_time' option, but applied to this module only.")}}],
|
|
|
|
example =>
|
|
|
|
[{?T("This example allows power users to have as much as 5000 "
|
|
|
|
"offline messages, administrators up to 2000, and all the "
|
|
|
|
"other users up to 100:"),
|
|
|
|
["acl:",
|
|
|
|
" admin:",
|
|
|
|
" user:",
|
|
|
|
" - admin1@localhost",
|
|
|
|
" - admin2@example.org",
|
|
|
|
" poweruser:",
|
|
|
|
" user:",
|
|
|
|
" - bob@example.org",
|
|
|
|
" - jane@example.org",
|
|
|
|
"",
|
|
|
|
"shaper_rules:",
|
|
|
|
" max_user_offline_messages:",
|
|
|
|
" - 5000: poweruser",
|
|
|
|
" - 2000: admin",
|
|
|
|
" - 100",
|
|
|
|
"",
|
|
|
|
"modules:",
|
|
|
|
" ...",
|
|
|
|
" mod_offline:",
|
|
|
|
" access_max_user_messages: max_user_offline_messages",
|
|
|
|
" ..."
|
|
|
|
]}]}.
|