Skip to content
Snippets Groups Projects
freedombone-archive-mail 2.64 KiB
Newer Older
Bob Mottram's avatar
Bob Mottram committed
#!/bin/bash
Bob Mottram's avatar
Bob Mottram committed
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
Bob Mottram's avatar
Bob Mottram committed
#
Bob Mottram's avatar
Bob Mottram committed
#                              Freedom in the Cloud
Bob Mottram's avatar
Bob Mottram committed
#
# Archives old email

# License
# =======
#
Bob Mottram's avatar
Bob Mottram committed
# Copyright (C) 2015-2019 Bob Mottram <bob@freedombone.net>
Bob Mottram's avatar
Bob Mottram committed
#
# This program is free software: you can redistribute it and/or modify
Bob Mottram's avatar
Bob Mottram committed
# it under the terms of the GNU Affero General Public License as published by
Bob Mottram's avatar
Bob Mottram committed
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Bob Mottram's avatar
Bob Mottram committed
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
Bob Mottram's avatar
Bob Mottram committed
#
Bob Mottram's avatar
Bob Mottram committed
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Bob Mottram's avatar
Bob Mottram committed

PROJECT_NAME='freedombone'

export TEXTDOMAIN=${PROJECT_NAME}-archive-mail
export TEXTDOMAINDIR="/usr/share/locale"

Bob Mottram's avatar
Bob Mottram committed
source "/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-validation"
Bob Mottram's avatar
Bob Mottram committed
for d in /home/*/ ; do
    USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
Bob Mottram's avatar
Bob Mottram committed
    if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
Bob Mottram's avatar
Bob Mottram committed
        # for every user who has a mail directory
Bob Mottram's avatar
Bob Mottram committed
        if [ -d "/home/$USERNAME/Maildir" ]; then
Bob Mottram's avatar
Bob Mottram committed
            MUTTRC=/home/$USERNAME/.muttrc
            # update archives
            python /usr/bin/cleanup-maildir --archive-folder="archive" --maildir-root="/home/$USERNAME/Maildir" archive ""
            # ensure the user has permissions on the archives
            for archive_dir in /home/$USERNAME/Maildir/archive-* ; do
Bob Mottram's avatar
Bob Mottram committed
                chown -R "$USERNAME":"$USERNAME" "$archive_dir"
Bob Mottram's avatar
Bob Mottram committed
            done
            # add the archive to .muttrc if needed
Bob Mottram's avatar
Bob Mottram committed
            if [ -f "$MUTTRC" ]; then
                MUTT_MAILBOXES=$(grep "mailboxes =" "$MUTTRC")
                YR=$(date +"%Y")
                PREV_YR=$((YR - 1))
                BACKUP_DIRECTORY=archive-$YR
                PREV_BACKUP_DIRECTORY=archive-$PREV_YR
Bob Mottram's avatar
Bob Mottram committed
                if [[ $MUTT_MAILBOXES != *$BACKUP_DIRECTORY* ]]; then
                    if [[ $MUTT_MAILBOXES == *$PREV_BACKUP_DIRECTORY* ]]; then
Bob Mottram's avatar
Bob Mottram committed
                        sed -i "s|$PREV_BACKUP_DIRECTORY|$PREV_BACKUP_DIRECTORY =$BACKUP_DIRECTORY|g" "$MUTTRC"
Bob Mottram's avatar
Bob Mottram committed
                        sed -i "s|$MUTT_MAILBOXES|$MUTT_MAILBOXES =$BACKUP_DIRECTORY|g" "$MUTTRC"
Bob Mottram's avatar
Bob Mottram committed
                    chown "$USERNAME":"$USERNAME" "$MUTTRC"
Bob Mottram's avatar
Bob Mottram committed
                fi
            fi
        fi
    fi
done

exit 0