]> git.rmz.io Git - dotfiles.git/commitdiff
isync: support for multiple accounts + make hooks more modular
authorSamir Benmendil <samir.benmendil@ultrahaptics.com>
Tue, 2 Jan 2018 10:50:59 +0000 (10:50 +0000)
committerSamir Benmendil <me@rmz.io>
Tue, 2 Jan 2018 11:59:43 +0000 (11:59 +0000)
isync/post-sync.d/10-youtube [new file with mode: 0755]
isync/sync-mail
systemd/user/mbsync@.service [moved from systemd/user/mbsync.service with 76% similarity]
systemd/user/mbsync@.timer [moved from systemd/user/mbsync.timer with 100% similarity]

diff --git a/isync/post-sync.d/10-youtube b/isync/post-sync.d/10-youtube
new file mode 100755 (executable)
index 0000000..b6b0aba
--- /dev/null
@@ -0,0 +1,58 @@
+#!/usr/bin/env bash
+
+errno=$1
+mbsync_args=$*
+
+if [[ $errno -ne 0 ]]; then
+    echo "mbsync failed, ignoring post-sync commands." >&2
+    exit
+fi
+
+if [[ "$mbsync_args" != "gmail" ]]; then
+    # only run for gmail account
+    exit
+fi
+
+yt_src_mb=youtube-orig
+yt_dst_mb=youtube
+yt_parser=$dir/parse-mail.py
+maildirs=( $(sed -nr 's/^Path\s*(.*)$/\1/p' $config) )
+
+if [[ -z $maildirs ]]; then
+    echo "Could not extract 'Path' from 'MaildirStore' in '$config'" >&2
+    exit 1
+fi
+
+shopt -s extglob
+shopt -s nullglob
+for maildir in "${maildirs[@]}"; do
+    # expand tilde
+    maildir=${maildir/#~/$HOME}
+
+    if [[ -d "$maildir/$yt_src_mb" ]]; then
+        echo "No mailbox at '$maildir/$yt_src_mb'"
+        continue
+    fi
+
+    for mail in "$maildir"/$yt_src_mb/new/* ; do
+        mangled_mail=${mail/$yt_src_mb/$yt_dst_mb}
+        # remove UID for mbsync to regenerate it
+        mangled_mail="${mangled_mail/,U=+([0-9])}"
+
+        echo -n "Parsing new message '$(basename "$mail")'..."
+        $yt_parser <"$mail" >"$mangled_mail"
+
+        if [[ $? -eq 0 ]]; then
+            echo " Success."
+        else
+            echo " Failure! Copying message as is."
+            cp "$mail" "$mangled_mail"
+        fi
+
+        # sync {a,m}time
+        touch --reference "$mail" "$mangled_mail"
+        mv "$mail" "${mail/new/cur}S"
+    done
+    # resync new yt_dst_mb
+    mbsync -c "$config" gmail-$yt_src_mb gmail-$yt_dst_mb
+done
index a3b8a7ab48742507cd7a40e489c0cbfbd51f33a3..1bd0f521bb2cad34f0ea9be35f6682cffff76810 100755 (executable)
@@ -1,60 +1,19 @@
 #!/usr/bin/env bash
 
+mbsync_args="$*"
+
 dir="$(dirname "${BASH_SOURCE[0]}")"
 config=$dir/config
 
-### pre-sync {{{
-
-### }}}
+for hook in $(find $dir/pre-sync.d -type f -executable); do
+    "$hook" $errno $mbsync_args
+done
 
-### sync {{{
-mbsync -c "$config" -a
+mbsync -c "$config" $mbsync_args
 errno=$?
 
-if [[ $errno -ne 0 ]]; then
-    echo "mbsync failed, ignoring post-sync commands." >&2
-    exit $errno
-fi
-### }}}
-
-### post-sync {{{
-yt_src_mb=youtube-orig
-yt_dst_mb=youtube
-yt_parser=$dir/parse-mail.py
-maildir=$(sed -nr 's/^Path\s*(.*)$/\1/p' $config)
-
-
-if [[ -z $maildir ]]; then
-    echo "Could not extract 'Path' from 'MaildirStore' in '$config'" >&2
-    exit 1
-fi
-
-# expand tilde
-maildir=${maildir/#~/$HOME}
-
-shopt -s extglob
-shopt -s nullglob
-for mail in "$maildir"/$yt_src_mb/new/* ; do
-    mangled_mail=${mail/$yt_src_mb/$yt_dst_mb}
-    # remove UID for mbsync to regenerate it
-    mangled_mail="${mangled_mail/,U=+([0-9])}"
-
-    echo -n "Parsing new message '$(basename "$mail")'..."
-    $yt_parser <"$mail" >"$mangled_mail"
-
-    if [[ $? -eq 0 ]]; then
-        echo " Success."
-    else
-        echo " Failure! Copying message as is."
-        cp "$mail" "$mangled_mail"
-    fi
-
-    # sync {a,m}time
-    touch --reference "$mail" "$mangled_mail"
-    mv "$mail" "${mail/new/cur}S"
+for hook in $(find $dir/post-sync.d -type f -executable); do
+    "$hook" $errno $mbsync_args
 done
-# resync new yt_dst_mb
-mbsync -c "$config" gmail-$yt_src_mb gmail-$yt_dst_mb
-
-### }}}
 
+exit $errno
similarity index 76%
rename from systemd/user/mbsync.service
rename to systemd/user/mbsync@.service
index 27f04caf86d15b642b88f092e24ab99170b51673..83941360db78804790951ca5919cdc1186c06ab2 100644 (file)
@@ -4,4 +4,4 @@ After=network.target network-online.target dbus.socket
 
 [Service]
 Type=oneshot
-ExecStart=%h/.config/isync/sync-mail
+ExecStart=%h/.config/isync/sync-mail %i