freedombone-archive-mail 2.6KB

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