diff options
Diffstat (limited to 'src/node_flat_odbc.erl')
-rw-r--r-- | src/node_flat_odbc.erl | 180 |
1 files changed, 93 insertions, 87 deletions
diff --git a/src/node_flat_odbc.erl b/src/node_flat_odbc.erl index c3f9c7e26..e3c579382 100644 --- a/src/node_flat_odbc.erl +++ b/src/node_flat_odbc.erl @@ -1,28 +1,27 @@ -%%% ==================================================================== -%%% ``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/. +%%%---------------------------------------------------------------------- +%%% File : node_flat_odbc.erl +%%% Author : Christophe Romain <christophe.romain@process-one.net> +%%% Purpose : Standard PubSub node plugin with ODBC backend +%%% Created : 1 Dec 2007 by Christophe Romain <christophe.romain@process-one.net> %%% %%% -%%% 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. +%%% ejabberd, Copyright (C) 2002-2016 ProcessOne %%% +%%% 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. %%% -%%% The Initial Developer of the Original Code is ProcessOne. -%%% Portions created by ProcessOne are Copyright 2006-2015, ProcessOne -%%% All Rights Reserved.'' -%%% This software is copyright 2006-2015, ProcessOne. +%%% 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. %%% -%%% @copyright 2006-2015 ProcessOne -%%% @author Christophe Romain <christophe.romain@process-one.net> -%%% [http://www.process-one.net/] -%%% @version {@vsn}, {@date} {@time} -%%% @end -%%% ==================================================================== +%%% 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. +%%% +%%%---------------------------------------------------------------------- %%% @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 @@ -46,8 +45,7 @@ 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/6, get_items/2, - get_items/7, get_items/3, get_item/7, + set_state/1, get_items/7, get_items/3, 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]). @@ -58,7 +56,7 @@ encode_host/1]). init(_Host, _ServerHost, _Opts) -> - pubsub_subscription_odbc:init(), + %%pubsub_subscription_odbc:init(), ok. terminate(_Host, _ServerHost) -> @@ -74,7 +72,7 @@ create_node_permission(Host, ServerHost, Node, ParentNode, Owner, Access) -> node_flat: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)), + {_U, _S, _R} = OwnerKey = jid:tolower(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), <<");">>]), @@ -96,10 +94,10 @@ delete_node(Nodes) -> {result, {default, broadcast, Reply}}. subscribe_node(Nidx, Sender, Subscriber, AccessModel, - SendLast, PresenceSubscription, RosterGroup, Options) -> - SubKey = jlib:jid_tolower(Subscriber), - GenKey = jlib:jid_remove_resource(SubKey), - Authorized = jlib:jid_tolower(jlib:jid_remove_resource(Sender)) == GenKey, + SendLast, PresenceSubscription, RosterGroup, _Options) -> + SubKey = jid:tolower(Subscriber), + GenKey = jid:remove_resource(SubKey), + Authorized = jid:tolower(jid:remove_resource(Sender)) == GenKey, {Affiliation, Subscriptions} = select_affiliation_subscriptions(Nidx, GenKey, SubKey), Whitelisted = lists:member(Affiliation, [member, publisher, owner]), PendingSubscription = lists:any(fun @@ -132,23 +130,33 @@ subscribe_node(Nidx, Sender, Subscriber, AccessModel, %% % 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 + %%{result, SubId} = pubsub_subscription_odbc:subscribe_node(Subscriber, Nidx, Options), + {NewSub, SubId} = case Subscriptions of + [{subscribed, Id}|_] -> + {subscribed, Id}; + [] -> + Id = pubsub_subscription_odbc:make_subid(), + Sub = case AccessModel of + authorize -> pending; + _ -> subscribed + end, + update_subscription(Nidx, SubKey, [{Sub, Id} | Subscriptions]), + {Sub, Id} 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}} + {subscribed, never} -> + {result, {default, subscribed, SubId}}; + {subscribed, _} -> + {result, {default, subscribed, SubId, send_last}}; + {_, _} -> + {result, {default, pending, SubId}} end end. unsubscribe_node(Nidx, Sender, Subscriber, SubId) -> - SubKey = jlib:jid_tolower(Subscriber), - GenKey = jlib:jid_remove_resource(SubKey), - Authorized = jlib:jid_tolower(jlib:jid_remove_resource(Sender)) == GenKey, + SubKey = jid:tolower(Subscriber), + GenKey = jid:remove_resource(SubKey), + Authorized = jid:tolower(jid:remove_resource(Sender)) == GenKey, {Affiliation, Subscriptions} = select_affiliation_subscriptions(Nidx, SubKey), SubIdExists = case SubId of <<>> -> false; @@ -201,15 +209,15 @@ unsubscribe_node(Nidx, Sender, Subscriber, SubId) -> delete_subscription(SubKey, Nidx, {Subscription, SubId}, Affiliation, Subscriptions) -> NewSubs = Subscriptions -- [{Subscription, SubId}], - pubsub_subscription_odbc:unsubscribe_node(SubKey, Nidx, SubId), + %%pubsub_subscription_odbc:unsubscribe_node(SubKey, Nidx, SubId), case {Affiliation, NewSubs} of {none, []} -> del_state(Nidx, SubKey); _ -> update_subscription(Nidx, SubKey, NewSubs) end. publish_item(Nidx, Publisher, PublishModel, MaxItems, ItemId, Payload) -> - SubKey = jlib:jid_tolower(Publisher), - GenKey = jlib:jid_remove_resource(SubKey), + SubKey = jid:tolower(Publisher), + GenKey = jid:remove_resource(SubKey), {Affiliation, Subscriptions} = select_affiliation_subscriptions(Nidx, GenKey, SubKey), Subscribed = case PublishModel of subscribers -> node_flat:is_subscribed(Subscriptions); @@ -224,9 +232,9 @@ publish_item(Nidx, Publisher, PublishModel, MaxItems, ItemId, Payload) -> {error, ?ERR_FORBIDDEN}; true -> if MaxItems > 0 -> - PubId = {now(), SubKey}, + PubId = {p1_time_compat:timestamp(), SubKey}, set_item(#pubsub_item{itemid = {ItemId, Nidx}, - creation = {now(), GenKey}, + creation = {p1_time_compat:timestamp(), GenKey}, modification = PubId, payload = Payload}), Items = [ItemId | itemids(Nidx, GenKey) -- [ItemId]], @@ -246,8 +254,8 @@ remove_extra_items(Nidx, MaxItems, ItemIds) -> {result, {NewItems, OldItems}}. delete_item(Nidx, Publisher, PublishModel, ItemId) -> - SubKey = jlib:jid_tolower(Publisher), - GenKey = jlib:jid_remove_resource(SubKey), + SubKey = jid:tolower(Publisher), + GenKey = jid:remove_resource(SubKey), {result, Affiliation} = get_affiliation(Nidx, GenKey), Allowed = Affiliation == publisher orelse Affiliation == owner orelse @@ -266,8 +274,8 @@ delete_item(Nidx, Publisher, PublishModel, ItemId) -> end. purge_node(Nidx, Owner) -> - SubKey = jlib:jid_tolower(Owner), - GenKey = jlib:jid_remove_resource(SubKey), + SubKey = jid:tolower(Owner), + GenKey = jid:remove_resource(SubKey), GenState = get_state(Nidx, GenKey), case GenState of #pubsub_state{affiliation = owner} -> @@ -283,8 +291,8 @@ purge_node(Nidx, Owner) -> end. get_entity_affiliations(Host, Owner) -> - SubKey = jlib:jid_tolower(Owner), - GenKey = jlib:jid_remove_resource(SubKey), + SubKey = jid:tolower(Owner), + GenKey = jid:remove_resource(SubKey), H = encode_host(Host), J = encode_jid(GenKey), Reply = case catch @@ -313,8 +321,8 @@ get_node_affiliations(Nidx) -> {result, Reply}. get_affiliation(Nidx, Owner) -> - SubKey = jlib:jid_tolower(Owner), - GenKey = jlib:jid_remove_resource(SubKey), + SubKey = jid:tolower(Owner), + GenKey = jid:remove_resource(SubKey), J = encode_jid(GenKey), Reply = case catch ejabberd_odbc:sql_query_t([<<"select affiliation from pubsub_state " @@ -328,8 +336,8 @@ get_affiliation(Nidx, Owner) -> {result, Reply}. set_affiliation(Nidx, Owner, Affiliation) -> - SubKey = jlib:jid_tolower(Owner), - GenKey = jlib:jid_remove_resource(SubKey), + SubKey = jid:tolower(Owner), + GenKey = jid:remove_resource(SubKey), {_, Subscriptions} = select_affiliation_subscriptions(Nidx, GenKey), case {Affiliation, Subscriptions} of {none, []} -> del_state(Nidx, GenKey); @@ -337,8 +345,8 @@ set_affiliation(Nidx, Owner, Affiliation) -> end. get_entity_subscriptions(Host, Owner) -> - SubKey = jlib:jid_tolower(Owner), - GenKey = jlib:jid_remove_resource(SubKey), + SubKey = jid:tolower(Owner), + GenKey = jid:remove_resource(SubKey), H = encode_host(Host), SJ = encode_jid(SubKey), GJ = encode_jid(GenKey), @@ -386,8 +394,8 @@ get_entity_subscriptions(Host, Owner) -> } ). get_entity_subscriptions_for_send_last(Host, Owner) -> - SubKey = jlib:jid_tolower(Owner), - GenKey = jlib:jid_remove_resource(SubKey), + SubKey = jid:tolower(Owner), + GenKey = jid:remove_resource(SubKey), H = encode_host(Host), SJ = encode_jid(SubKey), GJ = encode_jid(GenKey), @@ -450,7 +458,7 @@ get_node_subscriptions(Nidx) -> {result, Reply}. get_subscriptions(Nidx, Owner) -> - SubKey = jlib:jid_tolower(Owner), + SubKey = jid:tolower(Owner), J = encode_jid(SubKey), Reply = case catch ejabberd_odbc:sql_query_t([<<"select subscriptions from pubsub_state where " @@ -464,7 +472,7 @@ get_subscriptions(Nidx, Owner) -> {result, Reply}. set_subscriptions(Nidx, Owner, Subscription, SubId) -> - SubKey = jlib:jid_tolower(Owner), + SubKey = jid:tolower(Owner), SubState = get_state_without_itemids(Nidx, SubKey), case {SubId, SubState#pubsub_state.subscriptions} of {_, []} -> @@ -498,14 +506,15 @@ replace_subscription(_, [], Acc) -> Acc; 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, []), +new_subscription(_Nidx, _Owner, Subscription, SubState) -> + %%{result, SubId} = pubsub_subscription_odbc:subscribe_node(Owner, Nidx, []), + SubId = pubsub_subscription_odbc:make_subid(), 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), + %%pubsub_subscription_odbc:unsubscribe_node(SubState#pubsub_state.stateid, Nidx, SubId), NewSubs = [{S, Sid} || {S, Sid} <- SubState#pubsub_state.subscriptions, SubId =/= Sid], @@ -515,7 +524,7 @@ unsub_with_subid(Nidx, SubId, SubState) -> end. get_pending_nodes(Host, Owner) -> - GenKey = jlib:jid_remove_resource(jlib:jid_tolower(Owner)), + GenKey = jid:remove_resource(jid:tolower(Owner)), States = mnesia:match_object(#pubsub_state{stateid = {GenKey, '_'}, affiliation = owner, _ = '_'}), Nidxxs = [Nidx || #pubsub_state{stateid = {_, Nidx}} <- States], @@ -623,18 +632,18 @@ del_state(Nidx, JID) -> J, <<"' and nodeid='">>, Nidx, <<"';">>]), ok. -get_items(Nidx, _From) -> - case catch - 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, []} - end. +%get_items(Nidx, _From) -> +% case catch +% 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, []} +% end. get_items(Nidx, From, none) -> MaxItems = case catch @@ -719,12 +728,9 @@ get_items(Nidx, _From, {result, {[], none}} end. -get_items(Nidx, JID, AccessModel, PresenceSubscription, RosterGroup, SubId) -> - get_items(Nidx, JID, AccessModel, PresenceSubscription, RosterGroup, SubId, none). - get_items(Nidx, JID, AccessModel, PresenceSubscription, RosterGroup, _SubId, RSM) -> - SubKey = jlib:jid_tolower(JID), - GenKey = jlib:jid_remove_resource(SubKey), + SubKey = jid:tolower(JID), + GenKey = jid:remove_resource(SubKey), {Affiliation, Subscriptions} = select_affiliation_subscriptions(Nidx, GenKey, SubKey), Whitelisted = node_flat:can_fetch_item(Affiliation, Subscriptions), if %%SubId == "", ?? -> @@ -781,8 +787,8 @@ get_item(Nidx, ItemId) -> end. get_item(Nidx, ItemId, JID, AccessModel, PresenceSubscription, RosterGroup, _SubId) -> - SubKey = jlib:jid_tolower(JID), - GenKey = jlib:jid_remove_resource(SubKey), + SubKey = jid:tolower(JID), + GenKey = jid:remove_resource(SubKey), {Affiliation, Subscriptions} = select_affiliation_subscriptions(Nidx, GenKey, SubKey), Whitelisted = node_flat:can_fetch_item(Affiliation, Subscriptions), if %%SubId == "", ?? -> @@ -818,7 +824,7 @@ set_item(Item) -> {M, JID} = Item#pubsub_item.modification, P = encode_jid(JID), Payload = Item#pubsub_item.payload, - XML = ejabberd_odbc:escape(str:join([xml:element_to_binary(X) || X<-Payload], <<>>)), + XML = ejabberd_odbc:escape(str:join([fxml:element_to_binary(X) || X<-Payload], <<>>)), S = fun ({T1, T2, T3}) -> str:join([jlib:i2l(T1, 6), jlib:i2l(T2, 6), jlib:i2l(T3, 6)], <<":">>) end, @@ -943,7 +949,7 @@ update_subscription(Nidx, JID, Subscription) -> -> ljid() ). decode_jid(SJID) -> - jlib:jid_tolower(jlib:string_to_jid(SJID)). + jid:tolower(jid:from_string(SJID)). -spec(decode_affiliation/1 :: ( Arg :: binary()) @@ -983,7 +989,7 @@ decode_subscriptions(Subscriptions) -> -> binary() ). encode_jid(JID) -> - ejabberd_odbc:escape(jlib:jid_to_string(JID)). + ejabberd_odbc:escape(jid:to_string(JID)). -spec(encode_host/1 :: ( Host :: host()) @@ -1035,7 +1041,7 @@ raw_to_item(Nidx, [ItemId, SJID, Creation, Modification, XML]) -> [T1, T2, T3] = str:tokens(Str, <<":">>), {jlib:l2i(T1), jlib:l2i(T2), jlib:l2i(T3)} end, - Payload = case xml_stream:parse_element(XML) of + Payload = case fxml_stream:parse_element(XML) of {error, _Reason} -> []; El -> [El] end, |