host info updates
[distro-setup] / disabled / offlineimap-sync
1 #!/bin/bash
2 # I, Ian Kelling, follow the GNU license recommendations at
3 # https://www.gnu.org/licenses/license-recommendations.en.html. They
4 # recommend that small programs, < 300 lines, be licensed under the
5 # Apache License 2.0. This file contains or is part of one or more small
6 # programs. If a small program grows beyond 300 lines, I plan to switch
7 # its license to GPL.
8
9 # Copyright 2024 Ian Kelling
10
11 # Licensed under the Apache License, Version 2.0 (the "License");
12 # you may not use this file except in compliance with the License.
13 # You may obtain a copy of the License at
14
15 # http://www.apache.org/licenses/LICENSE-2.0
16
17 # Unless required by applicable law or agreed to in writing, software
18 # distributed under the License is distributed on an "AS IS" BASIS,
19 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 # See the License for the specific language governing permissions and
21 # limitations under the License.
22
23
24 set -eE -o pipefail
25 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
26 # mail-route can get messed up a bit randomly, I don't know why.
27 #/b/ds/mail-route up |& /b/log-quiet/log-once -1 mail-route
28
29 for f in $(awk '$1 == "localfolders" {print $NF}' ~/.offlineimaprc); do
30 mkdir -p $f
31 chmod 700 $f
32 done
33
34 offlineimap -u quiet
35 shopt -s nullglob
36
37 if grep -qP '^ *accounts.*fsf' ~/.offlineimaprc; then
38 if [[ ! -e /nocow/user/.mufsf ]]; then
39 mkdir -p /nocow/user/.mufsf
40 chmod 700 /nocow/user/.mufsf
41 mu index --maildir=/nocow/user/fsfmd
42 fi
43 fi
44
45 omv() { # offlineimap mv. move mail files within $src_base/$1 to /m/md/$2
46 src="$1"
47 dst="$2"
48 found_files=false
49 for x in new cur; do
50 files=($src_base/"$src"/$x/*)
51 if [[ $files ]]; then
52 found_files=true
53 mv "${files[@]}" /m/md/"$dst"/$x
54 fi
55 done
56 }
57
58 src_base=/m/offlineimap
59 omv "Sent Items" "Sent"
60 omv INBOX offlineimaptmp
61 src_base=/m/md
62 if $found_files; then
63 sieve-filter -eW ~/sieve/main.sieve offlineimaptmp &>/dev/null
64 # the default folder is INBOX for anything leftover
65 omv offlineimaptmp INBOX
66 # remove messages from remote host
67 offlineimap -u quiet
68 # this makes us sit and wait when we want to use mu and this is running in a cronjob.
69 # todo: emacs updates the index much faster. what command is it running? I'd like
70 # to just run that
71 # looks like it might be mu index --lazy-check, but that still takes like 10 seconds,
72 # figure out if that is the same speed, or if we can make it faster.
73 #mu index &>/dev/null ||:
74 fi
75
76
77 # delete based on http://deflexion.com/2006/05/imap-way-of-deleting-message
78 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
79
80 # to test new rules, update fsf-test.sieve, run these commands, then copy new fsf-test.sieve to fsf.sieve
81 # sieve-filter -o mail_location=maildir:/nocow/user/fsfmd:LAYOUT=fs:INBOX=/nocow/user/fsfmd/INBOX ~/sieve/fsf-test.sieve INBOX &>/tmp/testfsfsieve.log
82 # sed -rn '/^Performed actions:/{n;n;p}' /tmp/testfsfsieve.log | sort -u