2017-07-20 20:22:50 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_push.erl
|
|
|
|
%%% Author : Holger Weiss <holger@zedat.fu-berlin.de>
|
|
|
|
%%% Purpose : Push Notifications (XEP-0357)
|
|
|
|
%%% Created : 15 Jul 2017 by Holger Weiss <holger@zedat.fu-berlin.de>
|
|
|
|
%%%
|
|
|
|
%%%
|
2024-01-22 16:40:01 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2017-2024 ProcessOne
|
2017-07-20 20:22:50 +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.
|
|
|
|
%%%
|
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_push).
|
|
|
|
-author('holger@zedat.fu-berlin.de').
|
2024-11-08 19:27:22 +01:00
|
|
|
-protocol({xep, 357, '0.2', '17.08', "complete", ""}).
|
2017-07-20 20:22:50 +02:00
|
|
|
|
2018-01-31 14:57:43 +01:00
|
|
|
-behaviour(gen_mod).
|
2017-07-20 20:22:50 +02:00
|
|
|
|
|
|
|
%% gen_mod callbacks.
|
2018-01-23 08:54:52 +01:00
|
|
|
-export([start/2, stop/1, reload/3, mod_opt_type/1, mod_options/1, depends/2]).
|
2020-01-08 10:24:51 +01:00
|
|
|
-export([mod_doc/0]).
|
2017-07-20 20:22:50 +02:00
|
|
|
%% ejabberd_hooks callbacks.
|
|
|
|
-export([disco_sm_features/5, c2s_session_pending/1, c2s_copy_session/2,
|
2022-05-02 01:54:42 +02:00
|
|
|
c2s_session_resumed/1, c2s_handle_cast/2, c2s_stanza/3, mam_message/7,
|
|
|
|
offline_message/1, remove_user/2]).
|
2017-07-20 20:22:50 +02:00
|
|
|
|
|
|
|
%% gen_iq_handler callback.
|
|
|
|
-export([process_iq/1]).
|
|
|
|
|
|
|
|
%% ejabberd command.
|
|
|
|
-export([get_commands_spec/0, delete_old_sessions/1]).
|
|
|
|
|
2017-07-21 01:07:36 +02:00
|
|
|
%% API (used by mod_push_keepalive).
|
2018-06-13 21:43:40 +02:00
|
|
|
-export([notify/3, notify/5, notify/7, is_incoming_chat_msg/1]).
|
2017-07-21 01:07:36 +02:00
|
|
|
|
2017-11-10 16:02:22 +01:00
|
|
|
%% For IQ callbacks
|
|
|
|
-export([delete_session/3]).
|
|
|
|
|
2017-07-20 20:22:50 +02:00
|
|
|
-include("ejabberd_commands.hrl").
|
|
|
|
-include("logger.hrl").
|
2020-09-03 13:45:57 +02:00
|
|
|
-include_lib("xmpp/include/xmpp.hrl").
|
2019-06-22 16:08:45 +02:00
|
|
|
-include("translate.hrl").
|
2017-07-20 20:22:50 +02:00
|
|
|
|
|
|
|
-define(PUSH_CACHE, push_cache).
|
|
|
|
|
|
|
|
-type c2s_state() :: ejabberd_c2s:state().
|
2022-05-01 15:49:22 +02:00
|
|
|
-type push_session_id() :: erlang:timestamp().
|
|
|
|
-type push_session() :: {push_session_id(), ljid(), binary(), xdata()}.
|
2017-10-27 09:55:48 +02:00
|
|
|
-type err_reason() :: notfound | db_failure.
|
2018-06-13 21:43:40 +02:00
|
|
|
-type direction() :: send | recv | undefined.
|
2017-07-20 20:22:50 +02:00
|
|
|
|
|
|
|
-callback init(binary(), gen_mod:opts())
|
|
|
|
-> any().
|
2022-05-01 15:49:22 +02:00
|
|
|
-callback store_session(binary(), binary(), push_session_id(), jid(), binary(),
|
2017-07-20 20:22:50 +02:00
|
|
|
xdata())
|
2017-10-27 09:55:48 +02:00
|
|
|
-> {ok, push_session()} | {error, err_reason()}.
|
2017-07-20 20:22:50 +02:00
|
|
|
-callback lookup_session(binary(), binary(), jid(), binary())
|
2017-10-27 09:55:48 +02:00
|
|
|
-> {ok, push_session()} | {error, err_reason()}.
|
2022-05-01 15:49:22 +02:00
|
|
|
-callback lookup_session(binary(), binary(), push_session_id())
|
2017-10-27 09:55:48 +02:00
|
|
|
-> {ok, push_session()} | {error, err_reason()}.
|
2017-07-20 20:22:50 +02:00
|
|
|
-callback lookup_sessions(binary(), binary(), jid())
|
2017-10-27 09:55:48 +02:00
|
|
|
-> {ok, [push_session()]} | {error, err_reason()}.
|
2017-07-20 20:22:50 +02:00
|
|
|
-callback lookup_sessions(binary(), binary())
|
2017-10-27 09:55:48 +02:00
|
|
|
-> {ok, [push_session()]} | {error, err_reason()}.
|
2017-07-20 20:22:50 +02:00
|
|
|
-callback lookup_sessions(binary())
|
2017-10-27 09:55:48 +02:00
|
|
|
-> {ok, [push_session()]} | {error, err_reason()}.
|
2022-05-01 15:49:22 +02:00
|
|
|
-callback delete_session(binary(), binary(), push_session_id())
|
2017-10-27 09:55:48 +02:00
|
|
|
-> ok | {error, err_reason()}.
|
2017-07-20 20:22:50 +02:00
|
|
|
-callback delete_old_sessions(binary() | global, erlang:timestamp())
|
2017-10-27 09:55:48 +02:00
|
|
|
-> ok | {error, err_reason()}.
|
2017-07-20 20:22:50 +02:00
|
|
|
-callback use_cache(binary())
|
|
|
|
-> boolean().
|
|
|
|
-callback cache_nodes(binary())
|
|
|
|
-> [node()].
|
|
|
|
|
|
|
|
-optional_callbacks([use_cache/1, cache_nodes/1]).
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% gen_mod callbacks.
|
|
|
|
%%--------------------------------------------------------------------
|
2023-08-09 16:08:45 +02:00
|
|
|
-spec start(binary(), gen_mod:opts()) -> {ok, [gen_mod:registration()]}.
|
2017-07-20 20:22:50 +02:00
|
|
|
start(Host, Opts) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Mod = gen_mod:db_mod(Opts, ?MODULE),
|
2017-07-20 20:22:50 +02:00
|
|
|
Mod:init(Host, Opts),
|
|
|
|
init_cache(Mod, Host, Opts),
|
2023-08-09 16:08:45 +02:00
|
|
|
ejabberd_commands:register_commands(?MODULE, get_commands_spec()),
|
|
|
|
{ok, [{iq_handler, ejabberd_sm, ?NS_PUSH_0, process_iq},
|
|
|
|
{hook, disco_sm_features, disco_sm_features, 50},
|
|
|
|
{hook, c2s_session_pending, c2s_session_pending, 50},
|
|
|
|
{hook, c2s_copy_session, c2s_copy_session, 50},
|
|
|
|
{hook, c2s_session_resumed, c2s_session_resumed, 50},
|
|
|
|
{hook, c2s_handle_cast, c2s_handle_cast, 50},
|
|
|
|
{hook, c2s_handle_send, c2s_stanza, 50},
|
|
|
|
{hook, store_mam_message, mam_message, 50},
|
|
|
|
{hook, offline_message_hook, offline_message, 55},
|
|
|
|
{hook, remove_user, remove_user, 50}]}.
|
|
|
|
|
2017-07-20 20:22:50 +02:00
|
|
|
|
|
|
|
-spec stop(binary()) -> ok.
|
|
|
|
stop(Host) ->
|
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.
|
2017-07-20 20:22:50 +02:00
|
|
|
|
|
|
|
-spec reload(binary(), gen_mod:opts(), gen_mod:opts()) -> ok.
|
|
|
|
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-07-20 20:22:50 +02:00
|
|
|
if NewMod /= OldMod ->
|
|
|
|
NewMod:init(Host, NewOpts);
|
|
|
|
true ->
|
|
|
|
ok
|
2020-11-04 09:04:30 +01:00
|
|
|
end,
|
|
|
|
init_cache(NewMod, Host, NewOpts),
|
|
|
|
ok.
|
2017-07-20 20:22:50 +02:00
|
|
|
|
|
|
|
-spec depends(binary(), gen_mod:opts()) -> [{module(), hard | soft}].
|
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[].
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec mod_opt_type(atom()) -> econf:validator().
|
2023-09-08 19:49:17 +02:00
|
|
|
mod_opt_type(notify_on) ->
|
|
|
|
econf:enum([messages, all]);
|
2018-04-16 23:18:03 +02:00
|
|
|
mod_opt_type(include_sender) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:bool();
|
2018-04-16 23:18:03 +02:00
|
|
|
mod_opt_type(include_body) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:either(
|
|
|
|
econf:bool(),
|
|
|
|
econf:binary());
|
2017-07-20 20:22:50 +02:00
|
|
|
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
|
|
|
|
2018-04-16 18:14:07 +02:00
|
|
|
-spec mod_options(binary()) -> [{atom(), any()}].
|
2018-01-23 08:54:52 +01:00
|
|
|
mod_options(Host) ->
|
2023-09-08 19:49:17 +02:00
|
|
|
[{notify_on, all},
|
|
|
|
{include_sender, false},
|
2018-06-19 23:12:27 +02:00
|
|
|
{include_body, <<"New message">>},
|
2018-04-16 23:18:03 +02: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)}].
|
2017-07-20 20:22:50 +02:00
|
|
|
|
2020-01-08 10:24:51 +01:00
|
|
|
mod_doc() ->
|
|
|
|
#{desc =>
|
|
|
|
?T("This module implements the XMPP server's part of "
|
|
|
|
"the push notification solution specified in "
|
|
|
|
"https://xmpp.org/extensions/xep-0357.html"
|
|
|
|
"[XEP-0357: Push Notifications]. It does not generate, "
|
|
|
|
"for example, APNS or FCM notifications directly. "
|
|
|
|
"Instead, it's designed to work with so-called "
|
|
|
|
"\"app servers\" operated by third-party vendors of "
|
|
|
|
"mobile apps. Those app servers will usually trigger "
|
|
|
|
"notification delivery to the user's mobile device using "
|
2024-06-27 17:20:50 +02:00
|
|
|
"platform-dependent backend services such as FCM or APNS."),
|
2020-01-08 10:24:51 +01:00
|
|
|
opts =>
|
2023-09-08 19:49:17 +02:00
|
|
|
[{notify_on,
|
|
|
|
#{value => "messages | all",
|
2023-10-02 10:54:24 +02:00
|
|
|
note => "added in 23.10",
|
2023-09-08 19:49:17 +02:00
|
|
|
desc =>
|
|
|
|
?T("If this option is set to 'messages', notifications are "
|
|
|
|
"generated only for actual chat messages with a body text "
|
|
|
|
"(or some encrypted payload). If it's set to 'all', any "
|
|
|
|
"kind of XMPP stanza will trigger a notification. If "
|
|
|
|
"unsure, it's strongly recommended to stick to 'all', "
|
|
|
|
"which is the default value.")}},
|
|
|
|
{include_sender,
|
2020-01-08 10:24:51 +01:00
|
|
|
#{value => "true | false",
|
|
|
|
desc =>
|
|
|
|
?T("If this option is set to 'true', the sender's JID "
|
|
|
|
"is included with push notifications generated for "
|
|
|
|
"incoming messages with a body. "
|
|
|
|
"The default value is 'false'.")}},
|
|
|
|
{include_body,
|
|
|
|
#{value => "true | false | Text",
|
|
|
|
desc =>
|
|
|
|
?T("If this option is set to 'true', the message text "
|
|
|
|
"is included with push notifications generated for "
|
|
|
|
"incoming messages with a body. The option can instead "
|
|
|
|
"be set to a static 'Text', in which case the specified "
|
|
|
|
"text will be included in place of the actual message "
|
|
|
|
"body. This can be useful to signal the app server "
|
|
|
|
"whether the notification was triggered by a message "
|
|
|
|
"with body (as opposed to other types of traffic) "
|
|
|
|
"without leaking actual message contents. "
|
|
|
|
"The default value is \"New message\".")}},
|
|
|
|
{db_type,
|
|
|
|
#{value => "mnesia | sql",
|
|
|
|
desc =>
|
2021-08-21 18:31:21 +02:00
|
|
|
?T("Same as top-level _`default_db`_ option, but applied to this module only.")}},
|
2020-01-08 10:24:51 +01:00
|
|
|
{use_cache,
|
|
|
|
#{value => "true | false",
|
|
|
|
desc =>
|
2021-08-21 18:31:21 +02:00
|
|
|
?T("Same as top-level _`use_cache`_ option, but applied to this module only.")}},
|
2020-01-08 10:24:51 +01:00
|
|
|
{cache_size,
|
|
|
|
#{value => "pos_integer() | infinity",
|
|
|
|
desc =>
|
2021-08-21 18:31:21 +02:00
|
|
|
?T("Same as top-level _`cache_size`_ option, but applied to this module only.")}},
|
2020-01-08 10:24:51 +01:00
|
|
|
{cache_missed,
|
|
|
|
#{value => "true | false",
|
|
|
|
desc =>
|
2021-08-21 18:31:21 +02:00
|
|
|
?T("Same as top-level _`cache_missed`_ option, but applied to this module only.")}},
|
2020-01-08 10:24:51 +01:00
|
|
|
{cache_life_time,
|
|
|
|
#{value => "timeout()",
|
|
|
|
desc =>
|
2021-08-21 18:31:21 +02:00
|
|
|
?T("Same as top-level _`cache_life_time`_ option, but applied to this module only.")}}]}.
|
2020-01-08 10:24:51 +01:00
|
|
|
|
2017-07-20 20:22:50 +02:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% ejabberd command callback.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
-spec get_commands_spec() -> [ejabberd_commands()].
|
|
|
|
get_commands_spec() ->
|
|
|
|
[#ejabberd_commands{name = delete_old_push_sessions, tags = [purge],
|
|
|
|
desc = "Remove push sessions older than DAYS",
|
|
|
|
module = ?MODULE, function = delete_old_sessions,
|
|
|
|
args = [{days, integer}],
|
|
|
|
result = {res, rescode}}].
|
|
|
|
|
|
|
|
-spec delete_old_sessions(non_neg_integer()) -> ok | any().
|
|
|
|
delete_old_sessions(Days) ->
|
2019-02-27 09:56:20 +01:00
|
|
|
CurrentTime = erlang:system_time(microsecond),
|
2017-07-20 20:22:50 +02:00
|
|
|
Diff = Days * 24 * 60 * 60 * 1000000,
|
|
|
|
TimeStamp = misc:usec_to_now(CurrentTime - Diff),
|
|
|
|
DBTypes = lists:usort(
|
|
|
|
lists:map(
|
|
|
|
fun(Host) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case mod_push_opt:db_type(Host) of
|
2017-07-20 20:22:50 +02:00
|
|
|
sql -> {sql, Host};
|
|
|
|
Other -> {Other, global}
|
|
|
|
end
|
2019-06-14 11:33:26 +02:00
|
|
|
end, ejabberd_option:hosts())),
|
2017-07-20 20:22:50 +02:00
|
|
|
Results = lists:map(
|
|
|
|
fun({DBType, Host}) ->
|
|
|
|
Mod = gen_mod:db_mod(DBType, ?MODULE),
|
|
|
|
Mod:delete_old_sessions(Host, TimeStamp)
|
|
|
|
end, DBTypes),
|
|
|
|
ets_cache:clear(?PUSH_CACHE, ejabberd_cluster:get_nodes()),
|
|
|
|
case lists:filter(fun(Res) -> Res /= ok end, Results) of
|
|
|
|
[] ->
|
|
|
|
?INFO_MSG("Deleted push sessions older than ~B days", [Days]),
|
|
|
|
ok;
|
2020-11-04 09:07:30 +01:00
|
|
|
[{error, Reason} | _] ->
|
2020-11-04 10:10:20 +01:00
|
|
|
?ERROR_MSG("Error while deleting old push sessions: ~p", [Reason]),
|
2020-11-04 09:07:30 +01:00
|
|
|
Reason
|
2017-07-20 20:22:50 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Service discovery.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
-spec disco_sm_features(empty | {result, [binary()]} | {error, stanza_error()},
|
|
|
|
jid(), jid(), binary(), binary())
|
|
|
|
-> {result, [binary()]} | {error, stanza_error()}.
|
|
|
|
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},
|
|
|
|
#jid{luser = U, lserver = S}, <<"">>, _Lang) ->
|
|
|
|
{result, [?NS_PUSH_0 | OtherFeatures]};
|
|
|
|
disco_sm_features(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% IQ handlers.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
-spec process_iq(iq()) -> iq().
|
|
|
|
process_iq(#iq{type = get, lang = Lang} = IQ) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Value 'get' of 'type' attribute is not allowed"),
|
2017-07-20 20:22:50 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang));
|
|
|
|
process_iq(#iq{lang = Lang, sub_els = [#push_enable{node = <<>>}]} = IQ) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Enabling push without 'node' attribute is not supported"),
|
2017-07-20 20:22:50 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_feature_not_implemented(Txt, Lang));
|
|
|
|
process_iq(#iq{from = #jid{lserver = LServer} = JID,
|
|
|
|
to = #jid{lserver = LServer},
|
2017-10-26 19:11:43 +02:00
|
|
|
lang = Lang,
|
2017-07-20 20:22:50 +02:00
|
|
|
sub_els = [#push_enable{jid = PushJID,
|
|
|
|
node = Node,
|
|
|
|
xdata = XData}]} = IQ) ->
|
|
|
|
case enable(JID, PushJID, Node, XData) of
|
|
|
|
ok ->
|
|
|
|
xmpp:make_iq_result(IQ);
|
2017-10-26 19:11:43 +02:00
|
|
|
{error, db_failure} ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Database failure"),
|
2017-10-26 19:11:43 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_internal_server_error(Txt, Lang));
|
|
|
|
{error, notfound} ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("User session not found"),
|
2017-10-26 19:11:43 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_item_not_found(Txt, Lang))
|
2017-07-20 20:22:50 +02:00
|
|
|
end;
|
|
|
|
process_iq(#iq{from = #jid{lserver = LServer} = JID,
|
|
|
|
to = #jid{lserver = LServer},
|
2017-10-26 19:11:43 +02:00
|
|
|
lang = Lang,
|
2017-07-20 20:22:50 +02:00
|
|
|
sub_els = [#push_disable{jid = PushJID,
|
|
|
|
node = Node}]} = IQ) ->
|
|
|
|
case disable(JID, PushJID, Node) of
|
|
|
|
ok ->
|
|
|
|
xmpp:make_iq_result(IQ);
|
2017-10-26 19:11:43 +02:00
|
|
|
{error, db_failure} ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Database failure"),
|
2017-10-26 19:11:43 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_internal_server_error(Txt, Lang));
|
|
|
|
{error, notfound} ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Push record not found"),
|
2017-10-26 19:11:43 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_item_not_found(Txt, Lang))
|
2017-07-20 20:22:50 +02:00
|
|
|
end;
|
|
|
|
process_iq(IQ) ->
|
|
|
|
xmpp:make_error(IQ, xmpp:err_not_allowed()).
|
|
|
|
|
2017-10-27 09:55:48 +02:00
|
|
|
-spec enable(jid(), jid(), binary(), xdata()) -> ok | {error, err_reason()}.
|
2017-07-20 20:22:50 +02:00
|
|
|
enable(#jid{luser = LUser, lserver = LServer, lresource = LResource} = JID,
|
|
|
|
PushJID, Node, XData) ->
|
|
|
|
case ejabberd_sm:get_session_sid(LUser, LServer, LResource) of
|
2022-05-01 15:49:22 +02:00
|
|
|
{ID, PID} ->
|
|
|
|
case store_session(LUser, LServer, ID, PushJID, Node, XData) of
|
2017-07-20 20:22:50 +02:00
|
|
|
{ok, _} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Enabling push notifications for ~ts",
|
2017-07-20 20:22:50 +02:00
|
|
|
[jid:encode(JID)]),
|
2022-05-01 15:49:22 +02:00
|
|
|
ejabberd_c2s:cast(PID, {push_enable, ID}),
|
2022-05-01 15:26:21 +02:00
|
|
|
ejabberd_sm:set_user_info(LUser, LServer, LResource,
|
2022-05-01 15:49:22 +02:00
|
|
|
push_id, ID);
|
2017-10-27 09:55:48 +02:00
|
|
|
{error, _} = Err ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Cannot enable push for ~ts: database error",
|
2017-07-20 20:22:50 +02:00
|
|
|
[jid:encode(JID)]),
|
2017-10-27 09:55:48 +02:00
|
|
|
Err
|
2017-07-20 20:22:50 +02:00
|
|
|
end;
|
|
|
|
none ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Cannot enable push for ~ts: session not found",
|
2017-07-20 20:22:50 +02:00
|
|
|
[jid:encode(JID)]),
|
2017-10-26 19:11:43 +02:00
|
|
|
{error, notfound}
|
2017-07-20 20:22:50 +02:00
|
|
|
end.
|
|
|
|
|
2017-10-27 09:55:48 +02:00
|
|
|
-spec disable(jid(), jid(), binary() | undefined) -> ok | {error, err_reason()}.
|
2017-07-20 20:22:50 +02:00
|
|
|
disable(#jid{luser = LUser, lserver = LServer, lresource = LResource} = JID,
|
|
|
|
PushJID, Node) ->
|
2022-05-01 15:49:22 +02:00
|
|
|
case ejabberd_sm:get_session_pid(LUser, LServer, LResource) of
|
|
|
|
PID when is_pid(PID) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Disabling push notifications for ~ts",
|
2017-07-20 20:22:50 +02:00
|
|
|
[jid:encode(JID)]),
|
2022-05-01 15:26:21 +02:00
|
|
|
ejabberd_sm:del_user_info(LUser, LServer, LResource, push_id),
|
2017-07-20 20:22:50 +02:00
|
|
|
ejabberd_c2s:cast(PID, push_disable);
|
|
|
|
none ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Session not found while disabling push for ~ts",
|
2017-07-20 20:22:50 +02:00
|
|
|
[jid:encode(JID)])
|
|
|
|
end,
|
2017-10-26 19:11:43 +02:00
|
|
|
if Node /= <<>> ->
|
2017-07-20 20:22:50 +02:00
|
|
|
delete_session(LUser, LServer, PushJID, Node);
|
|
|
|
true ->
|
|
|
|
delete_sessions(LUser, LServer, PushJID)
|
|
|
|
end.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Hook callbacks.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
-spec c2s_stanza(c2s_state(), xmpp_element() | xmlel(), term()) -> c2s_state().
|
2018-04-16 01:08:56 +02:00
|
|
|
c2s_stanza(State, #stream_error{}, _SendResult) ->
|
|
|
|
State;
|
2022-05-01 17:51:07 +02:00
|
|
|
c2s_stanza(#{push_enabled := true, mgmt_state := pending} = State,
|
2018-04-16 23:18:03 +02:00
|
|
|
Pkt, _SendResult) ->
|
2018-04-16 18:12:46 +02:00
|
|
|
?DEBUG("Notifying client of stanza", []),
|
2021-07-28 18:22:39 +02:00
|
|
|
notify(State, Pkt, get_direction(Pkt)),
|
2017-07-20 20:22:50 +02:00
|
|
|
State;
|
|
|
|
c2s_stanza(State, _Pkt, _SendResult) ->
|
|
|
|
State.
|
|
|
|
|
|
|
|
-spec mam_message(message() | drop, binary(), binary(), jid(),
|
2018-12-05 11:14:29 +01:00
|
|
|
binary(), chat | groupchat, recv | send) -> message().
|
|
|
|
mam_message(#message{} = Pkt, LUser, LServer, _Peer, _Nick, chat, Dir) ->
|
2017-07-20 20:22:50 +02:00
|
|
|
case lookup_sessions(LUser, LServer) of
|
|
|
|
{ok, [_|_] = Clients} ->
|
|
|
|
case drop_online_sessions(LUser, LServer, Clients) of
|
|
|
|
[_|_] = Clients1 ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Notifying ~ts@~ts of MAM message", [LUser, LServer]),
|
2018-06-13 21:43:40 +02:00
|
|
|
notify(LUser, LServer, Clients1, Pkt, Dir);
|
2017-07-20 20:22:50 +02:00
|
|
|
[] ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
2017-11-06 00:26:20 +01:00
|
|
|
Pkt;
|
2018-12-05 11:14:29 +01:00
|
|
|
mam_message(Pkt, _LUser, _LServer, _Peer, _Nick, _Type, _Dir) ->
|
2017-07-20 20:22:50 +02:00
|
|
|
Pkt.
|
|
|
|
|
2020-01-05 20:08:54 +01:00
|
|
|
-spec offline_message({any(), message()}) -> {any(), message()}.
|
|
|
|
offline_message({offlined, #message{meta = #{mam_archived := true}}} = Acc) ->
|
|
|
|
Acc; % Push notification was triggered via MAM.
|
|
|
|
offline_message({offlined,
|
|
|
|
#message{to = #jid{luser = LUser,
|
|
|
|
lserver = LServer}} = Pkt} = Acc) ->
|
2017-07-20 20:22:50 +02:00
|
|
|
case lookup_sessions(LUser, LServer) of
|
|
|
|
{ok, [_|_] = Clients} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Notifying ~ts@~ts of offline message", [LUser, LServer]),
|
2018-06-13 21:43:40 +02:00
|
|
|
notify(LUser, LServer, Clients, Pkt, recv);
|
2017-07-20 20:22:50 +02:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
2020-01-05 20:08:54 +01:00
|
|
|
Acc;
|
|
|
|
offline_message(Acc) ->
|
|
|
|
Acc.
|
2017-07-20 20:22:50 +02:00
|
|
|
|
|
|
|
-spec c2s_session_pending(c2s_state()) -> c2s_state().
|
2022-05-01 17:51:07 +02:00
|
|
|
c2s_session_pending(#{push_enabled := true, mgmt_queue := Queue} = State) ->
|
2017-07-20 20:22:50 +02:00
|
|
|
case p1_queue:len(Queue) of
|
|
|
|
Len when Len > 0 ->
|
2018-04-16 18:12:46 +02:00
|
|
|
?DEBUG("Notifying client of unacknowledged stanza(s)", []),
|
2018-06-13 21:43:40 +02:00
|
|
|
{Pkt, Dir} = case mod_stream_mgmt:queue_find(
|
|
|
|
fun is_incoming_chat_msg/1, Queue) of
|
|
|
|
none -> {none, undefined};
|
2021-07-28 18:22:39 +02:00
|
|
|
Pkt0 -> {Pkt0, get_direction(Pkt0)}
|
2018-06-13 21:43:40 +02:00
|
|
|
end,
|
|
|
|
notify(State, Pkt, Dir),
|
2017-07-20 20:22:50 +02:00
|
|
|
State;
|
|
|
|
0 ->
|
|
|
|
State
|
|
|
|
end;
|
|
|
|
c2s_session_pending(State) ->
|
|
|
|
State.
|
|
|
|
|
|
|
|
-spec c2s_copy_session(c2s_state(), c2s_state()) -> c2s_state().
|
2022-05-01 17:51:07 +02:00
|
|
|
c2s_copy_session(State, #{push_enabled := true,
|
|
|
|
push_session_id := ID}) ->
|
|
|
|
State#{push_enabled => true,
|
|
|
|
push_session_id => ID};
|
2017-07-20 20:22:50 +02:00
|
|
|
c2s_copy_session(State, _) ->
|
|
|
|
State.
|
|
|
|
|
2022-05-02 01:54:42 +02:00
|
|
|
-spec c2s_session_resumed(c2s_state()) -> c2s_state().
|
|
|
|
c2s_session_resumed(#{push_session_id := ID,
|
|
|
|
user := U, server := S, resource := R} = State) ->
|
|
|
|
ejabberd_sm:set_user_info(U, S, R, push_id, ID),
|
|
|
|
State;
|
|
|
|
c2s_session_resumed(State) ->
|
|
|
|
State.
|
|
|
|
|
2017-07-20 20:22:50 +02:00
|
|
|
-spec c2s_handle_cast(c2s_state(), any()) -> c2s_state() | {stop, c2s_state()}.
|
2022-05-01 15:49:22 +02:00
|
|
|
c2s_handle_cast(State, {push_enable, ID}) ->
|
2022-05-01 17:51:07 +02:00
|
|
|
{stop, State#{push_enabled => true,
|
|
|
|
push_session_id => ID}};
|
2017-07-20 20:22:50 +02:00
|
|
|
c2s_handle_cast(State, push_disable) ->
|
2023-12-08 18:52:53 +01:00
|
|
|
State1 = maps:remove(push_enabled, State),
|
2022-05-01 17:51:07 +02:00
|
|
|
State2 = maps:remove(push_session_id, State1),
|
|
|
|
{stop, State2};
|
2017-07-20 20:22:50 +02:00
|
|
|
c2s_handle_cast(State, _Msg) ->
|
|
|
|
State.
|
|
|
|
|
2017-10-27 09:55:48 +02:00
|
|
|
-spec remove_user(binary(), binary()) -> ok | {error, err_reason()}.
|
2017-07-20 20:22:50 +02:00
|
|
|
remove_user(LUser, LServer) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Removing any push sessions of ~ts@~ts", [LUser, LServer]),
|
2017-07-20 20:22:50 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
LookupFun = fun() -> Mod:lookup_sessions(LUser, LServer) end,
|
|
|
|
delete_sessions(LUser, LServer, LookupFun, Mod).
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
2017-07-21 01:07:36 +02:00
|
|
|
%% Generate push notifications.
|
2017-07-20 20:22:50 +02:00
|
|
|
%%--------------------------------------------------------------------
|
2018-06-13 21:43:40 +02:00
|
|
|
-spec notify(c2s_state(), xmpp_element() | xmlel() | none, direction()) -> ok.
|
2022-05-01 15:26:21 +02:00
|
|
|
notify(#{jid := #jid{luser = LUser, lserver = LServer}} = State, Pkt, Dir) ->
|
|
|
|
case lookup_session(LUser, LServer, State) of
|
2017-07-20 20:22:50 +02:00
|
|
|
{ok, Client} ->
|
2018-06-13 21:43:40 +02:00
|
|
|
notify(LUser, LServer, [Client], Pkt, Dir);
|
2017-10-26 19:11:43 +02:00
|
|
|
_Err ->
|
2017-07-20 20:22:50 +02:00
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2018-04-16 23:18:03 +02:00
|
|
|
-spec notify(binary(), binary(), [push_session()],
|
2018-06-13 21:43:40 +02:00
|
|
|
xmpp_element() | xmlel() | none, direction()) -> ok.
|
|
|
|
notify(LUser, LServer, Clients, Pkt, Dir) ->
|
2017-07-20 20:22:50 +02:00
|
|
|
lists:foreach(
|
2022-05-01 15:49:22 +02:00
|
|
|
fun({ID, PushLJID, Node, XData}) ->
|
2019-02-11 22:29:49 +01:00
|
|
|
HandleResponse =
|
|
|
|
fun(#iq{type = result}) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("~ts accepted notification for ~ts@~ts (~ts)",
|
2019-02-11 22:29:49 +01:00
|
|
|
[jid:encode(PushLJID), LUser, LServer, Node]);
|
|
|
|
(#iq{type = error} = IQ) ->
|
|
|
|
case inspect_error(IQ) of
|
|
|
|
{wait, Reason} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("~ts rejected notification for "
|
|
|
|
"~ts@~ts (~ts) temporarily: ~ts",
|
2019-02-11 22:29:49 +01:00
|
|
|
[jid:encode(PushLJID), LUser,
|
|
|
|
LServer, Node, Reason]);
|
|
|
|
{Type, Reason} ->
|
|
|
|
spawn(?MODULE, delete_session,
|
2022-05-01 15:49:22 +02:00
|
|
|
[LUser, LServer, ID]),
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("~ts rejected notification for "
|
|
|
|
"~ts@~ts (~ts), disabling push: ~ts "
|
|
|
|
"(~ts)",
|
2019-02-11 22:29:49 +01:00
|
|
|
[jid:encode(PushLJID), LUser,
|
|
|
|
LServer, Node, Reason, Type])
|
|
|
|
end;
|
|
|
|
(timeout) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Timeout sending notification for ~ts@~ts (~ts) "
|
|
|
|
"to ~ts",
|
2019-02-11 22:29:49 +01:00
|
|
|
[LUser, LServer, Node, jid:encode(PushLJID)]),
|
|
|
|
ok % Hmm.
|
|
|
|
end,
|
2018-06-13 21:43:40 +02:00
|
|
|
notify(LServer, PushLJID, Node, XData, Pkt, Dir, HandleResponse)
|
2017-07-20 20:22:50 +02:00
|
|
|
end, Clients).
|
|
|
|
|
|
|
|
-spec notify(binary(), ljid(), binary(), xdata(),
|
2018-06-13 21:43:40 +02:00
|
|
|
xmpp_element() | xmlel() | none, direction(),
|
2017-07-20 20:22:50 +02:00
|
|
|
fun((iq() | timeout) -> any())) -> ok.
|
2021-07-28 18:22:39 +02:00
|
|
|
notify(LServer, PushLJID, Node, XData, Pkt0, Dir, HandleResponse) ->
|
|
|
|
Pkt = unwrap_message(Pkt0),
|
2017-07-20 20:22:50 +02:00
|
|
|
From = jid:make(LServer),
|
2023-09-08 19:49:17 +02:00
|
|
|
case {make_summary(LServer, Pkt, Dir), mod_push_opt:notify_on(LServer)} of
|
|
|
|
{undefined, messages} ->
|
|
|
|
?DEBUG("Suppressing notification for stanza without payload", []),
|
|
|
|
ok;
|
|
|
|
{Summary, _NotifyOn} ->
|
|
|
|
Item = #ps_item{sub_els = [#push_notification{xdata = Summary}]},
|
|
|
|
PubSub = #pubsub{publish = #ps_publish{node = Node, items = [Item]},
|
|
|
|
publish_options = XData},
|
|
|
|
IQ = #iq{type = set,
|
|
|
|
from = From,
|
|
|
|
to = jid:make(PushLJID),
|
|
|
|
id = p1_rand:get_string(),
|
|
|
|
sub_els = [PubSub]},
|
|
|
|
ejabberd_router:route_iq(IQ, HandleResponse)
|
|
|
|
end.
|
2017-07-20 20:22:50 +02:00
|
|
|
|
2018-04-17 00:27:07 +02:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Miscellaneous.
|
|
|
|
%%--------------------------------------------------------------------
|
2018-06-13 21:43:40 +02:00
|
|
|
-spec is_incoming_chat_msg(stanza()) -> boolean().
|
|
|
|
is_incoming_chat_msg(#message{} = Msg) ->
|
|
|
|
case get_direction(Msg) of
|
2021-04-08 19:36:16 +02:00
|
|
|
recv -> get_body_text(unwrap_message(Msg)) /= none;
|
2018-06-13 21:43:40 +02:00
|
|
|
send -> false
|
|
|
|
end;
|
|
|
|
is_incoming_chat_msg(_Stanza) ->
|
2018-04-17 00:27:07 +02:00
|
|
|
false.
|
|
|
|
|
2017-07-21 01:07:36 +02:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Internal functions.
|
|
|
|
%%--------------------------------------------------------------------
|
2022-05-01 15:49:22 +02:00
|
|
|
-spec store_session(binary(), binary(), push_session_id(), jid(), binary(),
|
|
|
|
xdata()) -> {ok, push_session()} | {error, err_reason()}.
|
|
|
|
store_session(LUser, LServer, ID, PushJID, Node, XData) ->
|
2017-07-20 20:22:50 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
delete_session(LUser, LServer, PushJID, Node),
|
|
|
|
case use_cache(Mod, LServer) of
|
|
|
|
true ->
|
|
|
|
ets_cache:delete(?PUSH_CACHE, {LUser, LServer},
|
|
|
|
cache_nodes(Mod, LServer)),
|
|
|
|
ets_cache:update(
|
|
|
|
?PUSH_CACHE,
|
2022-05-01 15:49:22 +02:00
|
|
|
{LUser, LServer, ID}, {ok, {ID, PushJID, Node, XData}},
|
2017-07-20 20:22:50 +02:00
|
|
|
fun() ->
|
2022-05-01 15:49:22 +02:00
|
|
|
Mod:store_session(LUser, LServer, ID, PushJID, Node,
|
2017-07-20 20:22:50 +02:00
|
|
|
XData)
|
|
|
|
end, cache_nodes(Mod, LServer));
|
|
|
|
false ->
|
2022-05-01 15:49:22 +02:00
|
|
|
Mod:store_session(LUser, LServer, ID, PushJID, Node, XData)
|
2017-07-20 20:22:50 +02:00
|
|
|
end.
|
|
|
|
|
2022-05-01 15:26:21 +02:00
|
|
|
-spec lookup_session(binary(), binary(), c2s_state())
|
2017-10-27 09:55:48 +02:00
|
|
|
-> {ok, push_session()} | error | {error, err_reason()}.
|
2022-05-01 15:49:22 +02:00
|
|
|
lookup_session(LUser, LServer, #{push_session_id := ID}) ->
|
2017-07-20 20:22:50 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
case use_cache(Mod, LServer) of
|
|
|
|
true ->
|
|
|
|
ets_cache:lookup(
|
2022-05-01 15:49:22 +02:00
|
|
|
?PUSH_CACHE, {LUser, LServer, ID},
|
|
|
|
fun() -> Mod:lookup_session(LUser, LServer, ID) end);
|
2017-07-20 20:22:50 +02:00
|
|
|
false ->
|
2022-05-01 15:49:22 +02:00
|
|
|
Mod:lookup_session(LUser, LServer, ID)
|
2017-07-20 20:22:50 +02:00
|
|
|
end.
|
|
|
|
|
2017-10-27 09:55:48 +02:00
|
|
|
-spec lookup_sessions(binary(), binary()) -> {ok, [push_session()]} | {error, err_reason()}.
|
2017-07-20 20:22:50 +02:00
|
|
|
lookup_sessions(LUser, LServer) ->
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
case use_cache(Mod, LServer) of
|
|
|
|
true ->
|
|
|
|
ets_cache:lookup(
|
|
|
|
?PUSH_CACHE, {LUser, LServer},
|
|
|
|
fun() -> Mod:lookup_sessions(LUser, LServer) end);
|
|
|
|
false ->
|
|
|
|
Mod:lookup_sessions(LUser, LServer)
|
|
|
|
end.
|
|
|
|
|
2022-05-01 15:49:22 +02:00
|
|
|
-spec delete_session(binary(), binary(), push_session_id())
|
|
|
|
-> ok | {error, db_failure}.
|
|
|
|
delete_session(LUser, LServer, ID) ->
|
2017-07-20 20:22:50 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
2022-05-01 15:49:22 +02:00
|
|
|
case Mod:delete_session(LUser, LServer, ID) of
|
2017-10-26 19:11:43 +02:00
|
|
|
ok ->
|
|
|
|
case use_cache(Mod, LServer) of
|
|
|
|
true ->
|
|
|
|
ets_cache:delete(?PUSH_CACHE, {LUser, LServer},
|
|
|
|
cache_nodes(Mod, LServer)),
|
2022-05-01 15:49:22 +02:00
|
|
|
ets_cache:delete(?PUSH_CACHE, {LUser, LServer, ID},
|
2017-10-26 19:11:43 +02:00
|
|
|
cache_nodes(Mod, LServer));
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end;
|
2017-10-27 09:55:48 +02:00
|
|
|
{error, _} = Err ->
|
|
|
|
Err
|
2017-07-20 20:22:50 +02:00
|
|
|
end.
|
|
|
|
|
2017-10-27 09:55:48 +02:00
|
|
|
-spec delete_session(binary(), binary(), jid(), binary()) -> ok | {error, err_reason()}.
|
2017-07-20 20:22:50 +02:00
|
|
|
delete_session(LUser, LServer, PushJID, Node) ->
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
case Mod:lookup_session(LUser, LServer, PushJID, Node) of
|
2022-05-01 15:49:22 +02:00
|
|
|
{ok, {ID, _, _, _}} ->
|
|
|
|
delete_session(LUser, LServer, ID);
|
2017-07-20 20:22:50 +02:00
|
|
|
error ->
|
2017-10-26 19:11:43 +02:00
|
|
|
{error, notfound};
|
2017-10-27 09:55:48 +02:00
|
|
|
{error, _} = Err ->
|
|
|
|
Err
|
2017-07-20 20:22:50 +02:00
|
|
|
end.
|
|
|
|
|
2017-10-27 09:55:48 +02:00
|
|
|
-spec delete_sessions(binary(), binary(), jid()) -> ok | {error, err_reason()}.
|
2017-07-20 20:22:50 +02:00
|
|
|
delete_sessions(LUser, LServer, PushJID) ->
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
LookupFun = fun() -> Mod:lookup_sessions(LUser, LServer, PushJID) end,
|
|
|
|
delete_sessions(LUser, LServer, LookupFun, Mod).
|
|
|
|
|
2017-10-26 19:11:43 +02:00
|
|
|
-spec delete_sessions(binary(), binary(), fun(() -> any()), module())
|
2017-10-27 09:55:48 +02:00
|
|
|
-> ok | {error, err_reason()}.
|
2017-07-20 20:22:50 +02:00
|
|
|
delete_sessions(LUser, LServer, LookupFun, Mod) ->
|
|
|
|
case LookupFun() of
|
2017-10-26 19:11:43 +02:00
|
|
|
{ok, []} ->
|
|
|
|
{error, notfound};
|
2017-07-20 20:22:50 +02:00
|
|
|
{ok, Clients} ->
|
|
|
|
case use_cache(Mod, LServer) of
|
|
|
|
true ->
|
|
|
|
ets_cache:delete(?PUSH_CACHE, {LUser, LServer},
|
|
|
|
cache_nodes(Mod, LServer));
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
lists:foreach(
|
2022-05-01 15:49:22 +02:00
|
|
|
fun({ID, _, _, _}) ->
|
|
|
|
ok = Mod:delete_session(LUser, LServer, ID),
|
2017-07-20 20:22:50 +02:00
|
|
|
case use_cache(Mod, LServer) of
|
|
|
|
true ->
|
|
|
|
ets_cache:delete(?PUSH_CACHE,
|
2022-05-01 15:49:22 +02:00
|
|
|
{LUser, LServer, ID},
|
2017-07-20 20:22:50 +02:00
|
|
|
cache_nodes(Mod, LServer));
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end, Clients);
|
2017-10-27 09:55:48 +02:00
|
|
|
{error, _} = Err ->
|
|
|
|
Err
|
2017-07-20 20:22:50 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
-spec drop_online_sessions(binary(), binary(), [push_session()])
|
|
|
|
-> [push_session()].
|
|
|
|
drop_online_sessions(LUser, LServer, Clients) ->
|
2022-05-01 15:49:22 +02:00
|
|
|
OnlineIDs = lists:filtermap(
|
2022-05-01 15:26:21 +02:00
|
|
|
fun({_, Info}) ->
|
|
|
|
case proplists:get_value(push_id, Info) of
|
|
|
|
OnlineID = {_, _, _} ->
|
|
|
|
{true, OnlineID};
|
|
|
|
undefined ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end, ejabberd_sm:get_user_info(LUser, LServer)),
|
2022-05-01 15:49:22 +02:00
|
|
|
[Client || {ID, _, _, _} = Client <- Clients,
|
|
|
|
not lists:member(ID, OnlineIDs)].
|
2017-07-20 20:22:50 +02:00
|
|
|
|
2018-06-13 21:43:40 +02:00
|
|
|
-spec make_summary(binary(), xmpp_element() | xmlel() | none, direction())
|
2018-04-16 23:18:03 +02:00
|
|
|
-> xdata() | undefined.
|
2023-09-09 17:33:16 +02:00
|
|
|
make_summary(Host, #message{from = From0} = Pkt, recv) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case {mod_push_opt:include_sender(Host),
|
|
|
|
mod_push_opt:include_body(Host)} of
|
2018-04-16 23:18:03 +02:00
|
|
|
{false, false} ->
|
|
|
|
undefined;
|
|
|
|
{IncludeSender, IncludeBody} ->
|
|
|
|
case get_body_text(Pkt) of
|
|
|
|
none ->
|
|
|
|
undefined;
|
|
|
|
Text ->
|
|
|
|
Fields1 = case IncludeBody of
|
|
|
|
StaticText when is_binary(StaticText) ->
|
|
|
|
[{'last-message-body', StaticText}];
|
|
|
|
true ->
|
|
|
|
[{'last-message-body', Text}];
|
|
|
|
false ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
Fields2 = case IncludeSender of
|
|
|
|
true ->
|
2023-09-09 17:33:16 +02:00
|
|
|
From = jid:remove_resource(From0),
|
2018-04-16 23:18:03 +02:00
|
|
|
[{'last-message-sender', From} | Fields1];
|
|
|
|
false ->
|
|
|
|
Fields1
|
|
|
|
end,
|
|
|
|
#xdata{type = submit, fields = push_summary:encode(Fields2)}
|
|
|
|
end
|
|
|
|
end;
|
2018-06-13 21:43:40 +02:00
|
|
|
make_summary(_Host, _Pkt, _Dir) ->
|
|
|
|
undefined.
|
|
|
|
|
2021-07-28 18:22:39 +02:00
|
|
|
-spec unwrap_message(Stanza) -> Stanza when Stanza :: stanza() | none.
|
2021-04-08 19:36:16 +02:00
|
|
|
unwrap_message(#message{meta = #{carbon_copy := true}} = Msg) ->
|
2018-10-25 01:05:45 +02:00
|
|
|
misc:unwrap_carbon(Msg);
|
2021-04-08 19:36:16 +02:00
|
|
|
unwrap_message(#message{type = normal} = Msg) ->
|
|
|
|
case misc:unwrap_mucsub_message(Msg) of
|
|
|
|
#message{} = InnerMsg ->
|
|
|
|
InnerMsg;
|
|
|
|
false ->
|
|
|
|
Msg
|
|
|
|
end;
|
|
|
|
unwrap_message(Stanza) ->
|
2018-06-13 21:43:40 +02:00
|
|
|
Stanza.
|
|
|
|
|
|
|
|
-spec get_direction(stanza()) -> direction().
|
|
|
|
get_direction(#message{meta = #{carbon_copy := true},
|
|
|
|
from = #jid{luser = U, lserver = S},
|
|
|
|
to = #jid{luser = U, lserver = S}}) ->
|
|
|
|
send;
|
|
|
|
get_direction(#message{}) ->
|
|
|
|
recv;
|
|
|
|
get_direction(_Stanza) ->
|
2018-04-16 23:18:03 +02:00
|
|
|
undefined.
|
|
|
|
|
|
|
|
-spec get_body_text(message()) -> binary() | none.
|
|
|
|
get_body_text(#message{body = Body} = Msg) ->
|
|
|
|
case xmpp:get_text(Body) of
|
|
|
|
Text when byte_size(Text) > 0 ->
|
|
|
|
Text;
|
|
|
|
<<>> ->
|
|
|
|
case body_is_encrypted(Msg) of
|
|
|
|
true ->
|
|
|
|
<<"(encrypted)">>;
|
|
|
|
false ->
|
|
|
|
none
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec body_is_encrypted(message()) -> boolean().
|
2019-03-11 22:58:53 +01:00
|
|
|
body_is_encrypted(#message{sub_els = MsgEls}) ->
|
|
|
|
case lists:keyfind(<<"encrypted">>, #xmlel.name, MsgEls) of
|
|
|
|
#xmlel{children = EncEls} ->
|
|
|
|
lists:keyfind(<<"payload">>, #xmlel.name, EncEls) /= false;
|
|
|
|
false ->
|
|
|
|
false
|
|
|
|
end.
|
2018-04-16 23:18:03 +02:00
|
|
|
|
2019-02-11 22:29:49 +01:00
|
|
|
-spec inspect_error(iq()) -> {atom(), binary()}.
|
|
|
|
inspect_error(IQ) ->
|
|
|
|
case xmpp:get_error(IQ) of
|
|
|
|
#stanza_error{type = Type} = Err ->
|
|
|
|
{Type, xmpp:format_stanza_error(Err)};
|
|
|
|
undefined ->
|
|
|
|
{undefined, <<"unrecognized error">>}
|
|
|
|
end.
|
|
|
|
|
2017-07-20 20:22:50 +02:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Caching.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
-spec init_cache(module(), binary(), gen_mod:opts()) -> ok.
|
|
|
|
init_cache(Mod, Host, Opts) ->
|
|
|
|
case use_cache(Mod, Host) of
|
|
|
|
true ->
|
2018-01-23 08:54:52 +01:00
|
|
|
CacheOpts = cache_opts(Opts),
|
2017-07-20 20:22:50 +02:00
|
|
|
ets_cache:new(?PUSH_CACHE, CacheOpts);
|
|
|
|
false ->
|
|
|
|
ets_cache:delete(?PUSH_CACHE)
|
|
|
|
end.
|
|
|
|
|
2018-01-23 08:54:52 +01:00
|
|
|
-spec cache_opts(gen_mod:opts()) -> [proplists:property()].
|
|
|
|
cache_opts(Opts) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
MaxSize = mod_push_opt:cache_size(Opts),
|
|
|
|
CacheMissed = mod_push_opt:cache_missed(Opts),
|
2019-06-15 11:53:16 +02:00
|
|
|
LifeTime = mod_push_opt:cache_life_time(Opts),
|
2017-07-20 20:22:50 +02:00
|
|
|
[{max_size, MaxSize}, {cache_missed, CacheMissed}, {life_time, LifeTime}].
|
|
|
|
|
|
|
|
-spec use_cache(module(), binary()) -> boolean().
|
|
|
|
use_cache(Mod, Host) ->
|
|
|
|
case erlang:function_exported(Mod, use_cache, 1) of
|
|
|
|
true -> Mod:use_cache(Host);
|
2019-06-14 11:33:26 +02:00
|
|
|
false -> mod_push_opt:use_cache(Host)
|
2017-07-20 20:22:50 +02:00
|
|
|
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.
|