Project

General

Profile

Feature #5864 » allow_regex_delimiters_v3.patch

Marius BĂLTEANU, 2016-12-11 18:23

View differences:

app/controllers/settings_controller.rb
33 33

  
34 34
  def edit
35 35
    @notifiables = Redmine::Notifiable.all
36
    valid = true
36 37
    if request.post?
37
      if Setting.set_all_from_params(params[:settings])
38
      if params[:settings][:mail_handler_enable_regex_delimiters] == '1'
39
        invalid_delimiters = Setting.validate_regex_delimiters params[:settings][:mail_handler_body_delimiters]
40
        if invalid_delimiters.count > 0
41
          valid = false
42
          flash[:error] = invalid_delimiters.join('\n')
43
        end
44
      end
45
      if valid && Setting.set_all_from_params(params[:settings])
38 46
        flash[:notice] = l(:notice_successful_update)
39 47
      end
40 48
      redirect_to settings_path(:tab => params[:tab])
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/models/setting.rb
259 259
  load_available_settings
260 260
  load_plugin_settings
261 261

  
262
  def self.validate_regex_delimiters(delimiters)
263
    invalid_delimiters = []
264
    delimiters.to_s.split(/[\r\n]+/).reject(&:blank?).each do |regex|
265
      begin
266
        Regexp.new(regex)
267
      rescue => exception
268
        invalid_delimiters << l(:error_invalid_regex_delimiter, :delimiter => regex)
269
      end
270
    end
271
    invalid_delimiters
272
  end
262 273
private
263 274

  
264 275
  def force_utf8_strings(arg)
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
215 215
  error_ldap_bind_credentials: "Invalid LDAP Account/Password"
216 216
  error_no_tracker_allowed_for_new_issue_in_project: "The project doesn't have any trackers for which you can create an issue"
217 217
  error_no_projects_with_tracker_allowed_for_new_issue: "There are no projects with trackers for which you can create an issue"
218
  error_invalid_regex_delimiter: "Delimiter \"%{delimiter}\" is not a valid regex"
218 219

  
219 220
  mail_subject_lost_password: "Your %{value} password"
220 221
  mail_body_lost_password: 'To change your password, click on the following link:'
......
448 449
  setting_attachment_extensions_allowed: Allowed extensions
449 450
  setting_attachment_extensions_denied: Disallowed extensions
450 451
  setting_new_item_menu_tab: Project menu tab for creating new objects
452
  setting_mail_handler_enable_regex_delimiters: Enable regex delimiters
451 453

  
452 454
  permission_add_project: Create project
453 455
  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/functional/settings_controller_test.rb
254 254
  ensure
255 255
    Redmine::Plugin.unregister(:foo)
256 256
  end
257

  
258
  def test_post_mail_handler_delimiters_should_not_save_invalid_regex_delimiters
259
    post :edit, :params => {
260
      :settings => {
261
        :mail_handler_enable_regex_delimiters => '1',
262
        :mail_handler_body_delimiters  => 'Abc[',
263
      }
264
    }
265

  
266
    assert_redirected_to '/settings'
267
    assert_equal '0', Setting.mail_handler_enable_regex_delimiters
268
    assert_equal '', Setting.mail_handler_body_delimiters
269

  
270
  end
271

  
272
  def test_post_mail_handler_delimiters_should_save_valid_regex_delimiters
273
    post :edit, :params => {
274
      :settings => {
275
        :mail_handler_enable_regex_delimiters => '1',
276
        :mail_handler_body_delimiters  => 'On .*, .* at .*, .* <.*<mailto:.*>> wrote:',
277
      }
278
    }
279

  
280
    assert_redirected_to '/settings'
281
    assert_equal '1', Setting.mail_handler_enable_regex_delimiters
282
    assert_equal 'On .*, .* at .*, .* <.*<mailto:.*>> wrote:', Setting.mail_handler_body_delimiters
283

  
284
  end
257 285
end
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'})
test/unit/setting_test.rb
110 110

  
111 111
  def test_setting_serialied_as_binary_should_be_loaded_as_utf8_encoded_strings
112 112
    yaml = <<-YAML
113
--- 
113
---
114 114
- keywords: !binary |
115 115
    Zml4ZXMsY2xvc2VzLNC40YHQv9GA0LDQstC70LXQvdC+LNCz0L7RgtC+0LLQ
116 116
    vizRgdC00LXQu9Cw0L3QvixmaXhlZA==
(6-6/7)