From 890b1c4f600515cb9f78aa818dad815e9fd8c5e3 Mon Sep 17 00:00:00 2001 From: Badlop Date: Tue, 3 Mar 2009 19:26:24 +0000 Subject: [PATCH] Merge 1834 from trunk. * src/mod_privacy.erl: Only run roster_get_jid_info if privacy list has subscription or group (thanks to George Hazan)(EJAB-851). Sort items in privacy list by order before storing (EJAB-848) * src/mod_privacy.hrl: Likewise * src/mod_privacy_odbc.erl: Likewise SVN Revision: 1947 --- ChangeLog | 6 +++++ src/mod_privacy.erl | 53 ++++++++++++++++++++----------------- src/mod_privacy.hrl | 2 +- src/mod_privacy_odbc.erl | 56 ++++++++++++++++++++++------------------ 4 files changed, 68 insertions(+), 49 deletions(-) diff --git a/ChangeLog b/ChangeLog index a390e5b18..8948d28a6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2009-03-03 Badlop + * src/mod_privacy.erl: Only run roster_get_jid_info if privacy + list has subscription or group (thanks to George Hazan)(EJAB-851). + Sort items in privacy list by order before storing (EJAB-848) + * src/mod_privacy.hrl: Likewise + * src/mod_privacy_odbc.erl: Likewise + * src/mod_muc/mod_muc_room.erl: Owner of a password protected room must provide the password, like other participants (EJAB-867) diff --git a/src/mod_privacy.erl b/src/mod_privacy.erl index 5bc30db0c..acd2fb380 100644 --- a/src/mod_privacy.erl +++ b/src/mod_privacy.erl @@ -322,7 +322,8 @@ process_active_set(LUser, LServer, Name) -> [#privacy{lists = Lists}] -> case lists:keysearch(Name, 1, Lists) of {value, {_, List}} -> - {result, [], #userlist{name = Name, list = List}}; + NeedDb = is_list_needdb(List), + {result, [], #userlist{name = Name, list = List, needdb = NeedDb}}; false -> {error, 'item-not-found'} end @@ -414,11 +415,7 @@ parse_items(Els) -> parse_items([], Res) -> %% Sort the items by their 'order' attribute - %% 5 is the position of 'order' attribute in a #listitem tuple - %% This integer can be calculated at runtime with: - %% 2 + length(lists:takewhile(fun(E) -> E =/= order end, - %% record_info(fields, listitem))), - lists:keysort(5, Res); + lists:keysort(#listitem.order, Res); parse_items([El = #xmlel{name = item} | Els], Res) -> Type = exmpp_xml:get_attribute_as_list(El, type, false), Value = exmpp_xml:get_attribute_as_list(El, value, false), @@ -524,6 +521,16 @@ parse_matches1(_Item, [#xmlel{} | _Els]) -> +is_list_needdb(Items) -> + lists:any( + fun(X) -> + case X#listitem.type of + subscription -> true; + group -> true; + _ -> false + end + end, Items). + get_user_list(_, User, Server) when is_binary(User), is_binary(Server) -> try @@ -555,7 +562,7 @@ get_user_list(_, User, Server) check_packet(_, User, Server, - #userlist{list = List}, + #userlist{list = List, needdb = NeedDb}, {From, To, #xmlel{name = PName}}, Dir) when PName =:= message ; @@ -569,37 +576,37 @@ check_packet(_, User, Server, {message, in} -> LJID = jlib:short_prepd_jid(From), {Subscription, Groups} = - ejabberd_hooks:run_fold( - roster_get_jid_info, - Server, - {none, []}, [User, Server, From]), + case NeedDb of + true -> ejabberd_hooks:run_fold(roster_get_jid_info, Server, {none, []}, [User, Server, From]); + false -> {[], []} + end, check_packet_aux(List, message, LJID, Subscription, Groups); {iq, in} -> LJID = jlib:short_prepd_jid(From), {Subscription, Groups} = - ejabberd_hooks:run_fold( - roster_get_jid_info, - Server, - {none, []}, [User, Server, From]), + case NeedDb of + true -> ejabberd_hooks:run_fold(roster_get_jid_info, Server, {none, []}, [User, Server, From]); + false -> {[], []} + end, check_packet_aux(List, iq, LJID, Subscription, Groups); {presence, in} -> LJID = jlib:short_prepd_jid(From), {Subscription, Groups} = - ejabberd_hooks:run_fold( - roster_get_jid_info, - Server, - {none, []}, [User, Server, From]), + case NeedDb of + true -> ejabberd_hooks:run_fold(roster_get_jid_info, Server, {none, []}, [User, Server, From]); + false -> {[], []} + end, check_packet_aux(List, presence_in, LJID, Subscription, Groups); {presence, out} -> LJID = jlib:short_prepd_jid(To), {Subscription, Groups} = - ejabberd_hooks:run_fold( - roster_get_jid_info, - Server, - {none, []}, [User, Server, To]), + case NeedDb of + true -> ejabberd_hooks:run_fold(roster_get_jid_info, Server, {none, []}, [User, Server, From]); + false -> {[], []} + end, check_packet_aux(List, presence_out, LJID, Subscription, Groups); _ -> diff --git a/src/mod_privacy.hrl b/src/mod_privacy.hrl index 24e097cda..c14e6ec4d 100644 --- a/src/mod_privacy.hrl +++ b/src/mod_privacy.hrl @@ -34,4 +34,4 @@ match_presence_out = false }). --record(userlist, {name = none, list = []}). +-record(userlist, {name = none, list = [], needdb = false }). diff --git a/src/mod_privacy_odbc.erl b/src/mod_privacy_odbc.erl index 67c7190ef..90fb41e62 100644 --- a/src/mod_privacy_odbc.erl +++ b/src/mod_privacy_odbc.erl @@ -322,7 +322,8 @@ process_active_set(LUser, LServer, Name) -> "match_presence_in", "match_presence_out"], RItems} -> Items = lists:map(fun raw_to_item/1, RItems), - {result, [], #userlist{name = Name, list = Items}}; + NeedDb = is_list_needdb(Items), + {result, [], #userlist{name = Name, list = Items, needdb = NeedDb}}; _ -> {error, 'internal-server-error'} end; @@ -415,11 +416,7 @@ parse_items(Els) -> parse_items([], Res) -> %% Sort the items by their 'order' attribute - %% 5 is the position of 'order' attribute in a #listitem tuple - %% This integer can be calculated at runtime with: - %% 2 + length(lists:takewhile(fun(E) -> E =/= order end, - %% record_info(fields, listitem))), - lists:keysort(5, Res); + lists:keysort(#listitem.order, Res); parse_items([El = #xmlel{name = item} | Els], Res) -> Type = exmpp_xml:get_attribute_as_list(El, type, false), Value = exmpp_xml:get_attribute_as_list(El, value, false), @@ -523,7 +520,15 @@ parse_matches1(_Item, [#xmlel{} | _Els]) -> - +is_list_needdb(Items) -> + lists:any( + fun(X) -> + case X#listitem.type of + subscription -> true; + group -> true; + _ -> false + end + end, Items). get_user_list(_, User, Server) when is_binary(User), is_binary(Server) -> @@ -540,7 +545,8 @@ get_user_list(_, User, Server) "match_presence_in", "match_presence_out"], RItems} -> Items = lists:map(fun raw_to_item/1, RItems), - #userlist{name = Default, list = Items}; + NeedDb = is_list_needdb(Items), + #userlist{name = Default, list = Items, needdb = NeedDb}; _ -> #userlist{} end; @@ -554,7 +560,7 @@ get_user_list(_, User, Server) check_packet(_, User, Server, - #userlist{list = List}, + #userlist{list = List, needdb = NeedDb}, {From, To, #xmlel{name = PName}}, Dir) when PName =:= message ; @@ -568,37 +574,37 @@ check_packet(_, User, Server, {message, in} -> LJID = jlib:short_prepd_jid(From), {Subscription, Groups} = - ejabberd_hooks:run_fold( - roster_get_jid_info, - Server, - {none, []}, [User, Server, From]), + case NeedDb of + true -> ejabberd_hooks:run_fold(roster_get_jid_info, Server, {none, []}, [User, Server, LJID]); + false -> {[], []} + end, check_packet_aux(List, message, LJID, Subscription, Groups); {iq, in} -> LJID = jlib:short_prepd_jid(From), {Subscription, Groups} = - ejabberd_hooks:run_fold( - roster_get_jid_info, - Server, - {none, []}, [User, Server, From]), + case NeedDb of + true -> ejabberd_hooks:run_fold(roster_get_jid_info, Server, {none, []}, [User, Server, LJID]); + false -> {[], []} + end, check_packet_aux(List, iq, LJID, Subscription, Groups); {presence, in} -> LJID = jlib:short_prepd_jid(From), {Subscription, Groups} = - ejabberd_hooks:run_fold( - roster_get_jid_info, - Server, - {none, []}, [User, Server, From]), + case NeedDb of + true -> ejabberd_hooks:run_fold(roster_get_jid_info, Server, {none, []}, [User, Server, LJID]); + false -> {[], []} + end, check_packet_aux(List, presence_in, LJID, Subscription, Groups); {presence, out} -> LJID = jlib:short_prepd_jid(To), {Subscription, Groups} = - ejabberd_hooks:run_fold( - roster_get_jid_info, - Server, - {none, []}, [User, Server, To]), + case NeedDb of + true -> ejabberd_hooks:run_fold(roster_get_jid_info, jlib:nameprep(Server), {none, []}, [User, Server, LJID]); + false -> {[], []} + end, check_packet_aux(List, presence_out, LJID, Subscription, Groups); _ ->