Re: dealing with spam

Subject: Re: dealing with spam

Date: Sun, 22 Mar 2026 15:50:07 +0200

To: Alan Schmitt, notmuch@notmuchmail.org

Cc:

From: Teemu Likonen


* 2026-03-22 11:24:43+0100, Alan Schmitt wrote:

> I would like to have a local spam bayesian filtering system that works
> with notmuch (so that I can use tagging to train and then filter
> mails), and I don’t know how to proceed. I searched the FAQ, the
> How-to, and the mailing-list archives and could not find much. Do you
> have any pointers or tips to share?

I have a full script written in the Fish shell language but it's pretty
much of my custom stuff that it would be probably too much as a starting
point. I'll paste some parts of it and strip it down to make it more
easily understandable. This is the Fish language and bogofilter spam
filtering program.

Check and tag new messages

    function bogofilter_check
        for msgid in (notmuch search --output=messages tag:new)
            notmuch show --exclude=false --format=raw $msgid | bogofilter

            switch $status
                case 0 # Spam
                    notmuch tag +spam $msgid
                    notmuch tag +learn-spam $msgid

                case 1 # Ham
                    notmuch tag +learn-ham $msgid

                case 2
                    notmuch tag +unsure $msgid

                case '*'
                    return 1
            end
        end
    end

Teach the spam system

    function bogofilter_execute
        set -l bf_opts $argv[1]
        set -l other $argv[2..]
        notmuch show --exclude=false --format=mbox $other | bogofilter -M "$bf_opts"
    end

    function bogofilter_teach
        # Mark old spam as read.
        notmuch tag -unread tag:unread AND tag:spam AND date:..7days

        # Convert conflicting learn tags as unsure.
        notmuch tag +unsure -spam -learn-ham -learn-spam \
            tag:learn-ham AND tag:learn-spam
        notmuch tag +unsure -spam -unlearn-ham -unlearn-spam \
            tag:unlearn-ham AND tag:unlearn-spam

        set -l query tag:learn-spam
        bogofilter_execute -s $query
        notmuch tag +spam-db +spam -learn-spam -unsure -- $query

        set -l query tag:learn-ham
        bogofilter_execute -n $query
        notmuch tag +ham-db -learn-ham -spam -unsure -- $query

        return 0
    end

Remove old spam message files

    function remove_old_spam
        # Save spam messages at least days_min and count_min.
        set -l days_min 365
        set -l count_min 1000

        set -l msgs (notmuch count tag:spam AND date:"$days_min"days..)
        if test "$msgs" -lt "$count_min"
            set msgs $count_min
        end

        notmuch search --exclude=false --output=files --format=text0 \
            --sort=newest-first --offset="$msgs" tag:spam | xargs -0r /usr/bin/rm -f
    end

-- 
/// Teemu Likonen - .-.. https://www.iki.fi/tlikonen/
// OpenPGP: DD3B8E8ABD28B98176E6A7CCCC9A5E615FCC1D93
/ old key: 6965F03973F0D4CA22B9410F0F2CAE0E07608462
signature.asc (application/pgp-signature)
_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org

Thread: