Personal website Rudolf Adamkovič

Home / Notmuch / Notmuch


My pre-new hook

The ~/.notmuch/hooks/pre-new script

runs just before notmuch new,

written in Bash:

set -e

# Signature:
#   file QUERY
# Description:
#   Return all files matching QUERY.
files() {
    QUERY=$@
    notmuch search --output=files --exclude=false ${QUERY}
}

# Signature:
#   move DIR QUERY
# Description:
#   Move all files matching QUERY to DIR.
move() {
    DIR=${1} QUERY=${@:2}
    FILES=($(files ${QUERY}))
    for FILE in ${FILES[*]}; do
        # if test -f ${FILE}; then
            TO_FILE=${FILE##*/} # just basename
            TO_FILE=${TO_FILE/,U=[0-9]*/} # no metadata
            mv ${FILE} ${DIR}/${TO_FILE}
        # fi
    done
}

# Signature:
#   delete QUERY
# Description:
#   Delete all files matching QUERY.
delete() {
    QUERY=${@}
    FILES=($(files ${QUERY}))
    if test ${#FILES[*]} -gt 0; then
        rm -f ${FILES[*]}
    fi
}

ROOT=$(notmuch config get database.mail_root)
mkdir -p ${ROOT}

echo "Moving not-spam messages..."
move ${ROOT}/INBOX/new folder:Spam and not is:spam and is:unread
move ${ROOT}/INBOX/cur folder:Spam and not is:spam and not is:unread

echo "Deleting messages..."
delete is:deleted

echo "Synchronizing with IMAP server..."
if ping -c 1 imap.fastmail.com &>/dev/null; then
    mbsync --all
else
    echo "IMAP server unreachable." >&2
fi

echo "Moving spam messages..."
move ${ROOT}/Spam/new is:spam and not folder:Spam and is:unread
move ${ROOT}/Spam/cur is:spam and not folder:Spam and not is:unread

echo "Tagging spam messages..."
notmuch tag +spam folder:Spam

echo "Tagging hard-core spammers..."
notmuch tag +spam -unread -- \
    from:robinhood.com or \
    from:golemclub.sk

© 2025 Rudolf Adamkovič under GNU General Public License version 3.
Made with Emacs and secret alien technologies of yesteryear.