2004-12-14 00:00:12 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_offline_odbc.erl
|
2007-08-28 16:36:39 +02:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2010-07-22 18:52:48 +02:00
|
|
|
%%% Purpose : Store and manage offline messages in relational database (obsolete)
|
2007-08-28 16:36:39 +02:00
|
|
|
%%% Created : 5 Jan 2003 by Alexey Shchepin <alexey@process-one.net>
|
2007-12-24 13:58:05 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2010-01-12 17:15:16 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2010 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
|
|
|
|
%%%
|
2004-12-14 00:00:12 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
2007-08-23 02:51:54 +02:00
|
|
|
|
2004-12-14 00:00:12 +01:00
|
|
|
-module(mod_offline_odbc).
|
2007-08-28 16:36:39 +02:00
|
|
|
-author('alexey@process-one.net').
|
2004-12-14 00:00:12 +01:00
|
|
|
|
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2007-08-28 16:36:39 +02:00
|
|
|
-export([count_offline_messages/2]).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
-export([start/2,
|
2009-06-15 19:44:04 +02:00
|
|
|
loop/2,
|
2005-06-20 05:18:13 +02:00
|
|
|
stop/1,
|
2004-12-14 00:00:12 +01:00
|
|
|
store_packet/3,
|
2005-07-13 05:24:13 +02:00
|
|
|
pop_offline_messages/3,
|
2009-06-30 18:54:46 +02:00
|
|
|
get_sm_features/5,
|
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]).
|
2004-12-14 00:00:12 +01:00
|
|
|
|
2008-08-06 15:44:58 +02:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
|
|
|
|
2004-12-14 00:00:12 +01:00
|
|
|
-include("ejabberd.hrl").
|
2008-07-17 17:33:50 +02:00
|
|
|
-include("web/ejabberd_http.hrl").
|
|
|
|
-include("web/ejabberd_web_admin.hrl").
|
2004-12-14 00:00:12 +01:00
|
|
|
|
2010-07-22 18:42:41 +02:00
|
|
|
-record(offline_msg, {user, server, timestamp, expire, from, to, packet}).
|
2004-12-14 00:00:12 +01:00
|
|
|
|
|
|
|
-define(PROCNAME, ejabberd_offline).
|
|
|
|
-define(OFFLINE_TABLE_LOCK_THRESHOLD, 1000).
|
|
|
|
|
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).
|
|
|
|
|
2007-08-28 16:36:39 +02:00
|
|
|
start(Host, Opts) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
HostB = list_to_binary(Host),
|
|
|
|
ejabberd_hooks:add(offline_message_hook, HostB,
|
2004-12-14 00:00:12 +01:00
|
|
|
?MODULE, store_packet, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(resend_offline_messages_hook, HostB,
|
2004-12-14 00:00:12 +01: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),
|
2005-07-13 05:24:13 +02:00
|
|
|
register(gen_mod:get_module_proc(Host, ?PROCNAME),
|
2009-06-15 19:44:04 +02:00
|
|
|
spawn(?MODULE, loop, [Host, AccessMaxOfflineMsgs])).
|
2004-12-14 00:00:12 +01:00
|
|
|
|
2009-06-15 19:44:04 +02:00
|
|
|
loop(Host, AccessMaxOfflineMsgs) ->
|
2004-12-14 00:00:12 +01:00
|
|
|
receive
|
2010-07-22 18:42:41 +02:00
|
|
|
#offline_msg{user = User, server = Server} = Msg ->
|
|
|
|
Msgs = receive_all(User, Server, [Msg]),
|
2007-08-28 16:36:39 +02:00
|
|
|
Len = length(Msgs),
|
2009-06-15 19:44:04 +02:00
|
|
|
MaxOfflineMsgs = get_max_user_messages(AccessMaxOfflineMsgs,
|
|
|
|
User, Host),
|
2007-08-28 16:36:39 +02:00
|
|
|
|
|
|
|
%% Only count existing messages if needed:
|
|
|
|
Count = if MaxOfflineMsgs =/= infinity ->
|
2010-07-22 18:42:41 +02:00
|
|
|
Len + count_offline_messages(User, Server);
|
2007-08-28 16:36:39 +02:00
|
|
|
true -> 0
|
|
|
|
end,
|
2010-01-09 17:44:33 +01:00
|
|
|
if
|
2007-08-28 16:36:39 +02:00
|
|
|
Count > MaxOfflineMsgs ->
|
|
|
|
discard_warn_sender(Msgs);
|
|
|
|
true ->
|
|
|
|
Query = lists:map(
|
|
|
|
fun(M) ->
|
|
|
|
Username =
|
|
|
|
ejabberd_odbc:escape(
|
2009-06-01 18:40:51 +02:00
|
|
|
exmpp_jid:prep_node_as_list(M#offline_msg.to)),
|
2007-08-28 16:36:39 +02:00
|
|
|
From = M#offline_msg.from,
|
|
|
|
To = M#offline_msg.to,
|
2008-08-06 15:44:58 +02:00
|
|
|
Packet0 = exmpp_stanza:set_jids(
|
2008-10-01 11:41:57 +02:00
|
|
|
M#offline_msg.packet,
|
2008-08-06 15:44:58 +02:00
|
|
|
From,
|
2008-10-01 11:41:57 +02:00
|
|
|
To),
|
2009-06-30 18:55:26 +02:00
|
|
|
Packet1 = exmpp_xml:append_children(
|
|
|
|
Packet0,
|
|
|
|
[jlib:timestamp_to_xml(
|
|
|
|
calendar:now_to_universal_time(
|
|
|
|
M#offline_msg.timestamp),
|
|
|
|
utc,
|
2010-01-09 17:44:33 +01:00
|
|
|
exmpp_jid:make("", Host, ""),
|
2009-06-30 18:55:26 +02:00
|
|
|
"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))]),
|
2007-08-28 16:36:39 +02:00
|
|
|
XML =
|
|
|
|
ejabberd_odbc:escape(
|
2008-10-01 11:41:57 +02:00
|
|
|
exmpp_xml:document_to_list(Packet1)),
|
2010-07-22 18:42:41 +02:00
|
|
|
odbc_queries:add_spool_sql(Server, Username, XML)
|
2007-08-28 16:36:39 +02:00
|
|
|
end, Msgs),
|
2010-07-22 18:42:41 +02:00
|
|
|
case catch odbc_queries:add_spool(Server, Query) of
|
2007-08-28 16:36:39 +02:00
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p~n", [Reason]);
|
2008-09-16 16:39:57 +02:00
|
|
|
{error, Reason} ->
|
|
|
|
?ERROR_MSG("~p~n", [Reason]);
|
2007-08-28 16:36:39 +02:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end
|
2004-12-14 00:00:12 +01:00
|
|
|
end,
|
2009-06-15 19:44:04 +02:00
|
|
|
loop(Host, AccessMaxOfflineMsgs);
|
2004-12-14 00:00:12 +01:00
|
|
|
_ ->
|
2009-06-15 19:44:04 +02:00
|
|
|
loop(Host, 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
|
2004-12-14 00:00:12 +01:00
|
|
|
end.
|
|
|
|
|
2010-07-22 18:42:41 +02:00
|
|
|
receive_all(Username, Server, Msgs) ->
|
2004-12-14 00:00:12 +01:00
|
|
|
receive
|
2010-07-22 18:42:41 +02:00
|
|
|
#offline_msg{user = Username, server = Server} = Msg ->
|
|
|
|
receive_all(Username, Server, [Msg | Msgs])
|
2004-12-14 00:00:12 +01:00
|
|
|
after 0 ->
|
2005-08-29 21:00:10 +02:00
|
|
|
lists:reverse(Msgs)
|
2004-12-14 00:00:12 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
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-12-14 00:00:12 +01:00
|
|
|
?MODULE, store_packet, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(resend_offline_messages_hook, HostB,
|
2004-12-14 00:00:12 +01: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-07-13 05:24:13 +02:00
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
|
|
|
exit(whereis(Proc), stop),
|
2004-12-14 00:00:12 +01:00
|
|
|
ok.
|
|
|
|
|
2009-06-30 18:54:46 +02:00
|
|
|
get_sm_features(Acc, _From, _To, "", _Lang) ->
|
|
|
|
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, []};
|
|
|
|
|
|
|
|
get_sm_features(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc.
|
|
|
|
|
|
|
|
|
2004-12-14 00:00:12 +01:00
|
|
|
store_packet(From, To, Packet) ->
|
2008-10-01 11:41:57 +02:00
|
|
|
Type = exmpp_stanza:get_type(Packet),
|
2004-12-14 00:00:12 +01: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-12-14 00:00:12 +01:00
|
|
|
true ->
|
2010-07-22 18:42:41 +02:00
|
|
|
LUser = exmpp_jid:prep_node_as_list(To),
|
|
|
|
LServer = exmpp_jid:prep_domain_as_list(To),
|
2004-12-14 00:00:12 +01:00
|
|
|
TimeStamp = now(),
|
2008-08-06 15:44:58 +02:00
|
|
|
Expire = find_x_expire(TimeStamp, Packet#xmlel.children),
|
2010-07-22 18:42:41 +02:00
|
|
|
Proc1 = gen_mod:get_module_proc(LServer, ?PROCNAME),
|
|
|
|
Proc = case whereis(Proc1) of
|
|
|
|
undefined ->
|
|
|
|
gen_mod:get_module_proc(global, ?PROCNAME);
|
|
|
|
_ ->
|
|
|
|
Proc1
|
|
|
|
end,
|
|
|
|
Proc !
|
2005-07-13 05:24:13 +02:00
|
|
|
#offline_msg{user = LUser,
|
2010-07-22 18:42:41 +02:00
|
|
|
server = LServer,
|
2005-07-13 05:24:13 +02:00
|
|
|
timestamp = TimeStamp,
|
|
|
|
expire = Expire,
|
|
|
|
from = From,
|
|
|
|
to = To,
|
|
|
|
packet = Packet},
|
2004-12-14 00:00:12 +01:00
|
|
|
stop;
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
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, _} ->
|
2004-12-14 00:00:12 +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 ->
|
2004-12-14 00:00:12 +01:00
|
|
|
true;
|
|
|
|
_ ->
|
2009-06-30 21:33:09 +02:00
|
|
|
ID = case exmpp_stanza:get_id(Packet) of
|
|
|
|
undefined ->
|
2008-08-06 15:44:58 +02:00
|
|
|
#xmlel{ns = ?NS_MESSAGE_EVENT, name = 'id'};
|
2004-12-14 00:00:12 +01:00
|
|
|
S ->
|
2008-08-06 15:44:58 +02:00
|
|
|
#xmlel{ns = ?NS_MESSAGE_EVENT, name = 'id',
|
2009-06-30 21:33:09 +02:00
|
|
|
children = [#xmlcdata{cdata =
|
|
|
|
S}]}
|
2004-12-14 00:00:12 +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'}]},
|
2004-12-14 00:00:12 +01:00
|
|
|
ejabberd_router:route(
|
2008-08-06 15:44:58 +02:00
|
|
|
To, From, exmpp_xml:set_children(Packet, [X])),
|
2004-12-14 00:00:12 +01:00
|
|
|
true
|
2009-06-30 21:33:09 +02:00
|
|
|
end;
|
2004-12-14 00:00:12 +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}).
|
2004-12-14 00:00:12 +01:00
|
|
|
|
|
|
|
find_x_expire(_, []) ->
|
|
|
|
never;
|
2008-08-06 15:44:58 +02:00
|
|
|
find_x_expire(TimeStamp, [#xmlel{ns = ?NS_MESSAGE_EXPIRE} = El | _Els]) ->
|
2009-01-21 14:34:26 +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', _} ->
|
|
|
|
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-12-14 00:00:12 +01:00
|
|
|
_ ->
|
2008-08-06 15:44:58 +02:00
|
|
|
never
|
|
|
|
end;
|
|
|
|
find_x_expire(TimeStamp, [_ | Els]) ->
|
|
|
|
find_x_expire(TimeStamp, Els).
|
2004-12-14 00:00:12 +01:00
|
|
|
|
|
|
|
|
2010-01-09 17:44:33 +01:00
|
|
|
pop_offline_messages(Ls, User, Server)
|
2009-01-03 16:15:38 +01:00
|
|
|
when is_binary(User), is_binary(Server) ->
|
2008-10-01 11:41:57 +02:00
|
|
|
try
|
2009-01-03 16:15:38 +01:00
|
|
|
LUser = binary_to_list(User),
|
|
|
|
LServer = binary_to_list(Server),
|
2008-10-01 11:41:57 +02:00
|
|
|
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}) ->
|
|
|
|
try
|
2010-01-09 17:44:33 +01:00
|
|
|
[El] = exmpp_xml:parse_document(XML,
|
|
|
|
[names_as_atom, {check_elems, xmpp},
|
2009-01-03 16:15:38 +01:00
|
|
|
{check_nss,xmpp}, {check_attrs,xmpp}]),
|
2009-06-01 18:35:55 +02:00
|
|
|
To = exmpp_jid:parse(
|
2008-10-01 11:41:57 +02:00
|
|
|
exmpp_stanza:get_recipient(El)),
|
2009-06-01 18:35:55 +02:00
|
|
|
From = exmpp_jid:parse(
|
2008-10-01 11:41:57 +02:00
|
|
|
exmpp_stanza:get_sender(El)),
|
|
|
|
[{route, From, To, El}]
|
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end, Rs);
|
|
|
|
_ ->
|
|
|
|
Ls
|
|
|
|
end
|
|
|
|
catch
|
2004-12-14 00:00:12 +01:00
|
|
|
_ ->
|
2008-10-01 11:41:57 +02:00
|
|
|
[]
|
2004-12-14 00:00:12 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2010-01-09 17:44:33 +01:00
|
|
|
remove_user(User, Server)
|
2009-01-03 16:15:38 +01:00
|
|
|
when is_binary(User), is_binary(Server) ->
|
2008-10-01 11:41:57 +02:00
|
|
|
try
|
2009-02-20 16:30:16 +01:00
|
|
|
LUser = binary_to_list(exmpp_stringprep:nodeprep(User)),
|
|
|
|
LServer = binary_to_list(exmpp_stringprep:nameprep(Server)),
|
2008-10-01 11:41:57 +02:00
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
odbc_queries:del_spool_msg(LServer, Username)
|
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2004-12-14 00:00:12 +01:00
|
|
|
|
2007-08-23 02:51:54 +02:00
|
|
|
|
2007-08-28 16:36:39 +02:00
|
|
|
%% Helper functions:
|
|
|
|
|
|
|
|
%% TODO: Warning - This function is a duplicate from mod_offline.erl
|
|
|
|
%% It is duplicate to stay consistent (many functions are duplicated
|
|
|
|
%% in this module). It will be refactored later on.
|
|
|
|
%% 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.",
|
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-28 16:36:39 +02:00
|
|
|
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.
|
|
|
|
|
|
|
|
user_queue(User, Server, Query, Lang) ->
|
2010-01-13 01:00:34 +01:00
|
|
|
{US, MsgsAll, Res} = try
|
2008-10-01 11:41:57 +02:00
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
2010-07-22 18:42:41 +02:00
|
|
|
Host = ejabberd_odbc:escape(LServer),
|
2008-10-01 11:41:57 +02:00
|
|
|
US0 = {LUser, LServer},
|
|
|
|
R = user_queue_parse_query(Username, LServer, Query),
|
|
|
|
M = case catch ejabberd_odbc:sql_query(
|
|
|
|
LServer,
|
|
|
|
["select username, xml from spool"
|
|
|
|
" where username='", Username, "'"
|
2010-07-22 18:42:41 +02:00
|
|
|
" and host='", Host, "'"
|
2008-10-01 11:41:57 +02:00
|
|
|
" order by seq;"]) of
|
|
|
|
{selected, ["username", "xml"], Rs} ->
|
|
|
|
lists:flatmap(
|
|
|
|
fun({_, XML}) ->
|
2010-01-09 17:44:33 +01:00
|
|
|
try exmpp_xml:parse_document(XML,
|
|
|
|
[names_as_atom, {check_elems, xmpp},
|
2009-01-03 16:15:38 +01:00
|
|
|
{check_nss,xmpp}, {check_attrs,xmpp}]) of
|
2008-10-01 11:41:57 +02:00
|
|
|
[El] ->
|
|
|
|
[El]
|
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end, Rs);
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
{US0, M, R}
|
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
{{"invalid", "invalid"}, [], nothing}
|
|
|
|
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(
|
2008-08-06 15:44:58 +02:00
|
|
|
fun(#xmlel{} = Msg) ->
|
2007-08-23 02:51:54 +02:00
|
|
|
ID = jlib:encode_base64(binary_to_list(term_to_binary(Msg))),
|
|
|
|
Packet = Msg,
|
2008-08-06 15:44:58 +02:00
|
|
|
FPacket = exmpp_xml:node_to_list(
|
|
|
|
exmpp_xml:indent_document(Packet, <<" ">>),
|
|
|
|
[?DEFAULT_NS], ?PREFIXED_NS),
|
2007-08-23 02:51:54 +02:00
|
|
|
?XE("tr",
|
2009-05-27 19:29:58 +02:00
|
|
|
[?XAE("td", [?XMLATTR('class', <<"valign">>)], [?INPUT("checkbox", "selected", ID)]),
|
|
|
|
?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"),
|
|
|
|
[us_to_list(US)]))] ++
|
|
|
|
case Res of
|
2009-01-19 16:27:07 +01:00
|
|
|
ok -> [?XREST("Submitted")];
|
2007-08-23 02:51:54 +02:00
|
|
|
nothing -> []
|
|
|
|
end ++
|
2009-05-27 19:29:58 +02: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", "Packet")
|
|
|
|
])]),
|
|
|
|
?XE("tbody",
|
|
|
|
if
|
|
|
|
FMsgs == [] ->
|
|
|
|
[?XE("tr",
|
2009-05-27 19:29:58 +02: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(Username, LServer, Query) ->
|
|
|
|
case lists:keysearch("delete", 1, Query) of
|
|
|
|
{value, _} ->
|
2010-07-22 18:42:41 +02:00
|
|
|
Host = ejabberd_odbc:escape(LServer),
|
2007-08-23 02:51:54 +02:00
|
|
|
Msgs = case catch ejabberd_odbc:sql_query(
|
|
|
|
LServer,
|
|
|
|
["select xml, seq from spool"
|
2010-07-22 18:42:41 +02:00
|
|
|
" where username='", Username, "' and"
|
|
|
|
" host='", Host, "'"
|
2007-08-23 02:51:54 +02:00
|
|
|
" order by seq;"]) of
|
|
|
|
{selected, ["xml", "seq"], Rs} ->
|
|
|
|
lists:flatmap(
|
|
|
|
fun({XML, Seq}) ->
|
2010-01-09 17:44:33 +01:00
|
|
|
try exmpp_xml:parse_document(XML,
|
|
|
|
[names_as_atom, {check_elems, xmpp},
|
2009-01-03 16:15:38 +01:00
|
|
|
{check_nss,xmpp}, {check_attrs,xmpp}]) of
|
2008-08-06 15:44:58 +02:00
|
|
|
[El] ->
|
2007-08-23 02:51:54 +02:00
|
|
|
[{El, Seq}]
|
2008-08-06 15:44:58 +02:00
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
[]
|
2007-08-23 02:51:54 +02:00
|
|
|
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"
|
2010-07-22 18:42:41 +02:00
|
|
|
" where username='", Username, "' and host='", Host, "'"
|
2007-08-23 02:51:54 +02:00
|
|
|
" and seq='", SSeq, "';"]);
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end, Msgs)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F),
|
|
|
|
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
|
|
|
|
2010-01-12 13:06:43 +01:00
|
|
|
get_queue_length(Username, LServer) ->
|
2010-07-22 18:42:41 +02:00
|
|
|
Host = ejabberd_odbc:escape(LServer),
|
2010-01-12 13:06:43 +01:00
|
|
|
case catch ejabberd_odbc:sql_query(
|
|
|
|
LServer,
|
|
|
|
["select count(*) from spool"
|
2010-07-22 18:42:41 +02:00
|
|
|
" where username='", Username, "'"
|
|
|
|
" and host='", Host, "';"]) of
|
2010-01-12 13:06:43 +01:00
|
|
|
{selected, [_], [{SCount}]} ->
|
|
|
|
SCount;
|
|
|
|
_ ->
|
|
|
|
0
|
|
|
|
end.
|
|
|
|
|
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),
|
|
|
|
IntermediateMsg = exmpp_xml:element("..."),
|
|
|
|
MsgsFirstN ++ [IntermediateMsg] ++ MsgsLastN.
|
|
|
|
|
2007-08-23 02:51:54 +02:00
|
|
|
webadmin_user(Acc, User, Server, Lang) ->
|
2010-07-22 18:42:41 +02:00
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
2010-01-12 13:06:43 +01:00
|
|
|
QueueLen = get_queue_length(Username, LServer),
|
|
|
|
FQueueLen = [?AC("queue/", 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) ->
|
|
|
|
case catch odbc_queries:del_spool_msg(Server, User) of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("Failed to remove offline messages: ~p", [Reason]),
|
|
|
|
{stop, error};
|
|
|
|
{error, Reason} ->
|
|
|
|
?ERROR_MSG("Failed to remove offline messages: ~p", [Reason]),
|
|
|
|
{stop, error};
|
|
|
|
_ ->
|
|
|
|
?INFO_MSG("Removed all offline messages for ~s@~s", [User, Server]),
|
|
|
|
{stop, ok}
|
|
|
|
end;
|
|
|
|
webadmin_user_parse_query(Acc, _Action, _User, _Server, _Query) ->
|
|
|
|
Acc.
|
2007-08-28 16:36:39 +02:00
|
|
|
|
|
|
|
%% ------------------------------------------------
|
|
|
|
%% mod_offline: number of messages quota management
|
|
|
|
|
|
|
|
%% Returns as integer the number of offline messages for a given user
|
|
|
|
count_offline_messages(LUser, LServer) ->
|
2007-10-06 19:35:15 +02:00
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
2010-07-22 18:42:41 +02:00
|
|
|
Host = ejabberd_odbc:escape(LServer),
|
2007-08-28 16:36:39 +02:00
|
|
|
case catch odbc_queries:count_records_where(
|
2010-07-22 18:42:41 +02:00
|
|
|
LServer, "spool", "where username='" ++ Username ++
|
|
|
|
"' and host='" ++ Host ++ "'") of
|
2007-08-28 16:36:39 +02:00
|
|
|
{selected, [_], [{Res}]} ->
|
|
|
|
list_to_integer(Res);
|
|
|
|
_ ->
|
|
|
|
0
|
|
|
|
end.
|