User color only depends on the email address.
[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();
7027794f 235 foreach ($messages as $id=>&$message) {
0580f927 236 if (!isset($this->overview[$id])) {
237 $this->overview[$id] = new BananaSpoolHead($id, $message);
238 $head =& $this->overview[$id];
239 $this->ids[$head->msgid] =& $head;
d3b026ab 240 $head->time = $time;
0580f927 241 }
7027794f 242 }
243
0580f927 244 // Build tree
0580f927 245 $null = null;
7027794f 246 foreach ($messages as $id=>&$message) {
0580f927 247 $msg =& $this->overview[$id];
248 $parents =& $this->getReferences($message);
249 while (!empty($parents) && ($msg->parent === $msg || is_null($msg->parent))) {
954b9478 250 @$msg->parent =& array_pop($parents);
3316e34e 251 }
0580f927 252
253 if (!is_null($msg->parent)) {
254 $parent =& $msg->parent;
255 $parent->children[] =& $msg;
256 while (!is_null($parent)) {
257 $parent->desc += $msg->desc;
d3b026ab 258 $parent->time = $time;
190f587f 259 $prev =& $parent;
0580f927 260 if ($parent !== $parent->parent) {
261 $parent =& $parent->parent;
7027794f 262 } else {
0580f927 263 $parent =& $null;
66e81236 264 }
e785d91c 265 }
810ac1df 266 }
810ac1df 267 }
7027794f 268 Banana::$protocole->updateSpool($messages);
269 return true;
dd7d1c59 270 }
e785d91c 271
bffb37b4 272 public function updateUnread($since)
75ff2f64 273 {
7027794f 274 if (empty($since)) {
275 return;
276 }
277
278 $newpostsids = Banana::$protocole->getNewIndexes($since);
b3d11736 279
7027794f 280 if (empty($newpostsids)) {
281 return;
282 }
283
7027794f 284 $newpostsids = array_intersect($newpostsids, array_keys($this->ids));
285 foreach ($newpostsids as $mid) {
0580f927 286 $overview =& $this->ids[$mid];
287 if ($overview->isread) {
288 $overview->isread = false;
289 while (!is_null($overview)) {
290 $overview->descunread++;
291 $overview =& $overview->parent;
e785d91c 292 }
810ac1df 293 }
7027794f 294 }
0580f927 295 $this->unreadnb += count($newpostsids);
2b974d09
FB
296
297 if (function_exists('hook_listReadMessages')) {
298 $msgs = hook_listReadMessages($this->group);
299 if (!is_array($msgs)) {
300 return;
301 }
302 foreach ($msgs as $msg) {
303 if (!is_numeric($msg)) {
304 if (!isset($this->ids[$msg])) {
305 continue;
306 }
05114a3e 307 $msg = $this->ids[$msg]->id;
2b974d09
FB
308 }
309 $this->markAsRead($msg);
310 }
311 }
7027794f 312 }
dd7d1c59 313
7027794f 314 public function setMode($mode)
315 {
e9360b11 316 $this->mode = $mode;
7027794f 317 switch ($mode) {
318 case Banana::SPOOL_UNREAD:
64f89fae 319 $num = max(array_keys($this->overview));
320 if ($this->overview[$num]->isread) {
321 break;
322 }
0580f927 323 foreach ($this->roots as &$root) {
324 if ($root->descunread == 0) {
325 $this->killdesc($root->id);
e785d91c 326 }
810ac1df 327 }
7027794f 328 break;
810ac1df 329 }
cced14b6 330 }
331
0580f927 332 /** Fetch list of references
333 */
334 public function &getReferences(array &$refs)
335 {
336 $references = array();
337 if (isset($refs['references'])) {
338 $text = preg_split('/\s/', str_replace('><', '> <', $refs['references']));
339 foreach ($text as $id=>&$value) {
340 if (isset($this->ids[$value])) {
341 $references[] =& $this->ids[$value];
342 }
343 }
344 } elseif (isset($refs['in-reply-to']) && isset($this->ids[$refs['in-reply-to']])) {
345 $references[] =& $this->ids[$refs['in-reply-to']];
346 }
347 return $references;
348 }
349
d3b026ab 350 /** Get the tree associated to a given id
351 */
352 public function &getTree($id)
353 {
1d521296 354 return BananaTree::build($id)->show();
d3b026ab 355 }
356
e9360b11 357 /** Mark the given id as read
358 * @param id MSGNUM of post
359 */
360 public function markAsRead($id)
361 {
1d521296 362 $overview =& $this->overview[$id];
363 if (!$overview->isread) {
364 $overview->isread = true;
d265608a 365 $this->unreadnb--;
1d521296 366 while (!is_null($overview)) {
367 $overview->descunread--;
368 $overview =& $overview->parent;
e9360b11 369 }
370 }
371 }
372
373 /** Mark all unread messages as read
374 */
375 public function markAllAsRead(array &$array = null)
376 {
d265608a 377 if (!$this->unreadnb) {
378 return;
379 }
0580f927 380 if (is_null($array) && !empty($this->roots)) {
e9360b11 381 $array =& $this->roots;
c4f176d8 382 } elseif (is_null($array)) {
c28d3016 383 return;
384 }
0580f927 385 foreach ($array as &$msg) {
386 if (!$msg->isread) {
387 $this->markAsRead($msg->id);
d265608a 388 if (!$this->unreadnb) {
389 return;
390 }
e9360b11 391 }
0580f927 392 if ($msg->descunread) {
393 $this->markAllAsRead($msg->children);
e9360b11 394 }
395 }
396 }
397
e785d91c 398 /** kill post and childrens
399 * @param $_id MSGNUM of post
400 */
7027794f 401 private function killdesc($_id)
e785d91c 402 {
0580f927 403 $overview =& $this->overview[$_id];
404 $children =& $overview->children;
405 if (sizeof($children)) {
406 foreach ($children as &$c) {
407 $this->killdesc($c->id);
e785d91c 408 }
409 }
410 unset($this->overview[$_id]);
0580f927 411 foreach ($this->roots as $k=>&$root) {
412 if ($root === $overview) {
413 unset($this->roots[$k]);
414 break;
415 }
e785d91c 416 }
0580f927 417 unset($this->ids[$overview->msgid]);
0580f927 418 $overview = null;
810ac1df 419 }
e785d91c 420
421 /** delete a post from overview
422 * @param $_id MSGNUM of post
423 */
7027794f 424 public function delid($_id, $write = true)
e785d91c 425 {
0580f927 426 if (!isset($this->overview[$_id])) {
427 return;
428 }
429
430 $overview =& $this->overview[$_id];
431 // Be sure it is not counted as unread
432 if (!$overview->isread) {
433 $this->markAsRead($_id);
434 }
435
436 $parent =& $overview->parent;
437
438 // Remove from the message tree
439 if (!is_null($parent)) {
694a26dd 440 $time = time();
0580f927 441 foreach ($parent->children as $key=>&$child) {
442 if ($child === $overview) {
443 unset($parent->children[$key]);
444 break;
e785d91c 445 }
446 }
0580f927 447 if (sizeof($overview->children)) {
0580f927 448 foreach ($overview->children as &$child) {
694a26dd 449 $parent->children[] =& $child;
450 $child->time = $time;
0580f927 451 $child->parent =& $parent;
452 }
e785d91c 453 }
0580f927 454 while (!is_null($parent)) {
455 $parent->desc--;
d3b026ab 456 $parent->time = $time;
0580f927 457 $parent =& $parent->parent;
cfb7fe5d 458 }
0580f927 459 }
64f89fae 460
0580f927 461 // Remove all refenrences and assign null to the object
462 unset($this->ids[$overview->msgid]);
463 unset($this->overview[$_id]);
d3b026ab 464 BananaTree::kill($_id);
0580f927 465 foreach ($this->roots as $k=>&$root) {
466 if ($root === $overview) {
467 unset($this->roots[$k]);
468 break;
7027794f 469 }
e785d91c 470 }
0580f927 471 $overview = null;
472
473 if ($write) {
474 $this->saveToFile();
475 }
35ca8036 476 }
810ac1df 477
8a30c7d6 478 public function formatDate(BananaSpoolHead $head)
7027794f 479 {
0580f927 480 $stamp = $head->date;
7027794f 481 $today = intval(time() / (24*3600));
482 $dday = intval($stamp / (24*3600));
483
484 if ($today == $dday) {
485 $format = "%H:%M";
486 } elseif ($today == 1 + $dday) {
487 $format = _b_('hier')." %H:%M";
488 } elseif ($today < 7 + $dday) {
489 $format = '%a %H:%M';
d924433f 490 } elseif ($today < 90 + $dday) {
7027794f 491 $format = '%a %e %b';
d924433f 492 } else {
493 $format = '%a %e %b %Y';
7027794f 494 }
22b95309 495 return strftime($format, $stamp);
7027794f 496 }
497
8a30c7d6 498 public function formatSubject(BananaSpoolHead $head)
66e81236 499 {
0580f927 500 $subject = $popup = $head->subject;
66e81236 501 $popup = $subject;
502 if (function_exists('hook_formatDisplayHeader')) {
503 list($subject, $link) = hook_formatDisplayHeader('subject', $subject, true);
504 } else {
505 $subject = banana_catchFormats(banana_entities(stripslashes($subject)));
506 $link = null;
507 }
508 if (empty($subject)) {
509 $subject = _b_('(pas de sujet)');
510 }
212ada1b
SJ
511 if (mb_strlen($subject) > 100) {
512 $subject = mb_substr($subject, 0, 99) . '&hellip;';
d2e741cb 513 }
bd05fff5 514 if ($head->id !== Banana::$artid) {
0580f927 515 $subject = Banana::$page->makeLink(Array('group' => $this->group, 'artid' => $head->id,
66e81236 516 'text' => $subject, 'popup' => $popup));
517 }
518 return $subject . $link;
519 }
520
8a30c7d6 521 public function formatFrom(BananaSpoolHead $head)
66e81236 522 {
0580f927 523 return BananaMessage::formatFrom($head->from);
66e81236 524 }
525
526 public function start()
527 {
528 if (Banana::$first) {
529 return Banana::$first;
530 } else {
531 $first = array_search(Banana::$artid, $this->roots);
532 return max(0, $first - Banana::$spool_tbefore);
533 }
534 }
535
536 public function context()
537 {
538 return Banana::$first ? Banana::$spool_tmax : Banana::$spool_tcontext;
539 }
540
d8e2470c 541 /** Return root message of the given thread
542 * @param id INTEGER id of a message
543 */
190f587f 544 public function &root($id)
7027794f 545 {
190f587f 546 $parent =& $this->overview[$id];
547 while (!is_null($parent->parent)) {
548 $parent =& $parent->parent;
d8e2470c 549 }
190f587f 550 return $parent;
d8e2470c 551 }
552
bffb37b4 553 /** Return the last post id with the given subject
554 * @param subject
555 */
556 public function getPostId($subject)
557 {
558 $subject = trim($subject);
559 $id = max(array_keys($this->overview));
560 while (isset($this->overview[$id])) {
561 $test = $this->overview[$id]->subject;
562 if (function_exists('hook_formatDisplayHeader')) {
563 $val = hook_formatDisplayHeader('subject', $test, true);
564 if (is_array($val)) {
0a5b736c 565 $test = banana_html_entity_decode($val[0]);
bffb37b4 566 } else {
0a5b736c 567 $test = banana_html_entity_decode($val);
bffb37b4 568 }
569 }
570 $test = trim($test);
bffb37b4 571 if ($test == $subject) {
572 return $id;
573 }
574 $id--;
575 }
576 return -1;
577 }
578
d8e2470c 579 /** Returns previous thread root index
580 * @param id INTEGER message number
581 */
7027794f 582 public function prevThread($id)
d8e2470c 583 {
190f587f 584 $root =& $this->root($id);
d8e2470c 585 $last = null;
190f587f 586 foreach ($this->roots as &$i) {
587 if ($i === $root) {
d8e2470c 588 return $last;
589 }
190f587f 590 $last = $i->id;
d8e2470c 591 }
592 return $last;
593 }
594
595 /** Returns next thread root index
596 * @param id INTEGER message number
597 */
7027794f 598 public function nextThread($id)
d8e2470c 599 {
190f587f 600 $root =& $this->root($id);
d8e2470c 601 $ok = false;
190f587f 602 foreach ($this->roots as &$i) {
d8e2470c 603 if ($ok) {
190f587f 604 return $i->id;
d8e2470c 605 }
190f587f 606 if ($i === $root) {
d8e2470c 607 $ok = true;
608 }
609 }
610 return null;
611 }
612
613 /** Return prev post in the thread
614 * @param id INTEGER message number
615 */
7027794f 616 public function prevPost($id)
d8e2470c 617 {
190f587f 618 $parent =& $this->overview[$id]->parent;
d8e2470c 619 if (is_null($parent)) {
620 return null;
621 }
190f587f 622 $last = $parent->id;
623 foreach ($parent->children as &$child) {
624 if ($child->id == $id) {
d8e2470c 625 return $last;
626 }
190f587f 627 $last = $child->id;
d8e2470c 628 }
629 return null;
630 }
631
632 /** Return next post in the thread
633 * @param id INTEGER message number
634 */
7027794f 635 public function nextPost($id)
d8e2470c 636 {
190f587f 637 $cur =& $this->overview[$id];
638 if (count($cur->children) != 0) {
639 return $cur->children[0]->id;
d8e2470c 640 }
64f89fae 641
190f587f 642 $parent =& $cur;
d8e2470c 643 while (true) {
190f587f 644 $parent =& $cur->parent;
d8e2470c 645 if (is_null($parent)) {
646 return null;
647 }
648 $ok = false;
190f587f 649 foreach ($parent->children as &$child) {
d8e2470c 650 if ($ok) {
190f587f 651 return $child->id;
d8e2470c 652 }
190f587f 653 if ($child === $cur) {
d8e2470c 654 $ok = true;
655 }
656 }
190f587f 657 $cur =& $parent;
d8e2470c 658 }
659 return null;
660 }
d634c13c 661
662 /** Look for an unread message in the thread rooted by the message
663 * @param id INTEGER message number
664 */
8a30c7d6 665 private function _nextUnread(BananaSpoolHead $cur)
d634c13c 666 {
190f587f 667 if (!$cur->isread) {
668 return $cur->id;
d634c13c 669 }
190f587f 670 foreach ($cur->children as &$child) {
bdad7c9d 671 $unread = $this->_nextUnread($child);
672 if (!is_null($unread)) {
673 return $unread;
64f89fae 674 }
d634c13c 675 }
676 return null;
677 }
678
679 /** Find next unread message
680 * @param id INTEGER message number
681 */
7027794f 682 public function nextUnread($id = null)
d634c13c 683 {
d265608a 684 if (!$this->unreadnb) {
685 return null;
686 }
687
b7d59a47 688 if (!is_null($id)) {
689 // Look in message children
190f587f 690 foreach ($this->overview[$id]->children as &$child) {
b7d59a47 691 $next = $this->_nextUnread($child);
692 if (!is_null($next)) {
693 return $next;
694 }
d634c13c 695 }
696 }
697
698 // Look in current thread
190f587f 699 if (is_null($id)) {
700 $cur = null;
701 } else {
702 $cur =& $this->overview[$id];
703 }
b7d59a47 704 do {
190f587f 705 if (is_null($cur)) {
706 $parent =& $cur;
707 $ok = true;
708 } else {
709 $parent =& $cur->parent;
710 $ok = false;
711 }
1e016f3a 712 if (!is_null($parent)) {
190f587f 713 $array =& $parent->children;
d634c13c 714 } else {
190f587f 715 $array =& $this->roots;
d634c13c 716 }
190f587f 717 foreach ($array as &$child) {
d634c13c 718 if ($ok) {
719 $next = $this->_nextUnread($child);
720 if (!is_null($next)) {
721 return $next;
722 }
723 }
190f587f 724 if ($child === $cur) {
d634c13c 725 $ok = true;
726 }
727 }
190f587f 728 $cur =& $parent;
b7d59a47 729 } while(!is_null($cur));
d634c13c 730 return null;
64f89fae 731 }
810ac1df 732}
598a1c53 733// vim:set et sw=4 sts=4 ts=4 enc=utf-8:
810ac1df 734?>