2016-04-14 11:18:04 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
2016-12-27 10:44:07 +01:00
|
|
|
%%% File : mod_irc_sql.erl
|
|
|
|
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2016-04-14 11:18:04 +02:00
|
|
|
%%% Created : 14 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2016-12-27 10:44:07 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2018-01-05 21:18:58 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2018 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-14 11:18:04 +02:00
|
|
|
-module(mod_irc_sql).
|
|
|
|
|
2016-05-04 20:01:05 +02:00
|
|
|
-compile([{parse_transform, ejabberd_sql_pt}]).
|
|
|
|
|
2016-04-14 11:18:04 +02:00
|
|
|
-behaviour(mod_irc).
|
|
|
|
|
|
|
|
%% API
|
|
|
|
-export([init/2, get_data/3, set_data/4, import/1, import/2, export/1]).
|
|
|
|
|
2016-08-03 19:57:05 +02:00
|
|
|
-include("jid.hrl").
|
2016-04-14 11:18:04 +02:00
|
|
|
-include("mod_irc.hrl").
|
2016-05-04 20:01:05 +02:00
|
|
|
-include("ejabberd_sql_pt.hrl").
|
2016-04-14 11:18:04 +02:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
init(_Host, _Opts) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
get_data(LServer, Host, From) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
SJID = jid:encode(jid:tolower(jid:remove_resource(From))),
|
2016-04-20 11:27:32 +02:00
|
|
|
case catch ejabberd_sql:sql_query(
|
2016-05-04 20:01:05 +02:00
|
|
|
LServer,
|
|
|
|
?SQL("select @(data)s from irc_custom"
|
2017-11-02 15:03:30 +01:00
|
|
|
" where jid=%(SJID)s and host=%(Host)s and %(LServer)H")) of
|
2016-05-04 20:01:05 +02:00
|
|
|
{selected, [{SData}]} ->
|
|
|
|
mod_irc:data_to_binary(From, ejabberd_sql:decode_term(SData));
|
|
|
|
{'EXIT', _} -> error;
|
|
|
|
{selected, _} -> empty
|
2016-04-14 11:18:04 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
set_data(LServer, Host, From, Data) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
SJID = jid:encode(jid:tolower(jid:remove_resource(From))),
|
2017-04-11 12:13:58 +02:00
|
|
|
SData = misc:term_to_expr(Data),
|
2016-04-14 11:18:04 +02:00
|
|
|
F = fun () ->
|
2016-05-04 20:01:05 +02:00
|
|
|
?SQL_UPSERT_T(
|
|
|
|
"irc_custom",
|
|
|
|
["!jid=%(SJID)s",
|
|
|
|
"!host=%(Host)s",
|
2017-11-02 15:03:30 +01:00
|
|
|
"server_host=%(LServer)s",
|
2016-05-04 20:01:05 +02:00
|
|
|
"data=%(SData)s"]),
|
2016-04-14 11:18:04 +02:00
|
|
|
ok
|
|
|
|
end,
|
2016-04-20 11:27:32 +02:00
|
|
|
ejabberd_sql:sql_transaction(LServer, F).
|
2016-04-14 11:18:04 +02:00
|
|
|
|
|
|
|
export(_Server) ->
|
|
|
|
[{irc_custom,
|
|
|
|
fun(Host, #irc_custom{us_host = {{U, S}, IRCHost},
|
|
|
|
data = Data}) ->
|
|
|
|
case str:suffix(Host, IRCHost) of
|
|
|
|
true ->
|
2017-02-26 08:07:12 +01:00
|
|
|
SJID = jid:encode(jid:make(U, S)),
|
2017-11-02 15:03:30 +01:00
|
|
|
LServer = ejabberd_router:host_of_route(IRCHost),
|
2017-04-11 12:13:58 +02:00
|
|
|
SData = misc:term_to_expr(Data),
|
2016-05-04 20:01:05 +02:00
|
|
|
[?SQL("delete from irc_custom"
|
2017-11-02 15:03:30 +01:00
|
|
|
" where jid=%(SJID)s and host=%(IRCHost)s and %(LServer)H;"),
|
|
|
|
?SQL_INSERT(
|
|
|
|
"irc_custom",
|
|
|
|
["jid=%(SJID)s",
|
|
|
|
"host=%(Host)s",
|
|
|
|
"server_host=%(LServer)s",
|
|
|
|
"data=%(SData)s"])];
|
2016-04-14 11:18:04 +02:00
|
|
|
false ->
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end}].
|
|
|
|
|
|
|
|
import(_LServer) ->
|
|
|
|
[{<<"select jid, host, data from irc_custom;">>,
|
|
|
|
fun([SJID, IRCHost, SData]) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
#jid{luser = U, lserver = S} = jid:decode(SJID),
|
2016-04-20 11:27:32 +02:00
|
|
|
Data = ejabberd_sql:decode_term(SData),
|
2016-04-14 11:18:04 +02:00
|
|
|
#irc_custom{us_host = {{U, S}, IRCHost},
|
|
|
|
data = Data}
|
|
|
|
end}].
|
|
|
|
|
|
|
|
import(_, _) ->
|
|
|
|
pass.
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|