Author: Michael Slusarz (slusarz@horde.org)
Inheritance: extends Ingo_Storage_FilterIterator
Example #1
0
 /**
  * Generates the script to do the filtering specified in the rules.
  */
 protected function _generate()
 {
     $filters = Ingo_Storage_FilterIterator_Skip::create($this->_params['storage'], $this->_params['skip']);
     foreach ($filters as $rule) {
         switch (get_class($rule)) {
             case 'Ingo_Rule_System_Vacation':
                 $this->_addItem(Ingo::RULE_VACATION, new Ingo_Script_Ispconfig_Vacation(array('vacation' => $rule)));
                 break;
         }
     }
 }
Example #2
0
 /**
  * Generates the scripts to do the filtering specified in the rules.
  */
 protected function _generate()
 {
     $filters = Ingo_Storage_FilterIterator_Skip::create($this->_params['storage'], $this->_params['skip']);
     foreach ($filters as $rule) {
         switch (get_class($rule)) {
             case 'Ingo_Rule_System_Vacation':
                 $this->_addItem(Ingo::RULE_VACATION, new Ingo_Script_String($this->_placeHolders($this->_params['vacation_unset'], Ingo::RULE_VACATION)));
                 if (!$rule->disable) {
                     $this->_addItem(Ingo::RULE_VACATION, new Ingo_Script_String($this->_placeHolders($this->_params['vacation_set'], Ingo::RULE_VACATION)));
                 }
                 break;
         }
     }
 }
Example #3
0
 /**
  * Generates the script to do the filtering specified in the rules.
  */
 protected function _generate()
 {
     $this->_addItem(Ingo::RULE_ALL, new Ingo_Script_Maildrop_Comment(_("maildrop script generated by Ingo") . ' (' . date('F j, Y, g:i a') . ')'));
     /* Add variable information, if present. */
     if (!empty($this->_params['variables']) && is_array($this->_params['variables'])) {
         foreach ($this->_params['variables'] as $key => $val) {
             $this->_addItem(Ingo::RULE_ALL, new Ingo_Script_Maildrop_Variable(array('name' => $key, 'value' => $val)));
         }
     }
     $filters = Ingo_Storage_FilterIterator_Skip::create($this->_params['storage'], $this->_params['skip']);
     foreach ($filters as $rule) {
         switch ($class = get_class($rule)) {
             case 'Ingo_Rule_System_Blacklist':
                 $this->_generateBlacklist($rule);
                 break;
             case 'Ingo_Rule_System_Forward':
                 $this->_generateForward($rule);
                 break;
             case 'Ingo_Rule_System_Spam':
                 $this->_generateSpam($rule);
                 break;
             case 'Ingo_Rule_System_Vacation':
                 $this->_generateVacation($rule);
                 break;
             case 'Ingo_Rule_System_Whitelist':
                 $this->_generateWhitelist($rule);
                 break;
             default:
                 if (!in_array($class, $this->_actions)) {
                     break;
                 }
                 /* Create filter if using AND. */
                 $recipe = new Ingo_Script_Maildrop_Recipe(array('action' => $class, 'action-value' => $rule->value, 'combine' => $rule->combine, 'disable' => $rule->disable), $this->_params);
                 foreach ($rule->conditions as $condition) {
                     $recipe->addCondition($condition);
                 }
                 $this->_addItem(Ingo::RULE_FILTER, new Ingo_Script_Maildrop_Comment($rule->name, $rule->disable, true));
                 $this->_addItem(Ingo::RULE_FILTER, $recipe);
         }
     }
 }
