From c02a904140feeaf757cd307645ad914a233b1039 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Fri, 04 Oct 2013 04:32:21 -0400
Subject: [PATCH] Backported managesieve_domains option support

---
 plugins/managesieve/config.inc.php.dist |    4 ++
 plugins/managesieve/managesieve.js      |    2 
 plugins/managesieve/managesieve.php     |   56 +++++++++++++++++++++++-----
 3 files changed, 51 insertions(+), 11 deletions(-)

diff --git a/plugins/managesieve/config.inc.php.dist b/plugins/managesieve/config.inc.php.dist
index 65dbcfc..4d90805 100644
--- a/plugins/managesieve/config.inc.php.dist
+++ b/plugins/managesieve/config.inc.php.dist
@@ -64,4 +64,8 @@
 // Scripts listed here will be not presented to the user.
 $rcmail_config['managesieve_filename_exceptions'] = array();
 
+// List of domains limiting destination emails in redirect action
+// If not empty, user will need to select domain from a list
+$config['managesieve_domains'] = array();
+
 ?>
diff --git a/plugins/managesieve/managesieve.js b/plugins/managesieve/managesieve.js
index 035ed7b..da8da67 100644
--- a/plugins/managesieve/managesieve.js
+++ b/plugins/managesieve/managesieve.js
@@ -642,7 +642,7 @@
     enabled = {},
     elems = {
       mailbox: document.getElementById('action_mailbox' + id),
-      target: document.getElementById('action_target' + id),
+      target: document.getElementById('redirect_target' + id),
       target_area: document.getElementById('action_target_area' + id),
       flags: document.getElementById('action_flags' + id),
       vacation: document.getElementById('action_vacation' + id),
diff --git a/plugins/managesieve/managesieve.php b/plugins/managesieve/managesieve.php
index 80face7..4e7fdb6 100644
--- a/plugins/managesieve/managesieve.php
+++ b/plugins/managesieve/managesieve.php
@@ -660,6 +660,7 @@
             $act_types      = get_input_value('_action_type', RCUBE_INPUT_POST, true);
             $mailboxes      = get_input_value('_action_mailbox', RCUBE_INPUT_POST, true);
             $act_targets    = get_input_value('_action_target', RCUBE_INPUT_POST, true);
+            $domain_targets = get_input_value('_action_target_domain', RCUBE_INPUT_POST);
             $area_targets   = get_input_value('_action_target_area', RCUBE_INPUT_POST, true);
             $reasons        = get_input_value('_action_reason', RCUBE_INPUT_POST, true);
             $addresses      = get_input_value('_action_addresses', RCUBE_INPUT_POST, true);
@@ -828,8 +829,7 @@
             $i = 0;
             // actions
             foreach($act_types as $idx => $type) {
-                $type   = $this->strip_value($type);
-                $target = $this->strip_value($act_targets[$idx]);
+                $type = $this->strip_value($type);
 
                 switch ($type) {
 
@@ -854,12 +854,25 @@
 
                 case 'redirect':
                 case 'redirect_copy':
+                    $target = $this->strip_value($act_targets[$idx]);
+                    $domain = $this->strip_value($domain_targets[$idx]);
+
+                    // force one of the configured domains
+                    $domains = (array) $this->rc->config->get('managesieve_domains');
+                    if (!empty($domains) && !empty($target)) {
+                        if (!$domain || !in_array($domain, $domains)) {
+                            $domain = $domains[0];
+                        }
+
+                        $target .= '@' . $domain;
+                    }
+
                     $this->form['actions'][$i]['target'] = $target;
 
-                    if ($this->form['actions'][$i]['target'] == '')
+                    if ($target == '')
                         $this->errors['actions'][$i]['target'] = $this->gettext('cannotbeempty');
-                    else if (!check_email($this->form['actions'][$i]['target']))
-                        $this->errors['actions'][$i]['target'] = $this->gettext('noemailwarning');
+                    else if (!rcube_utils::check_email($target))
+                        $this->errors['actions'][$i]['target'] = $this->plugin->gettext(!empty($domains) ? 'forbiddenchars' : 'noemailwarning');
 
                     if ($type == 'redirect_copy') {
                         $type = 'redirect';
@@ -1560,11 +1573,34 @@
 
         // actions target inputs
         $out .= '<td class="rowtargets">';
-        // shared targets
-        $out .= '<input type="text" name="_action_target['.$id.']" id="action_target' .$id. '" '
-            .'value="' .($action['type']=='redirect' ? Q($action['target'], 'strict', false) : ''). '" size="35" '
-            .'style="display:' .($action['type']=='redirect' ? 'inline' : 'none') .'" '
-            . $this->error_class($id, 'action', 'target', 'action_target') .' />';
+
+        // force domain selection in redirect email input
+        $domains = (array) $this->rc->config->get('managesieve_domains');
+        if (!empty($domains)) {
+            sort($domains);
+
+            $domain_select = new html_select(array('name' => "_action_target_domain[$id]", 'id' => 'action_target_domain'.$id));
+            $domain_select->add(array_combine($domains, $domains));
+
+            $parts = explode('@', $action['target']);
+
+            if (!empty($parts)) {
+                $action['domain'] = array_pop($parts);
+                $action['target'] = implode('@', $parts);
+            }
+        }
+
+        // redirect target
+        $out .= '<span id="redirect_target' . $id . '" style="white-space:nowrap;'
+            . ' display:' . ($action['type'] == 'redirect' ? 'inline' : 'none') . '">'
+            . '<input type="text" name="_action_target['.$id.']" id="action_target' .$id. '"'
+            . ' value="' .($action['type'] == 'redirect' ? Q($action['target'], 'strict', false) : '') . '"'
+            . (!empty($domains) ? ' size="20"' : ' size="35"')
+            . $this->error_class($id, 'action', 'target', 'action_target') .' />'
+            . (!empty($domains) ? ' @ ' . $domain_select->show($action['domain']) : '')
+            . '</span>';
+
+        // (e)reject target
         $out .= '<textarea name="_action_target_area['.$id.']" id="action_target_area' .$id. '" '
             .'rows="3" cols="35" '. $this->error_class($id, 'action', 'targetarea', 'action_target_area')
             .'style="display:' .(in_array($action['type'], array('reject', 'ereject')) ? 'inline' : 'none') .'">'

--
Gitblit v1.9.1