#!/bin/bash
# 
# Copyright (C) 2002 Laird Breyer
#  
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
# 
# Author:   Laird Breyer <laird@lbreyer.com>
#
# IMPLEMENTATION NOTES
#
# This script follows the mailcross testsuite interface
# requirements. Type man mailcross for details.
#
# The script accepts one of more commands on the command line,
# and may read STDIN and write STDOUT as follows:
#
# If $1 == "filter":
# In this case, a single email is expected on STDIN,
# and a list of category filenames is expected in $2, $3, etc.
# The script writes the category name corresponding to the 
# input email on STDOUT.
#
# If $1 == "learn":
# In this case, a standard mbox stream is expected on STDIN,
# while a suitable category file name is expected in $2. No output
# is written to STDOUT.
#
# If $1 == "clean":
# In this case, a directory is expected in $2, which is examined
# for old database information. If any old databases are found, they
# are purged or reset. No output is written to STDOUT.
#
# If $1 == "describe":
# In this case, STDIN and the command line are ignored. A single
# line is written on STDOUT, describing the filter functionality.
#
# If $1 == "bootstrap":
# In this case, the current script is copied to the directory $2,
# provided the classifier we're wrapping exists on the system.
#

# besides the standard interface, we also define a "mutilate" command,
# this cannot be a bash function, as it will be called by formail.

CRM="crm"
MIMEDEC=`which mimencode`
MUTILATE="$0 mutilate"

case "$1" in
    filter)
	shift
	ALLCATS=`for f in $@; do echo -n "$f.css " ; done`
	$MUTILATE | $CRM "-{
isolate (:lcr:)
alter (:lcr:) /[[:graph:]][-.,:[:alnum:]]*[[:graph:]]?/
isolate (:stats:)
{
        match (:data:) /.*/
        classify ($ALLCATS) ( :stats: ) [:data:] /:*:lcr:/ 
	match <nomultiline> (:x: :y: :z:) [:stats:] /\\\((.*\/)*(.*)\\.css\\\)/
	output /:*:z:/
}
}"
	;;
    learn)
	shift
	CATEGORY=`basename $1`
	DBPATH=${1/$CATEGORY/}
	formail -s $MUTILATE | $CRM "-{
isolate (:lcr:)
alter (:lcr:) /[[:graph:]][-.,:[:alnum:]]*[[:graph:]]?/
{
        match (:data:) /.*/
        learn <microgroom> (${DBPATH}${CATEGORY}.css) [:data:] /:*:lcr:/  
        exit /0/
}
}"
	;;
    clean)
	shift
	find "$1" -name "*.css" -exec rm {} \;
	find "$1" -name "*.toe" -exec rm -f {} \;
	find "$1" -name "*.foot" -exec rm -f {} \;
	find "$1" -name "*.tmp" -exec rm -f {} \;
	;;
    describe)
	VER="(unavailable?)"
	if [ -n "`which $CRM`" ] ; then
	    VER=`$CRM -v 2>&1 | head -1 | sed 's/^.*version //'`
	fi
	echo "crm114 $VER with default settings"
	;;
    bootstrap)
	if [ -d "$2" ] ; then
            if [ -n "`which $CRM`" -a -n "$MIMEDEC" ] ; then
		echo "selecting $0"
		cp "$0" "$2"
	    else
		echo "crm114 appears to be missing"
            fi
	else
	    echo "bad target directory $2"
	fi
	;;

    toe)
	ME="$0"
	shift
	TRUECAT="$1"
	shift
	CATS=`for f in "$@"; do echo -n "-c $f " ; done`
	cat > "$TEMPDIR/mailtoe.tmp"
	VERDICT=`cat $TEMPDIR/mailtoe.tmp | $ME filter "$@"`
	if [ "x$VERDICT" != "x`basename $TRUECAT`" ] ; then
	    cat "$TEMPDIR/mailtoe.tmp" >> $TRUECAT.toe
	    cat $TRUECAT.toe | $ME learn $TRUECAT
	fi
	echo -ne "$VERDICT"
	;;

    foot)
	ME="$0"
	shift
	TRUECAT="$1"
	shift
	CATS=`for f in "$@"; do echo -n "-c $f " ; done`
	cat > "$TEMPDIR/mailfoot.tmp"
	VERDICT=`cat "$TEMPDIR/mailfoot.tmp" | $ME filter "$@"`
	cat "$TEMPDIR/mailfoot.tmp" >> "$TRUECAT.foot"
	cat "$TRUECAT.foot" | $ME learn $TRUECAT
	echo -ne "$VERDICT"
	;;

    mutilate)
	# This function cleans up the email attachments
	# The code was taken verbatim from mailfilter.crm, version crm114-20031111-RC8
	if [ -z "$MIMEDEC" ] ; then
	    cat
	else
	    MIMEDEC="mimencode -u"
	    $CRM "-{
