From 520c36aa4c24ad3424b8fa3137b7576e6eacaf14 Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Tue, 11 Oct 2005 17:11:42 -0400
Subject: [PATCH] Better support for Courier IMAP

---
 program/include/main.inc                  |    9 ++
 program/steps/mail/compose.inc            |    7 ++
 program/include/rcube_smtp.inc            |    2 
 program/lib/imap.inc                      |    6 +-
 program/steps/mail/sendmail.inc           |   14 ++++
 program/steps/settings/manage_folders.inc |    2 
 program/include/rcube_imap.inc            |   86 +++++++++++++++++-----------
 7 files changed, 83 insertions(+), 43 deletions(-)

diff --git a/program/include/main.inc b/program/include/main.inc
index e2e7a00..2e5be7b 100644
--- a/program/include/main.inc
+++ b/program/include/main.inc
@@ -386,8 +386,13 @@
 
 function console($msg, $type=1)
   {
-  print $msg;
-  print "\n<hr>\n";
+  if ($GLOBALS['REMOTE_REQUEST'])
+    print "// $msg\n";
+  else
+    {
+    print $msg;
+    print "\n<hr>\n";
+    }
   }
 
 
diff --git a/program/include/rcube_imap.inc b/program/include/rcube_imap.inc
index 2143416..6985212 100644
--- a/program/include/rcube_imap.inc
+++ b/program/include/rcube_imap.inc
@@ -28,6 +28,7 @@
 class rcube_imap
   {
   var $conn;
+  var $root_ns = '';
   var $root_dir = '';
   var $mailbox = 'INBOX';
   var $list_page = 1;
@@ -54,24 +55,6 @@
     }
 
 
-  function iloha_imap($connection='')
-    {
-    if ($connection)
-      {
-      $a_url = parse_url($connection);
-      $scheme = $a_url['scheme'] ? $a_url['scheme'] : 'imap';
-      $port = $a_url['port'] ? $a_url['port'] : ($scheme=='imaps' ? 993 : 143);
-      $host = $a_url['host'];
-      $user = $a_url['user'];
-      $pass = $a_url['pass'];
-      
-      //var_dump($a_url);
-      
-      $this->connect($host, $user, $pass, $port);
-      }
-    }
-
-
   function connect($host, $user, $pass, $port=143, $use_ssl=FALSE)
     {
     global $ICL_PORT, $CONFIG;
@@ -79,21 +62,43 @@
     // check for Open-SSL support in PHP build
     if ($use_ssl && in_array('openssl', get_loaded_extensions()))
       $ICL_SSL = TRUE;
+    else if ($use_ssl)
+      {
+      raise_error(array('code' => 403,
+                        'type' => 'imap',
+                        'message' => 'Open SSL not available;'), TRUE, FALSE);
+      $port = 143;
+      }
 
     $ICL_PORT = $port;
     $this->conn = iil_Connect($host, $user, $pass, array('imap' => 'check'));
     $this->host = $host;
     $this->user = $user;
     $this->pass = $pass;
+    $this->port = $port;
+    $this->ssl = $use_ssl;
     
+    // print trace mesages
     if ($this->conn && ($CONFIG['debug_level'] & 8))
-      print $this->conn->message;
-      
+      console($this->conn->message);
+    
+    // write error log
     else if (!$this->conn && $GLOBALS['iil_error'])
       {
       raise_error(array('code' => 403,
                        'type' => 'imap',
                        'message' => $GLOBALS['iil_error']), TRUE, FALSE);
+      }
+
+    // get account namespace
+    if ($this->conn)
+      {
+      iil_C_NameSpace($this->conn);
+      
+      if (!empty($this->conn->delimiter))
+        $this->delimiter = $this->conn->delimiter;
+      if (!empty($this->conn->rootdir))
+        $this->root_ns = $this->conn->rootdir;
       }
 
     return $this->conn ? TRUE : FALSE;
@@ -107,12 +112,22 @@
     }
 
 
+  function reconnect()
+    {
+    $this->close();
+    $this->connect($this->host, $this->user, $this->pass, $this->port, $this->ssl);
+    }
+
+
   function set_rootdir($root)
     {
-    if (substr($root, -1, 1)==='/')
+    if (ereg('[\.\/]$', $root)) //(substr($root, -1, 1)==='/')
       $root = substr($root, 0, -1);
 
     $this->root_dir = $root;
+    
+    if (empty($this->delimiter))
+      $this->get_hierarchy_delimiter();
     }
 
 
@@ -368,10 +383,10 @@
     // return cached header
     if ($a_msg_headers[$uid])
       return $a_msg_headers[$uid];
-    
+
     $msg_id = $this->_uid2id($uid);
     $header = iil_C_FetchHeader($this->conn, $mailbox, $msg_id);
-    
+
     // write headers cache
     $a_msg_headers[$uid] = $header;
     $this->update_cache($mailbox.'.msg', $a_msg_headers);
@@ -427,11 +442,13 @@
     else
       $result = iil_C_Flag($this->conn, $this->mailbox, join(',', $msg_ids), $flag);
 
-
     // reload message headers if cached
     $cache_key = $this->mailbox.'.msg';
