changeset 736:b031831b2ac0

mod_archive: Fix duplicate messages stored Message exchanged between users on the same server would be stored twice. This is because both the message hook and the pre-message hook would store for both users. The solution is to make sure the pre-message hook only store for the 'from' user, and the post message hook only store for the 'to' user.
author Olivier Goffart <ogoffart@woboq.com>
date Wed, 04 Jul 2012 13:58:31 +0200
parents c1b0f0c33c6a
children e4ea03b060ed
files mod_archive/mod_archive.lua
diffstat 1 files changed, 16 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/mod_archive/mod_archive.lua	Wed Jul 04 13:49:57 2012 +0200
+++ b/mod_archive/mod_archive.lua	Wed Jul 04 13:58:31 2012 +0200
@@ -743,25 +743,29 @@
     return AUTO_ARCHIVING_ENABLED;
 end
 
-local function msg_handler(data)
+local function msg_handler(data, local_jid, other_jid, isfrom)
     module:log("debug", "-- Enter msg_handler()");
     local origin, stanza = data.origin, data.stanza;
     local body = stanza:child_with_name("body");
     local thread = stanza:child_with_name("thread");
     if body then
-        local from_node, from_host = jid.split(stanza.attr.from);
-        local to_node, to_host = jid.split(stanza.attr.to);
-        if hosts[from_host] and um.user_exists(from_node, from_host) and apply_pref(from_node, from_host, stanza.attr.to, thread) then
-            store_msg(stanza, from_node, from_host, true);
-        end
-        if hosts[to_host] and um.user_exists(to_node, to_host) and apply_pref(to_node, to_host, stanza.attr.from, thread) then
-            store_msg(stanza, to_node, to_host, false);
+        local local_node, local_host = jid.split(local_jid);
+        if hosts[local_host] and um.user_exists(local_node, local_host) and apply_pref(local_node, local_host, other_jid, thread) then
+            store_msg(stanza, local_node, local_host, isfrom);
         end
     end
 
     return nil;
 end
 
+local function message_handler(data)
+    msg_handler(data, data.stanza.attr.to,  data.stanza.attr.from, false)
+end
+
+local function premessage_handler(data)
+    msg_handler(data, data.stanza.attr.from,  data.stanza.attr.to, true)
+end
+
 -- Preferences
 module:hook("iq/self/urn:xmpp:archive:pref", preferences_handler);
 module:hook("iq/self/urn:xmpp:archive:itemremove", itemremove_handler);
@@ -776,10 +780,10 @@
 -- Replication
 module:hook("iq/self/urn:xmpp:archive:modified", modified_handler);
 
-module:hook("message/full", msg_handler, 10);
-module:hook("message/bare", msg_handler, 10);
-module:hook("pre-message/full", msg_handler, 10);
-module:hook("pre-message/bare", msg_handler, 10);
+module:hook("message/full", message_handler, 10);
+module:hook("message/bare", message_handler, 10);
+module:hook("pre-message/full", premessage_handler, 10);
+module:hook("pre-message/bare", premessage_handler, 10);
 
 -- TODO exactmatch
 -- TODO <item/> JID match