2016-04-13 20:07:32 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
2016-12-27 10:44:07 +01:00
|
|
|
%%% File : mod_muc_sql.erl
|
|
|
|
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2016-04-13 20:07:32 +02:00
|
|
|
%%% Created : 13 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2016-12-27 10:44:07 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2017-01-02 21:41:53 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2017 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 20:07:32 +02:00
|
|
|
-module(mod_muc_sql).
|
|
|
|
|
2016-05-04 20:01:05 +02:00
|
|
|
-compile([{parse_transform, ejabberd_sql_pt}]).
|
|
|
|
|
2016-04-13 20:07:32 +02:00
|
|
|
-behaviour(mod_muc).
|
2016-11-22 14:48:01 +01:00
|
|
|
-behaviour(mod_muc_room).
|
2016-04-13 20:07:32 +02:00
|
|
|
|
|
|
|
%% API
|
|
|
|
-export([init/2, store_room/4, restore_room/3, forget_room/3,
|
|
|
|
can_use_nick/4, get_rooms/2, get_nick/3, set_nick/4,
|
2016-11-22 14:48:01 +01:00
|
|
|
import/3, export/1]).
|
2017-01-13 10:03:39 +01:00
|
|
|
-export([register_online_room/3, unregister_online_room/3, find_online_room/2,
|
|
|
|
get_online_rooms/2, count_online_rooms/1, rsm_supported/0,
|
|
|
|
register_online_user/3, unregister_online_user/3,
|
2017-01-16 09:34:49 +01:00
|
|
|
count_online_rooms_by_user/2, get_online_rooms_by_user/2]).
|
2016-11-22 14:48:01 +01:00
|
|
|
-export([set_affiliation/6, set_affiliations/4, get_affiliation/5,
|
|
|
|
get_affiliations/3, search_affiliation/4]).
|
2016-04-13 20:07:32 +02:00
|
|
|
|
2016-07-26 10:29:17 +02:00
|
|
|
-include("jid.hrl").
|
2016-04-13 20:07:32 +02:00
|
|
|
-include("mod_muc.hrl").
|
|
|
|
-include("logger.hrl").
|
2016-05-04 20:01:05 +02:00
|
|
|
-include("ejabberd_sql_pt.hrl").
|
2016-04-13 20:07:32 +02:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
init(_Host, _Opts) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
store_room(LServer, Host, Name, Opts) ->
|
2016-05-04 20:01:05 +02:00
|
|
|
SOpts = jlib:term_to_expr(Opts),
|
2016-04-13 20:07:32 +02:00
|
|
|
F = fun () ->
|
2016-05-04 20:01:05 +02:00
|
|
|
?SQL_UPSERT_T(
|
|
|
|
"muc_room",
|
|
|
|
["!name=%(Name)s",
|
|
|
|
"!host=%(Host)s",
|
|
|
|
"opts=%(SOpts)s"])
|
2016-04-13 20:07:32 +02:00
|
|
|
end,
|
2016-04-20 11:27:32 +02:00
|
|
|
ejabberd_sql:sql_transaction(LServer, F).
|
2016-04-13 20:07:32 +02:00
|
|
|
|
|
|
|
restore_room(LServer, Host, Name) ->
|
2016-05-04 20:01:05 +02:00
|
|
|
case catch ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
|
|
|
?SQL("select @(opts)s from muc_room where name=%(Name)s"
|
|
|
|
" and host=%(Host)s")) of
|
|
|
|
{selected, [{Opts}]} ->
|
2016-04-20 11:27:32 +02:00
|
|
|
mod_muc:opts_to_binary(ejabberd_sql:decode_term(Opts));
|
2016-04-13 20:07:32 +02:00
|
|
|
_ ->
|
|
|
|
error
|
|
|
|
end.
|
|
|
|
|
|
|
|
forget_room(LServer, Host, Name) ->
|
|
|
|
F = fun () ->
|
2016-05-04 20:01:05 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("delete from muc_room where name=%(Name)s"
|
|
|
|
" and host=%(Host)s"))
|
2016-04-13 20:07:32 +02:00
|
|
|
end,
|
2016-04-20 11:27:32 +02:00
|
|
|
ejabberd_sql:sql_transaction(LServer, F).
|
2016-04-13 20:07:32 +02:00
|
|
|
|
|
|
|
can_use_nick(LServer, Host, JID, Nick) ->
|
|
|
|
SJID = jid:to_string(jid:tolower(jid:remove_resource(JID))),
|
2016-05-04 20:01:05 +02:00
|
|
|
case catch ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
|
|
|
?SQL("select @(jid)s from muc_registered "
|
|
|
|
"where nick=%(Nick)s"
|
|
|
|
" and host=%(Host)s")) of
|
|
|
|
{selected, [{SJID1}]} -> SJID == SJID1;
|
2016-04-13 20:07:32 +02:00
|
|
|
_ -> true
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_rooms(LServer, Host) ->
|
2016-05-04 20:01:05 +02:00
|
|
|
case catch ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
|
|
|
?SQL("select @(name)s, @(opts)s from muc_room"
|
|
|
|
" where host=%(Host)s")) of
|
|
|
|
{selected, RoomOpts} ->
|
2016-04-13 20:07:32 +02:00
|
|
|
lists:map(
|
2016-05-04 20:01:05 +02:00
|
|
|
fun({Room, Opts}) ->
|
2016-04-13 20:07:32 +02:00
|
|
|
#muc_room{name_host = {Room, Host},
|
|
|
|
opts = mod_muc:opts_to_binary(
|
2016-04-20 11:27:32 +02:00
|
|
|
ejabberd_sql:decode_term(Opts))}
|
2016-04-13 20:07:32 +02:00
|
|
|
end, RoomOpts);
|
|
|
|
Err ->
|
|
|
|
?ERROR_MSG("failed to get rooms: ~p", [Err]),
|
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_nick(LServer, Host, From) ->
|
2016-05-04 20:01:05 +02:00
|
|
|
SJID = jid:to_string(jid:tolower(jid:remove_resource(From))),
|
|
|
|
case catch ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
|
|
|
?SQL("select @(nick)s from muc_registered where"
|
|
|
|
" jid=%(SJID)s and host=%(Host)s")) of
|
|
|
|
{selected, [{Nick}]} -> Nick;
|
2016-04-13 20:07:32 +02:00
|
|
|
_ -> error
|
|
|
|
end.
|
|
|
|
|
|
|
|
set_nick(LServer, Host, From, Nick) ->
|
|
|
|
JID = jid:to_string(jid:tolower(jid:remove_resource(From))),
|
|
|
|
F = fun () ->
|
|
|
|
case Nick of
|
|
|
|
<<"">> ->
|
2016-04-20 11:27:32 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
2016-05-04 20:01:05 +02:00
|
|
|
?SQL("delete from muc_registered where"
|
|
|
|
" jid=%(JID)s and host=%(Host)s")),
|
2016-04-13 20:07:32 +02:00
|
|
|
ok;
|
|
|
|
_ ->
|
2016-04-20 11:27:32 +02:00
|
|
|
Allow = case ejabberd_sql:sql_query_t(
|
2016-05-04 20:01:05 +02:00
|
|
|
?SQL("select @(jid)s from muc_registered"
|
|
|
|
" where nick=%(Nick)s"
|
|
|
|
" and host=%(Host)s")) of
|
|
|
|
{selected, [{J}]} -> J == JID;
|
2016-04-13 20:07:32 +02:00
|
|
|
_ -> true
|
|
|
|
end,
|
|
|
|
if Allow ->
|
2016-05-04 20:01:05 +02:00
|
|
|
?SQL_UPSERT_T(
|
|
|
|
"muc_registered",
|
|
|
|
["!jid=%(JID)s",
|
|
|
|
"!host=%(Host)s",
|
|
|
|
"nick=%(Nick)s"]),
|
2016-04-13 20:07:32 +02:00
|
|
|
ok;
|
|
|
|
true ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
2016-04-20 11:27:32 +02:00
|
|
|
ejabberd_sql:sql_transaction(LServer, F).
|
2016-04-13 20:07:32 +02:00
|
|
|
|
2016-11-22 14:48:01 +01:00
|
|
|
set_affiliation(_ServerHost, _Room, _Host, _JID, _Affiliation, _Reason) ->
|
|
|
|
{error, not_implemented}.
|
|
|
|
|
|
|
|
set_affiliations(_ServerHost, _Room, _Host, _Affiliations) ->
|
|
|
|
{error, not_implemented}.
|
|
|
|
|
|
|
|
get_affiliation(_ServerHost, _Room, _Host, _LUser, _LServer) ->
|
|
|
|
{error, not_implemented}.
|
|
|
|
|
|
|
|
get_affiliations(_ServerHost, _Room, _Host) ->
|
|
|
|
{error, not_implemented}.
|
|
|
|
|
|
|
|
search_affiliation(_ServerHost, _Room, _Host, _Affiliation) ->
|
|
|
|
{error, not_implemented}.
|
|
|
|
|
2017-01-13 10:03:39 +01:00
|
|
|
register_online_room(_, _, _) ->
|
|
|
|
erlang:error(not_implemented).
|
|
|
|
|
|
|
|
unregister_online_room(_, _, _) ->
|
|
|
|
erlang:error(not_implemented).
|
|
|
|
|
|
|
|
find_online_room(_, _) ->
|
|
|
|
erlang:error(not_implemented).
|
|
|
|
|
|
|
|
count_online_rooms(_) ->
|
|
|
|
erlang:error(not_implemented).
|
|
|
|
|
|
|
|
get_online_rooms(_, _) ->
|
|
|
|
erlang:error(not_implemented).
|
|
|
|
|
|
|
|
rsm_supported() ->
|
|
|
|
false.
|
|
|
|
|
|
|
|
register_online_user(_, _, _) ->
|
|
|
|
erlang:error(not_implemented).
|
|
|
|
|
|
|
|
unregister_online_user(_, _, _) ->
|
|
|
|
erlang:error(not_implemented).
|
|
|
|
|
|
|
|
count_online_rooms_by_user(_, _) ->
|
|
|
|
erlang:error(not_implemented).
|
|
|
|
|
|
|
|
get_online_rooms_by_user(_, _) ->
|
|
|
|
erlang:error(not_implemented).
|
|
|
|
|
2016-04-13 20:07:32 +02:00
|
|
|
export(_Server) ->
|
|
|
|
[{muc_room,
|
|
|
|
fun(Host, #muc_room{name_host = {Name, RoomHost}, opts = Opts}) ->
|
|
|
|
case str:suffix(Host, RoomHost) of
|
|
|
|
true ->
|
2016-05-04 20:01:05 +02:00
|
|
|
SOpts = jlib:term_to_expr(Opts),
|
|
|
|
[?SQL("delete from muc_room where name=%(Name)s"
|
|
|
|
" and host=%(RoomHost)s;"),
|
|
|
|
?SQL("insert into muc_room(name, host, opts) "
|
|
|
|
"values ("
|
|
|
|
"%(Name)s, %(RoomHost)s, %(SOpts)s);")];
|
2016-04-13 20:07:32 +02:00
|
|
|
false ->
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end},
|
|
|
|
{muc_registered,
|
|
|
|
fun(Host, #muc_registered{us_host = {{U, S}, RoomHost},
|
|
|
|
nick = Nick}) ->
|
|
|
|
case str:suffix(Host, RoomHost) of
|
|
|
|
true ->
|
2016-05-04 20:01:05 +02:00
|
|
|
SJID = jid:to_string(jid:make(U, S, <<"">>)),
|
|
|
|
[?SQL("delete from muc_registered where"
|
|
|
|
" jid=%(SJID)s and host=%(RoomHost)s;"),
|
|
|
|
?SQL("insert into muc_registered(jid, host, "
|
|
|
|
"nick) values ("
|
|
|
|
"%(SJID)s, %(RoomHost)s, %(Nick)s);")];
|
2016-04-13 20:07:32 +02:00
|
|
|
false ->
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end}].
|
|
|
|
|
2016-11-22 14:48:01 +01:00
|
|
|
import(_, _, _) ->
|
|
|
|
ok.
|
2016-04-13 20:07:32 +02:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|