25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-22 16:20:52 +01:00

* src/mod_privacy.erl: Use hooks instead of direct function calls

* src/ejabberd_c2s.erl: Updated

SVN Revision: 659
This commit is contained in:
Alexey Shchepin 2006-10-05 03:17:41 +00:00
parent 94ec73686e
commit fa7693d355
4 changed files with 197 additions and 187 deletions

View File

@ -1,3 +1,8 @@
2006-10-05 Alexey Shchepin <alexey@sevcom.net>
* src/mod_privacy.erl: Use hooks instead of direct function calls
* src/ejabberd_c2s.erl: Updated
2006-10-01 Alexey Shchepin <alexey@sevcom.net> 2006-10-01 Alexey Shchepin <alexey@sevcom.net>
* src/shaper.erl: Bugfix * src/shaper.erl: Bugfix

View File

@ -390,11 +390,10 @@ wait_for_auth({xmlstreamelement, El}, StateData) ->
Fs1 = [LJID | Fs], Fs1 = [LJID | Fs],
Ts1 = [LJID | Ts], Ts1 = [LJID | Ts],
PrivList = PrivList =
case catch mod_privacy:get_user_list( ejabberd_hooks:run_fold(
U, StateData#state.server) of privacy_get_user_list, StateData#state.server,
{'EXIT', _} -> none; none,
PL -> PL [U, StateData#state.server]),
end,
{next_state, session_established, {next_state, session_established,
StateData#state{user = U, StateData#state{user = U,
resource = R, resource = R,
@ -700,11 +699,10 @@ wait_for_session({xmlstreamelement, El}, StateData) ->
Fs1 = [LJID | Fs], Fs1 = [LJID | Fs],
Ts1 = [LJID | Ts], Ts1 = [LJID | Ts],
PrivList = PrivList =
case catch mod_privacy:get_user_list( ejabberd_hooks:run_fold(
U, StateData#state.server) of privacy_get_user_list, StateData#state.server,
{'EXIT', _} -> none; none,
PL -> PL [U, StateData#state.server]),
end,
{next_state, session_established, {next_state, session_established,
StateData#state{sid = SID, StateData#state{sid = SID,
pres_f = ?SETS:from_list(Fs1), pres_f = ?SETS:from_list(Fs1),
@ -950,15 +948,14 @@ handle_info({route, From, To, Packet}, StateName, StateData) ->
"unsubscribed" -> "unsubscribed" ->
{true, Attrs, StateData}; {true, Attrs, StateData};
_ -> _ ->
%-ifdef(PRIVACY_SUPPORT). case ejabberd_hooks:run_fold(
case catch mod_privacy:check_packet( privacy_check_packet, StateData#state.server,
StateData#state.user, allow,
StateData#state.server, [StateData#state.user,
StateData#state.privacy_list, StateData#state.server,
{From, To, Packet}, StateData#state.privacy_list,
in) of {From, To, Packet},
{'EXIT', _Reason} -> in]) of
{true, Attrs, StateData};
allow -> allow ->
LFrom = jlib:jid_tolower(From), LFrom = jlib:jid_tolower(From),
LBFrom = jlib:jid_remove_resource(LFrom), LBFrom = jlib:jid_remove_resource(LFrom),
@ -994,9 +991,6 @@ handle_info({route, From, To, Packet}, StateName, StateData) ->
deny -> deny ->
{false, Attrs, StateData} {false, Attrs, StateData}
end end
%-elseif.
% {true, Attrs, StateData}
%-endif.
end; end;
"broadcast" -> "broadcast" ->
?DEBUG("broadcast~n~p~n", [Els]), ?DEBUG("broadcast~n~p~n", [Els]),
@ -1008,28 +1002,30 @@ handle_info({route, From, To, Packet}, StateName, StateData) ->
[{exit, Reason}] -> [{exit, Reason}] ->
{exit, Attrs, Reason}; {exit, Attrs, Reason};
[{privacy_list, PrivList, PrivListName}] -> [{privacy_list, PrivList, PrivListName}] ->
case catch mod_privacy:updated_list( case ejabberd_hooks:run_fold(
StateData#state.privacy_list, privacy_updated_list, StateData#state.server,
PrivList) of false,
{'EXIT', _} -> [StateData#state.privacy_list,
{false, Attrs, StateData}; PrivList]) of
NewPL -> false ->
PrivPushIQ = {false, Attrs, StateData};
#iq{type = set, xmlns = ?NS_PRIVACY, NewPL ->
id = "push", PrivPushIQ =
sub_el = [{xmlelement, "query", #iq{type = set, xmlns = ?NS_PRIVACY,
[{"xmlns", ?NS_PRIVACY}], id = "push",
[{xmlelement, "list", sub_el = [{xmlelement, "query",
[{"name", PrivListName}], [{"xmlns", ?NS_PRIVACY}],
[]}]}]}, [{xmlelement, "list",
PrivPushEl = [{"name", PrivListName}],
jlib:replace_from_to( []}]}]},
jlib:jid_remove_resource( PrivPushEl =
StateData#state.jid), jlib:replace_from_to(
StateData#state.jid, jlib:jid_remove_resource(
jlib:iq_to_xml(PrivPushIQ)), StateData#state.jid),
send_element(StateData, PrivPushEl), StateData#state.jid,
{false, Attrs, StateData#state{privacy_list = NewPL}} jlib:iq_to_xml(PrivPushIQ)),
send_element(StateData, PrivPushEl),
{false, Attrs, StateData#state{privacy_list = NewPL}}
end; end;
_ -> _ ->
{false, Attrs, StateData} {false, Attrs, StateData}
@ -1049,16 +1045,15 @@ handle_info({route, From, To, Packet}, StateName, StateData) ->
ejabberd_router:route(To, From, Err) ejabberd_router:route(To, From, Err)
end, end,
{false, Attrs, StateData}; {false, Attrs, StateData};
%-ifdef(PRIVACY_SUPPORT).
#iq{} -> #iq{} ->
case catch mod_privacy:check_packet( case ejabberd_hooks:run_fold(
StateData#state.user, privacy_check_packet, StateData#state.server,
StateData#state.server, allow,
StateData#state.privacy_list, [StateData#state.user,
{From, To, Packet}, StateData#state.server,
in) of StateData#state.privacy_list,
{'EXIT', _Reason} -> {From, To, Packet},
{true, Attrs, StateData}; in]) of
allow -> allow ->
{true, Attrs, StateData}; {true, Attrs, StateData};
deny -> deny ->
@ -1067,26 +1062,23 @@ handle_info({route, From, To, Packet}, StateName, StateData) ->
ejabberd_router:route(To, From, Err), ejabberd_router:route(To, From, Err),
{false, Attrs, StateData} {false, Attrs, StateData}
end; end;
%-endif.
_ -> _ ->
{true, Attrs, StateData} {true, Attrs, StateData}
end; end;
%-ifdef(PRIVACY_SUPPORT).
"message" -> "message" ->
case catch mod_privacy:check_packet( case ejabberd_hooks:run_fold(
StateData#state.user, privacy_check_packet, StateData#state.server,
StateData#state.server, allow,
StateData#state.privacy_list, [StateData#state.user,
{From, To, Packet}, StateData#state.server,
in) of StateData#state.privacy_list,
{'EXIT', _Reason} -> {From, To, Packet},
{true, Attrs, StateData}; in]) of
allow -> allow ->
{true, Attrs, StateData}; {true, Attrs, StateData};
deny -> deny ->
{false, Attrs, StateData} {false, Attrs, StateData}
end; end;
%-endif.
_ -> _ ->
{true, Attrs, StateData} {true, Attrs, StateData}
end, end,
@ -1252,21 +1244,19 @@ process_presence_probe(From, To, StateData) ->
if if
Cond1 -> Cond1 ->
Packet = StateData#state.pres_last, Packet = StateData#state.pres_last,
%-ifdef(PRIVACY_SUPPORT). case ejabberd_hooks:run_fold(
case catch mod_privacy:check_packet( privacy_check_packet, StateData#state.server,
StateData#state.user, allow,
StateData#state.server, [StateData#state.user,
StateData#state.privacy_list, StateData#state.server,
{To, From, Packet}, StateData#state.privacy_list,
out) of {To, From, Packet},
out]) of
deny -> deny ->
ok; ok;
_ -> allow ->
%-endif.
ejabberd_router:route(To, From, Packet) ejabberd_router:route(To, From, Packet)
%-ifdef(PRIVACY_SUPPORT).
end; end;
%-endif.
Cond2 -> Cond2 ->
ejabberd_router:route(To, From, ejabberd_router:route(To, From,
{xmlelement, "presence", {xmlelement, "presence",
@ -1420,21 +1410,19 @@ presence_track(From, To, Packet, StateData) ->
ejabberd_router:route(From, To, Packet), ejabberd_router:route(From, To, Packet),
StateData; StateData;
_ -> _ ->
%-ifdef(PRIVACY_SUPPORT). case ejabberd_hooks:run_fold(
case catch mod_privacy:check_packet( privacy_check_packet, StateData#state.server,
StateData#state.user, allow,
StateData#state.server, [StateData#state.user,
StateData#state.privacy_list, StateData#state.server,
{From, To, Packet}, StateData#state.privacy_list,
out) of {From, To, Packet},
out]) of
deny -> deny ->
ok; ok;
_ -> allow ->
%-endif.
ejabberd_router:route(From, To, Packet) ejabberd_router:route(From, To, Packet)
%-ifdef(PRIVACY_SUPPORT).
end, end,
%-endif.
I = remove_element(LTo, StateData#state.pres_i), I = remove_element(LTo, StateData#state.pres_i),
A = ?SETS:add_element(LTo, StateData#state.pres_a), A = ?SETS:add_element(LTo, StateData#state.pres_a),
StateData#state{pres_i = I, StateData#state{pres_i = I,
@ -1444,21 +1432,19 @@ presence_track(From, To, Packet, StateData) ->
presence_broadcast(StateData, From, JIDSet, Packet) -> presence_broadcast(StateData, From, JIDSet, Packet) ->
lists:foreach(fun(JID) -> lists:foreach(fun(JID) ->
FJID = jlib:make_jid(JID), FJID = jlib:make_jid(JID),
%-ifdef(PRIVACY_SUPPORT). case ejabberd_hooks:run_fold(
case catch mod_privacy:check_packet( privacy_check_packet, StateData#state.server,
StateData#state.user, allow,
StateData#state.server, [StateData#state.user,
StateData#state.privacy_list, StateData#state.server,
{From, FJID, Packet}, StateData#state.privacy_list,
out) of {From, FJID, Packet},
out]) of
deny -> deny ->
ok; ok;
_ -> allow ->
%-endif.
ejabberd_router:route(From, FJID, Packet) ejabberd_router:route(From, FJID, Packet)
%-ifdef(PRIVACY_SUPPORT).
end end
%-endif.
end, ?SETS:to_list(JIDSet)). end, ?SETS:to_list(JIDSet)).
presence_broadcast_to_trusted(StateData, From, T, A, Packet) -> presence_broadcast_to_trusted(StateData, From, T, A, Packet) ->
@ -1467,21 +1453,19 @@ presence_broadcast_to_trusted(StateData, From, T, A, Packet) ->
case ?SETS:is_element(JID, T) of case ?SETS:is_element(JID, T) of
true -> true ->
FJID = jlib:make_jid(JID), FJID = jlib:make_jid(JID),
%-ifdef(PRIVACY_SUPPORT). case ejabberd_hooks:run_fold(
case catch mod_privacy:check_packet( privacy_check_packet, StateData#state.server,
StateData#state.user, allow,
StateData#state.server, [StateData#state.user,
StateData#state.privacy_list, StateData#state.server,
{From, FJID, Packet}, StateData#state.privacy_list,
out) of {From, FJID, Packet},
out]) of
deny -> deny ->
ok; ok;
_ -> allow ->
%-endif.
ejabberd_router:route(From, FJID, Packet) ejabberd_router:route(From, FJID, Packet)
%-ifdef(PRIVACY_SUPPORT).
end; end;
%-endif.
_ -> _ ->
ok ok
end end
@ -1507,21 +1491,19 @@ presence_broadcast_first(From, StateData, Packet) ->
As = ?SETS:fold( As = ?SETS:fold(
fun(JID, A) -> fun(JID, A) ->
FJID = jlib:make_jid(JID), FJID = jlib:make_jid(JID),
%-ifdef(PRIVACY_SUPPORT). case ejabberd_hooks:run_fold(
case catch mod_privacy:check_packet( privacy_check_packet, StateData#state.server,
StateData#state.user, allow,
StateData#state.server, [StateData#state.user,
StateData#state.privacy_list, StateData#state.server,
{From, FJID, Packet}, StateData#state.privacy_list,
out) of {From, FJID, Packet},
out]) of
deny -> deny ->
ok; ok;
_ -> allow ->
%-endif.
ejabberd_router:route(From, FJID, Packet) ejabberd_router:route(From, FJID, Packet)
%-ifdef(PRIVACY_SUPPORT).
end, end,
%-endif.
?SETS:add_element(JID, A) ?SETS:add_element(JID, A)
end, end,
StateData#state.pres_a, StateData#state.pres_a,
@ -1571,15 +1553,17 @@ roster_change(IJID, ISubscription, StateData) ->
if if
Cond1 -> Cond1 ->
?DEBUG("C1: ~p~n", [LIJID]), ?DEBUG("C1: ~p~n", [LIJID]),
case catch mod_privacy:check_packet( case ejabberd_hooks:run_fold(
StateData#state.user, privacy_check_packet, StateData#state.server,
StateData#state.server, allow,
StateData#state.privacy_list, [StateData#state.user,
{From, To, P}, StateData#state.server,
out) of StateData#state.privacy_list,
{From, To, P},
out]) of
deny -> deny ->
ok; ok;
_ -> allow ->
ejabberd_router:route(From, To, P) ejabberd_router:route(From, To, P)
end, end,
A = ?SETS:add_element(LIJID, A = ?SETS:add_element(LIJID,
@ -1591,15 +1575,17 @@ roster_change(IJID, ISubscription, StateData) ->
?DEBUG("C2: ~p~n", [LIJID]), ?DEBUG("C2: ~p~n", [LIJID]),
PU = {xmlelement, "presence", PU = {xmlelement, "presence",
[{"type", "unavailable"}], []}, [{"type", "unavailable"}], []},
case catch mod_privacy:check_packet( case ejabberd_hooks:run_fold(
StateData#state.user, privacy_check_packet, StateData#state.server,
StateData#state.server, allow,
StateData#state.privacy_list, [StateData#state.user,
{From, To, PU}, StateData#state.server,
out) of StateData#state.privacy_list,
{From, To, PU},
out]) of
deny -> deny ->
ok; ok;
_ -> allow ->
ejabberd_router:route(From, To, PU) ejabberd_router:route(From, To, PU)
end, end,
I = remove_element(LIJID, I = remove_element(LIJID,
@ -1642,20 +1628,16 @@ process_privacy_iq(From, To,
{Res, NewStateData} = {Res, NewStateData} =
case Type of case Type of
get -> get ->
case catch R = ejabberd_hooks:run_fold(
mod_privacy:process_iq_get( privacy_iq_get, StateData#state.server,
From, To, IQ, {error, ?ERR_FEATURE_NOT_IMPLEMENTED},
StateData#state.privacy_list) of [From, To, IQ, StateData#state.privacy_list]),
{'EXIT', _} -> {R, StateData};
{{error, ?ERR_FEATURE_NOT_IMPLEMENTED}, StateData};
R -> {R, StateData}
end;
set -> set ->
case catch case ejabberd_hooks:run_fold(
mod_privacy:process_iq_set( privacy_iq_set, StateData#state.server,
From, To, IQ) of {error, ?ERR_FEATURE_NOT_IMPLEMENTED},
{'EXIT', _} -> [From, To, IQ]) of
{{error, ?ERR_FEATURE_NOT_IMPLEMENTED}, StateData};
{result, R, NewPrivList} -> {result, R, NewPrivList} ->
{{result, R}, {{result, R},
StateData#state{privacy_list = NewPrivList}}; StateData#state{privacy_list = NewPrivList}};
@ -1685,14 +1667,14 @@ resend_offline_messages(#state{user = User,
lists:foreach( lists:foreach(
fun({route, fun({route,
From, To, {xmlelement, Name, Attrs, Els} = Packet}) -> From, To, {xmlelement, Name, Attrs, Els} = Packet}) ->
Pass = case catch mod_privacy:check_packet( Pass = case ejabberd_hooks:run_fold(
User, privacy_check_packet, Server,
Server, allow,
PrivList, [User,
{From, To, Packet}, Server,
in) of PrivList,
{'EXIT', _Reason} -> {From, To, Packet},
true; in]) of
allow -> allow ->
true; true;
deny -> deny ->

View File

@ -14,13 +14,12 @@
-export([start/2, stop/1, -export([start/2, stop/1,
process_iq/3, process_iq/3,
process_iq_set/3, process_iq_set/4,
process_iq_get/4, process_iq_get/5,
get_user_list/2, get_user_list/3,
check_packet/5, check_packet/6,
updated_list/2]). updated_list/3]).
%-include_lib("mnemosyne/include/mnemosyne.hrl").
-include("ejabberd.hrl"). -include("ejabberd.hrl").
-include("jlib.hrl"). -include("jlib.hrl").
@ -47,37 +46,38 @@ start(Host, Opts) ->
mnesia:create_table(privacy, [{disc_copies, [node()]}, mnesia:create_table(privacy, [{disc_copies, [node()]},
{attributes, record_info(fields, privacy)}]), {attributes, record_info(fields, privacy)}]),
update_table(), update_table(),
ejabberd_hooks:add(privacy_iq_get, Host,
?MODULE, process_iq_get, 50),
ejabberd_hooks:add(privacy_iq_set, Host,
?MODULE, process_iq_set, 50),
ejabberd_hooks:add(privacy_get_user_list, Host,
?MODULE, get_user_list, 50),
ejabberd_hooks:add(privacy_check_packet, Host,
?MODULE, check_packet, 50),
ejabberd_hooks:add(privacy_updated_list, Host,
?MODULE, updated_list, 50),
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_PRIVACY, gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_PRIVACY,
?MODULE, process_iq, IQDisc). ?MODULE, process_iq, IQDisc).
stop(Host) -> stop(Host) ->
ejabberd_hooks:delete(privacy_iq_get, Host,
?MODULE, process_iq_get, 50),
ejabberd_hooks:delete(privacy_iq_set, Host,
?MODULE, process_iq_set, 50),
ejabberd_hooks:delete(privacy_get_user_list, Host,
?MODULE, get_user_list, 50),
ejabberd_hooks:delete(privacy_check_packet, Host,
?MODULE, check_packet, 50),
ejabberd_hooks:delete(privacy_updated_list, Host,
?MODULE, updated_list, 50),
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_PRIVACY). gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_PRIVACY).
process_iq(From, _To, IQ) -> process_iq(_From, _To, IQ) ->
#iq{type = Type, sub_el = SubEl} = IQ, SubEl = IQ#iq.sub_el,
#jid{lserver = Server} = From, IQ#iq{type = error, sub_el = [SubEl, ?ERR_NOT_ALLOWED]}.
Res =
case ?MYNAME of
Server ->
case Type of
set ->
%process_iq_set(From, To, IQ);
{error, ?ERR_NOT_ALLOWED};
get ->
{error, ?ERR_NOT_ALLOWED}
end;
_ ->
{error, ?ERR_NOT_ALLOWED}
end,
case Res of
{result, IQRes} ->
IQ#iq{type = result, sub_el = IQRes};
{error, Error} ->
IQ#iq{type = error, sub_el = [SubEl, Error]}
end.
process_iq_get(From, _To, #iq{sub_el = SubEl}, process_iq_get(_, From, _To, #iq{sub_el = SubEl},
#userlist{name = Active}) -> #userlist{name = Active}) ->
#jid{luser = LUser, lserver = LServer} = From, #jid{luser = LUser, lserver = LServer} = From,
{xmlelement, _, _, Els} = SubEl, {xmlelement, _, _, Els} = SubEl,
@ -242,7 +242,7 @@ list_to_action(S) ->
process_iq_set(From, _To, #iq{sub_el = SubEl}) -> process_iq_set(_, From, _To, #iq{sub_el = SubEl}) ->
#jid{luser = LUser, lserver = LServer} = From, #jid{luser = LUser, lserver = LServer} = From,
{xmlelement, _, _, Els} = SubEl, {xmlelement, _, _, Els} = SubEl,
case xml:remove_cdata(Els) of case xml:remove_cdata(Els) of
@ -517,7 +517,7 @@ parse_matches1(_Item, [{xmlelement, _, _, _} | _Els]) ->
get_user_list(User, Server) -> get_user_list(_, User, Server) ->
LUser = jlib:nodeprep(User), LUser = jlib:nodeprep(User),
LServer = jlib:nameprep(Server), LServer = jlib:nameprep(Server),
case catch mnesia:dirty_read(privacy, {LUser, LServer}) of case catch mnesia:dirty_read(privacy, {LUser, LServer}) of
@ -541,7 +541,7 @@ get_user_list(User, Server) ->
end. end.
check_packet(User, Server, check_packet(_, User, Server,
#userlist{list = List}, #userlist{list = List},
{From, To, {xmlelement, PName, _, _}}, {From, To, {xmlelement, PName, _, _}},
Dir) -> Dir) ->
@ -662,7 +662,8 @@ is_type_match(Type, Value, JID, Subscription, Groups) ->
end. end.
updated_list(#userlist{name = OldName} = Old, updated_list(_,
#userlist{name = OldName} = Old,
#userlist{name = NewName} = New) -> #userlist{name = NewName} = New) ->
if if
OldName == NewName -> OldName == NewName ->

View File

@ -91,6 +91,28 @@ CREATE INDEX i_vcard_search_lemail ON vcard_search(lemail);
CREATE INDEX i_vcard_search_lorgname ON vcard_search(lorgname); CREATE INDEX i_vcard_search_lorgname ON vcard_search(lorgname);
CREATE INDEX i_vcard_search_lorgunit ON vcard_search(lorgunit); CREATE INDEX i_vcard_search_lorgunit ON vcard_search(lorgunit);
CREATE TABLE privacy_default_list (
username text PRIMARY KEY,
name text NOT NULL
);
CREATE TABLE privacy_list (
username text NOT NULL,
name text NOT NULL,
type character(1) NOT NULL,
value text NOT NULL,
action character(1) NOT NULL,
ord NUMERIC NOT NULL,
match_all boolean NOT NULL,
match_iq boolean NOT NULL,
match_message boolean NOT NULL,
match_presence_in boolean NOT NULL,
match_presence_out boolean NOT NULL
);
CREATE INDEX i_privacy_list_username ON privacy_list USING btree (username);
--- To update from 0.9.8: --- To update from 0.9.8:
-- CREATE SEQUENCE spool_seq_seq; -- CREATE SEQUENCE spool_seq_seq;
-- ALTER TABLE spool ADD COLUMN seq integer; -- ALTER TABLE spool ADD COLUMN seq integer;