Project

General

Profile

Patch #4409 ยป 0001-Remove-the-body-of-incoming-emails-after-specific-de.patch

Eric Davis, 2009-12-16 00:20

View differences:

app/models/mail_handler.rb
MESSAGE_ID_RE = %r{^<redmine\.([a-z0-9_]+)\-(\d+)\.\d+@}
ISSUE_REPLY_SUBJECT_RE = %r{\[[^\]]*#(\d+)\]}
MESSAGE_REPLY_SUBJECT_RE = %r{\[[^\]]*msg(\d+)\]}
def dispatch
headers = [email.in_reply_to, email.references].flatten.compact
if headers.detect {|h| h.to_s =~ MESSAGE_ID_RE}
......
if issue.subject.blank?
issue.subject = '(no subject)'
end
issue.description = plain_text_body
# custom fields
issue.custom_field_values = issue.available_custom_fields.inject({}) do |h, c|
if value = get_keyword(c.name, :override => true)
......
end
h
end
issue.description = cleanup_body(plain_text_body)
# add To and Cc as watchers before saving so the watchers can reply to Redmine
add_watchers(issue)
issue.save!
......
raise UnauthorizedAction unless status.nil? || user.allowed_to?(:edit_issues, issue.project)
# add the note
journal = issue.init_journal(user, plain_text_body)
journal = issue.init_journal(user, cleanup_body(plain_text_body))
add_attachments(issue)
# check workflow
if status && issue.new_statuses_allowed_to(user).include?(status)
......
end
end
end
# Returns the text/plain part of the email
# If not found (eg. HTML-only email), returns the body with tags removed
def plain_text_body
......
user.save ? user : nil
end
end
private
# Removes the email body of text after the truncation configurations.
def cleanup_body(body)
if Setting.emails_truncate.present?
truncate_tokens = Setting.emails_truncate.split("\n")
truncate_tokens.each do |token|
next if token.blank?
regex = Regexp.new(token + '.*', Regexp::IGNORECASE | Regexp::MULTILINE)
body.gsub!(regex, '')
end
end
return body.chomp
end
end
app/views/settings/_mail_handler.rhtml
<% form_tag({:action => 'edit', :tab => 'mail_handler'}) do %>
<div class="box tabular settings">
<p>
<label><%= l(:setting_emails_truncate) %></label>
<%= text_area_tag 'settings[emails_truncate]', Setting.emails_truncate, :rows => 5, :cols => 80 %>
<br /><em><%= l(:text_line_separated) %></em>
</p>
</div>
<div class="box tabular settings">
<p><label><%= l(:setting_mail_handler_api_enabled) %></label>
<%= hidden_field_tag 'settings[mail_handler_api_enabled]', 0 %>
<%= check_box_tag 'settings[mail_handler_api_enabled]', 1, Setting.mail_handler_api_enabled?,
......
</div>
<%= submit_tag l(:button_save) %>
<% end %>
config/locales/en.yml
setting_issue_done_ratio_issue_field: Use the issue field
setting_issue_done_ratio_issue_status: Use the issue status
setting_start_of_week: Start calendars on
setting_emails_truncate: "Truncate emails after the following strings"
permission_add_project: Create project
permission_edit_project: Edit project
......
text_tracker_no_workflow: No workflow defined for this tracker
text_unallowed_characters: Unallowed characters
text_comma_separated: Multiple values allowed (comma separated).
text_line_separated: Multiple values allowed (line separated).
text_issues_ref_in_commit_messages: Referencing and fixing issues in commit messages
text_issue_added: "Issue {{id}} has been reported by {{author}}."
text_issue_updated: "Issue {{id}} has been updated by {{author}}."
config/settings.yml
default: ''
start_of_week:
default: ''
emails_truncate:
default: ''
test/fixtures/mail_handler/ticket_on_given_project.eml
malesuada fames ac turpis egestas. Quisque sit amet libero. In hac habitasse
platea dictumst.
---
This paragraph is after the delimiter so it shouldn't appear.
Nulla et nunc. Duis pede. Donec et ipsum. Nam ut dui tincidunt neque
sollicitudin iaculis. Duis vitae dolor. Vestibulum eget massa. Sed lorem.
Nullam volutpat cursus erat. Cras felis dolor, lacinia quis, rutrum et,
test/fixtures/mail_handler/ticket_reply.eml
Content-Transfer-Encoding: quoted-printable
This is reply
---
This is my sig.
------=_NextPart_000_0067_01C8D3CE.711F9CC0
Content-Type: text/html;
charset="utf-8"
test/unit/mail_handler_test.rb
assert issue.is_a?(Issue)
assert_equal 1, ActionMailer::Base.deliveries.size
end
context "truncate emails based on the Setting" do
context "with no setting" do
setup do
Setting.emails_truncate = ''
end
should "add the entire email into the issue" do
issue = submit_email('ticket_on_given_project.eml')
assert_issue_created(issue)
assert issue.description.include?('---')
assert issue.description.include?('This paragraph is after the delimiter')
end
end
context "with a single string" do
setup do
Setting.emails_truncate = '---'
end
should "truncate the email at the delimiter for the issue" do
issue = submit_email('ticket_on_given_project.eml')
assert_issue_created(issue)
assert !issue.description.include?('---')
assert !issue.description.include?('This paragraph is after the delimiter')
assert issue.description.include?('platea dictumst.')
end
end
context "with multiple strings" do
setup do
Setting.emails_truncate = "---\nBREAK\nid"
end
should "truncate the email at the first delimiter found (id)" do
issue = submit_email('ticket_on_given_project.eml')
assert_issue_created(issue)
assert !issue.description.include?('---')
assert !issue.description.include?('This paragraph is after the delimiter')
assert !issue.description.include?('platea dictumst.')
assert issue.description.include?('blandit eleifend augue. Nulla facilisi. Duis ')
end
end
end
def test_add_issue_with_strip_delimiter_should_not_have_text_after_delimiter
with_settings :emails_truncate => '---' do
# This email contains: 'Project: onlinestore'
issue = submit_email('ticket_on_given_project.eml')
assert issue.is_a?(Issue)
assert !issue.new_record?
issue.reload
assert_equal 'New ticket on a given project', issue.subject
assert !issue.description.include?("This paragraph is after the delimiter so it shouldn't appear."), "Delimited text was not stripped"
assert !issue.description.include?("---"), "Delimiter was not stripped"
end
end
def test_add_issue_with_strip_delimiter_should_not_have_delimiter
with_settings :emails_truncate => '---' do
# This email contains: 'Project: onlinestore'
issue = submit_email('ticket_on_given_project.eml')
assert issue.is_a?(Issue)
assert !issue.new_record?
issue.reload
assert_equal 'New ticket on a given project', issue.subject
assert !issue.description.include?("---"), "Delimiter was not stripped"
end
end
def test_add_issue_note
journal = submit_email('ticket_reply.eml')
assert journal.is_a?(Journal)
......
assert_equal 'This is a html-only email.', issue.description
end
def test_add_issue_note_with_strip_delimiter_should_not_have_text_after_delimiter
with_settings :emails_truncate => '---' do
journal = submit_email('ticket_reply.eml')
assert journal.is_a?(Journal)
assert !journal.notes.include?("This is my sig"), "Delimited text was not stripped"
end
end
def test_add_issue_note_with_strip_delimiter_should_not_have_delimiter
with_settings :emails_truncate => '---' do
journal = submit_email('ticket_reply.eml')
assert journal.is_a?(Journal)
assert !journal.notes.include?("---"), "Delimiter was not stripped"
end
end
private
def submit_email(filename, options={})
raw = IO.read(File.join(FIXTURES_PATH, filename))
MailHandler.receive(raw, options)
end
def assert_issue_created(issue)
assert issue.is_a?(Issue)
assert !issue.new_record?
issue.reload
end
end
    (1-1/1)