Browse Source

Fized issue #2723 - Unable to process mail with enabled follow up detection in content of attachments.

Martin Edenhofer 5 years ago
parent
commit
c8ef769829
2 changed files with 192 additions and 13 deletions
  1. 11 2
      app/models/channel/filter/follow_up_check.rb
  2. 181 11
      spec/models/channel/email_parser_spec.rb

+ 11 - 2
app/models/channel/filter/follow_up_check.rb

@@ -29,9 +29,18 @@ module Channel::Filter::FollowUpCheck
     # get ticket# from attachment
     if setting.include?('attachment') && mail[:attachments]
       mail[:attachments].each do |attachment|
-        next if !attachment[:data]
+        next if attachment[:data].blank?
+        next if attachment[:preferences].blank?
+        next if attachment[:preferences]['Mime-Type'].blank?
+
+        if %r{text/html}i.match?(attachment[:preferences]['Mime-Type'])
+          ticket = Ticket::Number.check(attachment[:data].html2text)
+        end
+
+        if %r{text/plain}i.match?(attachment[:preferences]['Mime-Type'])
+          ticket = Ticket::Number.check(attachment[:data])
+        end
 
-        ticket = Ticket::Number.check(attachment[:data].html2text)
         next if !ticket
 
         Rails.logger.debug { "Follow-up for '##{ticket.number}' in attachment." }

+ 181 - 11
spec/models/channel/email_parser_spec.rb

@@ -194,7 +194,7 @@ RSpec.describe Channel::EmailParser, type: :model do
           RAW
         end
 
-        shared_context 'ticket reference in attachment' do
+        shared_context 'ticket reference in text/plain attachment' do
           let(:raw_mail) { <<~RAW.chomp }
             From: me@example.com
             Content-Type: multipart/mixed; boundary="Apple-Mail=_ED77AC8D-FB6F-40E5-8FBE-D41FF5E1BAF2"
@@ -224,6 +224,96 @@ RSpec.describe Channel::EmailParser, type: :model do
           RAW
         end
 
+        shared_context 'ticket reference in text/html (as content) attachment' do
+          let(:raw_mail) { <<~RAW.chomp }
+            From: me@example.com
+            Content-Type: multipart/mixed; boundary="Apple-Mail=_ED77AC8D-FB6F-40E5-8FBE-D41FF5E1BAF2"
+            Subject: no reference
+            Date: Sun, 30 Aug 2015 23:20:54 +0200
+            To: Martin Edenhofer <me@znuny.com>
+            Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2104\))
+            X-Mailer: Apple Mail (2.2104)
+
+
+            --Apple-Mail=_ED77AC8D-FB6F-40E5-8FBE-D41FF5E1BAF2
+            Content-Transfer-Encoding: 7bit
+            Content-Type: text/plain;
+              charset=us-ascii
+
+            no reference
+            --Apple-Mail=_ED77AC8D-FB6F-40E5-8FBE-D41FF5E1BAF2
+            Content-Disposition: attachment;
+              filename=test1.txt
+            Content-Type: text/html;
+              name="test.txt"
+            Content-Transfer-Encoding: 7bit
+
+            <div>Some Text #{ticket_ref}</div>
+
+            --Apple-Mail=_ED77AC8D-FB6F-40E5-8FBE-D41FF5E1BAF2--
+          RAW
+        end
+
+        shared_context 'ticket reference in text/html (attribute) attachment' do
+          let(:raw_mail) { <<~RAW.chomp }
+            From: me@example.com
+            Content-Type: multipart/mixed; boundary="Apple-Mail=_ED77AC8D-FB6F-40E5-8FBE-D41FF5E1BAF2"
+            Subject: no reference
+            Date: Sun, 30 Aug 2015 23:20:54 +0200
+            To: Martin Edenhofer <me@znuny.com>
+            Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2104\))
+            X-Mailer: Apple Mail (2.2104)
+
+
+            --Apple-Mail=_ED77AC8D-FB6F-40E5-8FBE-D41FF5E1BAF2
+            Content-Transfer-Encoding: 7bit
+            Content-Type: text/plain;
+              charset=us-ascii
+
+            no reference
+            --Apple-Mail=_ED77AC8D-FB6F-40E5-8FBE-D41FF5E1BAF2
+            Content-Disposition: attachment;
+              filename=test1.txt
+            Content-Type: text/html;
+              name="test.txt"
+            Content-Transfer-Encoding: 7bit
+
+            <div>Some Text <b data-something="#{ticket_ref}">some text</b></div>
+
+            --Apple-Mail=_ED77AC8D-FB6F-40E5-8FBE-D41FF5E1BAF2--
+          RAW
+        end
+
+        shared_context 'ticket reference in image/jpg attachment' do
+          let(:raw_mail) { <<~RAW.chomp }
+            From: me@example.com
+            Content-Type: multipart/mixed; boundary="Apple-Mail=_ED77AC8D-FB6F-40E5-8FBE-D41FF5E1BAF2"
+            Subject: no reference
+            Date: Sun, 30 Aug 2015 23:20:54 +0200
+            To: Martin Edenhofer <me@znuny.com>
+            Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2104\))
+            X-Mailer: Apple Mail (2.2104)
+
+
+            --Apple-Mail=_ED77AC8D-FB6F-40E5-8FBE-D41FF5E1BAF2
+            Content-Transfer-Encoding: 7bit
+            Content-Type: text/plain;
+              charset=us-ascii
+
+            no reference
+            --Apple-Mail=_ED77AC8D-FB6F-40E5-8FBE-D41FF5E1BAF2
+            Content-Disposition: attachment;
+              filename=test1.jpg
+            Content-Type: image/jpg;
+              name="test.jpg"
+            Content-Transfer-Encoding: 7bit
+
+            Some Text #{ticket_ref}
+
+            --Apple-Mail=_ED77AC8D-FB6F-40E5-8FBE-D41FF5E1BAF2--
+          RAW
+        end
+
         shared_context 'ticket reference in In-Reply-To header' do
           let(:raw_mail) { <<~RAW.chomp }
             From: me@example.com
