2003-03-23 21:08:44 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_muc.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Purpose : MUC support (JEP-0045)
|
|
|
|
%%% Created : 19 Mar 2003 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_muc).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
-vsn('$Revision$ ').
|
|
|
|
|
2006-02-02 06:00:27 +01:00
|
|
|
-behaviour(gen_server).
|
2003-03-23 21:08:44 +01:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2006-02-02 06:00:27 +01:00
|
|
|
%% API
|
|
|
|
-export([start_link/2,
|
|
|
|
start/2,
|
2005-06-20 05:18:13 +02:00
|
|
|
stop/1,
|
2007-01-19 05:46:44 +01:00
|
|
|
room_destroyed/4,
|
2005-04-17 20:08:34 +02:00
|
|
|
store_room/3,
|
|
|
|
restore_room/2,
|
|
|
|
forget_room/2,
|
2003-12-17 21:13:21 +01:00
|
|
|
process_iq_disco_items/4,
|
2005-04-17 20:08:34 +02:00
|
|
|
can_use_nick/3]).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2006-02-02 06:00:27 +01:00
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
|
|
terminate/2, code_change/3]).
|
|
|
|
|
2003-03-23 21:08:44 +01:00
|
|
|
-include("ejabberd.hrl").
|
|
|
|
-include("jlib.hrl").
|
|
|
|
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
-record(muc_room, {name_host, opts}).
|
|
|
|
-record(muc_online_room, {name_host, pid}).
|
|
|
|
-record(muc_registered, {us_host, nick}).
|
2003-04-17 21:47:43 +02:00
|
|
|
|
2006-09-05 17:53:54 +02:00
|
|
|
-record(state, {host, server_host, access, history_size}).
|
2006-02-02 06:00:27 +01:00
|
|
|
|
2005-05-23 02:30:29 +02:00
|
|
|
-define(PROCNAME, ejabberd_mod_muc).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2006-02-02 06:00:27 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% API
|
|
|
|
%%====================================================================
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
|
|
|
|
%% Description: Starts the server
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
start_link(Host, Opts) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
|
|
|
gen_server:start_link({local, Proc}, ?MODULE, [Host, Opts], []).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2006-02-02 06:00:27 +01:00
|
|
|
start_supervisor(Host),
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
|
|
|
ChildSpec =
|
|
|
|
{Proc,
|
|
|
|
{?MODULE, start_link, [Host, Opts]},
|
|
|
|
temporary,
|
|
|
|
1000,
|
|
|
|
worker,
|
|
|
|
[?MODULE]},
|
|
|
|
supervisor:start_child(ejabberd_sup, ChildSpec).
|
|
|
|
|
|
|
|
stop(Host) ->
|
|
|
|
stop_supervisor(Host),
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
|
|
|
gen_server:call(Proc, stop),
|
|
|
|
supervisor:delete_child(ejabberd_sup, Proc).
|
|
|
|
|
2007-01-19 05:46:44 +01:00
|
|
|
room_destroyed(Host, Room, Pid, ServerHost) ->
|
2006-02-02 06:00:27 +01:00
|
|
|
gen_mod:get_module_proc(ServerHost, ?PROCNAME) !
|
2007-01-19 05:46:44 +01:00
|
|
|
{room_destroyed, {Room, Host}, Pid},
|
2006-02-02 06:00:27 +01:00
|
|
|
ok.
|
|
|
|
|
|
|
|
store_room(Host, Name, Opts) ->
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:write(#muc_room{name_host = {Name, Host},
|
|
|
|
opts = Opts})
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
|
|
|
restore_room(Host, Name) ->
|
|
|
|
case catch mnesia:dirty_read(muc_room, {Name, Host}) of
|
|
|
|
[#muc_room{opts = Opts}] ->
|
|
|
|
Opts;
|
|
|
|
_ ->
|
|
|
|
error
|
|
|
|
end.
|
|
|
|
|
|
|
|
forget_room(Host, Name) ->
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:delete({muc_room, {Name, Host}})
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
|
|
|
process_iq_disco_items(Host, From, To, #iq{lang = Lang} = IQ) ->
|
|
|
|
Res = IQ#iq{type = result,
|
|
|
|
sub_el = [{xmlelement, "query",
|
|
|
|
[{"xmlns", ?NS_DISCO_ITEMS}],
|
|
|
|
iq_disco_items(Host, From, Lang)}]},
|
|
|
|
ejabberd_router:route(To,
|
|
|
|
From,
|
|
|
|
jlib:iq_to_xml(Res)).
|
|
|
|
|
|
|
|
can_use_nick(_Host, _JID, "") ->
|
|
|
|
false;
|
|
|
|
can_use_nick(Host, JID, Nick) ->
|
|
|
|
{LUser, LServer, _} = jlib:jid_tolower(JID),
|
|
|
|
LUS = {LUser, LServer},
|
|
|
|
case catch mnesia:dirty_select(
|
|
|
|
muc_registered,
|
|
|
|
[{#muc_registered{us_host = '$1',
|
|
|
|
nick = Nick,
|
|
|
|
_ = '_'},
|
|
|
|
[{'==', {element, 2, '$1'}, Host}],
|
|
|
|
['$_']}]) of
|
|
|
|
{'EXIT', _Reason} ->
|
|
|
|
true;
|
|
|
|
[] ->
|
|
|
|
true;
|
|
|
|
[#muc_registered{us_host = {U, _Host}}] ->
|
|
|
|
U == LUS
|
|
|
|
end.
|
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: init(Args) -> {ok, State} |
|
|
|
|
%% {ok, State, Timeout} |
|
|
|
|
%% ignore |
|
|
|
|
%% {stop, Reason}
|
|
|
|
%% Description: Initiates the server
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
init([Host, Opts]) ->
|
2003-03-23 21:08:44 +01:00
|
|
|
mnesia:create_table(muc_room,
|
|
|
|
[{disc_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, muc_room)}]),
|
2003-04-17 21:47:43 +02:00
|
|
|
mnesia:create_table(muc_registered,
|
|
|
|
[{disc_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, muc_registered)}]),
|
2007-01-19 05:46:44 +01:00
|
|
|
mnesia:create_table(muc_online_room,
|
|
|
|
[{ram_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, muc_online_room)}]),
|
|
|
|
mnesia:add_table_copy(muc_online_room, node(), ram_copies),
|
2005-06-20 05:18:13 +02:00
|
|
|
MyHost = gen_mod:get_opt(host, Opts, "conference." ++ Host),
|
|
|
|
update_tables(MyHost),
|
2007-05-09 16:47:58 +02:00
|
|
|
clean_table_from_bad_node(node(), MyHost),
|
2003-04-17 21:47:43 +02:00
|
|
|
mnesia:add_table_index(muc_registered, nick),
|
2007-02-13 18:23:36 +01:00
|
|
|
mnesia:subscribe(system),
|
2004-05-17 22:36:41 +02:00
|
|
|
Access = gen_mod:get_opt(access, Opts, all),
|
|
|
|
AccessCreate = gen_mod:get_opt(access_create, Opts, all),
|
|
|
|
AccessAdmin = gen_mod:get_opt(access_admin, Opts, none),
|
2007-06-20 13:25:19 +02:00
|
|
|
AccessPersistent = gen_mod:get_opt(access_persistent, Opts, all),
|
2006-09-05 17:53:54 +02:00
|
|
|
HistorySize = gen_mod:get_opt(history_size, Opts, 20),
|
2006-02-02 06:00:27 +01:00
|
|
|
ejabberd_router:register_route(MyHost),
|
2007-06-20 13:25:19 +02:00
|
|
|
load_permanent_rooms(MyHost, Host, {Access, AccessCreate, AccessAdmin, AccessPersistent},
|
2006-09-05 17:53:54 +02:00
|
|
|
HistorySize),
|
2006-02-02 06:00:27 +01:00
|
|
|
{ok, #state{host = MyHost,
|
|
|
|
server_host = Host,
|
2007-06-20 13:25:19 +02:00
|
|
|
access = {Access, AccessCreate, AccessAdmin, AccessPersistent},
|
2006-09-05 17:53:54 +02:00
|
|
|
history_size = HistorySize}}.
|
2006-02-02 06:00:27 +01:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
|
|
|
|
%% {reply, Reply, State, Timeout} |
|
|
|
|
%% {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, Reply, State} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling call messages
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
handle_call(stop, _From, State) ->
|
|
|
|
{stop, normal, ok, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: handle_cast(Msg, State) -> {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling cast messages
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
handle_cast(_Msg, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: handle_info(Info, State) -> {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling all non call/cast messages
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
handle_info({route, From, To, Packet},
|
|
|
|
#state{host = Host,
|
|
|
|
server_host = ServerHost,
|
2006-09-05 17:53:54 +02:00
|
|
|
access = Access,
|
|
|
|
history_size = HistorySize} = State) ->
|
|
|
|
case catch do_route(Host, ServerHost, Access, HistorySize, From, To, Packet) of
|
2006-02-02 06:00:27 +01:00
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p", [Reason]);
|
2003-03-23 21:08:44 +01:00
|
|
|
_ ->
|
2006-02-02 06:00:27 +01:00
|
|
|
ok
|
|
|
|
end,
|
|
|
|
{noreply, State};
|
2007-01-19 05:46:44 +01:00
|
|
|
handle_info({room_destroyed, RoomHost, Pid}, State) ->
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:delete_object(#muc_online_room{name_host = RoomHost,
|
|
|
|
pid = Pid})
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F),
|
2006-02-02 06:00:27 +01:00
|
|
|
{noreply, State};
|
2007-02-13 18:23:36 +01:00
|
|
|
handle_info({mnesia_system_event, {mnesia_down, Node}}, State) ->
|
|
|
|
clean_table_from_bad_node(Node),
|
|
|
|
{noreply, State};
|
2006-02-02 06:00:27 +01:00
|
|
|
handle_info(_Info, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: terminate(Reason, State) -> void()
|
|
|
|
%% Description: This function is called by a gen_server when it is about to
|
|
|
|
%% terminate. It should be the opposite of Module:init/1 and do any necessary
|
|
|
|
%% cleaning up. When it returns, the gen_server terminates with Reason.
|
|
|
|
%% The return value is ignored.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
terminate(_Reason, State) ->
|
|
|
|
ejabberd_router:unregister_route(State#state.host),
|
|
|
|
ok.
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2006-02-02 06:00:27 +01:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
|
|
|
|
%% Description: Convert process state when code is changed
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
start_supervisor(Host) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ejabberd_mod_muc_sup),
|
|
|
|
ChildSpec =
|
|
|
|
{Proc,
|
|
|
|
{ejabberd_tmp_sup, start_link,
|
|
|
|
[Proc, mod_muc_room]},
|
|
|
|
permanent,
|
|
|
|
infinity,
|
|
|
|
supervisor,
|
|
|
|
[ejabberd_tmp_sup]},
|
|
|
|
supervisor:start_child(ejabberd_sup, ChildSpec).
|
|
|
|
|
|
|
|
stop_supervisor(Host) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ejabberd_mod_muc_sup),
|
|
|
|
supervisor:terminate_child(ejabberd_sup, Proc),
|
|
|
|
supervisor:delete_child(ejabberd_sup, Proc).
|
2003-03-23 21:08:44 +01:00
|
|
|
|
2006-09-05 17:53:54 +02:00
|
|
|
do_route(Host, ServerHost, Access, HistorySize, From, To, Packet) ->
|
2007-06-20 13:25:19 +02:00
|
|
|
{AccessRoute, _AccessCreate, _AccessAdmin, AccessPersistent} = Access,
|
2005-06-20 05:18:13 +02:00
|
|
|
case acl:match_rule(ServerHost, AccessRoute, From) of
|
2004-05-17 22:36:41 +02:00
|
|
|
allow ->
|
2006-09-05 17:53:54 +02:00
|
|
|
do_route1(Host, ServerHost, Access, HistorySize, From, To, Packet);
|
2004-05-17 22:36:41 +02:00
|
|
|
_ ->
|
|
|
|
{xmlelement, _Name, Attrs, _Els} = Packet,
|
|
|
|
Lang = xml:get_attr_s("xml:lang", Attrs),
|
|
|
|
ErrText = "Access denied by service policy",
|
|
|
|
Err = jlib:make_error_reply(Packet,
|
|
|
|
?ERRT_FORBIDDEN(Lang, ErrText)),
|
|
|
|
ejabberd_router:route(To, From, Err)
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2006-09-05 17:53:54 +02:00
|
|
|
do_route1(Host, ServerHost, Access, HistorySize, From, To, Packet) ->
|
2007-06-20 13:25:19 +02:00
|
|
|
{_AccessRoute, AccessCreate, AccessAdmin, AccessPersistent} = Access,
|
2003-08-15 21:17:12 +02:00
|
|
|
{Room, _, Nick} = jlib:jid_tolower(To),
|
2004-05-17 22:36:41 +02:00
|
|
|
{xmlelement, Name, Attrs, _Els} = Packet,
|
2003-03-23 21:08:44 +01:00
|
|
|
case Room of
|
|
|
|
"" ->
|
|
|
|
case Nick of
|
|
|
|
"" ->
|
2003-05-18 18:41:15 +02:00
|
|
|
case Name of
|
|
|
|
"iq" ->
|
|
|
|
case jlib:iq_query_info(Packet) of
|
2003-12-17 21:13:21 +01:00
|
|
|
#iq{type = get, xmlns = ?NS_DISCO_INFO = XMLNS,
|
2007-05-29 15:14:09 +02:00
|
|
|
sub_el = _SubEl, lang = Lang} = IQ ->
|
2003-12-17 21:13:21 +01:00
|
|
|
Res = IQ#iq{type = result,
|
|
|
|
sub_el = [{xmlelement, "query",
|
|
|
|
[{"xmlns", XMLNS}],
|
2007-05-29 15:14:09 +02:00
|
|
|
iq_disco_info(Lang)}]},
|
2003-05-18 18:41:15 +02:00
|
|
|
ejabberd_router:route(To,
|
|
|
|
From,
|
|
|
|
jlib:iq_to_xml(Res));
|
2003-12-17 21:13:21 +01:00
|
|
|
#iq{type = get,
|
|
|
|
xmlns = ?NS_DISCO_ITEMS} = IQ ->
|
2003-05-18 18:41:15 +02:00
|
|
|
spawn(?MODULE,
|
|
|
|
process_iq_disco_items,
|
2003-12-17 21:13:21 +01:00
|
|
|
[Host, From, To, IQ]);
|
|
|
|
#iq{type = get,
|
|
|
|
xmlns = ?NS_REGISTER = XMLNS,
|
2004-02-26 23:00:04 +01:00
|
|
|
lang = Lang,
|
2006-01-19 03:17:31 +01:00
|
|
|
sub_el = _SubEl} = IQ ->
|
2003-12-17 21:13:21 +01:00
|
|
|
Res = IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[{xmlelement, "query",
|
|
|
|
[{"xmlns", XMLNS}],
|
|
|
|
iq_get_register_info(
|
2005-04-17 20:08:34 +02:00
|
|
|
Host, From, Lang)}]},
|
2003-05-18 18:41:15 +02:00
|
|
|
ejabberd_router:route(To,
|
|
|
|
From,
|
|
|
|
jlib:iq_to_xml(Res));
|
2003-12-17 21:13:21 +01:00
|
|
|
#iq{type = set,
|
|
|
|
xmlns = ?NS_REGISTER = XMLNS,
|
2004-03-02 22:16:55 +01:00
|
|
|
lang = Lang,
|
2003-12-17 21:13:21 +01:00
|
|
|
sub_el = SubEl} = IQ ->
|
2005-04-17 20:08:34 +02:00
|
|
|
case process_iq_register_set(Host, From, SubEl, Lang) of
|
2003-05-18 18:41:15 +02:00
|
|
|
{result, IQRes} ->
|
2003-12-17 21:13:21 +01:00
|
|
|
Res = IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[{xmlelement, "query",
|
|
|
|
[{"xmlns", XMLNS}],
|
|
|
|
IQRes}]},
|
2003-05-18 18:41:15 +02:00
|
|
|
ejabberd_router:route(
|
|
|
|
To, From, jlib:iq_to_xml(Res));
|
|
|
|
{error, Error} ->
|
|
|
|
Err = jlib:make_error_reply(
|
|
|
|
Packet, Error),
|
|
|
|
ejabberd_router:route(
|
|
|
|
To, From, Err)
|
|
|
|
end;
|
2003-12-17 21:13:21 +01:00
|
|
|
#iq{type = get,
|
|
|
|
xmlns = ?NS_VCARD = XMLNS,
|
2004-02-26 23:00:04 +01:00
|
|
|
lang = Lang,
|
2006-01-19 03:17:31 +01:00
|
|
|
sub_el = _SubEl} = IQ ->
|
2003-12-17 21:13:21 +01:00
|
|
|
Res = IQ#iq{type = result,
|
|
|
|
sub_el =
|
2004-03-02 22:16:55 +01:00
|
|
|
[{xmlelement, "vCard",
|
2003-12-17 21:13:21 +01:00
|
|
|
[{"xmlns", XMLNS}],
|
|
|
|
iq_get_vcard(Lang)}]},
|
2003-05-18 18:41:15 +02:00
|
|
|
ejabberd_router:route(To,
|
|
|
|
From,
|
|
|
|
jlib:iq_to_xml(Res));
|
2003-12-17 21:13:21 +01:00
|
|
|
#iq{} ->
|
2003-04-17 21:47:43 +02:00
|
|
|
Err = jlib:make_error_reply(
|
2003-05-18 18:41:15 +02:00
|
|
|
Packet,
|
|
|
|
?ERR_FEATURE_NOT_IMPLEMENTED),
|
2003-12-17 21:13:21 +01:00
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
_ ->
|
|
|
|
ok
|
2003-04-17 21:47:43 +02:00
|
|
|
end;
|
2003-05-18 18:41:15 +02:00
|
|
|
"message" ->
|
|
|
|
case xml:get_attr_s("type", Attrs) of
|
|
|
|
"error" ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
2005-06-20 05:18:13 +02:00
|
|
|
case acl:match_rule(ServerHost, AccessAdmin, From) of
|
2003-05-18 18:41:15 +02:00
|
|
|
allow ->
|
|
|
|
Msg = xml:get_path_s(
|
|
|
|
Packet,
|
|
|
|
[{elem, "body"}, cdata]),
|
2005-04-17 20:08:34 +02:00
|
|
|
broadcast_service_message(Host, Msg);
|
2003-05-18 18:41:15 +02:00
|
|
|
_ ->
|
2004-03-02 22:16:55 +01:00
|
|
|
Lang = xml:get_attr_s("xml:lang", Attrs),
|
|
|
|
ErrText = "Only service administrators "
|
|
|
|
"are allowed to send service messages",
|
2003-05-18 18:41:15 +02:00
|
|
|
Err = jlib:make_error_reply(
|
|
|
|
Packet,
|
2004-03-02 22:16:55 +01:00
|
|
|
?ERRT_FORBIDDEN(Lang, ErrText)),
|
2003-05-18 18:41:15 +02:00
|
|
|
ejabberd_router:route(
|
|
|
|
To, From, Err)
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
"presence" ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
case xml:get_attr_s("type", Attrs) of
|
|
|
|
"error" ->
|
|
|
|
ok;
|
|
|
|
"result" ->
|
|
|
|
ok;
|
2003-03-23 21:08:44 +01:00
|
|
|
_ ->
|
|
|
|
Err = jlib:make_error_reply(
|
2003-06-30 14:24:35 +02:00
|
|
|
Packet, ?ERR_ITEM_NOT_FOUND),
|
2003-03-23 21:08:44 +01:00
|
|
|
ejabberd_router:route(To, From, Err)
|
2003-05-18 18:41:15 +02:00
|
|
|
end
|
2003-03-23 21:08:44 +01:00
|
|
|
end;
|
|
|
|
_ ->
|
2007-01-19 05:46:44 +01:00
|
|
|
case mnesia:dirty_read(muc_online_room, {Room, Host}) of
|
2003-03-23 21:08:44 +01:00
|
|
|
[] ->
|
|
|
|
Type = xml:get_attr_s("type", Attrs),
|
|
|
|
case {Name, Type} of
|
|
|
|
{"presence", ""} ->
|
2005-06-20 05:18:13 +02:00
|
|
|
case acl:match_rule(ServerHost, AccessCreate, From) of
|
2004-05-17 22:36:41 +02:00
|
|
|
allow ->
|
|
|
|
?DEBUG("MUC: open new room '~s'~n", [Room]),
|
|
|
|
{ok, Pid} = mod_muc_room:start(
|
2005-06-20 05:18:13 +02:00
|
|
|
Host, ServerHost, Access,
|
2006-09-05 17:53:54 +02:00
|
|
|
Room, HistorySize, From,
|
|
|
|
Nick),
|
2007-01-19 05:46:44 +01:00
|
|
|
register_room(Host, Room, Pid),
|
2004-05-17 22:36:41 +02:00
|
|
|
mod_muc_room:route(Pid, From, Nick, Packet),
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
Lang = xml:get_attr_s("xml:lang", Attrs),
|
|
|
|
ErrText = "Room creation is denied by service policy",
|
|
|
|
Err = jlib:make_error_reply(
|
|
|
|
Packet, ?ERRT_FORBIDDEN(Lang, ErrText)),
|
|
|
|
ejabberd_router:route(To, From, Err)
|
|
|
|
end;
|
2003-03-23 21:08:44 +01:00
|
|
|
_ ->
|
2004-03-02 22:16:55 +01:00
|
|
|
Lang = xml:get_attr_s("xml:lang", Attrs),
|
|
|
|
ErrText = "Conference room does not exist",
|
2003-03-23 21:08:44 +01:00
|
|
|
Err = jlib:make_error_reply(
|
2004-03-02 22:16:55 +01:00
|
|
|
Packet, ?ERRT_ITEM_NOT_FOUND(Lang, ErrText)),
|
2003-03-23 21:08:44 +01:00
|
|
|
ejabberd_router:route(To, From, Err)
|
|
|
|
end;
|
|
|
|
[R] ->
|
|
|
|
Pid = R#muc_online_room.pid,
|
2003-09-26 20:55:01 +02:00
|
|
|
?DEBUG("MUC: send to process ~p~n", [Pid]),
|
2003-03-23 21:08:44 +01:00
|
|
|
mod_muc_room:route(Pid, From, Nick, Packet),
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-09-05 17:53:54 +02:00
|
|
|
load_permanent_rooms(Host, ServerHost, Access, HistorySize) ->
|
2005-06-20 05:18:13 +02:00
|
|
|
case catch mnesia:dirty_select(
|
|
|
|
muc_room, [{#muc_room{name_host = {'_', Host}, _ = '_'},
|
|
|
|
[],
|
|
|
|
['$_']}]) of
|
2003-03-25 22:03:35 +01:00
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p", [Reason]),
|
|
|
|
ok;
|
|
|
|
Rs ->
|
2007-01-19 05:46:44 +01:00
|
|
|
lists:foreach(
|
|
|
|
fun(R) ->
|
|
|
|
{Room, Host} = R#muc_room.name_host,
|
|
|
|
case mnesia:dirty_read(muc_online_room, {Room, Host}) of
|
|
|
|
[] ->
|
|
|
|
{ok, Pid} = mod_muc_room:start(
|
|
|
|
Host,
|
|
|
|
ServerHost,
|
|
|
|
Access,
|
|
|
|
Room,
|
|
|
|
HistorySize,
|
|
|
|
R#muc_room.opts),
|
|
|
|
register_room(Host, Room, Pid);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end, Rs)
|
2003-03-25 22:03:35 +01:00
|
|
|
end.
|
|
|
|
|
2007-01-19 05:46:44 +01:00
|
|
|
register_room(Host, Room, Pid) ->
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:write(#muc_online_room{name_host = {Room, Host},
|
|
|
|
pid = Pid})
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
2003-03-26 21:51:18 +01:00
|
|
|
|
2007-05-29 15:14:09 +02:00
|
|
|
iq_disco_info(Lang) ->
|
2003-03-26 21:51:18 +01:00
|
|
|
[{xmlelement, "identity",
|
|
|
|
[{"category", "conference"},
|
|
|
|
{"type", "text"},
|
2007-05-29 15:14:09 +02:00
|
|
|
{"name", translate:translate(Lang, "Chatrooms")}], []},
|
2003-04-17 21:47:43 +02:00
|
|
|
{xmlelement, "feature", [{"var", ?NS_MUC}], []},
|
2003-05-09 21:32:59 +02:00
|
|
|
{xmlelement, "feature", [{"var", ?NS_REGISTER}], []},
|
|
|
|
{xmlelement, "feature", [{"var", ?NS_VCARD}], []}].
|
2003-03-26 21:51:18 +01:00
|
|
|
|
|
|
|
|
2004-03-08 21:20:15 +01:00
|
|
|
iq_disco_items(Host, From, Lang) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
lists:zf(fun(#muc_online_room{name_host = {Name, _Host}, pid = Pid}) ->
|
2003-03-26 21:51:18 +01:00
|
|
|
case catch gen_fsm:sync_send_all_state_event(
|
2004-03-08 21:20:15 +01:00
|
|
|
Pid, {get_disco_item, From, Lang}, 100) of
|
2003-03-26 21:51:18 +01:00
|
|
|
{item, Desc} ->
|
2007-03-22 03:26:35 +01:00
|
|
|
flush(),
|
2003-03-26 21:51:18 +01:00
|
|
|
{true,
|
|
|
|
{xmlelement, "item",
|
|
|
|
[{"jid", jlib:jid_to_string({Name, Host, ""})},
|
2003-03-27 15:06:17 +01:00
|
|
|
{"name", Desc}], []}};
|
|
|
|
_ ->
|
|
|
|
false
|
2003-03-26 21:51:18 +01:00
|
|
|
end
|
2005-04-17 20:08:34 +02:00
|
|
|
end, get_vh_rooms(Host)).
|
2003-03-26 21:51:18 +01:00
|
|
|
|
2007-03-22 03:26:35 +01:00
|
|
|
flush() ->
|
|
|
|
receive
|
|
|
|
_ ->
|
|
|
|
flush()
|
|
|
|
after 0 ->
|
|
|
|
ok
|
|
|
|
end.
|
2003-03-26 21:51:18 +01:00
|
|
|
|
2003-04-17 21:47:43 +02:00
|
|
|
-define(XFIELD(Type, Label, Var, Val),
|
|
|
|
{xmlelement, "field", [{"type", Type},
|
|
|
|
{"label", translate:translate(Lang, Label)},
|
|
|
|
{"var", Var}],
|
|
|
|
[{xmlelement, "value", [], [{xmlcdata, Val}]}]}).
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
iq_get_register_info(Host, From, Lang) ->
|
2003-04-17 21:47:43 +02:00
|
|
|
{LUser, LServer, _} = jlib:jid_tolower(From),
|
|
|
|
LUS = {LUser, LServer},
|
2005-04-17 20:08:34 +02:00
|
|
|
{Nick, Registered} =
|
|
|
|
case catch mnesia:dirty_read(muc_registered, {LUS, Host}) of
|
|
|
|
{'EXIT', _Reason} ->
|
|
|
|
{"", []};
|
|
|
|
[] ->
|
|
|
|
{"", []};
|
|
|
|
[#muc_registered{nick = N}] ->
|
|
|
|
{N, [{xmlelement, "registered", [], []}]}
|
|
|
|
end,
|
2003-12-06 20:58:49 +01:00
|
|
|
Registered ++
|
|
|
|
[{xmlelement, "instructions", [],
|
|
|
|
[{xmlcdata,
|
|
|
|
translate:translate(
|
2004-02-26 23:00:04 +01:00
|
|
|
Lang, "You need an x:data capable client to register nickname")}]},
|
2003-12-06 20:58:49 +01:00
|
|
|
{xmlelement, "x",
|
|
|
|
[{"xmlns", ?NS_XDATA}],
|
|
|
|
[{xmlelement, "title", [],
|
|
|
|
[{xmlcdata,
|
|
|
|
translate:translate(
|
2004-02-26 23:00:04 +01:00
|
|
|
Lang, "Nickname Registration at ") ++ Host}]},
|
2003-12-06 20:58:49 +01:00
|
|
|
{xmlelement, "instructions", [],
|
|
|
|
[{xmlcdata,
|
|
|
|
translate:translate(
|
2004-02-26 23:00:04 +01:00
|
|
|
Lang, "Enter nickname you want to register")}]},
|
2003-12-06 20:58:49 +01:00
|
|
|
?XFIELD("text-single", "Nickname", "nick", Nick)]}].
|
2003-04-17 21:47:43 +02:00
|
|
|
|
2006-01-19 03:17:31 +01:00
|
|
|
iq_set_register_info(Host, From, Nick, Lang) ->
|
2003-04-17 21:47:43 +02:00
|
|
|
{LUser, LServer, _} = jlib:jid_tolower(From),
|
|
|
|
LUS = {LUser, LServer},
|
2006-01-19 03:17:31 +01:00
|
|
|
F = fun() ->
|
|
|
|
case Nick of
|
|
|
|
"" ->
|
|
|
|
mnesia:delete({muc_registered, {LUS, Host}}),
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
Allow =
|
|
|
|
case mnesia:select(
|
|
|
|
muc_registered,
|
|
|
|
[{#muc_registered{us_host = '$1',
|
|
|
|
nick = Nick,
|
|
|
|
_ = '_'},
|
|
|
|
[{'==', {element, 2, '$1'}, Host}],
|
|
|
|
['$_']}]) of
|
|
|
|
[] ->
|
|
|
|
true;
|
|
|
|
[#muc_registered{us_host = {U, _Host}}] ->
|
|
|
|
U == LUS
|
|
|
|
end,
|
|
|
|
if
|
|
|
|
Allow ->
|
|
|
|
mnesia:write(
|
|
|
|
#muc_registered{us_host = {LUS, Host},
|
|
|
|
nick = Nick}),
|
2003-04-17 21:47:43 +02:00
|
|
|
ok;
|
2006-01-19 03:17:31 +01:00
|
|
|
true ->
|
|
|
|
false
|
2003-04-17 21:47:43 +02:00
|
|
|
end
|
2006-01-19 03:17:31 +01:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, ok} ->
|
|
|
|
{result, []};
|
|
|
|
{atomic, false} ->
|
|
|
|
ErrText = "Specified nickname is already registered",
|
|
|
|
{error, ?ERRT_CONFLICT(Lang, ErrText)};
|
|
|
|
_ ->
|
|
|
|
{error, ?ERR_INTERNAL_SERVER_ERROR}
|
2003-04-17 21:47:43 +02:00
|
|
|
end.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
process_iq_register_set(Host, From, SubEl, Lang) ->
|
2004-05-17 22:36:41 +02:00
|
|
|
{xmlelement, _Name, _Attrs, Els} = SubEl,
|
2006-01-19 03:17:31 +01:00
|
|
|
case xml:get_subtag(SubEl, "remove") of
|
|
|
|
false ->
|
|
|
|
case xml:remove_cdata(Els) of
|
|
|
|
[{xmlelement, "x", _Attrs1, _Els1} = XEl] ->
|
|
|
|
case {xml:get_tag_attr_s("xmlns", XEl),
|
|
|
|
xml:get_tag_attr_s("type", XEl)} of
|
|
|
|
{?NS_XDATA, "cancel"} ->
|
|
|
|
{result, []};
|
|
|
|
{?NS_XDATA, "submit"} ->
|
|
|
|
XData = jlib:parse_xdata_submit(XEl),
|
|
|
|
case XData of
|
|
|
|
invalid ->
|
|
|
|
{error, ?ERR_BAD_REQUEST};
|
|
|
|
_ ->
|
|
|
|
case lists:keysearch("nick", 1, XData) of
|
|
|
|
false ->
|
|
|
|
ErrText = "You must fill in field \"Nickname\" in the form",
|
|
|
|
{error, ?ERRT_NOT_ACCEPTABLE(Lang, ErrText)};
|
|
|
|
{value, {_, [Nick]}} ->
|
|
|
|
iq_set_register_info(Host, From, Nick, Lang)
|
|
|
|
end
|
|
|
|
end;
|
2003-04-17 21:47:43 +02:00
|
|
|
_ ->
|
2006-01-19 03:17:31 +01:00
|
|
|
{error, ?ERR_BAD_REQUEST}
|
2003-05-07 16:17:53 +02:00
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
{error, ?ERR_BAD_REQUEST}
|
2003-04-17 21:47:43 +02:00
|
|
|
end;
|
|
|
|
_ ->
|
2006-01-19 03:17:31 +01:00
|
|
|
iq_set_register_info(Host, From, "", Lang)
|
2003-04-17 21:47:43 +02:00
|
|
|
end.
|
|
|
|
|
2003-05-09 21:32:59 +02:00
|
|
|
iq_get_vcard(Lang) ->
|
|
|
|
[{xmlelement, "FN", [],
|
|
|
|
[{xmlcdata, "ejabberd/mod_muc"}]},
|
|
|
|
{xmlelement, "URL", [],
|
|
|
|
[{xmlcdata,
|
|
|
|
"http://ejabberd.jabberstudio.org/"}]},
|
|
|
|
{xmlelement, "DESC", [],
|
2003-12-06 20:58:49 +01:00
|
|
|
[{xmlcdata, translate:translate(Lang, "ejabberd MUC module\n"
|
2006-02-06 06:12:54 +01:00
|
|
|
"Copyright (c) 2003-2006 Alexey Shchepin")}]}].
|
2003-05-09 21:32:59 +02:00
|
|
|
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
broadcast_service_message(Host, Msg) ->
|
2003-05-18 18:41:15 +02:00
|
|
|
lists:foreach(
|
2004-05-17 22:36:41 +02:00
|
|
|
fun(#muc_online_room{pid = Pid}) ->
|
2003-05-18 18:41:15 +02:00
|
|
|
gen_fsm:send_all_state_event(
|
|
|
|
Pid, {service_message, Msg})
|
2005-04-17 20:08:34 +02:00
|
|
|
end, get_vh_rooms(Host)).
|
2003-05-18 18:41:15 +02:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_vh_rooms(Host) ->
|
2007-01-19 05:46:44 +01:00
|
|
|
mnesia:dirty_select(muc_online_room,
|
|
|
|
[{#muc_online_room{name_host = '$1', _ = '_'},
|
|
|
|
[{'==', {element, 2, '$1'}, Host}],
|
|
|
|
['$_']}]).
|
2003-05-18 18:41:15 +02:00
|
|
|
|
2003-04-17 21:47:43 +02:00
|
|
|
|
2007-02-13 18:23:36 +01:00
|
|
|
clean_table_from_bad_node(Node) ->
|
|
|
|
F = fun() ->
|
|
|
|
Es = mnesia:select(
|
|
|
|
muc_online_room,
|
|
|
|
[{#muc_online_room{pid = '$1', _ = '_'},
|
|
|
|
[{'==', {node, '$1'}, Node}],
|
|
|
|
['$_']}]),
|
|
|
|
lists:foreach(fun(E) ->
|
|
|
|
mnesia:delete_object(E)
|
|
|
|
end, Es)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
2007-05-09 16:47:58 +02:00
|
|
|
clean_table_from_bad_node(Node, Host) ->
|
|
|
|
F = fun() ->
|
|
|
|
Es = mnesia:select(
|
|
|
|
muc_online_room,
|
|
|
|
[{#muc_online_room{pid = '$1',
|
|
|
|
name_host = {'_', Host},
|
|
|
|
_ = '_'},
|
|
|
|
[{'==', {node, '$1'}, Node}],
|
|
|
|
['$_']}]),
|
|
|
|
lists:foreach(fun(E) ->
|
|
|
|
mnesia:delete_object(E)
|
|
|
|
end, Es)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
2003-03-26 21:51:18 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
update_tables(Host) ->
|
|
|
|
update_muc_room_table(Host),
|
|
|
|
update_muc_registered_table(Host).
|
|
|
|
|
|
|
|
update_muc_room_table(Host) ->
|
|
|
|
Fields = record_info(fields, muc_room),
|
|
|
|
case mnesia:table_info(muc_room, attributes) of
|
|
|
|
Fields ->
|
|
|
|
ok;
|
|
|
|
[name, opts] ->
|
|
|
|
?INFO_MSG("Converting muc_room table from "
|
|
|
|
"{name, opts} format", []),
|
|
|
|
{atomic, ok} = mnesia:create_table(
|
|
|
|
mod_muc_tmp_table,
|
|
|
|
[{disc_only_copies, [node()]},
|
|
|
|
{type, bag},
|
|
|
|
{local_content, true},
|
|
|
|
{record_name, muc_room},
|
|
|
|
{attributes, record_info(fields, muc_room)}]),
|
|
|
|
mnesia:transform_table(muc_room, ignore, Fields),
|
|
|
|
F1 = fun() ->
|
|
|
|
mnesia:write_lock_table(mod_muc_tmp_table),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(#muc_room{name_host = Name} = R, _) ->
|
|
|
|
mnesia:dirty_write(
|
|
|
|
mod_muc_tmp_table,
|
|
|
|
R#muc_room{name_host = {Name, Host}})
|
|
|
|
end, ok, muc_room)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F1),
|
|
|
|
mnesia:clear_table(muc_room),
|
|
|
|
F2 = fun() ->
|
|
|
|
mnesia:write_lock_table(muc_room),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(R, _) ->
|
|
|
|
mnesia:dirty_write(R)
|
|
|
|
end, ok, mod_muc_tmp_table)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F2),
|
|
|
|
mnesia:delete_table(mod_muc_tmp_table);
|
|
|
|
_ ->
|
|
|
|
?INFO_MSG("Recreating muc_room table", []),
|
|
|
|
mnesia:transform_table(muc_room, ignore, Fields)
|
|
|
|
end.
|
2003-03-26 21:51:18 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
|
|
|
|
update_muc_registered_table(Host) ->
|
|
|
|
Fields = record_info(fields, muc_registered),
|
|
|
|
case mnesia:table_info(muc_registered, attributes) of
|
|
|
|
Fields ->
|
|
|
|
ok;
|
|
|
|
[user, nick] ->
|
|
|
|
?INFO_MSG("Converting muc_registered table from "
|
|
|
|
"{user, nick} format", []),
|
|
|
|
{atomic, ok} = mnesia:create_table(
|
|
|
|
mod_muc_tmp_table,
|
|
|
|
[{disc_only_copies, [node()]},
|
|
|
|
{type, bag},
|
|
|
|
{local_content, true},
|
|
|
|
{record_name, muc_registered},
|
|
|
|
{attributes, record_info(fields, muc_registered)}]),
|
|
|
|
mnesia:del_table_index(muc_registered, nick),
|
|
|
|
mnesia:transform_table(muc_registered, ignore, Fields),
|
|
|
|
F1 = fun() ->
|
|
|
|
mnesia:write_lock_table(mod_muc_tmp_table),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(#muc_registered{us_host = US} = R, _) ->
|
|
|
|
mnesia:dirty_write(
|
|
|
|
mod_muc_tmp_table,
|
|
|
|
R#muc_registered{us_host = {US, Host}})
|
|
|
|
end, ok, muc_registered)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F1),
|
|
|
|
mnesia:clear_table(muc_registered),
|
|
|
|
F2 = fun() ->
|
|
|
|
mnesia:write_lock_table(muc_registered),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(R, _) ->
|
|
|
|
mnesia:dirty_write(R)
|
|
|
|
end, ok, mod_muc_tmp_table)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F2),
|
|
|
|
mnesia:delete_table(mod_muc_tmp_table);
|
|
|
|
_ ->
|
|
|
|
?INFO_MSG("Recreating muc_registered table", []),
|
|
|
|
mnesia:transform_table(muc_registered, ignore, Fields)
|
|
|
|
end.
|