Comparison

mod_archive/mod_archive.lua @ 210:2d63d50d9713

mod_archive: Replication is done!
author shinysky<shinysky1986(AT)gmail.com>
date Wed, 14 Jul 2010 16:49:38 +0800
parent 209:254c13e049f7
child 221:1861290055c0
comparison
equal deleted inserted replaced
209:254c13e049f7 210:2d63d50d9713
11 local datetime = require "util.datetime"; 11 local datetime = require "util.datetime";
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 17
17 module:add_feature("urn:xmpp:archive"); 18 module:add_feature("urn:xmpp:archive");
18 module:add_feature("urn:xmpp:archive:auto"); 19 module:add_feature("urn:xmpp:archive:auto");
19 module:add_feature("urn:xmpp:archive:manage"); 20 module:add_feature("urn:xmpp:archive:manage");
20 module:add_feature("urn:xmpp:archive:manual"); 21 module:add_feature("urn:xmpp:archive:manual");
372 local reply = st.reply(stanza):tag('list', {xmlns='urn:xmpp:archive'}); 373 local reply = st.reply(stanza):tag('list', {xmlns='urn:xmpp:archive'});
373 local count = table.getn(resset); 374 local count = table.getn(resset);
374 if count > 0 then 375 if count > 0 then
375 local max = elem.tags[1]:child_with_name("max"); 376 local max = elem.tags[1]:child_with_name("max");
376 if max then 377 if max then
377 max = tonumber(max:get_text()) or 100; 378 max = tonumber(max:get_text()) or DEFAULT_MAX;
378 else max = 100; end 379 else max = DEFAULT_MAX; end
379 local after = elem.tags[1]:child_with_name("after"); 380 local after = elem.tags[1]:child_with_name("after");
380 local before = elem.tags[1]:child_with_name("before"); 381 local before = elem.tags[1]:child_with_name("before");
381 local index = elem.tags[1]:child_with_name("index"); 382 local index = elem.tags[1]:child_with_name("index");
382 local s, e = 1, 1+max; 383 local s, e = 1, 1+max;
383 if after then 384 if after then
456 local reply = st.reply(stanza):tag('chat', collection.attr); 457 local reply = st.reply(stanza):tag('chat', collection.attr);
457 local count = table.getn(resset); 458 local count = table.getn(resset);
458 if count > 0 then 459 if count > 0 then
459 local max = elem.tags[1]:child_with_name("max"); 460 local max = elem.tags[1]:child_with_name("max");
460 if max then 461 if max then
461 max = tonumber(max:get_text()) or 100; 462 max = tonumber(max:get_text()) or DEFAULT_MAX;
462 else max = 100; end 463 else max = DEFAULT_MAX; end
463 local after = elem.tags[1]:child_with_name("after"); 464 local after = elem.tags[1]:child_with_name("after");
464 local before = elem.tags[1]:child_with_name("before"); 465 local before = elem.tags[1]:child_with_name("before");
465 local index = elem.tags[1]:child_with_name("index"); 466 local index = elem.tags[1]:child_with_name("index");
466 local s, e = 1, 1+max; 467 local s, e = 1, 1+max;
467 if after then 468 if after then
542 543
543 ------------------------------------------------------------ 544 ------------------------------------------------------------
544 -- Replication 545 -- Replication
545 ------------------------------------------------------------ 546 ------------------------------------------------------------
546 local function modified_handler(event) 547 local function modified_handler(event)
547 module:log("debug", "-- stanza:\n%s", tostring(event.stanza)); 548 local origin, stanza = event.origin, event.stanza;
549 local node, host = origin.username, origin.host;
550 local data = dm.list_load(node, host, ARCHIVE_DIR);
551 local elem = stanza.tags[1];
552 local resset = {}
553 if data then
554 for k, v in ipairs(data) do
555 local collection = st.deserialize(v);
556 local res = filter_start(elem.attr["start"], collection.attr["access"]);
557 if res then
558 table.insert(resset, collection);
559 end
560 end
561 end
562 local reply = st.reply(stanza):tag('modified', {xmlns='urn:xmpp:archive'});
563 local count = table.getn(resset);
564 if count > 0 then
565 local max = elem.tags[1]:child_with_name("max");
566 if max then
567 max = tonumber(max:get_text()) or DEFAULT_MAX;
568 else max = DEFAULT_MAX; end
569 local after = elem.tags[1]:child_with_name("after");
570 local before = elem.tags[1]:child_with_name("before");
571 local index = elem.tags[1]:child_with_name("index");
572 local s, e = 1, 1+max;
573 if after then
574 after = after:get_text();
575 s = find_coll(resset, after);
576 if not s then -- not found
577 origin.send(st.error_reply(stanza, "cancel", "item-not-found"));
578 return true;
579 end
580 s = s + 1;
581 e = s + max;
582 elseif before then
583 before = before:get_text();
584 if not before or before == '' then -- the last page
585 e = count + 1;
586 s = e - max;
587 else
588 e = find_coll(resset, before);
589 if not e then -- not found
590 origin.send(st.error_reply(stanza, "cancel", "item-not-found"));
591 return true;
592 end
593 s = e - max;
594 end
595 elseif index then
596 s = tonumber(index:get_text()) + 1; -- 0-based
597 e = s + max;
598 end
599 if s < 1 then s = 1; end
600 if e > count + 1 then e = count + 1; end
601 -- Assuming result set is sorted.
602 for i = s, e-1 do
603 if resset[i][1] then
604 reply:add_child(st.stanza('changed', resset[i].attr));
605 else
606 reply:add_child(st.stanza('removed', resset[i].attr));
607 end
608 end
609 local set = st.stanza('set', {xmlns = xmlns_rsm});
610 if s <= e-1 then
611 set:tag('first', {index=s-1}):text(gen_uid(resset[s])):up()
612 :tag('last'):text(gen_uid(resset[e-1])):up();
613 end
614 set:tag('count'):text(tostring(count)):up();
615 reply:add_child(set);
616 end
617 origin.send(reply);
548 return true; 618 return true;
549 end 619 end
550 620
551 ------------------------------------------------------------ 621 ------------------------------------------------------------
552 -- Message Handler 622 -- Message Handler