Project

General

Profile

Patch #11497 ยป 0012-Dry-up-MailHandler-logging.patch

Alex Shulgin, 2012-07-24 14:22

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

  
......
55 59
    sender_email = email.from.to_a.first.to_s.strip
56 60
    # Ignore emails received from the application emission address to avoid hell cycles
57 61
    if sender_email.downcase == Setting.mail_from.to_s.strip.downcase
58
      if logger && logger.info
59
        logger.info  "MailHandler: ignoring email from Redmine emission address [#{sender_email}]"
60
      end
62
      logger.info "MailHandler: ignoring email from Redmine emission address [#{sender_email}]"
61 63
      return false
62 64
    end
63 65
    # Ignore auto generated emails
......
66 68
      if value
67 69
        value = value.to_s.downcase
68 70
        if (ignored_value.is_a?(Regexp) && value.match(ignored_value)) || value == ignored_value
69
          if logger && logger.info
70
            logger.info "MailHandler: ignoring email with #{key}:#{value} header"
71
          end
71
          logger.info "MailHandler: ignoring email with #{key}:#{value} header"
72 72
          return false
73 73
        end
74 74
      end
75 75
    end
76 76
    @user = User.find_by_mail(sender_email) if sender_email.present?
77 77
    if @user && !@user.active?
78
      if logger && logger.info
79
        logger.info  "MailHandler: ignoring email from non-active user [#{@user.login}]"
80
      end
78
      logger.info "MailHandler: ignoring email from non-active user [#{@user.login}]"
81 79
      return false
82 80
    end
83 81
    if @user.nil?
......
88 86
      when 'create'
89 87
        @user = create_user_from_email
90 88
        if @user
91
          if logger && logger.info
92
            logger.info "MailHandler: [#{@user.login}] account created"
93
          end
89
          logger.info "MailHandler: [#{@user.login}] account created"
94 90
          Mailer.deliver_account_information(@user, @user.password)
95 91
        else
96
          if logger && logger.error
97
            logger.error "MailHandler: could not create account for [#{sender_email}]"
98
          end
92
          logger.error "MailHandler: could not create account for [#{sender_email}]"
99 93
          return false
100 94
        end
101 95
      else
102 96
        # Default behaviour, emails from unknown users are ignored
103
        if logger && logger.info
104
          logger.info  "MailHandler: ignoring email from unknown user [#{sender_email}]" 
105
        end
97
        logger.info  "MailHandler: ignoring email from unknown user [#{sender_email}]"
106 98
        return false
107 99
      end
108 100
    end
......
135 127
    end
136 128
  rescue ActiveRecord::RecordInvalid => e
137 129
    # TODO: send a email to the user
138
    logger.error e.message if logger
130
    logger.error e.message
139 131
    false
140 132
  rescue MissingInformation => e
141
    logger.error "MailHandler: missing information from #{user}: #{e.message}" if logger
133
    logger.error "MailHandler: missing information from #{user}: #{e.message}"
142 134
    false
143 135
  rescue UnauthorizedAction => e
144
    logger.error "MailHandler: unauthorized attempt from #{user}" if logger
136
    logger.error "MailHandler: unauthorized attempt from #{user}"
145 137
    false
146 138
  end
147 139

  
......
169 161
    # add To and Cc as watchers before saving so the watchers can reply to Redmine
170 162
    add_watchers(issue)
171 163
    issue.save!
164

  
172 165
    add_attachments(issue)
173
    logger.info "MailHandler: issue ##{issue.id} created by #{user}" if logger && logger.info
166
    logger.info "MailHandler: issue ##{issue.id} created by #{user}"
174 167
    issue
175 168
  end
176 169

  
......
199 192
    journal.notes = cleaned_up_text_body
200 193
    add_attachments(issue)
201 194
    issue.save!
202
    if logger && logger.info
203
      logger.info "MailHandler: issue ##{issue.id} updated by #{user}"
204
    end
195
    logger.info "MailHandler: issue ##{issue.id} updated by #{user}"
205 196
    journal
206 197
  end
207 198

  
......
232 223
        add_attachments(reply)
233 224
        reply
234 225
      else
235
        if logger && logger.info
236
          logger.info "MailHandler: ignoring reply from [#{sender_email}] to a locked topic"
237
        end
226
        logger.info "MailHandler: ignoring reply from [#{sender_email}] to a locked topic"
238 227
      end
239 228
    end
240 229
  end
......
411 400
      if user.save
412 401
        user
413 402
      else
414
        logger.error "MailHandler: failed to create User: #{user.errors.full_messages}" if logger
403
        logger.error "MailHandler: failed to create User: #{user.errors.full_messages}"
415 404
        nil
416 405
      end
417 406
    else
418
      logger.error "MailHandler: failed to create User: no FROM address found" if logger
407
      logger.error "MailHandler: failed to create User: no FROM address found"
419 408
      nil
420 409
    end
421 410
  end
    (1-1/1)