2002-11-18 21:39:47 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_c2s.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Purpose :
|
|
|
|
%%% Created : 16 Nov 2002 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_c2s).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
-vsn('$Revision$ ').
|
|
|
|
|
|
|
|
-behaviour(gen_fsm).
|
|
|
|
|
|
|
|
%% External exports
|
2003-02-06 20:09:22 +01:00
|
|
|
-export([start/2, receiver/3, sender/2, send_text/2, send_element/2]).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
|
|
|
%% gen_fsm callbacks
|
2002-11-29 21:55:12 +01:00
|
|
|
-export([init/1, wait_for_stream/2, wait_for_auth/2, session_established/2,
|
2003-01-24 21:18:33 +01:00
|
|
|
handle_event/3,
|
|
|
|
handle_sync_event/4,
|
|
|
|
code_change/4,
|
2002-11-30 19:46:16 +01:00
|
|
|
handle_info/3,
|
2002-11-29 21:55:12 +01:00
|
|
|
terminate/3]).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2003-01-02 22:01:12 +01:00
|
|
|
-include("namespaces.hrl").
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2002-12-17 21:49:45 +01:00
|
|
|
-define(SETS, gb_sets).
|
|
|
|
|
2002-12-07 21:27:26 +01:00
|
|
|
-record(state, {socket, sender, receiver, streamid,
|
2003-02-01 21:21:28 +01:00
|
|
|
access,
|
2002-12-17 21:49:45 +01:00
|
|
|
user = "", server = ?MYNAME, resource = "",
|
|
|
|
pres_t = ?SETS:new(),
|
|
|
|
pres_f = ?SETS:new(),
|
|
|
|
pres_a = ?SETS:new(),
|
|
|
|
pres_i = ?SETS:new(),
|
|
|
|
pres_last, pres_pri,
|
|
|
|
pres_timestamp,
|
|
|
|
pres_invis = false}).
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2002-12-17 21:49:45 +01:00
|
|
|
%-define(DBGFSM, true).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
|
|
|
-ifdef(DBGFSM).
|
|
|
|
-define(FSMOPTS, [{debug, [trace]}]).
|
|
|
|
-else.
|
|
|
|
-define(FSMOPTS, []).
|
|
|
|
-endif.
|
|
|
|
|
|
|
|
-define(STREAM_HEADER,
|
2002-11-19 21:58:49 +01:00
|
|
|
"<?xml version='1.0'?>"
|
2002-11-18 21:39:47 +01:00
|
|
|
"<stream:stream xmlns='jabber:client' "
|
|
|
|
"xmlns:stream='http://etherx.jabber.org/streams' "
|
|
|
|
"id='~s' from='~s'>"
|
|
|
|
).
|
|
|
|
|
2002-11-19 21:58:49 +01:00
|
|
|
-define(STREAM_TRAILER, "</stream:stream>").
|
|
|
|
|
|
|
|
-define(INVALID_NS_ERR, "<stream:error>Invalid Namespace</stream:error>").
|
|
|
|
|
2002-11-18 21:39:47 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2003-02-06 20:09:22 +01:00
|
|
|
start(SockData, Opts) ->
|
|
|
|
gen_fsm:start(ejabberd_c2s, [SockData, Opts], ?FSMOPTS).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Callback functions from gen_fsm
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: init/1
|
|
|
|
%% Returns: {ok, StateName, StateData} |
|
|
|
|
%% {ok, StateName, StateData, Timeout} |
|
|
|
|
%% ignore |
|
|
|
|
%% {stop, StopReason}
|
|
|
|
%%----------------------------------------------------------------------
|
2003-02-06 20:09:22 +01:00
|
|
|
init([{SockMod, Socket}, Opts]) ->
|
|
|
|
SenderPid = spawn(?MODULE, sender, [Socket, SockMod]),
|
|
|
|
ReceiverPid = spawn(?MODULE, receiver, [Socket, SockMod, self()]),
|
2003-02-01 21:21:28 +01:00
|
|
|
Access = case lists:keysearch(access, 1, Opts) of
|
|
|
|
{value, {_, A}} ->
|
|
|
|
A;
|
|
|
|
_ ->
|
|
|
|
all
|
|
|
|
end,
|
|
|
|
{ok, wait_for_stream, #state{socket = Socket,
|
2002-11-18 21:39:47 +01:00
|
|
|
receiver = ReceiverPid,
|
2003-02-01 21:21:28 +01:00
|
|
|
sender = SenderPid,
|
|
|
|
streamid = new_id(),
|
|
|
|
access = Access}}.
|
2002-11-18 21:39:47 +01:00
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: StateName/2
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
wait_for_stream({xmlstreamstart, Name, Attrs}, StateData) ->
|
2002-11-19 21:58:49 +01:00
|
|
|
% TODO
|
2002-11-20 21:19:20 +01:00
|
|
|
Header = io_lib:format(?STREAM_HEADER,
|
2002-12-07 21:27:26 +01:00
|
|
|
[StateData#state.streamid, ?MYNAME]),
|
2002-11-19 21:58:49 +01:00
|
|
|
send_text(StateData#state.sender, Header),
|
2002-11-18 21:39:47 +01:00
|
|
|
case lists:keysearch("xmlns:stream", 1, Attrs) of
|
|
|
|
{value, {"xmlns:stream", "http://etherx.jabber.org/streams"}} ->
|
|
|
|
% TODO
|
|
|
|
{next_state, wait_for_auth, StateData};
|
|
|
|
_ ->
|
2002-11-29 21:55:12 +01:00
|
|
|
send_text(StateData#state.sender, ?INVALID_NS_ERR ?STREAM_TRAILER),
|
2002-11-19 21:58:49 +01:00
|
|
|
{stop, normal, StateData}
|
2002-11-18 21:39:47 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
wait_for_stream(closed, StateData) ->
|
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
|
|
|
|
|
|
|
wait_for_auth({xmlstreamelement, El}, StateData) ->
|
2002-11-20 21:19:20 +01:00
|
|
|
case is_auth_packet(El) of
|
2003-01-15 20:54:44 +01:00
|
|
|
{auth, ID, {U, P, D, ""}} ->
|
|
|
|
Err = jlib:make_error_reply(El, "406", "Not Acceptable"),
|
|
|
|
send_element(StateData#state.sender, Err),
|
|
|
|
{next_state, wait_for_auth, StateData};
|
2002-11-23 21:55:05 +01:00
|
|
|
{auth, ID, {U, P, D, R}} ->
|
2002-11-20 21:19:20 +01:00
|
|
|
io:format("AUTH: ~p~n", [{U, P, D, R}]),
|
2003-02-01 21:21:28 +01:00
|
|
|
case acl:match_rule(StateData#state.access, {U, ?MYNAME, R}) of
|
|
|
|
allow ->
|
|
|
|
case ejabberd_auth:check_password(
|
|
|
|
U, P, StateData#state.streamid, D) of
|
|
|
|
true ->
|
|
|
|
ejabberd_sm:open_session(U, R),
|
|
|
|
Res = jlib:make_result_iq_reply(El),
|
|
|
|
send_element(StateData#state.sender, Res),
|
|
|
|
{Fs, Ts} = mod_roster:get_subscription_lists(U),
|
|
|
|
{next_state, session_established,
|
|
|
|
StateData#state{user = U,
|
|
|
|
resource = R,
|
|
|
|
pres_f = ?SETS:from_list(Fs),
|
|
|
|
pres_t = ?SETS:from_list(Ts)}};
|
|
|
|
_ ->
|
|
|
|
Err = jlib:make_error_reply(
|
|
|
|
El, "401", "Unauthorized"),
|
|
|
|
send_element(StateData#state.sender, Err),
|
|
|
|
{next_state, wait_for_auth, StateData}
|
|
|
|
end;
|
2002-11-23 21:55:05 +01:00
|
|
|
_ ->
|
2003-02-01 21:21:28 +01:00
|
|
|
Err = jlib:make_error_reply(El, "405", "Not Allowed"),
|
2002-11-23 21:55:05 +01:00
|
|
|
send_element(StateData#state.sender, Err),
|
|
|
|
{next_state, wait_for_auth, StateData}
|
|
|
|
end;
|
2002-11-20 21:19:20 +01:00
|
|
|
_ ->
|
2002-12-08 18:23:21 +01:00
|
|
|
case jlib:iq_query_info(El) of
|
2003-01-16 21:24:53 +01:00
|
|
|
{iq, ID, Type, ?NS_REGISTER, SubEl} ->
|
2002-12-08 18:23:21 +01:00
|
|
|
ResIQ = mod_register:process_iq(
|
|
|
|
{"", "", ""}, {"", ?MYNAME, ""},
|
2003-01-16 21:24:53 +01:00
|
|
|
{iq, ID, Type, ?NS_REGISTER, SubEl}),
|
2002-12-08 18:23:21 +01:00
|
|
|
Res1 = jlib:replace_from_to({"", ?MYNAME, ""},
|
|
|
|
{"", "", ""},
|
|
|
|
jlib:iq_to_xml(ResIQ)),
|
|
|
|
Res = jlib:remove_attr("to", Res1),
|
|
|
|
send_element(StateData#state.sender, Res),
|
|
|
|
{next_state, wait_for_auth, StateData};
|
|
|
|
_ ->
|
|
|
|
{next_state, wait_for_auth, StateData}
|
|
|
|
end
|
2002-11-27 21:46:29 +01:00
|
|
|
end;
|
2002-11-18 21:39:47 +01:00
|
|
|
|
|
|
|
wait_for_auth({xmlstreamend, Name}, StateData) ->
|
|
|
|
% TODO
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_auth(closed, StateData) ->
|
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
2002-11-27 21:46:29 +01:00
|
|
|
session_established({xmlstreamelement, El}, StateData) ->
|
|
|
|
{xmlelement, Name, Attrs, Els} = El,
|
2002-12-17 21:49:45 +01:00
|
|
|
Server = StateData#state.server,
|
2002-11-30 19:46:16 +01:00
|
|
|
FromJID = {StateData#state.user,
|
2002-12-17 21:49:45 +01:00
|
|
|
Server,
|
2002-11-30 19:46:16 +01:00
|
|
|
StateData#state.resource},
|
|
|
|
To = xml:get_attr_s("to", Attrs),
|
|
|
|
ToJID = case To of
|
|
|
|
"" ->
|
2002-12-17 21:49:45 +01:00
|
|
|
{"", Server, ""};
|
2002-11-30 19:46:16 +01:00
|
|
|
_ ->
|
|
|
|
jlib:string_to_jid(To)
|
|
|
|
end,
|
2002-12-17 21:49:45 +01:00
|
|
|
NewState =
|
|
|
|
case ToJID of
|
|
|
|
error ->
|
|
|
|
% TODO
|
|
|
|
StateData;
|
|
|
|
_ ->
|
|
|
|
case Name of
|
|
|
|
"presence" ->
|
|
|
|
case ToJID of
|
|
|
|
{"", Server, ""} ->
|
|
|
|
?DEBUG("presence_update(~p,~n\t~p,~n\t~p)",
|
|
|
|
[FromJID, El, StateData]),
|
|
|
|
presence_update(FromJID, El, StateData);
|
|
|
|
_ ->
|
2002-12-18 21:26:08 +01:00
|
|
|
presence_track(FromJID, ToJID, El, StateData)
|
2002-12-17 21:49:45 +01:00
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
ejabberd_router:route(FromJID, ToJID, El),
|
|
|
|
StateData
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
{next_state, session_established, NewState};
|
2002-11-27 21:46:29 +01:00
|
|
|
|
2002-12-11 21:57:45 +01:00
|
|
|
session_established({xmlstreamend, Name}, StateData) ->
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2002-11-27 21:46:29 +01:00
|
|
|
session_established(closed, StateData) ->
|
|
|
|
% TODO
|
|
|
|
{stop, normal, StateData}.
|
2002-11-18 21:39:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: StateName/3
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData} |
|
|
|
|
%% {stop, Reason, Reply, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
2003-01-24 21:18:33 +01:00
|
|
|
%state_name(Event, From, StateData) ->
|
|
|
|
% Reply = ok,
|
|
|
|
% {reply, Reply, state_name, StateData}.
|
2002-11-18 21:39:47 +01:00
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_event/3
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
handle_event(Event, StateName, StateData) ->
|
|
|
|
{next_state, StateName, StateData}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_sync_event/4
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData} |
|
|
|
|
%% {stop, Reason, Reply, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
handle_sync_event(Event, From, StateName, StateData) ->
|
|
|
|
Reply = ok,
|
|
|
|
{reply, Reply, StateName, StateData}.
|
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
code_change(OldVsn, StateName, StateData, Extra) ->
|
|
|
|
{ok, StateName, StateData}.
|
|
|
|
|
2002-11-18 21:39:47 +01:00
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_info/3
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
2002-11-30 19:46:16 +01:00
|
|
|
handle_info({send_text, Text}, StateName, StateData) ->
|
|
|
|
send_text(StateData#state.sender, Text),
|
2002-12-17 21:49:45 +01:00
|
|
|
{next_state, StateName, StateData};
|
2003-01-13 21:51:17 +01:00
|
|
|
handle_info(replaced, StateName, StateData) ->
|
|
|
|
% TODO
|
|
|
|
%send_text(StateData#state.sender, Text),
|
|
|
|
{stop, normal, StateData#state{user = ""}};
|
2002-12-17 21:49:45 +01:00
|
|
|
handle_info({route, From, To, Packet}, StateName, StateData) ->
|
|
|
|
{xmlelement, Name, Attrs, Els} = Packet,
|
2002-12-18 21:26:08 +01:00
|
|
|
{FU, FS, FR} = From,
|
2002-12-17 21:49:45 +01:00
|
|
|
{Pass, NewAttrs, NewState} =
|
|
|
|
case Name of
|
|
|
|
"presence" ->
|
|
|
|
case xml:get_attr_s("type", Attrs) of
|
|
|
|
"probe" ->
|
|
|
|
process_presence_probe(From, To, StateData),
|
|
|
|
{false, Attrs, StateData};
|
|
|
|
"error" ->
|
2003-01-17 20:58:42 +01:00
|
|
|
NewA = remove_element(From, StateData#state.pres_a),
|
|
|
|
{true, Attrs, StateData#state{pres_a = NewA}};
|
2002-12-17 21:49:45 +01:00
|
|
|
"invisible" ->
|
|
|
|
Attrs1 = lists:keydelete("type", 1, Attrs),
|
|
|
|
{true, [{"type", "unavailable"} | Attrs1], StateData};
|
|
|
|
"subscribe" ->
|
2002-12-18 21:26:08 +01:00
|
|
|
{true, Attrs, StateData};
|
|
|
|
"subscribed" ->
|
2002-12-17 21:49:45 +01:00
|
|
|
{true, Attrs, StateData};
|
|
|
|
"unsubscribe" ->
|
2002-12-18 21:26:08 +01:00
|
|
|
{true, Attrs, StateData};
|
|
|
|
"unsubscribed" ->
|
2002-12-17 21:49:45 +01:00
|
|
|
{true, Attrs, StateData};
|
|
|
|
_ ->
|
|
|
|
{true, Attrs, StateData}
|
|
|
|
end;
|
2002-12-22 21:56:29 +01:00
|
|
|
"broadcast" ->
|
|
|
|
?DEBUG("broadcast!!!!!!!!!!!~n~p~n", [Els]),
|
|
|
|
NewSt = case Els of
|
|
|
|
[{item, IJID, ISubscription}] ->
|
2003-01-29 18:12:23 +01:00
|
|
|
{false, Attrs,
|
|
|
|
roster_change(IJID, ISubscription,
|
|
|
|
StateData)};
|
|
|
|
[{exit, Reason}] ->
|
|
|
|
{exit, Attrs, Reason};
|
2002-12-22 21:56:29 +01:00
|
|
|
_ ->
|
2003-01-29 18:12:23 +01:00
|
|
|
{false, Attrs, StateData}
|
|
|
|
end;
|
2003-01-02 22:01:12 +01:00
|
|
|
"iq" ->
|
|
|
|
IQ = jlib:iq_query_info(Packet),
|
|
|
|
case IQ of
|
|
|
|
{iq, ID, Type, ?NS_VCARD, SubEl} ->
|
|
|
|
ResIQ = mod_vcard:process_sm_iq(From, To, IQ),
|
|
|
|
ejabberd_router:route(To,
|
|
|
|
From,
|
|
|
|
jlib:iq_to_xml(ResIQ)),
|
|
|
|
{false, Attrs, StateData};
|
2003-01-10 20:44:35 +01:00
|
|
|
%{iq, ID, Type, ?NS_VCARD, SubEl} ->
|
|
|
|
% TODO: don't pass packets until roster loaded
|
|
|
|
%{true, Attrs, StateData};
|
2003-01-02 22:01:12 +01:00
|
|
|
_ ->
|
|
|
|
{true, Attrs, StateData}
|
|
|
|
end;
|
2002-12-17 21:49:45 +01:00
|
|
|
_ ->
|
|
|
|
{true, Attrs, StateData}
|
|
|
|
end,
|
2003-01-29 18:12:23 +01:00
|
|
|
if
|
|
|
|
Pass == exit ->
|
|
|
|
{stop, normal, StateData};
|
|
|
|
Pass ->
|
2002-12-17 21:49:45 +01:00
|
|
|
Attrs2 = jlib:replace_from_to_attrs(jlib:jid_to_string(From),
|
|
|
|
jlib:jid_to_string(To),
|
|
|
|
NewAttrs),
|
|
|
|
Text = xml:element_to_string({xmlelement, Name, Attrs2, Els}),
|
2003-01-29 18:12:23 +01:00
|
|
|
send_text(StateData#state.sender, Text),
|
|
|
|
{next_state, StateName, NewState};
|
|
|
|
true ->
|
|
|
|
{next_state, StateName, NewState}
|
|
|
|
end.
|
2002-11-18 21:39:47 +01:00
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: terminate/3
|
|
|
|
%% Purpose: Shutdown the fsm
|
|
|
|
%% Returns: any
|
|
|
|
%%----------------------------------------------------------------------
|
2002-11-19 21:58:49 +01:00
|
|
|
terminate(Reason, StateName, StateData) ->
|
2002-11-30 19:46:16 +01:00
|
|
|
case StateData#state.user of
|
|
|
|
"" ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
ejabberd_sm:close_session(StateData#state.user,
|
2003-01-03 08:51:17 +01:00
|
|
|
StateData#state.resource),
|
|
|
|
From = {StateData#state.user,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.resource},
|
|
|
|
Packet = {xmlelement, "presence", [{"type", "unavailable"}], []},
|
|
|
|
ejabberd_sm:unset_presence(StateData#state.user,
|
|
|
|
StateData#state.resource),
|
|
|
|
presence_broadcast(From, StateData#state.pres_a, Packet),
|
|
|
|
presence_broadcast(From, StateData#state.pres_i, Packet)
|
2002-11-30 19:46:16 +01:00
|
|
|
end,
|
2002-11-19 21:58:49 +01:00
|
|
|
StateData#state.sender ! close,
|
2002-11-18 21:39:47 +01:00
|
|
|
ok.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2003-02-06 20:09:22 +01:00
|
|
|
receiver(Socket, SockMod, C2SPid) ->
|
2002-11-18 21:39:47 +01:00
|
|
|
XMLStreamPid = xml_stream:start(C2SPid),
|
2003-02-06 20:09:22 +01:00
|
|
|
receiver(Socket, SockMod, C2SPid, XMLStreamPid).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2003-02-06 20:09:22 +01:00
|
|
|
receiver(Socket, SockMod, C2SPid, XMLStreamPid) ->
|
|
|
|
case SockMod:recv(Socket, 0) of
|
2002-11-18 21:39:47 +01:00
|
|
|
{ok, Text} ->
|
|
|
|
xml_stream:send_text(XMLStreamPid, Text),
|
2003-02-06 20:09:22 +01:00
|
|
|
receiver(Socket, SockMod, C2SPid, XMLStreamPid);
|
2003-01-08 21:42:03 +01:00
|
|
|
{error, Reason} ->
|
2002-11-19 21:58:49 +01:00
|
|
|
exit(XMLStreamPid, closed),
|
2002-11-18 21:39:47 +01:00
|
|
|
gen_fsm:send_event(C2SPid, closed),
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2003-02-06 20:09:22 +01:00
|
|
|
sender(Socket, SockMod) ->
|
2002-11-18 21:39:47 +01:00
|
|
|
receive
|
2002-11-30 19:46:16 +01:00
|
|
|
{send_text, Text} ->
|
2003-02-06 20:09:22 +01:00
|
|
|
SockMod:send(Socket,Text),
|
|
|
|
sender(Socket, SockMod);
|
2002-11-19 21:58:49 +01:00
|
|
|
close ->
|
2003-02-06 20:09:22 +01:00
|
|
|
SockMod:close(Socket),
|
2002-11-18 21:39:47 +01:00
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
send_text(Pid, Text) ->
|
2002-11-30 19:46:16 +01:00
|
|
|
Pid ! {send_text, Text}.
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2002-11-23 21:55:05 +01:00
|
|
|
send_element(Pid, El) ->
|
|
|
|
send_text(Pid, xml:element_to_string(El)).
|
|
|
|
|
2002-11-20 21:19:20 +01:00
|
|
|
new_id() ->
|
2002-12-13 21:58:27 +01:00
|
|
|
randoms:get_string().
|
2002-11-20 21:19:20 +01:00
|
|
|
|
|
|
|
|
2003-01-16 21:24:53 +01:00
|
|
|
is_auth_packet(El) ->
|
|
|
|
case jlib:iq_query_info(El) of
|
|
|
|
{iq, ID, Type, ?NS_AUTH, SubEl} ->
|
|
|
|
{xmlelement, _, _, Els} = SubEl,
|
|
|
|
{auth, ID,
|
|
|
|
get_auth_tags(Els, "", "", "", "")};
|
|
|
|
_ ->
|
2002-11-20 21:19:20 +01:00
|
|
|
false
|
2003-01-16 21:24:53 +01:00
|
|
|
end.
|
|
|
|
|
2002-11-20 21:19:20 +01:00
|
|
|
|
|
|
|
get_auth_tags([{xmlelement, Name, Attrs, Els}| L], U, P, D, R) ->
|
|
|
|
CData = xml:get_cdata(Els),
|
|
|
|
case Name of
|
|
|
|
"username" ->
|
|
|
|
get_auth_tags(L, CData, P, D, R);
|
|
|
|
"password" ->
|
|
|
|
get_auth_tags(L, U, CData, D, R);
|
|
|
|
"digest" ->
|
|
|
|
get_auth_tags(L, U, P, CData, R);
|
|
|
|
"resource" ->
|
|
|
|
get_auth_tags(L, U, P, D, CData);
|
|
|
|
_ ->
|
|
|
|
get_auth_tags(L, U, P, D, R)
|
|
|
|
end;
|
|
|
|
get_auth_tags([_ | L], U, P, D, R) ->
|
|
|
|
get_auth_tags(L, U, P, D, R);
|
|
|
|
get_auth_tags([], U, P, D, R) ->
|
|
|
|
{U, P, D, R}.
|
|
|
|
|
|
|
|
|
2002-12-17 21:49:45 +01:00
|
|
|
process_presence_probe(From, To, StateData) ->
|
|
|
|
case StateData#state.pres_last of
|
|
|
|
undefined ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
Cond1 = (not StateData#state.pres_invis)
|
|
|
|
and ?SETS:is_element(From, StateData#state.pres_f)
|
|
|
|
and (not ?SETS:is_element(From, StateData#state.pres_i)),
|
|
|
|
Cond2 = StateData#state.pres_invis
|
|
|
|
and ?SETS:is_element(From, StateData#state.pres_f)
|
|
|
|
and ?SETS:is_element(From, StateData#state.pres_a),
|
|
|
|
if
|
|
|
|
Cond1 ->
|
|
|
|
ejabberd_router:route(To, From,
|
|
|
|
StateData#state.pres_last);
|
|
|
|
Cond2 ->
|
|
|
|
ejabberd_router:route(To, From,
|
|
|
|
{xmlelement, "presence",
|
|
|
|
[],
|
|
|
|
[]});
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
presence_update(From, Packet, StateData) ->
|
|
|
|
{xmlelement, Name, Attrs, Els} = Packet,
|
|
|
|
case xml:get_attr_s("type", Attrs) of
|
|
|
|
"unavailable" ->
|
2002-12-30 21:25:08 +01:00
|
|
|
ejabberd_sm:unset_presence(StateData#state.user,
|
|
|
|
StateData#state.resource),
|
2002-12-17 21:49:45 +01:00
|
|
|
presence_broadcast(From, StateData#state.pres_a, Packet),
|
|
|
|
presence_broadcast(From, StateData#state.pres_i, Packet),
|
|
|
|
StateData#state{pres_last = undefined,
|
|
|
|
pres_a = ?SETS:new(),
|
|
|
|
pres_i = ?SETS:new(),
|
|
|
|
pres_invis = false};
|
|
|
|
"invisible" ->
|
|
|
|
NewState =
|
|
|
|
if
|
|
|
|
not StateData#state.pres_invis ->
|
|
|
|
presence_broadcast(From, StateData#state.pres_a,
|
|
|
|
Packet),
|
|
|
|
presence_broadcast(From, StateData#state.pres_i,
|
|
|
|
Packet),
|
|
|
|
S1 = StateData#state{pres_last = undefined,
|
|
|
|
pres_a = ?SETS:new(),
|
|
|
|
pres_i = ?SETS:new(),
|
2003-01-17 20:58:42 +01:00
|
|
|
pres_invis = true},
|
2002-12-17 21:49:45 +01:00
|
|
|
presence_broadcast_first(From, S1, Packet);
|
|
|
|
true ->
|
|
|
|
StateData
|
|
|
|
end,
|
2003-01-17 20:58:42 +01:00
|
|
|
NewState;
|
2002-12-17 21:49:45 +01:00
|
|
|
"error" ->
|
|
|
|
StateData;
|
2002-12-20 21:42:08 +01:00
|
|
|
"subscribe" ->
|
|
|
|
StateData;
|
|
|
|
"subscribed" ->
|
|
|
|
StateData;
|
|
|
|
"unsubscribe" ->
|
|
|
|
StateData;
|
|
|
|
"unsubscribed" ->
|
|
|
|
StateData;
|
2002-12-17 21:49:45 +01:00
|
|
|
_ ->
|
2003-01-02 22:01:12 +01:00
|
|
|
update_priority(xml:get_subtag(Packet, "priority"), StateData),
|
2002-12-17 21:49:45 +01:00
|
|
|
FromUnavail = (StateData#state.pres_last == undefined) or
|
|
|
|
StateData#state.pres_invis,
|
|
|
|
?DEBUG("from unavail = ~p~n", [FromUnavail]),
|
|
|
|
NewState =
|
|
|
|
if
|
|
|
|
FromUnavail ->
|
|
|
|
% TODO: watching ourself
|
2003-01-05 21:24:59 +01:00
|
|
|
|
|
|
|
mod_offline:resend_offline_messages(
|
|
|
|
StateData#state.user),
|
2003-01-17 20:58:42 +01:00
|
|
|
presence_broadcast_first(
|
|
|
|
From, StateData#state{pres_last = Packet,
|
|
|
|
pres_invis = false
|
|
|
|
}, Packet);
|
2002-12-17 21:49:45 +01:00
|
|
|
true ->
|
|
|
|
presence_broadcast_to_trusted(From,
|
|
|
|
StateData#state.pres_f,
|
|
|
|
StateData#state.pres_a,
|
|
|
|
Packet),
|
2003-01-17 20:58:42 +01:00
|
|
|
StateData#state{pres_last = Packet,
|
|
|
|
pres_invis = false
|
|
|
|
}
|
2002-12-17 21:49:45 +01:00
|
|
|
end,
|
2003-01-17 20:58:42 +01:00
|
|
|
NewState
|
2002-12-17 21:49:45 +01:00
|
|
|
end.
|
|
|
|
|
2002-12-18 21:26:08 +01:00
|
|
|
presence_track(From, To, Packet, StateData) ->
|
|
|
|
{xmlelement, Name, Attrs, Els} = Packet,
|
2002-12-20 21:42:08 +01:00
|
|
|
LTo = jlib:jid_tolower(To),
|
|
|
|
User = StateData#state.user,
|
2002-12-18 21:26:08 +01:00
|
|
|
case xml:get_attr_s("type", Attrs) of
|
|
|
|
"unavailable" ->
|
2002-12-25 20:41:25 +01:00
|
|
|
ejabberd_router:route(From, To, Packet),
|
2002-12-20 21:42:08 +01:00
|
|
|
I = remove_element(LTo, StateData#state.pres_i),
|
|
|
|
A = remove_element(LTo, StateData#state.pres_a),
|
2002-12-18 21:26:08 +01:00
|
|
|
StateData#state{pres_i = I,
|
|
|
|
pres_a = A};
|
|
|
|
"invisible" ->
|
2002-12-25 20:41:25 +01:00
|
|
|
ejabberd_router:route(From, To, Packet),
|
2002-12-20 21:42:08 +01:00
|
|
|
I = ?SETS:add_element(LTo, StateData#state.pres_i),
|
|
|
|
A = remove_element(LTo, StateData#state.pres_a),
|
2002-12-18 21:26:08 +01:00
|
|
|
StateData#state{pres_i = I,
|
|
|
|
pres_a = A};
|
|
|
|
"subscribe" ->
|
2002-12-25 20:41:25 +01:00
|
|
|
ejabberd_router:route(jlib:jid_remove_resource(From), To, Packet),
|
2002-12-20 21:42:08 +01:00
|
|
|
mod_roster:out_subscription(User, To, subscribe),
|
2002-12-18 21:26:08 +01:00
|
|
|
StateData;
|
|
|
|
"subscribed" ->
|
2002-12-25 20:41:25 +01:00
|
|
|
ejabberd_router:route(jlib:jid_remove_resource(From), To, Packet),
|
2002-12-20 21:42:08 +01:00
|
|
|
mod_roster:out_subscription(User, To, subscribed),
|
2002-12-18 21:26:08 +01:00
|
|
|
StateData;
|
|
|
|
"unsubscribe" ->
|
2002-12-25 20:41:25 +01:00
|
|
|
ejabberd_router:route(jlib:jid_remove_resource(From), To, Packet),
|
2002-12-20 21:42:08 +01:00
|
|
|
mod_roster:out_subscription(User, To, unsubscribe),
|
2002-12-18 21:26:08 +01:00
|
|
|
StateData;
|
|
|
|
"unsubscribed" ->
|
2002-12-25 20:41:25 +01:00
|
|
|
ejabberd_router:route(jlib:jid_remove_resource(From), To, Packet),
|
2002-12-20 21:42:08 +01:00
|
|
|
mod_roster:out_subscription(User, To, unsubscribed),
|
|
|
|
StateData;
|
|
|
|
"error" ->
|
2002-12-25 20:41:25 +01:00
|
|
|
ejabberd_router:route(From, To, Packet),
|
2002-12-18 21:26:08 +01:00
|
|
|
StateData;
|
|
|
|
_ ->
|
2002-12-25 20:41:25 +01:00
|
|
|
ejabberd_router:route(From, To, Packet),
|
2002-12-20 21:42:08 +01:00
|
|
|
I = remove_element(LTo, StateData#state.pres_i),
|
|
|
|
A = ?SETS:add_element(LTo, StateData#state.pres_a),
|
2002-12-18 21:26:08 +01:00
|
|
|
StateData#state{pres_i = I,
|
|
|
|
pres_a = A}
|
|
|
|
end.
|
2002-12-17 21:49:45 +01:00
|
|
|
|
|
|
|
presence_broadcast(From, JIDSet, Packet) ->
|
|
|
|
lists:foreach(fun(JID) ->
|
|
|
|
ejabberd_router:route(From, JID, Packet)
|
|
|
|
end, ?SETS:to_list(JIDSet)).
|
|
|
|
|
|
|
|
presence_broadcast_to_trusted(From, T, A, Packet) ->
|
|
|
|
lists:foreach(fun(JID) ->
|
|
|
|
case ?SETS:is_element(JID, T) of
|
|
|
|
true ->
|
|
|
|
ejabberd_router:route(From, JID, Packet);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end, ?SETS:to_list(A)).
|
|
|
|
|
|
|
|
|
|
|
|
presence_broadcast_first(From, StateData, Packet) ->
|
|
|
|
{U, S, _} = From,
|
|
|
|
?SETS:fold(fun(JID, X) ->
|
|
|
|
ejabberd_router:route({U, S, ""}, JID,
|
|
|
|
{xmlelement, "presence",
|
|
|
|
[{"type", "probe"}],
|
|
|
|
[]}),
|
|
|
|
X
|
|
|
|
end,
|
|
|
|
[],
|
|
|
|
StateData#state.pres_t),
|
|
|
|
if
|
|
|
|
StateData#state.pres_invis ->
|
|
|
|
StateData;
|
|
|
|
true ->
|
|
|
|
As = ?SETS:fold(fun(JID, A) ->
|
|
|
|
ejabberd_router:route(From, JID, Packet),
|
|
|
|
?SETS:add_element(JID, A)
|
|
|
|
end,
|
|
|
|
StateData#state.pres_a,
|
|
|
|
StateData#state.pres_f),
|
|
|
|
StateData#state{pres_a = As}
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2002-12-18 21:26:08 +01:00
|
|
|
remove_element(E, Set) ->
|
|
|
|
case ?SETS:is_element(E, Set) of
|
|
|
|
true ->
|
|
|
|
?SETS:del_element(E, Set);
|
|
|
|
_ ->
|
|
|
|
Set
|
|
|
|
end.
|
|
|
|
|
2002-12-17 21:49:45 +01:00
|
|
|
|
2002-12-22 21:56:29 +01:00
|
|
|
roster_change(IJID, ISubscription, StateData) ->
|
|
|
|
LIJID = jlib:jid_tolower(IJID),
|
2003-01-10 20:44:35 +01:00
|
|
|
IsFrom = (ISubscription == both) or (ISubscription == from),
|
|
|
|
IsTo = (ISubscription == both) or (ISubscription == to),
|
|
|
|
FSet = if
|
|
|
|
IsFrom ->
|
|
|
|
?SETS:add_element(LIJID, StateData#state.pres_f);
|
|
|
|
true ->
|
|
|
|
remove_element(LIJID, StateData#state.pres_f)
|
|
|
|
end,
|
|
|
|
TSet = if
|
|
|
|
IsTo ->
|
|
|
|
?SETS:add_element(LIJID, StateData#state.pres_t);
|
|
|
|
true ->
|
|
|
|
remove_element(LIJID, StateData#state.pres_t)
|
|
|
|
end,
|
2002-12-22 21:56:29 +01:00
|
|
|
case StateData#state.pres_last of
|
|
|
|
unknown ->
|
2003-01-10 20:44:35 +01:00
|
|
|
StateData#state{pres_f = FSet, pres_t = TSet};
|
2002-12-22 21:56:29 +01:00
|
|
|
P ->
|
|
|
|
?DEBUG("roster changed for ~p~n", [StateData#state.user]),
|
2003-01-08 21:42:03 +01:00
|
|
|
From = {StateData#state.user,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.resource},
|
2003-01-10 20:44:35 +01:00
|
|
|
Cond1 = (not StateData#state.pres_invis) and IsFrom,
|
|
|
|
Cond2 = (not IsFrom)
|
|
|
|
and (?SETS:is_element(LIJID, StateData#state.pres_a) or
|
|
|
|
?SETS:is_element(LIJID, StateData#state.pres_i)),
|
2002-12-22 21:56:29 +01:00
|
|
|
if
|
|
|
|
Cond1 ->
|
|
|
|
?DEBUG("C1: ~p~n", [LIJID]),
|
|
|
|
ejabberd_router:route(From, IJID, P),
|
|
|
|
A = ?SETS:add_element(LIJID,
|
|
|
|
StateData#state.pres_a),
|
2003-01-10 20:44:35 +01:00
|
|
|
StateData#state{pres_a = A,
|
|
|
|
pres_f = FSet,
|
|
|
|
pres_t = TSet};
|
2002-12-22 21:56:29 +01:00
|
|
|
Cond2 ->
|
|
|
|
?DEBUG("C2: ~p~n", [LIJID]),
|
2003-01-10 20:44:35 +01:00
|
|
|
ejabberd_router:route(From, IJID,
|
|
|
|
{xmlelement, "presence",
|
|
|
|
[{"type", "unavailable"}], []}),
|
2002-12-22 21:56:29 +01:00
|
|
|
I = remove_element(LIJID,
|
|
|
|
StateData#state.pres_i),
|
|
|
|
A = remove_element(LIJID,
|
|
|
|
StateData#state.pres_a),
|
|
|
|
StateData#state{pres_i = I,
|
2003-01-10 20:44:35 +01:00
|
|
|
pres_a = A,
|
|
|
|
pres_f = FSet,
|
|
|
|
pres_t = TSet};
|
2002-12-22 21:56:29 +01:00
|
|
|
true ->
|
2003-01-10 20:44:35 +01:00
|
|
|
StateData#state{pres_f = FSet, pres_t = TSet}
|
2002-12-22 21:56:29 +01:00
|
|
|
end
|
|
|
|
end.
|
2002-11-27 21:46:29 +01:00
|
|
|
|
|
|
|
|
2002-12-30 21:25:08 +01:00
|
|
|
update_priority(El, StateData) ->
|
|
|
|
Pri = case El of
|
|
|
|
false ->
|
|
|
|
0;
|
|
|
|
_ ->
|
|
|
|
case catch list_to_integer(xml:get_tag_cdata(El)) of
|
|
|
|
P when is_integer(P) ->
|
|
|
|
P;
|
|
|
|
_ ->
|
|
|
|
0
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
ejabberd_sm:set_presence(StateData#state.user,
|
|
|
|
StateData#state.resource,
|
|
|
|
Pri).
|
|
|
|
|