mmv

mailserver bootstrap script
git clone git://git.yotsev.xyz/mmv.git
Log | Files | Refs | README | LICENSE

mdeluser (1207B)


      1 #!/bin/sh
      2 
      3 if [ $# != 1 ]; then
      4     echo "Error: Invalid number of arguments"
      5     echo "Usage: mdeluser username@domain.tld"
      6     return 1
      7 fi
      8 
      9 user="$(echo $1 | cut -d '@' -f 1)"
     10 domain="$(echo $1 | cut -d '@' -f 2)"
     11 
     12 if [ -z "$(grep "$domain" /etc/mail/domains 2> /dev/null)" ]; then
     13     echo "Error: Invalid domain"
     14     echo "Info: It should be one of the following:"
     15     cat /etc/mail/domains
     16     return 1
     17 fi
     18 
     19 if [ -z "$(grep "^$user@$domain" /etc/mail/credentials 2> /dev/null)" ]; then
     20     echo "Error: User doesn't exist in /etc/mail/credentials"
     21     return 1
     22 fi
     23 
     24 if [ -z "$(grep "^$user@$domain" /etc/mail/virtuals 2> /dev/null)" ]; then
     25     echo "Error: User doesn't exist in /etc/mail/virtuals"
     26     return 1
     27 fi
     28 
     29 echo -n "Delete user \"$user\"? (y/n): " &&
     30 read responce &&
     31 [ $responce = "y" ] &&
     32 sed "/^$user@$domain/d" /etc/mail/credentials > temp-cred-file &&
     33 mv temp-cred-file /etc/mail/credentials &&
     34 sed "/^$user@$domain/d" /etc/mail/virtuals > temp-virt-file &&
     35 mv temp-virt-file /etc/mail/virtuals
     36 
     37 echo -n "Delete all mail belonging to user \"$user\"? (y/n): " &&
     38 read responce &&
     39 [ $responce = "y" ] &&
     40 rm -rf /var/vmail/$domain/$user ||
     41 return 0
     42 
     43 rcctl restart smtpd
     44 rcctl restart dovecot