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