-    if ($result && ($a_cached_headers = $this->get_cache($cache_key)))
+    if ($this->caching_enabled && $result && ($a_cached_headers = $this->get_cache($cache_key)))
       {
+      // close and re-open connection      
+      $this->reconnect();
+
       foreach ($uids as $uid)
         {
         if (isset($a_cached_headers[$uid]))
@@ -492,12 +509,12 @@
     // exit if no message uids are specified
     if (!is_array($a_uids))
       return false;
-      
+
     // convert uids to message ids
     $a_mids = array();
     foreach ($a_uids as $uid)
       $a_mids[] = $this->_uid2id($uid, $from_mbox);
-        
+
     $moved = iil_C_Move($this->conn, join(',', $a_mids), $from_mbox, $to_mbox);
     
     // send expunge command in order to have the moved message
@@ -646,15 +663,18 @@
     $result = FALSE;
     $abs_name = $this->_mod_mailbox($name);
     $a_mailbox_cache = $this->get_cache('mailboxes');
+    
+    if (strlen($this->root_ns))
+      $abs_name = $this->root_ns.$abs_name;
 
     if (strlen($abs_name) && (!is_array($a_mailbox_cache) || !in_array($abs_name, $a_mailbox_cache)))
-      $result = iil_C_CreateFolder($this->conn, $abs_name);
+      $result = iil_C_CreateFolder($this->conn, iil_utf7_encode($abs_name));
 
     // update mailboxlist cache
     if ($result && $subscribe)
-      $this->subscribe($name);
+      $this->subscribe($this->root_ns.$name);
 
-    return $result;
+    return $result ? $this->root_ns.$name : FALSE;
     }
 
 
@@ -911,9 +931,9 @@
 
   function _mod_mailbox($mbox, $mode='in')
     {
-    if ($this->root_dir && $mode=='in')
-      $mbox = $this->root_dir.'/'.$mbox;
-    else if ($this->root_dir && $mode=='out')
+    if (!empty($this->root_dir) && $mode=='in')
+      $mbox = $this->root_dir.$this->delimiter.$mbox;
+    else if (strlen($this->root_dir) && $mode=='out')
       $mbox = substr($mbox, strlen($this->root_dir)+1);
 
     return $mbox;
diff --git a/program/include/rcube_smtp.inc b/program/include/rcube_smtp.inc
index 9f19327..fd45a00 100644
--- a/program/include/rcube_smtp.inc
+++ b/program/include/rcube_smtp.inc
@@ -1,4 +1,4 @@
-<?
+<?php
 
 /*
  +-----------------------------------------------------------------------+
diff --git a/program/lib/imap.inc b/program/lib/imap.inc
index 75b0abb..7d225c6 100644
--- a/program/lib/imap.inc
+++ b/program/lib/imap.inc
@@ -325,9 +325,9 @@
 	}
 	
 	//open socket connection
-	$conn->fp = @fsockopen($host, $ICL_PORT);
+	$conn->fp = @fsockopen($host, $ICL_PORT, &$errno, &$errstr, 10);
 	if (!$conn->fp){
-        $iil_error = "Could not connect to $host at port $ICL_PORT";
+        $iil_error = "Could not connect to $host at port $ICL_PORT: $errstr";
         $iil_errornum = -1;
 		return false;
 	}
@@ -1440,7 +1440,7 @@
 			$line=chop(iil_ReadLine($fp, 100));
 			if ($line[0]=="*") $c++;
 		}while (!iil_StartsWith($line, "flg"));
-		
+
 		if (iil_ParseResult($line) == 0){
 			iil_C_ExpireCachedItems($conn, $mailbox, $messages);
 			return $c;
diff --git a/program/steps/mail/compose.inc b/program/steps/mail/compose.inc
index 03f508b..f65e4ff 100644
--- a/program/steps/mail/compose.inc
+++ b/program/steps/mail/compose.inc
@@ -403,7 +403,12 @@
     
   // create a reply-subject
   else if (isset($REPLY_MESSAGE['subject']))
-    $subject = 'Re: '.$REPLY_MESSAGE['subject'];
+    {
+    if (strpos($REPLY_MESSAGE['subject'], 'Re:')===0)
+      $subject = $REPLY_MESSAGE['subject'];
+    else
+      $subject = 'Re: '.$REPLY_MESSAGE['subject'];
+    }
 
   // create a forward-subject
   else if (isset($FORWARD_MESSAGE['subject']))
diff --git a/program/steps/mail/sendmail.inc b/program/steps/mail/sendmail.inc
index 2172acc..6182126 100644
--- a/program/steps/mail/sendmail.inc
+++ b/program/steps/mail/sendmail.inc
@@ -216,10 +216,20 @@
 
   // check if mailbox exists
   if (!in_array_nocase($CONFIG['sent_mbox'], $IMAP->list_mailboxes()))
-    $IMAP->create_mailbox($CONFIG['sent_mbox'], TRUE);
+    $mbox = $IMAP->create_mailbox($CONFIG['sent_mbox'], TRUE);
+  else
+    $mbox = TRUE;
 
   // append message to sent box
-  $saved = $IMAP->save_message($CONFIG['sent_mbox'], $header_str."\r\n".$msg_body);
+  if ($mbox)
+    $saved = $IMAP->save_message($CONFIG['sent_mbox'], $header_str."\r\n".$msg_body);
+
+  // raise error if saving failed
+  if (!$saved)
+    raise_error(array('code' => 800,
+                      'type' => 'imap',
+                      'file' => __FILE__,
+                      'message' => "Could not save message in $CONFIG[sent_mbox]"), TRUE, FALSE);
   }
 
 
diff --git a/program/steps/settings/manage_folders.inc b/program/steps/settings/manage_folders.inc
index 8d74ab0..b960561 100644
--- a/program/steps/settings/manage_folders.inc
+++ b/program/steps/settings/manage_folders.inc
@@ -51,7 +51,7 @@
 
   if ($create && $_GET['_remote'])
     {
-    $commands = sprintf("this.add_folder_row('%s')", rep_specialchars_output($_GET['_name'], 'js'));
+    $commands = sprintf("this.add_folder_row('%s')", rep_specialchars_output($create, 'js'));
     rcube_remote_response($commands);
     }
   else if (!$create && $_GET['_remote'])

--
Gitblit v1.9.1