2015-12-15 16:11:29 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : node_flat.erl
|
|
|
|
%%% Author : Christophe Romain <christophe.romain@process-one.net>
|
|
|
|
%%% Purpose : Standard PubSub node plugin
|
|
|
|
%%% Created : 1 Dec 2007 by Christophe Romain <christophe.romain@process-one.net>
|
2015-04-08 17:12:05 +02:00
|
|
|
%%%
|
2013-03-14 10:33:02 +01:00
|
|
|
%%%
|
2017-01-02 21:41:53 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2017 ProcessOne
|
2015-04-08 17:12:05 +02:00
|
|
|
%%%
|
2015-12-15 16:11:29 +01:00
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
2013-03-14 10:33:02 +01:00
|
|
|
%%%
|
2015-12-15 16:11:29 +01:00
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2008-07-03 11:56:31 +02:00
|
|
|
%%%
|
2015-12-15 16:11:29 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
2008-07-03 11:56:31 +02:00
|
|
|
|
2015-07-22 10:37:26 +02:00
|
|
|
%%% @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>
|
|
|
|
|
2008-12-08 20:50:50 +01:00
|
|
|
-module(node_flat).
|
2015-04-08 17:12:05 +02:00
|
|
|
-behaviour(gen_pubsub_node).
|
2008-07-03 11:56:31 +02:00
|
|
|
-author('christophe.romain@process-one.net').
|
|
|
|
|
|
|
|
-include("pubsub.hrl").
|
2016-09-08 14:49:27 +02:00
|
|
|
-include("xmpp.hrl").
|
2008-07-03 11:56:31 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([init/3, terminate/2, options/0, features/0,
|
2015-04-08 17:12:05 +02:00
|
|
|
create_node_permission/6, create_node/2, delete_node/1,
|
|
|
|
purge_node/2, subscribe_node/8, unsubscribe_node/4,
|
2015-07-01 17:18:32 +02:00
|
|
|
publish_item/7, delete_item/4, remove_extra_items/3,
|
2015-04-08 17:12:05 +02:00
|
|
|
get_entity_affiliations/2, get_node_affiliations/1,
|
|
|
|
get_affiliation/2, set_affiliation/3,
|
|
|
|
get_entity_subscriptions/2, get_node_subscriptions/1,
|
|
|
|
get_subscriptions/2, set_subscriptions/4,
|
|
|
|
get_pending_nodes/2, get_states/1, get_state/2,
|
2015-09-04 12:36:33 +02:00
|
|
|
set_state/1, get_items/7, get_items/3, get_item/7,
|
2015-04-08 17:12:05 +02:00
|
|
|
get_item/2, set_item/1, get_item_name/3, node_to_path/1,
|
2015-07-22 10:37:26 +02:00
|
|
|
path_to_node/1, can_fetch_item/2, is_subscribed/1]).
|
2015-04-08 17:12:05 +02:00
|
|
|
|
|
|
|
init(_Host, _ServerHost, _Opts) ->
|
2016-11-15 18:35:20 +01:00
|
|
|
%pubsub_subscription:init(Host, ServerHost, Opts),
|
2016-11-30 11:09:17 +01:00
|
|
|
ejabberd_mnesia:create(?MODULE, pubsub_state,
|
2015-04-08 17:12:05 +02:00
|
|
|
[{disc_copies, [node()]},
|
|
|
|
{type, ordered_set},
|
|
|
|
{attributes, record_info(fields, pubsub_state)}]),
|
2016-11-30 11:09:17 +01:00
|
|
|
ejabberd_mnesia:create(?MODULE, pubsub_item,
|
2015-04-08 17:12:05 +02:00
|
|
|
[{disc_only_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, pubsub_item)}]),
|
2016-12-09 12:01:42 +01:00
|
|
|
ejabberd_mnesia:create(?MODULE, pubsub_orphan,
|
|
|
|
[{disc_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, pubsub_orphan)}]),
|
2015-04-08 17:12:05 +02:00
|
|
|
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-07-03 11:56:31 +02:00
|
|
|
|
2015-07-22 10:37:26 +02:00
|
|
|
terminate(_Host, _ServerHost) ->
|
|
|
|
ok.
|
2008-07-03 11:56:31 +02:00
|
|
|
|
|
|
|
options() ->
|
2015-07-22 10:37:26 +02:00
|
|
|
[{deliver_payloads, true},
|
|
|
|
{notify_config, false},
|
|
|
|
{notify_delete, false},
|
|
|
|
{notify_retract, true},
|
|
|
|
{purge_offline, false},
|
|
|
|
{persist_items, true},
|
|
|
|
{max_items, ?MAXITEMS},
|
|
|
|
{subscribe, true},
|
|
|
|
{access_model, open},
|
|
|
|
{roster_groups_allowed, []},
|
|
|
|
{publish_model, publishers},
|
|
|
|
{notification_type, headline},
|
|
|
|
{max_payload_size, ?MAX_PAYLOAD_SIZE},
|
|
|
|
{send_last_published_item, on_sub_and_presence},
|
|
|
|
{deliver_notifications, true},
|
2016-10-07 09:31:03 +02:00
|
|
|
{title, <<>>},
|
2016-04-21 23:47:07 +02:00
|
|
|
{presence_based_delivery, false},
|
|
|
|
{itemreply, none}].
|
2015-04-08 17:12:05 +02:00
|
|
|
|
|
|
|
features() ->
|
2015-07-22 10:37:26 +02:00
|
|
|
[<<"create-nodes">>,
|
|
|
|
<<"auto-create">>,
|
|
|
|
<<"access-authorize">>,
|
|
|
|
<<"delete-nodes">>,
|
|
|
|
<<"delete-items">>,
|
|
|
|
<<"get-pending">>,
|
|
|
|
<<"instant-nodes">>,
|
|
|
|
<<"manage-subscriptions">>,
|
|
|
|
<<"modify-affiliations">>,
|
|
|
|
<<"outcast-affiliation">>,
|
|
|
|
<<"persistent-items">>,
|
|
|
|
<<"publish">>,
|
|
|
|
<<"publish-only-affiliation">>,
|
|
|
|
<<"purge-nodes">>,
|
|
|
|
<<"retract-items">>,
|
|
|
|
<<"retrieve-affiliations">>,
|
|
|
|
<<"retrieve-items">>,
|
|
|
|
<<"retrieve-subscriptions">>,
|
|
|
|
<<"subscribe">>,
|
2016-09-08 14:49:27 +02:00
|
|
|
%%<<"subscription-options">>,
|
2015-12-09 16:57:43 +01:00
|
|
|
<<"subscription-notifications">>].
|
2015-07-22 10:37:26 +02:00
|
|
|
|
|
|
|
%% @doc Checks if the current user has the permission to create the requested node
|
|
|
|
%% <p>In flat node, any unused node name is allowed. The access parameter is also
|
|
|
|
%% checked. This parameter depends on the value of the
|
|
|
|
%% <tt>access_createnode</tt> ACL value in ejabberd config file.</p>
|
2015-04-08 17:12:05 +02:00
|
|
|
create_node_permission(Host, ServerHost, _Node, _ParentNode, Owner, Access) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LOwner = jid:tolower(Owner),
|
2008-07-23 01:41:44 +02:00
|
|
|
Allowed = case LOwner of
|
2015-04-08 17:12:05 +02:00
|
|
|
{<<"">>, Host, <<"">>} ->
|
|
|
|
true; % pubsub service always allowed
|
|
|
|
_ ->
|
|
|
|
acl:match_rule(ServerHost, Access, LOwner) =:= allow
|
|
|
|
end,
|
2008-07-03 11:56:31 +02:00
|
|
|
{result, Allowed}.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
create_node(Nidx, Owner) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
OwnerKey = jid:tolower(jid:remove_resource(Owner)),
|
2015-07-22 10:37:26 +02:00
|
|
|
set_state(#pubsub_state{stateid = {OwnerKey, Nidx},
|
|
|
|
affiliation = owner}),
|
|
|
|
{result, {default, broadcast}}.
|
|
|
|
|
|
|
|
delete_node(Nodes) ->
|
|
|
|
Tr = fun (#pubsub_state{stateid = {J, _}, subscriptions = Ss}) ->
|
|
|
|
lists:map(fun (S) -> {J, S} end, Ss)
|
|
|
|
end,
|
|
|
|
Reply = lists:map(fun (#pubsub_node{id = Nidx} = PubsubNode) ->
|
|
|
|
{result, States} = get_states(Nidx),
|
2016-12-09 11:35:51 +01:00
|
|
|
lists:foreach(fun (State) ->
|
2016-12-09 12:01:42 +01:00
|
|
|
del_items(Nidx, State#pubsub_state.items),
|
|
|
|
del_state(State#pubsub_state{items = []})
|
2015-07-22 10:37:26 +02:00
|
|
|
end, States),
|
2016-12-09 12:01:42 +01:00
|
|
|
del_orphan_items(Nidx),
|
2015-07-22 10:37:26 +02:00
|
|
|
{PubsubNode, lists:flatmap(Tr, States)}
|
|
|
|
end, Nodes),
|
|
|
|
{result, {default, broadcast, Reply}}.
|
|
|
|
|
|
|
|
%% @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>
|
2015-04-08 17:12:05 +02:00
|
|
|
subscribe_node(Nidx, Sender, Subscriber, AccessModel,
|
2015-12-09 16:57:43 +01:00
|
|
|
SendLast, PresenceSubscription, RosterGroup, _Options) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
SubKey = jid:tolower(Subscriber),
|
|
|
|
GenKey = jid:remove_resource(SubKey),
|
|
|
|
Authorized = jid:tolower(jid:remove_resource(Sender)) == GenKey,
|
2015-07-22 10:37:26 +02:00
|
|
|
GenState = get_state(Nidx, GenKey),
|
|
|
|
SubState = case SubKey of
|
|
|
|
GenKey -> GenState;
|
|
|
|
_ -> get_state(Nidx, SubKey)
|
|
|
|
end,
|
|
|
|
Affiliation = GenState#pubsub_state.affiliation,
|
|
|
|
Subscriptions = SubState#pubsub_state.subscriptions,
|
|
|
|
Whitelisted = lists:member(Affiliation, [member, publisher, owner]),
|
|
|
|
PendingSubscription = lists:any(fun
|
|
|
|
({pending, _}) -> true;
|
|
|
|
(_) -> false
|
|
|
|
end,
|
|
|
|
Subscriptions),
|
|
|
|
Owner = Affiliation == owner,
|
|
|
|
if not Authorized ->
|
|
|
|
{error,
|
2016-09-08 14:49:27 +02:00
|
|
|
mod_pubsub:extended_error((xmpp:err_bad_request()), mod_pubsub:err_invalid_jid())};
|
2015-07-22 10:37:26 +02:00
|
|
|
(Affiliation == outcast) or (Affiliation == publish_only) ->
|
2016-09-08 14:49:27 +02:00
|
|
|
{error, xmpp:err_forbidden()};
|
2015-07-22 10:37:26 +02:00
|
|
|
PendingSubscription ->
|
|
|
|
{error,
|
2016-09-08 14:49:27 +02:00
|
|
|
mod_pubsub:extended_error((xmpp:err_not_authorized()), mod_pubsub:err_pending_subscription())};
|
2015-07-22 10:37:26 +02:00
|
|
|
(AccessModel == presence) and (not PresenceSubscription) and (not Owner) ->
|
|
|
|
{error,
|
2016-09-08 14:49:27 +02:00
|
|
|
mod_pubsub:extended_error((xmpp:err_not_authorized()), mod_pubsub:err_presence_subscription_required())};
|
2015-07-22 10:37:26 +02:00
|
|
|
(AccessModel == roster) and (not RosterGroup) and (not Owner) ->
|
|
|
|
{error,
|
2016-09-08 14:49:27 +02:00
|
|
|
mod_pubsub:extended_error((xmpp:err_not_authorized()), mod_pubsub:err_not_in_roster_group())};
|
2015-07-22 10:37:26 +02:00
|
|
|
(AccessModel == whitelist) and (not Whitelisted) and (not Owner) ->
|
|
|
|
{error,
|
2016-09-08 14:49:27 +02:00
|
|
|
mod_pubsub:extended_error((xmpp:err_not_allowed()), mod_pubsub:err_closed_node())};
|
2015-07-22 10:37:26 +02:00
|
|
|
%%MustPay ->
|
|
|
|
%% % Payment is required for a subscription
|
|
|
|
%% {error, ?ERR_PAYMENT_REQUIRED};
|
|
|
|
%%ForbiddenAnonymous ->
|
|
|
|
%% % Requesting entity is anonymous
|
2016-09-08 14:49:27 +02:00
|
|
|
%% {error, xmpp:err_forbidden()};
|
2015-07-22 10:37:26 +02:00
|
|
|
true ->
|
2015-12-09 16:57:43 +01:00
|
|
|
%%SubId = pubsub_subscription:add_subscription(Subscriber, Nidx, Options),
|
|
|
|
{NewSub, SubId} = case Subscriptions of
|
|
|
|
[{subscribed, Id}|_] ->
|
|
|
|
{subscribed, Id};
|
|
|
|
[] ->
|
|
|
|
Id = pubsub_subscription:make_subid(),
|
|
|
|
Sub = case AccessModel of
|
|
|
|
authorize -> pending;
|
|
|
|
_ -> subscribed
|
|
|
|
end,
|
|
|
|
set_state(SubState#pubsub_state{subscriptions =
|
|
|
|
[{Sub, Id} | Subscriptions]}),
|
|
|
|
{Sub, Id}
|
2015-07-22 10:37:26 +02:00
|
|
|
end,
|
|
|
|
case {NewSub, SendLast} of
|
|
|
|
{subscribed, never} ->
|
|
|
|
{result, {default, subscribed, SubId}};
|
|
|
|
{subscribed, _} ->
|
|
|
|
{result, {default, subscribed, SubId, send_last}};
|
|
|
|
{_, _} ->
|
|
|
|
{result, {default, pending, SubId}}
|
|
|
|
end
|
|
|
|
end.
|
2008-07-03 11:56:31 +02:00
|
|
|
|
2015-07-22 10:37:26 +02:00
|
|
|
%% @doc <p>Unsubscribe the <tt>Subscriber</tt> from the <tt>Node</tt>.</p>
|
2015-04-08 17:12:05 +02:00
|
|
|
unsubscribe_node(Nidx, Sender, Subscriber, SubId) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
SubKey = jid:tolower(Subscriber),
|
|
|
|
GenKey = jid:remove_resource(SubKey),
|
|
|
|
Authorized = jid:tolower(jid:remove_resource(Sender)) == GenKey,
|
2015-07-22 10:37:26 +02:00
|
|
|
GenState = get_state(Nidx, GenKey),
|
|
|
|
SubState = case SubKey of
|
|
|
|
GenKey -> GenState;
|
|
|
|
_ -> get_state(Nidx, SubKey)
|
|
|
|
end,
|
|
|
|
Subscriptions = lists:filter(fun
|
|
|
|
({_Sub, _SubId}) -> true;
|
|
|
|
(_SubId) -> false
|
|
|
|
end,
|
|
|
|
SubState#pubsub_state.subscriptions),
|
|
|
|
SubIdExists = case SubId of
|
|
|
|
<<>> -> false;
|
|
|
|
Binary when is_binary(Binary) -> true;
|
|
|
|
_ -> false
|
|
|
|
end,
|
|
|
|
if
|
|
|
|
%% Requesting entity is prohibited from unsubscribing entity
|
|
|
|
not Authorized ->
|
2016-09-08 14:49:27 +02:00
|
|
|
{error, xmpp:err_forbidden()};
|
2015-07-22 10:37:26 +02:00
|
|
|
%% Entity did not specify SubId
|
|
|
|
%%SubId == "", ?? ->
|
2016-09-08 14:49:27 +02:00
|
|
|
%% {error, mod_pubsub:extended_error(xmpp:err_bad_request(), "subid-required")};
|
2015-07-22 10:37:26 +02:00
|
|
|
%% Invalid subscription identifier
|
|
|
|
%%InvalidSubId ->
|
2016-09-08 14:49:27 +02:00
|
|
|
%% {error, mod_pubsub:extended_error(?ERR_NOT_ACCEPTABLE, "invalid-subid")};
|
2015-07-22 10:37:26 +02:00
|
|
|
%% Requesting entity is not a subscriber
|
|
|
|
Subscriptions == [] ->
|
|
|
|
{error,
|
2016-09-08 14:49:27 +02:00
|
|
|
mod_pubsub:extended_error(xmpp:err_unexpected_request(), mod_pubsub:err_not_subscribed())};
|
2015-07-22 10:37:26 +02:00
|
|
|
%% Subid supplied, so use that.
|
|
|
|
SubIdExists ->
|
|
|
|
Sub = first_in_list(fun
|
|
|
|
({_, S}) when S == SubId -> true;
|
|
|
|
(_) -> false
|
|
|
|
end,
|
|
|
|
SubState#pubsub_state.subscriptions),
|
|
|
|
case Sub of
|
|
|
|
{value, S} ->
|
2016-12-09 11:35:51 +01:00
|
|
|
delete_subscriptions(SubState, [S]),
|
2015-07-22 10:37:26 +02:00
|
|
|
{result, default};
|
|
|
|
false ->
|
|
|
|
{error,
|
2016-09-08 14:49:27 +02:00
|
|
|
mod_pubsub:extended_error(xmpp:err_unexpected_request(), mod_pubsub:err_not_subscribed())}
|
2015-07-22 10:37:26 +02:00
|
|
|
end;
|
|
|
|
%% Asking to remove all subscriptions to the given node
|
|
|
|
SubId == all ->
|
2016-12-09 11:35:51 +01:00
|
|
|
delete_subscriptions(SubState, Subscriptions),
|
2015-07-22 10:37:26 +02:00
|
|
|
{result, default};
|
|
|
|
%% No subid supplied, but there's only one matching subscription
|
|
|
|
length(Subscriptions) == 1 ->
|
2016-12-09 11:35:51 +01:00
|
|
|
delete_subscriptions(SubState, Subscriptions),
|
2015-07-22 10:37:26 +02:00
|
|
|
{result, default};
|
|
|
|
%% No subid and more than one possible subscription match.
|
|
|
|
true ->
|
|
|
|
{error,
|
2016-09-08 14:49:27 +02:00
|
|
|
mod_pubsub:extended_error((xmpp:err_bad_request()), mod_pubsub:err_subid_required())}
|
2015-07-22 10:37:26 +02:00
|
|
|
end.
|
2008-07-03 11:56:31 +02:00
|
|
|
|
2016-12-09 11:35:51 +01:00
|
|
|
delete_subscriptions(SubState, Subscriptions) ->
|
2015-07-22 10:37:26 +02:00
|
|
|
NewSubs = lists:foldl(fun ({Subscription, SubId}, Acc) ->
|
2015-12-09 16:57:43 +01:00
|
|
|
%%pubsub_subscription:delete_subscription(SubKey, Nidx, SubId),
|
2015-07-22 10:37:26 +02:00
|
|
|
Acc -- [{Subscription, SubId}]
|
|
|
|
end, SubState#pubsub_state.subscriptions, Subscriptions),
|
|
|
|
case {SubState#pubsub_state.affiliation, NewSubs} of
|
2016-12-09 11:35:51 +01:00
|
|
|
{none, []} -> del_state(SubState);
|
2015-07-22 10:37:26 +02:00
|
|
|
_ -> set_state(SubState#pubsub_state{subscriptions = NewSubs})
|
|
|
|
end.
|
2008-07-03 11:56:31 +02:00
|
|
|
|
2015-07-22 10:37:26 +02:00
|
|
|
%% @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>
|
2015-07-01 17:18:32 +02:00
|
|
|
publish_item(Nidx, Publisher, PublishModel, MaxItems, ItemId, Payload,
|
|
|
|
_PubOpts) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
SubKey = jid:tolower(Publisher),
|
|
|
|
GenKey = jid:remove_resource(SubKey),
|
2015-07-22 10:37:26 +02:00
|
|
|
GenState = get_state(Nidx, GenKey),
|
|
|
|
SubState = case SubKey of
|
|
|
|
GenKey -> GenState;
|
|
|
|
_ -> get_state(Nidx, SubKey)
|
|
|
|
end,
|
|
|
|
Affiliation = GenState#pubsub_state.affiliation,
|
|
|
|
Subscribed = case PublishModel of
|
|
|
|
subscribers -> is_subscribed(GenState#pubsub_state.subscriptions) orelse
|
|
|
|
is_subscribed(SubState#pubsub_state.subscriptions);
|
|
|
|
_ -> undefined
|
|
|
|
end,
|
|
|
|
if not ((PublishModel == open) or
|
|
|
|
(PublishModel == publishers) and
|
|
|
|
((Affiliation == owner)
|
|
|
|
or (Affiliation == publisher)
|
|
|
|
or (Affiliation == publish_only))
|
|
|
|
or (Subscribed == true)) ->
|
2016-09-08 14:49:27 +02:00
|
|
|
{error, xmpp:err_forbidden()};
|
2015-07-22 10:37:26 +02:00
|
|
|
true ->
|
|
|
|
if MaxItems > 0 ->
|
2015-12-07 16:16:11 +01:00
|
|
|
Now = p1_time_compat:timestamp(),
|
2015-07-22 10:37:26 +02:00
|
|
|
PubId = {Now, SubKey},
|
|
|
|
Item = case get_item(Nidx, ItemId) of
|
|
|
|
{result, OldItem} ->
|
|
|
|
OldItem#pubsub_item{modification = PubId,
|
|
|
|
payload = Payload};
|
|
|
|
_ ->
|
|
|
|
#pubsub_item{itemid = {ItemId, Nidx},
|
|
|
|
creation = {Now, GenKey},
|
|
|
|
modification = PubId,
|
|
|
|
payload = Payload}
|
|
|
|
end,
|
|
|
|
Items = [ItemId | GenState#pubsub_state.items -- [ItemId]],
|
|
|
|
{result, {NI, OI}} = remove_extra_items(Nidx, MaxItems, Items),
|
|
|
|
set_item(Item),
|
|
|
|
set_state(GenState#pubsub_state{items = NI}),
|
|
|
|
{result, {default, broadcast, OI}};
|
|
|
|
true ->
|
|
|
|
{result, {default, broadcast, []}}
|
|
|
|
end
|
|
|
|
end.
|
2008-07-03 11:56:31 +02:00
|
|
|
|
2015-07-22 10:37:26 +02:00
|
|
|
%% @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:
|
|
|
|
%% ```remove_extra_items(Nidx, MaxItems, ItemIds) ->
|
|
|
|
%% node_default:remove_extra_items(Nidx, MaxItems, ItemIds).'''</p>
|
|
|
|
remove_extra_items(_Nidx, unlimited, ItemIds) ->
|
|
|
|
{result, {ItemIds, []}};
|
|
|
|
remove_extra_items(Nidx, MaxItems, ItemIds) ->
|
|
|
|
NewItems = lists:sublist(ItemIds, MaxItems),
|
|
|
|
OldItems = lists:nthtail(length(NewItems), ItemIds),
|
|
|
|
del_items(Nidx, OldItems),
|
|
|
|
{result, {NewItems, OldItems}}.
|
|
|
|
|
|
|
|
%% @doc <p>Triggers item deletion.</p>
|
|
|
|
%% <p>Default plugin: The user performing the deletion must be the node owner
|
|
|
|
%% or a publisher, or PublishModel being open.</p>
|
2015-04-08 17:12:05 +02:00
|
|
|
delete_item(Nidx, Publisher, PublishModel, ItemId) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
SubKey = jid:tolower(Publisher),
|
|
|
|
GenKey = jid:remove_resource(SubKey),
|
2015-07-22 10:37:26 +02:00
|
|
|
GenState = get_state(Nidx, GenKey),
|
|
|
|
#pubsub_state{affiliation = Affiliation, items = Items} = GenState,
|
|
|
|
Allowed = Affiliation == publisher orelse
|
|
|
|
Affiliation == owner orelse
|
2016-09-29 11:20:56 +02:00
|
|
|
(PublishModel == open andalso
|
|
|
|
case get_item(Nidx, ItemId) of
|
|
|
|
{result, #pubsub_item{creation = {_, GenKey}}} -> true;
|
|
|
|
_ -> false
|
|
|
|
end),
|
2015-07-22 10:37:26 +02:00
|
|
|
if not Allowed ->
|
2016-09-08 14:49:27 +02:00
|
|
|
{error, xmpp:err_forbidden()};
|
2015-07-22 10:37:26 +02:00
|
|
|
true ->
|
|
|
|
case lists:member(ItemId, Items) of
|
|
|
|
true ->
|
|
|
|
del_item(Nidx, ItemId),
|
|
|
|
set_state(GenState#pubsub_state{items = lists:delete(ItemId, Items)}),
|
|
|
|
{result, {default, broadcast}};
|
|
|
|
false ->
|
|
|
|
case Affiliation of
|
|
|
|
owner ->
|
|
|
|
{result, States} = get_states(Nidx),
|
|
|
|
lists:foldl(fun
|
|
|
|
(#pubsub_state{items = PI} = S, Res) ->
|
|
|
|
case lists:member(ItemId, PI) of
|
|
|
|
true ->
|
|
|
|
Nitems = lists:delete(ItemId, PI),
|
|
|
|
del_item(Nidx, ItemId),
|
|
|
|
set_state(S#pubsub_state{items = Nitems}),
|
|
|
|
{result, {default, broadcast}};
|
|
|
|
false ->
|
|
|
|
Res
|
|
|
|
end;
|
|
|
|
(_, Res) ->
|
|
|
|
Res
|
|
|
|
end,
|
2016-09-08 14:49:27 +02:00
|
|
|
{error, xmpp:err_item_not_found()}, States);
|
2015-07-22 10:37:26 +02:00
|
|
|
_ ->
|
2016-10-07 09:31:03 +02:00
|
|
|
{error, xmpp:err_forbidden()}
|
2015-07-22 10:37:26 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end.
|
2008-07-03 11:56:31 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
purge_node(Nidx, Owner) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
SubKey = jid:tolower(Owner),
|
|
|
|
GenKey = jid:remove_resource(SubKey),
|
2015-07-22 10:37:26 +02:00
|
|
|
GenState = get_state(Nidx, GenKey),
|
|
|
|
case GenState of
|
|
|
|
#pubsub_state{affiliation = owner} ->
|
|
|
|
{result, States} = get_states(Nidx),
|
|
|
|
lists:foreach(fun
|
|
|
|
(#pubsub_state{items = []}) ->
|
|
|
|
ok;
|
|
|
|
(#pubsub_state{items = Items} = S) ->
|
|
|
|
del_items(Nidx, Items),
|
|
|
|
set_state(S#pubsub_state{items = []})
|
|
|
|
end,
|
|
|
|
States),
|
2016-12-09 12:01:42 +01:00
|
|
|
del_orphan_items(Nidx),
|
2015-07-22 10:37:26 +02:00
|
|
|
{result, {default, broadcast}};
|
|
|
|
_ ->
|
2016-09-08 14:49:27 +02:00
|
|
|
{error, xmpp:err_forbidden()}
|
2015-07-22 10:37:26 +02:00
|
|
|
end.
|
2008-07-03 11:56:31 +02:00
|
|
|
|
2015-07-22 10:37:26 +02:00
|
|
|
%% @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>
|
2008-07-03 11:56:31 +02:00
|
|
|
get_entity_affiliations(Host, Owner) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
SubKey = jid:tolower(Owner),
|
|
|
|
GenKey = jid:remove_resource(SubKey),
|
2015-07-22 10:37:26 +02:00
|
|
|
States = mnesia:match_object(#pubsub_state{stateid = {GenKey, '_'}, _ = '_'}),
|
|
|
|
NodeTree = mod_pubsub:tree(Host),
|
|
|
|
Reply = lists:foldl(fun (#pubsub_state{stateid = {_, N}, affiliation = A}, Acc) ->
|
|
|
|
case NodeTree:get_node(N) of
|
|
|
|
#pubsub_node{nodeid = {Host, _}} = Node -> [{Node, A} | Acc];
|
|
|
|
_ -> Acc
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
[], States),
|
|
|
|
{result, Reply}.
|
2008-07-03 11:56:31 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_node_affiliations(Nidx) ->
|
2015-07-22 10:37:26 +02:00
|
|
|
{result, States} = get_states(Nidx),
|
|
|
|
Tr = fun (#pubsub_state{stateid = {J, _}, affiliation = A}) -> {J, A} end,
|
|
|
|
{result, lists:map(Tr, States)}.
|
2008-07-03 11:56:31 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_affiliation(Nidx, Owner) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
SubKey = jid:tolower(Owner),
|
|
|
|
GenKey = jid:remove_resource(SubKey),
|
2015-07-22 10:37:26 +02:00
|
|
|
#pubsub_state{affiliation = Affiliation} = get_state(Nidx, GenKey),
|
|
|
|
{result, Affiliation}.
|
2008-07-03 11:56:31 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
set_affiliation(Nidx, Owner, Affiliation) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
SubKey = jid:tolower(Owner),
|
|
|
|
GenKey = jid:remove_resource(SubKey),
|
2015-07-22 10:37:26 +02:00
|
|
|
GenState = get_state(Nidx, GenKey),
|
|
|
|
case {Affiliation, GenState#pubsub_state.subscriptions} of
|
2016-12-09 11:35:51 +01:00
|
|
|
{none, []} -> del_state(GenState);
|
2015-07-22 10:37:26 +02:00
|
|
|
_ -> set_state(GenState#pubsub_state{affiliation = Affiliation})
|
|
|
|
end.
|
2008-07-03 11:56:31 +02:00
|
|
|
|
2015-07-22 10:37:26 +02: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>
|
2008-07-03 11:56:31 +02:00
|
|
|
get_entity_subscriptions(Host, Owner) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
{U, D, _} = SubKey = jid:tolower(Owner),
|
|
|
|
GenKey = jid:remove_resource(SubKey),
|
2015-07-22 10:37:26 +02:00
|
|
|
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,
|
|
|
|
NodeTree = mod_pubsub:tree(Host),
|
|
|
|
Reply = lists:foldl(fun (#pubsub_state{stateid = {J, N}, subscriptions = Ss}, Acc) ->
|
|
|
|
case NodeTree:get_node(N) of
|
|
|
|
#pubsub_node{nodeid = {Host, _}} = Node ->
|
|
|
|
lists:foldl(fun ({Sub, SubId}, Acc2) ->
|
|
|
|
[{Node, Sub, SubId, J} | Acc2]
|
|
|
|
end,
|
|
|
|
Acc, Ss);
|
|
|
|
_ ->
|
|
|
|
Acc
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
[], States),
|
|
|
|
{result, Reply}.
|
2008-07-03 11:56:31 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_node_subscriptions(Nidx) ->
|
2015-07-22 10:37:26 +02:00
|
|
|
{result, States} = get_states(Nidx),
|
|
|
|
Tr = fun (#pubsub_state{stateid = {J, _}, subscriptions = Subscriptions}) ->
|
|
|
|
case Subscriptions of
|
|
|
|
[_ | _] ->
|
|
|
|
lists:foldl(fun ({S, SubId}, Acc) ->
|
|
|
|
[{J, S, SubId} | Acc]
|
|
|
|
end,
|
|
|
|
[], Subscriptions);
|
|
|
|
[] ->
|
|
|
|
[];
|
|
|
|
_ ->
|
|
|
|
[{J, none}]
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
{result, lists:flatmap(Tr, States)}.
|
2008-07-03 11:56:31 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_subscriptions(Nidx, Owner) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
SubKey = jid:tolower(Owner),
|
2015-07-22 10:37:26 +02:00
|
|
|
SubState = get_state(Nidx, SubKey),
|
|
|
|
{result, SubState#pubsub_state.subscriptions}.
|
2008-07-03 11:56:31 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
set_subscriptions(Nidx, Owner, Subscription, SubId) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
SubKey = jid:tolower(Owner),
|
2015-07-22 10:37:26 +02:00
|
|
|
SubState = get_state(Nidx, SubKey),
|
|
|
|
case {SubId, SubState#pubsub_state.subscriptions} of
|
|
|
|
{_, []} ->
|
|
|
|
case Subscription of
|
|
|
|
none ->
|
|
|
|
{error,
|
2016-09-08 14:49:27 +02:00
|
|
|
mod_pubsub:extended_error((xmpp:err_bad_request()), mod_pubsub:err_not_subscribed())};
|
2015-07-22 10:37:26 +02:00
|
|
|
_ ->
|
|
|
|
new_subscription(Nidx, Owner, Subscription, SubState)
|
|
|
|
end;
|
|
|
|
{<<>>, [{_, SID}]} ->
|
|
|
|
case Subscription of
|
2016-12-09 11:35:51 +01:00
|
|
|
none -> unsub_with_subid(SubState, SID);
|
2015-07-22 10:37:26 +02:00
|
|
|
_ -> replace_subscription({Subscription, SID}, SubState)
|
|
|
|
end;
|
|
|
|
{<<>>, [_ | _]} ->
|
|
|
|
{error,
|
2016-09-08 14:49:27 +02:00
|
|
|
mod_pubsub:extended_error((xmpp:err_bad_request()), mod_pubsub:err_subid_required())};
|
2015-07-22 10:37:26 +02:00
|
|
|
_ ->
|
|
|
|
case Subscription of
|
2016-12-09 11:35:51 +01:00
|
|
|
none -> unsub_with_subid(SubState, SubId);
|
2015-07-22 10:37:26 +02:00
|
|
|
_ -> replace_subscription({Subscription, SubId}, SubState)
|
|
|
|
end
|
|
|
|
end.
|
2008-07-03 11:56:31 +02:00
|
|
|
|
2015-07-22 10:37:26 +02:00
|
|
|
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]).
|
|
|
|
|
2015-12-09 16:57:43 +01:00
|
|
|
new_subscription(_Nidx, _Owner, Sub, SubState) ->
|
|
|
|
%%SubId = pubsub_subscription:add_subscription(Owner, Nidx, []),
|
|
|
|
SubId = pubsub_subscription:make_subid(),
|
2015-07-22 10:37:26 +02:00
|
|
|
Subs = SubState#pubsub_state.subscriptions,
|
|
|
|
set_state(SubState#pubsub_state{subscriptions = [{Sub, SubId} | Subs]}),
|
|
|
|
{Sub, SubId}.
|
|
|
|
|
2016-12-09 11:35:51 +01:00
|
|
|
unsub_with_subid(SubState, SubId) ->
|
2015-12-09 16:57:43 +01:00
|
|
|
%%pubsub_subscription:delete_subscription(SubState#pubsub_state.stateid, Nidx, SubId),
|
2015-07-22 10:37:26 +02:00
|
|
|
NewSubs = [{S, Sid}
|
|
|
|
|| {S, Sid} <- SubState#pubsub_state.subscriptions,
|
|
|
|
SubId =/= Sid],
|
|
|
|
case {NewSubs, SubState#pubsub_state.affiliation} of
|
2016-12-09 11:35:51 +01:00
|
|
|
{[], none} -> del_state(SubState);
|
2015-07-22 10:37:26 +02:00
|
|
|
_ -> set_state(SubState#pubsub_state{subscriptions = NewSubs})
|
|
|
|
end.
|
|
|
|
|
|
|
|
%% @doc <p>Returns a list of Owner's nodes on Host with pending
|
|
|
|
%% subscriptions.</p>
|
2009-07-20 10:53:56 +02:00
|
|
|
get_pending_nodes(Host, Owner) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
GenKey = jid:remove_resource(jid:tolower(Owner)),
|
2015-07-22 10:37:26 +02:00
|
|
|
States = mnesia:match_object(#pubsub_state{stateid = {GenKey, '_'},
|
|
|
|
affiliation = owner,
|
|
|
|
_ = '_'}),
|
|
|
|
NodeIdxs = [Nidx || #pubsub_state{stateid = {_, Nidx}} <- States],
|
|
|
|
NodeTree = mod_pubsub:tree(Host),
|
|
|
|
Reply = mnesia:foldl(fun (#pubsub_state{stateid = {_, Nidx}} = S, Acc) ->
|
|
|
|
case lists:member(Nidx, NodeIdxs) 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.
|
2009-07-20 10:53:56 +02:00
|
|
|
|
2015-07-22 10:37:26 +02: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:
|
|
|
|
%% ```get_states(Nidx) ->
|
|
|
|
%% node_default:get_states(Nidx).'''</p>
|
2015-04-08 17:12:05 +02:00
|
|
|
get_states(Nidx) ->
|
2015-07-22 10:37:26 +02:00
|
|
|
States = case catch mnesia:match_object(
|
|
|
|
#pubsub_state{stateid = {'_', Nidx}, _ = '_'}) of
|
|
|
|
List when is_list(List) -> List;
|
|
|
|
_ -> []
|
|
|
|
end,
|
|
|
|
{result, States}.
|
|
|
|
|
|
|
|
%% @doc <p>Returns a state (one state list), given its reference.</p>
|
|
|
|
get_state(Nidx, Key) ->
|
|
|
|
StateId = {Key, Nidx},
|
|
|
|
case catch mnesia:read({pubsub_state, StateId}) of
|
|
|
|
[State] when is_record(State, pubsub_state) -> State;
|
|
|
|
_ -> #pubsub_state{stateid = StateId}
|
|
|
|
end.
|
2008-07-03 11:56:31 +02:00
|
|
|
|
2015-07-22 10:37:26 +02:00
|
|
|
%% @doc <p>Write a state into database.</p>
|
|
|
|
set_state(State) when is_record(State, pubsub_state) ->
|
|
|
|
mnesia:write(State).
|
|
|
|
%set_state(_) -> {error, ?ERR_INTERNAL_SERVER_ERROR}.
|
|
|
|
|
|
|
|
%% @doc <p>Delete a state from database.</p>
|
2016-12-09 12:01:42 +01:00
|
|
|
del_state(#pubsub_state{stateid = {Key, Nidx}, items = Items}) ->
|
|
|
|
case Items of
|
|
|
|
[] ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
Orphan = #pubsub_orphan{nodeid = Nidx, items = Items},
|
|
|
|
mnesia:write(Orphan)
|
|
|
|
end,
|
|
|
|
mnesia:delete({pubsub_state, {Key, Nidx}}).
|
2015-07-22 10:37:26 +02:00
|
|
|
|
|
|
|
%% @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>
|
|
|
|
get_items(Nidx, _From, _RSM) ->
|
|
|
|
Items = mnesia:match_object(#pubsub_item{itemid = {'_', Nidx}, _ = '_'}),
|
2016-09-13 11:30:05 +02:00
|
|
|
{result, {lists:reverse(lists:keysort(#pubsub_item.modification, Items)), undefined}}.
|
2015-07-22 10:37:26 +02:00
|
|
|
|
2015-09-04 12:36:33 +02:00
|
|
|
get_items(Nidx, JID, AccessModel, PresenceSubscription, RosterGroup, _SubId, RSM) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
SubKey = jid:tolower(JID),
|
|
|
|
GenKey = jid:remove_resource(SubKey),
|
2015-07-22 10:37:26 +02:00
|
|
|
GenState = get_state(Nidx, GenKey),
|
|
|
|
SubState = get_state(Nidx, SubKey),
|
|
|
|
Affiliation = GenState#pubsub_state.affiliation,
|
|
|
|
BareSubscriptions = GenState#pubsub_state.subscriptions,
|
|
|
|
FullSubscriptions = SubState#pubsub_state.subscriptions,
|
|
|
|
Whitelisted = can_fetch_item(Affiliation, BareSubscriptions) orelse
|
|
|
|
can_fetch_item(Affiliation, FullSubscriptions),
|
|
|
|
if %%SubId == "", ?? ->
|
|
|
|
%% Entity has multiple subscriptions to the node but does not specify a subscription ID
|
2016-09-08 14:49:27 +02:00
|
|
|
%{error, mod_pubsub:extended_error(xmpp:err_bad_request(), "subid-required")};
|
2015-07-22 10:37:26 +02:00
|
|
|
%%InvalidSubId ->
|
|
|
|
%% Entity is subscribed but specifies an invalid subscription ID
|
2016-09-08 14:49:27 +02:00
|
|
|
%{error, mod_pubsub:extended_error(?ERR_NOT_ACCEPTABLE, "invalid-subid")};
|
2015-07-22 10:37:26 +02:00
|
|
|
(Affiliation == outcast) or (Affiliation == publish_only) ->
|
2016-09-08 14:49:27 +02:00
|
|
|
{error, xmpp:err_forbidden()};
|
2015-07-22 10:37:26 +02:00
|
|
|
(AccessModel == presence) and not PresenceSubscription ->
|
|
|
|
{error,
|
2016-09-08 14:49:27 +02:00
|
|
|
mod_pubsub:extended_error((xmpp:err_not_authorized()), mod_pubsub:err_presence_subscription_required())};
|
2015-07-22 10:37:26 +02:00
|
|
|
(AccessModel == roster) and not RosterGroup ->
|
|
|
|
{error,
|
2016-09-08 14:49:27 +02:00
|
|
|
mod_pubsub:extended_error((xmpp:err_not_authorized()), mod_pubsub:err_not_in_roster_group())};
|
2015-07-22 10:37:26 +02:00
|
|
|
(AccessModel == whitelist) and not Whitelisted ->
|
|
|
|
{error,
|
2016-09-08 14:49:27 +02:00
|
|
|
mod_pubsub:extended_error((xmpp:err_not_allowed()), mod_pubsub:err_closed_node())};
|
2015-07-22 10:37:26 +02:00
|
|
|
(AccessModel == authorize) and not Whitelisted ->
|
2016-09-08 14:49:27 +02:00
|
|
|
{error, xmpp:err_forbidden()};
|
2015-07-22 10:37:26 +02:00
|
|
|
%%MustPay ->
|
|
|
|
%% % Payment is required for a subscription
|
|
|
|
%% {error, ?ERR_PAYMENT_REQUIRED};
|
|
|
|
true ->
|
2015-09-04 12:36:33 +02:00
|
|
|
get_items(Nidx, JID, RSM)
|
2015-07-22 10:37:26 +02:00
|
|
|
end.
|
2008-07-03 11:56:31 +02:00
|
|
|
|
2015-07-22 10:37:26 +02:00
|
|
|
%% @doc <p>Returns an item (one item list), given its reference.</p>
|
2008-07-03 11:56:31 +02:00
|
|
|
|
2015-07-22 10:37:26 +02:00
|
|
|
get_item(Nidx, ItemId) ->
|
|
|
|
case mnesia:read({pubsub_item, {ItemId, Nidx}}) of
|
|
|
|
[Item] when is_record(Item, pubsub_item) -> {result, Item};
|
2016-09-08 14:49:27 +02:00
|
|
|
_ -> {error, xmpp:err_item_not_found()}
|
2015-07-22 10:37:26 +02:00
|
|
|
end.
|
2008-07-03 11:56:31 +02:00
|
|
|
|
2015-07-22 10:37:26 +02:00
|
|
|
get_item(Nidx, ItemId, JID, AccessModel, PresenceSubscription, RosterGroup, _SubId) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
SubKey = jid:tolower(JID),
|
|
|
|
GenKey = jid:remove_resource(SubKey),
|
2015-07-22 10:37:26 +02:00
|
|
|
GenState = get_state(Nidx, GenKey),
|
|
|
|
Affiliation = GenState#pubsub_state.affiliation,
|
|
|
|
Subscriptions = GenState#pubsub_state.subscriptions,
|
|
|
|
Whitelisted = can_fetch_item(Affiliation, Subscriptions),
|
|
|
|
if %%SubId == "", ?? ->
|
|
|
|
%% Entity has multiple subscriptions to the node but does not specify a subscription ID
|
2016-09-08 14:49:27 +02:00
|
|
|
%{error, mod_pubsub:extended_error(xmpp:err_bad_request(), "subid-required")};
|
2015-07-22 10:37:26 +02:00
|
|
|
%%InvalidSubId ->
|
|
|
|
%% Entity is subscribed but specifies an invalid subscription ID
|
2016-09-08 14:49:27 +02:00
|
|
|
%{error, mod_pubsub:extended_error(?ERR_NOT_ACCEPTABLE, "invalid-subid")};
|
2015-07-22 10:37:26 +02:00
|
|
|
(Affiliation == outcast) or (Affiliation == publish_only) ->
|
2016-09-08 14:49:27 +02:00
|
|
|
{error, xmpp:err_forbidden()};
|
2015-07-22 10:37:26 +02:00
|
|
|
(AccessModel == presence) and not PresenceSubscription ->
|
|
|
|
{error,
|
2016-09-08 14:49:27 +02:00
|
|
|
mod_pubsub:extended_error((xmpp:err_not_authorized()), mod_pubsub:err_presence_subscription_required())};
|
2015-07-22 10:37:26 +02:00
|
|
|
(AccessModel == roster) and not RosterGroup ->
|
|
|
|
{error,
|
2016-09-08 14:49:27 +02:00
|
|
|
mod_pubsub:extended_error((xmpp:err_not_authorized()), mod_pubsub:err_not_in_roster_group())};
|
2015-07-22 10:37:26 +02:00
|
|
|
(AccessModel == whitelist) and not Whitelisted ->
|
|
|
|
{error,
|
2016-09-08 14:49:27 +02:00
|
|
|
mod_pubsub:extended_error((xmpp:err_not_allowed()), mod_pubsub:err_closed_node())};
|
2015-07-22 10:37:26 +02:00
|
|
|
(AccessModel == authorize) and not Whitelisted ->
|
2016-09-08 14:49:27 +02:00
|
|
|
{error, xmpp:err_forbidden()};
|
2015-07-22 10:37:26 +02:00
|
|
|
%%MustPay ->
|
|
|
|
%% % Payment is required for a subscription
|
|
|
|
%% {error, ?ERR_PAYMENT_REQUIRED};
|
|
|
|
true ->
|
|
|
|
get_item(Nidx, ItemId)
|
|
|
|
end.
|
2008-07-03 11:56:31 +02:00
|
|
|
|
2015-07-22 10:37:26 +02:00
|
|
|
%% @doc <p>Write an item into database.</p>
|
|
|
|
set_item(Item) when is_record(Item, pubsub_item) ->
|
|
|
|
mnesia:write(Item).
|
|
|
|
%set_item(_) -> {error, ?ERR_INTERNAL_SERVER_ERROR}.
|
2009-04-08 17:53:46 +02:00
|
|
|
|
2015-07-22 10:37:26 +02:00
|
|
|
%% @doc <p>Delete an item from database.</p>
|
|
|
|
del_item(Nidx, ItemId) ->
|
|
|
|
mnesia:delete({pubsub_item, {ItemId, Nidx}}).
|
2008-07-03 11:56:31 +02:00
|
|
|
|
2015-07-22 10:37:26 +02:00
|
|
|
del_items(Nidx, ItemIds) ->
|
|
|
|
lists:foreach(fun (ItemId) -> del_item(Nidx, ItemId)
|
|
|
|
end,
|
|
|
|
ItemIds).
|
2008-07-09 23:26:25 +02:00
|
|
|
|
2016-12-09 12:01:42 +01:00
|
|
|
del_orphan_items(Nidx) ->
|
|
|
|
case mnesia:read({pubsub_orphan, Nidx}) of
|
|
|
|
[#pubsub_orphan{items = ItemIds}] ->
|
|
|
|
del_items(Nidx, ItemIds),
|
|
|
|
mnesia:delete({pubsub_orphan, Nidx});
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2015-07-22 10:37:26 +02:00
|
|
|
get_item_name(_Host, _Node, Id) ->
|
|
|
|
Id.
|
2009-10-20 17:03:07 +02:00
|
|
|
|
2015-07-22 10:37:26 +02:00
|
|
|
%% @doc <p>Return the path of the node. In flat it's just node id.</p>
|
2015-04-08 17:12:05 +02:00
|
|
|
node_to_path(Node) ->
|
|
|
|
[(Node)].
|
2009-10-20 17:03:07 +02:00
|
|
|
|
|
|
|
path_to_node(Path) ->
|
|
|
|
case Path of
|
2015-04-08 17:12:05 +02:00
|
|
|
% default slot
|
|
|
|
[Node] -> iolist_to_binary(Node);
|
|
|
|
% handle old possible entries, used when migrating database content to new format
|
|
|
|
[Node | _] when is_binary(Node) ->
|
|
|
|
iolist_to_binary(str:join([<<"">> | Path], <<"/">>));
|
|
|
|
% default case (used by PEP for example)
|
|
|
|
_ -> iolist_to_binary(Path)
|
2009-10-20 17:03:07 +02:00
|
|
|
end.
|
2015-07-22 10:37:26 +02:00
|
|
|
|
|
|
|
can_fetch_item(owner, _) -> true;
|
|
|
|
can_fetch_item(member, _) -> true;
|
|
|
|
can_fetch_item(publisher, _) -> true;
|
|
|
|
can_fetch_item(publish_only, _) -> false;
|
|
|
|
can_fetch_item(outcast, _) -> false;
|
|
|
|
can_fetch_item(none, Subscriptions) -> is_subscribed(Subscriptions).
|
|
|
|
%can_fetch_item(_Affiliation, _Subscription) -> false.
|
|
|
|
|
|
|
|
is_subscribed(Subscriptions) ->
|
|
|
|
lists:any(fun
|
|
|
|
({subscribed, _SubId}) -> true;
|
|
|
|
(_) -> false
|
|
|
|
end,
|
|
|
|
Subscriptions).
|
|
|
|
|
|
|
|
first_in_list(_Pred, []) ->
|
|
|
|
false;
|
|
|
|
first_in_list(Pred, [H | T]) ->
|
|
|
|
case Pred(H) of
|
|
|
|
true -> {value, H};
|
|
|
|
_ -> first_in_list(Pred, T)
|
|
|
|
end.
|