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
|
|
|
%%%
|
|
|
|
%%%
|
2012-02-23 16:52:34 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2012 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
|
|
|
%%%
|
2007-12-24 13:58:05 +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., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2003-01-05 21:24:59 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_offline).
|
2007-08-07 18:43:02 +02:00
|
|
|
-author('alexey@process-one.net').
|
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,
|
2012-05-04 06:19:52 +02:00
|
|
|
loop/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,
|
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,
|
|
|
|
webadmin_page/3,
|
2008-10-12 16:16:05 +02:00
|
|
|
webadmin_user/4,
|
2010-09-09 17:00:18 +02:00
|
|
|
webadmin_user_parse_query/5,
|
|
|
|
count_offline_messages/3]).
|
2003-01-05 21:24:59 +01:00
|
|
|
|
2004-09-10 22:57:00 +02:00
|
|
|
-include("ejabberd.hrl").
|
2003-03-09 21:46:47 +01:00
|
|
|
-include("jlib.hrl").
|
2008-07-16 18:58:42 +02:00
|
|
|
-include("web/ejabberd_http.hrl").
|
|
|
|
-include("web/ejabberd_web_admin.hrl").
|
2003-01-05 21:24:59 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
-record(offline_msg, {us, timestamp, expire, from, to, packet}).
|
2003-01-05 21:24:59 +01:00
|
|
|
|
2003-02-13 20:39:13 +01:00
|
|
|
-define(PROCNAME, ejabberd_offline).
|
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).
|
|
|
|
|
2007-08-13 19:28:26 +02:00
|
|
|
start(Host, Opts) ->
|
2012-04-27 11:52:05 +02:00
|
|
|
case gen_mod:db_type(Opts) of
|
|
|
|
mnesia ->
|
|
|
|
mnesia:create_table(offline_msg,
|
|
|
|
[{disc_only_copies, [node()]},
|
|
|
|
{type, bag},
|
|
|
|
{attributes,
|
|
|
|
record_info(fields, offline_msg)}]),
|
|
|
|
update_table();
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:add(offline_message_hook, Host,
|
2004-08-08 21:07:55 +02:00
|
|
|
?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),
|
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,
|
|
|
|
?MODULE, webadmin_user_parse_query, 50),
|
2010-09-09 17:00:18 +02:00
|
|
|
ejabberd_hooks:add(count_offline_messages, Host,
|
|
|
|
?MODULE, count_offline_messages, 50),
|
2009-06-15 19:43:18 +02:00
|
|
|
AccessMaxOfflineMsgs = gen_mod:get_opt(access_max_user_messages, Opts, max_user_offline_messages),
|
2005-06-20 05:18:13 +02:00
|
|
|
register(gen_mod:get_module_proc(Host, ?PROCNAME),
|
2012-05-04 06:19:52 +02:00
|
|
|
spawn(?MODULE, loop, [Host, AccessMaxOfflineMsgs])).
|
2003-02-13 20:39:13 +01:00
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
loop(Host, AccessMaxOfflineMsgs) ->
|
2003-02-13 20:39:13 +01:00
|
|
|
receive
|
2012-04-27 11:52:05 +02:00
|
|
|
#offline_msg{us = User} = Msg ->
|
|
|
|
DBType = gen_mod:db_type(Host, ?MODULE),
|
|
|
|
Msgs = receive_all(User, [Msg], DBType),
|
2004-08-22 23:54:14 +02:00
|
|
|
Len = length(Msgs),
|
2009-06-15 19:43:18 +02:00
|
|
|
MaxOfflineMsgs = get_max_user_messages(AccessMaxOfflineMsgs,
|
|
|
|
User, Host),
|
2012-04-27 11:52:05 +02:00
|
|
|
store_offline_msg(Host, User, Msgs, Len, MaxOfflineMsgs, DBType),
|
|
|
|
loop(Host, AccessMaxOfflineMsgs);
|
|
|
|
_ ->
|
|
|
|
loop(Host, AccessMaxOfflineMsgs)
|
|
|
|
end.
|
|
|
|
|
|
|
|
store_offline_msg(_Host, US, Msgs, Len, MaxOfflineMsgs, mnesia) ->
|
|
|
|
F = fun() ->
|
|
|
|
%% Only count messages if needed:
|
|
|
|
Count = if MaxOfflineMsgs =/= infinity ->
|
|
|
|
Len + p1_mnesia:count_records(
|
|
|
|
offline_msg,
|
|
|
|
#offline_msg{us=US, _='_'});
|
|
|
|
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,
|
|
|
|
mnesia:transaction(F);
|
|
|
|
store_offline_msg(Host, User, Msgs, Len, MaxOfflineMsgs, odbc) ->
|
|
|
|
Count = if MaxOfflineMsgs =/= infinity ->
|
|
|
|
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,
|
|
|
|
{xmlelement, Name, Attrs, Els} =
|
|
|
|
M#offline_msg.packet,
|
|
|
|
Attrs2 = jlib:replace_from_to_attrs(
|
|
|
|
jlib:jid_to_string(From),
|
|
|
|
jlib:jid_to_string(To),
|
|
|
|
Attrs),
|
|
|
|
Packet = {xmlelement, Name, Attrs2,
|
|
|
|
Els ++
|
|
|
|
[jlib:timestamp_to_xml(
|
|
|
|
calendar:now_to_universal_time(
|
|
|
|
M#offline_msg.timestamp),
|
|
|
|
utc,
|
|
|
|
jlib:make_jid("", Host, ""),
|
|
|
|
"Offline Storage"),
|
|
|
|
%% TODO: Delete the next three lines once XEP-0091 is Obsolete
|
|
|
|
jlib:timestamp_to_xml(
|
|
|
|
calendar:now_to_universal_time(
|
|
|
|
M#offline_msg.timestamp))]},
|
|
|
|
XML =
|
|
|
|
ejabberd_odbc:escape(
|
|
|
|
xml:element_to_binary(Packet)),
|
|
|
|
odbc_queries:add_spool_sql(Username, XML)
|
|
|
|
end, Msgs),
|
|
|
|
odbc_queries:add_spool(Host, Query)
|
2003-02-13 20:39:13 +01:00
|
|
|
end.
|
|
|
|
|
2009-06-15 19:43:18 +02:00
|
|
|
%% Function copied from ejabberd_sm.erl:
|
|
|
|
get_max_user_messages(AccessRule, LUser, Host) ->
|
|
|
|
case acl:match_rule(
|
|
|
|
Host, AccessRule, jlib:make_jid(LUser, Host, "")) of
|
|
|
|
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
|
2007-08-07 18:43:02 +02:00
|
|
|
#offline_msg{us=US} = Msg ->
|
2012-04-27 11:52:05 +02:00
|
|
|
receive_all(US, [Msg | Msgs], DBType)
|
2003-02-13 20:39:13 +01:00
|
|
|
after 0 ->
|
2012-04-27 11:52:05 +02:00
|
|
|
%% FIXME: the diff between mnesia and odbc version:
|
|
|
|
%%
|
|
|
|
%% after 0 ->
|
|
|
|
%% - Msgs
|
|
|
|
%% + lists:reverse(Msgs)
|
|
|
|
%% end.
|
|
|
|
%%
|
|
|
|
%% Is it a bug in mnesia version?
|
|
|
|
case DBType of
|
|
|
|
mnesia ->
|
|
|
|
Msgs;
|
|
|
|
odbc ->
|
|
|
|
lists:reverse(Msgs)
|
|
|
|
end
|
2003-02-13 20:39:13 +01:00
|
|
|
end.
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop(Host) ->
|
|
|
|
ejabberd_hooks:delete(offline_message_hook, Host,
|
2004-08-08 21:07:55 +02:00
|
|
|
?MODULE, store_packet, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:delete(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:delete(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:delete(anonymous_purge_hook, Host,
|
|
|
|
?MODULE, remove_user, 50),
|
2007-08-23 02:51:54 +02:00
|
|
|
ejabberd_hooks:delete(webadmin_page_host, Host,
|
|
|
|
?MODULE, webadmin_page, 50),
|
|
|
|
ejabberd_hooks:delete(webadmin_user, Host,
|
|
|
|
?MODULE, webadmin_user, 50),
|
2008-10-12 16:16:05 +02:00
|
|
|
ejabberd_hooks:delete(webadmin_user_parse_query, Host,
|
|
|
|
?MODULE, webadmin_user_parse_query, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
|
|
|
exit(whereis(Proc), stop),
|
|
|
|
{wait, Proc}.
|
2003-01-05 21:24:59 +01:00
|
|
|
|
|
|
|
store_packet(From, To, Packet) ->
|
2003-10-28 21:26:43 +01:00
|
|
|
Type = xml:get_tag_attr_s("type", Packet),
|
2004-08-08 21:07:55 +02:00
|
|
|
if
|
2005-12-07 14:01:33 +01:00
|
|
|
(Type /= "error") and (Type /= "groupchat") and
|
|
|
|
(Type /= "headline") ->
|
2010-09-09 17:00:18 +02:00
|
|
|
case check_event(From, To, Packet) of
|
2004-08-08 21:07:55 +02:00
|
|
|
true ->
|
2005-04-17 20:08:34 +02:00
|
|
|
#jid{luser = LUser, lserver = LServer} = To,
|
2004-08-08 21:07:55 +02:00
|
|
|
TimeStamp = now(),
|
2004-09-10 22:57:00 +02:00
|
|
|
{xmlelement, _Name, _Attrs, Els} = Packet,
|
|
|
|
Expire = find_x_expire(TimeStamp, Els),
|
2005-06-30 13:29:04 +02:00
|
|
|
gen_mod:get_module_proc(To#jid.lserver, ?PROCNAME) !
|
|
|
|
#offline_msg{us = {LUser, LServer},
|
|
|
|
timestamp = TimeStamp,
|
|
|
|
expire = Expire,
|
|
|
|
from = From,
|
|
|
|
to = To,
|
|
|
|
packet = Packet},
|
2004-08-08 21:07:55 +02:00
|
|
|
stop;
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
2003-01-05 21:24:59 +01:00
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2010-09-09 17:00:18 +02:00
|
|
|
check_event(From, To, Packet) ->
|
2003-01-05 21:24:59 +01:00
|
|
|
{xmlelement, Name, Attrs, Els} = Packet,
|
2010-09-09 17:00:18 +02:00
|
|
|
case find_x_event(Els) of
|
|
|
|
false ->
|
2009-06-30 21:32:22 +02:00
|
|
|
true;
|
2010-09-09 17:00:18 +02:00
|
|
|
El ->
|
2003-01-05 21:24:59 +01:00
|
|
|
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
|
|
|
|
"" ->
|
|
|
|
{xmlelement, "id", [], []};
|
|
|
|
S ->
|
|
|
|
{xmlelement, "id", [],
|
|
|
|
[{xmlcdata, S}]}
|
|
|
|
end,
|
|
|
|
ejabberd_router:route(
|
|
|
|
To, From, {xmlelement, Name, Attrs,
|
|
|
|
[{xmlelement, "x",
|
|
|
|
[{"xmlns", ?NS_EVENT}],
|
|
|
|
[ID,
|
|
|
|
{xmlelement, "offline", [], []}]}]
|
|
|
|
}),
|
|
|
|
true
|
2005-09-29 03:04:24 +02:00
|
|
|
end;
|
2003-01-05 21:24:59 +01:00
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2010-09-09 17:00:18 +02:00
|
|
|
find_x_event([]) ->
|
|
|
|
false;
|
|
|
|
find_x_event([{xmlcdata, _} | Els]) ->
|
|
|
|
find_x_event(Els);
|
|
|
|
find_x_event([El | Els]) ->
|
2003-01-05 21:24:59 +01:00
|
|
|
case xml:get_tag_attr_s("xmlns", El) of
|
|
|
|
?NS_EVENT ->
|
2010-09-09 17:00:18 +02:00
|
|
|
El;
|
2003-01-05 21:24:59 +01:00
|
|
|
_ ->
|
2010-09-09 17:00:18 +02:00
|
|
|
find_x_event(Els)
|
2003-01-05 21:24:59 +01:00
|
|
|
end.
|
|
|
|
|
2004-09-10 22:57:00 +02:00
|
|
|
find_x_expire(_, []) ->
|
|
|
|
never;
|
|
|
|
find_x_expire(TimeStamp, [{xmlcdata, _} | Els]) ->
|
|
|
|
find_x_expire(TimeStamp, Els);
|
|
|
|
find_x_expire(TimeStamp, [El | Els]) ->
|
|
|
|
case xml:get_tag_attr_s("xmlns", El) of
|
|
|
|
?NS_EXPIRE ->
|
2007-07-26 12:08:41 +02:00
|
|
|
Val = xml:get_tag_attr_s("seconds", El),
|
|
|
|
case catch list_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};
|
2004-09-10 22:57:00 +02:00
|
|
|
_ ->
|
|
|
|
never
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
find_x_expire(TimeStamp, Els)
|
|
|
|
end.
|
|
|
|
|
2003-01-05 21:24:59 +01:00
|
|
|
|
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},
|
2003-01-05 21:24:59 +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
|
|
|
|
{atomic, Rs} ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(R) ->
|
|
|
|
{xmlelement, Name, Attrs, Els} = R#offline_msg.packet,
|
|
|
|
ejabberd_sm !
|
|
|
|
{route,
|
|
|
|
R#offline_msg.from,
|
|
|
|
R#offline_msg.to,
|
|
|
|
{xmlelement, Name, Attrs,
|
|
|
|
Els ++
|
2003-01-11 21:25:11 +01:00
|
|
|
[jlib:timestamp_to_xml(
|
|
|
|
calendar:now_to_universal_time(
|
|
|
|
R#offline_msg.timestamp))]}}
|
2003-01-05 21:24:59 +01:00
|
|
|
end,
|
|
|
|
lists:keysort(#offline_msg.timestamp, Rs));
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
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,
|
|
|
|
gen_mod:db_type(LServer, ?MODULE)).
|
|
|
|
|
|
|
|
pop_offline_messages(Ls, LUser, LServer, mnesia) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
US = {LUser, LServer},
|
2004-01-18 21:42:09 +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
|
|
|
|
{atomic, Rs} ->
|
2004-09-10 22:57:00 +02:00
|
|
|
TS = now(),
|
|
|
|
Ls ++ lists:map(
|
|
|
|
fun(R) ->
|
|
|
|
{xmlelement, Name, Attrs, Els} = R#offline_msg.packet,
|
|
|
|
{route,
|
|
|
|
R#offline_msg.from,
|
|
|
|
R#offline_msg.to,
|
|
|
|
{xmlelement, Name, Attrs,
|
|
|
|
Els ++
|
|
|
|
[jlib:timestamp_to_xml(
|
2009-06-30 18:51:25 +02:00
|
|
|
calendar:now_to_universal_time(
|
|
|
|
R#offline_msg.timestamp),
|
|
|
|
utc,
|
2012-04-27 11:52:05 +02:00
|
|
|
jlib:make_jid("", LServer, ""),
|
2009-06-30 18:51:25 +02:00
|
|
|
"Offline Storage"),
|
|
|
|
%% TODO: Delete the next three lines once XEP-0091 is Obsolete
|
|
|
|
jlib:timestamp_to_xml(
|
|
|
|
calendar:now_to_universal_time(
|
2004-09-10 22:57:00 +02:00
|
|
|
R#offline_msg.timestamp))]}}
|
|
|
|
end,
|
|
|
|
lists:filter(
|
|
|
|
fun(R) ->
|
|
|
|
case R#offline_msg.expire of
|
|
|
|
never ->
|
|
|
|
true;
|
|
|
|
TimeStamp ->
|
|
|
|
TS < TimeStamp
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
lists:keysort(#offline_msg.timestamp, Rs)));
|
2004-01-18 21:42:09 +01:00
|
|
|
_ ->
|
2004-08-08 21:07:55 +02:00
|
|
|
Ls
|
2012-04-27 11:52:05 +02:00
|
|
|
end;
|
|
|
|
pop_offline_messages(Ls, LUser, LServer, odbc) ->
|
|
|
|
EUser = ejabberd_odbc:escape(LUser),
|
|
|
|
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 ->
|
|
|
|
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 ->
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end, Rs);
|
|
|
|
_ ->
|
|
|
|
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),
|
|
|
|
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(),
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:write_lock_table(offline_msg),
|
|
|
|
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)
|
|
|
|
end,
|
2012-04-27 11:52:05 +02:00
|
|
|
mnesia:transaction(F);
|
|
|
|
remove_expired_messages(_LServer, odbc) ->
|
|
|
|
%% TODO
|
|
|
|
{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),
|
|
|
|
remove_old_messages(Days, LServer, gen_mod:db_type(LServer, ?MODULE)).
|
2004-09-10 22:57:00 +02:00
|
|
|
|
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},
|
|
|
|
F = fun() ->
|
2004-08-08 21:07:55 +02:00
|
|
|
mnesia:write_lock_table(offline_msg),
|
2003-10-24 21:21:07 +02:00
|
|
|
mnesia:foldl(
|
|
|
|
fun(#offline_msg{timestamp = TS} = Rec, _Acc)
|
|
|
|
when TS < TimeStamp ->
|
|
|
|
mnesia:delete_object(Rec);
|
|
|
|
(_Rec, _Acc) -> ok
|
|
|
|
end, ok, offline_msg)
|
|
|
|
end,
|
2012-04-27 11:52:05 +02:00
|
|
|
mnesia:transaction(F);
|
|
|
|
remove_old_messages(_Days, _LServer, odbc) ->
|
|
|
|
%% TODO
|
|
|
|
{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),
|
2012-04-27 11:52:05 +02:00
|
|
|
remove_user(LUser, LServer, gen_mod:db_type(LServer, ?MODULE)).
|
|
|
|
|
|
|
|
remove_user(LUser, LServer, mnesia) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
US = {LUser, LServer},
|
2003-01-26 21:16:53 +01:00
|
|
|
F = fun() ->
|
2005-04-17 20:08:34 +02:00
|
|
|
mnesia:delete({offline_msg, US})
|
2003-01-26 21:16:53 +01:00
|
|
|
end,
|
2012-04-27 11:52:05 +02:00
|
|
|
mnesia:transaction(F);
|
|
|
|
remove_user(LUser, LServer, odbc) ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
odbc_queries:del_spool_msg(LServer, Username).
|
2004-09-10 22:57:00 +02:00
|
|
|
|
|
|
|
update_table() ->
|
|
|
|
Fields = record_info(fields, offline_msg),
|
|
|
|
case mnesia:table_info(offline_msg, attributes) of
|
|
|
|
Fields ->
|
|
|
|
ok;
|
2005-04-17 20:08:34 +02:00
|
|
|
[user, timestamp, expire, from, to, packet] ->
|
|
|
|
?INFO_MSG("Converting offline_msg table from "
|
|
|
|
"{user, timestamp, expire, from, to, packet} format", []),
|
|
|
|
Host = ?MYNAME,
|
|
|
|
{atomic, ok} = mnesia:create_table(
|
|
|
|
mod_offline_tmp_table,
|
|
|
|
[{disc_only_copies, [node()]},
|
|
|
|
{type, bag},
|
|
|
|
{local_content, true},
|
|
|
|
{record_name, offline_msg},
|
|
|
|
{attributes, record_info(fields, offline_msg)}]),
|
|
|
|
mnesia:transform_table(offline_msg, ignore, Fields),
|
|
|
|
F1 = fun() ->
|
|
|
|
mnesia:write_lock_table(mod_offline_tmp_table),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(#offline_msg{us = U} = R, _) ->
|
|
|
|
mnesia:dirty_write(
|
|
|
|
mod_offline_tmp_table,
|
|
|
|
R#offline_msg{us = {U, Host}})
|
|
|
|
end, ok, offline_msg)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F1),
|
|
|
|
mnesia:clear_table(offline_msg),
|
|
|
|
F2 = fun() ->
|
|
|
|
mnesia:write_lock_table(offline_msg),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(R, _) ->
|
|
|
|
mnesia:dirty_write(R)
|
|
|
|
end, ok, mod_offline_tmp_table)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F2),
|
|
|
|
mnesia:delete_table(mod_offline_tmp_table);
|
2004-09-10 22:57:00 +02:00
|
|
|
[user, timestamp, from, to, packet] ->
|
|
|
|
?INFO_MSG("Converting offline_msg table from "
|
|
|
|
"{user, timestamp, from, to, packet} format", []),
|
2005-04-17 20:08:34 +02:00
|
|
|
Host = ?MYNAME,
|
|
|
|
{atomic, ok} = mnesia:create_table(
|
|
|
|
mod_offline_tmp_table,
|
|
|
|
[{disc_only_copies, [node()]},
|
|
|
|
{type, bag},
|
|
|
|
{local_content, true},
|
|
|
|
{record_name, offline_msg},
|
|
|
|
{attributes, record_info(fields, offline_msg)}]),
|
2004-09-10 22:57:00 +02:00
|
|
|
mnesia:transform_table(
|
|
|
|
offline_msg,
|
|
|
|
fun({_, U, TS, F, T, P}) ->
|
|
|
|
{xmlelement, _Name, _Attrs, Els} = P,
|
|
|
|
Expire = find_x_expire(TS, Els),
|
2005-04-17 20:08:34 +02:00
|
|
|
#offline_msg{us = U,
|
2004-09-10 22:57:00 +02:00
|
|
|
timestamp = TS,
|
|
|
|
expire = Expire,
|
|
|
|
from = F,
|
|
|
|
to = T,
|
|
|
|
packet = P}
|
2005-04-17 20:08:34 +02:00
|
|
|
end, Fields),
|
|
|
|
F1 = fun() ->
|
|
|
|
mnesia:write_lock_table(mod_offline_tmp_table),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(#offline_msg{us = U} = R, _) ->
|
|
|
|
mnesia:dirty_write(
|
|
|
|
mod_offline_tmp_table,
|
|
|
|
R#offline_msg{us = {U, Host}})
|
|
|
|
end, ok, offline_msg)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F1),
|
|
|
|
mnesia:clear_table(offline_msg),
|
|
|
|
F2 = fun() ->
|
|
|
|
mnesia:write_lock_table(offline_msg),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(R, _) ->
|
|
|
|
mnesia:dirty_write(R)
|
|
|
|
end, ok, mod_offline_tmp_table)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F2),
|
|
|
|
mnesia:delete_table(mod_offline_tmp_table);
|
2004-09-10 22:57:00 +02:00
|
|
|
_ ->
|
|
|
|
?INFO_MSG("Recreating offline_msg table", []),
|
2005-04-17 20:08:34 +02:00
|
|
|
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) ->
|
|
|
|
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,
|
|
|
|
#request{us = _US,
|
|
|
|
path = ["user", U, "queue"],
|
|
|
|
q = Query,
|
|
|
|
lang = Lang} = _Request) ->
|
|
|
|
Res = user_queue(U, Host, Query, Lang),
|
|
|
|
{stop, Res};
|
|
|
|
|
|
|
|
webadmin_page(Acc, _, _) -> Acc.
|
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
read_all_msgs(LUser, LServer, mnesia) ->
|
|
|
|
US = {LUser, LServer},
|
|
|
|
lists:keysort(#offline_msg.timestamp,
|
|
|
|
mnesia:dirty_read({offline_msg, US}));
|
|
|
|
read_all_msgs(LUser, LServer, odbc) ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
case catch ejabberd_odbc:sql_query(
|
|
|
|
LServer,
|
|
|
|
["select xml from spool"
|
|
|
|
" where username='", Username, "'"
|
|
|
|
" order by seq;"]) of
|
|
|
|
{selected, ["username", "xml"], Rs} ->
|
|
|
|
lists:flatmap(
|
|
|
|
fun({XML}) ->
|
|
|
|
case xml_stream:parse_element(XML) of
|
|
|
|
{error, _Reason} ->
|
|
|
|
[];
|
|
|
|
El ->
|
|
|
|
[El]
|
|
|
|
end
|
|
|
|
end, Rs);
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
|
|
|
format_user_queue(Msgs, mnesia) ->
|
|
|
|
lists:map(
|
|
|
|
fun(#offline_msg{timestamp = TimeStamp, from = From, to = To,
|
|
|
|
packet = {xmlelement, Name, Attrs, Els}} = Msg) ->
|
|
|
|
ID = jlib:encode_base64(binary_to_list(term_to_binary(Msg))),
|
|
|
|
{{Year, Month, Day}, {Hour, Minute, Second}} =
|
|
|
|
calendar:now_to_local_time(TimeStamp),
|
|
|
|
Time = lists:flatten(
|
|
|
|
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 = {xmlelement, Name, Attrs2, 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);
|
|
|
|
format_user_queue(Msgs, odbc) ->
|
|
|
|
lists:map(
|
|
|
|
fun({xmlelement, _Name, _Attrs, _Els} = Msg) ->
|
|
|
|
ID = jlib:encode_base64(binary_to_list(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).
|
|
|
|
|
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),
|
|
|
|
Res = user_queue_parse_query(LUser, LServer, Query, DBType),
|
|
|
|
MsgsAll = read_all_msgs(LUser, LServer, DBType),
|
|
|
|
Msgs = get_messages_subset(User, Server, MsgsAll, DBType),
|
|
|
|
FMsgs = format_user_queue(Msgs, DBType),
|
2007-08-23 02:51:54 +02:00
|
|
|
[?XC("h1", io_lib:format(?T("~s's Offline Messages Queue"),
|
|
|
|
[us_to_list(US)]))] ++
|
|
|
|
case Res of
|
2009-01-12 22:48:34 +01:00
|
|
|
ok -> [?XREST("Submitted")];
|
2007-08-23 02:51:54 +02:00
|
|
|
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
|
|
|
|
)]),
|
|
|
|
?BR,
|
|
|
|
?INPUTT("submit", "delete", "Delete Selected")
|
|
|
|
])].
|
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
user_queue_parse_query(LUser, LServer, Query, mnesia) ->
|
|
|
|
US = {LUser, LServer},
|
2007-08-23 02:51:54 +02: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(
|
|
|
|
binary_to_list(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;
|
|
|
|
user_queue_parse_query(LUser, LServer, Query, odbc) ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
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(
|
|
|
|
binary_to_list(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}) ->
|
|
|
|
jlib:jid_to_string({User, Server, ""}).
|
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
get_queue_length(LUser, LServer) ->
|
|
|
|
get_queue_length(LUser, LServer, gen_mod:db_type(LServer, ?MODULE)).
|
|
|
|
|
|
|
|
get_queue_length(LUser, LServer, mnesia) ->
|
|
|
|
length(mnesia:dirty_read({offline_msg, {LUser, LServer}}));
|
|
|
|
get_queue_length(LUser, LServer, odbc) ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
case catch ejabberd_odbc:sql_query(
|
|
|
|
LServer,
|
|
|
|
["select count(*) from spool"
|
|
|
|
" where username='", Username, "';"]) of
|
|
|
|
{selected, [_], [{SCount}]} ->
|
|
|
|
list_to_integer(SCount);
|
|
|
|
_ ->
|
|
|
|
0
|
|
|
|
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,
|
|
|
|
max_user_offline_messages),
|
|
|
|
MaxOfflineMsgs = case get_max_user_messages(Access, User, Host) of
|
|
|
|
Number when is_integer(Number) -> Number;
|
|
|
|
_ -> 100
|
|
|
|
end,
|
|
|
|
Length = length(MsgsAll),
|
2012-04-27 11:52:05 +02:00
|
|
|
get_messages_subset2(MaxOfflineMsgs, Length, MsgsAll, DBType).
|
2010-01-13 00:58:22 +01:00
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
get_messages_subset2(Max, Length, MsgsAll, _DBType) when Length =< Max*2 ->
|
2010-01-13 00:58:22 +01:00
|
|
|
MsgsAll;
|
2012-04-27 11:52:05 +02:00
|
|
|
get_messages_subset2(Max, Length, MsgsAll, mnesia) ->
|
2010-01-13 00:58:22 +01:00
|
|
|
FirstN = Max,
|
|
|
|
{MsgsFirstN, Msgs2} = lists:split(FirstN, MsgsAll),
|
|
|
|
MsgsLastN = lists:nthtail(Length - FirstN - FirstN, Msgs2),
|
|
|
|
NoJID = jlib:make_jid("...", "...", ""),
|
|
|
|
IntermediateMsg = #offline_msg{timestamp = now(), from = NoJID, to = NoJID,
|
|
|
|
packet = {xmlelement, "...", [], []}},
|
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),
|
|
|
|
MsgsLastN = lists:nthtail(Length - FirstN - FirstN, Msgs2),
|
|
|
|
IntermediateMsg = {xmlelement, "...", [], []},
|
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) ->
|
2010-01-12 13:02:50 +01:00
|
|
|
QueueLen = get_queue_length(jlib:nodeprep(User), jlib:nameprep(Server)),
|
2007-08-23 02:51:54 +02:00
|
|
|
FQueueLen = [?AC("queue/",
|
|
|
|
integer_to_list(QueueLen))],
|
2008-10-12 16:16:05 +02:00
|
|
|
Acc ++ [?XCT("h3", "Offline Messages:")] ++ FQueueLen ++ [?C(" "), ?INPUTT("submit", "removealloffline", "Remove All Offline Messages")].
|
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
delete_all_msgs(User, Server) ->
|
|
|
|
LUser = jlib:nodeprep(User),
|
|
|
|
LServer = jlib:nameprep(Server),
|
|
|
|
delete_all_msgs(LUser, LServer, gen_mod:db_type(LServer, ?MODULE)).
|
|
|
|
|
|
|
|
delete_all_msgs(LUser, LServer, mnesia) ->
|
|
|
|
US = {LUser, LServer},
|
2008-10-12 16:16:05 +02:00
|
|
|
F = fun() ->
|
2012-04-27 11:52:05 +02:00
|
|
|
mnesia:write_lock_table(offline_msg),
|
|
|
|
lists:foreach(
|
|
|
|
fun(Msg) ->
|
|
|
|
mnesia:delete_object(Msg)
|
|
|
|
end, mnesia:dirty_read({offline_msg, US}))
|
2008-10-12 16:16:05 +02:00
|
|
|
end,
|
2012-04-27 11:52:05 +02:00
|
|
|
mnesia:transaction(F);
|
|
|
|
delete_all_msgs(LUser, LServer, odbc) ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
odbc_queries:del_spool_msg(LServer, Username),
|
|
|
|
%% TODO: process the output
|
|
|
|
{atomic, ok}.
|
|
|
|
|
|
|
|
webadmin_user_parse_query(_, "removealloffline", User, Server, _Query) ->
|
|
|
|
case delete_all_msgs(User, Server) of
|
2008-10-12 16:16:05 +02: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}
|
|
|
|
end;
|
|
|
|
webadmin_user_parse_query(Acc, _Action, _User, _Server, _Query) ->
|
|
|
|
Acc.
|
2010-09-09 17:00:18 +02:00
|
|
|
|
2012-05-04 06:19:52 +02:00
|
|
|
count_offline_messages(User, Server) ->
|
2010-09-09 17:00:18 +02:00
|
|
|
LUser = jlib:nodeprep(User),
|
|
|
|
LServer = jlib:nameprep(Server),
|
2012-05-04 06:19:52 +02:00
|
|
|
DBType = gen_mod:db_type(LServer, ?MODULE),
|
|
|
|
count_offline_messages(LUser, LServer, DBType).
|
|
|
|
|
|
|
|
count_offline_messages(LUser, LServer, mnesia) ->
|
2010-09-09 17:00:18 +02:00
|
|
|
US = {LUser, LServer},
|
|
|
|
F = fun () ->
|
|
|
|
p1_mnesia:count_records(
|
|
|
|
offline_msg,
|
|
|
|
#offline_msg{us=US, _='_'})
|
|
|
|
end,
|
2012-05-04 06:19:52 +02:00
|
|
|
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),
|
|
|
|
case catch odbc_queries:count_records_where(
|
|
|
|
LServer, "spool", "where username='" ++ Username ++ "'") of
|
|
|
|
{selected, [_], [{Res}]} ->
|
|
|
|
list_to_integer(Res);
|
|
|
|
_ ->
|
|
|
|
0
|
2012-05-04 06:19:52 +02:00
|
|
|
end;
|
2010-09-09 17:00:18 +02:00
|
|
|
|
2012-05-04 06:19:52 +02:00
|
|
|
count_offline_messages(_Acc, User, Server) ->
|
|
|
|
N = count_offline_messages(User, Server),
|
|
|
|
{stop, N}.
|