Fixes vim mode line.
[banana.git] / banana / message.inc.php
index 01686a1..a86ae2e 100644 (file)
@@ -36,7 +36,7 @@ final class BananaMessage extends BananaMimePart
     {
         $msg = new BananaMessage();
         $msg->msg_headers = $headers;
-        $msg->makeTextPart($body, 'text/plain', '8bits', 'UTF-8', 'fixed');
+        $msg->makeTextPart($body, 'text/plain', '8bits', 'UTF-8', 'flowed');
         if (!is_null($file)) {
             $msg->addAttachment($file);
         }
@@ -87,28 +87,22 @@ final class BananaMessage extends BananaMimePart
             return substr($res,0, -2);
 
           case "from":
-            return BananaMessage::formatFrom($text);
+            return BananaMessage::formatFrom($text, $this->headers['subject']);
 
           case "references": case "in-reply-to":
             $rsl     = "";
-            $parents = preg_grep('/^\d+$/', $this->getTranslatedReferences());
-            $p       = array_pop($parents);
-
-            $parents = array();
-            while (!is_null($p)) {
-                array_unshift($parents, $p);
-                $p         = Banana::$spool->overview[$p]->parent;  
-            }
+            $parents = Banana::$spool->getReferences($this->headers);
             $ndx = 1;
-            foreach ($parents as $p) {
+            while (!empty($parents)) {
+                $p = array_shift($parents);
                 $rsl .= Banana::$page->makeLink(Array('group' => Banana::$spool->group,
-                                                      'artid' => $p, 'text' => $ndx++)) . ' ';
+                                                      'artid' => $p->id, 'text' => $ndx++)) . ' ';
             }
             return $rsl;
 
           case "subject":
             $text = stripslashes($text);
-            $text = html_entities($text);
+            $text = banana_htmlentities($text);
             return banana_catchFormats($text);
 
           default:
@@ -119,7 +113,7 @@ final class BananaMessage extends BananaMimePart
     public function getSender()
     {
         $from = $this->headers['from'];
-        $name = trim(preg_replace('/<[^ ]*>/', '', $from));
+        $name = trim(strip_tags($from));
         if (empty($name)) {
             return $from;
         }
@@ -134,6 +128,8 @@ final class BananaMessage extends BananaMimePart
         }
         if ($hdr == 'date') {
             return strtotime($this->headers['date']);
+        } else if ($hdr == 'references' || $hdr == 'reply-to') {
+            return str_replace('><', '> <', $this->headers[$hdr]);
         } else {
             return $this->headers[$hdr];
         }
@@ -146,35 +142,63 @@ final class BananaMessage extends BananaMimePart
         return array_merge($headers, parent::getHeaders());
     }
 
