Browse Source

Email archiving command

Bob Mottram 9 years ago
parent
commit
986eaa0d7a
3 changed files with 74 additions and 22 deletions
  1. BIN
      man/freedombone-archive-mail.1.gz
  2. 13
    22
      src/freedombone
  3. 61
    0
      src/freedombone-archive-mail

BIN
man/freedombone-archive-mail.1.gz View File


+ 13
- 22
src/freedombone View File

@@ -5187,6 +5187,19 @@ function email_archiving {
5187 5187
       return
5188 5188
   fi
5189 5189
 
5190
+  # ensure that the mail archive script is up to date
5191
+  if [ -f /usr/local/bin/${PROJECT_NAME}-archive-mail ]; then
5192
+      cp /usr/local/bin/${PROJECT_NAME}-archive-mail /etc/cron.daily/archivemail
5193
+  else
5194
+      if [ -f /usr/bin/${PROJECT_NAME}-archive-mail ]; then
5195
+          cp /usr/bin/${PROJECT_NAME}-archive-mail /etc/cron.daily/archivemail
5196
+      else
5197
+          echo "/usr/bin/${PROJECT_NAME}-archive-email was not found. ${PROJECT_NAME} might not have fully installed."
5198
+          exit 62379
5199
+      fi
5200
+  fi
5201
+  chmod +x /etc/cron.daily/archivemail
5202
+
5190 5203
   # update to the next commit
5191 5204
   if [ -d $INSTALL_DIR/cleanup-maildir ]; then
5192 5205
       if grep -q "cleanup-maildir commit" $COMPLETION_FILE; then
@@ -5222,28 +5235,6 @@ function email_archiving {
5222 5235
   fi
5223 5236
 
5224 5237
   cp $INSTALL_DIR/cleanup-maildir/cleanup-maildir /usr/bin
5225
-  echo '#!/bin/bash' > /etc/cron.daily/archivemail
5226
-  echo 'for d in /home/*/ ; do' >> /etc/cron.daily/archivemail
5227
-  echo '  USERNAME=$(echo "$d" | awk -F '"'"'/'"'"' '"'"'{print $3}'"'"')' >> /etc/cron.daily/archivemail
5228
-  echo '  if [[ $USERNAME != "git" ]]; then' >> /etc/cron.daily/archivemail
5229
-  echo '    if [ -d /home/$USERNAME/Maildir ]; then' >> /etc/cron.daily/archivemail
5230
-  echo '      MUTTRC=/home/$USERNAME/.muttrc' >> /etc/cron.daily/archivemail
5231
-  echo '      python /usr/bin/cleanup-maildir --archive-folder="archive" --maildir-root="/home/$USERNAME/Maildir" archive ""' >> /etc/cron.daily/archivemail
5232
-  echo '      chown -R $USERNAME:$USERNAME /home/$USERNAME/Maildir/archive-*' >> /etc/cron.daily/archivemail
5233
-  echo '      if [ -f $MUTTRC ]; then' >> /etc/cron.daily/archivemail
5234
-  echo '        MUTT_MAILBOXES=$(grep "mailboxes =" $MUTTRC)' >> /etc/cron.daily/archivemail
5235
-  echo '        BACKUP_DIRECTORY=archive-$(date +"%Y")' >> /etc/cron.daily/archivemail
5236
-  echo '        if [[ $MUTT_MAILBOXES != *$BACKUP_DIRECTORY* ]]; then' >> /etc/cron.daily/archivemail
5237
-  echo '          sed -i "s|$MUTT_MAILBOXES|$MUTT_MAILBOXES =$BACKUP_DIRECTORY|g" $MUTTRC' >> /etc/cron.daily/archivemail
5238
-  echo '          chown $USERNAME:$USERNAME $MUTTRC' >> /etc/cron.daily/archivemail
5239
-  echo '        fi' >> /etc/cron.daily/archivemail
5240
-  echo '      fi' >> /etc/cron.daily/archivemail
5241
-  echo '    fi' >> /etc/cron.daily/archivemail
5242
-  echo '  fi' >> /etc/cron.daily/archivemail
5243
-  echo 'done' >> /etc/cron.daily/archivemail
5244
-
5245
-  echo 'exit 0' >> /etc/cron.daily/archivemail
5246
-  chmod +x /etc/cron.daily/archivemail
5247 5238
 
5248 5239
   echo 'email_archiving' >> $COMPLETION_FILE
5249 5240
 }

+ 61
- 0
src/freedombone-archive-mail View File

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