diff -urN fetchmail-svn/conf.c fetchmail-svn-ptt/conf.c
--- fetchmail-svn/conf.c	2008-01-11 20:40:08.000000000 +0100
+++ fetchmail-svn-ptt/conf.c	2008-01-14 16:30:10.458008130 +0100
@@ -336,6 +336,7 @@
 
 	booldump("fetchall", ctl->fetchall);
 	booldump("keep", ctl->keep);
+	numdump("days",  ctl->days);
 	booldump("flush", ctl->flush);
 	booldump("limitflush", ctl->limitflush);
 	booldump("rewrite", ctl->rewrite);
diff -urN fetchmail-svn/fetchmail.h fetchmail-svn-ptt/fetchmail.h
--- fetchmail-svn/fetchmail.h	2008-01-11 20:40:08.000000000 +0100
+++ fetchmail-svn-ptt/fetchmail.h	2008-01-14 16:36:25.029343537 +0100
@@ -330,6 +330,7 @@
     int fastuidlcount;		/* internal count for frequency of binary search */
     int	batchlimit;		/* max # msgs to pass in single SMTP session */
     int	expunge;		/* max # msgs to pass between expunges */
+    int	days;			/* max days to keep a message undeleted */
     flag use_ssl;		/* use SSL encrypted session */
     char *sslkey;		/* optional SSL private key file */
     char *sslcert;		/* optional SSL certificate file */
diff -urN fetchmail-svn/imap.c fetchmail-svn-ptt/imap.c
--- fetchmail-svn/imap.c	2008-01-11 20:40:08.000000000 +0100
+++ fetchmail-svn-ptt/imap.c	2008-01-14 19:04:13.500289251 +0100
@@ -13,6 +13,7 @@
 #include  <stdlib.h>
 #include  <limits.h>
 #include  <errno.h>
+#include  <time.h>
 #endif
 #include  "fetchmail.h"
 #include  "socket.h"
@@ -1092,6 +1093,91 @@
     return(PS_SUCCESS);
 }
 
+static int check_expired_messages(int sock, struct query *ctl)
+{
+	int ok, expired;
+	char *cp;
+        char buf[MSGBUFSIZE+1],
+	     expiry_date[MSGBUFSIZE+1];
+	time_t t;
+
+	expired = 0;
+	expiry_date[0] = '\0';
+	
+	t = time(NULL) - ((60*60*24)*ctl->days);
+        setlocale (LC_TIME, "C");
+	strftime(expiry_date, 12, "%d-%b-%Y", localtime(&t));
+	report_complete(stdout, "searching before %s\n", expiry_date);
+	gen_send(sock, "SEARCH SEEN BEFORE %s", expiry_date);
+	    ok = gen_recv(sock, buf, sizeof(buf));
+	    if (ok != 0)
+	    {
+		report(stderr, GT_("search for expired messages failed\n"));
+		return(PS_PROTOCOL);
+	    }
+	    else if ((cp = strstr(buf, "* SEARCH")))
+	    {
+		char	*ep;
+
+		cp += 8;	/* skip "* SEARCH" */
+		/* startcount is higher than count so that if there are no
+		 * unseen messages, imap_getsizes() will not need to do
+		 * anything! */
+		startcount = count + 1;
+
+		while (*cp && (unsigned char)*cp != '\0' && (unsigned char)*cp != 'A')
+		{
+		    /* skip whitespace */
+		    while (*cp && isspace((unsigned char)*cp))
+			cp++;
+		    if (*cp) 
+		    {
+			unsigned int um;
+			/*
+			 * Message numbers are between 1 and 2^32 inclusive,
+			 * so unsigned int is large enough.
+			 */
+			um=(unsigned int)strtol(cp,&ep,10);
+			if (um <= count)
+			{
+			    if ((ok = gen_transact(sock,
+			        imap_version >= IMAP4
+				    ? "STORE %d +FLAGS.SILENT (\\Seen \\Deleted)"
+				    : "STORE %d +FLAGS (\\Seen \\Deleted)", 
+			            um)))
+				return ok;
+			    else
+	                        expired++;
+
+  		            /* To avoid flooding the syslog when using --keep,
+  		             * report "Skipped message" only when:
+  		             *  1) --verbose is on, or
+  		             *  2) fetchmail does not use syslog
+		             */
+	    		    if (   (outlevel >= O_VERBOSE) ||
+			           (outlevel > O_SILENT && !run.use_syslog)
+	       			)
+                  	    {
+		                report_build(stdout, 
+			          GT_("flushing expired message %s@%s:%d\n"),
+			          ctl->remotename, ctl->server.truename, um);
+	                    }
+			    if (startcount > um)
+				startcount = um;
+			}
+			cp = ep;
+		    }
+		}
+	    }
+
+	if (expired > 0) {
+	    deletions += expired;
+	    report_build(stdout, "%d message expired and flushed\n", expired);
+	}
+
+    return(PS_SUCCESS);
+}
+
 static int imap_delete(int sock, struct query *ctl, int number)
 /* set delete flag for given message */
 {
@@ -1143,6 +1230,9 @@
 static int imap_end_mailbox_poll(int sock, struct query *ctl)
 /* cleanup mailbox before we idle or switch to another one */
 {
+    if (ctl->days > 0)
+        check_expired_messages(sock, ctl);
+
     if (deletions)
 	internal_expunge(sock);
     return(PS_SUCCESS);
diff -urN fetchmail-svn/rcfile_l.l fetchmail-svn-ptt/rcfile_l.l
--- fetchmail-svn/rcfile_l.l	2008-01-11 20:40:08.000000000 +0100
+++ fetchmail-svn-ptt/rcfile_l.l	2008-01-12 17:54:05.467155290 +0100
@@ -166,6 +166,7 @@
 no		{return NO;}
 
 keep		{ return KEEP; }
+days		{ return DAYS; }
 flush		{ return FLUSH; }
 limitflush	{ return LIMITFLUSH; }
 fetchall	{ return FETCHALL; }
diff -urN fetchmail-svn/rcfile_y.y fetchmail-svn-ptt/rcfile_y.y
--- fetchmail-svn/rcfile_y.y	2008-01-11 20:40:08.000000000 +0100
+++ fetchmail-svn-ptt/rcfile_y.y	2008-01-14 16:28:16.204379465 +0100
@@ -73,7 +73,7 @@
 %token <proto> PROTO AUTHTYPE
 %token <sval>  STRING
 %token <number> NUMBER
-%token NO KEEP FLUSH LIMITFLUSH FETCHALL REWRITE FORCECR STRIPCR PASS8BITS 
+%token NO KEEP DAYS FLUSH LIMITFLUSH FETCHALL REWRITE FORCECR STRIPCR PASS8BITS 
 %token DROPSTATUS DROPDELIVERED
 %token DNS SERVICE PORT UIDL INTERVAL MIMEDECODE IDLE CHECKALIAS 
 %token SSL SSLKEY SSLCERT SSLPROTO SSLCERTCK SSLCERTPATH SSLFINGERPRINT
@@ -313,6 +313,7 @@
 		| POSTCONNECT STRING	{current.postconnect = xstrdup($2);}
 
 		| KEEP			{current.keep        = FLAG_TRUE;}
+		| DAYS NUMBER		{current.days        = NUM_VALUE_IN($2);}
 		| FLUSH			{current.flush       = FLAG_TRUE;}
 		| LIMITFLUSH		{current.limitflush  = FLAG_TRUE;}
 		| FETCHALL		{current.fetchall    = FLAG_TRUE;}
