From fcb7625616a8bb657a9eb4e6349c3f64df365f26 Mon Sep 17 00:00:00 2001 From: Sony Mathew Date: Tue, 21 Jul 2020 12:20:46 +0530 Subject: [PATCH] Bug: Fixed quoted text parsing in incoming email (#1076) * Bugfix: Fixed quoted text parsing in incoming email We had changed the support email from an account based only value to account based with fallback on global config and environment variable. Incoming email quoted text parsing was still based on account based support email. Changed this to utilize the newly introduced fallback values from global config and environment for parsing quoted text. * Bugfix: Added one more sender agnostic regex for quoted text parsing --- app/presenters/mail_presenter.rb | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/app/presenters/mail_presenter.rb b/app/presenters/mail_presenter.rb index cd3d75fc4..f97dd1b4c 100644 --- a/app/presenters/mail_presenter.rb +++ b/app/presenters/mail_presenter.rb @@ -85,18 +85,30 @@ class MailPresenter < SimpleDelegator end def quoted_text_regexes - sender_agnostic_regexes = [ + return sender_agnostic_regexes if @account.nil? || account_support_email.blank? + + [ + Regexp.new("From:\s*" + Regexp.escape(account_support_email), Regexp::IGNORECASE), + Regexp.new('<' + Regexp.escape(account_support_email) + '>', Regexp::IGNORECASE), + Regexp.new(Regexp.escape(account_support_email) + "\s+wrote:", Regexp::IGNORECASE), + Regexp.new('On(.*)' + Regexp.escape(account_support_email) + '(.*)wrote:', Regexp::IGNORECASE) + ] + sender_agnostic_regexes + end + + def sender_agnostic_regexes + @sender_agnostic_regexes ||= [ Regexp.new("^.*On.*(\n)?wrote:$", Regexp::IGNORECASE), + Regexp.new('^.*On(.*)(.*)wrote:$', Regexp::IGNORECASE), Regexp.new("-+original\s+message-+\s*$", Regexp::IGNORECASE), Regexp.new("from:\s*$", Regexp::IGNORECASE) ] - return sender_agnostic_regexes if @account.nil? || @account.support_email.blank? + end - [ - Regexp.new("From:\s*" + Regexp.escape(@account.support_email), Regexp::IGNORECASE), - Regexp.new('<' + Regexp.escape(@account.support_email) + '>', Regexp::IGNORECASE), - Regexp.new(Regexp.escape(@account.support_email) + "\s+wrote:", Regexp::IGNORECASE), - Regexp.new('On(.*)' + Regexp.escape(@account.support_email) + '(.*)wrote:', Regexp::IGNORECASE) - ] + sender_agnostic_regexes + def account_support_email + @account_support_email ||= begin + @account.support_email || + GlobalConfig.get('MAILER_SUPPORT_EMAIL')['MAILER_SUPPORT_EMAIL'] || + ENV.fetch('MAILER_SENDER_EMAIL', nil) + end end end