Trimming whitespace and replacing tabs
[squirrelmail.git] / plugins / filters / filters.php
index 74729cd871929ef8cc82b7cb5e96c41566cf2f66..c326a1f5a7942b1f2cc85f5e044b507455c21d9e 100644 (file)
@@ -2,22 +2,6 @@
 /**
  * Message and Spam Filter Plugin - Filtering Functions
  *
- * This plugin filters your inbox into different folders based upon given
- * criteria.  It is most useful for people who are subscibed to mailing lists
- * to help organize their messages.  The argument stands that filtering is
- * not the place of the client, which is why this has been made a plugin for
- * SquirrelMail.  You may be better off using products such as Sieve or
- * Procmail to do your filtering so it happens even when SquirrelMail isn't
- * running.
- *
- * If you need help with this, or see improvements that can be made, please
- * email me directly at the address above.  I definately welcome suggestions
- * and comments.  This plugin, as is the case with all SquirrelMail plugins,
- * is not directly supported by the developers.  Please come to me off the
- * mailing list if you have trouble with it.
- *
- * Also view plugins/README.plugins for more information.
- *
  * @version $Id$
  * @copyright (c) 1999-2005 The SquirrelMail Project Team
  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
@@ -25,8 +9,60 @@
  * @subpackage filters
  */
 
