#!/bin/sh # # handle-auto # John Simpson # Original: 2009-09-07 # This version: 2009-12-18 # # allows "auto-_____@domain.xyz" automatic mailboxes. any messages sent to # an automatic address will be stored in a child folder within that folder. # if the child folder doesn't exist, it will be created automatically. # # this script should be called from ".qmail-auto-default" with a line like: # # |/var/qmail/bin/handle-auto PARENT # # where PARENT is the full path to the parent of where the automatic folders # will be created. if this location is the root of a Maildir (i.e. an INBOX) # then this value should end with "/". otherwise (i.e. if the automatic # folders will be created within another folder) then this value should NOT # end with a "/". # # documentation: http://qmail.jms1.net/scripts/handle-auto.shtml PATH=/usr/bin:/bin:/usr/local/bin:/var/qmail/bin ############################################################################### # # if "hostname -s" doesn't return a valid name on your system, or if you # don't want to waste the extra CPU cycles to look up the name every time, # you should set a static hostname value here. this value is used to build # the filename of the message in the final Maildir. HOSTNAME=`hostname -s` ############################################################################### # # if you want the automatic folder names to be uppercase or lowercase, # uncomment one of these lines. otherwise, folder names will be created in # whatever case they arrive in- which means "abc", "abC", "aBc", "aBC", "Abc", # "AbC", "ABc", and "ABC" will all be created as separate folders. #CASE=UPPER #CASE=LOWER ############################################################################### ############################################################################### ############################################################################### # # build the name of target maildir # # note: if you use some other IMAP server and it creates the folder names # differently, you will need to adjust this code, and probably change the # format of the PARENT value you pass in from the command line. if you end up # doing this, please let me know about it, so i can update the script with # notes for users of other IMAP servers. PARENT="${1:?$0: ERROR: No PARENT location specified on command line}" if [ "${CASE:-}" = "LOWER" ] then ENAME=`echo "${EXT2:-}" | tr 'A-Z' 'a-z'` elif [ "${CASE:-}" = "UPPER" ] then ENAME=`echo "${EXT2:-}" | tr 'a-z' 'A-Z'` else ENAME=${EXT2:-} fi TARGET="$PARENT${ENAME:+.$ENAME}" ######################################## # make sure the Maildir exists # but not if it's the root of an INBOX if [ ! -d "$TARGET/new" ] then if [ "${TARGET: -1}" = "/" ] then echo "$0: ERROR: cannot create non-existent mailbox" exit 1 else maildirmake $TARGET fi fi if [ ! -d "$TARGET/new" ] then echo "$0: ERROR: Maildir \"$TARGET\" cannot be created" exit 1 fi ######################################## # safely add the message to that maildir NOW=`date +%s` FILENAME=$NOW.P$$.${HOSTNAME:?$0: ERROR: missing HOSTNAME value} cat > $TARGET/tmp/$FILENAME chmod 600 $TARGET/tmp/$FILENAME mv $TARGET/tmp/$FILENAME $TARGET/new/$FILENAME ######################################## # outta here exit 0