Example #4
0
File: Imap.php Project: horde/horde
 /**
  * Performs the filtering specified in the rules.
  *
  * @param integer $change  The timestamp of the latest rule change during
  *                         the current session.
  */
 protected function _perform($change)
 {
     $api = $this->_params['api'];
     $notification = $this->_params['notification'];
     /* Indices that will be ignored by subsequent rules. */
     $ignore_ids = array();
     /* Only do filtering if:
        1. We have not done filtering before -or-
        2. The mailbox has changed -or-
        3. The rules have changed. */
     $cache = $api->getCache();
     if ($cache !== false && $cache == $change) {
         return;
     }
     $filters = Ingo_Storage_FilterIterator_Skip::create($this->_params['storage'], $this->_params['skip']);
     /* Parse through the rules, one-by-one. */
     foreach ($filters as $rule) {
         /* Check to make sure this is a valid rule and that the rule is
            not disabled. */
         if (!$this->_validRule($rule) || $rule->disable) {
             continue;
         }
         switch ($class = get_class($rule)) {
             case 'Ingo_Rule_System_Blacklist':
             case 'Ingo_Rule_System_Whitelist':
                 $addr = $rule->addresses;
                 $bl_folder = $class === 'Ingo_Rule_System_Blacklist' ? $rule->mailbox : null;
                 /* If list is empty, move on. */
                 if (empty($addr)) {
                     continue;
                 }
                 $addr = new Horde_Mail_Rfc822_List($addr);
                 $query = $this->_getQuery();
                 $or_ob = new Horde_Imap_Client_Search_Query();
                 foreach ($addr->bare_addresses as $val) {
                     $ob = new Horde_Imap_Client_Search_Query();
                     $ob->charset('UTF-8', false);
                     $ob->headerText('from', $val);
                     $or_ob->orSearch(array($ob));
                 }
                 $query->andSearch(array($or_ob));
                 $indices = $api->search($query);
                 if (!($msgs = $api->fetchEnvelope($indices))) {
                     continue;
                 }
                 /* Remove any indices that got in there by way of partial
                  * address match. */
                 $remove = array();
                 foreach ($msgs as $v) {
                     foreach ($v->getEnvelope()->from as $v2) {
                         if (!$addr->contains($v2)) {
                             $remove[] = $v->getUid();
                             break;
                         }
                     }
                 }
                 if ($remove) {
                     $indices = array_diff($indices, $remove);
                 }
                 if ($class === 'Ingo_Rule_System_Blacklist') {
                     $indices = array_diff($indices, $ignore_ids);
                     if (!empty($indices)) {
                         if (!empty($bl_folder)) {
                             $api->moveMessages($indices, $bl_folder);
                         } else {
                             $api->deleteMessages($indices);
                         }
                         $notification->push(sprintf(_("Filter activity: %s message(s) that matched the blacklist were deleted."), count($indices)), 'horde.message');
                     }
                 } else {
                     $ignore_ids = $indices;
                 }
                 break;
             case 'Ingo_Rule_User_Discard':
             case 'Ingo_Rule_User_Keep':
             case 'Ingo_Rule_User_Move':
             case 'Ingo_Rule_User_MoveKeep':
                 $base_query = $this->_getQuery();
                 $query = new Horde_Imap_Client_Search_Query();
                 foreach ($rule->conditions as $val) {
                     $ob = new Horde_Imap_Client_Search_Query();
                     if (!empty($val['type']) && $val['type'] == Ingo_Rule_User::TEST_SIZE) {
                         $ob->size($val['value'], $val['match'] == 'greater than');
                     } elseif (!empty($val['type']) && $val['type'] == Ingo_Rule_User::TEST_BODY) {
                         $ob->charset('UTF-8', false);
                         $ob->text($val['value'], true, $val['match'] == 'not contain');
                     } else {
                         if (strpos($val['field'], ',') == false) {
                             $ob->charset('UTF-8', false);
                             $ob->headerText($val['field'], $val['value'], $val['match'] == 'not contain');
                         } else {
                             foreach (explode(',', $val['field']) as $header) {
                                 $hdr_ob = new Horde_Imap_Client_Search_Query();
                                 $hdr_ob->charset('UTF-8', false);
                                 $hdr_ob->headerText($header, $val['value'], $val['match'] == 'not contain');
                                 if ($val['match'] == 'contains') {
                                     $ob->orSearch(array($hdr_ob));
                                 } elseif ($val['match'] == 'not contain') {
                                     $ob->andSearch(array($hdr_ob));
                                 }
                             }
                         }
                     }
                     switch ($rule->combine) {
                         case Ingo_Rule_User::COMBINE_ALL:
                             $query->andSearch(array($ob));
                             break;
                         case Ingo_Rule_User::COMBINE_ANY:
                             $query->orSearch(array($ob));
                             break;
                     }
                 }
                 $base_query->andSearch(array($query));
                 $indices = $api->search($base_query);
                 if ($indices = array_diff($indices, $ignore_ids)) {
                     if ($rule->stop) {
                         /* If the stop action is set, add these
                          * indices to the list of ids that will be
                          * ignored by subsequent rules. */
                         $ignore_ids = array_unique($indices + $ignore_ids);
                     }
                     /* Set the flags. */
                     if ($class !== 'Ingo_Rule_User_Discard') {
                         $flags = array();
                         if ($rule->flags & Ingo_Rule_User::FLAG_ANSWERED) {
                             $flags[] = '\\answered';
                         }
                         if ($rule->flags & Ingo_Rule_User::FLAG_DELETED) {
                             $flags[] = '\\deleted';
                         }
                         if ($rule->flags & Ingo_Rule_User::FLAG_FLAGGED) {
                             $flags[] = '\\flagged';
                         }
                         if ($rule->flags & Ingo_Rule_User::FLAG_SEEN) {
                             $flags[] = '\\seen';
                         }
                         if (!empty($flags)) {
                             $api->setMessageFlags($indices, $flags);
                         }
                     }
                     switch ($class) {
                         case 'Ingo_Rule_User_Keep':
                             /* Add these indices to the ignore list. */
                             $ignore_ids = array_unique($indices + $ignore_ids);
                             break;
                         case 'Ingo_Rule_User_Move':
                             /* We need to grab the envelope first. */
                             if ($this->_params['show_filter_msg'] && !($fetch = $api->fetchEnvelope($indices))) {
                                 continue 2;
                             }
                             $mbox = new Horde_Imap_Client_Mailbox($rule->value);
                             /* Move the messages to the requested mailbox. */
                             $api->moveMessages($indices, strval($mbox));
                             /* Display notification message(s). */
                             if ($this->_params['show_filter_msg']) {
                                 foreach ($fetch as $msg) {
                                     $envelope = $msg->getEnvelope();
                                     $notification->push(sprintf(_("Filter activity: The message \"%s\" from \"%s\" has been moved to the folder \"%s\"."), !empty($envelope->subject) ? Horde_Mime::decode($envelope->subject) : _("[No Subject]"), !empty($envelope->from) ? strval($envelope->from) : _("[No Sender]"), $mbox), 'horde.message');
                                 }
                             } else {
                                 $notification->push(sprintf(_("Filter activity: %s message(s) have been moved to the folder \"%s\"."), count($indices), $mbox), 'horde.message');
                             }
                             break;
                         case 'Ingo_Rule_User_Discard':
                             /* We need to grab the envelope first. */
                             if ($this->_params['show_filter_msg'] && !($fetch = $api->fetchEnvelope($indices))) {
                                 continue;
                             }
                             /* Delete the messages now. */
                             $api->deleteMessages($indices);
                             /* Display notification message(s). */
                             if ($this->_params['show_filter_msg']) {
                                 foreach ($fetch as $msg) {
                                     $envelope = $msg->getEnvelope();
                                     $notification->push(sprintf(_("Filter activity: The message \"%s\" from \"%s\" has been deleted."), !empty($envelope->subject) ? Horde_Mime::decode($envelope->subject) : _("[No Subject]"), !empty($envelope->from) ? strval($envelope->from) : _("[No Sender]")), 'horde.message');
                                 }
                             } else {
                                 $notification->push(sprintf(_("Filter activity: %s message(s) have been deleted."), count($indices)), 'horde.message');
                             }
                             break;
                         case 'Ingo_Rule_User_MoveKeep':
                             $mbox = new Horde_Imap_Client_Mailbox($rule->value);
                             /* Copy the messages to the requested mailbox. */
                             $api->copyMessages($indices, strval($mbox));
                             /* Display notification message(s). */
                             if ($this->_params['show_filter_msg']) {
                                 if (!($fetch = $api->fetchEnvelope($indices))) {
                                     continue;
                                 }
                                 foreach ($fetch as $msg) {
                                     $envelope = $msg->getEnvelope();
                                     $notification->push(sprintf(_("Filter activity: The message \"%s\" from \"%s\" has been copied to the folder \"%s\"."), !empty($envelope->subject) ? Horde_Mime::decode($envelope->subject) : _("[No Subject]"), !empty($envelope->from) ? strval($envelope->from) : _("[No Sender]"), $mbox), 'horde.message');
                                 }
                             } else {
                                 $notification->push(sprintf(_("Filter activity: %s message(s) have been copied to the folder \"%s\"."), count($indices), $mbox), 'horde.message');
                             }
                     }
                 }
                 break;
         }
     }
     /* Set cache flag. */
     $api->storeCache($change);
 }
