]> source.dussan.org Git - redmine.git/commitdiff
Exclude attachments from incoming emails based on file name (#3413).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 29 Sep 2013 11:50:49 +0000 (11:50 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 29 Sep 2013 11:50:49 +0000 (11:50 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@12167 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/mail_handler.rb
app/views/settings/_mail_handler.html.erb
config/locales/en.yml
config/locales/fr.yml
config/settings.yml
test/unit/mail_handler_test.rb

index fec2675fc1689f35b05e0bfda30cb2062fff6c60..3c9857873e7f2307a6b2e0d56e2d8231f8771500 100644 (file)
@@ -267,6 +267,7 @@ class MailHandler < ActionMailer::Base
   def add_attachments(obj)
     if email.attachments && email.attachments.any?
       email.attachments.each do |attachment|
+        next unless accept_attachment?(attachment)
         obj.attachments << Attachment.create(:container => obj,
                           :file => attachment.decoded,
                           :filename => attachment.filename,
@@ -276,6 +277,19 @@ class MailHandler < ActionMailer::Base
     end
   end
 
+  # Returns false if the +attachment+ of the incoming email should be ignored
+  def accept_attachment?(attachment)
+    @excluded ||= Setting.mail_handler_excluded_filenames.to_s.split(',').map(&:strip).reject(&:blank?)
+    @excluded.each do |pattern|
+      regexp = %r{\A#{Regexp.escape(pattern).gsub("\\*", ".*")}\z}i
+      if attachment.filename.to_s =~ regexp
+        logger.info "MailHandler: ignoring attachment #{attachment.filename} matching #{pattern}"
+        return false
+      end
+    end
+    true
+  end
+
   # Adds To and Cc as watchers of the given object if the sender has the
   # appropriate permission
   def add_watchers(obj)
index 1051436b52d9e70580c9b6e3331777f8364e2add..f255b4adc8225914bbe301464ab83414418f761b 100644 (file)
@@ -5,6 +5,11 @@
     <%= setting_text_area :mail_handler_body_delimiters, :rows => 5 %>
     <em class="info"><%= l(:text_line_separated) %></em>
   </p>
+  <p>
+    <%= setting_text_field :mail_handler_excluded_filenames, :size => 60 %>
+    <em class="info"><%= l(:text_comma_separated) %>
+    <%= l(:label_example) %>: smime.p7s, *.vcf</em>
+  </p>
 </div>
 
 <div class="box tabular settings">
index 6db1262838cc14040d25e5796b7210bd7ff7362b..ae00c7759ba08d60099f9eb8973ba2734d4a7f2c 100644 (file)
@@ -407,6 +407,7 @@ en:
   setting_non_working_week_days: Non-working days
   setting_jsonp_enabled: Enable JSONP support
   setting_default_projects_tracker_ids: Default trackers for new projects
+  setting_mail_handler_excluded_filenames: Exclude attachments by name
 
   permission_add_project: Create project
   permission_add_subprojects: Create subprojects
index 2b17af2b5d702be8462a390b0665dbea24f7ee8d..4cf57e73914815aea5052ea558496c20b96c5c69 100644 (file)
@@ -404,6 +404,7 @@ fr:
   setting_non_working_week_days: Jours non travaillés
   setting_jsonp_enabled: Activer le support JSONP
   setting_default_projects_tracker_ids: Trackers par défaut pour les nouveaux projets
+  setting_mail_handler_excluded_filenames: Exclure les fichiers attachés par leur nom
 
   permission_add_project: Créer un projet
   permission_add_subprojects: Créer des sous-projets
index 0c9f270a137c9fa9fd3ff2908ce10165df459002..61c24c7980298f44e42164bc876ff23d97402f35 100644 (file)
@@ -147,6 +147,8 @@ notified_events:
   - issue_updated
 mail_handler_body_delimiters:
   default: ''
+mail_handler_excluded_filenames:
+  default: ''
 mail_handler_api_enabled:
   default: 0
 mail_handler_api_key:
index 3244d8cd23d3bd764f12ebecb3fba1c7bda759c5..7e0f80cf3beb220eb255e0b861debd2a9da29da1 100644 (file)
@@ -759,6 +759,24 @@ class MailHandlerTest < ActiveSupport::TestCase
     end
   end
 
+  def test_attachments_that_match_mail_handler_excluded_filenames_should_be_ignored
+    with_settings :mail_handler_excluded_filenames => '*.vcf, *.jpg' do
+      issue = submit_email('ticket_with_attachment.eml', :issue => {:project => 'onlinestore'})
+      assert issue.is_a?(Issue)
+      assert !issue.new_record?
+      assert_equal 0, issue.reload.attachments.size
+    end
+  end
+
+  def test_attachments_that_do_not_match_mail_handler_excluded_filenames_should_be_attached
+    with_settings :mail_handler_excluded_filenames => '*.vcf, *.gif' do
+      issue = submit_email('ticket_with_attachment.eml', :issue => {:project => 'onlinestore'})
+      assert issue.is_a?(Issue)
+      assert !issue.new_record?
+      assert_equal 1, issue.reload.attachments.size
+    end
+  end
+
   def test_email_with_long_subject_line
     issue = submit_email('ticket_with_long_subject.eml')
     assert issue.is_a?(Issue)