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
|
|
|
%%%
|
|
|
|
%%%
|
2024-01-22 16:40:01 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2024 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
|
|
|
|
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
|
2021-09-13 07:15:11 +02:00
|
|
|
-export([init/2, store_room/5, store_changes/4,
|
|
|
|
restore_room/3, forget_room/3,
|
2016-04-13 20:07:32 +02:00
|
|
|
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-03-29 11:58:01 +02:00
|
|
|
-export([register_online_room/4, unregister_online_room/4, find_online_room/3,
|
|
|
|
get_online_rooms/3, count_online_rooms/2, rsm_supported/0,
|
|
|
|
register_online_user/4, unregister_online_user/4,
|
2017-10-31 14:00:41 +01:00
|
|
|
count_online_rooms_by_user/3, get_online_rooms_by_user/3,
|
2021-03-03 11:30:43 +01:00
|
|
|
get_subscribed_rooms/3, get_rooms_without_subscribers/2,
|
2022-02-18 14:01:22 +01:00
|
|
|
get_hibernated_rooms_older_than/3,
|
2021-12-14 07:54:00 +01:00
|
|
|
find_online_room_by_pid/2, remove_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]).
|
2024-01-24 20:49:59 +01:00
|
|
|
-export([sql_schemas/0]).
|
2016-04-13 20:07:32 +02:00
|
|
|
|
2020-09-03 13:45:57 +02:00
|
|
|
-include_lib("xmpp/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
|
|
|
|
%%%===================================================================
|
2017-03-29 11:58:01 +02:00
|
|
|
init(Host, Opts) ->
|
2024-01-24 20:49:59 +01:00
|
|
|
ejabberd_sql_schema:update_schema(Host, ?MODULE, sql_schemas()),
|
2019-06-14 11:33:26 +02:00
|
|
|
case gen_mod:ram_db_mod(Opts, mod_muc) of
|
2017-03-29 11:58:01 +02:00
|
|
|
?MODULE ->
|
|
|
|
clean_tables(Host);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2016-04-13 20:07:32 +02:00
|
|
|
|
2024-01-24 20:49:59 +01:00
|
|
|
sql_schemas() ->
|
2023-09-28 02:37:36 +02:00
|
|
|
[#sql_schema{
|
|
|
|
version = 1,
|
|
|
|
tables =
|
|
|
|
[#sql_table{
|
|
|
|
name = <<"muc_room">>,
|
|
|
|
columns =
|
|
|
|
[#sql_column{name = <<"name">>, type = text},
|
|
|
|
#sql_column{name = <<"host">>, type = text},
|
|
|
|
#sql_column{name = <<"server_host">>, type = text},
|
|
|
|
#sql_column{name = <<"opts">>, type = {text, big}},
|
|
|
|
#sql_column{name = <<"created_at">>, type = timestamp,
|
|
|
|
default = true}],
|
|
|
|
indices = [#sql_index{
|
|
|
|
columns = [<<"name">>, <<"host">>],
|
|
|
|
unique = true},
|
|
|
|
#sql_index{
|
|
|
|
columns = [<<"host">>, <<"created_at">>]}]},
|
|
|
|
#sql_table{
|
|
|
|
name = <<"muc_registered">>,
|
|
|
|
columns =
|
|
|
|
[#sql_column{name = <<"jid">>, type = text},
|
|
|
|
#sql_column{name = <<"host">>, type = text},
|
|
|
|
#sql_column{name = <<"server_host">>, type = text},
|
|
|
|
#sql_column{name = <<"nick">>, type = text},
|
|
|
|
#sql_column{name = <<"created_at">>, type = timestamp,
|
|
|
|
default = true}],
|
|
|
|
indices = [#sql_index{
|
|
|
|
columns = [<<"jid">>, <<"host">>],
|
|
|
|
unique = true},
|
|
|
|
#sql_index{
|
|
|
|
columns = [<<"nick">>]}]},
|
|
|
|
#sql_table{
|
|
|
|
name = <<"muc_online_room">>,
|
|
|
|
columns =
|
|
|
|
[#sql_column{name = <<"name">>, type = text},
|
|
|
|
#sql_column{name = <<"host">>, type = text},
|
|
|
|
#sql_column{name = <<"server_host">>, type = text},
|
|
|
|
#sql_column{name = <<"node">>, type = text},
|
|
|
|
#sql_column{name = <<"pid">>, type = text}],
|
|
|
|
indices = [#sql_index{
|
|
|
|
columns = [<<"name">>, <<"host">>],
|
|
|
|
unique = true}]},
|
|
|
|
#sql_table{
|
|
|
|
name = <<"muc_online_users">>,
|
|
|
|
columns =
|
|
|
|
[#sql_column{name = <<"username">>, type = text},
|
|
|
|
#sql_column{name = <<"server">>, type = {text, 75}},
|
|
|
|
#sql_column{name = <<"resource">>, type = text},
|
|
|
|
#sql_column{name = <<"name">>, type = text},
|
|
|
|
#sql_column{name = <<"host">>, type = {text, 75}},
|
|
|
|
#sql_column{name = <<"server_host">>, type = text},
|
|
|
|
#sql_column{name = <<"node">>, type = text}],
|
|
|
|
indices = [#sql_index{
|
|
|
|
columns = [<<"username">>, <<"server">>,
|
|
|
|
<<"resource">>, <<"name">>,
|
|
|
|
<<"host">>],
|
|
|
|
unique = true}]},
|
|
|
|
#sql_table{
|
|
|
|
name = <<"muc_room_subscribers">>,
|
|
|
|
columns =
|
|
|
|
[#sql_column{name = <<"room">>, type = text},
|
|
|
|
#sql_column{name = <<"host">>, type = text},
|
|
|
|
#sql_column{name = <<"jid">>, type = text},
|
|
|
|
#sql_column{name = <<"nick">>, type = text},
|
|
|
|
#sql_column{name = <<"nodes">>, type = text},
|
|
|
|
#sql_column{name = <<"created_at">>, type = timestamp,
|
|
|
|
default = true}],
|
|
|
|
indices = [#sql_index{
|
|
|
|
columns = [<<"host">>, <<"room">>, <<"jid">>],
|
|
|
|
unique = true},
|
|
|
|
#sql_index{
|
|
|
|
columns = [<<"host">>, <<"jid">>]},
|
|
|
|
#sql_index{
|
|
|
|
columns = [<<"jid">>]}]}]}].
|
|
|
|
|
2017-10-31 14:00:41 +01:00
|
|
|
store_room(LServer, Host, Name, Opts, ChangesHints) ->
|
|
|
|
{Subs, Opts2} = case lists:keytake(subscribers, 1, Opts) of
|
|
|
|
{value, {subscribers, S}, OptN} -> {S, OptN};
|
|
|
|
_ -> {[], Opts}
|
|
|
|
end,
|
|
|
|
SOpts = misc:term_to_expr(Opts2),
|
2022-02-18 14:01:22 +01:00
|
|
|
Timestamp = case lists:keyfind(hibernation_time, 1, Opts) of
|
2022-03-07 17:38:04 +01:00
|
|
|
false -> <<"1970-01-02 00:00:00">>;
|
2022-02-18 20:43:56 +01:00
|
|
|
{_, undefined} -> <<"1970-01-02 00:00:00">>;
|
2022-02-18 14:01:22 +01:00
|
|
|
{_, Time} -> usec_to_sql_timestamp(Time)
|
|
|
|
end,
|
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",
|
2017-11-02 15:03:30 +01:00
|
|
|
"server_host=%(LServer)s",
|
2022-02-18 14:01:22 +01:00
|
|
|
"opts=%(SOpts)s",
|
2022-02-18 20:43:56 +01:00
|
|
|
"created_at=%(Timestamp)t"]),
|
2017-11-02 15:03:30 +01:00
|
|
|
case ChangesHints of
|
|
|
|
Changes when is_list(Changes) ->
|
|
|
|
[change_room(Host, Name, Change) || Change <- Changes];
|
|
|
|
_ ->
|
|
|
|
ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("delete from muc_room_subscribers where "
|
|
|
|
"room=%(Name)s and host=%(Host)s")),
|
|
|
|
[change_room(Host, Name, {add_subscription, JID, Nick, Nodes})
|
|
|
|
|| {JID, Nick, Nodes} <- Subs]
|
|
|
|
end
|
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
|
|
|
|
2021-09-13 07:15:11 +02:00
|
|
|
store_changes(LServer, Host, Name, Changes) ->
|
|
|
|
F = fun () ->
|
|
|
|
[change_room(Host, Name, Change) || Change <- Changes]
|
|
|
|
end,
|
|
|
|
ejabberd_sql:sql_transaction(LServer, F).
|
|
|
|
|
2017-10-31 14:00:41 +01:00
|
|
|
change_room(Host, Room, {add_subscription, JID, Nick, Nodes}) ->
|
2017-10-31 14:21:34 +01:00
|
|
|
SJID = jid:encode(JID),
|
|
|
|
SNodes = misc:term_to_expr(Nodes),
|
2017-10-31 14:00:41 +01:00
|
|
|
?SQL_UPSERT_T(
|
|
|
|
"muc_room_subscribers",
|
|
|
|
["!jid=%(SJID)s",
|
|
|
|
"!host=%(Host)s",
|
|
|
|
"!room=%(Room)s",
|
|
|
|
"nick=%(Nick)s",
|
|
|
|
"nodes=%(SNodes)s"]);
|
|
|
|
change_room(Host, Room, {del_subscription, JID}) ->
|
2017-10-31 14:21:34 +01:00
|
|
|
SJID = jid:encode(JID),
|
2017-10-31 14:00:41 +01:00
|
|
|
ejabberd_sql:sql_query_t(?SQL("delete from muc_room_subscribers where "
|
|
|
|
"room=%(Room)s and host=%(Host)s and jid=%(SJID)s"));
|
|
|
|
change_room(Host, Room, Change) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Unsupported change on room ~ts@~ts: ~p", [Room, Host, Change]).
|
2017-10-31 14:00:41 +01:00
|
|
|
|
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}]} ->
|
2017-10-31 14:00:41 +01:00
|
|
|
OptsD = ejabberd_sql:decode_term(Opts),
|
|
|
|
case catch ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
2017-11-03 12:17:34 +01:00
|
|
|
?SQL("select @(jid)s, @(nick)s, @(nodes)s from muc_room_subscribers where room=%(Name)s"
|
2017-10-31 14:00:41 +01:00
|
|
|
" and host=%(Host)s")) of
|
|
|
|
{selected, []} ->
|
|
|
|
OptsR = mod_muc:opts_to_binary(OptsD),
|
|
|
|
case lists:keymember(subscribers, 1, OptsD) of
|
|
|
|
true ->
|
|
|
|
store_room(LServer, Host, Name, OptsR, undefined);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
OptsR;
|
|
|
|
{selected, Subs} ->
|
|
|
|
SubData = lists:map(
|
|
|
|
fun({Jid, Nick, Nodes}) ->
|
2017-10-31 14:21:34 +01:00
|
|
|
{jid:decode(Jid), Nick, ejabberd_sql:decode_term(Nodes)}
|
2017-10-31 14:00:41 +01:00
|
|
|
end, Subs),
|
|
|
|
Opts2 = lists:keystore(subscribers, 1, OptsD, {subscribers, SubData}),
|
|
|
|
mod_muc:opts_to_binary(Opts2);
|
|
|
|
_ ->
|
|
|
|
error
|
|
|
|
end;
|
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"
|
2017-10-31 14:00:41 +01:00
|
|
|
" and host=%(Host)s")),
|
|
|
|
ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("delete from muc_room_subscribers where room=%(Name)s"
|
2016-05-04 20:01:05 +02:00
|
|
|
" 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
|
|
|
|
2023-10-10 13:08:13 +02:00
|
|
|
can_use_nick(LServer, ServiceOrRoom, JID, Nick) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
SJID = jid:encode(jid:tolower(jid:remove_resource(JID))),
|
2023-10-10 13:08:13 +02:00
|
|
|
SqlQuery = case (jid:decode(ServiceOrRoom))#jid.lserver of
|
|
|
|
ServiceOrRoom ->
|
|
|
|
?SQL("select @(jid)s from muc_registered "
|
|
|
|
"where nick=%(Nick)s"
|
|
|
|
" and host=%(ServiceOrRoom)s");
|
|
|
|
Service ->
|
|
|
|
?SQL("select @(jid)s from muc_registered "
|
|
|
|
"where nick=%(Nick)s"
|
|
|
|
" and (host=%(ServiceOrRoom)s or host=%(Service)s)")
|
|
|
|
end,
|
|
|
|
case catch ejabberd_sql:sql_query(LServer, SqlQuery) of
|
2016-05-04 20:01:05 +02:00
|
|
|
{selected, [{SJID1}]} -> SJID == SJID1;
|
2016-04-13 20:07:32 +02:00
|
|
|
_ -> true
|
|
|
|
end.
|
|
|
|
|
2021-01-26 11:13:23 +01:00
|
|
|
get_rooms_without_subscribers(LServer, Host) ->
|
|
|
|
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(
|
|
|
|
fun({Room, Opts}) ->
|
|
|
|
OptsD = ejabberd_sql:decode_term(Opts),
|
|
|
|
#muc_room{name_host = {Room, Host},
|
|
|
|
opts = mod_muc:opts_to_binary(OptsD)}
|
|
|
|
end, RoomOpts);
|
|
|
|
_Err ->
|
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
2022-02-18 14:01:22 +01:00
|
|
|
get_hibernated_rooms_older_than(LServer, Host, Timestamp) ->
|
|
|
|
TimestampS = usec_to_sql_timestamp(Timestamp),
|
|
|
|
case catch ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
|
|
|
?SQL("select @(name)s, @(opts)s from muc_room"
|
2022-02-18 20:43:56 +01:00
|
|
|
" where host=%(Host)s and created_at < %(TimestampS)t and created_at > '1970-01-02 00:00:00'")) of
|
2022-02-18 14:01:22 +01:00
|
|
|
{selected, RoomOpts} ->
|
|
|
|
lists:map(
|
|
|
|
fun({Room, Opts}) ->
|
|
|
|
OptsD = ejabberd_sql:decode_term(Opts),
|
|
|
|
#muc_room{name_host = {Room, Host},
|
|
|
|
opts = mod_muc:opts_to_binary(OptsD)}
|
|
|
|
end, RoomOpts);
|
|
|
|
_Err ->
|
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
2016-04-13 20:07:32 +02:00
|
|
|
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} ->
|
2017-10-31 14:00:41 +01:00
|
|
|
case catch ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
|
|
|
?SQL("select @(room)s, @(jid)s, @(nick)s, @(nodes)s from muc_room_subscribers"
|
|
|
|
" where host=%(Host)s")) of
|
|
|
|
{selected, Subs} ->
|
|
|
|
SubsD = lists:foldl(
|
|
|
|
fun({Room, Jid, Nick, Nodes}, Dict) ->
|
2021-09-13 07:15:11 +02:00
|
|
|
Sub = {jid:decode(Jid),
|
|
|
|
Nick, ejabberd_sql:decode_term(Nodes)},
|
|
|
|
maps:update_with(
|
|
|
|
Room,
|
|
|
|
fun(SubAcc) ->
|
|
|
|
[Sub | SubAcc]
|
|
|
|
end,
|
|
|
|
[Sub],
|
|
|
|
Dict)
|
|
|
|
end, maps:new(), Subs),
|
2016-04-13 20:07:32 +02:00
|
|
|
lists:map(
|
2016-05-04 20:01:05 +02:00
|
|
|
fun({Room, Opts}) ->
|
2017-10-31 14:00:41 +01:00
|
|
|
OptsD = ejabberd_sql:decode_term(Opts),
|
2021-09-13 07:15:11 +02:00
|
|
|
OptsD2 = case {maps:find(Room, SubsD), lists:keymember(subscribers, 1, OptsD)} of
|
2017-10-31 14:00:41 +01:00
|
|
|
{_, true} ->
|
|
|
|
store_room(LServer, Host, Room, mod_muc:opts_to_binary(OptsD), undefined),
|
|
|
|
OptsD;
|
|
|
|
{{ok, SubsI}, false} ->
|
|
|
|
lists:keystore(subscribers, 1, OptsD, {subscribers, SubsI});
|
|
|
|
_ ->
|
|
|
|
OptsD
|
|
|
|
end,
|
2016-04-13 20:07:32 +02:00
|
|
|
#muc_room{name_host = {Room, Host},
|
2017-10-31 14:00:41 +01:00
|
|
|
opts = mod_muc:opts_to_binary(OptsD2)}
|
2016-04-13 20:07:32 +02:00
|
|
|
end, RoomOpts);
|
2017-12-17 17:46:55 +01:00
|
|
|
_Err ->
|
2017-10-31 14:00:41 +01:00
|
|
|
[]
|
|
|
|
end;
|
2017-12-17 17:46:55 +01:00
|
|
|
_Err ->
|
2016-04-13 20:07:32 +02:00
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_nick(LServer, Host, From) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
SJID = jid:encode(jid:tolower(jid:remove_resource(From))),
|
2016-05-04 20:01:05 +02:00
|
|
|
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.
|
|
|
|
|
2023-10-10 13:08:13 +02:00
|
|
|
set_nick(LServer, ServiceOrRoom, From, Nick) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
JID = jid:encode(jid:tolower(jid:remove_resource(From))),
|
2016-04-13 20:07:32 +02:00
|
|
|
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"
|
2023-10-10 13:08:13 +02:00
|
|
|
" jid=%(JID)s and host=%(ServiceOrRoom)s")),
|
2016-04-13 20:07:32 +02:00
|
|
|
ok;
|
|
|
|
_ ->
|
2023-10-10 13:08:13 +02:00
|
|
|
Service = (jid:decode(ServiceOrRoom))#jid.lserver,
|
|
|
|
SqlQuery = case (ServiceOrRoom == Service) of
|
|
|
|
true ->
|
|
|
|
?SQL("select @(jid)s, @(host)s from muc_registered "
|
|
|
|
"where nick=%(Nick)s"
|
|
|
|
" and host=%(ServiceOrRoom)s");
|
|
|
|
false ->
|
|
|
|
?SQL("select @(jid)s, @(host)s from muc_registered "
|
|
|
|
"where nick=%(Nick)s"
|
|
|
|
" and (host=%(ServiceOrRoom)s or host=%(Service)s)")
|
|
|
|
end,
|
|
|
|
Allow = case ejabberd_sql:sql_query_t(SqlQuery) of
|
|
|
|
{selected, []}
|
|
|
|
when (ServiceOrRoom == Service) ->
|
|
|
|
%% Registering in the service...
|
|
|
|
%% check if nick is registered for some room in this service
|
|
|
|
{selected, NickRegistrations} =
|
|
|
|
ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("select @(jid)s, @(host)s from muc_registered "
|
|
|
|
"where nick=%(Nick)s")),
|
|
|
|
not lists:any(fun({_NRJid, NRServiceOrRoom}) ->
|
|
|
|
Service == (jid:decode(NRServiceOrRoom))#jid.lserver end,
|
|
|
|
NickRegistrations);
|
|
|
|
{selected, []} ->
|
|
|
|
%% Nick not registered in any service or room
|
|
|
|
true;
|
|
|
|
{selected, [{_J, Host}]}
|
|
|
|
when (Host == Service) and (ServiceOrRoom /= Service) ->
|
|
|
|
%% Registering in a room, but the nick is already registered in the service
|
|
|
|
false;
|
|
|
|
{selected, [{J, _Host}]} ->
|
|
|
|
%% Registering in room (or service) a nick that is
|
|
|
|
%% already registered in this room (or service)
|
|
|
|
%% Only the owner of this registration can use the nick
|
|
|
|
J == JID
|
2016-04-13 20:07:32 +02:00
|
|
|
end,
|
|
|
|
if Allow ->
|
2016-05-04 20:01:05 +02:00
|
|
|
?SQL_UPSERT_T(
|
|
|
|
"muc_registered",
|
|
|
|
["!jid=%(JID)s",
|
2023-10-10 13:08:13 +02:00
|
|
|
"!host=%(ServiceOrRoom)s",
|
2017-11-02 15:03:30 +01:00
|
|
|
"server_host=%(LServer)s",
|
2016-05-04 20:01:05 +02:00
|
|
|
"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-03-29 11:58:01 +02:00
|
|
|
register_online_room(ServerHost, Room, Host, Pid) ->
|
2017-04-11 12:13:58 +02:00
|
|
|
PidS = misc:encode_pid(Pid),
|
2017-03-29 11:58:01 +02:00
|
|
|
NodeS = erlang:atom_to_binary(node(Pid), latin1),
|
|
|
|
case ?SQL_UPSERT(ServerHost,
|
|
|
|
"muc_online_room",
|
|
|
|
["!name=%(Room)s",
|
|
|
|
"!host=%(Host)s",
|
2017-11-02 15:03:30 +01:00
|
|
|
"server_host=%(ServerHost)s",
|
2017-03-29 11:58:01 +02:00
|
|
|
"node=%(NodeS)s",
|
|
|
|
"pid=%(PidS)s"]) of
|
|
|
|
ok ->
|
|
|
|
ok;
|
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end.
|
2017-01-13 10:03:39 +01:00
|
|
|
|
2017-03-29 11:58:01 +02:00
|
|
|
unregister_online_room(ServerHost, Room, Host, Pid) ->
|
|
|
|
%% TODO: report errors
|
2017-04-11 12:13:58 +02:00
|
|
|
PidS = misc:encode_pid(Pid),
|
2017-03-29 11:58:01 +02:00
|
|
|
NodeS = erlang:atom_to_binary(node(Pid), latin1),
|
|
|
|
ejabberd_sql:sql_query(
|
|
|
|
ServerHost,
|
|
|
|
?SQL("delete from muc_online_room where name=%(Room)s and "
|
|
|
|
"host=%(Host)s and node=%(NodeS)s and pid=%(PidS)s")).
|
2017-01-13 10:03:39 +01:00
|
|
|
|
2017-03-29 11:58:01 +02:00
|
|
|
find_online_room(ServerHost, Room, Host) ->
|
|
|
|
case ejabberd_sql:sql_query(
|
|
|
|
ServerHost,
|
|
|
|
?SQL("select @(pid)s, @(node)s from muc_online_room where "
|
|
|
|
"name=%(Room)s and host=%(Host)s")) of
|
|
|
|
{selected, [{PidS, NodeS}]} ->
|
2017-04-11 12:13:58 +02:00
|
|
|
try {ok, misc:decode_pid(PidS, NodeS)}
|
2017-03-30 16:51:37 +02:00
|
|
|
catch _:{bad_node, _} -> error
|
2017-03-29 11:58:01 +02:00
|
|
|
end;
|
|
|
|
{selected, []} ->
|
|
|
|
error;
|
2017-12-17 17:46:55 +01:00
|
|
|
_Err ->
|
2017-03-29 11:58:01 +02:00
|
|
|
error
|
|
|
|
end.
|
2017-01-13 10:03:39 +01:00
|
|
|
|
2021-03-03 11:30:43 +01:00
|
|
|
find_online_room_by_pid(ServerHost, Pid) ->
|
|
|
|
PidS = misc:encode_pid(Pid),
|
|
|
|
NodeS = erlang:atom_to_binary(node(Pid), latin1),
|
|
|
|
case ejabberd_sql:sql_query(
|
|
|
|
ServerHost,
|
|
|
|
?SQL("select @(name)s, @(host)s from muc_online_room where "
|
|
|
|
"node=%(NodeS)s and pid=%(PidS)s")) of
|
|
|
|
{selected, [{Room, Host}]} ->
|
|
|
|
{ok, Room, Host};
|
|
|
|
{selected, []} ->
|
|
|
|
error;
|
|
|
|
_Err ->
|
|
|
|
error
|
|
|
|
end.
|
|
|
|
|
2017-03-29 11:58:01 +02:00
|
|
|
count_online_rooms(ServerHost, Host) ->
|
|
|
|
case ejabberd_sql:sql_query(
|
|
|
|
ServerHost,
|
|
|
|
?SQL("select @(count(*))d from muc_online_room "
|
|
|
|
"where host=%(Host)s")) of
|
|
|
|
{selected, [{Num}]} ->
|
|
|
|
Num;
|
2017-12-17 17:46:55 +01:00
|
|
|
_Err ->
|
2017-03-29 11:58:01 +02:00
|
|
|
0
|
|
|
|
end.
|
2017-01-13 10:03:39 +01:00
|
|
|
|
2017-03-29 11:58:01 +02:00
|
|
|
get_online_rooms(ServerHost, Host, _RSM) ->
|
|
|
|
case ejabberd_sql:sql_query(
|
|
|
|
ServerHost,
|
|
|
|
?SQL("select @(name)s, @(pid)s, @(node)s from muc_online_room "
|
|
|
|
"where host=%(Host)s")) of
|
|
|
|
{selected, Rows} ->
|
|
|
|
lists:flatmap(
|
|
|
|
fun({Room, PidS, NodeS}) ->
|
2017-04-11 12:13:58 +02:00
|
|
|
try [{Room, Host, misc:decode_pid(PidS, NodeS)}]
|
2017-03-30 16:51:37 +02:00
|
|
|
catch _:{bad_node, _} -> []
|
2017-03-29 11:58:01 +02:00
|
|
|
end
|
|
|
|
end, Rows);
|
2017-12-17 17:46:55 +01:00
|
|
|
_Err ->
|
2017-03-29 12:20:15 +02:00
|
|
|
[]
|
2017-03-29 11:58:01 +02:00
|
|
|
end.
|
2017-01-13 10:03:39 +01:00
|
|
|
|
|
|
|
rsm_supported() ->
|
|
|
|
false.
|
|
|
|
|
2017-03-29 11:58:01 +02:00
|
|
|
register_online_user(ServerHost, {U, S, R}, Room, Host) ->
|
|
|
|
NodeS = erlang:atom_to_binary(node(), latin1),
|
|
|
|
case ?SQL_UPSERT(ServerHost, "muc_online_users",
|
|
|
|
["!username=%(U)s",
|
|
|
|
"!server=%(S)s",
|
|
|
|
"!resource=%(R)s",
|
|
|
|
"!name=%(Room)s",
|
|
|
|
"!host=%(Host)s",
|
2017-11-02 15:03:30 +01:00
|
|
|
"server_host=%(ServerHost)s",
|
2017-03-29 11:58:01 +02:00
|
|
|
"node=%(NodeS)s"]) of
|
|
|
|
ok ->
|
|
|
|
ok;
|
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end.
|
2017-01-13 10:03:39 +01:00
|
|
|
|
2017-03-29 11:58:01 +02:00
|
|
|
unregister_online_user(ServerHost, {U, S, R}, Room, Host) ->
|
|
|
|
%% TODO: report errors
|
|
|
|
ejabberd_sql:sql_query(
|
|
|
|
ServerHost,
|
|
|
|
?SQL("delete from muc_online_users where username=%(U)s and "
|
|
|
|
"server=%(S)s and resource=%(R)s and name=%(Room)s and "
|
|
|
|
"host=%(Host)s")).
|
2017-01-13 10:03:39 +01:00
|
|
|
|
2017-03-29 11:58:01 +02:00
|
|
|
count_online_rooms_by_user(ServerHost, U, S) ->
|
|
|
|
case ejabberd_sql:sql_query(
|
|
|
|
ServerHost,
|
|
|
|
?SQL("select @(count(*))d from muc_online_users where "
|
|
|
|
"username=%(U)s and server=%(S)s")) of
|
|
|
|
{selected, [{Num}]} ->
|
|
|
|
Num;
|
2017-12-17 17:46:55 +01:00
|
|
|
_Err ->
|
2017-03-29 11:58:01 +02:00
|
|
|
0
|
|
|
|
end.
|
2017-01-13 10:03:39 +01:00
|
|
|
|
2017-03-29 11:58:01 +02:00
|
|
|
get_online_rooms_by_user(ServerHost, U, S) ->
|
|
|
|
case ejabberd_sql:sql_query(
|
|
|
|
ServerHost,
|
|
|
|
?SQL("select @(name)s, @(host)s from muc_online_users where "
|
|
|
|
"username=%(U)s and server=%(S)s")) of
|
|
|
|
{selected, Rows} ->
|
|
|
|
Rows;
|
2017-12-17 17:46:55 +01:00
|
|
|
_Err ->
|
2017-03-29 11:58:01 +02:00
|
|
|
[]
|
|
|
|
end.
|
2017-01-13 10:03:39 +01:00
|
|
|
|
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 ->
|
2017-04-11 12:13:58 +02:00
|
|
|
SOpts = misc:term_to_expr(Opts),
|
2016-05-04 20:01:05 +02:00
|
|
|
[?SQL("delete from muc_room where name=%(Name)s"
|
|
|
|
" and host=%(RoomHost)s;"),
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL_INSERT(
|
|
|
|
"muc_room",
|
|
|
|
["name=%(Name)s",
|
2018-05-24 01:28:29 +02:00
|
|
|
"host=%(RoomHost)s",
|
2017-11-02 15:03:30 +01:00
|
|
|
"server_host=%(Host)s",
|
|
|
|
"opts=%(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 ->
|
2017-02-26 08:07:12 +01:00
|
|
|
SJID = jid:encode(jid:make(U, S)),
|
2016-05-04 20:01:05 +02:00
|
|
|
[?SQL("delete from muc_registered where"
|
|
|
|
" jid=%(SJID)s and host=%(RoomHost)s;"),
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL_INSERT(
|
|
|
|
"muc_registered",
|
|
|
|
["jid=%(SJID)s",
|
2018-05-24 01:28:29 +02:00
|
|
|
"host=%(RoomHost)s",
|
2017-11-02 15:03:30 +01:00
|
|
|
"server_host=%(Host)s",
|
|
|
|
"nick=%(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
|
|
|
|
2017-10-31 14:00:41 +01:00
|
|
|
get_subscribed_rooms(LServer, Host, Jid) ->
|
2017-10-31 14:21:34 +01:00
|
|
|
JidS = jid:encode(Jid),
|
2019-04-03 13:20:37 +02:00
|
|
|
case ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
2020-04-03 12:34:57 +02:00
|
|
|
?SQL("select @(room)s, @(nick)s, @(nodes)s from muc_room_subscribers "
|
2019-04-03 13:20:37 +02:00
|
|
|
"where jid=%(JidS)s and host=%(Host)s")) of
|
2017-10-31 14:00:41 +01:00
|
|
|
{selected, Subs} ->
|
2020-04-03 12:34:57 +02:00
|
|
|
{ok, [{jid:make(Room, Host), Nick, ejabberd_sql:decode_term(Nodes)}
|
|
|
|
|| {Room, Nick, Nodes} <- Subs]};
|
2017-12-17 17:46:55 +01:00
|
|
|
_Error ->
|
2019-04-03 13:20:37 +02:00
|
|
|
{error, db_failure}
|
2017-10-31 14:00:41 +01:00
|
|
|
end.
|
|
|
|
|
2021-12-14 07:54:00 +01:00
|
|
|
remove_user(LUser, LServer) ->
|
|
|
|
SJID = jid:encode(jid:make(LUser, LServer)),
|
|
|
|
ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
|
|
|
?SQL("delete from muc_room_subscribers where jid=%(SJID)s")),
|
|
|
|
ok.
|
|
|
|
|
2016-04-13 20:07:32 +02:00
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
2017-03-29 11:58:01 +02:00
|
|
|
clean_tables(ServerHost) ->
|
|
|
|
NodeS = erlang:atom_to_binary(node(), latin1),
|
2017-04-15 14:47:00 +02:00
|
|
|
?DEBUG("Cleaning SQL muc_online_room table...", []),
|
2017-03-29 11:58:01 +02:00
|
|
|
case ejabberd_sql:sql_query(
|
|
|
|
ServerHost,
|
|
|
|
?SQL("delete from muc_online_room where node=%(NodeS)s")) of
|
|
|
|
{updated, _} ->
|
|
|
|
ok;
|
|
|
|
Err1 ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?ERROR_MSG("Failed to clean 'muc_online_room' table: ~p", [Err1]),
|
2017-03-29 11:58:01 +02:00
|
|
|
Err1
|
|
|
|
end,
|
2017-04-15 14:47:00 +02:00
|
|
|
?DEBUG("Cleaning SQL muc_online_users table...", []),
|
2017-03-29 11:58:01 +02:00
|
|
|
case ejabberd_sql:sql_query(
|
|
|
|
ServerHost,
|
|
|
|
?SQL("delete from muc_online_users where node=%(NodeS)s")) of
|
|
|
|
{updated, _} ->
|
|
|
|
ok;
|
|
|
|
Err2 ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?ERROR_MSG("Failed to clean 'muc_online_users' table: ~p", [Err2]),
|
2017-03-29 11:58:01 +02:00
|
|
|
Err2
|
|
|
|
end.
|
2022-02-18 14:01:22 +01:00
|
|
|
|
|
|
|
usec_to_sql_timestamp(Timestamp) ->
|
2022-02-18 17:21:22 +01:00
|
|
|
TS = misc:usec_to_now(Timestamp),
|
|
|
|
case calendar:now_to_universal_time(TS) of
|
2022-02-18 14:01:22 +01:00
|
|
|
{{Year, Month, Day}, {Hour, Minute, Second}} ->
|
|
|
|
list_to_binary(io_lib:format("~4..0B-~2..0B-~2..0B "
|
|
|
|
"~2..0B:~2..0B:~2..0B",
|
|
|
|
[Year, Month, Day, Hour, Minute, Second]))
|
|
|
|
end.
|