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