1#!/bin/bash 2# 3# Copyright (C) 2002 Laird Breyer 4# 5# This program is free software; you can redistribute it and/or modify 6# it under the terms of the GNU General Public License as published by 7# the Free Software Foundation; either version 3 of the License, or 8# (at your option) any later version. 9# 10# This program is distributed in the hope that it will be useful, 11# but WITHOUT ANY WARRANTY; without even the implied warranty of 12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13# GNU General Public License for more details. 14# 15# You should have received a copy of the GNU General Public License 16# along with this program; if not, write to the Free Software 17# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18# 19# Author: Laird Breyer <laird@lbreyer.com> 20# 21# IMPLEMENTATION NOTES 22# 23# This script follows the mailcross testsuite interface 24# requirements. Type man mailcross for details. 25# 26# The script accepts one of more commands on the command line, 27# and may read STDIN and write STDOUT as follows: 28# 29# If $1 == "filter": 30# In this case, a single email is expected on STDIN, 31# and a list of category filenames is expected in $2, $3, etc. 32# The script writes the category name corresponding to the 33# input email on STDOUT. 34# 35# If $1 == "learn": 36# In this case, a standard mbox stream is expected on STDIN, 37# while a suitable category file name is expected in $2. No output 38# is written to STDOUT. 39# 40# If $1 == "clean": 41# In this case, a directory is expected in $2, which is examined 42# for old database information. If any old databases are found, they 43# are purged or reset. No output is written to STDOUT. 44# 45# If $1 == "describe": 46# In this case, STDIN and the command line are ignored. A single 47# line is written on STDOUT, describing the filter functionality. 48# 49# If $1 == "bootstrap": 50# In this case, the current script is copied to the directory $2, 51# provided the classifier we're wrapping exists on the system. 52# 53 54# besides the standard interface, we also define a "mutilate" command, 55# this cannot be a bash function, as it will be called by formail. 56 57CRM="crm" 58MIMEDEC=`which mimencode` 59MUTILATE="$0 mutilate" 60 61case "$1" in 62 filter) 63 shift 64 ALLCATS=`for f in $@; do echo -n "$f.css " ; done` 65 $MUTILATE | $CRM "-{ 66isolate (:lcr:) 67alter (:lcr:) /[[:graph:]][-.,:[:alnum:]]*[[:graph:]]?/ 68isolate (:stats:) 69{ 70 match (:data:) /.*/ 71 classify ($ALLCATS) ( :stats: ) [:data:] /:*:lcr:/ 72 match <nomultiline> (:x: :y: :z:) [:stats:] /\\\((.*\/)*(.*)\\.css\\\)/ 73 output /:*:z:/ 74} 75}" 76 ;; 77 learn) 78 shift 79 CATEGORY=`basename $1` 80 DBPATH=${1/$CATEGORY/} 81 formail -s $MUTILATE | $CRM "-{ 82isolate (:lcr:) 83alter (:lcr:) /[[:graph:]][-.,:[:alnum:]]*[[:graph:]]?/ 84{ 85 match (:data:) /.*/ 86 learn <microgroom> (${DBPATH}${CATEGORY}.css) [:data:] /:*:lcr:/ 87 exit /0/ 88} 89}" 90 ;; 91 clean) 92 shift 93 find "$1" -name "*.css" -exec rm {} \; 94 find "$1" -name "*.toe" -exec rm -f {} \; 95 find "$1" -name "*.foot" -exec rm -f {} \; 96 find "$1" -name "*.tmp" -exec rm -f {} \; 97 ;; 98 describe) 99 VER="(unavailable?)" 100 if [ -n "`which $CRM`" ] ; then 101 VER=`$CRM -v 2>&1 | head -1 | sed 's/^.*version //'` 102 fi 103 echo "crm114 $VER with default settings" 104 ;; 105 bootstrap) 106 if [ -d "$2" ] ; then 107 if [ -n "`which $CRM`" -a -n "$MIMEDEC" ] ; then 108 echo "selecting $0" 109 cp "$0" "$2" 110 else 111 echo "crm114 appears to be missing" 112 fi 113 else 114 echo "bad target directory $2" 115 fi 116 ;; 117 118 toe) 119 ME="$0" 120 shift 121 TRUECAT="$1" 122 shift 123 CATS=`for f in "$@"; do echo -n "-c $f " ; done` 124 cat > "$TEMPDIR/mailtoe.tmp" 125 VERDICT=`cat $TEMPDIR/mailtoe.tmp | $ME filter "$@"` 126 if [ "x$VERDICT" != "x`basename $TRUECAT`" ] ; then 127 cat "$TEMPDIR/mailtoe.tmp" >> $TRUECAT.toe 128 cat $TRUECAT.toe | $ME learn $TRUECAT 129 fi 130 echo -ne "$VERDICT" 131 ;; 132 133 foot) 134 ME="$0" 135 shift 136 TRUECAT="$1" 137 shift 138 CATS=`for f in "$@"; do echo -n "-c $f " ; done` 139 cat > "$TEMPDIR/mailfoot.tmp" 140 VERDICT=`cat "$TEMPDIR/mailfoot.tmp" | $ME filter "$@"` 141 cat "$TEMPDIR/mailfoot.tmp" >> "$TRUECAT.foot" 142 cat "$TRUECAT.foot" | $ME learn $TRUECAT 143 echo -ne "$VERDICT" 144 ;; 145 146 mutilate) 147 # This function cleans up the email attachments 148 # The code was taken verbatim from mailfilter.crm, version crm114-20031111-RC8 149 if [ -z "$MIMEDEC" ] ; then 150 cat 151 else 152 MIMEDEC="mimencode -u" 153 $CRM "-{ 154isolate (:do_base64:) /yes/ 155isolate (:undo_interruptus:) /yes/ 156isolate (:mime_decoder:) /$MIMEDEC/ 157isolate (:m_text:) // 158isolate (:b_text:) /:*:_dw:/ 159isolate (:i_text:) /:*:_dw:/ 160# 161# do we do any expansions? 162{ 163 # expansion 1: - do we perform base64 expansions? 164 { 165 { 166 match [:do_base64:] /yes/ 167 { 168 # yes, expand base64's if there are any 169 # 170 # Note: some spams don't even bother to use 171 # a 'Content-Transfer-Encoding' marker, 172 # and even fewer use Content-Type: text/whatever 173 # so we have to sort of wing it, when to expand 174 # what _might_ be base64 and when to ignore it. 175 # For now, if it says it's a base64, it gets 176 # expanded, no matter what the type. Maybe 177 # someday someone will put in a lockout for 178 # things like .jpg files, .doc files, etc. 179 # 180 isolate (:exp_text:) 181 match [:b_text:] (:a: :h: :b:) /(Content-Transfer-Encoding): base64(.*)/ 182 match (:c:) [:b:] /([a-zA-Z0-9+=!\/]+:*:_nl:)\{2,200\}/ 183 # 184 syscall (:*:c:) (:exp_text:) /:*:mime_decoder: / 185 # and stuff the result back into b_text for 186 # classification right in context. 187 alter (:c:) /:*:exp_text:/ 188 # and mark this piece of mime as \"prior\". 189 alter (:h:) /Content-Transfer-Prior-Encoding/ 190 # repeat till no more Mime base64 encodings 191 liaf 192 } 193 } 194 alius 195 { 196 # if no base64 expansions enabled, empty out :b_text: 197 # 198 alter (:b_text:) // 199 } 200 } 201 # 202 # If we had expansions, bust the html contents out of them, otherwise 203 # ignore b_text as it's redundant 204 { 205 { 206 match [:b_text:] /Content-Transfer-Prior-Encoding/ 207 alter (:i_text:) /:*:b_text:/ 208 } 209 alius 210 { 211 # if :b_text: _didn't_ have a base64, it's useless 212 alter (:b_text:) // 213 } 214 } 215 # expansion 2 : do we bust HTML comments ( a.k.a. 216 # hypertextus interruptus) out? 217 { 218 match [:undo_interruptus:] /yes/ 219 isolate (:commentbin:) // 220 { 221 match [:i_text:] (:comment:) /<!--([^-]|-[^-]|--[^>])*-->/ 222 alter (:commentbin:) /:*:commentbin: :*:comment:/ 223 alter (:comment:) // 224 liaf 225 } 226 # if we had at least 80 characters worth of comments, then 227 # it's worth using the decommented text, else not. 228 # (this my personal judgement call) 229 { 230 { 231 match [:commentbin:] /(.)\{80,\}/ 232 } 233 alius 234 { 235 alter (:i_text:) // 236 } 237 } 238 } 239} 240# and reassemble the mucked-over text into the :m_text: var, always 241# with the base64's expanded, then a second decommented copy 242# 243{ 244 alter (:m_text:) /:*:_dw: :*:_nl: :*:b_text: :*:_nl: :*:i_text: :*:_nl:/ 245} 246 247 output /:*:m_text:/ 248}" 249 fi 250 ;; 251esac 252