Skip to content
This repository has been archived by the owner on Nov 7, 2019. It is now read-only.

Commit

Permalink
Added presence handling to echo_client.erl (thanks to Kuleshov Alexan…
Browse files Browse the repository at this point in the history
…der)
  • Loading branch information
badlop committed Sep 18, 2011
1 parent 71f9853 commit c494182
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions examples/echo_client.erl
Expand Up @@ -86,11 +86,16 @@ loop(MySession) ->
Record = #received_packet{packet_type=message, Record = #received_packet{packet_type=message,
raw_packet=Packet, raw_packet=Packet,
type_attr=Type} when Type =/= "error" -> type_attr=Type} when Type =/= "error" ->
io:format("~p~n", [Record]), io:format("Received Message stanza:~n~p~n~n", [Record]),
echo_packet(MySession, Packet), echo_packet(MySession, Packet),
loop(MySession); loop(MySession);
%% If we receive a presence stanza, handle it
Record when Record#received_packet.packet_type == 'presence' ->
io:format("Received Presence stanza:~n~p~n~n", [Record]),
handle_presence(MySession, Record, Record#received_packet.raw_packet),
loop(MySession);
Record -> Record ->
io:format("~p~n", [Record]), io:format("Received a stanza:~n~p~n~n", [Record]),
loop(MySession) loop(MySession)
end. end.


Expand All @@ -102,3 +107,32 @@ echo_packet(MySession, Packet) ->
TmpPacket2 = exmpp_xml:set_attribute(TmpPacket, <<"to">>, From), TmpPacket2 = exmpp_xml:set_attribute(TmpPacket, <<"to">>, From),
NewPacket = exmpp_xml:remove_attribute(TmpPacket2, <<"id">>), NewPacket = exmpp_xml:remove_attribute(TmpPacket2, <<"id">>),
exmpp_session:send_packet(MySession, NewPacket). exmpp_session:send_packet(MySession, NewPacket).

handle_presence(Session, Packet, _Presence) ->
case exmpp_jid:make(_From = Packet#received_packet.from) of
JID ->
case _Type = Packet#received_packet.type_attr of
"available" ->
%% handle presence availabl
ok;
"unavailable" ->
%% handle presence unavailable
ok;
"subscribe" ->
presence_subscribed(Session, JID),
presence_subscribe(Session, JID);
"subscribed" ->
presence_subscribed(Session, JID),
presence_subscribe(Session, JID)
end
end.

presence_subscribed(Session, Recipient) ->
Presence_Subscribed = exmpp_presence:subscribed(),
Presence = exmpp_stanza:set_recipient(Presence_Subscribed, Recipient),
exmpp_session:send_packet(Session, Presence).

presence_subscribe(Session, Recipient) ->
Presence_Subscribe = exmpp_presence:subscribe(),
Presence = exmpp_stanza:set_recipient(Presence_Subscribe, Recipient),
exmpp_session:send_packet(Session, Presence).

0 comments on commit c494182

Please sign in to comment.