[imp] PATCH: User configurable attribution text with macros

Chris Hastie lists at oak-wood.co.uk
Thu Jan 23 22:07:54 PST 2003


The attached patches to imp/config/prefs.php.dist and
imp/lib/Compose.php allow a user to configure the text used to attribute
quoted text in replies.

The user may include macros in their chosen attribution text, that are
expanded as follows, relative to the message being replied to:

        %n      A new line
        %%      The '%' character
        %f      The sender name(s) and email address(es)
        %a      The sender email address(es)
        %p      The sender name(s)
        %r      The full RFC 2822 style date
        %d      The date as ddd, dd mmm YYYY (ie using strftime with
                "%a, %d %b %Y"
        %x      The date in the locale's default (ie using strftime with
                '%x'
        %c      The date and time in the locale's default (ie using
                strftime with '%c'
        %m      The original Message-ID
        %s      The original subject

Whilst a user has not set any non-default preferences from the Message
Composition options, the attribution text will be as before, in the
language which was selected at login. Changes to language options within
a session will not affect the attribution text however.

Once a user has saved changes from the Message Composition options, the
attribution text will be frozen and not respond to later changes of
language choice.

I haven't really tested how well the macro expansion deals with other
character sets.

imp/config/prefs.php includes a new gettext string, _("How to attribute
quoted lines in a reply:"), which I presume I need to document somewhere
so that it gets noticed by anyone doing translations. Hints appreciated!

I'm also aware that the meaning of the macros really needs to be
documented somewhere. Again, suggestions appreciated.
-- 
Chris Hastie
-------------- next part --------------
--- imp/config/prefs.php.dist,v 1.136
+++ imp/config/prefs.php	Wed Jan 22 22:59:11 2003
@@ -99,7 +99,7 @@
     'column' => _("Other Options"),
     'label' => _("Message Composition"),
     'desc' => _("Customize how you send mail and where drafts are saved."),
-    'members' => array('wrap_width', 'reply_quote', 'quote_prefix',
+    'members' => array('wrap_width', 'reply_quote', 'quote_prefix', 'attrib_text',
                        'compose_popup', 'compose_html', 'compose_confirm',
                        'folderselect', 'reply_headers', 'close_draft',
                        'set_priority'));
@@ -731,6 +731,14 @@
     'shared' => false,
     'type' => 'checkbox',
     'desc' => _("Include original message in a reply?"));
+
+// How should we attribute quoted lines in a reply?
+$_prefs['attrib_text'] = array(
+    'value' => _("Quoting").' %f',
+    'locked' => false,
+    'shared' => false,
+    'type' => 'text',
+    'desc' => _("How to attribute quoted lines in a reply:"));
 
 // How should we prefix quoted lines in a reply?
 $_prefs['quote_prefix'] = array(
-------------- next part --------------
--- imp/lib/Compose.php,v 1.15
+++ imp/lib/Compose.php	Thu Jan 23 22:02:44 2003
@@ -222,7 +222,7 @@
                 $msg .= $quote_str . $quote_str . '----- ' . _("End message") . " -----\n";
             }
         } else {
-            $msg = _("Quoting") . ' ' . $from . ":\n$quote_str$msg";
+            $msg = IMP_Compose::_expandAttribution($prefs->getValue('attrib_text'), $from, $h) . "\n$quote_str$msg";
         }
         $msg .= "\n";
 
@@ -474,6 +474,110 @@
 
         /* Delete the temporary file. */
         IMP_Compose::deleteAttachment($part);
+    }
+    
+    /**
+    * Expand macros in attribution text when replying to messages
+    *
+    * @access private
+    * 
+    * @param string $line   The line of attribution text
+    *
+    * @param string $from                        The email address of the
+    *                                            original sender.
+    *
+    * @param optional object IMP_Headers &$h     The IMP_Headers object for
+    *                                            the message
+    *
+    */
+    function _expandAttribution($line, $from, &$h) 
+    {        
+        /* First we'll get a comma seperated list of
+        email addresses and a comma seperated list of 
+        personal names out of $from (there just might 
+        be more than one of each) */
+        $addressList = '';
+        $nameList = '';
+        $addresses = @imap_rfc822_parse_adrlist($from, '');
+        foreach ($addresses as $entry) {
+            if (isset($entry->mailbox) && isset($entry->host)) {
+                if (strlen($addressList) >0) {
+                    $addressList .= ', ';
+                }
+                $addressList .= $entry->mailbox . '@' . $entry->host;
+            } elseif (isset($entry->mailbox)) {
+                if (strlen($addressList) >0) {
+                    $addressList .= ', ';
+                }                
+                $addressList .= $entry->mailbox;
+            }
+            if (isset($entry->personal)){
+                if (strlen($nameList) >0) {
+                    $nameList .= ', ';
+                } 
+                $nameList .= $entry->personal;
+            } elseif (isset($entry->mailbox)) {
+                if (strlen($nameList) >0) {
+                    $nameList .= ', ';
+                }                
+                $nameList .= $entry->mailbox;
+            } 
+        }        
+        
+        /* define the macros. Those that need $h will be added
+        to later */
+        $match = array (
+            'n' => "\n",                        // new line
+            '%' => '%',                         // the '%' character
+            'f' => $from,                       // Name and email address of original sender
+            'a' => $addressList,                // Senders email address(es)
+            'p' => $nameList,                   // Senders name(s)
+            'r' => '?',                         // RFC 822 date and time
+            'd' => '?',                         // Date as ddd, dd mmm yyyy
+            'x' => '?',                         // Date in locale's default
+            'c' => '?',                         // Date and time in locale's default
+            'm' => '<?>',                       // Message-ID
+            's' => _("[No Subject]")            // Message subject
+        );
+        if (!empty($h)){
+            $udate = strtotime($h->getOb('date', true));
+            $subject = $h->getOb('subject', true);
+            if (empty($subject)) {
+                $subject = _("[No Subject]");
+            }
+            $match2 = array (
+                'r' => $h->getOb('date', true),
+                'm' => $h->getOb('message_id'),
+                's' => $subject,
+                'd' => String::convertCharset(@strftime("%a, %d %b %Y", $udate), NLS::getCharset(true)),
+                'x' => String::convertCharset(@strftime("%x", $udate), NLS::getCharset(true)),
+                'c' => String::convertCharset(@strftime("%c", $udate), NLS::getCharset(true))
+            );
+            
+            $match = array_merge($match, $match2);
+        }
+        $length = strlen($line);
+        $current_line = '';
+        $esc = 0;
+        for ($i = 0; $i < $length; $i++) { 
+            $char = substr($line, $i, 1);
+            if ($char == "%" && !$esc) {
+                $esc = 1;
+                continue;
+            }
+            if ($esc) { 
+                if(array_key_exists($char, $match)) {
+                    $current_line .= $match[$char];
+                } else {
+                    $current_line .= $char;
+                }
+                $esc = 0;
+            } else {
+                $current_line .= $char;
+            }
+        }
+    
+        return $current_line;
     }
 
 }


More information about the imp mailing list