]> git.rmz.io Git - dotfiles.git/blob - isync/sync-mail
a3b8a7ab48742507cd7a40e489c0cbfbd51f33a3
[dotfiles.git] / isync / sync-mail
1 #!/usr/bin/env bash
2
3 dir="$(dirname "${BASH_SOURCE[0]}")"
4 config=$dir/config
5
6 ### pre-sync {{{
7
8 ### }}}
9
10 ### sync {{{
11 mbsync -c "$config" -a
12 errno=$?
13
14 if [[ $errno -ne 0 ]]; then
15 echo "mbsync failed, ignoring post-sync commands." >&2
16 exit $errno
17 fi
18 ### }}}
19
20 ### post-sync {{{
21 yt_src_mb=youtube-orig
22 yt_dst_mb=youtube
23 yt_parser=$dir/parse-mail.py
24 maildir=$(sed -nr 's/^Path\s*(.*)$/\1/p' $config)
25
26
27 if [[ -z $maildir ]]; then
28 echo "Could not extract 'Path' from 'MaildirStore' in '$config'" >&2
29 exit 1
30 fi
31
32 # expand tilde
33 maildir=${maildir/#~/$HOME}
34
35 shopt -s extglob
36 shopt -s nullglob
37 for mail in "$maildir"/$yt_src_mb/new/* ; do
38 mangled_mail=${mail/$yt_src_mb/$yt_dst_mb}
39 # remove UID for mbsync to regenerate it
40 mangled_mail="${mangled_mail/,U=+([0-9])}"
41
42 echo -n "Parsing new message '$(basename "$mail")'..."
43 $yt_parser <"$mail" >"$mangled_mail"
44
45 if [[ $? -eq 0 ]]; then
46 echo " Success."
47 else
48 echo " Failure! Copying message as is."
49 cp "$mail" "$mangled_mail"
50 fi
51
52 # sync {a,m}time
53 touch --reference "$mail" "$mangled_mail"
54 mv "$mail" "${mail/new/cur}S"
55 done
56 # resync new yt_dst_mb
57 mbsync -c "$config" gmail-$yt_src_mb gmail-$yt_dst_mb
58
59 ### }}}
60