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
This commit is contained in:
Jean-Philippe Lang 2013-09-29 11:50:49 +00:00
parent 2c97f9ecde
commit cfc05d310e
6 changed files with 41 additions and 0 deletions

View File

@ -267,6 +267,7 @@ class MailHandler < ActionMailer::Base
def add_attachments(obj) def add_attachments(obj)
if email.attachments && email.attachments.any? if email.attachments && email.attachments.any?
email.attachments.each do |attachment| email.attachments.each do |attachment|
next unless accept_attachment?(attachment)
obj.attachments << Attachment.create(:container => obj, obj.attachments << Attachment.create(:container => obj,
:file => attachment.decoded, :file => attachment.decoded,
:filename => attachment.filename, :filename => attachment.filename,
@ -276,6 +277,19 @@ class MailHandler < ActionMailer::Base
end end
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 # Adds To and Cc as watchers of the given object if the sender has the
# appropriate permission # appropriate permission
def add_watchers(obj) def add_watchers(obj)

View File

@ -5,6 +5,11 @@
<%= setting_text_area :mail_handler_body_delimiters, :rows => 5 %> <%= setting_text_area :mail_handler_body_delimiters, :rows => 5 %>
<em class="info"><%= l(:text_line_separated) %></em> <em class="info"><%= l(:text_line_separated) %></em>
</p> </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>
<div class="box tabular settings"> <div class="box tabular settings">

View File

@ -407,6 +407,7 @@ en:
setting_non_working_week_days: Non-working days setting_non_working_week_days: Non-working days
setting_jsonp_enabled: Enable JSONP support setting_jsonp_enabled: Enable JSONP support
setting_default_projects_tracker_ids: Default trackers for new projects 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_project: Create project
permission_add_subprojects: Create subprojects permission_add_subprojects: Create subprojects

View File

@ -404,6 +404,7 @@ fr:
setting_non_working_week_days: Jours non travaillés setting_non_working_week_days: Jours non travaillés
setting_jsonp_enabled: Activer le support JSONP setting_jsonp_enabled: Activer le support JSONP
setting_default_projects_tracker_ids: Trackers par défaut pour les nouveaux projets 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_project: Créer un projet
permission_add_subprojects: Créer des sous-projets permission_add_subprojects: Créer des sous-projets

View File

@ -147,6 +147,8 @@ notified_events:
- issue_updated - issue_updated
mail_handler_body_delimiters: mail_handler_body_delimiters:
default: '' default: ''
mail_handler_excluded_filenames:
default: ''
mail_handler_api_enabled: mail_handler_api_enabled:
default: 0 default: 0
mail_handler_api_key: mail_handler_api_key:

View File

@ -759,6 +759,24 @@ class MailHandlerTest < ActiveSupport::TestCase
end end
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 def test_email_with_long_subject_line
issue = submit_email('ticket_with_long_subject.eml') issue = submit_email('ticket_with_long_subject.eml')
assert issue.is_a?(Issue) assert issue.is_a?(Issue)