cf58a5
diff -Naur cyrus-imapd-2.3.1/README.autocreate cyrus-imapd-2.3.1-autocreate/README.autocreate
cf58a5
--- cyrus-imapd-2.3.1/README.autocreate	1970-01-01 02:00:00 +0200
cf58a5
+++ cyrus-imapd-2.3.1-autocreate/README.autocreate	2006-01-19 03:53:40 +0200
cf58a5
@@ -0,0 +1,211 @@
d0d307
+Cyrus IMAP autocreate Inbox patch
d0d307
+----------------------------------
d0d307
+
d0d307
+NOTE : This patch has been created at the University of Athens. For more info, as well 
cf58a5
+as more patches on Cyrus IMAPD server, please visit http://email.uoa.gr/ 
d0d307
+
d0d307
+The design of Cyrus IMAP server does not predict the automatic creation of users'
d0d307
+INBOX folders. The creation of a user's INBOX is considered to be an external task,
cf58a5
+that has to be completed as part of the user email account creation procedure. 
cf58a5
+Hence, to create a new email account the site administrator has to:
cf58a5
+
cf58a5
+  a) Include the new account in the user database for the authentication procedure
cf58a5
+     (e.g. sasldb, shadow, mysql, ldap).
cf58a5
+  b) Create the corresponding INBOX folder. 
d0d307
+
d0d307
+Alternatively, the user, if succesfully authenticated, may create his own INBOX folder,
d0d307
+as long as the configuration of the site allows it (see "autocreatequota" in imapd.conf).
cf58a5
+Unlike what not careful readers may think, enabling the "autocreatequota" option, doesn't 
d0d307
+lead to the automatic INBOX folder creation by Cyrus IMAP server.
d0d307
+In fact, "autocreate" means that the IMAP clients are allowed to automatically create 
d0d307
+the user INBOX. 
d0d307
+
d0d307
+This patch adds the functionality of automatic creation of the users' INBOX folders into
d0d307
+the Cyrus IMAP server. It is implemented as two features, namely the  "create on login"
d0d307
+and "create on post".
d0d307
+ 
d0d307
+
d0d307
+
d0d307
+Create on login
d0d307
+===============
d0d307
+This feauture provides automatic creation of a user's INBOX folder when all of the 
d0d307
+following requirements are met:
d0d307
+
d0d307
+i)  The user has succesfully passed the authentication procedure.
d0d307
+
cf58a5
+ii) The user's authorisation ID (typically the same as the user's
d0d307
+authentication ID)  doesn't belong to the imap_admins or admins
d0d307
+accounts (see imapd.conf).
d0d307
+
d0d307
+iii) The "autocreatequota" option in the imap configuration file 
d0d307
+has been set to a non zero value. 
d0d307
+
cf58a5
+iv) The corresponding to the user's authorisation ID INBOX folder
d0d307
+does not exist.
d0d307
+
d0d307
+The user's first login is the most typical case when all four requirements are met. 
cf58a5
+Note that if the authenticated ID is allowed to proxy to another account for which 
d0d307
+all of the above requirements are met, the corresponding INBOX folder for that account 
d0d307
+will be created.
d0d307
+
d0d307
+
d0d307
+
d0d307
+Create on post
d0d307
+==============
d0d307
+This feauture provides automatic creation of a user's INBOX folder when all of the 
d0d307
+following requirements are met. 
d0d307
+
cf58a5
+i) An email message addressed to the user has been received.  
d0d307
+
d0d307
+ii) The recipient is not any of the imap_admins or admins accounts. 
cf58a5
+Note that passing emails to admins or imap_admins accounts from 
d0d307
+the MTA to LMTP should be avoided in any case.
d0d307
+
d0d307
+iii) The recipient's INBOX does not exist.
d0d307
+
d0d307
+iv) The "autocreatequota" option in the imap configuration file 
d0d307
+has been set to a non zero value. 
d0d307
+
d0d307
+v) The "createonpost" option in the imap configuration file 
d0d307
+has been switched on. 
d0d307
+
d0d307
+
d0d307
+Besides the automatic creation of INBOX folder, additional functionalities are
d0d307
+provided:
d0d307
+
cf58a5
+  (A) Automatic creation of INBOX subfolders controlled by "autocreateinboxfolders"
d0d307
+configuration option. eg 
d0d307
+
d0d307
+autocreateinboxfolders: sent|drafts|spam|templates
d0d307
+
cf58a5
+  (B) Automatic subscription of INBOX subfolders controlled by "autosubscribeinboxfolders"
d0d307
+configuration option. eg
d0d307
+
d0d307
+autosubscribeinboxfolders: sent|spam
d0d307
+
d0d307
+Obviously, only subscription to subfolders included in the "autocreateinboxfolder"
cf58a5
+list is meaningful. 
d0d307
+
cf58a5
+  (C) Automatic subscription to shared folders (bulletin boards). The user gets
d0d307
+automatically subscribed to the shared folders declared in the "autosubscribesharedfolders"
d0d307
+configuration option in imapd.conf.
d0d307
+eg autosubscribesharedfolders: public_folder | public_folder.subfolder
d0d307
+
d0d307
+In order the above action to succeed, the shared folder has to pre-exist the INBOX creation
cf58a5
+and the user must have the appropriate permissions in order to be able to subscribe to the
d0d307
+shared folder.
d0d307
+
d0d307
+* A new config option has been added. 'autosubscribe_all_sharedfolders' is a yes/no
d0d307
+option. When set to yes, the user is automatically subscribed to all shared folders one 
d0d307
+has permission to subscribe to. Please, note that when this option is set to yes, then
d0d307
+'autosubscribesharedfolders' option is overriden.
d0d307
+
cf58a5
+  (D) Automatic creation of a predefined default sieve script.
d0d307
+
d0d307
+This is very useful when a default sieve script is used for every user. Usually, a
d0d307
+default anti-spam script may me be written in a file and copied to each user
d0d307
+sieve scripts upon the INBOX creation. The imapd.conf options that have been added
d0d307
+are 'autocreate_sieve_script', 'autocreate_sieve_compiledscript' and 
d0d307
+'generate_compiled_sieve_script'. 
d0d307
+
d0d307
+autocreate_sieve_script configuration option refers to the full path of the file 
d0d307
+that contains the sieve script. The default value is null and if no file is defined,
d0d307
+then no default script is created upon INBOX creation. (The feature is disabled)
d0d307
+eg autocreate_sieve_script: /etc/default_sieve_script
d0d307
+
d0d307
+autocreate_sieve_compiledscript configuration option refers to the full path of the
d0d307
+file that contains the bytecode compiled sieve script. If this filename is defined
d0d307
+in imapd.conf and the file exists, then it is automatically copied in the user's sieve
d0d307
+directory. If it is not defined, then a bytecode sieve script gets on the fly compiled
d0d307
+by the daemon. 
d0d307
+eg autocreate_sieve_compiledscript: /etc/default_sieve_script.bc
d0d307
+
d0d307
+generate_compiled_sieve_script is a boolean option that triggers the compilation of the 
d0d307
+source sieve script to bytecode sieve script. The file that the bytecode script will
d0d307
+be saved is pointed by autocreate_sieve_compiledscript.
d0d307
+
d0d307
+Ways of compiling a sieve script : 
d0d307
+1. Compile a sieve script using the standard sievec utility, distributed by CMU
d0d307
+2. Compile a sieve script using the compile_sieve utility, released by UoA. This 
d0d307
+   tool is almost identical to the sievec utility, with the difference that it 
d0d307
+   reads the input and output file from autocreate_sieve_script and 
d0d307
+   autocreate_sieve_compiledscript options in imapd.conf
d0d307
+3. Let cyrus create a compiled sieve script using a source script. Cyrus can be
d0d307
+   instructed to save the compiled script any time a compiled script does not exist.
d0d307
+
d0d307
+NOTES : 
d0d307
+1. In order this functionality to work, the following requirements must have been met:
d0d307
+   - 'sieveusehomedir' option must be 'no' in the configuration (default).
d0d307
+   - 'sievedir' option must have a valid value.
d0d307
+2. Currently, this patch checks the validity of the source script while generating a 
d0d307
+   bytecode compiled script, but not the validity of the bytecode sieve script file.
d0d307
+   The administrator should make sure that the provided files contain a valid sieve
d0d307
+   script as well as the compiled script is updated every time the source script changes.
d0d307
+
d0d307
+
cf58a5
+  (E) The administrator may control for which users and/or groups may the INBOXes 
cf58a5
+automatically be created. The autocreate_users option restricts the groups
cf58a5
+for which the patch will create the mailboxes. 
cf58a5
+
cf58a5
+The default value of autocreate_users is anyone. So, if not set at all, the patch will 
cf58a5
+work for all users. However, one may set:
cf58a5
+
cf58a5
+autocreate_users: user1 user2 group:group1 group:group2
cf58a5
+
cf58a5
+In that case, the INBOX will be created only for user1, user2 and the users that belong 
cf58a5
+to group1 and group2. 
cf58a5
+
cf58a5
+More refined control per service is provided by the options imap_autocreate_users,
cf58a5
+pop3_autocreate_users and lmtp_autocreate_users. These options override the 
cf58a5
+autocreate_users option and offer per service control.
cf58a5
+
cf58a5
+Example: 
cf58a5
+One may want to restrict the create on post functionality only for a specific group
cf58a5
+of users. To achieve this, the following lines must be added in the imapd.conf file:
cf58a5
+
cf58a5
+createonpost: yes
cf58a5
+lmtp_autocreate_users: group:groupname
cf58a5
+
cf58a5
+
d0d307
+
d0d307
+Issues to be considered 
d0d307
+=======================
d0d307
+
d0d307
+I) In order to use the create on post feauture one should be absolutely sure that: 
cf58a5
+a) The MTA checks the validity of the email recipient before sending the email to
d0d307
+LMTP. This is an RFC821 requirement. This usually expands to "the mta should be 
d0d307
+able to use the account database as user mailbox database". 
cf58a5
+b) Only authorised accounts/services can talk to LMTP.
d0d307
+
d0d307
+II) Especially in the case of imap logins, the current patch implementation checks
d0d307
+for the INBOX folder existence upon login, causing an extra mailbox lookup in most 
d0d307
+of the cases. 
d0d307
+A better approach would be to chase the "IMAP_MAILBOX_NONEXISTENT" error code and
d0d307
+check if the error is associated with an INBOX folder. However, this would mess up
d0d307
+Cyrus code. The way it was implemented may not have been the most performance
cf58a5
+optimised, but it produces a much cleaner and simple patch.
d0d307
+
d0d307
+
d0d307
+
d0d307
+Virtual Domains Support
d0d307
+=======================
d0d307
+
d0d307
+Virtual domains are supported by all versions of the patch for cyrus-imapd-2.2.1-BETA and 
d0d307
+later. However, it is not possible to declare different INBOX subfolders to be created or 
d0d307
+shared folders to be subscribed to for every domain.
d0d307
+
d0d307
+
d0d307
+
d0d307
+Things to be done
d0d307
+=================
d0d307
+
cf58a5
+1. Support MUPDATE
cf58a5
+
cf58a5
+It is within the developers' intentions to support the mupdate protocol, but serious 
cf58a5
+design issues on future cyrus releases have to resolved first.
d0d307
+
cf58a5
+2. Select different attributes (quota, partition, sieve filter, etc) depending on the group
cf58a5
+a user belongs to. 
d0d307
+
cf58a5
+For more information and updates please visit http://email.uoa.gr/projects/cyrus/autocreate
d0d307
+
cf58a5
diff -Naur cyrus-imapd-2.3.1/imap/Makefile.in cyrus-imapd-2.3.1-autocreate/imap/Makefile.in
cf58a5
--- cyrus-imapd-2.3.1/imap/Makefile.in	2005-12-13 17:28:43 +0200
cf58a5
+++ cyrus-imapd-2.3.1-autocreate/imap/Makefile.in	2006-01-19 03:53:41 +0200
cf58a5
@@ -101,7 +101,7 @@
d0d307
 	convert_code.o duplicate.o saslclient.o saslserver.o signals.o \
d0d307
 	annotate.o search_engines.o squat.o squat_internal.o mbdump.o \
cf58a5
 	imapparse.o telemetry.o user.o notify.o protocol.o idle.o quota_db.o \
cf58a5
-	sync_log.o $(SEEN) mboxkey.o
cf58a5
+	sync_log.o autosieve.o $(SEEN) mboxkey.o
d0d307
 
cf58a5
 IMAPDOBJS=pushstats.o backend.o imapd.o proxy.o imap_proxy.o index.o \
cf58a5
 	tls.o version.o
cf58a5
@@ -118,7 +118,7 @@
d0d307
 	fud smmapd reconstruct quota mbpath ipurge \
d0d307
 	cyrdump chk_cyrus cvt_cyrusdb deliver ctl_mboxlist \
d0d307
 	ctl_deliver ctl_cyrusdb squatter mbexamine cyr_expire arbitron \
cf58a5
-	unexpunge @IMAP_PROGS@
cf58a5
+	unexpunge compile_sieve @IMAP_PROGS@
d0d307
 
d0d307
 BUILTSOURCES = imap_err.c imap_err.h pushstats.c pushstats.h \
d0d307
 	lmtpstats.c lmtpstats.h xversion.h mupdate_err.c mupdate_err.h \
cf58a5
@@ -185,7 +185,7 @@
d0d307
 ### Services
d0d307
 idled: idled.o mutex_fake.o libimap.a $(DEPLIBS)
d0d307
 	$(CC) $(LDFLAGS) -o idled \
d0d307
-	 idled.o mutex_fake.o libimap.a $(DEPLIBS) $(LIBS)
d0d307
+	 idled.o mutex_fake.o libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307
 
cf58a5
 lmtpd: lmtpd.o proxy.o $(LMTPOBJS) $(SIEVE_OBJS) mutex_fake.o \
