Project

General

Profile

Patch #9102 ยป 0001-Dry-up-MailHandler-logging.patch

Alex Shulgin, 2011-08-22 22:02

View differences:

app/models/mail_handler.rb
24 24

  
25 25
  attr_reader :email, :user
26 26

  
27
  def logger
28
    @logger = super || Logger.new(nil)
29
  end
30

  
27 31
  def self.receive(email, options={})
28 32
    @@handler_options = options.dup
29 33

  
......
47 51
    sender_email = email.from.to_a.first.to_s.strip
48 52
    # Ignore emails received from the application emission address to avoid hell cycles
49 53
    if sender_email.downcase == Setting.mail_from.to_s.strip.downcase
50
      logger.info  "MailHandler: ignoring email from Redmine emission address [#{sender_email}]" if logger && logger.info
54
      logger.info  "MailHandler: ignoring email from Redmine emission address [#{sender_email}]"
51 55
      return false
52 56
    end
53 57
    @user = User.find_by_mail(sender_email) if sender_email.present?
54 58
    if @user && !@user.active?
55
      logger.info  "MailHandler: ignoring email from non-active user [#{@user.login}]" if logger && logger.info
59
      logger.info  "MailHandler: ignoring email from non-active user [#{@user.login}]"
56 60
      return false
57 61
    end
58 62
    if @user.nil?
......
63 67
      when 'create'
64 68
        @user = MailHandler.create_user_from_email(email)
65 69
        if @user
66
          logger.info "MailHandler: [#{@user.login}] account created" if logger && logger.info
70
          logger.info "MailHandler: [#{@user.login}] account created"
67 71
          Mailer.deliver_account_information(@user, @user.password)
68 72
        else
69
          logger.error "MailHandler: could not create account for [#{sender_email}]" if logger && logger.error
73
          logger.error "MailHandler: could not create account for [#{sender_email}]"
70 74
          return false
71 75
        end
72 76
      else
73 77
        # Default behaviour, emails from unknown users are ignored
74
        logger.info  "MailHandler: ignoring email from unknown user [#{sender_email}]" if logger && logger.info
78
        logger.info  "MailHandler: ignoring email from unknown user [#{sender_email}]"
75 79
        return false
76 80
      end
77 81
    end
......
104 108
    end
105 109
  rescue ActiveRecord::RecordInvalid => e
106 110
    # TODO: send a email to the user
107
    logger.error e.message if logger
111
    logger.error e.message
108 112
    false
109 113
  rescue MissingInformation => e
110
    logger.error "MailHandler: missing information from #{user}: #{e.message}" if logger
114
    logger.error "MailHandler: missing information from #{user}: #{e.message}"
111 115
    false
112 116
  rescue UnauthorizedAction => e
113
    logger.error "MailHandler: unauthorized attempt from #{user}" if logger
117
    logger.error "MailHandler: unauthorized attempt from #{user}"
114 118
    false
115 119
  end
116 120

  
......
144 148
      # to save the record to trigger an issue update mail.
145 149
      issue.save!
146 150
    end
147
    logger.info "MailHandler: issue ##{issue.id} created by #{user}" if logger && logger.info
151
    logger.info "MailHandler: issue ##{issue.id} created by #{user}"
148 152
    issue
149 153
  end
150 154

  
......
170 174
    journal.notes = cleaned_up_text_body
171 175
    add_attachments(issue)
172 176
    issue.save!
173
    logger.info "MailHandler: issue ##{issue.id} updated by #{user}" if logger && logger.info
177
    logger.info "MailHandler: issue ##{issue.id} updated by #{user}"
174 178
    journal
175 179
  end
176 180

  
......
201 205
        add_attachments(reply)
202 206
        reply
203 207
      else
204
        logger.info "MailHandler: ignoring reply from [#{sender_email}] to a locked topic" if logger && logger.info
208
        logger.info "MailHandler: ignoring reply from [#{sender_email}] to a locked topic"
205 209
      end
206 210
    end
207 211
  end
    (1-1/1)