annotate app/models/mail_handler.rb @ 1286:d0d6bbe9f2e0 redmine-2.2-integration

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