From 0c4e40b89cfeab5feb9cfa0aecd6f2233134a076 Mon Sep 17 00:00:00 2001
From: Jean-Philippe Lang <jp_lang@yahoo.fr>
Date: Mon, 19 Jan 2009 18:29:07 +0000
Subject: [PATCH] Use In-Reply-To and References headers to handle replies by
 email.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@2288 e93f8b46-1217-0410-a6f0-8f06a7374b81
---
 app/models/mail_handler.rb                  | 25 ++++++++++++++++++---
 test/fixtures/mail_handler/ticket_reply.eml |  3 ++-
 2 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rb
index 2f9adec73..f6be1f124 100644
--- a/app/models/mail_handler.rb
+++ b/app/models/mail_handler.rb
@@ -53,11 +53,22 @@ class MailHandler < ActionMailer::Base
   
   private
 
+  MESSAGE_ID_RE = %r{^<redmine\.([a-z0-9_]+)\-(\d+)\.\d+@}
   ISSUE_REPLY_SUBJECT_RE = %r{\[[^\]]+#(\d+)\]}
   
   def dispatch
-    if m = email.subject.match(ISSUE_REPLY_SUBJECT_RE)
-      receive_issue_update(m[1].to_i)
+    headers = [email.in_reply_to, email.references].flatten.compact
+    if headers.detect {|h| h.to_s =~ MESSAGE_ID_RE}
+      klass, object_id = $1, $2.to_i
+      method_name = "receive_#{klass}_reply"
+      if self.class.private_instance_methods.include?(method_name)
+        send method_name, object_id
+      else
+        # ignoring it
+      end
+    elsif m = email.subject.match(ISSUE_REPLY_SUBJECT_RE)
+      # for compatibility
+      receive_issue_reply(m[1].to_i)
     else
       receive_issue
     end
@@ -117,7 +128,7 @@ class MailHandler < ActionMailer::Base
   end
   
   # Adds a note to an existing issue
-  def receive_issue_update(issue_id)
+  def receive_issue_reply(issue_id)
     status =  (get_keyword(:status) && IssueStatus.find_by_name(get_keyword(:status)))
     
     issue = Issue.find_by_id(issue_id)
@@ -139,6 +150,14 @@ class MailHandler < ActionMailer::Base
     journal
   end
   
+  # Reply will be added to the issue
+  def receive_journal_reply(journal_id)
+    journal = Journal.find_by_id(journal_id)
+    if journal && journal.journalized_type == 'Issue'
+      receive_issue_reply(journal.journalized_id)
+    end
+  end
+  
   def add_attachments(obj)
     if email.has_attachments?
       email.attachments.each do |attachment|
diff --git a/test/fixtures/mail_handler/ticket_reply.eml b/test/fixtures/mail_handler/ticket_reply.eml
index 99fcfa0d1..74724ccf4 100644
--- a/test/fixtures/mail_handler/ticket_reply.eml
+++ b/test/fixtures/mail_handler/ticket_reply.eml
@@ -3,10 +3,11 @@ Received: from osiris ([127.0.0.1])
 	by OSIRIS
 	with hMailServer ; Sat, 21 Jun 2008 18:41:39 +0200
 Message-ID: <006a01c8d3bd$ad9baec0$0a00a8c0@osiris>
+In-Reply-To: <redmine.issue-2.20060719210421@osiris>
 From: "John Smith" <jsmith@somenet.foo>
 To: <redmine@somenet.foo>
 References: <485d0ad366c88_d7014663a025f@osiris.tmail>
-Subject: Re: [Cookbook - Feature #2] (New) Add ingredients categories
+Subject: Re: Add ingredients categories
 Date: Sat, 21 Jun 2008 18:41:39 +0200
 MIME-Version: 1.0
 Content-Type: multipart/alternative;
-- 
GitLab