Fix erroneous unread count in some specific cases.
[banana.git] / banana / nntp.inc.php
index f1dde9d..910075d 100644 (file)
@@ -14,8 +14,6 @@ require_once dirname(__FILE__) . '/protocoleinterface.inc.php';
 
 class BananaNNTP extends BananaNNTPCore implements BananaProtocoleInterface
 {
-    private $groupname = null;
-    private $description = null;
     private $ingroup = null;
 
     private $mode = null;
@@ -23,36 +21,32 @@ class BananaNNTP extends BananaNNTPCore implements BananaProtocoleInterface
 
     /** Build a protocole handler plugged on the given box
      */
-    public function __construct($box = null)
+    public function __construct()
     {
-        $url = parse_url(Banana::$host);
+        $url = parse_url(Banana::$nntp_host);
         if ($url['scheme'] == 'nntps' || $url['scheme'] == 'snntp') {
             $url['host'] = 'ssl://' . $url['host'];
-        }
-        if (!isset($url['port'])) {
+            if (!isset($url['port'])) {
+                $url['port'] = 563;
+            }
+        } else if (!isset($url['port'])) {
             $url['port'] = 119;
         }
-        if (!isset($url['user'])) {
-            parent::__construct($url['host'], $url['port']);
-        } else {
-            parent::__construct($url['host'], $url['port'], 120, false);
+        parent::__construct($url['host'], $url['port']);
+        if (isset($url['user'])) {
             $this->authinfo($url['user'], $url['pass']);
-        }      
-        $this->groupname = $box;
+        }
     }
 
     /** Return the descript;ion of the current box
      */
     public function getDescription()
     {
-        if ($this->description) {
-            return $this->description;
-        }
-        $descs = $this->xgtitle($this->groupname);
-        if (isset($descs[$this->groupname])) {
-            $this->description = $descs[$this->groupname];
+        $descs = $this->xgtitle(Banana::$group);
+        if (isset($descs[Banana::$group])) {
+            return trim(utf8_encode($descs[Banana::$group]));
         }
-        return $this->description;
+        return null;
     }
 
     /** Return the list of the boxes
@@ -69,7 +63,11 @@ class BananaNNTP extends BananaNNTPCore implements BananaProtocoleInterface
             } else {
                 $list = $this->listGroups();
                 if ($mode == Banana::BOXES_SUB) {
-                    $sub = array_flip(Banana::$profile['subscribe']);
+                    if (is_array(Banana::$profile['subscribe'])) {
+                        $sub = array_flip(Banana::$profile['subscribe']);
+                    } else {
+                        $sub = array();
+                    }
                     $list = array_intersect_key($list, $sub);
                 }
             }
@@ -80,10 +78,10 @@ class BananaNNTP extends BananaNNTPCore implements BananaProtocoleInterface
                     if (!is_utf8($desc)) {
                         $desc = utf8_encode($desc);
                     }
-                    $this->boxes[$group] = array('desc' => $desc);           
+                    $this->boxes[$group] = array('desc' => $desc);
                 } else {
                     $this->boxes[$group] = array('desc' => null);
-                }    
+                }
             }
             ksort($this->boxes);
         }
@@ -91,7 +89,12 @@ class BananaNNTP extends BananaNNTPCore implements BananaProtocoleInterface
             foreach ($this->boxes as $group=>&$desc) {
                 list($msgnum, $first, $last, $groupname) = $this->group($group);
                 $this->ingroup = $group;
-                $new = count($this->newnews($group, $since));
+                $new = $this->newnews($group, $since);
+                if (!is_array($new)) {
+                    $new = 0;
+                } else {
+                    $new = count($new);
+                }
                 $desc['msgnum'] = $msgnum;
                 $desc['unread'] = $new;
             }
@@ -101,25 +104,44 @@ class BananaNNTP extends BananaNNTPCore implements BananaProtocoleInterface
 
     /** Return a message
      * @param id Id of the emssage (can be either an Message-id or a message index)
-     * @param msg_headers Headers to process
-     * @param is_msgid If is set, $id is en Message-Id
      * @return A BananaMessage or null if the given id can't be retreived
      */
-    public function getMessage($id, array $msg_headers = array(), $is_msgid = false)
+    public function &getMessage($id)
     {
-        if (!$is_msgid && $this->groupname != $this->ingroup) {
+        $message = null;
+        if (is_numeric($id) && Banana::$group != $this->ingroup) {
             if (is_null(Banana::$spool)) {
-                $this->group($this->groupname);
-                $this->ingroup = $this->groupname;
+                $this->group(Banana::$group);
+                $this->ingroup = Banana::$group;
             } else {
                 $id = array_search($id, Banana::$spool->ids);
             }
         }
         $data = $this->article($id);
         if ($data !== false) {
-            return new BananaMessage($data);
+            $message = new BananaMessage($data);
         }
-        return null;
+        return $message;
+    }
+
+    /** Return the sources of the message
+     */
+    public function getMessageSource($id)
+    {
+        if (is_numeric($id) && Banana::$group != $this->ingroup) {
+            if (is_null(Banana::$spool)) {
+                $this->group(Banana::$group);
+                $this->ingroup = Banana::$group;
+            } else {
+                $id = array_search($id, Banana::$spool->ids);
+            }
+        }
+        $data = $this->article($id);
+        if ($data !== false) {
+            return implode("\n", $data);
+        }
+        $data = null;
+        return $data;
     }
 
     /** Return the indexes of the messages presents in the Box
@@ -127,8 +149,8 @@ class BananaNNTP extends BananaNNTPCore implements BananaProtocoleInterface
      */
     public function getIndexes()
     {
-        list($msgnum, $first, $last, $groupname) = $this->group($this->groupname);
-        $this->ingroup = $this->groupname;
+        list($msgnum, $first, $last, $groupname) = $this->group(Banana::$group);
+        $this->ingroup = Banana::$group;
         return array($msgnum, $first, $last);
     }
 
@@ -140,10 +162,11 @@ class BananaNNTP extends BananaNNTPCore implements BananaProtocoleInterface
         $messages = array();
         foreach ($msg_headers as $header) {
             $headers = $this->xhdr($header, $firstid, $lastid);
-            array_walk($headers, array('BananaMimePart', 'decodeHeader'));
             $header  = strtolower($header);
             if ($header == 'date') {
                 $headers = array_map('strtotime', $headers);
+            } else {
+                array_walk($headers, array('BananaMimePart', 'decodeHeader'));
             }
             foreach ($headers as $id=>&$value) {
                 if (!isset($messages[$id])) {
@@ -167,7 +190,7 @@ class BananaNNTP extends BananaNNTPCore implements BananaProtocoleInterface
      */
     public function getNewIndexes($since)
     {
-        return $this->newnews($this->groupname, $since);
+        return $this->newnews(Banana::$group, $since);
     }
 
     /** Return true if can post
@@ -193,7 +216,7 @@ class BananaNNTP extends BananaNNTPCore implements BananaProtocoleInterface
 
     /** Send the message
      */
-    public function send(BananaMessage &$message)
+    public function send(BananaMessage $message)
     {
         $sources = $message->get(true);
         return $this->post($sources);
@@ -201,13 +224,13 @@ class BananaNNTP extends BananaNNTPCore implements BananaProtocoleInterface
 
     /** Cancel the message
      */
-    public function cancel(BananaMessage &$message)
+    public function cancel(BananaMessage $message)
     {
         $headers = Array('From' => Banana::$profile['From'],
-                         'Newsgroups' => $this->groupname,
+                         'Newsgroups' => Banana::$group,
                          'Subject'    => 'cmsg ' . $message->getHeaderValue('message-id'),
                          'Control'    => 'cancel ' . $message->getHeaderValue('message-id'));
-        $headers = array_merge($headers, Banana::$custom_hdr);
+        $headers = array_merge($headers, Banana::$msgedit_headers);
         $body   = 'Message canceled with Banana';
         $msg    = BananaMessage::newMessage($headers, $body);
         return $this->send($msg);
@@ -219,29 +242,23 @@ class BananaNNTP extends BananaNNTPCore implements BananaProtocoleInterface
     {
         return 'NNTP';
     }
-}
 
-/*
-require_once dirname(__FILE__) . '/spool.inc.php';
-$time = microtime(true);
-$nntp = new BananaNNTP('xorg.promo.x2002');
-if (!$nntp->isValid()) {
-    echo "Beuh !\n";
-    exit;
+    /** Return the filename for the spool
+     */
+    public function filename()
+    {
+        $url  = parse_url(Banana::$nntp_host);
+        $file = '';
+        if (isset($url['host'])) {
+            $file .= $url['host'] . '_';
+        }
+        if (isset($url['port'])) {
+            $file .= $url['port'] . '_';
+        }
+        $file .= Banana::$group;
+        return $file;
+    }
 }
-Banana::$protocole =& $nntp;
-Banana::$spool =& BananaSpool::getSpool('xorg.promo.x2002');
-$msg = $nntp->getMessage(3424);
-echo '<html><head>
-        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
-        <link rel="stylesheet" type="text/css" href="css/banana.css"/>
-</head><body><table class="banana_msg"><tr><td>';
-//echo $msg->getFormattedBody('plain');
-echo $msg->getFormattedBody();
-echo '</td></tr></table></body></html>', "\n";
-$end = microtime(true);
-echo ($end - $time) . "s\n";
-*/ 
-
-// vim:set et sw=4 sts=4 ts=4:
+
+// vim:set et sw=4 sts=4 ts=4 enc=utf-8:
 ?>