Reduce mbox-helper overhead and fix some bugs
[banana.git] / banana / mbox.inc.php
index db3a1fd..01c329e 100644 (file)
@@ -72,16 +72,7 @@ class BananaMBox implements BananaProtocoleInterface
             $id = Banana::$spool->ids[$id];
         }
         $options = array ('-m ' . $id);
-        if (Banana::$spool->overview) {
-            if (Banana::$spool->overview[$id]) {
-               $options[] = '-p ' . $id . ':' . Banana::$spool->overview[$id]->storage['offset'];
-            } else {
-                $key       = max(array_keys(Banana::$spool->overview));
-                if ($key < $id) {
-                    $options[] = '-p ' . $key . ':' . Banana::$spool->overview[$key]->storage['offset'];
-                }
-            }
-        }
+        $this->getMBoxPosition($options, $id);
         return $this->callHelper('-b', $options);
     }
 
@@ -114,10 +105,10 @@ class BananaMBox implements BananaProtocoleInterface
     private function getCount()
     {
         $options = array();
-        if (Banana::$spool->overview) {
-            $key       = max(array_keys(Banana::$spool->overview));
-            $options[] = '-p ' . $key . ':' . Banana::$spool->overview[$key]->storage['offset'];
-        }
+        if (@filesize($this->getFileName()) == Banana::$spool->storage['size']) { 
+            return max(Banana::$spool->ids); 
+        } 
+        $this->getMBoxPosition($options);
         $val =& $this->callHelper('-c', $options);
         if (!$val) {
             return 0;
@@ -142,16 +133,7 @@ class BananaMBox implements BananaProtocoleInterface
         $headers = null;
         $options = array();
         $options[] = "-m $firstid:$lastid";
-        if (Banana::$spool->overview) {
-            if (isset(Banana::$spool->overview[$firstid])) {
-               $options[] = '-p ' . $firstid . ':' . Banana::$spool->overview[$firstid]->storage['offset'];
-            } else {
-                $key       = max(array_keys(Banana::$spool->overview));
-                if ($key < $firstid) {
-                    $options[] = '-p ' . $key . ':' . Banana::$spool->overview[$key]->storage['offset'];
-                }
-            }
-        }
+        $this->getMboxPosition($options, $firstid);
         $lines =& $this->callHelper('-d', $options, $msg_headers);
         if (!$lines) {
             return $headers;
@@ -174,20 +156,12 @@ class BananaMBox implements BananaProtocoleInterface
                     break;
                 }
                 $hval  = array_shift($lines);
-                if ($hval === '') {
-                    break;
-                }
                 if ($hname == 'date') {
                     $headers[$id][$hname] = @strtotime($hval);
                 } else {
                     $headers[$id][$hname] = $hval;
                 }
             }
-            if (!isset($headers[$id]['date'])) {
-                print_r($id);
-                print_r($offset);
-                print_r($headers[$id]);
-            }
         }
         array_walk_recursive($headers, array('BananaMimePart', 'decodeHeader'));
         return $headers;
@@ -202,6 +176,7 @@ class BananaMBox implements BananaProtocoleInterface
                 Banana::$spool->overview[$id]->storage['offset'] = $data['beginning'];
             }
         }
+        Banana::$spool->storage['size'] = @filesize($this->getFileName());
     }
 
     /** Return the indexes of the new messages since the give date
@@ -318,6 +293,25 @@ class BananaMBox implements BananaProtocoleInterface
 #######
 # MBox parser
 #######
+    
+    /** Add the '-p' optioin for callHelper
+     */
+    private function getMBoxPosition(array &$options, $id = null)
+    {
+        if (Banana::$spool->overview) {
+            if (!is_null($id) && isset(Banana::$spool->overview[$id])) {
+                $key = $id;
+            } else {
+                $key = max(Banana::$spool->ids);
+                if (!is_null($id) && $key >= $id) {
+                    return;
+                }
+            }
+            if (isset(Banana::$spool->overview[$key]->storage['offset'])) { 
+                $options[] = '-p ' . $key . ':' . Banana::$spool->overview[$key]->storage['offset'];
+            }
+        }
+    }
 
     private function &callHelper($action, array $options = array(), array $headers = array())
     {
@@ -329,7 +323,7 @@ class BananaMBox implements BananaProtocoleInterface
         exec($cmd, $out, $return);
         if ($this->debug) {
             $this->bt[] = array('action' => $cmd, 'time' => (microtime(true) - $start),
-                                'code' => $return, 'response' => count($out));
+                                'code' => $return, 'response' => count($out), 'error' => $return ? "Helper failed" : null);
         }
         if ($return != 0) {
             $this->_lasterrorno = 1;