Do not raise error when an address header field is broken

This hopefully fixes the issue this time around:

https://github.com/cantino/huginn/issues/745#issuecomment-176628911

Akinori MUSHA 8 years ago
parent
commit
6e77551256
2 changed files with 26 additions and 2 deletions
  1. 8 2
      app/models/agents/imap_folder_agent.rb
  2. 18 0
      spec/models/agents/imap_folder_agent_spec.rb

+ 8 - 2
app/models/agents/imap_folder_agent.rb

@@ -227,8 +227,14 @@ module Agents
227 227
             }
228 228
           when 'from', 'to', 'cc'
229 229
             value.present? or next true
230
-            header = mail.header[key] or next false
231
-            header.addresses.any? { |address|
230
+            begin
231
+              # Mail::Field really needs to define respond_to_missing?
232
+              # so we could use try(:addresses) here.
233
+              addresses = mail.header[key].addresses
234
+            rescue NoMethodError
235
+              next false
236
+            end
237
+            addresses.any? { |address|
232 238
               Array(value).any? { |pattern|
233 239
                 glob_match?(pattern, address)
234 240
               }

+ 18 - 0
spec/models/agents/imap_folder_agent_spec.rb

@@ -265,6 +265,24 @@ describe Agents::ImapFolderAgent do
265 265
         }
266 266
         expect { @checker.check }.to change { Event.count }.by(1)
267 267
       end
268
+
269
+      describe 'processing mails with a broken From header value' do
270
+        before do
271
+          # "from" patterns work against mail addresses and not
272
+          # against text parts, so these mails should be skipped if a
273
+          # "from" condition is given.
274
+          @mails.first.header['from'] = '.'
275
+          @mails.last.header['from'] = '@'
276
+        end
277
+
278
+        it 'should ignore them without failing if a "from" condition is given' do
279
+          @checker.options['conditions']['from'] = '*'
280
+
281
+          expect {
282
+            expect { @checker.check }.not_to change { Event.count }
283
+          }.not_to raise_exception
284
+        end
285
+      end
268 286
     end
269 287
   end
270 288