comparison app/models/mail_handler.rb @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 0a574315af3e 622f24f53b42
children
comparison
equal deleted inserted replaced
1297:0a574315af3e 1298:4f746d8966dd
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
36 # Project needs to be overridable if not specified 36 # Project needs to be overridable if not specified
37 @@handler_options[:allow_override] << 'project' unless @@handler_options[:issue].has_key?(:project) 37 @@handler_options[:allow_override] << 'project' unless @@handler_options[:issue].has_key?(:project)
38 # Status overridable by default 38 # Status overridable by default
39 @@handler_options[:allow_override] << 'status' unless @@handler_options[:issue].has_key?(:status) 39 @@handler_options[:allow_override] << 'status' unless @@handler_options[:issue].has_key?(:status)
40 40
41 @@handler_options[:no_permission_check] = (@@handler_options[:no_permission_check].to_s == '1' ? true : false) 41 @@handler_options[:no_account_notice] = (@@handler_options[:no_account_notice].to_s == '1')
42 @@handler_options[:no_notification] = (@@handler_options[:no_notification].to_s == '1')
43 @@handler_options[:no_permission_check] = (@@handler_options[:no_permission_check].to_s == '1')
42 44
43 email.force_encoding('ASCII-8BIT') if email.respond_to?(:force_encoding) 45 email.force_encoding('ASCII-8BIT') if email.respond_to?(:force_encoding)
44 super(email) 46 super(email)
45 end 47 end
46 48
95 @user = create_user_from_email 97 @user = create_user_from_email
96 if @user 98 if @user
97 if logger && logger.info 99 if logger && logger.info
98 logger.info "MailHandler: [#{@user.login}] account created" 100 logger.info "MailHandler: [#{@user.login}] account created"
99 end 101 end
100 Mailer.account_information(@user, @user.password).deliver 102 add_user_to_group(@@handler_options[:default_group])
103 unless @@handler_options[:no_account_notice]
104 Mailer.account_information(@user, @user.password).deliver
105 end
101 else 106 else
102 if logger && logger.error 107 if logger && logger.error
103 logger.error "MailHandler: could not create account for [#{sender_email}]" 108 logger.error "MailHandler: could not create account for [#{sender_email}]"
104 end 109 end
105 return false 110 return false
106 end 111 end
107 else 112 else
108 # Default behaviour, emails from unknown users are ignored 113 # Default behaviour, emails from unknown users are ignored
109 if logger && logger.info 114 if logger && logger.info
110 logger.info "MailHandler: ignoring email from unknown user [#{sender_email}]" 115 logger.info "MailHandler: ignoring email from unknown user [#{sender_email}]"
111 end 116 end
112 return false 117 return false
113 end 118 end
114 end 119 end
115 User.current = @user 120 User.current = @user
262 # appropriate permission 267 # appropriate permission
263 def add_watchers(obj) 268 def add_watchers(obj)
264 if user.allowed_to?("add_#{obj.class.name.underscore}_watchers".to_sym, obj.project) 269 if user.allowed_to?("add_#{obj.class.name.underscore}_watchers".to_sym, obj.project)
265 addresses = [email.to, email.cc].flatten.compact.uniq.collect {|a| a.strip.downcase} 270 addresses = [email.to, email.cc].flatten.compact.uniq.collect {|a| a.strip.downcase}
266 unless addresses.empty? 271 unless addresses.empty?
267 watchers = User.active.find(:all, :conditions => ['LOWER(mail) IN (?)', addresses]) 272 watchers = User.active.where('LOWER(mail) IN (?)', addresses).all
268 watchers.each {|w| obj.add_watcher(w)} 273 watchers.each {|w| obj.add_watcher(w)}
269 end 274 end
270 end 275 end
271 end 276 end
272 277
336 'estimated_hours' => get_keyword(:estimated_hours, :override => true), 341 'estimated_hours' => get_keyword(:estimated_hours, :override => true),
337 'done_ratio' => get_keyword(:done_ratio, :override => true, :format => '(\d|10)?0') 342 'done_ratio' => get_keyword(:done_ratio, :override => true, :format => '(\d|10)?0')
338 }.delete_if {|k, v| v.blank? } 343 }.delete_if {|k, v| v.blank? }
339 344
340 if issue.new_record? && attrs['tracker_id'].nil? 345 if issue.new_record? && attrs['tracker_id'].nil?
341 attrs['tracker_id'] = issue.project.trackers.find(:first).try(:id) 346 attrs['tracker_id'] = issue.project.trackers.first.try(:id)
342 end 347 end
343 348
344 attrs 349 attrs
345 end 350 end
346 351
394 # Truncating the email address would result in an invalid format 399 # Truncating the email address would result in an invalid format
395 user.mail = email_address 400 user.mail = email_address
396 assign_string_attribute_with_limit(user, 'login', email_address, User::LOGIN_LENGTH_LIMIT) 401 assign_string_attribute_with_limit(user, 'login', email_address, User::LOGIN_LENGTH_LIMIT)
397 402
398 names = fullname.blank? ? email_address.gsub(/@.*$/, '').split('.') : fullname.split 403 names = fullname.blank? ? email_address.gsub(/@.*$/, '').split('.') : fullname.split
399 assign_string_attribute_with_limit(user, 'firstname', names.shift) 404 assign_string_attribute_with_limit(user, 'firstname', names.shift, 30)
400 assign_string_attribute_with_limit(user, 'lastname', names.join(' ')) 405 assign_string_attribute_with_limit(user, 'lastname', names.join(' '), 30)
401 user.lastname = '-' if user.lastname.blank? 406 user.lastname = '-' if user.lastname.blank?
402 407
403 password_length = [Setting.password_min_length.to_i, 10].max 408 password_length = [Setting.password_min_length.to_i, 10].max
404 user.password = Redmine::Utils.random_hex(password_length / 2 + 1) 409 user.password = Redmine::Utils.random_hex(password_length / 2 + 1)
405 user.language = Setting.default_language 410 user.language = Setting.default_language
411 user.mail_notification = 'only_my_events'
406 412
407 unless user.valid? 413 unless user.valid?
408 user.login = "user#{Redmine::Utils.random_hex(6)}" unless user.errors[:login].blank? 414 user.login = "user#{Redmine::Utils.random_hex(6)}" unless user.errors[:login].blank?
409 user.firstname = "-" unless user.errors[:firstname].blank? 415 user.firstname = "-" unless user.errors[:firstname].blank?
410 user.lastname = "-" unless user.errors[:lastname].blank? 416 (puts user.errors[:lastname];user.lastname = "-") unless user.errors[:lastname].blank?
411 end 417 end
412 418
413 user 419 user
414 end 420 end
415 421
421 if m = from.match(/^"?(.+?)"?\s+<(.+@.+)>$/) 427 if m = from.match(/^"?(.+?)"?\s+<(.+@.+)>$/)
422 addr, name = m[2], m[1] 428 addr, name = m[2], m[1]
423 end 429 end
424 if addr.present? 430 if addr.present?
425 user = self.class.new_user_from_attributes(addr, name) 431 user = self.class.new_user_from_attributes(addr, name)
432 if @@handler_options[:no_notification]
433 user.mail_notification = 'none'
434 end
426 if user.save 435 if user.save
427 user 436 user
428 else 437 else
429 logger.error "MailHandler: failed to create User: #{user.errors.full_messages}" if logger 438 logger.error "MailHandler: failed to create User: #{user.errors.full_messages}" if logger
430 nil 439 nil
431 end 440 end
432 else 441 else
433 logger.error "MailHandler: failed to create User: no FROM address found" if logger 442 logger.error "MailHandler: failed to create User: no FROM address found" if logger
434 nil 443 nil
444 end
445 end
446
447 # Adds the newly created user to default group
448 def add_user_to_group(default_group)
449 if default_group.present?
450 default_group.split(',').each do |group_name|
451 if group = Group.named(group_name).first
452 group.users << @user
453 elsif logger
454 logger.warn "MailHandler: could not add user to [#{group_name}], group not found"
455 end
456 end
435 end 457 end
436 end 458 end
437 459
438 # Removes the email body of text after the truncation configurations. 460 # Removes the email body of text after the truncation configurations.
439 def cleanup_body(body) 461 def cleanup_body(body)
453 a.mail.to_s.downcase == keyword || 475 a.mail.to_s.downcase == keyword ||
454 a.login.to_s.downcase == keyword 476 a.login.to_s.downcase == keyword
455 } 477 }
456 if assignee.nil? && keyword.match(/ /) 478 if assignee.nil? && keyword.match(/ /)
457 firstname, lastname = *(keyword.split) # "First Last Throwaway" 479 firstname, lastname = *(keyword.split) # "First Last Throwaway"
458 assignee ||= assignable.detect {|a| 480 assignee ||= assignable.detect {|a|
459 a.is_a?(User) && a.firstname.to_s.downcase == firstname && 481 a.is_a?(User) && a.firstname.to_s.downcase == firstname &&
460 a.lastname.to_s.downcase == lastname 482 a.lastname.to_s.downcase == lastname
461 } 483 }
462 end 484 end
463 if assignee.nil? 485 if assignee.nil?