annotate .svn/pristine/c1/c1f9e2096ed8b684022346a547814bc032420b8c.svn-base @ 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 622f24f53b42
children
rev   line source
Chris@1295 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1295 3 #
Chris@1295 4 # This program is free software; you can redistribute it and/or
Chris@1295 5 # modify it under the terms of the GNU General Public License
Chris@1295 6 # as published by the Free Software Foundation; either version 2
Chris@1295 7 # of the License, or (at your option) any later version.
Chris@1295 8 #
Chris@1295 9 # This program is distributed in the hope that it will be useful,
Chris@1295 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1295 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1295 12 # GNU General Public License for more details.
Chris@1295 13 #
Chris@1295 14 # You should have received a copy of the GNU General Public License
Chris@1295 15 # along with this program; if not, write to the Free Software
Chris@1295 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1295 17
Chris@1295 18 class MailHandler < ActionMailer::Base
Chris@1295 19 include ActionView::Helpers::SanitizeHelper
Chris@1295 20 include Redmine::I18n
Chris@1295 21
Chris@1295 22 class UnauthorizedAction < StandardError; end
Chris@1295 23 class MissingInformation < StandardError; end
Chris@1295 24
Chris@1295 25 attr_reader :email, :user
Chris@1295 26
Chris@1295 27 def self.receive(email, options={})
Chris@1295 28 @@handler_options = options.dup
Chris@1295 29
Chris@1295 30 @@handler_options[:issue] ||= {}
Chris@1295 31
Chris@1295 32 if @@handler_options[:allow_override].is_a?(String)
Chris@1295 33 @@handler_options[:allow_override] = @@handler_options[:allow_override].split(',').collect(&:strip)
Chris@1295 34 end
Chris@1295 35 @@handler_options[:allow_override] ||= []
Chris@1295 36 # Project needs to be overridable if not specified
Chris@1295 37 @@handler_options[:allow_override] << 'project' unless @@handler_options[:issue].has_key?(:project)
Chris@1295 38 # Status overridable by default
Chris@1295 39 @@handler_options[:allow_override] << 'status' unless @@handler_options[:issue].has_key?(:status)
Chris@1295 40
Chris@1295 41 @@handler_options[:no_permission_check] = (@@handler_options[:no_permission_check].to_s == '1' ? true : false)
Chris@1295 42
Chris@1295 43 email.force_encoding('ASCII-8BIT') if email.respond_to?(:force_encoding)
Chris@1295 44 super(email)
Chris@1295 45 end
Chris@1295 46
Chris@1295 47 def logger
Chris@1295 48 Rails.logger
Chris@1295 49 end
Chris@1295 50
Chris@1295 51 cattr_accessor :ignored_emails_headers
Chris@1295 52 @@ignored_emails_headers = {
Chris@1295 53 'X-Auto-Response-Suppress' => 'oof',
Chris@1295 54 'Auto-Submitted' => /^auto-/
Chris@1295 55 }
Chris@1295 56
Chris@1295 57 # Processes incoming emails
Chris@1295 58 # Returns the created object (eg. an issue, a message) or false
Chris@1295 59 def receive(email)
Chris@1295 60 @email = email
Chris@1295 61 sender_email = email.from.to_a.first.to_s.strip
Chris@1295 62 # Ignore emails received from the application emission address to avoid hell cycles
Chris@1295 63 if sender_email.downcase == Setting.mail_from.to_s.strip.downcase
Chris@1295 64 if logger && logger.info
Chris@1295 65 logger.info "MailHandler: ignoring email from Redmine emission address [#{sender_email}]"
Chris@1295 66 end
Chris@1295 67 return false
Chris@1295 68 end
Chris@1295 69 # Ignore auto generated emails
Chris@1295 70 self.class.ignored_emails_headers.each do |key, ignored_value|
Chris@1295 71 value = email.header[key]
Chris@1295 72 if value
Chris@1295 73 value = value.to_s.downcase
Chris@1295 74 if (ignored_value.is_a?(Regexp) && value.match(ignored_value)) || value == ignored_value
Chris@1295 75 if logger && logger.info
Chris@1295 76 logger.info "MailHandler: ignoring email with #{key}:#{value} header"
Chris@1295 77 end
Chris@1295 78 return false
Chris@1295 79 end
Chris@1295 80 end
Chris@1295 81 end
Chris@1295 82 @user = User.find_by_mail(sender_email) if sender_email.present?
Chris@1295 83 if @user && !@user.active?
Chris@1295 84 if logger && logger.info
Chris@1295 85 logger.info "MailHandler: ignoring email from non-active user [#{@user.login}]"
Chris@1295 86 end
Chris@1295 87 return false
Chris@1295 88 end
Chris@1295 89 if @user.nil?
Chris@1295 90 # Email was submitted by an unknown user
Chris@1295 91 case @@handler_options[:unknown_user]
Chris@1295 92 when 'accept'
Chris@1295 93 @user = User.anonymous
Chris@1295 94 when 'create'
Chris@1295 95 @user = create_user_from_email
Chris@1295 96 if @user
Chris@1295 97 if logger && logger.info
Chris@1295 98 logger.info "MailHandler: [#{@user.login}] account created"
Chris@1295 99 end
Chris@1295 100 Mailer.account_information(@user, @user.password).deliver
Chris@1295 101 else
Chris@1295 102 if logger && logger.error
Chris@1295 103 logger.error "MailHandler: could not create account for [#{sender_email}]"
Chris@1295 104 end
Chris@1295 105 return false
Chris@1295 106 end
Chris@1295 107 else
Chris@1295 108 # Default behaviour, emails from unknown users are ignored
Chris@1295 109 if logger && logger.info
Chris@1295 110 logger.info "MailHandler: ignoring email from unknown user [#{sender_email}]"
Chris@1295 111 end
Chris@1295 112 return false
Chris@1295 113 end
Chris@1295 114 end
Chris@1295 115 User.current = @user
Chris@1295 116 dispatch
Chris@1295 117 end
Chris@1295 118
Chris@1295 119 private
Chris@1295 120
Chris@1295 121 MESSAGE_ID_RE = %r{^<?redmine\.([a-z0-9_]+)\-(\d+)\.\d+@}
Chris@1295 122 ISSUE_REPLY_SUBJECT_RE = %r{\[[^\]]*#(\d+)\]}
Chris@1295 123 MESSAGE_REPLY_SUBJECT_RE = %r{\[[^\]]*msg(\d+)\]}
Chris@1295 124
Chris@1295 125 def dispatch
Chris@1295 126 headers = [email.in_reply_to, email.references].flatten.compact
Chris@1295 127 subject = email.subject.to_s
Chris@1295 128 if headers.detect {|h| h.to_s =~ MESSAGE_ID_RE}
Chris@1295 129 klass, object_id = $1, $2.to_i
Chris@1295 130 method_name = "receive_#{klass}_reply"
Chris@1295 131 if self.class.private_instance_methods.collect(&:to_s).include?(method_name)
Chris@1295 132 send method_name, object_id
Chris@1295 133 else
Chris@1295 134 # ignoring it
Chris@1295 135 end
Chris@1295 136 elsif m = subject.match(ISSUE_REPLY_SUBJECT_RE)
Chris@1295 137 receive_issue_reply(m[1].to_i)
Chris@1295 138 elsif m = subject.match(MESSAGE_REPLY_SUBJECT_RE)
Chris@1295 139 receive_message_reply(m[1].to_i)
Chris@1295 140 else
Chris@1295 141 dispatch_to_default
Chris@1295 142 end
Chris@1295 143 rescue ActiveRecord::RecordInvalid => e
Chris@1295 144 # TODO: send a email to the user
Chris@1295 145 logger.error e.message if logger
Chris@1295 146 false
Chris@1295 147 rescue MissingInformation => e
Chris@1295 148 logger.error "MailHandler: missing information from #{user}: #{e.message}" if logger
Chris@1295 149 false
Chris@1295 150 rescue UnauthorizedAction => e
Chris@1295 151 logger.error "MailHandler: unauthorized attempt from #{user}" if logger
Chris@1295 152 false
Chris@1295 153 end
Chris@1295 154
Chris@1295 155 def dispatch_to_default
Chris@1295 156 receive_issue
Chris@1295 157 end
Chris@1295 158
Chris@1295 159 # Creates a new issue
Chris@1295 160 def receive_issue
Chris@1295 161 project = target_project
Chris@1295 162 # check permission
Chris@1295 163 unless @@handler_options[:no_permission_check]
Chris@1295 164 raise UnauthorizedAction unless user.allowed_to?(:add_issues, project)
Chris@1295 165 end
Chris@1295 166
Chris@1295 167 issue = Issue.new(:author => user, :project => project)
Chris@1295 168 issue.safe_attributes = issue_attributes_from_keywords(issue)
Chris@1295 169 issue.safe_attributes = {'custom_field_values' => custom_field_values_from_keywords(issue)}
Chris@1295 170 issue.subject = cleaned_up_subject
Chris@1295 171 if issue.subject.blank?
Chris@1295 172 issue.subject = '(no subject)'
Chris@1295 173 end
Chris@1295 174 issue.description = cleaned_up_text_body
Chris@1295 175
Chris@1295 176 # add To and Cc as watchers before saving so the watchers can reply to Redmine
Chris@1295 177 add_watchers(issue)
Chris@1295 178 issue.save!
Chris@1295 179 add_attachments(issue)
Chris@1295 180 logger.info "MailHandler: issue ##{issue.id} created by #{user}" if logger && logger.info
Chris@1295 181 issue
Chris@1295 182 end
Chris@1295 183
Chris@1295 184 # Adds a note to an existing issue
Chris@1295 185 def receive_issue_reply(issue_id, from_journal=nil)
Chris@1295 186 issue = Issue.find_by_id(issue_id)
Chris@1295 187 return unless issue
Chris@1295 188 # check permission
Chris@1295 189 unless @@handler_options[:no_permission_check]
Chris@1295 190 unless user.allowed_to?(:add_issue_notes, issue.project) ||
Chris@1295 191 user.allowed_to?(:edit_issues, issue.project)
Chris@1295 192 raise UnauthorizedAction
Chris@1295 193 end
Chris@1295 194 end
Chris@1295 195
Chris@1295 196 # ignore CLI-supplied defaults for new issues
Chris@1295 197 @@handler_options[:issue].clear
Chris@1295 198
Chris@1295 199 journal = issue.init_journal(user)
Chris@1295 200 if from_journal && from_journal.private_notes?
Chris@1295 201 # If the received email was a reply to a private note, make the added note private
Chris@1295 202 issue.private_notes = true
Chris@1295 203 end
Chris@1295 204 issue.safe_attributes = issue_attributes_from_keywords(issue)
Chris@1295 205 issue.safe_attributes = {'custom_field_values' => custom_field_values_from_keywords(issue)}
Chris@1295 206 journal.notes = cleaned_up_text_body
Chris@1295 207 add_attachments(issue)
Chris@1295 208 issue.save!
Chris@1295 209 if logger && logger.info
Chris@1295 210 logger.info "MailHandler: issue ##{issue.id} updated by #{user}"
Chris@1295 211 end
Chris@1295 212 journal
Chris@1295 213 end
Chris@1295 214
Chris@1295 215 # Reply will be added to the issue
Chris@1295 216 def receive_journal_reply(journal_id)
Chris@1295 217 journal = Journal.find_by_id(journal_id)
Chris@1295 218 if journal && journal.journalized_type == 'Issue'
Chris@1295 219 receive_issue_reply(journal.journalized_id, journal)
Chris@1295 220 end
Chris@1295 221 end
Chris@1295 222
Chris@1295 223 # Receives a reply to a forum message
Chris@1295 224 def receive_message_reply(message_id)
Chris@1295 225 message = Message.find_by_id(message_id)
Chris@1295 226 if message
Chris@1295 227 message = message.root
Chris@1295 228
Chris@1295 229 unless @@handler_options[:no_permission_check]
Chris@1295 230 raise UnauthorizedAction unless user.allowed_to?(:add_messages, message.project)
Chris@1295 231 end
Chris@1295 232
Chris@1295 233 if !message.locked?
Chris@1295 234 reply = Message.new(:subject => cleaned_up_subject.gsub(%r{^.*msg\d+\]}, '').strip,
Chris@1295 235 :content => cleaned_up_text_body)
Chris@1295 236 reply.author = user
Chris@1295 237 reply.board = message.board
Chris@1295 238 message.children << reply
Chris@1295 239 add_attachments(reply)
Chris@1295 240 reply
Chris@1295 241 else
Chris@1295 242 if logger && logger.info
Chris@1295 243 logger.info "MailHandler: ignoring reply from [#{sender_email}] to a locked topic"
Chris@1295 244 end
Chris@1295 245 end
Chris@1295 246 end
Chris@1295 247 end
Chris@1295 248
Chris@1295 249 def add_attachments(obj)
Chris@1295 250 if email.attachments && email.attachments.any?
Chris@1295 251 email.attachments.each do |attachment|
Chris@1295 252 obj.attachments << Attachment.create(:container => obj,
Chris@1295 253 :file => attachment.decoded,
Chris@1295 254 :filename => attachment.filename,
Chris@1295 255 :author => user,
Chris@1295 256 :content_type => attachment.mime_type)
Chris@1295 257 end
Chris@1295 258 end
Chris@1295 259 end
Chris@1295 260
Chris@1295 261 # Adds To and Cc as watchers of the given object if the sender has the
Chris@1295 262 # appropriate permission
Chris@1295 263 def add_watchers(obj)
Chris@1295 264 if user.allowed_to?("add_#{obj.class.name.underscore}_watchers".to_sym, obj.project)
Chris@1295 265 addresses = [email.to, email.cc].flatten.compact.uniq.collect {|a| a.strip.downcase}
Chris@1295 266 unless addresses.empty?
Chris@1295 267 watchers = User.active.find(:all, :conditions => ['LOWER(mail) IN (?)', addresses])
Chris@1295 268 watchers.each {|w| obj.add_watcher(w)}
Chris@1295 269 end
Chris@1295 270 end
Chris@1295 271 end
Chris@1295 272
Chris@1295 273 def get_keyword(attr, options={})
Chris@1295 274 @keywords ||= {}
Chris@1295 275 if @keywords.has_key?(attr)
Chris@1295 276 @keywords[attr]
Chris@1295 277 else
Chris@1295 278 @keywords[attr] = begin
Chris@1295 279 if (options[:override] || @@handler_options[:allow_override].include?(attr.to_s)) &&
Chris@1295 280 (v = extract_keyword!(plain_text_body, attr, options[:format]))
Chris@1295 281 v
Chris@1295 282 elsif !@@handler_options[:issue][attr].blank?
Chris@1295 283 @@handler_options[:issue][attr]
Chris@1295 284 end
Chris@1295 285 end
Chris@1295 286 end
Chris@1295 287 end
Chris@1295 288
Chris@1295 289 # Destructively extracts the value for +attr+ in +text+
Chris@1295 290 # Returns nil if no matching keyword found
Chris@1295 291 def extract_keyword!(text, attr, format=nil)
Chris@1295 292 keys = [attr.to_s.humanize]
Chris@1295 293 if attr.is_a?(Symbol)
Chris@1295 294 if user && user.language.present?
Chris@1295 295 keys << l("field_#{attr}", :default => '', :locale => user.language)
Chris@1295 296 end
Chris@1295 297 if Setting.default_language.present?
Chris@1295 298 keys << l("field_#{attr}", :default => '', :locale => Setting.default_language)
Chris@1295 299 end
Chris@1295 300 end
Chris@1295 301 keys.reject! {|k| k.blank?}
Chris@1295 302 keys.collect! {|k| Regexp.escape(k)}
Chris@1295 303 format ||= '.+'
Chris@1295 304 keyword = nil
Chris@1295 305 regexp = /^(#{keys.join('|')})[ \t]*:[ \t]*(#{format})\s*$/i
Chris@1295 306 if m = text.match(regexp)
Chris@1295 307 keyword = m[2].strip
Chris@1295 308 text.gsub!(regexp, '')
Chris@1295 309 end
Chris@1295 310 keyword
Chris@1295 311 end
Chris@1295 312
Chris@1295 313 def target_project
Chris@1295 314 # TODO: other ways to specify project:
Chris@1295 315 # * parse the email To field
Chris@1295 316 # * specific project (eg. Setting.mail_handler_target_project)
Chris@1295 317 target = Project.find_by_identifier(get_keyword(:project))
Chris@1295 318 raise MissingInformation.new('Unable to determine target project') if target.nil?
Chris@1295 319 target
Chris@1295 320 end
Chris@1295 321
Chris@1295 322 # Returns a Hash of issue attributes extracted from keywords in the email body
Chris@1295 323 def issue_attributes_from_keywords(issue)
Chris@1295 324 assigned_to = (k = get_keyword(:assigned_to, :override => true)) && find_assignee_from_keyword(k, issue)
Chris@1295 325
Chris@1295 326 attrs = {
Chris@1295 327 'tracker_id' => (k = get_keyword(:tracker)) && issue.project.trackers.named(k).first.try(:id),
Chris@1295 328 'status_id' => (k = get_keyword(:status)) && IssueStatus.named(k).first.try(:id),
Chris@1295 329 'priority_id' => (k = get_keyword(:priority)) && IssuePriority.named(k).first.try(:id),
Chris@1295 330 'category_id' => (k = get_keyword(:category)) && issue.project.issue_categories.named(k).first.try(:id),
Chris@1295 331 'assigned_to_id' => assigned_to.try(:id),
Chris@1295 332 'fixed_version_id' => (k = get_keyword(:fixed_version, :override => true)) &&
Chris@1295 333 issue.project.shared_versions.named(k).first.try(:id),
Chris@1295 334 'start_date' => get_keyword(:start_date, :override => true, :format => '\d{4}-\d{2}-\d{2}'),
Chris@1295 335 'due_date' => get_keyword(:due_date, :override => true, :format => '\d{4}-\d{2}-\d{2}'),
Chris@1295 336 'estimated_hours' => get_keyword(:estimated_hours, :override => true),
Chris@1295 337 'done_ratio' => get_keyword(:done_ratio, :override => true, :format => '(\d|10)?0')
Chris@1295 338 }.delete_if {|k, v| v.blank? }
Chris@1295 339
Chris@1295 340 if issue.new_record? && attrs['tracker_id'].nil?
Chris@1295 341 attrs['tracker_id'] = issue.project.trackers.find(:first).try(:id)
Chris@1295 342 end
Chris@1295 343
Chris@1295 344 attrs
Chris@1295 345 end
Chris@1295 346
Chris@1295 347 # Returns a Hash of issue custom field values extracted from keywords in the email body
Chris@1295 348 def custom_field_values_from_keywords(customized)
Chris@1295 349 customized.custom_field_values.inject({}) do |h, v|
Chris@1295 350 if keyword = get_keyword(v.custom_field.name, :override => true)
Chris@1295 351 h[v.custom_field.id.to_s] = v.custom_field.value_from_keyword(keyword, customized)
Chris@1295 352 end
Chris@1295 353 h
Chris@1295 354 end
Chris@1295 355 end
Chris@1295 356
Chris@1295 357 # Returns the text/plain part of the email
Chris@1295 358 # If not found (eg. HTML-only email), returns the body with tags removed
Chris@1295 359 def plain_text_body
Chris@1295 360 return @plain_text_body unless @plain_text_body.nil?
Chris@1295 361
Chris@1295 362 part = email.text_part || email.html_part || email
Chris@1295 363 @plain_text_body = Redmine::CodesetUtil.to_utf8(part.body.decoded, part.charset)
Chris@1295 364
Chris@1295 365 # strip html tags and remove doctype directive
Chris@1295 366 @plain_text_body = strip_tags(@plain_text_body.strip)
Chris@1295 367 @plain_text_body.sub! %r{^<!DOCTYPE .*$}, ''
Chris@1295 368 @plain_text_body
Chris@1295 369 end
Chris@1295 370
Chris@1295 371 def cleaned_up_text_body
Chris@1295 372 cleanup_body(plain_text_body)
Chris@1295 373 end
Chris@1295 374
Chris@1295 375 def cleaned_up_subject
Chris@1295 376 subject = email.subject.to_s
Chris@1295 377 subject.strip[0,255]
Chris@1295 378 end
Chris@1295 379
Chris@1295 380 def self.full_sanitizer
Chris@1295 381 @full_sanitizer ||= HTML::FullSanitizer.new
Chris@1295 382 end
Chris@1295 383
Chris@1295 384 def self.assign_string_attribute_with_limit(object, attribute, value, limit=nil)
Chris@1295 385 limit ||= object.class.columns_hash[attribute.to_s].limit || 255
Chris@1295 386 value = value.to_s.slice(0, limit)
Chris@1295 387 object.send("#{attribute}=", value)
Chris@1295 388 end
Chris@1295 389
Chris@1295 390 # Returns a User from an email address and a full name
Chris@1295 391 def self.new_user_from_attributes(email_address, fullname=nil)
Chris@1295 392 user = User.new
Chris@1295 393
Chris@1295 394 # Truncating the email address would result in an invalid format
Chris@1295 395 user.mail = email_address
Chris@1295 396 assign_string_attribute_with_limit(user, 'login', email_address, User::LOGIN_LENGTH_LIMIT)
Chris@1295 397
Chris@1295 398 names = fullname.blank? ? email_address.gsub(/@.*$/, '').split('.') : fullname.split
Chris@1295 399 assign_string_attribute_with_limit(user, 'firstname', names.shift)
Chris@1295 400 assign_string_attribute_with_limit(user, 'lastname', names.join(' '))
Chris@1295 401 user.lastname = '-' if user.lastname.blank?
Chris@1295 402
Chris@1295 403 password_length = [Setting.password_min_length.to_i, 10].max
Chris@1295 404 user.password = Redmine::Utils.random_hex(password_length / 2 + 1)
Chris@1295 405 user.language = Setting.default_language
Chris@1295 406
Chris@1295 407 unless user.valid?
Chris@1295 408 user.login = "user#{Redmine::Utils.random_hex(6)}" unless user.errors[:login].blank?
Chris@1295 409 user.firstname = "-" unless user.errors[:firstname].blank?
Chris@1295 410 user.lastname = "-" unless user.errors[:lastname].blank?
Chris@1295 411 end
Chris@1295 412
Chris@1295 413 user
Chris@1295 414 end
Chris@1295 415
Chris@1295 416 # Creates a User for the +email+ sender
Chris@1295 417 # Returns the user or nil if it could not be created
Chris@1295 418 def create_user_from_email
Chris@1295 419 from = email.header['from'].to_s
Chris@1295 420 addr, name = from, nil
Chris@1295 421 if m = from.match(/^"?(.+?)"?\s+<(.+@.+)>$/)
Chris@1295 422 addr, name = m[2], m[1]
Chris@1295 423 end
Chris@1295 424 if addr.present?
Chris@1295 425 user = self.class.new_user_from_attributes(addr, name)
Chris@1295 426 if user.save
Chris@1295 427 user
Chris@1295 428 else
Chris@1295 429 logger.error "MailHandler: failed to create User: #{user.errors.full_messages}" if logger
Chris@1295 430 nil
Chris@1295 431 end
Chris@1295 432 else
Chris@1295 433 logger.error "MailHandler: failed to create User: no FROM address found" if logger
Chris@1295 434 nil
Chris@1295 435 end
Chris@1295 436 end
Chris@1295 437
Chris@1295 438 # Removes the email body of text after the truncation configurations.
Chris@1295 439 def cleanup_body(body)
Chris@1295 440 delimiters = Setting.mail_handler_body_delimiters.to_s.split(/[\r\n]+/).reject(&:blank?).map {|s| Regexp.escape(s)}
Chris@1295 441 unless delimiters.empty?
Chris@1295 442 regex = Regexp.new("^[> ]*(#{ delimiters.join('|') })\s*[\r\n].*", Regexp::MULTILINE)
Chris@1295 443 body = body.gsub(regex, '')
Chris@1295 444 end
Chris@1295 445 body.strip
Chris@1295 446 end
Chris@1295 447
Chris@1295 448 def find_assignee_from_keyword(keyword, issue)
Chris@1295 449 keyword = keyword.to_s.downcase
Chris@1295 450 assignable = issue.assignable_users
Chris@1295 451 assignee = nil
Chris@1295 452 assignee ||= assignable.detect {|a|
Chris@1295 453 a.mail.to_s.downcase == keyword ||
Chris@1295 454 a.login.to_s.downcase == keyword
Chris@1295 455 }
Chris@1295 456 if assignee.nil? && keyword.match(/ /)
Chris@1295 457 firstname, lastname = *(keyword.split) # "First Last Throwaway"
Chris@1295 458 assignee ||= assignable.detect {|a|
Chris@1295 459 a.is_a?(User) && a.firstname.to_s.downcase == firstname &&
Chris@1295 460 a.lastname.to_s.downcase == lastname
Chris@1295 461 }
Chris@1295 462 end
Chris@1295 463 if assignee.nil?
Chris@1295 464 assignee ||= assignable.detect {|a| a.name.downcase == keyword}
Chris@1295 465 end
Chris@1295 466 assignee
Chris@1295 467 end
Chris@1295 468 end