summaryrefslogtreecommitdiffstats
path: root/src/core/ephraim_conn.erl
blob: 7a6ddae5bb8241dfd0f97f466503d3b56330fd94 (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
-module(ephraim_conn).
-compile([debug_info, export_all]).
-include_lib("exmpp/include/exmpp_client.hrl").

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

-spec init() -> ok.
init() ->
    application:start(exmpp),
    Session = exmpp_session:start(),
    JID = exmpp_jid:make("ephraim-test", "jabber.ccc.de", "Bar"),
    exmpp_session:auth_basic_digest(Session, JID, "foobarbla"),
    io:format("Connecting...~n"),
    exmpp_session:connect_TCP(Session, "jabber.ccc.de", 5222),
    io:format("Connected.~n"),
    session(#conn_state{session=Session}).

-spec session(#conn_state{}) -> ok.
session(State) -> 
    io:format("Logging in...~n"),
    exmpp_session:login(State#conn_state.session),
    io:format("Getting roster...~n"),
    exmpp_session:send_packet(State#conn_state.session, exmpp_client_roster:get_roster("foo")),
    io:format("Setting presence...~n"),
    exmpp_session:send_packet(State#conn_state.session, exmpp_presence:set_status(exmpp_presence:available(), "Foo/Test\\Bar")),
    io:format("Ok.~n"),
    loop(State).

-spec loop(#conn_state{}) -> ok.
loop(State) ->
    receive
        stop ->
            exmpp_session:stop(State#conn_state.session);
	
	#received_packet{packet_type=presence, raw_packet=Packet} ->
	    ephraim ! {roster, {presence, Packet}},
            loop(State);
	
        #received_packet{packet_type=message, raw_packet=Packet} ->
	    From = exmpp_xml:get_attribute(Packet, from, <<"unknown">>),
	    HasElement = exmpp_xml:has_element(Packet,'http://jabber.org/protocol/pubsub#event',event),
	    if HasElement ->
		    ephraim ! {receive_event, From, Packet};
	       true ->
		    ephraim ! {receive_message, From, Packet}
	    end,
	    loop(State);
        
	#received_packet{packet_type=iq, raw_packet=Packet} ->
	    IQ = exmpp_iq:xmlel_to_iq(Packet),
	    ephraim ! {receive_iq, IQ},
            loop(State);
	
	{send_packet, Packet} ->
	    exmpp_session:send_packet(State#conn_state.session, Packet),
	    loop(State);
	
        Msg ->
            io:format("ephraim_conn: ~p~n", [Msg]),
            loop(State)
    end.