diff --new-file -u -r imp-prepatch/config/prefs.php.dist imp/config/prefs.php.dist
--- imp-prepatch/config/prefs.php.dist	2008-01-07 12:03:26.929641841 -0500
+++ imp/config/prefs.php.dist	2008-01-09 16:44:32.872590681 -0500
@@ -45,7 +45,8 @@
                        'delete_attachments_monthly',
                        'delete_attachments_monthly_keep', 'purge_trash',
                        'purge_trash_interval', 'purge_trash_keep',
-                       'purge_spam', 'purge_spam_interval', 'purge_spam_keep')
+                       'purge_spam', 'purge_spam_interval', 'purge_spam_keep',
+                       'purge_folders','purge_folders_max_days','purge_folders_list_folders')
 );
 
 $prefGroups['compose'] = array(
@@ -460,6 +461,34 @@
     'shared' => false,
     'type' => 'implicit');
 
+// Whether to enable or disable folder purging.
+$_prefs['purge_folders'] = array(
+       'value' => 0,
+       'locked' => false,
+       'shared' => false,
+       'type' => 'checkbox',
+       'desc' => _("Enable folder message purging?"));
+
+// The number of days after which any and all messages will be deleted.
+// Negative values mean that there is no limit.
+$_prefs['purge_folders_max_days'] = array(
+       'value' => -1,
+       'locked' => false,
+       'shared' => false,
+       'type' => 'number',
+       'desc' => _("Maximum number of days before purging all folders (-1 for unlimited)."));
+
+// special display-only helper pref
+$_prefs['purge_folders_list_folders'] = array(
+	'type' => 'special');
+
+//stores the per-folder purge_options.
+$_prefs['purge_folders_list'] = array(
+       'value' => 'a:0:{}',
+       'locked' => false,
+       'shared' => false,
+       'type' => 'implicit');
+
 // End Login/Maintenance preferences
 
 
diff --new-file -u -r imp-prepatch/lib/Maintenance/imp.php imp/lib/Maintenance/imp.php
--- imp-prepatch/lib/Maintenance/imp.php	2008-01-07 12:03:28.157452343 -0500
+++ imp/lib/Maintenance/imp.php	2008-01-07 12:16:59.134694283 -0500
@@ -33,7 +33,8 @@
         'delete_attachments_monthly' => MAINTENANCE_MONTHLY,
         'purge_sentmail'             => MAINTENANCE_MONTHLY,
         'purge_spam'                 => MAINTENANCE_MONTHLY,
-        'purge_trash'                => MAINTENANCE_MONTHLY
+        'purge_trash'                => MAINTENANCE_MONTHLY,
+        'purge_folders'              => MAINTENANCE_EVERY
     );
 
 }
