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
|
|
|
%%%
|
|
|
|
%%%
|
2015-01-08 17:34:43 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2015 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
|
|
|
|
|
|
|
-protocol({xep, 22, '1.4'}).
|
|
|
|
-protocol({xep, 23, '1.3'}).
|
|
|
|
-protocol({xep, 160, '1.0'}).
|
|
|
|
|
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).
|
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
-export([count_offline_messages/2]).
|
|
|
|
|
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,
|
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,
|
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,
|
2015-10-07 00:06:58 +02:00
|
|
|
get_offline_els/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
|
|
|
|
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).
|
|
|
|
|
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]) ->
|
2015-03-30 11:15:29 +02:00
|
|
|
case gen_mod:db_type(Host, Opts) of
|
2013-03-14 10:33:02 +01:00
|
|
|
mnesia ->
|
|
|
|
mnesia:create_table(offline_msg,
|
|
|
|
[{disc_only_copies, [node()]}, {type, bag},
|
|
|
|
{attributes, record_info(fields, offline_msg)}]),
|
|
|
|
update_table();
|
|
|
|
_ -> ok
|
2012-04-27 11:52:05 +02:00
|
|
|
end,
|
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),
|
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),
|
|
|
|
AccessMaxOfflineMsgs =
|
|
|
|
gen_mod:get_opt(access_max_user_messages, Opts,
|
|
|
|
fun(A) when is_atom(A) -> A end,
|
|
|
|
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),
|
|
|
|
store_offline_msg(Host, UserServer, Msgs, Len, MaxOfflineMsgs, DBType),
|
|
|
|
{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),
|
|
|
|
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),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) -> {ok, State}.
|
2003-02-13 20:39:13 +01:00
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
store_offline_msg(_Host, US, Msgs, Len, MaxOfflineMsgs,
|
|
|
|
mnesia) ->
|
|
|
|
F = fun () ->
|
|
|
|
Count = if MaxOfflineMsgs =/= infinity ->
|
2014-07-17 06:31:54 +02:00
|
|
|
Len + count_mnesia_records(US);
|
2013-03-14 10:33:02 +01:00
|
|
|
true -> 0
|
|
|
|
end,
|
|
|
|
if Count > MaxOfflineMsgs -> discard_warn_sender(Msgs);
|
|
|
|
true ->
|
|
|
|
if Len >= (?OFFLINE_TABLE_LOCK_THRESHOLD) ->
|
|
|
|
mnesia:write_lock_table(offline_msg);
|
|
|
|
true -> ok
|
|
|
|
end,
|
|
|
|
lists:foreach(fun (M) -> mnesia:write(M) end, Msgs)
|
|
|
|
end
|
|
|
|
end,
|
2012-04-27 11:52:05 +02:00
|
|
|
mnesia:transaction(F);
|
2012-11-09 11:43:53 +01:00
|
|
|
store_offline_msg(Host, {User, _Server}, Msgs, Len, MaxOfflineMsgs, odbc) ->
|
2012-04-27 11:52:05 +02:00
|
|
|
Count = if MaxOfflineMsgs =/= infinity ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Len + count_offline_messages(User, Host);
|
|
|
|
true -> 0
|
|
|
|
end,
|
|
|
|
if Count > MaxOfflineMsgs -> discard_warn_sender(Msgs);
|
|
|
|
true ->
|
|
|
|
Query = lists:map(fun (M) ->
|
|
|
|
Username =
|
|
|
|
ejabberd_odbc:escape((M#offline_msg.to)#jid.luser),
|
|
|
|
From = M#offline_msg.from,
|
|
|
|
To = M#offline_msg.to,
|
2014-12-07 15:55:18 +01:00
|
|
|
Packet =
|
|
|
|
jlib:replace_from_to(From, To,
|
|
|
|
M#offline_msg.packet),
|
|
|
|
NewPacket =
|
|
|
|
jlib:add_delay_info(Packet, Host,
|
|
|
|
M#offline_msg.timestamp,
|
|
|
|
<<"Offline Storage">>),
|
2013-03-14 10:33:02 +01:00
|
|
|
XML =
|
2014-12-07 15:55:18 +01:00
|
|
|
ejabberd_odbc:escape(xml:element_to_binary(NewPacket)),
|
2013-03-14 10:33:02 +01:00
|
|
|
odbc_queries:add_spool_sql(Username, XML)
|
|
|
|
end,
|
|
|
|
Msgs),
|
|
|
|
odbc_queries:add_spool(Host, Query)
|
2012-11-06 16:58:08 +01:00
|
|
|
end;
|
|
|
|
store_offline_msg(Host, {User, _}, Msgs, Len, MaxOfflineMsgs,
|
|
|
|
riak) ->
|
|
|
|
Count = if MaxOfflineMsgs =/= infinity ->
|
|
|
|
Len + count_offline_messages(User, Host);
|
|
|
|
true -> 0
|
|
|
|
end,
|
2015-03-13 18:29:57 +01:00
|
|
|
if
|
2012-11-06 16:58:08 +01:00
|
|
|
Count > MaxOfflineMsgs ->
|
|
|
|
discard_warn_sender(Msgs);
|
|
|
|
true ->
|
|
|
|
lists:foreach(
|
2012-11-13 13:56:27 +01:00
|
|
|
fun(#offline_msg{us = US,
|
|
|
|
timestamp = TS} = M) ->
|
2014-07-14 05:49:02 +02:00
|
|
|
ejabberd_riak:put(M, offline_msg_schema(),
|
|
|
|
[{i, TS}, {'2i', [{<<"us">>, US}]}])
|
2012-11-06 16:58:08 +01:00
|
|
|
end, Msgs)
|
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(
|
2013-03-14 10:33:02 +01:00
|
|
|
Host, AccessRule, jlib:make_jid(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;
|
2012-11-06 16:58:08 +01:00
|
|
|
odbc -> 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,
|
|
|
|
{result, Feats ++ [?NS_FEATURE_MSGOFFLINE]};
|
|
|
|
|
|
|
|
get_sm_features(_Acc, _From, _To, ?NS_FEATURE_MSGOFFLINE, _Lang) ->
|
|
|
|
%% override all lesser features...
|
|
|
|
{result, []};
|
|
|
|
|
|
|
|
get_sm_features(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc.
|
|
|
|
|
2014-10-27 12:14:52 +01:00
|
|
|
need_to_store(LServer, Packet) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Type = xml:get_tag_attr_s(<<"type">>, Packet),
|
|
|
|
if (Type /= <<"error">>) and (Type /= <<"groupchat">>)
|
2014-10-27 12:14:52 +01:00
|
|
|
and (Type /= <<"headline">>) ->
|
|
|
|
case gen_mod:get_module_opt(
|
|
|
|
LServer, ?MODULE, store_empty_body,
|
|
|
|
fun(V) when is_boolean(V) -> V end,
|
|
|
|
true) of
|
|
|
|
false ->
|
|
|
|
xml:get_subtag(Packet, <<"body">>) /= false;
|
|
|
|
true ->
|
|
|
|
true
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
|
|
|
store_packet(From, To, Packet) ->
|
|
|
|
case need_to_store(To#jid.lserver, Packet) of
|
|
|
|
true ->
|
2015-10-07 00:06:58 +02:00
|
|
|
case has_no_store_hint(Packet) of
|
|
|
|
false ->
|
|
|
|
case check_event(From, To, Packet) of
|
|
|
|
true ->
|
|
|
|
#jid{luser = LUser, lserver = LServer} = To,
|
|
|
|
TimeStamp = now(),
|
|
|
|
#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;
|
|
|
|
_ -> ok
|
|
|
|
end;
|
|
|
|
_ -> ok
|
|
|
|
end;
|
|
|
|
false -> ok
|
2003-01-05 21:24:59 +01:00
|
|
|
end.
|
|
|
|
|
2015-07-28 21:08:33 +02:00
|
|
|
has_no_store_hint(Packet) ->
|
|
|
|
xml:get_subtag_with_xmlns(Packet, <<"no-store">>, ?NS_HINTS) =/= false
|
|
|
|
orelse
|
|
|
|
xml:get_subtag_with_xmlns(Packet, <<"no-storage">>, ?NS_HINTS) =/= false.
|
2014-05-12 00:00:34 +02:00
|
|
|
|
2009-06-30 21:32:22 +02:00
|
|
|
%% Check if the packet has any content about XEP-0022 or XEP-0085
|
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 ->
|
|
|
|
case xml:get_subtag(El, <<"id">>) of
|
|
|
|
false ->
|
|
|
|
case xml:get_subtag(El, <<"offline">>) of
|
|
|
|
false -> true;
|
|
|
|
_ ->
|
|
|
|
ID = case xml:get_tag_attr_s(<<"id">>, Packet) of
|
|
|
|
<<"">> ->
|
|
|
|
#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.
|
|
|
|
|
2009-06-30 21:32:22 +02:00
|
|
|
%% Check if the packet has subelements about XEP-0022, XEP-0085 or other
|
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]) ->
|
|
|
|
case xml:get_tag_attr_s(<<"xmlns">>, El) of
|
|
|
|
?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]) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case xml:get_tag_attr_s(<<"xmlns">>, El) of
|
|
|
|
?NS_EXPIRE ->
|
|
|
|
Val = xml:get_tag_attr_s(<<"seconds">>, El),
|
|
|
|
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) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
LUser = jlib:nodeprep(User),
|
2005-04-17 20:08:34 +02:00
|
|
|
LServer = jlib:nameprep(Server),
|
|
|
|
US = {LUser, LServer},
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () ->
|
2005-04-17 20:08:34 +02:00
|
|
|
Rs = mnesia:wread({offline_msg, US}),
|
|
|
|
mnesia:delete({offline_msg, US}),
|
2003-01-05 21:24:59 +01:00
|
|
|
Rs
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{atomic, Rs} ->
|
|
|
|
lists:foreach(fun (R) ->
|
|
|
|
ejabberd_sm !
|
|
|
|
{route, R#offline_msg.from, R#offline_msg.to,
|
2014-12-07 15:55:18 +01:00
|
|
|
jlib:add_delay_info(R#offline_msg.packet,
|
|
|
|
LServer,
|
|
|
|
R#offline_msg.timestamp,
|
|
|
|
<<"Offline Storage">>)}
|
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) ->
|
2004-01-18 21:42:09 +01:00
|
|
|
LUser = jlib:nodeprep(User),
|
2005-04-17 20:08:34 +02:00
|
|
|
LServer = jlib:nameprep(Server),
|
2012-04-27 11:52:05 +02:00
|
|
|
pop_offline_messages(Ls, LUser, LServer,
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_mod:db_type(LServer, ?MODULE)).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
|
|
|
pop_offline_messages(Ls, LUser, LServer, mnesia) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
US = {LUser, LServer},
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () ->
|
2005-04-17 20:08:34 +02:00
|
|
|
Rs = mnesia:wread({offline_msg, US}),
|
|
|
|
mnesia:delete({offline_msg, US}),
|
2004-01-18 21:42:09 +01:00
|
|
|
Rs
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{atomic, Rs} ->
|
|
|
|
TS = now(),
|
|
|
|
Ls ++
|
|
|
|
lists:map(fun (R) ->
|
|
|
|
offline_msg_to_route(LServer, R)
|
2004-09-10 22:57:00 +02:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:filter(fun (R) ->
|
|
|
|
case R#offline_msg.expire of
|
|
|
|
never -> true;
|
|
|
|
TimeStamp -> TS < TimeStamp
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
lists:keysort(#offline_msg.timestamp, Rs)));
|
|
|
|
_ -> Ls
|
2012-04-27 11:52:05 +02:00
|
|
|
end;
|
|
|
|
pop_offline_messages(Ls, LUser, LServer, odbc) ->
|
|
|
|
EUser = ejabberd_odbc:escape(LUser),
|
2013-03-14 10:33:02 +01:00
|
|
|
case odbc_queries:get_and_del_spool_msg_t(LServer,
|
|
|
|
EUser)
|
|
|
|
of
|
|
|
|
{atomic, {selected, [<<"username">>, <<"xml">>], Rs}} ->
|
|
|
|
Ls ++
|
|
|
|
lists:flatmap(fun ([_, XML]) ->
|
|
|
|
case xml_stream:parse_element(XML) of
|
|
|
|
{error, _Reason} ->
|
|
|
|
[];
|
|
|
|
El ->
|
|
|
|
case offline_msg_to_route(LServer, El) of
|
|
|
|
error ->
|
|
|
|
[];
|
|
|
|
RouteMsg ->
|
|
|
|
[RouteMsg]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
Rs);
|
|
|
|
_ -> Ls
|
2012-11-06 16:58:08 +01:00
|
|
|
end;
|
|
|
|
pop_offline_messages(Ls, LUser, LServer, riak) ->
|
2014-07-14 05:49:02 +02:00
|
|
|
case ejabberd_riak:get_by_index(offline_msg, offline_msg_schema(),
|
2012-11-13 13:56:27 +01:00
|
|
|
<<"us">>, {LUser, LServer}) of
|
2012-11-06 16:58:08 +01:00
|
|
|
{ok, Rs} ->
|
2012-11-13 13:56:27 +01:00
|
|
|
try
|
|
|
|
lists:foreach(
|
|
|
|
fun(#offline_msg{timestamp = T}) ->
|
|
|
|
ok = ejabberd_riak:delete(offline_msg, T)
|
|
|
|
end, Rs),
|
|
|
|
TS = now(),
|
|
|
|
Ls ++ lists:map(
|
|
|
|
fun (R) ->
|
|
|
|
offline_msg_to_route(LServer, R)
|
|
|
|
end,
|
|
|
|
lists:filter(
|
|
|
|
fun(R) ->
|
|
|
|
case R#offline_msg.expire of
|
|
|
|
never -> true;
|
|
|
|
TimeStamp -> TS < TimeStamp
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
lists:keysort(#offline_msg.timestamp, Rs)))
|
|
|
|
catch _:{badmatch, _} ->
|
|
|
|
Ls
|
|
|
|
end;
|
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) ->
|
|
|
|
LServer = jlib:nameprep(Server),
|
2013-03-14 10:33:02 +01:00
|
|
|
remove_expired_messages(LServer,
|
|
|
|
gen_mod:db_type(LServer, ?MODULE)).
|
2009-06-30 18:51:15 +02:00
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
remove_expired_messages(_LServer, mnesia) ->
|
2004-09-10 22:57:00 +02:00
|
|
|
TimeStamp = now(),
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () ->
|
2004-09-10 22:57:00 +02:00
|
|
|
mnesia:write_lock_table(offline_msg),
|
2013-03-14 10:33:02 +01:00
|
|
|
mnesia:foldl(fun (Rec, _Acc) ->
|
|
|
|
case Rec#offline_msg.expire of
|
|
|
|
never -> ok;
|
|
|
|
TS ->
|
|
|
|
if TS < TimeStamp ->
|
|
|
|
mnesia:delete_object(Rec);
|
|
|
|
true -> ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
ok, offline_msg)
|
2004-09-10 22:57:00 +02:00
|
|
|
end,
|
2012-04-27 11:52:05 +02:00
|
|
|
mnesia:transaction(F);
|
2012-11-06 16:58:08 +01:00
|
|
|
remove_expired_messages(_LServer, odbc) -> {atomic, ok};
|
|
|
|
remove_expired_messages(_LServer, riak) -> {atomic, ok}.
|
2004-09-10 22:57:00 +02:00
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
remove_old_messages(Days, Server) ->
|
|
|
|
LServer = jlib:nameprep(Server),
|
2013-03-14 10:33:02 +01:00
|
|
|
remove_old_messages(Days, LServer,
|
|
|
|
gen_mod:db_type(LServer, ?MODULE)).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
|
|
|
remove_old_messages(Days, _LServer, mnesia) ->
|
2003-10-24 21:21:07 +02:00
|
|
|
{MegaSecs, Secs, _MicroSecs} = now(),
|
|
|
|
S = MegaSecs * 1000000 + Secs - 60 * 60 * 24 * Days,
|
|
|
|
MegaSecs1 = S div 1000000,
|
|
|
|
Secs1 = S rem 1000000,
|
|
|
|
TimeStamp = {MegaSecs1, Secs1, 0},
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () ->
|
2004-08-08 21:07:55 +02:00
|
|
|
mnesia:write_lock_table(offline_msg),
|
2013-03-14 10:33:02 +01:00
|
|
|
mnesia:foldl(fun (#offline_msg{timestamp = TS} = Rec,
|
|
|
|
_Acc)
|
|
|
|
when TS < TimeStamp ->
|
|
|
|
mnesia:delete_object(Rec);
|
|
|
|
(_Rec, _Acc) -> ok
|
|
|
|
end,
|
|
|
|
ok, offline_msg)
|
2003-10-24 21:21:07 +02:00
|
|
|
end,
|
2012-04-27 11:52:05 +02:00
|
|
|
mnesia:transaction(F);
|
2015-03-13 18:29:57 +01:00
|
|
|
|
|
|
|
remove_old_messages(Days, LServer, odbc) ->
|
|
|
|
case catch ejabberd_odbc:sql_query(
|
|
|
|
LServer,
|
|
|
|
[<<"DELETE FROM spool"
|
|
|
|
" WHERE created_at < "
|
|
|
|
"DATE_SUB(CURDATE(), INTERVAL ">>,
|
|
|
|
integer_to_list(Days), <<" DAY);">>]) of
|
|
|
|
{updated, N} ->
|
|
|
|
?INFO_MSG("~p message(s) deleted from offline spool", [N]);
|
|
|
|
_Error ->
|
|
|
|
?ERROR_MSG("Cannot delete message in offline spool: ~p", [_Error])
|
|
|
|
end,
|
2012-11-06 16:58:08 +01:00
|
|
|
{atomic, ok};
|
|
|
|
remove_old_messages(_Days, _LServer, riak) ->
|
2012-04-27 11:52:05 +02:00
|
|
|
{atomic, ok}.
|
2003-10-24 21:21:07 +02:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user(User, Server) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
LUser = jlib:nodeprep(User),
|
2005-04-17 20:08:34 +02:00
|
|
|
LServer = jlib:nameprep(Server),
|
2013-03-14 10:33:02 +01:00
|
|
|
remove_user(LUser, LServer,
|
|
|
|
gen_mod:db_type(LServer, ?MODULE)).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
|
|
|
remove_user(LUser, LServer, mnesia) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
US = {LUser, LServer},
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () -> mnesia:delete({offline_msg, US}) end,
|
2012-04-27 11:52:05 +02:00
|
|
|
mnesia:transaction(F);
|
|
|
|
remove_user(LUser, LServer, odbc) ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
2012-11-06 16:58:08 +01:00
|
|
|
odbc_queries:del_spool_msg(LServer, Username);
|
|
|
|
remove_user(LUser, LServer, riak) ->
|
2012-11-13 13:56:27 +01:00
|
|
|
{atomic, ejabberd_riak:delete_by_index(offline_msg,
|
|
|
|
<<"us">>, {LUser, LServer})}.
|
2004-09-10 22:57:00 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
jid_to_binary(#jid{user = U, server = S, resource = R,
|
|
|
|
luser = LU, lserver = LS, lresource = LR}) ->
|
|
|
|
#jid{user = iolist_to_binary(U),
|
|
|
|
server = iolist_to_binary(S),
|
|
|
|
resource = iolist_to_binary(R),
|
|
|
|
luser = iolist_to_binary(LU),
|
|
|
|
lserver = iolist_to_binary(LS),
|
|
|
|
lresource = iolist_to_binary(LR)}.
|
|
|
|
|
2004-09-10 22:57:00 +02:00
|
|
|
update_table() ->
|
|
|
|
Fields = record_info(fields, offline_msg),
|
|
|
|
case mnesia:table_info(offline_msg, attributes) of
|
2013-03-14 10:33:02 +01:00
|
|
|
Fields ->
|
|
|
|
ejabberd_config:convert_table_to_binary(
|
|
|
|
offline_msg, Fields, bag,
|
|
|
|
fun(#offline_msg{us = {U, _}}) -> U end,
|
|
|
|
fun(#offline_msg{us = {U, S},
|
|
|
|
from = From,
|
|
|
|
to = To,
|
|
|
|
packet = El} = R) ->
|
|
|
|
R#offline_msg{us = {iolist_to_binary(U),
|
|
|
|
iolist_to_binary(S)},
|
|
|
|
from = jid_to_binary(From),
|
|
|
|
to = jid_to_binary(To),
|
|
|
|
packet = xml:to_xmlel(El)}
|
|
|
|
end);
|
|
|
|
_ ->
|
|
|
|
?INFO_MSG("Recreating offline_msg table", []),
|
|
|
|
mnesia:transform_table(offline_msg, ignore, Fields)
|
2004-09-10 22:57:00 +02:00
|
|
|
end.
|
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.">>,
|
|
|
|
Lang = xml:get_tag_attr_s(<<"xml:lang">>, Packet),
|
|
|
|
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) ->
|
|
|
|
get_offline_els(LUser, LServer, gen_mod:db_type(LServer, ?MODULE)).
|
|
|
|
|
2014-07-10 12:16:33 +02:00
|
|
|
get_offline_els(LUser, LServer, DBType)
|
|
|
|
when DBType == mnesia; DBType == riak ->
|
2012-11-13 13:56:27 +01:00
|
|
|
Msgs = read_all_msgs(LUser, LServer, DBType),
|
2013-06-24 12:04:56 +02:00
|
|
|
lists:map(
|
|
|
|
fun(Msg) ->
|
|
|
|
{route, From, To, Packet} = offline_msg_to_route(LServer, Msg),
|
|
|
|
jlib:replace_from_to(From, To, Packet)
|
|
|
|
end, Msgs);
|
|
|
|
get_offline_els(LUser, LServer, odbc) ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
case catch ejabberd_odbc:sql_query(LServer,
|
2014-07-10 12:16:33 +02:00
|
|
|
[<<"select xml from spool where username='">>,
|
|
|
|
Username, <<"' order by seq;">>]) of
|
2013-06-24 12:04:56 +02:00
|
|
|
{selected, [<<"xml">>], Rs} ->
|
|
|
|
lists:flatmap(
|
|
|
|
fun([XML]) ->
|
|
|
|
case xml_stream:parse_element(XML) of
|
|
|
|
#xmlel{} = El ->
|
|
|
|
case offline_msg_to_route(LServer, El) of
|
|
|
|
{route, _, _, NewEl} ->
|
|
|
|
[NewEl];
|
|
|
|
error ->
|
|
|
|
[]
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end, Rs);
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
offline_msg_to_route(LServer, #offline_msg{} = R) ->
|
|
|
|
{route, R#offline_msg.from, R#offline_msg.to,
|
2014-12-07 15:55:18 +01:00
|
|
|
jlib:add_delay_info(R#offline_msg.packet, LServer, R#offline_msg.timestamp,
|
|
|
|
<<"Offline Storage">>)};
|
2013-03-14 10:33:02 +01:00
|
|
|
offline_msg_to_route(_LServer, #xmlel{} = El) ->
|
|
|
|
To = jlib:string_to_jid(xml:get_tag_attr_s(<<"to">>, El)),
|
|
|
|
From = jlib:string_to_jid(xml:get_tag_attr_s(<<"from">>, El)),
|
|
|
|
if (To /= error) and (From /= error) ->
|
|
|
|
{route, From, To, El};
|
|
|
|
true ->
|
|
|
|
error
|
|
|
|
end.
|
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
read_all_msgs(LUser, LServer, mnesia) ->
|
|
|
|
US = {LUser, LServer},
|
|
|
|
lists:keysort(#offline_msg.timestamp,
|
2013-03-14 10:33:02 +01:00
|
|
|
mnesia:dirty_read({offline_msg, US}));
|
2012-11-13 13:56:27 +01:00
|
|
|
read_all_msgs(LUser, LServer, riak) ->
|
|
|
|
case ejabberd_riak:get_by_index(
|
2014-07-14 05:49:02 +02:00
|
|
|
offline_msg, offline_msg_schema(),
|
|
|
|
<<"us">>, {LUser, LServer}) of
|
2012-11-13 13:56:27 +01:00
|
|
|
{ok, Rs} ->
|
|
|
|
lists:keysort(#offline_msg.timestamp, Rs);
|
|
|
|
_Err ->
|
|
|
|
[]
|
|
|
|
end;
|
2012-04-27 11:52:05 +02:00
|
|
|
read_all_msgs(LUser, LServer, odbc) ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
2013-03-14 10:33:02 +01:00
|
|
|
case catch ejabberd_odbc:sql_query(LServer,
|
|
|
|
[<<"select xml from spool where username='">>,
|
|
|
|
Username, <<"' order by seq;">>])
|
|
|
|
of
|
|
|
|
{selected, [<<"xml">>], Rs} ->
|
|
|
|
lists:flatmap(fun ([XML]) ->
|
|
|
|
case xml_stream:parse_element(XML) of
|
|
|
|
{error, _Reason} -> [];
|
|
|
|
El -> [El]
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
Rs);
|
|
|
|
_ -> []
|
2012-04-27 11:52:05 +02:00
|
|
|
end.
|
|
|
|
|
2012-11-13 13:56:27 +01:00
|
|
|
format_user_queue(Msgs, DBType) when DBType == mnesia; DBType == riak ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:map(fun (#offline_msg{timestamp = TimeStamp,
|
|
|
|
from = From, to = To,
|
|
|
|
packet =
|
|
|
|
#xmlel{name = Name, attrs = Attrs,
|
|
|
|
children = Els}} =
|
|
|
|
Msg) ->
|
|
|
|
ID = jlib:encode_base64((term_to_binary(Msg))),
|
|
|
|
{{Year, Month, Day}, {Hour, Minute, Second}} =
|
|
|
|
calendar:now_to_local_time(TimeStamp),
|
|
|
|
Time =
|
|
|
|
iolist_to_binary(io_lib:format("~w-~.2.0w-~.2.0w ~.2.0w:~.2.0w:~.2.0w",
|
|
|
|
[Year, Month, Day,
|
|
|
|
Hour, Minute,
|
|
|
|
Second])),
|
|
|
|
SFrom = jlib:jid_to_string(From),
|
|
|
|
STo = jlib:jid_to_string(To),
|
|
|
|
Attrs2 = jlib:replace_from_to_attrs(SFrom, STo, Attrs),
|
|
|
|
Packet = #xmlel{name = Name, attrs = Attrs2,
|
|
|
|
children = Els},
|
|
|
|
FPacket = ejabberd_web_admin:pretty_print_xml(Packet),
|
|
|
|
?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,
|
|
|
|
Msgs);
|
2012-04-27 11:52:05 +02:00
|
|
|
format_user_queue(Msgs, odbc) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:map(fun (#xmlel{} = Msg) ->
|
|
|
|
ID = jlib:encode_base64((term_to_binary(Msg))),
|
|
|
|
Packet = Msg,
|
|
|
|
FPacket = ejabberd_web_admin:pretty_print_xml(Packet),
|
|
|
|
?XE(<<"tr">>,
|
|
|
|
[?XAE(<<"td">>, [{<<"class">>, <<"valign">>}],
|
|
|
|
[?INPUT(<<"checkbox">>, <<"selected">>, ID)]),
|
|
|
|
?XAE(<<"td">>, [{<<"class">>, <<"valign">>}],
|
|
|
|
[?XC(<<"pre">>, FPacket)])])
|
|
|
|
end,
|
|
|
|
Msgs).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
2007-08-23 02:51:54 +02:00
|
|
|
user_queue(User, Server, Query, Lang) ->
|
2012-04-27 11:52:05 +02:00
|
|
|
LUser = jlib:nodeprep(User),
|
|
|
|
LServer = jlib:nameprep(Server),
|
|
|
|
US = {LUser, LServer},
|
|
|
|
DBType = gen_mod:db_type(LServer, ?MODULE),
|
2013-03-14 10:33:02 +01:00
|
|
|
Res = user_queue_parse_query(LUser, LServer, Query,
|
|
|
|
DBType),
|
2012-04-27 11:52:05 +02:00
|
|
|
MsgsAll = read_all_msgs(LUser, LServer, DBType),
|
2013-03-14 10:33:02 +01:00
|
|
|
Msgs = get_messages_subset(US, Server, MsgsAll,
|
|
|
|
DBType),
|
2012-04-27 11:52:05 +02:00
|
|
|
FMsgs = format_user_queue(Msgs, DBType),
|
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
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
user_queue_parse_query(LUser, LServer, Query, mnesia) ->
|
|
|
|
US = {LUser, LServer},
|
2013-03-14 10:33:02 +01:00
|
|
|
case lists:keysearch(<<"delete">>, 1, Query) of
|
|
|
|
{value, _} ->
|
|
|
|
Msgs = lists:keysort(#offline_msg.timestamp,
|
|
|
|
mnesia:dirty_read({offline_msg, US})),
|
|
|
|
F = fun () ->
|
|
|
|
lists:foreach(fun (Msg) ->
|
|
|
|
ID =
|
|
|
|
jlib:encode_base64((term_to_binary(Msg))),
|
|
|
|
case lists:member({<<"selected">>,
|
|
|
|
ID},
|
|
|
|
Query)
|
|
|
|
of
|
|
|
|
true -> mnesia:delete_object(Msg);
|
|
|
|
false -> ok
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
Msgs)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F),
|
|
|
|
ok;
|
|
|
|
false -> nothing
|
2012-04-27 11:52:05 +02:00
|
|
|
end;
|
2012-11-13 13:56:27 +01:00
|
|
|
user_queue_parse_query(LUser, LServer, Query, riak) ->
|
|
|
|
case lists:keysearch(<<"delete">>, 1, Query) of
|
|
|
|
{value, _} ->
|
|
|
|
Msgs = read_all_msgs(LUser, LServer, riak),
|
|
|
|
lists:foreach(
|
|
|
|
fun (Msg) ->
|
|
|
|
ID = jlib:encode_base64((term_to_binary(Msg))),
|
|
|
|
case lists:member({<<"selected">>, ID}, Query) of
|
|
|
|
true ->
|
|
|
|
ejabberd_riak:delete(offline_msg,
|
|
|
|
Msg#offline_msg.timestamp);
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
Msgs),
|
|
|
|
ok;
|
|
|
|
false ->
|
|
|
|
nothing
|
|
|
|
end;
|
2012-04-27 11:52:05 +02:00
|
|
|
user_queue_parse_query(LUser, LServer, Query, odbc) ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
2013-03-14 10:33:02 +01:00
|
|
|
case lists:keysearch(<<"delete">>, 1, Query) of
|
|
|
|
{value, _} ->
|
|
|
|
Msgs = case catch ejabberd_odbc:sql_query(LServer,
|
|
|
|
[<<"select xml, seq from spool where username='">>,
|
|
|
|
Username,
|
|
|
|
<<"' order by seq;">>])
|
|
|
|
of
|
|
|
|
{selected, [<<"xml">>, <<"seq">>], Rs} ->
|
|
|
|
lists:flatmap(fun ([XML, Seq]) ->
|
|
|
|
case xml_stream:parse_element(XML)
|
|
|
|
of
|
|
|
|
{error, _Reason} -> [];
|
|
|
|
El -> [{El, Seq}]
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
Rs);
|
|
|
|
_ -> []
|
|
|
|
end,
|
|
|
|
F = fun () ->
|
|
|
|
lists:foreach(fun ({Msg, Seq}) ->
|
|
|
|
ID =
|
|
|
|
jlib:encode_base64((term_to_binary(Msg))),
|
|
|
|
case lists:member({<<"selected">>,
|
|
|
|
ID},
|
|
|
|
Query)
|
|
|
|
of
|
|
|
|
true ->
|
|
|
|
SSeq =
|
|
|
|
ejabberd_odbc:escape(Seq),
|
|
|
|
catch
|
|
|
|
ejabberd_odbc:sql_query(LServer,
|
|
|
|
[<<"delete from spool where username='">>,
|
|
|
|
Username,
|
|
|
|
<<"' and seq='">>,
|
|
|
|
SSeq,
|
|
|
|
<<"';">>]);
|
|
|
|
false -> ok
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
Msgs)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F),
|
|
|
|
ok;
|
|
|
|
false -> nothing
|
2007-08-23 02:51:54 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
us_to_list({User, Server}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
jlib: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) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
get_queue_length(LUser, LServer,
|
|
|
|
gen_mod:db_type(LServer, ?MODULE)).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
|
|
|
get_queue_length(LUser, LServer, mnesia) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
length(mnesia:dirty_read({offline_msg,
|
|
|
|
{LUser, LServer}}));
|
2012-11-13 13:56:27 +01:00
|
|
|
get_queue_length(LUser, LServer, riak) ->
|
|
|
|
case ejabberd_riak:count_by_index(offline_msg,
|
|
|
|
<<"us">>, {LUser, LServer}) of
|
|
|
|
{ok, N} ->
|
|
|
|
N;
|
|
|
|
_ ->
|
|
|
|
0
|
|
|
|
end;
|
2012-04-27 11:52:05 +02:00
|
|
|
get_queue_length(LUser, LServer, odbc) ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
2013-03-14 10:33:02 +01:00
|
|
|
case catch ejabberd_odbc:sql_query(LServer,
|
|
|
|
[<<"select count(*) from spool where username='">>,
|
|
|
|
Username, <<"';">>])
|
|
|
|
of
|
|
|
|
{selected, [_], [[SCount]]} ->
|
|
|
|
jlib:binary_to_integer(SCount);
|
|
|
|
_ -> 0
|
2012-04-27 11:52:05 +02:00
|
|
|
end.
|
2010-01-12 13:02:50 +01:00
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
get_messages_subset(User, Host, MsgsAll, DBType) ->
|
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,
|
|
|
|
User, Host)
|
|
|
|
of
|
|
|
|
Number when is_integer(Number) -> Number;
|
|
|
|
_ -> 100
|
2010-01-13 00:58:22 +01:00
|
|
|
end,
|
|
|
|
Length = length(MsgsAll),
|
2013-03-14 10:33:02 +01:00
|
|
|
get_messages_subset2(MaxOfflineMsgs, Length, MsgsAll,
|
|
|
|
DBType).
|
2010-01-13 00:58:22 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
get_messages_subset2(Max, Length, MsgsAll, _DBType)
|
|
|
|
when Length =< Max * 2 ->
|
2010-01-13 00:58:22 +01:00
|
|
|
MsgsAll;
|
2012-11-13 13:56:27 +01:00
|
|
|
get_messages_subset2(Max, Length, MsgsAll, DBType)
|
|
|
|
when DBType == mnesia; DBType == riak ->
|
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),
|
|
|
|
NoJID = jlib:make_jid(<<"...">>, <<"...">>, <<"">>),
|
|
|
|
IntermediateMsg = #offline_msg{timestamp = now(),
|
|
|
|
from = NoJID, to = NoJID,
|
|
|
|
packet =
|
|
|
|
#xmlel{name = <<"...">>, attrs = [],
|
|
|
|
children = []}},
|
2012-04-27 11:52:05 +02:00
|
|
|
MsgsFirstN ++ [IntermediateMsg] ++ MsgsLastN;
|
|
|
|
get_messages_subset2(Max, Length, MsgsAll, odbc) ->
|
|
|
|
FirstN = Max,
|
|
|
|
{MsgsFirstN, Msgs2} = lists:split(FirstN, MsgsAll),
|
2013-03-14 10:33:02 +01:00
|
|
|
MsgsLastN = lists:nthtail(Length - FirstN - FirstN,
|
|
|
|
Msgs2),
|
|
|
|
IntermediateMsg = #xmlel{name = <<"...">>, attrs = [],
|
|
|
|
children = []},
|
2010-01-13 00:58:22 +01:00
|
|
|
MsgsFirstN ++ [IntermediateMsg] ++ MsgsLastN.
|
|
|
|
|
2007-08-23 02:51:54 +02:00
|
|
|
webadmin_user(Acc, User, Server, Lang) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
QueueLen = get_queue_length(jlib:nodeprep(User),
|
|
|
|
jlib:nameprep(Server)),
|
|
|
|
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) ->
|
|
|
|
LUser = jlib:nodeprep(User),
|
|
|
|
LServer = jlib:nameprep(Server),
|
2013-03-14 10:33:02 +01:00
|
|
|
delete_all_msgs(LUser, LServer,
|
|
|
|
gen_mod:db_type(LServer, ?MODULE)).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
|
|
|
delete_all_msgs(LUser, LServer, mnesia) ->
|
|
|
|
US = {LUser, LServer},
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () ->
|
|
|
|
mnesia:write_lock_table(offline_msg),
|
|
|
|
lists:foreach(fun (Msg) -> mnesia:delete_object(Msg)
|
|
|
|
end,
|
|
|
|
mnesia:dirty_read({offline_msg, US}))
|
|
|
|
end,
|
2012-04-27 11:52:05 +02:00
|
|
|
mnesia:transaction(F);
|
2012-11-13 13:56:27 +01:00
|
|
|
delete_all_msgs(LUser, LServer, riak) ->
|
|
|
|
Res = ejabberd_riak:delete_by_index(offline_msg,
|
|
|
|
<<"us">>, {LUser, LServer}),
|
|
|
|
{atomic, Res};
|
2012-04-27 11:52:05 +02:00
|
|
|
delete_all_msgs(LUser, LServer, odbc) ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
odbc_queries:del_spool_msg(LServer, Username),
|
|
|
|
{atomic, ok}.
|
|
|
|
|
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) ->
|
|
|
|
LUser = jlib:nodeprep(User),
|
|
|
|
LServer = jlib:nameprep(Server),
|
|
|
|
DBType = gen_mod:db_type(LServer, ?MODULE),
|
|
|
|
count_offline_messages(LUser, LServer, DBType).
|
|
|
|
|
|
|
|
count_offline_messages(LUser, LServer, mnesia) ->
|
|
|
|
US = {LUser, LServer},
|
|
|
|
F = fun () ->
|
2014-07-17 06:31:54 +02:00
|
|
|
count_mnesia_records(US)
|
2012-11-13 13:56:27 +01:00
|
|
|
end,
|
|
|
|
case catch mnesia:async_dirty(F) of
|
|
|
|
I when is_integer(I) -> I;
|
|
|
|
_ -> 0
|
|
|
|
end;
|
|
|
|
count_offline_messages(LUser, LServer, odbc) ->
|
2012-04-27 11:52:05 +02:00
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
2012-11-13 13:56:27 +01:00
|
|
|
case catch odbc_queries:count_records_where(LServer,
|
|
|
|
<<"spool">>,
|
|
|
|
<<"where username='",
|
|
|
|
Username/binary, "'">>)
|
|
|
|
of
|
|
|
|
{selected, [_], [[Res]]} ->
|
|
|
|
jlib:binary_to_integer(Res);
|
|
|
|
_ -> 0
|
|
|
|
end;
|
|
|
|
count_offline_messages(LUser, LServer, riak) ->
|
|
|
|
case ejabberd_riak:count_by_index(
|
|
|
|
offline_msg, <<"us">>, {LUser, LServer}) of
|
|
|
|
{ok, Res} ->
|
|
|
|
Res;
|
2012-04-27 11:52:05 +02:00
|
|
|
_ ->
|
|
|
|
0
|
2015-03-27 12:28:11 +01:00
|
|
|
end.
|
2013-07-21 12:24:36 +02:00
|
|
|
|
2014-07-17 06:31:54 +02:00
|
|
|
%% Return the number of records matching a given match expression.
|
|
|
|
%% This function is intended to be used inside a Mnesia transaction.
|
|
|
|
%% The count has been written to use the fewest possible memory by
|
|
|
|
%% getting the record by small increment and by using continuation.
|
|
|
|
-define(BATCHSIZE, 100).
|
|
|
|
|
|
|
|
count_mnesia_records(US) ->
|
|
|
|
MatchExpression = #offline_msg{us = US, _ = '_'},
|
|
|
|
case mnesia:select(offline_msg, [{MatchExpression, [], [[]]}],
|
|
|
|
?BATCHSIZE, read) of
|
|
|
|
{Result, Cont} ->
|
|
|
|
Count = length(Result),
|
|
|
|
count_records_cont(Cont, Count);
|
|
|
|
'$end_of_table' ->
|
|
|
|
0
|
|
|
|
end.
|
|
|
|
|
|
|
|
count_records_cont(Cont, Count) ->
|
|
|
|
case mnesia:select(Cont) of
|
|
|
|
{Result, Cont} ->
|
|
|
|
NewCount = Count + length(Result),
|
|
|
|
count_records_cont(Cont, NewCount);
|
|
|
|
'$end_of_table' ->
|
|
|
|
Count
|
|
|
|
end.
|
|
|
|
|
2014-07-14 05:49:02 +02:00
|
|
|
offline_msg_schema() ->
|
|
|
|
{record_info(fields, offline_msg), #offline_msg{}}.
|
|
|
|
|
2013-07-21 12:24:36 +02:00
|
|
|
export(_Server) ->
|
|
|
|
[{offline_msg,
|
|
|
|
fun(Host, #offline_msg{us = {LUser, LServer},
|
|
|
|
timestamp = TimeStamp, from = From, to = To,
|
|
|
|
packet = Packet})
|
|
|
|
when LServer == Host ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
2015-03-11 20:38:22 +01:00
|
|
|
Packet1 = jlib:replace_from_to(From, To, Packet),
|
|
|
|
Packet2 = jlib:add_delay_info(Packet1, LServer, TimeStamp,
|
|
|
|
<<"Offline Storage">>),
|
|
|
|
XML = ejabberd_odbc:escape(xml:element_to_binary(Packet2)),
|
2013-07-21 12:24:36 +02:00
|
|
|
[[<<"delete from spool where username='">>, Username, <<"';">>],
|
|
|
|
[<<"insert into spool(username, xml) values ('">>,
|
|
|
|
Username, <<"', '">>, XML, <<"');">>]];
|
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end}].
|
|
|
|
|
|
|
|
import(LServer) ->
|
|
|
|
[{<<"select username, xml from spool;">>,
|
|
|
|
fun([LUser, XML]) ->
|
|
|
|
El = #xmlel{} = xml_stream:parse_element(XML),
|
|
|
|
From = #jid{} = jlib:string_to_jid(
|
2013-07-22 11:23:40 +02:00
|
|
|
xml:get_attr_s(<<"from">>, El#xmlel.attrs)),
|
2013-07-21 12:24:36 +02:00
|
|
|
To = #jid{} = jlib:string_to_jid(
|
2013-07-22 11:23:40 +02:00
|
|
|
xml:get_attr_s(<<"to">>, El#xmlel.attrs)),
|
2013-07-21 12:24:36 +02:00
|
|
|
Stamp = xml:get_path_s(El, [{elem, <<"delay">>},
|
2013-07-23 07:24:42 +02:00
|
|
|
{attr, <<"stamp">>}]),
|
|
|
|
TS = case jlib:datetime_string_to_timestamp(Stamp) of
|
|
|
|
{_, _, _} = Now ->
|
|
|
|
Now;
|
|
|
|
undefined ->
|
|
|
|
now()
|
|
|
|
end,
|
2013-07-21 12:24:36 +02:00
|
|
|
Expire = find_x_expire(TS, El#xmlel.children),
|
|
|
|
#offline_msg{us = {LUser, LServer},
|
|
|
|
from = From, to = To,
|
|
|
|
timestamp = TS, expire = Expire}
|
|
|
|
end}].
|
|
|
|
|
|
|
|
import(_LServer, mnesia, #offline_msg{} = Msg) ->
|
|
|
|
mnesia:dirty_write(Msg);
|
2013-07-21 14:53:44 +02:00
|
|
|
import(_LServer, riak, #offline_msg{us = US, timestamp = TS} = M) ->
|
2014-07-14 05:49:02 +02:00
|
|
|
ejabberd_riak:put(M, offline_msg_schema(),
|
|
|
|
[{i, TS}, {'2i', [{<<"us">>, US}]}]);
|
2013-07-21 12:24:36 +02:00
|
|
|
import(_, _, _) ->
|
|
|
|
pass.
|
2015-06-01 14:38:27 +02:00
|
|
|
|
|
|
|
mod_opt_type(access_max_user_messages) ->
|
|
|
|
fun (A) -> A end;
|
|
|
|
mod_opt_type(db_type) -> fun gen_mod:v_db/1;
|
|
|
|
mod_opt_type(store_empty_body) ->
|
|
|
|
fun (V) when is_boolean(V) -> V end;
|
|
|
|
mod_opt_type(_) ->
|
|
|
|
[access_max_user_messages, db_type, store_empty_body].
|