diff -Nur dovecot-1.1.4.orig/configure.in dovecot-1.1.4/configure.in
--- dovecot-1.1.4.orig/configure.in	2008-10-05 12:04:09.000000000 -0500
+++ dovecot-1.1.4/configure.in	2008-10-08 15:27:24.000000000 -0500
@@ -2387,6 +2387,7 @@
 src/util/Makefile
 src/plugins/Makefile
 src/plugins/acl/Makefile
+src/plugins/autoexpire/Makefile
 src/plugins/convert/Makefile
 src/plugins/expire/Makefile
 src/plugins/fts/Makefile
diff -Nur dovecot-1.1.4.orig/src/plugins/autoexpire/autoexpire-env.c dovecot-1.1.4/src/plugins/autoexpire/autoexpire-env.c
--- dovecot-1.1.4.orig/src/plugins/autoexpire/autoexpire-env.c	1969-12-31 18:00:00.000000000 -0600
+++ dovecot-1.1.4/src/plugins/autoexpire/autoexpire-env.c	2008-10-08 18:40:10.000000000 -0500
@@ -0,0 +1,87 @@
+/* Copyright (c) 2006-2008 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "array.h"
+#include "imap-match.h"
+#include "autoexpire-env.h"
+
+#include <stdlib.h>
+
+struct autoexpire_box {
+	const char *pattern;
+	struct imap_match_glob *glob;
+
+	unsigned int expire_secs;
+};
+
+struct autoexpire_env {
+	pool_t pool;
+	ARRAY_DEFINE(autoexpire_boxes, struct autoexpire_box);
+};
+
+static void autoexpire_env_parse(struct autoexpire_env *env, const char *str)
+{
+	struct autoexpire_box box;
+	char *const *names;
+	unsigned int len;
+
+	if (str == NULL)
+		return;
+
+	names = p_strsplit(env->pool, str, " ");
+	len = str_array_length((const char *const *)names);
+
+	p_array_init(&env->autoexpire_boxes, env->pool, len / 2);
+	for (; *names != NULL; names += 2) {
+		if (names[1] == NULL) {
+			i_fatal("autoexpire: Missing expire days for mailbox '%s'",
+				*names);
+		}
+
+		box.pattern = *names;
+		/* FIXME: hardcoded separator isn't very good */
+		box.glob = imap_match_init(env->pool, box.pattern, TRUE, '/');
+		box.expire_secs = strtoul(names[1], NULL, 10) * 3600 * 24;
+
+		array_append(&env->autoexpire_boxes, &box, 1);
+	}
+}
+
+struct autoexpire_env *autoexpire_env_init(const char *expunges)
+{
+	struct autoexpire_env *env;
+	pool_t pool;
+
+	pool = pool_alloconly_create("Expire pool", 512);
+	env = p_new(pool, struct autoexpire_env, 1);
+	env->pool = pool;
+
+	autoexpire_env_parse(env, expunges);
+	return env;
+}
+
+void autoexpire_env_deinit(struct autoexpire_env *env)
+{
+	pool_unref(&env->pool);
+}
+
+unsigned int autoexpire_box_find(struct autoexpire_env *env, const char *name)
+{
+	const struct autoexpire_box *autoexpire_boxes;
+	unsigned int i, count;
+	unsigned int secs, expunge_min = 0;
+
+	autoexpire_boxes = array_get(&env->autoexpire_boxes, &count);
+	for (i = 0; i < count; i++) {
+		if (imap_match(autoexpire_boxes[i].glob, name) == IMAP_MATCH_YES) {
+
+			secs = autoexpire_boxes[i].expire_secs;
+			i_assert(secs > 0);
+
+			if (expunge_min == 0 || expunge_min > secs)
+				expunge_min = secs;
+		}
+	}
+	return expunge_min;
+}
+
diff -Nur dovecot-1.1.4.orig/src/plugins/autoexpire/autoexpire-env.h dovecot-1.1.4/src/plugins/autoexpire/autoexpire-env.h
--- dovecot-1.1.4.orig/src/plugins/autoexpire/autoexpire-env.h	1969-12-31 18:00:00.000000000 -0600
+++ dovecot-1.1.4/src/plugins/autoexpire/autoexpire-env.h	2008-10-08 17:48:30.000000000 -0500
@@ -0,0 +1,11 @@
+#ifndef AUTOEXPIRE_ENV_H
+#define AUTOEXPIRE_ENV_H
+
+struct autoexpire_env;
+
+struct autoexpire_env *autoexpire_env_init(const char *expunges);
+void autoexpire_env_deinit(struct autoexpire_env *env);
+
+unsigned int autoexpire_box_find(struct autoexpire_env *env, const char *name);
+
+#endif
diff -Nur dovecot-1.1.4.orig/src/plugins/autoexpire/autoexpire-plugin.c dovecot-1.1.4/src/plugins/autoexpire/autoexpire-plugin.c
--- dovecot-1.1.4.orig/src/plugins/autoexpire/autoexpire-plugin.c	1969-12-31 18:00:00.000000000 -0600
+++ dovecot-1.1.4/src/plugins/autoexpire/autoexpire-plugin.c	2008-10-08 18:39:49.000000000 -0500
@@ -0,0 +1,149 @@
+/* Copyright (c) 2005-2008 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "ioloop.h"
+#include "array.h"
+#include "istream.h"
+#include "str.h"
+#include "mail-namespace.h"
+#include "index-mail.h"
+#include "index-storage.h"
+#include "mail-search.h"
+#include "autoexpire-env.h"
+#include "autoexpire-plugin.h"
+
+#include <stdlib.h>
+#include <sys/time.h>
+
+#define AUTOEXPIRE_CONTEXT(obj) \
+	MODULE_CONTEXT(obj, autoexpire_storage_module)
+
+static MODULE_CONTEXT_DEFINE_INIT(autoexpire_storage_module,
+				  &mail_storage_module_register);
+
+struct autoexpire {
+	struct autoexpire_env *env;
+	void (*next_hook_mail_storage_created)(struct mail_storage *storage);
+};
+
+struct autoexpire_mailbox {
+	union mailbox_module_context module_ctx;
+	time_t expire_secs;
+};
+
+static struct autoexpire autoexpire;
+
+static int  autoexpire_mailbox_close(struct mailbox *box)
+{
+	struct mailbox_transaction_context *trans;
+	struct mail_search_context *search_ctx;
+  struct mail_search_arg search_arg;
+  struct autoexpire_mailbox *xpr_box = AUTOEXPIRE_CONTEXT(box);
+	struct mail *mail;
+  struct timeval now;
+  int ret, rc, expunged = 0;
+  time_t saved_time, expire_time;
+
+  rc = gettimeofday( &now, NULL);
+  if (rc == 0) {
+    memset(&search_arg, 0, sizeof(search_arg));
+    search_arg.type = SEARCH_BEFORE;
+    expire_time = now.tv_sec - xpr_box->expire_secs;
+    search_arg.value.time = expire_time;
+
+	  trans = mailbox_transaction_begin(box, 0);
+	  search_ctx = mailbox_search_init(trans, NULL, &search_arg, NULL);
+	  mail = mail_alloc(trans, MAIL_FETCH_SAVE_DATE, NULL);
+
+    do {
+			ret = mailbox_search_next(search_ctx, mail);
+      if (ret > 0) {
+        if (mail_get_save_date(mail, &saved_time) >= 0 ) {
+          if (saved_time < expire_time ) {
+            mail_expunge(mail);
+            expunged = 1;
+          }
+        }
+      }
+    } while (ret > 0);
+		mail_free(&mail);
+		(void)mailbox_search_deinit(&search_ctx);
+
+    (void)mailbox_transaction_commit(&trans);
+    return 0;
+  }
+  return -1;
+}
+
+static void
+mailbox_autoexpire_hook(struct mailbox *box, time_t expire_secs)
+{
+	struct autoexpire_mailbox *xpr_box;
+
+	xpr_box = p_new(box->pool, struct autoexpire_mailbox, 1);
+	xpr_box->module_ctx.super = box->v;
+
+	box->v.close = autoexpire_mailbox_close;
+
+	xpr_box->expire_secs = expire_secs;
+
+	MODULE_CONTEXT_SET(box, autoexpire_storage_module, xpr_box);
+}
+
+static struct mailbox *
+autoexpire_mailbox_open(struct mail_storage *storage, const char *name,
+		    struct istream *input, enum mailbox_open_flags flags)
+{
+	union mail_storage_module_context *xpr_storage =
+		AUTOEXPIRE_CONTEXT(storage);
+	struct mailbox *box;
+	string_t *vname;
+	unsigned int secs;
+
+	box = xpr_storage->super.mailbox_open(storage, name, input, flags);
+	if (box != NULL) {
+		vname = t_str_new(128);
+		(void)mail_namespace_get_vname(storage->ns, vname, name);
+
+		secs = autoexpire_box_find(autoexpire.env, str_c(vname));
+		if (secs > 0)
+			mailbox_autoexpire_hook(box, secs);
+	}
+	return box;
+}
+
+
+static void autoexpire_mail_storage_created(struct mail_storage *storage)
+{
+	union mail_storage_module_context *xpr_storage;
+
+	xpr_storage =
+		p_new(storage->pool, union mail_storage_module_context, 1);
+	xpr_storage->super = storage->v;
+	storage->v.mailbox_open = autoexpire_mailbox_open;
+
+	MODULE_CONTEXT_SET_SELF(storage, autoexpire_storage_module, xpr_storage);
+
+	if (autoexpire.next_hook_mail_storage_created != NULL)
+		autoexpire.next_hook_mail_storage_created(storage);
+}
+
+void autoexpire_plugin_init(void)
+{
+	const char *expunge_env;
+
+	expunge_env = getenv("AUTOEXPIRE");
+	if (expunge_env != NULL) {
+		autoexpire.env = autoexpire_env_init(expunge_env);
+		autoexpire.next_hook_mail_storage_created =
+			hook_mail_storage_created;
+		hook_mail_storage_created = autoexpire_mail_storage_created;
+	}
+}
+
+void autoexpire_plugin_deinit(void)
+{
+		hook_mail_storage_created =
+			autoexpire.next_hook_mail_storage_created;
+		autoexpire_env_deinit(autoexpire.env);
+}
diff -Nur dovecot-1.1.4.orig/src/plugins/autoexpire/autoexpire-plugin.h dovecot-1.1.4/src/plugins/autoexpire/autoexpire-plugin.h
--- dovecot-1.1.4.orig/src/plugins/autoexpire/autoexpire-plugin.h	1969-12-31 18:00:00.000000000 -0600
+++ dovecot-1.1.4/src/plugins/autoexpire/autoexpire-plugin.h	2008-10-08 12:18:02.000000000 -0500
@@ -0,0 +1,7 @@
+#ifndef AUTOEXPIRE_PLUGIN_H
+#define AUTOEXPIRE_PLUGIN_H
+
+void autoexpire_plugin_init(void);
+void autoexpire_plugin_deinit(void);
+
+#endif
diff -Nur dovecot-1.1.4.orig/src/plugins/autoexpire/Makefile.am dovecot-1.1.4/src/plugins/autoexpire/Makefile.am
--- dovecot-1.1.4.orig/src/plugins/autoexpire/Makefile.am	1969-12-31 18:00:00.000000000 -0600
+++ dovecot-1.1.4/src/plugins/autoexpire/Makefile.am	2008-10-08 16:43:14.000000000 -0500
@@ -0,0 +1,28 @@
+AM_CPPFLAGS = \
+	-I$(top_srcdir)/src/lib \
+	-I$(top_srcdir)/src/lib-mail \
+	-I$(top_srcdir)/src/lib-imap \
+	-I$(top_srcdir)/src/lib-index \
+	-I$(top_srcdir)/src/lib-storage \
+	-I$(top_srcdir)/src/lib-storage/index
+
+lib11_autoexpire_plugin_la_LDFLAGS = -module -avoid-version
+
+module_LTLIBRARIES = \
+	lib11_autoexpire_plugin.la
+
+lib11_autoexpire_plugin_la_SOURCES = \
+	autoexpire-env.c \
+	autoexpire-plugin.c
+
+noinst_HEADERS = \
+	autoexpire-env.h \
+	autoexpire-plugin.h
+
+install-exec-local:
+	for d in imap pop3 lda; do \
+	  $(mkdir_p) $(DESTDIR)$(moduledir)/$$d; \
+	  rm -f $(DESTDIR)$(moduledir)/$$d/lib11_autoexpire_plugin$(MODULE_SUFFIX); \
+	  $(LN_S) ../lib11_autoexpire_plugin$(MODULE_SUFFIX) $(DESTDIR)$(moduledir)/$$d; \
+	done
+
diff -Nur dovecot-1.1.4.orig/src/plugins/Makefile.am dovecot-1.1.4/src/plugins/Makefile.am
--- dovecot-1.1.4.orig/src/plugins/Makefile.am	2008-07-13 07:16:17.000000000 -0500
+++ dovecot-1.1.4/src/plugins/Makefile.am	2008-10-08 15:17:22.000000000 -0500
@@ -12,5 +12,5 @@
 
 SUBDIRS = \
 	acl convert expire fts fts-squat lazy-expunge mail-log mbox-snarf \
-	quota imap-quota trash \
+	quota imap-quota trash autoexpire \
 	$(ZLIB) $(FTS_LUCENE) $(FTS_SOLR)
