diff -Naur cyrus-imapd-2.2.2-BETA/imap/lmtpd.c cyrus-imapd-2.2.2-BETA-autosievefolder.uncompiled/imap/lmtpd.c --- cyrus-imapd-2.2.2-BETA/imap/lmtpd.c 2003-10-24 21:24:06.000000000 +0300 +++ cyrus-imapd-2.2.2-BETA-autosievefolder.uncompiled/imap/lmtpd.c 2003-12-15 14:33:48.775254000 +0200 @@ -167,6 +167,8 @@ static int sieve_usehomedir = 0; static const char *sieve_dir = NULL; #endif +static int autosieve_subfolder(char *userid, struct auth_state *auth_state, + char *subfolder); /* per-user/session state */ static struct protstream *deliver_out, *deliver_in; @@ -623,6 +625,19 @@ sd->username, mdata->notifyheader, fc->mailbox, quotaoverride, 0); + if (ret == IMAP_MAILBOX_NONEXISTENT) { + /* if "plus" folder under INBOX, then try to create it */ + ret = autosieve_subfolder(sd->username, sd->authstate, fc->mailbox); + + if (!ret) + ret = deliver_mailbox(md->data, mdata->stage, md->size, + fc->imapflags->flag, fc->imapflags->nflags, + sd->username, sd->authstate, md->id, + sd->username, mdata->notifyheader, + fc->mailbox, quotaoverride, 0); + } + + if (ret == 0) { snmp_increment(SIEVE_FILEINTO, 1); @@ -1472,3 +1487,89 @@ append_removestage(stage); } + +#define SEP '|' + +static int autosieve_subfolder(char *userid, struct auth_state *auth_state, + char *subfolder) +{ + char foldername [MAX_MAILBOX_NAME+1]; + char f [MAX_MAILBOX_NAME+1]; + const char *subf ; + char *p, *q, *next_subf, *mbox; + int len, r = 0; + int createsievefolder = 0; + + + /* Check if subfolder is NULL */ + if(subfolder == NULL) + return IMAP_MAILBOX_NONEXISTENT; + + if ((subfolder[0] != 'i' && subfolder[0] != 'I') || + strncasecmp(subfolder, "inbox", 5) || + subfolder[5] != lmtpd_namespace.hier_sep) + return IMAP_MAILBOX_NONEXISTENT; + + + if (config_getswitch(IMAPOPT_ANYSIEVEFOLDER)) + createsievefolder = 1; + else if ((subf = config_getstring(IMAPOPT_AUTOSIEVEFOLDERS)) != NULL) { + /* + * mbox contains only the subfolder to be created name + */ + mbox = strchr(subfolder, (int) lmtpd_namespace.hier_sep); + mbox++; + + + /* Roll through subf */ + next_subf = (char *) subf; + while (*next_subf) { + for (p = next_subf ; isspace((int) *p) || *p == SEP ; p++); + for (next_subf = p ; *next_subf && *next_subf != SEP ; next_subf++); + for (q = next_subf ; q > p && (isspace((int) *q) || *q == SEP || !*q); q--); + + if (!*p) continue; + /* + * This is a preliminary length check based on the assumption + * that the *final* internal format will be something + * like user.userid.subfolder(s). + */ + len = q - p + 1; + + if (len > sizeof(f) - strlen(userid) - 5) + r = IMAP_MAILBOX_BADNAME; + + if (!r) { + strncpy(f, p, len); + f[len] = '\0'; + + if (!strcmp(f, mbox)) { + createsievefolder = 1; + break; + } + } + } + } + + if (createsievefolder) { + /* Translate any separators in user */ + if (userid) mboxname_hiersep_tointernal(&lmtpd_namespace, userid, 0); + + r = (*lmtpd_namespace.mboxname_tointernal) (&lmtpd_namespace, + subfolder, userid, foldername); + if (!r) + r = mboxlist_createmailbox(foldername, MAILBOX_FORMAT_NORMAL, NULL, + 1, userid, auth_state, 0, 0, 0); + if (!r) { + mboxlist_changesub(foldername, userid, auth_state, 1, 1); + syslog(LOG_DEBUG, "Ondemand subfolder: User %s, subfolder %s creation succeeded.", + userid, foldername); + return 0; + } else { + syslog(LOG_ERR, "Requested subfolder: User %s, subfolder %s creation failed. %s", + userid, foldername,error_message(r)); + return r; + } + } else + return IMAP_MAILBOX_NONEXISTENT; +} diff -Naur cyrus-imapd-2.2.2-BETA/lib/imapoptions cyrus-imapd-2.2.2-BETA-autosievefolder.uncompiled/lib/imapoptions --- cyrus-imapd-2.2.2-BETA/lib/imapoptions 2003-10-22 23:05:15.000000000 +0300 +++ cyrus-imapd-2.2.2-BETA-autosievefolder.uncompiled/lib/imapoptions 2003-12-15 14:33:49.492904000 +0200 @@ -544,6 +544,15 @@ /* If enabled, lmtpd will look for Sieve scripts in user's home directories: ~user/.sieve. */ +{ "anysievefolder", 0, SWITCH } +/* It must be "yes" in order to permit the autocreation of any INBOX subfolder + requested by a sieve filter, through the "fileinto" action. (default = no) */ + +{ "autosievefolders", NULL, STRING } +/* It is a "|" separated list of subfolders of INBOX that will be automatically created, + if requested by a sieve filter, through the "fileinto" action. (default = null) + i.e. autosievefolders: Junk | Spam */ + { "singleinstancestore", 1, SWITCH } /* If enabled, lmtpd and nntpd attempt to only write one copy of a message per partition and create hard links, resulting in a potentially large