2016-04-13 12:04:04 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
2016-12-27 10:44:07 +01:00
|
|
|
%%% File : mod_announce_sql.erl
|
|
|
|
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2016-04-13 12:04:04 +02:00
|
|
|
%%% Created : 13 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-13 12:04:04 +02:00
|
|
|
-module(mod_announce_sql).
|
2016-12-27 10:44:07 +01:00
|
|
|
|
2016-04-13 12:04:04 +02:00
|
|
|
-behaviour(mod_announce).
|
|
|
|
|
2016-05-04 20:01:05 +02:00
|
|
|
-compile([{parse_transform, ejabberd_sql_pt}]).
|
|
|
|
|
2016-04-13 12:04:04 +02:00
|
|
|
%% API
|
|
|
|
-export([init/2, set_motd_users/2, set_motd/2, delete_motd/1,
|
2016-11-22 14:48:01 +01:00
|
|
|
get_motd/1, is_motd_user/2, set_motd_user/2, import/3,
|
|
|
|
export/1]).
|
2016-04-13 12:04:04 +02:00
|
|
|
|
2016-07-26 10:29:17 +02:00
|
|
|
-include("xmpp.hrl").
|
2016-04-13 12:04:04 +02:00
|
|
|
-include("mod_announce.hrl").
|
2016-05-04 20:01:05 +02:00
|
|
|
-include("ejabberd_sql_pt.hrl").
|
2017-05-22 15:14:28 +02:00
|
|
|
-include("logger.hrl").
|
2016-04-13 12:04:04 +02:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
init(_Host, _Opts) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
set_motd_users(LServer, USRs) ->
|
|
|
|
F = fun() ->
|
|
|
|
lists:foreach(
|
|
|
|
fun({U, _S, _R}) ->
|
2016-05-04 20:01:05 +02:00
|
|
|
?SQL_UPSERT_T(
|
|
|
|
"motd",
|
|
|
|
["!username=%(U)s",
|
2017-11-02 15:03:30 +01:00
|
|
|
"!server_host=%(LServer)s",
|
2016-05-04 20:01:05 +02:00
|
|
|
"xml=''"])
|
2016-04-13 12:04:04 +02:00
|
|
|
end, USRs)
|
|
|
|
end,
|
2017-05-22 15:14:28 +02:00
|
|
|
transaction(LServer, F).
|
2016-04-13 12:04:04 +02:00
|
|
|
|
|
|
|
set_motd(LServer, Packet) ->
|
2016-05-04 20:01:05 +02:00
|
|
|
XML = fxml:element_to_binary(Packet),
|
2016-04-13 12:04:04 +02:00
|
|
|
F = fun() ->
|
2016-05-04 20:01:05 +02:00
|
|
|
?SQL_UPSERT_T(
|
|
|
|
"motd",
|
|
|
|
["!username=''",
|
2017-11-02 15:03:30 +01:00
|
|
|
"!server_host=%(LServer)s",
|
2016-05-04 20:01:05 +02:00
|
|
|
"xml=%(XML)s"])
|
2016-04-13 12:04:04 +02:00
|
|
|
end,
|
2017-05-22 15:14:28 +02:00
|
|
|
transaction(LServer, F).
|
2016-04-13 12:04:04 +02:00
|
|
|
|
|
|
|
delete_motd(LServer) ->
|
|
|
|
F = fun() ->
|
2017-11-02 15:03:30 +01:00
|
|
|
ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("delete from motd where %(LServer)H"))
|
2016-04-13 12:04:04 +02:00
|
|
|
end,
|
2017-05-22 15:14:28 +02:00
|
|
|
transaction(LServer, F).
|
2016-04-13 12:04:04 +02:00
|
|
|
|
|
|
|
get_motd(LServer) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
case catch ejabberd_sql:sql_query(
|
2016-05-04 20:01:05 +02:00
|
|
|
LServer,
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL("select @(xml)s from motd"
|
|
|
|
" where username='' and %(LServer)H")) of
|
2016-05-04 20:01:05 +02:00
|
|
|
{selected, [{XML}]} ->
|
2017-05-22 15:14:28 +02:00
|
|
|
parse_element(XML);
|
|
|
|
{selected, []} ->
|
|
|
|
error;
|
2016-04-13 12:04:04 +02:00
|
|
|
_ ->
|
2017-05-22 15:14:28 +02:00
|
|
|
{error, db_failure}
|
2016-04-13 12:04:04 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
is_motd_user(LUser, LServer) ->
|
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 @(username)s from motd"
|
2017-11-02 15:03:30 +01:00
|
|
|
" where username=%(LUser)s and %(LServer)H")) of
|
2016-05-04 20:01:05 +02:00
|
|
|
{selected, [_|_]} ->
|
2017-05-22 15:14:28 +02:00
|
|
|
{ok, true};
|
|
|
|
{selected, []} ->
|
|
|
|
{ok, false};
|
2016-04-13 12:04:04 +02:00
|
|
|
_ ->
|
2017-05-22 15:14:28 +02:00
|
|
|
{error, db_failure}
|
2016-04-13 12:04:04 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
set_motd_user(LUser, LServer) ->
|
|
|
|
F = fun() ->
|
2016-05-04 20:01:05 +02:00
|
|
|
?SQL_UPSERT_T(
|
|
|
|
"motd",
|
|
|
|
["!username=%(LUser)s",
|
2017-11-02 15:03:30 +01:00
|
|
|
"!server_host=%(LServer)s",
|
2016-05-04 20:01:05 +02:00
|
|
|
"xml=''"])
|
|
|
|
end,
|
2017-05-22 15:14:28 +02:00
|
|
|
transaction(LServer, F).
|
2016-04-13 12:04:04 +02:00
|
|
|
|
|
|
|
export(_Server) ->
|
|
|
|
[{motd,
|
|
|
|
fun(Host, #motd{server = LServer, packet = El})
|
|
|
|
when LServer == Host ->
|
2016-05-04 20:01:05 +02:00
|
|
|
XML = fxml:element_to_binary(El),
|
2017-11-02 15:03:30 +01:00
|
|
|
[?SQL("delete from motd where username='' and %(LServer)H;"),
|
|
|
|
?SQL_INSERT(
|
|
|
|
"motd",
|
|
|
|
["username=''",
|
|
|
|
"server_host=%(LServer)s",
|
|
|
|
"xml=%(XML)s"])];
|
2016-04-13 12:04:04 +02:00
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end},
|
|
|
|
{motd_users,
|
|
|
|
fun(Host, #motd_users{us = {LUser, LServer}})
|
|
|
|
when LServer == Host, LUser /= <<"">> ->
|
2017-11-02 15:03:30 +01:00
|
|
|
[?SQL("delete from motd where username=%(LUser)s and %(LServer)H;"),
|
|
|
|
?SQL_INSERT(
|
|
|
|
"motd",
|
|
|
|
["username=%(LUser)s",
|
|
|
|
"server_host=%(LServer)s",
|
|
|
|
"xml=''"])];
|
2016-04-13 12:04:04 +02:00
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end}].
|
|
|
|
|
2016-11-22 14:48:01 +01:00
|
|
|
import(_, _, _) ->
|
|
|
|
ok.
|
2016-04-13 12:04:04 +02:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
2017-05-22 15:14:28 +02:00
|
|
|
transaction(LServer, F) ->
|
|
|
|
case ejabberd_sql:sql_transaction(LServer, F) of
|
|
|
|
{atomic, _} -> ok;
|
|
|
|
_ -> {error, db_failure}
|
|
|
|
end.
|
|
|
|
|
|
|
|
parse_element(XML) ->
|
|
|
|
case fxml_stream:parse_element(XML) of
|
|
|
|
El when is_record(El, xmlel) ->
|
|
|
|
{ok, El};
|
|
|
|
_ ->
|
|
|
|
?ERROR_MSG("malformed XML element in SQL table "
|
|
|
|
"'motd' for username='': ~s", [XML]),
|
|
|
|
{error, db_failure}
|
|
|
|
end.
|