diff --git a/src/mod_pubsub/mod_pubsub.erl b/src/mod_pubsub/mod_pubsub.erl index a275c334c..3aa758006 100644 --- a/src/mod_pubsub/mod_pubsub.erl +++ b/src/mod_pubsub/mod_pubsub.erl @@ -668,6 +668,7 @@ disco_sm_items(Acc, From, To, NodeB, _Lang) -> Host = exmpp_jid:prep_domain_as_list(To), LJID = jlib:short_prepd_bare_jid(To), Action = fun(#pubsub_node{type = Type, id = NodeId}) -> + % TODO call get_items/6 instead for access control (EJAB-1033) case node_call(Type, get_items, [NodeId, From]) of {result, []} -> none; @@ -1111,6 +1112,7 @@ iq_disco_items(Host, Item, From) -> %% TODO That is, remove name attribute (or node?, please check for 2.1) Action = fun(#pubsub_node{type = Type, id = NodeId}) -> + % TODO call get_items/6 instead for access control (EJAB-1033) NodeItems = case node_call(Type, get_items, [NodeId, From]) of {result, I} -> I; _ -> [] diff --git a/src/mod_pubsub/mod_pubsub_odbc.erl b/src/mod_pubsub/mod_pubsub_odbc.erl index 9195447f8..901fac0c3 100644 --- a/src/mod_pubsub/mod_pubsub_odbc.erl +++ b/src/mod_pubsub/mod_pubsub_odbc.erl @@ -494,6 +494,7 @@ disco_sm_items(Acc, From, To, NodeB, _Lang) -> Host = exmpp_jid:prep_domain_as_list(To), LJID = jlib:short_prepd_bare_jid(To), Action = fun(#pubsub_node{type = Type, id = NodeId}) -> + % TODO call get_items/6 instead for access control (EJAB-1033) case node_call(Type, get_items, [NodeId, From]) of {result, []} -> none; @@ -940,6 +941,7 @@ iq_disco_items(Host, Item, From, RSM) -> %% TODO That is, remove name attribute (or node?, please check for 2.1) Action = fun(#pubsub_node{type = Type, id = NodeId}) -> + %% TODO call get_items/6 instead for access control (EJAB-1033) {NodeItems, RsmOut} = case node_call(Type, get_items, [NodeId, From, RSM]) of {result, I} -> I; _ -> {[], none} diff --git a/src/mod_pubsub/node_hometree.erl b/src/mod_pubsub/node_hometree.erl index 1fe14b14b..1cb017c4c 100644 --- a/src/mod_pubsub/node_hometree.erl +++ b/src/mod_pubsub/node_hometree.erl @@ -556,12 +556,30 @@ delete_item(NodeId, Publisher, PublishModel, ItemId) -> case lists:member(ItemId, Items) of true -> del_item(NodeId, ItemId), - NewItems = lists:delete(ItemId, Items), - set_state(GenState#pubsub_state{items = NewItems}), + set_state(GenState#pubsub_state{items = lists:delete(ItemId, Items)}), {result, {default, broadcast}}; false -> - %% Non-existent node or item - {error, 'item-not-found'} + case Affiliation of + owner -> + %% Owner can delete other publishers items as well + {result, States} = get_states(NodeId), + lists:foldl( + fun(#pubsub_state{items = PI, affiliation = publisher} = S, Res) -> + case lists:member(ItemId, PI) of + true -> + del_item(NodeId, ItemId), + set_state(S#pubsub_state{items = lists:delete(ItemId, PI)}), + {result, {default, broadcast}}; + false -> + Res + end; + (_, Res) -> + Res + end, {error, 'item-not-found'}, States); + _ -> + %% Non-existent node or item + {error, 'item-not-found'} + end end end. @@ -574,9 +592,15 @@ purge_node(NodeId, Owner) -> GenKey = jlib:short_prepd_bare_jid(Owner), GenState = get_state(NodeId, GenKey), case GenState of - #pubsub_state{items = Items, affiliation = owner} -> - del_items(NodeId, Items), - set_state(GenState#pubsub_state{items = []}), + #pubsub_state{affiliation = owner} -> + {result, States} = get_states(NodeId), + lists:foreach( + fun(#pubsub_state{items = []}) -> + ok; + (#pubsub_state{items = Items} = S) -> + del_items(NodeId, Items), + set_state(S#pubsub_state{items = []}) + end, States), {result, {default, broadcast}}; _ -> %% Entity is not owner diff --git a/src/mod_pubsub/node_hometree_odbc.erl b/src/mod_pubsub/node_hometree_odbc.erl index 91c9be46f..801141b3b 100644 --- a/src/mod_pubsub/node_hometree_odbc.erl +++ b/src/mod_pubsub/node_hometree_odbc.erl @@ -562,9 +562,12 @@ purge_node(NodeId, Owner) -> GenKey = jlib:short_prepd_bare_jid(Owner), GenState = get_state(NodeId, GenKey), case GenState of - #pubsub_state{items = Items, affiliation = owner} -> - del_items(NodeId, Items), - %% set new item list use useless + #pubsub_state{affiliation = owner} -> + {result, States} = get_states(NodeId), + lists:foreach( + fun(#pubsub_state{items = []}) -> ok; + (#pubsub_state{items = Items}) -> del_items(NodeId, Items) + end, States), {result, {default, broadcast}}; _ -> %% Entity is not owner diff --git a/src/mod_pubsub/pubsub_odbc.patch b/src/mod_pubsub/pubsub_odbc.patch index 370f94bac..39fa40205 100644 --- a/src/mod_pubsub/pubsub_odbc.patch +++ b/src/mod_pubsub/pubsub_odbc.patch @@ -1,5 +1,5 @@ ---- mod_pubsub.erl 2009-09-01 15:20:44.000000000 +0200 -+++ mod_pubsub_odbc.erl 2009-09-01 15:22:27.000000000 +0200 +--- mod_pubsub.erl 2009-09-09 23:42:26.000000000 +0200 ++++ mod_pubsub_odbc.erl 2009-09-09 23:42:26.000000000 +0200 @@ -45,7 +45,7 @@ %%% TODO %%% plugin: generate Reply (do not use broadcast atom anymore) @@ -251,7 +251,7 @@ true -> % resource not concerned about that subscription ok -@@ -809,10 +635,10 @@ +@@ -810,10 +636,10 @@ {result, Subscriptions} = node_action(Host, PType, get_entity_subscriptions, [Host, Subscriber]), lists:foreach(fun ({Node, subscribed, _, JID}) -> @@ -264,7 +264,7 @@ true -> node_action(Host, Type, unsubscribe_node, [NodeId, Subscriber, JID, all]); false -> -@@ -930,11 +756,12 @@ +@@ -931,11 +757,12 @@ end, ejabberd_router:route(To, From, Res); #iq{type = get, ns = ?NS_DISCO_ITEMS, @@ -279,7 +279,7 @@ {result, IQRes} -> Result = #xmlel{ns = ?NS_DISCO_ITEMS, name = 'query', attrs = QAttrs, -@@ -1037,7 +864,7 @@ +@@ -1038,7 +865,7 @@ [] -> ["leaf"]; %% No sub-nodes: it's a leaf node _ -> @@ -288,7 +288,7 @@ {result, []} -> ["collection"]; {result, _} -> ["leaf", "collection"]; _ -> [] -@@ -1053,8 +880,9 @@ +@@ -1054,8 +881,9 @@ []; true -> [#xmlel{ns = ?NS_DISCO_INFO, name = 'feature', attrs = [?XMLATTR('var', ?NS_PUBSUB_s)]} | @@ -300,7 +300,7 @@ end, features(Type))] end, %% TODO: add meta-data info (spec section 5.4) -@@ -1082,14 +910,15 @@ +@@ -1083,14 +911,15 @@ #xmlel{ns = ?NS_DISCO_INFO, name = 'feature', attrs = [?XMLATTR('var', ?NS_DISCO_ITEMS_s)]}, #xmlel{ns = ?NS_DISCO_INFO, name = 'feature', attrs = [?XMLATTR('var', ?NS_PUBSUB_s)]}, #xmlel{ns = ?NS_DISCO_INFO, name = 'feature', attrs = [?XMLATTR('var', ?NS_VCARD_s)]}] ++ @@ -319,7 +319,7 @@ {result, lists:map( fun(#pubsub_node{nodeid = {_, SubNode}}) -> SN = node_to_string(SubNode), -@@ -1099,7 +928,7 @@ +@@ -1100,7 +929,7 @@ ?XMLATTR('node', SN), ?XMLATTR('name', RN)]} end, tree_action(Host, get_subnodes, [Host, [], From]))}; @@ -328,11 +328,13 @@ case string:tokens(Item, "!") of [_SNode, _ItemID] -> {result, []}; -@@ -1111,9 +940,9 @@ +@@ -1112,10 +941,10 @@ %% TODO That is, remove name attribute (or node?, please check for 2.1) Action = fun(#pubsub_node{type = Type, id = NodeId}) -> +- % TODO call get_items/6 instead for access control (EJAB-1033) - NodeItems = case node_call(Type, get_items, [NodeId, From]) of ++ %% TODO call get_items/6 instead for access control (EJAB-1033) + {NodeItems, RsmOut} = case node_call(Type, get_items, [NodeId, From, RSM]) of {result, I} -> I; - _ -> [] @@ -340,7 +342,7 @@ end, Nodes = lists:map( fun(#pubsub_node{nodeid = {_, SubNode}}) -> -@@ -1129,7 +958,7 @@ +@@ -1131,7 +960,7 @@ #xmlel{ns = ?NS_DISCO_ITEMS, name = 'item', attrs = [?XMLATTR('jid', Host), ?XMLATTR('node', SN), ?XMLATTR('name', Name)]} end, NodeItems), @@ -349,7 +351,7 @@ end, case transaction(Host, Node, Action, sync_dirty) of {result, {_, Result}} -> {result, Result}; -@@ -1268,7 +1097,8 @@ +@@ -1270,7 +1099,8 @@ (_, Acc) -> Acc end, [], exmpp_xml:remove_cdata_from_list(Els)), @@ -359,7 +361,7 @@ {get, 'subscriptions'} -> get_subscriptions(Host, Node, From, Plugins); {get, 'affiliations'} -> -@@ -1290,8 +1120,9 @@ +@@ -1292,8 +1122,9 @@ end. iq_pubsub_owner(Host, ServerHost, From, IQType, SubEl, Lang) -> @@ -371,7 +373,7 @@ case Action of [#xmlel{name = Name, attrs = Attrs, children = Els}] -> Node = case Host of -@@ -1421,7 +1252,8 @@ +@@ -1423,7 +1254,8 @@ _ -> [] end end, @@ -381,7 +383,7 @@ sync_dirty) of {result, Res} -> Res; Err -> Err -@@ -1466,7 +1298,7 @@ +@@ -1468,7 +1300,7 @@ %%% authorization handling @@ -390,7 +392,7 @@ Lang = "en", %% TODO fix {U, S, R} = Subscriber, Stanza = #xmlel{ns = ?NS_JABBER_CLIENT, name = 'message', children = -@@ -1496,7 +1328,7 @@ +@@ -1498,7 +1330,7 @@ lists:foreach(fun(Owner) -> {U, S, R} = Owner, ejabberd_router ! {route, service_jid(Host), exmpp_jid:make(U, S, R), Stanza} @@ -399,7 +401,7 @@ find_authorization_response(Packet) -> Els = Packet#xmlel.children, -@@ -1559,8 +1391,8 @@ +@@ -1561,8 +1393,8 @@ "true" -> true; _ -> false end, @@ -410,7 +412,7 @@ {result, Subscriptions} = node_call(Type, get_subscriptions, [NodeId, Subscriber]), if not IsApprover -> -@@ -1750,7 +1582,7 @@ +@@ -1752,7 +1584,7 @@ end, Reply = #xmlel{ns = ?NS_PUBSUB, name = 'pubsub', children = [#xmlel{ns = ?NS_PUBSUB, name = 'create', attrs = nodeAttr(Node)}]}, @@ -419,7 +421,7 @@ {result, {Result, broadcast}} -> %%Lang = "en", %% TODO: fix %%OwnerKey = jlib:jid_tolower(jlib:jid_remove_resource(Owner)), -@@ -1859,7 +1691,7 @@ +@@ -1861,7 +1693,7 @@ %%
  • The node does not exist.
  • %% subscribe_node(Host, Node, From, JID, Configuration) -> @@ -428,7 +430,7 @@ Subscriber = try jlib:short_prepd_jid(exmpp_jid:parse(JID)) catch -@@ -1867,7 +1699,7 @@ +@@ -1869,7 +1701,7 @@ {undefined, undefined, undefined} end, SubId = uniqid(), @@ -437,7 +439,7 @@ Features = features(Type), SubscribeFeature = lists:member("subscribe", Features), OptionsFeature = lists:member("subscription-options", Features), -@@ -1886,9 +1718,13 @@ +@@ -1888,9 +1720,13 @@ {"", "", ""} -> {false, false}; _ -> @@ -454,7 +456,7 @@ end end, if -@@ -2213,7 +2049,7 @@ +@@ -2215,7 +2051,7 @@ %%

    The permission are not checked in this function.

    %% @todo We probably need to check that the user doing the query has the right %% to read the items. @@ -463,7 +465,7 @@ MaxItems = if SMaxItems == "" -> ?MAXITEMS; -@@ -2252,11 +2088,11 @@ +@@ -2254,11 +2090,11 @@ node_call(Type, get_items, [NodeId, From, AccessModel, PresenceSubscription, RosterGroup, @@ -477,7 +479,7 @@ SendItems = case ItemIDs of [] -> Items; -@@ -2269,7 +2105,7 @@ +@@ -2271,7 +2107,7 @@ %% number of items sent to MaxItems: {result, #xmlel{ns = ?NS_PUBSUB, name = 'pubsub', children = [#xmlel{ns = ?NS_PUBSUB, name = 'items', attrs = nodeAttr(Node), children = @@ -486,7 +488,7 @@ Error -> Error end -@@ -2301,16 +2137,25 @@ +@@ -2303,16 +2139,25 @@ %% @doc

    Resend the items of a node to the user.

    %% @todo use cache-last-item feature send_items(Host, Node, NodeId, Type, LJID, last) -> @@ -519,7 +521,7 @@ send_items(Host, Node, NodeId, Type, {LU, LS, LR} = LJID, Number) -> ToSend = case node_action(Host, Type, get_items, [NodeId, LJID]) of {result, []} -> -@@ -2431,29 +2276,12 @@ +@@ -2433,29 +2278,12 @@ error -> {error, 'bad-request'}; _ -> @@ -552,7 +554,7 @@ end, Entities), {result, []}; _ -> -@@ -2508,11 +2336,11 @@ +@@ -2510,11 +2338,11 @@ end. read_sub(Subscriber, Node, NodeID, SubID, Lang) -> @@ -566,7 +568,7 @@ OptionsEl = #xmlel{ns = ?NS_PUBSUB, name = 'options', attrs = [?XMLATTR('node', node_to_string(Node)), ?XMLATTR('jid', exmpp_jid:to_binary(Subscriber)), -@@ -2546,7 +2374,7 @@ +@@ -2548,7 +2376,7 @@ _ -> {"", "", ""} %%pablo TODO: "" or <<>> ?. short_jid uses exmpp_jid:node/1, etc. that returns binaries end, @@ -575,7 +577,7 @@ {result, Subs} = node_call(Type, get_subscriptions, [NodeID, Subscriber]), SubIDs = lists:foldl(fun({subscribed, SID}, Acc) -> -@@ -2566,7 +2394,7 @@ +@@ -2568,7 +2396,7 @@ end. write_sub(Subscriber, NodeID, SubID, Options) -> @@ -584,7 +586,7 @@ {error, notfound} -> {error, extended_error('not-acceptable', "invalid-subid")}; {result, _} -> -@@ -2739,8 +2567,8 @@ +@@ -2741,8 +2569,8 @@ ?XMLATTR('subsription', subscription_to_string(Sub)) | nodeAttr(Node)]}]}]}, ejabberd_router ! {route, service_jid(Host), JID, Stanza} end, @@ -595,7 +597,7 @@ true -> Result = lists:foldl(fun({JID, Subscription, SubId}, Acc) -> -@@ -3027,7 +2855,7 @@ +@@ -3029,7 +2857,7 @@ {Depth, [{N, get_node_subs(N)} || N <- Nodes]} end, tree_call(Host, get_parentnodes_tree, [Host, Node, service_jid(Host)]))} end, @@ -604,7 +606,7 @@ {result, CollSubs} -> CollSubs; _ -> [] end. -@@ -3041,9 +2869,9 @@ +@@ -3043,9 +2871,9 @@ get_options_for_subs(NodeID, Subs) -> lists:foldl(fun({JID, subscribed, SubID}, Acc) -> @@ -616,7 +618,7 @@ _ -> Acc end; (_, Acc) -> -@@ -3238,6 +3066,30 @@ +@@ -3240,6 +3068,30 @@ Result end. @@ -647,7 +649,7 @@ %% @spec (Host, Options) -> MaxItems %% Host = host() %% Options = [Option] -@@ -3614,7 +3466,13 @@ +@@ -3616,7 +3468,13 @@ tree_action(Host, Function, Args) -> ?DEBUG("tree_action ~p ~p ~p",[Host,Function,Args]), Fun = fun() -> tree_call(Host, Function, Args) end, @@ -662,7 +664,7 @@ %% @doc

    node plugin call.

    node_call(Type, Function, Args) -> -@@ -3634,13 +3492,13 @@ +@@ -3636,13 +3494,13 @@ node_action(Host, Type, Function, Args) -> ?DEBUG("node_action ~p ~p ~p ~p",[Host,Type,Function,Args]), @@ -678,7 +680,7 @@ case tree_call(Host, get_node, [Host, Node]) of N when is_record(N, pubsub_node) -> case Action(N) of -@@ -3653,8 +3511,15 @@ +@@ -3655,8 +3513,15 @@ end end, Trans). @@ -696,7 +698,7 @@ {result, Result} -> {result, Result}; {error, Error} -> {error, Error}; {atomic, {result, Result}} -> {result, Result}; -@@ -3662,6 +3527,15 @@ +@@ -3664,6 +3529,15 @@ {aborted, Reason} -> ?ERROR_MSG("transaction return internal error: ~p~n", [{aborted, Reason}]), {error, 'internal-server-error'}; @@ -712,7 +714,7 @@ {'EXIT', Reason} -> ?ERROR_MSG("transaction return internal error: ~p~n", [{'EXIT', Reason}]), {error, 'internal-server-error'}; -@@ -3670,6 +3544,16 @@ +@@ -3672,6 +3546,16 @@ {error, 'internal-server-error'} end. diff --git a/src/mod_pubsub/pubsub_subscription.erl b/src/mod_pubsub/pubsub_subscription.erl index e00dacd37..eacc63d16 100644 --- a/src/mod_pubsub/pubsub_subscription.erl +++ b/src/mod_pubsub/pubsub_subscription.erl @@ -38,8 +38,6 @@ read_subscription/3, write_subscription/4]). --include_lib("stdlib/include/qlc.hrl"). - -include("pubsub.hrl"). -include_lib("exmpp/include/exmpp.hrl"). @@ -94,30 +92,34 @@ init() -> subscribe_node(JID, NodeID, Options) -> try mnesia:sync_dirty(fun add_subscription/3, [JID, NodeID, Options]) of - Result -> {result, Result} - catch - Error -> Error + {error, Error} -> {error, Error}; + Result -> {result, Result} + catch + Error -> Error end. unsubscribe_node(JID, NodeID, SubID) -> try mnesia:sync_dirty(fun delete_subscription/3, [JID, NodeID, SubID]) of + {error, Error} -> {error, Error}; Result -> {result, Result} catch - Error -> Error + Error -> Error end. get_subscription(JID, NodeID, SubID) -> try mnesia:sync_dirty(fun read_subscription/3, [JID, NodeID, SubID]) of - Result -> {result, Result} + {error, Error} -> {error, Error}; + Result -> {result, Result} catch - Error -> Error + Error -> Error end. set_subscription(JID, NodeID, SubID, Options) -> try mnesia:sync_dirty(fun write_subscription/4, [JID, NodeID, SubID, Options]) of + {error, Error} -> {error, Error}; Result -> {result, Result} catch Error -> Error @@ -167,26 +169,23 @@ create_table() -> add_subscription(_JID, _NodeID, Options) -> SubID = make_subid(), - Record = #pubsub_subscription{subid = SubID, options = Options}, - mnesia:write(Record), + mnesia:write(#pubsub_subscription{subid = SubID, options = Options}), SubID. -delete_subscription(JID, NodeID, SubID) -> - Sub = read_subscription(JID, NodeID, SubID), - mnesia:delete({pubsub_subscription, SubID}), - Sub. +delete_subscription(_JID, _NodeID, SubID) -> + mnesia:delete({pubsub_subscription, SubID}). read_subscription(_JID, _NodeID, SubID) -> - Q = qlc:q([Sub || Sub <- mnesia:table(pubsub_subscription), - Sub#pubsub_subscription.subid == SubID]), - case qlc:e(Q) of - [Sub] -> Sub; - [] -> mnesia:abort({error, notfound}) + case mnesia:read({pubsub_subscription, SubID}) of + [Sub] -> Sub; + _ -> {error, notfound} end. write_subscription(JID, NodeID, SubID, Options) -> - Sub = read_subscription(JID, NodeID, SubID), - mnesia:write(Sub#pubsub_subscription{options = Options}). + case read_subscription(JID, NodeID, SubID) of + {error, notfound} -> {error, notfound}; + Sub -> mnesia:write(Sub#pubsub_subscription{options = Options}) + end. make_subid() -> {T1, T2, T3} = now(),