annotate .svn/pristine/f4/f477bf3402d32b68d566290356d0f3926584dab7.svn-base @ 1464:261b3d9a4903 redmine-2.4

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