Comparison

mod_archive/mod_archive.lua @ 182:43d9e0944276

mod_archive: now auto archiving is almost done.
author shinysky<shinysky1986(AT)gmail.com>
date Mon, 21 Jun 2010 15:28:57 +0800
parent 178:62f47a93b5b7
child 187:670c99e96c52
comparison
equal deleted inserted replaced
181:15bc93c85a08 182:43d9e0944276
19 module:add_feature("urn:xmpp:archive:pref"); 19 module:add_feature("urn:xmpp:archive:pref");
20 20
21 ------------------------------------------------------------ 21 ------------------------------------------------------------
22 -- Utils 22 -- Utils
23 ------------------------------------------------------------ 23 ------------------------------------------------------------
24 local function load_prefs(node, host, dir) 24 local function load_prefs(node, host)
25 return st.deserialize(dm.load(node, host, dir or PREFS_DIR)); 25 return st.deserialize(dm.load(node, host, PREFS_DIR));
26 end 26 end
27 27
28 local function store_prefs(data, node, host, dir) 28 local function store_prefs(data, node, host)
29 dm.store(node, host, dir or PREFS_DIR, st.preserialize(data)); 29 dm.store(node, host, PREFS_DIR, st.preserialize(data));
30 end 30 end
31 31
32 local function store_msg(data, node, host, dir) 32 local function store_msg(msg, node, host, isfrom)
33 dm.list_append(node, host, dir or ARCHIVE_DIR, st.preserialize(data)); 33 local body = msg:child_with_name("body");
34 local thread = msg:child_with_name("thread");
35 local data = dm.list_load(node, host, ARCHIVE_DIR);
36 local tag = (isfrom and "from") or "to";
37 if data then
38 for k, v in ipairs(data) do
39 -- <chat with='juliet@capulet.com/chamber'
40 -- start='1469-07-21T02:56:15Z'
41 -- thread='damduoeg08'
42 -- subject='She speaks!'
43 -- version='1'>
44 -- <from secs='0'><body>Art thou not Romeo, and a Montague?</body></from>
45 -- <to secs='11'><body>Neither, fair saint, if either thee dislike.</body></to>
46 -- <from secs='7'><body>How cam'st thou hither, tell me, and wherefore?</body></from>
47 -- <note utc='1469-07-21T03:04:35Z'>I think she might fancy me.</note>
48 -- </chat>
49 local collection = st.deserialize(v);
50 if collection.attr["thread"] == thread:get_text() then
51 -- TODO figure out secs
52 collection:tag(tag, {secs='1'}):add_child(body);
53 local ver = tonumber(collection.attr["version"]) + 1;
54 collection.attr["version"] = tostring(ver);
55 data[k] = collection;
56 dm.list_store(node, host, ARCHIVE_DIR, st.preserialize(data));
57 return;
58 end
59 end
60 end
61 -- not found, create new collection
62 -- TODO figure out start time
63 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'});
64 collection:tag(tag, {secs='0'}):add_child(body);
65 dm.list_append(node, host, ARCHIVE_DIR, st.preserialize(collection));
34 end 66 end
35 67
36 ------------------------------------------------------------ 68 ------------------------------------------------------------
37 -- Preferences 69 -- Preferences
38 ------------------------------------------------------------ 70 ------------------------------------------------------------
65 data:tag('auto', {save='false'}):up(); 97 data:tag('auto', {save='false'}):up();
66 end 98 end
67 local elem = stanza.tags[1].tags[1]; -- iq:pref:xxx 99 local elem = stanza.tags[1].tags[1]; -- iq:pref:xxx
68 if not elem then return false end 100 if not elem then return false end
69 -- "default" | "item" | "session" | "method" 101 -- "default" | "item" | "session" | "method"
70 -- FIXME there may be many item/session/method sections!!
71 elem.attr["xmlns"] = nil; -- TODO why there is an extra xmlns attr? 102 elem.attr["xmlns"] = nil; -- TODO why there is an extra xmlns attr?
72 if elem.name == "default" then 103 if elem.name == "default" then
73 local setting = data:child_with_name(elem.name) 104 local setting = data:child_with_name(elem.name)
74 for k, v in pairs(elem.attr) do 105 for k, v in pairs(elem.attr) do
75 setting.attr[k] = v; 106 setting.attr[k] = v;
249 280
250 ------------------------------------------------------------ 281 ------------------------------------------------------------
251 -- Message Handler 282 -- Message Handler
252 ------------------------------------------------------------ 283 ------------------------------------------------------------
253 local function msg_handler(data) 284 local function msg_handler(data)
285 -- TODO if not auto_archive_enabled then return nil;
254 module:log("debug", "-- Enter msg_handler()"); 286 module:log("debug", "-- Enter msg_handler()");
255 local origin, stanza = data.origin, data.stanza; 287 local origin, stanza = data.origin, data.stanza;
256 local body = stanza:child_with_name("body"); 288 local body = stanza:child_with_name("body");
289 local thread = stanza:child_with_name("thread");
257 module:log("debug", "-- msg:\n%s", tostring(stanza)); 290 module:log("debug", "-- msg:\n%s", tostring(stanza));
258 if body then 291 if body then
259 module:log("debug", "-- msg body:\n%s", tostring(body)); 292 module:log("debug", "-- msg body:\n%s", tostring(body));
260 -- module:log("debug", "-- msg body text:\n%s", body:get_text()); 293 -- TODO mapping messages and conversations to collections if no thread
261 local from_node, from_host = jid.split(stanza.attr.from); 294 if thread then
262 local to_node, to_host = jid.split(stanza.attr.to); 295 module:log("debug", "-- msg thread:\n%s", tostring(thread));
263 -- FIXME the format of collections 296 -- module:log("debug", "-- msg body text:\n%s", body:get_text());
264 if from_host == "localhost" then -- FIXME only archive messages of users on this host 297 local from_node, from_host = jid.split(stanza.attr.from);
265 store_msg(stanza, from_node, from_host); 298 local to_node, to_host = jid.split(stanza.attr.to);
266 end 299 -- FIXME only archive messages of users on this host
267 if to_host == "localhost" then 300 if from_host == "localhost" then
268 store_msg(stanza, to_node, to_host); 301 store_msg(stanza, from_node, from_host, true);
302 end
303 if to_host == "localhost" then
304 store_msg(stanza, to_node, to_host, false);
305 end
269 end 306 end
270 end 307 end
271 return nil; 308 return nil;
272 end 309 end
273 310