[ejabberd] Disconnecting active sessions

Holger Weiß holger at CIS.FU-Berlin.DE
Mon Aug 16 00:10:13 MSD 2010


The following patch implements a disconnect_user/2 function which can be
called via ejabberctl in order to disconnect all active sessions of a
given user.  However, I'm (very) new to ejabberd, so I might well have
overlooked that it provides such a feature already or that it could be
implemented in a better way.

---
 src/ejabberd_c2s.erl |    3 +++
 src/ejabberd_sm.erl  |   18 +++++++++++++++++-
 2 files changed, 20 insertions(+), 1 deletions(-)

diff --git a/src/ejabberd_c2s.erl b/src/ejabberd_c2s.erl
index 3092877..71df505 100644
--- a/src/ejabberd_c2s.erl
+++ b/src/ejabberd_c2s.erl
@@ -1365,6 +1365,9 @@ handle_info({route, From, To, Packet}, StateName, StateData) ->
 handle_info({'DOWN', Monitor, _Type, _Object, _Info}, _StateName, StateData)
   when Monitor == StateData#state.socket_monitor ->
     {stop, normal, StateData};
+handle_info(disconnect, _StateName, StateData) ->
+    send_text(StateData, ?STREAM_TRAILER),
+    {stop, normal, StateData};
 handle_info(system_shutdown, StateName, StateData) ->
     case StateName of
 	wait_for_stream ->
diff --git a/src/ejabberd_sm.erl b/src/ejabberd_sm.erl
index f6d0927..4bd0601 100644
--- a/src/ejabberd_sm.erl
+++ b/src/ejabberd_sm.erl
@@ -55,6 +55,7 @@
 	 connected_users/0,
 	 connected_users_number/0,
 	 user_resources/2,
+	 disconnect_user/2,
 	 get_session_pid/1,
 	 get_user_info/3,
 	 get_user_ip/1,
@@ -833,7 +834,13 @@ commands() ->
 		       desc = "List user's connected resources",
 		       module = ?MODULE, function = user_resources,
 		       args = [{user, string}, {host, string}],
-		       result = {resources, {list, {resource, string}}}}
+		       result = {resources, {list, {resource, string}}}},
+     #ejabberd_commands{name = disconnect_user,
+		       tags = [session],
+		       desc = "Disconnect user's active sessions",
+		       module = ?MODULE, function = disconnect_user,
+		       args = [{user, string}, {host, string}],
+		       result = {num_resources, integer}}
 	].
 
 connected_users() ->
@@ -849,6 +856,15 @@ user_resources(User, Server) ->
                                     list_to_binary(Server)),
     lists:sort(Resources).
 
+disconnect_user(User, Server) ->
+    Resources = get_user_resources(User, Server),
+    lists:map(
+	fun(Resource) ->
+	    SID = get_session_pid(User, Server, Resource),
+	    SID ! disconnect
+	end, Resources),
+    length(Resources).
+
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %%% Update Mnesia tables
-- 
Holger


More information about the ejabberd mailing list