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 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'app/models/mail_handler.rb') 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) -- cgit v1.2.3