cf58a5
 	 libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(SERVICE)
cf58a5
@@ -199,10 +199,10 @@
cf58a5
 	 $(SERVICE) lmtpd.o proxy.o $(LMTPOBJS) $(SIEVE_OBJS) \
cf58a5
 	 mutex_fake.o libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS) $(LIB_WRAP)
d0d307
 
d0d307
-imapd: xversion $(IMAPDOBJS) mutex_fake.o libimap.a $(DEPLIBS) $(SERVICE)
d0d307
+imapd: xversion $(IMAPDOBJS) mutex_fake.o libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(SERVICE)
d0d307
 	$(CC) $(LDFLAGS) -o imapd \
d0d307
 	 $(SERVICE) $(IMAPDOBJS) mutex_fake.o \
d0d307
-	libimap.a $(DEPLIBS) $(LIBS) $(LIB_WRAP)
d0d307
+	libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS) $(LIB_WRAP)
d0d307
 
d0d307
 imapd.pure: $(IMAPDOBJS) mutex_fake.o libimap.a $(DEPLIBS) $(SERVICE)
d0d307
 	$(PURIFY) $(PUREOPT) $(CC) $(LDFLAGS) -o imapd.pure \
cf58a5
@@ -219,7 +219,7 @@
d0d307
 	$(CC) $(LDFLAGS) -o mupdate \
d0d307
 	 $(SERVICETHREAD) mupdate.o mupdate-slave.o mupdate-client.o \
d0d307
 	 mutex_pthread.o tls.o libimap.a \
d0d307
-	 $(DEPLIBS) $(LIBS) $(LIB_WRAP) -lpthread
d0d307
+	 $(SIEVE_LIBS) $(DEPLIBS) $(LIBS) $(LIB_WRAP) -lpthread
d0d307
 
d0d307
 mupdate.pure: mupdate.o mupdate-slave.o mupdate-client.o mutex_pthread.o \
d0d307
 	libimap.a $(DEPLIBS)
cf58a5
@@ -228,118 +228,122 @@
d0d307
 	 mutex_pthread.o libimap.a $(DEPLIBS) $(LIBS) $(LIB_WRAP) -lpthread
d0d307
 
cf58a5
 pop3d: pop3d.o proxy.o backend.o tls.o mutex_fake.o libimap.a \
cf58a5
-	$(DEPLIBS) $(SERVICE)
cf58a5
+	$(SIEVE_LIBS) $(DEPLIBS) $(SERVICE) 
cf58a5
 	$(CC) $(LDFLAGS) -o pop3d pop3d.o proxy.o backend.o tls.o $(SERVICE) \
d0d307
-	 mutex_fake.o libimap.a $(DEPLIBS) $(LIBS) $(LIB_WRAP)
d0d307
+	 mutex_fake.o libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS) $(LIB_WRAP)
d0d307
 
cf58a5
 nntpd: nntpd.o proxy.o backend.o index.o smtpclient.o spool.o tls.o \
cf58a5
-	 mutex_fake.o nntp_err.o libimap.a $(DEPLIBS) $(SERVICE)
cf58a5
+	 mutex_fake.o nntp_err.o libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(SERVICE) 
cf58a5
 	$(CC) $(LDFLAGS) -o nntpd nntpd.o proxy.o backend.o index.o spool.o \
d0d307
 	 smtpclient.o tls.o $(SERVICE) mutex_fake.o nntp_err.o \
d0d307
-	 libimap.a $(DEPLIBS) $(LIBS) $(LIB_WRAP)
d0d307
+	 libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS) $(LIB_WRAP)
d0d307
 
d0d307
-fud: fud.o libimap.a mutex_fake.o $(DEPLIBS) $(SERVICE)
cf58a5
+fud: fud.o libimap.a mutex_fake.o $(DEPLIBS) $(SERVICE) $(SIEVE_LIBS)
d0d307
 	$(CC) $(LDFLAGS) -o fud $(SERVICE) fud.o mutex_fake.o libimap.a \
d0d307
-	$(DEPLIBS) $(LIBS) $(LIB_WRAP)
d0d307
+	$(SIEVE_LIBS) $(DEPLIBS) $(LIBS) $(LIB_WRAP)
d0d307
 
d0d307
-smmapd: smmapd.o libimap.a mutex_fake.o $(DEPLIBS) $(SERVICE)
cf58a5
+smmapd: smmapd.o libimap.a mutex_fake.o $(DEPLIBS) $(SERVICE) $(SIEVE_LIBS)
d0d307
 	$(CC) $(LDFLAGS) -o smmapd $(SERVICE) smmapd.o mutex_fake.o libimap.a \
d0d307
-	$(DEPLIBS) $(LIBS) $(LIB_WRAP)
d0d307
+	$(SIEVE_LIBS) $(DEPLIBS) $(LIBS) $(LIB_WRAP)
d0d307
 
cf58a5
 sync_server: sync_server.o sync_support.o sync_commit.o \
cf58a5
-	imapparse.o tls.o libimap.a mutex_fake.o $(DEPLIBS) $(SERVICE)
cf58a5
+	imapparse.o tls.o libimap.a mutex_fake.o $(DEPLIBS) $(SERVICE) $(SIEVE_LIBS)
cf58a5
 	$(CC) $(LDFLAGS) -o \
cf58a5
 	sync_server sync_server.o sync_support.o sync_commit.o \
cf58a5
 	imapparse.o tls.o $(SERVICE) libimap.a mutex_fake.o \
cf58a5
-	$(DEPLIBS) $(LIBS) $(LIB_WRAP)
cf58a5
+	$(SIEVE_LIBS) $(DEPLIBS) $(LIBS) $(LIB_WRAP)
cf58a5
 
d0d307
 ### Command Line Utilities
d0d307
-arbitron: arbitron.o $(CLIOBJS) libimap.a $(DEPLIBS)
cf58a5
+arbitron: arbitron.o $(CLIOBJS) libimap.a $(DEPLIBS) $(SIEVE_LIBS)
d0d307
 	$(CC) $(LDFLAGS) -o arbitron arbitron.o $(CLIOBJS) \
d0d307
-	libimap.a $(DEPLIBS) $(LIBS)
d0d307
+	libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307
 
d0d307
-cvt_cyrusdb: cvt_cyrusdb.o mutex_fake.o libimap.a $(DEPLIBS)
cf58a5
+cvt_cyrusdb: cvt_cyrusdb.o mutex_fake.o libimap.a $(DEPLIBS) $(SIEVE_LIBS)
d0d307
 	$(CC) $(LDFLAGS) -o cvt_cyrusdb cvt_cyrusdb.o $(CLIOBJS) \
d0d307
-	libimap.a $(DEPLIBS) $(LIBS)
d0d307
+	libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307
 
d0d307
-chk_cyrus: chk_cyrus.o mutex_fake.o libimap.a $(DEPLIBS)
cf58a5
+chk_cyrus: chk_cyrus.o mutex_fake.o libimap.a $(DEPLIBS) $(SIEVE_LIBS)
d0d307
 	$(CC) $(LDFLAGS) -o chk_cyrus chk_cyrus.o $(CLIOBJS) \
d0d307
-	libimap.a $(DEPLIBS) $(LIBS)
d0d307
+	libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307
 
cf58a5
-deliver: deliver.o $(LMTPOBJS) proxy.o mutex_fake.o libimap.a $(DEPLIBS)
cf58a5
+deliver: deliver.o $(LMTPOBJS) proxy.o mutex_fake.o libimap.a $(DEPLIBS) $(SIEVE_LIBS)
cf58a5
 	$(CC) $(LDFLAGS) -o deliver deliver.o $(LMTPOBJS) proxy.o \
d0d307
-	mutex_fake.o libimap.a $(DEPLIBS) $(LIBS)
d0d307
+	mutex_fake.o libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307
 
d0d307
-ctl_deliver: ctl_deliver.o $(CLIOBJS) libimap.a $(DEPLIBS)
cf58a5
+ctl_deliver: ctl_deliver.o $(CLIOBJS) libimap.a $(DEPLIBS) $(SIEVE_LIBS)
d0d307
 	$(CC) $(LDFLAGS) -o \
