Alternc  latest
Alternc logiel libre pour l'hébergement
update_mails.sh
Go to the documentation of this file.
1 #!/bin/bash
2 # This script look in the database wich mail should be DELETEd
3 
4 # Source some configuration file
5 for CONFIG_FILE in \
6  /etc/alternc/local.sh \
7  /usr/lib/alternc/functions.sh
8  do
9  if [ ! -r "$CONFIG_FILE" ]; then
10  echo "Can't access $CONFIG_FILE."
11  exit 1
12  fi
13  . "$CONFIG_FILE"
14 done
15 
16 stop_if_jobs_locked
17 
18 LOCK_FILE="/run/alternc/update_mails"
19 
20 # ALTERNC_MAIL is from local.sh
21 
22 # Somes check before start operations
23 if [ `id -u` -ne 0 ]; then
24  log_error "must be launched as root"
25 elif [ -f "$LOCK_FILE" ]; then
26  process=$(ps f -p `cat "$LOCK_FILE"|tail -1`|tail -1|awk '{print $NF;}')
27  if [ "$(basename $process)" = "$(basename "$0")" ] ; then
28  log_error "last cron unfinished or stale lock file ($LOCK_FILE)."
29  else
30  rm "$LOCK_FILE"
31  fi
32 fi
33 
34 # If there is ionice, add it to the command line
35 ionice=""
36 ionice > /dev/null && ionice="ionice -c 3 "
37 
38 # We lock the application
39 echo $$ > "$LOCK_FILE"
40 
41 # List the local addresses to DELETE
42 # Foreach => Mark for deleting and start deleting the files
43 # If process is interrupted, the row isn't deleted. We have to force it by reseting mail_action to 'DELETE'
44 mysql_query "SELECT id, address_id, quote(replace(path,'!','\\!')) FROM mailbox WHERE mail_action='DELETE';"|while read id address_id path ; do
45  mysql_query "UPDATE mailbox set mail_action='DELETING' WHERE id=$id;"
46  /usr/lib/alternc/mail_dodelete.php "$address_id"
47  # Check there is no instruction of changing directory, and check the first part of the string
48  if [[ "$path" =~ '../' || "$path" =~ '/..' || ! "'$ALTERNC_MAIL'" == "${path:0:$((${#ALTERNC_MAIL}+1))}'" ]] ; then
49  # The path will be empty for mailman addresses
50  if [[ "$path" != "''" ]]; then
51  echo "Error : this directory will not be deleted, pattern incorrect"
52  continue
53  fi
54  fi
55 
56  # If no dir, DELETE
57  # If dir and rm ok, DELETE
58  # Other case, do nothing
59  if [ -d "${path//\'/}" ] ; then
60  $ionice rm -rf "${path//\'/}" && mysql_query "DELETE FROM mailbox WHERE id=$id AND mail_action='DELETING';"
61  # Do the rm again in case of newly added file during delete. Should not be usefull
62  test -d "${path//\'/}" && $ionice rm -rf "${path//\'/}"
63  else
64  mysql_query "DELETE FROM mailbox WHERE id=$id AND mail_action='DELETING';"
65  fi
66 done
67 
68 # List the adresses to DELETE
69 # delete aliases of this pop/imap email too :
70 mysql_query "DELETE r FROM recipient r LEFT JOIN address a ON r.address_id = a.id WHERE a.mail_action = 'DELETE' OR a.mail_action = 'DELETING';"
71 # Delete if only if there isn't any mailbox refering to it
72 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');"
73 
74 # Delete the lock
75 rm -f "$LOCK_FILE"