forum_message_creation_by_mail.patch

Stefan Bellus, 2011-04-07 10:40

Download (2.65 KB)

View differences:

app/models/mail_handler_with_forum_messages_creation_by_email.rb 2011-04-07 10:16:25.796713000 +0200
81 81
  
82 82
  private
83 83

  
84
  MESSAGE_ID_RE = %r{^<redmine\.([a-z0-9_]+)\-(\d+)\.\d+@}
85 84
  ISSUE_REPLY_SUBJECT_RE = %r{\[[^\]]*#(\d+)\]}
85
  MESSAGE_ID_RE = %r{^<redmine\.([a-z0-9_]+)\-(\d+)\.\d+@}
86 86
  MESSAGE_REPLY_SUBJECT_RE = %r{\[[^\]]*msg(\d+)\]}
87
  
87
  MESSAGE_SUBJECT = %r{(\[.* - .*\])}
88
  MESSAGE_SUBJECT_SPLIT = %r{(\[)(.*)( - )(.*)(\])}
89

  
88 90
  def dispatch
89 91
    headers = [email.in_reply_to, email.references].flatten.compact
90 92
    if headers.detect {|h| h.to_s =~ MESSAGE_ID_RE}
......
99 101
      receive_issue_reply(m[1].to_i)
100 102
    elsif m = email.subject.match(MESSAGE_REPLY_SUBJECT_RE)
101 103
      receive_message_reply(m[1].to_i)
104
    elsif email.subject.match(MESSAGE_SUBJECT)
105
      receive_forum_message
102 106
    else
103 107
      dispatch_to_default
104 108
    end
......
117 121
  def dispatch_to_default
118 122
    receive_issue
119 123
  end
120
  
124

  
125
  # Creates a new forum message
126
  def receive_forum_message
127
    project = target_project
128
    board = target_board
129
    # check permission
130
    unless @@handler_options[:no_permission_check]
131
      raise UnauthorizedAction unless user.allowed_to?(:edit_messages, project)
132
    end
133
    message = Message.new(:author => user, :board => board)
134
    message.subject = email.subject.split(MESSAGE_SUBJECT_SPLIT)[6].strip
135
    message.content = cleaned_up_text_body
136
    message.save!
137
    add_attachments(message)
138
    logger.info "MailHandler: message ##{message.id} - #{message.subject} created by #{user}" if logger && logger.info
139
    message
140
  end
141

  
121 142
  # Creates a new issue
122 143
  def receive_issue
123 144
    project = target_project
......
253 274
    # TODO: other ways to specify project:
254 275
    # * parse the email To field
255 276
    # * specific project (eg. Setting.mail_handler_target_project)
256
    target = Project.find_by_identifier(get_keyword(:project))
277
    unless target = Project.find_by_identifier(get_keyword(:project))
278
      target = Project.find_by_name(email.subject.split(MESSAGE_SUBJECT_SPLIT)[2])
279
    end
257 280
    raise MissingInformation.new('Unable to determine target project') if target.nil?
258 281
    target
259 282
  end
260 283
  
284
  def target_board
285
    target = Board.find_by_name(email.subject.split(MESSAGE_SUBJECT_SPLIT)[4].strip)
286
    raise MissingInformation.new('Unable to determine target board/forum') if target.nil?
287
    target
288
  end
289

  
261 290
  # Returns a Hash of issue attributes extracted from keywords in the email body
262 291
  def issue_attributes_from_keywords(issue)
263 292
    assigned_to = (k = get_keyword(:assigned_to, :override => true)) && find_user_from_keyword(k)