Some improvements for banana 'stand-alone'
[banana.git] / banana / banana.inc.php.in
index af969b2..5a514bc 100644 (file)
@@ -1,49 +1,63 @@
 <?php
 /********************************************************************************
-* install.d/config.inc.php : configuration file
+* banana/banana.inc.php : banana main file
 * --------------------------
 *
 * This file is part of the banana distribution
 * Copyright: See COPYING files that comes with this distribution
 ********************************************************************************/
 
+require_once dirname(__FILE__) . '/text.func.inc.php';
+
 class Banana
 {
-    var $maxspool    = 3000;
-
-    var $hdecode     = array('from','name','organization','subject');
-    var $parse_hdr   = array('content-disposition', 'content-transfer-encoding',
-                             'content-type', 'content-id', 'date', 'followup-to',
-                             'from', 'message-id', 'newsgroups', 'organization',
-                             'references', 'subject', 'x-face');
-    var $show_hdr    = array('from', 'newsgroups', 'followup', 'date',
-                             'organization', 'references', 'x-face');
 
-    /** Favorites MIMEtypes to use, by order for reading multipart messages
-     */
-    var $body_mime   = array('text/plain', 'text/html', 'text/richtext');
-    /** Indicate wether posting attachment is allowed
-     */
-    var $can_attach  = true;
-    /** Maximum allowed file size for attachment
-     */
-    var $maxfilesize = 100000;
-    /** Indicate wether x-face should be skinned as specials data or not
-     */
-    var $formatxface = true;
+#######
+# Configuration variables
+#######
+
+### General ###
+    static public $profile = Array( 'signature'  => '',
+                                    'headers' => array('From' => 'Anonymous <anonymouse@example.com>'),
+                                    'display' => 0,
+                                    'lastnews' => 0,
+                                    'locale'  => 'fr_FR.UTF-8',
+                                    'subscribe' => array(),
+                                    'autoup' => 1);
+    static public $boxpattern;
+    static public $withtabs = true;
+    static public $baseurl   = null;
+    static public $mimeparts = array();
+
+### Spool ###
+    static public $spool_root    = '/var/spool/banana';
+    static public $spool_max     = 3000;
+    static public $spool_tbefore = 5;
+    static public $spool_tafter  = 5;
+    static public $spool_tmax    = 50;
+    static public $spool_boxlist = true;
+
+### Message processing ###
+    static public $msgparse_headers = array('content-disposition', 'content-transfer-encoding',
+                                       'content-type', 'content-id', 'date', 'followup-to',
+                                       'from', 'message-id', 'newsgroups', 'organization',
+                                       'references', 'subject', 'x-face', 'in-reply-to',
+                                       'to', 'cc', 'reply-to');
+
+### Message display ###
+    static public $msgshow_headers   = array('from', 'newsgroups', 'followup-to', 'to', 'cc', 'reply-to',
+                                       'organization', 'date', 'references', 'in-reply-to');
+    static public $msgshow_mimeparts = array('multipart/report', 'multipart/mixed', 
+                                             'text/html', 'text/plain', 'text/enriched', 'text', 'message');
+    static public $msgshow_xface     = true;
+    static public $msgshow_wrap      = 80;
+    static public $msgshow_externalimages = false;
+    static public $msgshow_hasextimages   = false;
+    static public $msgshow_withthread = true;
+    static public $msgshow_javascript = true;
 
-    /** Regexp for selecting newsgroups to show (if empty, match all newsgroups)
-     * ex : '^xorg\..*' for xorg.*
-     */
-    var $grp_pattern;
-
-    var $tbefore     = 5;
-    var $tafter      = 5;
-    var $tmax        = 50;
-
-    var $wrap        = 74;
     /** Match an url
-     * Should be included in a regexp delimited using ! (eg: "!$url_regexp!i")
+     * Should be included in a regexp delimited using /, !, , or @ (eg: "/$url_regexp/ui")
      * If it matches, return 3 main parts :
      *  \\1 and \\3 are delimiters
      *  \\2 is the url
@@ -53,523 +67,674 @@ class Banana
      *   $matches[2] = "http://www.polytechnique.org"
      *   $matches[3] = "]"
      */
-    var $url_regexp  = '(["\[])?((?:https?|ftp|news)://(?:&amp;|,*[a-z@0-9.~%$£µ&i#\-+=_/\?])*)(["\]])?';
+    static public $msgshow_url     = '(["\[\<])?((?:[a-z]+:\/\/|www\.)(?:[\.\,\;\!\:]*[a-z\@0-9~%$£µ&i#\-+=_\/\?]+)+)(["\]\>])?';
 
-    
-    /** Boundary for multipart messages
-     */
-    var $boundary    = 'bananaBoundary42';
+### Message edition ###
+    static public $msgedit_canattach  = true;
+    static public $msgedit_maxfilesize = 100000;
     /** Global headers to use for messages
      */
-    var $custom      = "Mime-Version: 1.0\nUser-Agent: Banana @VERSION@\n";
-    /** Global headers to use from multipart messages
+    static public $msgedit_headers  = array('Mime-Version' => '1.0', 'User-Agent' => 'Banana @VERSION@');
+    /** Mime type order for quoting
      */
-    var $custom_mp   = "Content-Type: multipart/mixed; boundary=\"bananaBoundary42\"\nContent-Transfer-Encoding: 7bit\n";
-    /** Body type when using plain text
-     */
-    var $custom_plain= "Content-Type: text/plain; charset=utf-8\nContent-Transfert-Encoding: 8bit\n"; 
-
+    static public $msgedit_mimeparts = array('multipart/report', 'multipart/mixed', 'text/plain', 'text/enriched', 'text/html', 'text', 'message');
+
+### Feed configuration ###
+    static public $feed_active         = true;
+    static public $feed_format         = 'rss2';
+    static public $feed_updateOnDemand = false; // Update the feed each time sbd check it
+    static public $feed_copyright      = null;  // Global copyright informations
+    static public $feed_generator      = 'Banana @VERSION@'; // Feed generator
+    static public $feed_email          = null;  // Admin e-mail
+    static public $feed_namePrefix     = 'Banana :: ';
+    static public $feed_size           = 15;    // Number of messages in the feed
+
+### Protocole ###
     /** News serveur to use
      */
-    var $host        = 'news://localhost:119/';
-
-    /** User profile
+    static public $nntp_host   = 'news://localhost:119/';
+
+    static public $mbox_path   = '/var/mail';
+    static public $mbox_helper = './mbox-helper';
+
+### Debug ###
+    static public $debug_nntp   = false;
+    static public $debug_mbox   = false;
+    static public $debug_smarty = false;
+
+
+#######
+# Constants
+#######    
+
+    // Actions
+    const ACTION_BOX_NEEDED = 1; // mask
+    const ACTION_BOX_LIST   = 2;
+    const ACTION_BOX_SUBS   = 4;
+    const ACTION_BOX_FEED   = 8;
+    const ACTION_MSG_LIST   = 3;
+    const ACTION_MSG_READ   = 5;
+    const ACTION_MSG_NEW    = 9;
+    const ACTION_MSG_CANCEL = 17;
+    const ACTION_MSG_IMAGES = 33;
+
+    // Box list view
+    const BOXES_ALL = 0;
+    const BOXES_SUB = 1;
+    const BOXES_NEW = 2;
+
+    // Spool view mode
+    const SPOOL_ALL    = 0;
+    const SPOOL_UNREAD = 1;
+
+
+#######
+# Runtime variables
+#######
+
+    static public $protocole   = null;
+    static public $spool       = null;
+    static public $message     = null;
+    static public $page        = null;
+
+    static public $group       = null;
+    static public $artid       = null;
+    static public $action      = null;
+    static public $part        = null;
+    static public $first       = null;
+
+    /** Class parameters storage
      */
-    var $profile     = Array( 'name' => 'Anonymous <anonymouse@example.com>', 'sig'  => '', 'org'  => '',
-            'customhdr' =>'', 'display' => 0, 'lastnews' => 0, 'locale'  => 'fr_FR', 'subscribe' => array());
-    
-    var $state       = Array('group' => null, 'artid' => null, 'action' => null);
-    var $nntp;
-    var $groups;
-    var $newgroups;
-    var $post;
-    var $spool;
+    public $params;
 
-    var $get;
 
-    function Banana()
-    {
-        $this->_require('NetNNTP');
-        setlocale(LC_ALL,  $this->profile['locale']);
-        $this->nntp = new nntp($this->host);
-        if (!$this->nntp || !$this->nntp->valid) {
-            $this->nntp = null;
-        }
-    }
+#######
+# Banana Implementation
+#######
 
-    /** Run Banana
-     * @param STRING class Name of the class to use
-     * @param ARRAY  myget If defined is used instead of get
+    /** Build the instance of Banana
+     * This constructor only call \ref loadParams, connect to the server, and build the Smarty page
+     * @param protocole Protocole to use
      */
-    function run($class = 'Banana', $myget = null)
+    public function __construct($params = null, $protocole = 'NNTP', $pageclass = 'BananaPage')
     {
-        global $banana;
-
-        Banana::_require('misc');
-        $banana = new $class();
-
-        if (is_null($myget)) {
-            $banana->get = $_GET;
+        if (is_null($params)) {
+            $this->params = $_GET;
         } else {
-            $banana->get = $myget;
-        }
-
-        if (!$banana->nntp) {
-            $banana->state['page'] = 'error';
-            return makeTable('<p class="error">'._b_('Impossible de contacter le serveur').'</p>');
-        }
-
-        $group  = empty($banana->get['group']) ? null : $banana->get['group'];
-        if (!is_null($group)
-                &&  isset($banana->grp_pattern) && !preg_match('/' . $banana->grp_pattern . '/', $group)) {
-            $banana->state['page'] = 'error';
-            return makeTable('<p class="error">'
-                    . $group . _b_(' : ce newsgroup n\'existe pas ou vous n\'avez pas l\'autorisation d\'y accéder')
-                    . '</p>');
-        }
-        $artid  = empty($banana->get['artid']) ? null : strtolower($banana->get['artid']);
-        $partid = !isset($banana->get['part']) ? -1 : $banana->get['part'];
-        $action = !isset($banana->get['action']) ? null : $banana->get['action'];
-        $banana->state = Array ('group' => $group, 'artid' => $artid, 'action' => $action);
+            $this->params = $params;
+        }
+        $this->loadParams();
+
+        // connect to protocole handler
+        $classname = 'Banana' . $protocole;
+        if (!class_exists($classname)) {
+            Banana::load($protocole);
+        }    
+        Banana::$protocole = new $classname(Banana::$group);
+
+        // build the page
+        if ($pageclass == 'BananaPage') {
+            Banana::load('page');
+        }
+        Banana::$page = new $pageclass;
+        $types = array('multipart/report' => _b_('Rapport d\'erreur'),
+                       'multipart/mixed'  => _b_('Composition'),
+                       'text/html'        => _b_('Texte formaté'),
+                       'text/plain'       => _b_('Texte brut'),
+                       'text/enriched'    => _b_('Texte enrichi'),
+                       'text'             => _b_('Texte'),
+                       'message/rfc822'   => _b_('Mail'),
+                       'message'          => _b_('Message'),
+                       'source'           => _b_('Source'));
+        Banana::$mimeparts = array_merge($types, Banana::$mimeparts);
+    }
 
-        if (is_null($group)) {
-            if (isset($banana->get['subscribe'])) {
-                $banana->state['page'] = 'subscribe';
-                return makeTable($banana->action_listSubs());
-            } elseif (isset($_POST['validsubs'])) {
-                $banana->action_saveSubs();
+    /** Fill state vars (Banana::$group, Banana::$artid, Banana::$action, Banana;:$part, Banana::$first)
+     */
+    protected function loadParams()
+    {
+        Banana::$group = isset($this->params['group']) ? $this->params['group'] : null;
+        Banana::$artid = isset($this->params['artid']) ? $this->params['artid'] : null;
+        Banana::$first = isset($this->params['first']) ? $this->params['first'] : null;
+        Banana::$part  = isset($this->params['part']) ? $this->params['part'] : 'text';
+
+        $action = @$this->params['action'];
+        if ($action == 'rss' || $action == 'rss2' || $action == 'atom') {
+            if ($action == 'rss') {
+                $action = 'rss2';
             }
-            $banana->state['page'] = 'forums';
-            return makeTable($banana->action_listGroups());
-
-        } elseif (is_null($artid)) {
-            if (isset($_POST['action']) && $_POST['action'] == 'new') {
-                return makeTable($banana->action_doFup($group, isset($_POST['artid']) ? intval($_POST['artid']) : -1));
-            } elseif ($action == 'new') {
-                $banana->state['page'] = 'action';
-                return makeTable($banana->action_newFup($group));
+            Banana::$feed_format = $action;
+            Banana::$action = Banana::ACTION_BOX_FEED;
+            return;
+        }
+    
+        // Look for the action to execute
+        if (is_null(Banana::$group)) {
+            if ($action  == 'subscribe') {
+                Banana::$action = Banana::ACTION_BOX_SUBS;
             } else {
-                $banana->state['page'] = 'group';
-                return makeTable($banana->action_showThread($group, isset($banana->get['first']) ? intval($banana->get['first']) : 1));
+                Banana::$action = Banana::ACTION_BOX_LIST;
             }
-
-        } else {
-            if (isset($_POST['action']) && $_POST['action']=='cancel') {
-                $res = $banana->action_cancelArticle($group, $artid);
+            return;
+        }
+        
+        if (is_null(Banana::$artid)) {
+            if ($action == 'new') {
+                Banana::$action = Banana::ACTION_MSG_NEW;
             } else {
-                $res = '';
-            }
-
-            if (!is_null($action)) {
-                $banana->state['page'] = 'action';
-                switch ($action) {
-                    case 'cancel':
-                        $res .= $banana->action_showArticle($group, $artid, $partid);
-                        if ($banana->post->checkcancel()) {
-                            $form = '<p class="error">'._b_('Voulez-vous vraiment annuler ce message ?').'</p>'
-                                  . '<form action="' 
-                                  . htmlentities(makeLink(Array('group' => $group,
-                                                                'artid' => $artid)))
-                                  . '" method="post"><p>'
-                                  . '<input type="hidden" name="action" value="cancel" />'
-                                  . '<input type="submit" value="Annuler !" />'
-                                  . '</p></form>';
-                            return makeTable($form . $res);
-                        }
-                        return makeTable("" . $res);
-
-                    case 'new':
-                        return makeTable($banana->action_newFup($group, $artid));
-                }
-            }
-
-            if (isset($banana->get['pj'])) {
-                $view = false;
-                if ($action == 'view') {
-                    $view = true;
-                }
-                $att = $banana->action_getAttachment($group, $artid, $banana->get['pj'], $view);
-                return makeTable($res . $att);
+                Banana::$action = Banana::ACTION_MSG_LIST;
             }
-            
-            $banana->state['page'] = 'message';
-            return makeTable($banana->action_showArticle($group, $artid, $partid));
+            return;
+        }
+        switch ($action) {
+          case 'new':
+            Banana::$action = Banana::ACTION_MSG_NEW;
+            return;
+          case 'cancel':
+            Banana::$action = Banana::ACTION_MSG_CANCEL;
+            return;
+          case 'showext':
+            Banana::$action = Banana::ACTION_MSG_IMAGES;
+            return;
+          default:
+            Banana::$action = Banana::ACTION_MSG_READ;
         }
     }
 
-    /**************************************************************************/
-    /* actions                                                                */
-    /**************************************************************************/
-
-    function action_saveSubs()
-    {
-        return;
-    }
-
-    function action_listGroups()
+    /** Run Banana
+     * This function need user profile to be initialised
+     */
+    public function run()
     {
-        $this->_newGroup();
+        // Configure locales
+        setlocale(LC_ALL,  Banana::$profile['locale']);
         
-        $res  = $this->groups->to_html();
-        if (count($this->newgroups->overview)) {
-            $res .= '<p>'._b_('Les forums suivants ont été créés depuis ton dernier passage :').'</p>';
-            $res .= $this->newgroups->to_html();
+        // Check if the state is valid
+        if (Banana::$protocole->lastErrNo()) {
+            return Banana::$page->kill(_b_('Une erreur a été rencontrée lors de la connexion au serveur') . '<br />'
+                                      . Banana::$protocole->lastError());
+        }
+        if (!Banana::$protocole->isValid()) {
+            return Banana::$page->kill(_b_('Connexion non-valide'));
+        }
+        if (Banana::$action & Banana::ACTION_BOX_NEEDED) {
+            if(Banana::$boxpattern && !preg_match('/' . Banana::$boxpattern . '/i', Banana::$group)) {
+                Banana::$page->setPage('group');        
+                return Banana::$page->kill(_b_("Ce newsgroup n'existe pas ou vous n'avez pas l'autorisation d'y accéder"));
+            }
         }
 
-        $this->nntp->quit();
-        return $res;
+        // Dispatch to the action handlers
+        switch (Banana::$action) {
+          case Banana::ACTION_BOX_SUBS:
+            $error = $this->action_subscribe();
+            break;
+          case Banana::ACTION_BOX_LIST:
+            $error = $this->action_listBoxes();
+            break;
+          case Banana::ACTION_BOX_FEED:
+            $this->action_feed(); // generate its own xml
+            break;
+          case Banana::ACTION_MSG_LIST:
+            $error = $this->action_showThread(Banana::$group, Banana::$first);
+            break;
+          case Banana::ACTION_MSG_IMAGES:
+            Banana::$msgshow_externalimages = true;
+          case Banana::ACTION_MSG_READ:
+            $error = $this->action_showMessage(Banana::$group, Banana::$artid, Banana::$part);
+            break;
+          case Banana::ACTION_MSG_NEW:
+            $error = $this->action_newMessage(Banana::$group, Banana::$artid);
+            break;
+          case Banana::ACTION_MSG_CANCEL:
+            $error = $this->action_cancelMessage(Banana::$group, Banana::$artid);
+            break;
+          default:
+            $error = _b_("L'action demandée n'est pas supportée par Banana");
+        }
+
+        // Generate the page
+        if (is_string($error)) {
+            return Banana::$page->kill($error);
+        }
+        return Banana::$page->run();
     }
 
-    function action_listSubs()
+    /** Build and post a new message
+     * @return postid (or -1 if the message has not been found)
+     */
+    public function post($dest, $reply, $subject, $body)
     {
-        $this->_require('groups');
-        $this->groups = new BananaGroups(BANANA_GROUP_ALL);
-        
-        $res  = $this->groups->to_html(true);
+        $hdrs = Banana::$protocole->requestedHeaders();
+        $headers                 = Banana::$profile['headers'];
+        $headers[$hdrs['dest']]  = $dest;
+        if ($reply) {
+            $headers[$hdrs['reply']] = $reply;
+        } 
+        $headers['Subject']      = $subject;
+        $msg = BananaMessage::newMessage($headers, $body);
+        if (Banana::$protocole->send($msg)) {
+            Banana::$group = ($reply ? $reply : $dest);
+            $this->loadSpool(Banana::$group);
+            return Banana::$spool->getPostId($subject);
+        }
+        return -1;
+    }
 
-        $this->nntp->quit();
-        return $res;
+    /** Return the CSS code to include in the headers
+     */
+    public function css()
+    {
+        return Banana::$page->css;
     }
 
-    function action_showThread($group, $first)
+    /** Return the Link to the feed of the page
+     */
+    public function feed()
     {
-        if (!$this->_newSpool($group, $this->profile['display'], $this->profile['lastnews'])) {
-            return '<p class="error">'._b_('Impossible charger la liste des messages de ') . $group . '</p>';
+        if (!Banana::$feed_active) {
+            return null;
         }
+        return Banana::$page->makeURL(array('group' => Banana::$group, 'action' => Banana::$feed_format));
+    }
 
-        if ($first > count($this->spool->overview)) {
-            $first = count($this->spool->overview);
+    /** Return the execution backtrace of the current BananaProtocole
+     */
+    public function backtrace()
+    {
+        if (Banana::$protocole) {
+            return Banana::$protocole->backtrace();
         }
-
-        $first = $first - ($first % $this->tmax) + 1;
-        
-        $pages = displayPages($first);
-        $res  = $pages . $this->spool->to_html($first, $first+$this->tmax) . $pages;
-
-        $this->nntp->quit();
-        
-        return $res;
+        return null;
     }
 
-    function action_showArticle($group, $id, $part)
+    /**************************************************************************/
+    /* actions                                                                */
+    /**************************************************************************/
+    protected function action_saveSubs($groups)
     {
-        if (!$this->_newSpool($group, $this->profile['display'], $this->profile['lastnews'])) {
-            return '<p class="error">'._b_('Impossible charger la liste des messages de ') . $group . '</p>';
-        }
+        Banana::$profile['subscribe'] = $groups;
+        return true;
+    }
 
-        if (!$this->_newPost($id)) {
-            if ($this->nntp->lasterrorcode == "423") {
-                $this->spool->delid($id);
-            }
-            $this->nntp->quit();
-            return '<p class="error">'._b_('Impossible d\'accéder au message.   Le message a peut-être été annulé').'</p>';
+    protected function action_subscribe()
+    {
+        Banana::$page->setPage('subscribe');
+        if (isset($_POST['validsubs'])) {
+            $this->action_saveSubs(array_keys($_POST['subscribe']));
+            Banana::$page->redirect();
         }
-
-        $res = $this->post->to_html($part);
-
-        $this->nntp->quit();
-        
-        return $res;
+        $groups = Banana::$protocole->getBoxList(Banana::BOXES_ALL);
+        Banana::$page->assign('groups', $groups);
+        return true;
     }
 
-    function action_getAttachment($group, $id, $pjid, $action)
+    protected function action_listBoxes()
     {
-        if (!$this->_newSpool($group, $this->profile['display'], $this->profile['lastnews'])) {
-            return '<p class="error">'._b_('Impossible charger la liste des messages').'</p>';
+        Banana::$page->setPage('forums');
+        $groups    = Banana::$protocole->getBoxList(Banana::BOXES_SUB, Banana::$profile['lastnews'], true);
+        Banana::$page->assign('groups', $groups);
+        if (empty(Banana::$profile['subscribe']) || Banana::$profile['lastnews']) {
+            $newgroups = Banana::$protocole->getBoxList(Banana::BOXES_NEW, Banana::$profile['lastnews'], true);
+            Banana::$page->assign('newgroups', $newgroups);
         }
+        return true;
+    }
 
-        if (!$this->_newPost($id)) {
-            if ($this->nntp->lasterrorcode == "423") {
-                $this->spool->delid($id);
+    protected function action_feed()
+    {
+        Banana::load('feed');
+        if (Banana::$group) {
+            if (Banana::$feed_updateOnDemand) { 
+                $this->loadSpool(Banana::$group); 
+            } 
+            $feed =& BananaFeed::getFeed();
+            $feed->toXML();
+        }
+        if (Banana::$profile['subscribe']) {
+            $subfeed = null;
+            foreach (Banana::$profile['subscribe'] as $group) {
+                Banana::$group = $group;
+                if (Banana::$feed_updateOnDemand) {
+                    $this->loadSpool($group);
+                }
+                $feed =& BananaFeed::getFeed();
+                $subfeed =& BananaFeed::merge($subfeed, $feed, _b_('Abonnements'), _b_('Mes abonnements Banana'));
             }
-            $this->nntp->quit();
-            return '<p class="error">'._b_('Impossible d\'accéder au message.   Le message a peut-être été annulé').'</p>';
-        }
-
-        $this->nntp->quit();
-        if ($this->post->get_attachment($pjid, $action)) {
-            return "";
-        } else {
-            return '<p calss="error">'._b_('Impossible d\'accéder à la pièce jointe.').'</p>';
+            $subfeed->toXML();
         }
+        Banana::$page->feed();
     }
 
-    function action_cancelArticle($group, $id)
+    protected function action_showThread($group, $first)
     {
-        if (!$this->_newSpool($group, $this->profile['display'], $this->profile['lastnews'])) {
-            return '<p class="error">'._b_('Impossible charger la liste des messages').'</p>';
-        }
-
-        if (!$this->_newPost($id)) {
-            return '<p class="error">'._b_('Impossible de trouver le message à annuler').'</p>';
-        }
-        $mid  = array_search($id, $this->spool->ids);
-
-        if (!$this->post->checkcancel()) {
-            return '<p class="error">'._b_('Vous n\'avez pas les permissions pour annuler ce message').'</p>'; 
-        }
-        $msg = 'From: '.$this->profile['name']."\n"
-             . "Newsgroups: $group\n"
-             . "Subject: cmsg $mid\n"
-             . $this->custom
-             . "Control: cancel $mid\n"
-             . "\n"
-             . "Message canceled with Banana";
-        if ($this->nntp->post($msg)) {
-            $this->spool->delid($id);
-            $this->nntp->quit();
-            redirectInBanana(Array('group' => $group,
-                           'first' => $id));
-        } else {
-            return '<p class="error">'._b_('Impossible d\'annuler le message').'</p>';
+        Banana::$page->setPage('thread');
+        if (!$this->loadSpool($group)) {
+            return _b_('Impossible charger la liste des messages de ') . $group;
+        }
+        if (Banana::$spool_boxlist) {
+            $groups = Banana::$protocole->getBoxList(Banana::BOXES_SUB, Banana::$profile['lastnews'], true);
+            Banana::$page->assign('groups', $groups);
         }
+        Banana::$page->assign('msgbypage', Banana::$spool_tmax);
+        return true;
     }
 
-    function action_newFup($group, $id = -1)
+    protected function action_showMessage($group, $artid, $partid = 'text')
     {
-        $subject = $body = '';
-        $target  = $group;
-        
-        if (@$_POST['action'] == 'new') {
-            $subject  = $_POST['subject'];
-            $body     = $_POST['body'];
-            $target   = $_POST['newsgroups'];
-            $followup = $_POST['followup'];
-            $this->state['page']   = 'action';
-            $this->state['group']  = $group;
-            $this->state['action'] = 'new';
-            if ($id != -1) {
-                $this->state['artid'] = $id;
+        Banana::$page->setPage('message');
+        $istext = $partid == 'text' || $partid == 'source'
+                || preg_match('!^[-a-z0-9_]+/[-a-z0-9_]+$!', $partid);
+        if ($istext) {
+            $this->loadSpool($group);
+        }
+        $msg =& $this->loadMessage($group, $artid);
+        if (is_null($msg)) {
+            $this->loadSpool($group);
+            $this->removeMessage($group, $artid);
+            return _b_('Le message demandé n\'existe pas. Il est possible qu\'il ait été annulé');
+        }
+        if ($partid == 'xface') {
+            $msg->getXFace();
+            exit;
+        } elseif (!$istext) {
+            $part = $msg->getPartById($partid);
+            if (!is_null($part)) {
+                $part->send(true);
+            }
+            $part = $msg->getFile($partid);
+            if (!is_null($part)) {
+                $part->send();
             }
-        } elseif ($id > 0) {
-            $this->nntp->group($group);
-            if ($this->_newPost($id)) {
-                $subject  = 'Re: ' . preg_replace("/^re\s*:\s*/i", '', $this->post->headers['subject']);
-                $body     = utf8_encode($this->post->name." "._b_("a écrit"))." :\n"
-                                        . wrap($this->post->get_body(), "> ")
-                          . $this->profile['sig'] ? "\n\n-- \n". $this->profile['sig'] : '';
-                $target   = isset($this->post->headers['followup-to']) ?
-                                $this->post->headers['followup-to'] : $this->post->headers['newsgroups'];
-                $followup = null;
+            exit;
+        } elseif ($partid == 'text') {
+            $partid = null;
+            Banana::$page->assign('body', $msg->getFormattedBody($partid));
+        } elseif ($partid == 'source') {
+            $text = Banana::$protocole->getMessageSource($artid);
+            if (!is_utf8($text)) {
+                $text = utf8_encode($text);
             }
+            Banana::$page->assign('body', '<pre>' . banana_htmlentities($text) . '</pre>');
         } else {
-            $subject = $target = $followup = null;
-            $body = $this->profile['sig'] ? "\n\n-- \n". $this->profile['sig'] : '';
-        }
-
-        $this->nntp->quit();
-
-        $html  = '<form enctype="multipart/form-data" action="'
-               . htmlentities(makeLink(Array('group' => $group)))
-               . '" method="post" accept-charset="utf-8">'
-               . '<table class="bicol" cellpadding="0" cellspacing="0">'
-               . '<tr><th colspan="2">' . _b_('En-têtes') . '</th></tr>'
-               . '<tr><td>' . _b_('Nom') . '</td>'
-               . '<td>' . htmlentities($this->profile['name']) . '</td></tr>'
-               . '<tr><td>' . _b_('Sujet') . '</td>'
-               . '<td><input type="text" name="subject" value="' . htmlentities($subject) . '" size="60" /></td></tr>'
-               . '<tr><td>' . _b_('Forums') . '</td>'
-               . '<td><input type="text" name="newsgroups" value="' . htmlentities($target) . '" size="60" /></td></tr>'
-               . '<tr><td>' . _b_('Suivi à') . '</td>'
-               . '<td><input type="text" name="followup" value="' . htmlentities($followup). '" size="60" /></td></tr>'
-               . '<tr><td>' . _b_('Organisation') . '</td>'
-               . '<td>' . $this->profile['org'] . '</td></tr>'
-               . '<tr><th colspan="2">' . _b_('Corps') . '</th></tr>'
-               . '<tr><td colspan="2"><textarea name="body" cols="74" rows="16">'
-               .  $body . '</textarea></td></tr>';
-        if ($this->can_attach) {
-            $html .= '<tr><th colspan="2">' . _b_('Pièce jointe') . '</th></tr>'
-                  . '<tr><td colspan="2">'
-                  . '<input type="hidden" name="MAX_FILE_SIZE" value="' . $this->maxfilesize . '" />'
-                  . '<input type="file" name="newpj" size="40"/></td></tr>';
-        }
-        $html .= '<tr><th colspan="2">';
-        if ($id != -1) {
-            $html .= '<input type="hidden" name="artid" value="' . $id . '" />';
-        }
-        $html .= '<input type="hidden" name="action" value="new" />'
-              . '<input type="submit" value="' . _b_('Envoyer le message') . '" /></th></tr>'
-              . '</table></form>';
-
-        return $html;
+            Banana::$page->assign('body', $msg->getFormattedBody($partid));
+        }
+
+        if (Banana::$profile['autoup']) {
+            Banana::$spool->markAsRead($artid);
+        }
+        if (Banana::$spool_boxlist) {
+            $groups    = Banana::$protocole->getBoxList(Banana::BOXES_SUB, Banana::$profile['lastnews'], true);
+            Banana::$page->assign('groups', $groups);
+        }    
+        Banana::$page->assign_by_ref('message', $msg);
+        Banana::$page->assign('extimages', Banana::$msgshow_hasextimages);
+        Banana::$page->assign('headers', Banana::$msgshow_headers);
+        Banana::$page->assign('type', $partid);
+        return true;
     }
 
-    function action_doFup($group, $artid = -1)
+    protected function action_newMessage($group, $artid)
     {
-        if ( ! (is_utf8($_POST['subject']) && is_utf8($_POST['body']))) {
-            foreach(Array('subject', 'body') as $key) {
-                $_POST[$key] = utf8_encode($_POST[$key]);
+        Banana::$page->setPage('new');
+        if (!Banana::$protocole->canSend()) {
+            return _b_('Vous n\'avez pas le droit de poster');
+        }
+        $hdrs    = Banana::$protocole->requestedHeaders();
+        $headers = array();
+        foreach ($hdrs as $header) {
+            $headers[$header] = array('name' => BananaMessage::translateHeaderName($header));
+            if (isset(Banana::$profile['headers'][$header])) {
+                $headers[$header]['fixed'] = Banana::$profile['headers'][$header];
             }
         }
-       
-        $forums = preg_split('/\s*(,|;)\s*/', $_POST['newsgroups']);
-        $fup    = $_POST['followup'];
-        if (sizeof($forums) > 1) {
-            if (empty($fup)) {
-                $fup = $forums[0];
+        if (isset($_POST['sendmessage'])) {
+            $hdr_values = array();
+            foreach ($hdrs as $header) {
+                $hdr_values[$header] = isset($headers[$header]['fixed']) ? $headers[$header]['fixed'] : @$_POST[$header];
+                if (!is_utf8($hdr_values[$header])) {
+                    $hdr_values[$header] = utf8_encode($hdr_values[$header]);
+                }
+                if ($headers != 'Subject') {
+                    $hdr_values[$header] = str_replace(', ', ',', $hdr_values[$header]);
+                }
             }
-        }
-        $to     = implode(',', $forums);
-        
-        if (!$this->_newSpool($group, $this->profile['display'], $this->profile['lastnews'])) {
-            return '<p class="error">'._b_('Impossible charger la liste des messages').'</p>';
-        }
-        
-        $body = preg_replace("/\n\.[ \t\r]*\n/m", "\n..\n", $_POST['body']);
-        $msg  = 'From: ' . $this->profile['name'] . "\n"
-              . "Newsgroups: ". $to . "\n"
-              . "Subject: " . headerEncode($_POST['subject'], 128) . "\n"
-              . (empty($this->profile['org']) ? '' : "Organization: {$this->profile['org']}\n")
-              . (empty($fup) ? '' : 'Followup-To: ' . $fup . "\n");
-
-        if ($artid != -1) {
-            $this->_require('post');
-            $post = new BananaPost($artid);
-            if (!$post || !$post->valid) {
-                return '<p class="error">'._b_('Impossible charger le message d\'origine').'</p>';
+            if (!is_null($artid)) {
+                $old =& $this->loadMessage($group, $artid);
+                $hdr_values['References'] = $old->getHeaderValue('references') . $old->getHeaderValue('message-id');
             }
-            $refs = ( isset($post->headers['references']) ? $post->headers['references']." " : "" );
-            $msg .= "References: $refs{$post->headers['message-id']}\n";
-        }
-
-        $body_headers  = $this->custom_plain;
-        $body          = wrap($body, "");
-
-        // include attachment in the body
-        $uploaded = $this->_upload('newpj');
-        switch ($uploaded['error']) {
-            case UPLOAD_ERR_OK:
-                $this->custom = $this->custom_mp.$this->custom;
-                $body         = $this->_make_part($body_headers, $body);
-                $file_head    = 'Content-Type: '.$uploaded['type'].'; name="'.$uploaded['name']."\"\n"
-                              . 'Content-Transfer-Encoding: '.$uploaded['encoding']."\n"
-                              . 'Content-Disposition: attachment; filename="'.$uploaded['name']."\"\n";
-                $body        .= $this->_make_part($file_head, $uploaded['data']);
-                $body        .= "\n--".$this->boundary.'--';
-                break;
-
-            case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE:
-                return '<p class="error">'._b_('Fichier trop gros pour être envoyé : ')
-                        .$uploaded['name'].'</p>'.$this->action_showThread($group, $artid);
-
-            case UPLOAD_ERR_PARTIAL:
-                return '<p class="error">'._b_('Erreur lors de l\'upload de ')
-                        .$uploaded['name'].'</p>'.$this->action_showThread($group, $artid);
-
-            case UPLOAD_ERR_NO_FILE:
-                return '<p class="error">'._b_('Le fichier spécifié n\'existe pas : ')
-                        .$uploaded['name'].'</p>'.$this->action_showThread($group, $artid);
-
-            case UPLOAD_ERR_NO_TMP_DIR:
-                return '<p class="error">'._b_('Une erreur est survenue sur le serveur lors de l\'upload de ')
-                        .$uploaded['name'].'</p>'.$this->action_showThread($group, $artid);
-
-            default:
-                $this->custom = $body_headers.$this->custom;
-        }
-
-        // finalise and post the message
-        $msg .= $this->custom.$this->profile['customhdr']."\n".$body;
-
-        if ($this->nntp->post($msg)) {
-            $dir = Array('group' => $group);
-            if ($artid != -1) {
-                $dir['first'] = $artid;
+            $msg = null;
+            if (isset($_POST['body']) && !is_utf8($_POST['body'])) {
+                $_POST['body'] = utf8_encode($_POST['body']);
+            }
+            if (empty($hdr_values['Subject'])) {
+                Banana::$page->trig(_b_('Le message doit avoir un sujet'));
+            } elseif (Banana::$msgedit_canattach && isset($_FILES['attachment']) && $_FILES['attachment']['name']) {
+                $uploaded =& $_FILES['attachment'];
+                if (!is_uploaded_file($uploaded['tmp_name'])) {
+                    Banana::$page->trig(_b_('Une erreur est survenue lors du téléchargement du fichier'));
+                } else {
+                    $msg = BananaMessage::newMessage($hdr_values, $_POST['body'], $uploaded);
+                }
+            } else {
+                $msg = BananaMessage::newMessage($hdr_values, $_POST['body']);
+            }
+            if (!is_null($msg)) {
+                if (Banana::$protocole->send($msg)) {
+                    $this->loadSpool($group);
+                    $newid = Banana::$spool->updateUnread(Banana::$profile['lastnews']);
+                    Banana::$page->redirect(array('group' => $group, 'artid' => $newid ? $newid : $artid));
+                } else {
+                    Banana::$page->trig(_b_('Une erreur est survenue lors de l\'envoi du message :') . '<br />'
+                                   . Banana::$protocole->lastError());
+                }
             }
-            redirectInBanana($dir);
         } else {
-            return '<p class="error">' . _b_('Impossible de poster le message. Le serveur a retourné l\'erreur :') . '</p>'
-                   . '<pre class="error">' . utf8_encode($this->nntp->lasterrortext) .'</pre>'
-                   . $this->action_newFup($group, $artid);
+            if (!is_null($artid)) {
+                $msg    =& $this->loadMessage($group, $artid);
+                $body    = $msg->getSender() . _b_(' a écrit :') . "\n" . $msg->quote();
+                $subject = $msg->getHeaderValue('subject');
+                $headers['Subject']['user'] = 'Re: ' . preg_replace("/^re\s*:\s*/i", '', $subject);
+                $target  = $msg->getHeaderValue($hdrs['reply']);
+                if (empty($target)) {
+                    $target = $group;
+                }
+                $headers[$hdrs['dest']]['user'] =& $target;
+            } else {
+                $body    = '';
+                $headers[$hdrs['dest']]['user'] = $group;
+            }
+            if (Banana::$profile['signature']) {
+                $body .=  "\n\n-- \n" . Banana::$profile['signature'];
+            }
+            Banana::$page->assign('body', $body);
         }
-    }
 
-    /**************************************************************************/
-    /* Private functions                                                      */
-    /**************************************************************************/
+        Banana::$page->assign('maxfilesize', Banana::$msgedit_maxfilesize);
+        Banana::$page->assign('can_attach', Banana::$msgedit_canattach);
+        Banana::$page->assign('headers', $headers);
+        return true;
+    }
 
-    function _newSpool($group, $disp=0, $since='') {
-        $this->_require('spool');
-        if (!$this->spool || $this->spool->group != $group) {
-            $this->spool = new BananaSpool($group, $disp, $since);
-            if (!$this->spool || !$this->spool->valid) {
-                $this->spool = null;
-                return false;
+    protected function action_cancelMessage($group, $artid)
+    {
+        Banana::$page->setPage('cancel');
+        $msg =& $this->loadMessage($group, $artid);
+        if (!$msg->canCancel()) {
+            return _b_('Vous n\'avez pas les droits suffisants pour supprimer ce message');
+        }
+        if (isset($_POST['cancel'])) {
+            $this->loadSpool($group);
+            $ndx = Banana::$spool->getNdX($id) - 1;
+            if (!Banana::$protocole->cancel($msg)) {
+                return _b_('Une erreur s\'est produite lors de l\'annulation du message :') . '<br />'
+                       . Banana::$protocole->lastError();
             }
+            if ($ndx < 50) {
+                 $ndx = 0;
+            }
+            $this->removeMessage($group, $artid);
+            Banana::$page->redirect(Array('group' => $group, 'first' => $ndx));
         }
-        if (count($this->profile['subscribe']) > 0) {
-            $this->_newGroup(false);
-        }
+
+        Banana::$page->assign_by_ref('message', $msg);
+        Banana::$page->assign('body', $msg->getFormattedBody());
+        Banana::$page->assign('headers', Banana::$msgshow_headers);
         return true;
     }
 
-    function _newPost($id)
+    /**************************************************************************/
+    /* Spoolgen functions                                                     */
+    /**************************************************************************/
+
+    private function checkErrors()
     {
-        $this->_require('post');
-        $this->post = new BananaPost($id);
-        if (!$this->post || !$this->post->valid) {
-            $this->post = null;
+        if (Banana::$protocole->lastErrno()) {
+            echo "\nL'erreur suivante s'est produite : "
+                . Banana::$protocole->lastErrno() . " "
+                . Banana::$protocole->lastError() . "\n";
             return false;
         }
         return true;
     }
 
-    function _newGroup($showNew = true)
+    static public function createAllSpool(array $protos)
     {
-        $this->_require('groups');
-        $this->groups = new BananaGroups(BANANA_GROUP_SUB);
-        if ($showNew && $this->groups->type == BANANA_GROUP_SUB) {
-            $this->newgroups = new BananaGroups(BANANA_GROUP_NEW);
+        foreach ($protos as $proto) {
+            $banana = new Banana(array(), $proto);
+
+            if (!$banana->checkErrors()) {
+                continue;
+            }
+            $groups = Banana::$protocole->getBoxList();
+            if (!$banana->checkErrors()) {
+                continue;
+            }
+
+            print "** $proto **\n";
+            foreach (array_keys($groups) as $g) {
+                print "Generating spool for $g: ";
+                Banana::$group = $g;
+                $spool = $banana->loadSpool($g);
+                if (!$banana->checkErrors()) {
+                    break;
+                }
+                print "done.\n";
+                unset($spool);
+                Banana::$spool = null;
+            }
+            print "\n";
         }
     }
 
-    function _require($file)
+    static public function refreshAllFeeds(array $protos)
     {
-        require_once (dirname(__FILE__).'/'.$file.'.inc.php');
+        Banana::load('feed');
+        Banana::$feed_updateOnDemand = true; // In order to force update
+        foreach ($protos as $proto) {
+            $banana = new Banana(array(), $proto);
+
+            if (!$banana->checkErrors()) {
+                continue;
+            }
+            $groups = Banana::$protocole->getBoxList();
+            if (!$banana->checkErrors()) {
+                continue;
+            }
+
+            print "** $proto **\n";
+            foreach (array_keys($groups) as $g) {
+                print "Generating feed cache for $g: ";
+                Banana::$group = $g;
+                $spool = $banana->loadSpool($g);
+                if (!$banana->checkErrors()) {
+                    break;
+                }
+                $feed  =& BananaFeed::getFeed();
+                print "done.\n";
+                unset($feed);
+                unset($spool);
+                Banana::$spool = null;
+            }
+            print "\n";
+        }
     }
+    /**************************************************************************/
+    /* Private functions                                                      */
+    /**************************************************************************/
 
-    function _upload($file)
+    protected function loadSpool($group)
     {
-        if ($_FILES[$file]['name'] == "") {
-            return Array( 'error' => -1 );
+        Banana::load('spool');
+        if (!Banana::$spool || Banana::$spool->group != $group) {
+            $clean = false;
+            if (php_sapi_name() != 'cli') {
+                if ($group == @$_SESSION['banana_group'] && isset($_SESSION['banana_spool'])) {
+                    Banana::$spool = unserialize($_SESSION['banana_spool']);
+                    $clean = @(Banana::$profile['lastnews'] != $_SESSION['banana_lastnews']);
+                } else {
+                    unset($_SESSION['banana_message']);
+                    unset($_SESSION['banana_artid']);
+                    unset($_SESSION['banana_showhdr']);
+                }
+            }
+            BananaSpool::getSpool($group, Banana::$profile['lastnews'], Banana::$profile['autoup'] || $clean);
+            if (php_sapi_name() != 'cli') {
+                $_SESSION['banana_group'] = $group;
+                if (!Banana::$profile['display']) {
+                    $_SESSION['banana_spool'] = serialize(Banana::$spool);
+                    $_SESSION['banana_lastnews'] = Banana::$profile['lastnews'];
+                }
+            }
+            Banana::$spool->setMode(Banana::$profile['display'] ? Banana::SPOOL_UNREAD : Banana::SPOOL_ALL);
         }
+        return true;
+    }
 
-        // upload
-        $_FILES[$file]['tmp_name'];
+    protected function &loadMessage($group, $artid)
+    {
+        Banana::load('message');
+        if ($group == @$_SESSION['banana_group'] && $artid == @$_SESSION['banana_artid']
+            && isset($_SESSION['banana_message'])) {
+            $message = unserialize($_SESSION['banana_message']);
+            Banana::$msgshow_headers = $_SESSION['banana_showhdr'];
+        }  else {
+            $message = Banana::$protocole->getMessage($artid);
+            $_SESSION['banana_group'] = $group;
+            $_SESSION['banana_artid'] = $artid;
+            $_SESSION['banana_message'] = serialize($message);
+            $_SESSION['banana_showhdr'] = Banana::$msgshow_headers;
+        }
+        Banana::$message =& $message;
+        return $message;
+    }
 
-        // test if upload is ok
-        $file    = $_FILES[$file];
-        if ($file['size'] == 0 || $file['error'] != 0) {
-            if ($file['error'] == 0) {
-                $file['error'] = -1;
+    protected function removeMessage($group, $artid)
+    {
+        Banana::$spool->delId($artid);
+        if ($group == $_SESSION['banana_group']) {
+            if (!Banana::$profile['display']) {
+                $_SESSION['banana_spool'] = serialize(Banana::$spool);
+            }
+            if ($artid == $_SESSION['banana_artid']) {
+                unset($_SESSION['banana_message']);
+                unset($_SESSION['banana_showhdr']);
+                unset($_SESSION['banana_artid']);
             }
-            return $file;
-        }
-
-        // adding custum data
-        $mime    = rtrim(shell_exec('file -bi '.$file['tmp_name'])); //Because mime_content_type don't work :(
-        $encod   = 'base64';
-        if (preg_match("@([^ ]+/[^ ]+); (.*)@", $mime, $format)) {
-            $mime  = $format[1];
-            $encod = $format[2];
-        }
-        $data = fread(fopen($file['tmp_name'], 'r'), $file['size']);
-        if ($encod == 'base64') {
-            $data = chunk_split(base64_encode($data));
         }
-        $file['name']     = basename($file['name']);
-        $file['type']     = $mime;
-        $file['encoding'] = $encod;
-        $file['data']     = $data;
-
-        return $file;
+        $this->loadSpool($group);
+        return true;
     }
 
-    function _make_part($headers, $body)
+    static private function load($file)
     {
-        return "\n--".$this->boundary."\n".$headers."\n".$body;
+        $file = strtolower($file) . '.inc.php';
+        if (!@include_once dirname(__FILE__) . "/$file") {
+            require_once $file;
+        }
     }
 }
 
-// vim:set et sw=4 sts=4 ts=4
+// vim:set et sw=4 sts=4 ts=4 enc=utf-8:
 ?>