2016-04-15 12:44:33 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
2016-12-27 10:44:07 +01:00
|
|
|
%%% File : mod_offline_sql.erl
|
|
|
|
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2016-04-15 12:44:33 +02:00
|
|
|
%%% Created : 15 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2016-12-27 10:44:07 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2023-01-09 17:09:06 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2023 ProcessOne
|
2016-12-27 10:44:07 +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.
|
|
|
|
%%%
|
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2016-04-15 12:44:33 +02:00
|
|
|
-module(mod_offline_sql).
|
|
|
|
|
|
|
|
|
|
|
|
-behaviour(mod_offline).
|
|
|
|
|
2017-05-21 22:21:13 +02:00
|
|
|
-export([init/2, store_message/1, pop_messages/2, remove_expired_messages/1,
|
2016-04-15 12:44:33 +02:00
|
|
|
remove_old_messages/2, remove_user/2, read_message_headers/2,
|
|
|
|
read_message/3, remove_message/3, read_all_messages/2,
|
2022-04-08 17:41:17 +02:00
|
|
|
remove_all_messages/2, count_messages/2, import/1, export/1, remove_old_messages_batch/3]).
|
2016-04-15 12:44:33 +02:00
|
|
|
|
2020-09-03 13:45:57 +02:00
|
|
|
-include_lib("xmpp/include/xmpp.hrl").
|
2016-04-15 12:44:33 +02:00
|
|
|
-include("mod_offline.hrl").
|
|
|
|
-include("logger.hrl").
|
|
|
|
-include("ejabberd_sql_pt.hrl").
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
2023-09-28 02:37:36 +02:00
|
|
|
init(Host, _Opts) ->
|
|
|
|
ejabberd_sql_schema:update_schema(Host, ?MODULE, schemas()),
|
2016-04-15 12:44:33 +02:00
|
|
|
ok.
|
|
|
|
|
2023-09-28 02:37:36 +02:00
|
|
|
schemas() ->
|
|
|
|
[#sql_schema{
|
|
|
|
version = 1,
|
|
|
|
tables =
|
|
|
|
[#sql_table{
|
|
|
|
name = <<"spool">>,
|
|
|
|
columns =
|
|
|
|
[#sql_column{name = <<"username">>, type = text},
|
|
|
|
#sql_column{name = <<"server_host">>, type = text},
|
|
|
|
#sql_column{name = <<"xml">>, type = {text, big}},
|
|
|
|
#sql_column{name = <<"seq">>, type = bigserial},
|
|
|
|
#sql_column{name = <<"created_at">>, type = timestamp,
|
|
|
|
default = true}],
|
|
|
|
indices = [#sql_index{
|
|
|
|
columns = [<<"server_host">>, <<"username">>]},
|
|
|
|
#sql_index{
|
|
|
|
columns = [<<"created_at">>]}]}]}].
|
|
|
|
|
2017-05-21 22:21:13 +02:00
|
|
|
store_message(#offline_msg{us = {LUser, LServer}} = M) ->
|
|
|
|
From = M#offline_msg.from,
|
|
|
|
To = M#offline_msg.to,
|
|
|
|
Packet = xmpp:set_from_to(M#offline_msg.packet, From, To),
|
2018-10-25 01:05:45 +02:00
|
|
|
NewPacket = misc:add_delay_info(
|
2017-05-21 22:21:13 +02:00
|
|
|
Packet, jid:make(LServer),
|
|
|
|
M#offline_msg.timestamp,
|
|
|
|
<<"Offline Storage">>),
|
|
|
|
XML = fxml:element_to_binary(
|
|
|
|
xmpp:encode(NewPacket)),
|
2017-05-23 11:25:13 +02:00
|
|
|
case ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL_INSERT(
|
|
|
|
"spool",
|
|
|
|
["username=%(LUser)s",
|
|
|
|
"server_host=%(LServer)s",
|
|
|
|
"xml=%(XML)s"])) of
|
2017-05-21 22:21:13 +02:00
|
|
|
{updated, _} ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
{error, db_failure}
|
2016-04-15 12:44:33 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
pop_messages(LUser, LServer) ->
|
2017-05-23 11:25:13 +02:00
|
|
|
case get_and_del_spool_msg_t(LServer, LUser) of
|
2016-04-15 12:44:33 +02:00
|
|
|
{atomic, {selected, Rs}} ->
|
|
|
|
{ok, lists:flatmap(
|
|
|
|
fun({_, XML}) ->
|
|
|
|
case xml_to_offline_msg(XML) of
|
|
|
|
{ok, Msg} ->
|
|
|
|
[Msg];
|
|
|
|
_Err ->
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end, Rs)};
|
|
|
|
Err ->
|
|
|
|
{error, Err}
|
|
|
|
end.
|
|
|
|
|
|
|
|
remove_expired_messages(_LServer) ->
|
|
|
|
%% TODO
|
|
|
|
{atomic, ok}.
|
|
|
|
|
|
|
|
remove_old_messages(Days, LServer) ->
|
2017-05-23 11:25:13 +02:00
|
|
|
case ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
2018-12-04 23:56:39 +01:00
|
|
|
fun(pgsql, _) ->
|
|
|
|
ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("DELETE FROM spool"
|
|
|
|
" WHERE created_at <"
|
2019-05-18 20:16:33 +02:00
|
|
|
" NOW() - %(Days)d * INTERVAL '1 DAY'"));
|
2021-01-27 19:40:35 +01:00
|
|
|
(sqlite, _) ->
|
|
|
|
ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("DELETE FROM spool"
|
|
|
|
" WHERE created_at <"
|
|
|
|
" DATETIME('now', '-%(Days)d days')"));
|
2018-12-04 23:56:39 +01:00
|
|
|
(_, _) ->
|
|
|
|
ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("DELETE FROM spool"
|
|
|
|
" WHERE created_at < NOW() - INTERVAL %(Days)d DAY"))
|
|
|
|
end)
|
|
|
|
of
|
2016-04-15 12:44:33 +02:00
|
|
|
{updated, N} ->
|
|
|
|
?INFO_MSG("~p message(s) deleted from offline spool", [N]);
|
|
|
|
_Error ->
|
|
|
|
?ERROR_MSG("Cannot delete message in offline spool: ~p", [_Error])
|
|
|
|
end,
|
|
|
|
{atomic, ok}.
|
|
|
|
|
2022-04-08 17:41:17 +02:00
|
|
|
remove_old_messages_batch(LServer, Days, Batch) ->
|
|
|
|
case ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
|
|
|
fun(pgsql, _) ->
|
|
|
|
ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("DELETE FROM spool"
|
|
|
|
" WHERE created_at <"
|
|
|
|
" NOW() - %(Days)d * INTERVAL '1 DAY' LIMIT %(Batch)d"));
|
|
|
|
(sqlite, _) ->
|
|
|
|
ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("DELETE FROM spool"
|
|
|
|
" WHERE created_at <"
|
|
|
|
" DATETIME('now', '-%(Days)d days') LIMIT %(Batch)d"));
|
|
|
|
(_, _) ->
|
|
|
|
ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("DELETE FROM spool"
|
|
|
|
" WHERE created_at < NOW() - INTERVAL %(Days)d DAY LIMIT %(Batch)d"))
|
|
|
|
end)
|
|
|
|
of
|
|
|
|
{updated, N} ->
|
|
|
|
{ok, N};
|
|
|
|
Error ->
|
|
|
|
{error, Error}
|
|
|
|
end.
|
|
|
|
|
2016-04-15 12:44:33 +02:00
|
|
|
remove_user(LUser, LServer) ->
|
2017-05-23 11:25:13 +02:00
|
|
|
ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL("delete from spool where username=%(LUser)s and %(LServer)H")).
|
2016-04-15 12:44:33 +02:00
|
|
|
|
|
|
|
read_message_headers(LUser, LServer) ->
|
2017-05-23 11:25:13 +02:00
|
|
|
case ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
|
|
|
?SQL("select @(xml)s, @(seq)d from spool"
|
2017-11-02 15:03:30 +01:00
|
|
|
" where username=%(LUser)s and %(LServer)H order by seq")) of
|
2016-05-24 17:44:07 +02:00
|
|
|
{selected, Rows} ->
|
2016-04-15 12:44:33 +02:00
|
|
|
lists:flatmap(
|
2016-05-24 17:44:07 +02:00
|
|
|
fun({XML, Seq}) ->
|
2016-04-15 12:44:33 +02:00
|
|
|
case xml_to_offline_msg(XML) of
|
|
|
|
{ok, #offline_msg{from = From,
|
|
|
|
to = To,
|
2016-11-18 11:38:08 +01:00
|
|
|
timestamp = TS,
|
2016-04-15 12:44:33 +02:00
|
|
|
packet = El}} ->
|
2016-11-18 11:38:08 +01:00
|
|
|
[{Seq, From, To, TS, El}];
|
2016-04-15 12:44:33 +02:00
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end, Rows);
|
|
|
|
_Err ->
|
2019-07-01 13:36:05 +02:00
|
|
|
error
|
2016-04-15 12:44:33 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
read_message(LUser, LServer, Seq) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
case ejabberd_sql:sql_query(
|
2016-04-15 12:44:33 +02:00
|
|
|
LServer,
|
2016-05-24 17:44:07 +02:00
|
|
|
?SQL("select @(xml)s from spool where username=%(LUser)s"
|
2017-11-02 15:03:30 +01:00
|
|
|
" and %(LServer)H"
|
2016-05-24 17:44:07 +02:00
|
|
|
" and seq=%(Seq)d")) of
|
|
|
|
{selected, [{RawXML}|_]} ->
|
2016-04-15 12:44:33 +02:00
|
|
|
case xml_to_offline_msg(RawXML) of
|
|
|
|
{ok, Msg} ->
|
|
|
|
{ok, Msg};
|
|
|
|
_ ->
|
|
|
|
error
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
error
|
|
|
|
end.
|
|
|
|
|
|
|
|
remove_message(LUser, LServer, Seq) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
ejabberd_sql:sql_query(
|
2016-04-15 12:44:33 +02:00
|
|
|
LServer,
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL("delete from spool where username=%(LUser)s and %(LServer)H"
|
2016-05-24 17:44:07 +02:00
|
|
|
" and seq=%(Seq)d")),
|
2016-04-15 12:44:33 +02:00
|
|
|
ok.
|
|
|
|
|
|
|
|
read_all_messages(LUser, LServer) ->
|
2017-05-23 11:25:13 +02:00
|
|
|
case ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
|
|
|
?SQL("select @(xml)s from spool where "
|
2017-11-02 15:03:30 +01:00
|
|
|
"username=%(LUser)s and %(LServer)H order by seq")) of
|
2016-04-15 12:44:33 +02:00
|
|
|
{selected, Rs} ->
|
|
|
|
lists:flatmap(
|
|
|
|
fun({XML}) ->
|
|
|
|
case xml_to_offline_msg(XML) of
|
|
|
|
{ok, Msg} -> [Msg];
|
|
|
|
_ -> []
|
|
|
|
end
|
|
|
|
end, Rs);
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
|
|
|
remove_all_messages(LUser, LServer) ->
|
2017-05-23 11:25:13 +02:00
|
|
|
remove_user(LUser, LServer),
|
2016-04-15 12:44:33 +02:00
|
|
|
{atomic, ok}.
|
|
|
|
|
|
|
|
count_messages(LUser, LServer) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
case catch ejabberd_sql:sql_query(
|
2016-04-15 12:44:33 +02:00
|
|
|
LServer,
|
|
|
|
?SQL("select @(count(*))d from spool "
|
2017-11-02 15:03:30 +01:00
|
|
|
"where username=%(LUser)s and %(LServer)H")) of
|
2016-04-15 12:44:33 +02:00
|
|
|
{selected, [{Res}]} ->
|
2019-06-30 20:14:37 +02:00
|
|
|
{cache, Res};
|
|
|
|
{selected, []} ->
|
|
|
|
{cache, 0};
|
|
|
|
_ ->
|
|
|
|
{nocache, 0}
|
2016-04-15 12:44:33 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
export(_Server) ->
|
|
|
|
[{offline_msg,
|
2017-06-28 11:14:49 +02:00
|
|
|
fun(Host, #offline_msg{us = {LUser, LServer}})
|
|
|
|
when LServer == Host ->
|
2017-11-02 15:03:30 +01:00
|
|
|
[?SQL("delete from spool where username=%(LUser)s"
|
|
|
|
" and %(LServer)H;")];
|
2017-06-28 11:14:49 +02:00
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end},
|
|
|
|
{offline_msg,
|
2016-04-15 12:44:33 +02:00
|
|
|
fun(Host, #offline_msg{us = {LUser, LServer},
|
|
|
|
timestamp = TimeStamp, from = From, to = To,
|
2016-11-13 08:44:53 +01:00
|
|
|
packet = El})
|
2016-04-15 12:44:33 +02:00
|
|
|
when LServer == Host ->
|
2016-11-13 08:44:53 +01:00
|
|
|
try xmpp:decode(El, ?NS_CLIENT, [ignore_els]) of
|
|
|
|
Packet ->
|
|
|
|
Packet1 = xmpp:set_from_to(Packet, From, To),
|
2018-10-25 01:05:45 +02:00
|
|
|
Packet2 = misc:add_delay_info(
|
2016-11-13 08:44:53 +01:00
|
|
|
Packet1, jid:make(LServer),
|
|
|
|
TimeStamp, <<"Offline Storage">>),
|
|
|
|
XML = fxml:element_to_binary(xmpp:encode(Packet2)),
|
2017-11-02 15:03:30 +01:00
|
|
|
[?SQL_INSERT(
|
|
|
|
"spool",
|
|
|
|
["username=%(LUser)s",
|
|
|
|
"server_host=%(LServer)s",
|
|
|
|
"xml=%(XML)s"])]
|
2016-11-13 08:44:53 +01:00
|
|
|
catch _:{xmpp_codec, Why} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Failed to decode packet ~p of user ~ts@~ts: ~ts",
|
2016-11-13 08:44:53 +01:00
|
|
|
[El, LUser, LServer, xmpp:format_error(Why)]),
|
|
|
|
[]
|
|
|
|
end;
|
2016-04-15 12:44:33 +02:00
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end}].
|
|
|
|
|
2016-11-22 14:48:01 +01:00
|
|
|
import(_) ->
|
|
|
|
ok.
|
2016-04-15 12:44:33 +02:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
|
|
|
xml_to_offline_msg(XML) ->
|
|
|
|
case fxml_stream:parse_element(XML) of
|
|
|
|
#xmlel{} = El ->
|
|
|
|
el_to_offline_msg(El);
|
|
|
|
Err ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Got ~p when parsing XML packet ~ts",
|
2016-04-15 12:44:33 +02:00
|
|
|
[Err, XML]),
|
|
|
|
Err
|
|
|
|
end.
|
|
|
|
|
|
|
|
el_to_offline_msg(El) ->
|
|
|
|
To_s = fxml:get_tag_attr_s(<<"to">>, El),
|
|
|
|
From_s = fxml:get_tag_attr_s(<<"from">>, El),
|
2017-02-26 08:07:12 +01:00
|
|
|
try
|
|
|
|
To = jid:decode(To_s),
|
|
|
|
From = jid:decode(From_s),
|
|
|
|
{ok, #offline_msg{us = {To#jid.luser, To#jid.lserver},
|
|
|
|
from = From,
|
|
|
|
to = To,
|
|
|
|
packet = El}}
|
|
|
|
catch _:{bad_jid, To_s} ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?ERROR_MSG("Failed to get 'to' JID from offline XML ~p", [El]),
|
2016-04-15 12:44:33 +02:00
|
|
|
{error, bad_jid_to};
|
2017-02-26 08:07:12 +01:00
|
|
|
_:{bad_jid, From_s} ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?ERROR_MSG("Failed to get 'from' JID from offline XML ~p", [El]),
|
2017-02-26 08:07:12 +01:00
|
|
|
{error, bad_jid_from}
|
2016-04-15 12:44:33 +02:00
|
|
|
end.
|
2017-05-23 11:25:13 +02:00
|
|
|
|
|
|
|
get_and_del_spool_msg_t(LServer, LUser) ->
|
|
|
|
F = fun () ->
|
|
|
|
Result =
|
|
|
|
ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("select @(username)s, @(xml)s from spool where "
|
2017-11-02 15:03:30 +01:00
|
|
|
"username=%(LUser)s and %(LServer)H order by seq;")),
|
2020-04-17 15:30:28 +02:00
|
|
|
DResult =
|
|
|
|
ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("delete from spool where"
|
|
|
|
" username=%(LUser)s and %(LServer)H;")),
|
|
|
|
case {Result, DResult} of
|
|
|
|
{{selected, Rs}, {updated, DC}} when length(Rs) /= DC ->
|
|
|
|
ejabberd_sql:restart(concurent_insert);
|
|
|
|
_ ->
|
|
|
|
Result
|
|
|
|
end
|
2017-05-23 11:25:13 +02:00
|
|
|
end,
|
|
|
|
ejabberd_sql:sql_transaction(LServer, F).
|