Prevents error on newsgroup without any message.
[banana.git] / banana / spool.inc.php
index b6355b8..e0a144a 100644 (file)
@@ -54,28 +54,19 @@ class BananaSpoolHead
      */
     public function __construct($id, array &$message)
     {
+        list($name, $from) = BananaMessage::extractMail($message['from']);
         $this->id         = $id;
         $this->msgid      = @$message['message-id'];
         $this->date       = $message['date'];
         $this->subject    = @$message['subject'];
         $this->from       = $message['from'];
-        $this->color      = sprintf('#%06x', abs(crc32($this->from) % 0xffffff));
+        $this->color      = sprintf('#%06x', abs(crc32($from) % 0xffffff));
         $this->desc       = 1;
         $this->isread     = true;
         $this->descunread = 0;
-        if (preg_match("/^([^ ]+@[^ ]+) \((.*)\)$/", $this->from, $regs)) {
-            $this->name = $regs[2];
-        }
-        if (preg_match("/^\"?([^<>\"]+)\"? +<(.+@.+)>$/", $this->from, $regs)) {
-            $this->name = preg_replace("/^'(.*)'$/", '\1', $regs[1]);
-            $this->name = stripslashes($this->name);
-        }
-        if ($this->name) {
-            $this->name =  preg_replace("/\\\(\(|\))/","\\1", $this->name);
-        } else if (preg_match("/([^< ]+)@([^> ]+)/", $this->from, $regs)) {
-            $this->name = $regs[1];
-        } else {
-            $this->name = 'Anonymous';
+        $this->name       = $name;
+        if ($name === $from) {
+            $this->name   = 'Anonymous';
         }
     }
 }
@@ -164,7 +155,7 @@ class BananaSpool
         return $spool;
     }
 
-    private function compare(&$a, &$b)
+    private function compare($a, $b)
     {
         return ($b->date - $a->date);
     }
@@ -241,38 +232,42 @@ class BananaSpool
 
         // Build all the new Spool Heads
         $time = time();
