2007-12-01 06:16:30 +01:00
|
|
|
%%% ====================================================================
|
2007-12-24 14:57:53 +01:00
|
|
|
%%% ``The contents of this file are subject to the Erlang Public License,
|
|
|
|
%%% Version 1.1, (the "License"); you may not use this file except in
|
|
|
|
%%% compliance with the License. You should have received a copy of the
|
|
|
|
%%% Erlang Public License along with this software. If not, it can be
|
|
|
|
%%% retrieved via the world wide web at http://www.erlang.org/.
|
|
|
|
%%%
|
|
|
|
%%% Software distributed under the License is distributed on an "AS IS"
|
|
|
|
%%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
|
|
|
|
%%% the License for the specific language governing rights and limitations
|
|
|
|
%%% under the License.
|
|
|
|
%%%
|
2008-09-16 16:39:57 +02:00
|
|
|
%%% The Initial Developer of the Original Code is ProcessOne.
|
2009-01-19 15:47:33 +01:00
|
|
|
%%% Portions created by ProcessOne are Copyright 2006-2009, ProcessOne
|
2007-12-24 14:57:53 +01:00
|
|
|
%%% All Rights Reserved.''
|
2009-01-19 15:47:33 +01:00
|
|
|
%%% This software is copyright 2006-2009, ProcessOne.
|
2007-12-01 06:16:30 +01:00
|
|
|
%%%
|
2007-12-24 14:57:53 +01:00
|
|
|
%%%
|
2009-01-19 15:47:33 +01:00
|
|
|
%%% @copyright 2006-2009 ProcessOne
|
2007-12-01 06:16:30 +01:00
|
|
|
%%% @author Christophe Romain <christophe.romain@process-one.net>
|
|
|
|
%%% [http://www.process-one.net/]
|
|
|
|
%%% @version {@vsn}, {@date} {@time}
|
|
|
|
%%% @end
|
|
|
|
%%% ====================================================================
|
|
|
|
|
|
|
|
%%% @doc The module <strong>{@module}</strong> is the pep PubSub plugin.
|
|
|
|
%%% <p>PubSub plugin nodes are using the {@link gen_pubsub_node} behaviour.</p>
|
|
|
|
|
|
|
|
-module(node_pep).
|
|
|
|
-author('christophe.romain@process-one.net').
|
|
|
|
|
2008-12-05 16:13:09 +01:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
|
|
|
|
2008-07-11 14:48:27 +02:00
|
|
|
-include("ejabberd.hrl").
|
2007-12-01 06:16:30 +01:00
|
|
|
-include("pubsub.hrl").
|
|
|
|
|
|
|
|
-behaviour(gen_pubsub_node).
|
|
|
|
|
|
|
|
%% API definition
|
|
|
|
-export([init/3, terminate/2,
|
|
|
|
options/0, features/0,
|
|
|
|
create_node_permission/6,
|
2009-05-01 01:17:38 +02:00
|
|
|
create_node/2,
|
|
|
|
delete_node/1,
|
|
|
|
purge_node/2,
|
2009-08-25 19:14:30 +02:00
|
|
|
subscribe_node/8,
|
2009-05-01 01:17:38 +02:00
|
|
|
unsubscribe_node/4,
|
|
|
|
publish_item/6,
|
2009-05-07 02:54:44 +02:00
|
|
|
delete_item/4,
|
2009-05-01 01:17:38 +02:00
|
|
|
remove_extra_items/3,
|
2007-12-01 06:16:30 +01:00
|
|
|
get_entity_affiliations/2,
|
2009-05-01 01:17:38 +02:00
|
|
|
get_node_affiliations/1,
|
|
|
|
get_affiliation/2,
|
|
|
|
set_affiliation/3,
|
2007-12-01 06:16:30 +01:00
|
|
|
get_entity_subscriptions/2,
|
2009-05-01 01:17:38 +02:00
|
|
|
get_node_subscriptions/1,
|
2009-08-25 19:14:30 +02:00
|
|
|
get_subscriptions/2,
|
|
|
|
set_subscriptions/4,
|
|
|
|
get_pending_nodes/2,
|
2009-05-01 01:17:38 +02:00
|
|
|
get_states/1,
|
|
|
|
get_state/2,
|
2007-12-01 06:16:30 +01:00
|
|
|
set_state/1,
|
2009-05-01 01:17:38 +02:00
|
|
|
get_items/6,
|
|
|
|
get_items/2,
|
|
|
|
get_item/7,
|
|
|
|
get_item/2,
|
2008-02-06 19:04:23 +01:00
|
|
|
set_item/1,
|
|
|
|
get_item_name/3
|
2007-12-01 06:16:30 +01:00
|
|
|
]).
|
|
|
|
|
|
|
|
init(Host, ServerHost, Opts) ->
|
2009-05-29 01:21:50 +02:00
|
|
|
node_hometree:init(Host, ServerHost, Opts),
|
2008-07-11 14:48:27 +02:00
|
|
|
complain_if_modcaps_disabled(ServerHost),
|
2007-12-01 06:16:30 +01:00
|
|
|
ok.
|
|
|
|
|
|
|
|
terminate(Host, ServerHost) ->
|
2009-05-29 01:21:50 +02:00
|
|
|
node_hometree:terminate(Host, ServerHost),
|
2007-12-01 06:16:30 +01:00
|
|
|
ok.
|
|
|
|
|
|
|
|
options() ->
|
2009-08-25 19:14:30 +02:00
|
|
|
[{deliver_payloads, true},
|
2007-12-01 06:16:30 +01:00
|
|
|
{notify_config, false},
|
|
|
|
{notify_delete, false},
|
|
|
|
{notify_retract, false},
|
|
|
|
{persist_items, false},
|
2009-09-23 18:32:06 +02:00
|
|
|
{max_items, ?MAXITEMS},
|
2007-12-01 06:16:30 +01:00
|
|
|
{subscribe, true},
|
|
|
|
{access_model, presence},
|
2008-01-25 10:24:10 +01:00
|
|
|
{roster_groups_allowed, []},
|
2007-12-01 06:16:30 +01:00
|
|
|
{publish_model, publishers},
|
2009-09-24 21:47:01 +02:00
|
|
|
{notification_type, headline},
|
2007-12-01 06:16:30 +01:00
|
|
|
{max_payload_size, ?MAX_PAYLOAD_SIZE},
|
|
|
|
{send_last_published_item, on_sub_and_presence},
|
|
|
|
{deliver_notifications, true},
|
|
|
|
{presence_based_delivery, true}].
|
|
|
|
|
|
|
|
features() ->
|
|
|
|
["create-nodes", %*
|
|
|
|
"auto-create", %*
|
2007-12-22 02:13:06 +01:00
|
|
|
"auto-subscribe", %*
|
2007-12-01 06:16:30 +01:00
|
|
|
"delete-nodes", %*
|
2009-02-19 12:29:01 +01:00
|
|
|
"delete-items", %*
|
2007-12-22 02:13:06 +01:00
|
|
|
"filtered-notifications", %*
|
|
|
|
"modify-affiliations",
|
2007-12-01 06:16:30 +01:00
|
|
|
"outcast-affiliation",
|
|
|
|
"persistent-items",
|
|
|
|
"publish", %*
|
|
|
|
"purge-nodes",
|
|
|
|
"retract-items",
|
|
|
|
"retrieve-affiliations",
|
|
|
|
"retrieve-items", %*
|
|
|
|
"retrieve-subscriptions",
|
2007-12-22 02:13:06 +01:00
|
|
|
"subscribe" %*
|
2007-12-01 06:16:30 +01:00
|
|
|
].
|
|
|
|
|
2008-09-16 16:39:57 +02:00
|
|
|
create_node_permission(Host, ServerHost, _Node, _ParentNode, Owner, Access) ->
|
2008-12-05 16:13:09 +01:00
|
|
|
LOwner = jlib:short_prepd_jid(Owner),
|
2009-02-27 04:19:53 +01:00
|
|
|
{User, Server, Resource} = LOwner,
|
2008-09-16 16:39:57 +02:00
|
|
|
Allowed = case LOwner of
|
2008-12-05 16:13:09 +01:00
|
|
|
{undefined, Host, undefined} ->
|
2008-09-16 16:39:57 +02:00
|
|
|
true; % pubsub service always allowed
|
|
|
|
_ ->
|
2009-06-01 18:26:00 +02:00
|
|
|
JID = exmpp_jid:make(User, Server, Resource),
|
2009-02-27 04:19:53 +01:00
|
|
|
case acl:match_rule(ServerHost, Access, JID) of
|
2008-09-16 16:39:57 +02:00
|
|
|
allow ->
|
|
|
|
case Host of
|
|
|
|
{User, Server, _} -> true;
|
|
|
|
_ -> false
|
|
|
|
end;
|
2008-10-08 14:02:30 +02:00
|
|
|
E ->
|
2009-02-27 04:19:53 +01:00
|
|
|
?DEBUG("Create not allowed : ~p~n", [E]),
|
|
|
|
false
|
2008-09-16 16:39:57 +02:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
{result, Allowed}.
|
2009-02-27 04:19:53 +01:00
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
create_node(NodeId, Owner) ->
|
2009-05-29 01:21:50 +02:00
|
|
|
case node_hometree:create_node(NodeId, Owner) of
|
2007-12-01 06:16:30 +01:00
|
|
|
{result, _} -> {result, []};
|
|
|
|
Error -> Error
|
|
|
|
end.
|
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
delete_node(Removed) ->
|
2009-05-29 01:21:50 +02:00
|
|
|
case node_hometree:delete_node(Removed) of
|
2007-12-01 06:16:30 +01:00
|
|
|
{result, {_, _, Removed}} -> {result, {[], Removed}};
|
|
|
|
Error -> Error
|
|
|
|
end.
|
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
subscribe_node(NodeId, Sender, Subscriber, AccessModel,
|
2009-08-25 19:14:30 +02:00
|
|
|
SendLast, PresenceSubscription, RosterGroup, Options) ->
|
2009-05-29 01:21:50 +02:00
|
|
|
node_hometree:subscribe_node(
|
2009-05-01 01:17:38 +02:00
|
|
|
NodeId, Sender, Subscriber, AccessModel, SendLast,
|
2009-08-25 19:14:30 +02:00
|
|
|
PresenceSubscription, RosterGroup, Options).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
unsubscribe_node(NodeId, Sender, Subscriber, SubID) ->
|
2009-05-29 01:21:50 +02:00
|
|
|
case node_hometree:unsubscribe_node(NodeId, Sender, Subscriber, SubID) of
|
2007-12-01 06:16:30 +01:00
|
|
|
{error, Error} -> {error, Error};
|
|
|
|
{result, _} -> {result, []}
|
|
|
|
end.
|
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
publish_item(NodeId, Publisher, Model, MaxItems, ItemId, Payload) ->
|
2009-05-29 01:21:50 +02:00
|
|
|
node_hometree:publish_item(NodeId, Publisher, Model, MaxItems, ItemId, Payload).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
remove_extra_items(NodeId, MaxItems, ItemIds) ->
|
2009-05-29 01:21:50 +02:00
|
|
|
node_hometree:remove_extra_items(NodeId, MaxItems, ItemIds).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-05-07 02:54:44 +02:00
|
|
|
delete_item(NodeId, Publisher, PublishModel, ItemId) ->
|
2009-05-29 01:21:50 +02:00
|
|
|
node_hometree:delete_item(NodeId, Publisher, PublishModel, ItemId).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
purge_node(NodeId, Owner) ->
|
2009-05-29 01:21:50 +02:00
|
|
|
node_hometree:purge_node(NodeId, Owner).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2007-12-18 15:45:26 +01:00
|
|
|
get_entity_affiliations(_Host, Owner) ->
|
2009-05-26 23:50:13 +02:00
|
|
|
{_, D, _} = SubKey = jlib:jid_tolower(Owner),
|
|
|
|
SubKey = jlib:jid_tolower(Owner),
|
|
|
|
GenKey = jlib:jid_remove_resource(SubKey),
|
|
|
|
States = mnesia:match_object(#pubsub_state{stateid = {GenKey, '_'}, _ = '_'}),
|
2009-08-28 18:28:22 +02:00
|
|
|
NodeTree = case catch ets:lookup(gen_mod:get_module_proc(D, config), nodetree) of
|
2009-05-26 23:50:13 +02:00
|
|
|
[{nodetree, N}] -> N;
|
2009-08-25 19:14:30 +02:00
|
|
|
_ -> nodetree_tree
|
2009-05-26 23:50:13 +02:00
|
|
|
end,
|
|
|
|
Reply = lists:foldl(fun(#pubsub_state{stateid = {_, N}, affiliation = A}, Acc) ->
|
|
|
|
case NodeTree:get_node(N) of
|
|
|
|
#pubsub_node{nodeid = {{_, D, _}, _}} = Node -> [{Node, A}|Acc];
|
|
|
|
_ -> Acc
|
|
|
|
end
|
|
|
|
end, [], States),
|
|
|
|
{result, Reply}.
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
get_node_affiliations(NodeId) ->
|
2009-05-29 01:21:50 +02:00
|
|
|
node_hometree:get_node_affiliations(NodeId).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
get_affiliation(NodeId, Owner) ->
|
2009-05-29 01:21:50 +02:00
|
|
|
node_hometree:get_affiliation(NodeId, Owner).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
set_affiliation(NodeId, Owner, Affiliation) ->
|
2009-05-29 01:21:50 +02:00
|
|
|
node_hometree:set_affiliation(NodeId, Owner, Affiliation).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-05-20 00:29:28 +02:00
|
|
|
get_entity_subscriptions(_Host, Owner) ->
|
|
|
|
{U, D, _} = SubKey = jlib:jid_tolower(Owner),
|
|
|
|
GenKey = jlib:jid_remove_resource(SubKey),
|
|
|
|
States = case SubKey of
|
|
|
|
GenKey -> mnesia:match_object(
|
|
|
|
#pubsub_state{stateid = {{U, D, '_'}, '_'}, _ = '_'});
|
|
|
|
_ -> mnesia:match_object(
|
|
|
|
#pubsub_state{stateid = {GenKey, '_'}, _ = '_'})
|
|
|
|
++ mnesia:match_object(
|
|
|
|
#pubsub_state{stateid = {SubKey, '_'}, _ = '_'})
|
|
|
|
end,
|
2009-08-28 18:28:22 +02:00
|
|
|
NodeTree = case catch ets:lookup(gen_mod:get_module_proc(D, config), nodetree) of
|
2009-05-26 23:50:13 +02:00
|
|
|
[{nodetree, N}] -> N;
|
2009-08-25 19:14:30 +02:00
|
|
|
_ -> nodetree_tree
|
2009-05-26 23:50:13 +02:00
|
|
|
end,
|
2009-08-25 19:14:30 +02:00
|
|
|
Reply = lists:foldl(fun(#pubsub_state{stateid = {J, N}, subscriptions = Ss}, Acc) ->
|
2009-05-26 23:50:13 +02:00
|
|
|
case NodeTree:get_node(N) of
|
2009-08-25 19:14:30 +02:00
|
|
|
#pubsub_node{nodeid = {{_, D, _}, _}} = Node ->
|
|
|
|
lists:foldl(fun({subscribed, SubID}, Acc2) ->
|
|
|
|
[{Node, subscribed, SubID, J} | Acc2];
|
|
|
|
({pending, _SubID}, Acc2) ->
|
|
|
|
[{Node, pending, J} | Acc2];
|
|
|
|
(S, Acc2) ->
|
|
|
|
[{Node, S, J} | Acc2]
|
|
|
|
end, Acc, Ss);
|
2009-05-26 23:50:13 +02:00
|
|
|
_ -> Acc
|
2009-05-20 00:29:28 +02:00
|
|
|
end
|
|
|
|
end, [], States),
|
|
|
|
{result, Reply}.
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
get_node_subscriptions(NodeId) ->
|
|
|
|
%% note: get_node_subscriptions is used for broadcasting
|
|
|
|
%% there should not have any subscriptions
|
|
|
|
%% but that call returns also all subscription to none
|
|
|
|
%% and this is required for broadcast to occurs
|
|
|
|
%% DO NOT REMOVE
|
2009-05-29 01:21:50 +02:00
|
|
|
node_hometree:get_node_subscriptions(NodeId).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-08-25 19:14:30 +02:00
|
|
|
get_subscriptions(NodeId, Owner) ->
|
|
|
|
node_hometree:get_subscriptions(NodeId, Owner).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-08-25 19:14:30 +02:00
|
|
|
set_subscriptions(NodeId, Owner, Subscription, SubId) ->
|
|
|
|
node_hometree:set_subscriptions(NodeId, Owner, Subscription, SubId).
|
|
|
|
|
|
|
|
get_pending_nodes(Host, Owner) ->
|
|
|
|
node_hometree:get_pending_nodes(Host, Owner).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
get_states(NodeId) ->
|
2009-05-29 01:21:50 +02:00
|
|
|
node_hometree:get_states(NodeId).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
get_state(NodeId, JID) ->
|
2009-05-29 01:21:50 +02:00
|
|
|
node_hometree:get_state(NodeId, JID).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
|
|
|
set_state(State) ->
|
2009-05-29 01:21:50 +02:00
|
|
|
node_hometree:set_state(State).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
get_items(NodeId, From) ->
|
2009-05-29 01:21:50 +02:00
|
|
|
node_hometree:get_items(NodeId, From).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
get_items(NodeId, JID, AccessModel, PresenceSubscription, RosterGroup, SubId) ->
|
2009-05-29 01:21:50 +02:00
|
|
|
node_hometree:get_items(NodeId, JID, AccessModel, PresenceSubscription, RosterGroup, SubId).
|
2008-07-11 14:48:27 +02:00
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
get_item(NodeId, ItemId) ->
|
2009-05-29 01:21:50 +02:00
|
|
|
node_hometree:get_item(NodeId, ItemId).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
get_item(NodeId, ItemId, JID, AccessModel, PresenceSubscription, RosterGroup, SubId) ->
|
2009-05-29 01:21:50 +02:00
|
|
|
node_hometree:get_item(NodeId, ItemId, JID, AccessModel, PresenceSubscription, RosterGroup, SubId).
|
2008-07-11 14:48:27 +02:00
|
|
|
|
2007-12-01 06:16:30 +01:00
|
|
|
set_item(Item) ->
|
2009-05-29 01:21:50 +02:00
|
|
|
node_hometree:set_item(Item).
|
2008-02-06 19:04:23 +01:00
|
|
|
|
|
|
|
get_item_name(Host, Node, Id) ->
|
2009-05-29 01:21:50 +02:00
|
|
|
node_hometree:get_item_name(Host, Node, Id).
|
2008-07-11 14:48:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
%%%
|
|
|
|
%%% Internal
|
|
|
|
%%%
|
|
|
|
|
|
|
|
%% @doc Check mod_caps is enabled, otherwise show warning.
|
|
|
|
%% The PEP plugin for mod_pubsub requires mod_caps to be enabled in the host.
|
|
|
|
%% Check that the mod_caps module is enabled in that Jabber Host
|
|
|
|
%% If not, show a warning message in the ejabberd log file.
|
|
|
|
complain_if_modcaps_disabled(ServerHost) ->
|
|
|
|
Modules = ejabberd_config:get_local_option({modules, ServerHost}),
|
|
|
|
ModCaps = [mod_caps_enabled || {mod_caps, _Opts} <- Modules],
|
|
|
|
case ModCaps of
|
|
|
|
[] ->
|
|
|
|
?WARNING_MSG("The PEP plugin is enabled in mod_pubsub of host ~p. "
|
|
|
|
"This plugin requires mod_caps to be enabled, "
|
|
|
|
"but it isn't.", [ServerHost]);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|