From eeb85f425760028fd87515f6c7a186ab100283ec Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Tue, 02 Nov 2010 10:53:54 -0400
Subject: [PATCH] - Add option to place replies in the folder of the message being replied to (#1485945) 

---
 program/steps/mail/compose.inc |   50 ++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 40 insertions(+), 10 deletions(-)

diff --git a/program/steps/mail/compose.inc b/program/steps/mail/compose.inc
index 3661f43..421ff32 100644
--- a/program/steps/mail/compose.inc
+++ b/program/steps/mail/compose.inc
@@ -100,12 +100,8 @@
   }
 
   // check if folder for saving sent messages exists and is subscribed (#1486802)
-  if (($sent_folder = $_SESSION['compose']['param']['sent_mbox']) && !$IMAP->mailbox_exists($sent_folder, true)) {
-    // folder may exist but isn't subscribed (#1485241)
-    if (!$IMAP->mailbox_exists($sent_folder))
-      $IMAP->create_mailbox($sent_folder, true);
-    else
-      $IMAP->subscribe($sent_folder);
+  if ($sent_folder = $_SESSION['compose']['param']['sent_mbox']) {
+    rcmail_check_sent_folder($sent_folder, true);
   }
 
   // redirect to a unique URL with all parameters stored in session
@@ -177,6 +173,13 @@
       $MESSAGE->reply_all = $_SESSION['compose']['param']['all'];
 
     $OUTPUT->set_env('compose_mode', 'reply');
+
+    // Save the sent message in the same folder of the message being replied to
+    if ($RCMAIL->config->get('reply_same_folder') && ($sent_folder = $_SESSION['compose']['mailbox'])
+      && rcmail_check_sent_folder($sent_folder, false)
+    ) {
+      $_SESSION['compose']['param']['sent_mbox'] = $sent_folder;
+    }
   }
   else if ($compose_mode == RCUBE_COMPOSE_DRAFT)
   {
@@ -191,8 +194,15 @@
         $_SESSION['compose']['forward_uid'] = $info['uid'];
 
       $_SESSION['compose']['mailbox'] = $info['folder'];
+
+      // Save the sent message in the same folder of the message being replied to
+      if ($RCMAIL->config->get('reply_same_folder') && ($sent_folder = $info['folder'])
+        && rcmail_check_sent_folder($sent_folder, false)
+      ) {
+        $_SESSION['compose']['param']['sent_mbox'] = $sent_folder;
+      }
     }
-    
+
     if ($MESSAGE->headers->in_reply_to)
       $_SESSION['compose']['reply_msgid'] = '<'.$MESSAGE->headers->in_reply_to.'>';
 
@@ -1261,6 +1271,26 @@
 }
 
 
+function rcmail_check_sent_folder($folder, $create=false)
+{
+  global $IMAP;
+
+  if ($IMAP->mailbox_exists($folder, true)) {
+    return true;
+  }
+
+  // folder may exist but isn't subscribed (#1485241)
+  if ($create) {
+    if (!$IMAP->mailbox_exists($folder))
+      return $IMAP->create_mailbox($folder, true);
+    else
+      return $IMAP->subscribe($folder);
+  }
+
+  return false;
+}
+
+
 function get_form_tags($attrib)
 {
   global $RCMAIL, $MESSAGE_FORM;
@@ -1274,13 +1304,13 @@
     $form_start = empty($attrib['form']) ? $RCMAIL->output->form_tag(array('name' => "form", 'method' => "post")) : '';
     $form_start .= $hiddenfields->show();
   }
-    
+
   $form_end = ($MESSAGE_FORM && !strlen($attrib['form'])) ? '</form>' : '';
   $form_name = !empty($attrib['form']) ? $attrib['form'] : 'form';
-  
+
   if (!$MESSAGE_FORM)
     $RCMAIL->output->add_gui_object('messageform', $form_name);
-  
+
   $MESSAGE_FORM = $form_name;
 
   return array($form_start, $form_end);

--
Gitblit v1.9.1