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
|
|
|
%%%
|
|
|
|
%%%
|
2016-01-13 12:29:14 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2016 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
|
|
|
|
2014-07-31 12:25:37 +02:00
|
|
|
-define(GEN_SERVER, p1_server).
|
|
|
|
-behaviour(?GEN_SERVER).
|
2003-01-05 21:24:59 +01:00
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
-export([start/2,
|
2015-10-07 00:06:58 +02:00
|
|
|
start_link/2,
|
2005-06-20 05:18:13 +02:00
|
|
|
stop/1,
|
2003-01-05 21:24:59 +01:00
|
|
|
store_packet/3,
|
2016-01-28 12:23:51 +01:00
|
|
|
store_offline_msg/5,
|
2005-04-17 20:08:34 +02:00
|
|
|
resend_offline_messages/2,
|
|
|
|
pop_offline_messages/3,
|
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,
|
|
|
|
handle_offline_query/3,
|
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,
|
2015-10-07 00:06:58 +02:00
|
|
|
import/1,
|
|
|
|
import/3,
|
|
|
|
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,
|
2007-08-23 02:51:54 +02:00
|
|
|
webadmin_page/3,
|
2008-10-12 16:16:05 +02:00
|
|
|
webadmin_user/4,
|
|
|
|
webadmin_user_parse_query/5]).
|
2003-01-05 21:24:59 +01:00
|
|
|
|
2014-07-31 12:25:37 +02:00
|
|
|
-export([init/1, handle_call/3, handle_cast/2,
|
2015-06-01 14:38:27 +02:00
|
|
|
handle_info/2, terminate/2, code_change/3,
|
|
|
|
mod_opt_type/1]).
|
2014-07-31 12:25:37 +02:00
|
|
|
|
2016-03-04 11:09:14 +01:00
|
|
|
-deprecated({get_queue_length,2}).
|
|
|
|
|
2004-09-10 22:57:00 +02:00
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-03-09 21:46:47 +01:00
|
|
|
-include("jlib.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
|
|
|
|
2003-02-13 20:39:13 +01:00
|
|
|
-define(PROCNAME, ejabberd_offline).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
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).
|
|
|
|
|
2016-04-15 12:44:33 +02:00
|
|
|
-type us() :: {binary(), binary()}.
|
|
|
|
-callback init(binary(), gen_mod:opts()) -> any().
|
|
|
|
-callback import(binary(), #offline_msg{}) -> ok | pass.
|
|
|
|
-callback store_messages(binary(), us(), [#offline_msg{}],
|
|
|
|
non_neg_integer(), non_neg_integer()) ->
|
|
|
|
{atomic, any()}.
|
|
|
|
-callback pop_messages(binary(), binary()) ->
|
|
|
|
{atomic, [#offline_msg{}]} | {aborted, any()}.
|
|
|
|
-callback remove_expired_messages(binary()) -> {atomic, any()}.
|
|
|
|
-callback remove_old_messages(non_neg_integer(), binary()) -> {atomic, any()}.
|
|
|
|
-callback remove_user(binary(), binary()) -> {atomic, any()}.
|
|
|
|
-callback read_message_headers(binary(), binary()) -> any().
|
|
|
|
-callback read_message(binary(), binary(), non_neg_integer()) ->
|
|
|
|
{ok, #offline_msg{}} | error.
|
|
|
|
-callback remove_message(binary(), binary(), non_neg_integer()) -> ok.
|
|
|
|
-callback read_all_messages(binary(), binary()) -> [#offline_msg{}].
|
|
|
|
-callback remove_all_messages(binary(), binary()) -> {atomic, any()}.
|
|
|
|
-callback count_messages(binary(), binary()) -> non_neg_integer().
|
|
|
|
|
2014-07-31 12:25:37 +02:00
|
|
|
start_link(Host, Opts) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
|
|
|
?GEN_SERVER:start_link({local, Proc}, ?MODULE,
|
|
|
|
[Host, Opts], []).
|
|
|
|
|
2007-08-13 19:28:26 +02:00
|
|
|
start(Host, Opts) ->
|
2014-07-31 12:25:37 +02:00
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
|
|
|
ChildSpec = {Proc, {?MODULE, start_link, [Host, Opts]},
|
2015-03-09 12:28:55 +01:00
|
|
|
transient, 1000, worker, [?MODULE]},
|
2014-07-31 12:25:37 +02:00
|
|
|
supervisor:start_child(ejabberd_sup, ChildSpec).
|
|
|
|
|
|
|
|
stop(Host) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
2015-03-12 10:12:22 +01:00
|
|
|
catch ?GEN_SERVER:call(Proc, stop),
|
|
|
|
supervisor:terminate_child(ejabberd_sup, Proc),
|
2014-07-31 12:25:37 +02:00
|
|
|
supervisor:delete_child(ejabberd_sup, Proc),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
|
|
|
|
|
|
|
init([Host, Opts]) ->
|
2016-04-15 12:44:33 +02:00
|
|
|
Mod = gen_mod:db_mod(Host, Opts, ?MODULE),
|
|
|
|
Mod:init(Host, Opts),
|
2016-02-09 15:59:54 +01:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, fun gen_iq_handler:check_type/1,
|
|
|
|
no_queue),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_hooks:add(offline_message_hook, Host, ?MODULE,
|
|
|
|
store_packet, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:add(resend_offline_messages_hook, Host,
|
2004-08-08 21:07:55 +02:00
|
|
|
?MODULE, pop_offline_messages, 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),
|
2007-05-12 18:28:34 +02:00
|
|
|
ejabberd_hooks:add(anonymous_purge_hook, Host,
|
|
|
|
?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),
|
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,
|
|
|
|
?MODULE, handle_offline_query, IQDisc),
|
2015-10-07 00:06:58 +02:00
|
|
|
AccessMaxOfflineMsgs =
|
|
|
|
gen_mod:get_opt(access_max_user_messages, Opts,
|
2016-06-21 13:18:24 +02:00
|
|
|
fun acl:shaper_rules_validator/1,
|
2015-10-07 00:06:58 +02:00
|
|
|
max_user_offline_messages),
|
2014-07-31 12:25:37 +02:00
|
|
|
{ok,
|
|
|
|
#state{host = Host,
|
|
|
|
access_max_offline_messages = AccessMaxOfflineMsgs}}.
|
|
|
|
|
|
|
|
|
|
|
|
handle_call(stop, _From, State) ->
|
|
|
|
{stop, normal, ok, State}.
|
|
|
|
|
|
|
|
|
|
|
|
handle_cast(_Msg, State) -> {noreply, State}.
|
|
|
|
|
|
|
|
|
|
|
|
handle_info(#offline_msg{us = UserServer} = Msg, State) ->
|
|
|
|
#state{host = Host,
|
|
|
|
access_max_offline_messages = AccessMaxOfflineMsgs} = State,
|
|
|
|
DBType = gen_mod:db_type(Host, ?MODULE),
|
|
|
|
Msgs = receive_all(UserServer, [Msg], DBType),
|
|
|
|
Len = length(Msgs),
|
|
|
|
MaxOfflineMsgs = get_max_user_messages(AccessMaxOfflineMsgs,
|
|
|
|
UserServer, Host),
|
2016-04-15 12:44:33 +02:00
|
|
|
store_offline_msg(Host, UserServer, Msgs, Len, MaxOfflineMsgs),
|
2014-07-31 12:25:37 +02:00
|
|
|
{noreply, State};
|
|
|
|
|
|
|
|
handle_info(_Info, State) ->
|
|
|
|
?ERROR_MSG("got unexpected info: ~p", [_Info]),
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
|
|
|
|
terminate(_Reason, State) ->
|
|
|
|
Host = State#state.host,
|
|
|
|
ejabberd_hooks:delete(offline_message_hook, Host,
|
|
|
|
?MODULE, store_packet, 50),
|
|
|
|
ejabberd_hooks:delete(resend_offline_messages_hook,
|
|
|
|
Host, ?MODULE, pop_offline_messages, 50),
|
|
|
|
ejabberd_hooks:delete(remove_user, Host, ?MODULE,
|
|
|
|
remove_user, 50),
|
|
|
|
ejabberd_hooks:delete(anonymous_purge_hook, 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),
|
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),
|
2016-02-09 15:59:54 +01:00
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_FLEX_OFFLINE),
|
2014-07-31 12:25:37 +02:00
|
|
|
ok.
|
|
|
|
|
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) -> {ok, State}.
|
2003-02-13 20:39:13 +01:00
|
|
|
|
2016-01-28 12:23:51 +01:00
|
|
|
store_offline_msg(Host, US, Msgs, Len, MaxOfflineMsgs) ->
|
2016-04-15 12:44:33 +02:00
|
|
|
Mod = gen_mod:db_mod(Host, ?MODULE),
|
|
|
|
case Mod:store_messages(Host, US, Msgs, Len, MaxOfflineMsgs) of
|
|
|
|
{atomic, discard} ->
|
|
|
|
discard_warn_sender(Msgs);
|
|
|
|
_ ->
|
|
|
|
ok
|
2009-06-15 19:43:18 +02:00
|
|
|
end.
|
|
|
|
|
2012-06-06 12:55:35 +02:00
|
|
|
get_max_user_messages(AccessRule, {User, Server}, Host) ->
|
2009-06-15 19:43:18 +02:00
|
|
|
case acl:match_rule(
|
2015-11-24 16:44:13 +01:00
|
|
|
Host, AccessRule, 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.
|
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
receive_all(US, Msgs, DBType) ->
|
2003-02-13 20:39:13 +01:00
|
|
|
receive
|
2013-03-14 10:33:02 +01:00
|
|
|
#offline_msg{us = US} = Msg ->
|
|
|
|
receive_all(US, [Msg | Msgs], DBType)
|
|
|
|
after 0 ->
|
|
|
|
case DBType of
|
|
|
|
mnesia -> Msgs;
|
2016-04-20 11:27:32 +02:00
|
|
|
sql -> lists:reverse(Msgs);
|
2012-11-13 13:56:27 +01:00
|
|
|
riak -> Msgs
|
2013-03-14 10:33:02 +01:00
|
|
|
end
|
2003-02-13 20:39:13 +01:00
|
|
|
end.
|
|
|
|
|
2014-07-31 12:25:37 +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-02-09 15:59:54 +01:00
|
|
|
get_sm_identity(_Acc, #jid{luser = U, lserver = S}, #jid{luser = U, lserver = S},
|
|
|
|
?NS_FLEX_OFFLINE, _Lang) ->
|
|
|
|
Identity = #xmlel{name = <<"identity">>,
|
|
|
|
attrs = [{<<"category">>, <<"automation">>},
|
|
|
|
{<<"type">>, <<"message-list">>}]},
|
|
|
|
[Identity];
|
|
|
|
get_sm_identity(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc.
|
|
|
|
|
|
|
|
get_sm_items(_Acc, #jid{luser = U, lserver = S, lresource = R} = JID,
|
|
|
|
#jid{luser = U, lserver = S},
|
|
|
|
?NS_FLEX_OFFLINE, _Lang) ->
|
|
|
|
case ejabberd_sm:get_session_pid(U, S, R) of
|
|
|
|
Pid when is_pid(Pid) ->
|
|
|
|
Hdrs = read_message_headers(U, S),
|
|
|
|
BareJID = jid:to_string(jid:remove_resource(JID)),
|
|
|
|
Pid ! dont_ask_offline,
|
|
|
|
{result, lists:map(
|
2016-04-15 12:44:33 +02:00
|
|
|
fun({Node, From, _To, _El}) ->
|
2016-02-09 15:59:54 +01:00
|
|
|
#xmlel{name = <<"item">>,
|
|
|
|
attrs = [{<<"jid">>, BareJID},
|
|
|
|
{<<"node">>, Node},
|
2016-04-15 12:44:33 +02:00
|
|
|
{<<"name">>, jid:to_string(From)}]}
|
2016-02-09 15:59:54 +01:00
|
|
|
end, Hdrs)};
|
|
|
|
none ->
|
|
|
|
{result, []}
|
|
|
|
end;
|
|
|
|
get_sm_items(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc.
|
|
|
|
|
2016-03-03 12:10:06 +01:00
|
|
|
get_info(_Acc, #jid{luser = U, lserver = S, lresource = R},
|
|
|
|
#jid{luser = U, lserver = S}, ?NS_FLEX_OFFLINE, _Lang) ->
|
2016-02-09 15:59:54 +01:00
|
|
|
N = jlib:integer_to_binary(count_offline_messages(U, S)),
|
2016-03-03 12:10:06 +01:00
|
|
|
case ejabberd_sm:get_session_pid(U, S, R) of
|
|
|
|
Pid when is_pid(Pid) ->
|
|
|
|
Pid ! dont_ask_offline;
|
|
|
|
none ->
|
|
|
|
ok
|
|
|
|
end,
|
2016-02-09 15:59:54 +01:00
|
|
|
[#xmlel{name = <<"x">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_XDATA},
|
|
|
|
{<<"type">>, <<"result">>}],
|
|
|
|
children = [#xmlel{name = <<"field">>,
|
|
|
|
attrs = [{<<"var">>, <<"FORM_TYPE">>},
|
|
|
|
{<<"type">>, <<"hidden">>}],
|
|
|
|
children = [#xmlel{name = <<"value">>,
|
|
|
|
children = [{xmlcdata,
|
|
|
|
?NS_FLEX_OFFLINE}]}]},
|
|
|
|
#xmlel{name = <<"field">>,
|
|
|
|
attrs = [{<<"var">>, <<"number_of_messages">>}],
|
|
|
|
children = [#xmlel{name = <<"value">>,
|
|
|
|
children = [{xmlcdata, N}]}]}]}];
|
|
|
|
get_info(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc.
|
|
|
|
|
|
|
|
handle_offline_query(#jid{luser = U, lserver = S} = From,
|
|
|
|
#jid{luser = U, lserver = S} = _To,
|
|
|
|
#iq{type = Type, sub_el = SubEl} = IQ) ->
|
|
|
|
case Type of
|
|
|
|
get ->
|
|
|
|
case fxml:get_subtag(SubEl, <<"fetch">>) of
|
|
|
|
#xmlel{} ->
|
|
|
|
handle_offline_fetch(From);
|
|
|
|
false ->
|
|
|
|
handle_offline_items_view(From, SubEl)
|
|
|
|
end;
|
|
|
|
set ->
|
|
|
|
case fxml:get_subtag(SubEl, <<"purge">>) of
|
|
|
|
#xmlel{} ->
|
|
|
|
delete_all_msgs(U, S);
|
|
|
|
false ->
|
|
|
|
handle_offline_items_remove(From, SubEl)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
IQ#iq{type = result, sub_el = []};
|
2016-03-31 10:00:29 +02:00
|
|
|
handle_offline_query(_From, _To, #iq{sub_el = SubEl, lang = Lang} = IQ) ->
|
|
|
|
Txt = <<"Query to another users is forbidden">>,
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERRT_FORBIDDEN(Lang, Txt)]}.
|
2016-02-09 15:59:54 +01:00
|
|
|
|
|
|
|
handle_offline_items_view(JID, #xmlel{children = Items}) ->
|
|
|
|
{U, S, R} = jid:tolower(JID),
|
|
|
|
lists:foreach(
|
|
|
|
fun(Node) ->
|
|
|
|
case fetch_msg_by_node(JID, Node) of
|
|
|
|
{ok, OfflineMsg} ->
|
|
|
|
case offline_msg_to_route(S, OfflineMsg) of
|
|
|
|
{route, From, To, El} ->
|
|
|
|
NewEl = set_offline_tag(El, Node),
|
|
|
|
case ejabberd_sm:get_session_pid(U, S, R) of
|
|
|
|
Pid when is_pid(Pid) ->
|
|
|
|
Pid ! {route, From, To, NewEl};
|
|
|
|
none ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
error ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
error ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end, get_nodes_from_items(Items, <<"view">>)).
|
|
|
|
|
|
|
|
handle_offline_items_remove(JID, #xmlel{children = Items}) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(Node) ->
|
|
|
|
remove_msg_by_node(JID, Node)
|
|
|
|
end, get_nodes_from_items(Items, <<"remove">>)).
|
|
|
|
|
|
|
|
get_nodes_from_items(Items, Action) ->
|
|
|
|
lists:flatmap(
|
|
|
|
fun(#xmlel{name = <<"item">>, attrs = Attrs}) ->
|
|
|
|
case fxml:get_attr_s(<<"action">>, Attrs) of
|
|
|
|
Action ->
|
|
|
|
case fxml:get_attr_s(<<"node">>, Attrs) of
|
|
|
|
<<"">> ->
|
|
|
|
[];
|
|
|
|
TS ->
|
|
|
|
[TS]
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end;
|
|
|
|
(_) ->
|
|
|
|
[]
|
|
|
|
end, Items).
|
|
|
|
|
|
|
|
set_offline_tag(#xmlel{children = Els} = El, Node) ->
|
|
|
|
OfflineEl = #xmlel{name = <<"offline">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_FLEX_OFFLINE}],
|
|
|
|
children = [#xmlel{name = <<"item">>,
|
|
|
|
attrs = [{<<"node">>, Node}]}]},
|
|
|
|
El#xmlel{children = [OfflineEl|Els]}.
|
|
|
|
|
|
|
|
handle_offline_fetch(#jid{luser = U, lserver = S, lresource = R}) ->
|
|
|
|
case ejabberd_sm:get_session_pid(U, S, R) of
|
|
|
|
none ->
|
|
|
|
ok;
|
|
|
|
Pid when is_pid(Pid) ->
|
|
|
|
Pid ! dont_ask_offline,
|
|
|
|
lists:foreach(
|
2016-04-15 12:44:33 +02:00
|
|
|
fun({Node, From, To, El}) ->
|
|
|
|
NewEl = set_offline_tag(El, Node),
|
|
|
|
Pid ! {route, From, To, NewEl}
|
2016-02-09 15:59:54 +01:00
|
|
|
end, read_message_headers(U, S))
|
|
|
|
end.
|
|
|
|
|
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-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),
|
|
|
|
Mod:remove_message(LUser, LServer, I);
|
|
|
|
_ ->
|
2016-02-09 15:59:54 +01:00
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2014-10-27 12:14:52 +01:00
|
|
|
need_to_store(LServer, Packet) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
Type = fxml:get_tag_attr_s(<<"type">>, Packet),
|
2013-03-14 10:33:02 +01:00
|
|
|
if (Type /= <<"error">>) and (Type /= <<"groupchat">>)
|
2014-10-27 12:14:52 +01:00
|
|
|
and (Type /= <<"headline">>) ->
|
2016-02-09 15:59:54 +01:00
|
|
|
case has_offline_tag(Packet) of
|
|
|
|
false ->
|
|
|
|
case check_store_hint(Packet) of
|
|
|
|
store ->
|
|
|
|
true;
|
|
|
|
no_store ->
|
|
|
|
false;
|
|
|
|
none ->
|
|
|
|
case gen_mod:get_module_opt(
|
|
|
|
LServer, ?MODULE, store_empty_body,
|
|
|
|
fun(V) when is_boolean(V) -> V;
|
|
|
|
(unless_chat_state) -> unless_chat_state
|
|
|
|
end,
|
|
|
|
unless_chat_state) of
|
|
|
|
false ->
|
|
|
|
fxml:get_subtag(Packet, <<"body">>) /= false;
|
|
|
|
unless_chat_state ->
|
2016-06-05 22:36:56 +02:00
|
|
|
not jlib:is_standalone_chat_state(Packet);
|
2016-02-09 15:59:54 +01:00
|
|
|
true ->
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
false
|
2014-10-27 12:14:52 +01:00
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
|
|
|
store_packet(From, To, Packet) ->
|
|
|
|
case need_to_store(To#jid.lserver, Packet) of
|
|
|
|
true ->
|
2015-12-08 21:46:36 +01:00
|
|
|
case check_event(From, To, Packet) of
|
|
|
|
true ->
|
|
|
|
#jid{luser = LUser, lserver = LServer} = To,
|
|
|
|
TimeStamp = p1_time_compat:timestamp(),
|
|
|
|
#xmlel{children = Els} = Packet,
|
|
|
|
Expire = find_x_expire(TimeStamp, Els),
|
|
|
|
gen_mod:get_module_proc(To#jid.lserver, ?PROCNAME) !
|
|
|
|
#offline_msg{us = {LUser, LServer},
|
|
|
|
timestamp = TimeStamp, expire = Expire,
|
|
|
|
from = From, to = To, packet = Packet},
|
|
|
|
stop;
|
2015-10-07 00:06:58 +02:00
|
|
|
_ -> ok
|
|
|
|
end;
|
|
|
|
false -> ok
|
2003-01-05 21:24:59 +01:00
|
|
|
end.
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
has_store_hint(Packet) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
fxml:get_subtag_with_xmlns(Packet, <<"store">>, ?NS_HINTS) =/= false.
|
2015-12-08 21:46:36 +01:00
|
|
|
|
2015-07-28 21:08:33 +02:00
|
|
|
has_no_store_hint(Packet) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
fxml:get_subtag_with_xmlns(Packet, <<"no-store">>, ?NS_HINTS) =/= false
|
2015-07-28 21:08:33 +02:00
|
|
|
orelse
|
2016-02-03 19:03:17 +01:00
|
|
|
fxml:get_subtag_with_xmlns(Packet, <<"no-storage">>, ?NS_HINTS) =/= false.
|
2014-05-12 00:00:34 +02:00
|
|
|
|
2016-02-09 15:59:54 +01:00
|
|
|
has_offline_tag(Packet) ->
|
|
|
|
fxml:get_subtag_with_xmlns(Packet, <<"offline">>, ?NS_FLEX_OFFLINE) =/= false.
|
|
|
|
|
2015-12-08 21:58:54 +01:00
|
|
|
%% Check if the packet has any content about XEP-0022
|
2013-03-14 10:33:02 +01:00
|
|
|
check_event(From, To, Packet) ->
|
|
|
|
#xmlel{name = Name, attrs = Attrs, children = Els} =
|
|
|
|
Packet,
|
|
|
|
case find_x_event(Els) of
|
|
|
|
false -> true;
|
|
|
|
El ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_subtag(El, <<"id">>) of
|
2013-03-14 10:33:02 +01:00
|
|
|
false ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_subtag(El, <<"offline">>) of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> true;
|
|
|
|
_ ->
|
2016-02-03 19:03:17 +01:00
|
|
|
ID = case fxml:get_tag_attr_s(<<"id">>, Packet) of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"">> ->
|
|
|
|
#xmlel{name = <<"id">>, attrs = [],
|
|
|
|
children = []};
|
|
|
|
S ->
|
|
|
|
#xmlel{name = <<"id">>, attrs = [],
|
|
|
|
children = [{xmlcdata, S}]}
|
|
|
|
end,
|
|
|
|
ejabberd_router:route(To, From,
|
|
|
|
#xmlel{name = Name, attrs = Attrs,
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"x">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>,
|
|
|
|
?NS_EVENT}],
|
|
|
|
children =
|
|
|
|
[ID,
|
|
|
|
#xmlel{name
|
|
|
|
=
|
|
|
|
<<"offline">>,
|
|
|
|
attrs
|
|
|
|
=
|
|
|
|
[],
|
|
|
|
children
|
|
|
|
=
|
|
|
|
[]}]}]}),
|
|
|
|
true
|
|
|
|
end;
|
|
|
|
_ -> false
|
|
|
|
end
|
2003-01-05 21:24:59 +01:00
|
|
|
end.
|
|
|
|
|
2015-12-08 21:58:54 +01:00
|
|
|
%% Check if the packet has subelements about XEP-0022
|
2013-03-14 10:33:02 +01:00
|
|
|
find_x_event([]) -> false;
|
|
|
|
find_x_event([{xmlcdata, _} | Els]) ->
|
|
|
|
find_x_event(Els);
|
|
|
|
find_x_event([El | Els]) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_tag_attr_s(<<"xmlns">>, El) of
|
2013-03-14 10:33:02 +01:00
|
|
|
?NS_EVENT -> El;
|
|
|
|
_ -> find_x_event(Els)
|
2003-01-05 21:24:59 +01:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
find_x_expire(_, []) -> never;
|
2004-09-10 22:57:00 +02:00
|
|
|
find_x_expire(TimeStamp, [{xmlcdata, _} | Els]) ->
|
|
|
|
find_x_expire(TimeStamp, Els);
|
|
|
|
find_x_expire(TimeStamp, [El | Els]) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_tag_attr_s(<<"xmlns">>, El) of
|
2013-03-14 10:33:02 +01:00
|
|
|
?NS_EXPIRE ->
|
2016-02-03 19:03:17 +01:00
|
|
|
Val = fxml:get_tag_attr_s(<<"seconds">>, El),
|
2013-03-14 10:33:02 +01:00
|
|
|
case catch jlib:binary_to_integer(Val) of
|
|
|
|
{'EXIT', _} -> never;
|
|
|
|
Int when Int > 0 ->
|
|
|
|
{MegaSecs, Secs, MicroSecs} = TimeStamp,
|
|
|
|
S = MegaSecs * 1000000 + Secs + Int,
|
|
|
|
MegaSecs1 = S div 1000000,
|
|
|
|
Secs1 = S rem 1000000,
|
|
|
|
{MegaSecs1, Secs1, MicroSecs};
|
|
|
|
_ -> never
|
|
|
|
end;
|
|
|
|
_ -> find_x_expire(TimeStamp, Els)
|
2004-09-10 22:57:00 +02:00
|
|
|
end.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
resend_offline_messages(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),
|
|
|
|
case Mod:pop_messages(LUser, LServer) of
|
|
|
|
{ok, Rs} ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:foreach(fun (R) ->
|
2016-04-15 12:44:33 +02:00
|
|
|
ejabberd_sm ! offline_msg_to_route(LServer, R)
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
|
|
|
lists:keysort(#offline_msg.timestamp, Rs));
|
|
|
|
_ -> ok
|
2003-01-05 21:24:59 +01:00
|
|
|
end.
|
2003-01-26 21:16:53 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
pop_offline_messages(Ls, 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),
|
|
|
|
case Mod:pop_messages(LUser, LServer) of
|
|
|
|
{ok, Rs} ->
|
|
|
|
TS = p1_time_compat:timestamp(),
|
|
|
|
Ls ++
|
|
|
|
lists:map(fun (R) ->
|
|
|
|
offline_msg_to_route(LServer, R)
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
2016-04-15 12:44:33 +02:00
|
|
|
lists:filter(
|
|
|
|
fun(#offline_msg{packet = Pkt} = R) ->
|
|
|
|
#xmlel{children = Els} = Pkt,
|
|
|
|
Expire = case R#offline_msg.expire of
|
|
|
|
undefined ->
|
|
|
|
find_x_expire(TS, Els);
|
|
|
|
Exp ->
|
|
|
|
Exp
|
|
|
|
end,
|
|
|
|
case Expire of
|
|
|
|
never -> true;
|
|
|
|
TimeStamp -> TS < TimeStamp
|
|
|
|
end
|
|
|
|
end, Rs));
|
2012-11-06 16:58:08 +01:00
|
|
|
_ ->
|
|
|
|
Ls
|
2004-01-18 21:42:09 +01:00
|
|
|
end.
|
|
|
|
|
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),
|
|
|
|
Mod:remove_expired_messages(LServer).
|
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),
|
|
|
|
Mod:remove_old_messages(Days, LServer).
|
2003-10-24 21:21:07 +02:00
|
|
|
|
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),
|
|
|
|
Mod:remove_user(LUser, LServer).
|
2007-08-13 12:27:28 +02:00
|
|
|
|
|
|
|
%% Helper functions:
|
|
|
|
|
|
|
|
%% Warn senders that their messages have been discarded:
|
|
|
|
discard_warn_sender(Msgs) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:foreach(fun (#offline_msg{from = From, to = To,
|
|
|
|
packet = Packet}) ->
|
|
|
|
ErrText = <<"Your contact offline message queue is "
|
|
|
|
"full. The message has been discarded.">>,
|
2016-02-03 19:03:17 +01:00
|
|
|
Lang = fxml:get_tag_attr_s(<<"xml:lang">>, Packet),
|
2013-03-14 10:33:02 +01:00
|
|
|
Err = jlib:make_error_reply(Packet,
|
|
|
|
?ERRT_RESOURCE_CONSTRAINT(Lang,
|
|
|
|
ErrText)),
|
|
|
|
ejabberd_router:route(To, From, Err)
|
|
|
|
end,
|
|
|
|
Msgs).
|
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-04-15 12:44:33 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Hdrs = Mod:read_message_headers(LUser, LServer),
|
2013-06-24 12:04:56 +02:00
|
|
|
lists:map(
|
2016-04-15 12:44:33 +02:00
|
|
|
fun({_Seq, From, To, Packet}) ->
|
|
|
|
jlib:replace_from_to(From, To, Packet)
|
|
|
|
end, Hdrs).
|
2013-06-24 12:04:56 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
offline_msg_to_route(LServer, #offline_msg{} = R) ->
|
2016-04-15 12:44:33 +02:00
|
|
|
El = case R#offline_msg.timestamp of
|
|
|
|
undefined ->
|
|
|
|
R#offline_msg.packet;
|
|
|
|
TS ->
|
|
|
|
jlib:add_delay_info(R#offline_msg.packet, LServer, TS,
|
|
|
|
<<"Offline Storage">>)
|
|
|
|
end,
|
|
|
|
{route, R#offline_msg.from, R#offline_msg.to, El}.
|
2016-02-09 15:59:54 +01:00
|
|
|
|
|
|
|
read_message_headers(LUser, LServer) ->
|
2016-04-15 12:44:33 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
lists:map(
|
|
|
|
fun({Seq, From, To, El}) ->
|
|
|
|
Node = integer_to_binary(Seq),
|
|
|
|
{Node, From, To, El}
|
|
|
|
end, Mod:read_message_headers(LUser, LServer)).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
2016-04-15 12:44:33 +02:00
|
|
|
format_user_queue(Hdrs) ->
|
|
|
|
lists:map(
|
|
|
|
fun({Seq, From, To, El}) ->
|
|
|
|
ID = integer_to_binary(Seq),
|
|
|
|
FPacket = ejabberd_web_admin:pretty_print_xml(El),
|
|
|
|
SFrom = jid:to_string(From),
|
|
|
|
STo = jid:to_string(To),
|
|
|
|
Stamp = fxml:get_path_s(El, [{elem, <<"delay">>},
|
|
|
|
{attr, <<"stamp">>}]),
|
|
|
|
Time = case jlib:datetime_string_to_timestamp(Stamp) of
|
|
|
|
{_, _, _} = Now ->
|
|
|
|
{{Year, Month, Day}, {Hour, Minute, Second}} =
|
|
|
|
calendar:now_to_local_time(Now),
|
|
|
|
iolist_to_binary(
|
|
|
|
io_lib:format(
|
|
|
|
"~w-~.2.0w-~.2.0w ~.2.0w:~.2.0w:~.2.0w",
|
|
|
|
[Year, Month, Day, Hour, Minute,
|
|
|
|
Second]));
|
|
|
|
_ ->
|
|
|
|
<<"">>
|
|
|
|
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
|
|
|
|
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),
|
|
|
|
Res = user_queue_parse_query(LUser, LServer, Query),
|
|
|
|
HdrsAll = Mod:read_message_headers(LUser, LServer),
|
|
|
|
Hdrs = get_messages_subset(US, Server, HdrsAll),
|
|
|
|
FMsgs = format_user_queue(Hdrs),
|
2013-03-14 10:33:02 +01:00
|
|
|
[?XC(<<"h1">>,
|
|
|
|
list_to_binary(io_lib:format(?T(<<"~s's Offline Messages Queue">>),
|
|
|
|
[us_to_list(US)])))]
|
|
|
|
++
|
|
|
|
case Res of
|
|
|
|
ok -> [?XREST(<<"Submitted">>)];
|
|
|
|
nothing -> []
|
|
|
|
end
|
|
|
|
++
|
|
|
|
[?XAE(<<"form">>,
|
|
|
|
[{<<"action">>, <<"">>}, {<<"method">>, <<"post">>}],
|
|
|
|
[?XE(<<"table">>,
|
|
|
|
[?XE(<<"thead">>,
|
|
|
|
[?XE(<<"tr">>,
|
|
|
|
[?X(<<"td">>), ?XCT(<<"td">>, <<"Time">>),
|
|
|
|
?XCT(<<"td">>, <<"From">>),
|
|
|
|
?XCT(<<"td">>, <<"To">>),
|
|
|
|
?XCT(<<"td">>, <<"Packet">>)])]),
|
|
|
|
?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">>,
|
|
|
|
<<"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, _} ->
|
|
|
|
case lists:keyfind(<<"selected">>, 1, Query) of
|
|
|
|
{_, Seq} ->
|
|
|
|
case catch binary_to_integer(Seq) of
|
|
|
|
I when is_integer(I), I>=0 ->
|
|
|
|
Mod:remove_message(LUser, LServer, I),
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
nothing
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
nothing
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
nothing
|
2007-08-23 02:51:54 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
us_to_list({User, Server}) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:to_string({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) ->
|
2010-01-13 00:58:22 +01:00
|
|
|
Access = gen_mod:get_module_opt(Host, ?MODULE, access_max_user_messages,
|
2013-03-14 10:33:02 +01:00
|
|
|
fun(A) when is_atom(A) -> A end,
|
2010-01-13 00:58:22 +01:00
|
|
|
max_user_offline_messages),
|
2013-03-14 10:33:02 +01:00
|
|
|
MaxOfflineMsgs = case get_max_user_messages(Access,
|
2016-06-08 19:34:05 +02:00
|
|
|
{User, Host}, Host)
|
2013-03-14 10:33:02 +01:00
|
|
|
of
|
|
|
|
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),
|
2015-11-24 16:44:13 +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/">>,
|
|
|
|
(iolist_to_binary(integer_to_list(QueueLen))))],
|
|
|
|
Acc ++
|
|
|
|
[?XCT(<<"h3">>, <<"Offline Messages:">>)] ++
|
|
|
|
FQueueLen ++
|
|
|
|
[?C(<<" ">>),
|
|
|
|
?INPUTT(<<"submit">>, <<"removealloffline">>,
|
|
|
|
<<"Remove All Offline Messages">>)].
|
2008-10-12 16:16:05 +02:00
|
|
|
|
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),
|
|
|
|
Mod:remove_all_messages(LUser, LServer).
|
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
|
2013-03-14 10:33:02 +01:00
|
|
|
{aborted, Reason} ->
|
|
|
|
?ERROR_MSG("Failed to remove offline messages: ~p",
|
|
|
|
[Reason]),
|
|
|
|
{stop, error};
|
|
|
|
{atomic, ok} ->
|
|
|
|
?INFO_MSG("Removed all offline messages for ~s@~s",
|
|
|
|
[User, Server]),
|
|
|
|
{stop, ok}
|
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
|
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),
|
2016-04-15 12:44:33 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:count_messages(LUser, LServer).
|
2012-11-13 13:56:27 +01:00
|
|
|
|
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
|
|
|
|
|
|
|
import(LServer) ->
|
2016-04-15 12:44:33 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:import(LServer).
|
|
|
|
|
|
|
|
import(LServer, DBType, Data) ->
|
|
|
|
Mod = gen_mod:db_mod(DBType, ?MODULE),
|
|
|
|
Mod:import(LServer, Data).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
|
|
|
mod_opt_type(access_max_user_messages) ->
|
2016-06-21 13:18:24 +02:00
|
|
|
fun acl:shaper_rules_validator/1;
|
2016-04-27 16:10:50 +02:00
|
|
|
mod_opt_type(db_type) -> fun(T) -> ejabberd_config:v_db(?MODULE, T) end;
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(store_empty_body) ->
|
2015-11-25 08:58:22 +01:00
|
|
|
fun (V) when is_boolean(V) -> V;
|
|
|
|
(unless_chat_state) -> unless_chat_state
|
|
|
|
end;
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(_) ->
|
|
|
|
[access_max_user_messages, db_type, store_empty_body].
|