2009-08-07 10:26:47 +02: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/.
|
2015-04-08 17:12:05 +02:00
|
|
|
%%%
|
2013-03-14 10:33:02 +01:00
|
|
|
%%%
|
2009-08-07 10:26:47 +02:00
|
|
|
%%% 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.
|
2015-04-08 17:12:05 +02:00
|
|
|
%%%
|
2013-03-14 10:33:02 +01:00
|
|
|
%%%
|
2009-08-07 10:26:47 +02:00
|
|
|
%%% The Initial Developer of the Original Code is ProcessOne.
|
2015-01-08 17:34:43 +01:00
|
|
|
%%% Portions created by ProcessOne are Copyright 2006-2015, ProcessOne
|
2009-08-07 10:26:47 +02:00
|
|
|
%%% All Rights Reserved.''
|
2015-01-08 17:34:43 +01:00
|
|
|
%%% This software is copyright 2006-2015, ProcessOne.
|
2009-08-07 10:26:47 +02:00
|
|
|
%%%
|
2015-01-08 17:34:43 +01:00
|
|
|
%%% @copyright 2006-2015 ProcessOne
|
2009-08-07 10:26:47 +02: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>
|
|
|
|
|
|
|
|
-module(node_hometree_odbc).
|
2015-04-08 17:12:05 +02:00
|
|
|
-behaviour(gen_pubsub_node).
|
2009-08-07 10:26:47 +02:00
|
|
|
-author('christophe.romain@process-one.net').
|
|
|
|
|
|
|
|
-include("pubsub.hrl").
|
|
|
|
-include("jlib.hrl").
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
-export([init/3, terminate/2, options/0, features/0,
|
|
|
|
create_node_permission/6, create_node/2, delete_node/1,
|
|
|
|
purge_node/2, subscribe_node/8, unsubscribe_node/4,
|
|
|
|
publish_item/6, delete_item/4, remove_extra_items/3,
|
|
|
|
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,
|
|
|
|
set_state/1, get_items/7, get_items/3,
|
|
|
|
get_items/6, get_items/2, get_item/7,
|
|
|
|
get_item/2, set_item/1, get_item_name/3, node_to_path/1,
|
|
|
|
path_to_node/1,
|
|
|
|
get_entity_subscriptions_for_send_last/2, get_last_items/3]).
|
|
|
|
|
|
|
|
-export([decode_jid/1, encode_jid/1,
|
|
|
|
decode_affiliation/1, decode_subscriptions/1,
|
|
|
|
encode_affiliation/1, encode_subscriptions/1,
|
|
|
|
encode_host/1]).
|
|
|
|
|
|
|
|
init(Host, ServerHost, _Opts) ->
|
|
|
|
pubsub_subscription_odbc:init(),
|
|
|
|
Owner = mod_pubsub:service_jid(Host),
|
|
|
|
mod_pubsub:create_node(Host, ServerHost, <<"/home">>, Owner, <<"hometree">>),
|
|
|
|
mod_pubsub:create_node(Host, ServerHost, <<"/home/", ServerHost/binary>>, Owner, <<"hometree">>),
|
|
|
|
ok.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
terminate(_Host, _ServerHost) ->
|
|
|
|
ok.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
|
|
|
options() ->
|
2015-04-08 17:12:05 +02:00
|
|
|
[{odbc, true}, {rsm, true} | node_hometree:options()].
|
|
|
|
|
2009-08-07 10:26:47 +02:00
|
|
|
features() ->
|
2015-04-08 17:12:05 +02:00
|
|
|
[<<"rsm">> | node_hometree:features()].
|
|
|
|
|
2009-08-07 10:26:47 +02:00
|
|
|
%% @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>
|
2015-04-08 17:12:05 +02:00
|
|
|
create_node_permission(Host, ServerHost, Node, ParentNode, Owner, Access) ->
|
|
|
|
node_hometree:create_node_permission(Host, ServerHost, Node, ParentNode, Owner, Access).
|
|
|
|
|
|
|
|
create_node(Nidx, Owner) ->
|
|
|
|
{_U, _S, _R} = OwnerKey = jlib:jid_tolower(jlib:jid_remove_resource(Owner)),
|
|
|
|
State = #pubsub_state{stateid = {OwnerKey, Nidx}, affiliation = owner},
|
|
|
|
catch ejabberd_odbc:sql_query_t([<<"insert into pubsub_state(nodeid, jid, affiliation, subscriptions) "
|
|
|
|
"values(">>, state_to_raw(Nidx, State), <<");">>]),
|
2009-08-07 10:26:47 +02:00
|
|
|
{result, {default, broadcast}}.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
delete_node(Nodes) ->
|
|
|
|
Reply = lists:map(fun (#pubsub_node{id = Nidx} = Node) ->
|
|
|
|
Subscriptions = case catch
|
|
|
|
ejabberd_odbc:sql_query_t([<<"select jid, subscriptions "
|
|
|
|
"from pubsub_state where nodeid='">>, Nidx, <<"';">>])
|
|
|
|
of
|
|
|
|
{selected, [<<"jid">>, <<"subscriptions">>], RItems} ->
|
|
|
|
[{decode_jid(SJID), decode_subscriptions(Subs)} || [SJID, Subs] <- RItems];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
{Node, Subscriptions}
|
|
|
|
end,
|
|
|
|
Nodes),
|
2009-08-07 10:26:47 +02:00
|
|
|
{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,
|
|
|
|
SendLast, PresenceSubscription, RosterGroup, Options) ->
|
2009-08-07 10:26:47 +02:00
|
|
|
SubKey = jlib:jid_tolower(Subscriber),
|
|
|
|
GenKey = jlib:jid_remove_resource(SubKey),
|
2015-04-08 17:12:05 +02:00
|
|
|
Authorized = jlib:jid_tolower(jlib:jid_remove_resource(Sender)) == GenKey,
|
|
|
|
{Affiliation, Subscriptions} = select_affiliation_subscriptions(Nidx, GenKey, SubKey),
|
|
|
|
Whitelisted = lists:member(Affiliation, [member, publisher, owner]),
|
|
|
|
PendingSubscription = lists:any(fun
|
|
|
|
({pending, _}) -> true;
|
|
|
|
(_) -> false
|
|
|
|
end,
|
|
|
|
Subscriptions),
|
2015-04-21 15:14:39 +02:00
|
|
|
Owner = Affiliation == owner,
|
2013-03-14 10:33:02 +01:00
|
|
|
if not Authorized ->
|
2015-04-08 17:12:05 +02:00
|
|
|
{error,
|
|
|
|
?ERR_EXTENDED((?ERR_BAD_REQUEST), <<"invalid-jid">>)};
|
2015-04-21 17:23:59 +02:00
|
|
|
(Affiliation == outcast) or (Affiliation == publish_only) ->
|
2015-04-08 17:12:05 +02:00
|
|
|
{error, ?ERR_FORBIDDEN};
|
|
|
|
PendingSubscription ->
|
|
|
|
{error,
|
|
|
|
?ERR_EXTENDED((?ERR_NOT_AUTHORIZED), <<"pending-subscription">>)};
|
2015-04-21 15:14:39 +02:00
|
|
|
(AccessModel == presence) and (not PresenceSubscription) and (not Owner) ->
|
2015-04-08 17:12:05 +02:00
|
|
|
{error,
|
|
|
|
?ERR_EXTENDED((?ERR_NOT_AUTHORIZED), <<"presence-subscription-required">>)};
|
2015-04-21 15:14:39 +02:00
|
|
|
(AccessModel == roster) and (not RosterGroup) and (not Owner) ->
|
2015-04-08 17:12:05 +02:00
|
|
|
{error,
|
|
|
|
?ERR_EXTENDED((?ERR_NOT_AUTHORIZED), <<"not-in-roster-group">>)};
|
2015-04-21 15:14:39 +02:00
|
|
|
(AccessModel == whitelist) and (not Whitelisted) and (not Owner) ->
|
2015-04-08 17:12:05 +02:00
|
|
|
{error,
|
|
|
|
?ERR_EXTENDED((?ERR_NOT_ALLOWED), <<"closed-node">>)};
|
|
|
|
%%MustPay ->
|
|
|
|
%% % Payment is required for a subscription
|
|
|
|
%% {error, ?ERR_PAYMENT_REQUIRED};
|
|
|
|
%%ForbiddenAnonymous ->
|
|
|
|
%% % Requesting entity is anonymous
|
|
|
|
%% {error, ?ERR_FORBIDDEN};
|
|
|
|
true ->
|
|
|
|
{result, SubId} = pubsub_subscription_odbc:subscribe_node(Subscriber, Nidx, Options),
|
|
|
|
NewSub = case AccessModel of
|
|
|
|
authorize -> pending;
|
|
|
|
_ -> subscribed
|
|
|
|
end,
|
|
|
|
update_subscription(Nidx, SubKey, [{NewSub, SubId} | Subscriptions]),
|
|
|
|
case {NewSub, SendLast} of
|
|
|
|
{subscribed, never} -> {result, {default, subscribed, SubId}};
|
|
|
|
{subscribed, _} -> {result, {default, subscribed, SubId, send_last}};
|
|
|
|
{_, _} -> {result, {default, pending, SubId}}
|
|
|
|
end
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% @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) ->
|
2009-08-07 10:26:47 +02:00
|
|
|
SubKey = jlib:jid_tolower(Subscriber),
|
|
|
|
GenKey = jlib:jid_remove_resource(SubKey),
|
2015-04-08 17:12:05 +02:00
|
|
|
Authorized = jlib:jid_tolower(jlib:jid_remove_resource(Sender)) == GenKey,
|
|
|
|
{Affiliation, Subscriptions} = select_affiliation_subscriptions(Nidx, SubKey),
|
2009-08-07 10:26:47 +02:00
|
|
|
SubIdExists = case SubId of
|
2015-04-08 17:12:05 +02:00
|
|
|
<<>> -> false;
|
|
|
|
Binary when is_binary(Binary) -> true;
|
|
|
|
_ -> false
|
|
|
|
end,
|
2009-08-07 10:26:47 +02:00
|
|
|
if
|
2015-04-08 17:12:05 +02:00
|
|
|
%% Requesting entity is prohibited from unsubscribing entity
|
|
|
|
not Authorized ->
|
|
|
|
{error, ?ERR_FORBIDDEN};
|
|
|
|
%% Entity did not specify SubId
|
|
|
|
%%SubId == "", ?? ->
|
|
|
|
%% {error, ?ERR_EXTENDED(?ERR_BAD_REQUEST, "subid-required")};
|
|
|
|
%% Invalid subscription identifier
|
|
|
|
%%InvalidSubId ->
|
|
|
|
%% {error, ?ERR_EXTENDED(?ERR_NOT_ACCEPTABLE, "invalid-subid")};
|
|
|
|
%% Requesting entity is not a subscriber
|
|
|
|
Subscriptions == [] ->
|
|
|
|
{error,
|
|
|
|
?ERR_EXTENDED((?ERR_UNEXPECTED_REQUEST_CANCEL), <<"not-subscribed">>)};
|
|
|
|
%% Subid supplied, so use that.
|
|
|
|
SubIdExists ->
|
|
|
|
Sub = first_in_list(fun
|
|
|
|
({_, S}) when S == SubId -> true;
|
|
|
|
(_) -> false
|
|
|
|
end,
|
|
|
|
Subscriptions),
|
|
|
|
case Sub of
|
|
|
|
{value, S} ->
|
|
|
|
delete_subscription(SubKey, Nidx, S, Affiliation, Subscriptions),
|
|
|
|
{result, default};
|
|
|
|
false ->
|
|
|
|
{error,
|
|
|
|
?ERR_EXTENDED((?ERR_UNEXPECTED_REQUEST_CANCEL), <<"not-subscribed">>)}
|
|
|
|
end;
|
|
|
|
%% Asking to remove all subscriptions to the given node
|
|
|
|
SubId == all ->
|
|
|
|
[delete_subscription(SubKey, Nidx, S, Affiliation, Subscriptions)
|
|
|
|
|| S <- Subscriptions],
|
|
|
|
{result, default};
|
|
|
|
%% No subid supplied, but there's only one matching subscription
|
|
|
|
length(Subscriptions) == 1 ->
|
|
|
|
delete_subscription(SubKey, Nidx, hd(Subscriptions), Affiliation, Subscriptions),
|
|
|
|
{result, default};
|
|
|
|
%% No subid and more than one possible subscription match.
|
|
|
|
true ->
|
|
|
|
{error,
|
|
|
|
?ERR_EXTENDED((?ERR_BAD_REQUEST), <<"subid-required">>)}
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
delete_subscription(SubKey, Nidx, {Subscription, SubId}, Affiliation, Subscriptions) ->
|
2009-08-07 10:26:47 +02:00
|
|
|
NewSubs = Subscriptions -- [{Subscription, SubId}],
|
2015-04-08 17:12:05 +02:00
|
|
|
pubsub_subscription_odbc:unsubscribe_node(SubKey, Nidx, SubId),
|
2009-08-07 10:26:47 +02:00
|
|
|
case {Affiliation, NewSubs} of
|
2015-04-08 17:12:05 +02:00
|
|
|
{none, []} -> del_state(Nidx, SubKey);
|
|
|
|
_ -> update_subscription(Nidx, SubKey, NewSubs)
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% @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-04-08 17:12:05 +02:00
|
|
|
publish_item(Nidx, Publisher, PublishModel, MaxItems, ItemId, Payload) ->
|
2009-08-07 10:26:47 +02:00
|
|
|
SubKey = jlib:jid_tolower(Publisher),
|
|
|
|
GenKey = jlib:jid_remove_resource(SubKey),
|
2015-04-08 17:12:05 +02:00
|
|
|
{Affiliation, Subscriptions} = select_affiliation_subscriptions(Nidx, GenKey, SubKey),
|
2009-08-07 10:26:47 +02:00
|
|
|
Subscribed = case PublishModel of
|
2015-04-08 17:12:05 +02:00
|
|
|
subscribers -> is_subscribed(Subscriptions);
|
|
|
|
_ -> undefined
|
|
|
|
end,
|
|
|
|
if not ((PublishModel == open) or
|
|
|
|
(PublishModel == publishers) and
|
2015-04-21 17:23:59 +02:00
|
|
|
((Affiliation == owner)
|
|
|
|
or (Affiliation == publisher)
|
|
|
|
or (Affiliation == publish_only))
|
2015-04-08 17:12:05 +02:00
|
|
|
or (Subscribed == true)) ->
|
|
|
|
{error, ?ERR_FORBIDDEN};
|
|
|
|
true ->
|
|
|
|
if MaxItems > 0 ->
|
|
|
|
PubId = {now(), SubKey},
|
|
|
|
set_item(#pubsub_item{itemid = {ItemId, Nidx},
|
|
|
|
creation = {now(), GenKey},
|
|
|
|
modification = PubId,
|
|
|
|
payload = Payload}),
|
|
|
|
Items = [ItemId | itemids(Nidx, GenKey) -- [ItemId]],
|
|
|
|
{result, {_, OI}} = remove_extra_items(Nidx, MaxItems, Items),
|
|
|
|
{result, {default, broadcast, OI}};
|
|
|
|
true ->
|
|
|
|
{result, {default, broadcast, []}}
|
|
|
|
end
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% @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:
|
2015-04-08 17:12:05 +02:00
|
|
|
%% ```remove_extra_items(Nidx, MaxItems, ItemIds) ->
|
|
|
|
%% node_default:remove_extra_items(Nidx, MaxItems, ItemIds).'''</p>
|
|
|
|
remove_extra_items(_Nidx, unlimited, ItemIds) ->
|
2009-08-07 10:26:47 +02:00
|
|
|
{result, {ItemIds, []}};
|
2015-04-08 17:12:05 +02:00
|
|
|
remove_extra_items(Nidx, MaxItems, ItemIds) ->
|
2009-08-07 10:26:47 +02:00
|
|
|
NewItems = lists:sublist(ItemIds, MaxItems),
|
|
|
|
OldItems = lists:nthtail(length(NewItems), ItemIds),
|
2015-04-08 17:12:05 +02:00
|
|
|
del_items(Nidx, OldItems),
|
2009-08-07 10:26:47 +02:00
|
|
|
{result, {NewItems, OldItems}}.
|
|
|
|
|
|
|
|
%% @doc <p>Triggers item deletion.</p>
|
|
|
|
%% <p>Default plugin: The user performing the deletion must be the node owner
|
2015-04-08 17:12:05 +02:00
|
|
|
%% or a publisher, or PublishModel being open.</p>
|
|
|
|
delete_item(Nidx, Publisher, PublishModel, ItemId) ->
|
2009-08-07 10:26:47 +02:00
|
|
|
SubKey = jlib:jid_tolower(Publisher),
|
|
|
|
GenKey = jlib:jid_remove_resource(SubKey),
|
2015-04-08 17:12:05 +02:00
|
|
|
{result, Affiliation} = get_affiliation(Nidx, GenKey),
|
2013-03-14 10:33:02 +01:00
|
|
|
Allowed = Affiliation == publisher orelse
|
2015-04-08 17:12:05 +02:00
|
|
|
Affiliation == owner orelse
|
|
|
|
PublishModel == open orelse
|
|
|
|
case get_item(Nidx, ItemId) of
|
|
|
|
{result, #pubsub_item{creation = {_, GenKey}}} -> true;
|
|
|
|
_ -> false
|
|
|
|
end,
|
|
|
|
if not Allowed ->
|
|
|
|
{error, ?ERR_FORBIDDEN};
|
|
|
|
true ->
|
|
|
|
case del_item(Nidx, ItemId) of
|
|
|
|
{updated, 1} -> {result, {default, broadcast}};
|
|
|
|
_ -> {error, ?ERR_ITEM_NOT_FOUND}
|
|
|
|
end
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
purge_node(Nidx, Owner) ->
|
2009-08-07 10:26:47 +02:00
|
|
|
SubKey = jlib:jid_tolower(Owner),
|
|
|
|
GenKey = jlib:jid_remove_resource(SubKey),
|
2015-04-08 17:12:05 +02:00
|
|
|
GenState = get_state(Nidx, GenKey),
|
2009-08-07 10:26:47 +02:00
|
|
|
case GenState of
|
2015-04-08 17:12:05 +02:00
|
|
|
#pubsub_state{affiliation = owner} ->
|
|
|
|
{result, States} = get_states(Nidx),
|
|
|
|
lists:foreach(fun
|
|
|
|
(#pubsub_state{items = []}) -> ok;
|
|
|
|
(#pubsub_state{items = Items}) -> del_items(Nidx, Items)
|
|
|
|
end,
|
|
|
|
States),
|
|
|
|
{result, {default, broadcast}};
|
|
|
|
_ ->
|
|
|
|
{error, ?ERR_FORBIDDEN}
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% @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>
|
|
|
|
get_entity_affiliations(Host, Owner) ->
|
|
|
|
SubKey = jlib:jid_tolower(Owner),
|
|
|
|
GenKey = jlib:jid_remove_resource(SubKey),
|
2015-04-08 17:12:05 +02:00
|
|
|
H = encode_host(Host),
|
2009-08-07 10:26:47 +02:00
|
|
|
J = encode_jid(GenKey),
|
2013-03-14 10:33:02 +01:00
|
|
|
Reply = case catch
|
2015-04-08 17:12:05 +02:00
|
|
|
ejabberd_odbc:sql_query_t([<<"select node, type, i.nodeid, affiliation "
|
|
|
|
"from pubsub_state i, pubsub_node n where "
|
|
|
|
"i.nodeid = n.nodeid and jid='">>, J, <<"' and host='">>, H, <<"';">>])
|
|
|
|
of
|
|
|
|
{selected, [<<"node">>, <<"type">>, <<"nodeid">>, <<"affiliation">>], RItems} ->
|
|
|
|
[{nodetree_tree_odbc:raw_to_node(Host, [N, <<"">>, T, I]), decode_affiliation(A)}
|
|
|
|
|| [N, T, I, A] <- RItems];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
2009-08-07 10:26:47 +02:00
|
|
|
{result, Reply}.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_node_affiliations(Nidx) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Reply = case catch
|
2015-04-08 17:12:05 +02:00
|
|
|
ejabberd_odbc:sql_query_t([<<"select jid, affiliation from pubsub_state "
|
|
|
|
"where nodeid='">>, Nidx, <<"';">>])
|
|
|
|
of
|
|
|
|
{selected, [<<"jid">>, <<"affiliation">>], RItems} ->
|
|
|
|
[{decode_jid(J), decode_affiliation(A)} || [J, A] <- RItems];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
2009-08-07 10:26:47 +02:00
|
|
|
{result, Reply}.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_affiliation(Nidx, Owner) ->
|
2009-08-07 10:26:47 +02:00
|
|
|
SubKey = jlib:jid_tolower(Owner),
|
|
|
|
GenKey = jlib:jid_remove_resource(SubKey),
|
|
|
|
J = encode_jid(GenKey),
|
2013-03-14 10:33:02 +01:00
|
|
|
Reply = case catch
|
2015-04-08 17:12:05 +02:00
|
|
|
ejabberd_odbc:sql_query_t([<<"select affiliation from pubsub_state "
|
|
|
|
"where nodeid='">>, Nidx, <<"' and jid='">>, J, <<"';">>])
|
|
|
|
of
|
|
|
|
{selected, [<<"affiliation">>], [[A]]} ->
|
|
|
|
decode_affiliation(A);
|
|
|
|
_ ->
|
|
|
|
none
|
|
|
|
end,
|
2009-08-07 10:26:47 +02:00
|
|
|
{result, Reply}.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
set_affiliation(Nidx, Owner, Affiliation) ->
|
2009-08-07 10:26:47 +02:00
|
|
|
SubKey = jlib:jid_tolower(Owner),
|
|
|
|
GenKey = jlib:jid_remove_resource(SubKey),
|
2015-04-08 17:12:05 +02:00
|
|
|
{_, Subscriptions} = select_affiliation_subscriptions(Nidx, GenKey),
|
2009-08-07 10:26:47 +02:00
|
|
|
case {Affiliation, Subscriptions} of
|
2015-04-08 17:12:05 +02:00
|
|
|
{none, []} -> del_state(Nidx, GenKey);
|
|
|
|
_ -> update_affiliation(Nidx, GenKey, Affiliation)
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% @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>
|
|
|
|
get_entity_subscriptions(Host, Owner) ->
|
|
|
|
SubKey = jlib:jid_tolower(Owner),
|
|
|
|
GenKey = jlib:jid_remove_resource(SubKey),
|
2015-04-08 17:12:05 +02:00
|
|
|
H = encode_host(Host),
|
2009-08-07 10:26:47 +02:00
|
|
|
SJ = encode_jid(SubKey),
|
|
|
|
GJ = encode_jid(GenKey),
|
|
|
|
Query = case SubKey of
|
2015-04-08 17:12:05 +02:00
|
|
|
GenKey ->
|
|
|
|
[<<"select node, type, i.nodeid, jid, subscriptions "
|
|
|
|
"from pubsub_state i, pubsub_node n "
|
|
|
|
"where i.nodeid = n.nodeid and jid like '">>, GJ, <<"%' and host='">>, H, <<"';">>];
|
|
|
|
_ ->
|
|
|
|
[<<"select node, type, i.nodeid, jid, subscriptions "
|
|
|
|
"from pubsub_state i, pubsub_node n "
|
|
|
|
"where i.nodeid = n.nodeid and jid in ('">>, SJ, <<"', '">>, GJ, <<"') and host='">>, H, <<"';">>]
|
|
|
|
end,
|
2009-08-07 10:26:47 +02:00
|
|
|
Reply = case catch ejabberd_odbc:sql_query_t(Query) of
|
2015-04-08 17:12:05 +02:00
|
|
|
{selected,
|
|
|
|
[<<"node">>, <<"type">>, <<"nodeid">>, <<"jid">>, <<"subscriptions">>], RItems} ->
|
|
|
|
lists:foldl(fun ([N, T, I, J, S], Acc) ->
|
|
|
|
Node = nodetree_tree_odbc:raw_to_node(Host, [N, <<"">>, T, I]),
|
|
|
|
Jid = decode_jid(J),
|
|
|
|
case decode_subscriptions(S) of
|
|
|
|
[] ->
|
|
|
|
[{Node, none, Jid} | Acc];
|
|
|
|
Subs ->
|
|
|
|
lists:foldl(fun ({Sub, SubId}, Acc2) ->
|
|
|
|
[{Node, Sub, SubId, Jid} | Acc2]
|
|
|
|
end,
|
|
|
|
Acc, Subs)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
[], RItems);
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
2009-08-07 10:26:47 +02:00
|
|
|
{result, Reply}.
|
|
|
|
|
|
|
|
%% do the same as get_entity_subscriptions but filter result only to
|
|
|
|
%% nodes having send_last_published_item=on_sub_and_presence
|
|
|
|
%% as this call avoid seeking node, it must return node and type as well
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec(get_entity_subscriptions_for_send_last/2 ::
|
2015-04-08 17:12:05 +02:00
|
|
|
(
|
|
|
|
Host :: mod_pubsub:hostPubsub(),
|
|
|
|
Owner :: jid())
|
2013-03-14 10:33:02 +01:00
|
|
|
-> {result,
|
2015-04-08 17:12:05 +02:00
|
|
|
[{mod_pubsub:pubsubNode(),
|
|
|
|
mod_pubsub:subscription(),
|
|
|
|
mod_pubsub:subId(),
|
|
|
|
ljid()}]
|
|
|
|
}
|
|
|
|
).
|
2009-08-07 10:26:47 +02:00
|
|
|
get_entity_subscriptions_for_send_last(Host, Owner) ->
|
|
|
|
SubKey = jlib:jid_tolower(Owner),
|
|
|
|
GenKey = jlib:jid_remove_resource(SubKey),
|
2015-04-08 17:12:05 +02:00
|
|
|
H = encode_host(Host),
|
2009-08-07 10:26:47 +02:00
|
|
|
SJ = encode_jid(SubKey),
|
|
|
|
GJ = encode_jid(GenKey),
|
|
|
|
Query = case SubKey of
|
2015-04-08 17:12:05 +02:00
|
|
|
GenKey ->
|
|
|
|
[<<"select node, type, i.nodeid, jid, subscriptions "
|
|
|
|
"from pubsub_state i, pubsub_node n, pubsub_node_option o "
|
|
|
|
"where i.nodeid = n.nodeid and n.nodeid = o.nodeid and name='send_last_published_item' "
|
|
|
|
"and val='on_sub_and_presence' and jid like '">>, GJ, <<"%' and host='">>, H, <<"';">>];
|
|
|
|
_ ->
|
|
|
|
[<<"select node, type, i.nodeid, jid, subscriptions "
|
|
|
|
"from pubsub_state i, pubsub_node n, pubsub_node_option o "
|
|
|
|
"where i.nodeid = n.nodeid and n.nodeid = o.nodeid and name='send_last_published_item' "
|
|
|
|
"and val='on_sub_and_presence' and jid in ('">>, SJ, <<"', '">>, GJ, <<"') and host='">>, H, <<"';">>]
|
|
|
|
end,
|
2009-08-07 10:26:47 +02:00
|
|
|
Reply = case catch ejabberd_odbc:sql_query_t(Query) of
|
2015-04-08 17:12:05 +02:00
|
|
|
{selected,
|
|
|
|
[<<"node">>, <<"type">>, <<"nodeid">>, <<"jid">>, <<"subscriptions">>], RItems} ->
|
|
|
|
lists:foldl(fun ([N, T, I, J, S], Acc) ->
|
|
|
|
Node = nodetree_tree_odbc:raw_to_node(Host, [N, <<"">>, T, I]),
|
|
|
|
Jid = decode_jid(J),
|
|
|
|
case decode_subscriptions(S) of
|
|
|
|
[] ->
|
|
|
|
[{Node, none, Jid} | Acc];
|
|
|
|
Subs ->
|
|
|
|
lists:foldl(fun ({Sub, SubId}, Acc2) ->
|
|
|
|
[{Node, Sub, SubId, Jid}| Acc2]
|
|
|
|
end,
|
|
|
|
Acc, Subs)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
[], RItems);
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
2009-08-07 10:26:47 +02:00
|
|
|
{result, Reply}.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_node_subscriptions(Nidx) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Reply = case catch
|
2015-04-08 17:12:05 +02:00
|
|
|
ejabberd_odbc:sql_query_t([<<"select jid, subscriptions from pubsub_state "
|
|
|
|
"where nodeid='">>, Nidx, <<"';">>])
|
|
|
|
of
|
|
|
|
{selected, [<<"jid">>, <<"subscriptions">>], RItems} ->
|
|
|
|
lists:foldl(fun ([J, S], Acc) ->
|
|
|
|
Jid = decode_jid(J),
|
|
|
|
case decode_subscriptions(S) of
|
|
|
|
[] ->
|
|
|
|
[{Jid, none} | Acc];
|
|
|
|
Subs ->
|
|
|
|
lists:foldl(fun ({Sub, SubId}, Acc2) ->
|
|
|
|
[{Jid, Sub, SubId} | Acc2]
|
|
|
|
end,
|
|
|
|
Acc, Subs)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
[], RItems);
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
2009-08-07 10:26:47 +02:00
|
|
|
{result, Reply}.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_subscriptions(Nidx, Owner) ->
|
2009-08-07 10:26:47 +02:00
|
|
|
SubKey = jlib:jid_tolower(Owner),
|
|
|
|
J = encode_jid(SubKey),
|
2013-03-14 10:33:02 +01:00
|
|
|
Reply = case catch
|
2015-04-08 17:12:05 +02:00
|
|
|
ejabberd_odbc:sql_query_t([<<"select subscriptions from pubsub_state where "
|
|
|
|
"nodeid='">>, Nidx, <<"' and jid='">>, J, <<"';">>])
|
|
|
|
of
|
|
|
|
{selected, [<<"subscriptions">>], [[S]]} ->
|
|
|
|
decode_subscriptions(S);
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
2009-08-07 10:26:47 +02:00
|
|
|
{result, Reply}.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
set_subscriptions(Nidx, Owner, Subscription, SubId) ->
|
2009-08-07 10:26:47 +02:00
|
|
|
SubKey = jlib:jid_tolower(Owner),
|
2015-04-08 17:12:05 +02:00
|
|
|
SubState = get_state_without_itemids(Nidx, SubKey),
|
2009-08-07 10:26:47 +02:00
|
|
|
case {SubId, SubState#pubsub_state.subscriptions} of
|
2015-04-08 17:12:05 +02:00
|
|
|
{_, []} ->
|
|
|
|
case Subscription of
|
|
|
|
none ->
|
|
|
|
{error,
|
|
|
|
?ERR_EXTENDED((?ERR_BAD_REQUEST), <<"not-subscribed">>)};
|
|
|
|
_ ->
|
|
|
|
new_subscription(Nidx, Owner, Subscription, SubState)
|
|
|
|
end;
|
|
|
|
{<<>>, [{_, SID}]} ->
|
|
|
|
case Subscription of
|
|
|
|
none -> unsub_with_subid(Nidx, SID, SubState);
|
|
|
|
_ -> replace_subscription({Subscription, SID}, SubState)
|
|
|
|
end;
|
|
|
|
{<<>>, [_ | _]} ->
|
|
|
|
{error,
|
|
|
|
?ERR_EXTENDED((?ERR_BAD_REQUEST), <<"subid-required">>)};
|
|
|
|
_ ->
|
|
|
|
case Subscription of
|
|
|
|
none -> unsub_with_subid(Nidx, SubId, SubState);
|
|
|
|
_ -> replace_subscription({Subscription, SubId}, SubState)
|
|
|
|
end
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
replace_subscription(NewSub, SubState) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
NewSubs = replace_subscription(NewSub, SubState#pubsub_state.subscriptions, []),
|
2009-08-07 10:26:47 +02:00
|
|
|
set_state(SubState#pubsub_state{subscriptions = NewSubs}).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
replace_subscription(_, [], Acc) -> Acc;
|
2015-04-08 17:12:05 +02:00
|
|
|
replace_subscription({Sub, SubId}, [{_, SubId} | T], Acc) ->
|
|
|
|
replace_subscription({Sub, SubId}, T, [{Sub, SubId} | Acc]).
|
|
|
|
|
|
|
|
new_subscription(Nidx, Owner, Subscription, SubState) ->
|
|
|
|
{result, SubId} = pubsub_subscription_odbc:subscribe_node(Owner, Nidx, []),
|
|
|
|
Subscriptions = [{Subscription, SubId} | SubState#pubsub_state.subscriptions],
|
|
|
|
set_state(SubState#pubsub_state{subscriptions = Subscriptions}),
|
|
|
|
{Subscription, SubId}.
|
|
|
|
|
|
|
|
unsub_with_subid(Nidx, SubId, SubState) ->
|
|
|
|
pubsub_subscription_odbc:unsubscribe_node(SubState#pubsub_state.stateid, Nidx, SubId),
|
|
|
|
NewSubs = [{S, Sid}
|
|
|
|
|| {S, Sid} <- SubState#pubsub_state.subscriptions,
|
|
|
|
SubId =/= Sid],
|
2009-08-07 10:26:47 +02:00
|
|
|
case {NewSubs, SubState#pubsub_state.affiliation} of
|
2015-04-08 17:12:05 +02:00
|
|
|
{[], none} -> del_state(Nidx, element(1, SubState#pubsub_state.stateid));
|
|
|
|
_ -> set_state(SubState#pubsub_state{subscriptions = NewSubs})
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% @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)),
|
2015-04-08 17:12:05 +02:00
|
|
|
States = mnesia:match_object(#pubsub_state{stateid = {GenKey, '_'},
|
|
|
|
affiliation = owner, _ = '_'}),
|
|
|
|
Nidxxs = [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, Nidxxs) of
|
|
|
|
true ->
|
|
|
|
case get_nodes_helper(NodeTree, S) of
|
|
|
|
{value, Node} -> [Node | Acc];
|
|
|
|
false -> Acc
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
Acc
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
[], pubsub_state),
|
2009-08-07 10:26:47 +02:00
|
|
|
{result, Reply}.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_nodes_helper(NodeTree, #pubsub_state{stateid = {_, N}, subscriptions = Subs}) ->
|
|
|
|
HasPending = fun
|
|
|
|
({pending, _}) -> true;
|
|
|
|
(pending) -> true;
|
|
|
|
(_) -> false
|
|
|
|
end,
|
2009-08-07 10:26:47 +02:00
|
|
|
case lists:any(HasPending, Subs) of
|
2015-04-08 17:12:05 +02:00
|
|
|
true ->
|
|
|
|
case NodeTree:get_node(N) of
|
|
|
|
#pubsub_node{nodeid = {_, Node}} -> {value, Node};
|
|
|
|
_ -> false
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
false
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% @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:
|
2015-04-08 17:12:05 +02:00
|
|
|
%% ```get_states(Nidx) ->
|
|
|
|
%% node_default:get_states(Nidx).'''</p>
|
|
|
|
get_states(Nidx) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case catch
|
2015-04-08 17:12:05 +02:00
|
|
|
ejabberd_odbc:sql_query_t([<<"select jid, affiliation, subscriptions "
|
|
|
|
"from pubsub_state where nodeid='">>, Nidx, <<"';">>])
|
|
|
|
of
|
|
|
|
{selected,
|
|
|
|
[<<"jid">>, <<"affiliation">>, <<"subscriptions">>], RItems} ->
|
|
|
|
{result,
|
|
|
|
lists:map(fun ([SJID, Aff, Subs]) ->
|
|
|
|
#pubsub_state{stateid = {decode_jid(SJID), Nidx},
|
|
|
|
items = itemids(Nidx, SJID),
|
|
|
|
affiliation = decode_affiliation(Aff),
|
|
|
|
subscriptions = decode_subscriptions(Subs)}
|
|
|
|
end,
|
|
|
|
RItems)};
|
|
|
|
_ ->
|
|
|
|
{result, []}
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% @doc <p>Returns a state (one state list), given its reference.</p>
|
2015-04-08 17:12:05 +02:00
|
|
|
get_state(Nidx, JID) ->
|
|
|
|
State = get_state_without_itemids(Nidx, JID),
|
2009-08-07 10:26:47 +02:00
|
|
|
{SJID, _} = State#pubsub_state.stateid,
|
2015-04-08 17:12:05 +02:00
|
|
|
State#pubsub_state{items = itemids(Nidx, SJID)}.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-spec(get_state_without_itemids/2 ::
|
2015-04-08 17:12:05 +02:00
|
|
|
(Nidx :: mod_pubsub:nodeIdx(),
|
|
|
|
Key :: ljid()) ->
|
|
|
|
mod_pubsub:pubsubState()
|
|
|
|
).
|
|
|
|
get_state_without_itemids(Nidx, JID) ->
|
2009-08-07 10:26:47 +02:00
|
|
|
J = encode_jid(JID),
|
2013-03-14 10:33:02 +01:00
|
|
|
case catch
|
2015-04-08 17:12:05 +02:00
|
|
|
ejabberd_odbc:sql_query_t([<<"select jid, affiliation, subscriptions "
|
|
|
|
"from pubsub_state where jid='">>, J, <<"' and nodeid='">>, Nidx, <<"';">>])
|
|
|
|
of
|
|
|
|
{selected,
|
|
|
|
[<<"jid">>, <<"affiliation">>, <<"subscriptions">>], [[SJID, Aff, Subs]]} ->
|
|
|
|
#pubsub_state{stateid = {decode_jid(SJID), Nidx},
|
|
|
|
affiliation = decode_affiliation(Aff),
|
|
|
|
subscriptions = decode_subscriptions(Subs)};
|
|
|
|
_ ->
|
|
|
|
#pubsub_state{stateid = {JID, Nidx}}
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% @doc <p>Write a state into database.</p>
|
|
|
|
set_state(State) ->
|
2015-04-08 17:12:05 +02:00
|
|
|
{_, Nidx} = State#pubsub_state.stateid,
|
|
|
|
set_state(Nidx, State).
|
|
|
|
|
|
|
|
set_state(Nidx, State) ->
|
2009-08-07 10:26:47 +02:00
|
|
|
{JID, _} = State#pubsub_state.stateid,
|
|
|
|
J = encode_jid(JID),
|
|
|
|
S = encode_subscriptions(State#pubsub_state.subscriptions),
|
|
|
|
A = encode_affiliation(State#pubsub_state.affiliation),
|
2013-03-14 10:33:02 +01:00
|
|
|
case catch
|
2015-04-08 17:12:05 +02:00
|
|
|
ejabberd_odbc:sql_query_t([<<"update pubsub_state set subscriptions='">>, S, <<"', affiliation='">>, A,
|
|
|
|
<<"' where nodeid='">>, Nidx, <<"' and jid='">>, J, <<"';">>])
|
|
|
|
of
|
|
|
|
{updated, 1} ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
catch
|
|
|
|
ejabberd_odbc:sql_query_t([<<"insert into pubsub_state(nodeid, jid, affiliation, subscriptions) "
|
|
|
|
"values('">>,
|
|
|
|
Nidx, <<"', '">>, J, <<"', '">>, A, <<"', '">>, S, <<"');">>])
|
2009-08-07 10:26:47 +02:00
|
|
|
end,
|
2015-04-08 17:12:05 +02:00
|
|
|
ok.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
|
|
|
%% @doc <p>Delete a state from database.</p>
|
2015-04-08 17:12:05 +02:00
|
|
|
del_state(Nidx, JID) ->
|
2009-08-07 10:26:47 +02:00
|
|
|
J = encode_jid(JID),
|
2015-04-08 17:12:05 +02:00
|
|
|
catch ejabberd_odbc:sql_query_t([<<"delete from pubsub_state where jid='">>,
|
|
|
|
J, <<"' and nodeid='">>, Nidx, <<"';">>]),
|
2009-08-07 10:26:47 +02:00
|
|
|
ok.
|
|
|
|
|
|
|
|
%% @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:
|
2015-04-08 17:12:05 +02:00
|
|
|
%% ```get_items(Nidx, From) ->
|
|
|
|
%% node_default:get_items(Nidx, From).'''</p>
|
|
|
|
get_items(Nidx, _From) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case catch
|
2015-04-08 17:12:05 +02:00
|
|
|
ejabberd_odbc:sql_query_t([<<"select itemid, publisher, creation, modification, payload "
|
|
|
|
"from pubsub_item where nodeid='">>, Nidx,
|
|
|
|
<<"' order by modification desc;">>])
|
|
|
|
of
|
|
|
|
{selected,
|
|
|
|
[<<"itemid">>, <<"publisher">>, <<"creation">>, <<"modification">>, <<"payload">>], RItems} ->
|
|
|
|
{result, [raw_to_item(Nidx, RItem) || RItem <- RItems]};
|
|
|
|
_ ->
|
|
|
|
{result, []}
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_items(Nidx, From, none) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
MaxItems = case catch
|
2015-04-08 17:12:05 +02:00
|
|
|
ejabberd_odbc:sql_query_t([<<"select val from pubsub_node_option "
|
|
|
|
"where nodeid='">>, Nidx, <<"' and name='max_items';">>])
|
|
|
|
of
|
|
|
|
{selected, [<<"val">>], [[Value]]} ->
|
|
|
|
Tokens = element(2, erl_scan:string(binary_to_list(<<Value/binary, ".">>))),
|
|
|
|
element(2, erl_parse:parse_term(Tokens));
|
|
|
|
_ ->
|
|
|
|
?MAXITEMS
|
|
|
|
end,
|
|
|
|
get_items(Nidx, From, #rsm_in{max = MaxItems});
|
|
|
|
get_items(Nidx, _From,
|
|
|
|
#rsm_in{max = M, direction = Direction, id = I, index = IncIndex}) ->
|
|
|
|
Max = ejabberd_odbc:escape(jlib:i2l(M)),
|
2013-03-14 10:33:02 +01:00
|
|
|
{Way, Order} = case Direction of
|
2015-04-08 17:12:05 +02:00
|
|
|
% aft -> {<<"<">>, <<"desc">>};
|
|
|
|
% before when I == <<>> -> {<<"is not">>, <<"asc">>};
|
|
|
|
% before -> {<<">">>, <<"asc">>};
|
|
|
|
% _ when IncIndex =/= undefined ->
|
|
|
|
% {<<"<">>, <<"desc">>}; % using index
|
|
|
|
_ ->
|
|
|
|
{<<"is not">>, <<"desc">>}% Can be better
|
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
[AttrName, Id] = case I of
|
2015-04-08 17:12:05 +02:00
|
|
|
undefined when IncIndex =/= undefined ->
|
|
|
|
case catch
|
|
|
|
ejabberd_odbc:sql_query_t([<<"select modification from pubsub_item pi "
|
|
|
|
"where exists ( select count(*) as count1 "
|
|
|
|
"from pubsub_item where nodeid='">>, Nidx,
|
|
|
|
<<"' and modification > pi.modification having count1 = ">>,
|
|
|
|
ejabberd_odbc:escape(jlib:i2l(IncIndex)), <<" );">>])
|
|
|
|
of
|
|
|
|
{selected, [_], [[O]]} ->
|
|
|
|
[<<"modification">>, <<"'", O/binary, "'">>];
|
|
|
|
_ ->
|
|
|
|
[<<"modification">>, <<"null">>]
|
|
|
|
end;
|
|
|
|
undefined ->
|
|
|
|
[<<"modification">>, <<"null">>];
|
|
|
|
<<>> ->
|
|
|
|
[<<"modification">>, <<"null">>];
|
|
|
|
I ->
|
|
|
|
[A, B] = str:tokens(ejabberd_odbc:escape(jlib:i2l(I)), <<"@">>),
|
|
|
|
[A, <<"'", B/binary, "'">>]
|
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
Count = case catch
|
2015-04-08 17:12:05 +02:00
|
|
|
ejabberd_odbc:sql_query_t([<<"select count(*) from pubsub_item where nodeid='">>, Nidx, <<"';">>])
|
|
|
|
of
|
|
|
|
{selected, [_], [[C]]} -> C;
|
|
|
|
_ -> <<"0">>
|
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
case catch
|
2015-04-08 17:12:05 +02:00
|
|
|
ejabberd_odbc:sql_query_t([<<"select itemid, publisher, creation, modification, payload "
|
|
|
|
"from pubsub_item where nodeid='">>, Nidx,
|
|
|
|
<<"' and ">>, AttrName, <<" ">>, Way, <<" ">>, Id, <<" order by ">>,
|
|
|
|
AttrName, <<" ">>, Order, <<" limit ">>, jlib:i2l(Max), <<" ;">>])
|
|
|
|
of
|
|
|
|
{selected,
|
|
|
|
[<<"itemid">>, <<"publisher">>, <<"creation">>, <<"modification">>, <<"payload">>], RItems} ->
|
|
|
|
case RItems of
|
|
|
|
[[_, _, _, F, _]|_] ->
|
|
|
|
Index = case catch
|
|
|
|
ejabberd_odbc:sql_query_t([<<"select count(*) from pubsub_item "
|
|
|
|
"where nodeid='">>, Nidx, <<"' and ">>,
|
|
|
|
AttrName, <<" > '">>, F, <<"';">>])
|
|
|
|
of
|
|
|
|
%{selected, [_], [{C}, {In}]} -> [string:strip(C, both, $"), string:strip(In, both, $")];
|
|
|
|
{selected, [_], [[In]]} -> In;
|
|
|
|
_ -> <<"0">>
|
|
|
|
end,
|
|
|
|
[_, _, _, L, _] = lists:last(RItems),
|
|
|
|
RsmOut = #rsm_out{count = Count, index = Index,
|
|
|
|
first = <<"modification@", F/binary>>,
|
|
|
|
last = <<"modification@", (jlib:i2l(L))/binary>>},
|
|
|
|
{result, {[raw_to_item(Nidx, RItem) || RItem <- RItems], RsmOut}};
|
|
|
|
[] ->
|
|
|
|
{result, {[], #rsm_out{count = Count}}}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
{result, {[], none}}
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_items(Nidx, JID, AccessModel, PresenceSubscription, RosterGroup, SubId) ->
|
|
|
|
get_items(Nidx, JID, AccessModel, PresenceSubscription, RosterGroup, SubId, none).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_items(Nidx, JID, AccessModel, PresenceSubscription, RosterGroup, _SubId, RSM) ->
|
2009-08-07 10:26:47 +02:00
|
|
|
SubKey = jlib:jid_tolower(JID),
|
|
|
|
GenKey = jlib:jid_remove_resource(SubKey),
|
2015-04-08 17:12:05 +02:00
|
|
|
{Affiliation, Subscriptions} = select_affiliation_subscriptions(Nidx, GenKey, SubKey),
|
|
|
|
Whitelisted = can_fetch_item(Affiliation, Subscriptions),
|
2013-03-14 10:33:02 +01:00
|
|
|
if %%SubId == "", ?? ->
|
2015-04-08 17:12:05 +02:00
|
|
|
%% Entity has multiple subscriptions to the node but does not specify a subscription ID
|
|
|
|
%{error, ?ERR_EXTENDED(?ERR_BAD_REQUEST, "subid-required")};
|
|
|
|
%%InvalidSubId ->
|
|
|
|
%% Entity is subscribed but specifies an invalid subscription ID
|
|
|
|
%{error, ?ERR_EXTENDED(?ERR_NOT_ACCEPTABLE, "invalid-subid")};
|
2015-04-21 17:23:59 +02:00
|
|
|
(Affiliation == outcast) or (Affiliation == publish_only) ->
|
2015-04-08 17:12:05 +02:00
|
|
|
{error, ?ERR_FORBIDDEN};
|
|
|
|
(AccessModel == presence) and not PresenceSubscription ->
|
|
|
|
{error,
|
|
|
|
?ERR_EXTENDED((?ERR_NOT_AUTHORIZED), <<"presence-subscription-required">>)};
|
|
|
|
(AccessModel == roster) and not RosterGroup ->
|
|
|
|
{error,
|
|
|
|
?ERR_EXTENDED((?ERR_NOT_AUTHORIZED), <<"not-in-roster-group">>)};
|
|
|
|
(AccessModel == whitelist) and not Whitelisted ->
|
|
|
|
{error,
|
|
|
|
?ERR_EXTENDED((?ERR_NOT_ALLOWED), <<"closed-node">>)};
|
|
|
|
(AccessModel == authorize) and not Whitelisted ->
|
|
|
|
{error, ?ERR_FORBIDDEN};
|
|
|
|
%%MustPay ->
|
|
|
|
%% % Payment is required for a subscription
|
|
|
|
%% {error, ?ERR_PAYMENT_REQUIRED};
|
|
|
|
true ->
|
|
|
|
get_items(Nidx, JID, RSM)
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_last_items(Nidx, _From, Count) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case catch
|
2015-04-08 17:12:05 +02:00
|
|
|
ejabberd_odbc:sql_query_t([<<"select itemid, publisher, creation, modification, payload "
|
|
|
|
"from pubsub_item where nodeid='">>, Nidx,
|
|
|
|
<<"' order by modification desc limit ">>, jlib:i2l(Count), <<";">>])
|
|
|
|
of
|
|
|
|
{selected,
|
|
|
|
[<<"itemid">>, <<"publisher">>, <<"creation">>, <<"modification">>, <<"payload">>], RItems} ->
|
|
|
|
{result, [raw_to_item(Nidx, RItem) || RItem <- RItems]};
|
|
|
|
_ ->
|
|
|
|
{result, []}
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% @doc <p>Returns an item (one item list), given its reference.</p>
|
2015-04-08 17:12:05 +02:00
|
|
|
get_item(Nidx, ItemId) ->
|
|
|
|
I = ejabberd_odbc:escape(ItemId),
|
2013-03-14 10:33:02 +01:00
|
|
|
case catch
|
2015-04-08 17:12:05 +02:00
|
|
|
ejabberd_odbc:sql_query_t([<<"select itemid, publisher, creation, "
|
|
|
|
"modification, payload from pubsub_item "
|
|
|
|
"where nodeid='">>, Nidx, <<"' and itemid='">>, I, <<"';">>])
|
|
|
|
of
|
|
|
|
{selected,
|
|
|
|
[<<"itemid">>, <<"publisher">>, <<"creation">>, <<"modification">>, <<"payload">>], [RItem]} ->
|
|
|
|
{result, raw_to_item(Nidx, RItem)};
|
|
|
|
_ ->
|
|
|
|
{error, ?ERR_ITEM_NOT_FOUND}
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_item(Nidx, ItemId, JID, AccessModel, PresenceSubscription, RosterGroup, _SubId) ->
|
2009-08-07 10:26:47 +02:00
|
|
|
SubKey = jlib:jid_tolower(JID),
|
|
|
|
GenKey = jlib:jid_remove_resource(SubKey),
|
2015-04-08 17:12:05 +02:00
|
|
|
{Affiliation, Subscriptions} = select_affiliation_subscriptions(Nidx, GenKey, SubKey),
|
|
|
|
Whitelisted = can_fetch_item(Affiliation, Subscriptions),
|
2013-03-14 10:33:02 +01:00
|
|
|
if %%SubId == "", ?? ->
|
2015-04-08 17:12:05 +02:00
|
|
|
%% Entity has multiple subscriptions to the node but does not specify a subscription ID
|
|
|
|
%{error, ?ERR_EXTENDED(?ERR_BAD_REQUEST, "subid-required")};
|
|
|
|
%%InvalidSubId ->
|
|
|
|
%% Entity is subscribed but specifies an invalid subscription ID
|
|
|
|
%{error, ?ERR_EXTENDED(?ERR_NOT_ACCEPTABLE, "invalid-subid")};
|
2015-04-21 17:23:59 +02:00
|
|
|
(Affiliation == outcast) or (Affiliation == publish_only) ->
|
2015-04-08 17:12:05 +02:00
|
|
|
{error, ?ERR_FORBIDDEN};
|
|
|
|
(AccessModel == presence) and not PresenceSubscription ->
|
|
|
|
{error,
|
|
|
|
?ERR_EXTENDED((?ERR_NOT_AUTHORIZED), <<"presence-subscription-required">>)};
|
|
|
|
(AccessModel == roster) and not RosterGroup ->
|
|
|
|
{error,
|
|
|
|
?ERR_EXTENDED((?ERR_NOT_AUTHORIZED), <<"not-in-roster-group">>)};
|
|
|
|
(AccessModel == whitelist) and not Whitelisted ->
|
|
|
|
{error,
|
|
|
|
?ERR_EXTENDED((?ERR_NOT_ALLOWED), <<"closed-node">>)};
|
|
|
|
(AccessModel == authorize) and not Whitelisted ->
|
|
|
|
{error, ?ERR_FORBIDDEN};
|
|
|
|
%%MustPay ->
|
|
|
|
%% % Payment is required for a subscription
|
|
|
|
%% {error, ?ERR_PAYMENT_REQUIRED};
|
|
|
|
true ->
|
|
|
|
get_item(Nidx, ItemId)
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% @doc <p>Write an item into database.</p>
|
|
|
|
set_item(Item) ->
|
2015-04-08 17:12:05 +02:00
|
|
|
{ItemId, Nidx} = Item#pubsub_item.itemid,
|
|
|
|
I = ejabberd_odbc:escape(ItemId),
|
2009-08-07 10:26:47 +02:00
|
|
|
{C, _} = Item#pubsub_item.creation,
|
|
|
|
{M, JID} = Item#pubsub_item.modification,
|
|
|
|
P = encode_jid(JID),
|
|
|
|
Payload = Item#pubsub_item.payload,
|
2015-04-08 17:12:05 +02:00
|
|
|
XML = ejabberd_odbc:escape(str:join([xml:element_to_binary(X) || X<-Payload], <<>>)),
|
2013-03-14 10:33:02 +01:00
|
|
|
S = fun ({T1, T2, T3}) ->
|
2015-04-08 17:12:05 +02:00
|
|
|
str:join([jlib:i2l(T1, 6), jlib:i2l(T2, 6), jlib:i2l(T3, 6)], <<":">>)
|
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
case catch
|
2015-04-08 17:12:05 +02:00
|
|
|
ejabberd_odbc:sql_query_t([<<"update pubsub_item set publisher='">>, P,
|
|
|
|
<<"', modification='">>, S(M),
|
|
|
|
<<"', payload='">>, XML,
|
|
|
|
<<"' where nodeid='">>, Nidx, <<"' and itemid='">>, I, <<"';">>])
|
|
|
|
of
|
|
|
|
{updated, 1} ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
catch
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_odbc:sql_query_t([<<"insert into pubsub_item (nodeid, itemid, "
|
2015-04-08 17:12:05 +02:00
|
|
|
"publisher, creation, modification, payload) "
|
|
|
|
"values('">>, Nidx, <<"', '">>, I, <<"', '">>, P,
|
|
|
|
<<"', '">>, S(C), <<"', '">>, S(M),
|
|
|
|
<<"', '">>, XML, <<"');">>])
|
2009-08-07 10:26:47 +02:00
|
|
|
end,
|
2015-04-08 17:12:05 +02:00
|
|
|
ok.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
|
|
|
%% @doc <p>Delete an item from database.</p>
|
2015-04-08 17:12:05 +02:00
|
|
|
del_item(Nidx, ItemId) ->
|
|
|
|
I = ejabberd_odbc:escape(ItemId),
|
|
|
|
catch ejabberd_odbc:sql_query_t([<<"delete from pubsub_item where itemid='">>,
|
|
|
|
I, <<"' and nodeid='">>, Nidx, <<"';">>]).
|
|
|
|
|
|
|
|
del_items(_, []) ->
|
|
|
|
ok;
|
|
|
|
del_items(Nidx, [ItemId]) ->
|
|
|
|
del_item(Nidx, ItemId);
|
|
|
|
del_items(Nidx, ItemIds) ->
|
|
|
|
I = str:join([[<<"'">>, ejabberd_odbc:escape(X), <<"'">>] || X <- ItemIds], <<",">>),
|
2013-03-14 10:33:02 +01:00
|
|
|
catch
|
2015-04-08 17:12:05 +02:00
|
|
|
ejabberd_odbc:sql_query_t([<<"delete from pubsub_item where itemid in (">>,
|
|
|
|
I, <<") and nodeid='">>, Nidx, <<"';">>]).
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_item_name(_Host, _Node, Id) ->
|
|
|
|
Id.
|
2009-10-20 17:03:07 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
node_to_path(Node) ->
|
|
|
|
node_hometree:node_to_path(Node).
|
2009-10-20 17:03:07 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
path_to_node(Path) ->
|
2015-04-08 17:12:05 +02:00
|
|
|
node_hometree:path_to_node(Path).
|
2013-06-14 00:04:25 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
can_fetch_item(owner, _) -> true;
|
|
|
|
can_fetch_item(member, _) -> true;
|
|
|
|
can_fetch_item(publisher, _) -> true;
|
2015-04-21 17:23:59 +02:00
|
|
|
can_fetch_item(publish_only, _) -> false;
|
2013-03-14 10:33:02 +01:00
|
|
|
can_fetch_item(outcast, _) -> false;
|
2013-06-24 16:31:38 +02:00
|
|
|
can_fetch_item(none, Subscriptions) -> is_subscribed(Subscriptions).
|
2015-04-08 17:12:05 +02:00
|
|
|
%can_fetch_item(_Affiliation, _Subscription) -> false.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
|
|
|
is_subscribed(Subscriptions) ->
|
2015-04-08 17:12:05 +02:00
|
|
|
lists:any(fun
|
|
|
|
({subscribed, _SubId}) -> true;
|
|
|
|
(_) -> false
|
|
|
|
end,
|
|
|
|
Subscriptions).
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
first_in_list(_Pred, []) ->
|
|
|
|
false;
|
2009-08-07 10:26:47 +02:00
|
|
|
first_in_list(Pred, [H | T]) ->
|
|
|
|
case Pred(H) of
|
2015-04-08 17:12:05 +02:00
|
|
|
true -> {value, H};
|
|
|
|
_ -> first_in_list(Pred, T)
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
itemids(Nidx, {U, S, R}) ->
|
|
|
|
itemids(Nidx, encode_jid({U, S, R}));
|
|
|
|
itemids(Nidx, SJID) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case catch
|
2015-04-08 17:12:05 +02:00
|
|
|
ejabberd_odbc:sql_query_t([<<"select itemid from pubsub_item where "
|
|
|
|
"nodeid='">>, Nidx, <<"' and publisher like '">>, SJID,
|
|
|
|
<<"%' order by modification desc;">>])
|
|
|
|
of
|
|
|
|
{selected, [<<"itemid">>], RItems} ->
|
|
|
|
[ItemId || [ItemId] <- RItems];
|
|
|
|
_ ->
|
|
|
|
[]
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
select_affiliation_subscriptions(Nidx, JID) ->
|
2009-08-07 10:26:47 +02:00
|
|
|
J = encode_jid(JID),
|
2013-03-14 10:33:02 +01:00
|
|
|
case catch
|
2015-04-08 17:12:05 +02:00
|
|
|
ejabberd_odbc:sql_query_t([<<"select affiliation,subscriptions from "
|
|
|
|
"pubsub_state where nodeid='">>,
|
|
|
|
Nidx, <<"' and jid='">>, J, <<"';">>])
|
|
|
|
of
|
|
|
|
{selected, [<<"affiliation">>, <<"subscriptions">>], [[A, S]]} ->
|
|
|
|
{decode_affiliation(A), decode_subscriptions(S)};
|
|
|
|
_ ->
|
|
|
|
{none, []}
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
select_affiliation_subscriptions(Nidx, JID, JID) ->
|
|
|
|
select_affiliation_subscriptions(Nidx, JID);
|
|
|
|
select_affiliation_subscriptions(Nidx, GenKey, SubKey) ->
|
|
|
|
{result, Affiliation} = get_affiliation(Nidx, GenKey),
|
|
|
|
{result, Subscriptions} = get_subscriptions(Nidx, SubKey),
|
2009-08-07 10:26:47 +02:00
|
|
|
{Affiliation, Subscriptions}.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
update_affiliation(Nidx, JID, Affiliation) ->
|
2009-08-07 10:26:47 +02:00
|
|
|
J = encode_jid(JID),
|
|
|
|
A = encode_affiliation(Affiliation),
|
2013-03-14 10:33:02 +01:00
|
|
|
case catch
|
2015-04-08 17:12:05 +02:00
|
|
|
ejabberd_odbc:sql_query_t([<<"update pubsub_state set affiliation='">>,
|
|
|
|
A, <<"' where nodeid='">>, Nidx,
|
|
|
|
<<"' and jid='">>, J, <<"';">>])
|
|
|
|
of
|
|
|
|
{updated, 1} ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
catch
|
|
|
|
ejabberd_odbc:sql_query_t([<<"insert into pubsub_state(nodeid, jid, affiliation, subscriptions) "
|
|
|
|
"values('">>, Nidx, <<"', '">>, J, <<"', '">>, A, <<"', '');">>])
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
update_subscription(Nidx, JID, Subscription) ->
|
2009-08-07 10:26:47 +02:00
|
|
|
J = encode_jid(JID),
|
|
|
|
S = encode_subscriptions(Subscription),
|
2013-03-14 10:33:02 +01:00
|
|
|
case catch
|
2015-04-08 17:12:05 +02:00
|
|
|
ejabberd_odbc:sql_query_t([<<"update pubsub_state set subscriptions='">>, S,
|
|
|
|
<<"' where nodeid='">>, Nidx, <<"' and jid='">>, J, <<"';">>])
|
|
|
|
of
|
|
|
|
{updated, 1} ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
catch
|
|
|
|
ejabberd_odbc:sql_query_t([<<"insert into pubsub_state(nodeid, jid, affiliation, subscriptions) "
|
|
|
|
"values('">>, Nidx, <<"', '">>, J, <<"', 'n', '">>, S, <<"');">>])
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
-spec(decode_jid/1 ::
|
|
|
|
( SJID :: binary())
|
|
|
|
-> ljid()
|
|
|
|
).
|
|
|
|
decode_jid(SJID) ->
|
|
|
|
jlib:jid_tolower(jlib:string_to_jid(SJID)).
|
|
|
|
|
|
|
|
-spec(decode_affiliation/1 ::
|
|
|
|
( Arg :: binary())
|
|
|
|
-> atom()
|
|
|
|
).
|
2013-03-14 10:33:02 +01:00
|
|
|
decode_affiliation(<<"o">>) -> owner;
|
|
|
|
decode_affiliation(<<"p">>) -> publisher;
|
2015-05-22 18:47:17 +02:00
|
|
|
decode_affiliation(<<"u">>) -> publish_only;
|
2013-03-14 10:33:02 +01:00
|
|
|
decode_affiliation(<<"m">>) -> member;
|
|
|
|
decode_affiliation(<<"c">>) -> outcast;
|
2009-08-07 10:26:47 +02:00
|
|
|
decode_affiliation(_) -> none.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
-spec(decode_subscription/1 ::
|
|
|
|
( Arg :: binary())
|
|
|
|
-> atom()
|
|
|
|
).
|
2013-03-14 10:33:02 +01:00
|
|
|
decode_subscription(<<"s">>) -> subscribed;
|
|
|
|
decode_subscription(<<"p">>) -> pending;
|
|
|
|
decode_subscription(<<"u">>) -> unconfigured;
|
2009-08-07 10:26:47 +02:00
|
|
|
decode_subscription(_) -> none.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
-spec(decode_subscriptions/1 ::
|
|
|
|
( Subscriptions :: binary())
|
|
|
|
-> [] | [{atom(), binary()},...]
|
|
|
|
).
|
2009-08-07 10:26:47 +02:00
|
|
|
decode_subscriptions(Subscriptions) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:foldl(fun (Subscription, Acc) ->
|
2015-04-08 17:12:05 +02:00
|
|
|
case str:tokens(Subscription, <<":">>) of
|
|
|
|
[S, SubId] -> [{decode_subscription(S), SubId} | Acc];
|
|
|
|
_ -> Acc
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
[], str:tokens(Subscriptions, <<",">>)).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
-spec(encode_jid/1 ::
|
|
|
|
( JID :: ljid())
|
|
|
|
-> binary()
|
|
|
|
).
|
2013-03-14 10:33:02 +01:00
|
|
|
encode_jid(JID) ->
|
2015-04-08 17:12:05 +02:00
|
|
|
ejabberd_odbc:escape(jlib:jid_to_string(JID)).
|
|
|
|
|
|
|
|
-spec(encode_host/1 ::
|
|
|
|
( Host :: host())
|
|
|
|
-> binary()
|
|
|
|
).
|
|
|
|
encode_host({_U, _S, _R} = LJID) -> encode_jid(LJID);
|
|
|
|
encode_host(Host) -> ejabberd_odbc:escape(Host).
|
|
|
|
|
|
|
|
-spec(encode_affiliation/1 ::
|
|
|
|
( Arg :: atom())
|
|
|
|
-> binary()
|
|
|
|
).
|
2013-03-14 10:33:02 +01:00
|
|
|
encode_affiliation(owner) -> <<"o">>;
|
|
|
|
encode_affiliation(publisher) -> <<"p">>;
|
2015-05-22 18:47:17 +02:00
|
|
|
encode_affiliation(publish_only) -> <<"u">>;
|
2013-03-14 10:33:02 +01:00
|
|
|
encode_affiliation(member) -> <<"m">>;
|
|
|
|
encode_affiliation(outcast) -> <<"c">>;
|
|
|
|
encode_affiliation(_) -> <<"n">>.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
-spec(encode_subscription/1 ::
|
|
|
|
( Arg :: atom())
|
|
|
|
-> binary()
|
|
|
|
).
|
2013-03-14 10:33:02 +01:00
|
|
|
encode_subscription(subscribed) -> <<"s">>;
|
|
|
|
encode_subscription(pending) -> <<"p">>;
|
|
|
|
encode_subscription(unconfigured) -> <<"u">>;
|
|
|
|
encode_subscription(_) -> <<"n">>.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
-spec(encode_subscriptions/1 ::
|
|
|
|
( Subscriptions :: [] | [{atom(), binary()},...])
|
|
|
|
-> binary()
|
|
|
|
).
|
2009-08-07 10:26:47 +02:00
|
|
|
encode_subscriptions(Subscriptions) ->
|
2015-04-08 17:12:05 +02:00
|
|
|
str:join([<<(encode_subscription(S))/binary, ":", SubId/binary>>
|
|
|
|
|| {S, SubId} <- Subscriptions], <<",">>).
|
2009-08-07 10:26:47 +02:00
|
|
|
|
|
|
|
%%% record getter/setter
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
state_to_raw(Nidx, State) ->
|
2009-08-07 10:26:47 +02:00
|
|
|
{JID, _} = State#pubsub_state.stateid,
|
|
|
|
J = encode_jid(JID),
|
|
|
|
A = encode_affiliation(State#pubsub_state.affiliation),
|
2015-04-08 17:12:05 +02:00
|
|
|
S = encode_subscriptions(State#pubsub_state.subscriptions),
|
|
|
|
[<<"'">>, Nidx, <<"', '">>, J, <<"', '">>, A, <<"', '">>, S, <<"'">>].
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
raw_to_item(Nidx, [ItemId, SJID, Creation, Modification, XML]) ->
|
2009-08-07 10:26:47 +02:00
|
|
|
JID = decode_jid(SJID),
|
2013-03-14 10:33:02 +01:00
|
|
|
ToTime = fun (Str) ->
|
2015-04-08 17:12:05 +02:00
|
|
|
[T1, T2, T3] = str:tokens(Str, <<":">>),
|
|
|
|
{jlib:l2i(T1), jlib:l2i(T2), jlib:l2i(T3)}
|
|
|
|
end,
|
2009-08-07 10:26:47 +02:00
|
|
|
Payload = case xml_stream:parse_element(XML) of
|
2015-04-08 17:12:05 +02:00
|
|
|
{error, _Reason} -> [];
|
|
|
|
El -> [El]
|
|
|
|
end,
|
|
|
|
#pubsub_item{itemid = {ItemId, Nidx},
|
|
|
|
creation = {ToTime(Creation), JID},
|
|
|
|
modification = {ToTime(Modification), JID},
|
|
|
|
payload = Payload}.
|