##// END OF EJS Templates
Strip keywords from received email body (#2436)....
Jean-Philippe Lang -
r2365:2d3b3cee156c
parent child
Show More
@@ -1,232 +1,239
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class MailHandler < ActionMailer::Base
19 19 include ActionView::Helpers::SanitizeHelper
20 20
21 21 class UnauthorizedAction < StandardError; end
22 22 class MissingInformation < StandardError; end
23 23
24 24 attr_reader :email, :user
25 25
26 26 def self.receive(email, options={})
27 27 @@handler_options = options.dup
28 28
29 29 @@handler_options[:issue] ||= {}
30 30
31 31 @@handler_options[:allow_override] = @@handler_options[:allow_override].split(',').collect(&:strip) if @@handler_options[:allow_override].is_a?(String)
32 32 @@handler_options[:allow_override] ||= []
33 33 # Project needs to be overridable if not specified
34 34 @@handler_options[:allow_override] << 'project' unless @@handler_options[:issue].has_key?(:project)
35 35 # Status overridable by default
36 36 @@handler_options[:allow_override] << 'status' unless @@handler_options[:issue].has_key?(:status)
37 37 super email
38 38 end
39 39
40 40 # Processes incoming emails
41 41 def receive(email)
42 42 @email = email
43 43 @user = User.active.find_by_mail(email.from.first.to_s.strip)
44 44 unless @user
45 45 # Unknown user => the email is ignored
46 46 # TODO: ability to create the user's account
47 47 logger.info "MailHandler: email submitted by unknown user [#{email.from.first}]" if logger && logger.info
48 48 return false
49 49 end
50 50 User.current = @user
51 51 dispatch
52 52 end
53 53
54 54 private
55 55
56 56 MESSAGE_ID_RE = %r{^<redmine\.([a-z0-9_]+)\-(\d+)\.\d+@}
57 57 ISSUE_REPLY_SUBJECT_RE = %r{\[[^\]]+#(\d+)\]}
58 58 MESSAGE_REPLY_SUBJECT_RE = %r{\[[^\]]+msg(\d+)\]}
59 59
60 60 def dispatch
61 61 headers = [email.in_reply_to, email.references].flatten.compact
62 62 if headers.detect {|h| h.to_s =~ MESSAGE_ID_RE}
63 63 klass, object_id = $1, $2.to_i
64 64 method_name = "receive_#{klass}_reply"
65 65 if self.class.private_instance_methods.include?(method_name)
66 66 send method_name, object_id
67 67 else
68 68 # ignoring it
69 69 end
70 70 elsif m = email.subject.match(ISSUE_REPLY_SUBJECT_RE)
71 71 receive_issue_reply(m[1].to_i)
72 72 elsif m = email.subject.match(MESSAGE_REPLY_SUBJECT_RE)
73 73 receive_message_reply(m[1].to_i)
74 74 else
75 75 receive_issue
76 76 end
77 77 rescue ActiveRecord::RecordInvalid => e
78 78 # TODO: send a email to the user
79 79 logger.error e.message if logger
80 80 false
81 81 rescue MissingInformation => e
82 82 logger.error "MailHandler: missing information from #{user}: #{e.message}" if logger
83 83 false
84 84 rescue UnauthorizedAction => e
85 85 logger.error "MailHandler: unauthorized attempt from #{user}" if logger
86 86 false
87 87 end
88 88
89 89 # Creates a new issue
90 90 def receive_issue
91 91 project = target_project
92 92 tracker = (get_keyword(:tracker) && project.trackers.find_by_name(get_keyword(:tracker))) || project.trackers.find(:first)
93 93 category = (get_keyword(:category) && project.issue_categories.find_by_name(get_keyword(:category)))
94 94 priority = (get_keyword(:priority) && Enumeration.find_by_opt_and_name('IPRI', get_keyword(:priority)))
95 95 status = (get_keyword(:status) && IssueStatus.find_by_name(get_keyword(:status)))
96 96
97 97 # check permission
98 98 raise UnauthorizedAction unless user.allowed_to?(:add_issues, project)
99 99 issue = Issue.new(:author => user, :project => project, :tracker => tracker, :category => category, :priority => priority)
100 100 # check workflow
101 101 if status && issue.new_statuses_allowed_to(user).include?(status)
102 102 issue.status = status
103 103 end
104 104 issue.subject = email.subject.chomp.toutf8
105 105 issue.description = plain_text_body
106 106 # custom fields
107 107 issue.custom_field_values = issue.available_custom_fields.inject({}) do |h, c|
108 108 if value = get_keyword(c.name, :override => true)
109 109 h[c.id] = value
110 110 end
111 111 h
112 112 end
113 113 issue.save!
114 114 add_attachments(issue)
115 115 logger.info "MailHandler: issue ##{issue.id} created by #{user}" if logger && logger.info
116 116 # add To and Cc as watchers
117 117 add_watchers(issue)
118 118 # send notification after adding watchers so that they can reply to Redmine
119 119 Mailer.deliver_issue_add(issue) if Setting.notified_events.include?('issue_added')
120 120 issue
121 121 end
122 122
123 123 def target_project
124 124 # TODO: other ways to specify project:
125 125 # * parse the email To field
126 126 # * specific project (eg. Setting.mail_handler_target_project)
127 127 target = Project.find_by_identifier(get_keyword(:project))
128 128 raise MissingInformation.new('Unable to determine target project') if target.nil?
129 129 target
130 130 end
131 131
132 132 # Adds a note to an existing issue
133 133 def receive_issue_reply(issue_id)
134 134 status = (get_keyword(:status) && IssueStatus.find_by_name(get_keyword(:status)))
135 135
136 136 issue = Issue.find_by_id(issue_id)
137 137 return unless issue
138 138 # check permission
139 139 raise UnauthorizedAction unless user.allowed_to?(:add_issue_notes, issue.project) || user.allowed_to?(:edit_issues, issue.project)
140 140 raise UnauthorizedAction unless status.nil? || user.allowed_to?(:edit_issues, issue.project)
141 141
142 142 # add the note
143 143 journal = issue.init_journal(user, plain_text_body)
144 144 add_attachments(issue)
145 145 # check workflow
146 146 if status && issue.new_statuses_allowed_to(user).include?(status)
147 147 issue.status = status
148 148 end
149 149 issue.save!
150 150 logger.info "MailHandler: issue ##{issue.id} updated by #{user}" if logger && logger.info
151 151 Mailer.deliver_issue_edit(journal) if Setting.notified_events.include?('issue_updated')
152 152 journal
153 153 end
154 154
155 155 # Reply will be added to the issue
156 156 def receive_journal_reply(journal_id)
157 157 journal = Journal.find_by_id(journal_id)
158 158 if journal && journal.journalized_type == 'Issue'
159 159 receive_issue_reply(journal.journalized_id)
160 160 end
161 161 end
162 162
163 163 # Receives a reply to a forum message
164 164 def receive_message_reply(message_id)
165 165 message = Message.find_by_id(message_id)
166 166 if message
167 167 message = message.root
168 168 if user.allowed_to?(:add_messages, message.project) && !message.locked?
169 169 reply = Message.new(:subject => email.subject.gsub(%r{^.*msg\d+\]}, '').strip,
170 170 :content => plain_text_body)
171 171 reply.author = user
172 172 reply.board = message.board
173 173 message.children << reply
174 174 add_attachments(reply)
175 175 reply
176 176 else
177 177 raise UnauthorizedAction
178 178 end
179 179 end
180 180 end
181 181
182 182 def add_attachments(obj)
183 183 if email.has_attachments?
184 184 email.attachments.each do |attachment|
185 185 Attachment.create(:container => obj,
186 186 :file => attachment,
187 187 :author => user,
188 188 :content_type => attachment.content_type)
189 189 end
190 190 end
191 191 end
192 192
193 193 # Adds To and Cc as watchers of the given object if the sender has the
194 194 # appropriate permission
195 195 def add_watchers(obj)
196 196 if user.allowed_to?("add_#{obj.class.name.underscore}_watchers".to_sym, obj.project)
197 197 addresses = [email.to, email.cc].flatten.compact.uniq.collect {|a| a.strip.downcase}
198 198 unless addresses.empty?
199 199 watchers = User.active.find(:all, :conditions => ['LOWER(mail) IN (?)', addresses])
200 200 watchers.each {|w| obj.add_watcher(w)}
201 201 end
202 202 end
203 203 end
204 204
205 205 def get_keyword(attr, options={})
206 if (options[:override] || @@handler_options[:allow_override].include?(attr.to_s)) && plain_text_body =~ /^#{attr}:[ \t]*(.+)$/i
207 $1.strip
208 elsif !@@handler_options[:issue][attr].blank?
209 @@handler_options[:issue][attr]
206 @keywords ||= {}
207 if @keywords.has_key?(attr)
208 @keywords[attr]
209 else
210 @keywords[attr] = begin
211 if (options[:override] || @@handler_options[:allow_override].include?(attr.to_s)) && plain_text_body.gsub!(/^#{attr}:[ \t]*(.+)\s*$/i, '')
212 $1.strip
213 elsif !@@handler_options[:issue][attr].blank?
214 @@handler_options[:issue][attr]
215 end
216 end
210 217 end
211 218 end
212 219
213 220 # Returns the text/plain part of the email
214 221 # If not found (eg. HTML-only email), returns the body with tags removed
215 222 def plain_text_body
216 223 return @plain_text_body unless @plain_text_body.nil?
217 224 parts = @email.parts.collect {|c| (c.respond_to?(:parts) && !c.parts.empty?) ? c.parts : c}.flatten
218 225 if parts.empty?
219 226 parts << @email
220 227 end
221 228 plain_text_part = parts.detect {|p| p.content_type == 'text/plain'}
222 229 if plain_text_part.nil?
223 230 # no text/plain part found, assuming html-only email
224 231 # strip html tags and remove doctype directive
225 232 @plain_text_body = strip_tags(@email.body.to_s)
226 233 @plain_text_body.gsub! %r{^<!DOCTYPE .*$}, ''
227 234 else
228 235 @plain_text_body = plain_text_part.body.to_s
229 236 end
230 237 @plain_text_body.strip!
231 238 end
232 239 end
@@ -1,180 +1,185
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.dirname(__FILE__) + '/../test_helper'
19 19
20 20 class MailHandlerTest < Test::Unit::TestCase
21 21 fixtures :users, :projects,
22 22 :enabled_modules,
23 23 :roles,
24 24 :members,
25 25 :issues,
26 26 :issue_statuses,
27 27 :workflows,
28 28 :trackers,
29 29 :projects_trackers,
30 30 :enumerations,
31 31 :issue_categories,
32 32 :custom_fields,
33 33 :custom_fields_trackers,
34 34 :boards,
35 35 :messages
36 36
37 37 FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures/mail_handler'
38 38
39 39 def setup
40 40 ActionMailer::Base.deliveries.clear
41 41 end
42 42
43 43 def test_add_issue
44 44 # This email contains: 'Project: onlinestore'
45 45 issue = submit_email('ticket_on_given_project.eml')
46 46 assert issue.is_a?(Issue)
47 47 assert !issue.new_record?
48 48 issue.reload
49 49 assert_equal 'New ticket on a given project', issue.subject
50 50 assert_equal User.find_by_login('jsmith'), issue.author
51 51 assert_equal Project.find(2), issue.project
52 assert_equal IssueStatus.find_by_name('Resolved'), issue.status
52 53 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
54 # keywords should be removed from the email body
55 assert !issue.description.match(/^Project:/i)
56 assert !issue.description.match(/^Status:/i)
53 57 end
54 58
55 59 def test_add_issue_with_status
56 60 # This email contains: 'Project: onlinestore' and 'Status: Resolved'
57 61 issue = submit_email('ticket_on_given_project.eml')
58 62 assert issue.is_a?(Issue)
59 63 assert !issue.new_record?
60 64 issue.reload
61 65 assert_equal Project.find(2), issue.project
62 66 assert_equal IssueStatus.find_by_name("Resolved"), issue.status
63 67 end
64 68
65 69 def test_add_issue_with_attributes_override
66 70 issue = submit_email('ticket_with_attributes.eml', :allow_override => 'tracker,category,priority')
67 71 assert issue.is_a?(Issue)
68 72 assert !issue.new_record?
69 73 issue.reload
70 74 assert_equal 'New ticket on a given project', issue.subject
71 75 assert_equal User.find_by_login('jsmith'), issue.author
72 76 assert_equal Project.find(2), issue.project
73 77 assert_equal 'Feature request', issue.tracker.to_s
74 78 assert_equal 'Stock management', issue.category.to_s
75 79 assert_equal 'Urgent', issue.priority.to_s
76 80 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
77 81 end
78 82
79 83 def test_add_issue_with_partial_attributes_override
80 84 issue = submit_email('ticket_with_attributes.eml', :issue => {:priority => 'High'}, :allow_override => ['tracker'])
81 85 assert issue.is_a?(Issue)
82 86 assert !issue.new_record?
83 87 issue.reload
84 88 assert_equal 'New ticket on a given project', issue.subject
85 89 assert_equal User.find_by_login('jsmith'), issue.author
86 90 assert_equal Project.find(2), issue.project
87 91 assert_equal 'Feature request', issue.tracker.to_s
88 92 assert_nil issue.category
89 93 assert_equal 'High', issue.priority.to_s
90 94 assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.')
91 95 end
92 96
93 97 def test_add_issue_with_attachment_to_specific_project
94 98 issue = submit_email('ticket_with_attachment.eml', :issue => {:project => 'onlinestore'})
95 99 assert issue.is_a?(Issue)
96 100 assert !issue.new_record?
97 101 issue.reload
98 102 assert_equal 'Ticket created by email with attachment', issue.subject
99 103 assert_equal User.find_by_login('jsmith'), issue.author
100 104 assert_equal Project.find(2), issue.project
101 105 assert_equal 'This is a new ticket with attachments', issue.description
102 106 # Attachment properties
103 107 assert_equal 1, issue.attachments.size
104 108 assert_equal 'Paella.jpg', issue.attachments.first.filename
105 109 assert_equal 'image/jpeg', issue.attachments.first.content_type
106 110 assert_equal 10790, issue.attachments.first.filesize
107 111 end
108 112
109 113 def test_add_issue_with_custom_fields
110 114 issue = submit_email('ticket_with_custom_fields.eml', :issue => {:project => 'onlinestore'})
111 115 assert issue.is_a?(Issue)
112 116 assert !issue.new_record?
113 117 issue.reload
114 118 assert_equal 'New ticket with custom field values', issue.subject
115 119 assert_equal 'Value for a custom field', issue.custom_value_for(CustomField.find_by_name('Searchable field')).value
120 assert !issue.description.match(/^searchable field:/i)
116 121 end
117 122
118 123 def test_add_issue_with_cc
119 124 issue = submit_email('ticket_with_cc.eml', :issue => {:project => 'ecookbook'})
120 125 assert issue.is_a?(Issue)
121 126 assert !issue.new_record?
122 127 issue.reload
123 128 assert issue.watched_by?(User.find_by_mail('dlopper@somenet.foo'))
124 129 assert_equal 1, issue.watchers.size
125 130 end
126 131
127 132 def test_add_issue_note
128 133 journal = submit_email('ticket_reply.eml')
129 134 assert journal.is_a?(Journal)
130 135 assert_equal User.find_by_login('jsmith'), journal.user
131 136 assert_equal Issue.find(2), journal.journalized
132 137 assert_match /This is reply/, journal.notes
133 138 end
134 139
135 140 def test_add_issue_note_with_status_change
136 141 # This email contains: 'Status: Resolved'
137 142 journal = submit_email('ticket_reply_with_status.eml')
138 143 assert journal.is_a?(Journal)
139 144 issue = Issue.find(journal.issue.id)
140 145 assert_equal User.find_by_login('jsmith'), journal.user
141 146 assert_equal Issue.find(2), journal.journalized
142 147 assert_match /This is reply/, journal.notes
143 148 assert_equal IssueStatus.find_by_name("Resolved"), issue.status
144 149 end
145 150
146 151 def test_reply_to_a_message
147 152 m = submit_email('message_reply.eml')
148 153 assert m.is_a?(Message)
149 154 assert !m.new_record?
150 155 m.reload
151 156 assert_equal 'Reply via email', m.subject
152 157 # The email replies to message #2 which is part of the thread of message #1
153 158 assert_equal Message.find(1), m.parent
154 159 end
155 160
156 161 def test_reply_to_a_message_by_subject
157 162 m = submit_email('message_reply_by_subject.eml')
158 163 assert m.is_a?(Message)
159 164 assert !m.new_record?
160 165 m.reload
161 166 assert_equal 'Reply to the first post', m.subject
162 167 assert_equal Message.find(1), m.parent
163 168 end
164 169
165 170 def test_should_strip_tags_of_html_only_emails
166 171 issue = submit_email('ticket_html_only.eml', :issue => {:project => 'ecookbook'})
167 172 assert issue.is_a?(Issue)
168 173 assert !issue.new_record?
169 174 issue.reload
170 175 assert_equal 'HTML email', issue.subject
171 176 assert_equal 'This is a html-only email.', issue.description
172 177 end
173 178
174 179 private
175 180
176 181 def submit_email(filename, options={})
177 182 raw = IO.read(File.join(FIXTURES_PATH, filename))
178 183 MailHandler.receive(raw, options)
179 184 end
180 185 end
General Comments 0
You need to be logged in to leave comments. Login now