Example #5
0
 /**
  * Generates the procmail scripts to do the filtering specified in the
  * rules.
  */
 protected function _generate()
 {
     $this->_addItem(Ingo::RULE_ALL, new Ingo_Script_Procmail_Comment(_("procmail script generated by Ingo") . ' (' . date('F j, Y, g:i a') . ')'));
     if (isset($this->_params['forward_file']) && isset($this->_params['forward_string'])) {
         $this->_addItem(Ingo::RULE_ALL, new Ingo_Script_String($this->_params['forward_string']), $this->_params['forward_file']);
     }
     /* Add variable information, if present. */
     if (!empty($this->_params['variables']) && is_array($this->_params['variables'])) {
         foreach ($this->_params['variables'] as $key => $val) {
             $this->_addItem(Ingo::RULE_ALL, new Ingo_Script_Procmail_Variable(array('name' => $key, 'value' => $val)));
         }
     }
     $filters = Ingo_Storage_FilterIterator_Skip::create($this->_params['storage'], $this->_params['skip']);
     foreach ($filters as $rule) {
         switch ($class = get_class($rule)) {
             case 'Ingo_Rule_System_Blacklist':
                 $this->_generateBlacklist($rule);
                 break;
             case 'Ingo_Rule_System_Forward':
                 $this->_generateForward($rule);
                 break;
             case 'Ingo_Rule_System_Vacation':
                 $this->_generateVacation($rule);
                 break;
             case 'Ingo_Rule_System_Whitelist':
                 $this->_generateWhitelist($rule);
                 break;
             default:
                 if (!in_array($class, $this->_actions)) {
                     break;
                 }
                 /* Create filter if using AND. */
                 switch ($rule->combine) {
                     case Ingo_Rule_User::COMBINE_ALL:
                         $recipe = new Ingo_Script_Procmail_Recipe(array('action' => $class, 'action-value' => $rule->value, 'disable' => $rule->disable), $this->_params);
                         if (!$rule->stop) {
                             $recipe->addFlag('c');
                         }
                         foreach ($rule->conditions as $condition) {
                             $recipe->addCondition($condition);
                         }
                         $this->_addItem(Ingo::RULE_FILTER, new Ingo_Script_Procmail_Comment($rule->name, $rule->disable, true));
                         $this->_addItem(Ingo::RULE_FILTER, $recipe);
                         break;
                     case Ingo_Rule_User::COMBINE_ANY:
                         /* Create filter if using OR */
                         $this->_addItem(Ingo::RULE_FILTER, new Ingo_Script_Procmail_Comment($rule->name, $rule->disable, true));
                         $loop = 0;
                         foreach ($rule->conditions as $condition) {
                             $recipe = new Ingo_Script_Procmail_Recipe(array('action' => $class, 'action-value' => $rule->value, 'disable' => $rule->disable), $this->_params);
                             if ($loop++) {
                                 $recipe->addFlag('E');
                             }
                             if (!$rule->stop) {
                                 $recipe->addFlag('c');
                             }
                             $recipe->addCondition($condition);
                             $this->_addItem(Ingo::RULE_FILTER, $recipe);
                         }
                         break;
                 }
         }
     }
     // If an external delivery program is used, add final rule
     // to deliver to $DEFAULT
     if (isset($this->_params['delivery_agent'])) {
         $this->_addItem(Ingo::RULE_FILTER, new Ingo_Script_Procmail_Default($this->_params));
     }
 }
