Skip to content

Instantly share code, notes, and snippets.

@make-github-pseudonymous-again
Created May 14, 2017 20:33
Show Gist options
  • Save make-github-pseudonymous-again/9a49224a47cd153a67dc4af2feeaa379 to your computer and use it in GitHub Desktop.
Save make-github-pseudonymous-again/9a49224a47cd153a67dc4af2feeaa379 to your computer and use it in GitHub Desktop.
OfflineIMAP hooks for abunchoftags
#!/usr/bin/env sh
ALLMAILQUERY='( is:spam OR NOT is:spam ) AND ( is:deleted OR NOT is:deleted ) AND ( is:muted OR NOT is:muted )'
CACHE="$HOME/.cache/keywsync"
BEFORE="$CACHE/before"
STATE="$CACHE/state"
state=$(cat $STATE)
# Run notmuch new to detect any new or deleted files, or and renames.
if [ $state -eq 4 ] ; then
echo
echo '@#! post/4' ;
notmuch new
echo 5 > "$STATE"
state=5
fi
# Synchronize tags remote-to-local (-k) using a query that filters out anything
# but the maildir in question. Use the --mtime flag to only sync messages that
# match the query and are modified after offlineimap was run: echo
# $before_offlineimap.
if [ $state -eq 5 ] ; then
echo
echo '@#! post/5' ;
before=$(cat "$BEFORE")
keywsync -m "$HOME/.mail" -k -p -q "$ALLMAILQUERY" --mtime "$before" --no-replace-chars
echo 0 > "$STATE"
fi
# Store the current database revision for the next lastmod search in the
# local-to-remote step of your next search: $ notmuch_get_revision /path/to/db.
# Alternatively, store the revision from before the local-to-remote sync. In
# that way it is possible to detect local changes that happened during the
# sync. This will re-check the messages that were modified as part of the
# remote-to-local sync.
# I CHOSE THE ALTERNATIVE WAY, SEE presynchook
# notmuch_get_revision > $LASTMOD
#!/usr/bin/env sh
ALLMAILQUERY='( is:spam OR NOT is:spam ) AND ( is:deleted OR NOT is:deleted ) AND ( is:muted OR NOT is:muted )'
CACHE="$HOME/.cache/keywsync"
LASTMOD="$CACHE/lastmod"
NEXTMOD="$CACHE/nextmod"
BEFORE="$CACHE/before"
STATE="$CACHE/state"
mkdir -p "$CACHE"
# Synchronize tags local-to-remote (-t), now all tag changes done in the
# notmuch db are synchronized with the message files (preferably using a
# lastmod: query [1] which catches messages where have been done after the
# revision of the db at the time of the last remote-to-local synchronization)
if [ ! -e "$LASTMOD" ] ; then
echo '-1' > "$LASTMOD"
fi
if [ ! -e "$STATE" ] ; then
echo 0 > "$STATE"
fi
state=$(cat $STATE)
# ``Alternatively, store the revision from before the local-to-remote sync. In
# that way it is possible to detect local changes that happened during the
# sync. This will re-check the messages that were modified as part of the
# remote-to-local sync.''
if [ $state -eq 0 ] ; then
echo
echo '@#! pre/0' ;
notmuch_get_revision "$HOME/.mail" > "$NEXTMOD"
echo 1 > "$STATE"
state=1
fi
if [ $state -eq 1 ] ; then
echo
echo '@#! pre/1' ;
lastmod=`expr $(cat $LASTMOD) + 1`
beforesync=$(cat "$NEXTMOD")
keywsync -m "$HOME/.mail" -t -p -q "lastmod:$lastmod..$beforesync AND ($ALLMAILQUERY)" --no-replace-chars
echo 2 > "$STATE"
state=2
fi
if [ $state -eq 2 ] ; then
echo
echo '@#! pre/2' ;
cat "$NEXTMOD" > "$LASTMOD"
echo 3 > "$STATE"
state=3
fi
# Save the current unix time: $ before_offlineimap=$( date +%s )
if [ $state -eq 3 ] ; then
echo
echo '@#! pre/3' ;
date +%s > "$BEFORE"
echo 4 > "$STATE"
state=4
fi
# Run offlineimap to synchronize your local maildir and messages with the
# remote. According to the offlineimap documentation [0] the X-Keywords flags
# are synchronized in the same way as maildir flags (whatever that means [2]).
# (NOTHING TO DO)
# SEE postsynchook FOR THE REST
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment