Skip to content

Commit

Permalink
More efficient determination of attribution text
Browse files Browse the repository at this point in the history
Only parse headers/from address as needed
Fix setting attribution text when from address doesn't exist.
Remove incorrect placeholders
  • Loading branch information
slusarz committed Aug 26, 2014
1 parent 715a4a0 commit 296622b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 68 deletions.
6 changes: 4 additions & 2 deletions imp/lib/Compose.php
Expand Up @@ -2094,14 +2094,16 @@ public function replyMessageText($contents, array $opts = array())
$from = $h->getOb('from');

if ($prefs->getValue('reply_headers') && !empty($h)) {
$from_text = strval(new IMP_Prefs_AttribText($from, $h, '%f'));

$msg_pre = '----- ' .
($from ? sprintf(_("Message from %s"), $from) : _("Message")) .
($from_text ? sprintf(_("Message from %s"), $from_text) : _("Message")) .
/* Extra '-'s line up with "End Message" below. */
" ---------\n" .
$this->_getMsgHeaders($h);

$msg_post = "\n\n----- " .
($from ? sprintf(_("End message from %s"), $from) : _("End message")) .
($from_text ? sprintf(_("End message from %s"), $from_text) : _("End message")) .
" -----\n";
} else {
$msg_pre = strval(new IMP_Prefs_AttribText($from, $h));
Expand Down
137 changes: 71 additions & 66 deletions imp/lib/Prefs/AttribText.php
Expand Up @@ -34,76 +34,81 @@ class IMP_Prefs_AttribText
*
* @param string|Horde_Mail_Rfc822_Object $from The email address of the
* original sender.
* @param Horde_Mime_Headers $h The headers object for the
* message.
* @param Horde_Mime_Headers $h The headers object for
* the message.
* @param string $attrib Use this for the
* attribution config
* instead of the default
* prefs version.
*/
public function __construct($from, Horde_Mime_Headers $h)
public function __construct($from, Horde_Mime_Headers $h, $attrib = null)
{
global $prefs;

$addressList = $nameList = array();
$addr_list = IMP::parseAddressList($from);

foreach ($addr_list as $addr) {
if (!is_null($addr->mailbox)) {
$addressList[] = $addr->bare_address;
}

if (!is_null($addr->personal)) {
$nameList[] = $addr->personal;
} elseif (!is_null($addr->mailbox)) {
$nameList[] = $addr->mailbox;
}
}

/* Define the macros. */
if (is_array($message_id = $h->getValue('message_id'))) {
$message_id = reset($message_id);
}
if (!($subject = $h->getValue('subject'))) {
$subject = _("[No Subject]");
}
$udate = strtotime($h->getValue('date'));

$match = array(
/* New line. */
'/%n/' => "\n",

/* The '%' character. */
'/%%/' => '%',

/* Name and email address of original sender. */
'/%f/' => $from->writeAddress(array('noquote' => true)),

/* Senders email address(es). */
'/%a/' => implode(', ', $addressList),

/* Senders name(s). */
'/%p/' => implode(', ', $nameList),

/* RFC 822 date and time. */
'/%r/' => $h->getValue('date'),

/* Date as ddd, dd mmm yyyy. */
'/%d/' => strftime("%a, %d %b %Y", $udate),

/* Date in locale's default. */
'/%x/' => strftime("%x", $udate),

/* Date and time in locale's default. */
'/%c/' => strftime("%c", $udate),

/* Message-ID. */
'/%m/' => $message_id,

/* Message subject. */
'/%s/' => $subject
);

$this->_text = preg_replace(
array_keys($match),
array_values($match),
$prefs->getValue('attrib_text')
$this->_text = preg_replace_callback(
'/\%./',
function ($matches) use ($from, $h) {
switch ($matches[0]) {
case '%n': /* New line. */
return "\n";

case '%%': /* Percent character. */
return '%';

case '%f': /* Name and email address of original sender. */
if ($from) {
$from = new Horde_Mail_Rfc822_Address($from);
return $from->writeAddress(array('noquote' => true));
}
return _("Unknown Sender");

case '%a': /* Senders email address(es). */
case '%p': /* Senders name(s). */
$out = array();
foreach (IMP::parseAddressList($from) as $addr) {
if ($matches[0] == '%a') {
if (!is_null($addr->mailbox)) {
$out[] = $addr->bare_address;
}
} else {
$out[] = $addr->label;
}
}
return count($out)
? implode(', ', $out)
: _("Unknown Sender");

case '%r': /* RFC 822 date and time. */
return $h->getValue('date');

case '%d': /* Date as ddd, dd mmm yyyy. */
return strftime(
"%a, %d %b %Y",
strtotime($h->getValue('date'))
);

case '%c': /* Date and time in locale's default. */
case '%x': /* Date in locale's default. */
return strftime(
$matches[0],
strtotime($h->getValue('date'))
);

case '%m': /* Message-ID. */
return is_array($message_id = $h->getValue('message-id'))
? reset($message_id)
: $message_id;

case '%s': /* Message subject. */
return strlen($subject = $h->getValue('subject'))
? $subject
: _("[No Subject]");

default:
return '';
}
},
is_null($attrib) ? $prefs->getValue('attrib_text') : $attrib
);
}

Expand Down

0 comments on commit 296622b

Please sign in to comment.