346e22
diff -Naur cyrus-imapd-2.3.12p2/README.autosievefolder cyrus-imapd-2.3.12p2-autosieve.uncompiled/README.autosievefolder
346e22
--- cyrus-imapd-2.3.12p2/README.autosievefolder	1970-01-01 02:00:00.000000000 +0200
346e22
+++ cyrus-imapd-2.3.12p2-autosieve.uncompiled/README.autosievefolder	2008-05-06 15:16:21.000000000 +0300
d0d307
@@ -0,0 +1,42 @@
d0d307
+Cyrus IMAP autosievefolder patch
d0d307
+----------------------------------
d0d307
+
d0d307
+NOTE : This patch has been created at the University of Athens. For more info, as well 
d0d307
+as more patches on Cyrus IMAPD server, please visit http://email.uoa.gr 
d0d307
+
d0d307
+
d0d307
+  When the lmtpd daemon receives an email message prior to delivering it to the 
d0d307
+INBOX folder of the user, checks if the user has specified sieve filters. If the
d0d307
+user has specified sieve filters the filters are evaluated. If the message matches
d0d307
+any of the filters the action that is specified in the filter is executed. If the action 
d0d307
+is FileInto it is stored in the subfolder specified in the filter. If the 
d0d307
+subfolder doesn't exist then the message is sent to the INBOX folder of the user.
d0d307
+
d0d307
+  With this patch if the folder doesn't exist AND the name of the subfolder is 
d0d307
+specified in the autosievefolders option, OR the anysievefolder is set to 
d0d307
+yes in the cyrus-imap configuration file then the subfolder is created and the mail 
d0d307
+is stored there.
d0d307
+
d0d307
+
d0d307
+Check the following options of the imapd.conf file
d0d307
+==================================================
d0d307
+
d0d307
+* anysievefolder : It must be "yes" in order to permit the autocreation of any 
d0d307
+INBOX subfolder requested by a sieve filter, through the "fileinto" action. (default = no)
d0d307
+* autosievefolders : It is a "|" separated list of subfolders of INBOX that will be 
d0d307
+automatically created, if requested by a sieve filter, through the "fileinto" 
d0d307
+action. (default = null)
d0d307
+	i.e. autosievefolders: Junk | Spam
d0d307
+
d0d307
+WARNING: anysievefolder, takes precedence over autosievefolders . Which means that if 
d0d307
+anysievefolder is set to "yes", cyrus will create any INBOX subfolder requested, no-matter what the value of autosievefolders is.
d0d307
+
d0d307
+
d0d307
+Things to be done
d0d307
+=================
d0d307
+
d0d307
+1. Support cyrus wildcards in the autosievefolders option. 
d0d307
+
d0d307
+
d0d307
+For more information and updates please visit http://email.uoa.gr/projects/cyrus/autosievefolder
d0d307
+
346e22
diff -Naur cyrus-imapd-2.3.12p2/imap/lmtp_sieve.c cyrus-imapd-2.3.12p2-autosieve.uncompiled/imap/lmtp_sieve.c
346e22
--- cyrus-imapd-2.3.12p2/imap/lmtp_sieve.c	2008-03-24 19:09:17.000000000 +0200
346e22
+++ cyrus-imapd-2.3.12p2-autosieve.uncompiled/imap/lmtp_sieve.c	2008-05-06 15:16:21.000000000 +0300
Tomas Janousek 915c4a
@@ -88,6 +88,9 @@
cf58a5
     struct auth_state *authstate;
cf58a5
 } script_data_t;
d0d307
 
d0d307
+static int autosieve_subfolder(char *userid, struct auth_state *auth_state,
d0d307
+                               char *subfolder, struct namespace *namespace);
d0d307
+
d0d307
 static char *make_sieve_db(const char *user)
d0d307
 {
d0d307
     static char buf[MAX_MAILBOX_PATH+1];
Tomas Janousek 915c4a
@@ -484,7 +487,20 @@
d0d307
 			      sd->username, mdata->notifyheader,
d0d307
 			      namebuf, quotaoverride, 0);
d0d307
     }
d0d307
-
cf58a5
+    
d0d307
+    if (ret == IMAP_MAILBOX_NONEXISTENT) {
d0d307
+        /* if "plus" folder under INBOX, then try to create it */
d0d307
+        ret = autosieve_subfolder((char *) sd->username, sd->authstate, namebuf, mdata->namespace);
d0d307
+
cf58a5
+	/* Try to deliver the mail again. */
d0d307
+        if (!ret)
cf58a5
+            ret = deliver_mailbox(md->f, mdata->content, mdata->stage, md->size,
d0d307
+                                  fc->imapflags->flag, fc->imapflags->nflags,
d0d307
+                                  (char *) sd->username, sd->authstate, md->id,
d0d307
+                                  sd->username, mdata->notifyheader,
d0d307
+                                  namebuf, quotaoverride, 0);
d0d307
+    }
cf58a5
+    
d0d307
     if (!ret) {
d0d307
 	snmp_increment(SIEVE_FILEINTO, 1);
d0d307
 	return SIEVE_OK;
Tomas Janousek 686149
@@ -935,3 +951,80 @@
cf58a5
        we'll do normal delivery */
d0d307
     return r;
d0d307
 }
