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>
|
|
|
|
%%% Purpose : Store and manage offline messages in Mnesia database.
|
|
|
|
%%% Created : 5 Jan 2003 by Alexey Shchepin <alexey@process-one.net>
|
2007-12-24 13:58:05 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2011-02-14 13:47:22 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2011 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-19 15:47:33 +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
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2010-07-22 18:49:12 +02:00
|
|
|
%%% Database schema (version / storage / table)
|
|
|
|
%%%
|
2011-01-18 23:25:17 +01:00
|
|
|
%%% In this module, timestamp is the timestamp expressed in milliseconds.
|
|
|
|
%%% in jlib.erl, the timestamp is a tuple {{Y,M,D},{H,M,S}} or {{Y,M,D},{H,M,S},{Unit,Value}
|
|
|
|
%%% where Unit = milliseconds
|
|
|
|
%%% and Value = integer() in the proper unit
|
|
|
|
%%%
|
2010-07-22 18:49:12 +02:00
|
|
|
%%% 2.1.x / mnesia / offline_msg
|
|
|
|
%%% us = {Username::string(), Host::string()}
|
|
|
|
%%% timestamp = now()
|
|
|
|
%%% expire = never | ???
|
|
|
|
%%% from = jid_old()
|
|
|
|
%%% to = jid_old()
|
|
|
|
%%% packet = xmlelement()
|
|
|
|
%%%
|
|
|
|
%%% 2.1.x / odbc / spool
|
|
|
|
%%% username = varchar250
|
|
|
|
%%% xml = text
|
|
|
|
%%% seq = bigint unsigned
|
|
|
|
%%%
|
|
|
|
%%% 3.0.0-prealpha / mnesia / offline_msg
|
|
|
|
%%% us = {Username::string(), Host::string()}
|
|
|
|
%%% timestamp = now()
|
|
|
|
%%% expire = never | ???
|
|
|
|
%%% from = jid()
|
|
|
|
%%% to = jid()
|
|
|
|
%%% packet = #xmlel
|
|
|
|
%%%
|
|
|
|
%%% 3.0.0-prealpha / odbc / spool
|
|
|
|
%%% Same as 2.1.x
|
|
|
|
%%%
|
|
|
|
%%% 3.0.0-alpha / mnesia / offline_msg
|
2011-04-11 12:54:37 +02:00
|
|
|
%%% user_host = {Username::binary(), Host::binary()}
|
2010-07-22 18:49:12 +02:00
|
|
|
%%% timestamp = integer()
|
|
|
|
%%% expire = 0 | integer()
|
|
|
|
%%% from = jid()
|
|
|
|
%%% to = jid()
|
2011-04-11 15:48:10 +02:00
|
|
|
%%% packet = binary()
|
2010-07-22 18:49:12 +02:00
|
|
|
%%%
|
|
|
|
%%% 3.0.0-alpha / odbc / offline_msg
|
|
|
|
%%% user = varchar150
|
|
|
|
%%% host = varchar150
|
|
|
|
%%% timestamp = bigint
|
|
|
|
%%% expire = bigint
|
|
|
|
%%% from = varchar150
|
|
|
|
%%% to = varchar150
|
|
|
|
%%% packet = varchar150
|
|
|
|
|
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).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
-export([start/2,
|
2009-06-15 19:44:04 +02:00
|
|
|
loop/1,
|
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:54:46 +02:00
|
|
|
get_sm_features/5,
|
2004-09-10 22:57:00 +02:00
|
|
|
remove_expired_messages/0,
|
2003-10-24 21:21:07 +02:00
|
|
|
remove_old_messages/1,
|
2007-08-23 02:51:54 +02:00
|
|
|
remove_user/2,
|
2010-01-12 13:06:43 +01:00
|
|
|
get_queue_length/2,
|
2007-08-23 02:51:54 +02:00
|
|
|
webadmin_page/3,
|
2008-10-13 12:11:19 +02:00
|
|
|
webadmin_user/4,
|
|
|
|
webadmin_user_parse_query/5]).
|
2003-01-05 21:24:59 +01:00
|
|
|
|
2008-08-06 15:44:58 +02:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
|
|
|
|
2004-09-10 22:57:00 +02:00
|
|
|
-include("ejabberd.hrl").
|
2008-07-17 17:33:50 +02:00
|
|
|
-include("web/ejabberd_http.hrl").
|
|
|
|
-include("web/ejabberd_web_admin.hrl").
|
2003-01-05 21:24:59 +01:00
|
|
|
|
2011-04-11 15:48:10 +02:00
|
|
|
%% The packet is stored serialized as a binary
|
2010-07-22 18:49:12 +02:00
|
|
|
-record(offline_msg, {user_host, 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
|
|
|
|
2008-08-06 15:44:58 +02:00
|
|
|
% These are the namespace already declared by the stream opening. This is
|
|
|
|
% used at serialization time.
|
|
|
|
-define(DEFAULT_NS, ?NS_JABBER_CLIENT).
|
|
|
|
-define(PREFIXED_NS, [{?NS_XMPP, ?NS_XMPP_pfx}]).
|
|
|
|
|
2009-06-15 19:44:04 +02:00
|
|
|
%% default value for the maximum number of user messages
|
|
|
|
-define(MAX_USER_MESSAGES, infinity).
|
|
|
|
|
2011-07-11 19:46:26 +02:00
|
|
|
start(Host, Opts) when is_list(Host) ->
|
|
|
|
start(list_to_binary(Host), Opts);
|
|
|
|
start(HostB, Opts) ->
|
|
|
|
% Host = binary_to_list(HostB),
|
2010-07-22 18:44:33 +02:00
|
|
|
Backend = gen_mod:get_opt(backend, Opts, mnesia),
|
|
|
|
gen_storage:create_table(Backend, HostB, offline_msg,
|
|
|
|
[{disc_only_copies, [node()]},
|
2011-07-11 19:46:26 +02:00
|
|
|
{odbc_host, HostB},
|
2010-07-22 18:44:33 +02:00
|
|
|
{type, bag},
|
|
|
|
{attributes, record_info(fields, offline_msg)},
|
2010-07-22 18:49:12 +02:00
|
|
|
{types, [{user_host, {text, text}},
|
2010-07-22 18:44:33 +02:00
|
|
|
{timestamp, bigint},
|
|
|
|
{expire, bigint},
|
2010-07-22 18:49:12 +02:00
|
|
|
{from, jid},
|
|
|
|
{to, jid}]}]),
|
2011-07-11 19:46:26 +02:00
|
|
|
update_table(HostB, Backend),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(offline_message_hook, HostB,
|
2004-08-08 21:07:55 +02:00
|
|
|
?MODULE, store_packet, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(resend_offline_messages_hook, HostB,
|
2004-08-08 21:07:55 +02:00
|
|
|
?MODULE, pop_offline_messages, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(remove_user, HostB,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, remove_user, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(anonymous_purge_hook, HostB,
|
2007-05-12 18:28:34 +02:00
|
|
|
?MODULE, remove_user, 50),
|
2009-06-30 18:54:46 +02:00
|
|
|
ejabberd_hooks:add(disco_sm_features, HostB,
|
|
|
|
?MODULE, get_sm_features, 50),
|
|
|
|
ejabberd_hooks:add(disco_local_features, HostB,
|
|
|
|
?MODULE, get_sm_features, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(webadmin_page_host, HostB,
|
2007-08-23 02:51:54 +02:00
|
|
|
?MODULE, webadmin_page, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(webadmin_user, HostB,
|
2007-08-23 02:51:54 +02:00
|
|
|
?MODULE, webadmin_user, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(webadmin_user_parse_query, HostB,
|
2008-10-13 12:11:19 +02:00
|
|
|
?MODULE, webadmin_user_parse_query, 50),
|
2009-06-15 19:44:04 +02:00
|
|
|
AccessMaxOfflineMsgs = gen_mod:get_opt(access_max_user_messages, Opts, max_user_offline_messages),
|
2011-07-11 19:46:26 +02:00
|
|
|
register(gen_mod:get_module_proc(HostB, ?PROCNAME),
|
2009-06-15 19:44:04 +02:00
|
|
|
spawn(?MODULE, loop, [AccessMaxOfflineMsgs])).
|
2003-02-13 20:39:13 +01:00
|
|
|
|
2011-04-11 15:48:10 +02:00
|
|
|
stanza_to_store(Stanza) ->
|
|
|
|
exmpp_xml:document_to_binary(Stanza).
|
|
|
|
store_to_stanza(StoreBinary) ->
|
|
|
|
[Xml] = exmpp_xml:parse_document(StoreBinary),
|
|
|
|
Xml.
|
|
|
|
|
2009-06-15 19:44:04 +02:00
|
|
|
loop(AccessMaxOfflineMsgs) ->
|
2003-02-13 20:39:13 +01:00
|
|
|
receive
|
2010-07-22 18:49:12 +02:00
|
|
|
#offline_msg{user_host=US} = Msg ->
|
2007-08-07 18:43:02 +02:00
|
|
|
Msgs = receive_all(US, [Msg]),
|
2004-08-22 23:54:14 +02:00
|
|
|
Len = length(Msgs),
|
2010-07-22 18:44:33 +02:00
|
|
|
%% TODO: is lower?
|
2009-06-15 19:44:04 +02:00
|
|
|
{User, Host} = US,
|
|
|
|
MaxOfflineMsgs = get_max_user_messages(AccessMaxOfflineMsgs,
|
|
|
|
User, Host),
|
2003-02-13 20:39:13 +01:00
|
|
|
F = fun() ->
|
2007-08-08 11:48:41 +02:00
|
|
|
%% Only count messages if needed:
|
2010-07-22 18:44:33 +02:00
|
|
|
Count =
|
|
|
|
if MaxOfflineMsgs =/= infinity ->
|
2010-07-22 18:49:12 +02:00
|
|
|
Len + get_queue_length(User, Host);
|
2007-08-08 11:48:41 +02:00
|
|
|
true ->
|
|
|
|
0
|
|
|
|
end,
|
2004-08-22 23:54:14 +02:00
|
|
|
if
|
2007-08-20 20:02:12 +02:00
|
|
|
Count > MaxOfflineMsgs ->
|
2007-08-13 12:27:28 +02:00
|
|
|
discard_warn_sender(Msgs);
|
2004-08-22 23:54:14 +02:00
|
|
|
true ->
|
2007-08-07 18:43:02 +02:00
|
|
|
if
|
|
|
|
Len >= ?OFFLINE_TABLE_LOCK_THRESHOLD ->
|
2010-07-22 18:44:33 +02:00
|
|
|
gen_storage:write_lock_table(Host, offline_msg);
|
2007-08-07 18:43:02 +02:00
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
lists:foreach(fun(M) ->
|
2010-07-22 18:44:33 +02:00
|
|
|
gen_storage:write(Host, M)
|
2011-04-11 15:48:10 +02:00
|
|
|
end, Msgs)
|
2007-08-07 18:43:02 +02:00
|
|
|
end
|
2003-02-13 20:39:13 +01:00
|
|
|
end,
|
2010-07-22 18:44:33 +02:00
|
|
|
gen_storage:transaction(Host, offline_msg, F),
|
2009-06-15 19:44:04 +02:00
|
|
|
loop(AccessMaxOfflineMsgs);
|
2003-02-13 20:39:13 +01:00
|
|
|
_ ->
|
2009-06-15 19:44:04 +02:00
|
|
|
loop(AccessMaxOfflineMsgs)
|
|
|
|
end.
|
|
|
|
|
|
|
|
%% Function copied from ejabberd_sm.erl:
|
|
|
|
get_max_user_messages(AccessRule, LUser, Host) ->
|
|
|
|
case acl:match_rule(
|
2009-06-16 20:28:06 +02:00
|
|
|
Host, AccessRule, exmpp_jid:make(LUser, Host, "")) of
|
2009-06-15 19:44:04 +02:00
|
|
|
Max when is_integer(Max) -> Max;
|
|
|
|
infinity -> infinity;
|
|
|
|
_ -> ?MAX_USER_MESSAGES
|
2003-02-13 20:39:13 +01:00
|
|
|
end.
|
|
|
|
|
2007-08-07 18:43:02 +02:00
|
|
|
receive_all(US, Msgs) ->
|
2003-02-13 20:39:13 +01:00
|
|
|
receive
|
2010-07-22 18:49:12 +02:00
|
|
|
#offline_msg{user_host=US} = Msg ->
|
2007-08-07 18:43:02 +02:00
|
|
|
receive_all(US, [Msg | Msgs])
|
2003-02-13 20:39:13 +01:00
|
|
|
after 0 ->
|
|
|
|
Msgs
|
|
|
|
end.
|
|
|
|
|
2003-01-05 21:24:59 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop(Host) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
HostB = list_to_binary(Host),
|
|
|
|
ejabberd_hooks:delete(offline_message_hook, HostB,
|
2004-08-08 21:07:55 +02:00
|
|
|
?MODULE, store_packet, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(resend_offline_messages_hook, HostB,
|
2004-08-08 21:07:55 +02:00
|
|
|
?MODULE, pop_offline_messages, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(remove_user, HostB,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, remove_user, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(anonymous_purge_hook, HostB,
|
2007-05-12 18:28:34 +02:00
|
|
|
?MODULE, remove_user, 50),
|
2009-06-30 18:54:46 +02:00
|
|
|
ejabberd_hooks:delete(disco_sm_features, HostB, ?MODULE, get_sm_features, 50),
|
|
|
|
ejabberd_hooks:delete(disco_local_features, HostB, ?MODULE, get_sm_features, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(webadmin_page_host, HostB,
|
2007-08-23 02:51:54 +02:00
|
|
|
?MODULE, webadmin_page, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(webadmin_user, HostB,
|
2007-08-23 02:51:54 +02:00
|
|
|
?MODULE, webadmin_user, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(webadmin_user_parse_query, HostB,
|
2008-10-13 12:11:19 +02:00
|
|
|
?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
|
|
|
|
2010-04-29 05:38:25 +02:00
|
|
|
get_sm_features(Acc, _From, _To, <<>>, _Lang) ->
|
2009-06-30 18:54:46 +02:00
|
|
|
Feats = case Acc of
|
|
|
|
{result, I} -> I;
|
|
|
|
_ -> []
|
|
|
|
end,
|
2009-06-30 18:55:26 +02:00
|
|
|
{result, Feats ++ [?NS_MSGOFFLINE]};
|
2009-06-30 18:54:46 +02:00
|
|
|
|
2009-06-30 18:55:26 +02:00
|
|
|
get_sm_features(_Acc, _From, _To, ?NS_MSGOFFLINE, _Lang) ->
|
2009-06-30 18:54:46 +02:00
|
|
|
%% override all lesser features...
|
|
|
|
{result, []};
|
|
|
|
|
2010-05-10 15:10:24 +02:00
|
|
|
get_sm_features(Acc, _From, _To, _Node, _Lang) ->
|
2009-06-30 18:54:46 +02:00
|
|
|
Acc.
|
|
|
|
|
|
|
|
|
2003-01-05 21:24:59 +01:00
|
|
|
store_packet(From, To, Packet) ->
|
2008-08-06 15:44:58 +02:00
|
|
|
Type = exmpp_stanza:get_type(Packet),
|
2004-08-08 21:07:55 +02:00
|
|
|
if
|
2009-01-09 20:18:46 +01:00
|
|
|
(Type /= <<"error">>) and (Type /= <<"groupchat">>) and
|
|
|
|
(Type /= <<"headline">>) ->
|
2009-06-30 21:33:09 +02:00
|
|
|
case check_event_chatstates(From, To, Packet) of
|
2004-08-08 21:07:55 +02:00
|
|
|
true ->
|
2011-04-11 12:54:37 +02:00
|
|
|
LUser = exmpp_jid:prep_node(To),
|
|
|
|
LServer = exmpp_jid:prep_domain(To),
|
2010-07-22 18:44:33 +02:00
|
|
|
TimeStamp = make_timestamp(),
|
2008-08-06 15:44:58 +02:00
|
|
|
Expire = find_x_expire(TimeStamp, Packet#xmlel.children),
|
2011-07-11 19:46:26 +02:00
|
|
|
gen_mod:get_module_proc_existing(LServer, ?PROCNAME) !
|
2010-07-22 18:49:12 +02:00
|
|
|
#offline_msg{user_host = {LUser, LServer},
|
2005-06-30 13:29:04 +02:00
|
|
|
timestamp = TimeStamp,
|
|
|
|
expire = Expire,
|
|
|
|
from = From,
|
|
|
|
to = To,
|
2011-04-11 15:48:10 +02:00
|
|
|
packet = stanza_to_store(Packet)},
|
2004-08-08 21:07:55 +02:00
|
|
|
stop;
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
2003-01-05 21:24:59 +01:00
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2009-06-30 21:33:09 +02:00
|
|
|
%% Check if the packet has any content about XEP-0022 or XEP-0085
|
|
|
|
check_event_chatstates(From, To, Packet) ->
|
|
|
|
case find_x_event_chatstates(Packet#xmlel.children, {false, false, false}) of
|
|
|
|
%% There wasn't any x:event or chatstates subelements
|
|
|
|
{false, false, _} ->
|
2003-01-05 21:24:59 +01:00
|
|
|
true;
|
2009-06-30 21:33:09 +02:00
|
|
|
%% There a chatstates subelement and other stuff, but no x:event
|
|
|
|
{false, CEl, true} when CEl /= false ->
|
|
|
|
true;
|
|
|
|
%% There was only a subelement: a chatstates
|
|
|
|
{false, CEl, false} when CEl /= false ->
|
|
|
|
%% Don't allow offline storage
|
|
|
|
false;
|
|
|
|
%% There was an x:event element, and maybe also other stuff
|
|
|
|
{El, _, _} when El /= false->
|
2008-08-06 15:44:58 +02:00
|
|
|
case exmpp_xml:get_element(El, 'id') of
|
|
|
|
undefined ->
|
|
|
|
case exmpp_xml:get_element(El, 'offline') of
|
|
|
|
undefined ->
|
2003-01-05 21:24:59 +01:00
|
|
|
true;
|
|
|
|
_ ->
|
2008-08-06 15:44:58 +02:00
|
|
|
ID = case exmpp_stanza:get_id(Packet) of
|
|
|
|
undefined ->
|
|
|
|
#xmlel{ns = ?NS_MESSAGE_EVENT, name = 'id'};
|
2003-01-05 21:24:59 +01:00
|
|
|
S ->
|
2008-08-06 15:44:58 +02:00
|
|
|
#xmlel{ns = ?NS_MESSAGE_EVENT, name = 'id',
|
|
|
|
children = [#xmlcdata{cdata =
|
2009-01-09 20:18:46 +01:00
|
|
|
S}]}
|
2003-01-05 21:24:59 +01:00
|
|
|
end,
|
2008-08-06 15:44:58 +02:00
|
|
|
X = #xmlel{ns = ?NS_MESSAGE_EVENT, name = 'x', children =
|
|
|
|
[ID, #xmlel{ns = ?NS_MESSAGE_EVENT, name = 'offline'}]},
|
2003-01-05 21:24:59 +01:00
|
|
|
ejabberd_router:route(
|
2008-08-06 15:44:58 +02:00
|
|
|
To, From, exmpp_xml:set_children(Packet, [X])),
|
2003-01-05 21:24:59 +01:00
|
|
|
true
|
2005-09-29 03:04:24 +02:00
|
|
|
end;
|
2003-01-05 21:24:59 +01:00
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2009-06-30 21:33:09 +02:00
|
|
|
%% Check if the packet has subelements about XEP-0022, XEP-0085 or other
|
|
|
|
find_x_event_chatstates([], Res) ->
|
|
|
|
Res;
|
|
|
|
find_x_event_chatstates([#xmlel{ns = ?NS_MESSAGE_EVENT} = El | Els], {_, B, C}) ->
|
|
|
|
find_x_event_chatstates(Els, {El, B, C});
|
|
|
|
find_x_event_chatstates([#xmlel{ns = ?NS_CHATSTATES} = El | Els], {A, _, C}) ->
|
|
|
|
find_x_event_chatstates(Els, {A, El, C});
|
|
|
|
find_x_event_chatstates([#xmlcdata{} = _ | Els], {A, B, C}) ->
|
|
|
|
find_x_event_chatstates(Els, {A, B, C});
|
|
|
|
find_x_event_chatstates([_ | Els], {A, B, _}) ->
|
|
|
|
find_x_event_chatstates(Els, {A, B, true}).
|
2003-01-05 21:24:59 +01:00
|
|
|
|
2004-09-10 22:57:00 +02:00
|
|
|
find_x_expire(_, []) ->
|
2010-07-22 18:49:12 +02:00
|
|
|
0;
|
2008-08-06 15:44:58 +02:00
|
|
|
find_x_expire(TimeStamp, [#xmlel{ns = ?NS_MESSAGE_EXPIRE} = El | _Els]) ->
|
2010-11-29 20:44:31 +01:00
|
|
|
Val = exmpp_xml:get_attribute_as_list(El, <<"seconds">>, ""),
|
2008-08-06 15:44:58 +02:00
|
|
|
case catch list_to_integer(Val) of
|
|
|
|
{'EXIT', _} ->
|
2010-07-22 18:49:12 +02:00
|
|
|
0;
|
2008-08-06 15:44:58 +02:00
|
|
|
Int when Int > 0 ->
|
2010-08-13 17:01:08 +02:00
|
|
|
TimeStamp + Int;
|
2004-09-10 22:57:00 +02:00
|
|
|
_ ->
|
2010-07-22 18:49:12 +02:00
|
|
|
0
|
2008-08-06 15:44:58 +02:00
|
|
|
end;
|
|
|
|
find_x_expire(TimeStamp, [_ | Els]) ->
|
|
|
|
find_x_expire(TimeStamp, Els).
|
2004-09-10 22:57:00 +02:00
|
|
|
|
2003-01-05 21:24:59 +01:00
|
|
|
|
2011-04-11 12:54:37 +02:00
|
|
|
%% @spec(User::string(), Server::string()) -> ok
|
2005-04-17 20:08:34 +02:00
|
|
|
resend_offline_messages(User, Server) ->
|
2008-09-25 12:46:00 +02:00
|
|
|
try
|
2011-04-11 12:54:37 +02:00
|
|
|
LUser = exmpp_stringprep:nodeprep(list_to_binary(User)),
|
|
|
|
LServer = exmpp_stringprep:nameprep(list_to_binary(Server)),
|
2008-09-25 12:46:00 +02:00
|
|
|
US = {LUser, LServer},
|
|
|
|
F = fun() ->
|
2010-07-22 18:44:33 +02:00
|
|
|
Rs = gen_storage:read(LServer, offline_msg, US, write),
|
|
|
|
gen_storage:delete(LServer, {offline_msg, US}),
|
|
|
|
Rs
|
2008-09-25 12:46:00 +02:00
|
|
|
end,
|
2010-07-22 18:44:33 +02:00
|
|
|
case gen_storage:transaction(LServer, offline_msg, F) of
|
2008-09-25 12:46:00 +02:00
|
|
|
{atomic, Rs} ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(R) ->
|
2011-04-11 15:48:10 +02:00
|
|
|
Packet = store_to_stanza(R#offline_msg.packet),
|
2011-01-18 23:25:17 +01:00
|
|
|
TimeXml = timestamp_to_xml(R#offline_msg.timestamp, Server),
|
|
|
|
TimeXml91 = timestamp_to_xml(R#offline_msg.timestamp),
|
2008-09-25 12:46:00 +02:00
|
|
|
ejabberd_sm !
|
|
|
|
{route,
|
|
|
|
R#offline_msg.from,
|
|
|
|
R#offline_msg.to,
|
2009-06-30 18:55:26 +02:00
|
|
|
exmpp_xml:append_children(
|
|
|
|
Packet,
|
2011-01-18 23:25:17 +01:00
|
|
|
[TimeXml, TimeXml91]
|
2009-06-30 18:55:26 +02:00
|
|
|
)}
|
2008-09-25 12:46:00 +02:00
|
|
|
end,
|
|
|
|
lists:keysort(#offline_msg.timestamp, Rs));
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
catch
|
2003-01-05 21:24:59 +01:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2003-01-26 21:16:53 +01:00
|
|
|
|
2011-04-11 12:54:37 +02:00
|
|
|
%% @spec(Ls::list(), User::binary(), Server::binary()) -> list()
|
2009-01-03 16:15:38 +01:00
|
|
|
pop_offline_messages(Ls, User, Server)
|
|
|
|
when is_binary(User), is_binary(Server) ->
|
2008-09-25 12:46:00 +02:00
|
|
|
try
|
2011-04-11 12:54:37 +02:00
|
|
|
US = {User, Server},
|
2008-09-25 12:46:00 +02:00
|
|
|
F = fun() ->
|
2010-07-22 18:44:33 +02:00
|
|
|
Rs = gen_storage:read(Server, offline_msg, US, write),
|
|
|
|
gen_storage:delete(Server, {offline_msg, US}),
|
|
|
|
Rs
|
2008-09-25 12:46:00 +02:00
|
|
|
end,
|
2010-07-22 18:44:33 +02:00
|
|
|
case gen_storage:transaction(Server, offline_msg, F) of
|
2008-09-25 12:46:00 +02:00
|
|
|
{atomic, Rs} ->
|
2010-07-22 18:44:33 +02:00
|
|
|
TS = make_timestamp(),
|
2008-09-25 12:46:00 +02:00
|
|
|
Ls ++ lists:map(
|
|
|
|
fun(R) ->
|
2011-04-11 15:48:10 +02:00
|
|
|
Packet = store_to_stanza(R#offline_msg.packet),
|
2011-01-18 23:25:17 +01:00
|
|
|
TimeXml = timestamp_to_xml(R#offline_msg.timestamp, Server),
|
|
|
|
TimeXml91 = timestamp_to_xml(R#offline_msg.timestamp),
|
2008-09-25 12:46:00 +02:00
|
|
|
{route,
|
|
|
|
R#offline_msg.from,
|
|
|
|
R#offline_msg.to,
|
2009-06-30 18:55:26 +02:00
|
|
|
exmpp_xml:append_children(
|
|
|
|
Packet,
|
2011-01-18 23:25:17 +01:00
|
|
|
[TimeXml, TimeXml91]
|
2009-06-30 18:55:26 +02:00
|
|
|
)}
|
2008-09-25 12:46:00 +02:00
|
|
|
end,
|
|
|
|
lists:filter(
|
|
|
|
fun(R) ->
|
|
|
|
case R#offline_msg.expire of
|
2010-07-22 18:49:12 +02:00
|
|
|
0 ->
|
2008-09-25 12:46:00 +02:00
|
|
|
true;
|
|
|
|
TimeStamp ->
|
|
|
|
TS < TimeStamp
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
lists:keysort(#offline_msg.timestamp, Rs)));
|
|
|
|
_ ->
|
|
|
|
Ls
|
|
|
|
end
|
|
|
|
catch
|
2004-01-18 21:42:09 +01:00
|
|
|
_ ->
|
2004-08-08 21:07:55 +02:00
|
|
|
Ls
|
2004-01-18 21:42:09 +01:00
|
|
|
end.
|
|
|
|
|
2009-06-30 18:54:46 +02:00
|
|
|
|
2004-09-10 22:57:00 +02:00
|
|
|
remove_expired_messages() ->
|
2010-07-22 18:44:33 +02:00
|
|
|
TimeStamp = make_timestamp(),
|
|
|
|
lists:foreach(
|
2010-08-16 17:16:18 +02:00
|
|
|
fun(HostB) ->
|
2010-07-22 18:44:33 +02:00
|
|
|
F = fun() ->
|
|
|
|
gen_storage:delete_where(
|
2010-08-16 17:16:18 +02:00
|
|
|
HostB, offline_msg,
|
2010-07-22 18:44:33 +02:00
|
|
|
[{'andalso',
|
|
|
|
{'=/=', expire, 0},
|
|
|
|
{'<', expire, TimeStamp}}])
|
|
|
|
end,
|
2010-08-16 17:16:18 +02:00
|
|
|
gen_storage:transaction(HostB, offline_msg, F)
|
2010-07-22 18:44:33 +02:00
|
|
|
end, gen_storage:all_table_hosts(offline_msg)).
|
2004-09-10 22:57:00 +02:00
|
|
|
|
2003-10-24 21:21:07 +02:00
|
|
|
remove_old_messages(Days) ->
|
2011-01-18 23:25:17 +01:00
|
|
|
Timestamp = make_timestamp() - 1000 * 60 * 60 * 24 * Days,
|
2010-07-22 18:44:33 +02:00
|
|
|
lists:foreach(
|
2010-08-16 17:16:18 +02:00
|
|
|
fun(HostB) ->
|
2010-07-22 18:44:33 +02:00
|
|
|
F = fun() ->
|
2010-08-13 17:01:08 +02:00
|
|
|
gen_storage:delete_where(HostB, offline_msg,
|
2010-07-22 18:44:33 +02:00
|
|
|
[{'<', timestamp, Timestamp}])
|
|
|
|
end,
|
2010-08-13 17:01:08 +02:00
|
|
|
{atomic, _} = gen_storage:transaction(HostB, offline_msg, F)
|
2010-07-22 18:44:33 +02:00
|
|
|
end, gen_storage:all_table_hosts(offline_msg)).
|
2003-10-24 21:21:07 +02:00
|
|
|
|
2011-04-11 12:54:37 +02:00
|
|
|
%% @spec(User::binary(), Server::binary()) -> ok
|
2009-01-03 16:15:38 +01:00
|
|
|
remove_user(User, Server) when is_binary(User), is_binary(Server) ->
|
2008-09-25 12:46:00 +02:00
|
|
|
try
|
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
|
|
|
US = {LUser, LServer},
|
|
|
|
F = fun() ->
|
2010-07-22 18:44:33 +02:00
|
|
|
gen_storage:delete(LServer, {offline_msg, US})
|
2008-09-25 12:46:00 +02:00
|
|
|
end,
|
2010-07-22 18:44:33 +02:00
|
|
|
gen_storage:transaction(LServer, offline_msg, F)
|
2008-09-25 12:46:00 +02:00
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2004-09-10 22:57:00 +02:00
|
|
|
|
2010-07-22 18:49:12 +02:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2007-08-13 12:27:28 +02:00
|
|
|
|
2011-07-11 19:46:26 +02:00
|
|
|
update_table(global, Storage) ->
|
|
|
|
[update_table(HostB, Storage) || HostB <- ejabberd_hosts:get_hosts(ejabberd)];
|
|
|
|
|
2010-07-22 18:49:12 +02:00
|
|
|
update_table(Host, mnesia) ->
|
|
|
|
gen_storage_migration:migrate_mnesia(
|
|
|
|
Host, offline_msg,
|
|
|
|
[{offline_msg, [us, timestamp, expire, from, to, packet],
|
|
|
|
%% The field name 'us' changes to 'user_host',
|
|
|
|
%% but its position in the erlang record is the same,
|
|
|
|
%% so we can refer to it using the new field name 'user_host'.
|
|
|
|
fun(#offline_msg{user_host = {US_U, US_S},
|
2011-01-18 23:25:17 +01:00
|
|
|
timestamp = {TsMegaSecs, TsSecs, TsMicroSecs},
|
2010-07-22 18:49:12 +02:00
|
|
|
expire = Expire,
|
|
|
|
from = From,
|
|
|
|
to = To,
|
|
|
|
packet = Packet} = OM) ->
|
|
|
|
%% Convert "" to undefined in JIDs.
|
|
|
|
US_U1 = convert_jid_to_exmpp(US_U),
|
|
|
|
US_S1 = convert_jid_to_exmpp(US_S),
|
|
|
|
From1 = jlib:from_old_jid(From),
|
|
|
|
To1 = jlib:from_old_jid(To),
|
|
|
|
Expire1 = case Expire of
|
|
|
|
never ->
|
|
|
|
0;
|
2010-08-13 17:01:08 +02:00
|
|
|
Ts ->
|
|
|
|
Ts
|
2010-07-22 18:49:12 +02:00
|
|
|
end,
|
|
|
|
PacketXmlel = exmpp_xml:xmlelement_to_xmlel(
|
|
|
|
Packet, [?DEFAULT_NS], ?PREFIXED_NS),
|
2011-04-11 15:48:10 +02:00
|
|
|
Packet1 = stanza_to_store(PacketXmlel),
|
2010-07-22 18:49:12 +02:00
|
|
|
OM#offline_msg{user_host = {US_U1, US_S1},
|
2011-01-18 23:25:17 +01:00
|
|
|
timestamp = TsMegaSecs * 1000000000 + TsSecs * 1000 + TsMicroSecs div 1000,
|
2010-07-22 18:49:12 +02:00
|
|
|
expire = Expire1,
|
|
|
|
from = From1,
|
|
|
|
to = To1,
|
|
|
|
packet = Packet1}
|
|
|
|
end}]);
|
|
|
|
|
|
|
|
update_table(Host, odbc) ->
|
|
|
|
gen_storage_migration:migrate_odbc(
|
|
|
|
Host, [offline_msg],
|
|
|
|
[{"spool", ["username", "xml", "seq"],
|
|
|
|
fun(_, Username, XmlS, _Seq) ->
|
|
|
|
[Xmlel] = Els = exmpp_xml:parse_document(XmlS, [names_as_atom]),
|
|
|
|
From = jlib:short_prepd_jid(
|
|
|
|
exmpp_jid:parse(
|
|
|
|
exmpp_stanza:get_sender(Xmlel))),
|
|
|
|
Expire = find_x_expire(0, Els),
|
|
|
|
Timestamp = find_x_timestamp(Els),
|
|
|
|
[#offline_msg{user_host = {Username, Host},
|
|
|
|
timestamp = Timestamp,
|
|
|
|
expire = Expire,
|
|
|
|
from = From,
|
|
|
|
to = {Username, Host, ""},
|
|
|
|
packet = XmlS}]
|
|
|
|
end}]).
|
2008-09-25 12:46:00 +02:00
|
|
|
|
|
|
|
convert_jid_to_exmpp("") -> undefined;
|
2011-04-11 12:54:37 +02:00
|
|
|
convert_jid_to_exmpp(V) -> list_to_binary(V).
|
2007-08-13 12:27:28 +02:00
|
|
|
|
2011-01-18 23:25:17 +01:00
|
|
|
%% Return the current timestamp in milliseconds
|
2010-07-22 18:44:33 +02:00
|
|
|
make_timestamp() ->
|
2011-01-18 23:25:17 +01:00
|
|
|
{MegaSecs, Secs, MicroSecs} = now(),
|
|
|
|
MegaSecs * 1000000000 + Secs * 1000 + MicroSecs div 1000.
|
2010-07-22 18:44:33 +02:00
|
|
|
|
|
|
|
timestamp_to_now(Timestamp) ->
|
2011-01-18 23:25:17 +01:00
|
|
|
MegaSecs = Timestamp div 1000000000,
|
|
|
|
Secs = (Timestamp rem 1000000000) div 1000,
|
|
|
|
MicroSecs = (Timestamp rem 1000) * 1000,
|
|
|
|
{MegaSecs, Secs, MicroSecs}.
|
|
|
|
|
|
|
|
timestamp_to_isostring(Timestamp) ->
|
|
|
|
{Date, Time} = calendar:now_to_universal_time(timestamp_to_now(Timestamp)),
|
|
|
|
Milliseconds = (Timestamp rem 1000),
|
|
|
|
UniversalTimeSubsecond = {Date, Time, {milliseconds, Milliseconds}},
|
|
|
|
{TimeStr, ZoneStr} = jlib:timestamp_to_iso(UniversalTimeSubsecond, utc),
|
|
|
|
TimeStr ++ ZoneStr.
|
|
|
|
|
|
|
|
timestamp_to_xml(Timestamp, Server) ->
|
|
|
|
{Date, Time} = calendar:now_to_universal_time(timestamp_to_now(Timestamp)),
|
|
|
|
Milliseconds = (Timestamp rem 1000),
|
|
|
|
UniversalTimeSubsecond = {Date, Time, {milliseconds, Milliseconds}},
|
|
|
|
jlib:timestamp_to_xml(
|
|
|
|
UniversalTimeSubsecond,
|
|
|
|
utc,
|
|
|
|
exmpp_jid:make("", Server, ""),
|
|
|
|
"Offline Storage").
|
|
|
|
|
|
|
|
%% TODO: Delete once XEP-0091 is Obsolete
|
|
|
|
timestamp_to_xml(Timestamp) ->
|
|
|
|
jlib:timestamp_to_xml(
|
|
|
|
calendar:now_to_universal_time(
|
|
|
|
timestamp_to_now(Timestamp))).
|
2007-08-13 12:27:28 +02:00
|
|
|
|
2010-07-22 18:49:12 +02:00
|
|
|
find_x_timestamp([]) ->
|
|
|
|
make_timestamp();
|
|
|
|
find_x_timestamp([{xmlcdata, _} | Els]) ->
|
|
|
|
find_x_timestamp(Els);
|
|
|
|
|
|
|
|
find_x_timestamp([#xmlel{ns = ?NS_DELAY} = El | Els]) ->
|
2010-11-29 20:44:31 +01:00
|
|
|
Stamp = exmpp_xml:get_attribute_as_list(El, <<"stamp">>, ""),
|
2010-07-22 18:49:12 +02:00
|
|
|
case jlib:datetime_string_to_timestamp(Stamp) of
|
|
|
|
undefined -> find_x_timestamp(Els);
|
2011-01-18 23:25:17 +01:00
|
|
|
{MegaSecs, Secs, _MicroSecs} -> MegaSecs * 1000000000 + Secs * 1000
|
2010-07-22 18:49:12 +02:00
|
|
|
end;
|
|
|
|
find_x_timestamp([_ | Els]) ->
|
|
|
|
find_x_timestamp(Els).
|
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2007-08-13 12:27:28 +02:00
|
|
|
%% Warn senders that their messages have been discarded:
|
|
|
|
discard_warn_sender(Msgs) ->
|
|
|
|
lists:foreach(
|
2011-04-11 15:48:10 +02:00
|
|
|
fun(#offline_msg{from=From, to=To, packet=PacketStored}) ->
|
|
|
|
Packet = store_to_stanza(PacketStored),
|
2007-08-13 12:27:28 +02:00
|
|
|
ErrText = "Your contact offline message queue is full. The message has been discarded.",
|
2010-06-18 16:41:47 +02:00
|
|
|
Error = exmpp_stanza:error(Packet#xmlel.ns, 'resource-constraint',
|
2008-08-06 15:44:58 +02:00
|
|
|
{"en", ErrText}),
|
|
|
|
Err = exmpp_stanza:reply_with_error(Packet, Error),
|
2007-08-13 12:27:28 +02:00
|
|
|
ejabberd_router:route(
|
2008-09-25 12:46:00 +02:00
|
|
|
To,
|
|
|
|
From, Err)
|
2007-08-13 12:27:28 +02:00
|
|
|
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.
|
|
|
|
|
|
|
|
user_queue(User, Server, Query, Lang) ->
|
2011-05-18 19:46:35 +02:00
|
|
|
ServerB = list_to_binary(Server),
|
2008-09-25 12:46:00 +02:00
|
|
|
US0 = {
|
2011-04-11 12:54:37 +02:00
|
|
|
exmpp_stringprep:nodeprep(list_to_binary(User)),
|
|
|
|
exmpp_stringprep:nameprep(list_to_binary(Server))
|
2008-09-25 12:46:00 +02:00
|
|
|
},
|
2011-04-11 13:14:30 +02:00
|
|
|
{US, Res, MsgsAll} = try
|
2008-09-25 12:46:00 +02:00
|
|
|
{
|
|
|
|
US0,
|
2011-04-11 13:14:30 +02:00
|
|
|
user_queue_parse_query(US0, Query),
|
|
|
|
lists:keysort(#offline_msg.timestamp,
|
2011-05-18 19:46:35 +02:00
|
|
|
gen_storage:dirty_read(ServerB, {offline_msg, US0}))
|
2008-09-25 12:46:00 +02:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
_ ->
|
2011-05-18 19:46:35 +02:00
|
|
|
{{"invalid", "invalid"}, [], []}
|
2008-09-25 12:46:00 +02:00
|
|
|
end,
|
2010-01-13 01:00:34 +01:00
|
|
|
Msgs = get_messages_subset(User, Server, MsgsAll),
|
2007-08-23 02:51:54 +02:00
|
|
|
FMsgs =
|
|
|
|
lists:map(
|
|
|
|
fun(#offline_msg{timestamp = TimeStamp, from = From, to = To,
|
2011-04-11 15:48:10 +02:00
|
|
|
packet = PacketStored} = Msg) ->
|
2007-08-23 02:51:54 +02:00
|
|
|
ID = jlib:encode_base64(binary_to_list(term_to_binary(Msg))),
|
2011-01-18 23:25:17 +01:00
|
|
|
Time = timestamp_to_isostring(TimeStamp),
|
2009-06-01 18:52:14 +02:00
|
|
|
SFrom = exmpp_jid:to_list(From),
|
|
|
|
STo = exmpp_jid:to_list(To),
|
2011-04-11 15:48:10 +02:00
|
|
|
Packet = store_to_stanza(PacketStored),
|
2008-09-25 12:46:00 +02:00
|
|
|
Packet1 = exmpp_stanza:set_jids(Packet, SFrom, STo),
|
2008-08-06 15:44:58 +02:00
|
|
|
FPacket = exmpp_xml:node_to_list(
|
|
|
|
exmpp_xml:indent_document(Packet1, <<" ">>),
|
|
|
|
[?DEFAULT_NS], ?PREFIXED_NS),
|
2007-08-23 02:51:54 +02:00
|
|
|
?XE("tr",
|
2010-11-29 20:44:31 +01:00
|
|
|
[?XAE("td", [?XMLATTR(<<"class">>, <<"valign">>)], [?INPUT("checkbox", "selected", ID)]),
|
|
|
|
?XAC("td", [?XMLATTR(<<"class">>, <<"valign">>)], Time),
|
|
|
|
?XAC("td", [?XMLATTR(<<"class">>, <<"valign">>)], SFrom),
|
|
|
|
?XAC("td", [?XMLATTR(<<"class">>, <<"valign">>)], STo),
|
|
|
|
?XAE("td", [?XMLATTR(<<"class">>, <<"valign">>)], [?XC("pre", FPacket)])]
|
2007-08-23 02:51:54 +02:00
|
|
|
)
|
|
|
|
end, Msgs),
|
|
|
|
[?XC("h1", io_lib:format(?T("~s's Offline Messages Queue"),
|
2010-07-22 18:49:12 +02:00
|
|
|
[us_to_list(US)]))] ++
|
2007-08-23 02:51:54 +02:00
|
|
|
case Res of
|
2011-04-11 13:28:54 +02:00
|
|
|
ok -> [?XREST("Submitted")];
|
2007-08-23 02:51:54 +02:00
|
|
|
nothing -> []
|
|
|
|
end ++
|
2010-11-29 20:44:31 +01:00
|
|
|
[?XAE("form", [?XMLATTR(<<"action">>, <<"">>), ?XMLATTR(<<"method">>, <<"post">>)],
|
2007-08-23 02:51:54 +02:00
|
|
|
[?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",
|
2010-11-29 20:44:31 +01:00
|
|
|
[?XAC("td", [?XMLATTR(<<"colspan">>, <<"4">>)], " ")]
|
2007-08-23 02:51:54 +02:00
|
|
|
)];
|
|
|
|
true ->
|
|
|
|
FMsgs
|
|
|
|
end
|
|
|
|
)]),
|
|
|
|
?BR,
|
|
|
|
?INPUTT("submit", "delete", "Delete Selected")
|
|
|
|
])].
|
|
|
|
|
|
|
|
user_queue_parse_query(US, Query) ->
|
|
|
|
case lists:keysearch("delete", 1, Query) of
|
|
|
|
{value, _} ->
|
2010-07-22 18:44:33 +02:00
|
|
|
{_, LServer} = US,
|
2007-08-23 02:51:54 +02:00
|
|
|
Msgs = lists:keysort(#offline_msg.timestamp,
|
2010-07-22 18:44:33 +02:00
|
|
|
gen_storage:dirty_read(LServer, {offline_msg, US})),
|
2007-08-23 02:51:54 +02:00
|
|
|
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 ->
|
2010-07-22 18:44:33 +02:00
|
|
|
gen_storage:delete_object(LServer, Msg);
|
2007-08-23 02:51:54 +02:00
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end, Msgs)
|
|
|
|
end,
|
2010-07-22 18:44:33 +02:00
|
|
|
gen_storage:transaction(LServer, offline_msg, F),
|
2007-08-23 02:51:54 +02:00
|
|
|
ok;
|
|
|
|
false ->
|
|
|
|
nothing
|
|
|
|
end.
|
|
|
|
|
|
|
|
us_to_list({User, Server}) ->
|
2009-06-01 18:52:14 +02:00
|
|
|
exmpp_jid:to_list(User, Server).
|
2007-08-23 02:51:54 +02:00
|
|
|
|
2011-05-27 11:23:44 +02:00
|
|
|
%% @spec (User::binary(), Host::binary()) -> integer()
|
2010-07-22 18:49:12 +02:00
|
|
|
get_queue_length(User, Host) ->
|
2011-05-27 11:23:44 +02:00
|
|
|
gen_storage:dirty_count_records(Host, offline_msg, [{'=', user_host, {User, Host}}]).
|
2010-01-12 13:06:43 +01:00
|
|
|
|
2010-01-13 01:00:34 +01:00
|
|
|
get_messages_subset(User, Host, MsgsAll) ->
|
|
|
|
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),
|
|
|
|
get_messages_subset2(MaxOfflineMsgs, Length, MsgsAll).
|
|
|
|
|
|
|
|
get_messages_subset2(Max, Length, MsgsAll) when Length =< Max*2 ->
|
|
|
|
MsgsAll;
|
|
|
|
get_messages_subset2(Max, Length, MsgsAll) ->
|
|
|
|
FirstN = Max,
|
|
|
|
{MsgsFirstN, Msgs2} = lists:split(FirstN, MsgsAll),
|
|
|
|
MsgsLastN = lists:nthtail(Length - FirstN - FirstN, Msgs2),
|
|
|
|
NoJID = exmpp_jid:make("...", "...", ""),
|
2011-01-18 23:25:17 +01:00
|
|
|
TimeStamp = make_timestamp(),
|
|
|
|
IntermediateMsg = #offline_msg{timestamp = TimeStamp, from = NoJID, to = NoJID,
|
2011-04-11 15:48:10 +02:00
|
|
|
packet = <<"...">>},
|
2010-01-13 01:00:34 +01:00
|
|
|
MsgsFirstN ++ [IntermediateMsg] ++ MsgsLastN.
|
|
|
|
|
2007-08-23 02:51:54 +02:00
|
|
|
webadmin_user(Acc, User, Server, Lang) ->
|
2011-07-11 19:46:26 +02:00
|
|
|
LUser = list_to_binary(exmpp_stringprep:nodeprep(User)),
|
|
|
|
LServer = list_to_binary(exmpp_stringprep:nameprep(Server)),
|
2010-07-22 18:49:12 +02:00
|
|
|
QueueLen = get_queue_length(LUser, LServer),
|
2010-01-12 13:06:43 +01:00
|
|
|
FQueueLen = [?AC("queue/", integer_to_list(QueueLen))],
|
2008-10-13 12:11:19 +02:00
|
|
|
Acc ++ [?XCT("h3", "Offline Messages:")] ++ FQueueLen ++ [?C(" "), ?INPUTT("submit", "removealloffline", "Remove All Offline Messages")].
|
|
|
|
|
|
|
|
webadmin_user_parse_query(_, "removealloffline", User, Server, _Query) ->
|
2011-04-11 12:54:37 +02:00
|
|
|
UserB = list_to_binary(User),
|
|
|
|
ServerB = list_to_binary(Server),
|
|
|
|
US = {UserB, ServerB},
|
2008-10-13 12:11:19 +02:00
|
|
|
F = fun() ->
|
2011-04-11 12:54:37 +02:00
|
|
|
gen_storage:write_lock_table(ServerB, offline_msg),
|
2008-10-13 12:11:19 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(Msg) ->
|
2011-04-11 12:54:37 +02:00
|
|
|
gen_storage:delete_object(ServerB, Msg)
|
|
|
|
end, gen_storage:read(ServerB, {offline_msg, US}))
|
2008-10-13 12:11:19 +02:00
|
|
|
end,
|
2011-04-11 12:54:37 +02:00
|
|
|
case gen_storage:transaction(ServerB, offline_msg, F) of
|
2008-10-13 12:11:19 +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-07-22 18:44:33 +02:00
|
|
|
|