#!/usr/local/bin/tclsh
#Mail filter by F. Voloch.
#add filtering criteria to each of the fields below
#if creating a new field variable remember to set a default value!!

#really bad subjects go to limbo and dubious subjects to spam folder

set bad_subject_list {"Low Cost" "Hot babes" "Hu Bin" Playstation \
aphrodisiac discount penis horny Credit Card "ks_c_5601-1987" \
marketing sales WSEAS Spice ADV: ICTCM debt ITEC Mortgage \
"EUC-KR" Quotes}

set subject_list {"LIBRARY NEWS" Informational resume "\\$" Money Free \
Official supplies grader position Operational Visa Phone} 

#maybe need to preceed dots by two backslashes to prevent them from
#being interpreted as wildcard. One backslash doesn't seem enough.

set sender_list {ynmail digitaldivide excite.com wseas co.kr iwww.net \
hanmail abc.org.br dirnafax chat.ru scientificworld \
ibroadcast wses.org jreed@sigmaxi.org 3dnet}

set received_list {chat.ru netfirms.com snd.edu.gr forged cat.or.th \
"\\.net.co" asimco.com.cn hanmail}

set reply_list {excite.com zorbrooks@hotmail.com altavista.com yahoo.co.uk \
bemsn@aol.com swirve.com snowboarding.com fyiaj@yahoo.com}

set content_list {alternative related}

set korean_list {ks_c_5601-1987 euc-kr EUC-KR \
ISO-2022-KR KS_C_5601-1987}

set friend_list {myfriend@yahoo.co.uk otherfriend@korea}

#for forwarding uncomment next line and add email address, see also below

#set new address@domain

set replyline none
set fromline none
set subline none
set contline none
set receivedline none
set virusline none

set message [read stdin]

foreach line [split $message \n] {
if {[regexp {^From} $line]} {
append fromline $line
}
if {[regexp {^Reply-To:} $line]} {
append replyline $line
}
if {[regexp {^Subject:} $line]} {
append subline $line
}
if {[regexp {^Content-Type:} $line]} {
append contline $line
}
if {[regexp {^Received:} $line]} {
append receivedline $line
}
if {[regexp {^TVqQAA} $line]} {
append virusline $line
break
}
#uncomment next lines to get rid of nigerian scams or mail with links to pictures on the web.
#if {[regexp -nocase {nigeria} $line]} {
#append virusline $line
#break
#}
#if {[regexp -nocase {<img} $line]} {
#append virusline $line
#break
#}
}

set ok 1


foreach sender $sender_list {
if {[regexp ($sender)+ $fromline]} {
set ok 0
}
}

foreach subject $subject_list {
if {[regexp -nocase ($subject)+ $subline]} {
set ok 0
}
}

foreach sender $reply_list {
if {[regexp ($sender)+ $replyline]} {
set ok 0
}
}

foreach content $content_list {
if {[regexp ($content)+ $contline]} {
set ok 0
}
}

foreach content $received_list {
if {[regexp ($content)+ $receivedline]} {
set ok 0
}
}

foreach subject $bad_subject_list {
if {[regexp -nocase ($subject)+ $subline]} {
set ok -1
}
}

#a few exceptions first to deal with korean content

foreach content $korean_list {
if {[regexp ($content)+ $contline]} {
set ok -1 
}
}

#just in case you filter a friend by accident

foreach friend $friend_list {
if {[regexp ($friend)+ $fromline]} {
set ok 1
}
}

#finally kill all viruses

if {$virusline != "none"} {
set ok -1
}

if {$ok == 1} {
#to forward the message, uncomment next line
#puts [exec sendmail $new << $message]
#put message in inbox
set logfile [open inbox a+]
puts $logfile $message
close $logfile
} elseif {$ok == 0} {
#put the spam in a log file
set logfile [open spam a+]
puts $logfile $message
close $logfile
} elseif {$ok == -1} {
#really bad spam (ok=-1) goes to limbo but keep trace of it
set logfile [open crap a+]
puts $logfile $fromline
puts $logfile $subline
puts $logfile $virusline
close $logfile
}




