2013-01-23 19:26:42 +00:00
|
|
|
|
#!/bin/bash
|
2013-01-23 13:47:52 +00:00
|
|
|
|
. /usr/lib/alternc/functions.sh
|
|
|
|
|
|
2013-01-23 19:26:42 +00:00
|
|
|
|
#You can call this script either without arguments, inwich case each maildir quotas will be recalculated
|
|
|
|
|
#or you can call it with a directory reffering to a maildir to just sync one mailbox
|
|
|
|
|
|
2014-05-23 15:59:40 +00:00
|
|
|
|
function showhelp() {
|
|
|
|
|
echo "FIXME: some help"
|
|
|
|
|
exit
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Generate the $maildirs list based on the arguments
|
2014-05-23 15:21:26 +00:00
|
|
|
|
while getopts "a:m:d:c:" optname
|
|
|
|
|
do
|
|
|
|
|
case "$optname" in
|
|
|
|
|
"a")
|
2014-05-23 15:59:40 +00:00
|
|
|
|
# All mails
|
|
|
|
|
# FIXME replace it by a select in da DB
|
2014-05-23 15:21:26 +00:00
|
|
|
|
maildirs=`find "$ALTERNC_MAIL/" -maxdepth 2 -mindepth 2 -type d`
|
|
|
|
|
;;
|
|
|
|
|
"m")
|
2014-05-23 15:59:40 +00:00
|
|
|
|
# An email
|
2014-05-23 15:21:26 +00:00
|
|
|
|
if [[ "$OPTARG" =~ ^[^\@]*@[^\@]*$ ]] ; then
|
|
|
|
|
if [[ "$(mysql_query "select userdb_home from dovecot_view where user = '$OPTARG'")" ]]; then
|
|
|
|
|
maildirs=$(mysql_query "select userdb_home from dovecot_view where user = '$OPTARG'")
|
|
|
|
|
else
|
|
|
|
|
echo "Bad mail provided"
|
2014-05-23 15:59:40 +00:00
|
|
|
|
showhelp
|
2014-05-23 15:21:26 +00:00
|
|
|
|
fi
|
|
|
|
|
else
|
|
|
|
|
echo "Bad mail provided"
|
2014-05-23 15:59:40 +00:00
|
|
|
|
showhelp
|
2014-05-23 15:21:26 +00:00
|
|
|
|
fi
|
|
|
|
|
;;
|
|
|
|
|
"d")
|
2014-05-23 15:59:40 +00:00
|
|
|
|
# Expecting a domain
|
|
|
|
|
|
|
|
|
|
# Check if domain is well-formed
|
|
|
|
|
if [[ ! "$OPTARG" =~ ^[a-z\-]+(\.[a-z\-]+)+$ ]] ; then
|
|
|
|
|
echo "Bad domain provided"
|
|
|
|
|
showhelp
|
2014-05-23 15:21:26 +00:00
|
|
|
|
fi
|
2014-05-23 15:59:40 +00:00
|
|
|
|
|
|
|
|
|
# Attemp to get from database.
|
|
|
|
|
if [[ ! "$(mysql_query "select domaine from domaines where domaine = '$OPTARG'")" ]]; then
|
|
|
|
|
# Seem to be empty
|
|
|
|
|
echo "Bad domain provided"
|
|
|
|
|
showhelp
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
maildirs=$(mysql_query "select userdb_home from dovecot_view where user like '%@$OPTARG'")
|
2014-05-23 15:21:26 +00:00
|
|
|
|
;;
|
|
|
|
|
"c")
|
2014-05-23 15:59:40 +00:00
|
|
|
|
# An account
|
2014-05-23 15:21:26 +00:00
|
|
|
|
if [[ "$OPTARG" =~ ^[a-z]*$ ]] ; then
|
|
|
|
|
if [[ "$(mysql_query "select domaine from domaines where domaine = '$1'")" ]]; then
|
|
|
|
|
maildirs=$(mysql_query "select userdb_home from dovecot_view where userdb_uid = $OPTARG")
|
|
|
|
|
else
|
|
|
|
|
echo "Bad account provided"
|
2014-05-23 15:59:40 +00:00
|
|
|
|
showhelp
|
2014-05-23 15:21:26 +00:00
|
|
|
|
fi
|
|
|
|
|
else
|
|
|
|
|
echo "Bad account provided"
|
2014-05-23 15:59:40 +00:00
|
|
|
|
showhelp
|
2014-05-23 15:21:26 +00:00
|
|
|
|
fi
|
|
|
|
|
;;
|
|
|
|
|
"?")
|
|
|
|
|
echo "Unknown option $OPTARG - stop processing"
|
2014-05-23 15:59:40 +00:00
|
|
|
|
showhelp
|
2013-01-23 19:26:42 +00:00
|
|
|
|
exit
|
2014-05-23 15:21:26 +00:00
|
|
|
|
;;
|
|
|
|
|
":")
|
|
|
|
|
echo "No argument value for option $OPTARG - stop processing"
|
2014-05-23 15:59:40 +00:00
|
|
|
|
showhelp
|
2014-05-23 15:21:26 +00:00
|
|
|
|
exit
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
# Should not occur
|
|
|
|
|
echo "Unknown error while processing options"
|
2014-05-23 15:59:40 +00:00
|
|
|
|
showhelp
|
2014-05-23 15:21:26 +00:00
|
|
|
|
exit
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
|
2014-05-23 15:59:40 +00:00
|
|
|
|
# Now we have $maildirs, we can work on it
|
2013-01-23 19:26:42 +00:00
|
|
|
|
|
2014-05-23 15:59:40 +00:00
|
|
|
|
# FIXME add check if maildir is empty
|
2013-01-23 13:47:52 +00:00
|
|
|
|
|
|
|
|
|
#Then we loop through every maildir to get the maildir size
|
2014-05-23 15:21:26 +00:00
|
|
|
|
for i in $maildirs ; do
|
2013-01-23 19:26:42 +00:00
|
|
|
|
|
2014-05-23 15:59:40 +00:00
|
|
|
|
if [ ! -d "$i" ];then
|
|
|
|
|
echo "The maildir $i does not exists. It's quota won't be resync"
|
|
|
|
|
continue
|
|
|
|
|
fi
|
2013-01-24 10:27:42 +00:00
|
|
|
|
|
2014-05-23 15:59:40 +00:00
|
|
|
|
# We grep only mails, not the others files
|
|
|
|
|
mails=`find $i -type f | egrep "(^$i)*[0-9]+\.M"`
|
2013-01-24 10:27:42 +00:00
|
|
|
|
|
2014-05-23 15:59:40 +00:00
|
|
|
|
# This part count the total mailbox size (mails + sieve scripts + ...)
|
|
|
|
|
size=`du -b -s $i|awk '{print $1}'`
|
2013-01-24 10:27:42 +00:00
|
|
|
|
|
2014-05-23 15:59:40 +00:00
|
|
|
|
mail_count=`echo $mails|wc -w`
|
|
|
|
|
echo "folder : "$i
|
|
|
|
|
echo "mail count : "$mail_count
|
|
|
|
|
echo "dir size : "$size
|
|
|
|
|
echo ""
|
|
|
|
|
#update the mailbox table accordingly
|
|
|
|
|
mysql_query "UPDATE mailbox SET bytes=$size WHERE path='$i' ; "
|
|
|
|
|
mysql_query "UPDATE mailbox SET messages=$mail_count WHERE path='$i' ; "
|
2013-01-23 13:47:52 +00:00
|
|
|
|
done
|
|
|
|
|
|