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
561 561

  
562 562
  # Removes the email body of text after the truncation configurations.
563 563
  def cleanup_body(body)
564
    delimiters = Setting.mail_handler_body_delimiters.to_s.split(/[\r\n]+/).reject(&:blank?).map {|s| Regexp.escape(s)}
564
    delimiters = Setting.mail_handler_body_delimiters.to_s.split(/[\r\n]+/).reject(&:blank?)
565
    delimiters = delimiters.map {|s| Regexp.escape(s)} unless Setting.mail_handler_enable_regex_delimiters?
566

  
565 567
    unless delimiters.empty?
566 568
      regex = Regexp.new("^[> ]*(#{ delimiters.join('|') })\s*[\r\n].*", Regexp::MULTILINE)
567 569
      body = body.gsub(regex, '')
app/views/settings/_mail_handler.html.erb
6 6
    <em class="info"><%= l(:text_line_separated) %></em>
7 7
  </p>
8 8
  <p>
9
    <%= setting_check_box :mail_handler_enable_regex_delimiters %>
10
  </p>
11
  <p>
9 12
    <%= setting_text_field :mail_handler_excluded_filenames, :size => 60 %>
10 13
    <em class="info"><%= l(:text_comma_separated) %>
11 14
    <%= l(:label_example) %>: smime.p7s, *.vcf</em>
config/locales/en.yml
448 448
  setting_attachment_extensions_allowed: Allowed extensions
449 449
  setting_attachment_extensions_denied: Disallowed extensions
450 450
  setting_new_item_menu_tab: Project menu tab for creating new objects
451
  setting_mail_handler_enable_regex_delimiters: Enable regex delimiters
451 452

  
452 453
  permission_add_project: Create project
453 454
  permission_add_subprojects: Create subprojects
config/settings.yml
182 182
  - issue_updated
183 183
mail_handler_body_delimiters:
184 184
  default: ''
185
mail_handler_enable_regex_delimiters:
186
  default: 0
185 187
mail_handler_excluded_filenames:
186 188
  default: ''
187 189
mail_handler_api_enabled:
test/fixtures/mail_handler/ticket_reply_from_mail.eml
1
Return-Path: <jsmith@somenet.foo>
2
Received: from osiris ([127.0.0.1])
3
	by OSIRIS
4
	with hMailServer; Wed, 12 Oct 2016 03:05:50 -0700
5
Message-ID: <000501c8d452$a95cd7e0$0a00a8c0@osiris>
6
From: "John Smith" <JSmith@somenet.foo>
7
To: <redmine@somenet.foo>
8
Subject: New ticket on a given project
9
Date: Wed, 12 Oct 2016 13:05:38 +0300
10
MIME-Version: 1.0
11
Content-Type: text/plain;
12
	format=flowed;
13
	charset="iso-8859-1";
14
	reply-type=original
15
Content-Transfer-Encoding: 7bit
16
X-Priority: 3
17
X-MSMail-Priority: Normal
18
X-Mailer: Microsoft Outlook Express 6.00.2900.2869
19
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2869
20

  
21
Project: onlinestore
22
Status: Resolved
23
due date: 2010-12-31
24
Start Date:2010-01-01
25
Assigned to: John Smith
26
fixed version: alpha
27
estimated hours: 2.5
28
remaining hours: 1
29
done ratio: 30
30

  
31
This paragraph is before delimiter
32

  
33
On Wed, 11 Oct at 1:05 PM, Jon Smith <jsmith@somenet.foo<mailto:jsmith@somenet.foo>> wrote:
34

  
35
This paragraph is after the delimiter
test/unit/mail_handler_test.rb
977 977
    end
978 978
  end
979 979

  
980
  test "truncate emails using a regex delimiter" do
981
    delimiter = "On .*, .* at .*, .* <.*<mailto:.*>> wrote:"
982
    with_settings :mail_handler_enable_regex_delimiters => '1', :mail_handler_body_delimiters => delimiter do
983
      issue = submit_email('ticket_reply_from_mail.eml')
984
      assert_issue_created(issue)
985
      assert issue.description.include?('This paragraph is before delimiter')
986
      assert !issue.description.include?('On Wed, 11 Oct at 1:05 PM, Jon Smith <jsmith@somenet.foo<mailto:jsmith@somenet.foo>> wrote:')
987
      assert !issue.description.include?('This paragraph is after the delimiter')
988
    end
989

  
990
    with_settings :mail_handler_enable_regex_delimiters => '0', :mail_handler_body_delimiters => delimiter do
991
      issue = submit_email('ticket_reply_from_mail.eml')
992
      assert_issue_created(issue)
993
      assert issue.description.include?('This paragraph is before delimiter')
994
      assert issue.description.include?('On Wed, 11 Oct at 1:05 PM, Jon Smith <jsmith@somenet.foo<mailto:jsmith@somenet.foo>> wrote:')
995
      assert issue.description.include?('This paragraph is after the delimiter')
996
    end
997
  end
998

  
980 999
  def test_attachments_that_match_mail_handler_excluded_filenames_should_be_ignored
981 1000
    with_settings :mail_handler_excluded_filenames => '*.vcf, *.jpg' do
982 1001
      issue = submit_email('ticket_with_attachment.eml', :issue => {:project => 'onlinestore'})
(5-5/7)