fixed filters so they can be applied to only new mail or to all mail. tested
authorjmunro <jmunro@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Fri, 26 Apr 2002 19:52:15 +0000 (19:52 +0000)
committerjmunro <jmunro@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Fri, 26 Apr 2002 19:52:15 +0000 (19:52 +0000)
with Cyrus, Courier, and UW on debian. Also fixed some E_ALL warnings and
changed the plugin to use sqimap_session_id() instead of hardcoded 'A3999'

git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@2761 7612ce4b-ef26-0410-bec9-ea0150e637f0

plugins/filters/filters.php
plugins/filters/options.php

index 2c5c880..581adb4 100644 (file)
@@ -89,14 +89,6 @@ function filters_bulkquery($filters_spam_scan, $filters, $read) {
         $MsgNum = $Chunks[1];
 
         $i ++;
-        $Scan = 1;
-
-        // Check for normal IMAP servers
-        if ($filters_spam_scan == 'new') {
-            if (is_int(strpos($Chunks[4], '\Seen'))) {
-                $Scan = 0;
-            }
-        }
 
         // Look through all of the Received headers for IP addresses
         // Stop when I get ")" on a line
@@ -108,32 +100,28 @@ function filters_bulkquery($filters_spam_scan, $filters, $read) {
             // Check to see if this line is the right "Received from" line
             // to check
             if (is_int(strpos($read[$i], $SpamFilters_YourHop))) {
-
-                // short-circuit and skip work if we don't scan this one
-                if ($Scan) {
-                    $read[$i] = ereg_replace('[^0-9\.]', ' ', $read[$i]);
-                    $elements = explode(' ', $read[$i]);
-                    foreach ($elements as $value) {
-                        if ($value != '' &&
-                            ereg('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}',
-                                $value, $regs)) {
-                            $Chunks = explode('.', $value);
-                            $IP = $Chunks[3] . '.' . $Chunks[2] . '.' .
-                                  $Chunks[1] . '.' . $Chunks[0];
-                            foreach ($filters as $key => $value) {
-                                if ($filters[$key]['enabled'] &&
-                                          $filters[$key]['dns']) {
-                                    if (strlen($SpamFilters_DNScache[$IP.'.'.$filters[$key]['dns']]) == 0) {
-                                       $IPs[$IP] = true;
-                                       break;
-                                    }
+                $read[$i] = ereg_replace('[^0-9\.]', ' ', $read[$i]);
+                $elements = explode(' ', $read[$i]);
+                foreach ($elements as $value) {
+                    if ($value != '' &&
+                        ereg('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}',
+                            $value, $regs)) {
+                        $Chunks = explode('.', $value);
+                        $IP = $Chunks[3] . '.' . $Chunks[2] . '.' .
+                              $Chunks[1] . '.' . $Chunks[0];
+                        foreach ($filters as $key => $value) {
+                            if ($filters[$key]['enabled'] &&
+                                      $filters[$key]['dns']) {
+                                if (strlen($SpamFilters_DNScache[$IP.'.'.$filters[$key]['dns']]) == 0) {
+                                   $IPs[$IP] = true;
+                                   break;
                                 }
                             }
-                            // If we've checked one IP and YourHop is
-                            // just a space
-                            if ($SpamFilters_YourHop == ' ') {
-                                break;  // don't check any more
-                            }
+                        }
+                        // If we've checked one IP and YourHop is
+                        // just a space
+                        if ($SpamFilters_YourHop == ' ') {
+                            break;  // don't check any more
                         }
                     }
                 }
@@ -295,8 +283,10 @@ function start_filters() {
 
 
 function user_filters($imap_stream) {
+    global $data_dir, $username;
     $filters = load_filters();
     if (! $filters) return;
+    $filters_user_scan = getPref($data_dir, $username, 'filters_user_scan');
 
     sqimap_mailbox_select($imap_stream, 'INBOX');
 
@@ -309,16 +299,16 @@ function user_filters($imap_stream) {
             *  and the other for CC.
             */
             filter_search_and_delete($imap_stream, 'TO',
-            $filters[$i]['what'], $filters[$i]['folder']);
+            $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan);
             filter_search_and_delete($imap_stream, 'CC',
-            $filters[$i]['what'], $filters[$i]['folder']);
+            $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan);
         } else {
             /*
             *  If it's a normal TO, CC, SUBJECT, or FROM, then handle it
             *  normally.
             */
             filter_search_and_delete($imap_stream, $filters[$i]['where'],
-            $filters[$i]['what'], $filters[$i]['folder']);
+            $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan);
         }
     }
     // Clean out the mailbox whether or not auto_expunge is on
@@ -326,15 +316,21 @@ function user_filters($imap_stream) {
     sqimap_mailbox_expunge($imap_stream, 'INBOX');
 }
 
-function filter_search_and_delete($imap, $where, $what, $where_to) {
+function filter_search_and_delete($imap, $where, $what, $where_to, $user_scan) {
     global $languages, $squirrelmail_language;
+    if ($user_scan == 'new') {
+        $category = 'UNSEEN';
+    } else {
+        $category = 'ALL';
+    }
+
     if (isset($languages[$squirrelmail_language]['CHARSET']) &&
         $languages[$squirrelmail_language]['CHARSET']) {
         $search_str = "SEARCH CHARSET "
             . strtoupper($languages[$squirrelmail_language]['CHARSET']) 
-            . " ALL ";
+            . ' ' . $category . ' ';
     } else {
-        $search_str = "SEARCH CHARSET US-ASCII ALL ";
+        $search_str = 'SEARCH CHARSET US-ASCII ' . $category . ' ';
     }
     if ($where == "Header") {
        $what = explode(':', $what);
@@ -395,12 +391,38 @@ function spam_filters($imap_stream) {
 
     // Ask for a big list of all "Received" headers in the inbox with
     // flags for each message.  Kinda big.
-    fputs($imap_stream, 'A3999 FETCH 1:* (FLAGS BODY.PEEK[HEADER.FIELDS ' .
-        "(RECEIVED)])\r\n");
-
-    $read = filters_sqimap_read_data ($imap_stream, 'A3999', true,
+    //fputs($imap_stream, 'A3999 FETCH 1:* (FLAGS BODY.PEEK[HEADER.FIELDS ' .
+    //    "(RECEIVED)])\r\n");
+    $sid = sqimap_session_id();
+    if ($filters_spam_scan != 'new') {
+        fputs($imap_stream, $sid.' FETCH 1:* (FLAGS BODY.PEEK[HEADER.FIELDS ' .
+            "(RECEIVED)])\r\n");
+    } else {
+        fputs ($imap_stream, $sid.' SEARCH UNSEEN' . "\r\n");
+        $read = filters_sqimap_read_data ($imap_stream, $sid, true,
+                                          $response, $message);
+        $sid = sqimap_session_id();
+        if ($response != 'OK' || trim($read[0]) == '* SEARCH') {
+            fputs($imap_stream,
+                  $sid.' FETCH 1:* (FLAGS BODY.PEEK[HEADER.FIELDS ' .
+                     "(RECEIVED)])\r\n");
+        } else {
+           $read[0] = trim($read[0]);
+            $i = 0;
+            $imap_query = $sid.' FETCH ';
+            $Chunks = explode(' ', $read[0]);
+            for ($i=2; $i < (count($Chunks)-1) ; $i++) {
+                $imap_query .= $Chunks[$i].',';
+            }
+            $imap_query .= $Chunks[count($Chunks)-1];
+            $imap_query .= ' (FLAGS BODY.PEEK[HEADER.FIELDS ';
+            $imap_query .= "(RECEIVED)])\r\n";
+            fputs($imap_stream, $imap_query);
+        }
+    }
+    
+    $read = filters_sqimap_read_data ($imap_stream, $sid, true,
                                     $response, $message);
-
     if ($response != 'OK') {
         return;
     }
@@ -422,14 +444,6 @@ function spam_filters($imap_stream) {
         $IPs = array();
         $i ++;
         $IsSpam = 0;
-        $Scan = 1;
-
-        // Check for normal IMAP servers
-        if ($filters_spam_scan == 'new') {
-            if (is_int(strpos($Chunks[4], '\Seen'))) {
-                $Scan = 0;
-            }
-        }
 
         // Look through all of the Received headers for IP addresses
         // Stop when I get ")" on a line
@@ -443,25 +457,23 @@ function spam_filters($imap_stream) {
             if (is_int(strpos($read[$i], $SpamFilters_YourHop))) {
 
                 // short-circuit and skip work if we don't scan this one
-                if ($Scan) {
-                    $read[$i] = ereg_replace('[^0-9\.]', ' ', $read[$i]);
-                    $elements = explode(' ', $read[$i]);
-                    foreach ($elements as $value) {
-                        if ($value != '' &&
-                            ereg('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}',
-                                $value, $regs)) {
-                            $Chunks = explode('.', $value);
-                            if (filters_spam_check_site($Chunks[0],
-                                    $Chunks[1], $Chunks[2], $Chunks[3],
-                                    $filters)) {
-                                $IsSpam ++;
-                                break;  // no sense in checking more IPs
-                            }
-                            // If we've checked one IP and YourHop is
-                            // just a space
-                            if ($SpamFilters_YourHop == ' ') {
-                                break;  // don't check any more
-                            }
+                $read[$i] = ereg_replace('[^0-9\.]', ' ', $read[$i]);
+                $elements = explode(' ', $read[$i]);
+                foreach ($elements as $value) {
+                    if ($value != '' &&
+                        ereg('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}',
+                            $value, $regs)) {
+                        $Chunks = explode('.', $value);
+                        if (filters_spam_check_site($Chunks[0],
+                                $Chunks[1], $Chunks[2], $Chunks[3],
+                                $filters)) {
+                            $IsSpam ++;
+                            break;  // no sense in checking more IPs
+                        }
+                        // If we've checked one IP and YourHop is
+                        // just a space
+                        if ($SpamFilters_YourHop == ' ') {
+                            break;  // don't check any more
                         }
                     }
                 }
@@ -500,15 +512,17 @@ function filters_spam_check_site($a, $b, $c, $d, &$filters) {
             if ($filters[$key]['dns']) {
                 $filter_revip = $d . '.' . $c . '.' . $b . '.' . $a . '.' .
                                 $filters[$key]['dns'];
-                if (strlen($SpamFilters_DNScache[$filter_revip]['L']) == 0) {
-                    $SpamFilters_DNScache[$filter_revip]['L'] =
+                if (isset($SpamFilters_DNScache[$filter_revip])) {
+                    if (strlen($SpamFilters_DNScache[$filter_revip]['L']) == 0) {
+                        $SpamFilters_DNScache[$filter_revip]['L'] =
                                         gethostbyname($filter_revip);
-                    $SpamFilters_DNScache[$filter_revip]['T'] =
+                        $SpamFilters_DNScache[$filter_revip]['T'] =
                                            time() + $SpamFilters_CacheTTL;
-                }
-                if ($SpamFilters_DNScache[$filter_revip]['L'] ==
-                    $filters[$key]['result']) {
-                    return 1;
+                    }
+                    if ($SpamFilters_DNScache[$filter_revip]['L'] ==
+                        $filters[$key]['result']) {
+                        return 1;
+                    }
                 }
             }
         }
@@ -518,6 +532,7 @@ function filters_spam_check_site($a, $b, $c, $d, &$filters) {
 
 function load_filters() {
     global $data_dir, $username;
+
     $filters = array();
     for ($i=0; $fltr = getPref($data_dir, $username, 'filter' . $i); $i++) {
         $ary = explode(',', $fltr);
index ec836f6..77a62fb 100644 (file)
       filter_swap($theid, $theid - 1);
    } elseif (isset($action) && $action == 'move_down') {
       filter_swap($theid, $theid + 1);
+   } elseif (isset($user_submit)) {
+       echo "<br><center><b>"._("Saved Scan type")."</b></center>\n";
+       setPref($data_dir, $username, 'filters_user_scan', $filters_user_scan_set);
    }
 
    $filters = load_filters();
+   $filters_user_scan = getPref($data_dir, $username, 'filters_user_scan');
 
    echo '<br>' .
         '<table width=95% align=center border=0 cellpadding=2 cellspacing=0>'.
         "<tr><td bgcolor=\"$color[0]\">".
         '<center><b>' . _("Options") . ' -  ' . _("Message Filtering") . '</b></center>'.
         '</td></tr></table>'.
-        '<br><center>[<a href="options.php?action=add">' . _("New") .
+
+        '<br><form method=post action="options.php">'.
+        '<center>'.
+        '<table cellpadding=2 cellspacing=0 border=0>'.
+        '<tr>'.
+            '<th align=right nowrap>' . _("What to Scan:") . '</th>'.
+            '<td><select name="filters_user_scan_set">'.
+            '<option value=""';
+    if ($filters_user_scan == '') {
+        echo ' SELECTED';
+    }
+    echo '>' . _("All messages") . '</option>'.
+            '<option value="new"';
+    if ($filters_user_scan == 'new') {
+        echo ' SELECTED';
+    }
+    echo '>' . _("Only unread messages") . '</option>' .
+            '</select>'.
+        '</td>'.
+        '<td><input type=submit name="user_submit" value="' . _("Save") . '"></td></tr>'.
+        '</table>'.
+        '</center>'.
+        '</form>'.
+
+        '<center>[<a href="options.php?action=add">' . _("New") .
         '</a>] - [<a href="../../src/options.php">' . _("Done") . '</a>]</center><br>';
 
     if (isset($action) && ($action == 'add' || $action == 'edit')) {