2 . /usr/lib/alternc/functions.sh
4 #You can call this script either without arguments, inwich case each maildir quotas will be recalculated
5 #or you can call it with a directory reffering to a maildir to just sync one mailbox
8 echo "FIXME: some help"
12 # FIXME: storing THAT amount of data in MAILDIRS (on BIG install like Lautre.net) may crash the shell?
14 # Generate the $maildirs list based on the arguments
15 while getopts "am:d:c:" optname
20 maildirs=$(mysql_query "select userdb_home from dovecot_view order by 1")
24 if [[ "$OPTARG" =~ ^[^\@]*@[^\@]*$ ]] ; then
25 if [[ "$(mysql_query "select userdb_home from dovecot_view where user = '$OPTARG'")" ]]; then
26 maildirs=$(mysql_query "select userdb_home from dovecot_view where user = '$OPTARG' order by 1")
28 echo "Bad mail provided"
32 echo "Bad mail provided"
39 # Check if domain is well-formed
40 if [[ ! "$OPTARG" =~ ^[a-z\-]+(\.[a-z\-]+)+$ ]] ; then
41 echo "Bad domain provided"
45 # Attemp to get from database.
46 if [[ ! "$(mysql_query "select domaine from domaines where domaine = '$OPTARG'")" ]]; then
48 echo "Bad domain provided"
52 maildirs=$(mysql_query "select userdb_home from dovecot_view where user like '%@$OPTARG' order by 1")
56 if [[ "$OPTARG" =~ ^[a-z]*$ ]] ; then
57 if [[ "$(mysql_query "select domaine from domaines where domaine = '$1'")" ]]; then
58 maildirs=$(mysql_query "select userdb_home from dovecot_view where userdb_uid = $OPTARG order by 1")
60 echo "Bad account provided"
64 echo "Bad account provided"
69 echo "Unknown option $OPTARG - stop processing"
74 echo "No argument value for option $OPTARG - stop processing"
80 echo "Unknown error while processing options"
87 # Now we have $maildirs, we can work on it
89 # FIXME add check if maildir is empty
91 #Then we loop through every maildir to get the maildir size
92 for i in $maildirs ; do
95 echo "The maildir $i does not exists. It's quota won't be resync"
99 # We grep only mails, not the others files
100 mails=`find $i -type f | egrep "(^$i)*[0-9]+\.M"`
102 # This part count the total mailbox size (mails + sieve scripts + ...)
103 size=`du -b -s $i|awk '{print $1}'`
105 mail_count=`echo $mails|wc -w`
107 echo "mail count : "$mail_count
108 echo "dir size : "$size
110 #update the mailbox table accordingly
111 MAILADD=`basename $i`
112 MAILADD=${MAILADD/_/@}
113 mysql_query "REPLACE INTO dovecot_quota VALUES('$MAILADD', $size, $mail_count);"
116 # may cause a problem, let's fix this here :)
117 mysql_query "UPDATE mailbox SET quota=0 WHERE quota IS NULL;"