[imp] Message Filters

Max Kalika max@the-triumvirate.net
Sat, 11 Nov 2000 19:57:53 -0800


---------------------- multipart/mixed attachment
Quoting Chuck Hagenbuch <chuck@horde.org>:

> IMAP has nothing to do with mail delivery, which is why it is a less than
> ideal platform to do filtering with. However, enough people have requested
> this feature that it will probably make it into some future version of IMP.

I took up the challenge. :-)

> To be clear, though:
> 1. It will not become an official part of 2.2

naturally

2. It will not become an official part of 2.3. Filtering qualifies as a big
   feature, and we've got a freeze on large features for 2.3 in the interest
   of pushing it out sooner.

granted, but if its functioning enough (i.e. completely) would you consider 
putting it in for this release?

3. I'm happy to accept code to do filtering, and if someone else writes it
   that'll accelerate the rate of it being included in IMP, but it will have
   to be very cleanly written and well abstracted and as efficient as
   possible to make it in.

I think I did it right.  Tracking is done via preferences and all the necessary 
checks are in place.  Applying rules is done by doing an imap_search to aquire 
the message IDs and then perform the action using the in-place functions --
IMP_message::delete() and IMP_message::copy().

Functionality:

Filter rules are created in a new "Filter" interface (placed between 
preferences and Search on the menu).  This screen lists the current rules with 
the option to delete, move up, and move down individual rules.  There is also a 
form to create new rules.

The mailbox view is altered with a "Apply Filter Rules" icon next to 
the "refresh" icon.  This is only visible if the mailbox is "INBOX".  Currently 
only unseen and undeleted messages will be looked at for filtering purposes (I 
was debating whether to change that to just undeleted, but left it for now).  
When the icon is clicked, messages are filtered and any that qualify for the 
rules, are marked deleted.  I figure its best to leave to the user to expunge.  
And those that have "hide deleted" enabled, they will imediatly note the 
changes.

Anyway, the patches are below.  I'm also not very good at making -- stealing -- 
icons, but the attatched will do for now.

--mk23

Added files: (attatched)

imp/filters.php
imp/templates/filters/list.inc
imp/templates/filters/create.inc
imp/graphics/filter.gif
imp/graphics/do_filter.gif

Patches:

[root@host imp]# cvs diff -u templates/menu/main.inc 
Index: templates/menu/main.inc
===================================================================
RCS file: /cvs/horde/imp/templates/menu/main.inc,v
retrieving revision 2.79
diff -u -r2.79 main.inc
--- templates/menu/main.inc     2000/11/12 02:40:40     2.79
+++ templates/menu/main.inc     2000/11/12 03:49:10
@@ -22,6 +22,9 @@
 if ($conf['passwd']['enabled']) {
     pmenuItem($conf['passwd']['url'], _("Change Password"), 'lock.gif');
 }
+if (($conf['prefs']['driver'] != '') && ($conf['prefs']['driver'] != 'none') 
&& $conf['user']['allow_filters'] && $prefs->isChangeable('filters')) {
+    pmenuItem(Horde::url('filters.php'), _("Filters"), 'filter.gif');
+}
 pmenuItem(Horde::url('search.php'), _("Search"), 'search.gif');
 
 /* Horde menu items */

[root@host imp]# cvs diff -u mailbox.php 
Index: mailbox.php
===================================================================
RCS file: /cvs/horde/imp/mailbox.php,v
retrieving revision 2.214
diff -u -r2.214 mailbox.php
--- mailbox.php 2000/11/11 18:21:35     2.214
+++ mailbox.php 2000/11/12 03:36:44
@@ -244,7 +244,29 @@
         $imp['searchquery'] = trim($query);
         $imp['searchfolders'] = $search_folders;
         break;
-        
+
+    case FILTER:
+        $filters = @unserialize($prefs->getValue('filters'));
+        if(is_array($filters) && count($filters) && $imp['protocol'] !
= 'pop3') {
+            $baseQuery = 'UNSEEN UNDELETED ';
+            for($i = 0; $i < count($filters); $i++) {
+                $query = $baseQuery . strtoupper($filters[$i]
['field']) . ' "' . $filters[$i]['text'] . '"';
+                $indices = imap_search($imp['stream'], $query, SE_UID);
+                if(isset($indices) && is_array($indices)) {
+                    if($filters[$i]['action'] == 'delete') {
+                        if(!IMP_message::delete($indices)) {
+                        // error message here
+                        }
+                    }
+                    else {
+                        if(!IMP_message::copy($filters[$i]['folder'], 
$indices, MOVE_MESSAGES)) {
+                        // error message here
+                        }
+                    }
+                }
+            }
+        }
+        break;
     }
 }


[root@host imp]# cvs diff -u lib/IMP.php 
Index: lib/IMP.php
===================================================================
RCS file: /cvs/horde/imp/lib/IMP.php,v
retrieving revision 1.91
diff -u -r1.91 IMP.php
--- lib/IMP.php 2000/11/08 19:41:34     1.91
+++ lib/IMP.php 2000/11/12 03:38:05
@@ -63,6 +63,11 @@
 define('POLL_FOLDERS',         53);
 define('FLAG_MESSAGES',        54);
 define('PRINT_MESSAGE',        55);
+define('FILTER',               56);
+define('FILTER_CREATE',        57);
+define('FILTER_DELETE',        58);
+define('FILTER_DOWN',          59);
+define('FILTER_UP',            60);
 
 // defines for IMAP flags
 define('IMP_ALL',       0);


[root@host imp]# cvs diff -u config/conf.php.dist 
Index: config/conf.php.dist
===================================================================
RCS file: /cvs/horde/imp/config/conf.php.dist,v
retrieving revision 1.97
diff -u -r1.97 conf.php.dist
--- config/conf.php.dist        2000/11/08 22:10:48     1.97
+++ config/conf.php.dist        2000/11/12 03:41:37
@@ -202,6 +202,11 @@
 // raw message, or to download it and save it to disk.
 $conf['user']['allow_view_source'] = true;
 
+// Should we allow users to set up filtering rules for incoming mail?
+// To make this work, the 'filters' preference must be also enabled
+// in prefs.php
+$conf['user']['allow_view_source'] = true;
+
 // If this is anything other than false, then it is assumed to be the
 // URL of an alternate login screen which will be used in place of
 // IMP's default one.


[root@host imp]# cvs diff -u config/prefs.php.dist
Index: config/prefs.php.dist
===================================================================
RCS file: /cvs/horde/imp/config/prefs.php.dist,v
retrieving revision 1.15
diff -u -r1.15 prefs.php.dist
--- config/prefs.php.dist       2000/11/08 19:41:29     1.15
+++ config/prefs.php.dist       2000/11/12 03:43:06
@@ -75,3 +75,6 @@
 # javascript popups, and expand folder tree by default
 nav_popup, 0, 1
 nav_expanded, 0, 1
+
+# filters tracking
+filters, , 1
---------------------- multipart/mixed attachment
A non-text attachment was scrubbed...
Name: filters.tar.gz
Type: application/x-gzip-compressed
Size: 3906 bytes
Desc: not available
Url : http://lists.horde.org/archives/dev/attachments/eb35cce9/filters.tar.gz

---------------------- multipart/mixed attachment--