2016-04-13 12:04:04 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
2016-12-27 10:44:07 +01:00
|
|
|
%%% File : mod_announce_mnesia.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
|
|
|
%%%
|
|
|
|
%%%
|
2021-01-27 16:55:25 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2021 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_mnesia).
|
2016-12-27 10:44:07 +01:00
|
|
|
|
2016-04-13 12:04:04 +02:00
|
|
|
-behaviour(mod_announce).
|
|
|
|
|
|
|
|
%% 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]).
|
2017-04-23 15:37:58 +02:00
|
|
|
-export([need_transform/1, transform/1]).
|
2016-04-13 12:04:04 +02:00
|
|
|
|
2020-09-03 13:45:57 +02:00
|
|
|
-include_lib("xmpp/include/xmpp.hrl").
|
2016-04-13 12:04:04 +02:00
|
|
|
-include("mod_announce.hrl").
|
|
|
|
-include("logger.hrl").
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
init(_Host, _Opts) ->
|
2016-11-30 11:09:17 +01:00
|
|
|
ejabberd_mnesia:create(?MODULE, motd,
|
2017-05-22 15:14:28 +02:00
|
|
|
[{disc_only_copies, [node()]},
|
2016-04-13 12:04:04 +02:00
|
|
|
{attributes,
|
|
|
|
record_info(fields, motd)}]),
|
2016-11-30 11:09:17 +01:00
|
|
|
ejabberd_mnesia:create(?MODULE, motd_users,
|
2017-05-22 15:14:28 +02:00
|
|
|
[{disc_only_copies, [node()]},
|
2016-04-13 12:04:04 +02:00
|
|
|
{attributes,
|
2017-04-23 15:37:58 +02:00
|
|
|
record_info(fields, motd_users)}]).
|
2016-04-13 12:04:04 +02:00
|
|
|
|
|
|
|
set_motd_users(_LServer, USRs) ->
|
|
|
|
F = fun() ->
|
|
|
|
lists:foreach(
|
|
|
|
fun({U, S, _R}) ->
|
|
|
|
mnesia:write(#motd_users{us = {U, S}})
|
|
|
|
end, USRs)
|
|
|
|
end,
|
2017-05-22 15:14:28 +02:00
|
|
|
transaction(F).
|
2016-04-13 12:04:04 +02:00
|
|
|
|
|
|
|
set_motd(LServer, Packet) ->
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:write(#motd{server = LServer, packet = Packet})
|
|
|
|
end,
|
2017-05-22 15:14:28 +02:00
|
|
|
transaction(F).
|
2016-04-13 12:04:04 +02:00
|
|
|
|
|
|
|
delete_motd(LServer) ->
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:delete({motd, LServer}),
|
|
|
|
mnesia:write_lock_table(motd_users),
|
|
|
|
Users = mnesia:select(
|
|
|
|
motd_users,
|
|
|
|
[{#motd_users{us = '$1', _ = '_'},
|
|
|
|
[{'==', {element, 2, '$1'}, LServer}],
|
|
|
|
['$1']}]),
|
|
|
|
lists:foreach(fun(US) ->
|
|
|
|
mnesia:delete({motd_users, US})
|
|
|
|
end, Users)
|
|
|
|
end,
|
2017-05-22 15:14:28 +02:00
|
|
|
transaction(F).
|
2016-04-13 12:04:04 +02:00
|
|
|
|
|
|
|
get_motd(LServer) ->
|
|
|
|
case mnesia:dirty_read({motd, LServer}) of
|
|
|
|
[#motd{packet = Packet}] ->
|
|
|
|
{ok, Packet};
|
2017-05-22 15:14:28 +02:00
|
|
|
[] ->
|
2016-04-13 12:04:04 +02:00
|
|
|
error
|
|
|
|
end.
|
|
|
|
|
|
|
|
is_motd_user(LUser, LServer) ->
|
|
|
|
case mnesia:dirty_read({motd_users, {LUser, LServer}}) of
|
2017-05-22 15:14:28 +02:00
|
|
|
[#motd_users{}] -> {ok, true};
|
|
|
|
_ -> {ok, false}
|
2016-04-13 12:04:04 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
set_motd_user(LUser, LServer) ->
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:write(#motd_users{us = {LUser, LServer}})
|
|
|
|
end,
|
2017-05-22 15:14:28 +02:00
|
|
|
transaction(F).
|
2016-04-13 12:04:04 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
need_transform({motd, S, _}) when is_list(S) ->
|
2017-04-23 15:37:58 +02:00
|
|
|
?INFO_MSG("Mnesia table 'motd' will be converted to binary", []),
|
|
|
|
true;
|
2019-06-14 11:33:26 +02:00
|
|
|
need_transform({motd_users, {U, S}, _}) when is_list(U) orelse is_list(S) ->
|
2017-04-23 15:37:58 +02:00
|
|
|
?INFO_MSG("Mnesia table 'motd_users' will be converted to binary", []),
|
|
|
|
true;
|
|
|
|
need_transform(_) ->
|
|
|
|
false.
|
|
|
|
|
|
|
|
transform(#motd{server = S, packet = P} = R) ->
|
|
|
|
NewS = iolist_to_binary(S),
|
|
|
|
NewP = fxml:to_xmlel(P),
|
|
|
|
R#motd{server = NewS, packet = NewP};
|
|
|
|
transform(#motd_users{us = {U, S}} = R) ->
|
|
|
|
NewUS = {iolist_to_binary(U), iolist_to_binary(S)},
|
|
|
|
R#motd_users{us = NewUS}.
|
|
|
|
|
2016-11-22 14:48:01 +01:00
|
|
|
import(LServer, <<"motd">>, [<<>>, XML, _TimeStamp]) ->
|
|
|
|
El = fxml_stream:parse_element(XML),
|
|
|
|
mnesia:dirty_write(#motd{server = LServer, packet = El});
|
|
|
|
import(LServer, <<"motd">>, [LUser, <<>>, _TimeStamp]) ->
|
|
|
|
mnesia:dirty_write(#motd_users{us = {LUser, LServer}}).
|
2016-04-13 12:04:04 +02:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
2017-05-22 15:14:28 +02:00
|
|
|
transaction(F) ->
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, Res} ->
|
|
|
|
Res;
|
|
|
|
{aborted, Reason} ->
|
|
|
|
?ERROR_MSG("Mnesia transaction failed: ~p", [Reason]),
|
|
|
|
{error, db_failure}
|
|
|
|
end.
|