2016-04-15 14:11:31 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
2016-12-27 10:44:07 +01:00
|
|
|
%%% File : mod_mam_sql.erl
|
|
|
|
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2016-04-15 14:11:31 +02:00
|
|
|
%%% Created : 15 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2016-12-27 10:44:07 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2019-01-08 22:53:27 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2019 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 14:11:31 +02:00
|
|
|
-module(mod_mam_sql).
|
|
|
|
|
2016-05-04 20:01:05 +02:00
|
|
|
|
2016-04-15 14:11:31 +02:00
|
|
|
-behaviour(mod_mam).
|
|
|
|
|
|
|
|
%% API
|
|
|
|
-export([init/2, remove_user/2, remove_room/3, delete_old_messages/3,
|
2019-05-02 11:12:22 +02:00
|
|
|
extended_fields/0, store/8, write_prefs/4, get_prefs/2, select/7, export/1, remove_from_archive/3,
|
|
|
|
is_empty_for_user/2, is_empty_for_room/3, select_with_mucsub/6]).
|
2016-04-15 14:11:31 +02:00
|
|
|
|
|
|
|
-include_lib("stdlib/include/ms_transform.hrl").
|
2016-07-25 12:50:30 +02:00
|
|
|
-include("xmpp.hrl").
|
2016-04-15 14:11:31 +02:00
|
|
|
-include("mod_mam.hrl").
|
|
|
|
-include("logger.hrl").
|
2016-05-04 20:01:05 +02:00
|
|
|
-include("ejabberd_sql_pt.hrl").
|
2019-03-29 16:11:50 +01:00
|
|
|
-include("mod_muc_room.hrl").
|
2016-04-15 14:11:31 +02:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
init(_Host, _Opts) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
remove_user(LUser, LServer) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
ejabberd_sql:sql_query(
|
2016-04-15 14:11:31 +02:00
|
|
|
LServer,
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL("delete from archive where username=%(LUser)s and %(LServer)H")),
|
2016-04-20 11:27:32 +02:00
|
|
|
ejabberd_sql:sql_query(
|
2016-04-15 14:11:31 +02:00
|
|
|
LServer,
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL("delete from archive_prefs where username=%(LUser)s and %(LServer)H")).
|
2016-04-15 14:11:31 +02:00
|
|
|
|
|
|
|
remove_room(LServer, LName, LHost) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
LUser = jid:encode({LName, LHost, <<>>}),
|
2016-04-15 14:11:31 +02:00
|
|
|
remove_user(LUser, LServer).
|
|
|
|
|
2018-04-20 13:27:39 +02:00
|
|
|
remove_from_archive(LUser, LServer, none) ->
|
|
|
|
case ejabberd_sql:sql_query(LServer,
|
|
|
|
?SQL("delete from archive where username=%(LUser)s and %(LServer)H")) of
|
|
|
|
{error, Reason} -> {error, Reason};
|
|
|
|
_ -> ok
|
|
|
|
end;
|
|
|
|
remove_from_archive(LUser, LServer, WithJid) ->
|
|
|
|
Peer = jid:encode(jid:remove_resource(WithJid)),
|
|
|
|
case ejabberd_sql:sql_query(LServer,
|
|
|
|
?SQL("delete from archive where username=%(LUser)s and %(LServer)H and bare_peer=%(Peer)s")) of
|
|
|
|
{error, Reason} -> {error, Reason};
|
|
|
|
_ -> ok
|
|
|
|
end.
|
|
|
|
|
2016-04-15 14:11:31 +02:00
|
|
|
delete_old_messages(ServerHost, TimeStamp, Type) ->
|
2017-11-02 15:03:30 +01:00
|
|
|
TS = now_to_usec(TimeStamp),
|
|
|
|
case Type of
|
|
|
|
all ->
|
|
|
|
ejabberd_sql:sql_query(
|
|
|
|
ServerHost,
|
|
|
|
?SQL("delete from archive"
|
|
|
|
" where timestamp < %(TS)d and %(ServerHost)H"));
|
|
|
|
_ ->
|
|
|
|
SType = misc:atom_to_binary(Type),
|
|
|
|
ejabberd_sql:sql_query(
|
|
|
|
ServerHost,
|
|
|
|
?SQL("delete from archive"
|
|
|
|
" where timestamp < %(TS)d"
|
|
|
|
" and kind=%(SType)s"
|
|
|
|
" and %(ServerHost)H"))
|
|
|
|
end,
|
2016-04-15 14:11:31 +02:00
|
|
|
ok.
|
|
|
|
|
|
|
|
extended_fields() ->
|
2016-11-18 11:38:08 +01:00
|
|
|
[{withtext, <<"">>}].
|
2016-04-15 14:11:31 +02:00
|
|
|
|
2017-11-14 22:02:48 +01:00
|
|
|
store(Pkt, LServer, {LUser, LHost}, Type, Peer, Nick, _Dir, TS) ->
|
2016-04-15 14:11:31 +02:00
|
|
|
SUser = case Type of
|
|
|
|
chat -> LUser;
|
2017-02-26 08:07:12 +01:00
|
|
|
groupchat -> jid:encode({LUser, LHost, <<>>})
|
2016-04-15 14:11:31 +02:00
|
|
|
end,
|
2017-02-26 08:07:12 +01:00
|
|
|
BarePeer = jid:encode(
|
2016-04-15 14:11:31 +02:00
|
|
|
jid:tolower(
|
|
|
|
jid:remove_resource(Peer))),
|
2017-02-26 08:07:12 +01:00
|
|
|
LPeer = jid:encode(
|
2016-04-15 14:11:31 +02:00
|
|
|
jid:tolower(Peer)),
|
|
|
|
Body = fxml:get_subtag_cdata(Pkt, <<"body">>),
|
2017-04-11 12:13:58 +02:00
|
|
|
SType = misc:atom_to_binary(Type),
|
2019-06-14 11:33:26 +02:00
|
|
|
XML = case mod_mam_opt:compress_xml(LServer) of
|
2018-11-28 11:25:04 +01:00
|
|
|
true ->
|
|
|
|
J1 = case Type of
|
|
|
|
chat -> jid:encode({LUser, LHost, <<>>});
|
|
|
|
groupchat -> SUser
|
|
|
|
end,
|
|
|
|
xml_compress:encode(Pkt, J1, LPeer);
|
|
|
|
_ ->
|
|
|
|
fxml:element_to_binary(Pkt)
|
|
|
|
end,
|
2016-04-20 11:27:32 +02:00
|
|
|
case ejabberd_sql:sql_query(
|
2016-05-04 20:01:05 +02:00
|
|
|
LServer,
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL_INSERT(
|
|
|
|
"archive",
|
|
|
|
["username=%(SUser)s",
|
|
|
|
"server_host=%(LServer)s",
|
2017-11-14 22:02:48 +01:00
|
|
|
"timestamp=%(TS)d",
|
2017-11-02 15:03:30 +01:00
|
|
|
"peer=%(LPeer)s",
|
|
|
|
"bare_peer=%(BarePeer)s",
|
|
|
|
"xml=%(XML)s",
|
|
|
|
"txt=%(Body)s",
|
|
|
|
"kind=%(SType)s",
|
|
|
|
"nick=%(Nick)s"])) of
|
2016-04-15 14:11:31 +02:00
|
|
|
{updated, _} ->
|
2017-11-14 22:02:48 +01:00
|
|
|
ok;
|
2016-04-15 14:11:31 +02:00
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end.
|
|
|
|
|
|
|
|
write_prefs(LUser, _LServer, #archive_prefs{default = Default,
|
|
|
|
never = Never,
|
|
|
|
always = Always},
|
|
|
|
ServerHost) ->
|
|
|
|
SDefault = erlang:atom_to_binary(Default, utf8),
|
2017-04-11 12:13:58 +02:00
|
|
|
SAlways = misc:term_to_expr(Always),
|
|
|
|
SNever = misc:term_to_expr(Never),
|
2016-05-24 17:44:07 +02:00
|
|
|
case ?SQL_UPSERT(
|
|
|
|
ServerHost,
|
|
|
|
"archive_prefs",
|
|
|
|
["!username=%(LUser)s",
|
2017-11-02 15:03:30 +01:00
|
|
|
"!server_host=%(ServerHost)s",
|
2016-05-24 17:44:07 +02:00
|
|
|
"def=%(SDefault)s",
|
|
|
|
"always=%(SAlways)s",
|
|
|
|
"never=%(SNever)s"]) of
|
2017-05-18 11:10:36 +02:00
|
|
|
ok ->
|
2016-04-15 14:11:31 +02:00
|
|
|
ok;
|
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_prefs(LUser, LServer) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
case ejabberd_sql:sql_query(
|
2016-04-15 14:11:31 +02:00
|
|
|
LServer,
|
2016-05-24 17:44:07 +02:00
|
|
|
?SQL("select @(def)s, @(always)s, @(never)s from archive_prefs"
|
2017-11-02 15:03:30 +01:00
|
|
|
" where username=%(LUser)s and %(LServer)H")) of
|
2016-05-24 17:44:07 +02:00
|
|
|
{selected, [{SDefault, SAlways, SNever}]} ->
|
2016-04-15 14:11:31 +02:00
|
|
|
Default = erlang:binary_to_existing_atom(SDefault, utf8),
|
2016-04-20 11:27:32 +02:00
|
|
|
Always = ejabberd_sql:decode_term(SAlways),
|
|
|
|
Never = ejabberd_sql:decode_term(SNever),
|
2016-04-15 14:11:31 +02:00
|
|
|
{ok, #archive_prefs{us = {LUser, LServer},
|
|
|
|
default = Default,
|
|
|
|
always = Always,
|
|
|
|
never = Never}};
|
|
|
|
_ ->
|
|
|
|
error
|
|
|
|
end.
|
|
|
|
|
|
|
|
select(LServer, JidRequestor, #jid{luser = LUser} = JidArchive,
|
2019-05-02 11:12:22 +02:00
|
|
|
MAMQuery, RSM, MsgType, Flags) ->
|
2016-04-15 14:11:31 +02:00
|
|
|
User = case MsgType of
|
|
|
|
chat -> LUser;
|
2018-12-05 11:14:29 +01:00
|
|
|
_ -> jid:encode(JidArchive)
|
2016-04-15 14:11:31 +02:00
|
|
|
end,
|
2019-03-29 16:11:50 +01:00
|
|
|
{Query, CountQuery} = make_sql_query(User, LServer, MAMQuery, RSM, none),
|
2019-05-02 11:12:22 +02:00
|
|
|
do_select_query(LServer, JidRequestor, JidArchive, RSM, MsgType, Query, CountQuery, Flags).
|
2019-03-29 16:11:50 +01:00
|
|
|
|
|
|
|
-spec select_with_mucsub(binary(), jid(), jid(), mam_query:result(),
|
2019-05-02 11:12:22 +02:00
|
|
|
#rsm_set{} | undefined, all | only_count | only_messages) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
{[{binary(), non_neg_integer(), xmlel()}], boolean(), non_neg_integer()} |
|
2019-03-29 16:11:50 +01:00
|
|
|
{error, db_failure}.
|
|
|
|
select_with_mucsub(LServer, JidRequestor, #jid{luser = LUser} = JidArchive,
|
2019-05-02 11:12:22 +02:00
|
|
|
MAMQuery, RSM, Flags) ->
|
2019-03-29 18:41:51 +01:00
|
|
|
Extra = case gen_mod:db_mod(LServer, mod_muc) of
|
|
|
|
mod_muc_sql ->
|
2019-03-29 16:11:50 +01:00
|
|
|
subscribers_table;
|
|
|
|
_ ->
|
|
|
|
SubRooms = case mod_muc_admin:find_hosts(LServer) of
|
|
|
|
[First|_] ->
|
2019-04-03 13:20:37 +02:00
|
|
|
case mod_muc:get_subscribed_rooms(First, JidRequestor) of
|
|
|
|
{ok, L} -> L;
|
|
|
|
{error, _} -> []
|
|
|
|
end;
|
2019-03-29 16:11:50 +01:00
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
2019-04-03 13:20:37 +02:00
|
|
|
[jid:encode(Jid) || {Jid, _} <- SubRooms]
|
2019-03-29 16:11:50 +01:00
|
|
|
end,
|
|
|
|
{Query, CountQuery} = make_sql_query(LUser, LServer, MAMQuery, RSM, Extra),
|
2019-05-02 11:12:22 +02:00
|
|
|
do_select_query(LServer, JidRequestor, JidArchive, RSM, chat, Query, CountQuery, Flags).
|
2019-03-29 16:11:50 +01:00
|
|
|
|
2019-05-02 11:12:22 +02:00
|
|
|
do_select_query(LServer, JidRequestor, #jid{luser = LUser} = JidArchive, RSM,
|
|
|
|
MsgType, Query, CountQuery, Flags) ->
|
2016-04-15 14:11:31 +02:00
|
|
|
% TODO from XEP-0313 v0.2: "To conserve resources, a server MAY place a
|
|
|
|
% reasonable limit on how many stanzas may be pushed to a client in one
|
|
|
|
% request. If a query returns a number of stanzas greater than this limit
|
|
|
|
% and the client did not specify a limit using RSM then the server should
|
|
|
|
% return a policy-violation error to the client." We currently don't do this
|
|
|
|
% for v0.2 requests, but we do limit #rsm_in.max for v0.3 and newer.
|
2019-05-02 11:12:22 +02:00
|
|
|
QRes = case Flags of
|
|
|
|
all ->
|
|
|
|
{ejabberd_sql:sql_query(LServer, Query), ejabberd_sql:sql_query(LServer, CountQuery)};
|
|
|
|
only_messages ->
|
|
|
|
{ejabberd_sql:sql_query(LServer, Query), {selected, ok, [[<<"0">>]]}};
|
|
|
|
only_count ->
|
|
|
|
{{selected, ok, []}, ejabberd_sql:sql_query(LServer, CountQuery)}
|
|
|
|
end,
|
|
|
|
case QRes of
|
2016-04-15 14:11:31 +02:00
|
|
|
{{selected, _, Res}, {selected, _, [[Count]]}} ->
|
2016-11-18 11:38:08 +01:00
|
|
|
{Max, Direction, _} = get_max_direction_id(RSM),
|
2016-04-15 14:11:31 +02:00
|
|
|
{Res1, IsComplete} =
|
2019-03-29 16:11:50 +01:00
|
|
|
if Max >= 0 andalso Max /= undefined andalso length(Res) > Max ->
|
|
|
|
if Direction == before ->
|
|
|
|
{lists:nthtail(1, Res), false};
|
|
|
|
true ->
|
|
|
|
{lists:sublist(Res, Max), false}
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
{Res, true}
|
|
|
|
end,
|
|
|
|
MucState = #state{config = #config{anonymous = true}},
|
|
|
|
JidArchiveS = jid:encode(JidArchive),
|
2016-04-15 14:11:31 +02:00
|
|
|
{lists:flatmap(
|
2019-03-29 16:11:50 +01:00
|
|
|
fun([TS, XML, PeerBin, Kind, Nick]) ->
|
|
|
|
case make_archive_el(JidArchiveS, TS, XML, PeerBin, Kind, Nick,
|
|
|
|
MsgType, JidRequestor, JidArchive) of
|
|
|
|
{ok, El} ->
|
|
|
|
[{TS, binary_to_integer(TS), El}];
|
|
|
|
{error, _} ->
|
|
|
|
[]
|
|
|
|
end;
|
|
|
|
([User, TS, XML, PeerBin, Kind, Nick]) when User == LUser ->
|
|
|
|
case make_archive_el(JidArchiveS, TS, XML, PeerBin, Kind, Nick,
|
|
|
|
MsgType, JidRequestor, JidArchive) of
|
2017-01-20 21:21:06 +01:00
|
|
|
{ok, El} ->
|
|
|
|
[{TS, binary_to_integer(TS), El}];
|
|
|
|
{error, _} ->
|
2016-04-15 14:11:31 +02:00
|
|
|
[]
|
2019-03-29 16:11:50 +01:00
|
|
|
end;
|
|
|
|
([User, TS, XML, PeerBin, Kind, Nick]) ->
|
|
|
|
case make_archive_el(User, TS, XML, PeerBin, Kind, Nick,
|
|
|
|
{groupchat, member, MucState}, JidRequestor,
|
|
|
|
jid:decode(User)) of
|
|
|
|
{ok, El} ->
|
|
|
|
mod_mam:wrap_as_mucsub([{TS, binary_to_integer(TS), El}],
|
|
|
|
JidRequestor);
|
|
|
|
{error, _} ->
|
|
|
|
[]
|
2016-04-15 14:11:31 +02:00
|
|
|
end
|
2019-03-29 16:11:50 +01:00
|
|
|
end, Res1), IsComplete, binary_to_integer(Count)};
|
2016-04-15 14:11:31 +02:00
|
|
|
_ ->
|
|
|
|
{[], false, 0}
|
|
|
|
end.
|
|
|
|
|
2017-05-07 14:58:11 +02:00
|
|
|
export(_Server) ->
|
|
|
|
[{archive_prefs,
|
|
|
|
fun(Host, #archive_prefs{us =
|
|
|
|
{LUser, LServer},
|
|
|
|
default = Default,
|
|
|
|
always = Always,
|
|
|
|
never = Never})
|
|
|
|
when LServer == Host ->
|
|
|
|
SDefault = erlang:atom_to_binary(Default, utf8),
|
|
|
|
SAlways = misc:term_to_expr(Always),
|
|
|
|
SNever = misc:term_to_expr(Never),
|
2017-11-02 15:03:30 +01:00
|
|
|
[?SQL_INSERT(
|
|
|
|
"archive_prefs",
|
|
|
|
["username=%(LUser)s",
|
|
|
|
"server_host=%(LServer)s",
|
|
|
|
"def=%(SDefault)s",
|
|
|
|
"always=%(SAlways)s",
|
|
|
|
"never=%(SNever)s"])];
|
2017-05-07 14:58:11 +02:00
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end},
|
|
|
|
{archive_msg,
|
2017-06-26 14:38:12 +02:00
|
|
|
fun(Host, #archive_msg{us ={LUser, LServer},
|
2017-05-07 14:58:11 +02:00
|
|
|
id = _ID, timestamp = TS, peer = Peer,
|
|
|
|
type = Type, nick = Nick, packet = Pkt})
|
|
|
|
when LServer == Host ->
|
|
|
|
TStmp = now_to_usec(TS),
|
|
|
|
SUser = case Type of
|
2017-06-26 14:38:12 +02:00
|
|
|
chat -> LUser;
|
|
|
|
groupchat -> jid:encode({LUser, LServer, <<>>})
|
2017-05-07 14:58:11 +02:00
|
|
|
end,
|
2017-05-10 11:17:12 +02:00
|
|
|
BarePeer = jid:encode(jid:tolower(jid:remove_resource(Peer))),
|
|
|
|
LPeer = jid:encode(jid:tolower(Peer)),
|
2017-05-07 14:58:11 +02:00
|
|
|
XML = fxml:element_to_binary(Pkt),
|
|
|
|
Body = fxml:get_subtag_cdata(Pkt, <<"body">>),
|
2017-05-10 12:05:52 +02:00
|
|
|
SType = misc:atom_to_binary(Type),
|
2017-11-02 15:03:30 +01:00
|
|
|
[?SQL_INSERT(
|
|
|
|
"archive",
|
|
|
|
["username=%(SUser)s",
|
|
|
|
"server_host=%(LServer)s",
|
|
|
|
"timestamp=%(TStmp)d",
|
|
|
|
"peer=%(LPeer)s",
|
|
|
|
"bare_peer=%(BarePeer)s",
|
|
|
|
"xml=%(XML)s",
|
|
|
|
"txt=%(Body)s",
|
|
|
|
"kind=%(SType)s",
|
|
|
|
"nick=%(Nick)s"])];
|
2017-05-07 14:58:11 +02:00
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end}].
|
|
|
|
|
2019-01-27 15:35:06 +01:00
|
|
|
is_empty_for_user(LUser, LServer) ->
|
|
|
|
case ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
2019-02-21 23:50:16 +01:00
|
|
|
?SQL("select @(1)d from archive"
|
|
|
|
" where username=%(LUser)s and %(LServer)H limit 1")) of
|
|
|
|
{selected, [{1}]} ->
|
|
|
|
false;
|
|
|
|
_ ->
|
|
|
|
true
|
|
|
|
end.
|
2019-01-27 15:35:06 +01:00
|
|
|
|
|
|
|
is_empty_for_room(LServer, LName, LHost) ->
|
|
|
|
LUser = jid:encode({LName, LHost, <<>>}),
|
|
|
|
is_empty_for_user(LUser, LServer).
|
|
|
|
|
2016-04-15 14:11:31 +02:00
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
|
|
|
now_to_usec({MSec, Sec, USec}) ->
|
|
|
|
(MSec*1000000 + Sec)*1000000 + USec.
|
|
|
|
|
|
|
|
usec_to_now(Int) ->
|
|
|
|
Secs = Int div 1000000,
|
|
|
|
USec = Int rem 1000000,
|
|
|
|
MSec = Secs div 1000000,
|
|
|
|
Sec = Secs rem 1000000,
|
|
|
|
{MSec, Sec, USec}.
|
|
|
|
|
2019-03-29 16:11:50 +01:00
|
|
|
make_sql_query(User, LServer, MAMQuery, RSM, ExtraUsernames) ->
|
2016-11-18 11:38:08 +01:00
|
|
|
Start = proplists:get_value(start, MAMQuery),
|
|
|
|
End = proplists:get_value('end', MAMQuery),
|
|
|
|
With = proplists:get_value(with, MAMQuery),
|
|
|
|
WithText = proplists:get_value(withtext, MAMQuery),
|
2016-07-25 12:50:30 +02:00
|
|
|
{Max, Direction, ID} = get_max_direction_id(RSM),
|
2019-06-14 11:33:26 +02:00
|
|
|
ODBCType = ejabberd_option:sql_type(LServer),
|
2016-05-24 17:44:07 +02:00
|
|
|
Escape =
|
|
|
|
case ODBCType of
|
|
|
|
mssql -> fun ejabberd_sql:standard_escape/1;
|
|
|
|
sqlite -> fun ejabberd_sql:standard_escape/1;
|
|
|
|
_ -> fun ejabberd_sql:escape/1
|
|
|
|
end,
|
2016-04-15 14:11:31 +02:00
|
|
|
LimitClause = if is_integer(Max), Max >= 0, ODBCType /= mssql ->
|
2016-09-24 22:34:28 +02:00
|
|
|
[<<" limit ">>, integer_to_binary(Max+1)];
|
2016-04-15 14:11:31 +02:00
|
|
|
true ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
TopClause = if is_integer(Max), Max >= 0, ODBCType == mssql ->
|
2016-09-24 22:34:28 +02:00
|
|
|
[<<" TOP ">>, integer_to_binary(Max+1)];
|
2016-04-15 14:11:31 +02:00
|
|
|
true ->
|
|
|
|
[]
|
|
|
|
end,
|
2016-09-13 15:56:34 +02:00
|
|
|
WithTextClause = if is_binary(WithText), WithText /= <<>> ->
|
2016-07-25 12:50:30 +02:00
|
|
|
[<<" and match (txt) against ('">>,
|
2016-09-13 15:56:34 +02:00
|
|
|
Escape(WithText), <<"')">>];
|
|
|
|
true ->
|
2016-07-25 12:50:30 +02:00
|
|
|
[]
|
|
|
|
end,
|
|
|
|
WithClause = case catch jid:tolower(With) of
|
2016-04-15 14:11:31 +02:00
|
|
|
{_, _, <<>>} ->
|
|
|
|
[<<" and bare_peer='">>,
|
2017-02-26 08:07:12 +01:00
|
|
|
Escape(jid:encode(With)),
|
2016-04-15 14:11:31 +02:00
|
|
|
<<"'">>];
|
|
|
|
{_, _, _} ->
|
|
|
|
[<<" and peer='">>,
|
2017-02-26 08:07:12 +01:00
|
|
|
Escape(jid:encode(With)),
|
2016-04-15 14:11:31 +02:00
|
|
|
<<"'">>];
|
2016-07-25 12:50:30 +02:00
|
|
|
_ ->
|
2016-04-15 14:11:31 +02:00
|
|
|
[]
|
|
|
|
end,
|
2016-09-24 22:34:28 +02:00
|
|
|
PageClause = case catch binary_to_integer(ID) of
|
2016-04-15 14:11:31 +02:00
|
|
|
I when is_integer(I), I >= 0 ->
|
|
|
|
case Direction of
|
|
|
|
before ->
|
|
|
|
[<<" AND timestamp < ">>, ID];
|
2016-07-25 12:50:30 +02:00
|
|
|
'after' ->
|
2016-04-15 14:11:31 +02:00
|
|
|
[<<" AND timestamp > ">>, ID];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
StartClause = case Start of
|
|
|
|
{_, _, _} ->
|
|
|
|
[<<" and timestamp >= ">>,
|
2016-09-24 22:34:28 +02:00
|
|
|
integer_to_binary(now_to_usec(Start))];
|
2016-04-15 14:11:31 +02:00
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
EndClause = case End of
|
|
|
|
{_, _, _} ->
|
|
|
|
[<<" and timestamp <= ">>,
|
2016-09-24 22:34:28 +02:00
|
|
|
integer_to_binary(now_to_usec(End))];
|
2016-04-15 14:11:31 +02:00
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
2016-05-24 17:44:07 +02:00
|
|
|
SUser = Escape(User),
|
2017-11-02 15:03:30 +01:00
|
|
|
SServer = Escape(LServer),
|
2016-04-15 14:11:31 +02:00
|
|
|
|
2019-03-29 16:11:50 +01:00
|
|
|
HostMatch = case ejabberd_sql:use_new_schema() of
|
|
|
|
true ->
|
2019-04-01 10:56:03 +02:00
|
|
|
[<<" and server_host='", SServer/binary, "'">>];
|
2019-03-29 16:11:50 +01:00
|
|
|
_ ->
|
|
|
|
<<"">>
|
|
|
|
end,
|
|
|
|
|
|
|
|
{UserSel, UserWhere} = case ExtraUsernames of
|
|
|
|
Users when is_list(Users) ->
|
2019-04-30 13:36:31 +02:00
|
|
|
EscUsers = [<<"'", (Escape(U))/binary, "'">> || U <- [User | Users]],
|
2019-03-29 16:11:50 +01:00
|
|
|
{<<" username,">>,
|
2019-04-11 12:00:13 +02:00
|
|
|
[<<" username in (">>, str:join(EscUsers, <<",">>), <<")">>]};
|
2019-03-29 16:11:50 +01:00
|
|
|
subscribers_table ->
|
2019-04-30 13:36:31 +02:00
|
|
|
SJid = Escape(jid:encode({User, LServer, <<>>})),
|
2019-04-30 18:33:12 +02:00
|
|
|
RoomName = case ODBCType of
|
|
|
|
sqlite ->
|
|
|
|
<<"room || '@' || host">>;
|
|
|
|
_ ->
|
|
|
|
<<"concat(room, '@', host)">>
|
|
|
|
end,
|
2019-03-29 16:11:50 +01:00
|
|
|
{<<" username,">>,
|
|
|
|
[<<" (username = '">>, SUser, <<"'">>,
|
2019-04-30 18:33:12 +02:00
|
|
|
<<" or username in (select ">>, RoomName,
|
|
|
|
<<" from muc_room_subscribers where jid='">>, SJid, <<"'">>, HostMatch, <<"))">>]};
|
2019-03-29 16:11:50 +01:00
|
|
|
_ ->
|
|
|
|
{<<>>, [<<" username='">>, SUser, <<"'">>]}
|
|
|
|
end,
|
|
|
|
|
|
|
|
Query = [<<"SELECT ">>, TopClause, UserSel,
|
|
|
|
<<" timestamp, xml, peer, kind, nick"
|
|
|
|
" FROM archive WHERE">>, UserWhere, HostMatch,
|
|
|
|
WithClause, WithTextClause,
|
|
|
|
StartClause, EndClause, PageClause],
|
2016-04-15 14:11:31 +02:00
|
|
|
|
|
|
|
QueryPage =
|
|
|
|
case Direction of
|
|
|
|
before ->
|
|
|
|
% ID can be empty because of
|
|
|
|
% XEP-0059: Result Set Management
|
|
|
|
% 2.5 Requesting the Last Page in a Result Set
|
2019-03-29 16:11:50 +01:00
|
|
|
[<<"SELECT">>, UserSel, <<" timestamp, xml, peer, kind, nick FROM (">>,
|
|
|
|
Query, <<" ORDER BY timestamp DESC ">>,
|
2016-04-15 14:11:31 +02:00
|
|
|
LimitClause, <<") AS t ORDER BY timestamp ASC;">>];
|
|
|
|
_ ->
|
|
|
|
[Query, <<" ORDER BY timestamp ASC ">>,
|
|
|
|
LimitClause, <<";">>]
|
|
|
|
end,
|
2019-03-29 16:11:50 +01:00
|
|
|
{QueryPage,
|
|
|
|
[<<"SELECT COUNT(*) FROM archive WHERE ">>, UserWhere,
|
|
|
|
HostMatch, WithClause, WithTextClause,
|
|
|
|
StartClause, EndClause, <<";">>]}.
|
2016-07-25 12:50:30 +02:00
|
|
|
|
|
|
|
-spec get_max_direction_id(rsm_set() | undefined) ->
|
|
|
|
{integer() | undefined,
|
|
|
|
before | 'after' | undefined,
|
|
|
|
binary()}.
|
|
|
|
get_max_direction_id(RSM) ->
|
|
|
|
case RSM of
|
|
|
|
#rsm_set{max = Max, before = Before} when is_binary(Before) ->
|
|
|
|
{Max, before, Before};
|
|
|
|
#rsm_set{max = Max, 'after' = After} when is_binary(After) ->
|
|
|
|
{Max, 'after', After};
|
|
|
|
#rsm_set{max = Max} ->
|
|
|
|
{Max, undefined, <<>>};
|
|
|
|
_ ->
|
|
|
|
{undefined, undefined, <<>>}
|
|
|
|
end.
|
2017-01-20 21:21:06 +01:00
|
|
|
|
2018-11-28 11:25:04 +01:00
|
|
|
-spec make_archive_el(binary(), binary(), binary(), binary(), binary(),
|
2017-01-20 21:21:06 +01:00
|
|
|
binary(), _, jid(), jid()) ->
|
|
|
|
{ok, xmpp_element()} | {error, invalid_jid |
|
|
|
|
invalid_timestamp |
|
|
|
|
invalid_xml}.
|
2018-11-28 11:25:04 +01:00
|
|
|
make_archive_el(User, TS, XML, Peer, Kind, Nick, MsgType, JidRequestor, JidArchive) ->
|
|
|
|
case xml_compress:decode(XML, User, Peer) of
|
2017-01-20 21:21:06 +01:00
|
|
|
#xmlel{} = El ->
|
|
|
|
try binary_to_integer(TS) of
|
|
|
|
TSInt ->
|
2017-02-26 08:07:12 +01:00
|
|
|
try jid:decode(Peer) of
|
|
|
|
PeerJID ->
|
2017-01-20 21:21:06 +01:00
|
|
|
Now = usec_to_now(TSInt),
|
|
|
|
PeerLJID = jid:tolower(PeerJID),
|
|
|
|
T = case Kind of
|
|
|
|
<<"">> -> chat;
|
|
|
|
null -> chat;
|
2017-04-11 12:13:58 +02:00
|
|
|
_ -> misc:binary_to_atom(Kind)
|
2017-01-20 21:21:06 +01:00
|
|
|
end,
|
|
|
|
mod_mam:msg_to_el(
|
|
|
|
#archive_msg{timestamp = Now,
|
|
|
|
id = TS,
|
|
|
|
packet = El,
|
|
|
|
type = T,
|
|
|
|
nick = Nick,
|
|
|
|
peer = PeerLJID},
|
2017-02-26 08:07:12 +01:00
|
|
|
MsgType, JidRequestor, JidArchive)
|
|
|
|
catch _:{bad_jid, _} ->
|
2017-01-20 21:21:06 +01:00
|
|
|
?ERROR_MSG("Malformed 'peer' field with value "
|
2019-09-23 14:17:20 +02:00
|
|
|
"'~ts' detected for user ~ts in table "
|
2017-01-20 21:21:06 +01:00
|
|
|
"'archive': invalid JID",
|
2017-02-26 08:07:12 +01:00
|
|
|
[Peer, jid:encode(JidArchive)]),
|
2017-01-20 21:21:06 +01:00
|
|
|
{error, invalid_jid}
|
|
|
|
end
|
|
|
|
catch _:_ ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Malformed 'timestamp' field with value '~ts' "
|
|
|
|
"detected for user ~ts in table 'archive': "
|
2017-01-20 21:21:06 +01:00
|
|
|
"not an integer",
|
2017-02-26 08:07:12 +01:00
|
|
|
[TS, jid:encode(JidArchive)]),
|
2017-01-20 21:21:06 +01:00
|
|
|
{error, invalid_timestamp}
|
|
|
|
end;
|
|
|
|
{error, {_, Reason}} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Malformed 'xml' field with value '~ts' detected "
|
|
|
|
"for user ~ts in table 'archive': ~ts",
|
2017-02-26 08:07:12 +01:00
|
|
|
[XML, jid:encode(JidArchive), Reason]),
|
2017-01-20 21:21:06 +01:00
|
|
|
{error, invalid_xml}
|
|
|
|
end.
|