aboutsummaryrefslogtreecommitdiff
path: root/src/mod_pubsub/node_default.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/mod_pubsub/node_default.erl')
-rw-r--r--src/mod_pubsub/node_default.erl147
1 files changed, 85 insertions, 62 deletions
diff --git a/src/mod_pubsub/node_default.erl b/src/mod_pubsub/node_default.erl
index f52c97648..ae4717cef 100644
--- a/src/mod_pubsub/node_default.erl
+++ b/src/mod_pubsub/node_default.erl
@@ -277,11 +277,16 @@ delete_node(Host, Removed) ->
%% <p>In the default plugin module, the record is unchanged.</p>
subscribe_node(Host, Node, Sender, Subscriber, AccessModel,
SendLast, PresenceSubscription, RosterGroup) ->
- SubscriberKey = jlib:jid_tolower(jlib:jid_remove_resource(Subscriber)),
- Authorized = (jlib:jid_tolower(jlib:jid_remove_resource(Sender)) == SubscriberKey),
- State = get_state(Host, Node, SubscriberKey),
- #pubsub_state{affiliation = Affiliation,
- subscription = Subscription} = State,
+ SubKey = jlib:jid_tolower(Subscriber),
+ GenKey = jlib:jid_remove_resource(SubKey),
+ Authorized = (jlib:jid_tolower(jlib:jid_remove_resource(Sender)) == GenKey),
+ GenState = get_state(Host, Node, GenKey),
+ SubState = case SubKey of
+ GenKey -> GenState;
+ _ -> get_state(Host, Node, SubKey)
+ end,
+ Affiliation = GenState#pubsub_state.affiliation,
+ Subscription = SubState#pubsub_state.subscription,
Whitelisted = lists:member(Affiliation, [member, publisher, owner]),
if
not Authorized ->
@@ -321,7 +326,7 @@ subscribe_node(Host, Node, Sender, Subscriber, AccessModel,
true ->
subscribed
end,
- set_state(State#pubsub_state{subscription = NewSubscription}),
+ set_state(SubState#pubsub_state{subscription = NewSubscription}),
case NewSubscription of
subscribed ->
case SendLast of
@@ -343,9 +348,16 @@ subscribe_node(Host, Node, Sender, Subscriber, AccessModel,
%% Reason = mod_pubsub:stanzaError()
%% @doc <p>Unsubscribe the <tt>Subscriber</tt> from the <tt>Node</tt>.</p>
unsubscribe_node(Host, Node, Sender, Subscriber, _SubId) ->
- SubscriberKey = jlib:jid_tolower(jlib:jid_remove_resource(Subscriber)),
- Authorized = (jlib:jid_tolower(jlib:jid_remove_resource(Sender)) == SubscriberKey),
- State = get_state(Host, Node, SubscriberKey),
+ SubKey = jlib:jid_tolower(Subscriber),
+ GenKey = jlib:jid_remove_resource(SubKey),
+ Authorized = (jlib:jid_tolower(jlib:jid_remove_resource(Sender)) == GenKey),
+ GenState = get_state(Host, Node, GenKey),
+ SubState = case SubKey of
+ GenKey -> GenState;
+ _ -> get_state(Host, Node, SubKey)
+ end,
+ Affiliation = GenState#pubsub_state.affiliation,
+ Subscription = SubState#pubsub_state.subscription,
if
%% Entity did not specify SubID
%%SubID == "", ?? ->
@@ -354,17 +366,18 @@ unsubscribe_node(Host, Node, Sender, Subscriber, _SubId) ->
%%InvalidSubID ->
%% {error, ?ERR_EXTENDED(?ERR_NOT_ACCEPTABLE, "invalid-subid")};
%% Requesting entity is not a subscriber
- State#pubsub_state.subscription == none ->
+ Subscription == none ->
{error, ?ERR_EXTENDED(?ERR_UNEXPECTED_REQUEST, "not-subscribed")};
%% Requesting entity is prohibited from unsubscribing entity
- (not Authorized) and (State#pubsub_state.affiliation =/= owner) ->
+ (not Authorized) and (Affiliation =/= owner) ->
{error, ?ERR_FORBIDDEN};
%% Was just subscriber, remove the record
- State#pubsub_state.affiliation == none ->
- del_state(State#pubsub_state.stateid),
+ Affiliation == none ->
+ del_state(SubState#pubsub_state.stateid),
{result, default};
true ->
- set_state(State#pubsub_state{subscription = none}),
+ %% TODO, may require better clean
+ set_state(SubState#pubsub_state{subscription = none}),
{result, default}
end.
@@ -408,10 +421,15 @@ unsubscribe_node(Host, Node, Sender, Subscriber, _SubId) ->
%% </p>
%% <p>In the default plugin module, the record is unchanged.</p>
publish_item(Host, Node, Publisher, PublishModel, MaxItems, ItemId, Payload) ->
- PublisherKey = jlib:jid_tolower(jlib:jid_remove_resource(Publisher)),
- State = get_state(Host, Node, PublisherKey),
- #pubsub_state{affiliation = Affiliation,
- subscription = Subscription} = State,
+ SubKey = jlib:jid_tolower(Publisher),
+ GenKey = jlib:jid_remove_resource(SubKey),
+ GenState = get_state(Host, Node, GenKey),
+ SubState = case SubKey of
+ GenKey -> GenState;
+ _ -> get_state(Host, Node, SubKey)
+ end,
+ Affiliation = GenState#pubsub_state.affiliation,
+ Subscription = SubState#pubsub_state.subscription,
if
not ((PublishModel == open)
or ((PublishModel == publishers)
@@ -421,7 +439,7 @@ publish_item(Host, Node, Publisher, PublishModel, MaxItems, ItemId, Payload) ->
%% Entity does not have sufficient privileges to publish to node
{error, ?ERR_FORBIDDEN};
true ->
- PubId = {PublisherKey, now()}, %% TODO, uses {now(),PublisherKey} for sorting (EJAB-824)
+ PubId = {SubKey, now()}, %% TODO, uses {now(),PublisherKey} for sorting (EJAB-824)
%% TODO: check creation, presence, roster (EJAB-663)
Item = case get_item(Host, Node, ItemId) of
{result, OldItem} ->
@@ -429,17 +447,17 @@ publish_item(Host, Node, Publisher, PublishModel, MaxItems, ItemId, Payload) ->
payload = Payload};
_ ->
#pubsub_item{itemid = {ItemId, {Host, Node}},
- creation = PubId,
+ creation = {GenKey, now()},
modification = PubId,
payload = Payload}
end,
- Items = [ItemId | State#pubsub_state.items--[ItemId]],
+ Items = [ItemId | GenState#pubsub_state.items--[ItemId]],
{result, {NI, OI}} = remove_extra_items(
Host, Node, MaxItems, Items),
if MaxItems > 0 -> set_item(Item);
true -> ok
end,
- set_state(State#pubsub_state{items = NI}),
+ set_state(GenState#pubsub_state{items = NI}),
{result, {default, broadcast, OI}}
end.
@@ -480,12 +498,13 @@ remove_extra_items(Host, Node, MaxItems, ItemIds) ->
%% <p>Default plugin: The user performing the deletion must be the node owner
%% or a publisher.</p>
delete_item(Host, Node, Publisher, ItemId) ->
- PublisherKey = jlib:jid_tolower(jlib:jid_remove_resource(Publisher)),
- State = get_state(Host, Node, PublisherKey),
- #pubsub_state{affiliation = Affiliation, items = Items} = State,
+ SubKey = jlib:jid_tolower(Publisher),
+ GenKey = jlib:jid_remove_resource(SubKey),
+ GenState = get_state(Host, Node, GenKey),
+ #pubsub_state{affiliation = Affiliation, items = Items} = GenState,
Allowed = (Affiliation == publisher) orelse (Affiliation == owner)
orelse case get_item(Host, Node, ItemId) of
- {result, #pubsub_item{creation = {PublisherKey, _}}} -> true;
+ {result, #pubsub_item{creation = {GenKey, _}}} -> true;
_ -> false
end,
if
@@ -497,7 +516,7 @@ delete_item(Host, Node, Publisher, ItemId) ->
{result, _} ->
del_item(Host, Node, ItemId),
NewItems = lists:delete(ItemId, Items),
- set_state(State#pubsub_state{items = NewItems}),
+ set_state(GenState#pubsub_state{items = NewItems}),
{result, {default, broadcast}};
_ ->
%% Non-existent node or item
@@ -512,8 +531,10 @@ delete_item(Host, Node, Publisher, ItemId) ->
%% Node = mod_pubsub:pubsubNode()
%% Owner = mod_pubsub:jid()
purge_node(Host, Node, Owner) ->
- OwnerKey = jlib:jid_tolower(jlib:jid_remove_resource(Owner)),
- case get_state(Host, Node, OwnerKey) of
+ SubKey = jlib:jid_tolower(Owner),
+ GenKey = jlib:jid_remove_resource(SubKey),
+ GenState = get_state(Host, Node, GenKey),
+ case GenState of
#pubsub_state{items = Items, affiliation = owner} ->
del_items(Host, Node, Items),
{result, {default, broadcast}};
@@ -533,9 +554,10 @@ purge_node(Host, Node, Owner) ->
%% that will be added to the affiliation stored in the main
%% <tt>pubsub_state</tt> table.</p>
get_entity_affiliations(Host, Owner) ->
- OwnerKey = jlib:jid_tolower(jlib:jid_remove_resource(Owner)),
+ SubKey = jlib:jid_tolower(Owner),
+ GenKey = jlib:jid_remove_resource(SubKey),
States = mnesia:match_object(
- #pubsub_state{stateid = {OwnerKey, {Host, '_'}}, _ = '_'}),
+ #pubsub_state{stateid = {GenKey, {Host, '_'}}, _ = '_'}),
Tr = fun(#pubsub_state{stateid = {_, {_, N}}, affiliation = A}) ->
{N, A}
end,
@@ -550,14 +572,16 @@ get_node_affiliations(Host, Node) ->
{result, lists:map(Tr, States)}.
get_affiliation(Host, Node, Owner) ->
- OwnerKey = jlib:jid_tolower(jlib:jid_remove_resource(Owner)),
- State = get_state(Host, Node, OwnerKey),
- {result, State#pubsub_state.affiliation}.
+ SubKey = jlib:jid_tolower(Owner),
+ GenKey = jlib:jid_remove_resource(SubKey),
+ GenState = get_state(Host, Node, GenKey),
+ {result, GenState#pubsub_state.affiliation}.
set_affiliation(Host, Node, Owner, Affiliation) ->
- OwnerKey = jlib:jid_tolower(jlib:jid_remove_resource(Owner)),
- State = get_state(Host, Node, OwnerKey),
- set_state(State#pubsub_state{affiliation = Affiliation}),
+ SubKey = jlib:jid_tolower(Owner),
+ GenKey = jlib:jid_remove_resource(SubKey),
+ GenState = get_state(Host, Node, GenKey),
+ set_state(GenState#pubsub_state{affiliation = Affiliation}),
ok.
%% @spec (Host, Owner) -> [{Node,Subscription}]
@@ -572,11 +596,16 @@ set_affiliation(Host, Node, Owner, Affiliation) ->
%% that will be added to the affiliation stored in the main
%% <tt>pubsub_state</tt> table.</p>
get_entity_subscriptions(Host, Owner) ->
- OwnerKey = jlib:jid_tolower(jlib:jid_remove_resource(Owner)),
- States = mnesia:match_object(
- #pubsub_state{stateid = {OwnerKey, {Host, '_'}}, _ = '_'}),
- Tr = fun(#pubsub_state{stateid = {_, {_, N}}, subscription = S}) ->
- {N, S}
+ States = case jlib:jid_tolower(Owner) of
+ {U, D, ""} -> mnesia:match_object(
+ #pubsub_state{stateid = {{U, D, '_'}, {Host, '_'}}, _ = '_'});
+ {U, D, R} -> mnesia:match_object(
+ #pubsub_state{stateid = {{U, D, ""}, {Host, '_'}}, _ = '_'})
+ ++ mnesia:match_object(
+ #pubsub_state{stateid = {{U, D, R}, {Host, '_'}}, _ = '_'})
+ end,
+ Tr = fun(#pubsub_state{stateid = {J, {_, N}}, subscription = S}) ->
+ {N, S, J}
end,
{result, lists:map(Tr, States)}.
@@ -589,14 +618,14 @@ get_node_subscriptions(Host, Node) ->
{result, lists:map(Tr, States)}.
get_subscription(Host, Node, Owner) ->
- OwnerKey = jlib:jid_tolower(jlib:jid_remove_resource(Owner)),
- State = get_state(Host, Node, OwnerKey),
- {result, State#pubsub_state.subscription}.
+ SubKey = jlib:jid_tolower(Owner),
+ SubState = get_state(Host, Node, SubKey),
+ {result, SubState#pubsub_state.subscription}.
set_subscription(Host, Node, Owner, Subscription) ->
- OwnerKey = jlib:jid_tolower(jlib:jid_remove_resource(Owner)),
- State = get_state(Host, Node, OwnerKey),
- set_state(State#pubsub_state{subscription = Subscription}),
+ SubKey = jlib:jid_tolower(Owner),
+ SubState = get_state(Host, Node, SubKey),
+ set_state(SubState#pubsub_state{subscription = Subscription}),
ok.
%% @spec (Host, Node) -> [States] | []
@@ -664,10 +693,10 @@ get_items(Host, Node, _From) ->
#pubsub_item{itemid = {'_', {Host, Node}}, _ = '_'}),
{result, Items}.
get_items(Host, Node, JID, AccessModel, PresenceSubscription, RosterGroup, _SubId) ->
- State = get_state(Host, Node, jlib:jid_tolower(jlib:jid_remove_resource(JID))),
- #pubsub_state{affiliation = Affiliation,
- subscription = Subscription} = State,
- Subscribed = not ((Subscription == none) or (Subscription == pending)),
+ SubKey = jlib:jid_tolower(JID),
+ GenKey = jlib:jid_remove_resource(SubKey),
+ GenState = get_state(Host, Node, GenKey),
+ Affiliation = GenState#pubsub_state.affiliation,
Whitelisted = lists:member(Affiliation, [member, publisher, owner]),
if
%%SubID == "", ?? ->
@@ -679,9 +708,6 @@ get_items(Host, Node, JID, AccessModel, PresenceSubscription, RosterGroup, _SubI
Affiliation == outcast ->
%% Requesting entity is blocked
{error, ?ERR_FORBIDDEN};
- (AccessModel == open) and (not Subscribed) ->
- %% Entity is not subscribed
- {error, ?ERR_EXTENDED(?ERR_NOT_AUTHORIZED, "not-subscribed")};
(AccessModel == presence) and (not PresenceSubscription) ->
%% Entity is not authorized to create a subscription (presence subscription required)
{error, ?ERR_EXTENDED(?ERR_NOT_AUTHORIZED, "presence-subscription-required")};
@@ -715,10 +741,10 @@ get_item(Host, Node, ItemId) ->
{error, ?ERR_ITEM_NOT_FOUND}
end.
get_item(Host, Node, ItemId, JID, AccessModel, PresenceSubscription, RosterGroup, _SubId) ->
- State = get_state(Host, Node, jlib:jid_tolower(jlib:jid_remove_resource(JID))),
- #pubsub_state{affiliation = Affiliation,
- subscription = Subscription} = State,
- Subscribed = not ((Subscription == none) or (Subscription == pending)),
+ SubKey = jlib:jid_tolower(JID),
+ GenKey = jlib:jid_remove_resource(SubKey),
+ GenState = get_state(Host, Node, GenKey),
+ Affiliation = GenState#pubsub_state.affiliation,
Whitelisted = lists:member(Affiliation, [member, publisher, owner]),
if
%%SubID == "", ?? ->
@@ -730,9 +756,6 @@ get_item(Host, Node, ItemId, JID, AccessModel, PresenceSubscription, RosterGroup
Affiliation == outcast ->
%% Requesting entity is blocked
{error, ?ERR_FORBIDDEN};
- (AccessModel == open) and (not Subscribed) ->
- %% Entity is not subscribed
- {error, ?ERR_EXTENDED(?ERR_NOT_AUTHORIZED, "not-subscribed")};
(AccessModel == presence) and (not PresenceSubscription) ->
%% Entity is not authorized to create a subscription (presence subscription required)
{error, ?ERR_EXTENDED(?ERR_NOT_AUTHORIZED, "presence-subscription-required")};