Comparison

mod_archive/mod_archive.lua @ 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
parent 182:43d9e0944276
child 188:5e8ea3733dc6
comparison
equal deleted inserted replaced
186:ba3837c565c9 187:670c99e96c52
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'}); 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); 64 collection:tag(tag, {secs='0'}):add_child(body);
65 dm.list_append(node, host, ARCHIVE_DIR, st.preserialize(collection)); 65 dm.list_append(node, host, ARCHIVE_DIR, st.preserialize(collection));
66 end 66 end
67 67
68 local function save_result(collection)
69 local save = st.stanza('save', {xmlns='urn:xmpp:archive'});
70 local chat = st.stanza('chat', collection.attr);
71 save:add_child(chat);
72 return save;
73 end
74
68 ------------------------------------------------------------ 75 ------------------------------------------------------------
69 -- Preferences 76 -- Preferences
70 ------------------------------------------------------------ 77 ------------------------------------------------------------
71 local function preferences_handler(event) 78 local function preferences_handler(event)
72 local origin, stanza = event.origin, event.stanza; 79 local origin, stanza = event.origin, event.stanza;
176 end 183 end
177 return true; 184 return true;
178 end 185 end
179 186
180 local function itemremove_handler(event) 187 local function itemremove_handler(event)
188 -- TODO use 'assert' to check imcoming stanza?
189 -- or use pcall() to catch exceptions?
181 local origin, stanza = event.origin, event.stanza; 190 local origin, stanza = event.origin, event.stanza;
182 if stanza.attr.type ~= "set" then 191 if stanza.attr.type ~= "set" then
183 return false; 192 return false;
184 end 193 end
185 local elem = stanza.tags[1].tags[1]; 194 local elem = stanza.tags[1].tags[1];
272 module:log("debug", "-- stanza:\n%s", tostring(event.stanza)); 281 module:log("debug", "-- stanza:\n%s", tostring(event.stanza));
273 return true; 282 return true;
274 end 283 end
275 284
276 local function save_handler(event) 285 local function save_handler(event)
277 module:log("debug", "-- stanza:\n%s", tostring(event.stanza)); 286 local origin, stanza = event.origin, event.stanza;
287 if stanza.attr.type ~= "set" then
288 return false;
289 end
290 local elem = stanza.tags[1].tags[1];
291 if not elem or elem.name ~= "chat" then
292 return false;
293 end
294 local node, host = origin.username, origin.host;
295 local data = dm.list_load(node, host, ARCHIVE_DIR);
296 if data then
297 for k, v in ipairs(data) do
298 local collection = st.deserialize(v);
299 if collection.attr["with"] == elem.attr["with"]
300 and collection.attr["start"] == elem.attr["start"] then
301 -- TODO check if there're duplicates
302 for newchild in elem:children() do
303 if type(newchild) == "table" then
304 collection:add_child(newchild)
305 end
306 end
307 local ver = tonumber(collection.attr["version"]) + 1;
308 collection.attr["version"] = tostring(ver);
309 collection.attr["subject"] = elem.attr["subject"];
310 origin.send(st.reply(stanza):add_child(save_result(collection)));
311 data[k] = collection;
312 dm.list_store(node, host, ARCHIVE_DIR, st.preserialize(data));
313 return true;
314 end
315 end
316 end
317 -- not found, create new collection
318 elem.attr["version"] = "0";
319 origin.send(st.reply(stanza):add_child(save_result(elem)));
320 dm.list_append(node, host, ARCHIVE_DIR, st.preserialize(elem));
321 -- TODO unsuccessful reply
278 return true; 322 return true;
279 end 323 end
280 324
281 ------------------------------------------------------------ 325 ------------------------------------------------------------
282 -- Message Handler 326 -- Message Handler