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
|
|
|
|
%%% ====================================================================
|
|
|
|
|
|
|
|
%%% @todo The item table should be handled by the plugin, but plugin that do
|
|
|
|
%%% not want to manage it should be able to use the default behaviour.
|
|
|
|
%%% @todo Plugin modules should be able to register to receive presence update
|
|
|
|
%%% send to pubsub.
|
|
|
|
|
|
|
|
%%% @doc The module <strong>{@module}</strong> is the default PubSub plugin.
|
|
|
|
%%% <p>It is used as a default for all unknown PubSub node type. It can serve
|
|
|
|
%%% as a developer basis and reference to build its own custom pubsub node
|
|
|
|
%%% types.</p>
|
|
|
|
%%% <p>PubSub plugin nodes are using the {@link gen_node} behaviour.</p>
|
|
|
|
%%% <p><strong>The API isn't stabilized yet</strong>. The pubsub plugin
|
|
|
|
%%% development is still a work in progress. However, the system is already
|
|
|
|
%%% useable and useful as is. Please, send us comments, feedback and
|
|
|
|
%%% improvements.</p>
|
|
|
|
|
2009-05-29 01:21:50 +02:00
|
|
|
-module(node_hometree).
|
2007-12-01 06:16:30 +01:00
|
|
|
-author('christophe.romain@process-one.net').
|
|
|
|
|
|
|
|
-include("pubsub.hrl").
|
2009-08-25 19:14:30 +02:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
2007-12-01 06:16:30 +01:00
|
|
|
|
|
|
|
-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
|
|
|
]).
|
|
|
|
|
|
|
|
%% ================
|
|
|
|
%% API definition
|
|
|
|
%% ================
|
|
|
|
|
2008-04-01 12:11:39 +02:00
|
|
|
%% @spec (Host, ServerHost, Opts) -> any()
|
2007-12-01 06:16:30 +01:00
|
|
|
%% Host = mod_pubsub:host()
|
2008-04-01 12:11:39 +02:00
|
|
|
%% ServerHost = mod_pubsub:host()
|
2007-12-01 06:16:30 +01:00
|
|
|
%% Opts = list()
|
|
|
|
%% @doc <p>Called during pubsub modules initialisation. Any pubsub plugin must
|
|
|
|
%% implement this function. It can return anything.</p>
|
|
|
|
%% <p>This function is mainly used to trigger the setup task necessary for the
|
|
|
|
%% plugin. It can be used for example by the developer to create the specific
|
|
|
|
%% module database schema if it does not exists yet.</p>
|
|
|
|
init(_Host, _ServerHost, _Opts) ->
|
2009-08-25 19:14:30 +02:00
|
|
|
pubsub_subscription:init(),
|
2007-12-01 06:16:30 +01:00
|
|
|
mnesia:create_table(pubsub_state,
|
|
|
|
[{disc_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, pubsub_state)}]),
|
|
|
|
mnesia:create_table(pubsub_item,
|
|
|
|
[{disc_only_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, pubsub_item)}]),
|
|
|
|
ItemsFields = record_info(fields, pubsub_item),
|
|
|
|
case mnesia:table_info(pubsub_item, attributes) of
|
|
|
|
ItemsFields -> ok;
|
|
|
|
_ ->
|
|
|
|
mnesia:transform_table(pubsub_item, ignore, ItemsFields)
|
|
|
|
end,
|
|
|
|
ok.
|
|
|
|
|
2008-04-01 12:11:39 +02:00
|
|
|
%% @spec (Host, ServerHost) -> any()
|
2007-12-01 06:16:30 +01:00
|
|
|
%% Host = mod_pubsub:host()
|
2008-04-01 12:11:39 +02:00
|
|
|
%% ServerHost = host()
|
2007-12-01 06:16:30 +01:00
|
|
|
%% @doc <p>Called during pubsub modules termination. Any pubsub plugin must
|
|
|
|
%% implement this function. It can return anything.</p>
|
|
|
|
terminate(_Host, _ServerHost) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
%% @spec () -> [Option]
|
|
|
|
%% Option = mod_pubsub:nodeOption()
|
|
|
|
%% @doc Returns the default pubsub node options.
|
|
|
|
%% <p>Example of function return value:</p>
|
|
|
|
%% ```
|
|
|
|
%% [{deliver_payloads, true},
|
|
|
|
%% {notify_config, false},
|
|
|
|
%% {notify_delete, false},
|
|
|
|
%% {notify_retract, true},
|
|
|
|
%% {persist_items, true},
|
|
|
|
%% {max_items, 10},
|
|
|
|
%% {subscribe, true},
|
|
|
|
%% {access_model, open},
|
|
|
|
%% {publish_model, publishers},
|
|
|
|
%% {max_payload_size, 100000},
|
|
|
|
%% {send_last_published_item, never},
|
|
|
|
%% {presence_based_delivery, false}]'''
|
|
|
|
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, true},
|
|
|
|
{persist_items, true},
|
|
|
|
{max_items, ?MAXITEMS div 2},
|
|
|
|
{subscribe, true},
|
|
|
|
{access_model, open},
|
2008-01-25 10:24:10 +01:00
|
|
|
{roster_groups_allowed, []},
|
2007-12-01 06:16:30 +01:00
|
|
|
{publish_model, publishers},
|
|
|
|
{max_payload_size, ?MAX_PAYLOAD_SIZE},
|
2007-12-12 17:16:00 +01:00
|
|
|
{send_last_published_item, on_sub_and_presence},
|
2007-12-01 06:16:30 +01:00
|
|
|
{deliver_notifications, true},
|
|
|
|
{presence_based_delivery, false}].
|
|
|
|
|
|
|
|
%% @spec () -> []
|
|
|
|
%% @doc Returns the node features
|
|
|
|
features() ->
|
|
|
|
["create-nodes",
|
|
|
|
"auto-create",
|
2009-08-25 19:14:30 +02:00
|
|
|
"access-authorize",
|
2007-12-01 06:16:30 +01:00
|
|
|
"delete-nodes",
|
2009-02-19 12:29:01 +01:00
|
|
|
"delete-items",
|
2009-08-25 19:14:30 +02:00
|
|
|
"get-pending",
|
2007-12-01 06:16:30 +01:00
|
|
|
"instant-nodes",
|
|
|
|
"manage-subscriptions",
|
2007-12-22 02:13:06 +01:00
|
|
|
"modify-affiliations",
|
2009-08-25 19:14:30 +02:00
|
|
|
"multi-subscribe",
|
2007-12-01 06:16:30 +01:00
|
|
|
"outcast-affiliation",
|
|
|
|
"persistent-items",
|
|
|
|
"publish",
|
|
|
|
"purge-nodes",
|
|
|
|
"retract-items",
|
|
|
|
"retrieve-affiliations",
|
|
|
|
"retrieve-items",
|
|
|
|
"retrieve-subscriptions",
|
|
|
|
"subscribe",
|
2009-08-25 19:14:30 +02:00
|
|
|
"subscription-notifications",
|
|
|
|
"subscription-options"
|
2007-12-01 06:16:30 +01:00
|
|
|
].
|
|
|
|
|
2008-04-01 12:11:39 +02:00
|
|
|
%% @spec (Host, ServerHost, Node, ParentNode, Owner, Access) -> bool()
|
2007-12-01 06:16:30 +01:00
|
|
|
%% Host = mod_pubsub:host()
|
|
|
|
%% ServerHost = mod_pubsub:host()
|
|
|
|
%% Node = mod_pubsub:pubsubNode()
|
2008-04-01 12:11:39 +02:00
|
|
|
%% ParentNode = mod_pubsub:pubsubNode()
|
2007-12-01 06:16:30 +01:00
|
|
|
%% Owner = mod_pubsub:jid()
|
|
|
|
%% Access = all | atom()
|
|
|
|
%% @doc Checks if the current user has the permission to create the requested node
|
|
|
|
%% <p>In {@link node_default}, the permission is decided by the place in the
|
|
|
|
%% hierarchy where the user is creating the node. The access parameter is also
|
|
|
|
%% checked in the default module. This parameter depends on the value of the
|
|
|
|
%% <tt>access_createnode</tt> ACL value in ejabberd config file.</p>
|
|
|
|
%% <p>This function also check that node can be created a a children of its
|
|
|
|
%% parent node</p>
|
|
|
|
%% <p>PubSub plugins can redefine the PubSub node creation rights as they
|
|
|
|
%% which. They can simply delegate this check to the {@link node_default}
|
|
|
|
%% module by implementing this function like this:
|
2009-05-01 01:17:38 +02:00
|
|
|
%% ```check_create_user_permission(Host, ServerHost, Node, ParentNode, Owner, Access) ->
|
|
|
|
%% node_default:check_create_user_permission(Host, ServerHost, Node, ParentNode, Owner, Access).'''</p>
|
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),
|
2007-12-01 06:16:30 +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
|
|
|
|
_ ->
|
2008-12-08 12:20:21 +01:00
|
|
|
{LU, LS, LR} = LOwner,
|
2009-06-01 18:26:00 +02:00
|
|
|
case acl:match_rule(ServerHost, Access, exmpp_jid:make(LU, LS, LR)) of
|
2007-12-22 01:04:05 +01:00
|
|
|
allow ->
|
2008-07-11 14:48:27 +02:00
|
|
|
case Node of
|
|
|
|
["home", Server, User | _] -> true;
|
|
|
|
_ -> false
|
2007-12-22 01:04:05 +01:00
|
|
|
end;
|
|
|
|
_ ->
|
2008-09-16 16:39:57 +02:00
|
|
|
false
|
|
|
|
end
|
|
|
|
end,
|
2008-07-11 14:48:27 +02:00
|
|
|
{result, Allowed}.
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
%% @spec (NodeId, Owner) ->
|
2007-12-01 06:16:30 +01:00
|
|
|
%% {result, Result} | exit
|
2009-05-01 01:17:38 +02:00
|
|
|
%% NodeId = mod_pubsub:pubsubNodeId()
|
2007-12-01 06:16:30 +01:00
|
|
|
%% Owner = mod_pubsub:jid()
|
|
|
|
%% @doc <p></p>
|
2009-05-01 01:17:38 +02:00
|
|
|
create_node(NodeId, Owner) ->
|
2008-12-05 16:13:09 +01:00
|
|
|
OwnerKey = jlib:short_prepd_bare_jid(Owner),
|
2009-05-01 01:17:38 +02:00
|
|
|
set_state(#pubsub_state{stateid = {OwnerKey, NodeId}, affiliation = owner}),
|
2007-12-01 06:16:30 +01:00
|
|
|
{result, {default, broadcast}}.
|
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
%% @spec (Removed) -> ok
|
2007-12-01 06:16:30 +01:00
|
|
|
%% Removed = [mod_pubsub:pubsubNode()]
|
|
|
|
%% @doc <p>purge items of deleted nodes after effective deletion.</p>
|
2009-05-01 01:17:38 +02:00
|
|
|
delete_node(Removed) ->
|
2009-08-25 19:14:30 +02:00
|
|
|
Tr = fun(#pubsub_state{stateid = {J, _}, subscriptions = Ss}) ->
|
|
|
|
lists:map(fun(S) ->
|
|
|
|
{J, S}
|
|
|
|
end, Ss)
|
2009-05-01 01:17:38 +02:00
|
|
|
end,
|
|
|
|
Reply = lists:map(
|
|
|
|
fun(#pubsub_node{id = NodeId} = PubsubNode) ->
|
|
|
|
{result, States} = get_states(NodeId),
|
|
|
|
lists:foreach(
|
|
|
|
fun(#pubsub_state{stateid = {LJID, _}, items = Items}) ->
|
|
|
|
del_items(NodeId, Items),
|
|
|
|
del_state(NodeId, LJID)
|
|
|
|
end, States),
|
2009-08-25 19:14:30 +02:00
|
|
|
{PubsubNode, lists:flatmap(Tr, States)}
|
2009-05-01 01:17:38 +02:00
|
|
|
end, Removed),
|
|
|
|
{result, {default, broadcast, Reply}}.
|
|
|
|
|
|
|
|
%% @spec (NodeId, Sender, Subscriber, AccessModel, SendLast, PresenceSubscription, RosterGroup) ->
|
2007-12-01 06:16:30 +01:00
|
|
|
%% {error, Reason} | {result, Result}
|
|
|
|
%% @doc <p>Accepts or rejects subcription requests on a PubSub node.</p>
|
|
|
|
%% <p>The mechanism works as follow:
|
|
|
|
%% <ul>
|
|
|
|
%% <li>The main PubSub module prepares the subscription and passes the
|
|
|
|
%% result of the preparation as a record.</li>
|
|
|
|
%% <li>This function gets the prepared record and several other parameters and
|
|
|
|
%% can decide to:<ul>
|
|
|
|
%% <li>reject the subscription;</li>
|
|
|
|
%% <li>allow it as is, letting the main module perform the database
|
|
|
|
%% persistance;</li>
|
|
|
|
%% <li>allow it, modifying the record. The main module will store the
|
|
|
|
%% modified record;</li>
|
|
|
|
%% <li>allow it, but perform the needed persistance operations.</li></ul>
|
|
|
|
%% </li></ul></p>
|
|
|
|
%% <p>The selected behaviour depends on the return parameter:
|
|
|
|
%% <ul>
|
|
|
|
%% <li><tt>{error, Reason}</tt>: an IQ error result will be returned. No
|
|
|
|
%% subscription will actually be performed.</li>
|
|
|
|
%% <li><tt>true</tt>: Subscribe operation is allowed, based on the
|
|
|
|
%% unmodified record passed in parameter <tt>SubscribeResult</tt>. If this
|
|
|
|
%% parameter contains an error, no subscription will be performed.</li>
|
|
|
|
%% <li><tt>{true, PubsubState}</tt>: Subscribe operation is allowed, but
|
|
|
|
%% the {@link mod_pubsub:pubsubState()} record returned replaces the value
|
|
|
|
%% passed in parameter <tt>SubscribeResult</tt>.</li>
|
|
|
|
%% <li><tt>{true, done}</tt>: Subscribe operation is allowed, but the
|
|
|
|
%% {@link mod_pubsub:pubsubState()} will be considered as already stored and
|
|
|
|
%% no further persistance operation will be performed. This case is used,
|
|
|
|
%% when the plugin module is doing the persistance by itself or when it want
|
|
|
|
%% to completly disable persistance.</li></ul>
|
|
|
|
%% </p>
|
|
|
|
%% <p>In the default plugin module, the record is unchanged.</p>
|
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-01-09 20:18:46 +01:00
|
|
|
SubKey = jlib:short_prepd_jid(Subscriber),
|
2009-01-08 18:27:47 +01:00
|
|
|
GenKey = jlib:short_prepd_bare_jid(SubKey),
|
|
|
|
Authorized = (jlib:short_prepd_bare_jid(Sender) == GenKey),
|
2009-05-01 01:17:38 +02:00
|
|
|
GenState = get_state(NodeId, GenKey),
|
2009-01-08 18:27:47 +01:00
|
|
|
SubState = case SubKey of
|
|
|
|
GenKey -> GenState;
|
2009-05-01 01:17:38 +02:00
|
|
|
_ -> get_state(NodeId, SubKey)
|
2009-01-08 18:27:47 +01:00
|
|
|
end,
|
|
|
|
Affiliation = GenState#pubsub_state.affiliation,
|
2009-08-25 19:14:30 +02:00
|
|
|
Subscriptions = SubState#pubsub_state.subscriptions,
|
2009-01-03 01:58:18 +01:00
|
|
|
Whitelisted = lists:member(Affiliation, [member, publisher, owner]),
|
2009-08-25 19:14:30 +02:00
|
|
|
PendingSubscription = lists:any(fun({pending, _}) -> true;
|
|
|
|
(_) -> false
|
|
|
|
end, Subscriptions),
|
2007-12-01 06:16:30 +01:00
|
|
|
if
|
|
|
|
not Authorized ->
|
|
|
|
%% JIDs do not match
|
2008-12-05 16:13:09 +01:00
|
|
|
{error, ?ERR_EXTENDED('bad-request', "invalid-jid")};
|
2007-12-01 06:16:30 +01:00
|
|
|
Affiliation == outcast ->
|
|
|
|
%% Requesting entity is blocked
|
2008-12-05 16:13:09 +01:00
|
|
|
{error, 'forbidden'};
|
2009-08-25 19:14:30 +02:00
|
|
|
PendingSubscription ->
|
2008-07-11 14:48:27 +02:00
|
|
|
%% Requesting entity has pending subscription
|
2008-12-05 16:13:09 +01:00
|
|
|
{error, ?ERR_EXTENDED('not-authorized', "pending-subscription")};
|
2007-12-01 06:16:30 +01:00
|
|
|
(AccessModel == presence) and (not PresenceSubscription) ->
|
|
|
|
%% Entity is not authorized to create a subscription (presence subscription required)
|
2008-12-05 16:13:09 +01:00
|
|
|
{error, ?ERR_EXTENDED('not-authorized', "presence-subscription-required")};
|
2007-12-01 06:16:30 +01:00
|
|
|
(AccessModel == roster) and (not RosterGroup) ->
|
|
|
|
%% Entity is not authorized to create a subscription (not in roster group)
|
2008-12-05 16:13:09 +01:00
|
|
|
{error, ?ERR_EXTENDED('not-authorized', "not-in-roster-group")};
|
2009-01-03 01:58:18 +01:00
|
|
|
(AccessModel == whitelist) and (not Whitelisted) ->
|
|
|
|
%% Node has whitelist access model and entity lacks required affiliation
|
2008-12-05 16:13:09 +01:00
|
|
|
{error, ?ERR_EXTENDED('not-allowed', "closed-node")};
|
2009-03-10 12:13:46 +01:00
|
|
|
(AccessModel == authorize) -> % TODO: to be done
|
2007-12-01 06:16:30 +01:00
|
|
|
%% Node has authorize access model
|
2008-12-05 16:13:09 +01:00
|
|
|
{error, 'forbidden'};
|
2007-12-01 06:16:30 +01:00
|
|
|
%%MustPay ->
|
|
|
|
%% % Payment is required for a subscription
|
|
|
|
%% {error, ?ERR_PAYMENT_REQUIRED};
|
|
|
|
%%ForbiddenAnonymous ->
|
|
|
|
%% % Requesting entity is anonymous
|
2008-12-05 16:13:09 +01:00
|
|
|
%% {error, 'forbidden'};
|
2007-12-01 06:16:30 +01:00
|
|
|
true ->
|
2009-08-25 19:14:30 +02:00
|
|
|
case pubsub_subscription:subscribe_node(Subscriber, NodeId, Options) of
|
|
|
|
{result, SubId} ->
|
|
|
|
NewSub = case AccessModel of
|
|
|
|
authorize -> pending;
|
|
|
|
_ -> subscribed
|
|
|
|
end,
|
|
|
|
set_state(SubState#pubsub_state{subscriptions = [{NewSub, SubId} | Subscriptions]}),
|
|
|
|
case {NewSub, SendLast} of
|
|
|
|
{subscribed, never} ->
|
|
|
|
{result, {default, subscribed, SubId}};
|
|
|
|
{subscribed, _} ->
|
|
|
|
{result, {default, subscribed, SubId, send_last}};
|
|
|
|
{_, _} ->
|
|
|
|
{result, {default, pending, SubId}}
|
2007-12-01 06:16:30 +01:00
|
|
|
end;
|
|
|
|
_ ->
|
2009-08-25 19:14:30 +02:00
|
|
|
{error, 'internal-server-error'}
|
2007-12-01 06:16:30 +01:00
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2009-08-25 19:14:30 +02:00
|
|
|
%% @spec (NodeId, Sender, Subscriber, SubId) ->
|
2007-12-01 06:16:30 +01:00
|
|
|
%% {error, Reason} | {result, []}
|
2009-05-01 01:17:38 +02:00
|
|
|
%% NodeId = mod_pubsub:pubsubNodeId()
|
2007-12-01 06:16:30 +01:00
|
|
|
%% Sender = mod_pubsub:jid()
|
|
|
|
%% Subscriber = mod_pubsub:jid()
|
2009-08-25 19:14:30 +02:00
|
|
|
%% SubId = mod_pubsub:subid()
|
2007-12-01 06:16:30 +01:00
|
|
|
%% Reason = mod_pubsub:stanzaError()
|
|
|
|
%% @doc <p>Unsubscribe the <tt>Subscriber</tt> from the <tt>Node</tt>.</p>
|
2009-08-25 19:14:30 +02:00
|
|
|
unsubscribe_node(NodeId, Sender, Subscriber, SubId) ->
|
2009-01-08 18:27:47 +01:00
|
|
|
SubKey = jlib:short_prepd_jid(Subscriber),
|
|
|
|
GenKey = jlib:short_prepd_bare_jid(SubKey),
|
|
|
|
Authorized = (jlib:short_prepd_bare_jid(Sender) == GenKey),
|
2009-05-01 01:17:38 +02:00
|
|
|
GenState = get_state(NodeId, GenKey),
|
2009-01-08 18:27:47 +01:00
|
|
|
SubState = case SubKey of
|
|
|
|
GenKey -> GenState;
|
2009-05-01 01:17:38 +02:00
|
|
|
_ -> get_state(NodeId, SubKey)
|
2009-01-08 18:27:47 +01:00
|
|
|
end,
|
2009-08-25 19:14:30 +02:00
|
|
|
Subscriptions = lists:filter(fun({_Sub, _SubId}) -> true;
|
|
|
|
(_SubId) -> false
|
|
|
|
end, SubState#pubsub_state.subscriptions),
|
|
|
|
SubIdExists = case SubId of
|
|
|
|
[] -> false;
|
|
|
|
List when is_list(List) -> true;
|
|
|
|
_ -> false
|
|
|
|
end,
|
2009-01-03 01:29:36 +01:00
|
|
|
if
|
2009-01-11 07:24:42 +01:00
|
|
|
%% Requesting entity is prohibited from unsubscribing entity
|
|
|
|
not Authorized ->
|
|
|
|
{error, 'forbidden'};
|
2009-08-25 19:14:30 +02:00
|
|
|
%% Entity did not specify SubId
|
|
|
|
%%SubId == "", ?? ->
|
2009-01-03 01:29:36 +01:00
|
|
|
%% {error, ?ERR_EXTENDED('bad-request', "subid-required")};
|
|
|
|
%% Invalid subscription identifier
|
2009-08-25 19:14:30 +02:00
|
|
|
%%InvalidSubId ->
|
2009-01-03 01:29:36 +01:00
|
|
|
%% {error, ?ERR_EXTENDED('not-acceptable', "invalid-subid")};
|
|
|
|
%% Requesting entity is not a subscriber
|
2009-08-25 19:14:30 +02:00
|
|
|
Subscriptions == [] ->
|
2008-12-05 16:13:09 +01:00
|
|
|
{error, ?ERR_EXTENDED('unexpected-request', "not-subscribed")};
|
2009-08-25 19:14:30 +02:00
|
|
|
%% Subid supplied, so use that.
|
|
|
|
SubIdExists ->
|
|
|
|
Sub = first_in_list(fun(S) ->
|
|
|
|
case S of
|
|
|
|
{_Sub, SubId} -> true;
|
|
|
|
_ -> false
|
|
|
|
end
|
|
|
|
end, SubState#pubsub_state.subscriptions),
|
|
|
|
case Sub of
|
|
|
|
{value, S} ->
|
|
|
|
delete_subscription(SubKey, NodeId, S, SubState),
|
|
|
|
{result, default};
|
|
|
|
false ->
|
|
|
|
{error, ?ERR_EXTENDED('unexpected-request',
|
|
|
|
"not-subscribed")}
|
|
|
|
end;
|
|
|
|
%% No subid supplied, but there's only one matching
|
|
|
|
%% subscription, so use that.
|
|
|
|
length(Subscriptions) == 1 ->
|
|
|
|
delete_subscription(SubKey, NodeId, hd(Subscriptions), SubState),
|
2009-01-03 01:29:36 +01:00
|
|
|
{result, default};
|
|
|
|
true ->
|
2009-08-25 19:14:30 +02:00
|
|
|
{error, ?ERR_EXTENDED('bad-request', "subid-required")}
|
2007-12-01 06:16:30 +01:00
|
|
|
end.
|
|
|
|
|
2009-08-25 19:14:30 +02:00
|
|
|
delete_subscription(SubKey, NodeID, {Subscription, SubId}, SubState) ->
|
|
|
|
Affiliation = SubState#pubsub_state.affiliation,
|
|
|
|
AllSubs = SubState#pubsub_state.subscriptions,
|
|
|
|
NewSubs = AllSubs -- [{Subscription, SubId}],
|
|
|
|
pubsub_subscription:unsubscribe_node(SubKey, NodeID, SubId),
|
|
|
|
case {Affiliation, NewSubs} of
|
|
|
|
{none, []} ->
|
|
|
|
% Just a regular subscriber, and this is final item, so
|
|
|
|
% delete the state.
|
|
|
|
del_state(NodeID, SubKey);
|
|
|
|
_ ->
|
|
|
|
set_state(SubState#pubsub_state{subscriptions = NewSubs})
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
%% @spec (NodeId, Publisher, PublishModel, MaxItems, ItemId, Payload) ->
|
2007-12-01 06:16:30 +01:00
|
|
|
%% {true, PubsubItem} | {result, Reply}
|
2009-05-01 01:17:38 +02:00
|
|
|
%% NodeId = mod_pubsub:pubsubNodeId()
|
2007-12-01 06:16:30 +01:00
|
|
|
%% Publisher = mod_pubsub:jid()
|
|
|
|
%% PublishModel = atom()
|
|
|
|
%% MaxItems = integer()
|
|
|
|
%% ItemId = string()
|
|
|
|
%% Payload = term()
|
|
|
|
%% @doc <p>Publishes the item passed as parameter.</p>
|
|
|
|
%% <p>The mechanism works as follow:
|
|
|
|
%% <ul>
|
|
|
|
%% <li>The main PubSub module prepares the item to publish and passes the
|
|
|
|
%% result of the preparation as a {@link mod_pubsub:pubsubItem()} record.</li>
|
|
|
|
%% <li>This function gets the prepared record and several other parameters and can decide to:<ul>
|
|
|
|
%% <li>reject the publication;</li>
|
|
|
|
%% <li>allow the publication as is, letting the main module perform the database persistance;</li>
|
|
|
|
%% <li>allow the publication, modifying the record. The main module will store the modified record;</li>
|
|
|
|
%% <li>allow it, but perform the needed persistance operations.</li></ul>
|
|
|
|
%% </li></ul></p>
|
|
|
|
%% <p>The selected behaviour depends on the return parameter:
|
|
|
|
%% <ul>
|
|
|
|
%% <li><tt>{error, Reason}</tt>: an iq error result will be return. No
|
|
|
|
%% publication is actually performed.</li>
|
|
|
|
%% <li><tt>true</tt>: Publication operation is allowed, based on the
|
|
|
|
%% unmodified record passed in parameter <tt>Item</tt>. If the <tt>Item</tt>
|
|
|
|
%% parameter contains an error, no subscription will actually be
|
|
|
|
%% performed.</li>
|
|
|
|
%% <li><tt>{true, Item}</tt>: Publication operation is allowed, but the
|
|
|
|
%% {@link mod_pubsub:pubsubItem()} record returned replaces the value passed
|
|
|
|
%% in parameter <tt>Item</tt>. The persistance will be performed by the main
|
|
|
|
%% module.</li>
|
|
|
|
%% <li><tt>{true, done}</tt>: Publication operation is allowed, but the
|
|
|
|
%% {@link mod_pubsub:pubsubItem()} will be considered as already stored and
|
|
|
|
%% no further persistance operation will be performed. This case is used,
|
|
|
|
%% when the plugin module is doing the persistance by itself or when it want
|
|
|
|
%% to completly disable persistance.</li></ul>
|
|
|
|
%% </p>
|
|
|
|
%% <p>In the default plugin module, the record is unchanged.</p>
|
2009-05-01 01:17:38 +02:00
|
|
|
publish_item(NodeId, Publisher, PublishModel, MaxItems, ItemId, Payload) ->
|
2009-01-08 18:27:47 +01:00
|
|
|
SubKey = jlib:short_prepd_jid(Publisher),
|
2009-08-25 22:03:28 +02:00
|
|
|
GenKey = jlib:short_prepd_bare_jid(Publisher),
|
2009-05-01 01:17:38 +02:00
|
|
|
GenState = get_state(NodeId, GenKey),
|
2009-01-08 18:27:47 +01:00
|
|
|
SubState = case SubKey of
|
|
|
|
GenKey -> GenState;
|
2009-05-01 01:17:38 +02:00
|
|
|
_ -> get_state(NodeId, SubKey)
|
2009-01-08 18:27:47 +01:00
|
|
|
end,
|
|
|
|
Affiliation = GenState#pubsub_state.affiliation,
|
2009-08-25 19:14:30 +02:00
|
|
|
Subscribed = case PublishModel of
|
|
|
|
subscribers -> is_subscribed(SubState#pubsub_state.subscriptions);
|
|
|
|
_ -> undefined
|
|
|
|
end,
|
2007-12-01 06:16:30 +01:00
|
|
|
if
|
|
|
|
not ((PublishModel == open)
|
|
|
|
or ((PublishModel == publishers)
|
|
|
|
and ((Affiliation == owner) or (Affiliation == publisher)))
|
2009-08-25 19:14:30 +02:00
|
|
|
or (Subscribed == true)) ->
|
2007-12-01 06:16:30 +01:00
|
|
|
%% Entity does not have sufficient privileges to publish to node
|
2008-12-05 16:13:09 +01:00
|
|
|
{error, 'forbidden'};
|
2007-12-01 06:16:30 +01:00
|
|
|
true ->
|
2009-03-10 12:13:46 +01:00
|
|
|
%% TODO: check creation, presence, roster
|
2009-05-01 01:17:38 +02:00
|
|
|
if MaxItems > 0 ->
|
2009-09-05 01:34:05 +02:00
|
|
|
Now = now(),
|
|
|
|
PubId = {Now, SubKey},
|
2009-05-01 01:17:38 +02:00
|
|
|
Item = case get_item(NodeId, ItemId) of
|
2009-01-03 01:29:36 +01:00
|
|
|
{result, OldItem} ->
|
|
|
|
OldItem#pubsub_item{modification = PubId,
|
|
|
|
payload = Payload};
|
|
|
|
_ ->
|
2009-05-01 01:17:38 +02:00
|
|
|
#pubsub_item{itemid = {ItemId, NodeId},
|
2009-09-05 01:34:05 +02:00
|
|
|
creation = {Now, GenKey},
|
2007-12-01 06:16:30 +01:00
|
|
|
modification = PubId,
|
2009-01-03 01:29:36 +01:00
|
|
|
payload = Payload}
|
2007-12-01 06:16:30 +01:00
|
|
|
end,
|
2009-05-01 01:17:38 +02:00
|
|
|
Items = [ItemId | GenState#pubsub_state.items--[ItemId]],
|
|
|
|
{result, {NI, OI}} = remove_extra_items(NodeId, MaxItems, Items),
|
|
|
|
set_item(Item),
|
|
|
|
set_state(GenState#pubsub_state{items = NI}),
|
|
|
|
{result, {default, broadcast, OI}};
|
|
|
|
true ->
|
|
|
|
{result, {default, broadcast, []}}
|
|
|
|
end
|
2007-12-01 06:16:30 +01:00
|
|
|
end.
|
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
%% @spec (NodeId, MaxItems, ItemIds) -> {NewItemIds,OldItemIds}
|
|
|
|
%% NodeId = mod_pubsub:pubsubNodeId()
|
2007-12-01 06:16:30 +01:00
|
|
|
%% MaxItems = integer() | unlimited
|
|
|
|
%% ItemIds = [ItemId::string()]
|
|
|
|
%% NewItemIds = [ItemId::string()]
|
|
|
|
%% @doc <p>This function is used to remove extra items, most notably when the
|
|
|
|
%% maximum number of items has been reached.</p>
|
|
|
|
%% <p>This function is used internally by the core PubSub module, as no
|
|
|
|
%% permission check is performed.</p>
|
|
|
|
%% <p>In the default plugin module, the oldest items are removed, but other
|
|
|
|
%% rules can be used.</p>
|
|
|
|
%% <p>If another PubSub plugin wants to delegate the item removal (and if the
|
|
|
|
%% plugin is using the default pubsub storage), it can implements this function like this:
|
2009-05-01 01:17:38 +02:00
|
|
|
%% ```remove_extra_items(NodeId, MaxItems, ItemIds) ->
|
|
|
|
%% node_default:remove_extra_items(NodeId, MaxItems, ItemIds).'''</p>
|
|
|
|
remove_extra_items(_NodeId, unlimited, ItemIds) ->
|
2007-12-01 06:16:30 +01:00
|
|
|
{result, {ItemIds, []}};
|
2009-05-01 01:17:38 +02:00
|
|
|
remove_extra_items(NodeId, MaxItems, ItemIds) ->
|
2007-12-01 06:16:30 +01:00
|
|
|
NewItems = lists:sublist(ItemIds, MaxItems),
|
|
|
|
OldItems = lists:nthtail(length(NewItems), ItemIds),
|
|
|
|
%% Remove extra items:
|
2009-05-01 01:17:38 +02:00
|
|
|
del_items(NodeId, OldItems),
|
2007-12-01 06:16:30 +01:00
|
|
|
%% Return the new items list:
|
|
|
|
{result, {NewItems, OldItems}}.
|
|
|
|
|
2009-05-07 02:54:44 +02:00
|
|
|
%% @spec (NodeId, Publisher, PublishModel, ItemId) ->
|
2007-12-01 06:16:30 +01:00
|
|
|
%% {error, Reason::stanzaError()} |
|
|
|
|
%% {result, []}
|
2009-05-01 01:17:38 +02:00
|
|
|
%% NodeId = mod_pubsub:pubsubNodeId()
|
2009-05-07 02:54:44 +02:00
|
|
|
%% Publisher = mod_pubsub:jid()
|
|
|
|
%% PublishModel = atom()
|
2007-12-01 06:16:30 +01:00
|
|
|
%% ItemId = string()
|
|
|
|
%% @doc <p>Triggers item deletion.</p>
|
|
|
|
%% <p>Default plugin: The user performing the deletion must be the node owner
|
2009-08-25 21:54:44 +02:00
|
|
|
%% or a publisher, or PublishModel being open.</p>
|
2009-05-07 02:54:44 +02:00
|
|
|
delete_item(NodeId, Publisher, PublishModel, ItemId) ->
|
2009-01-08 18:27:47 +01:00
|
|
|
GenKey = jlib:short_prepd_bare_jid(Publisher),
|
2009-05-01 01:17:38 +02:00
|
|
|
GenState = get_state(NodeId, GenKey),
|
2009-01-08 18:27:47 +01:00
|
|
|
#pubsub_state{affiliation = Affiliation, items = Items} = GenState,
|
2007-12-01 06:16:30 +01:00
|
|
|
Allowed = (Affiliation == publisher) orelse (Affiliation == owner)
|
2009-05-07 02:54:44 +02:00
|
|
|
orelse (PublishModel == open)
|
2009-05-01 01:17:38 +02:00
|
|
|
orelse case get_item(NodeId, ItemId) of
|
2009-05-07 02:54:44 +02:00
|
|
|
{result, #pubsub_item{creation = {_, GenKey}}} -> true;
|
2007-12-01 06:16:30 +01:00
|
|
|
_ -> false
|
|
|
|
end,
|
|
|
|
if
|
|
|
|
not Allowed ->
|
|
|
|
%% Requesting entity does not have sufficient privileges
|
2008-12-05 16:13:09 +01:00
|
|
|
{error, 'forbidden'};
|
2007-12-01 06:16:30 +01:00
|
|
|
true ->
|
2009-05-01 01:17:38 +02:00
|
|
|
case lists:member(ItemId, Items) of
|
|
|
|
true ->
|
|
|
|
del_item(NodeId, ItemId),
|
2009-09-09 23:49:23 +02:00
|
|
|
set_state(GenState#pubsub_state{items = lists:delete(ItemId, Items)}),
|
2007-12-01 06:16:30 +01:00
|
|
|
{result, {default, broadcast}};
|
2009-05-01 01:17:38 +02:00
|
|
|
false ->
|
2009-09-09 23:49:23 +02:00
|
|
|
case Affiliation of
|
|
|
|
owner ->
|
|
|
|
%% Owner can delete other publishers items as well
|
|
|
|
{result, States} = get_states(NodeId),
|
|
|
|
lists:foldl(
|
|
|
|
fun(#pubsub_state{items = PI, affiliation = publisher} = S, Res) ->
|
|
|
|
case lists:member(ItemId, PI) of
|
|
|
|
true ->
|
|
|
|
del_item(NodeId, ItemId),
|
|
|
|
set_state(S#pubsub_state{items = lists:delete(ItemId, PI)}),
|
|
|
|
{result, {default, broadcast}};
|
|
|
|
false ->
|
|
|
|
Res
|
|
|
|
end;
|
|
|
|
(_, Res) ->
|
|
|
|
Res
|
|
|
|
end, {error, 'item-not-found'}, States);
|
|
|
|
_ ->
|
|
|
|
%% Non-existent node or item
|
|
|
|
{error, 'item-not-found'}
|
|
|
|
end
|
2007-12-01 06:16:30 +01:00
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
%% @spec (NodeId, Owner) ->
|
2008-04-01 12:11:39 +02:00
|
|
|
%% {error, Reason::stanzaError()} |
|
|
|
|
%% {result, {default, broadcast}}
|
2009-05-01 01:17:38 +02:00
|
|
|
%% NodeId = mod_pubsub:pubsubNodeId()
|
2008-04-01 12:11:39 +02:00
|
|
|
%% Owner = mod_pubsub:jid()
|
2009-05-01 01:17:38 +02:00
|
|
|
purge_node(NodeId, Owner) ->
|
2009-01-08 18:27:47 +01:00
|
|
|
GenKey = jlib:short_prepd_bare_jid(Owner),
|
2009-05-01 01:17:38 +02:00
|
|
|
GenState = get_state(NodeId, GenKey),
|
2009-01-08 18:27:47 +01:00
|
|
|
case GenState of
|
2009-09-09 23:49:23 +02:00
|
|
|
#pubsub_state{affiliation = owner} ->
|
|
|
|
{result, States} = get_states(NodeId),
|
|
|
|
lists:foreach(
|
|
|
|
fun(#pubsub_state{items = []}) ->
|
|
|
|
ok;
|
|
|
|
(#pubsub_state{items = Items} = S) ->
|
|
|
|
del_items(NodeId, Items),
|
|
|
|
set_state(S#pubsub_state{items = []})
|
|
|
|
end, States),
|
2007-12-01 06:16:30 +01:00
|
|
|
{result, {default, broadcast}};
|
|
|
|
_ ->
|
2009-01-03 01:29:36 +01:00
|
|
|
%% Entity is not owner
|
|
|
|
{error, 'forbidden'}
|
2007-12-01 06:16:30 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% @spec (Host, JID) -> [{Node,Affiliation}]
|
|
|
|
%% Host = host()
|
|
|
|
%% JID = mod_pubsub:jid()
|
|
|
|
%% @doc <p>Return the current affiliations for the given user</p>
|
|
|
|
%% <p>The default module reads affiliations in the main Mnesia
|
|
|
|
%% <tt>pubsub_state</tt> table. If a plugin stores its data in the same
|
|
|
|
%% table, it should return an empty list, as the affiliation will be read by
|
|
|
|
%% the default PubSub module. Otherwise, it should return its own affiliation,
|
|
|
|
%% that will be added to the affiliation stored in the main
|
|
|
|
%% <tt>pubsub_state</tt> table.</p>
|
2009-05-20 00:29:28 +02:00
|
|
|
get_entity_affiliations(Host, Owner) ->
|
2009-01-08 18:27:47 +01:00
|
|
|
GenKey = jlib:short_prepd_bare_jid(Owner),
|
2009-05-20 00:29:28 +02:00
|
|
|
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(Host, 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-05-20 00:29:28 +02:00
|
|
|
Reply = lists:foldl(fun(#pubsub_state{stateid = {_, N}, affiliation = A}, Acc) ->
|
2009-05-26 23:50:13 +02:00
|
|
|
case NodeTree:get_node(N) of
|
|
|
|
#pubsub_node{nodeid = {Host, _}} = Node -> [{Node, A}|Acc];
|
|
|
|
_ -> 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_affiliations(NodeId) ->
|
|
|
|
{result, States} = get_states(NodeId),
|
2009-08-25 19:14:30 +02:00
|
|
|
Tr = fun(#pubsub_state{stateid = {J, _}, affiliation = A}) ->
|
|
|
|
{J, A}
|
2007-12-01 06:16:30 +01:00
|
|
|
end,
|
|
|
|
{result, lists:map(Tr, States)}.
|
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
get_affiliation(NodeId, Owner) ->
|
2009-01-08 18:27:47 +01:00
|
|
|
GenKey = jlib:short_prepd_bare_jid(Owner),
|
2009-05-01 01:17:38 +02:00
|
|
|
GenState = get_state(NodeId, GenKey),
|
2009-01-08 18:27:47 +01:00
|
|
|
{result, GenState#pubsub_state.affiliation}.
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-08-25 19:42:25 +02:00
|
|
|
set_affiliation(NodeId, Owner, Affiliation) when ?IS_JID(Owner)->
|
2009-01-08 18:27:47 +01:00
|
|
|
GenKey = jlib:short_prepd_bare_jid(Owner),
|
2009-05-01 01:17:38 +02:00
|
|
|
GenState = get_state(NodeId, GenKey),
|
2009-08-25 19:14:30 +02:00
|
|
|
case {Affiliation, GenState#pubsub_state.subscriptions} of
|
2009-01-11 03:05:41 +01:00
|
|
|
{none, none} ->
|
2009-05-01 01:17:38 +02:00
|
|
|
del_state(NodeId, GenKey);
|
2009-01-11 03:05:41 +01:00
|
|
|
_ ->
|
|
|
|
set_state(GenState#pubsub_state{affiliation = Affiliation})
|
2009-05-01 01:17:38 +02:00
|
|
|
end.
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2008-04-01 12:11:39 +02:00
|
|
|
%% @spec (Host, Owner) -> [{Node,Subscription}]
|
2007-12-01 06:16:30 +01:00
|
|
|
%% Host = host()
|
2008-04-01 12:11:39 +02:00
|
|
|
%% Owner = mod_pubsub:jid()
|
2007-12-01 06:16:30 +01:00
|
|
|
%% @doc <p>Return the current subscriptions for the given user</p>
|
|
|
|
%% <p>The default module reads subscriptions in the main Mnesia
|
|
|
|
%% <tt>pubsub_state</tt> table. If a plugin stores its data in the same
|
|
|
|
%% table, it should return an empty list, as the affiliation will be read by
|
|
|
|
%% the default PubSub module. Otherwise, it should return its own affiliation,
|
|
|
|
%% that will be added to the affiliation stored in the main
|
|
|
|
%% <tt>pubsub_state</tt> table.</p>
|
2009-05-20 00:29:28 +02:00
|
|
|
get_entity_subscriptions(Host, Owner) ->
|
2009-02-27 22:09:44 +01:00
|
|
|
{U, D, _} = SubKey = jlib:short_prepd_jid(Owner),
|
2009-08-25 19:14:30 +02:00
|
|
|
GenKey = jlib:short_prepd_bare_jid(Owner),
|
2009-02-27 04:19:53 +01:00
|
|
|
States = case SubKey of
|
|
|
|
GenKey -> mnesia:match_object(
|
2009-05-01 01:17:38 +02:00
|
|
|
#pubsub_state{stateid = {{U, D, '_'}, '_'}, _ = '_'});
|
2009-02-27 04:19:53 +01:00
|
|
|
_ -> mnesia:match_object(
|
2009-05-01 01:17:38 +02:00
|
|
|
#pubsub_state{stateid = {GenKey, '_'}, _ = '_'})
|
2009-02-27 04:19:53 +01:00
|
|
|
++ mnesia:match_object(
|
2009-05-01 01:17:38 +02:00
|
|
|
#pubsub_state{stateid = {SubKey, '_'}, _ = '_'})
|
2009-01-08 18:27:47 +01:00
|
|
|
end,
|
2009-08-28 18:28:22 +02:00
|
|
|
NodeTree = case catch ets:lookup(gen_mod:get_module_proc(Host, 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 = {Host, _}} = Node ->
|
|
|
|
lists:foldl(fun({Sub, SubId}, Acc2) ->
|
|
|
|
[{Node, Sub, SubId, 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) ->
|
|
|
|
{result, States} = get_states(NodeId),
|
2009-08-25 19:14:30 +02:00
|
|
|
Tr = fun(#pubsub_state{stateid = {J, _}, subscriptions = Subscriptions}) ->
|
|
|
|
%% TODO: get rid of cases to handle non-list subscriptions
|
|
|
|
case Subscriptions of
|
|
|
|
[_|_] ->
|
|
|
|
lists:foldl(fun({S, SubId}, Acc) ->
|
|
|
|
[{J, S, SubId} | Acc];
|
|
|
|
(S, Acc) ->
|
|
|
|
[{J, S} | Acc]
|
|
|
|
end, [], Subscriptions);
|
|
|
|
[] ->
|
|
|
|
[];
|
|
|
|
_ ->
|
|
|
|
[{J, none}]
|
|
|
|
end
|
2007-12-01 06:16:30 +01:00
|
|
|
end,
|
2009-08-25 19:14:30 +02:00
|
|
|
{result, lists:flatmap(Tr, States)}.
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-08-25 19:14:30 +02:00
|
|
|
get_subscriptions(NodeId, Owner) ->
|
2009-01-11 03:05:41 +01:00
|
|
|
SubKey = jlib:short_prepd_jid(Owner),
|
2009-05-01 01:17:38 +02:00
|
|
|
SubState = get_state(NodeId, SubKey),
|
2009-08-25 19:14:30 +02:00
|
|
|
{result, SubState#pubsub_state.subscriptions}.
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-08-25 19:14:30 +02:00
|
|
|
set_subscriptions(NodeId, Owner, Subscription, SubId) ->
|
2009-01-11 03:05:41 +01:00
|
|
|
SubKey = jlib:short_prepd_jid(Owner),
|
2009-05-01 01:17:38 +02:00
|
|
|
SubState = get_state(NodeId, SubKey),
|
2009-08-25 19:14:30 +02:00
|
|
|
case {SubId, SubState#pubsub_state.subscriptions} of
|
|
|
|
{_, []} ->
|
2009-08-25 21:54:44 +02:00
|
|
|
case Subscription of
|
2009-08-27 10:26:22 +02:00
|
|
|
none -> {error, ?ERR_EXTENDED('bad_request', "not-subscribed")};
|
2009-08-25 21:54:44 +02:00
|
|
|
_ -> new_subscription(NodeId, Owner, Subscription, SubState)
|
|
|
|
end;
|
2009-08-25 19:14:30 +02:00
|
|
|
{"", [{_, SID}]} ->
|
|
|
|
case Subscription of
|
|
|
|
none -> unsub_with_subid(NodeId, SID, SubState);
|
|
|
|
_ -> replace_subscription({Subscription, SID}, SubState)
|
|
|
|
end;
|
|
|
|
{"", [_|_]} ->
|
|
|
|
{error, ?ERR_EXTENDED('bad_request', "subid-required")};
|
2009-01-11 03:05:41 +01:00
|
|
|
_ ->
|
2009-08-25 19:14:30 +02:00
|
|
|
case Subscription of
|
|
|
|
none -> unsub_with_subid(NodeId, SubId, SubState);
|
|
|
|
_ -> replace_subscription({Subscription, SubId}, SubState)
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
replace_subscription(NewSub, SubState) ->
|
|
|
|
NewSubs = replace_subscription(NewSub,
|
|
|
|
SubState#pubsub_state.subscriptions, []),
|
|
|
|
set_state(SubState#pubsub_state{subscriptions = NewSubs}).
|
|
|
|
|
|
|
|
replace_subscription(_, [], Acc) ->
|
|
|
|
Acc;
|
|
|
|
replace_subscription({Sub, SubId}, [{_, SubID} | T], Acc) ->
|
|
|
|
replace_subscription({Sub, SubId}, T, [{Sub, SubID} | Acc]).
|
|
|
|
|
2009-08-25 21:54:44 +02:00
|
|
|
new_subscription(NodeId, Owner, Subscription, SubState) ->
|
|
|
|
SubId = pubsub_subscription:add_subscription(Owner, NodeId, []),
|
|
|
|
Subscriptions = SubState#pubsub_state.subscriptions,
|
|
|
|
set_state(SubState#pubsub_state{subscriptions = [{Subscription, SubId} | Subscriptions]}),
|
|
|
|
{Subscription, SubId}.
|
|
|
|
|
2009-08-25 19:14:30 +02:00
|
|
|
unsub_with_subid(NodeId, SubId, SubState) ->
|
2009-08-25 21:54:44 +02:00
|
|
|
pubsub_subscription:delete_subscription(SubState#pubsub_state.stateid,
|
2009-08-25 19:14:30 +02:00
|
|
|
NodeId, SubId),
|
|
|
|
NewSubs = lists:filter(fun ({_, SID}) -> SubId =/= SID end,
|
|
|
|
SubState#pubsub_state.subscriptions),
|
|
|
|
case {NewSubs, SubState#pubsub_state.affiliation} of
|
|
|
|
{[], none} ->
|
|
|
|
del_state(NodeId, element(1, SubState#pubsub_state.stateid));
|
|
|
|
_ ->
|
|
|
|
set_state(SubState#pubsub_state{subscriptions = NewSubs})
|
|
|
|
end.
|
|
|
|
%% @spec (Host, Owner) -> {result, [Node]} | {error, Reason}
|
|
|
|
%% Host = host()
|
|
|
|
%% Owner = jid()
|
|
|
|
%% Node = pubsubNode()
|
|
|
|
%% @doc <p>Returns a list of Owner's nodes on Host with pending
|
|
|
|
%% subscriptions.</p>
|
|
|
|
get_pending_nodes(Host, Owner) ->
|
|
|
|
GenKey = jlib:jid_remove_resource(jlib:jid_tolower(Owner)),
|
|
|
|
States = mnesia:match_object(#pubsub_state{stateid = {GenKey, '_'},
|
|
|
|
affiliation = owner,
|
|
|
|
_ = '_'}),
|
|
|
|
NodeIDs = [ID || #pubsub_state{stateid = {_, ID}} <- States],
|
2009-08-28 18:28:22 +02:00
|
|
|
NodeTree = case catch ets:lookup(gen_mod:get_module_proc(Host, config), nodetree) of
|
2009-08-25 19:14:30 +02:00
|
|
|
[{nodetree, N}] -> N;
|
|
|
|
_ -> nodetree_tree
|
|
|
|
end,
|
|
|
|
Reply = mnesia:foldl(fun(#pubsub_state{stateid = {_, NID}} = S, Acc) ->
|
|
|
|
case lists:member(NID, NodeIDs) of
|
|
|
|
true ->
|
|
|
|
case get_nodes_helper(NodeTree, S) of
|
|
|
|
{value, Node} -> [Node | Acc];
|
|
|
|
false -> Acc
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
Acc
|
|
|
|
end
|
|
|
|
end, [], pubsub_state),
|
|
|
|
{result, Reply}.
|
|
|
|
|
|
|
|
get_nodes_helper(NodeTree,
|
|
|
|
#pubsub_state{stateid = {_, N}, subscriptions = Subs}) ->
|
|
|
|
HasPending = fun ({pending, _}) -> true;
|
|
|
|
(pending) -> true;
|
|
|
|
(_) -> false
|
|
|
|
end,
|
|
|
|
case lists:any(HasPending, Subs) of
|
|
|
|
true ->
|
|
|
|
case NodeTree:get_node(N) of
|
|
|
|
#pubsub_node{nodeid = {_, Node}} ->
|
|
|
|
{value, Node};
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
false
|
|
|
|
end.
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
%% @spec (NodeId) -> [States] | []
|
|
|
|
%% NodeId = mod_pubsub:pubsubNodeId()
|
2007-12-01 06:16:30 +01:00
|
|
|
%% @doc Returns the list of stored states for a given node.
|
|
|
|
%% <p>For the default PubSub module, states are stored in Mnesia database.</p>
|
|
|
|
%% <p>We can consider that the pubsub_state table have been created by the main
|
|
|
|
%% mod_pubsub module.</p>
|
|
|
|
%% <p>PubSub plugins can store the states where they wants (for example in a
|
|
|
|
%% relational database).</p>
|
|
|
|
%% <p>If a PubSub plugin wants to delegate the states storage to the default node,
|
|
|
|
%% they can implement this function like this:
|
2009-05-01 01:17:38 +02:00
|
|
|
%% ```get_states(NodeId) ->
|
|
|
|
%% node_default:get_states(NodeId).'''</p>
|
|
|
|
get_states(NodeId) ->
|
2009-08-25 19:14:30 +02:00
|
|
|
States = case catch mnesia:match_object(
|
|
|
|
#pubsub_state{stateid = {'_', NodeId}, _ = '_'}) of
|
|
|
|
List when is_list(List) -> List;
|
|
|
|
_ -> []
|
|
|
|
end,
|
2007-12-01 06:16:30 +01:00
|
|
|
{result, States}.
|
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
%% @spec (NodeId, JID) -> [State] | []
|
|
|
|
%% NodeId = mod_pubsub:pubsubNodeId()
|
2007-12-01 06:16:30 +01:00
|
|
|
%% JID = mod_pubsub:jid()
|
|
|
|
%% State = mod_pubsub:pubsubItems()
|
|
|
|
%% @doc <p>Returns a state (one state list), given its reference.</p>
|
2009-05-01 01:17:38 +02:00
|
|
|
get_state(NodeId, JID) ->
|
|
|
|
StateId = {JID, NodeId},
|
2009-08-25 19:14:30 +02:00
|
|
|
case catch mnesia:read({pubsub_state, StateId}) of
|
2009-01-03 16:15:38 +01:00
|
|
|
[State] when is_record(State, pubsub_state) -> State;
|
2009-01-03 01:29:36 +01:00
|
|
|
_ -> #pubsub_state{stateid=StateId}
|
2007-12-01 06:16:30 +01:00
|
|
|
end.
|
|
|
|
|
2008-04-01 12:11:39 +02:00
|
|
|
%% @spec (State) -> ok | {error, Reason::stanzaError()}
|
2007-12-01 06:16:30 +01:00
|
|
|
%% State = mod_pubsub:pubsubStates()
|
|
|
|
%% @doc <p>Write a state into database.</p>
|
|
|
|
set_state(State) when is_record(State, pubsub_state) ->
|
|
|
|
mnesia:write(State);
|
|
|
|
set_state(_) ->
|
2008-12-05 16:13:09 +01:00
|
|
|
{error, 'internal-server-error'}.
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-01-03 01:29:36 +01:00
|
|
|
%% @spec (StateId) -> ok | {error, Reason::stanzaError()}
|
|
|
|
%% StateId = mod_pubsub:pubsubStateId()
|
|
|
|
%% @doc <p>Delete a state from database.</p>
|
2009-05-01 01:17:38 +02:00
|
|
|
del_state(NodeId, JID) ->
|
|
|
|
mnesia:delete({pubsub_state, {JID, NodeId}}).
|
2009-01-03 01:29:36 +01:00
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
%% @spec (NodeId, From) -> [Items] | []
|
|
|
|
%% NodeId = mod_pubsub:pubsubNodeId()
|
2007-12-01 06:16:30 +01:00
|
|
|
%% Items = mod_pubsub:pubsubItems()
|
|
|
|
%% @doc Returns the list of stored items for a given node.
|
|
|
|
%% <p>For the default PubSub module, items are stored in Mnesia database.</p>
|
|
|
|
%% <p>We can consider that the pubsub_item table have been created by the main
|
|
|
|
%% mod_pubsub module.</p>
|
|
|
|
%% <p>PubSub plugins can store the items where they wants (for example in a
|
|
|
|
%% relational database), or they can even decide not to persist any items.</p>
|
|
|
|
%% <p>If a PubSub plugin wants to delegate the item storage to the default node,
|
|
|
|
%% they can implement this function like this:
|
2009-05-01 01:17:38 +02:00
|
|
|
%% ```get_items(NodeId, From) ->
|
|
|
|
%% node_default:get_items(NodeId, From).'''</p>
|
|
|
|
get_items(NodeId, _From) ->
|
2009-05-27 00:50:11 +02:00
|
|
|
Items = mnesia:match_object(#pubsub_item{itemid = {'_', NodeId}, _ = '_'}),
|
|
|
|
{result, lists:reverse(lists:keysort(#pubsub_item.modification, Items))}.
|
2009-05-01 01:17:38 +02:00
|
|
|
get_items(NodeId, JID, AccessModel, PresenceSubscription, RosterGroup, _SubId) ->
|
2009-08-25 19:14:30 +02:00
|
|
|
SubKey = jlib:short_prepd_jid(JID),
|
2009-01-08 18:27:47 +01:00
|
|
|
GenKey = jlib:short_prepd_bare_jid(JID),
|
2009-05-01 01:17:38 +02:00
|
|
|
GenState = get_state(NodeId, GenKey),
|
2009-08-25 19:14:30 +02:00
|
|
|
SubState = get_state(NodeId, SubKey),
|
2009-01-08 18:27:47 +01:00
|
|
|
Affiliation = GenState#pubsub_state.affiliation,
|
2009-08-25 19:14:30 +02:00
|
|
|
Subscriptions = SubState#pubsub_state.subscriptions,
|
|
|
|
Whitelisted = can_fetch_item(Affiliation, Subscriptions),
|
2008-07-11 14:48:27 +02:00
|
|
|
if
|
2009-08-25 19:14:30 +02:00
|
|
|
%%SubId == "", ?? ->
|
2008-07-11 14:48:27 +02:00
|
|
|
%% Entity has multiple subscriptions to the node but does not specify a subscription ID
|
2008-12-05 16:13:09 +01:00
|
|
|
%{error, ?ERR_EXTENDED('bad-request', "subid-required")};
|
2009-08-25 19:14:30 +02:00
|
|
|
%%InvalidSubId ->
|
2008-07-11 14:48:27 +02:00
|
|
|
%% Entity is subscribed but specifies an invalid subscription ID
|
2008-12-05 16:13:09 +01:00
|
|
|
%{error, ?ERR_EXTENDED('not-acceptable', "invalid-subid")};
|
2008-07-11 14:48:27 +02:00
|
|
|
Affiliation == outcast ->
|
|
|
|
%% Requesting entity is blocked
|
2008-12-05 16:13:09 +01:00
|
|
|
{error, 'forbidden'};
|
2008-07-11 14:48:27 +02:00
|
|
|
(AccessModel == presence) and (not PresenceSubscription) ->
|
|
|
|
%% Entity is not authorized to create a subscription (presence subscription required)
|
2008-12-05 16:13:09 +01:00
|
|
|
{error, ?ERR_EXTENDED('not-authorized', "presence-subscription-required")};
|
2008-07-11 14:48:27 +02:00
|
|
|
(AccessModel == roster) and (not RosterGroup) ->
|
|
|
|
%% Entity is not authorized to create a subscription (not in roster group)
|
2008-12-05 16:13:09 +01:00
|
|
|
{error, ?ERR_EXTENDED('not-authorized', "not-in-roster-group")};
|
2009-01-03 01:58:18 +01:00
|
|
|
(AccessModel == whitelist) and (not Whitelisted) ->
|
|
|
|
%% Node has whitelist access model and entity lacks required affiliation
|
2008-12-05 16:13:09 +01:00
|
|
|
{error, ?ERR_EXTENDED('not-allowed', "closed-node")};
|
2008-07-11 14:48:27 +02:00
|
|
|
(AccessModel == authorize) -> % TODO: to be done
|
|
|
|
%% Node has authorize access model
|
2008-12-05 16:13:09 +01:00
|
|
|
{error, 'forbidden'};
|
2008-07-11 14:48:27 +02:00
|
|
|
%%MustPay ->
|
|
|
|
%% % Payment is required for a subscription
|
|
|
|
%% {error, ?ERR_PAYMENT_REQUIRED};
|
|
|
|
true ->
|
2009-05-01 01:17:38 +02:00
|
|
|
get_items(NodeId, JID)
|
2008-07-11 14:48:27 +02:00
|
|
|
end.
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
%% @spec (NodeId, ItemId) -> [Item] | []
|
|
|
|
%% NodeId = mod_pubsub:pubsubNodeId()
|
2007-12-01 06:16:30 +01:00
|
|
|
%% ItemId = string()
|
|
|
|
%% Item = mod_pubsub:pubsubItems()
|
|
|
|
%% @doc <p>Returns an item (one item list), given its reference.</p>
|
2009-05-01 01:17:38 +02:00
|
|
|
get_item(NodeId, ItemId) ->
|
|
|
|
case mnesia:read({pubsub_item, {ItemId, NodeId}}) of
|
2007-12-01 06:16:30 +01:00
|
|
|
[Item] when is_record(Item, pubsub_item) ->
|
|
|
|
{result, Item};
|
|
|
|
_ ->
|
2008-12-05 16:13:09 +01:00
|
|
|
{error, 'item-not-found'}
|
2007-12-01 06:16:30 +01:00
|
|
|
end.
|
2009-05-01 01:17:38 +02:00
|
|
|
get_item(NodeId, ItemId, JID, AccessModel, PresenceSubscription, RosterGroup, _SubId) ->
|
2009-01-08 18:27:47 +01:00
|
|
|
GenKey = jlib:short_prepd_bare_jid(JID),
|
2009-05-01 01:17:38 +02:00
|
|
|
GenState = get_state(NodeId, GenKey),
|
2009-01-08 18:27:47 +01:00
|
|
|
Affiliation = GenState#pubsub_state.affiliation,
|
2009-08-25 19:14:30 +02:00
|
|
|
Subscriptions = GenState#pubsub_state.subscriptions,
|
|
|
|
Whitelisted = can_fetch_item(Affiliation, Subscriptions),
|
2008-07-11 14:48:27 +02:00
|
|
|
if
|
2009-08-25 19:14:30 +02:00
|
|
|
%%SubId == "", ?? ->
|
2008-07-11 14:48:27 +02:00
|
|
|
%% Entity has multiple subscriptions to the node but does not specify a subscription ID
|
2008-12-05 16:13:09 +01:00
|
|
|
%{error, ?ERR_EXTENDED('bad-request', "subid-required")};
|
2008-07-11 14:48:27 +02:00
|
|
|
%%InvalidSubID ->
|
|
|
|
%% Entity is subscribed but specifies an invalid subscription ID
|
2008-12-05 16:13:09 +01:00
|
|
|
%{error, ?ERR_EXTENDED('not-acceptable', "invalid-subid")};
|
2008-07-11 14:48:27 +02:00
|
|
|
Affiliation == outcast ->
|
|
|
|
%% Requesting entity is blocked
|
2008-12-05 16:13:09 +01:00
|
|
|
{error, 'forbidden'};
|
2008-07-11 14:48:27 +02:00
|
|
|
(AccessModel == presence) and (not PresenceSubscription) ->
|
|
|
|
%% Entity is not authorized to create a subscription (presence subscription required)
|
2008-12-05 16:13:09 +01:00
|
|
|
{error, ?ERR_EXTENDED('not-authorized', "presence-subscription-required")};
|
2008-07-11 14:48:27 +02:00
|
|
|
(AccessModel == roster) and (not RosterGroup) ->
|
|
|
|
%% Entity is not authorized to create a subscription (not in roster group)
|
2008-12-05 16:13:09 +01:00
|
|
|
{error, ?ERR_EXTENDED('not-authorized', "not-in-roster-group")};
|
2009-01-03 01:58:18 +01:00
|
|
|
(AccessModel == whitelist) and (not Whitelisted) ->
|
|
|
|
%% Node has whitelist access model and entity lacks required affiliation
|
2008-12-05 16:13:09 +01:00
|
|
|
{error, ?ERR_EXTENDED('not-allowed', "closed-node")};
|
2008-07-11 14:48:27 +02:00
|
|
|
(AccessModel == authorize) -> % TODO: to be done
|
|
|
|
%% Node has authorize access model
|
2008-12-05 16:13:09 +01:00
|
|
|
{error, 'forbidden'};
|
2008-07-11 14:48:27 +02:00
|
|
|
%%MustPay ->
|
|
|
|
%% % Payment is required for a subscription
|
|
|
|
%% {error, ?ERR_PAYMENT_REQUIRED};
|
|
|
|
true ->
|
2009-05-01 01:17:38 +02:00
|
|
|
get_item(NodeId, ItemId)
|
2008-07-11 14:48:27 +02:00
|
|
|
end.
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2008-04-01 12:11:39 +02:00
|
|
|
%% @spec (Item) -> ok | {error, Reason::stanzaError()}
|
2007-12-01 06:16:30 +01:00
|
|
|
%% Item = mod_pubsub:pubsubItems()
|
2009-02-27 04:19:53 +01:00
|
|
|
%% @doc <p>Write an item into database.</p>
|
2007-12-01 06:16:30 +01:00
|
|
|
set_item(Item) when is_record(Item, pubsub_item) ->
|
|
|
|
mnesia:write(Item);
|
|
|
|
set_item(_) ->
|
2008-12-05 16:13:09 +01:00
|
|
|
{error, 'internal-server-error'}.
|
2008-02-06 19:04:23 +01:00
|
|
|
|
2009-05-01 01:17:38 +02:00
|
|
|
%% @spec (NodeId, ItemId) -> ok | {error, Reason::stanzaError()}
|
|
|
|
%% NodeId = mod_pubsub:pubsubNodeId()
|
2009-01-03 01:29:36 +01:00
|
|
|
%% ItemId = string()
|
|
|
|
%% @doc <p>Delete an item from database.</p>
|
2009-05-01 01:17:38 +02:00
|
|
|
del_item(NodeId, ItemId) ->
|
|
|
|
mnesia:delete({pubsub_item, {ItemId, NodeId}}).
|
|
|
|
del_items(NodeId, ItemIds) ->
|
2009-01-03 01:29:36 +01:00
|
|
|
lists:foreach(fun(ItemId) ->
|
2009-05-01 01:17:38 +02:00
|
|
|
del_item(NodeId, ItemId)
|
2009-01-03 01:29:36 +01:00
|
|
|
end, ItemIds).
|
|
|
|
|
2008-02-06 19:04:23 +01:00
|
|
|
%% @doc <p>Return the name of the node if known: Default is to return
|
|
|
|
%% node id.</p>
|
2009-05-29 01:21:50 +02:00
|
|
|
get_item_name(_Host, _Node, Id) ->
|
2008-02-06 19:04:23 +01:00
|
|
|
Id.
|
2009-02-28 00:56:46 +01:00
|
|
|
|
|
|
|
%% @spec (Affiliation, Subscription) -> true | false
|
|
|
|
%% Affiliation = owner | member | publisher | outcast | none
|
|
|
|
%% Subscription = subscribed | none
|
|
|
|
%% @doc Determines if the combination of Affiliation and Subscribed
|
|
|
|
%% are allowed to get items from a node.
|
|
|
|
can_fetch_item(owner, _) -> true;
|
|
|
|
can_fetch_item(member, _) -> true;
|
|
|
|
can_fetch_item(publisher, _) -> true;
|
|
|
|
can_fetch_item(outcast, _) -> false;
|
2009-08-25 19:14:30 +02:00
|
|
|
can_fetch_item(none, Subscriptions) -> is_subscribed(Subscriptions);
|
2009-02-28 00:56:46 +01:00
|
|
|
can_fetch_item(_Affiliation, _Subscription) -> false.
|
2009-05-07 02:54:44 +02:00
|
|
|
|
2009-08-25 19:14:30 +02:00
|
|
|
is_subscribed(Subscriptions) ->
|
|
|
|
lists:any(fun ({subscribed, _SubId}) -> true;
|
|
|
|
(_) -> false
|
|
|
|
end, Subscriptions).
|
|
|
|
|
|
|
|
%% Returns the first item where Pred() is true in List
|
|
|
|
first_in_list(_Pred, []) ->
|
|
|
|
false;
|
|
|
|
first_in_list(Pred, [H | T]) ->
|
|
|
|
case Pred(H) of
|
|
|
|
true -> {value, H};
|
|
|
|
_ -> first_in_list(Pred, T)
|
|
|
|
end.
|