25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-22 16:20:52 +01:00
xmpp.chapril.org-ejabberd/src/mod_muc_sql.erl

180 lines
5.8 KiB
Erlang
Raw Normal View History

%%%-------------------------------------------------------------------
%%% @author Evgeny Khramtsov <ekhramtsov@process-one.net>
%%% @copyright (C) 2016, Evgeny Khramtsov
%%% @doc
%%%
%%% @end
%%% Created : 13 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
%%%-------------------------------------------------------------------
-module(mod_muc_sql).
2016-05-04 20:01:05 +02:00
-compile([{parse_transform, ejabberd_sql_pt}]).
-behaviour(mod_muc).
%% 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,
import/1, import/2, export/1]).
-include("jid.hrl").
-include("mod_muc.hrl").
-include("logger.hrl").
2016-05-04 20:01:05 +02:00
-include("ejabberd_sql_pt.hrl").
%%%===================================================================
%%% 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),
F = fun () ->
2016-05-04 20:01:05 +02:00
?SQL_UPSERT_T(
"muc_room",
["!name=%(Name)s",
"!host=%(Host)s",
"opts=%(SOpts)s"])
end,
2016-04-20 11:27:32 +02:00
ejabberd_sql:sql_transaction(LServer, F).
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));
_ ->
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"))
end,
2016-04-20 11:27:32 +02:00
ejabberd_sql:sql_transaction(LServer, F).
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;
_ -> 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} ->
lists:map(
2016-05-04 20:01:05 +02:00
fun({Room, Opts}) ->
#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))}
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;
_ -> 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")),
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;
_ -> 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"]),
ok;
true ->
false
end
end
end,
2016-04-20 11:27:32 +02:00
ejabberd_sql:sql_transaction(LServer, F).
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);")];
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);")];
false ->
[]
end
end}].
import(_LServer) ->
[{<<"select name, host, opts from muc_room;">>,
fun([Name, RoomHost, SOpts]) ->
2016-04-20 11:27:32 +02:00
Opts = mod_muc:opts_to_binary(ejabberd_sql:decode_term(SOpts)),
#muc_room{name_host = {Name, RoomHost}, opts = Opts}
end},
{<<"select jid, host, nick from muc_registered;">>,
fun([J, RoomHost, Nick]) ->
#jid{user = U, server = S} = jid:from_string(J),
#muc_registered{us_host = {{U, S}, RoomHost},
nick = Nick}
end}].
import(_, _) ->
pass.
%%%===================================================================
%%% Internal functions
%%%===================================================================