From 6c7ac2ef4b6e9482fd47b290dd9b07e5f8a0bd54 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Mon, 07 Jan 2013 13:51:38 -0500
Subject: [PATCH] Support tls:// prefix in managesieve_host option

---
 plugins/managesieve/config.inc.php.dist |    3 +--
 plugins/managesieve/Changelog           |    2 ++
 plugins/managesieve/managesieve.php     |   15 ++++++++++++---
 3 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/plugins/managesieve/Changelog b/plugins/managesieve/Changelog
index 50e51a6..19799a3 100644
--- a/plugins/managesieve/Changelog
+++ b/plugins/managesieve/Changelog
@@ -1,3 +1,5 @@
+- Support tls:// prefix in managesieve_host option
+
 * version 6.1 [2012-12-21]
 -----------------------------------------------------------
 - Fixed filter activation/deactivation confirmation message (#1488765)
diff --git a/plugins/managesieve/config.inc.php.dist b/plugins/managesieve/config.inc.php.dist
index 1f34564..65dbcfc 100644
--- a/plugins/managesieve/config.inc.php.dist
+++ b/plugins/managesieve/config.inc.php.dist
@@ -25,8 +25,7 @@
 $rcmail_config['managesieve_auth_pw'] = null;
 
 // use or not TLS for managesieve server connection
-// it's because I've problems with TLS and dovecot's managesieve plugin
-// and it's not needed on localhost
+// Note: tls:// prefix in managesieve_host is also supported
 $rcmail_config['managesieve_usetls'] = false;
 
 // default contents of filters script (eg. default spam filter)
diff --git a/plugins/managesieve/managesieve.php b/plugins/managesieve/managesieve.php
index 1579d62..d30404a 100644
--- a/plugins/managesieve/managesieve.php
+++ b/plugins/managesieve/managesieve.php
@@ -200,10 +200,19 @@
         $include_path .= ini_get('include_path');
         set_include_path($include_path);
 
-        $host = rcube_parse_host($this->rc->config->get('managesieve_host', 'localhost'));
+        // Get connection parameters
+        $host = $this->rc->config->get('managesieve_host', 'localhost');
+        $port = $this->rc->config->get('managesieve_port');
+        $tls  = $this->rc->config->get('managesieve_usetls', false);
+
+        $host = rcube_parse_host($host);
         $host = rcube_idn_to_ascii($host);
 
-        $port = $this->rc->config->get('managesieve_port');
+        // remove tls:// prefix, set TLS flag
+        if (($host = preg_replace('|^tls://|i', '', $host, 1, $cnt)) && $cnt) {
+            $tls = true;
+        }
+
         if (empty($port)) {
             $port = getservbyname('sieve', 'tcp');
             if (empty($port)) {
@@ -216,8 +225,8 @@
             'password'  => $this->rc->decrypt($_SESSION['password']),
             'host'      => $host,
             'port'      => $port,
+            'usetls'    => $tls,
             'auth_type' => $this->rc->config->get('managesieve_auth_type'),
-            'usetls'    => $this->rc->config->get('managesieve_usetls', false),
             'disabled'  => $this->rc->config->get('managesieve_disabled_extensions'),
             'debug'     => $this->rc->config->get('managesieve_debug', false),
             'auth_cid'  => $this->rc->config->get('managesieve_auth_cid'),

--
Gitblit v1.9.1