diff --new-file -u -r imp-prepatch/lib/Maintenance/Task/purge_folders.php imp/lib/Maintenance/Task/purge_folders.php
--- imp-prepatch/lib/Maintenance/Task/purge_folders.php	1969-12-31 19:00:00.000000000 -0500
+++ imp/lib/Maintenance/Task/purge_folders.php	2008-01-09 15:57:16.102591409 -0500
@@ -0,0 +1,101 @@
+<?php
+/**
+ * Maintenance module that purges messages from folders that are older than the limit per folder
+ *
+ * $Horde: imp/lib/Maintenance/Task/purge_folders.php,v 1.0 2005/08/11 03:23:37 selsky Exp $
+ *
+ * Copyright 2006 Brook Davis <brook@linuxbox.com>
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author  Brook Davis <brook@linuxbox.com>
+ * @since   IMP 2.3.7
+ * @package Horde_Maintenance
+ */
+class Maintenance_Task_purge_folders extends Maintenance_Task {
+
+    /**
+     * Purges messages from folders that are older than the specified limit in days.
+     *
+     * @return boolean  Whether all folders were purged.
+     */
+    function doMaintenance()
+    {
+	global $prefs, $notification;
+
+	/* get the folder list */
+	require_once IMP_BASE . '/lib/Folder.php';
+	$imp_folder = &IMP_Folder::singleton();
+	$folders = $imp_folder->flist();
+
+	// Get the array of purge values
+	$limits = $prefs->getValue('purge_folders_list');
+	$limits = unserialize($limits);
+
+	$total_purged = 0;
+	
+	foreach($folders as $folder_name => $folder){
+
+		/* they must have deleted the folder or something, move on */
+		if (!$imp_folder->exists($folder_name)) {
+			continue;
+		}
+
+		/* obtain the limit as set by the user, or else use the max for all folders */
+		$limit = ( isset($limits[$folder_name]) && is_numeric($limits[$folder_name]) ) ? $limits[$folder_name] : -1;
+		$max_limit = $prefs->getValue('purge_folders_max_days');
+		if ($max_limit > 0 && ($max_limit < $limit || $limit < 0)) {
+			$limit = $max_limit;
+		}
+
+		/* at this point $limit is either what was set by the user for this folder, 
+		 * or the max value for all folders */
+		if ($limit > 0) {
+
+			/* Get the current UNIX timestamp minus the number of days specified
+			 * in purge_folders_list[FOLDER_NAME].  If a message in this folder has a 
+			 * timestamp prior to this value, it will be deleted. */
+			$del_time = date("r", time() - ($limit * 86400));
+
+			/* change to the folder we are working in */
+			require_once IMP_BASE . '/lib/Message.php';
+			$imp_imap = &IMP_IMAP::singleton();
+			$imp_message = &IMP_Message::singleton();;
+			$imp_imap->changeMbox($folder_name, IMP_IMAP_AUTO);
+
+			/* get all messages older than the limit set in the purge_folders_list array for this folder */
+			$msg_ids = @imap_search($imp_imap->stream(), "BEFORE \"$del_time\"", SE_UID);
+			
+			/* continue if no messages found */
+			if (empty($msg_ids)) {
+				continue;
+			}
+
+			/* Go through the message list and delete the messages. Quit on failure */
+			$indices = array($folder_name => $msg_ids);
+			if ($imp_message->delete($indices, true)) {
+				$msgcount = count($msg_ids);
+				$total_purged += $msgcount;
+				$notification->push(sprintf(_("Purged %d message(s) from %s folder, which were older than %d day(s)."), $msgcount, $folder_name, $limit), 'horde.message');
+			} else {
+				return false;
+			}
+		}
+	}
+	$notification->push(sprintf("Purge folders task completed.  Purged %d messages.",$total_purged),'horde.message');
+	return true;
+    }
+
+    /**
+     * Returns information for the maintenance function.
+     *
+     * @return string  Description of what the operation is going to do during
+     *                 this login.
+     */
+    function describeMaintenance()
+    {
+                       global $prefs;
+        return _("Purge all folders of messages using the dates set in prefences?");
+    }
+}
diff --new-file -u -r imp-prepatch/lib/prefs.php imp/lib/prefs.php
--- imp-prepatch/lib/prefs.php	2008-01-07 12:03:27.417566536 -0500
+++ imp/lib/prefs.php	2008-01-09 16:08:33.997744783 -0500
@@ -15,6 +15,43 @@
 * @package Horde_Prefs
 */  

