annotate .svn/pristine/c1/c1f9e2096ed8b684022346a547814bc032420b8c.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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