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

Retreive connection type in sm_register_connection_hook

SVN Revision: 1194
This commit is contained in:
Christophe Romain 2008-02-15 16:35:32 +00:00
parent def7b0ff90
commit 04d688c1ba
4 changed files with 64 additions and 21 deletions

View File

@ -1,3 +1,10 @@
2008-02-15 Christophe Romain <christophe.romain@process-one.net>
* src/ejabberd_sm.erl: Retreive connection type in
sm_register_connection_hook
* src/ejabberd_c2s.erl: Likewise
* src/ejabberd_auth_anonymous.erl: Likewise
2008-02-14 Badlop <badlop@process-one.net> 2008-02-14 Badlop <badlop@process-one.net>
* src/mod_muc/mod_muc_room.erl: Allow unmoderated rooms (EJAB-437) * src/mod_muc/mod_muc_room.erl: Allow unmoderated rooms (EJAB-437)

View File

@ -33,8 +33,8 @@
is_login_anonymous_enabled/1, is_login_anonymous_enabled/1,
anonymous_user_exist/2, anonymous_user_exist/2,
allow_multiple_connections/1, allow_multiple_connections/1,
register_connection/2, register_connection/3,
unregister_connection/2 unregister_connection/3
]). ]).
@ -141,14 +141,14 @@ remove_connection(SID, LUser, LServer) ->
mnesia:transaction(F). mnesia:transaction(F).
%% Register connection %% Register connection
register_connection(SID, #jid{luser = LUser, lserver = LServer}) -> register_connection(SID, #jid{luser = LUser, lserver = LServer}, _) ->
US = {LUser, LServer}, US = {LUser, LServer},
mnesia:sync_dirty( mnesia:sync_dirty(
fun() -> mnesia:write(#anonymous{us = US, sid=SID}) fun() -> mnesia:write(#anonymous{us = US, sid=SID})
end). end).
%% Remove an anonymous user from the anonymous users table %% Remove an anonymous user from the anonymous users table
unregister_connection(SID, #jid{luser = LUser, lserver = LServer}) -> unregister_connection(SID, #jid{luser = LUser, lserver = LServer}, _) ->
purge_hook(anonymous_user_exist(LUser, LServer), purge_hook(anonymous_user_exist(LUser, LServer),
LUser, LServer), LUser, LServer),
remove_connection(SID, LUser, LServer). remove_connection(SID, LUser, LServer).

View File

@ -85,6 +85,7 @@
pres_timestamp, pres_timestamp,
pres_invis = false, pres_invis = false,
privacy_list = none, privacy_list = none,
conn = unknown,
ip, ip,
lang}). lang}).
@ -437,9 +438,10 @@ wait_for_auth({xmlstreamelement, El}, StateData) ->
[StateData#state.socket, [StateData#state.socket,
jlib:jid_to_string(JID)]), jlib:jid_to_string(JID)]),
SID = {now(), self()}, SID = {now(), self()},
IP = StateData#state.ip, Conn = get_conn_type(StateData),
Info = [{ip, StateData#state.ip}, {conn, Conn}],
ejabberd_sm:open_session( ejabberd_sm:open_session(
SID, U, StateData#state.server, R, IP), SID, U, StateData#state.server, R, Info),
Res1 = jlib:make_result_iq_reply(El), Res1 = jlib:make_result_iq_reply(El),
Res = setelement(4, Res1, []), Res = setelement(4, Res1, []),
send_element(StateData, Res), send_element(StateData, Res),
@ -464,6 +466,7 @@ wait_for_auth({xmlstreamelement, El}, StateData) ->
resource = R, resource = R,
jid = JID, jid = JID,
sid = SID, sid = SID,
conn = Conn,
pres_f = ?SETS:from_list(Fs1), pres_f = ?SETS:from_list(Fs1),
pres_t = ?SETS:from_list(Ts1), pres_t = ?SETS:from_list(Ts1),
privacy_list = PrivList}); privacy_list = PrivList});
@ -785,9 +788,10 @@ wait_for_session({xmlstreamelement, El}, StateData) ->
[StateData#state.socket, [StateData#state.socket,
jlib:jid_to_string(JID)]), jlib:jid_to_string(JID)]),
SID = {now(), self()}, SID = {now(), self()},
IP = StateData#state.ip, Conn = get_conn_type(StateData),
Info = [{ip, StateData#state.ip}, {conn, Conn}],
ejabberd_sm:open_session( ejabberd_sm:open_session(
SID, U, StateData#state.server, R, IP), SID, U, StateData#state.server, R, Info),
Res = jlib:make_result_iq_reply(El), Res = jlib:make_result_iq_reply(El),
send_element(StateData, Res), send_element(StateData, Res),
change_shaper(StateData, JID), change_shaper(StateData, JID),
@ -807,6 +811,7 @@ wait_for_session({xmlstreamelement, El}, StateData) ->
fsm_next_state(session_established, fsm_next_state(session_established,
StateData#state{ StateData#state{
sid = SID, sid = SID,
conn = Conn,
pres_f = ?SETS:from_list(Fs1), pres_f = ?SETS:from_list(Fs1),
pres_t = ?SETS:from_list(Ts1), pres_t = ?SETS:from_list(Ts1),
privacy_list = PrivList}); privacy_list = PrivList});
@ -1368,6 +1373,17 @@ get_auth_tags([_ | L], U, P, D, R) ->
get_auth_tags([], U, P, D, R) -> get_auth_tags([], U, P, D, R) ->
{U, P, D, R}. {U, P, D, R}.
get_conn_type(StateData) ->
case StateData#state.sockmod of
ejabberd_http_poll -> http_poll;
ejabberd_http_bind -> http_bind;
_ ->
case (StateData#state.sockmod):get_sockmod(StateData#state.socket) of
ejabberd_zlib -> c2s_compressed;
tls -> c2s_tls;
gen_tcp -> c2s
end
end.
process_presence_probe(From, To, StateData) -> process_presence_probe(From, To, StateData) ->
LFrom = jlib:jid_tolower(From), LFrom = jlib:jid_tolower(From),
@ -1427,12 +1443,13 @@ presence_update(From, Packet, StateData) ->
StatusTag -> StatusTag ->
xml:get_tag_cdata(StatusTag) xml:get_tag_cdata(StatusTag)
end, end,
Info = [{ip, StateData#state.ip},{conn, StateData#state.conn}],
ejabberd_sm:unset_presence(StateData#state.sid, ejabberd_sm:unset_presence(StateData#state.sid,
StateData#state.user, StateData#state.user,
StateData#state.server, StateData#state.server,
StateData#state.resource, StateData#state.resource,
Status, Status,
StateData#state.ip), Info),
presence_broadcast(StateData, From, StateData#state.pres_a, Packet), presence_broadcast(StateData, From, StateData#state.pres_a, Packet),
presence_broadcast(StateData, From, StateData#state.pres_i, Packet), presence_broadcast(StateData, From, StateData#state.pres_i, Packet),
StateData#state{pres_last = undefined, StateData#state{pres_last = undefined,
@ -1758,13 +1775,14 @@ roster_change(IJID, ISubscription, StateData) ->
update_priority(Priority, Packet, StateData) -> update_priority(Priority, Packet, StateData) ->
Info = [{ip, StateData#state.ip},{conn, StateData#state.conn}],
ejabberd_sm:set_presence(StateData#state.sid, ejabberd_sm:set_presence(StateData#state.sid,
StateData#state.user, StateData#state.user,
StateData#state.server, StateData#state.server,
StateData#state.resource, StateData#state.resource,
Priority, Priority,
Packet, Packet,
StateData#state.ip). Info).
get_priority_from_presence(PresencePacket) -> get_priority_from_presence(PresencePacket) ->
case xml:get_subtag(PresencePacket, "priority") of case xml:get_subtag(PresencePacket, "priority") of

View File

@ -48,6 +48,7 @@
unregister_iq_handler/2, unregister_iq_handler/2,
ctl_process/2, ctl_process/2,
get_session_pid/3, get_session_pid/3,
get_user_info/3,
get_user_ip/3 get_user_ip/3
]). ]).
@ -84,21 +85,25 @@ route(From, To, Packet) ->
ok ok
end. end.
open_session(SID, User, Server, Resource, IP) -> open_session(SID, User, Server, Resource, Info) ->
set_session(SID, User, Server, Resource, undefined, IP), set_session(SID, User, Server, Resource, undefined, Info),
check_for_sessions_to_replace(User, Server, Resource), check_for_sessions_to_replace(User, Server, Resource),
JID = jlib:make_jid(User, Server, Resource), JID = jlib:make_jid(User, Server, Resource),
ejabberd_hooks:run(sm_register_connection_hook, JID#jid.lserver, ejabberd_hooks:run(sm_register_connection_hook, JID#jid.lserver,
[SID, JID]). [SID, JID, Info]).
close_session(SID, User, Server, Resource) -> close_session(SID, User, Server, Resource) ->
Info = case mnesia:dirty_read({session, SID}) of
[] -> [];
[#session{info=I}] -> I
end,
F = fun() -> F = fun() ->
mnesia:delete({session, SID}) mnesia:delete({session, SID})
end, end,
mnesia:sync_dirty(F), mnesia:sync_dirty(F),
JID = jlib:make_jid(User, Server, Resource), JID = jlib:make_jid(User, Server, Resource),
ejabberd_hooks:run(sm_remove_connection_hook, JID#jid.lserver, ejabberd_hooks:run(sm_remove_connection_hook, JID#jid.lserver,
[SID, JID]). [SID, JID, Info]).
check_in_subscription(Acc, User, Server, _JID, _Type, _Reason) -> check_in_subscription(Acc, User, Server, _JID, _Type, _Reason) ->
case ejabberd_auth:is_user_exists(User, Server) of case ejabberd_auth:is_user_exists(User, Server) of
@ -143,14 +148,29 @@ get_user_ip(User, Server, Resource) ->
proplists:get_value(ip, Session#session.info) proplists:get_value(ip, Session#session.info)
end. end.
get_user_info(User, Server, Resource) ->
LUser = jlib:nodeprep(User),
LServer = jlib:nameprep(Server),
LResource = jlib:resourceprep(Resource),
USR = {LUser, LServer, LResource},
case mnesia:dirty_index_read(session, USR, #session.usr) of
[] ->
offline;
Ss ->
Session = lists:max(Ss),
Node = node(element(2, Session#session.sid)),
Conn = proplists:get_value(conn, Session#session.info),
IP = proplists:get_value(ip, Session#session.info),
[{node, Node}, {conn, Conn}, {ip, IP}]
end.
set_presence(SID, User, Server, Resource, Priority, Presence, IP) -> set_presence(SID, User, Server, Resource, Priority, Presence, Info) ->
set_session(SID, User, Server, Resource, Priority, IP), set_session(SID, User, Server, Resource, Priority, Info),
ejabberd_hooks:run(set_presence_hook, jlib:nameprep(Server), ejabberd_hooks:run(set_presence_hook, jlib:nameprep(Server),
[User, Server, Resource, Presence]). [User, Server, Resource, Presence]).
unset_presence(SID, User, Server, Resource, Status, IP) -> unset_presence(SID, User, Server, Resource, Status, Info) ->
set_session(SID, User, Server, Resource, undefined, IP), set_session(SID, User, Server, Resource, undefined, Info),
ejabberd_hooks:run(unset_presence_hook, jlib:nameprep(Server), ejabberd_hooks:run(unset_presence_hook, jlib:nameprep(Server),
[User, Server, Resource, Status]). [User, Server, Resource, Status]).
@ -318,13 +338,12 @@ code_change(_OldVsn, State, _Extra) ->
%%% Internal functions %%% Internal functions
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
set_session(SID, User, Server, Resource, Priority, IP) -> set_session(SID, User, Server, Resource, Priority, Info) ->
LUser = jlib:nodeprep(User), LUser = jlib:nodeprep(User),
LServer = jlib:nameprep(Server), LServer = jlib:nameprep(Server),
LResource = jlib:resourceprep(Resource), LResource = jlib:resourceprep(Resource),
US = {LUser, LServer}, US = {LUser, LServer},
USR = {LUser, LServer, LResource}, USR = {LUser, LServer, LResource},
Info = [{ip, IP}],
F = fun() -> F = fun() ->
mnesia:write(#session{sid = SID, mnesia:write(#session{sid = SID,
usr = USR, usr = USR,
@ -349,7 +368,6 @@ clean_table_from_bad_node(Node) ->
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
do_route(From, To, Packet) -> do_route(From, To, Packet) ->
?DEBUG("session manager~n\tfrom ~p~n\tto ~p~n\tpacket ~P~n", ?DEBUG("session manager~n\tfrom ~p~n\tto ~p~n\tpacket ~P~n",
[From, To, Packet, 8]), [From, To, Packet, 8]),