From cfc05d310e711de8326fbf9e71cbe7ffb264c54a Mon Sep 17 00:00:00 2001
From: Jean-Philippe Lang
Date: Sun, 29 Sep 2013 11:50:49 +0000
Subject: Exclude attachments from incoming emails based on file name (#3413).
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@12167 e93f8b46-1217-0410-a6f0-8f06a7374b81
---
app/models/mail_handler.rb | 14 ++++++++++++++
app/views/settings/_mail_handler.html.erb | 5 +++++
config/locales/en.yml | 1 +
config/locales/fr.yml | 1 +
config/settings.yml | 2 ++
test/unit/mail_handler_test.rb | 18 ++++++++++++++++++
6 files changed, 41 insertions(+)
diff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rb
index fec2675fc..3c9857873 100644
--- a/app/models/mail_handler.rb
+++ b/app/models/mail_handler.rb
@@ -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)
diff --git a/app/views/settings/_mail_handler.html.erb b/app/views/settings/_mail_handler.html.erb
index 1051436b5..f255b4adc 100644
--- a/app/views/settings/_mail_handler.html.erb
+++ b/app/views/settings/_mail_handler.html.erb
@@ -5,6 +5,11 @@
<%= setting_text_area :mail_handler_body_delimiters, :rows => 5 %>
<%= l(:text_line_separated) %>
+
+ <%= setting_text_field :mail_handler_excluded_filenames, :size => 60 %>
+ <%= l(:text_comma_separated) %>
+ <%= l(:label_example) %>: smime.p7s, *.vcf
+
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 6db126283..ae00c7759 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -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
diff --git a/config/locales/fr.yml b/config/locales/fr.yml
index 2b17af2b5..4cf57e739 100644
--- a/config/locales/fr.yml
+++ b/config/locales/fr.yml
@@ -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
diff --git a/config/settings.yml b/config/settings.yml
index 0c9f270a1..61c24c798 100644
--- a/config/settings.yml
+++ b/config/settings.yml
@@ -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:
diff --git a/test/unit/mail_handler_test.rb b/test/unit/mail_handler_test.rb
index 3244d8cd2..7e0f80cf3 100644
--- a/test/unit/mail_handler_test.rb
+++ b/test/unit/mail_handler_test.rb
@@ -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)
--
cgit v1.2.3