comparison mod_archive/mod_archive.lua @ 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
comparison
equal deleted inserted replaced
735:c1b0f0c33c6a 736:b031831b2ac0
741 return tobool(default.attr['save']) ~= false; 741 return tobool(default.attr['save']) ~= false;
742 end 742 end
743 return AUTO_ARCHIVING_ENABLED; 743 return AUTO_ARCHIVING_ENABLED;
744 end 744 end
745 745
746 local function msg_handler(data) 746 local function msg_handler(data, local_jid, other_jid, isfrom)
747 module:log("debug", "-- Enter msg_handler()"); 747 module:log("debug", "-- Enter msg_handler()");
748 local origin, stanza = data.origin, data.stanza; 748 local origin, stanza = data.origin, data.stanza;
749 local body = stanza:child_with_name("body"); 749 local body = stanza:child_with_name("body");
750 local thread = stanza:child_with_name("thread"); 750 local thread = stanza:child_with_name("thread");
751 if body then 751 if body then
752 local from_node, from_host = jid.split(stanza.attr.from); 752 local local_node, local_host = jid.split(local_jid);
753 local to_node, to_host = jid.split(stanza.attr.to); 753 if hosts[local_host] and um.user_exists(local_node, local_host) and apply_pref(local_node, local_host, other_jid, thread) then
754 if hosts[from_host] and um.user_exists(from_node, from_host) and apply_pref(from_node, from_host, stanza.attr.to, thread) then 754 store_msg(stanza, local_node, local_host, isfrom);
755 store_msg(stanza, from_node, from_host, true);
756 end
757 if hosts[to_host] and um.user_exists(to_node, to_host) and apply_pref(to_node, to_host, stanza.attr.from, thread) then
758 store_msg(stanza, to_node, to_host, false);
759 end 755 end
760 end 756 end
761 757
762 return nil; 758 return nil;
759 end
760
761 local function message_handler(data)
762 msg_handler(data, data.stanza.attr.to, data.stanza.attr.from, false)
763 end
764
765 local function premessage_handler(data)
766 msg_handler(data, data.stanza.attr.from, data.stanza.attr.to, true)
763 end 767 end
764 768
765 -- Preferences 769 -- Preferences
766 module:hook("iq/self/urn:xmpp:archive:pref", preferences_handler); 770 module:hook("iq/self/urn:xmpp:archive:pref", preferences_handler);
767 module:hook("iq/self/urn:xmpp:archive:itemremove", itemremove_handler); 771 module:hook("iq/self/urn:xmpp:archive:itemremove", itemremove_handler);
774 module:hook("iq/self/urn:xmpp:archive:retrieve", retrieve_handler); 778 module:hook("iq/self/urn:xmpp:archive:retrieve", retrieve_handler);
775 module:hook("iq/self/urn:xmpp:archive:remove", remove_handler); 779 module:hook("iq/self/urn:xmpp:archive:remove", remove_handler);
776 -- Replication 780 -- Replication
777 module:hook("iq/self/urn:xmpp:archive:modified", modified_handler); 781 module:hook("iq/self/urn:xmpp:archive:modified", modified_handler);
778 782
779 module:hook("message/full", msg_handler, 10); 783 module:hook("message/full", message_handler, 10);
780 module:hook("message/bare", msg_handler, 10); 784 module:hook("message/bare", message_handler, 10);
781 module:hook("pre-message/full", msg_handler, 10); 785 module:hook("pre-message/full", premessage_handler, 10);
782 module:hook("pre-message/bare", msg_handler, 10); 786 module:hook("pre-message/bare", premessage_handler, 10);
783 787
784 -- TODO exactmatch 788 -- TODO exactmatch
785 -- TODO <item/> JID match 789 -- TODO <item/> JID match
786 -- TODO 'open attr' in removing a collection 790 -- TODO 'open attr' in removing a collection
787 -- TODO save = body/message/stream 791 -- TODO save = body/message/stream