Remove Default Mail

From brokenpoet.org wiki

Make sure to edit if you do not want this to remove all mail.



wget http://scripts.brokenpoet.org/removedefaultmail



Contents

Final Version (2008.09.23)

  • Added option flags for all, users, exceptions, verbose, setting to fail and reporting
  • Wrote mail removal into two functions for verbose and not verbose
  • Added reporting Fu
  • Rewrote 'set to fail' so that it can work for limited users from domainusrs variable
  • Other random tweaks
#!/bin/bash
# created by bcathey - date unknown

# Mod Date, 2008.09.15
# added opts, progress, etc

# Script can do the following

# * ( -r ) run a report on disk usage
# * ( -a ) remove all cPanel user's Catch-All Mail
# * ( -v ) do so with verbose output
# * ( -e ) make exceptions for specific users listed with spaces
#          ie: joe bob tim
# * ( -u ) run for specific users listed with spaces
#          ie: joe bob tim
# * ( -f ) set all users (or listed users) Catch-All accounts to :fail: 
#          while removing the mail

# help
# usage help_func
help_func () {
 echo
 echo "Usage: removedefaultmail [-afrvh] [-u users] or [-e users]"
 echo
 echo "      -a     remove default mail for all cPanel users"
 echo 
 echo "      -f     set the 'Catch-All' of all users (or selected users) to :fail:"
 echo
 echo "      -r     generate a user disk usage report"
 echo
 echo "      -v     verbose output while removing mail"
 echo
 echo "      -h     display this help output"
 echo 
 echo "      -u     remove default mail only for specified users"
 echo 
 echo "      -e     remove default mail for all users except those noted"
 echo
 exit 0
}

# chk_user
# usage chk_user [ exp or users ]
chk_user () {
 chk=$(cat /etc/domainusers | cut -d':' -f1)
 for user in $1
 do 
   if [ -n "`echo $chk | grep -w $user`" ]
     then
       :
     else
       echo
       echo "ERROR: user $user is not a valid cPanel user"
       echo
       exit 1
   fi
 done
} 

# remove_mail_verbose
# usage remove_mail_verbose [ domainusrs ]
remove_mail_verbose () {
  filetotal=
  echo
  echo "/-----------------------------------------\\"
  echo "| Removing mail from 'Catch-All' accounts |"
  echo "\-----------------------------------------/"
  for user in $1
  do
    if [ -a /home/$user/mail/cur ]
    then
      usertotal=$(find /home/$user/mail/cur -type f | wc -l)
    fi
    if [ -a /home/$user/mail/new ]
    then
      usertotal=$(( $usertotal + $(find /home/$user/mail/new -type f | wc -l)))
    fi
    printf "%2s%-12s%21s%6s\n" "" $user $usertotal "files"
    filetotal=$(( $filetotal + $usertotal ))

    if [ -a /home/$user/mail/cur ]
    then
      rm -rf /home/$user/mail/cur 2>/dev/null 
    fi
    sudo -u $user mkdir -m 700 /home/$user/mail/cur 2>/dev/null

    if [ -a /home/$user/mail/new ]
    then
      rm -rf /home/$user/mail/new 2>/dev/null
    fi	
    sudo -u $user mkdir -m 700 /home/$user/mail/new 2>/dev/null
  done
  echo "-------------------------------------------"
  printf "%2s%-12s%21s%6s\n" "" "total" $filetotal "files"
}


# remove_mail
# usage remove_mail [ domainusrs ]
remove_mail () {
  echo -n "Removing mail from all 'Catch-All' accounts "
  for user in $1
  do
    if [ -a /home/$user/mail/cur ]
    then
      rm -rf /home/$user/mail/cur 2>/dev/null &
      while ps |grep $! &>/dev/null
      do
        echo -n "."
        sleep 3
      done
    fi
    sudo -u $user mkdir -m 700 /home/$user/mail/cur 2>/dev/null

    if [ -a /home/$user/mail/new ]
    then
      rm -rf /home/$user/mail/new 2>/dev/null &
      while ps |grep $! &>/dev/null
      do
        echo -n "."
	sleep 3
      done
    fi  
    sudo -u $user mkdir -m 700 /home/$user/mail/new 2>/dev/null
  done
  echo
  echo "Done"
  echo
}



# set flag vars to empty
all= exp= fail= report= users= verbose= domainusrs=

# process options
while getopts :ae:fru:vh opt
do
  case $opt in
    a)  all=true
        ;;
    f)  fail=true
        ;;
    r)  report=true
        ;;
    v)  verbose=true
        ;;
    u)  users=$OPTARG
        ;; 
    e)  exp=$OPTARG
        ;;
    h)  help_func
        ;;
    *)  echo "$0: invalid option -$OPTARG" >&2
        echo
        echo
        echo "Usage: removedefaultmail [-afrvh] [-u users] or [-e users]"
        echo
        echo "      -a     remove default mail for all cPanel users"
        echo 
        echo "      -f     set the 'Catch-All' of all users (or selected users) to :fail:"
        echo
        echo "      -r     generate a user disk usage report"
        echo
        echo "      -v     verbose output while removing mail"
        echo 
        echo "      -h     display this help output"
        echo 
        echo "      -u     remove default mail only for specified users"
        echo 
        echo "      -e     remove default mail for all users except those noted"
        echo
        exit 1
        ;;
  esac
done	


# Remove options and leave arguments

shift $((OPTIND -1))

# Get the rest of the list
if [ "$exp" ]
then
  exp="$exp $@"
  chk_user "$exp"
fi

