#!/bin/bash
#
# evaluate binary classifiers of dspam tagging of a given mailbox for the given time period
#
# © 2017 Lars Täuber GPL 2.1
#
# 201703 initial version
#


if [ $# -lt 1 ]
then
    echo "usage: $0 USER [DAYS] - only take mails into account that are not older than DAYS days"
    exit 1
fi

if [ $# -ge 2 ]
then
    DAYS=$2
fi

           HAM=$(doveadm search -u $1 ${DAYS:+SAVEDSINCE ${DAYS}d} HEADER X-DSPAM-Result '' NOT MAILBOX INBOX/Junk | wc -l)
          SPAM=$(doveadm search -u $1 ${DAYS:+SAVEDSINCE ${DAYS}d} HEADER X-DSPAM-Result ''     MAILBOX INBOX/Junk | wc -l)

     SPAM_TRUE=$(doveadm search -u $1 ${DAYS:+SAVEDSINCE ${DAYS}d} HEADER X-DSPAM-Result 'Spam'     MAILBOX INBOX/Junk | wc -l)
    SPAM_FALSE=$(doveadm search -u $1 ${DAYS:+SAVEDSINCE ${DAYS}d} HEADER X-DSPAM-Result 'Spam' NOT MAILBOX INBOX/Junk | wc -l)

      HAM_TRUE=$(doveadm search -u $1 ${DAYS:+SAVEDSINCE ${DAYS}d} HEADER X-DSPAM-Result 'Innocent'    NOT MAILBOX INBOX/Junk | wc -l)
 let HAM_TRUE+=$(doveadm search -u $1 ${DAYS:+SAVEDSINCE ${DAYS}d} HEADER X-DSPAM-Result 'Whitelisted' NOT MAILBOX INBOX/Junk | wc -l)

     HAM_FALSE=$(doveadm search -u $1 ${DAYS:+SAVEDSINCE ${DAYS}d} HEADER X-DSPAM-Result 'Innocent'    MAILBOX INBOX/Junk | wc -l)
let HAM_FALSE+=$(doveadm search -u $1 ${DAYS:+SAVEDSINCE ${DAYS}d} HEADER X-DSPAM-Result 'Whitelisted' MAILBOX INBOX/Junk | wc -l)

printf "tp = %6d / %6d => %5s%%\n" $SPAM_TRUE  $SPAM $(echo -e "scale=2\n 100 * $SPAM_TRUE  / $SPAM" | bc -l)
printf "fn = %6d / %6d => %5s%%\n" $HAM_FALSE  $SPAM $(echo -e "scale=2\n 100 * $HAM_FALSE  / $SPAM" | bc -l)

printf "tn = %6d / %6d => %5s%%\n" $HAM_TRUE   $HAM  $(echo -e "scale=2\n 100 * $HAM_TRUE   / $HAM"  | bc -l)
printf "fp = %6d / %6d => %5s%%\n" $SPAM_FALSE $HAM  $(echo -e "scale=2\n 100 * $SPAM_FALSE / $HAM"  | bc -l)