+/** load config */
+if (file_exists(SM_PATH . 'plugins/filters/config.php'))
+    include_once (SM_PATH . 'plugins/filters/config.php');
+
+/**
+ * Init Hooks
+ * @access private
+ */
+function filters_init_hooks () {
+    global $squirrelmail_plugin_hooks;
+    if (!file_exists(SM_PATH . 'plugins/filters/config.php')) return;
+    if (sqgetGlobalVar('mailbox',$mailbox,SQ_FORM)) {
+        sqgetGlobalVar('mailbox',$mailbox,SQ_FORM);
+    } else {
+        $mailbox = 'INBOX';
+    }
+
+    $squirrelmail_plugin_hooks['left_main_before']['filters'] = 'start_filters_hook';
+    if (isset($mailbox) && $mailbox == 'INBOX') {
+        $squirrelmail_plugin_hooks['right_main_after_header']['filters'] = 'start_filters_hook';
+    }
+    $squirrelmail_plugin_hooks['optpage_register_block']['filters'] = 'filters_optpage_register_block_hook';
+    $squirrelmail_plugin_hooks['special_mailbox']['filters'] = 'filters_special_mailbox';
+    $squirrelmail_plugin_hooks['rename_or_delete_folder']['filters'] = 'update_for_folder_hook';
+    $squirrelmail_plugin_hooks['webmail_bottom']['filters'] = 'start_filters_hook';
+}
+
+/**
+ * Register option blocks
+ * @access private
+ */
+function filters_optpage_register_block() {
+    global $optpage_blocks, $AllowSpamFilters;
+    if (!file_exists(SM_PATH . 'plugins/filters/config.php')) return;
+
+    $optpage_blocks[] = array(
+        'name' => _("Message Filters"),
+        'url'  => SM_PATH . 'plugins/filters/options.php',
+        'desc' => _("Filtering enables messages with different criteria to be automatically filtered into different folders for easier organization."),
+        'js'   => false
+    );
+
+    if ($AllowSpamFilters) {
+        $optpage_blocks[] = array(
+            'name' => _("SPAM Filters"),
+            'url'  => SM_PATH . 'plugins/filters/spamoptions.php',
+            'desc' => _("SPAM filters allow you to select from various DNS based blacklists to detect junk email in your INBOX and move it to another folder (like Trash)."),
+            'js'   => false
+        );
+    }
+}
+
 /**
- * FIXME: Undocumented function
+ * Saves the DNS Cache to disk
  * @access private
  */
 function filters_SaveCache () {
@@ -45,7 +81,7 @@ function filters_SaveCache () {
        $fp = fopen($data_dir . '/dnscache', 'r');
        flock($fp,LOCK_EX);
     }
-    $fp1=fopen($data_dir . '/dnscache', 'w+');
+    $fp1 = fopen($data_dir . '/dnscache', 'w+');
 
     foreach ($SpamFilters_DNScache as $Key=> $Value) {
        $tstr = $Key . ',' . $Value['L'] . ',' . $Value['T'] . "\n";
@@ -57,7 +93,7 @@ function filters_SaveCache () {
 }
 
 /**
- * FIXME: Undocumented function
+ * Loads the DNS Cache from disk
  * @access private
  */
 function filters_LoadCache () {
@@ -67,20 +103,21 @@ function filters_LoadCache () {
         $SpamFilters_DNScache = array();
         if ($fp = fopen ($data_dir . '/dnscache', 'r')) {
             flock($fp,LOCK_SH);
-            while ($data=fgetcsv($fp,1024)) {
+            while ($data = fgetcsv($fp,1024)) {
                if ($data[2] > time()) {
                   $SpamFilters_DNScache[$data[0]]['L'] = $data[1];
                   $SpamFilters_DNScache[$data[0]]['T'] = $data[2];
                }
             }
-
             flock($fp,LOCK_UN);
         }
     }
 }
 
 /**
- * FIXME: Undocumented function
+ * Uses the BulkQuery executable to query all the RBLs at once
+ * @param array $filters Array of SPAM Fitlers
+ * @param array $IPs Array of IP Addresses
  * @access private
  */
 function filters_bulkquery($filters, $IPs) {
@@ -121,13 +158,15 @@ function filters_bulkquery($filters, $IPs) {
 }
 
 /**
- * FIXME: Undocumented function
+ * Starts the filtering process
  * @access private
  */
 function start_filters() {
     global $imapServerAddress, $imapPort, $imap_stream, $imapConnection,
            $UseSeparateImapConnection, $AllowSpamFilters;
 
+    if (!file_exists(SM_PATH . 'plugins/filters/config.php')) return;
+
     sqgetGlobalVar('username', $username, SQ_SESSION);
     sqgetGlobalVar('key',      $key,      SQ_COOKIE);
 
@@ -164,7 +203,8 @@ function start_filters() {
 }
 
 /**
- * FIXME: Undocumented function
+ * Does the loop through each filter
+ * @param stream imap_stream the stream to read from
  * @access private
  */
 function user_filters($imap_stream) {
@@ -186,6 +226,12 @@ function user_filters($imap_stream) {
                   $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan, $expunge);
             $expunge = filter_search_and_delete($imap_stream, 'CC',
                   $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan, $expunge);
+        } else if ($filters[$i]['where'] == 'Header and Body') {
+            $expunge = filter_search_and_delete($imap_stream, 'TEXT',
+                  $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan, $expunge);
+        } else if ($filters[$i]['where'] == 'Message Body') {
+            $expunge = filter_search_and_delete($imap_stream, 'BODY',
+                  $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan, $expunge);
         } else {
             /*
             *  If it's a normal TO, CC, SUBJECT, or FROM, then handle it
@@ -203,18 +249,29 @@ function user_filters($imap_stream) {
 }
 
 /**
- * FIXME: Undocumented function
+ * Creates and runs the IMAP command to filter messages
+ * @param string $where Which part of the message to search (TO, CC, SUBJECT, etc...)
+ * @param string $what String to search for
+ * @param string $where_to Folder it will move to
+ * @param string $user_scan Whether to search all or just unseen
+ * @param string $should_expunge
+ * @param boolean $where Which part of location to search
  * @access private
  */
 function filter_search_and_delete($imap_stream, $where, $what, $where_to, $user_scan,
                                   $should_expunge) {
     global $languages, $squirrelmail_language, $allow_charset_search, $imap_server_type;
 
+    if (strtolower($where_to) == 'inbox') {
+        return array();
+    }
+
     if ($user_scan == 'new') {
         $category = 'UNSEEN';
     } else {
         $category = 'ALL';
     }
+    $category .= ' UNDELETED';
 
     if ($allow_charset_search &&
         isset($languages[$squirrelmail_language]['CHARSET']) &&
@@ -231,7 +288,8 @@ function filter_search_and_delete($imap_stream, $where, $what, $where_to, $user_
         $what  = addslashes(trim($what[1]));
     }
 
-    if ($imap_server_type == 'macosx') {
+    // see comments in squirrelmail sqimap_search function
+    if ($imap_server_type == 'macosx' || $imap_server_type == 'hmailserver') {
         $search_str .= ' ' . $where . ' ' . $what;
     } else {
         $search_str .= ' ' . $where . ' {' . strlen($what) . "}\r\n"
@@ -242,7 +300,7 @@ function filter_search_and_delete($imap_stream, $where, $what, $where_to, $user_
     $read = sqimap_run_command($imap_stream, $search_str, true, $response, $message, TRUE);
     if (isset($read[0])) {
         $ids = array();
-        for ($i=0,$iCnt=count($read);$i<$iCnt;++$i) {
+        for ($i = 0, $iCnt = count($read); $i < $iCnt; ++$i) {
             if (preg_match("/^\* SEARCH (.+)$/", $read[$i], $regs)) {
                 $ids = preg_split("/ /", trim($regs[1]));
             break;
@@ -251,7 +309,7 @@ function filter_search_and_delete($imap_stream, $where, $what, $where_to, $user_
         if ($response == 'OK' && count($ids)) {
             if (sqimap_mailbox_exists($imap_stream, $where_to)) {
                  $should_expunge = true;
-                 sqimap_msgs_list_move ($imap_stream, $ids, $where_to);
+                 sqimap_msgs_list_move ($imap_stream, $ids, $where_to, false);
             }
         }
     }
@@ -259,7 +317,8 @@ function filter_search_and_delete($imap_stream, $where, $what, $where_to, $user_
 }
 
 /**
- * FIXME: Undocumented function
+ * Loops through all the Received Headers to find IP Addresses
+ * @param stream imap_stream the stream to read from
  * @access private
  */
 function spam_filters($imap_stream) {
@@ -299,7 +358,7 @@ function spam_filters($imap_stream) {
         $search_array = array();
         $read = sqimap_run_command($imap_stream, 'SEARCH UNSEEN', true, $response, $message, TRUE);
         if (isset($read[0])) {
-            for ($i=0,$iCnt=count($read);$i<$iCnt;++$i) {
+            for ($i = 0, $iCnt = count($read); $i < $iCnt; ++$i) {
                 if (preg_match("/^\* SEARCH (.+)$/", $read[$i], $regs)) {
                     $search_array = preg_split("/ /", trim($regs[1]));
                 break;
@@ -383,9 +442,14 @@ function spam_filters($imap_stream) {
 }
 
 /**
- * FIXME: Undocumented function
  * Does the loop through each enabled filter for the specified IP address.
  * IP format:  $a.$b.$c.$d
+ * @param int $a First subset of IP
+ * @param int $b Second subset of IP
+ * @param int $c Third subset of IP
+ * @param int $d Forth subset of IP
+ * @param array $filters The Spam Filters
+ * @return boolean Whether the IP is Spam
  * @access private
  */
 function filters_spam_check_site($a, $b, $c, $d, &$filters) {
@@ -419,14 +483,15 @@ function filters_spam_check_site($a, $b, $c, $d, &$filters) {
 }
 
 /**
- * FIXME: Undocumented function
+ * Loads the filters from the user preferences
+ * @return array All the user filters
  * @access private
  */
 function load_filters() {
     global $data_dir, $username;
 
     $filters = array();
-    for ($i=0; $fltr = getPref($data_dir, $username, 'filter' . $i); $i++) {
+    for ($i = 0; $fltr = getPref($data_dir, $username, 'filter' . $i); $i++) {
         $ary = explode(',', $fltr);
         $filters[$i]['where'] = $ary[0];
         $filters[$i]['what'] = $ary[1];
@@ -436,7 +501,8 @@ function load_filters() {
 }
 
 /**
- * FIXME: Undocumented function
+ * Loads the Spam Filters and checks the preferences for the enabled status
+ * @return array All the spam filters
  * @access private
  */
 function load_spam_filters() {
@@ -709,22 +775,21 @@ function load_spam_filters() {
         _("FREE - Distributed Sender Boycott List - UN-Confirmed Relays");
 
     foreach ($filters as $Key => $Value) {
-        $filters[$Key]['enabled'] = getPref($data_dir, $username,
-            $filters[$Key]['prefname']);
+        $filters[$Key]['enabled'] = getPref($data_dir, $username, $filters[$Key]['prefname']);
     }
 
     return $filters;
 }
 
 /**
- * FIXME: Undocumented function
+ * Removes a User filter
+ * @param int $id ID of the filter to remove
  * @access private
  */
 function remove_filter ($id) {
     global $data_dir, $username;
 
-    while ($nextFilter = getPref($data_dir, $username, 'filter' .
-        ($id + 1))) {
+    while ($nextFilter = getPref($data_dir, $username, 'filter' . ($id + 1))) {
         setPref($data_dir, $username, 'filter' . $id, $nextFilter);
         $id ++;
     }
@@ -733,7 +798,9 @@ function remove_filter ($id) {
 }
 
 /**
- * FIXME: Undocumented function
+ * Swaps two filters
+ * @param int $id1 ID of first filter to swap
+ * @param int $id2 ID of second filter to swap
  * @access private
  */
 function filter_swap($id1, $id2) {
@@ -749,20 +816,23 @@ function filter_swap($id1, $id2) {
 }
 
 /**
- * This update the filter rules when renaming or deleting folders
+ * This updates the filter rules when renaming or deleting folders
  * @param array $args
  * @access private
  */
 function update_for_folder ($args) {
+
+    if (!file_exists(SM_PATH . 'plugins/filters/config.php')) return;
+
     $old_folder = $args[0];
-        $new_folder = $args[2];
-        $action = $args[1];
+    $new_folder = $args[2];
+    $action = $args[1];
     global $data_dir, $username;
     $filters = array();
     $filters = load_filters();
     $filter_count = count($filters);
     $p = 0;
-    for ($i=0;$i<$filter_count;$i++) {
+    for ($i = 0; $i < $filter_count; $i++) {
         if (!empty($filters)) {
             if ($old_folder == $filters[$i]['folder']) {
                 if ($action == 'rename') {
@@ -792,4 +862,5 @@ function do_error($string) {
     echo $string;
     echo "</font></p>\n";
 }
+
 ?>
\ No newline at end of file