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