To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / models / mail_handler.rb @ 1360:45dbcd39b9e9

History | View | Annotate | Download (16.3 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2012  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17

    
18
class MailHandler < ActionMailer::Base
19
  include ActionView::Helpers::SanitizeHelper
20
  include Redmine::I18n
21

    
22
  class UnauthorizedAction < StandardError; end
23
  class MissingInformation < StandardError; end
24

    
25
  attr_reader :email, :user
26

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

    
30
    @@handler_options[:issue] ||= {}
31

    
32
    if @@handler_options[:allow_override].is_a?(String)
33
      @@handler_options[:allow_override] = @@handler_options[:allow_override].split(',').collect(&:strip)
34
    end
35
    @@handler_options[:allow_override] ||= []
36
    # Project needs to be overridable if not specified
37
    @@handler_options[:allow_override] << 'project' unless @@handler_options[:issue].has_key?(:project)
38
    # Status overridable by default
39
    @@handler_options[:allow_override] << 'status' unless @@handler_options[:issue].has_key?(:status)
40

    
41
    @@handler_options[:no_permission_check] = (@@handler_options[:no_permission_check].to_s == '1' ? true : false)
42

    
43
    email.force_encoding('ASCII-8BIT') if email.respond_to?(:force_encoding)
44
    super(email)
45
  end
46

    
47
  def logger
48
    Rails.logger
49
  end
50

    
51
  cattr_accessor :ignored_emails_headers
52
  @@ignored_emails_headers = {
53
    'X-Auto-Response-Suppress' => 'oof',
54
    'Auto-Submitted' => /^auto-/
55
  }
56

    
57
  # Processes incoming emails
58
  # Returns the created object (eg. an issue, a message) or false
59
  def receive(email)
60
    @email = email
61
    sender_email = email.from.to_a.first.to_s.strip
62
    # Ignore emails received from the application emission address to avoid hell cycles
63
    if sender_email.downcase == Setting.mail_from.to_s.strip.downcase
64
      if logger && logger.info
65
        logger.info  "MailHandler: ignoring email from Redmine emission address [#{sender_email}]"
66
      end
67
      return false
68
    end
69
    # Ignore auto generated emails
70
    self.class.ignored_emails_headers.each do |key, ignored_value|
71
      value = email.header[key]
72
      if value
73
        value = value.to_s.downcase
74
        if (ignored_value.is_a?(Regexp) && value.match(ignored_value)) || value == ignored_value
75
          if logger && logger.info
76
            logger.info "MailHandler: ignoring email with #{key}:#{value} header"
77
          end
78
          return false
79
        end
80
      end
81
    end
82
    @user = User.find_by_mail(sender_email) if sender_email.present?
83
    if @user && !@user.active?
84
      if logger && logger.info
85
        logger.info  "MailHandler: ignoring email from non-active user [#{@user.login}]"
86
      end
87
      return false
88
    end
89
    if @user.nil?
90
      # Email was submitted by an unknown user
91
      case @@handler_options[:unknown_user]
92
      when 'accept'
93
        @user = User.anonymous
94
      when 'create'
95
        @user = create_user_from_email
96
        if @user
97
          if logger && logger.info
98
            logger.info "MailHandler: [#{@user.login}] account created"
99
          end
100
          Mailer.account_information(@user, @user.password).deliver
101
        else
102
          if logger && logger.error
103
            logger.error "MailHandler: could not create account for [#{sender_email}]"
104
          end
105
          return false
106
        end
107
      else
108
        # Default behaviour, emails from unknown users are ignored
109
        if logger && logger.info
110
          logger.info  "MailHandler: ignoring email from unknown user [#{sender_email}]" 
111
        end
112
        return false
113
      end
114
    end
115
    User.current = @user
116
    dispatch
117
  end
118

    
119
  private
120

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

    
125
  def dispatch
126
    headers = [email.in_reply_to, email.references].flatten.compact
127
    subject = email.subject.to_s
128
    if headers.detect {|h| h.to_s =~ MESSAGE_ID_RE}
129
      klass, object_id = $1, $2.to_i
130
      method_name = "receive_#{klass}_reply"
131
      if self.class.private_instance_methods.collect(&:to_s).include?(method_name)
132
        send method_name, object_id
133
      else
134
        # ignoring it
135
      end
136
    elsif m = subject.match(ISSUE_REPLY_SUBJECT_RE)
137
      receive_issue_reply(m[1].to_i)
138
    elsif m = subject.match(MESSAGE_REPLY_SUBJECT_RE)
139
      receive_message_reply(m[1].to_i)
140
    else
141
      dispatch_to_default
142
    end
143
  rescue ActiveRecord::RecordInvalid => e
144
    # TODO: send a email to the user
145
    logger.error e.message if logger
146
    false
147
  rescue MissingInformation => e
148
    logger.error "MailHandler: missing information from #{user}: #{e.message}" if logger
149
    false
150
  rescue UnauthorizedAction => e
151
    logger.error "MailHandler: unauthorized attempt from #{user}" if logger
152
    false
153
  end
154

    
155
  def dispatch_to_default
156
    receive_issue
157
  end
158

    
159
  # Creates a new issue
160
  def receive_issue
161
    project = target_project
162
    # check permission
163
    unless @@handler_options[:no_permission_check]
164
      raise UnauthorizedAction unless user.allowed_to?(:add_issues, project)
165
    end
166

    
167
    issue = Issue.new(:author => user, :project => project)
168
    issue.safe_attributes = issue_attributes_from_keywords(issue)
169
    issue.safe_attributes = {'custom_field_values' => custom_field_values_from_keywords(issue)}
170
    issue.subject = cleaned_up_subject
171
    if issue.subject.blank?
172
      issue.subject = '(no subject)'
173
    end
174
    issue.description = cleaned_up_text_body
175

    
176
    # add To and Cc as watchers before saving so the watchers can reply to Redmine
177
    add_watchers(issue)
178
    issue.save!
179
    add_attachments(issue)
180
    logger.info "MailHandler: issue ##{issue.id} created by #{user}" if logger && logger.info
181
    issue
182
  end
183

    
184
  # Adds a note to an existing issue
185
  def receive_issue_reply(issue_id, from_journal=nil)
186
    issue = Issue.find_by_id(issue_id)
187
    return unless issue
188
    # check permission
189
    unless @@handler_options[:no_permission_check]
190
      unless user.allowed_to?(:add_issue_notes, issue.project) ||
191
               user.allowed_to?(:edit_issues, issue.project)
192
        raise UnauthorizedAction
193
      end
194
    end
195

    
196
    # ignore CLI-supplied defaults for new issues
197
    @@handler_options[:issue].clear
198

    
199
    journal = issue.init_journal(user)
200
    if from_journal && from_journal.private_notes?
201
      # If the received email was a reply to a private note, make the added note private
202
      issue.private_notes = true
203
    end
204
    issue.safe_attributes = issue_attributes_from_keywords(issue)
205
    issue.safe_attributes = {'custom_field_values' => custom_field_values_from_keywords(issue)}
206
    journal.notes = cleaned_up_text_body
207
    add_attachments(issue)
208
    issue.save!
209
    if logger && logger.info
210
      logger.info "MailHandler: issue ##{issue.id} updated by #{user}"
211
    end
212
    journal
213
  end
214

    
215
  # Reply will be added to the issue
216
  def receive_journal_reply(journal_id)
217
    journal = Journal.find_by_id(journal_id)
218
    if journal && journal.journalized_type == 'Issue'
219
      receive_issue_reply(journal.journalized_id, journal)
220
    end
221
  end
222

    
223
  # Receives a reply to a forum message
224
  def receive_message_reply(message_id)
225
    message = Message.find_by_id(message_id)
226
    if message
227
      message = message.root
228

    
229
      unless @@handler_options[:no_permission_check]
230
        raise UnauthorizedAction unless user.allowed_to?(:add_messages, message.project)
231
      end
232

    
233
      if !message.locked?
234
        reply = Message.new(:subject => cleaned_up_subject.gsub(%r{^.*msg\d+\]}, '').strip,
235
                            :content => cleaned_up_text_body)