if [ "$users" ]
then
  users="$users $@"
  chk_user "$users"
fi

# check to make sure they are only using all, users, or exceptions
# and not a combition

if [ "$all" -a "$exp" -o "$all" -a "$users" -o "$users" -a "$exp" ]
then
  echo
  echo "$0: Invalid Options"
  echo "Must use only ONE of the following -a -e -u"
  echo
  exit 1
fi



# assign $domainusrs based on flags
if [ "$all" = "true" ]
then
  domainusrs=$(cat /etc/domainusers | cut -d':' -f1 | sort)
elif [ "$users" ]
then
  domainusrs=$(echo -n $users)
elif [ "$exp" ]
then
  exp=$(echo -n $exp | sed 's/\ /|/g')
  domainusrs=$(cat /etc/domainusers | cut -d':' -f1 | sort | awk '!/'$exp'/')
fi

# check verbose or all and process

if [ "$domainusrs" ]
then
  if [ "$fail" ]
  then
    echo
    echo "Setting users 'Catch-All' to :fail:"
    echo
    for user in $domainusrs
    do
      for file in $(find /etc/valiases -user $user)
      do
        sed 's/\*\:.*/\*\:\ \:fail\:\ No\ Such\ User\ Here/g' $file > $file.tmp
        cat $file.tmp > $file
        rm -f $file.tmp
      done
    done
  echo "Done"
  echo
  fi

  if [ -n "$verbose" ]
  then
    remove_mail_verbose "$domainusrs"
    exit 0
  else
    remove_mail "$domainusrs"
    exit 0
  fi
fi

if [ -n "$report" ] 
then
  printf "User          Count     Size | From Catch-All :   new :   cur\n"
  echo "-------------------------------------------------------------"
  tot_count=0
  tot_mcount=0
  for user in $(cat /etc/domainusers |cut -d':' -f1|sort)
  do 
    count=$(find /home/$user -type f|wc -l)
    size=$(du -hs /home/$user|awk '{print $1}')
    if [ -a /home/$user/mail/new ]
    then 
      cmcount=$(find /home/$user/mail/cur -type f|wc -l)
      nmcount=$(find /home/$user/mail/new -type f|wc -l)
      mcount=$(($cmcount + $nmcount))
      nsize=$(du -hs /home/$user/mail/new|awk '{print $1}')
      csize=$(du -hs /home/$user/mail/cur|awk '{print $1}')
    else 
      cmcount=""
      nmcount=""
      mcount=""
      nsize=""
      csize=""
    fi
    tot_count=$(($count+$tot_count))
    tot_mcount=$(($mcount+$tot_mcount))
    printf "%-9s%10s%9s | %14s : %5s : %5s\n" $user $count $size $mcount $nsize $csize
  done
  echo "-------------------------------------------------------------"
  printf "%19s%26s\n" $tot_count $tot_mcount
  exit 0
fi

# if you are here something went wrong or no options were passed

help_func

exit 0


New Version (2008.09.13)

  • Added checking for existence of folders first
  • Changed the file removal to simply remove and recreate the directories cur and new
  • Added a progress indicator
  • Setup error passing so all errors go to /dev/null
#!/bin/bash
# created by bcathey - date unknown

# Finally put this into a script rather than a one liner
# I had to write a variation of this to remove all mail except #for certain users.
# Simply put in the usernames where it says user#
# The "|" means OR - you can exclude more users by simply adding another |username to the section awk '!/user1|user2/'
# ie: awk '!/user1|user2|user3|user4/' 

echo
echo -n "Removing mail from 'Catch-All' accounts "

for i in $(cat /etc/domainusers | cut -d':' -f1 | sort); do 

# Comment out the previous line and uncomment the following to exclude users
#for i in $(cat /etc/domainusers | cut -d':' -f1 | sort | awk '!/user1|user2/' ); do 

  if [ -a /home/$i/mail/cur ]
  then
    rm -rf /home/$i/mail/cur 2>/dev/null &
    while ps |grep $! &>/dev/null; do
      echo -n "."
      sleep 3
    done
  fi
  sudo -u $i mkdir -m 700 /home/$i/mail/cur 2>/dev/null

  if [ -a /home/$i/mail/new ]
  then
    rm -rf /home/$i/mail/new 2>/dev/null &
    while ps |grep $! &>/dev/null; do
      echo -n "."
      sleep 3
    done
  fi
  sudo -u $i mkdir -m 700 /home/$i/mail/new 2>/dev/null
done

echo
echo "Done"


Old Version

#!/bin/bash
# created by bcathey - date unknown

# Finally put this into a script rather than a one liner
# I had to write a variation of this to remove all mail except for certain users.
# Simply put in the usernames where it says user#
# The "|" means OR - you can exclude more users by simply adding another |username to the section awk '!/user1|user2/'
# ie: awk '!/user1|user2|user3|user4/' 

for i in $(cat /etc/domainusers |cut -d':' -f1); do 

# Comment out the previos line and uncomment the following to exclude users
# for i in $(cat /etc/domainusers |cut -d':' -f1|awk '!/user1|user2/' ); do 

    echo "Removing CUR mail for $i"
    find /home/$i/mail/cur/ -type f|xargs rm -rf
    echo "Removing NEW mail for $i"
    find /home/$i/mail/new/ -type f|xargs rm -rf
done


Notes / To Do

  • add usage info
  • add command line options
    • -a | --all : for all
    • -u | --users : only for specific users
    • -e | --exceptions : not these users (implies all)
    • -f | --fail : set users to :fail:
    • -v | --verbose
    • --report | gives report of disk usage
Personal tools