After replying returns on the last message
[banana.git] / banana / misc.inc.php
index 09cd285..fe76cd9 100644 (file)
@@ -300,7 +300,7 @@ function formatDisplayHeader($_header,$_text) {
 
         case "from":
             return formatFrom($_text);
-        
+
         case "references":
             $rsl     = "";
             $ndx     = 1;
@@ -380,52 +380,56 @@ function formatFrom($text) {
     return preg_replace("/\\\(\(|\))/","\\1",$result);
 }
 
+function makeTab($link, $text)
+{
+    return Array(makeHREF($link, $text),
+                 $text);
+}
+
 function displayTabs()
 {
     global $banana;
     extract($banana->state);
-    
-    $res  = Array();
     if (function_exists('hook_shortcuts') && $cstm = hook_shortcuts()) {
         $res = $cstm;
     }
  
-    array_push($res,
-               makeHREF(Array('subscribe' => 1), _b_('Abonnements')),
-               is_null($group) ? Array(_b_('Les forums'), true) : makeHREF(Array(), _b_('Les forums')));
+    $res['subscribe'] = makeTab(Array('subscribe' => 1), _b_('Abonnements'));
+    $res['forums']    = makeTab(Array(), _b_('Les forums'));
 
     if (!is_null($group)) {
-        $grplink = makeHREF(Array('group' => $group), $group);
-        $grpcur  = Array($group, true);
+        $res['group'] = makeTab(Array('group' => $group), $group);
         if (is_null($artid)) {
             if (@$action == 'new') {
-                array_push($res, $grplink, Array(_b_('Nouveau Message'), true));
-            } else {
-                array_push($res, $grpcur);
+                $res['action'] = makeTab(Array('group'  => $group,
+                                               'action' => 'new'),
+                                         _b_('Nouveau Message'));
             }
         } else {
+            $res['message'] = makeTab(Array('group' => $group,
+                                            'artid' => $artid),
+                                      _b_('Message'));
             if (!is_null($action)) {
-                array_push($res,
-                           $grplink,
-                           makeHREF(Array('group' => $group,
-                                          'artid' => $artid),
-                                    _b_('Message')));
                 if ($action == 'new') {
-                    array_push($res, Array(_b_('RĂ©pondre'), true));
+                    $res['action'] = makeTab(Array('group'  => $group,
+                                                   'artid'  => $artid,
+                                                   'action' => 'new'),
+                                             _b_('RĂ©ponse'));
                 } elseif ($action == 'cancel') {
-                    array_push($res, Array(_b_('Annuler'), true));
+                    $res['action'] = makeTab(Array('group'  => $group,
+                                                   'artid'  => $artid,
+                                                   'action' => 'cancel'),
+                                             _b_('Annuler'));
                 }
-            } else {
-                array_push($res, $grplink, Array(_b_('Message'), true));
             }
         }
     }
     $ret = '<ul id="onglet">';
-    foreach ($res as $onglet) {
-        if (is_string($onglet)) {
-            $ret .= '<li>' . $onglet . '</li>';
+    foreach ($res as $name=>$onglet) {
+        if ($name != @$page) {
+            $ret .= '<li>' . $onglet[0] . '</li>';
         } else {
-            $ret .= '<li class="actif">' . $onglet[0] . '</li>';
+            $ret .= '<li class="actif">' . $onglet[1] . '</li>';
         }
     }
     $ret .= '</ul>';
@@ -461,11 +465,17 @@ function displayPages($first = -1)
 
 function makeTable($text)
 {
-    return '<table class="cadre_a_onglet" cellpadding="0" cellspacing="0">'
+    $links = null;
+    if (function_exists('hook_browsingAction')) {
+        $links = hook_browsingAction();
+    }
+
+    return '<table class="cadre_a_onglet" cellpadding="0" cellspacing="0" width="100%">'
          . '<tr><td>'
          . displayTabs()
          . '</td></tr>'
          . '<tr><td class="conteneur_tab">'
+         . $links
          . $text
          . '</td></tr>'
          . '</table>';
@@ -475,16 +485,14 @@ function makeTable($text)
  *  FORMATTING STUFF : BODY
  */
 
-function autoformat($text, $part = false)
+function autoformat($text, $force = 0)
 {
     global $banana;
     $length = $banana->wrap;
-    $all = null;
-    if (!$part) {
-        $all = ', all=1';
-    }
-    
-    $cmd = "echo ".escapeshellarg($text)." | perl -MText::Autoformat -e 'autoformat {left=>1, right=>$length$all };'";
+    $force  = $force ? 1 : 0; 
+    $cmd = 'echo ' . escapeshellarg($text)
+         . ' | perl -MText::Autoformat -e \'autoformat {left=>1, right=>' . $length . ', all=>' . $force . ' };\'';
+
     exec($cmd, $result, $ret);
     if ($ret != 0) {
         $result = split("\n", $text);
@@ -492,7 +500,7 @@ function autoformat($text, $part = false)
     return $result;
 }                                
 
-function wrap($text, $_prefix="", $_force=false)
+function wrap($text, $_prefix="", $_force=false, $firstpass = true)
 {
     $parts = preg_split("/\n-- ?\n/", $text);
     if (count($parts)  >1) {
@@ -501,7 +509,7 @@ function wrap($text, $_prefix="", $_force=false)
     } else {
         $sign = '';
     }
-   
+
     global $banana;
     $url    = $banana->url_regexp;
     $length = $banana->wrap;
@@ -522,7 +530,7 @@ function wrap($text, $_prefix="", $_force=false)
                 }
                     
                 if ($format) {
-                    $result = array_merge($result, autoformat(join("\n", $next)));
+                    $result = array_merge($result, autoformat(join("\n", $next), $firstpass));
                 } else {
                     $result = array_merge($result, $next);
                 }
@@ -537,24 +545,30 @@ function wrap($text, $_prefix="", $_force=false)
                     }
                 }
             } else {
-                if (strlen($line) > 2 * $max) {
-                    $next = array_merge($next, autoformat($line, true));
-                } else {
-                    $format = true;
-                    array_push($next, $line);
-                }
+                array_push($next, $line);
+                $format = true;
             }
         } else {
             array_push($next, $line);
         }
     }
     if ($format) {
-        $result = array_merge($result, autoformat(join("\n", $next)));
+        $result = array_merge($result, autoformat(join("\n", $next), $firstpass));
     } else {
         $result = array_merge($result, $next);
     }
 
-    return $_prefix.join("\n$_prefix", $result).($_prefix ? '' : $sign);
+    $break = "\n";
+    $prefix = null;
+    if (!$firstpass) {
+        $break .= $_prefix;
+        $prefix = $_prefix;
+    }
+    $result = $prefix.join($break, $result).($prefix ? '' : $sign);
+    if ($firstpass) {
+        return wrap($result, $_prefix, $_force, false);
+    }
+    return $result;
 }
 
 function cutlink($link)
