Project

General

Profile

Feature #5864 » allow_regex_delimiters_v2.patch

Marius BĂLTEANU, 2016-11-23 21:50

View differences:

app/models/mail_handler.rb
# Removes the email body of text after the truncation configurations.
def cleanup_body(body)
delimiters = Setting.mail_handler_body_delimiters.to_s.split(/[\r\n]+/).reject(&:blank?).map {|s| Regexp.escape(s)}
delimiters = Setting.mail_handler_body_delimiters.to_s.split(/[\r\n]+/).reject(&:blank?)
delimiters = delimiters.map {|s| Regexp.escape(s)} unless Setting.mail_handler_enable_regex_delimiters?
unless delimiters.empty?
regex = Regexp.new("^[> ]*(#{ delimiters.join('|') })\s*[\r\n].*", Regexp::MULTILINE)
body = body.gsub(regex, '')
app/views/settings/_mail_handler.html.erb
<em class="info"><%= l(:text_line_separated) %></em>
</p>
<p>
<%= setting_check_box :mail_handler_enable_regex_delimiters %>
</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>
config/locales/en.yml
setting_attachment_extensions_allowed: Allowed extensions
setting_attachment_extensions_denied: Disallowed extensions
setting_new_item_menu_tab: Project menu tab for creating new objects
setting_mail_handler_enable_regex_delimiters: Enable regex delimiters
permission_add_project: Create project
permission_add_subprojects: Create subprojects
config/settings.yml
- issue_updated
mail_handler_body_delimiters:
default: ''
mail_handler_enable_regex_delimiters:
default: 0
mail_handler_excluded_filenames:
default: ''
mail_handler_api_enabled:
test/fixtures/mail_handler/ticket_reply_from_mail.eml
Return-Path: <jsmith@somenet.foo>
Received: from osiris ([127.0.0.1])
by OSIRIS
with hMailServer; Wed, 12 Oct 2016 03:05:50 -0700
Message-ID: <000501c8d452$a95cd7e0$0a00a8c0@osiris>
From: "John Smith" <JSmith@somenet.foo>
To: <redmine@somenet.foo>
Subject: New ticket on a given project
Date: Wed, 12 Oct 2016 13:05:38 +0300
MIME-Version: 1.0
Content-Type: text/plain;
format=flowed;
charset="iso-8859-1";
reply-type=original
Content-Transfer-Encoding: 7bit
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 6.00.2900.2869
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2869
Project: onlinestore
Status: Resolved
due date: 2010-12-31
Start Date:2010-01-01
Assigned to: John Smith
fixed version: alpha
estimated hours: 2.5
remaining hours: 1
done ratio: 30
This paragraph is before delimiter
On Wed, 11 Oct at 1:05 PM, Jon Smith <jsmith@somenet.foo<mailto:jsmith@somenet.foo>> wrote:
This paragraph is after the delimiter
test/unit/mail_handler_test.rb
end
end
test "truncate emails using a regex delimiter" do
delimiter = "On .*, .* at .*, .* <.*<mailto:.*>> wrote:"
with_settings :mail_handler_enable_regex_delimiters => '1', :mail_handler_body_delimiters => delimiter do
issue = submit_email('ticket_reply_from_mail.eml')
assert_issue_created(issue)
assert issue.description.include?('This paragraph is before delimiter')
assert !issue.description.include?('On Wed, 11 Oct at 1:05 PM, Jon Smith <jsmith@somenet.foo<mailto:jsmith@somenet.foo>> wrote:')
assert !issue.description.include?('This paragraph is after the delimiter')
end
with_settings :mail_handler_enable_regex_delimiters => '0', :mail_handler_body_delimiters => delimiter do
issue = submit_email('ticket_reply_from_mail.eml')
assert_issue_created(issue)
assert issue.description.include?('This paragraph is before delimiter')
assert issue.description.include?('On Wed, 11 Oct at 1:05 PM, Jon Smith <jsmith@somenet.foo<mailto:jsmith@somenet.foo>> wrote:')
assert issue.description.include?('This paragraph is after the delimiter')
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'})
(5-5/7)