From eb1ee0803e51fbbdd3f8966c511b5212aed91524 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Thu, 20 Sep 2012 06:23:44 -0400
Subject: [PATCH] Support old notify extension

---
 plugins/managesieve/lib/rcube_sieve_script.php |   33 +++++++++++++++++++++++----------
 plugins/managesieve/Changelog                  |    1 +
 plugins/managesieve/tests/src/parser_notify_a  |    7 +++----
 plugins/managesieve/package.xml                |    4 ++--
 plugins/managesieve/tests/src/parser_notify_b  |    5 ++---
 5 files changed, 31 insertions(+), 19 deletions(-)

diff --git a/plugins/managesieve/Changelog b/plugins/managesieve/Changelog
index 482cff0..32eb3cb 100644
--- a/plugins/managesieve/Changelog
+++ b/plugins/managesieve/Changelog
@@ -1,4 +1,5 @@
 - Fixed issue with DBMail bug [http://pear.php.net/bugs/bug.php?id=19077] (#1488594)
+- Added support for enotify/notify (RFC5435, RFC5436, draft-ietf-sieve-notify-04)
 
 * version 5.2 [2012-07-24]
 -----------------------------------------------------------
diff --git a/plugins/managesieve/lib/rcube_sieve_script.php b/plugins/managesieve/lib/rcube_sieve_script.php
index debe9c1..fe63141 100644
--- a/plugins/managesieve/lib/rcube_sieve_script.php
+++ b/plugins/managesieve/lib/rcube_sieve_script.php
@@ -41,7 +41,9 @@
         'variables',                // RFC5229
         'body',                     // RFC5173
         'subaddress',               // RFC5233
-        // @TODO: enotify/notify, spamtest+virustest, mailbox, date
+        'enotify',                  // RFC5435
+        'notify',                   // draft-ietf-sieve-notify-04
+        // @TODO: spamtest+virustest, mailbox, date
     );
 
     /**
@@ -197,6 +199,9 @@
                 }
             }
         }
+
+        $imapflags = in_array('imap4flags', $this->supported) ? 'imap4flags' : 'imapflags';
+        $notify    = in_array('enotify', $this->supported) ? 'enotify' : 'notify';
 
         // rules
         foreach ($this->content as $rule) {
@@ -370,11 +375,7 @@
                     case 'addflag':
                     case 'setflag':
                     case 'removeflag':
-                        if (in_array('imap4flags', $this->supported))
-                            array_push($exts, 'imap4flags');
-                        else
-                            array_push($exts, 'imapflags');
-
+                        array_push($exts, $imapflags);
                         $action_script .= $action['type'].' '
                             . self::escape_string($action['target']);
                         break;
@@ -404,8 +405,9 @@
                         break;
 
                     case 'notify':
-                        array_push($exts, 'enotify');
+                        array_push($exts, $notify);
                         $action_script .= 'notify';
+
                         foreach (array('from', 'importance', 'options', 'message') as $n_tag) {
                             if (!empty($action[$n_tag])) {
                                 $action_script .= " :$n_tag " . self::escape_string($action[$n_tag]);
@@ -420,7 +422,12 @@
                         else {
                             $method = $action['method'];
                         }
-                        $action_script .= " " . self::escape_string($method);
+
+                        // method is optional in notify extension
+                        if (!empty($method)) {
+                            $action_script .= ($notify == 'notify' ? " :method " : " ") . self::escape_string($method);
+                        }
+
                         break;
 
                     case 'vacation':
@@ -861,15 +868,21 @@
                 break;
 
             case 'notify':
-                $notify = array('type' => 'notify', 'method' => array_pop($tokens));
+                $notify = array('type' => 'notify');
 
                 // Parameters: :from, :importance, :options, :message
+                //     additional (optional) :method parameter for notify extension
                 for ($i=0, $len=count($tokens); $i<$len; $i++) {
                     $tok = strtolower($tokens[$i]);
                     if ($tok[0] == ':') {
-                        $notify[substr($tok, 1)] = $tokens[$i+1];
+                        $notify[substr($tok, 1)] = $tokens[++$i];
+                    }
+                    else {
+                        // unnamed parameter is a :method in enotify extension
+                        $notify['method'] = $tokens[$i];
                     }
                 }
+
                 $method_components = parse_url($notify['method']);
                 if ($method_components['scheme'] == 'mailto') {
                     $notify['address'] = $method_components['path'];
diff --git a/plugins/managesieve/package.xml b/plugins/managesieve/package.xml
index cde78c9..20fec78 100644
--- a/plugins/managesieve/package.xml
+++ b/plugins/managesieve/package.xml
@@ -17,9 +17,9 @@
 		<email>alec@alec.pl</email>
 		<active>yes</active>
 	</lead>
-	<date>2012-06-21</date>
+	<date>2012-07-24</date>
 	<version>
-		<release>5.1</release>
+		<release>5.2</release>
 		<api>5.0</api>
 	</version>
 	<stability>
diff --git a/plugins/managesieve/tests/src/parser_notify_a b/plugins/managesieve/tests/src/parser_notify_a
index 68a9ef5..a8b44c5 100644
--- a/plugins/managesieve/tests/src/parser_notify_a
+++ b/plugins/managesieve/tests/src/parser_notify_a
@@ -1,8 +1,8 @@
-require ["enotify","variables"];
+require ["notify","variables"];
 # rule:[notify1]
 if header :contains "from" "boss@example.org"
 {
-	notify :importance "1" :message "This is probably very important" "mailto:alm@example.com";
+	notify :importance "1" :message "This is probably very important";
 	stop;
 }
 # rule:[subject]
@@ -14,6 +14,5 @@
 if header :matches "From" "*"
 {
 	set "from" "${1}";
-	notify :importance "3" :message "${from}: ${subject}" "mailto:alm@example.com";
+	notify :importance "3" :message "${from}: ${subject}" :method "mailto:test@example.org";
 }
-
diff --git a/plugins/managesieve/tests/src/parser_notify_b b/plugins/managesieve/tests/src/parser_notify_b
index 8854658..cf80a97 100644
--- a/plugins/managesieve/tests/src/parser_notify_b
+++ b/plugins/managesieve/tests/src/parser_notify_b
@@ -1,4 +1,4 @@
-require ["envelope","variables","enotify"];
+require ["envelope","variables","notify"];
 # rule:[from]
 if envelope :all :matches "from" "*"
 {
@@ -13,6 +13,5 @@
 if address :all :matches "from" "*"
 {
 	set "from_addr" "${1}";
-	notify :message "${from_addr}${env_from}: ${subject}" "mailto:alm@example.com";
+	notify :message "${from_addr}${env_from}: ${subject}" :method "sms:1234567890";
 }
-

--
Gitblit v1.9.1