# HG changeset patch # User Thilo Cestonaro # Date 1277197407 -7200 # Node ID 3bce55e453b1d51e6c1e65b9d9265f3b2e35a094 # Parent 03cb7511ae54a5e9585ce5ba490213830ad3b268# Parent 43d9e0944276cbdc0a2e2a0af5281dd7c2a06fc3 merge diff -r 03cb7511ae54 -r 3bce55e453b1 mod_archive/mod_archive.lua --- a/mod_archive/mod_archive.lua Tue Jun 22 11:00:26 2010 +0200 +++ b/mod_archive/mod_archive.lua Tue Jun 22 11:03:27 2010 +0200 @@ -21,16 +21,48 @@ ------------------------------------------------------------ -- Utils ------------------------------------------------------------ -local function load_prefs(node, host, dir) - return st.deserialize(dm.load(node, host, dir or PREFS_DIR)); +local function load_prefs(node, host) + return st.deserialize(dm.load(node, host, PREFS_DIR)); +end + +local function store_prefs(data, node, host) + dm.store(node, host, PREFS_DIR, st.preserialize(data)); end -local function store_prefs(data, node, host, dir) - dm.store(node, host, dir or PREFS_DIR, st.preserialize(data)); -end - -local function store_msg(data, node, host, dir) - dm.list_append(node, host, dir or ARCHIVE_DIR, st.preserialize(data)); +local function store_msg(msg, node, host, isfrom) + local body = msg:child_with_name("body"); + local thread = msg:child_with_name("thread"); + local data = dm.list_load(node, host, ARCHIVE_DIR); + local tag = (isfrom and "from") or "to"; + if data then + for k, v in ipairs(data) do + -- + -- Art thou not Romeo, and a Montague? + -- Neither, fair saint, if either thee dislike. + -- How cam'st thou hither, tell me, and wherefore? + -- I think she might fancy me. + -- + local collection = st.deserialize(v); + if collection.attr["thread"] == thread:get_text() then + -- TODO figure out secs + collection:tag(tag, {secs='1'}):add_child(body); + local ver = tonumber(collection.attr["version"]) + 1; + collection.attr["version"] = tostring(ver); + data[k] = collection; + dm.list_store(node, host, ARCHIVE_DIR, st.preserialize(data)); + return; + end + end + end + -- not found, create new collection + -- TODO figure out start time + local collection = st.stanza('chat', {with = isfrom and msg.attr.to or msg.attr.from, start='2010-06-01T09:56:15Z', thread=thread:get_text(), version='0'}); + collection:tag(tag, {secs='0'}):add_child(body); + dm.list_append(node, host, ARCHIVE_DIR, st.preserialize(collection)); end ------------------------------------------------------------ @@ -67,7 +99,6 @@ local elem = stanza.tags[1].tags[1]; -- iq:pref:xxx if not elem then return false end -- "default" | "item" | "session" | "method" - -- FIXME there may be many item/session/method sections!! elem.attr["xmlns"] = nil; -- TODO why there is an extra xmlns attr? if elem.name == "default" then local setting = data:child_with_name(elem.name) @@ -251,21 +282,27 @@ -- Message Handler ------------------------------------------------------------ local function msg_handler(data) + -- TODO if not auto_archive_enabled then return nil; 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"); module:log("debug", "-- msg:\n%s", tostring(stanza)); if body then module:log("debug", "-- msg body:\n%s", tostring(body)); - -- module:log("debug", "-- msg body text:\n%s", body:get_text()); - local from_node, from_host = jid.split(stanza.attr.from); - local to_node, to_host = jid.split(stanza.attr.to); - -- FIXME the format of collections - if from_host == "localhost" then -- FIXME only archive messages of users on this host - store_msg(stanza, from_node, from_host); - end - if to_host == "localhost" then - store_msg(stanza, to_node, to_host); + -- TODO mapping messages and conversations to collections if no thread + if thread then + module:log("debug", "-- msg thread:\n%s", tostring(thread)); + -- module:log("debug", "-- msg body text:\n%s", body:get_text()); + local from_node, from_host = jid.split(stanza.attr.from); + local to_node, to_host = jid.split(stanza.attr.to); + -- FIXME only archive messages of users on this host + if from_host == "localhost" then + store_msg(stanza, from_node, from_host, true); + end + if to_host == "localhost" then + store_msg(stanza, to_node, to_host, false); + end end end return nil;