From 700f96f277ad19b592d1224d567528f0622dc0e2 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 25 Jul 2012 15:58:08 +0200 Subject: [PATCH] Fixed/added workaround for followup problem with outlook emails. --- app/models/channel/email_parser.rb | 38 ++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/app/models/channel/email_parser.rb b/app/models/channel/email_parser.rb index 84624f7b6..eedd78cb2 100644 --- a/app/models/channel/email_parser.rb +++ b/app/models/channel/email_parser.rb @@ -379,4 +379,42 @@ class Channel::EmailParser return string end +end + +# workaround to parse subjects with 2 different encodings correctly (e. g. quoted-printable see test/fixtures/mail9.box) +module Mail + module Encodings + def Encodings.value_decode(str) + # Optimization: If there's no encoded-words in the string, just return it + return str unless str.index("=?") + + str = str.gsub(/\?=(\s*)=\?/, '?==?') # Remove whitespaces between 'encoded-word's + + # Split on white-space boundaries with capture, so we capture the white-space as well + str.split(/([ \t])/).map do |text| + if text.index('=?') .nil? + text + else + # Join QP encoded-words that are adjacent to avoid decoding partial chars +# text.gsub!(/\?\=\=\?.+?\?[Qq]\?/m, '') if text =~ /\?==\?/ + + # Search for occurences of quoted strings or plain strings + text.scan(/( # Group around entire regex to include it in matches + \=\?[^?]+\?([QB])\?[^?]+?\?\= # Quoted String with subgroup for encoding method + | # or + .+?(?=\=\?|$) # Plain String + )/xmi).map do |matches| + string, method = *matches + if method == 'b' || method == 'B' + b_value_decode(string) + elsif method == 'q' || method == 'Q' + q_value_decode(string) + else + string + end + end + end + end.join("") + end + end end \ No newline at end of file