To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / models / mail_handler.rb @ 1298:4f746d8966dd

History | View | Annotate | Download (17.1 KB)

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