Class: MailHandler

Inherits:
ActionMailer::Base
  • Object
show all
Includes:
ActionView::Helpers::SanitizeHelper
Defined in:
app/models/mail_handler.rb

Overview

redMine - project management software Copyright (C) 2006-2007 Jean-Philippe Lang

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Defined Under Namespace

Classes: MissingInformation, UnauthorizedAction

Constant Summary

MESSAGE_ID_RE =
%r{^<redmine\.([a-z0-9_]+)\-(\d+)\.\d+@}
ISSUE_REPLY_SUBJECT_RE =
%r{\[[^\]]*#(\d+)\]}
MESSAGE_REPLY_SUBJECT_RE =
%r{\[[^\]]*msg(\d+)\]}

Instance Attribute Summary

Class Method Summary

Instance Method Summary

Instance Attribute Details

- (Object) email (readonly)

Returns the value of attribute email



24
25
26
# File 'app/models/mail_handler.rb', line 24

def 
  @email
end

- (Object) user (readonly)

Returns the value of attribute user



24
25
26
# File 'app/models/mail_handler.rb', line 24

def user
  @user
end

Class Method Details

+ (Object) receive(email, options = {})



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/mail_handler.rb', line 26

def self.receive(, options={})
  @@handler_options = options.dup
  
  @@handler_options[:issue] ||= {}
  
  @@handler_options[:allow_override] = @@handler_options[:allow_override].split(',').collect(&:strip) if @@handler_options[:allow_override].is_a?(String)
  @@handler_options[:allow_override] ||= []
  # Project needs to be overridable if not specified
  @@handler_options[:allow_override] << 'project' unless @@handler_options[:issue].has_key?(:project)
  # Status overridable by default
  @@handler_options[:allow_override] << 'status' unless @@handler_options[:issue].has_key?(:status)    
  
  @@handler_options[:no_permission_check] = (@@handler_options[:no_permission_check].to_s == '1' ? true : false)
  super 
end

Instance Method Details

- (Object) receive(email)

Processes incoming emails Returns the created object (eg. an issue, a message) or false



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/mail_handler.rb', line 44

def receive()
  @email = 
  sender_email = .from.to_a.first.to_s.strip
  # Ignore emails received from the application emission address to avoid hell cycles
  if sender_email.downcase == Setting.mail_from.to_s.strip.downcase
    logger.info  "MailHandler: ignoring email from Redmine emission address [#{sender_email}]" if logger && logger.info
    return false
  end
  @user = User.find_by_mail(sender_email)
  if @user && !@user.active?
    logger.info  "MailHandler: ignoring email from non-active user [#{@user.login}]" if logger && logger.info
    return false
  end
  if @user.nil?
    # Email was submitted by an unknown user
    case @@handler_options[:unknown_user]
    when 'accept'
      @user = User.anonymous
    when 'create'
      @user = MailHandler.create_user_from_email()
      if @user
        logger.info "MailHandler: [#{@user.login}] account created" if logger && logger.info
        Mailer.(@user, @user.password)
      else
        logger.error "MailHandler: could not create account for [#{sender_email}]" if logger && logger.error
        return false
      end
    else
      # Default behaviour, emails from unknown users are ignored
      logger.info  "MailHandler: ignoring email from unknown user [#{sender_email}]" if logger && logger.info
      return false
    end
  end
  User.current = @user
  dispatch
end