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