+function handle_purge_folders_list_folders($updated) {
+       global $prefs, $notification;
+
+       $status = true;
+       $arr = Util::getFormData('purge_folders_list');
+       $max = $prefs->getValue('purge_folders_max_days');
+       
+       foreach ($arr as $k=>$v) {
+               if (!is_numeric($v) && $v !== '') {
+                       $notification->push(_("All of non-empty purge_folders fields need to be numbers (-1 for no limit)."), 'horde.error');
+                       $updated = false;
+                       $status = false;
+               } else if (($v < 0 || $v > $max) && $max > 0) {
+                       // Ensure we're under the max
+                       $arr[$k] = $max;
+               }
+       }
+
+       if ($status) {
+               require_once(IMP_BASE.'/lib/Folder.php');
+               $imp_folder = &IMP_Folder::singleton();
+               $folders = $imp_folder->flist();
+
+               // We might need to create the preference...
+               if (!isset($prefs->_prefs['purge_folders_list'])) {
+                       $updated = $updated | $prefs->add('purge_folders_list', serialize($arr));
+               } else {
+                       $updated = $updated | $prefs->setValue('purge_folders_list', serialize($arr));
+               }
+               if ($updated) {
+                       $prefs->setDirty('purge_folders_list', true);
+               }
+               $prefs->store();
+       }
+       return $updated;
+}
+
 function handle_sentmailselect($updated)
 {
     global $conf, $prefs, $identity;
diff --new-file -u -r imp-prepatch/templates/prefs/purge_folders_list_folders.inc imp/templates/prefs/purge_folders_list_folders.inc
--- imp-prepatch/templates/prefs/purge_folders_list_folders.inc	1969-12-31 19:00:00.000000000 -0500
+++ imp/templates/prefs/purge_folders_list_folders.inc	2008-01-09 15:39:00.633784016 -0500
@@ -0,0 +1,81 @@
+<?php
+/**
+ * Maintenance module that purges messages from folders that are older than the limit per folder
+ *
+ * $Horde: imp/templates/prefs/purge_folders_list.inc,v 1.0 2005/08/11 03:23:37 selsky Exp $
+ *
+ * Copyright 2006 Brook Davis <brook@linuxbox.com>
+ * Extended 2006 Dan Sherman <dan@linuxbox.com>
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author  Brook Davis <brook@linuxbox.com>
+ * @since   IMP 2.3.7
+ * @package Horde_Maintenance
+ */
+
+$imp_folder = &IMP_Folder::singleton();
+$folders = $imp_folder->flist();
+$purge_folders_list_val = $prefs->getValue('purge_folders_list');
+
+$purge_folders_enabled = $prefs->getValue('purge_folders');
+$purge_folders_locked = $prefs->isLocked('purge_folders');
+
+$purge_folders_max_days = $prefs->getValue('purge_folders_max_days');
+
+$skip_list_folders = ($purge_folders_enabled == 0 && $purge_folders_locked == 1);
+
+if (!empty($purge_folders_list_val) && !empty($folders) && !$skip_list_folders) {
+       $purge_folders_list_val = unserialize($purge_folders_list_val);
+       echo Horde::label('purge_folders_list', _("Set per-folder message purging options below.  Messages older than the number of days entered will be deleted on your next login.  Leave empty to use the maximum allowed (currently ".($purge_folders_max_days > 0 ? $purge_folders_max_days . " days" : "unlimited"  ).($prefs->isLocked('purge_folders_max_days') ? "" : ", configurable above").")."));
+       echo "<br>";
+       foreach ($folders as $k=>$v) {
+               if (!isset($purge_folders_list_val[$v['val']])) {
+                       $purge_folders_list_val[$v['val']] = "";
+               }
+               echo "<input type=\"text\" id=\"purge_folders_list[".$v['val']."]\" name=\"purge_folders_list[".$v['val']."]\" size=3 maxlength=3 value=\"".$purge_folders_list_val[$v['val']]."\" style='font-size:90%'/>\n";
+               if (substr_count($v['val'],'.')) {
+               $label = substr_replace($v['val'], '', 0, strrpos($v['val'],'.')+1);
+
+               for ($i = substr_count($v['val'],'.'); $i >= 1; $i--) {
+                       $parts = explode('.',$v['val']);
+                       for ($j = 0; $j < $i; $j++)
+                               array_pop($parts);
+                       if (is_last_in_tree(implode('.',$parts), $folders))
+                               echo _('<img src="../themes/graphics/tree/blank.png">');
+                       else
+                               echo _('<img src="../themes/graphics/tree/line.png">');
+               }
+
+                       $last = is_last_in_tree($v['val'], $folders);
+                       if ($last)
+                               echo _('<img src="../themes/graphics/tree/joinbottom.png"><img src="../imp/themes/graphics/folders/folder.png">'.$label."<br>");
+                       else
+                               echo _('<img src="../themes/graphics/tree/join.png"><img src="../imp/themes/graphics/folders/folder.png">'.$label."<br>");
+
+
+               } else {
+                       echo _('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="../imp/themes/graphics/folders/folder.png">'.$v['val']."<br>");
+               }
+       }
+}
+
+
+function is_last_in_tree($value, $folders) {
+       $last = true;
+       if (strpos($value,'.')) {
+               $found_self = false;
+               $parent = substr($value,0,strrpos($value,'.')+1);
+
+               foreach ($folders as $f) {
+                       if ($f['val'] == $value)
+                               $found_self = true;
+                       if ($found_self && $f['val'] != $value && preg_match('/^'.preg_quote($parent).'/', $f['val']) && substr_count($f['val'],'.') == substr_count($value,'.'))
+                               $last = false;
+               }
+       }
+
+       return $last;
+}
+
+?>