d0d307
-	 $@ ctl_deliver.o $(CLIOBJS) libimap.a $(DEPLIBS) $(LIBS)
d0d307
+	 $@ ctl_deliver.o $(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307
 
d0d307
-ctl_mboxlist: ctl_mboxlist.o mupdate-client.o $(CLIOBJS) libimap.a $(DEPLIBS)
cf58a5
+ctl_mboxlist: ctl_mboxlist.o mupdate-client.o $(CLIOBJS) libimap.a $(DEPLIBS) $(SIEVE_LIBS)
d0d307
 	$(CC) $(LDFLAGS) -o $@ ctl_mboxlist.o mupdate-client.o $(CLIOBJS) \
d0d307
-	libimap.a $(DEPLIBS) $(LIBS)
d0d307
+	libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307
 
d0d307
-ctl_cyrusdb: ctl_cyrusdb.o $(CLIOBJS) libimap.a $(DEPLIBS)
cf58a5
+ctl_cyrusdb: ctl_cyrusdb.o $(CLIOBJS) libimap.a $(DEPLIBS) $(SIEVE_LIBS)
d0d307
 	$(CC) $(LDFLAGS) -o \
d0d307
-	 $@ ctl_cyrusdb.o $(CLIOBJS) libimap.a $(DEPLIBS) $(LIBS)
d0d307
+	 $@ ctl_cyrusdb.o $(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307
 
d0d307
-cyr_expire: cyr_expire.o $(CLIOBJS) libimap.a $(DEPLIBS)
cf58a5
+cyr_expire: cyr_expire.o $(CLIOBJS) libimap.a $(DEPLIBS) $(SIEVE_LIBS)
d0d307
 	$(CC) $(LDFLAGS) -o $@ cyr_expire.o $(CLIOBJS) \
d0d307
-	libimap.a $(DEPLIBS) $(LIBS)
d0d307
+	libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307
 
d0d307
-fetchnews: fetchnews.o $(CLIOBJS) libimap.a $(DEPLIBS)
cf58a5
+fetchnews: fetchnews.o $(CLIOBJS) libimap.a $(DEPLIBS) $(SIEVE_LIBS)
d0d307
 	$(CC) $(LDFLAGS) -o \
d0d307
-	 $@ fetchnews.o $(CLIOBJS) libimap.a $(DEPLIBS) $(LIBS)
d0d307
+	 $@ fetchnews.o $(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307
 
d0d307
-squatter: squatter.o index.o squat_build.o $(CLIOBJS) libimap.a $(DEPLIBS)
cf58a5
+squatter: squatter.o index.o squat_build.o $(CLIOBJS) libimap.a $(DEPLIBS) $(SIEVE_LIBS)
d0d307
 	$(CC) $(LDFLAGS) -o squatter squatter.o index.o squat_build.o \
d0d307
-	$(CLIOBJS) libimap.a $(DEPLIBS) $(LIBS)
d0d307
+	$(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307
 
d0d307
-mbpath: mbpath.o $(CLIOBJS) libimap.a $(DEPLIBS)
cf58a5
+mbpath: mbpath.o $(CLIOBJS) libimap.a $(DEPLIBS) $(SIEVE_LIBS)
d0d307
 	$(CC) $(LDFLAGS) -o mbpath mbpath.o $(CLIOBJS) libimap.a \
d0d307
-	$(DEPLIBS) $(LIBS)
cf58a5
-
d0d307
-ipurge: ipurge.o $(CLIOBJS) libimap.a $(DEPLIBS)
cf58a5
+	$(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
cf58a5
+	
cf58a5
+ipurge: ipurge.o $(CLIOBJS) libimap.a $(DEPLIBS) $(SIEVE_LIBS)
d0d307
 	$(CC) $(LDFLAGS) -o ipurge ipurge.o $(CLIOBJS) \
d0d307
-	libimap.a $(DEPLIBS) $(LIBS)
d0d307
+	libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307
 
d0d307
-cyrdump: cyrdump.o index.o $(CLIOBJS) libimap.a $(DEPLIBS)
cf58a5
+cyrdump: cyrdump.o index.o $(CLIOBJS) libimap.a $(DEPLIBS) $(SIEVE_LIBS)
d0d307
 	$(CC) $(LDFLAGS) -o cyrdump cyrdump.o index.o $(CLIOBJS) \
d0d307
-	libimap.a $(DEPLIBS) $(LIBS)
d0d307
+	libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307
 
d0d307
-mbexamine: mbexamine.o $(CLIOBJS) libimap.a $(DEPLIBS)
cf58a5
+mbexamine: mbexamine.o $(CLIOBJS) libimap.a $(DEPLIBS) $(SIEVE_LIBS)
d0d307
 	$(CC) $(LDFLAGS) -o \
d0d307
-	 mbexamine mbexamine.o $(CLIOBJS) libimap.a $(DEPLIBS) $(LIBS)
d0d307
+	 mbexamine mbexamine.o $(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307
 
d0d307
-reconstruct: reconstruct.o $(CLIOBJS) libimap.a $(DEPLIBS)
cf58a5
+reconstruct: reconstruct.o $(CLIOBJS) libimap.a $(DEPLIBS) $(SIEVE_LIBS)
d0d307
 	$(CC) $(LDFLAGS) -o \
d0d307
-	 reconstruct reconstruct.o $(CLIOBJS) libimap.a $(DEPLIBS) $(LIBS)
d0d307
+	 reconstruct reconstruct.o $(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307
 
d0d307
-quota: quota.o $(CLIOBJS) libimap.a $(DEPLIBS)
cf58a5
+quota: quota.o $(CLIOBJS) libimap.a $(DEPLIBS) $(SIEVE_LIBS)
d0d307
 	$(CC) $(LDFLAGS) -o quota quota.o $(CLIOBJS) \
d0d307
-	libimap.a $(DEPLIBS) $(LIBS)
d0d307
+	libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307
 
d0d307
-tls_prune: tls_prune.o tls.o $(CLIOBJS) libimap.a $(DEPLIBS)
cf58a5
+tls_prune: tls_prune.o tls.o $(CLIOBJS) libimap.a $(DEPLIBS) $(SIEVE_LIBS)
d0d307
 	$(CC) $(LDFLAGS) -o \
d0d307
-	 $@ tls_prune.o tls.o $(CLIOBJS) libimap.a $(DEPLIBS) $(LIBS)
d0d307
+	 $@ tls_prune.o tls.o $(CLIOBJS) libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
d0d307
 
cf58a5
-unexpunge: unexpunge.o $(CLIOBJS) libimap.a $(DEPLIBS)
cf58a5
+unexpunge: unexpunge.o $(CLIOBJS) libimap.a $(DEPLIBS) $(SIEVE_LIBS)
cf58a5
 	$(CC) $(LDFLAGS) -o $@ unexpunge.o $(CLIOBJS) \
cf58a5
-	libimap.a $(DEPLIBS) $(LIBS)
cf58a5
+	libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
cf58a5
 
cf58a5
-make_md5: make_md5.o libimap.a mutex_fake.o $(DEPLIBS)
cf58a5
-	$(CC) $(LDFLAGS) -o make_md5 make_md5.o libimap.a mutex_fake.o $(DEPLIBS) $(LIBS)
cf58a5
+make_md5: make_md5.o libimap.a mutex_fake.o $(DEPLIBS) $(SIEVE_LIBS)
cf58a5
+	$(CC) $(LDFLAGS) -o make_md5 make_md5.o libimap.a mutex_fake.o $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
cf58a5
 
cf58a5
 sync_client: sync_client.o sync_support.o \
cf58a5
-	backend.o tls.o imapparse.o libimap.a mutex_fake.o $(DEPLIBS)
cf58a5
+	backend.o tls.o imapparse.o libimap.a mutex_fake.o $(DEPLIBS) $(SIEVE_LIBS)
cf58a5
 	$(CC) $(LDFLAGS) -o \
cf58a5
 	sync_client sync_client.o sync_support.o \
cf58a5
-	backend.o tls.o imapparse.o libimap.a mutex_fake.o $(DEPLIBS) $(LIBS)
cf58a5
+	backend.o tls.o imapparse.o libimap.a mutex_fake.o $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
cf58a5
 
cf58a5
 sync_reset: sync_reset.o sync_support.o sync_commit.o \
cf58a5
-	libimap.a mutex_fake.o $(DEPLIBS)
cf58a5
+	libimap.a mutex_fake.o $(DEPLIBS) $(SIEVE_LIBS)
cf58a5
 	$(CC) $(LDFLAGS) -o \
cf58a5
 	sync_reset sync_reset.o sync_support.o sync_commit.o \
cf58a5
-	libimap.a mutex_fake.o $(DEPLIBS) $(LIBS)
cf58a5
+	libimap.a mutex_fake.o $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
cf58a5
+
cf58a5
+compile_sieve: compile_sieve.o libimap.a $(DEPLIBS) $(SIEVE_LIBS)
cf58a5
+	$(CC) $(LDFLAGS) -o compile_sieve compile_sieve.o $(CLIOBJS) \
cf58a5
+	libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS)
cf58a5
 
d0d307
 ### Other Misc Targets
d0d307
 
cf58a5
diff -Naur cyrus-imapd-2.3.1/imap/autosieve.c cyrus-imapd-2.3.1-autocreate/imap/autosieve.c
cf58a5
--- cyrus-imapd-2.3.1/imap/autosieve.c	1970-01-01 02:00:00 +0200
cf58a5
+++ cyrus-imapd-2.3.1-autocreate/imap/autosieve.c	2006-01-19 03:53:41 +0200
d0d307
@@ -0,0 +1,587 @@
d0d307
+#include <stdio.h>
d0d307
+#include <stdlib.h>
d0d307
+#include <string.h>
d0d307
+
d0d307
+#ifdef HAVE_UNISTD_H
d0d307
+#include <unistd.h>
d0d307
+#endif
d0d307
+
d0d307
+#include <errno.h>
d0d307
+#include <sys types.h="">
d0d307
+#include <sys stat.h="">
d0d307
+#include <sys uio.h="">
d0d307
+#include <fcntl.h>
d0d307
+#include <ctype.h>
d0d307
+#include <time.h>
d0d307
+#include <syslog.h>
d0d307
+#include <com_err.h>
d0d307
+#include <config.h>
d0d307
+
d0d307
+#include "global.h"
d0d307
+#include "util.h"
d0d307
+#include "mailbox.h"
d0d307
+#include "imap_err.h"
d0d307
+#include "sieve_interface.h"
d0d307
+#include "script.h"
d0d307
+
d0d307
+#define TIMSIEVE_FAIL 	-1
d0d307
+#define TIMSIEVE_OK 	0
d0d307
+#define MAX_FILENAME	1024
d0d307
+
d0d307
+static int get_script_name(char *sievename, size_t buflen, const char *filename);
d0d307
+static int get_script_dir(char *sieve_script_dir, size_t buflen, char *userid, const char *sieve_dir);
d0d307
+int autoadd_sieve(char *userid, const char *source_script);
d0d307
+
d0d307
+static void fatal(const char *s, int code);
d0d307
+static void foo(void);
d0d307
+static int sieve_notify(void *ac __attribute__((unused)),
d0d307
+                        void *interp_context __attribute__((unused)),
d0d307
+                        void *script_context __attribute__((unused)),
d0d307
+                        void *message_context __attribute__((unused)),
d0d307
+                        const char **errmsg __attribute__((unused)));
d0d307
+static int mysieve_error(int lineno, const char *msg,
d0d307
+                  void *i __attribute__((unused)), void *s);
d0d307
+static int is_script_parsable(FILE *stream, char **errstr, sieve_script_t **ret);
d0d307
+
d0d307
+
d0d307
+sieve_vacation_t vacation2 = {
d0d307
+    0,                          /* min response */
d0d307
+    0,                          /* max response */
d0d307
+    (sieve_callback *) &foo,    /* autorespond() */
d0d307
+    (sieve_callback *) &foo     /* send_response() */
d0d307
+};
d0d307
+
d0d307
+
d0d307
+/*
d0d307
+ * Find the name of the sieve script
d0d307
+ * given the source script and compiled script names
d0d307
+ */
d0d307
+static int get_script_name(char *sievename, size_t buflen, const char *filename)
d0d307
+{
d0d307
+  char *p;
d0d307
+  int r;
d0d307
+
d0d307
+  p = strrchr(filename, '/');
d0d307
+  if (p == NULL)
d0d307
+      p = (char *) filename;
d0d307
+  else
d0d307
+      p++;
d0d307
+
d0d307
+  r = strlcpy(sievename, p, buflen) - buflen;
d0d307
+  return (r >= 0 || r == -buflen ? 1 : 0);
d0d307
+}
d0d307
+
d0d307
+
d0d307
+/*
d0d307
+ * Find the directory where the sieve scripts of the user
d0d307
+ * reside
d0d307
+ */
d0d307
+static int get_script_dir(char *sieve_script_dir, size_t buflen, char *userid, const char *sieve_dir)
d0d307
+{
d0d307
+    char *user = NULL, *domain = NULL;
d0d307
+
d0d307
+    /* Setup the user and the domain */
d0d307
+    if(config_virtdomains && (domain = strchr(userid, '@'))) {
d0d307
+        user = (char *) xmalloc((domain - userid +1) * sizeof(char));
d0d307
+        strlcpy(user, userid, domain - userid + 1);
d0d307
+        domain++;
d0d307
+    } else
d0d307
+        user = userid;
d0d307
+
d0d307
+    /*  Find the dir path where the sieve scripts of the user will reside */   
d0d307
+    if (config_virtdomains && domain) {
d0d307
+         if(snprintf(sieve_script_dir, buflen, "%s%s%c/%s/%c/%s/",
d0d307
+              sieve_dir, FNAME_DOMAINDIR, dir_hash_c(domain), domain, dir_hash_c(user), user) >= buflen) {
d0d307
+                 free(user);
d0d307
+                 return 1;
d0d307
+ 	 }
d0d307
+    } else {
d0d307
+         if(snprintf(sieve_script_dir, buflen, "%s/%c/%s/", 
d0d307
+     	      sieve_dir, dir_hash_c(user), user) >= buflen) 
d0d307
+                 return 1;
d0d307
+    }
d0d307
+
d0d307
+    /* Free the xmalloced user memory, reserved above */
d0d307
+    if(user != userid)
d0d307
+        free(user);
d0d307
+
d0d307
+    return 0;
d0d307
+}
d0d307
+
d0d307
+int autoadd_sieve(char *userid, const char *source_script)
d0d307
+{   
d0d307
+    sieve_script_t *s = NULL;
d0d307
+    bytecode_info_t *bc = NULL;
d0d307
+    char *err = NULL;
d0d307
+    FILE *in_stream, *out_fp;
d0d307
+    int out_fd, in_fd, r, k;
d0d307
+    int do_compile = 0;
d0d307
+    const char *sieve_dir = NULL;
d0d307
+    const char *compiled_source_script = NULL;
d0d307
+    char sievename[MAX_FILENAME];
d0d307
+    char sieve_script_name[MAX_FILENAME];
d0d307
+    char sieve_script_dir[MAX_FILENAME];
d0d307
+    char sieve_bcscript_name[MAX_FILENAME];
d0d307
+    char sieve_default[MAX_FILENAME];
d0d307
+    char sieve_tmpname[MAX_FILENAME];
d0d307
+    char sieve_bctmpname[MAX_FILENAME];
d0d307
+    char sieve_bclink_name[MAX_FILENAME];
d0d307
+    char buf[4096];
d0d307
+    mode_t oldmask;
d0d307
+    struct stat statbuf;
d0d307
+
d0d307
+    /* We don't support using the homedirectory, like timsieved */
d0d307
+    if (config_getswitch(IMAPOPT_SIEVEUSEHOMEDIR)) {
d0d307
+        syslog(LOG_WARNING,"autocreate_sieve: autocreate_sieve does not work with sieveusehomedir option in imapd.conf");
d0d307
+        return 1;
d0d307
+    }
d0d307
+
d0d307
+    /* Check if sievedir is defined in imapd.conf */
d0d307
+    if(!(sieve_dir = config_getstring(IMAPOPT_SIEVEDIR))) { 
d0d307
+        syslog(LOG_WARNING, "autocreate_sieve: sievedir option is not defined. Check imapd.conf");
d0d307
+        return 1;
d0d307
+    }
d0d307
+
d0d307
+    /* Check if autocreate_sieve_compiledscript is defined in imapd.conf */
d0d307
+    if(!(compiled_source_script  = config_getstring(IMAPOPT_AUTOCREATE_SIEVE_COMPILEDSCRIPT))) {
d0d307
+        syslog(LOG_WARNING, "autocreate_sieve: autocreate_sieve_compiledscript option is not defined. Compiling it");
d0d307
+        do_compile = 1;
d0d307
+    }
d0d307
+
d0d307
+    if(get_script_dir(sieve_script_dir, sizeof(sieve_script_dir), userid, sieve_dir)) {
d0d307
+        syslog(LOG_WARNING, "autocreate_sieve: Cannot find sieve scripts directory");
d0d307
+        return 1;
d0d307
+    }
d0d307
+
d0d307
+    if (get_script_name(sievename, sizeof(sievename), source_script)) {
d0d307
+        syslog(LOG_WARNING, "autocreate_sieve: Invalid sieve script %s", source_script);
d0d307
+        return 1;
d0d307
+    }
d0d307
+
d0d307
+    if(snprintf(sieve_tmpname, sizeof(sieve_tmpname), "%s%s.script.NEW",sieve_script_dir, sievename) >= sizeof(sieve_tmpname)) {
d0d307
+        syslog(LOG_WARNING, "autocreate_sieve: Invalid sieve path %s, %s, %s", sieve_dir, sievename, userid);
d0d307
+        return 1;
d0d307
+    }
d0d307
+    if(snprintf(sieve_bctmpname, sizeof(sieve_bctmpname), "%s%s.bc.NEW",sieve_script_dir, sievename) >= sizeof(sieve_bctmpname)) {
d0d307
+        syslog(LOG_WARNING, "autocreate_sieve: Invalid sieve path %s, %s, %s", sieve_dir, sievename, userid);
d0d307
+        return 1;
d0d307
+    }    
d0d307
+    if(snprintf(sieve_script_name, sizeof(sieve_script_name), "%s%s.script",sieve_script_dir, sievename) >= sizeof(sieve_script_name)) {
d0d307
+        syslog(LOG_WARNING, "autocreate_sieve: Invalid sieve path %s, %s, %s", sieve_dir, sievename, userid);
d0d307
+        return 1;
d0d307
+    }
d0d307
+    if(snprintf(sieve_bcscript_name, sizeof(sieve_bcscript_name), "%s%s.bc",sieve_script_dir, sievename) >= sizeof(sieve_bcscript_name)) {
d0d307
+        syslog(LOG_WARNING, "autocreate_sieve: Invalid sieve path %s, %s, %s", sieve_dir, sievename, userid);
d0d307
+        return 1;
d0d307
+    }
d0d307
+    if(snprintf(sieve_default, sizeof(sieve_default), "%s%s",sieve_script_dir,"defaultbc") >= sizeof(sieve_default)) {
d0d307
+        syslog(LOG_WARNING, "autocreate_sieve: Invalid sieve path %s, %s, %s", sieve_dir, sievename, userid);
d0d307
+        return 1;
d0d307
+    }
d0d307
+    if(snprintf(sieve_bclink_name, sizeof(sieve_bclink_name), "%s.bc", sievename) >= sizeof(sieve_bclink_name))  {
d0d307
+        syslog(LOG_WARNING, "autocreate_sieve: Invalid sieve path %s, %s, %s", sieve_dir, sievename, userid);
d0d307
+        return 1;
d0d307
+    }
d0d307
+
d0d307
+    /* Check if a default sieve filter alrady exists */
d0d307
+    if(!stat(sieve_default,&statbuf)) {
d0d307
+        syslog(LOG_WARNING,"autocreate_sieve: Default sieve script already exists");
d0d307
+        fclose(in_stream);
d0d307
+        return 1;
d0d307
+    }
d0d307
+
d0d307
+    /* Open the source script. if there is a problem with that exit */
d0d307
+    in_stream = fopen(source_script, "r");
d0d307
+    if(!in_stream) {
d0d307
+        syslog(LOG_WARNING,"autocreate_sieve: Unable to open sieve script %s. Check permissions",source_script);
d0d307
+        return 1;
d0d307
+    }
d0d307
+    
d0d307
+    
d0d307
+    /* 
d0d307
+     * At this point we start the modifications of the filesystem 
d0d307
+     */
d0d307
+
d0d307
+    /* Create the directory where the sieve scripts will reside */
d0d307
+    r = cyrus_mkdir(sieve_script_dir, 0755);
d0d307
+    if(r == -1) {
d0d307
+        /* If this fails we just leave */
d0d307
+        syslog(LOG_WARNING,"autocreate_sieve: Unable to create directory %s. Check permissions",sieve_script_name);
d0d307
+        return 1;
d0d307
+    }
d0d307
+
d0d307
+    /*
d0d307
+     * We open the file that will be used as the bc file. If this file exists, overwrite it 
d0d307
+     * since something bad has happened. We open the file here so that this error checking is
d0d307
+     * done before we try to open the rest of the files to start copying etc. 
d0d307
+     */
d0d307
+    out_fd = open(sieve_bctmpname, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
d0d307
+    if(out_fd < 0) {
d0d307
+        if(errno == EEXIST) {
d0d307
+            syslog(LOG_WARNING,"autocreate_sieve: File %s already exists. Probaly left over. Ignoring",sieve_bctmpname);
d0d307
+        } else if (errno == EACCES) {
d0d307
+            syslog(LOG_WARNING,"autocreate_sieve: No access to create file %s. Check permissions",sieve_bctmpname);
d0d307
+            fclose(in_stream);
d0d307
+            return 1;
d0d307
+        } else {
d0d307
+            syslog(LOG_WARNING,"autocreate_sieve: Unable to create %s. Unknown error",sieve_bctmpname);
d0d307
+            fclose(in_stream);
d0d307
+            return 1;
d0d307
+        }
d0d307
+    }
d0d307
+
d0d307
+    if(!do_compile && compiled_source_script && (in_fd = open(compiled_source_script, O_RDONLY)) != -1) {
d0d307
+        while((r = read(in_fd, buf, sizeof(buf))) > 0) {
d0d307
+            if((k=write(out_fd, buf,r)) < 0) {
d0d307
+                syslog(LOG_WARNING, "autocreate_sieve: Error writing to file: %s, error: %d", sieve_bctmpname, errno);
d0d307
+                close(out_fd);
d0d307
+                close(in_fd);
d0d307
+                fclose(in_stream);
d0d307
+                unlink(sieve_bctmpname);
d0d307
+                return 1;
d0d307
+           }
d0d307
+        } 
d0d307
+
d0d307
+        if(r == 0) { /* EOF */
d0d307
+            close(out_fd);
d0d307
+            close(in_fd);
d0d307
+        } else if (r < 0) {
d0d307
+            syslog(LOG_WARNING, "autocreate_sieve: Error reading compiled script file: %s. Will try to compile it", 
d0d307
+                           compiled_source_script);
d0d307
+            close(in_fd);
d0d307
+            do_compile = 1;
d0d307
+            if(lseek(out_fd, 0, SEEK_SET)) {
d0d307
+                syslog(LOG_WARNING, "autocreate_sieve: Major IO problem. Aborting");
d0d307
+                return 1;
d0d307
+            }
d0d307
+        }
d0d307
+        close(in_fd);
d0d307
+    } else {
d0d307
+        if(compiled_source_script)
d0d307
+              syslog(LOG_WARNING,"autocreate_sieve: Problem opening compiled script file: %s. Compiling it", compiled_source_script);
d0d307
+        do_compile = 1;
d0d307
+    }
d0d307
+
d0d307
+
d0d307
+    /* Because we failed to open a precompiled bc sieve script, we compile one */
d0d307
+    if(do_compile) {
d0d307
+       if(is_script_parsable(in_stream,&err, &s) == TIMSIEVE_FAIL) {
d0d307
+            if(err && *err) {
d0d307
+               syslog(LOG_WARNING,"autocreate_sieve: Error while parsing script %s.",err);
d0d307
+               free(err);
d0d307
+            } else
d0d307
+                syslog(LOG_WARNING,"autocreate_sieve: Error while parsing script");
d0d307
+    
d0d307
+            unlink(sieve_bctmpname);
d0d307
+            fclose(in_stream);
d0d307
+            close(out_fd);
d0d307
+            return 1;
d0d307
+        }
d0d307
+
d0d307
+        /* generate the bytecode */
d0d307
+        if(sieve_generate_bytecode(&bc, s) == TIMSIEVE_FAIL) {
d0d307
+            syslog(LOG_WARNING,"autocreate_sieve: problem compiling sieve script");
d0d307
+            /* removing the copied script and cleaning up memory */
d0d307
+            unlink(sieve_bctmpname);
d0d307
+            sieve_script_free(&s);
d0d307
+            fclose(in_stream);
d0d307
+            close(out_fd);
d0d307
+            return 1;
d0d307
+        }
d0d307
+
d0d307
+        if(sieve_emit_bytecode(out_fd, bc) == TIMSIEVE_FAIL) {
d0d307
+            syslog(LOG_WARNING,"autocreate_sieve: problem emiting sieve script");
d0d307
+            /* removing the copied script and cleaning up memory */
d0d307
+            unlink(sieve_bctmpname);
d0d307
+            sieve_free_bytecode(&bc);
d0d307
+            sieve_script_free(&s);
d0d307
+            fclose(in_stream);
d0d307
+            close(out_fd);
d0d307
+            return 1;
d0d307
+        }
d0d307
+
d0d307
+        /* clean up the memory */
d0d307
+        sieve_free_bytecode(&bc);
d0d307
+        sieve_script_free(&s);
d0d307
+    }
d0d307
+
d0d307
+    close(out_fd);
d0d307
+    rewind(in_stream);
d0d307
+
d0d307
+    /* Copy the initial script */
d0d307
+    oldmask = umask(077);
d0d307
+    if((out_fp = fopen(sieve_tmpname, "w")) == NULL) {
d0d307
+        syslog(LOG_WARNING,"autocreate_sieve: Unable to open %s destination sieve script", sieve_tmpname);
d0d307
+        unlink(sieve_bctmpname);
d0d307
+        umask(oldmask);
d0d307
+        fclose(in_stream);
d0d307
+        return 1;
d0d307
+    }
d0d307
+    umask(oldmask);
d0d307
+
d0d307
+    while((r = fread(buf,sizeof(char), sizeof(buf), in_stream))) {
d0d307
+        if( fwrite(buf,sizeof(char), r, out_fp) != r) {
d0d307
+            syslog(LOG_WARNING,"autocreate_sieve: Problem writing to sieve script file: %s",sieve_tmpname);
d0d307
+            fclose(out_fp);
d0d307
+            unlink(sieve_tmpname);
d0d307
+            unlink(sieve_bctmpname);
d0d307
+            fclose(in_stream);
d0d307
+            return 1;
d0d307
+        }
d0d307
+    }
d0d307
+    
d0d307
+    if(feof(in_stream)) {
d0d307
+        fclose(out_fp);
d0d307
+    } else { /* ferror */
d0d307
+        fclose(out_fp);
d0d307
+        unlink(sieve_tmpname);
d0d307
+        unlink(sieve_bctmpname);
d0d307
+        fclose(in_stream);
d0d307
+        return 1;
d0d307
+    }
d0d307
+
d0d307
+    /* Renaming the necessary stuff */
d0d307
+    if(rename(sieve_tmpname, sieve_script_name)) {
d0d307
+        unlink(sieve_tmpname);
d0d307
+        unlink(sieve_bctmpname);
d0d307
+        return 1;
d0d307
+    }
d0d307
+
d0d307
+    if(rename(sieve_bctmpname, sieve_bcscript_name)) {
d0d307
+        unlink(sieve_bctmpname);
d0d307
+        unlink(sieve_bcscript_name);
d0d307
+        return 1;
d0d307
+    }
d0d307
+
d0d307
+    /* end now with the symlink */
d0d307
+    if(symlink(sieve_bclink_name, sieve_default)) {
d0d307
+        if(errno != EEXIST) {
d0d307
+            syslog(LOG_WARNING, "autocreate_sieve: problem making the default link.");
d0d307
+            /* Lets delete the files */
d0d307
+            unlink(sieve_script_name);
d0d307
+            unlink(sieve_bcscript_name);
d0d307
+        }
d0d307
+    }
d0d307
+
d0d307
+    /* 
d0d307
+     * If everything has succeeded AND we have compiled the script AND we have requested
d0d307
+     * to generate the global script so that it is not compiled each time then we create it.
d0d307
+     */
d0d307
+    if(do_compile && 
d0d307
+          config_getswitch(IMAPOPT_GENERATE_COMPILED_SIEVE_SCRIPT)) {
d0d307
+
d0d307
+        if(!compiled_source_script) {
d0d307
+            syslog(LOG_WARNING, "autocreate_sieve: To save a compiled sieve script, autocreate_sieve_compiledscript must have been defined in imapd.conf");
d0d307
+            return 0;
d0d307
+        }
d0d307
+
d0d307
+        if(snprintf(sieve_tmpname, sizeof(sieve_tmpname), "%s.NEW", compiled_source_script) >= sizeof(sieve_tmpname))
d0d307
+            return 0;
d0d307
+
d0d307
+        /*
d0d307
+         * Copy everything from the newly created bc sieve sieve script.
d0d307
+         */
d0d307
+        if((in_fd = open(sieve_bcscript_name, O_RDONLY))<0) {
d0d307
+            return 0;
d0d307
+        }
d0d307
+
d0d307
+        if((out_fd = open(sieve_tmpname, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) {
d0d307
+            if(errno == EEXIST) {
d0d307
+               /* Someone is already doing this so just bail out. */
d0d307
+               syslog(LOG_WARNING, "autocreate_sieve: %s already exists. Some other instance processing it, or it is left over", sieve_tmpname);
d0d307
+                close(in_fd);
d0d307
+                return 0; 
d0d307
+            } else if (errno == EACCES) {
d0d307
+                syslog(LOG_WARNING,"autocreate_sieve: No access to create file %s. Check permissions",sieve_tmpname);
d0d307
+                close(in_fd);
d0d307
+                return 0;
d0d307
+            } else {
d0d307
+                syslog(LOG_WARNING,"autocreate_sieve: Unable to create %s",sieve_tmpname);
d0d307
+                close(in_fd);
d0d307
+                return 0;
d0d307
+            }
d0d307
+        }
d0d307
+
d0d307
+        while((r = read(in_fd, buf, sizeof(buf))) > 0) {
d0d307
+            if((k = write(out_fd,buf,r)) < 0) {
d0d307
+                syslog(LOG_WARNING, "autocreate_sieve: Error writing to file: %s, error: %d", sieve_tmpname, errno);
d0d307
+                close(out_fd);
d0d307
+                close(in_fd);
d0d307
+                unlink(sieve_tmpname);
d0d307
+                return 0;
d0d307
+           }
d0d307
+        }
d0d307
+
d0d307
+        if(r == 0 ) { /*EOF */
d0d307
+            close(out_fd);
d0d307
+            close(in_fd);
d0d307
+        } else if (r < 0) {
d0d307
+                syslog(LOG_WARNING, "autocreate_sieve: Error writing to file: %s, error: %d", sieve_tmpname, errno);
d0d307
+                close(out_fd);
d0d307
+                close(in_fd);
d0d307
+                unlink(sieve_tmpname);
d0d307
+                return 0;
d0d307
+        }
d0d307
+
d0d307
+        /* Rename the temporary created sieve script to its final name. */
d0d307
+        if(rename(sieve_tmpname, compiled_source_script)) {
d0d307
+            if(errno != EEXIST) {
d0d307
+               unlink(sieve_tmpname);
d0d307
+               unlink(compiled_source_script);
d0d307
+        }
d0d307
+            return 0;
d0d307
+        }
d0d307
+
d0d307
+        syslog(LOG_NOTICE, "autocreate_sieve: Compiled sieve script was successfully saved in %s", compiled_source_script);
d0d307
+    }
d0d307
+
d0d307
+    return 0;
d0d307
+}
d0d307
+
d0d307
+static void fatal(const char *s, int code)
d0d307
+{   
d0d307
+    printf("Fatal error: %s (%d)\r\n", s, code);
d0d307
+    exit(1);
d0d307
+}
d0d307
+
d0d307
+/* to make larry's stupid functions happy :) */
d0d307
+static void foo(void)
d0d307
+{
d0d307
+    fatal("stub function called", 0);
d0d307
+}
d0d307
+
d0d307
+static int sieve_notify(void *ac __attribute__((unused)),
d0d307
+                        void *interp_context __attribute__((unused)),
d0d307
+                        void *script_context __attribute__((unused)),
d0d307
+                        void *message_context __attribute__((unused)),
d0d307
+                        const char **errmsg __attribute__((unused)))
d0d307
+{
d0d307
+    fatal("stub function called", 0);
d0d307
+    return SIEVE_FAIL;
d0d307
+}
d0d307
+
d0d307
+static int mysieve_error(int lineno, const char *msg,
d0d307
+                  void *i __attribute__((unused)), void *s)
d0d307
+{
d0d307
+    char buf[1024];
d0d307
+    char **errstr = (char **) s;
d0d307
+
d0d307
+    snprintf(buf, 80, "line %d: %s\r\n", lineno, msg);
d0d307
+    *errstr = (char *) xrealloc(*errstr, strlen(*errstr) + strlen(buf) + 30);
d0d307
+    syslog(LOG_DEBUG, "%s", buf);
d0d307
+    strcat(*errstr, buf);
d0d307
+
d0d307
+    return SIEVE_OK;
d0d307
+}
d0d307
+
d0d307
+/* end the boilerplate */
d0d307
+
d0d307
+/* returns TRUE or FALSE */
d0d307
+int is_script_parsable(FILE *stream, char **errstr, sieve_script_t **ret)
d0d307
+{
d0d307
+    sieve_interp_t *i;
d0d307
+    sieve_script_t *s;
d0d307
+    int res;
d0d307
+
d0d307
+    res = sieve_interp_alloc(&i, NULL);
d0d307
+    if (res != SIEVE_OK) {
d0d307
+        syslog(LOG_WARNING, "sieve_interp_alloc() returns %d\n", res);
d0d307
+        return TIMSIEVE_FAIL;
d0d307
+    }
d0d307
+
d0d307
+    res = sieve_register_redirect(i, (sieve_callback *) &foo);
d0d307
+    if (res != SIEVE_OK) {
d0d307
+        syslog(LOG_WARNING, "sieve_register_redirect() returns %d\n", res);
d0d307
+        return TIMSIEVE_FAIL;
d0d307
+    }
d0d307
+    res = sieve_register_discard(i, (sieve_callback *) &foo);
d0d307
+    if (res != SIEVE_OK) {
d0d307
+        syslog(LOG_WARNING, "sieve_register_discard() returns %d\n", res);
d0d307
+        return TIMSIEVE_FAIL;
d0d307
+    }
d0d307
+    res = sieve_register_reject(i, (sieve_callback *) &foo);
d0d307
+    if (res != SIEVE_OK) {
d0d307
+        syslog(LOG_WARNING, "sieve_register_reject() returns %d\n", res);
d0d307
+        return TIMSIEVE_FAIL;
d0d307
+    }
d0d307
+    res = sieve_register_fileinto(i, (sieve_callback *) &foo);
d0d307
+    if (res != SIEVE_OK) {
d0d307
+        syslog(LOG_WARNING, "sieve_register_fileinto() returns %d\n", res);
d0d307
+        return TIMSIEVE_FAIL;
d0d307
+    }
d0d307
+    res = sieve_register_keep(i, (sieve_callback *) &foo);
d0d307
+    if (res != SIEVE_OK) {
d0d307
+        syslog(LOG_WARNING, "sieve_register_keep() returns %d\n", res);
d0d307
+        return TIMSIEVE_FAIL;
d0d307
+    }
d0d307
+
d0d307
+    res = sieve_register_imapflags(i, NULL);
d0d307
+    if (res != SIEVE_OK) {
d0d307
+        syslog(LOG_WARNING, "sieve_register_imapflags() returns %d\n", res);
d0d307
+        return TIMSIEVE_FAIL;
d0d307
+    }
d0d307
+
d0d307
+    res = sieve_register_size(i, (sieve_get_size *) &foo);
d0d307
+    if (res != SIEVE_OK) {
d0d307
+        syslog(LOG_WARNING, "sieve_register_size() returns %d\n", res);
d0d307
+        return TIMSIEVE_FAIL;
d0d307
+    }
d0d307
+
d0d307
+    res = sieve_register_header(i, (sieve_get_header *) &foo);
d0d307
+    if (res != SIEVE_OK) {
d0d307
+        syslog(LOG_WARNING, "sieve_register_header() returns %d\n", res);
d0d307
+        return TIMSIEVE_FAIL;
d0d307
+    }
d0d307
+
d0d307
+    res = sieve_register_envelope(i, (sieve_get_envelope *) &foo);
d0d307
+    if (res != SIEVE_OK) {
d0d307
+        syslog(LOG_WARNING, "sieve_register_envelope() returns %d\n", res);
d0d307
+        return TIMSIEVE_FAIL;
d0d307
+    }
d0d307
+
d0d307
+    res = sieve_register_vacation(i, &vacation2);
d0d307
+    if (res != SIEVE_OK) {
d0d307
+        syslog(LOG_WARNING, "sieve_register_vacation() returns %d\n", res);
d0d307
+        return TIMSIEVE_FAIL;
d0d307
+    }
d0d307
+
d0d307
+    res = sieve_register_notify(i, &sieve_notify);
d0d307
+    if (res != SIEVE_OK) {
d0d307
+        syslog(LOG_WARNING, "sieve_register_notify() returns %d\n", res);
d0d307
+        return TIMSIEVE_FAIL;
d0d307
+    }
d0d307
+
d0d307
+    res = sieve_register_parse_error(i, &mysieve_error);
d0d307
+    if (res != SIEVE_OK) {
d0d307
+        syslog(LOG_WARNING, "sieve_register_parse_error() returns %d\n", res);
d0d307
+        return TIMSIEVE_FAIL;
d0d307
+    }
d0d307
+
d0d307
+    rewind(stream);
d0d307
+
d0d307
+    *errstr = (char *) xmalloc(20 * sizeof(char));
d0d307
+    strcpy(*errstr, "script errors:\r\n");
d0d307
+
d0d307
+    res = sieve_script_parse(i, stream, errstr, &s);
d0d307
+
d0d307
+    if (res == SIEVE_OK) {
d0d307
+        if(ret) {
d0d307
+            *ret = s;
d0d307
+        } else {
d0d307
+            sieve_script_free(&s);
d0d307
+        }
d0d307
+        free(*errstr);
d0d307
+        *errstr = NULL;
d0d307
+    }
d0d307
+
d0d307
+    /* free interpreter */
d0d307
+    sieve_interp_free(&i);
d0d307
+
d0d307
+    return (res == SIEVE_OK) ? TIMSIEVE_OK : TIMSIEVE_FAIL;
d0d307
+}
d0d307
+
d0d307
+/*
d0d307
+ * Btw the initial date of this patch is Sep, 02 2004 which is the birthday of
d0d307
+ * Pavlos. Author of cyrusmaster. So consider this patch as his birthday present
d0d307
+ */
d0d307
+
cf58a5
diff -Naur cyrus-imapd-2.3.1/imap/compile_sieve.c cyrus-imapd-2.3.1-autocreate/imap/compile_sieve.c
cf58a5
--- cyrus-imapd-2.3.1/imap/compile_sieve.c	1970-01-01 02:00:00 +0200
cf58a5
+++ cyrus-imapd-2.3.1-autocreate/imap/compile_sieve.c	2006-01-19 03:53:41 +0200
d0d307
@@ -0,0 +1,364 @@
d0d307
+/* This tool compiles the sieve script from a command
d0d307
+line so that it can be used wby the autoadd patch */
d0d307
+#include <stdio.h>
d0d307
+#include <stdlib.h>
d0d307
+
d0d307
+#include <config.h>
d0d307
+#include <string.h>
d0d307
+#ifdef HAVE_UNISTD_H
d0d307
+#include <unistd.h>
d0d307
+#endif
d0d307
+#include <errno.h>
d0d307
+#include <sys types.h="">
d0d307
+#include <sys stat.h="">
d0d307
+#include <sys uio.h="">
d0d307
+#include <fcntl.h>
d0d307
+#include <ctype.h>
d0d307
+#include <time.h>
d0d307
+#include <com_err.h>
d0d307
+
d0d307
+#include "global.h"
d0d307
+
d0d307
+#include "util.h"
d0d307
+#include "mailbox.h"
d0d307
+#include "imap_err.h"
d0d307
+#include "sieve_interface.h"
d0d307
+#include "script.h"
d0d307
+
d0d307
+#include <pwd.h>
d0d307
+
d0d307
+#define TIMSIEVE_FAIL 		-1
d0d307
+#define TIMSIEVE_OK 		0
d0d307
+#define MAX_FILENAME_SIZE	100
d0d307
+
d0d307
+/* Needed by libconfig */
d0d307
+const int config_need_data = 0;
d0d307
+
d0d307
+static int is_script_parsable(FILE *stream, char **errstr, sieve_script_t **ret);
d0d307
+
d0d307
+static void fatal(const char *s, int code)
d0d307
+{   
d0d307
+    printf("Fatal error: %s (%d)\r\n", s, code);
d0d307
+
d0d307
+    exit(1);
d0d307
+}
d0d307
+
d0d307
+void usage(void)
d0d307
+{
d0d307
+    fprintf(stderr,
d0d307
+            "Usage:\n\tcompile_sieve [-C <altconfig>] [-i <infile> -o <outfile>]\n");
d0d307
+    exit(-1);
d0d307
+}
d0d307
+
d0d307
+
d0d307
+int main (int argc, char **argv)
d0d307
+{   
d0d307
+
d0d307
+    sieve_script_t *s = NULL;
d0d307
+    bytecode_info_t *bc = NULL;
d0d307
+    char *err = NULL;
d0d307
+    FILE *in_stream;
d0d307
+    int  out_fd,r, k, opt;
d0d307
+    char *source_script = NULL;
d0d307
+    char *compiled_source_script = NULL;
d0d307
+    mode_t oldmask;
d0d307
+    struct stat statbuf;
d0d307
+    char *alt_config = NULL;
d0d307
+    extern char *optarg;
d0d307
+    char sieve_tmpname[MAX_MAILBOX_NAME+1];
d0d307
+
d0d307
+    if (geteuid() == 0) fatal("must run as the Cyrus user", EC_USAGE);
d0d307
+
d0d307
+    while((opt = getopt(argc, argv, "C:i:o:")) != EOF) {
d0d307
+        switch (opt) {
d0d307
+            case 'C': /* alt config file */
d0d307
+	        alt_config =  optarg;
d0d307
+	        break;
d0d307
+	    case 'i': /* input script file */
d0d307
+		source_script = optarg;
d0d307
+		break;
d0d307
+	    case 'o': /* output script file */
d0d307
+		compiled_source_script = optarg;
d0d307
+		break;
d0d307
+	    default:
d0d307
+	        usage();
d0d307
+		break;
d0d307
+	}
d0d307
+    }
d0d307
+
d0d307
+    if(source_script && !compiled_source_script) {
d0d307
+	    fprintf(stderr, "No output file was defined\n");
d0d307
+	    usage();
d0d307
+    } else if (!source_script && compiled_source_script) {
d0d307
+	    fprintf(stderr, "No input file was defined\n");
d0d307
+	    usage();
d0d307
+    }	
d0d307
+
d0d307
+    /*
d0d307
+     * If no <infile> has been defined, then read them from
d0d307
+     * the configuration file.
d0d307
+     */
d0d307
+    if (!source_script && !compiled_source_script) { 
d0d307
+	    cyrus_init(alt_config, "compile_sieve", 0);
d0d307
+
d0d307
+	    /* Initially check if we want to have the sieve script created */
d0d307
+	    if(!(source_script = (char *) config_getstring(IMAPOPT_AUTOCREATE_SIEVE_SCRIPT))) {
d0d307
+        	fprintf(stderr,"autocreate_sieve_script option not defined. Check imapd.conf\n");
d0d307
+	        return 1;
d0d307
+	    }
d0d307
+
d0d307
+	    /* Check if we have an already compiled sieve script*/
d0d307
+	    if(!(compiled_source_script = (char *) config_getstring(IMAPOPT_AUTOCREATE_SIEVE_COMPILEDSCRIPT))) {
d0d307
+	        fprintf(stderr, "autocreate_sieve_compiledscript option not defined. Check imapd.conf\n");
d0d307
+		return 1;
d0d307
+	    }
d0d307
+
d0d307
+	    if(!strrchr(source_script,'/') || !strrchr(compiled_source_script,'/')) {
d0d307
+       		/* 
d0d307
+		 * At this point the only think that is inconsistent is the directory 
d0d307
+		 * that was created. But if the user will have any sieve scripts then 
d0d307
+		 * they will eventually go there, so no big deal 
d0d307
+		 */
d0d307
+	        fprintf(stderr, 
d0d307
+			"In imapd.conf the full path of the filenames must be defined\n");
d0d307
+	       	return 1;
d0d307
+	    }
d0d307
+    }
d0d307
+
d0d307
+    printf("input file : %s, output file : %s\n", source_script, compiled_source_script);
d0d307
+
d0d307
+
d0d307
+    if(strlen(compiled_source_script) + sizeof(".NEW") + 1 > sizeof(sieve_tmpname)) {
d0d307
+	    fprintf(stderr, "Filename %s is too big\n", compiled_source_script);
d0d307
+	    return 1;
d0d307
+    }
d0d307
+    	
d0d307
+    snprintf(sieve_tmpname, sizeof(sieve_tmpname), "%s.NEW", compiled_source_script);
d0d307
+
d0d307
+    in_stream = fopen(source_script,"r");
d0d307
+
d0d307
+    if(!in_stream) {
d0d307
+        fprintf(stderr,"Unable to open %s source sieve script\n",source_script);
d0d307
+        return;
d0d307
+    }
d0d307
+
d0d307
+    /* 
d0d307
+     * We open the file that will be used as the bc file. If this file exists, overwrite it 
d0d307
+     * since something bad has happened. We open the file here so that this error checking is
d0d307
+     * done before we try to open the rest of the files to start copying etc. 
d0d307
+     */
d0d307
+    out_fd = open(sieve_tmpname, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
d0d307
+    if(out_fd < 0) {
d0d307
+        if(errno == EEXIST) {
d0d307
+            fprintf(stderr, "File %s already exists\n", sieve_tmpname);
d0d307
+        } else if (errno == EACCES) {
d0d307
+            fprintf(stderr,"No access to create file %s. Please check that you have the correct permissions\n",
d0d307
+			    sieve_tmpname);
d0d307
+        } else {
d0d307
+            fprintf(stderr,"Unable to create %s. Please check that you have the correct permissions\n", 
d0d307
+			    sieve_tmpname);
d0d307
+        }
d0d307
+	
d0d307
+	fclose(in_stream);
d0d307
+	return 1;
d0d307
+    }
d0d307
+
d0d307
+    if(is_script_parsable(in_stream,&err, &s) == TIMSIEVE_FAIL) {
d0d307
+        if(err && *err) {
d0d307
+           fprintf(stderr, "Error while parsing script %s\n",err);
d0d307
+           free(err);
d0d307
+        }
d0d307
+        else
d0d307
+            fprintf(stderr,"Error while parsing script\n");
d0d307
+            unlink(sieve_tmpname);
d0d307
+	    fclose(in_stream);
d0d307
+	    close(out_fd);
d0d307
+        return;
d0d307
+   }
d0d307
+
d0d307
+
d0d307
+    /* generate the bytecode */
d0d307
+    if(sieve_generate_bytecode(&bc,s) == TIMSIEVE_FAIL) {
d0d307
+        fprintf(stderr,"Error occured while compiling sieve script\n");
d0d307
+        /* removing the copied script and cleaning up memory */
d0d307
+        unlink(sieve_tmpname);
d0d307
+        sieve_script_free(&s);
d0d307
+        fclose(in_stream);
d0d307
+        close(out_fd);
d0d307
+        return;
d0d307
+    }
d0d307
+    if(sieve_emit_bytecode(out_fd,bc) == TIMSIEVE_FAIL) {
d0d307
+        fprintf(stderr, "Error occured while emitting sieve script\n");
d0d307
+        unlink(sieve_tmpname);
d0d307
+        sieve_free_bytecode(&bc);
d0d307
+        sieve_script_free(&s);
d0d307
+        fclose(in_stream);
d0d307
+        close(out_fd);
d0d307
+        return;
d0d307
+    }
d0d307
+
d0d307
+    /* clean up the memory */
d0d307
+    sieve_free_bytecode(&bc);
d0d307
+    sieve_script_free(&s);
d0d307
+
d0d307
+    close(out_fd);
d0d307
+
d0d307
+    if(rename(sieve_tmpname, compiled_source_script)) {
d0d307
+        if(errno != EEXIST) {
d0d307
+            unlink(sieve_tmpname);
d0d307
+            unlink(compiled_source_script);
d0d307
+            return 1;
d0d307
+        }
d0d307
+    }
d0d307
+    return 0;
d0d307
+}
d0d307
+
d0d307
+
d0d307
+/* to make larry's stupid functions happy :) */
d0d307
+static void foo(void)
d0d307
+{
d0d307
+    fatal("stub function called", 0);
d0d307
+}
d0d307
+
d0d307
+extern sieve_vacation_t vacation2;/* = {
d0d307
+    0,                          / min response /
d0d307
+    0,                          / max response /
d0d307
+    (sieve_callback *) &foo,    / autorespond() /
d0d307
+    (sieve_callback *) &foo     / send_response() /
d0d307
+}; */
d0d307
+
d0d307
+static int sieve_notify(void *ac __attribute__((unused)),
d0d307
+                        void *interp_context __attribute__((unused)),
d0d307
+                        void *script_context __attribute__((unused)),
d0d307
+                        void *message_context __attribute__((unused)),
d0d307
+                        const char **errmsg __attribute__((unused)))
d0d307
+{
d0d307
+    fatal("stub function called", 0);
d0d307
+    return SIEVE_FAIL;
d0d307
+}
d0d307
+
d0d307
+static int mysieve_error(int lineno, const char *msg,
d0d307
+                  void *i __attribute__((unused)), void *s)
d0d307
+{
d0d307
+    char buf[1024];
d0d307
+    char **errstr = (char **) s;
d0d307
+
d0d307
+    snprintf(buf, 80, "line %d: %s\r\n", lineno, msg);
d0d307
+    *errstr = (char *) xrealloc(*errstr, strlen(*errstr) + strlen(buf) + 30);
d0d307
+    fprintf(stderr, "%s\n", buf);
d0d307
+    strcat(*errstr, buf);
d0d307
+
d0d307
+    return SIEVE_OK;
d0d307
+}
d0d307
+
d0d307
+/* end the boilerplate */
d0d307
+
d0d307
+/* returns TRUE or FALSE */
d0d307
+int is_script_parsable(FILE *stream, char **errstr, sieve_script_t **ret)
d0d307
+{
d0d307
+    sieve_interp_t *i;
d0d307
+    sieve_script_t *s;
d0d307
+    int res;
d0d307
+
d0d307
+    res = sieve_interp_alloc(&i, NULL);
d0d307
+    if (res != SIEVE_OK) {
d0d307
+        fprintf(stderr, "sieve_interp_alloc() returns %d\n", res);
d0d307
+        return TIMSIEVE_FAIL;
d0d307
+    }
d0d307
+
d0d307
+    res = sieve_register_redirect(i, (sieve_callback *) &foo);
d0d307
+    if (res != SIEVE_OK) {
d0d307
+        fprintf(stderr, "sieve_register_redirect() returns %d\n", res);
d0d307
+        return TIMSIEVE_FAIL;
d0d307
+    }
d0d307
+    res = sieve_register_discard(i, (sieve_callback *) &foo);
d0d307
+    if (res != SIEVE_OK) {
d0d307
+        fprintf(stderr, "sieve_register_discard() returns %d\n", res);
d0d307
+        return TIMSIEVE_FAIL;
d0d307
+    }
d0d307
+    res = sieve_register_reject(i, (sieve_callback *) &foo);
d0d307
+    if (res != SIEVE_OK) {
d0d307
+        fprintf(stderr, "sieve_register_reject() returns %d\n", res);
d0d307
+        return TIMSIEVE_FAIL;
d0d307
+    }
d0d307
+    res = sieve_register_fileinto(i, (sieve_callback *) &foo);
d0d307
+    if (res != SIEVE_OK) {
d0d307
+        fprintf(stderr, "sieve_register_fileinto() returns %d\n", res);
d0d307
+        return TIMSIEVE_FAIL;
d0d307
+    }
d0d307
+    res = sieve_register_keep(i, (sieve_callback *) &foo);
d0d307
+    if (res != SIEVE_OK) {
d0d307
+        fprintf(stderr, "sieve_register_keep() returns %d\n", res);
d0d307
+        return TIMSIEVE_FAIL;
d0d307
+    }
d0d307
+
d0d307
+    res = sieve_register_imapflags(i, NULL);
d0d307
+    if (res != SIEVE_OK) {
d0d307
+        fprintf(stderr, "sieve_register_imapflags() returns %d\n", res);
d0d307
+        return TIMSIEVE_FAIL;
d0d307
+    }
d0d307
+
d0d307
+    res = sieve_register_size(i, (sieve_get_size *) &foo);
d0d307
+    if (res != SIEVE_OK) {
d0d307
+        fprintf(stderr, "sieve_register_size() returns %d\n", res);
d0d307
+        return TIMSIEVE_FAIL;
d0d307
+    }
d0d307
+
d0d307
+    res = sieve_register_header(i, (sieve_get_header *) &foo);
d0d307
+    if (res != SIEVE_OK) {
d0d307
+        fprintf(stderr, "sieve_register_header() returns %d\n", res);
d0d307
+        return TIMSIEVE_FAIL;
d0d307
+    }
d0d307
+
d0d307
+    res = sieve_register_envelope(i, (sieve_get_envelope *) &foo);
d0d307
+    if (res != SIEVE_OK) {
d0d307
+        fprintf(stderr, "sieve_register_envelope() returns %d\n", res);
d0d307
+        return TIMSIEVE_FAIL;
d0d307
+    }
d0d307
+
d0d307
+    res = sieve_register_vacation(i, &vacation2);
d0d307
+    if (res != SIEVE_OK) {
d0d307
+        fprintf(stderr, "sieve_register_vacation() returns %d\n", res);
d0d307
+        return TIMSIEVE_FAIL;
d0d307
+    }
d0d307
+
d0d307
+    res = sieve_register_notify(i, &sieve_notify);
d0d307
+    if (res != SIEVE_OK) {
d0d307
+        fprintf(stderr, "sieve_register_notify() returns %d\n", res);
d0d307
+        return TIMSIEVE_FAIL;
d0d307
+    }
d0d307
+
d0d307
+    res = sieve_register_parse_error(i, &mysieve_error);
d0d307
+    if (res != SIEVE_OK) {
d0d307
+        fprintf(stderr, "sieve_register_parse_error() returns %d\n", res);
d0d307
+        return TIMSIEVE_FAIL;
d0d307
+    }
d0d307
+
d0d307
+    rewind(stream);
d0d307
+
d0d307
+    *errstr = (char *) xmalloc(20 * sizeof(char));
d0d307
+    strcpy(*errstr, "script errors:\r\n");
d0d307
+
d0d307
+    res = sieve_script_parse(i, stream, errstr, &s);
d0d307
+
d0d307
+    if (res == SIEVE_OK) {
d0d307
+        if(ret) {
d0d307
+            *ret = s;
d0d307
+        } else {
d0d307
+            sieve_script_free(&s);
d0d307
+        }
d0d307
+        free(*errstr);
d0d307
+        *errstr = NULL;
d0d307
+    }
d0d307
+
d0d307
+    /* free interpreter */
d0d307
+    sieve_interp_free(&i);
d0d307
+
d0d307
+    return (res == SIEVE_OK) ? TIMSIEVE_OK : TIMSIEVE_FAIL;
d0d307
+}
d0d307
+
d0d307
+
d0d307
+
d0d307
+
d0d307
+
d0d307
+
cf58a5
diff -Naur cyrus-imapd-2.3.1/imap/imapd.c cyrus-imapd-2.3.1-autocreate/imap/imapd.c
cf58a5
--- cyrus-imapd-2.3.1/imap/imapd.c	2005-12-13 21:35:50 +0200
cf58a5
+++ cyrus-imapd-2.3.1-autocreate/imap/imapd.c	2006-01-19 03:53:41 +0200
cf58a5
@@ -193,6 +193,7 @@
d0d307
 void motd_file(int fd);
d0d307
 void shut_down(int code);
d0d307
 void fatal(const char *s, int code);
d0d307
+void autocreate_inbox(void);
d0d307
 
d0d307
 void cmdloop(void);
d0d307
 void cmd_login(char *tag, char *user);
cf58a5
@@ -1889,6 +1890,43 @@
d0d307
 }
d0d307
 
d0d307
 /*
d0d307
+ * Autocreate Inbox and subfolders upon login
d0d307
+ */
d0d307
+void autocreate_inbox()
d0d307
+{
d0d307
+    char inboxname[MAX_MAILBOX_NAME+1];
d0d307
+    int autocreatequota;
d0d307
+    int r;
d0d307
+ 
d0d307
+    /*
d0d307
+     * Exlude admin's accounts
d0d307
+     */
d0d307
+    if (imapd_userisadmin || imapd_userisproxyadmin)
d0d307
+        return;
d0d307
+ 
d0d307
+    /*
d0d307
+     * Exclude anonymous
d0d307
+     */
d0d307
+    if (!strcmp(imapd_userid, "anonymous"))
d0d307
+        return;
d0d307
+ 
d0d307
+    if ((autocreatequota = config_getint(IMAPOPT_AUTOCREATEQUOTA))) {
d0d307
+        /* This is actyally not required
d0d307
+           as long as the lenght of userid is ok */
d0d307
+           r = (*imapd_namespace.mboxname_tointernal) (&imapd_namespace,
d0d307
+                                      "INBOX", imapd_userid, inboxname);
d0d307
+           if (!r)
cf58a5
+               r = mboxlist_lookup(inboxname, NULL, NULL);
d0d307
+ 
cf58a5
+           if (r == IMAP_MAILBOX_NONEXISTENT) {
cf58a5
+                mboxlist_autocreateinbox(&imapd_namespace, imapd_userid,
d0d307
+                         imapd_authstate, inboxname, autocreatequota);
cf58a5
+	   }
d0d307
+     }
d0d307
+}
d0d307
+
d0d307
+
d0d307
+/*
d0d307
  * Perform a LOGIN command
d0d307
  */
d0d307
 void cmd_login(char *tag, char *user)
cf58a5
@@ -2047,6 +2085,9 @@
d0d307
 				strcspn(imapd_userid, "@") : 0);
d0d307
 
d0d307
     freebuf(&passwdbuf);
d0d307
+
d0d307
+    autocreate_inbox();
d0d307
+
d0d307
     return;
d0d307
 }
d0d307
 
cf58a5
@@ -2195,6 +2236,8 @@
d0d307
 				config_virtdomains ?
d0d307
 				strcspn(imapd_userid, "@") : 0);
d0d307
 
d0d307
+    autocreate_inbox();
d0d307
+
d0d307
     return;
d0d307
 }
d0d307
 
cf58a5
diff -Naur cyrus-imapd-2.3.1/imap/lmtpd.c cyrus-imapd-2.3.1-autocreate/imap/lmtpd.c
cf58a5
--- cyrus-imapd-2.3.1/imap/lmtpd.c	2005-12-13 01:10:59 +0200
cf58a5
+++ cyrus-imapd-2.3.1-autocreate/imap/lmtpd.c	2006-01-19 03:53:41 +0200
cf58a5
@@ -114,6 +114,8 @@
d0d307
 static FILE *spoolfile(message_data_t *msgdata);
d0d307
 static void removespool(message_data_t *msgdata);
d0d307
 
cf58a5
+static int autocreate_inbox(const char *user, const char *domain);
d0d307
+
d0d307
 /* current namespace */
d0d307
 static struct namespace lmtpd_namespace;
d0d307
 
cf58a5
@@ -924,6 +926,86 @@
d0d307
     exit(code);
d0d307
 }
d0d307
 
cf58a5
+
d0d307
+/*
d0d307
+ * Autocreate Inbox and subfolders upon login
d0d307
+ */
cf58a5
+int autocreate_inbox(const char *user, const char *domain)
d0d307
+{
cf58a5
+    struct auth_state *auth_state;
d0d307
+    char inboxname[MAX_MAILBOX_NAME+1];
cf58a5
+    char *rcpt_userid = NULL;
d0d307
+    int autocreatequota;
cf58a5
+    int r = 0;
cf58a5
+
cf58a5
+    if (user == NULL)
cf58a5
+          return IMAP_MAILBOX_NONEXISTENT;
cf58a5
+
cf58a5
+    if (domain != NULL) {
cf58a5
+	int k;
cf58a5
+	
cf58a5
+	rcpt_userid = (char *) xmalloc((strlen(user) + strlen(domain) + 2) * sizeof(char));
cf58a5
+	k = strlcpy(rcpt_userid, user, strlen(user) + 1);
cf58a5
+  	*(rcpt_userid + k) = '@';
cf58a5
+       strlcpy(rcpt_userid + k + 1, domain, strlen(domain) + 1);
cf58a5
+    } else {
cf58a5
+	rcpt_userid = (char *) user;
cf58a5
+    }
cf58a5
+
d0d307
+
d0d307
+    /*
d0d307
+     * Exclude anonymous
d0d307
+     */
cf58a5
+    if (!strcmp(rcpt_userid, "anonymous")) {
cf58a5
+	if (rcpt_userid != user) {
cf58a5
+	    free(rcpt_userid);
cf58a5
+	}
d0d307
+
cf58a5
+        return IMAP_MAILBOX_NONEXISTENT;
cf58a5
+    }
cf58a5
+    
d0d307
+    /*
d0d307
+     * Check for autocreatequota and createonpost
d0d307
+     */
d0d307
+    if (!(autocreatequota = config_getint(IMAPOPT_AUTOCREATEQUOTA)) ||
cf58a5
+        !(config_getswitch(IMAPOPT_CREATEONPOST))) {
cf58a5
+	    
cf58a5
+	if (rcpt_userid != user) {
cf58a5
+	    free(rcpt_userid);
cf58a5
+	}
cf58a5
+	
d0d307
+        return IMAP_MAILBOX_NONEXISTENT;
cf58a5
+     }
cf58a5
+
d0d307
+
d0d307
+    /*
d0d307
+     * Exclude admin's accounts
d0d307
+     */
cf58a5
+     auth_state = auth_newstate(rcpt_userid);
cf58a5
+     
cf58a5
+     if (global_authisa(auth_state, IMAPOPT_ADMINS)) {
cf58a5
+	if (rcpt_userid != user) {
cf58a5
+	    free(rcpt_userid);
cf58a5
+	}
d0d307
+
cf58a5
+        return IMAP_MAILBOX_NONEXISTENT;
cf58a5
+     }
cf58a5
+     
d0d307
+     r = (*lmtpd_namespace.mboxname_tointernal) (&lmtpd_namespace,
d0d307
+                                "INBOX", rcpt_userid, inboxname);
cf58a5
+     
d0d307
+     if (!r)
cf58a5
+ 	r = mboxlist_autocreateinbox(&lmtpd_namespace, rcpt_userid,
cf58a5
+                         auth_state, inboxname, autocreatequota);
cf58a5
+     
cf58a5
+     if (rcpt_userid != user) {
cf58a5
+	free(rcpt_userid);
cf58a5
+     }
cf58a5
+       
d0d307
+     return r;
d0d307
+}
d0d307
+
d0d307
+
cf58a5
 static int verify_user(const char *user, const char *domain, char *mailbox,
d0d307
 		       long quotacheck, struct auth_state *authstate)
d0d307
 {
cf58a5
@@ -967,6 +1049,15 @@
d0d307
 	 */
cf58a5
 	r = mlookup(namebuf, &server, &acl, NULL);
cf58a5
 
cf58a5
+	/* If user mailbox does not exist, then invoke autocreate inbox function */
cf58a5
+	if (r == IMAP_MAILBOX_NONEXISTENT) {
cf58a5
+	    r = autocreate_inbox(user, domain);
cf58a5
+
cf58a5
+	    /* Try to locate the mailbox again */
d0d307
+	    if (!r)
cf58a5
+		r = mlookup(namebuf, &server, &acl, NULL);
cf58a5
+	}
cf58a5
+
cf58a5
 	if (r == IMAP_MAILBOX_NONEXISTENT && !user &&
cf58a5
 	    config_getswitch(IMAPOPT_LMTP_FUZZY_MAILBOX_MATCH) &&
cf58a5
 	    /* see if we have a mailbox whose name is close */
cf58a5
@@ -993,6 +1084,7 @@
cf58a5
 			     aclcheck, (quotacheck < 0)
cf58a5
 			     || config_getswitch(IMAPOPT_LMTP_STRICT_QUOTA) ?
cf58a5
 			     quotacheck : 0);
d0d307
+
cf58a5
 	}
d0d307
     }
d0d307
 
cf58a5
diff -Naur cyrus-imapd-2.3.1/imap/mboxlist.c cyrus-imapd-2.3.1-autocreate/imap/mboxlist.c
cf58a5
--- cyrus-imapd-2.3.1/imap/mboxlist.c	2005-12-13 21:36:03 +0200
cf58a5
+++ cyrus-imapd-2.3.1-autocreate/imap/mboxlist.c	2006-01-19 03:53:41 +0200
d0d307
@@ -81,6 +81,12 @@
d0d307
 #include "mboxlist.h"
d0d307
 #include "quota.h"
d0d307
 
d0d307
+#ifdef USE_SIEVE
d0d307
+extern int autoadd_sieve(char *userid, 
d0d307
+		const char *source_script);
d0d307
+#endif
d0d307
+
d0d307
+
d0d307
 #define DB config_mboxlist_db
d0d307
 #define SUBDB config_subscription_db
d0d307
 
d0d307
@@ -98,11 +104,29 @@
d0d307
 static int mboxlist_changequota(const char *name, int matchlen, int maycreate,
d0d307
 				void *rock);
d0d307
 
d0d307
+static int mboxlist_autochangesub(char *name, int matchlen, int maycreate,
d0d307
+			      void *rock);
d0d307
+
d0d307
+static int mboxlist_autosubscribe_sharedfolders(struct namespace *namespace,
d0d307
+                        char *userid, char *auth_userid,
d0d307
+                        struct auth_state *auth_state);
d0d307
+
d0d307
 struct change_rock {
d0d307
     struct quota *quota;
d0d307
     struct txn **tid;
d0d307
 };
d0d307
 
d0d307
+/*
d0d307
+ * Struct needed to be passed as void *rock to
d0d307
+ * mboxlist_autochangesub();
d0d307
+ */
d0d307
+struct changesub_rock_st {
d0d307
+        char *userid;
d0d307
+        char *auth_userid;
d0d307
+        struct auth_state *auth_state;
d0d307
+};
d0d307
+
d0d307
+
d0d307
 #define FNAME_SUBSSUFFIX ".sub"
d0d307
 
d0d307
 /*
cf58a5
@@ -3245,3 +3269,349 @@
d0d307
 
d0d307
     return DB->abort(mbdb, tid);
d0d307
 }
d0d307
+
d0d307
+/*
d0d307
+ * Automatically subscribe user to *ALL* shared folders,
d0d307
+ * one has permissions to be subscribed to.
d0d307
+ * INBOX subfolders are excluded.
d0d307
+ */
d0d307
+static int mboxlist_autochangesub(char *name, int matchlen, int maycreate,
d0d307
+                        void *rock) {
d0d307
+
d0d307
+  struct changesub_rock_st *changesub_rock = (struct changesub_rock_st *) rock;
d0d307
+  char *userid = changesub_rock->userid;
d0d307
+  char *auth_userid = changesub_rock->auth_userid;
d0d307
+  struct auth_state *auth_state = changesub_rock->auth_state;
d0d307
+  int r;
d0d307
+
d0d307
+
d0d307
+  if((strlen(name) == 5 && !strncmp(name, "INBOX", 5)) || /* Exclude INBOX */
d0d307
+     (strlen(name) > 5  && !strncmp(name, "INBOX.",6)) || /* Exclude INBOX subfolders */
d0d307
+     (strlen(name) > 4  && !strncmp(name, "user.", 5)))   /* Exclude other users' folders */
d0d307
+	  return 0;
d0d307
+
d0d307
+     
d0d307
+  r = mboxlist_changesub(name, userid, auth_state, 1, 0);
d0d307
+
d0d307
+  if (r) {
d0d307
+      syslog(LOG_WARNING,
d0d307
+             "autosubscribe: User %s to folder %s, subscription failed: %s",
d0d307
+             auth_userid, name, error_message(r));
d0d307
+  } else {
d0d307
+      syslog(LOG_NOTICE,
d0d307
+             "autosubscribe: User %s to folder %s, subscription succeeded",
d0d307
+             auth_userid, name);
d0d307
+  }
d0d307
+
d0d307
+  return 0;
d0d307
+}
d0d307
+
d0d307
+#define SEP '|'
d0d307
+
d0d307
+/*
d0d307
+ * Automatically subscribe user to a shared folder.
d0d307
+ * Subscription is done successfully, if the shared
d0d307
+ * folder exists and the user has the necessary 
d0d307
+ * permissions.
d0d307
+ */
d0d307
+static int mboxlist_autosubscribe_sharedfolders(struct namespace *namespace,
d0d307
+                        char *userid, char *auth_userid,
d0d307
+                        struct auth_state *auth_state) {
d0d307
+        
d0d307
+    const char *sub ;
d0d307
+    char *p, *q, *next_sub;
d0d307
+    char folder[MAX_MAILBOX_NAME+1], name[MAX_MAILBOX_NAME+1], mailboxname[MAX_MAILBOX_NAME+1];
d0d307
+    int len;
d0d307
+    int r = 0;
d0d307
+    int subscribe_all_sharedfolders = 0;
d0d307
+
d0d307
+    subscribe_all_sharedfolders = config_getswitch(IMAPOPT_AUTOSUBSCRIBE_ALL_SHAREDFOLDERS);
d0d307
+
d0d307
+    /*
d0d307
+     * If subscribeallsharedfolders is set to yes in imapd.conf, then
d0d307
+     * subscribe user to every shared folder one has the apropriate 
d0d307
+     * permissions.
d0d307
+     */
d0d307
+    if(subscribe_all_sharedfolders) {
d0d307
+       char pattern[MAX_MAILBOX_PATH+1];
d0d307
+       struct changesub_rock_st changesub_rock;
d0d307
+
d0d307
+       strcpy(pattern, "*");
d0d307
+       changesub_rock.userid = userid;
d0d307
+       changesub_rock.auth_userid = auth_userid;
d0d307
+       changesub_rock.auth_state = auth_state;
d0d307
+
d0d307
+       r = mboxlist_findall(namespace, pattern, 0, userid,
d0d307
+                            auth_state, mboxlist_autochangesub, &changesub_rock);
d0d307
+
d0d307
+       return r;
d0d307
+    }
d0d307
+
d0d307
+    if ((sub=config_getstring(IMAPOPT_AUTOSUBSCRIBESHAREDFOLDERS)) == NULL)
d0d307
+       return r;
d0d307
+
d0d307
+    next_sub = (char *) sub;
d0d307
+    while (*next_sub) {
d0d307
+        for (p = next_sub ; isspace((int) *p) || *p == SEP ; p++);
d0d307
+        for (next_sub = p ; *next_sub && *next_sub != SEP ; next_sub++);
d0d307
+        for (q = next_sub ; q > p && (isspace((int) *q) || *q == SEP || !*q) ; q--);
d0d307
+        if (!*p ) continue;
d0d307
+
d0d307
+        len = q - p + 1;
d0d307
+        /* Check for folder length */
d0d307
+        if (len  > sizeof(folder)-1)
d0d307
+                continue;
d0d307
+
d0d307
+        if (!r) {
d0d307
+                strncpy(folder, p, len);
d0d307
+                folder[len] = '\0';
d0d307
+
d0d307
+               strlcpy(name, namespace->prefix[NAMESPACE_SHARED], sizeof(name));
d0d307
+               len = strlcat(name, folder, sizeof(name));
d0d307
+
d0d307
+               r = (namespace->mboxname_tointernal) (namespace, name, userid,
d0d307
+                                                                  mailboxname);
d0d307
+       }
d0d307
+                                                                  
d0d307
+        if (!r)
d0d307
+               r = mboxlist_changesub(mailboxname, userid, auth_state, 1, 0);
d0d307
+
d0d307
+        if (!r) {
d0d307
+                syslog(LOG_NOTICE, "autosubscribe: User %s to %s succeeded", 
d0d307
+                       userid, folder);
d0d307
+        } else {
d0d307
+                syslog(LOG_WARNING, "autosubscribe: User %s to %s failed: %s", 
d0d307
+                       userid, folder, error_message(r));
d0d307
+                r = 0;
d0d307
+        }
d0d307
+    }
d0d307
+
d0d307
+    return r;
d0d307
+}
d0d307
+
d0d307
+
d0d307
+
d0d307
+int mboxlist_autocreateinbox(struct namespace *namespace,
d0d307
+                        char *userid,
d0d307
+                        struct auth_state *auth_state,
d0d307
+                        char *mailboxname, int autocreatequota) {
d0d307
+    char name [MAX_MAILBOX_NAME+1];
d0d307
+    char folder [MAX_MAILBOX_NAME+1];
d0d307
+    char *auth_userid = NULL;
d0d307
+    char *partition = NULL;
d0d307
+    const char *crt;
d0d307
+    const char *sub;
d0d307
+    char *p, *q, *next_crt, *next_sub;
d0d307
+    int len;
d0d307
+    int r = 0;
d0d307
+    int numcrt = 0;
d0d307
+    int numsub = 0;
d0d307
+#ifdef USE_SIEVE
d0d307
+    const char *source_script;
d0d307
+#endif
d0d307
+
cf58a5
+
cf58a5
+
cf58a5
+    auth_userid = auth_canonuser(auth_state);
cf58a5
+    if (auth_userid == NULL) {
cf58a5
+         /*
cf58a5
+          * Couldn't get cannon userid
cf58a5
+          */
cf58a5
+          syslog(LOG_ERR,
cf58a5
+                 "autocreateinbox: Could not get canonified userid for user %s", userid);
cf58a5
+          return IMAP_PARTITION_UNKNOWN;
cf58a5
+    }
cf58a5
+
cf58a5
+    /* Added this for debug information. */
cf58a5
+    syslog(LOG_DEBUG, "autocreateinbox: autocreate inbox for user %s was called", auth_userid);
cf58a5
+
d0d307
+   /*
d0d307
+    * While this is not needed for admins
d0d307
+    * and imap_admins accounts, it would be
d0d307
+    * better to separate *all* admins and
d0d307
+    * proxyservers from normal accounts
d0d307
+    * (accounts that have mailboxes).
d0d307
+    * UOA Specific note(1): Even if we do not
d0d307
+    * exclude these servers-classes here,
d0d307
+    * UOA specific code, will neither return
d0d307
+    * role, nor create INBOX, because none of these
d0d307
+    * administrative accounts belong to  the
d0d307
+    * mailRecipient objectclass, or have imapPartition.
d0d307
+    * UOA Specific note(2): Another good reason for doing
d0d307
+    * this, is to prevent the code, from getting into
d0d307
+    * cyrus_ldap.c because of the continues MSA logins to LMTPd.
d0d307
+    */
d0d307
+
d0d307
+   /*
d0d307
+    * admins and the coresponding imap
d0d307
+    * service, had already been excluded.
d0d307
+    */
d0d307
+
d0d307
+   /*
d0d307
+    * Do we really need group membership
d0d307
+    * for admins or service_admins?
d0d307
+    */
d0d307
+    if (global_authisa(auth_state, IMAPOPT_ADMINS)) return 0;
d0d307
+
d0d307
+   /*
d0d307
+    * Do we really need group membership
d0d307
+    * for proxyservers?
d0d307
+    */
d0d307
+    if (global_authisa(auth_state, IMAPOPT_PROXYSERVERS)) return 0;
d0d307
+
cf58a5
+    /* 
cf58a5
+     * Check if user belongs to the autocreate_users group. This option
cf58a5
+     * controls for whom the mailbox may be automatically created. Default
cf58a5
+     * value for this option is 'anyone'. So, if not declared, all mailboxes
cf58a5
+     * will be created.
cf58a5
+     */
cf58a5
+    if (!global_authisa(auth_state, IMAPOPT_AUTOCREATE_USERS)) {
cf58a5
+	    syslog(LOG_DEBUG, "autocreateinbox: User %s does not belong to the autocreate_users. No mailbox is created",
cf58a5
+			    auth_userid);
cf58a5
+	    return IMAP_MAILBOX_NONEXISTENT;
d0d307
+    }
d0d307
+
d0d307
+#if 0
d0d307
+        /*
d0d307
+         * Get Partition info or return.
d0d307
+         * (Here you should propably use
d0d307
+         * you own "get_partition(char *userid)"
d0d307
+         * function. Otherwise all new INBOXes will be
d0d307
+         * created into whatever partition has been declared
d0d307
+         * as default in your imapd.conf)
d0d307
+         */
d0d307
+
d0d307
+        partition = get_partition(userid);
d0d307
+        if (partition == NULL) {
d0d307
+            /*
d0d307
+             * Couldn't get partition info
d0d307
+             */
d0d307
+            syslog(LOG_ERR,
d0d307
+                   "Could not get imapPartition info for user %s", userid);
d0d307
+            return IMAP_PARTITION_UNKNOWN;
d0d307
+        }
d0d307
+#endif
d0d307
+
d0d307
+    r = mboxlist_createmailbox(mailboxname, MAILBOX_FORMAT_NORMAL, NULL,
d0d307
+                                      1, userid, auth_state, 0, 0, 0);
d0d307
+
d0d307
+    if (!r && autocreatequota > 0)
d0d307
+        r = mboxlist_setquota(mailboxname, autocreatequota, 0);
d0d307
+
d0d307
+    if (!r)
d0d307
+        r = mboxlist_changesub(mailboxname, userid,
d0d307
+                              auth_state, 1, 1);
d0d307
+
d0d307
+    if (!r) {
d0d307
+       syslog(LOG_NOTICE, "autocreateinbox: User %s, INBOX was successfully created in partition %s", 
d0d307
+               auth_userid, partition == NULL ? "default" : partition);
d0d307
+    } else {
d0d307
+       syslog(LOG_ERR, "autocreateinbox: User %s, INBOX failed. %s", 
d0d307
+               auth_userid, error_message(r));
d0d307
+    }
d0d307
+
d0d307
+#if 0
d0d307
+    /* Allocated from get_partition, and not needed any more */
d0d307
+    free_partition(partition);
d0d307
+#endif
d0d307
+
d0d307
+    if (r) return r;
d0d307
+
d0d307
+    /* INBOX's subfolders */
d0d307
+    if ((crt=config_getstring(IMAPOPT_AUTOCREATEINBOXFOLDERS)))
d0d307
+        sub=config_getstring(IMAPOPT_AUTOSUBSCRIBEINBOXFOLDERS);
d0d307
+
d0d307
+    /* Roll through crt */
d0d307
+    next_crt = (char *) crt;
d0d307
+    while (next_crt!=NULL && *next_crt) {
d0d307
+          for (p = next_crt ; isspace((int) *p) || *p == SEP ; p++);
d0d307
+          for (next_crt = p ; *next_crt && *next_crt != SEP ; next_crt++);
d0d307
+          for (q = next_crt ; q > p && (isspace((int) *q) || *q == SEP || !*q); q--);
d0d307
+
d0d307
+          if (!*p) continue;
d0d307
+
d0d307
+          len = q - p + 1;
d0d307
+
d0d307
+          /* First time we check for length */
d0d307
+          if (len > sizeof(folder) - 5)
d0d307
+              r = IMAP_MAILBOX_BADNAME;
d0d307
+
d0d307
+          if (!r) {
d0d307
+                  strncpy(folder, p, len);
d0d307
+                  folder[len] = '\0';
d0d307
+
d0d307
+                  strlcpy(name, namespace->prefix[NAMESPACE_INBOX], sizeof(name));
d0d307
+                  len = strlcat(name, folder, sizeof(name));
d0d307
+          }
d0d307
+
d0d307
+          if (!r)
d0d307
+            r = (namespace->mboxname_tointernal) (namespace, name, userid,
d0d307
+                                                 mailboxname);
d0d307
+          if (!r)
d0d307
+             r = mboxlist_createmailbox(mailboxname, MAILBOX_FORMAT_NORMAL, NULL,
d0d307
+                                            1, userid, auth_state, 0, 0, 0);
d0d307
+
d0d307
+          if (!r) {
d0d307
+            numcrt++;
d0d307
+            syslog(LOG_NOTICE, "autocreateinbox: User %s, subfolder %s creation succeeded.", 
d0d307
+               auth_userid, name);
d0d307
+         } else {
d0d307
+             syslog(LOG_WARNING, "autocreateinbox: User %s, subfolder %s creation failed. %s", 
d0d307
+               auth_userid, name, error_message(r));
d0d307
+             r=0;
d0d307
+             continue;
d0d307
+          }
d0d307
+
d0d307
+          /* Roll through sub */
d0d307
+          next_sub = (char *) sub;
d0d307
+          while (next_sub!=NULL && *next_sub) {
d0d307
+                for (p = next_sub ; isspace((int) *p) || *p == SEP ; p++);
d0d307
+                for (next_sub = p ; *next_sub && *next_sub != SEP ; next_sub++);
d0d307
+                for (q = next_sub ; q > p && (isspace((int) *q) || *q == SEP || !*q) ; q--);
d0d307
+                if (!*p ) continue;
d0d307
+
d0d307
+                len = q - p + 1;
d0d307
+
d0d307
+                if (len != strlen(folder) || strncmp(folder, p, len))
d0d307
+                    continue;
d0d307
+
d0d307
+                r = mboxlist_changesub(mailboxname, userid, auth_state, 1, 1);
d0d307
+
d0d307
+               if (!r) {
d0d307
+                   numsub++;
d0d307
+                   syslog(LOG_NOTICE,"autocreateinbox: User %s, subscription to %s succeeded",
d0d307
+                       auth_userid, name);
d0d307
+               } else
d0d307
+                    syslog(LOG_WARNING, "autocreateinbox: User %s, subscription to  %s failed. %s",
d0d307
+                       auth_userid, name, error_message(r));
d0d307
+
d0d307
+                break;
d0d307
+         }
d0d307
+    }
d0d307
+
d0d307
+    if (crt!=NULL && *crt)
d0d307
+       syslog(LOG_INFO, "User %s, Inbox subfolders, created %d, subscribed %d", 
d0d307
+               auth_userid, numcrt, numsub);
d0d307
+
d0d307
+    /*
d0d307
+     * Check if shared folders are available for subscription.
d0d307
+     */
d0d307
+    mboxlist_autosubscribe_sharedfolders(namespace, userid, auth_userid, auth_state);
d0d307
+
d0d307
+#ifdef USE_SIEVE
d0d307
+    /*
d0d307
+     * Here the autocreate sieve script feature is iniated from.
d0d307
+     */
d0d307
+    source_script = config_getstring(IMAPOPT_AUTOCREATE_SIEVE_SCRIPT);
d0d307
+ 
d0d307
+    if (source_script) {
d0d307
+        if (!autoadd_sieve(userid, source_script))
d0d307
+            syslog(LOG_NOTICE, "autocreate_sieve: User %s, default sieve script creation succeeded", auth_userid);
d0d307
+        else
d0d307
+            syslog(LOG_WARNING, "autocreate_sieve: User %s, default sieve script creation failed", auth_userid);
d0d307
+    }
d0d307
+#endif
d0d307
+
d0d307
+    return r;
d0d307
+}
d0d307
+
cf58a5
diff -Naur cyrus-imapd-2.3.1/imap/mboxlist.h cyrus-imapd-2.3.1-autocreate/imap/mboxlist.h
cf58a5
--- cyrus-imapd-2.3.1/imap/mboxlist.h	2005-02-21 21:25:40 +0200
cf58a5
+++ cyrus-imapd-2.3.1-autocreate/imap/mboxlist.h	2006-01-19 03:53:41 +0200
cf58a5
@@ -203,4 +203,10 @@
d0d307
 int mboxlist_commit(struct txn *tid);
d0d307
 int mboxlist_abort(struct txn *tid);
d0d307
 
d0d307
+int mboxlist_autocreateinbox(struct namespace *namespace,
d0d307
+                        char *userid,
d0d307
+                        struct auth_state *auth_state,
d0d307
+                        char *mailboxname, int autocreatequota);
d0d307
+
d0d307
+
d0d307
 #endif
cf58a5
diff -Naur cyrus-imapd-2.3.1/imap/pop3d.c cyrus-imapd-2.3.1-autocreate/imap/pop3d.c
cf58a5
--- cyrus-imapd-2.3.1/imap/pop3d.c	2005-12-13 21:36:08 +0200
cf58a5
+++ cyrus-imapd-2.3.1-autocreate/imap/pop3d.c	2006-01-19 03:53:41 +0200
cf58a5
@@ -155,6 +155,8 @@
d0d307
 static char popd_apop_chal[45 + MAXHOSTNAMELEN + 1]; /* <rand.time@hostname> */
d0d307
 static void cmd_apop(char *response);
d0d307
 
d0d307
+static int autocreate_inbox(char *inboxname, char *userid);
d0d307
+
d0d307
 static void cmd_auth(char *arg);
d0d307
 static void cmd_capa(void);
d0d307
 static void cmd_pass(char *pass);
cf58a5
@@ -1206,6 +1208,7 @@
cf58a5
 	popd_userid = xstrdup(userbuf);
d0d307
 	prot_printf(popd_out, "+OK Name is a valid mailbox\r\n");
d0d307
     }
d0d307
+
d0d307
 }
d0d307
 
d0d307
 void cmd_pass(char *pass)
cf58a5
@@ -1477,6 +1480,43 @@
d0d307
 }
d0d307
 
d0d307
 /*
d0d307
+ * Autocreate Inbox and subfolders upon login
d0d307
+ */
d0d307
+int autocreate_inbox(char *inboxname, char *auth_userid)
d0d307
+{
cf58a5
+    struct auth_state *auth_state;
d0d307
+    int autocreatequota;
d0d307
+    int r;
d0d307
+
cf58a5
+    if (inboxname == NULL || auth_userid == NULL)
d0d307
+	    return IMAP_MAILBOX_NONEXISTENT;
d0d307
+    
d0d307
+    /*
d0d307
+     * Exclude anonymous
d0d307
+     */
d0d307
+    if (!strcmp(popd_userid, "anonymous"))
d0d307
+        return IMAP_MAILBOX_NONEXISTENT;
d0d307
+