2012-08-25 10:35:23 +00:00
#!/bin/bash
# This script look in the database wich mail should be DELETEd
# Source some configuration file
for CONFIG_FILE in \
/etc/alternc/local.sh \
/usr/lib/alternc/functions.sh
do
if [ ! -r " $CONFIG_FILE " ] ; then
echo " Can't access $CONFIG_FILE . "
exit 1
fi
. " $CONFIG_FILE "
done
2013-09-12 09:28:31 +00:00
stop_if_jobs_locked
2012-08-26 14:36:06 +00:00
LOCK_FILE = "/var/run/alternc/update_mails"
2012-08-25 10:35:23 +00:00
2013-02-18 13:54:00 +00:00
# ALTERNC_MAIL is from local.sh
2012-08-25 10:35:23 +00:00
2012-08-25 10:41:25 +00:00
# Somes check before start operations
if [ ` id -u` -ne 0 ] ; then
log_error "must be launched as root"
elif [ -f " $LOCK_FILE " ] ; then
process = $( ps f -p ` cat " $LOCK_FILE " | tail -1` | tail -1| awk '{print $NF;}' )
if [ " $( basename $process ) " = " $( basename " $0 " ) " ] ; then
log_error " last cron unfinished or stale lock file ( $LOCK_FILE ). "
else
rm " $LOCK_FILE "
fi
fi
2012-08-25 13:24:11 +00:00
# If there is ionice, add it to the command line
ionice = ""
ionice > /dev/null && ionice = "ionice -c 3 "
2012-08-25 10:41:25 +00:00
# We lock the application
echo $$ > " $LOCK_FILE "
2012-08-25 10:35:23 +00:00
# List the local addresses to DELETE
# Foreach => Mark for deleting and start deleting the files
# If process is interrupted, the row isn't deleted. We have to force it by reseting mail_action to 'DELETE'
2013-02-08 13:48:57 +00:00
mysql_query "SELECT id, address_id, quote(replace(path,'!','\\!')) FROM mailbox WHERE mail_action='DELETE';" | while read id address_id path ; do
2012-08-25 10:35:23 +00:00
mysql_query " UPDATE mailbox set mail_action='DELETING' WHERE id= $id ; "
2013-02-08 13:48:57 +00:00
/usr/lib/alternc/mail_dodelete.php " $address_id "
2012-08-25 10:35:23 +00:00
# Check there is no instruction of changing directory, and check the first part of the string
2013-02-18 13:54:00 +00:00
if [ [ " $path " = ~ '../' || " $path " = ~ '/..' || ! " ' $ALTERNC_MAIL ' " = = " ${ path : 0 : $(( ${# ALTERNC_MAIL } + 1 )) } ' " ] ] ; then
2012-10-08 13:07:24 +00:00
# The path will be empty for mailman addresses
if [ [ " $path " != "''" ] ] ; then
echo "Error : this directory will not be deleted, pattern incorrect"
continue
fi
2012-08-25 10:35:23 +00:00
fi
2012-08-25 13:24:11 +00:00
# If no dir, DELETE
# If dir and rm ok, DELETE
# Other case, do nothing
2013-02-08 13:48:57 +00:00
if [ -d " ${ path // \' / } " ] ; then
2013-02-08 13:59:42 +00:00
$ionice rm -rf " ${ path // \' / } " && mysql_query " DELETE FROM mailbox WHERE id= $id AND mail_action='DELETING'; "
2013-09-25 09:50:53 +00:00
# Do the rm again in case of newly added file during delete. Should not be usefull
2013-02-08 13:59:42 +00:00
test -d " ${ path // \' / } " && $ionice rm -rf " ${ path // \' / } "
2012-08-25 13:24:11 +00:00
else
2012-10-15 09:24:31 +00:00
mysql_query " DELETE FROM mailbox WHERE id= $id AND mail_action='DELETING'; "
2012-08-25 13:24:11 +00:00
fi
2012-08-25 10:35:23 +00:00
done
# List the adresses to DELETE
# Delete if only if there isn't any mailbox refering to it
2014-03-17 15:44:47 +00:00
mysql_query "delete a FROM address a LEFT JOIN mailbox m ON a.id = m.address_id WHERE m.id IS NULL AND (a.mail_action = 'DELETE' OR a.mail_action = 'DELETING');"
2012-08-25 10:35:23 +00:00
2012-08-25 10:41:25 +00:00
# Delete the lock
rm -f " $LOCK_FILE "