-        foreach ($messages as $id=>&$message) {
-            if (!isset($this->overview[$id])) {
-                $this->overview[$id] = new BananaSpoolHead($id, $message);
-                $head =& $this->overview[$id];
-                $this->ids[$head->msgid] =& $head;
-                $head->time = $time;
-            }
-        }
-
-        // Build tree
-        $null = null;
-        foreach ($messages as $id=>&$message) {
-            $msg         =& $this->overview[$id];
-            $parents     =& $this->getReferences($message);
-            while (!empty($parents) && ($msg->parent === $msg || is_null($msg->parent))) {
-                @$msg->parent =& array_pop($parents);
-            }
-
-            if (!is_null($msg->parent)) {
-                $parent =& $msg->parent;
-                $parent->children[] =& $msg;
-                while (!is_null($parent)) {
-                    $parent->desc += $msg->desc;
-                    $parent->time  = $time;
-                    $prev =& $parent;
-                    if ($parent !== $parent->parent) {
-                        $parent =& $parent->parent;
-                    } else {
-                        $parent =& $null;
+        if (!empty($messages)) {
+            foreach ($messages as $id=>&$message) {
+                if (!isset($this->overview[$id])) {
+                    $this->overview[$id] = new BananaSpoolHead($id, $message);
+                    $head =& $this->overview[$id];
+                    $this->ids[$head->msgid] =& $head;
+                    $head->time = $time;
+                }
+            }
+
+            // Build tree
+            $null = null;
+            foreach ($messages as $id=>&$message) {
+                $msg         =& $this->overview[$id];
+                $parents     =& $this->getReferences($message);
+                while (!empty($parents) && ($msg->parent === $msg || is_null($msg->parent))) {
+                    @$msg->parent =& array_pop($parents);
+                }
+
+                if (!is_null($msg->parent)) {
+                    $parent =& $msg->parent;
+                    $parent->children[] =& $msg;
+                    while (!is_null($parent)) {
+                        $parent->desc += $msg->desc;
+                        $parent->time  = $time;
+                        $prev =& $parent;
+                        if ($parent !== $parent->parent) {
+                            $parent =& $parent->parent;
+                        } else {
+                            $parent =& $null;
+                        }
                     }
                 }
             }
+        } else {
+            $messages = array();
         }
         Banana::$protocole->updateSpool($messages);
         return true;
@@ -302,6 +297,22 @@ class BananaSpool
             }
         }
         $this->unreadnb += count($newpostsids);
+
+        if (function_exists('hook_listReadMessages')) {
+            $msgs = hook_listReadMessages($this->group);
+            if (!is_array($msgs)) {
+                return;
+            }
+            foreach ($msgs as $msg) {
+                if (!is_numeric($msg)) {
+                    if (!isset($this->ids[$msg])) {
+                        continue;
+                    }
+                    $msg = $this->ids[$msg]->id;
+                }
+                $this->markAsRead($msg);
+            }
+        }
     }
 
     public function setMode($mode)
@@ -344,7 +355,7 @@ class BananaSpool
      */
     public function &getTree($id)
     {
-        return BananaTree::build($id)->data;
+        return BananaTree::build($id)->show();
     }
 
     /** Mark the given id as read
@@ -352,12 +363,13 @@ class BananaSpool
      */
     public function markAsRead($id)
     {
-        if (!$this->overview[$id]->isread) {
-            $this->overview[$id]->isread = true;
+        $overview =& $this->overview[$id];
+        if (!$overview->isread) {
+            $overview->isread = true;
             $this->unreadnb--;
-            while (isset($id)) {
-                $this->overview[$id]->descunread--;
-                $id = $this->overview[$id]->parent;
+            while (!is_null($overview)) {
+                $overview->descunread--;
+                $overview =& $overview->parent;
             }
         }
     }
@@ -429,6 +441,7 @@ class BananaSpool
 
         // Remove from the message tree
         if (!is_null($parent)) {
+            $time = time();
             foreach ($parent->children as $key=>&$child) {
                 if ($child === $overview) {
                     unset($parent->children[$key]);
@@ -436,12 +449,12 @@ class BananaSpool
                 }
             }
             if (sizeof($overview->children)) {
-                $parent->children = array_merge($parent->children, $overview->children);
                 foreach ($overview->children as &$child) {
+                    $parent->children[] =& $child;
+                    $child->time   = $time;
                     $child->parent =& $parent;
                 }
             }
-            $time = time();
             while (!is_null($parent)) {
                 $parent->desc--;
                 $parent->time = $time;
@@ -466,7 +479,7 @@ class BananaSpool
         }
     }
 
-    public function formatDate(BananaSpoolHead &$head)
+    public function formatDate(BananaSpoolHead $head)
     {
         $stamp  = $head->date;
         $today  = intval(time() / (24*3600));
@@ -486,7 +499,7 @@ class BananaSpool
         return strftime($format, $stamp);
     }
 
-    public function formatSubject(BananaSpoolHead &$head)
+    public function formatSubject(BananaSpoolHead $head)
     {
         $subject = $popup = $head->subject;
         $popup = $subject;
@@ -499,14 +512,17 @@ class BananaSpool
         if (empty($subject)) {
             $subject = _b_('(pas de sujet)');
         }
-        if ($head->id != Banana::$artid) {
+        if (mb_strlen($subject) > 100) {
+            $subject = mb_substr($subject, 0, 99) . '&hellip;';
+        }
+        if ($head->id !== Banana::$artid) {
             $subject = Banana::$page->makeLink(Array('group' => $this->group, 'artid' => $head->id,
                                                      'text'  => $subject, 'popup' => $popup));
         }
         return $subject . $link;
     }
 
-    public function formatFrom(BananaSpoolHead &$head)
+    public function formatFrom(BananaSpoolHead $head)
     {
         return BananaMessage::formatFrom($head->from);
     }
@@ -526,37 +542,6 @@ class BananaSpool
         return Banana::$first ? Banana::$spool_tmax : Banana::$spool_tcontext;
     }
 
-
-    /** computes linear post index
-     * @param $_id INTEGER MSGNUM of post
-     * @return INTEGER linear index of post
-     */
-    public function getNdX($_id)
-    {
-        $ndx    = 1;
-        $id_cur = $_id;
-        while (true) {
-            $id_parent = $this->overview[$id_cur]->parent;
-            if (is_null($id_parent)) break;
-            $pos = array_search($id_cur, $this->overview[$id_parent]->children);
-
-            for ($i = 0; $i < $pos ; $i++) {
-                $ndx += $this->overview[$this->overview[$id_parent]->children[$i]]->desc;
-            }
-            $ndx++; //noeud père
-
-            $id_cur = $id_parent;
-        }
-
-        foreach ($this->roots as $i) {
-            if ($i==$id_cur) {
-                break;
-            }
-            $ndx += $this->overview[$i]->desc;
-        }
-        return $ndx;
-    }
-
     /** Return root message of the given thread
      * @param id INTEGER id of a message
      */
@@ -681,7 +666,7 @@ class BananaSpool
     /** Look for an unread message in the thread rooted by the message
      * @param id INTEGER message number
      */
-    private function _nextUnread(BananaSpoolHead &$cur)
+    private function _nextUnread(BananaSpoolHead $cur)
     {
         if (!$cur->isread) {
             return $cur->id;