isolate (:do_base64:) /yes/
isolate (:undo_interruptus:) /yes/
isolate (:mime_decoder:) /$MIMEDEC/
isolate (:m_text:) //
isolate (:b_text:) /:*:_dw:/
isolate (:i_text:) /:*:_dw:/
#
#       do we do any expansions?
{
        #   expansion 1: - do we perform base64 expansions?
        {
                {
                        match [:do_base64:] /yes/
                        {
                            #  yes, expand base64's if there are any
                            #
                            #    Note: some spams don't even bother to use
                            #    a 'Content-Transfer-Encoding' marker,
                            #    and even fewer use Content-Type: text/whatever
                            #    so we have to sort of wing it, when to expand
                            #    what _might_ be base64 and when to ignore it.
                            #    For now, if it says it's a base64, it gets
                            #    expanded, no matter what the type.  Maybe
                            #    someday someone will put in a lockout for
                            #    things like .jpg files, .doc files, etc.
                            # 
                            isolate (:exp_text:)
                            match [:b_text:] (:a: :h: :b:) /(Content-Transfer-Encoding): base64(.*)/
                            match (:c:) [:b:] /([a-zA-Z0-9+=!\/]+:*:_nl:)\{2,200\}/
                            #
                            syscall (:*:c:) (:exp_text:) /:*:mime_decoder: /
                            #   and stuff the result back into b_text for
                            #   classification right in context.
                            alter (:c:) /:*:exp_text:/
                            #   and mark this piece of mime as \"prior\".
                            alter (:h:) /Content-Transfer-Prior-Encoding/
                            #   repeat till no more Mime base64 encodings 
                            liaf
                        }
                }
                alius
                {
                        #   if no base64 expansions enabled, empty out :b_text:
                        #                       
                        alter (:b_text:) //
                }
        }
        #
        #   If we had expansions, bust the html contents out of them, otherwise
        #   ignore b_text as it's redundant
        {
                {
                        match [:b_text:] /Content-Transfer-Prior-Encoding/
                        alter (:i_text:) /:*:b_text:/
                }
                alius
                {
                        #   if :b_text: _didn't_ have a base64, it's useless
                        alter (:b_text:) //
                }
        }
        #   expansion 2 :  do we bust HTML comments ( a.k.a. 
        #    hypertextus interruptus) out?
        {
                match [:undo_interruptus:] /yes/
                isolate (:commentbin:) //
                {
                    match [:i_text:] (:comment:) /<!--([^-]|-[^-]|--[^>])*-->/
                    alter (:commentbin:) /:*:commentbin: :*:comment:/
                    alter (:comment:) //
                    liaf
                }
                #     if we had at least 80 characters worth of comments, then
                #     it's worth using the decommented text, else not.
                #     (this my personal judgement call)
                {
                   {
                       match [:commentbin:] /(.)\{80,\}/
                   }
                   alius
                   {
                       alter (:i_text:) //
                   }
                }
        }
}
#    and reassemble the mucked-over text into the :m_text: var, always
#    with the base64's expanded, then a second decommented copy 
#
{
       alter (:m_text:) /:*:_dw: :*:_nl: :*:b_text: :*:_nl: :*:i_text: :*:_nl:/
}

    output /:*:m_text:/
}"
	fi
	;;
esac