236
        reply.author = user
237
        reply.board = message.board
238
        message.children << reply
239
        add_attachments(reply)
240
        reply
241
      else
242
        if logger && logger.info
243
          logger.info "MailHandler: ignoring reply from [#{sender_email}] to a locked topic"
244
        end
245
      end
246
    end
247
  end
248

    
249
  def add_attachments(obj)
250
    if email.attachments && email.attachments.any?
251
      email.attachments.each do |attachment|
252
        obj.attachments << Attachment.create(:container => obj,
253
                          :file => attachment.decoded,
254
                          :filename => attachment.filename,
255
                          :author => user,
256
                          :content_type => attachment.mime_type)
257
      end
258
    end
259
  end
260

    
261
  # Adds To and Cc as watchers of the given object if the sender has the
262
  # appropriate permission
263
  def add_watchers(obj)
264
    if user.allowed_to?("add_#{obj.class.name.underscore}_watchers".to_sym, obj.project)
265
      addresses = [email.to, email.cc].flatten.compact.uniq.collect {|a| a.strip.downcase}
266
      unless addresses.empty?
267
        watchers = User.active.find(:all, :conditions => ['LOWER(mail) IN (?)', addresses])
268
        watchers.each {|w| obj.add_watcher(w)}
269
      end
270
    end
