summaryrefslogtreecommitdiffstats
path: root/src/core/ephraim_conn.erl
blob: 9bc6d2fbdf3930c9bfcc8ec32ae4cb280a26aa82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
-module(ephraim_conn).
-include_lib("exmpp/include/exmpp_client.hrl").
-include_lib("exmpp/include/exmpp_xml.hrl").
-behaviour(gen_fsm).
-export([start_link/1]).
-export([init/1, handle_event/3, handle_sync_event/4, handle_info/3, terminate/3, code_change/4]).
-export([connected/2, disconnected/2]).
-export([connect/1, send_packet/2]).

-record(conn_state, {
	  event_manager :: pid(),
	  session       :: any()
	 }).

start_link(EventManager) ->
    gen_fsm:start_link(?MODULE, EventManager, []).

connect(Connection) ->
    gen_fsm:send_event(Connection, connect).
send_packet(Connection, Packet) ->
    gen_fsm:send_event(Connection, {send_packet, Packet}).

%-spec init() -> ok.
init(EventManager) ->
    process_flag(trap_exit, true),

    {ok, disconnected, #conn_state{event_manager=EventManager}}.

disconnected(connect, Data) -> 
    {jid, JID} = ephraim_config:get(jid),
    {password, Password} = ephraim_config:get(password),
    {server, Server, Port} = ephraim_config:get(server),

    Session = exmpp_session:start(),
    exmpp_session:auth_basic_digest(Session, exmpp_jid:parse(JID), Password),
    io:format("Connecting...~n"),
    exmpp_session:connect_TCP(Session, Server, Port),
    io:format("Connected.~n"),

    io:format("Logging in...~n"),
    exmpp_session:login(Session),
    io:format("Getting profile...~n"),
    exmpp_session:send_packet(Session, exmpp_iq:get('jabber:client', #xmlel{ns='vcard-temp',name='vCard'})),
    io:format("Getting roster...~n"),
    exmpp_session:send_packet(Session, exmpp_client_roster:get_roster("foo")),
    io:format("Setting presence...~n"),
    exmpp_session:send_packet(Session, exmpp_presence:set_status(exmpp_presence:available(), "Foo/Test\\Bar")),
    io:format("Ok.~n"),
    {next_state,connected,Data#conn_state{session=Session}};
disconnected({send_packet,_}, _Data) ->
    erlang:error(disconnected).

connected(connect, Data) ->
    {next_state,connected,Data};
connected({send_packet, Packet}, Data) ->
    exmpp_session:send_packet(Data#conn_state.session, Packet),
    {next_state,connected,Data}.

handle_info(Msg = #received_packet{}, State, Data) ->
    advanced_event:notify(Data#conn_state.event_manager, {received_packet,Msg}),
    {next_state, State, Data};
handle_info(Msg, State, Data) ->
    io:format("ephraim_conn: ~p~n", [Msg]),
    {next_state, State, Data}.

handle_event(_Event, _State, _Data) ->
    erlang:error(handle_event).
handle_sync_event(_Event, _From, _State, _Data) ->
    erlang:error(handle_sync_event).

terminate(_Reason, connected, Data) ->
    io:format("Disconnecting...~n"),
    exmpp_session:stop(Data#conn_state.session);
terminate(_Reason, _State, _Data) ->
    ok.

code_change(_OldVersion, State, Data, _Extra) ->
    {ok, State, Data}.