##// END OF EJS Templates
Exclude attachments from incoming emails based on file name (#3413)....
Jean-Philippe Lang -
r11937:cfc05d310e71
parent child
Show More
@@ -267,6 +267,7 class MailHandler < ActionMailer::Base
267 def add_attachments(obj)
267 def add_attachments(obj)
268 if email.attachments && email.attachments.any?
268 if email.attachments && email.attachments.any?
269 email.attachments.each do |attachment|
269 email.attachments.each do |attachment|
270 next unless accept_attachment?(attachment)
270 obj.attachments << Attachment.create(:container => obj,
271 obj.attachments << Attachment.create(:container => obj,
271 :file => attachment.decoded,
272 :file => attachment.decoded,
272 :filename => attachment.filename,
273 :filename => attachment.filename,
@@ -276,6 +277,19 class MailHandler < ActionMailer::Base
276 end
277 end
277 end
278 end
278
279
280 # Returns false if the +attachment+ of the incoming email should be ignored
281 def accept_attachment?(attachment)
282 @excluded ||= Setting.mail_handler_excluded_filenames.to_s.split(',').map(&:strip).reject(&:blank?)
283 @excluded.each do |pattern|
284 regexp = %r{\A#{Regexp.escape(pattern).gsub("\\*", ".*")}\z}i
285 if attachment.filename.to_s =~ regexp
286 logger.info "MailHandler: ignoring attachment #{attachment.filename} matching #{pattern}"
287 return false
288 end
289 end
290 true
291 end
292
279 # Adds To and Cc as watchers of the given object if the sender has the
293 # Adds To and Cc as watchers of the given object if the sender has the
280 # appropriate permission
294 # appropriate permission
281 def add_watchers(obj)
295 def add_watchers(obj)
@@ -5,6 +5,11
5 <%= setting_text_area :mail_handler_body_delimiters, :rows => 5 %>
5 <%= setting_text_area :mail_handler_body_delimiters, :rows => 5 %>
6 <em class="info"><%= l(:text_line_separated) %></em>
6 <em class="info"><%= l(:text_line_separated) %></em>
7 </p>
7 </p>
8 <p>
9 <%= setting_text_field :mail_handler_excluded_filenames, :size => 60 %>
10 <em class="info"><%= l(:text_comma_separated) %>
11 <%= l(:label_example) %>: smime.p7s, *.vcf</em>
12 </p>
8 </div>
13 </div>
9
14
10 <div class="box tabular settings">
15 <div class="box tabular settings">
@@ -407,6 +407,7 en:
407 setting_non_working_week_days: Non-working days
407 setting_non_working_week_days: Non-working days
408 setting_jsonp_enabled: Enable JSONP support
408 setting_jsonp_enabled: Enable JSONP support
409 setting_default_projects_tracker_ids: Default trackers for new projects
409 setting_default_projects_tracker_ids: Default trackers for new projects
410 setting_mail_handler_excluded_filenames: Exclude attachments by name
410
411
411 permission_add_project: Create project
412 permission_add_project: Create project
412 permission_add_subprojects: Create subprojects
413 permission_add_subprojects: Create subprojects
@@ -404,6 +404,7 fr:
404 setting_non_working_week_days: Jours non travaillés
404 setting_non_working_week_days: Jours non travaillés
405 setting_jsonp_enabled: Activer le support JSONP
405 setting_jsonp_enabled: Activer le support JSONP
406 setting_default_projects_tracker_ids: Trackers par défaut pour les nouveaux projets
406 setting_default_projects_tracker_ids: Trackers par défaut pour les nouveaux projets
407 setting_mail_handler_excluded_filenames: Exclure les fichiers attachés par leur nom
407
408
408 permission_add_project: Créer un projet
409 permission_add_project: Créer un projet
409 permission_add_subprojects: Créer des sous-projets
410 permission_add_subprojects: Créer des sous-projets
@@ -147,6 +147,8 notified_events:
147 - issue_updated
147 - issue_updated
148 mail_handler_body_delimiters:
148 mail_handler_body_delimiters:
149 default: ''
149 default: ''
150 mail_handler_excluded_filenames:
151 default: ''
150 mail_handler_api_enabled:
152 mail_handler_api_enabled:
151 default: 0
153 default: 0
152 mail_handler_api_key:
154 mail_handler_api_key:
@@ -759,6 +759,24 class MailHandlerTest < ActiveSupport::TestCase
759 end
759 end
760 end
760 end
761
761
762 def test_attachments_that_match_mail_handler_excluded_filenames_should_be_ignored
763 with_settings :mail_handler_excluded_filenames => '*.vcf, *.jpg' do
764 issue = submit_email('ticket_with_attachment.eml', :issue => {:project => 'onlinestore'})
765 assert issue.is_a?(Issue)
766 assert !issue.new_record?
767 assert_equal 0, issue.reload.attachments.size
768 end
769 end
770
771 def test_attachments_that_do_not_match_mail_handler_excluded_filenames_should_be_attached
772 with_settings :mail_handler_excluded_filenames => '*.vcf, *.gif' do
773 issue = submit_email('ticket_with_attachment.eml', :issue => {:project => 'onlinestore'})
774 assert issue.is_a?(Issue)
775 assert !issue.new_record?
776 assert_equal 1, issue.reload.attachments.size
777 end
778 end
779
762 def test_email_with_long_subject_line
780 def test_email_with_long_subject_line
763 issue = submit_email('ticket_with_long_subject.eml')
781 issue = submit_email('ticket_with_long_subject.eml')
764 assert issue.is_a?(Issue)
782 assert issue.is_a?(Issue)
General Comments 0
You need to be logged in to leave comments. Login now