changeset 187:670c99e96c52

mod_archive: first commit for manual archiving, need polishing
author shinysky<shinysky1986(AT)gmail.com>
date Sun, 27 Jun 2010 14:03:56 +0800
parents ba3837c565c9
children 5e8ea3733dc6
files mod_archive/mod_archive.lua
diffstat 1 files changed, 45 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mod_archive/mod_archive.lua	Tue Jun 22 11:29:48 2010 +0200
+++ b/mod_archive/mod_archive.lua	Sun Jun 27 14:03:56 2010 +0800
@@ -65,6 +65,13 @@
     dm.list_append(node, host, ARCHIVE_DIR, st.preserialize(collection));
 end
 
+local function save_result(collection)
+    local save = st.stanza('save', {xmlns='urn:xmpp:archive'});
+    local chat = st.stanza('chat', collection.attr);
+    save:add_child(chat);
+    return save;
+end 
+
 ------------------------------------------------------------
 -- Preferences
 ------------------------------------------------------------
@@ -178,6 +185,8 @@
 end
 
 local function itemremove_handler(event)
+    -- TODO use 'assert' to check imcoming stanza?
+    -- or use pcall() to catch exceptions?
     local origin, stanza = event.origin, event.stanza;
     if stanza.attr.type ~= "set" then
         return false;
@@ -274,7 +283,42 @@
 end
 
 local function save_handler(event)
-    module:log("debug", "-- stanza:\n%s", tostring(event.stanza));
+    local origin, stanza = event.origin, event.stanza;
+    if stanza.attr.type ~= "set" then
+        return false;
+    end
+    local elem = stanza.tags[1].tags[1];
+    if not elem or elem.name ~= "chat" then
+        return false;
+    end
+    local node, host = origin.username, origin.host;
+	local data = dm.list_load(node, host, ARCHIVE_DIR);
+    if data then
+        for k, v in ipairs(data) do
+            local collection = st.deserialize(v);
+            if collection.attr["with"] == elem.attr["with"]
+                and collection.attr["start"] == elem.attr["start"] then
+                -- TODO check if there're duplicates
+                for newchild in elem:children() do
+                    if type(newchild) == "table" then
+                        collection:add_child(newchild)
+                    end
+                end
+                local ver = tonumber(collection.attr["version"]) + 1;
+                collection.attr["version"] = tostring(ver);
+                collection.attr["subject"] = elem.attr["subject"];
+                origin.send(st.reply(stanza):add_child(save_result(collection)));
+                data[k] = collection;
+                dm.list_store(node, host, ARCHIVE_DIR, st.preserialize(data));
+                return true;
+            end
+        end
+    end
+    -- not found, create new collection
+    elem.attr["version"] = "0";
+    origin.send(st.reply(stanza):add_child(save_result(elem)));
+    dm.list_append(node, host, ARCHIVE_DIR, st.preserialize(elem));
+    -- TODO unsuccessful reply
     return true;
 end