[ejabberd] Getting the status of connected users

Mickael Remond mickael.remond at erlang-fr.org
Sat Feb 14 20:23:37 MSK 2004


* Alexey Shchepin <alexey at sevcom.net> [2004-02-11 23:48:11 +0200]:

> Hello, Mickael!
> 
> This status is stored only inside state of ejabberd_c2s FSMs
> (#state.pres_last), and it seems the simplest way is to add processing of
> e.g. handle_sync_event(get_presence, ...) to ejabberd_c2s, and then send such
> requests to all c2s processes.

You will find as an attachement a possible patch.
I hope this could be of interest for other people.

The code to walk all ejabberd_c2s process is handle outside of the
ejabberd code itself (You need to get the list of c2s supervisor childs
and then you can ask all users for availability.

Another approach could be to be able to pass a fun to the c2s process,
so that it use the callback when a user change its state. You can then
pass a fun to log user state change or update a database ...

-- 
Mickaël Rémond
 http://www.erlang-projects.org/
-------------- next part --------------
--- ejabberd_c2s.erl	2004-02-14 17:15:39.000000000 +0000
+++ ejabberd_c2s.erl.new	2004-02-14 17:15:07.000000000 +0000
@@ -16,7 +16,8 @@
 -export([start/2,
 	 start_link/2,
 	 send_text/2,
-	 send_element/2]).
+	 send_element/2,
+	 get_presence/1]).
 
 %% gen_fsm callbacks
 -export([init/1,
@@ -90,6 +91,11 @@
 start_link(SockData, Opts) ->
     gen_fsm:start_link(ejabberd_c2s, [SockData, Opts], ?FSMOPTS).
 
+%% Return Username, Resource and presence information
+get_presence(FsmRef) ->
+    gen_fsm:sync_send_all_state_event(FsmRef, {get_presence}, 1000).
+			  
+
 %%%----------------------------------------------------------------------
 %%% Callback functions from gen_fsm
 %%%----------------------------------------------------------------------
@@ -660,6 +666,19 @@
 %%          {stop, Reason, NewStateData}                          |
 %%          {stop, Reason, Reply, NewStateData}                    
 %%----------------------------------------------------------------------
+% handle_sync_event(get_presence, From, StateName, StateData) ->
+%     Reply = {StateName, StateData},
+%     {reply, Reply, StateName, StateData};
+handle_sync_event({get_presence}, _From, StateName, StateData) ->
+    User = StateData#state.user,
+    Status = case StateData#state.pres_last of
+		 {xmlelement,"presence",[],[]} ->
+		     "Available";
+		 {xmlelement,"presence",[],[StatusTag]} ->
+		     xml:get_tag_cdata(StatusTag)
+	     end,
+    Reply    = {User, Status},
+    {reply, Reply, StateName, StateData};
 handle_sync_event(_Event, _From, StateName, StateData) ->
     Reply = ok,
     {reply, Reply, StateName, StateData}.


More information about the ejabberd mailing list