fix-safe_receive-logging-error.diff

Go MAEDA, 2019-01-17 17:58

Download (6.27 KB)

View differences:

app/models/mail_handler.rb (working copy)
54 54
  def self.safe_receive(*args)
55 55
    receive(*args)
56 56
  rescue Exception => e
57
    logger.error "MailHandler: an unexpected error occurred when receiving email: #{e.message}" if logger
57
    Rails.logger.error "MailHandler: an unexpected error occurred when receiving email: #{e.message}"
58 58
    return false
59 59
  end
60 60

  
......
74 74
    options
75 75
  end
76 76

  
77
  def logger
78
    Rails.logger
79
  end
80

  
81 77
  cattr_accessor :ignored_emails_headers
82 78
  self.ignored_emails_headers = {
83 79
    'Auto-Submitted' => /\Aauto-(replied|generated)/,
......
92 88
    sender_email = email.from.to_a.first.to_s.strip
93 89
    # Ignore emails received from the application emission address to avoid hell cycles
94 90
    if sender_email.casecmp(Setting.mail_from.to_s.strip) == 0
95
      if logger
96
        logger.info  "MailHandler: ignoring email from Redmine emission address [#{sender_email}]"
97
      end
91
      Rails.logger.info  "MailHandler: ignoring email from Redmine emission address [#{sender_email}]"
98 92
      return false
99 93
    end
100 94
    # Ignore auto generated emails
......
103 97
      if value
104 98
        value = value.to_s.downcase
105 99
        if (ignored_value.is_a?(Regexp) && value.match(ignored_value)) || value == ignored_value
106
          if logger
107
            logger.info "MailHandler: ignoring email with #{key}:#{value} header"
108
          end
100
          Rails.logger.info "MailHandler: ignoring email with #{key}:#{value} header"
109 101
          return false
110 102
        end
111 103
      end
112 104
    end
113 105
    @user = User.find_by_mail(sender_email) if sender_email.present?
114 106
    if @user && !@user.active?
115
      if logger
116
        logger.info  "MailHandler: ignoring email from non-active user [#{@user.login}]"
117
      end
107
      Rails.logger.info  "MailHandler: ignoring email from non-active user [#{@user.login}]"
118 108
      return false
119 109
    end
120 110
    if @user.nil?
......
125 115
      when 'create'
126 116
        @user = create_user_from_email
127 117
        if @user
128
          if logger
129
            logger.info "MailHandler: [#{@user.login}] account created"
130
          end
118
          Rails.logger.info "MailHandler: [#{@user.login}] account created"
131 119
          add_user_to_group(handler_options[:default_group])
132 120
          unless handler_options[:no_account_notice]
133 121
            ::Mailer.deliver_account_information(@user, @user.password)
134 122
          end
135 123
        else
136
          if logger
137
            logger.error "MailHandler: could not create account for [#{sender_email}]"
138
          end
124
          Rails.logger.error "MailHandler: could not create account for [#{sender_email}]"
139 125
          return false
140 126
        end
141 127
      else
142 128
        # Default behaviour, emails from unknown users are ignored
143
        if logger
144
          logger.info  "MailHandler: ignoring email from unknown user [#{sender_email}]"
145
        end
129
        Rails.logger.info  "MailHandler: ignoring email from unknown user [#{sender_email}]"
146 130
        return false
147 131
      end
148 132
    end
......
176 160
    end
177 161
  rescue ActiveRecord::RecordInvalid => e
178 162
    # TODO: send a email to the user
179
    logger.error "MailHandler: #{e.message}" if logger
163
    Rails.logger.error "MailHandler: #{e.message}"
180 164
    false
181 165
  rescue MissingInformation => e
182
    logger.error "MailHandler: missing information from #{user}: #{e.message}" if logger
166
    Rails.logger.error "MailHandler: missing information from #{user}: #{e.message}"
183 167
    false
184 168
  rescue UnauthorizedAction => e
185
    logger.error "MailHandler: unauthorized attempt from #{user}" if logger
169
    Rails.logger.error "MailHandler: unauthorized attempt from #{user}"
186 170
    false
187 171
  end
188 172

  
......
220 204
    add_watchers(issue)
221 205
    issue.save!
222 206
    add_attachments(issue)
223
    logger.info "MailHandler: issue ##{issue.id} created by #{user}" if logger
207
    Rails.logger.info "MailHandler: issue ##{issue.id} created by #{user}"
224 208
    issue
225 209
  end
226 210

  
......
252 236
    add_watchers(issue)
253 237
    issue.save!
254 238
    add_attachments(issue)
255
    if logger
256
      logger.info "MailHandler: issue ##{issue.id} updated by #{user}"
257
    end
239
    Rails.logger.info "MailHandler: issue ##{issue.id} updated by #{user}"
258 240
    journal
259 241
  end
260 242

  
......
285 267
        add_attachments(reply)
286 268
        reply
287 269
      else
288
        if logger
289
          logger.info "MailHandler: ignoring reply from [#{sender_email}] to a locked topic"
290
        end
270
        Rails.logger.info "MailHandler: ignoring reply from [#{sender_email}] to a locked topic"
291 271
      end
292 272
    end
293 273
  end
......
316 296
        regexp = %r{\A#{Regexp.escape(pattern).gsub("\\*", ".*")}\z}i
317 297
      end
318 298
      if attachment.filename.to_s =~ regexp
319
        logger.info "MailHandler: ignoring attachment #{attachment.filename} matching #{pattern}"
299
        Rails.logger.info "MailHandler: ignoring attachment #{attachment.filename} matching #{pattern}"
320 300
        return false
321 301
      end
322 302
    end
......
549 529
      if user.save
550 530
        user
551 531
      else
552
        logger.error "MailHandler: failed to create User: #{user.errors.full_messages}" if logger
532
        Rails.logger.error "MailHandler: failed to create User: #{user.errors.full_messages}"
553 533
        nil
554 534
      end
555 535
    else
556
      logger.error "MailHandler: failed to create User: no FROM address found" if logger
536
      Rails.logger.error "MailHandler: failed to create User: no FROM address found"
557 537
      nil
558 538
    end
559 539
  end
......
564 544
      default_group.split(',').each do |group_name|
565 545
        if group = Group.named(group_name).first
566 546
          group.users << @user
567
        elsif logger
568
          logger.warn "MailHandler: could not add user to [#{group_name}], group not found"
547
        else
548
          Rails.logger.warn "MailHandler: could not add user to [#{group_name}], group not found"
569 549
        end
570 550
      end
571 551
    end
......
579 559
      begin
580 560
        delimiters = delimiters.map {|s| Regexp.new(s)}
581 561
      rescue RegexpError => e
582
        logger.error "MailHandler: invalid regexp delimiter found in mail_handler_body_delimiters setting (#{e.message})" if logger
562
        logger.error "MailHandler: invalid regexp delimiter found in mail_handler_body_delimiters setting (#{e.message})"
583 563
      end
584 564
    end
585 565