Rudolf Adamkovič Personal site


Configuration: 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 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
notmuch tag -spam is:spam

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

echo "Synchronizing with IMAP server..."
mbsync --all

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