comparison mod_archive/mod_archive.lua @ 221:1861290055c0

mod_archive: interpret preferences when do auto archiving.
author shinysky<shinysky1986(AT)gmail.com>
date Sun, 25 Jul 2010 01:44:57 +0800
parents 2d63d50d9713
children 6e6a08b0531a
comparison
equal deleted inserted replaced
220:263858d40ceb 221:1861290055c0
12 12
13 local PREFS_DIR = "archive_prefs"; 13 local PREFS_DIR = "archive_prefs";
14 local ARCHIVE_DIR = "archive"; 14 local ARCHIVE_DIR = "archive";
15 local xmlns_rsm = "http://jabber.org/protocol/rsm"; 15 local xmlns_rsm = "http://jabber.org/protocol/rsm";
16 local DEFAULT_MAX = 100; 16 local DEFAULT_MAX = 100;
17
18 local FORCE_ARCHIVING = false;
19 local AUTO_ARCHIVING_ENABLED = true;
17 20
18 module:add_feature("urn:xmpp:archive"); 21 module:add_feature("urn:xmpp:archive");
19 module:add_feature("urn:xmpp:archive:auto"); 22 module:add_feature("urn:xmpp:archive:auto");
20 module:add_feature("urn:xmpp:archive:manage"); 23 module:add_feature("urn:xmpp:archive:manage");
21 module:add_feature("urn:xmpp:archive:manual"); 24 module:add_feature("urn:xmpp:archive:manual");
77 return save; 80 return save;
78 end 81 end
79 82
80 local function gen_uid(c) 83 local function gen_uid(c)
81 return c.attr["start"] .. c.attr["with"]; 84 return c.attr["start"] .. c.attr["with"];
85 end
86
87 local function tobool(s)
88 if not s then return nil; end
89 s = s:lower();
90 if s == 'true' or s == '1' then
91 return true;
92 elseif s == 'false' or s == '0' then
93 return false;
94 else
95 return nil;
96 end
82 end 97 end
83 98
84 ------------------------------------------------------------ 99 ------------------------------------------------------------
85 -- Preferences 100 -- Preferences
86 ------------------------------------------------------------ 101 ------------------------------------------------------------
619 end 634 end
620 635
621 ------------------------------------------------------------ 636 ------------------------------------------------------------
622 -- Message Handler 637 -- Message Handler
623 ------------------------------------------------------------ 638 ------------------------------------------------------------
639 local function find_pref(pref, name, k, v, exactmatch)
640 for i, child in ipairs(pref) do
641 if child.name == name then
642 if k and v then
643 if exactmatch and child.attr[k] == v then
644 return child;
645 elseif not exactmatch then
646 if tobool(child.attr['exactmatch']) then
647 if child.attr[k] == v then
648 return child;
649 end
650 elseif filter_with(child.attr[k], v) then
651 return child;
652 end
653 end
654 else
655 return child;
656 end
657 end
658 end
659 return nil;
660 end
661
662 local function apply_pref(node, host, jid, thread)
663 if FORCE_ARCHIVING then return true; end
664
665 local pref = load_prefs(node, host);
666 if not pref then
667 return AUTO_ARCHIVING_ENABLED;
668 end
669 local auto = pref:child_with_name('auto');
670 if not tobool(auto.attr['save']) then
671 return false;
672 end
673 if thread then
674 local child = find_pref(pref, 'session', 'thread', thread, true);
675 if child then
676 return tobool(child.attr['save']) ~= false;
677 end
678 end
679 local child = find_pref(pref, 'item', 'jid', jid, false); -- JID Matching
680 if child then
681 return tobool(child.attr['save']) ~= false;
682 end
683 local default = pref:child_with_name('default');
684 if default then
685 return tobool(default.attr['save']) ~= false;
686 end
687 return AUTO_ARCHIVING_ENABLED;
688 end
689
624 local function msg_handler(data) 690 local function msg_handler(data)
625 -- TODO if not auto_archive_enabled then return nil;
626 module:log("debug", "-- Enter msg_handler()"); 691 module:log("debug", "-- Enter msg_handler()");
627 local origin, stanza = data.origin, data.stanza; 692 local origin, stanza = data.origin, data.stanza;
628 local body = stanza:child_with_name("body"); 693 local body = stanza:child_with_name("body");
629 local thread = stanza:child_with_name("thread"); 694 local thread = stanza:child_with_name("thread");
630 module:log("debug", "-- msg:\n%s", tostring(stanza));
631 if body then 695 if body then
632 module:log("debug", "-- msg body:\n%s", tostring(body)); 696 local from_node, from_host = jid.split(stanza.attr.from);
633 -- TODO mapping messages and conversations to collections if no thread 697 local to_node, to_host = jid.split(stanza.attr.to);
634 if thread then 698 -- FIXME only archive messages of users on this host
635 module:log("debug", "-- msg thread:\n%s", tostring(thread)); 699 if from_host == "localhost" and apply_pref(from_node, from_host, stanza.attr.to, thread) then
636 -- module:log("debug", "-- msg body text:\n%s", body:get_text()); 700 store_msg(stanza, from_node, from_host, true);
637 local from_node, from_host = jid.split(stanza.attr.from); 701 end
638 local to_node, to_host = jid.split(stanza.attr.to); 702 if to_host == "localhost" and apply_pref(to_node, to_host, stanza.attr.from, thread) then
639 -- FIXME only archive messages of users on this host 703 store_msg(stanza, to_node, to_host, false);
640 if from_host == "localhost" then 704 end
641 store_msg(stanza, from_node, from_host, true); 705 end
642 end 706
643 if to_host == "localhost" then
644 store_msg(stanza, to_node, to_host, false);
645 end
646 end
647 end
648 return nil; 707 return nil;
649 end 708 end
650 709
651 -- Preferences 710 -- Preferences
652 module:hook("iq/self/urn:xmpp:archive:pref", preferences_handler); 711 module:hook("iq/self/urn:xmpp:archive:pref", preferences_handler);