271
  end
272

    
273
  def get_keyword(attr, options={})
274
    @keywords ||= {}
275
    if @keywords.has_key?(attr)
276
      @keywords[attr]
277
    else
278
      @keywords[attr] = begin
279
        if (options[:override] || @@handler_options[:allow_override].include?(attr.to_s)) &&
280
              (v = extract_keyword!(plain_text_body, attr, options[:format]))
281
          v
282
        elsif !@@handler_options[:issue][attr].blank?
283
          @@handler_options[:issue][attr]
284
        end
285
      end
286
    end
287
  end
288

    
289
  # Destructively extracts the value for +attr+ in +text+
290
  # Returns nil if no matching keyword found
291
  def extract_keyword!(text, attr, format=nil)
292
    keys = [attr.to_s.humanize]
293
    if attr.is_a?(Symbol)
294
      if user && user.language.present?
295
        keys << l("field_#{attr}", :default => '', :locale =>  user.language)
296
      end
297
      if Setting.default_language.present?
298
        keys << l("field_#{attr}", :default => '', :locale =>  Setting.default_language)
299
      end
300
    end
301
    keys.reject! {|k| k.blank?}
302
    keys.collect! {|k| Regexp.escape(k)}
303
    format ||= '.+'
304
    keyword = nil
305
    regexp = /^(#{keys.join('|')})[ \t]*:[ \t]*(#{format})\s*$/i
306
    if m = text.match(regexp)
307
      keyword = m[2].strip
308
      text.gsub!(regexp, '')
309
    end
310
    keyword
311
  end
312

    
313
  def target_project
314
    # TODO: other ways to specify project:
315
    # * parse the email To field
316
    # * specific project (eg. Setting.mail_handler_target_project)
317
    target = Project.find_by_identifier(get_keyword(:project))
318
    raise MissingInformation.new('Unable to determine target project') if target.nil?
319
    target
320
  end
321

    
322
  # Returns a Hash of issue attributes extracted from keywords in the email body
323
  def issue_attributes_from_keywords(issue)
324
    assigned_to = (k = get_keyword(:assigned_to, :override => true)) && find_assignee_from_keyword(k, issue)
325

    
326
    attrs = {
327
      'tracker_id' => (k = get_keyword(:tracker)) && issue.project.trackers.named(k).first.try(:id),
328
      'status_id' =>  (k = get_keyword(:status)) && IssueStatus.named(k).first.try(:id),
329
      'priority_id' => (k = get_keyword(:priority)) && IssuePriority.named(k).first.try(:id),
330
      'category_id' => (k = get_keyword(:category)) && issue.project.issue_categories.named(k).first.try(:id),
331
      'assigned_to_id' => assigned_to.try(:id),
332
      'fixed_version_id' => (k = get_keyword(:fixed_version, :override => true)) &&
333
                                issue.project.shared_versions.named(k).first.try(:id),
334
      'start_date' => get_keyword(:start_date, :override => true, :format => '\d{4}-\d{2}-\d{2}'),
335
      'due_date' => get_keyword(:due_date, :override => true, :format => '\d{4}-\d{2}-\d{2}'),
336
      'estimated_hours' => get_keyword(:estimated_hours, :override => true),
337
      'done_ratio' => get_keyword(:done_ratio, :override => true, :format => '(\d|10)?0')
338
    }.delete_if {|k, v| v.blank? }
339

    
340
    if issue.new_record? && attrs['tracker_id'].nil?
341
      attrs['tracker_id'] = issue.project.trackers.find(:first).try(:id)
342
    end
343

    
344
    attrs
345
  end
346

    
347
  # Returns a Hash of issue custom field values extracted from keywords in the email body
348
  def custom_field_values_from_keywords(customized)
349
    customized.custom_field_values.inject({}) do |h, v|
350
      if keyword = get_keyword(v.custom_field.name, :override => true)
351
        h[v.custom_field.id.to_s] = v.custom_field.value_from_keyword(keyword, customized)
352
      end
353
      h
354
    end
355
  end
356

    
357
  # Returns the text/plain part of the email
358
  # If not found (eg. HTML-only email), returns the body with tags removed
359
  def plain_text_body
360
    return @plain_text_body unless @plain_text_body.nil?
361

    
362
    part = email.text_part || email.html_part || email
363
    @plain_text_body = Redmine::CodesetUtil.to_utf8(part.body.decoded, part.charset)
364

    
365
    # strip html tags and remove doctype directive
366
    @plain_text_body = strip_tags(@plain_text_body.strip)
367
    @plain_text_body.sub! %r{^<!DOCTYPE .*$}, ''
368
    @plain_text_body
369
  end
370

    
371
  def cleaned_up_text_body
372
    cleanup_body(plain_text_body)
373
  end
374

    
375
  def cleaned_up_subject
376
    subject = email.subject.to_s
377
    subject.strip[0,255]
378
  end
379

    
380
  def self.full_sanitizer
381
    @full_sanitizer ||= HTML::FullSanitizer.new
382
  end
383

    
384
  def self.assign_string_attribute_with_limit(object, attribute, value, limit=nil)
385
    limit ||= object.class.columns_hash[attribute.to_s].limit || 255
386
    value = value.to_s.slice(0, limit)
387
    object.send("#{attribute}=", value)
388
  end
389

    
390
  # Returns a User from an email address and a full name
391
  def self.new_user_from_attributes(email_address, fullname=nil)
392
    user = User.new
393

    
394
    # Truncating the email address would result in an invalid format
395
    user.mail = email_address
396
    assign_string_attribute_with_limit(user, 'login', email_address, User::LOGIN_LENGTH_LIMIT)
397

    
398
    names = fullname.blank? ? email_address.gsub(/@.*$/, '').split('.') : fullname.split
399
    assign_string_attribute_with_limit(user, 'firstname', names.shift)
400
    assign_string_attribute_with_limit(user, 'lastname', names.join(' '))
401
    user.lastname = '-' if user.lastname.blank?
402

    
403
    password_length = [Setting.password_min_length.to_i, 10].max
404
    user.password = Redmine::Utils.random_hex(password_length / 2 + 1)
405
    user.language = Setting.default_language
406

    
407
    unless user.valid?
408
      user.login = "user#{Redmine::Utils.random_hex(6)}" unless user.errors[:login].blank?
409
      user.firstname = "-" unless user.errors[:firstname].blank?
410
      user.lastname  = "-" unless user.errors[:lastname].blank?
411
    end
412

    
413
    user
414
  end
415

    
416
  # Creates a User for the +email+ sender
417
  # Returns the user or nil if it could not be created
418
  def create_user_from_email
419
    from = email.header['from'].to_s
420
    addr, name = from, nil
421
    if m = from.match(/^"?(.+?)"?\s+<(.+@.+)>$/)
422
      addr, name = m[2], m[1]
423
    end
424
    if addr.present?
425
      user = self.class.new_user_from_attributes(addr, name)
426
      if user.save
427
        user
428
      else
429
        logger.error "MailHandler: failed to create User: #{user.errors.full_messages}" if logger
430
        nil
431
      end
432
    else
433
      logger.error "MailHandler: failed to create User: no FROM address found" if logger
434
      nil
435
    end
436
  end
437

    
438
  # Removes the email body of text after the truncation configurations.
439
  def cleanup_body(body)
440
    delimiters = Setting.mail_handler_body_delimiters.to_s.split(/[\r\n]+/).reject(&:blank?).map {|s| Regexp.escape(s)}
441
    unless delimiters.empty?
442
      regex = Regexp.new("^[> ]*(#{ delimiters.join('|') })\s*[\r\n].*", Regexp::MULTILINE)
443
      body = body.gsub(regex, '')
444
    end
445
    body.strip
446
  end
447

    
448
  def find_assignee_from_keyword(keyword, issue)
449
    keyword = keyword.to_s.downcase
450
    assignable = issue.assignable_users
451
    assignee = nil
452
    assignee ||= assignable.detect {|a|
453
                   a.mail.to_s.downcase == keyword ||
454
                     a.login.to_s.downcase == keyword
455
                 }
456
    if assignee.nil? && keyword.match(/ /)
457
      firstname, lastname = *(keyword.split) # "First Last Throwaway"
458
      assignee ||= assignable.detect {|a| 
459
                     a.is_a?(User) && a.firstname.to_s.downcase == firstname &&
460
                       a.lastname.to_s.downcase == lastname
461
                   }
462
    end
463
    if assignee.nil?
464
      assignee ||= assignable.detect {|a| a.name.downcase == keyword}
465
    end
466
    assignee
467
  end
468
end