Fixes vim mode line.
[banana.git] / banana / spool.inc.php
CommitLineData
810ac1df 1<?php
2/********************************************************************************
3* include/spool.inc.php : spool subroutines
4* -----------------------
5*
6* This file is part of the banana distribution
7* Copyright: See COPYING files that comes with this distribution
8********************************************************************************/
9
7027794f 10require_once dirname(__FILE__) . '/banana.inc.php';
d3b026ab 11require_once dirname(__FILE__) . '/tree.inc.php';
3ca86dfe 12
d3b026ab 13define('BANANA_SPOOL_VERSION', '0.5.14');
01681efd 14
810ac1df 15/** Class spoolhead
16 * class used in thread overviews
17 */
d4c19591 18class BananaSpoolHead
e785d91c 19{
0580f927 20 public $id;
21 public $msgid;
22
e785d91c 23 /** date (timestamp) */
7027794f 24 public $date;
e785d91c 25 /** subject */
7027794f 26 public $subject;
e785d91c 27 /** author */
7027794f 28 public $from;
66e81236 29 public $name;
dce7d862 30 public $color;
e785d91c 31 /** reference of parent */
7027794f 32 public $parent = null;
e785d91c 33 /** array of children */
7027794f 34 public $children = Array();
e785d91c 35 /** true if post is read */
7027794f 36 public $isread;
e785d91c 37 /** number of posts deeper in this branch of tree */
7027794f 38 public $desc;
e785d91c 39 /** same as desc, but counts only unread posts */
7027794f 40 public $descunread;
d3b026ab 41 /** last time the number of children has been updated */
42 public $time = 0;
7027794f 43
44 /** storage data */
45 public $storage = array();
810ac1df 46
e785d91c 47 /** constructor
48 * @param $_date INTEGER timestamp of post
49 * @param $_subject STRING subject of post
50 * @param $_from STRING author of post
51 * @param $_desc INTEGER desc value (1 for a new post)
52 * @param $_read BOOLEAN true if read
53 * @param $_descunread INTEGER descunread value (0 for a new post)
54 */
0580f927 55 public function __construct($id, array &$message)
e785d91c 56 {
16800641 57 list($name, $from) = BananaMessage::extractMail($message['from']);
0580f927 58 $this->id = $id;
954b9478 59 $this->msgid = @$message['message-id'];
7027794f 60 $this->date = $message['date'];
954b9478 61 $this->subject = @$message['subject'];
7027794f 62 $this->from = $message['from'];
16800641 63 $this->color = sprintf('#%06x', abs(crc32($from) % 0xffffff));
7027794f 64 $this->desc = 1;
65 $this->isread = true;
66 $this->descunread = 0;
16800641
FB
67 $this->name = $name;
68 if ($name === $from) {
69 $this->name = 'Anonymous';
66e81236 70 }
e785d91c 71 }
810ac1df 72}
73
3ca86dfe 74
d4c19591 75class BananaSpool
e785d91c 76{
7027794f 77 private $version;
e9360b11 78 private $mode;
7027794f 79
e785d91c 80 /** group name */
7027794f 81 public $group;
82 /** spool */
0580f927 83 public $overview = array();
e785d91c 84 /** array msgid => msgnum */
0580f927 85 public $ids = array();
cced14b6 86 /** thread starts */
0580f927 87 public $roots = array();
66e81236 88
dc5f77ad 89 /** protocole specific data */
90 public $storage = array();
7027794f 91
d265608a 92 private $unreadnb = 0;
810ac1df 93
e785d91c 94 /** constructor
e785d91c 95 * @param $_group STRING group name
96 * @param $_display INTEGER 1 => all posts, 2 => only threads with new posts
97 * @param $_since INTEGER time stamp (used for read/unread)
98 */
7027794f 99 protected function __construct($group)
e785d91c 100 {
7027794f 101 $this->version = BANANA_SPOOL_VERSION;
e9360b11 102 $this->mode = Banana::SPOOL_ALL;
7027794f 103 $this->group = $group;
104 }
fb6428c8 105
e02edbfe 106 public static function &getSpool($group, $since = 0, $clean = false)
7027794f 107 {
108 if (!is_null(Banana::$spool) && Banana::$spool->group == $group) {
b3d11736 109 $spool =& Banana::$spool;
82c17a91 110 } else {
e02edbfe 111 $spool =& BananaSpool::readFromFile($group);
64f89fae 112 }
7027794f 113 if (is_null($spool)) {
114 $spool = new BananaSpool($group);
82c17a91 115 }
7027794f 116 Banana::$spool =& $spool;
117 $spool->build();
b3d11736 118 if ($clean) {
119 $spool->markAllAsRead();
120 }
7027794f 121 $spool->updateUnread($since);
122 return $spool;
123 }
e785d91c 124
dcb7f48d 125 public static function getPath($file = null)
7027794f 126 {
dcb7f48d 127 $path = Banana::$spool_root . '/' . Banana::$protocole->name() . '/' . Banana::$protocole->filename();
128 if (!is_dir($path)) {
129 if (file_exists($path)) {
130 @unlink($path);
131 }
132 mkdir($path, 0777, true);
dd7d1c59 133 }
dcb7f48d 134 return $path . '/' . $file;
135 }
136
137 private static function spoolFilename()
138 {
139 return BananaSpool::getPath('spool');
dd7d1c59 140 }
141
e02edbfe 142 private static function &readFromFile($group)
dd7d1c59 143 {
e02edbfe 144 $spool = null;
dcb7f48d 145 $file = BananaSpool::spoolFilename();
7027794f 146 if (!file_exists($file)) {
e02edbfe 147 return $spool;
dd7d1c59 148 }
7027794f 149 $spool = unserialize(file_get_contents($file));
e9360b11 150 if ($spool->version != BANANA_SPOOL_VERSION || $spool->mode != Banana::SPOOL_ALL) {
e02edbfe 151 $spool = null;
152 return $spool;
7027794f 153 }
d265608a 154 $spool->markAllAsRead();
7027794f 155 return $spool;
156 }
157
8a30c7d6 158 private function compare($a, $b)
7027794f 159 {
0580f927 160 return ($b->date - $a->date);
dd7d1c59 161 }
162
7027794f 163 private function saveToFile()
dd7d1c59 164 {
dcb7f48d 165 $file = BananaSpool::spoolFilename();
dd7d1c59 166
167 $this->roots = Array();
0580f927 168 foreach($this->overview as &$msg) {
dd7d1c59 169 if (is_null($msg->parent)) {
0580f927 170 $this->roots[] =& $msg;
dd7d1c59 171 }
172 }
345c3a85 173 usort($this->roots, array($this, 'compare'));
2c606d23 174
e9360b11 175 if ($this->mode == Banana::SPOOL_ALL) {
176 file_put_contents($file, serialize($this));
64f89fae 177 }
dd7d1c59 178 }
179
7027794f 180 private function build()
181 {
182 $threshold = 0;
183
184 // Compute the range of indexes
185 list($msgnum, $first, $last) = Banana::$protocole->getIndexes();
186 if ($last < $first) {
6bd49b4b 187 $threshold = $first + $msgnum - $last;
7027794f 188 $threshold = (int)(log($threshold)/log(2));
189 $threshold = (2 ^ ($threshold + 1)) - 1;
190 }
e9360b11 191 if (Banana::$spool_max && Banana::$spool_max < $msgnum) {
192 $first = $last - Banana::$spool_max;
7027794f 193 if ($first < 0) {
194 $first += $threshold;
195 }
196 }
197 $clean = $this->clean($first, $last, $msgnum);
198 $update = $this->update($first, $last, $msgnum);
64f89fae 199
7027794f 200 if ($clean || $update) {
201 $this->saveToFile();
202 }
203 }
64f89fae 204
7027794f 205 private function clean(&$first, &$last, $msgnum)
9090c673 206 {
7027794f 207 $do_save = false;
0580f927 208 if (!empty($this->overview)) {
7027794f 209 $mids = array_keys($this->overview);
210 foreach ($mids as $id) {
211 if (($first <= $last && ($id < $first || $id > $last))
e9360b11 212 || ($first > $last && $id < $first && $id > $last)) {
7027794f 213 $this->delid($id, false);
214 $do_save = true;
215 }
216 }
217 if (!empty($this->overview)) {
218 $first = max(array_keys($this->overview))+1;
219 }
220 }
221 return $do_save;
9090c673
PHM
222 }
223
7027794f 224 private function update(&$first, &$last, $msgnum)
dd7d1c59 225 {
64f89fae 226 if ($first > $last || !$msgnum) {
7027794f 227 return false;
dd7d1c59 228 }
229
7027794f 230 $messages =& Banana::$protocole->getMessageHeaders($first, $last,
231 array('Date', 'Subject', 'From', 'Message-ID', 'References', 'In-Reply-To'));
dd7d1c59 232
0580f927 233 // Build all the new Spool Heads
d3b026ab 234 $time = time();
797579f4
SJ
235 if (!empty($messages)) {
236 foreach ($messages as $id=>&$message) {
237 if (!isset($this->overview[$id])) {
238 $this->overview[$id] = new BananaSpoolHead($id, $message);
239 $head =& $this->overview[$id];
240 $this->ids[$head->msgid] =& $head;
241 $head->time = $time;
242 }
0580f927 243 }
7027794f 244
797579f4
SJ
245 // Build tree
246 $null = null;
247 foreach ($messages as $id=>&$message) {
248 $msg =& $this->overview[$id];
249 $parents =& $this->getReferences($message);
250 while (!empty($parents) && ($msg->parent === $msg || is_null($msg->parent))) {
251 @$msg->parent =& array_pop($parents);
252 }
0580f927 253
797579f4
SJ
254 if (!is_null($msg->parent)) {
255 $parent =& $msg->parent;
256 $parent->children[] =& $msg;
257 while (!is_null($parent)) {
258 $parent->desc += $msg->desc;
259 $parent->time = $time;
260 $prev =& $parent;
261 if ($parent !== $parent->parent) {
262 $parent =& $parent->parent;
263 } else {
264 $parent =& $null;
265 }
66e81236 266 }
e785d91c 267 }
810ac1df 268 }
797579f4
SJ
269 } else {
270 $messages = array();
810ac1df 271 }
7027794f 272 Banana::$protocole->updateSpool($messages);
273 return true;
dd7d1c59 274 }
e785d91c 275
bffb37b4 276 public function updateUnread($since)
75ff2f64 277 {
7027794f 278 if (empty($since)) {
279 return;
280 }
281
282 $newpostsids = Banana::$protocole->getNewIndexes($since);
b3d11736 283
7027794f 284 if (empty($newpostsids)) {
285 return;
286 }
287
7027794f 288 $newpostsids = array_intersect($newpostsids, array_keys($this->ids));
289 foreach ($newpostsids as $mid) {
0580f927 290 $overview =& $this->ids[$mid];
291 if ($overview->isread) {
292 $overview->isread = false;
293 while (!is_null($overview)) {
294 $overview->descunread++;
295 $overview =& $overview->parent;
e785d91c 296 }
810ac1df 297 }
7027794f 298 }
0580f927 299 $this->unreadnb += count($newpostsids);
2b974d09
FB
300
301 if (function_exists('hook_listReadMessages')) {
302 $msgs = hook_listReadMessages($this->group);
303 if (!is_array($msgs)) {
304 return;
305 }
306 foreach ($msgs as $msg) {
307 if (!is_numeric($msg)) {
308 if (!isset($this->ids[$msg])) {
309 continue;
310 }
05114a3e 311 $msg = $this->ids[$msg]->id;
2b974d09
FB
312 }
313 $this->markAsRead($msg);
314 }
315 }
7027794f 316 }
dd7d1c59 317
7027794f 318 public function setMode($mode)
319 {
e9360b11 320 $this->mode = $mode;
7027794f 321 switch ($mode) {
322 case Banana::SPOOL_UNREAD:
64f89fae 323 $num = max(array_keys($this->overview));
324 if ($this->overview[$num]->isread) {
325 break;
326 }
0580f927 327 foreach ($this->roots as &$root) {
328 if ($root->descunread == 0) {
329 $this->killdesc($root->id);
e785d91c 330 }
810ac1df 331 }
7027794f 332 break;
810ac1df 333 }
cced14b6 334 }
335
0580f927 336 /** Fetch list of references
337 */
338 public function &getReferences(array &$refs)
339 {
340 $references = array();
341 if (isset($refs['references'])) {
342 $text = preg_split('/\s/', str_replace('><', '> <', $refs['references']));
343 foreach ($text as $id=>&$value) {
344 if (isset($this->ids[$value])) {
345 $references[] =& $this->ids[$value];
346 }
347 }
348 } elseif (isset($refs['in-reply-to']) && isset($this->ids[$refs['in-reply-to']])) {
349 $references[] =& $this->ids[$refs['in-reply-to']];
350 }
351 return $references;
352 }
353
d3b026ab 354 /** Get the tree associated to a given id
355 */
356 public function &getTree($id)
357 {
1d521296 358 return BananaTree::build($id)->show();
d3b026ab 359 }
360
e9360b11 361 /** Mark the given id as read
362 * @param id MSGNUM of post
363 */
364 public function markAsRead($id)
365 {
1d521296 366 $overview =& $this->overview[$id];
367 if (!$overview->isread) {
368 $overview->isread = true;
d265608a 369 $this->unreadnb--;
1d521296 370 while (!is_null($overview)) {
371 $overview->descunread--;
372 $overview =& $overview->parent;
e9360b11 373 }
374 }
375 }
376
377 /** Mark all unread messages as read
378 */
379 public function markAllAsRead(array &$array = null)
380 {
d265608a 381 if (!$this->unreadnb) {
382 return;
383 }
0580f927 384 if (is_null($array) && !empty($this->roots)) {
e9360b11 385 $array =& $this->roots;
c4f176d8 386 } elseif (is_null($array)) {
c28d3016 387 return;
388 }
0580f927 389 foreach ($array as &$msg) {
390 if (!$msg->isread) {
391 $this->markAsRead($msg->id);
d265608a 392 if (!$this->unreadnb) {
393 return;
394 }
e9360b11 395 }
0580f927 396 if ($msg->descunread) {
397 $this->markAllAsRead($msg->children);
e9360b11 398 }
399 }
400 }
401
e785d91c 402 /** kill post and childrens
403 * @param $_id MSGNUM of post
404 */
7027794f 405 private function killdesc($_id)
e785d91c 406 {
0580f927 407 $overview =& $this->overview[$_id];
408 $children =& $overview->children;
409 if (sizeof($children)) {
410 foreach ($children as &$c) {
411 $this->killdesc($c->id);
e785d91c 412 }
413 }
414 unset($this->overview[$_id]);
0580f927 415 foreach ($this->roots as $k=>&$root) {
416 if ($root === $overview) {
417 unset($this->roots[$k]);
418 break;
419 }
e785d91c 420 }
0580f927 421 unset($this->ids[$overview->msgid]);
0580f927 422 $overview = null;
810ac1df 423 }
e785d91c 424
425 /** delete a post from overview
426 * @param $_id MSGNUM of post
427 */
7027794f 428 public function delid($_id, $write = true)
e785d91c 429 {
0580f927 430 if (!isset($this->overview[$_id])) {
431 return;
432 }
433
434 $overview =& $this->overview[$_id];
435 // Be sure it is not counted as unread
436 if (!$overview->isread) {
437 $this->markAsRead($_id);
438 }
439
440 $parent =& $overview->parent;
441
442 // Remove from the message tree
443 if (!is_null($parent)) {
694a26dd 444 $time = time();
0580f927 445 foreach ($parent->children as $key=>&$child) {
446 if ($child === $overview) {
447 unset($parent->children[$key]);
448 break;
e785d91c 449 }
450 }
0580f927 451 if (sizeof($overview->children)) {
0580f927 452 foreach ($overview->children as &$child) {
694a26dd 453 $parent->children[] =& $child;
454 $child->time = $time;
0580f927 455 $child->parent =& $parent;
456 }
e785d91c 457 }
0580f927 458 while (!is_null($parent)) {
459 $parent->desc--;
d3b026ab 460 $parent->time = $time;
0580f927 461 $parent =& $parent->parent;
cfb7fe5d 462 }
0580f927 463 }
64f89fae 464
0580f927 465 // Remove all refenrences and assign null to the object
466 unset($this->ids[$overview->msgid]);
467 unset($this->overview[$_id]);
d3b026ab 468 BananaTree::kill($_id);
0580f927 469 foreach ($this->roots as $k=>&$root) {
470 if ($root === $overview) {
471 unset($this->roots[$k]);
472 break;
7027794f 473 }
e785d91c 474 }
0580f927 475 $overview = null;
476
477 if ($write) {
478 $this->saveToFile();
479 }
35ca8036 480 }
810ac1df 481
8a30c7d6 482 public function formatDate(BananaSpoolHead $head)
7027794f 483 {
0580f927 484 $stamp = $head->date;
7027794f 485 $today = intval(time() / (24*3600));
486 $dday = intval($stamp / (24*3600));
487
488 if ($today == $dday) {
489 $format = "%H:%M";
490 } elseif ($today == 1 + $dday) {
491 $format = _b_('hier')." %H:%M";
492 } elseif ($today < 7 + $dday) {
493 $format = '%a %H:%M';
d924433f 494 } elseif ($today < 90 + $dday) {
7027794f 495 $format = '%a %e %b';
d924433f 496 } else {
497 $format = '%a %e %b %Y';
7027794f 498 }
22b95309 499 return strftime($format, $stamp);
7027794f 500 }
501
8a30c7d6 502 public function formatSubject(BananaSpoolHead $head)
66e81236 503 {
0580f927 504 $subject = $popup = $head->subject;
66e81236 505 $popup = $subject;
506 if (function_exists('hook_formatDisplayHeader')) {
507 list($subject, $link) = hook_formatDisplayHeader('subject', $subject, true);
508 } else {
509 $subject = banana_catchFormats(banana_entities(stripslashes($subject)));
510 $link = null;
511 }
512 if (empty($subject)) {
513 $subject = _b_('(pas de sujet)');
514 }
212ada1b
SJ
515 if (mb_strlen($subject) > 100) {
516 $subject = mb_substr($subject, 0, 99) . '&hellip;';
d2e741cb 517 }
bd05fff5 518 if ($head->id !== Banana::$artid) {
0580f927 519 $subject = Banana::$page->makeLink(Array('group' => $this->group, 'artid' => $head->id,
66e81236 520 'text' => $subject, 'popup' => $popup));
521 }
522 return $subject . $link;
523 }
524
8a30c7d6 525 public function formatFrom(BananaSpoolHead $head)
66e81236 526 {
0580f927 527 return BananaMessage::formatFrom($head->from);
66e81236 528 }
529
530 public function start()
531 {
532 if (Banana::$first) {
533 return Banana::$first;
534 } else {
535 $first = array_search(Banana::$artid, $this->roots);
536 return max(0, $first - Banana::$spool_tbefore);
537 }
538 }
539
540 public function context()
541 {
542 return Banana::$first ? Banana::$spool_tmax : Banana::$spool_tcontext;
543 }
544
d8e2470c 545 /** Return root message of the given thread
546 * @param id INTEGER id of a message
547 */
190f587f 548 public function &root($id)
7027794f 549 {
190f587f 550 $parent =& $this->overview[$id];
551 while (!is_null($parent->parent)) {
552 $parent =& $parent->parent;
d8e2470c 553 }
190f587f 554 return $parent;
d8e2470c 555 }
556
bffb37b4 557 /** Return the last post id with the given subject
558 * @param subject
559 */
560 public function getPostId($subject)
561 {
562 $subject = trim($subject);
563 $id = max(array_keys($this->overview));
564 while (isset($this->overview[$id])) {
565 $test = $this->overview[$id]->subject;
566 if (function_exists('hook_formatDisplayHeader')) {
567 $val = hook_formatDisplayHeader('subject', $test, true);
568 if (is_array($val)) {
0a5b736c 569 $test = banana_html_entity_decode($val[0]);
bffb37b4 570 } else {
0a5b736c 571 $test = banana_html_entity_decode($val);
bffb37b4 572 }
573 }
574 $test = trim($test);
bffb37b4 575 if ($test == $subject) {
576 return $id;
577 }
578 $id--;
579 }
580 return -1;
581 }
582
d8e2470c 583 /** Returns previous thread root index
584 * @param id INTEGER message number
585 */
7027794f 586 public function prevThread($id)
d8e2470c 587 {
190f587f 588 $root =& $this->root($id);
d8e2470c 589 $last = null;
190f587f 590 foreach ($this->roots as &$i) {
591 if ($i === $root) {
d8e2470c 592 return $last;
593 }
190f587f 594 $last = $i->id;
d8e2470c 595 }
596 return $last;
597 }
598
599 /** Returns next thread root index
600 * @param id INTEGER message number
601 */
7027794f 602 public function nextThread($id)
d8e2470c 603 {
190f587f 604 $root =& $this->root($id);
d8e2470c 605 $ok = false;
190f587f 606 foreach ($this->roots as &$i) {
d8e2470c 607 if ($ok) {
190f587f 608 return $i->id;
d8e2470c 609 }
190f587f 610 if ($i === $root) {
d8e2470c 611 $ok = true;
612 }
613 }
614 return null;
615 }
616
617 /** Return prev post in the thread
618 * @param id INTEGER message number
619 */
7027794f 620 public function prevPost($id)
d8e2470c 621 {
190f587f 622 $parent =& $this->overview[$id]->parent;
d8e2470c 623 if (is_null($parent)) {
624 return null;
625 }
190f587f 626 $last = $parent->id;
627 foreach ($parent->children as &$child) {
628 if ($child->id == $id) {
d8e2470c 629 return $last;
630 }
190f587f 631 $last = $child->id;
d8e2470c 632 }
633 return null;
634 }
635
636 /** Return next post in the thread
637 * @param id INTEGER message number
638 */
7027794f 639 public function nextPost($id)
d8e2470c 640 {
190f587f 641 $cur =& $this->overview[$id];
642 if (count($cur->children) != 0) {
643 return $cur->children[0]->id;
d8e2470c 644 }
64f89fae 645
190f587f 646 $parent =& $cur;
d8e2470c 647 while (true) {
190f587f 648 $parent =& $cur->parent;
d8e2470c 649 if (is_null($parent)) {
650 return null;
651 }
652 $ok = false;
190f587f 653 foreach ($parent->children as &$child) {
d8e2470c 654 if ($ok) {
190f587f 655 return $child->id;
d8e2470c 656 }
190f587f 657 if ($child === $cur) {
d8e2470c 658 $ok = true;
659 }
660 }
190f587f 661 $cur =& $parent;
d8e2470c 662 }
663 return null;
664 }
d634c13c 665
666 /** Look for an unread message in the thread rooted by the message
667 * @param id INTEGER message number
668 */
8a30c7d6 669 private function _nextUnread(BananaSpoolHead $cur)
d634c13c 670 {
190f587f 671 if (!$cur->isread) {
672 return $cur->id;
d634c13c 673 }
190f587f 674 foreach ($cur->children as &$child) {
bdad7c9d 675 $unread = $this->_nextUnread($child);
676 if (!is_null($unread)) {
677 return $unread;
64f89fae 678 }
d634c13c 679 }
680 return null;
681 }
682
683 /** Find next unread message
684 * @param id INTEGER message number
685 */
7027794f 686 public function nextUnread($id = null)
d634c13c 687 {
d265608a 688 if (!$this->unreadnb) {
689 return null;
690 }
691
b7d59a47 692 if (!is_null($id)) {
693 // Look in message children
190f587f 694 foreach ($this->overview[$id]->children as &$child) {
b7d59a47 695 $next = $this->_nextUnread($child);
696 if (!is_null($next)) {
697 return $next;
698 }
d634c13c 699 }
700 }
701
702 // Look in current thread
190f587f 703 if (is_null($id)) {
704 $cur = null;
705 } else {
706 $cur =& $this->overview[$id];
707 }
b7d59a47 708 do {
190f587f 709 if (is_null($cur)) {
710 $parent =& $cur;
711 $ok = true;
712 } else {
713 $parent =& $cur->parent;
714 $ok = false;
715 }
1e016f3a 716 if (!is_null($parent)) {
190f587f 717 $array =& $parent->children;
d634c13c 718 } else {
190f587f 719 $array =& $this->roots;
d634c13c 720 }
190f587f 721 foreach ($array as &$child) {
d634c13c 722 if ($ok) {
723 $next = $this->_nextUnread($child);
724 if (!is_null($next)) {
725 return $next;
726 }
727 }
190f587f 728 if ($child === $cur) {
d634c13c 729 $ok = true;
730 }
731 }
190f587f 732 $cur =& $parent;
b7d59a47 733 } while(!is_null($cur));
d634c13c 734 return null;
64f89fae 735 }
810ac1df 736}
a90b6fc9 737// vim:set et sw=4 sts=4 ts=4 fenc=utf-8:
810ac1df 738?>