User color only depends on the email address.
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Wed, 29 Jun 2011 12:42:40 +0000 (14:42 +0200)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Wed, 29 Jun 2011 12:42:40 +0000 (14:42 +0200)
This should fix issues with the same user appearing in different colors
depending on the client he is using.

Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
banana/message.inc.php
banana/spool.inc.php

index da5b05c..357c26a 100644 (file)
@@ -142,30 +142,33 @@ final class BananaMessage extends BananaMimePart
         return array_merge($headers, parent::getHeaders());
     }
 
-    static public function formatFrom($text, $subject = '')
+    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 ($subject) {
-           $subject = '?subject=' . banana_htmlentities(_b_('Re: ') . $subject, ENT_QUOTES);
-        }
-        if (preg_match("/^<?([^< ]+@[^> ]+)>?$/", $text, $regs)) {
-            $result = $mailto . $regs[1] . $subject . '">' . banana_htmlentities($regs[1]) . '</a>';
-        }
-        if (preg_match("/^([^ ]+@[^ ]+) \((.*)\)$/", $text, $regs)) {
-            $result = $mailto . $regs[1] . $subject . '">' . banana_htmlentities($regs[2]) . '</a>';
-        }
         if (preg_match("/^\"?([^<>\"]+)\"? +<(.+@.+)>$/", $text, $regs)) {
+            # From: Mark Horton <mark@cbosgd.ATT.COM>
             $nom = preg_replace("/^'(.*)'$/", '\1', $regs[1]);
             $nom = stripslashes($nom);
-            $result = $mailto . $regs[2] . $subject . '">' . banana_htmlentities($nom) . '</a>';
+            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));
+        }
+    }
+
+    static public function formatFrom($text, $subject = '')
+    {
+        list($name, $email) = self::extractMail($text);
+        if ($subject) {
+           $subject = '?subject=' . banana_htmlentities(_b_('Re: ') . $subject, ENT_QUOTES);
         }
-        return preg_replace("/\\\(\(|\))/","\\1",$result);
+        $result = '<a href="mailto:' . $email . $subject . '">' . banana_htmlentities($name) . '</a>';
+        return preg_replace("/\\\(\(|\))/","\\1", $result);
     }
 
     public function getAuthorName()
index 074a758..ace19f7 100644 (file)
@@ -54,28 +54,19 @@ class BananaSpoolHead
      */
     public function __construct($id, array &$message)
     {
+        list($name, $from) = BananaMessage::extractMail($message['from']);
         $this->id         = $id;
         $this->msgid      = @$message['message-id'];
         $this->date       = $message['date'];
         $this->subject    = @$message['subject'];
         $this->from       = $message['from'];
-        $this->color      = sprintf('#%06x', abs(crc32($this->from) % 0xffffff));
+        $this->color      = sprintf('#%06x', abs(crc32($from) % 0xffffff));
         $this->desc       = 1;
         $this->isread     = true;
         $this->descunread = 0;
-        if (preg_match("/^([^ ]+@[^ ]+) \((.*)\)$/", $this->from, $regs)) {
-            $this->name = $regs[2];
-        }
-        if (preg_match("/^\"?([^<>\"]+)\"? +<(.+@.+)>$/", $this->from, $regs)) {
-            $this->name = preg_replace("/^'(.*)'$/", '\1', $regs[1]);
-            $this->name = stripslashes($this->name);
-        }
-        if ($this->name) {
-            $this->name =  preg_replace("/\\\(\(|\))/","\\1", $this->name);
-        } else if (preg_match("/([^< ]+)@([^> ]+)/", $this->from, $regs)) {
-            $this->name = $regs[1];
-        } else {
-            $this->name = 'Anonymous';
+        $this->name       = $name;
+        if ($name === $from) {
+            $this->name   = 'Anonymous';
         }
     }
 }