Support composite header rules with IMAP backends (Bug #7343).
authorJan Schneider <jan@horde.org>
Tue, 22 Dec 2009 14:34:54 +0000 (15:34 +0100)
committerJan Schneider <jan@horde.org>
Tue, 22 Dec 2009 14:36:20 +0000 (15:36 +0100)
ingo/docs/CHANGES
ingo/lib/Script/Imap.php

index 718523a..bf873bd 100644 (file)
@@ -10,6 +10,7 @@ v2.0-git
 v1.2.4-cvs
 ----------
 
+[jan] Support composite header rules with IMAP backends (Bug #7343).
 [jan] Fix unconditional debug output with Net_Sieve earlier than 1.2.0
       (Bug #8794).
 [jan] Add Sieve configuration to use UTF-8 encoded folder names (for Dovecot)
index 74e1660..5da4828 100644 (file)
@@ -88,9 +88,10 @@ class Ingo_Script_Imap extends Ingo_Script
      * Perform the filtering specified in the rules.
      *
      * @param array $params  The parameter array. It MUST contain:
-     * <pre>
-     * 'mailbox' - The name of the mailbox to filter.
-     * </pre>
+     *                       - 'mailbox': The name of the mailbox to filter.
+     *                       - 'show_filter_msg': Show detailed filter status
+     *                          messages?
+     *                       - 'filter_seen': Only filter seen messages?
      *
      * @return boolean  True if filtering performed, false if not.
      */
@@ -149,13 +150,7 @@ class Ingo_Script_Imap extends Ingo_Script
 
                 $query = new Horde_Imap_Client_Search_Query();
                 foreach ($addr as $val) {
-                    $ob = new Horde_Imap_Client_Search_Query();
-                    $ob->flag('\\deleted', false);
-                    if ($params['filter_seen'] == Ingo_Script::FILTER_UNSEEN) {
-                        $ob->flag('\\seen', false);
-                    } elseif ($params['filter_seen'] == Ingo_Script::FILTER_SEEN) {
-                        $ob->flag('\\seen', true);
-                    }
+                    $ob = $this->_getQuery($params);
                     $ob->headerText('from', $val);
                     $search_array[] = $ob;
                 }
@@ -198,13 +193,7 @@ class Ingo_Script_Imap extends Ingo_Script
             case Ingo_Storage::ACTION_DISCARD:
                 $query = new Horde_Imap_Client_Search_Query();
                 foreach ($rule['conditions'] as $val) {
-                    $ob = new Horde_Imap_Client_Search_Query();
-                    $ob->flag('\\deleted', false);
-                    if ($params['filter_seen'] == Ingo_Script::FILTER_UNSEEN) {
-                        $ob->flag('\\seen', false);
-                    } elseif ($params['filter_seen'] == Ingo_Script::FILTER_SEEN) {
-                        $ob->flag('\\seen', true);
-                    }
+                    $ob = $this->_getQuery($params);
                     if (!empty($val['type']) &&
                         ($val['type'] == Ingo_Storage::TYPE_SIZE)) {
                         $ob->size($val['value'], ($val['match'] == 'greater than'));
@@ -212,7 +201,21 @@ class Ingo_Script_Imap extends Ingo_Script
                               ($val['type'] == Ingo_Storage::TYPE_BODY)) {
                         $ob->text($val['value'], true, ($val['match'] == 'not contain'));
                     } else {
-                        $ob->headerText($val['field'], $val['value'], ($val['match'] == 'not contain'));
+                        if (strpos($val['field'], ',') == false) {
+                            $ob->headerText($val['field'], $val['value'], $val['match'] == 'not contain');
+                        } else {
+                            $headers = array();
+                            foreach (explode($val['field'], ',') as $header) {
+                                $headerOb = $this->_getQuery($params);
+                                $headerOb->headerText($header, $val['value'], $val['match'] == 'not contain');
+                                $headers[] = $headerOb;
+                            }
+                            if ($val['match'] == 'contains') {
+                                $ob->orSearch($headers);
+                            } elseif ($val['match'] == 'not contain') {
+                                $ob->andSearch($headers);
+                            }
+                        }
                     }
                     $search_array[] = $ob;
                 }
@@ -359,4 +362,20 @@ class Ingo_Script_Imap extends Ingo_Script
             : false;
     }
 
+    /**
+     * Returns a query object prepared for adding further criteria.
+     *
+     * @param array $params  The parameter array. It MUST contain:
+     *                       - 'filter_seen': Only filter seen messages?
+     *
+     * @return Ingo_IMAP_Search_Query  A query object.
+     */
+    protected function _getQuery($params)
+    {
+        $ob = new Horde_Imap_Client_Search_Query();
+        $ob->flag('\\deleted', false);
+        $ob->flag('\\seen', $params['filter_seen'] == Ingo_Script::FILTER_SEEN);
+        return $ob;
+    }
+
 }