25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-28 16:34:13 +01:00

avoid calling gen_server on internal events (EJAB-1156)

SVN Revision: 2886
This commit is contained in:
Christophe Romain 2010-01-12 15:14:47 +00:00
parent 6ddff15b39
commit e5373de2b9
3 changed files with 171 additions and 219 deletions

View File

@ -122,6 +122,7 @@
]). ]).
-define(PROCNAME, ejabberd_mod_pubsub). -define(PROCNAME, ejabberd_mod_pubsub).
-define(LOOPNAME, ejabberd_mod_pubsub_loop).
-define(PLUGIN_PREFIX, "node_"). -define(PLUGIN_PREFIX, "node_").
-define(TREE_PREFIX, "nodetree_"). -define(TREE_PREFIX, "nodetree_").
@ -194,6 +195,7 @@ init([ServerHost, Opts]) ->
ets:insert(gen_mod:get_module_proc(ServerHost, config), {last_item_cache, LastItemCache}), ets:insert(gen_mod:get_module_proc(ServerHost, config), {last_item_cache, LastItemCache}),
ets:insert(gen_mod:get_module_proc(ServerHost, config), {max_items_node, MaxItemsNode}), ets:insert(gen_mod:get_module_proc(ServerHost, config), {max_items_node, MaxItemsNode}),
ets:insert(gen_mod:get_module_proc(ServerHost, config), {pep_mapping, PepMapping}), ets:insert(gen_mod:get_module_proc(ServerHost, config), {pep_mapping, PepMapping}),
ets:insert(gen_mod:get_module_proc(ServerHost, config), {host, Host}),
ejabberd_hooks:add(disco_sm_identity, ServerHost, ?MODULE, disco_sm_identity, 75), ejabberd_hooks:add(disco_sm_identity, ServerHost, ?MODULE, disco_sm_identity, 75),
ejabberd_hooks:add(disco_sm_features, ServerHost, ?MODULE, disco_sm_features, 75), ejabberd_hooks:add(disco_sm_features, ServerHost, ?MODULE, disco_sm_features, 75),
ejabberd_hooks:add(disco_sm_items, ServerHost, ?MODULE, disco_sm_items, 75), ejabberd_hooks:add(disco_sm_items, ServerHost, ?MODULE, disco_sm_items, 75),
@ -227,7 +229,8 @@ init([ServerHost, Opts]) ->
max_items_node = MaxItemsNode, max_items_node = MaxItemsNode,
nodetree = NodeTree, nodetree = NodeTree,
plugins = Plugins}, plugins = Plugins},
SendLoop = spawn(?MODULE, send_loop, [State]), SendLoop = spawn(?MODULE, send_loop, [State]), %% TODO put this in supervision
register(gen_mod:get_module_proc(ServerHost, ?LOOPNAME), SendLoop),
{ok, State#state{send_loop = SendLoop}}. {ok, State#state{send_loop = SendLoop}}.
%% @spec (Host, ServerHost, Opts) -> Plugins %% @spec (Host, ServerHost, Opts) -> Plugins
@ -470,18 +473,6 @@ update_state_database(_Host, _ServerHost) ->
ok ok
end. end.
send_queue(State, Msg) ->
Pid = State#state.send_loop,
case is_process_alive(Pid) of
true ->
Pid ! Msg,
State;
false ->
SendLoop = spawn(?MODULE, send_loop, [State]),
SendLoop ! Msg,
State#state{send_loop = SendLoop}
end.
send_loop(State) -> send_loop(State) ->
receive receive
{presence, JID, Pid} -> {presence, JID, Pid} ->
@ -717,18 +708,20 @@ disco_sm_items(Acc, From, To, SNode, _Lang) ->
%% %%
presence_probe(#jid{luser = User, lserver = Server, lresource = Resource} = JID, JID, Pid) -> presence_probe(#jid{luser = User, lserver = Server, lresource = Resource} = JID, JID, Pid) ->
Proc = gen_mod:get_module_proc(Server, ?PROCNAME),
%%?DEBUG("presence probe self ~s@~s/~s ~s@~s/~s",[User,Server,Resource,element(2,JID),element(3,JID),element(4,JID)]), %%?DEBUG("presence probe self ~s@~s/~s ~s@~s/~s",[User,Server,Resource,element(2,JID),element(3,JID),element(4,JID)]),
gen_server:cast(Proc, {presence, JID, Pid}), presence(Server, {presence, JID, Pid}),
gen_server:cast(Proc, {presence, User, Server, [Resource], JID}); presence(Server, {presence, User, Server, [Resource], JID});
presence_probe(#jid{luser = User, lserver = Server}, #jid{luser = User, lserver = Server}, _Pid) -> presence_probe(#jid{luser = User, lserver = Server}, #jid{luser = User, lserver = Server}, _Pid) ->
%% ignore presence_probe from other ressources for the current user %% ignore presence_probe from other ressources for the current user
%% this way, we do not send duplicated last items if user already connected with other clients %% this way, we do not send duplicated last items if user already connected with other clients
ok; ok;
presence_probe(#jid{luser = User, lserver = Server, lresource = Resource}, #jid{lserver = Host} = JID, _Pid) -> presence_probe(#jid{luser = User, lserver = Server, lresource = Resource}, #jid{lserver = Host} = JID, _Pid) ->
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
%%?DEBUG("presence probe peer ~s@~s/~s ~s@~s/~s",[User,Server,Resource,element(2,JID),element(3,JID),element(4,JID)]), %%?DEBUG("presence probe peer ~s@~s/~s ~s@~s/~s",[User,Server,Resource,element(2,JID),element(3,JID),element(4,JID)]),
gen_server:cast(Proc, {presence, User, Server, [Resource], JID}). presence(Host, {presence, User, Server, [Resource], JID}).
presence(ServerHost, Presence) ->
Proc = gen_mod:get_module_proc(ServerHost, ?LOOPNAME),
Proc ! Presence.
%% ------- %% -------
%% subscription hooks handling functions %% subscription hooks handling functions
@ -741,17 +734,39 @@ out_subscription(User, Server, JID, subscribed) ->
[] -> user_resources(PUser, PServer); [] -> user_resources(PUser, PServer);
_ -> [PResource] _ -> [PResource]
end, end,
Proc = gen_mod:get_module_proc(Server, ?PROCNAME), presence(Server, {presence, PUser, PServer, PResources, Owner});
gen_server:cast(Proc, {presence, PUser, PServer, PResources, Owner});
out_subscription(_,_,_,_) -> out_subscription(_,_,_,_) ->
ok. ok.
in_subscription(_, User, Server, Owner, unsubscribed, _) -> in_subscription(_, User, Server, Owner, unsubscribed, _) ->
Subscriber = jlib:make_jid(User, Server, ""), unsubscribe_user(jlib:make_jid(User, Server, ""), Owner);
Proc = gen_mod:get_module_proc(Server, ?PROCNAME),
gen_server:cast(Proc, {unsubscribe, Subscriber, Owner});
in_subscription(_, _, _, _, _, _) -> in_subscription(_, _, _, _, _, _) ->
ok. ok.
unsubscribe_user(Entity, Owner) ->
BJID = jlib:jid_tolower(jlib:jid_remove_resource(Owner)),
Host = host(element(2, BJID)),
spawn(fun() ->
lists:foreach(fun(PType) ->
{result, Subscriptions} = node_action(Host, PType, get_entity_subscriptions, [Host, Entity]),
lists:foreach(fun
({#pubsub_node{options = Options, owners = Owners, id = NodeId}, subscribed, _, JID}) ->
case get_option(Options, access_model) of
presence ->
case lists:member(BJID, Owners) of
true ->
node_action(Host, PType, unsubscribe_node, [NodeId, Entity, JID, all]);
false ->
{result, ok}
end;
_ ->
{result, ok}
end;
(_) ->
ok
end, Subscriptions)
end, plugins(Host))
end).
%% ------- %% -------
%% user remove hook handling function %% user remove hook handling function
%% %%
@ -759,8 +774,22 @@ in_subscription(_, _, _, _, _, _) ->
remove_user(User, Server) -> remove_user(User, Server) ->
LUser = jlib:nodeprep(User), LUser = jlib:nodeprep(User),
LServer = jlib:nameprep(Server), LServer = jlib:nameprep(Server),
Proc = gen_mod:get_module_proc(Server, ?PROCNAME), Entity = jlib:make_jid(LUser, LServer, ""),
gen_server:cast(Proc, {remove_user, LUser, LServer}). Host = host(LServer),
spawn(fun() ->
lists:foreach(fun(PType) ->
{result, Subscriptions} = node_action(Host, PType, get_entity_subscriptions, [Host, Entity]),
lists:foreach(fun
({#pubsub_node{id = NodeId}, subscribed, _, JID}) -> node_action(Host, PType, unsubscribe_node, [NodeId, Entity, JID, all]);
(_) -> ok
end, Subscriptions),
{result, Affiliations} = node_action(Host, PType, get_entity_affiliations, [Host, Entity]),
lists:foreach(fun
({#pubsub_node{nodeid = {H, N}}, owner}) -> delete_node(H, N, Entity);
({#pubsub_node{id = NodeId}, _}) -> node_action(Host, PType, set_affiliation, [NodeId, Entity, none])
end, Affiliations)
end, plugins(Host))
end).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Function: %% Function:
@ -791,65 +820,6 @@ handle_call(stop, _From, State) ->
%% Description: Handling cast messages %% Description: Handling cast messages
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @private %% @private
handle_cast({presence, JID, Pid}, State) ->
%% A new resource is available. send last published items
{noreply, send_queue(State, {presence, JID, Pid})};
handle_cast({presence, User, Server, Resources, JID}, State) ->
%% A new resource is available. send last published PEP items
{noreply, send_queue(State, {presence, User, Server, Resources, JID})};
handle_cast({remove_user, LUser, LServer}, State) ->
spawn(fun() ->
Host = State#state.host,
Owner = jlib:make_jid(LUser, LServer, ""),
%% remove user's subscriptions
lists:foreach(fun(PType) ->
{result, Subscriptions} = node_action(Host, PType, get_entity_subscriptions, [Host, Owner]),
lists:foreach(fun
({#pubsub_node{nodeid = {H, N}}, subscribed, _, JID}) ->
unsubscribe_node(H, N, Owner, JID, all);
(_) ->
ok
end, Subscriptions),
{result, Affiliations} = node_action(Host, PType, get_entity_affiliations, [Host, Owner]),
lists:foreach(fun
({#pubsub_node{nodeid = {H, N}}, owner}) ->
delete_node(H, N, Owner);
(_) ->
ok
end, Affiliations)
end, State#state.plugins)
end),
{noreply, State};
handle_cast({unsubscribe, Subscriber, Owner}, State) ->
spawn(fun() ->
Host = State#state.host,
BJID = jlib:jid_tolower(jlib:jid_remove_resource(Owner)),
lists:foreach(fun(PType) ->
{result, Subscriptions} = node_action(Host, PType, get_entity_subscriptions, [Host, Subscriber]),
lists:foreach(fun
({Node, subscribed, _, JID}) ->
#pubsub_node{options = Options, owners = Owners, type = Type, id = NodeId} = Node,
case get_option(Options, access_model) of
presence ->
case lists:member(BJID, Owners) of
true ->
node_action(Host, Type, unsubscribe_node, [NodeId, Subscriber, JID, all]);
false ->
{result, ok}
end;
_ ->
{result, ok}
end;
(_) ->
ok
end, Subscriptions)
end, State#state.plugins)
end),
{noreply, State};
handle_cast(_Msg, State) -> handle_cast(_Msg, State) ->
{noreply, State}. {noreply, State}.
@ -3580,6 +3550,12 @@ get_cached_item(Host, NodeId) ->
%%%% plugin handling %%%% plugin handling
host(ServerHost) ->
case catch ets:lookup(gen_mod:get_module_proc(ServerHost, config), host) of
[{host, Host}] -> Host;
_ -> "pubsub."++ServerHost
end.
plugins(Host) -> plugins(Host) ->
case catch ets:lookup(gen_mod:get_module_proc(Host, config), plugins) of case catch ets:lookup(gen_mod:get_module_proc(Host, config), plugins) of
[{plugins, []}] -> [?STDNODE]; [{plugins, []}] -> [?STDNODE];

View File

@ -122,6 +122,7 @@
]). ]).
-define(PROCNAME, ejabberd_mod_pubsub_odbc). -define(PROCNAME, ejabberd_mod_pubsub_odbc).
-define(LOOPNAME, ejabberd_mod_pubsub_loop).
-define(PLUGIN_PREFIX, "node_"). -define(PLUGIN_PREFIX, "node_").
-define(TREE_PREFIX, "nodetree_"). -define(TREE_PREFIX, "nodetree_").
@ -194,6 +195,7 @@ init([ServerHost, Opts]) ->
ets:insert(gen_mod:get_module_proc(ServerHost, config), {last_item_cache, LastItemCache}), ets:insert(gen_mod:get_module_proc(ServerHost, config), {last_item_cache, LastItemCache}),
ets:insert(gen_mod:get_module_proc(ServerHost, config), {max_items_node, MaxItemsNode}), ets:insert(gen_mod:get_module_proc(ServerHost, config), {max_items_node, MaxItemsNode}),
ets:insert(gen_mod:get_module_proc(ServerHost, config), {pep_mapping, PepMapping}), ets:insert(gen_mod:get_module_proc(ServerHost, config), {pep_mapping, PepMapping}),
ets:insert(gen_mod:get_module_proc(ServerHost, config), {host, Host}),
ejabberd_hooks:add(disco_sm_identity, ServerHost, ?MODULE, disco_sm_identity, 75), ejabberd_hooks:add(disco_sm_identity, ServerHost, ?MODULE, disco_sm_identity, 75),
ejabberd_hooks:add(disco_sm_features, ServerHost, ?MODULE, disco_sm_features, 75), ejabberd_hooks:add(disco_sm_features, ServerHost, ?MODULE, disco_sm_features, 75),
ejabberd_hooks:add(disco_sm_items, ServerHost, ?MODULE, disco_sm_items, 75), ejabberd_hooks:add(disco_sm_items, ServerHost, ?MODULE, disco_sm_items, 75),
@ -225,7 +227,8 @@ init([ServerHost, Opts]) ->
max_items_node = MaxItemsNode, max_items_node = MaxItemsNode,
nodetree = NodeTree, nodetree = NodeTree,
plugins = Plugins}, plugins = Plugins},
SendLoop = spawn(?MODULE, send_loop, [State]), SendLoop = spawn(?MODULE, send_loop, [State]), %% TODO put this in supervision
register(gen_mod:get_module_proc(ServerHost, ?LOOPNAME), SendLoop),
{ok, State#state{send_loop = SendLoop}}. {ok, State#state{send_loop = SendLoop}}.
%% @spec (Host, ServerHost, Opts) -> Plugins %% @spec (Host, ServerHost, Opts) -> Plugins
@ -275,18 +278,6 @@ init_nodes(Host, ServerHost, _NodeTree, Plugins) ->
ok ok
end. end.
send_queue(State, Msg) ->
Pid = State#state.send_loop,
case is_process_alive(Pid) of
true ->
Pid ! Msg,
State;
false ->
SendLoop = spawn(?MODULE, send_loop, [State]),
SendLoop ! Msg,
State#state{send_loop = SendLoop}
end.
send_loop(State) -> send_loop(State) ->
receive receive
{presence, JID, Pid} -> {presence, JID, Pid} ->
@ -520,18 +511,20 @@ disco_sm_items(Acc, From, To, SNode, _Lang) ->
%% %%
presence_probe(#jid{luser = User, lserver = Server, lresource = Resource} = JID, JID, Pid) -> presence_probe(#jid{luser = User, lserver = Server, lresource = Resource} = JID, JID, Pid) ->
Proc = gen_mod:get_module_proc(Server, ?PROCNAME),
%%?DEBUG("presence probe self ~s@~s/~s ~s@~s/~s",[User,Server,Resource,element(2,JID),element(3,JID),element(4,JID)]), %%?DEBUG("presence probe self ~s@~s/~s ~s@~s/~s",[User,Server,Resource,element(2,JID),element(3,JID),element(4,JID)]),
gen_server:cast(Proc, {presence, JID, Pid}), presence(Server, {presence, JID, Pid}),
gen_server:cast(Proc, {presence, User, Server, [Resource], JID}); presence(Server, {presence, User, Server, [Resource], JID});
presence_probe(#jid{luser = User, lserver = Server}, #jid{luser = User, lserver = Server}, _Pid) -> presence_probe(#jid{luser = User, lserver = Server}, #jid{luser = User, lserver = Server}, _Pid) ->
%% ignore presence_probe from other ressources for the current user %% ignore presence_probe from other ressources for the current user
%% this way, we do not send duplicated last items if user already connected with other clients %% this way, we do not send duplicated last items if user already connected with other clients
ok; ok;
presence_probe(#jid{luser = User, lserver = Server, lresource = Resource}, #jid{lserver = Host} = JID, _Pid) -> presence_probe(#jid{luser = User, lserver = Server, lresource = Resource}, #jid{lserver = Host} = JID, _Pid) ->
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
%%?DEBUG("presence probe peer ~s@~s/~s ~s@~s/~s",[User,Server,Resource,element(2,JID),element(3,JID),element(4,JID)]), %%?DEBUG("presence probe peer ~s@~s/~s ~s@~s/~s",[User,Server,Resource,element(2,JID),element(3,JID),element(4,JID)]),
gen_server:cast(Proc, {presence, User, Server, [Resource], JID}). presence(Host, {presence, User, Server, [Resource], JID}).
presence(ServerHost, Presence) ->
Proc = gen_mod:get_module_proc(ServerHost, ?LOOPNAME),
Proc ! Presence.
%% ------- %% -------
%% subscription hooks handling functions %% subscription hooks handling functions
@ -544,17 +537,39 @@ out_subscription(User, Server, JID, subscribed) ->
[] -> user_resources(PUser, PServer); [] -> user_resources(PUser, PServer);
_ -> [PResource] _ -> [PResource]
end, end,
Proc = gen_mod:get_module_proc(Server, ?PROCNAME), presence(Server, {presence, PUser, PServer, PResources, Owner});
gen_server:cast(Proc, {presence, PUser, PServer, PResources, Owner});
out_subscription(_,_,_,_) -> out_subscription(_,_,_,_) ->
ok. ok.
in_subscription(_, User, Server, Owner, unsubscribed, _) -> in_subscription(_, User, Server, Owner, unsubscribed, _) ->
Subscriber = jlib:make_jid(User, Server, ""), unsubscribe_user(jlib:make_jid(User, Server, ""), Owner);
Proc = gen_mod:get_module_proc(Server, ?PROCNAME),
gen_server:cast(Proc, {unsubscribe, Subscriber, Owner});
in_subscription(_, _, _, _, _, _) -> in_subscription(_, _, _, _, _, _) ->
ok. ok.
unsubscribe_user(Entity, Owner) ->
BJID = jlib:jid_tolower(jlib:jid_remove_resource(Owner)),
Host = host(element(2, BJID)),
spawn(fun() ->
lists:foreach(fun(PType) ->
{result, Subscriptions} = node_action(Host, PType, get_entity_subscriptions, [Host, Entity]),
lists:foreach(fun
({#pubsub_node{options = Options, id = NodeId}, subscribed, _, JID}) ->
case get_option(Options, access_model) of
presence ->
case lists:member(BJID, node_owners(Host, PType, NodeId)) of
true ->
node_action(Host, PType, unsubscribe_node, [NodeId, Entity, JID, all]);
false ->
{result, ok}
end;
_ ->
{result, ok}
end;
(_) ->
ok
end, Subscriptions)
end, plugins(Host))
end).
%% ------- %% -------
%% user remove hook handling function %% user remove hook handling function
%% %%
@ -562,8 +577,22 @@ in_subscription(_, _, _, _, _, _) ->
remove_user(User, Server) -> remove_user(User, Server) ->
LUser = jlib:nodeprep(User), LUser = jlib:nodeprep(User),
LServer = jlib:nameprep(Server), LServer = jlib:nameprep(Server),
Proc = gen_mod:get_module_proc(Server, ?PROCNAME), Entity = jlib:make_jid(LUser, LServer, ""),
gen_server:cast(Proc, {remove_user, LUser, LServer}). Host = host(LServer),
spawn(fun() ->
lists:foreach(fun(PType) ->
{result, Subscriptions} = node_action(Host, PType, get_entity_subscriptions, [Host, Entity]),
lists:foreach(fun
({#pubsub_node{id = NodeId}, subscribed, _, JID}) -> node_action(Host, PType, unsubscribe_node, [NodeId, Entity, JID, all]);
(_) -> ok
end, Subscriptions),
{result, Affiliations} = node_action(Host, PType, get_entity_affiliations, [Host, Entity]),
lists:foreach(fun
({#pubsub_node{nodeid = {H, N}}, owner}) -> delete_node(H, N, Entity);
({#pubsub_node{id = NodeId}, _}) -> node_action(Host, PType, set_affiliation, [NodeId, Entity, none])
end, Affiliations)
end, plugins(Host))
end).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Function: %% Function:
@ -594,65 +623,6 @@ handle_call(stop, _From, State) ->
%% Description: Handling cast messages %% Description: Handling cast messages
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @private %% @private
handle_cast({presence, JID, Pid}, State) ->
%% A new resource is available. send last published items
{noreply, send_queue(State, {presence, JID, Pid})};
handle_cast({presence, User, Server, Resources, JID}, State) ->
%% A new resource is available. send last published PEP items
{noreply, send_queue(State, {presence, User, Server, Resources, JID})};
handle_cast({remove_user, LUser, LServer}, State) ->
spawn(fun() ->
Host = State#state.host,
Owner = jlib:make_jid(LUser, LServer, ""),
%% remove user's subscriptions
lists:foreach(fun(PType) ->
{result, Subscriptions} = node_action(Host, PType, get_entity_subscriptions, [Host, Owner]),
lists:foreach(fun
({#pubsub_node{nodeid = {H, N}}, subscribed, _, JID}) ->
unsubscribe_node(H, N, Owner, JID, all);
(_) ->
ok
end, Subscriptions),
{result, Affiliations} = node_action(Host, PType, get_entity_affiliations, [Host, Owner]),
lists:foreach(fun
({#pubsub_node{nodeid = {H, N}}, owner}) ->
delete_node(H, N, Owner);
(_) ->
ok
end, Affiliations)
end, State#state.plugins)
end),
{noreply, State};
handle_cast({unsubscribe, Subscriber, Owner}, State) ->
spawn(fun() ->
Host = State#state.host,
BJID = jlib:jid_tolower(jlib:jid_remove_resource(Owner)),
lists:foreach(fun(PType) ->
{result, Subscriptions} = node_action(Host, PType, get_entity_subscriptions, [Host, Subscriber]),
lists:foreach(fun
({Node, subscribed, _, JID}) ->
#pubsub_node{options = Options, type = Type, id = NodeId} = Node,
case get_option(Options, access_model) of
presence ->
case lists:member(BJID, node_owners(Host, Type, NodeId)) of
true ->
node_action(Host, Type, unsubscribe_node, [NodeId, Subscriber, JID, all]);
false ->
{result, ok}
end;
_ ->
{result, ok}
end;
(_) ->
ok
end, Subscriptions)
end, State#state.plugins)
end),
{noreply, State};
handle_cast(_Msg, State) -> handle_cast(_Msg, State) ->
{noreply, State}. {noreply, State}.
@ -3413,6 +3383,12 @@ get_cached_item(Host, NodeId) ->
%%%% plugin handling %%%% plugin handling
host(ServerHost) ->
case catch ets:lookup(gen_mod:get_module_proc(ServerHost, config), host) of
[{host, Host}] -> Host;
_ -> "pubsub."++ServerHost
end.
plugins(Host) -> plugins(Host) ->
case catch ets:lookup(gen_mod:get_module_proc(Host, config), plugins) of case catch ets:lookup(gen_mod:get_module_proc(Host, config), plugins) of
[{plugins, []}] -> [?STDNODE]; [{plugins, []}] -> [?STDNODE];

View File

@ -1,5 +1,5 @@
--- mod_pubsub.erl 2010-01-12 14:25:24.809994066 +0100 --- mod_pubsub.erl 2010-01-12 16:09:01.000000000 +0100
+++ mod_pubsub_odbc.erl 2010-01-12 14:26:04.098050300 +0100 +++ mod_pubsub_odbc.erl 2010-01-12 16:09:09.000000000 +0100
@@ -42,7 +42,7 @@ @@ -42,7 +42,7 @@
%%% 6.2.3.1, 6.2.3.5, and 6.3. For information on subscription leases see %%% 6.2.3.1, 6.2.3.5, and 6.3. For information on subscription leases see
%%% XEP-0060 section 12.18. %%% XEP-0060 section 12.18.
@ -37,10 +37,10 @@
--define(PROCNAME, ejabberd_mod_pubsub). --define(PROCNAME, ejabberd_mod_pubsub).
+-define(PROCNAME, ejabberd_mod_pubsub_odbc). +-define(PROCNAME, ejabberd_mod_pubsub_odbc).
-define(LOOPNAME, ejabberd_mod_pubsub_loop).
-define(PLUGIN_PREFIX, "node_"). -define(PLUGIN_PREFIX, "node_").
-define(TREE_PREFIX, "nodetree_"). -define(TREE_PREFIX, "nodetree_").
@@ -217,8 +217,6 @@
@@ -215,8 +215,6 @@
ok ok
end, end,
ejabberd_router:register_route(Host), ejabberd_router:register_route(Host),
@ -49,7 +49,7 @@
init_nodes(Host, ServerHost, NodeTree, Plugins), init_nodes(Host, ServerHost, NodeTree, Plugins),
State = #state{host = Host, State = #state{host = Host,
server_host = ServerHost, server_host = ServerHost,
@@ -269,207 +267,14 @@ @@ -272,207 +270,14 @@
init_nodes(Host, ServerHost, _NodeTree, Plugins) -> init_nodes(Host, ServerHost, _NodeTree, Plugins) ->
%% TODO, this call should be done plugin side %% TODO, this call should be done plugin side
@ -257,10 +257,10 @@
- ok - ok
- end. - end.
- -
send_queue(State, Msg) -> send_loop(State) ->
Pid = State#state.send_loop, receive
case is_process_alive(Pid) of {presence, JID, Pid} ->
@@ -492,17 +297,15 @@ @@ -483,17 +288,15 @@
%% for each node From is subscribed to %% for each node From is subscribed to
%% and if the node is so configured, send the last published item to From %% and if the node is so configured, send the last published item to From
lists:foreach(fun(PType) -> lists:foreach(fun(PType) ->
@ -284,20 +284,20 @@
true -> true ->
% resource not concerned about that subscription % resource not concerned about that subscription
ok ok
@@ -831,10 +634,10 @@ @@ -749,10 +552,10 @@
{result, Subscriptions} = node_action(Host, PType, get_entity_subscriptions, [Host, Subscriber]), lists:foreach(fun(PType) ->
{result, Subscriptions} = node_action(Host, PType, get_entity_subscriptions, [Host, Entity]),
lists:foreach(fun lists:foreach(fun
({Node, subscribed, _, JID}) -> - ({#pubsub_node{options = Options, owners = Owners, id = NodeId}, subscribed, _, JID}) ->
- #pubsub_node{options = Options, owners = Owners, type = Type, id = NodeId} = Node, + ({#pubsub_node{options = Options, id = NodeId}, subscribed, _, JID}) ->
+ #pubsub_node{options = Options, type = Type, id = NodeId} = Node,
case get_option(Options, access_model) of case get_option(Options, access_model) of
presence -> presence ->
- case lists:member(BJID, Owners) of - case lists:member(BJID, Owners) of
+ case lists:member(BJID, node_owners(Host, Type, NodeId)) of + case lists:member(BJID, node_owners(Host, PType, NodeId)) of
true -> true ->
node_action(Host, Type, unsubscribe_node, [NodeId, Subscriber, JID, all]); node_action(Host, PType, unsubscribe_node, [NodeId, Entity, JID, all]);
false -> false ->
@@ -949,7 +752,8 @@ @@ -919,7 +722,8 @@
sub_el = SubEl} = IQ -> sub_el = SubEl} = IQ ->
{xmlelement, _, QAttrs, _} = SubEl, {xmlelement, _, QAttrs, _} = SubEl,
Node = xml:get_attr_s("node", QAttrs), Node = xml:get_attr_s("node", QAttrs),
@ -307,7 +307,7 @@
{result, IQRes} -> {result, IQRes} ->
jlib:iq_to_xml( jlib:iq_to_xml(
IQ#iq{type = result, IQ#iq{type = result,
@@ -1066,7 +870,7 @@ @@ -1036,7 +840,7 @@
[] -> [] ->
["leaf"]; %% No sub-nodes: it's a leaf node ["leaf"]; %% No sub-nodes: it's a leaf node
_ -> _ ->
@ -316,7 +316,7 @@
{result, []} -> ["collection"]; {result, []} -> ["collection"];
{result, _} -> ["leaf", "collection"]; {result, _} -> ["leaf", "collection"];
_ -> [] _ -> []
@@ -1082,8 +886,9 @@ @@ -1052,8 +856,9 @@
[]; [];
true -> true ->
[{xmlelement, "feature", [{"var", ?NS_PUBSUB}], []} | [{xmlelement, "feature", [{"var", ?NS_PUBSUB}], []} |
@ -328,7 +328,7 @@
end, features(Type))] end, features(Type))]
end, end,
%% TODO: add meta-data info (spec section 5.4) %% TODO: add meta-data info (spec section 5.4)
@@ -1112,8 +917,9 @@ @@ -1082,8 +887,9 @@
{xmlelement, "feature", [{"var", ?NS_PUBSUB}], []}, {xmlelement, "feature", [{"var", ?NS_PUBSUB}], []},
{xmlelement, "feature", [{"var", ?NS_COMMANDS}], []}, {xmlelement, "feature", [{"var", ?NS_COMMANDS}], []},
{xmlelement, "feature", [{"var", ?NS_VCARD}], []}] ++ {xmlelement, "feature", [{"var", ?NS_VCARD}], []}] ++
@ -340,7 +340,7 @@
end, features(Host, Node))}; end, features(Host, Node))};
<<?NS_COMMANDS>> -> <<?NS_COMMANDS>> ->
command_disco_info(Host, Node, From); command_disco_info(Host, Node, From);
@@ -1123,7 +929,7 @@ @@ -1093,7 +899,7 @@
node_disco_info(Host, Node, From) node_disco_info(Host, Node, From)
end. end.
@ -349,7 +349,7 @@
case tree_action(Host, get_subnodes, [Host, <<>>, From]) of case tree_action(Host, get_subnodes, [Host, <<>>, From]) of
Nodes when is_list(Nodes) -> Nodes when is_list(Nodes) ->
{result, lists:map( {result, lists:map(
@@ -1135,14 +941,14 @@ @@ -1105,14 +911,14 @@
Other -> Other ->
Other Other
end; end;
@ -367,7 +367,7 @@
case string:tokens(Item, "!") of case string:tokens(Item, "!") of
[_SNode, _ItemID] -> [_SNode, _ItemID] ->
{result, []}; {result, []};
@@ -1150,10 +956,10 @@ @@ -1120,10 +926,10 @@
Node = string_to_node(SNode), Node = string_to_node(SNode),
Action = Action =
fun(#pubsub_node{type = Type, id = NodeId}) -> fun(#pubsub_node{type = Type, id = NodeId}) ->
@ -381,7 +381,7 @@
end, end,
Nodes = lists:map( Nodes = lists:map(
fun(#pubsub_node{nodeid = {_, SubNode}}) -> fun(#pubsub_node{nodeid = {_, SubNode}}) ->
@@ -1166,7 +972,7 @@ @@ -1136,7 +942,7 @@
{result, Name} = node_call(Type, get_item_name, [Host, Node, RN]), {result, Name} = node_call(Type, get_item_name, [Host, Node, RN]),
{xmlelement, "item", [{"jid", Host}, {"name", Name}], []} {xmlelement, "item", [{"jid", Host}, {"name", Name}], []}
end, NodeItems), end, NodeItems),
@ -390,7 +390,7 @@
end, end,
case transaction(Host, Node, Action, sync_dirty) of case transaction(Host, Node, Action, sync_dirty) of
{result, {_, Result}} -> {result, Result}; {result, {_, Result}} -> {result, Result};
@@ -1295,7 +1101,8 @@ @@ -1265,7 +1071,8 @@
(_, Acc) -> (_, Acc) ->
Acc Acc
end, [], xml:remove_cdata(Els)), end, [], xml:remove_cdata(Els)),
@ -400,7 +400,7 @@
{get, "subscriptions"} -> {get, "subscriptions"} ->
get_subscriptions(Host, Node, From, Plugins); get_subscriptions(Host, Node, From, Plugins);
{get, "affiliations"} -> {get, "affiliations"} ->
@@ -1318,7 +1125,9 @@ @@ -1288,7 +1095,9 @@
iq_pubsub_owner(Host, ServerHost, From, IQType, SubEl, Lang) -> iq_pubsub_owner(Host, ServerHost, From, IQType, SubEl, Lang) ->
{xmlelement, _, _, SubEls} = SubEl, {xmlelement, _, _, SubEls} = SubEl,
@ -411,7 +411,7 @@
case Action of case Action of
[{xmlelement, Name, Attrs, Els}] -> [{xmlelement, Name, Attrs, Els}] ->
Node = string_to_node(xml:get_attr_s("node", Attrs)), Node = string_to_node(xml:get_attr_s("node", Attrs)),
@@ -1448,7 +1257,8 @@ @@ -1418,7 +1227,8 @@
_ -> [] _ -> []
end end
end, end,
@ -421,7 +421,7 @@
sync_dirty) of sync_dirty) of
{result, Res} -> Res; {result, Res} -> Res;
Err -> Err Err -> Err
@@ -1487,7 +1297,7 @@ @@ -1457,7 +1267,7 @@
%%% authorization handling %%% authorization handling
@ -430,7 +430,7 @@
Lang = "en", %% TODO fix Lang = "en", %% TODO fix
Stanza = {xmlelement, "message", Stanza = {xmlelement, "message",
[], [],
@@ -1516,7 +1326,7 @@ @@ -1486,7 +1296,7 @@
[{xmlelement, "value", [], [{xmlcdata, "false"}]}]}]}]}, [{xmlelement, "value", [], [{xmlcdata, "false"}]}]}]}]},
lists:foreach(fun(Owner) -> lists:foreach(fun(Owner) ->
ejabberd_router:route(service_jid(Host), jlib:make_jid(Owner), Stanza) ejabberd_router:route(service_jid(Host), jlib:make_jid(Owner), Stanza)
@ -439,7 +439,7 @@
find_authorization_response(Packet) -> find_authorization_response(Packet) ->
{xmlelement, _Name, _Attrs, Els} = Packet, {xmlelement, _Name, _Attrs, Els} = Packet,
@@ -1580,8 +1390,8 @@ @@ -1550,8 +1360,8 @@
"true" -> true; "true" -> true;
_ -> false _ -> false
end, end,
@ -450,7 +450,7 @@
{result, Subscriptions} = node_call(Type, get_subscriptions, [NodeId, Subscriber]), {result, Subscriptions} = node_call(Type, get_subscriptions, [NodeId, Subscriber]),
if if
not IsApprover -> not IsApprover ->
@@ -1772,7 +1582,7 @@ @@ -1742,7 +1552,7 @@
Reply = [{xmlelement, "pubsub", [{"xmlns", ?NS_PUBSUB}], Reply = [{xmlelement, "pubsub", [{"xmlns", ?NS_PUBSUB}],
[{xmlelement, "create", nodeAttr(Node), [{xmlelement, "create", nodeAttr(Node),
[]}]}], []}]}],
@ -459,7 +459,7 @@
{result, {Result, broadcast}} -> {result, {Result, broadcast}} ->
%%Lang = "en", %% TODO: fix %%Lang = "en", %% TODO: fix
%%OwnerKey = jlib:jid_tolower(jlib:jid_remove_resource(Owner)), %%OwnerKey = jlib:jid_tolower(jlib:jid_remove_resource(Owner)),
@@ -1880,7 +1690,7 @@ @@ -1850,7 +1660,7 @@
%%<li>The node does not exist.</li> %%<li>The node does not exist.</li>
%%</ul> %%</ul>
subscribe_node(Host, Node, From, JID, Configuration) -> subscribe_node(Host, Node, From, JID, Configuration) ->
@ -468,7 +468,7 @@
{result, GoodSubOpts} -> GoodSubOpts; {result, GoodSubOpts} -> GoodSubOpts;
_ -> invalid _ -> invalid
end, end,
@@ -1888,7 +1698,7 @@ @@ -1858,7 +1668,7 @@
error -> {"", "", ""}; error -> {"", "", ""};
J -> jlib:jid_tolower(J) J -> jlib:jid_tolower(J)
end, end,
@ -477,7 +477,7 @@
Features = features(Type), Features = features(Type),
SubscribeFeature = lists:member("subscribe", Features), SubscribeFeature = lists:member("subscribe", Features),
OptionsFeature = lists:member("subscription-options", Features), OptionsFeature = lists:member("subscription-options", Features),
@@ -1907,9 +1717,13 @@ @@ -1877,9 +1687,13 @@
{"", "", ""} -> {"", "", ""} ->
{false, false}; {false, false};
_ -> _ ->
@ -494,7 +494,7 @@
end end
end, end,
if if
@@ -2240,7 +2054,7 @@ @@ -2210,7 +2024,7 @@
%% <p>The permission are not checked in this function.</p> %% <p>The permission are not checked in this function.</p>
%% @todo We probably need to check that the user doing the query has the right %% @todo We probably need to check that the user doing the query has the right
%% to read the items. %% to read the items.
@ -503,7 +503,7 @@
MaxItems = MaxItems =
if if
SMaxItems == "" -> get_max_items_node(Host); SMaxItems == "" -> get_max_items_node(Host);
@@ -2279,11 +2093,11 @@ @@ -2249,11 +2063,11 @@
node_call(Type, get_items, node_call(Type, get_items,
[NodeId, From, [NodeId, From,
AccessModel, PresenceSubscription, RosterGroup, AccessModel, PresenceSubscription, RosterGroup,
@ -517,7 +517,7 @@
SendItems = case ItemIDs of SendItems = case ItemIDs of
[] -> [] ->
Items; Items;
@@ -2296,7 +2110,8 @@ @@ -2266,7 +2080,8 @@
%% number of items sent to MaxItems: %% number of items sent to MaxItems:
{result, [{xmlelement, "pubsub", [{"xmlns", ?NS_PUBSUB}], {result, [{xmlelement, "pubsub", [{"xmlns", ?NS_PUBSUB}],
[{xmlelement, "items", nodeAttr(Node), [{xmlelement, "items", nodeAttr(Node),
@ -527,7 +527,7 @@
Error -> Error ->
Error Error
end end
@@ -2328,16 +2143,27 @@ @@ -2298,16 +2113,27 @@
%% @doc <p>Resend the items of a node to the user.</p> %% @doc <p>Resend the items of a node to the user.</p>
%% @todo use cache-last-item feature %% @todo use cache-last-item feature
send_items(Host, Node, NodeId, Type, LJID, last) -> send_items(Host, Node, NodeId, Type, LJID, last) ->
@ -561,7 +561,7 @@
send_items(Host, Node, NodeId, Type, LJID, Number) -> send_items(Host, Node, NodeId, Type, LJID, Number) ->
ToSend = case node_action(Host, Type, get_items, [NodeId, LJID]) of ToSend = case node_action(Host, Type, get_items, [NodeId, LJID]) of
{result, []} -> {result, []} ->
@@ -2463,29 +2289,12 @@ @@ -2433,29 +2259,12 @@
error -> error ->
{error, ?ERR_BAD_REQUEST}; {error, ?ERR_BAD_REQUEST};
_ -> _ ->
@ -594,7 +594,7 @@
end, Entities), end, Entities),
{result, []}; {result, []};
_ -> _ ->
@@ -2538,11 +2347,11 @@ @@ -2508,11 +2317,11 @@
end. end.
read_sub(Subscriber, Node, NodeID, SubID, Lang) -> read_sub(Subscriber, Node, NodeID, SubID, Lang) ->
@ -608,7 +608,7 @@
OptionsEl = {xmlelement, "options", [{"jid", jlib:jid_to_string(Subscriber)}, OptionsEl = {xmlelement, "options", [{"jid", jlib:jid_to_string(Subscriber)},
{"subid", SubID}|nodeAttr(Node)], {"subid", SubID}|nodeAttr(Node)],
[XdataEl]}, [XdataEl]},
@@ -2568,7 +2377,7 @@ @@ -2538,7 +2347,7 @@
end. end.
set_options_helper(Configuration, JID, NodeID, SubID, Type) -> set_options_helper(Configuration, JID, NodeID, SubID, Type) ->
@ -617,7 +617,7 @@
{result, GoodSubOpts} -> GoodSubOpts; {result, GoodSubOpts} -> GoodSubOpts;
_ -> invalid _ -> invalid
end, end,
@@ -2597,7 +2406,7 @@ @@ -2567,7 +2376,7 @@
write_sub(_Subscriber, _NodeID, _SubID, invalid) -> write_sub(_Subscriber, _NodeID, _SubID, invalid) ->
{error, extended_error(?ERR_BAD_REQUEST, "invalid-options")}; {error, extended_error(?ERR_BAD_REQUEST, "invalid-options")};
write_sub(Subscriber, NodeID, SubID, Options) -> write_sub(Subscriber, NodeID, SubID, Options) ->
@ -626,7 +626,7 @@
{error, notfound} -> {error, notfound} ->
{error, extended_error(?ERR_NOT_ACCEPTABLE, "invalid-subid")}; {error, extended_error(?ERR_NOT_ACCEPTABLE, "invalid-subid")};
{result, _} -> {result, _} ->
@@ -2765,8 +2574,8 @@ @@ -2735,8 +2544,8 @@
{"subscription", subscription_to_string(Sub)} | nodeAttr(Node)], []}]}]}, {"subscription", subscription_to_string(Sub)} | nodeAttr(Node)], []}]}]},
ejabberd_router:route(service_jid(Host), jlib:make_jid(JID), Stanza) ejabberd_router:route(service_jid(Host), jlib:make_jid(JID), Stanza)
end, end,
@ -637,7 +637,7 @@
true -> true ->
Result = lists:foldl(fun({JID, Subscription, SubId}, Acc) -> Result = lists:foldl(fun({JID, Subscription, SubId}, Acc) ->
@@ -3050,7 +2859,7 @@ @@ -3020,7 +2829,7 @@
{Depth, [{N, get_node_subs(N)} || N <- Nodes]} {Depth, [{N, get_node_subs(N)} || N <- Nodes]}
end, tree_call(Host, get_parentnodes_tree, [Host, Node, service_jid(Host)]))} end, tree_call(Host, get_parentnodes_tree, [Host, Node, service_jid(Host)]))}
end, end,
@ -646,7 +646,7 @@
{result, CollSubs} -> CollSubs; {result, CollSubs} -> CollSubs;
_ -> [] _ -> []
end. end.
@@ -3064,9 +2873,9 @@ @@ -3034,9 +2843,9 @@
get_options_for_subs(NodeID, Subs) -> get_options_for_subs(NodeID, Subs) ->
lists:foldl(fun({JID, subscribed, SubID}, Acc) -> lists:foldl(fun({JID, subscribed, SubID}, Acc) ->
@ -658,7 +658,7 @@
_ -> Acc _ -> Acc
end; end;
(_, Acc) -> (_, Acc) ->
@@ -3294,6 +3103,30 @@ @@ -3264,6 +3073,30 @@
Result Result
end. end.
@ -689,7 +689,7 @@
%% @spec (Host, Options) -> MaxItems %% @spec (Host, Options) -> MaxItems
%% Host = host() %% Host = host()
%% Options = [Option] %% Options = [Option]
@@ -3680,7 +3513,13 @@ @@ -3656,7 +3489,13 @@
tree_action(Host, Function, Args) -> tree_action(Host, Function, Args) ->
?DEBUG("tree_action ~p ~p ~p",[Host,Function,Args]), ?DEBUG("tree_action ~p ~p ~p",[Host,Function,Args]),
Fun = fun() -> tree_call(Host, Function, Args) end, Fun = fun() -> tree_call(Host, Function, Args) end,
@ -704,7 +704,7 @@
%% @doc <p>node plugin call.</p> %% @doc <p>node plugin call.</p>
node_call(Type, Function, Args) -> node_call(Type, Function, Args) ->
@@ -3700,13 +3539,13 @@ @@ -3676,13 +3515,13 @@
node_action(Host, Type, Function, Args) -> node_action(Host, Type, Function, Args) ->
?DEBUG("node_action ~p ~p ~p ~p",[Host,Type,Function,Args]), ?DEBUG("node_action ~p ~p ~p ~p",[Host,Type,Function,Args]),
@ -720,7 +720,7 @@
case tree_call(Host, get_node, [Host, Node]) of case tree_call(Host, get_node, [Host, Node]) of
N when is_record(N, pubsub_node) -> N when is_record(N, pubsub_node) ->
case Action(N) of case Action(N) of
@@ -3719,8 +3558,14 @@ @@ -3695,8 +3534,14 @@
end end
end, Trans). end, Trans).
@ -737,7 +737,7 @@
{result, Result} -> {result, Result}; {result, Result} -> {result, Result};
{error, Error} -> {error, Error}; {error, Error} -> {error, Error};
{atomic, {result, Result}} -> {result, Result}; {atomic, {result, Result}} -> {result, Result};
@@ -3728,6 +3573,15 @@ @@ -3704,6 +3549,15 @@
{aborted, Reason} -> {aborted, Reason} ->
?ERROR_MSG("transaction return internal error: ~p~n", [{aborted, Reason}]), ?ERROR_MSG("transaction return internal error: ~p~n", [{aborted, Reason}]),
{error, ?ERR_INTERNAL_SERVER_ERROR}; {error, ?ERR_INTERNAL_SERVER_ERROR};
@ -753,7 +753,7 @@
{'EXIT', Reason} -> {'EXIT', Reason} ->
?ERROR_MSG("transaction return internal error: ~p~n", [{'EXIT', Reason}]), ?ERROR_MSG("transaction return internal error: ~p~n", [{'EXIT', Reason}]),
{error, ?ERR_INTERNAL_SERVER_ERROR}; {error, ?ERR_INTERNAL_SERVER_ERROR};
@@ -3736,6 +3590,17 @@ @@ -3712,6 +3566,17 @@
{error, ?ERR_INTERNAL_SERVER_ERROR} {error, ?ERR_INTERNAL_SERVER_ERROR}
end. end.