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

-spec init() -> ok.
init() ->
    loop().

-spec handleEvent(binary(), #xmlel{}) -> ok.
handleEvent(From, Item) ->
    HasAvatarData = exmpp_xml:has_element(Item, 'urn:xmpp:avatar:data', data),
    HasAvatarMetaData = exmpp_xml:has_element(Item, 'urn:xmpp:avatar:metadata', metadata),
    if HasAvatarData ->
	    Element = exmpp_xml:get_element(Item, 'urn:xmpp:avatar:data', data),
	    Data = base64:decode(exmpp_xml:get_cdata_from_list(Element#xmlel.children)),
	    ephraim ! {roster, {avatar, From, Data}},
	    ok;
       HasAvatarMetaData ->
	    ok;
       true ->
	    io:format("Received unhandled event from ~p:~n~p~n", [From, Item])
    end.

-spec handleEvents(binary(), [#xmlel{}]) -> ok.
handleEvents(_, []) ->
    ok;
handleEvents(From, [Item|Rest]) ->
    handleEvent(From, Item),
    handleEvents(From, Rest).

-spec loop() -> ok.
loop() ->
    receive
        stop ->
	    ok;
	
	{receive_event, From, Packet} ->
	    Event = exmpp_xml:get_element(Packet, 'http://jabber.org/protocol/pubsub#event', event),
	    Items = exmpp_xml:get_element(Event, 'http://jabber.org/protocol/pubsub#event', items),
	    handleEvents(From, Items#xmlel.children),
	    loop();
	
	Msg ->
	    io:format("ephraim_event: ~p~n", [Msg]),
	    loop()
    end.