comparison mod_archive/mod_archive.lua @ 752:9bbd99f2057a

mod_archive: cope with <list> requests which don't have a <set> tag
author Oliver Gerlich <oliver.gerlich@gmx.de>
date Mon, 11 Jun 2012 22:32:45 +0200
parents dcb8e7d2c711
children 9d5731af2c27
comparison
equal deleted inserted replaced
710:b0c0acccd7c4 752:9bbd99f2057a
453 end 453 end
454 local reply = st.reply(stanza):tag('list', {xmlns='urn:xmpp:archive'}); 454 local reply = st.reply(stanza):tag('list', {xmlns='urn:xmpp:archive'});
455 local count = table.getn(resset); 455 local count = table.getn(resset);
456 if count > 0 then 456 if count > 0 then
457 list_reverse(resset); 457 list_reverse(resset);
458 local max = elem.tags[1]:child_with_name("max"); 458 local s, e = 1, 1+DEFAULT_MAX;
459 if max then 459 local rsmset = elem:child_with_name("set")
460 max = tonumber(max:get_text()) or DEFAULT_MAX; 460 if rsmset then
461 else max = DEFAULT_MAX; end 461 local max = elem.tags[1]:child_with_name("max");
462 local after = elem.tags[1]:child_with_name("after"); 462 if max then
463 local before = elem.tags[1]:child_with_name("before"); 463 max = tonumber(max:get_text()) or DEFAULT_MAX;
464 local index = elem.tags[1]:child_with_name("index"); 464 else max = DEFAULT_MAX; end
465 local s, e = 1, 1+max; 465 e = 1 + max
466 if after then 466 local after = elem.tags[1]:child_with_name("after");
467 after = after:get_text(); 467 local before = elem.tags[1]:child_with_name("before");
468 s = find_coll(resset, after); 468 local index = elem.tags[1]:child_with_name("index");
469 if not s then -- not found 469 if after then
470 origin.send(st.error_reply(stanza, "cancel", "item-not-found")); 470 after = after:get_text();
471 return true; 471 s = find_coll(resset, after);
472 end 472 if not s then -- not found
473 s = s + 1;
474 e = s + max;
475 elseif before then
476 before = before:get_text();
477 if not before or before == '' then -- the last page
478 e = count + 1;
479 s = e - max;
480 else
481 e = find_coll(resset, before);
482 if not e then -- not found
483 origin.send(st.error_reply(stanza, "cancel", "item-not-found")); 473 origin.send(st.error_reply(stanza, "cancel", "item-not-found"));
484 return true; 474 return true;
485 end 475 end
486 s = e - max; 476 s = s + 1;
487 end 477 e = s + max;
488 elseif index then 478 elseif before then
489 s = tonumber(index:get_text()) + 1; -- 0-based 479 before = before:get_text();
490 e = s + max; 480 if not before or before == '' then -- the last page
481 e = count + 1;
482 s = e - max;
483 else
484 e = find_coll(resset, before);
485 if not e then -- not found
486 origin.send(st.error_reply(stanza, "cancel", "item-not-found"));
487 return true;
488 end
489 s = e - max;
490 end
491 elseif index then
492 s = tonumber(index:get_text()) + 1; -- 0-based
493 e = s + max;
494 end
491 end 495 end
492 if s < 1 then s = 1; end 496 if s < 1 then s = 1; end
493 if e > count + 1 then e = count + 1; end 497 if e > count + 1 then e = count + 1; end
494 for i = s, e-1 do 498 for i = s, e-1 do
495 reply:add_child(st.stanza('chat', resset[i].attr)); 499 reply:add_child(st.stanza('chat', resset[i].attr));
536 end 540 end
537 collection.attr['xmlns'] = 'urn:xmpp:archive'; 541 collection.attr['xmlns'] = 'urn:xmpp:archive';
538 local reply = st.reply(stanza):tag('chat', collection.attr); 542 local reply = st.reply(stanza):tag('chat', collection.attr);
539 local count = table.getn(resset); 543 local count = table.getn(resset);
540 if count > 0 then 544 if count > 0 then
541 local max = elem.tags[1]:child_with_name("max"); 545 local s, e = 1, 1+DEFAULT_MAX;
542 if max then 546 local rsmset = elem:child_with_name("set")
543 max = tonumber(max:get_text()) or DEFAULT_MAX; 547 if rsmset then
544 else max = DEFAULT_MAX; end 548 local max = elem.tags[1]:child_with_name("max");
545 local after = elem.tags[1]:child_with_name("after"); 549 if max then
546 local before = elem.tags[1]:child_with_name("before"); 550 max = tonumber(max:get_text()) or DEFAULT_MAX;
547 local index = elem.tags[1]:child_with_name("index"); 551 else max = DEFAULT_MAX; end
548 local s, e = 1, 1+max; 552 e = 1+max
549 if after then 553 local after = elem.tags[1]:child_with_name("after");
550 after = tonumber(after:get_text()); 554 local before = elem.tags[1]:child_with_name("before");
551 if not after or after < 1 or after > count then -- not found 555 local index = elem.tags[1]:child_with_name("index");
552 origin.send(st.error_reply(stanza, "cancel", "item-not-found")); 556 --local s, e = 1, 1+max;
553 return true; 557 if after then
554 end 558 after = tonumber(after:get_text());
555 s = after + 1; 559 if not after or after < 1 or after > count then -- not found
556 e = s + max; 560 origin.send(st.error_reply(stanza, "cancel", "item-not-found"));
557 elseif before then 561 return true;
558 before = tonumber(before:get_text()); 562 end
559 if not before then -- the last page 563 s = after + 1;
560 e = count + 1; 564 e = s + max;
561 s = e - max; 565 elseif before then
562 elseif before < 1 or before > count then 566 before = tonumber(before:get_text());
563 origin.send(st.error_reply(stanza, "cancel", "item-not-found")); 567 if not before then -- the last page
564 return true; 568 e = count + 1;
565 else 569 s = e - max;
566 e = before; 570 elseif before < 1 or before > count then
567 s = e - max; 571 origin.send(st.error_reply(stanza, "cancel", "item-not-found"));
568 end 572 return true;
569 elseif index then 573 else
570 s = tonumber(index:get_text()) + 1; -- 0-based 574 e = before;
571 e = s + max; 575 s = e - max;
576 end
577 elseif index then
578 s = tonumber(index:get_text()) + 1; -- 0-based
579 e = s + max;
580 end
572 end 581 end
573 if s < 1 then s = 1; end 582 if s < 1 then s = 1; end
574 if e > count + 1 then e = count + 1; end 583 if e > count + 1 then e = count + 1; end
575 for i = s, e-1 do 584 for i = s, e-1 do
576 reply:add_child(resset[i]); 585 reply:add_child(resset[i]);