4fac2474686ff9d11d139fe7d7ffb373ef7c6580
[distro-setup] / offlineimap-sync
1 #!/bin/bash
2
3 set -eE -o pipefail
4 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
5 # mail-route can get messed up a bit randomly, I don't know why.
6 #/b/ds/mail-route up | /b/log-quiet/log-once -1 mail-route
7
8 offlineimap -u quiet
9 shopt -s nullglob
10
11 omv() { # offlineimap mv. move mail files within $src_base/$1 to /m/md/$2
12 src="$1"
13 dst="$2"
14 found_files=false
15 for x in new cur; do
16 files=($src_base/"$src"/$x/*)
17 if [[ $files ]]; then
18 found_files=true
19 mv "${files[@]}" /m/md/"$dst"/$x
20 fi
21 done
22 }
23
24 src_base=/m/offlineimap
25 omv "Sent Items" "Sent"
26 omv INBOX offlineimaptmp
27 src_base=/m/md
28 if $found_files; then
29 sieve-filter -eW ~/sieve/main.sieve offlineimaptmp &>/dev/null
30 # the default folder is INBOX for anything leftover
31 omv offlineimaptmp INBOX
32 # remove messages from remote host
33 offlineimap -u quiet
34 # this makes us sit and wait when we want to use mu and this is running in a cronjob.
35 # todo: emacs updates the index much faster. what command is it running? I'd like
36 # to just run that
37 # looks like it might be mu index --lazy-check, but that still takes like 10 seconds,
38 # figure out if that is the same speed, or if we can make it faster.
39 #mu index &>/dev/null ||:
40 fi
41
42 # find but ignore directories which dont exist, assuming first args are directories
43 # and a following arg starts with -
44 myfind() {
45 dirs=()
46 for d; do
47 if [[ $d == -* ]]; then
48 # past dirs, onto options
49 break
50 fi
51 shift
52 if [[ -e $d ]]; then
53 dirs+=($d)
54 fi
55 done
56 if [[ $dirs ]]; then
57 find ${dirs[@]} $@
58 fi
59 }
60
61 # qemu-devel is our biggest list by far, so occasionally
62 # I want to hop into conversations about our mailing
63 # systems there, but I don't need many old messages.
64 myfind /nocow/user/fsfmd/l/qemu-devel/new -type f -mtime +14 -execdir rm -- '{}' +
65
66
67 myfind /nocow/user/fsfmd/{sec,Spam,Drafts,{rtcc,sysadmin,l/outreachy-mentors}/new} -type f -mtime +100 -execdir rm -- '{}' +
68
69 myfind /nocow/user/fsfmd/log -type f -mtime +300 -execdir rm -- '{}' +
70
71
72 # delete based on http://deflexion.com/2006/05/imap-way-of-deleting-message
73 sieve-filter -eW -o mail_location=maildir:/nocow/user/fsfmd:LAYOUT=fs:INBOX=/nocow/user/fsfmd/INBOX ~/sieve/fsf.sieve INBOX delete &>>/tmp/fsfsieve.log
74
75 # to test new rules, update fsf-test.sieve, run these commands, then copy new fsf-test.sieve to fsf.sieve
76 # sieve-filter -o mail_location=maildir:/nocow/user/fsfmd:LAYOUT=fs:INBOX=/nocow/user/fsfmd/INBOX ~/sieve/fsf-test.sieve INBOX &>/tmp/testfsfsieve.log
77 # sed -rn '/^Performed actions:/{n;n;p}' /tmp/testfsfsieve.log | sort -u