@@ -573,6 +587,27 @@ function cleanurl($url)
     return '<a href="'.$url.'" title="'.$url.'">'.cutlink($url).'</a>';
 }
 
+function catchMailLink($email)
+{
+    global $banana;
+    $mid = '<' . $email . '>';
+    if (isset($banana->spool->ids[$mid])) {
+        return makeHREF(Array('group' => $banana->state['group'],
+                              'artid' => $banana->spool->ids[$mid]),
+                        $email);
+    } elseif (strpos($email, '$') !== false) {
+        return $email;
+    }   
+    return '<a href="mailto:' . $email . '">' . $email . '</a>';
+}
+
+/** Remove quotation marks
+ */
+function replaceQuotes($text)
+{
+    return stripslashes(preg_replace("@(^|<pre>|\n)&gt;[ \t\r]*@i", '\1', $text));
+}
+
 function formatbody($_text, $format='plain', $flowed=false)
 {
     if ($format == 'html') {
@@ -589,7 +624,9 @@ function formatbody($_text, $format='plain', $flowed=false)
         $url  = $banana->url_regexp;
         $res  = preg_replace("/(&lt;|&gt;|&quot;)/", " \\1 ", $res);
         $res  = preg_replace("!$url!ie", "'\\1'.cleanurl('\\2').'\\3'", $res);
-        $res  = preg_replace('/(["\[])?(?:mailto:)?([a-z0-9.\-+_]+@[a-z0-9.\-+_]+)(["\]])?/i', '\1<a href="mailto:\2">\2</a>\3', $res);
+        $res  = preg_replace('/(["\[])?(?:mailto:|news:)?([a-z0-9.\-+_\$]+@([\-.+_]?[a-z0-9])+)(["\]])?/ie',
+                             "'\\1' . catchMailLink('\\2') . '\\4'",
+                             $res);
         $res  = preg_replace("/ (&lt;|&gt;|&quot;) /", "\\1", $res);
 
         if ($format == 'richtext') {
@@ -605,10 +642,10 @@ function formatbody($_text, $format='plain', $flowed=false)
         $sign  = '<hr style="width: 100%; margin: 1em 0em; " />';
     } else {
         while (preg_match("@(^|<pre>|\n)&gt;@i", $res)) {
-            $res  = preg_replace("@(^|<pre>|\n)((&gt;[^\n]*\n)+)@ie",
+            $res  = preg_replace("@(^|<pre>|\n)((&gt;[^\n]*(?:\n|$))+)@ie",
                 "'\\1</pre><blockquote><pre>'"
-                .".stripslashes(preg_replace('@(^|<pre>|\n)&gt;[ \\t\\r]*@i', '\\1', '\\2'))"
-                .".'</pre></blockquote><pre>'",
+                ." . replaceQuotes('\\2')"
+                ." . '</pre></blockquote><pre>'",
                 $res);
         }
         $res = preg_replace("@<pre>-- ?\n@", "<pre>\n-- \n", $res);