Fixes vim mode line.
[banana.git] / banana / spool.inc.php
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
10 require_once dirname(__FILE__) . '/banana.inc.php';
11 require_once dirname(__FILE__) . '/tree.inc.php';
12
13 define('BANANA_SPOOL_VERSION', '0.5.14');
14
15 /** Class spoolhead
16 * class used in thread overviews
17 */
18 class BananaSpoolHead
19 {
20 public $id;
21 public $msgid;
22
23 /** date (timestamp) */
24 public $date;
25 /** subject */
26 public $subject;
27 /** author */
28 public $from;
29 public $name;
30 public $color;
31 /** reference of parent */
32 public $parent = null;
33 /** array of children */
34 public $children = Array();
35 /** true if post is read */
36 public $isread;
37 /** number of posts deeper in this branch of tree */
38 public $desc;
39 /** same as desc, but counts only unread posts */
40 public $descunread;
41 /** last time the number of children has been updated */
42 public $time = 0;
43
44 /** storage data */
45 public $storage = array();
46
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 */
55 public function __construct($id, array &$message)
56 {
57 list($name, $from) = BananaMessage::extractMail($message['from']);
58 $this->id = $id;
59 $this->msgid = @$message['message-id'];
60 $this->date = $message['date'];
61 $this->subject = @$message['subject'];
62 $this->from = $message['from'];
63 $this->color = sprintf('#%06x', abs(crc32($from) % 0xffffff));
64 $this->desc = 1;
65 $this->isread = true;
66 $this->descunread = 0;
67 $this->name = $name;
68 if ($name === $from) {
69 $this->name = 'Anonymous';
70 }
71 }
72 }
73
74
75 class BananaSpool
76 {
77 private $version;
78 private $mode;
79
80 /** group name */
81 public $group;
82 /** spool */
83 public $overview = array();
84 /** array msgid => msgnum */
85 public $ids = array();
86 /** thread starts */
87 public $roots = array();
88
89 /** protocole specific data */
90 public $storage = array();
91
92 private $unreadnb = 0;
93
94 /** constructor
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 */
99 protected function __construct($group)
100 {
101 $this->version = BANANA_SPOOL_VERSION;
102 $this->mode = Banana::SPOOL_ALL;
103 $this->group = $group;
104 }
105
106 public static function &getSpool($group, $since = 0, $clean = false)
107 {
108 if (!is_null(Banana::$spool) && Banana::$spool->group == $group) {
109 $spool =& Banana::$spool;
110 } else {
111 $spool =& BananaSpool::readFromFile($group);
112 }
113 if (is_null($spool)) {
114 $spool = new BananaSpool($group);
115 }
116 Banana::$spool =& $spool;
117 $spool->build();
118 if ($clean) {
119 $spool->markAllAsRead();
120 }
121 $spool->updateUnread($since);
122 return $spool;
123 }
124
125 public static function getPath($file = null)
126 {
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);
133 }
134 return $path . '/' . $file;
135 }
136
137 private static function spoolFilename()
138 {
139 return BananaSpool::getPath('spool');
140 }
141
142 private static function &readFromFile($group)
143 {
144 $spool = null;
145 $file = BananaSpool::spoolFilename();
146 if (!file_exists($file)) {
147 return $spool;
148 }
149 $spool = unserialize(file_get_contents($file));
150 if ($spool->version != BANANA_SPOOL_VERSION || $spool->mode != Banana::SPOOL_ALL) {
151 $spool = null;
152 return $spool;
153 }
154 $spool->markAllAsRead();
155 return $spool;
156 }
157
158 private function compare($a, $b)
159 {
160 return ($b->date - $a->date);
161 }
162
163 private function saveToFile()
164 {
165 $file = BananaSpool::spoolFilename();
166
167 $this->roots = Array();
168 foreach($this->overview as &$msg) {
169 if (is_null($msg->parent)) {
170 $this->roots[] =& $msg;
171 }
172 }
173 usort($this->roots, array($this, 'compare'));
174
175 if ($this->mode == Banana::SPOOL_ALL) {
176 file_put_contents($file, serialize($this));
177 }
178 }
179
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) {
187 $threshold = $first + $msgnum - $last;
188 $threshold = (int)(log($threshold)/log(2));
189 $threshold = (2 ^ ($threshold + 1)) - 1;
190 }
191 if (Banana::$spool_max && Banana::$spool_max < $msgnum) {
192 $first = $last - Banana::$spool_max;
193 if ($first < 0) {
194 $first += $threshold;
195 }
196 }
197 $clean = $this->clean($first, $last, $msgnum);
198 $update = $this->update($first, $last, $msgnum);
199
200 if ($clean || $update) {
201 $this->saveToFile();
202 }
203 }
204
205 private function clean(&$first, &$last, $msgnum)
206 {
207 $do_save = false;
208 if (!empty($this->overview)) {
209 $mids = array_keys($this->overview);
210 foreach ($mids as $id) {
211 if (($first <= $last && ($id < $first || $id > $last))
212 || ($first > $last && $id < $first && $id > $last)) {
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;
222 }
223
224 private function update(&$first, &$last, $msgnum)
225 {
226 if ($first > $last || !$msgnum) {
227 return false;
228 }
229
230 $messages =& Banana::$protocole->getMessageHeaders($first, $last,
231 array('Date', 'Subject', 'From', 'Message-ID', 'References', 'In-Reply-To'));
232
233 // Build all the new Spool Heads
234 $time = time();
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 }
243 }
244
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 }
253
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 }
266 }
267 }
268 }
269 } else {
270 $messages = array();
271 }
272 Banana::$protocole->updateSpool($messages);
273 return true;
274 }
275
276 public function updateUnread($since)
277 {
278 if (empty($since)) {
279 return;
280 }
281
282 $newpostsids = Banana::$protocole->getNewIndexes($since);
283
284 if (empty($newpostsids)) {
285 return;
286 }
287
288 $newpostsids = array_intersect($newpostsids, array_keys($this->ids));
289 foreach ($newpostsids as $mid) {
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;
296 }
297 }
298 }
299 $this->unreadnb += count($newpostsids);
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 }
311 $msg = $this->ids[$msg]->id;
312 }
313 $this->markAsRead($msg);
314 }
315 }
316 }
317
318 public function setMode($mode)
319 {
320 $this->mode = $mode;
321 switch ($mode) {
322 case Banana::SPOOL_UNREAD:
323 $num = max(array_keys($this->overview));
324 if ($this->overview[$num]->isread) {
325 break;
326 }
327 foreach ($this->roots as &$root) {
328 if ($root->descunread == 0) {
329 $this->killdesc($root->id);
330 }
331 }
332 break;
333 }
334 }
335
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
354 /** Get the tree associated to a given id
355 */
356 public function &getTree($id)
357 {
358 return BananaTree::build($id)->show();
359 }
360
361 /** Mark the given id as read
362 * @param id MSGNUM of post
363 */
364 public function markAsRead($id)
365 {
366 $overview =& $this->overview[$id];
367 if (!$overview->isread) {
368 $overview->isread = true;
369 $this->unreadnb--;
370 while (!is_null($overview)) {
371 $overview->descunread--;
372 $overview =& $overview->parent;
373 }
374 }
375 }
376
377 /** Mark all unread messages as read
378 */
379 public function markAllAsRead(array &$array = null)
380 {
381 if (!$this->unreadnb) {
382 return;
383 }
384 if (is_null($array) && !empty($this->roots)) {
385 $array =& $this->roots;
386 } elseif (is_null($array)) {
387 return;
388 }
389 foreach ($array as &$msg) {
390 if (!$msg->isread) {
391 $this->markAsRead($msg->id);
392 if (!$this->unreadnb) {
393 return;
394 }
395 }
396 if ($msg->descunread) {
397 $this->markAllAsRead($msg->children);
398 }
399 }
400 }
401
402 /** kill post and childrens
403 * @param $_id MSGNUM of post
404 */
405 private function killdesc($_id)
406 {
407 $overview =& $this->overview[$_id];
408 $children =& $overview->children;
409 if (sizeof($children)) {
410 foreach ($children as &$c) {
411 $this->killdesc($c->id);
412 }
413 }
414 unset($this->overview[$_id]);
415 foreach ($this->roots as $k=>&$root) {
416 if ($root === $overview) {
417 unset($this->roots[$k]);
418 break;
419 }
420 }
421 unset($this->ids[$overview->msgid]);
422 $overview = null;
423 }
424
425 /** delete a post from overview
426 * @param $_id MSGNUM of post
427 */
428 public function delid($_id, $write = true)
429 {
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)) {
444 $time = time();
445 foreach ($parent->children as $key=>&$child) {
446 if ($child === $overview) {
447 unset($parent->children[$key]);
448 break;
449 }
450 }
451 if (sizeof($overview->children)) {
452 foreach ($overview->children as &$child) {
453 $parent->children[] =& $child;
454 $child->time = $time;
455 $child->parent =& $parent;
456 }
457 }
458 while (!is_null($parent)) {
459 $parent->desc--;
460 $parent->time = $time;
461 $parent =& $parent->parent;
462 }
463 }
464
465 // Remove all refenrences and assign null to the object
466 unset($this->ids[$overview->msgid]);
467 unset($this->overview[$_id]);
468 BananaTree::kill($_id);
469 foreach ($this->roots as $k=>&$root) {
470 if ($root === $overview) {
471 unset($this->roots[$k]);
472 break;
473 }
474 }
475 $overview = null;
476
477 if ($write) {
478 $this->saveToFile();
479 }
480 }
481
482 public function formatDate(BananaSpoolHead $head)
483 {
484 $stamp = $head->date;
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';
494 } elseif ($today < 90 + $dday) {
495 $format = '%a %e %b';
496 } else {
497 $format = '%a %e %b %Y';
498 }
499 return strftime($format, $stamp);
500 }
501
502 public function formatSubject(BananaSpoolHead $head)
503 {
504 $subject = $popup = $head->subject;
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 }
515 if (mb_strlen($subject) > 100) {
516 $subject = mb_substr($subject, 0, 99) . '&hellip;';
517 }
518 if ($head->id !== Banana::$artid) {
519 $subject = Banana::$page->makeLink(Array('group' => $this->group, 'artid' => $head->id,
520 'text' => $subject, 'popup' => $popup));
521 }
522 return $subject . $link;
523 }
524
525 public function formatFrom(BananaSpoolHead $head)
526 {
527 return BananaMessage::formatFrom($head->from);
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
545 /** Return root message of the given thread
546 * @param id INTEGER id of a message
547 */
548 public function &root($id)
549 {
550 $parent =& $this->overview[$id];
551 while (!is_null($parent->parent)) {
552 $parent =& $parent->parent;
553 }
554 return $parent;
555 }
556
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)) {
569 $test = banana_html_entity_decode($val[0]);
570 } else {
571 $test = banana_html_entity_decode($val);
572 }
573 }
574 $test = trim($test);
575 if ($test == $subject) {
576 return $id;
577 }
578 $id--;
579 }
580 return -1;
581 }
582
583 /** Returns previous thread root index
584 * @param id INTEGER message number
585 */
586 public function prevThread($id)
587 {
588 $root =& $this->root($id);
589 $last = null;
590 foreach ($this->roots as &$i) {
591 if ($i === $root) {
592 return $last;
593 }
594 $last = $i->id;
595 }
596 return $last;
597 }
598
599 /** Returns next thread root index
600 * @param id INTEGER message number
601 */
602 public function nextThread($id)
603 {
604 $root =& $this->root($id);
605 $ok = false;
606 foreach ($this->roots as &$i) {
607 if ($ok) {
608 return $i->id;
609 }
610 if ($i === $root) {
611 $ok = true;
612 }
613 }
614 return null;
615 }
616
617 /** Return prev post in the thread
618 * @param id INTEGER message number
619 */
620 public function prevPost($id)
621 {
622 $parent =& $this->overview[$id]->parent;
623 if (is_null($parent)) {
624 return null;
625 }
626 $last = $parent->id;
627 foreach ($parent->children as &$child) {
628 if ($child->id == $id) {
629 return $last;
630 }
631 $last = $child->id;
632 }
633 return null;
634 }
635
636 /** Return next post in the thread
637 * @param id INTEGER message number
638 */
639 public function nextPost($id)
640 {
641 $cur =& $this->overview[$id];
642 if (count($cur->children) != 0) {
643 return $cur->children[0]->id;
644 }
645
646 $parent =& $cur;
647 while (true) {
648 $parent =& $cur->parent;
649 if (is_null($parent)) {
650 return null;
651 }
652 $ok = false;
653 foreach ($parent->children as &$child) {
654 if ($ok) {
655 return $child->id;
656 }
657 if ($child === $cur) {
658 $ok = true;
659 }
660 }
661 $cur =& $parent;
662 }
663 return null;
664 }
665
666 /** Look for an unread message in the thread rooted by the message
667 * @param id INTEGER message number
668 */
669 private function _nextUnread(BananaSpoolHead $cur)
670 {
671 if (!$cur->isread) {
672 return $cur->id;
673 }
674 foreach ($cur->children as &$child) {
675 $unread = $this->_nextUnread($child);
676 if (!is_null($unread)) {
677 return $unread;
678 }
679 }
680 return null;
681 }
682
683 /** Find next unread message
684 * @param id INTEGER message number
685 */
686 public function nextUnread($id = null)
687 {
688 if (!$this->unreadnb) {
689 return null;
690 }
691
692 if (!is_null($id)) {
693 // Look in message children
694 foreach ($this->overview[$id]->children as &$child) {
695 $next = $this->_nextUnread($child);
696 if (!is_null($next)) {
697 return $next;
698 }
699 }
700 }
701
702 // Look in current thread
703 if (is_null($id)) {
704 $cur = null;
705 } else {
706 $cur =& $this->overview[$id];
707 }
708 do {
709 if (is_null($cur)) {
710 $parent =& $cur;
711 $ok = true;
712 } else {
713 $parent =& $cur->parent;
714 $ok = false;
715 }
716 if (!is_null($parent)) {
717 $array =& $parent->children;
718 } else {
719 $array =& $this->roots;
720 }
721 foreach ($array as &$child) {
722 if ($ok) {
723 $next = $this->_nextUnread($child);
724 if (!is_null($next)) {
725 return $next;
726 }
727 }
728 if ($child === $cur) {
729 $ok = true;
730 }
731 }
732 $cur =& $parent;
733 } while(!is_null($cur));
734 return null;
735 }
736 }
737 // vim:set et sw=4 sts=4 ts=4 fenc=utf-8:
738 ?>