##// END OF EJS Templates
Rails3: replace deprecated Errors#on to Errors#[] at app/models/mail_handler.rb...
Toshi MARUYAMA -
r8400:f01dc248772e
parent child
Show More
@@ -1,401 +1,401
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 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 include Redmine::I18n
21 21
22 22 class UnauthorizedAction < StandardError; end
23 23 class MissingInformation < StandardError; end
24 24
25 25 attr_reader :email, :user
26 26
27 27 def self.receive(email, options={})
28 28 @@handler_options = options.dup
29 29
30 30 @@handler_options[:issue] ||= {}
31 31
32 32 @@handler_options[:allow_override] = @@handler_options[:allow_override].split(',').collect(&:strip) if @@handler_options[:allow_override].is_a?(String)
33 33 @@handler_options[:allow_override] ||= []
34 34 # Project needs to be overridable if not specified
35 35 @@handler_options[:allow_override] << 'project' unless @@handler_options[:issue].has_key?(:project)
36 36 # Status overridable by default
37 37 @@handler_options[:allow_override] << 'status' unless @@handler_options[:issue].has_key?(:status)
38 38
39 39 @@handler_options[:no_permission_check] = (@@handler_options[:no_permission_check].to_s == '1' ? true : false)
40 40 super email
41 41 end
42 42
43 43 # Processes incoming emails
44 44 # Returns the created object (eg. an issue, a message) or false
45 45 def receive(email)
46 46 @email = email
47 47 sender_email = email.from.to_a.first.to_s.strip
48 48 # Ignore emails received from the application emission address to avoid hell cycles
49 49 if sender_email.downcase == Setting.mail_from.to_s.strip.downcase
50 50 logger.info "MailHandler: ignoring email from Redmine emission address [#{sender_email}]" if logger && logger.info
51 51 return false
52 52 end
53 53 @user = User.find_by_mail(sender_email) if sender_email.present?
54 54 if @user && !@user.active?
55 55 logger.info "MailHandler: ignoring email from non-active user [#{@user.login}]" if logger && logger.info
56 56 return false
57 57 end
58 58 if @user.nil?
59 59 # Email was submitted by an unknown user
60 60 case @@handler_options[:unknown_user]
61 61 when 'accept'
62 62 @user = User.anonymous
63 63 when 'create'
64 64 @user = create_user_from_email(email)
65 65 if @user
66 66 logger.info "MailHandler: [#{@user.login}] account created" if logger && logger.info
67 67 Mailer.deliver_account_information(@user, @user.password)
68 68 else
69 69 logger.error "MailHandler: could not create account for [#{sender_email}]" if logger && logger.error
70 70 return false
71 71 end
72 72 else
73 73 # Default behaviour, emails from unknown users are ignored
74 74 logger.info "MailHandler: ignoring email from unknown user [#{sender_email}]" if logger && logger.info
75 75 return false
76 76 end
77 77 end
78 78 User.current = @user
79 79 dispatch
80 80 end
81 81
82 82 private
83 83
84 84 MESSAGE_ID_RE = %r{^<redmine\.([a-z0-9_]+)\-(\d+)\.\d+@}
85 85 ISSUE_REPLY_SUBJECT_RE = %r{\[[^\]]*#(\d+)\]}
86 86 MESSAGE_REPLY_SUBJECT_RE = %r{\[[^\]]*msg(\d+)\]}
87 87
88 88 def dispatch
89 89 headers = [email.in_reply_to, email.references].flatten.compact
90 90 if headers.detect {|h| h.to_s =~ MESSAGE_ID_RE}
91 91 klass, object_id = $1, $2.to_i
92 92 method_name = "receive_#{klass}_reply"
93 93 if self.class.private_instance_methods.collect(&:to_s).include?(method_name)
94 94 send method_name, object_id
95 95 else
96 96 # ignoring it
97 97 end
98 98 elsif m = email.subject.match(ISSUE_REPLY_SUBJECT_RE)
99 99 receive_issue_reply(m[1].to_i)
100 100 elsif m = email.subject.match(MESSAGE_REPLY_SUBJECT_RE)
101 101 receive_message_reply(m[1].to_i)
102 102 else
103 103 dispatch_to_default
104 104 end
105 105 rescue ActiveRecord::RecordInvalid => e
106 106 # TODO: send a email to the user
107 107 logger.error e.message if logger
108 108 false
109 109 rescue MissingInformation => e
110 110 logger.error "MailHandler: missing information from #{user}: #{e.message}" if logger
111 111 false
112 112 rescue UnauthorizedAction => e
113 113 logger.error "MailHandler: unauthorized attempt from #{user}" if logger
114 114 false
115 115 end
116 116
117 117 def dispatch_to_default
118 118 receive_issue
119 119 end
120 120
121 121 # Creates a new issue
122 122 def receive_issue
123 123 project = target_project
124 124 # check permission
125 125 unless @@handler_options[:no_permission_check]
126 126 raise UnauthorizedAction unless user.allowed_to?(:add_issues, project)
127 127 end
128 128
129 129 issue = Issue.new(:author => user, :project => project)
130 130 issue.safe_attributes = issue_attributes_from_keywords(issue)
131 131 issue.safe_attributes = {'custom_field_values' => custom_field_values_from_keywords(issue)}
132 132 issue.subject = email.subject.to_s.chomp[0,255]
133 133 if issue.subject.blank?
134 134 issue.subject = '(no subject)'
135 135 end
136 136 issue.description = cleaned_up_text_body
137 137
138 138 # add To and Cc as watchers before saving so the watchers can reply to Redmine
139 139 add_watchers(issue)
140 140 issue.save!
141 141 add_attachments(issue)
142 142 logger.info "MailHandler: issue ##{issue.id} created by #{user}" if logger && logger.info
143 143 issue
144 144 end
145 145
146 146 # Adds a note to an existing issue
147 147 def receive_issue_reply(issue_id)
148 148 issue = Issue.find_by_id(issue_id)
149 149 return unless issue
150 150 # check permission
151 151 unless @@handler_options[:no_permission_check]
152 152 raise UnauthorizedAction unless user.allowed_to?(:add_issue_notes, issue.project) || user.allowed_to?(:edit_issues, issue.project)
153 153 end
154 154
155 155 # ignore CLI-supplied defaults for new issues
156 156 @@handler_options[:issue].clear
157 157
158 158 journal = issue.init_journal(user)
159 159 issue.safe_attributes = issue_attributes_from_keywords(issue)
160 160 issue.safe_attributes = {'custom_field_values' => custom_field_values_from_keywords(issue)}
161 161 journal.notes = cleaned_up_text_body
162 162 add_attachments(issue)
163 163 issue.save!
164 164 logger.info "MailHandler: issue ##{issue.id} updated by #{user}" if logger && logger.info
165 165 journal
166 166 end
167 167
168 168 # Reply will be added to the issue
169 169 def receive_journal_reply(journal_id)
170 170 journal = Journal.find_by_id(journal_id)
171 171 if journal && journal.journalized_type == 'Issue'
172 172 receive_issue_reply(journal.journalized_id)
173 173 end
174 174 end
175 175
176 176 # Receives a reply to a forum message
177 177 def receive_message_reply(message_id)
178 178 message = Message.find_by_id(message_id)
179 179 if message
180 180 message = message.root
181 181
182 182 unless @@handler_options[:no_permission_check]
183 183 raise UnauthorizedAction unless user.allowed_to?(:add_messages, message.project)
184 184 end
185 185
186 186 if !message.locked?
187 187 reply = Message.new(:subject => email.subject.gsub(%r{^.*msg\d+\]}, '').strip,
188 188 :content => cleaned_up_text_body)
189 189 reply.author = user
190 190 reply.board = message.board
191 191 message.children << reply
192 192 add_attachments(reply)
193 193 reply
194 194 else
195 195 logger.info "MailHandler: ignoring reply from [#{sender_email}] to a locked topic" if logger && logger.info
196 196 end
197 197 end
198 198 end
199 199
200 200 def add_attachments(obj)
201 201 if email.attachments && email.attachments.any?
202 202 email.attachments.each do |attachment|
203 203 obj.attachments << Attachment.create(:container => obj,
204 204 :file => attachment,
205 205 :author => user,
206 206 :content_type => attachment.content_type)
207 207 end
208 208 end
209 209 end
210 210
211 211 # Adds To and Cc as watchers of the given object if the sender has the
212 212 # appropriate permission
213 213 def add_watchers(obj)
214 214 if user.allowed_to?("add_#{obj.class.name.underscore}_watchers".to_sym, obj.project)
215 215 addresses = [email.to, email.cc].flatten.compact.uniq.collect {|a| a.strip.downcase}
216 216 unless addresses.empty?
217 217 watchers = User.active.find(:all, :conditions => ['LOWER(mail) IN (?)', addresses])
218 218 watchers.each {|w| obj.add_watcher(w)}
219 219 end
220 220 end
221 221 end
222 222
223 223 def get_keyword(attr, options={})
224 224 @keywords ||= {}
225 225 if @keywords.has_key?(attr)
226 226 @keywords[attr]
227 227 else
228 228 @keywords[attr] = begin
229 229 if (options[:override] || @@handler_options[:allow_override].include?(attr.to_s)) && (v = extract_keyword!(plain_text_body, attr, options[:format]))
230 230 v
231 231 elsif !@@handler_options[:issue][attr].blank?
232 232 @@handler_options[:issue][attr]
233 233 end
234 234 end
235 235 end
236 236 end
237 237
238 238 # Destructively extracts the value for +attr+ in +text+
239 239 # Returns nil if no matching keyword found
240 240 def extract_keyword!(text, attr, format=nil)
241 241 keys = [attr.to_s.humanize]
242 242 if attr.is_a?(Symbol)
243 243 keys << l("field_#{attr}", :default => '', :locale => user.language) if user && user.language.present?
244 244 keys << l("field_#{attr}", :default => '', :locale => Setting.default_language) if Setting.default_language.present?
245 245 end
246 246 keys.reject! {|k| k.blank?}
247 247 keys.collect! {|k| Regexp.escape(k)}
248 248 format ||= '.+'
249 249 text.gsub!(/^(#{keys.join('|')})[ \t]*:[ \t]*(#{format})\s*$/i, '')
250 250 $2 && $2.strip
251 251 end
252 252
253 253 def target_project
254 254 # TODO: other ways to specify project:
255 255 # * parse the email To field
256 256 # * specific project (eg. Setting.mail_handler_target_project)
257 257 target = Project.find_by_identifier(get_keyword(:project))
258 258 raise MissingInformation.new('Unable to determine target project') if target.nil?
259 259 target
260 260 end
261 261
262 262 # Returns a Hash of issue attributes extracted from keywords in the email body
263 263 def issue_attributes_from_keywords(issue)
264 264 assigned_to = (k = get_keyword(:assigned_to, :override => true)) && find_assignee_from_keyword(k, issue)
265 265
266 266 attrs = {
267 267 'tracker_id' => (k = get_keyword(:tracker)) && issue.project.trackers.named(k).first.try(:id),
268 268 'status_id' => (k = get_keyword(:status)) && IssueStatus.named(k).first.try(:id),
269 269 'priority_id' => (k = get_keyword(:priority)) && IssuePriority.named(k).first.try(:id),
270 270 'category_id' => (k = get_keyword(:category)) && issue.project.issue_categories.named(k).first.try(:id),
271 271 'assigned_to_id' => assigned_to.try(:id),
272 272 'fixed_version_id' => (k = get_keyword(:fixed_version, :override => true)) && issue.project.shared_versions.named(k).first.try(:id),
273 273 'start_date' => get_keyword(:start_date, :override => true, :format => '\d{4}-\d{2}-\d{2}'),
274 274 'due_date' => get_keyword(:due_date, :override => true, :format => '\d{4}-\d{2}-\d{2}'),
275 275 'estimated_hours' => get_keyword(:estimated_hours, :override => true),
276 276 'done_ratio' => get_keyword(:done_ratio, :override => true, :format => '(\d|10)?0')
277 277 }.delete_if {|k, v| v.blank? }
278 278
279 279 if issue.new_record? && attrs['tracker_id'].nil?
280 280 attrs['tracker_id'] = issue.project.trackers.find(:first).try(:id)
281 281 end
282 282
283 283 attrs
284 284 end
285 285
286 286 # Returns a Hash of issue custom field values extracted from keywords in the email body
287 287 def custom_field_values_from_keywords(customized)
288 288 customized.custom_field_values.inject({}) do |h, v|
289 289 if value = get_keyword(v.custom_field.name, :override => true)
290 290 h[v.custom_field.id.to_s] = value
291 291 end
292 292 h
293 293 end
294 294 end
295 295
296 296 # Returns the text/plain part of the email
297 297 # If not found (eg. HTML-only email), returns the body with tags removed
298 298 def plain_text_body
299 299 return @plain_text_body unless @plain_text_body.nil?
300 300 parts = @email.parts.collect {|c| (c.respond_to?(:parts) && !c.parts.empty?) ? c.parts : c}.flatten
301 301 if parts.empty?
302 302 parts << @email
303 303 end
304 304 plain_text_part = parts.detect {|p| p.content_type == 'text/plain'}
305 305 if plain_text_part.nil?
306 306 # no text/plain part found, assuming html-only email
307 307 # strip html tags and remove doctype directive
308 308 @plain_text_body = strip_tags(@email.body.to_s)
309 309 @plain_text_body.gsub! %r{^<!DOCTYPE .*$}, ''
310 310 else
311 311 @plain_text_body = plain_text_part.body.to_s
312 312 end
313 313 @plain_text_body.strip!
314 314 @plain_text_body
315 315 end
316 316
317 317 def cleaned_up_text_body
318 318 cleanup_body(plain_text_body)
319 319 end
320 320
321 321 def self.full_sanitizer
322 322 @full_sanitizer ||= HTML::FullSanitizer.new
323 323 end
324 324
325 325 def self.assign_string_attribute_with_limit(object, attribute, value)
326 326 limit = object.class.columns_hash[attribute.to_s].limit || 255
327 327 value = value.to_s.slice(0, limit)
328 328 object.send("#{attribute}=", value)
329 329 end
330 330
331 331 # Returns a User from an email address and a full name
332 332 def self.new_user_from_attributes(email_address, fullname=nil)
333 333 user = User.new
334 334
335 335 # Truncating the email address would result in an invalid format
336 336 user.mail = email_address
337 337 assign_string_attribute_with_limit(user, 'login', email_address)
338 338
339 339 names = fullname.blank? ? email_address.gsub(/@.*$/, '').split('.') : fullname.split
340 340 assign_string_attribute_with_limit(user, 'firstname', names.shift)
341 341 assign_string_attribute_with_limit(user, 'lastname', names.join(' '))
342 342 user.lastname = '-' if user.lastname.blank?
343 343
344 344 password_length = [Setting.password_min_length.to_i, 10].max
345 345 user.password = ActiveSupport::SecureRandom.hex(password_length / 2 + 1)
346 346 user.language = Setting.default_language
347 347
348 348 unless user.valid?
349 user.login = "user#{ActiveSupport::SecureRandom.hex(6)}" if user.errors.on(:login)
350 user.firstname = "-" if user.errors.on(:firstname)
351 user.lastname = "-" if user.errors.on(:lastname)
349 user.login = "user#{ActiveSupport::SecureRandom.hex(6)}" unless user.errors[:login].blank?
350 user.firstname = "-" unless user.errors[:firstname].blank?
351 user.lastname = "-" unless user.errors[:lastname].blank?
352 352 end
353 353
354 354 user
355 355 end
356 356
357 357 # Creates a User for the +email+ sender
358 358 # Returns the user or nil if it could not be created
359 359 def create_user_from_email(email)
360 360 addr = email.from_addrs.to_a.first
361 361 if addr && !addr.spec.blank?
362 362 user = self.class.new_user_from_attributes(addr.spec, addr.name)
363 363 if user.save
364 364 user
365 365 else
366 366 logger.error "MailHandler: failed to create User: #{user.errors.full_messages}" if logger
367 367 nil
368 368 end
369 369 else
370 370 logger.error "MailHandler: failed to create User: no FROM address found" if logger
371 371 nil
372 372 end
373 373 end
374 374
375 375 private
376 376
377 377 # Removes the email body of text after the truncation configurations.
378 378 def cleanup_body(body)
379 379 delimiters = Setting.mail_handler_body_delimiters.to_s.split(/[\r\n]+/).reject(&:blank?).map {|s| Regexp.escape(s)}
380 380 unless delimiters.empty?
381 381 regex = Regexp.new("^[> ]*(#{ delimiters.join('|') })\s*[\r\n].*", Regexp::MULTILINE)
382 382 body = body.gsub(regex, '')
383 383 end
384 384 body.strip
385 385 end
386 386
387 387 def find_assignee_from_keyword(keyword, issue)
388 388 keyword = keyword.to_s.downcase
389 389 assignable = issue.assignable_users
390 390 assignee = nil
391 391 assignee ||= assignable.detect {|a| a.mail.to_s.downcase == keyword || a.login.to_s.downcase == keyword}
392 392 if assignee.nil? && keyword.match(/ /)
393 393 firstname, lastname = *(keyword.split) # "First Last Throwaway"
394 394 assignee ||= assignable.detect {|a| a.is_a?(User) && a.firstname.to_s.downcase == firstname && a.lastname.to_s.downcase == lastname}
395 395 end
396 396 if assignee.nil?
397 397 assignee ||= assignable.detect {|a| a.is_a?(Group) && a.name.downcase == keyword}
398 398 end
399 399 assignee
400 400 end
401 401 end
General Comments 0
You need to be logged in to leave comments. Login now