@@ -328,8 +418,23 @@ RSpec.describe Channel::EmailParser, type: :model do
             include_examples 'creates a new ticket'
           end
 
-          context 'when attachment contains ticket reference' do
-            include_context 'ticket reference in attachment'
+          context 'when text/plain attachment contains ticket reference' do
+            include_context 'ticket reference in text/plain attachment'
+            include_examples 'creates a new ticket'
+          end
+
+          context 'when text/html attachment (as content) contains ticket reference' do
+            include_context 'ticket reference in text/html (as content) attachment'
+            include_examples 'creates a new ticket'
+          end
+
+          context 'when text/html attachment (attribute) contains ticket reference' do
+            include_context 'ticket reference in text/html (attribute) attachment'
+            include_examples 'creates a new ticket'
+          end
+
+          context 'when image/jpg attachment contains ticket reference' do
+            include_context 'ticket reference in image/jpg attachment'
             include_examples 'creates a new ticket'
           end
 
@@ -448,8 +553,23 @@ RSpec.describe Channel::EmailParser, type: :model do
             end
           end
 
-          context 'when attachment contains ticket reference' do
-            include_context 'ticket reference in attachment'
+          context 'when text/plain attachment contains ticket reference' do
+            include_context 'ticket reference in text/plain attachment'
+            include_examples 'creates a new ticket'
+          end
+
+          context 'when text/html attachment (as content) contains ticket reference' do
+            include_context 'ticket reference in text/html (as content) attachment'
+            include_examples 'creates a new ticket'
+          end
+
+          context 'when text/html attachment (attribute) contains ticket reference' do
+            include_context 'ticket reference in text/html (attribute) attachment'
+            include_examples 'creates a new ticket'
+          end
+
+          context 'when image/jpg attachment contains ticket reference' do
+            include_context 'ticket reference in image/jpg attachment'
             include_examples 'creates a new ticket'
           end
 
@@ -491,11 +611,26 @@ RSpec.describe Channel::EmailParser, type: :model do
             include_examples 'creates a new ticket'
           end
 
-          context 'when attachment contains ticket reference' do
-            include_context 'ticket reference in attachment'
+          context 'when text/plain attachment contains ticket reference' do
+            include_context 'ticket reference in text/plain attachment'
+            include_examples 'adds message to ticket'
+          end
+
+          context 'when text/html attachment (as content) contains ticket reference' do
+            include_context 'ticket reference in text/html (as content) attachment'
             include_examples 'adds message to ticket'
           end
 
+          context 'when text/html attachment (attribute) contains ticket reference' do
+            include_context 'ticket reference in text/html (attribute) attachment'
+            include_examples 'creates a new ticket'
+          end
+
+          context 'when image/jpg attachment contains ticket reference' do
+            include_context 'ticket reference in image/jpg attachment'
+            include_examples 'creates a new ticket'
+          end
+
           context 'when In-Reply-To header contains article message-id' do
             include_context 'ticket reference in In-Reply-To header'
             include_examples 'creates a new ticket'
@@ -534,8 +669,28 @@ RSpec.describe Channel::EmailParser, type: :model do
             include_examples 'creates a new ticket'
           end
 
-          context 'when attachment contains ticket reference' do
-            include_context 'ticket reference in attachment'
+          context 'when text/plain attachment contains ticket reference' do
+            include_context 'ticket reference in text/plain attachment'
+            include_examples 'creates a new ticket'
+          end
+
+          context 'when text/html attachment (as content) contains ticket reference' do
+            include_context 'ticket reference in text/html (as content) attachment'
+            include_examples 'creates a new ticket'
+          end
+
+          context 'when text/html attachment (attribute) contains ticket reference' do
+            include_context 'ticket reference in text/html (attribute) attachment'
+            include_examples 'creates a new ticket'
+          end
+
+          context 'when image/jpg attachment contains ticket reference' do
+            include_context 'ticket reference in image/jpg attachment'
+            include_examples 'creates a new ticket'
+          end
+
+          context 'when image/jpg attachment contains ticket reference' do
+            include_context 'ticket reference in image/jpg attachment'
             include_examples 'creates a new ticket'
           end
 
@@ -592,11 +747,26 @@ RSpec.describe Channel::EmailParser, type: :model do
             include_examples 'adds message to ticket'
           end
 
-          context 'when attachment contains ticket reference' do
-            include_context 'ticket reference in attachment'
+          context 'when text/plain attachment contains ticket reference' do
+            include_context 'ticket reference in text/plain attachment'
+            include_examples 'adds message to ticket'
+          end
+
+          context 'when text/html attachment (as content) contains ticket reference' do
+            include_context 'ticket reference in text/html (as content) attachment'
             include_examples 'adds message to ticket'
           end
 
+          context 'when text/html attachment (attribute) contains ticket reference' do
+            include_context 'ticket reference in text/html (attribute) attachment'
+            include_examples 'creates a new ticket'
+          end
+
+          context 'when image/jpg attachment contains ticket reference' do
+            include_context 'ticket reference in image/jpg attachment'
+            include_examples 'creates a new ticket'
+          end
+
           context 'when In-Reply-To header contains article message-id' do
             include_context 'ticket reference in In-Reply-To header'
             include_examples 'adds message to ticket'