-    static public function formatFrom($text)
+    static public function extractMail($text)
     {
-#     From: mark@cbosgd.ATT.COM
-#     From: <mark@cbosgd.ATT.COM>
-#     From: mark@cbosgd.ATT.COM (Mark Horton)
-#     From: Mark Horton <mark@cbosgd.ATT.COM>
-        $mailto = '<a href="mailto:';
-    
-        $result = banana_htmlentities($text);
-        if (preg_match("/^([^ ]+@[^ ]+)$/", $text, $regs)) {
-            $result = $mailto . $regs[1] . '">' . banana_htmlentities($regs[1]) . '</a>';
+        if (preg_match("/^\"?([^<>\"]+)\"? +<(.+@.+)>$/", $text, $regs)) {
+            # From: Mark Horton <mark@cbosgd.ATT.COM>
+            $nom = preg_replace("/^'(.*)'$/", '\1', $regs[1]);
+            $nom = stripslashes($nom);
+            return array($nom, strtolower($regs[2]));
+        } else if (preg_match("/^([^ ]+@[^ ]+) \((.*)\)$/", $text, $regs)) {
+            # From: mark@cbosgd.ATT.COM (Mark Horton)
+            return array($regs[2], strtolower($regs[1]));
+        } else if (preg_match("/^<?([^< ]+@[^> ]+)>?$/", $text, $regs)) {
+            # From: <mark@cbosgd.ATT.COM>
+            return array($regs[1], strtolower($regs[1]));
+        } else {
+            # From: mark@cbosgd.ATT.COM
+            return array($text, strtolower($text));
         }
-        if (preg_match("/^<(.+@.+)>$/", $text, $regs)) {
-            $result = $mailto . $regs[1] . '">' . banana_htmlentities($regs[1]) . '</a>';
+    }
+
+    static public function formatFrom($text, $subject = '')
+    {
+        list($name, $email) = self::extractMail($text);
+        if ($subject) {
+           $subject = '?subject=' . banana_htmlentities(_b_('Re: ') . $subject, ENT_QUOTES);
         }
+        $result = '<a href="mailto:' . $email . $subject . '">' . banana_htmlentities($name) . '</a>';
+        return preg_replace("/\\\(\(|\))/","\\1", $result);
+    }
+
+    public function getAuthorName()
+    {
+        $text = $this->getHeaderValue('From');
+        $name = null;
         if (preg_match("/^([^ ]+@[^ ]+) \((.*)\)$/", $text, $regs)) {
-            $result = $mailto . $regs[1] . '">' . banana_htmlentities($regs[2]) . '</a>';
-        }   
+            $name = $regs[2];
+        }
         if (preg_match("/^\"?([^<>\"]+)\"? +<(.+@.+)>$/", $text, $regs)) {
-            $nom = preg_replace("/^'(.*)'$/", '\1', $regs[1]);
-            $nom = stripslashes($nom);
-            $result = $mailto . $regs[2] . '">' . banana_htmlentities($nom) . '</a>';
+            $name = preg_replace("/^'(.*)'$/", '\1', $regs[1]);
+            $name = stripslashes($name);
+        }
+        if ($name) {
+            return preg_replace("/\\\(\(|\))/","\\1", $name);
+        }
+
+        if (function_exists('hook_getAuthorName') && $name = hook_getAuthorName($this)) {
+            return $name;
         }
-        return preg_replace("/\\\(\(|\))/","\\1",$result);
+
+        if (preg_match("/([^< ]+)@([^> ]+)/", $text, $regs)) {
+            return $regs[1];
+        }
+        return 'Anonymous';
     }
 
     static public function formatDate($text)
     {
-        return utf8_encode(strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($text)));
+        return strftime("%A %d %B %Y, %H:%M (fuseau serveur)", strtotime($text));
     }
 
     public function translateHeaders()
@@ -196,30 +220,18 @@ final class BananaMessage extends BananaMimePart
         return preg_split('/\s/', $text);
     }
 
-    public function getTranslatedReferences()
-    {
-        return BananaMessage::formatReferences($this->headers);
-    }
-
-    static public function formatReferences(array &$refs)
-    {
-        if (isset($refs['references'])) {
-            $text = str_replace('><', '> <', $refs['references']);
-            return preg_split('/\s/', strtr($text, Banana::$spool->ids));
-        } elseif (isset($refs['in-reply-to'])) {
-            return array(Banana::$spool->ids[$refs['in-reply-to']]);
-        } else {
-            return array();
-        }
-    }
-
     public function hasXFace()
     {
-        return Banana::$msgshow_xface && isset($this->headers['x-face']);
+        return Banana::$msgshow_xface &&
+               ((function_exists('hook_hasxface') && hook_hasXFace($this->headers))
+               || isset($this->headers['x-face']));
     }
 
     public function getXFace()
     {
+        if (function_exists('hook_getxface') && hook_getXFace($this->headers)) {
+            return;
+        }
         header('Content-Type: image/gif');
         $xface = $this->headers['x-face'];
         passthru('echo ' . escapeshellarg($xface)
@@ -270,14 +282,35 @@ final class BananaMessage extends BananaMimePart
         if (function_exists('hook_checkcancel')) {
             return hook_checkcancel($this->headers);
         }
-        return Banana::$profile['name'] == $this->headers['from'];
+        return Banana::$profile['headers']['From'] == $this->headers['from'];
     }
 
     public function canSend()
     {
         return Banana::$protocole->canSend();
     }
+
+    public function getSignature()
+    {
+        $email = $this->getHeaderValue('from');
+        if (preg_match('/<?([^ <]+@[^ >]+)>?/', $email, $matches)) {
+            $email = $matches[1];
+        }
+        $signature = BananaMimePart::getSignature();
+        if (empty($signature)) {
+            return $signature;
+        } else {
+            foreach ($signature['identity'] as $ident) {
+                if (strpos($ident, "<$email>") !== false) {
+                    return $signature;
+                }
+            }
+            $signature['certified'] = false;
+            $signature['certification_error'] = 'mauvaise identité';
+        }
+        return $signature;
+    }
 }
 
-// vim:set et sw=4 sts=4 ts=4 enc=utf-8:
+// vim:set et sw=4 sts=4 ts=4 fenc=utf-8:
 ?>