[Dovecot] auto-detection of user mailbox type - followup

John Harper harper at utsc.utoronto.ca
Fri Feb 9 17:57:24 UTC 2007

In http://www.dovecot.org/list/dovecot/2006-December/018253.html

Timo Sirainen wrote
> You can still change NAMESPACE_* environments. I don't remember how  
> exactly now, but you can do eg. "set > /tmp/sets" in the script, see  
> what it has set and modify them in the script.

in response to my query regarding setting the mail_location in
namespaces from a mail_executable script. The purpose of this is to be
able to select on a per-user basis whether they're using Maildir or
mbox format (I don't want to convert 12000 users all at once, and some
users have no need or desire to change formats - they still use
mail/elm etc) and also have the UW-IMAP compatibility namespaces to
take care of all the clients currently configured with imap server
directory = mail/.

This all works, with one small bug left. For reference, here's the gist:

In the dovecot.conf file place the usual namespace directives
following the Namespaces wiki:

namespace private {
            separator = /
            prefix = 
            inbox = yes
#for UW backwards compatibility:
namespace private {
            separator = /
            prefix = mail/
            hidden = yes        
namespace private {
             separator = /
             prefix = ~/mail/
             hidden = yes       
namespace private {
             separator = /
             prefix = ~%u/mail/
             hidden = yes       

define the mail_executable under protocol imap:

  mail_executable = /usr/local/dovecot/lbin/imap.sh

and write the imap.sh script:

------start imap.sh---------------------------------------------------

# determine if user is in mbox or Maildir mode
# Maildir takes precedence and is identified by the existence of 
# both a .procmailrc recipe and the Maildir directory.
# Without a .procmailrc file, there's no recipe to force delivery in
# Maildir format, so any existing Maildir is spurious
# The .procmailrc should contain  DEFAULT=Maildir/
# Set the absolute path to this script as the mail_executable for
# the imap protocol in dovecot.conf. 

# It appears that this script is run as the user in their home dir

if [ -f .procmailrc -a -d Maildir ]; then
    MAIL='maildir:~/Maildir'; export MAIL
    MAIL='mbox:~/mail:INBOX=~/.mail'; export MAIL

# each namespace defined in the conf file needs to have the mail
# location defined which is set in environment vars


#debugging to see what else we can play with
#set >> /tmp/d

exec /usr/local/dovecot/libexec/dovecot/imap

------end imap.sh-----------------------------------------------------

However, it appears that the root dir '~/mail' string is used at some
point before the tilde-expansion takes place. I see a literal ~
directory in the users home, with an empty mail directory
inside. After that it picks up the proper location and works
correctly. I tried a %h and got a literal %h directory. So for now I
made a simple workaround by using the absolute path to the home

# use home variable (depends on this script running in users home)
# During startup it seems like ~/mail is interpreted as a string and
# a spurious ~ folder is created, before the ~ is expanded
if [ -f .procmailrc -a -d Maildir ]; then
    MAIL="maildir:$home/Maildir"; export MAIL
    MAIL="mbox:$home/mail:INBOX=$home/.mail"; export MAIL


John Harper
Senior Systems Administrator
Information and Instructional Technology Services
University of Toronto Scarborough
harper at utsc.utoronto.ca

More information about the dovecot mailing list