Example #6
0
 /**
  * Generates the Sieve script to do the filtering specified in the rules.
  */
 protected function _generate()
 {
     $this->_addItem(Ingo::RULE_ALL, new Ingo_Script_Sieve_Comment("Sieve Filter\n\n" . _("Generated by Ingo") . " (http://www.horde.org/apps/ingo/)\n" . trim(strftime($this->_params['date_format'] . ', ' . $this->_params['time_format']))));
     $filters = Ingo_Storage_FilterIterator_Skip::create($this->_params['storage'], $this->_params['skip']);
     foreach ($filters as $rule) {
         /* Check to make sure this is a valid rule and that the rule
            is not disabled. */
         if ($rule->disable || !$this->_validRule($rule)) {
             continue;
         }
         $action = array();
         switch (get_class($rule)) {
             case 'Ingo_Rule_User_Keep':
                 if ($rule->has_flags) {
                     $action[] = new Ingo_Script_Sieve_Action_Addflag(array('flags' => $rule->flags, 'imapflags' => !empty($this->_params['imapflags'])));
                 }
                 $action[] = new Ingo_Script_Sieve_Action_Keep();
                 if ($rule->has_flags) {
                     $action[] = new Ingo_Script_Sieve_Action_Removeflag(array('flags' => $rule->flags, 'imapflags' => !empty($this->_params['imapflags'])));
                 }
                 break;
             case 'Ingo_Rule_User_Discard':
                 $action[] = new Ingo_Script_Sieve_Action_Discard();
                 break;
             case 'Ingo_Rule_User_Move':
                 if ($rule->has_flags) {
                     $action[] = new Ingo_Script_Sieve_Action_Addflag(array('flags' => $rule->flags, 'imapflags' => !empty($this->_params['imapflags'])));
                 }
                 $action[] = new Ingo_Script_Sieve_Action_Fileinto(array_merge($this->_params, array('folder' => $rule->value)));
                 if ($rule->has_flags) {
                     $action[] = new Ingo_Script_Sieve_Action_Removeflag(array('flags' => $rule->flags, 'imapflags' => !empty($this->_params['imapflags'])));
                 }
                 break;
             case 'Ingo_Rule_User_Reject':
                 $action[] = new Ingo_Script_Sieve_Action_Reject(array('reason' => $rule->value));
                 break;
             case 'Ingo_Rule_User_Redirect':
                 $parser = new Horde_Mail_Rfc822();
                 foreach ($parser->parseAddressList($rule->value) as $address) {
                     $action[] = new Ingo_Script_Sieve_Action_Redirect(array('address' => $address));
                 }
                 break;
             case 'Ingo_Rule_User_RedirectKeep':
                 if ($rule->has_flags) {
                     $action[] = new Ingo_Script_Sieve_Action_Addflag(array('flags' => $rule->flags, 'imapflags' => !empty($this->_params['imapflags'])));
                 }
                 $parser = new Horde_Mail_Rfc822();
                 foreach ($parser->parseAddressList($rule->value) as $address) {
                     $action[] = new Ingo_Script_Sieve_Action_Redirect(array('address' => $address));
                 }
                 $action[] = new Ingo_Script_Sieve_Action_Keep();
                 if ($rule->has_flags) {
                     $action[] = new Ingo_Script_Sieve_Action_Removeflag(array('flags' => $rule->flags, 'imapflags' => !empty($this->_params['imapflags'])));
                 }
                 break;
             case 'Ingo_Rule_User_MoveKeep':
                 if ($rule->has_flags) {
                     $action[] = new Ingo_Script_Sieve_Action_Addflag(array('flags' => $rule->flags, 'imapflags' => !empty($this->_params['imapflags'])));
                 }
                 $action[] = new Ingo_Script_Sieve_Action_Keep();
                 $action[] = new Ingo_Script_Sieve_Action_Fileinto(array_merge($this->_params, array('folder' => $rule->value)));
                 if ($rule->has_flags) {
                     $action[] = new Ingo_Script_Sieve_Action_Removeflag(array('flags' => $rule->flags, 'imapflags' => !empty($this->_params['imapflags'])));
                 }
                 break;
             case 'Ingo_Rule_User_FlagOnly':
                 if ($rule->has_flags) {
                     $action[] = new Ingo_Script_Sieve_Action_Addflag(array('flags' => $rule->flags, 'imapflags' => !empty($this->_params['imapflags'])));
                 }
                 break;
             case 'Ingo_Rule_User_Notify':
                 $action[] = new Ingo_Script_Sieve_Action_Notify(array('address' => $rule->value, 'name' => $rule->name, 'notify' => !empty($this->_params['notify'])));
                 break;
             case 'Ingo_Rule_System_Whitelist':
                 $this->_addWhitelistBlocks($rule);
                 continue 2;
             case 'Ingo_Rule_System_Blacklist':
                 $this->_addBlacklistBlocks($rule);
                 continue 2;
             case 'Ingo_Rule_System_Vacation':
                 $this->_addVacationBlocks($rule);
                 continue 2;
             case 'Ingo_Rule_System_Forward':
                 $this->_addForwardBlocks($rule);
                 continue 2;
             case 'Ingo_Rule_System_Spam':
                 $this->_addSpamBlocks($rule);
                 continue 2;
         }
         $this->_addItem(Ingo::RULE_FILTER, new Ingo_Script_Sieve_Comment($rule->name));
         if ($rule->stop) {
             $action[] = new Ingo_Script_Sieve_Action_Stop();
         }
         $test = $rule->combine == Ingo_Rule_User::COMBINE_ANY ? new Ingo_Script_Sieve_Test_Anyof() : new Ingo_Script_Sieve_Test_Allof();
         foreach ($rule->conditions as $condition) {
             $tmp = '';
             switch ($condition['match']) {
                 case 'equal':
                     $tmp = new Ingo_Script_Sieve_Test_Relational(array('comparison' => 'eq', 'headers' => $condition['field'], 'value' => $condition['value']));
                     $test->addTest($tmp);
                     break;
                 case 'not equal':
                     $tmp = new Ingo_Script_Sieve_Test_Relational(array('comparison' => 'ne', 'headers' => $condition['field'], 'value' => $condition['value']));
                     $test->addTest($tmp);
                     break;
                 case 'less than':
                     if ($condition['field'] == 'Size') {
                         /* Message Size Test. */
                         $tmp = new Ingo_Script_Sieve_Test_Size(array('comparison' => ':under', 'size' => $condition['value']));
                     } else {
                         /* Relational Test. */
                         $tmp = new Ingo_Script_Sieve_Test_Relational(array('comparison' => 'lt', 'headers' => $condition['field'], 'value' => $condition['value']));
                     }
                     $test->addTest($tmp);
                     break;
                 case 'less than or equal to':
                     $tmp = new Ingo_Script_Sieve_Test_Relational(array('comparison' => 'le', 'headers' => $condition['field'], 'value' => $condition['value']));
                     $test->addTest($tmp);
                     break;
                 case 'greater than':
                     if ($condition['field'] == 'Size') {
                         /* Message Size Test. */
                         $tmp = new Ingo_Script_Sieve_Test_Size(array('comparison' => ':over', 'size' => $condition['value']));
                     } else {
                         /* Relational Test. */
                         $tmp = new Ingo_Script_Sieve_Test_Relational(array('comparison' => 'gt', 'headers' => $condition['field'], 'value' => $condition['value']));
                     }
                     $test->addTest($tmp);
                     break;
                 case 'greater than or equal to':
                     $tmp = new Ingo_Script_Sieve_Test_Relational(array('comparison' => 'ge', 'headers' => $condition['field'], 'value' => $condition['value']));
                     $test->addTest($tmp);
                     break;
                 case 'exists':
                     $tmp = new Ingo_Script_Sieve_Test_Exists(array('headers' => $condition['field']));
                     $test->addTest($tmp);
                     break;
                 case 'not exist':
                     $tmp = new Ingo_Script_Sieve_Test_Exists(array('headers' => $condition['field']));
                     $test->addTest(new Ingo_Script_Sieve_Test_Not($tmp));
                     break;
                 case 'contains':
                 case 'not contain':
                 case 'is':
                 case 'not is':
                 case 'begins with':
                 case 'not begins with':
                 case 'ends with':
                 case 'not ends with':
                 case 'regex':
                 case 'not regex':
                 case 'matches':
                 case 'not matches':
                     $comparator = isset($condition['case']) && $condition['case'] ? 'i;octet' : 'i;ascii-casemap';
                     $vals = array('headers' => preg_replace('/(.)(?<!\\\\)\\,(.)/', "\$1\n\$2", $condition['field']), 'comparator' => $comparator);
                     $use_address_test = false;
                     if ($condition['match'] != 'regex') {
                         $condition['value'] = preg_replace('/(.)(?<!\\\\)\\,(.)/', "\$1\n\$2", $condition['value']);
                     }
                     /* Do 'smarter' searching for fields where we know we have
                      * e-mail addresses. */
                     if (preg_match('/^(From|To|Cc|Bcc)/', $condition['field'])) {
                         $vals['addresses'] = $condition['value'];
                         $use_address_test = true;
                     } else {
                         $vals['strings'] = $condition['value'];
                     }
                     switch ($condition['match']) {
                         case 'contains':
                             $vals['match-type'] = ':contains';
                             if ($use_address_test) {
                                 $tmp = new Ingo_Script_Sieve_Test_Address($vals);
                             } elseif ($condition['field'] == 'Body') {
                                 $tmp = new Ingo_Script_Sieve_Test_Body($vals);
                             } else {
                                 $tmp = new Ingo_Script_Sieve_Test_Header($vals);
                             }
                             $test->addTest($tmp);
                             break;
                         case 'not contain':
                             $vals['match-type'] = ':contains';
                             if ($use_address_test) {
                                 $tmp = new Ingo_Script_Sieve_Test_Address($vals);
                             } elseif ($condition['field'] == 'Body') {
                                 $tmp = new Ingo_Script_Sieve_Test_Body($vals);
                             } else {
                                 $tmp = new Ingo_Script_Sieve_Test_Header($vals);
                             }
                             $test->addTest(new Ingo_Script_Sieve_Test_Not($tmp));
                             break;
                         case 'is':
                             $vals['match-type'] = ':is';
                             if ($use_address_test) {
                                 $tmp = new Ingo_Script_Sieve_Test_Address($vals);
                             } elseif ($condition['field'] == 'Body') {
                                 $tmp = new Ingo_Script_Sieve_Test_Body($vals);
                             } else {
                                 $tmp = new Ingo_Script_Sieve_Test_Header($vals);
                             }
                             $test->addTest($tmp);
                             break;
                         case 'not is':
                             $vals['match-type'] = ':is';
                             if ($use_address_test) {
                                 $tmp = new Ingo_Script_Sieve_Test_Address($vals);
                             } elseif ($condition['field'] == 'Body') {
                                 $tmp = new Ingo_Script_Sieve_Test_Body($vals);
                             } else {
                                 $tmp = new Ingo_Script_Sieve_Test_Header($vals);
                             }
                             $test->addTest(new Ingo_Script_Sieve_Test_Not($tmp));
                             break;
                         case 'begins with':
                             $vals['match-type'] = ':matches';
                             if ($use_address_test) {
                                 $add_arr = preg_split('(\\r\\n|\\n|\\r)', $vals['addresses']);
                                 if (count($add_arr) > 1) {
                                     foreach ($add_arr as $k => $v) {
                                         $add_arr[$k] = $v . '*';
                                     }
                                     $vals['addresses'] = implode("\r\n", $add_arr);
                                 } else {
                                     $vals['addresses'] .= '*';
                                 }
                                 $tmp = new Ingo_Script_Sieve_Test_Address($vals);
                             } else {
                                 $add_arr = preg_split('(\\r\\n|\\n|\\r)', $vals['strings']);
                                 if (count($add_arr) > 1) {
                                     foreach ($add_arr as $k => $v) {
                                         $add_arr[$k] = $v . '*';
                                     }
                                     $vals['strings'] = implode("\r\n", $add_arr);
                                 } else {
                                     $vals['strings'] .= '*';
                                 }
                                 if ($condition['field'] == 'Body') {
                                     $tmp = new Ingo_Script_Sieve_Test_Body($vals);
                                 } else {
                                     $tmp = new Ingo_Script_Sieve_Test_Header($vals);
                                 }
                             }
                             $test->addTest($tmp);
                             break;
                         case 'not begins with':
                             $vals['match-type'] = ':matches';
                             if ($use_address_test) {
                                 $add_arr = preg_split('(\\r\\n|\\n|\\r)', $vals['addresses']);
                                 if (count($add_arr) > 1) {
                                     foreach ($add_arr as $k => $v) {
                                         $add_arr[$k] = $v . '*';
                                     }
                                     $vals['addresses'] = implode("\r\n", $add_arr);
                                 } else {
                                     $vals['addresses'] .= '*';
                                 }
                                 $tmp = new Ingo_Script_Sieve_Test_Address($vals);
                             } else {
                                 $add_arr = preg_split('(\\r\\n|\\n|\\r)', $vals['strings']);
                                 if (count($add_arr) > 1) {
                                     foreach ($add_arr as $k => $v) {
                                         $add_arr[$k] = $v . '*';
                                     }
                                     $vals['strings'] = implode("\r\n", $add_arr);
                                 } else {
                                     $vals['strings'] .= '*';
                                 }
                                 if ($condition['field'] == 'Body') {
                                     $tmp = new Ingo_Script_Sieve_Test_Body($vals);
                                 } else {
                                     $tmp = new Ingo_Script_Sieve_Test_Header($vals);
                                 }
                             }
                             $test->addTest(new Ingo_Script_Sieve_Test_Not($tmp));
                             break;
                         case 'ends with':
                             $vals['match-type'] = ':matches';
                             if ($use_address_test) {
                                 $add_arr = preg_split('(\\r\\n|\\n|\\r)', $vals['addresses']);
                                 if (count($add_arr) > 1) {
                                     foreach ($add_arr as $k => $v) {
                                         $add_arr[$k] = '*' . $v;
                                     }
                                     $vals['addresses'] = implode("\r\n", $add_arr);
                                 } else {
                                     $vals['addresses'] = '*' . $vals['addresses'];
                                 }
                                 $tmp = new Ingo_Script_Sieve_Test_Address($vals);
                             } else {
                                 $add_arr = preg_split('(\\r\\n|\\n|\\r)', $vals['strings']);
                                 if (count($add_arr) > 1) {
                                     foreach ($add_arr as $k => $v) {
                                         $add_arr[$k] = '*' . $v;
                                     }
                                     $vals['strings'] = implode("\r\n", $add_arr);
                                 } else {
                                     $vals['strings'] = '*' . $vals['strings'];
                                 }
                                 if ($condition['field'] == 'Body') {
                                     $tmp = new Ingo_Script_Sieve_Test_Body($vals);
                                 } else {
                                     $tmp = new Ingo_Script_Sieve_Test_Header($vals);
                                 }
                             }
                             $test->addTest($tmp);
                             break;
                         case 'not ends with':
                             $vals['match-type'] = ':matches';
                             if ($use_address_test) {
                                 $add_arr = preg_split('(\\r\\n|\\n|\\r)', $vals['addresses']);
                                 if (count($add_arr) > 1) {
                                     foreach ($add_arr as $k => $v) {
                                         $add_arr[$k] = '*' . $v;
                                     }
                                     $vals['addresses'] = implode("\r\n", $add_arr);
                                 } else {
                                     $vals['addresses'] = '*' . $vals['addresses'];
                                 }
                                 $tmp = new Ingo_Script_Sieve_Test_Address($vals);
                             } else {
                                 $add_arr = preg_split('(\\r\\n|\\n|\\r)', $vals['strings']);
                                 if (count($add_arr) > 1) {
                                     foreach ($add_arr as $k => $v) {
                                         $add_arr[$k] = '*' . $v;
                                     }
                                     $vals['strings'] = implode("\r\n", $add_arr);
                                 } else {
                                     $vals['strings'] = '*' . $vals['strings'];
                                 }
                                 if ($condition['field'] == 'Body') {
                                     $tmp = new Ingo_Script_Sieve_Test_Body($vals);
                                 } else {
                                     $tmp = new Ingo_Script_Sieve_Test_Header($vals);
                                 }
                             }
                             $test->addTest(new Ingo_Script_Sieve_Test_Not($tmp));
                             break;
                         case 'regex':
                         case 'not regex':
                             $vals['match-type'] = ':regex';
                             if ($use_address_test) {
                                 $tmp = new Ingo_Script_Sieve_Test_Address($vals);
                             } elseif ($condition['field'] == 'Body') {
                                 $tmp = new Ingo_Script_Sieve_Test_Body($vals);
                             } else {
                                 $tmp = new Ingo_Script_Sieve_Test_Header($vals);
                             }
                             if ($condition['match'] == 'not regex') {
                                 $tmp = new Ingo_Script_Sieve_Test_Not($tmp);
                             }
                             $test->addTest($tmp);
                             break;
                         case 'matches':
                             $vals['match-type'] = ':matches';
                             if ($use_address_test) {
                                 $tmp = new Ingo_Script_Sieve_Test_Address($vals);
                             } elseif ($condition['field'] == 'Body') {
                                 $tmp = new Ingo_Script_Sieve_Test_Body($vals);
                             } else {
                                 $tmp = new Ingo_Script_Sieve_Test_Header($vals);
                             }
                             $test->addTest($tmp);
                             break;
                         case 'not matches':
                             $vals['match-type'] = ':matches';
                             if ($use_address_test) {
                                 $tmp = new Ingo_Script_Sieve_Test_Address($vals);
                             } elseif ($condition['field'] == 'Body') {
                                 $tmp = new Ingo_Script_Sieve_Test_Body($vals);
                             } else {
                                 $tmp = new Ingo_Script_Sieve_Test_Header($vals);
                             }
                             $test->addTest(new Ingo_Script_Sieve_Test_Not($tmp));
                             break;
                     }
             }
         }
         $if = new Ingo_Script_Sieve_If($test);
         $if->setActions($action);
         $this->_addItem(Ingo::RULE_FILTER, $if);
     }
     /* Add blocks that have to go to the end. */
     foreach ($this->_endBlocks as $block) {
         $this->_addItem(Ingo::RULE_FILTER, $block);
     }
 }