freedombone-archive-mail 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Archives old email
  12. # License
  13. # =======
  14. #
  15. # Copyright (C) 2015-2016 Bob Mottram <bob@freedombone.net>
  16. #
  17. # This program is free software: you can redistribute it and/or modify
  18. # it under the terms of the GNU Affero General Public License as published by
  19. # the Free Software Foundation, either version 3 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. # GNU Affero General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU Affero General Public License
  28. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. PROJECT_NAME='freedombone'
  30. export TEXTDOMAIN=${PROJECT_NAME}-archive-mail
  31. export TEXTDOMAINDIR="/usr/share/locale"
  32. source /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-validation
  33. for d in /home/*/ ; do
  34. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  35. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  36. # for every user who has a mail directory
  37. if [ -d /home/$USERNAME/Maildir ]; then
  38. MUTTRC=/home/$USERNAME/.muttrc
  39. # update archives
  40. python /usr/bin/cleanup-maildir --archive-folder="archive" --maildir-root="/home/$USERNAME/Maildir" archive ""
  41. # ensure the user has permissions on the archives
  42. for archive_dir in /home/$USERNAME/Maildir/archive-* ; do
  43. chown -R $USERNAME:$USERNAME $archive_dir
  44. done
  45. # add the archive to .muttrc if needed
  46. if [ -f $MUTTRC ]; then
  47. MUTT_MAILBOXES=$(grep "mailboxes =" $MUTTRC)
  48. YR=$(date +"%Y")
  49. PREV_YR=$((YR - 1))
  50. BACKUP_DIRECTORY=archive-$YR
  51. PREV_BACKUP_DIRECTORY=archive-$PREV_YR
  52. if [[ $MUTT_MAILBOXES != *$BACKUP_DIRECTORY* ]]; then
  53. if [[ $MUTT_MAILBOXES == *$PREV_BACKUP_DIRECTORY* ]]; then
  54. sed -i "s|$PREV_BACKUP_DIRECTORY|$PREV_BACKUP_DIRECTORY =$BACKUP_DIRECTORY|g" $MUTTRC
  55. else
  56. sed -i "s|$MUTT_MAILBOXES|$MUTT_MAILBOXES =$BACKUP_DIRECTORY|g" $MUTTRC
  57. fi
  58. chown $USERNAME:$USERNAME $MUTTRC
  59. fi
  60. fi
  61. fi
  62. fi
  63. done
  64. exit 0