From 308360fbe0f1ff1701990706c599e6b829199343 Mon Sep 17 00:00:00 2001 From: damencho Date: Tue, 18 Jul 2017 11:11:14 -0500 Subject: [PATCH] Cleans poltergeists table on remove occupant and when muc is destroyed. --- .../prosody-plugins/mod_muc_poltergeist.lua | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/resources/prosody-plugins/mod_muc_poltergeist.lua b/resources/prosody-plugins/mod_muc_poltergeist.lua index ae7b4d37c..d687ea081 100644 --- a/resources/prosody-plugins/mod_muc_poltergeist.lua +++ b/resources/prosody-plugins/mod_muc_poltergeist.lua @@ -66,6 +66,24 @@ function get_username(room, user_id) return poltergeists[room_name][user_id]; end +-- Removes poltergeist values from table +-- @param room the room instance +-- @param nick the user nick +function remove_username(room, nick) + local room_name = jid.node(room.jid); + if (poltergeists[room_name]) then + local user_id_to_remove; + for name,username in pairs(poltergeists[room_name]) do + if (string.sub(username, 0, 8) == nick) then + user_id_to_remove = name; + end + end + if (user_id_to_remove) then + poltergeists[room_name][user_id_to_remove] = nil; + end + end +end + -- if we found that a session for a user with id has a poltergiest already -- created, retrieve its jid and return it to the authentication -- so we can reuse it and we that real user will replace the poltergiest @@ -152,6 +170,7 @@ function remove_poltergeist_occupant(room, nick, ignore) end room:handle_normal_presence( prosody.hosts[poltergeist_component], leave_presence); + remove_username(room, nick); end -- Checks for existance of a poltergeist occupant @@ -185,6 +204,14 @@ module:hook("muc-broadcast-presence", function (event) end end, -100); +-- cleanup room table after room is destroyed +module:hook("muc-room-destroyed",function(event) + local room_name = jid.node(event.room.jid); + if (poltergeists[room_name]) then + poltergeists[room_name] = nil; + end +end); + --- Handles request for creating/managing poltergeists -- @param event the http event, holds the request query -- @return GET response, containing a json with response details