TDD your Sieve filters

Do you find cumbersome to craft your Sieve E-mail filters?

I know I do, but with a little help, it becomes dead easy to iterate on them without needing to wait for receiving E-mails from your best friends and foes alike.

I normally use claws-mail and claws-mail-managesieve to retrieve and push sieve filters, because its support is quite good. Edit your sieve filter in your editor of choice (E.g: Spacemacs with sieve-mode or even better, php-mode), and test it against a downloaded mail with sieve-test(1) from the dovecot-sieve Debian package.

We can use sieve-test against a folder of your most annoying E-mail types. With that, you can build yourself a TDD harness for them, with something as simple as:

#!/bin/bash

if [ "$1" == "-h" ]; then
    echo "Usage: $0 [mailfile] \
    \
    Runs and formats sieve-test(1) against ./mail-examples/* \
    Eg: $0 \
    \
    If mailfile is provided, it runs against ./mail-examples/[mailfile] \
    (note that [mailfile] can contain globs) \
    Eg: $0 github-\*.eml"
    echo
    exit
fi

DIR=./mail-examples

if [ -z "$1" ]; then
    MAILFILE="$DIR/*"
else
    MAILFILE="$DIR/$1"
fi

for i in ${MAILFILE};
do
    echo -e "\e[1mProcessing \e[93m$i\e[0m"
    sieve-test sieve.sieve "$i" 2>&1 | sed 's/^/     /'
    echo
done

Remember that normally your Email provider adds their own headers to the mail; you can use them to further cribe your received mails.

Cheers!

Víctor Cuadrado Juan

I'm Víctor Cuadrado Juan, a developer and FOSS enthusiast, in love with Linux. Currently living in Nürnberg, Germany. Feel free to waste your precious time around here, or to contact me.