From 059dc8b63571a721ea71ab4d260ee1906eed652c Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Fri, 4 Nov 2016 19:11:01 +0100 Subject: [PATCH] identity_select: Support Received header (#5085) --- CHANGELOG | 1 + plugins/identity_select/composer.json | 2 +- plugins/identity_select/identity_select.php | 31 +++++++++++++++++++-- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 5e8939ad0..5c92ba85f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,7 @@ CHANGELOG Roundcube Webmail =========================== +- identity_select: Support Received header (#5085) - Plugin API: Added get_compose_responses hook (#5457) - Display error when trying to upload more files than specified in max_file_uploads (#5483) - Add missing sql upgrade file for 'ip' column resize in session table (#5465) diff --git a/plugins/identity_select/composer.json b/plugins/identity_select/composer.json index c31239360..7304bd463 100644 --- a/plugins/identity_select/composer.json +++ b/plugins/identity_select/composer.json @@ -3,7 +3,7 @@ "type": "roundcube-plugin", "description": "On reply to a message user identity selection is based on\n\t\tcontent of standard headers like From, To, Cc and Return-Path.\n\t\tHere you can add header(s) set by your SMTP server (e.g.\n\t\tDelivered-To, Envelope-To, X-Envelope-To, X-RCPT-TO) to make\n\t\tidentity selection more accurate.", "license": "GPLv3+", - "version": "1.0", + "version": "1.1", "authors": [ { "name": "Aleksander Machniak", diff --git a/plugins/identity_select/identity_select.php b/plugins/identity_select/identity_select.php index 7909ebb61..07aa97b47 100644 --- a/plugins/identity_select/identity_select.php +++ b/plugins/identity_select/identity_select.php @@ -12,6 +12,10 @@ * Enable the plugin in config.inc.php and add your desired headers: * $config['identity_select_headers'] = array('Delivered-To'); * + * Note: 'Received' header is also supported, but has bigger impact + * on performance, as it's body is potentially much bigger + * than other headers used by Roundcube + * * @author Aleksander Machniak * @license GNU GPLv3+ */ @@ -52,9 +56,9 @@ class identity_select extends rcube_plugin $rcmail = rcmail::get_instance(); foreach ((array)$rcmail->config->get('identity_select_headers', array()) as $header) { - if ($header = $p['message']->headers->get($header, false)) { + if ($emails = $this->get_email_from_header($p['message'], $header)) { foreach ($p['identities'] as $idx => $ident) { - if (in_array($ident['email_ascii'], (array)$header)) { + if (in_array($ident['email_ascii'], $emails)) { $p['selected'] = $idx; break 2; } @@ -64,4 +68,27 @@ class identity_select extends rcube_plugin return $p; } + + /** + * Extract email address from specified message header + */ + protected function get_email_from_header($message, $header) + { + $value = $message->headers->get($header, false); + + if (strtolower($header) == 'received') { + // find first email address in all Received headers + $email = null; + foreach ((array) $value as $entry) { + if (preg_match('/[\s\t]+for[\s\t]+<([^>]+)>/', $entry, $matches)) { + $email = $matches[1]; + break; + } + } + + $value = $email; + } + + return (array) $value; + } }