Can configure spool root
[banana.git] / banana / mbox.inc.php
index c36bb5c..2a9cdae 100644 (file)
@@ -13,8 +13,7 @@ require_once dirname(__FILE__) . '/message.inc.php';
 
 class BananaMBox implements BananaProtocoleInterface
 {
-    private $boxname;
-
+    private $inbox        = null;
     private $file         = null;
     private $filesize     = null;
     private $current_id   = null;
@@ -30,41 +29,27 @@ class BananaMBox implements BananaProtocoleInterface
 
     /** Build a protocole handler plugged on the given box
      */
-    public function __construct($box = null)
+    public function __construct()
     {
-        $this->boxname = $box;
-        $filename = $this->getFileName($box);
-        if (is_null($filename)) {
-            return;
-        }
-        $this->filesize = filesize($filename);
-        $this->file = @fopen($filename, 'r');
-        if (!$this->file) {
-            $this->_lasterrno = 1;
-            $this->_lasterror = _b_('Can\'t open file');
-            $this->file = null;
-        }
-        $this->current_id   = 0;
-        $this->at_beginning = true;
+        $this->open();
     }
 
     /** Close the file
      */
     public function __destruct()
     {
-        if ($this->file) {
-            fclose($this->file);
-        }
+        $this->close();
     }
 
     /** Indicate if the Protocole handler has been succesfully built
      */
     public function isValid()
     {
-        return is_null($this->boxname) || !is_null($this->file);
+        return true;
+        //!Banana::$group || $this->file;
     }
     
-    /** Indicate last error n°
+    /** Indicate last error n°
      */
     public function lastErrNo()
     {
@@ -93,33 +78,57 @@ class BananaMBox implements BananaProtocoleInterface
      */
     public function getBoxList($mode = Banana::BOXES_ALL, $since = 0, $withstats = false)
     {
-        return array($this->boxname => array('desc' => '', 'msgnum' => 0, 'unread' => 0));
+        return array(Banana::$group => array('desc' => '', 'msgnum' => 0, 'unread' => 0));
     }
 
     /** 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 || !is_numeric($id)) {
-            if (is_null(Banana::$spool)) {
-                return null;
+        $this->open();
+        $message = null;
+        if (is_null($this->file)) {
+            return $message;
+        }
+        if (!is_numeric($id)) {
+            if (!Banana::$spool) {
+                return $message;
             }
             $id = Banana::$spool->ids[$id];
         }
-        $message = $this->readMessages(array($id));
-        if (empty($message)) {
-            return null;
+        $messages = $this->readMessages(array($id));
+        if (!empty($messages)) {
+            $message = new BananaMessage($messages[$id]['message']);
         }
-        $msg = new BananaMessage($message[$id]['message']);
-        return $msg;
+        return $message;    
     }
 
+    /** Return the sources of the given message
+     */
+    public function getMessageSource($id)
+    {
+        $this->open();
+        $message = null;
+        if (is_null($this->file)) {
+            return $message;
+        }
+        if (!is_numeric($id)) {
+            if (!Banana::$spool) { 
+                return $message;
+            }   
+            $id = Banana::$spool->ids[$id];
+        } 
+        $message = $this->readMessages(array($id));
+        return implode("\n", $message[$id]['message']);
+    }   
+
+    /** Compute the number of messages of the box
+     */
     private function getCount()
     {
+        $this->open();
         $this->count = count(Banana::$spool->overview);
         $max = @max(array_keys(Banana::$spool->overview));
         if ($max && Banana::$spool->overview[$max]->storage['next'] == $this->filesize) {
@@ -135,6 +144,10 @@ class BananaMBox implements BananaProtocoleInterface
      */
     public function getIndexes()
     {
+        $this->open();
+        if (is_null($this->file)) {
+            return array(0, 0, 0);
+        }
         if (is_null($this->count)) {
             $this->getCount();
         }
@@ -146,19 +159,23 @@ class BananaMBox implements BananaProtocoleInterface
      */
     public function &getMessageHeaders($firstid, $lastid, array $msg_headers = array())
     {
+        $this->open();
         $msg_headers = array_map('strtolower', $msg_headers);
         $messages =& $this->readMessages(range($firstid, $lastid), true);
         $msg_headers = array_map('strtolower', $msg_headers);
         $headers  = array();
+        if (is_null($this->file)) {
+            return $headers;
+        }
         foreach ($msg_headers as $header) {
             foreach ($messages as $id=>&$message) {
                 if (!isset($headers[$id])) {
                     $headers[$id] = array('beginning' => $message['beginning'], 'end' => $message['end']);
                 }
                 if ($header == 'date') {
-                    $headers[$id][$header] = strtotime($message['message'][$header]);
+                    $headers[$id][$header] = @strtotime($message['message'][$header]);
                 } else {
-                    $headers[$id][$header] = $message['message'][$header];
+                    $headers[$id][$header] = @$message['message'][$header];
                 }
             }
         }
@@ -184,6 +201,10 @@ class BananaMBox implements BananaProtocoleInterface
      */
     public function getNewIndexes($since)
     {
+        $this->open();
+        if (is_null($this->file)) {
+            return array();
+        }
         if (is_null($this->new_messages)) {
             $this->getCount(); 
         }
@@ -220,7 +241,19 @@ class BananaMBox implements BananaProtocoleInterface
      */
     public function send(BananaMessage &$message)
     {
-        return true;
+        $headers = $message->getHeaders();
+        $to      = $headers['To'];
+        $subject = $headers['Subject'];
+        unset($headers['To']);
+        unset($headers['Subject']);
+        $hdrs    = '';
+        foreach ($headers as $key=>$value) {
+            if (!empty($value)) {
+                $hdrs .= "$key: $value\r\n";
+            }    
+        }
+        $body = $message->get(false);
+        return mail($to, $subject, $body, $hdrs);
     }
 
     /** Cancel a message
@@ -238,27 +271,73 @@ class BananaMBox implements BananaProtocoleInterface
         return 'MBOX';
     }
 
+    /** Return the spool filename
+     */
+    public function filename()
+    {
+        @list($mail, $domain) = explode('@', Banana::$group);
+        $file = "";
+        if (isset($domain)) {
+            $file = $domain . '_';
+        }
+        return $file . $mail;
+    }
+
 #######
 # Filesystem functions
 #######
 
-    protected function getFileName($box)
+    protected function getFileName()
     {
-        if (is_null($box)) {
+        if (is_null(Banana::$group)) {
             return null;
         }
-        @list($mail, $domain) = explode('@', $box);
-        if ($mail == 'staff') {
-            return '/home/x2003bruneau/staff.polytechnique.org_innovation.mbox';
-        } else {
-            return '/var/mail/' . $mail;
-        }
+        @list($mail, $domain) = explode('@', Banana::$group);
+        return Banana::$mbox_path . '/' . $mail;
     }
 
 #######
 # MBox parser
 #######
 
+    private function open()
+    {
+        if ($this->inbox == Banana::$group) {
+            return;
+        }
+        $filename = $this->getFileName();
+        if (is_null($filename)) {
+            return;
+        }
+        $this->file = @fopen($filename, 'r');
+        if (!$this->file) {
+            $this->file = null;
+            $this->filesize = 0;
+        } else {
+            $this->filesize = filesize($filename);
+        }
+        $this->current_id   = 0;
+        $this->at_beginning = true;
+        $this->inbox        = Banana::$group;
+    }
+
+    private function close()
+    {
+        if (is_null($this->file)) {
+            return;
+        }
+        fclose($this->file);
+        $this->inbox        = null;
+        $this->file         = null;
+        $this->filesize     = null;
+        $this->current_id   = null;
+        $this->at_beginning = false;
+        $this->file_cache   = null;
+        $this->count        = null;
+        $this->new_messages = null;
+        $this->messages     = null;
+    }
+
     /** Go to the given message
      */
     private function goTo($id)
@@ -399,7 +478,7 @@ class BananaMBox implements BananaProtocoleInterface
      */
     private function &readMessages(array $ids, $strip = false, $from = false)
     {
-        if (!is_null($this->messages)) {
+        if ($this->messages) {
             return $this->messages;
         }
         sort($ids);
@@ -412,7 +491,11 @@ class BananaMBox implements BananaProtocoleInterface
             }
             if ($id != $this->current_id || !$this->at_beginning) {
                 if (!$this->goTo($id)) {
-                    continue;
+                    if (count($ids)) {
+                        continue;
+                    } else {
+                        break;
+                    }
                 }
             }
             $beginning = ftell($this->file);
@@ -427,5 +510,5 @@ class BananaMBox implements BananaProtocoleInterface
     }
 }
 
-// vim:set et sw=4 sts=4 ts=4:
+// vim:set et sw=4 sts=4 ts=4 enc=utf-8:
 ?>