d0d307
+
d0d307
+
d0d307
+#define SEP '|'
d0d307
+
d0d307
+static int autosieve_subfolder(char *userid, struct auth_state *auth_state,
d0d307
+                               char *subfolder, struct namespace *namespace)
d0d307
+{
d0d307
+     char option_name_external[MAX_MAILBOX_NAME + 1];
d0d307
+     char option_name_internal[MAX_MAILBOX_NAME + 1];
d0d307
+     const char *subf ;
d0d307
+     char *p, *q, *next_subf;
d0d307
+     int len, r = 0;
d0d307
+     int createsievefolder = 0;
d0d307
+
d0d307
+    /* Check if subfolder or userid are NULL */
cf58a5
+    if(userid == NULL || subfolder == NULL)
d0d307
+         return IMAP_MAILBOX_NONEXISTENT;
d0d307
+
cf58a5
+    syslog(LOG_DEBUG, "autosievefolder: autosieve_subfolder() was called for user %s, folder %s", 
cf58a5
+		    userid, subfolder);
cf58a5
+
cf58a5
+    if (config_getswitch(IMAPOPT_ANYSIEVEFOLDER)) {
d0d307
+         createsievefolder = 1;
cf58a5
+    } else if ((subf = config_getstring(IMAPOPT_AUTOSIEVEFOLDERS)) != NULL) {
d0d307
+         /* Roll through subf */
d0d307
+         next_subf = (char *) subf;
d0d307
+         while (*next_subf) {
d0d307
+              for (p = next_subf ; isspace((int) *p) || *p == SEP ; p++);
d0d307
+              for (next_subf = p ; *next_subf && *next_subf != SEP ; next_subf++);
d0d307
+              for (q = next_subf ; q > p && (isspace((int) *q) || *q == SEP || !*q); q--);
d0d307
+
d0d307
+              if (!*p) continue;
d0d307
+                    
d0d307
+              len = q - p + 1;
d0d307
+             /*
d0d307
+              * This is a preliminary length check based on the assumption
d0d307
+              * that the *final* internal format will be something
d0d307
+              * like user.userid.subfolder(s).
d0d307
+              */
d0d307
+              if (len > sizeof(option_name_external) - strlen(userid) - 5)
d0d307
+                   return IMAP_MAILBOX_BADNAME;
d0d307
+
d0d307
+              strlcpy(option_name_external, namespace->prefix[NAMESPACE_INBOX], sizeof(option_name_external));
d0d307
+	      strncat(option_name_external, p, len);
d0d307
+                    
d0d307
+              /* 
d0d307
+               * Transform the option folder name to internal namespace and compare it
d0d307
+	       * with what must be created.
d0d307
+               */
d0d307
+              r = namespace->mboxname_tointernal(namespace, option_name_external, userid, option_name_internal);
d0d307
+              if (r) continue;
d0d307
+
d0d307
+              if (!strcmp(option_name_internal, subfolder)) {
d0d307
+                  createsievefolder = 1;
d0d307
+                  break;
d0d307
+              }
d0d307
+         }
d0d307
+    }
d0d307
+
d0d307
+    if (createsievefolder) {
d0d307
+        /* Folder is already in internal namespace format */
d0d307
+        r = mboxlist_createmailbox(subfolder, MAILBOX_FORMAT_NORMAL, NULL,
d0d307
+                                           1, userid, auth_state, 0, 0, 0);
d0d307
+        if (!r) {
d0d307
+            mboxlist_changesub(subfolder, userid, auth_state, 1, 1);
cf58a5
+            syslog(LOG_DEBUG, "autosievefolder: User %s, folder %s creation succeeded",
d0d307
+                                                   userid, subfolder);
d0d307
+            return 0;
d0d307
+        } else {
d0d307
+            syslog(LOG_ERR, "autosievefolder: User %s, folder %s creation failed. %s",
d0d307
+                                                  userid, subfolder,error_message(r));
d0d307
+            return r;
d0d307
+        }
d0d307
+    } else
d0d307
+        return IMAP_MAILBOX_NONEXISTENT;
d0d307
+}
d0d307
+
346e22
diff -Naur cyrus-imapd-2.3.12p2/lib/imapoptions cyrus-imapd-2.3.12p2-autosieve.uncompiled/lib/imapoptions
346e22
--- cyrus-imapd-2.3.12p2/lib/imapoptions	2008-04-11 23:07:00.000000000 +0300
346e22
+++ cyrus-imapd-2.3.12p2-autosieve.uncompiled/lib/imapoptions	2008-05-06 15:16:21.000000000 +0300
346e22
@@ -942,6 +942,15 @@
d0d307
 /* If enabled, lmtpd will look for Sieve scripts in user's home
d0d307
    directories: ~user/.sieve. */
d0d307
 
d0d307
+{ "anysievefolder", 0, SWITCH }
d0d307
+/* It must be "yes" in order to permit the autocreation of any INBOX subfolder 
d0d307
+   requested by a sieve filter, through the "fileinto" action. (default = no) */
d0d307
+
d0d307
+{ "autosievefolders", NULL, STRING }
d0d307
+/* It is a "|" separated list of subfolders of INBOX that will be automatically created, 
d0d307
+   if requested by a sieve filter, through the "fileinto" action. (default = null)
d0d307
+   i.e. autosievefolders: Junk | Spam */
d0d307
+
d0d307
 { "singleinstancestore", 1, SWITCH }
cf58a5
 /* If enabled, imapd, lmtpd and nntpd attempt to only write one copy
cf58a5
    of a message per partition and create hard links, resulting in a