[Spamscan-cvs] spamscan/bin spamscan,1.3,1.4
Brought to you by:
destari
|
From: <de...@us...> - 2003-07-22 20:52:28
|
Update of /cvsroot/spamscan/spamscan/bin In directory sc8-pr-cvs1:/tmp/cvs-serv3364 Modified Files: spamscan Log Message: Major changes happening. Added smarts to allow user to analyze mail messages, and build an index. Also allows user to view db all over an email interface. Not fully implemented, but should be bug free. Index: spamscan =================================================================== RCS file: /cvsroot/spamscan/spamscan/bin/spamscan,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** spamscan 22 Jul 2003 01:57:57 -0000 1.3 --- spamscan 22 Jul 2003 20:52:25 -0000 1.4 *************** *** 42,46 **** ########################################################## ! $version = "1.70b"; $url = "http:\/\/spamscan.sourceforge.net"; --- 42,46 ---- ########################################################## ! $version = "1.71b"; $url = "http:\/\/spamscan.sourceforge.net"; *************** *** 55,62 **** --- 55,64 ---- if (!($inbody) && !($endofmsg)) { if ($_ =~ /^From\: (.+)/gci) { + $fromemail = $1; $fromfield = $message_line; } elsif ($_ =~ /^To\: (.+)/gci) { $tofield = $message_line; } elsif ($_ =~ /^Subject\: (.+)/gci) { + $subjemail = $1; $subjectfield = $message_line; $xheader = $message_line; *************** *** 76,83 **** next; } else { ! if ($_ =~ /^\s*spamscan\s+(.+)\s+(.+)/cgi) { # ! $spampassword = $1; ! $spamcommand = $2; } $body = $body . $_; --- 78,93 ---- next; } else { ! $spamcmdtmp = $_; ! chomp $spamcmdtmp; ! if ($spamcmdtmp =~ /^\s*spamscan\s+(.+)\s+(.+)/gi) { # ! $spamcmdtmp =~ s/^\s+//g; ! $spamcmd = $spamcmdtmp; ! @spamcmdline = split/\s+/,$spamcmd; ! $spampassword = $spamcmdline[1]; ! $spamcommand = $spamcmdline[2]; ! next; ! } else { ! $bodyonly = $bodyonly . $_; } $body = $body . $_; *************** *** 92,96 **** $body =~ s/([0-9])\,([0-9])/\1\2/gi; $body =~ s/\>\<\/\\\]\[\{\}\=\+/ /gi; ! $body =~ s/\(\)\"\'\`\,\.\?\!\;/\n/gi; # Now, lets look for indicators in the From: field: --- 102,115 ---- $body =~ s/([0-9])\,([0-9])/\1\2/gi; $body =~ s/\>\<\/\\\]\[\{\}\=\+/ /gi; ! $body =~ s/\(\)\"\'\`\,\.\?\!\;/ /gi; ! ! $bodyonly = "$subjemail" . $bodyonly; ! $bodyonly =~ s/\n/ /gi; ! $bodyonly =~ s/<[^>]*>//gs; ! $bodyonly =~ s/\>\<\/\\\]\[\{\}\=\+\*\:/ /gi; ! $bodyonly =~ s/\(\)\"\'\`\,\.\?\!\;/ /gi; ! $bodyonly =~ s/\s+/ /gi; ! ! $learntext = $bodyonly; # Now, lets look for indicators in the From: field: *************** *** 124,128 **** } ! # run thru the filters &from_filter; --- 143,147 ---- } ! ################# # run thru the filters &from_filter; *************** *** 130,135 **** &body_filter; ! # then, put it all together, and determine if it's spam or not. ! &spam_algorithm; @junk = split/\s+/,$mailmessage[$fromfield]; --- 149,161 ---- &body_filter; ! if ($spampassword eq $passcode) { ! $from_value = $to_value = $body_value = 0; ! @spamreasons = (); ! &spam_algorithm; ! &do_command; ! } else { ! # then, put it all together, and determine if it's spam or not. ! &spam_algorithm; ! } @junk = split/\s+/,$mailmessage[$fromfield]; *************** *** 156,160 **** $xheaderdone = 1; print RMAIL "X-Spam-Scanned: $progname $version - $url\n"; - print RMAIL "X-Spam-Scanned: $spamcommand $spampassword\n"; print RMAIL "$spamreason_msg"; print RMAIL $line; --- 182,185 ---- *************** *** 308,312 **** $xheaderdone = 1; print LOCALMAILER "X-Spam-Scanned: $progname $version - $url\n"; ! # if ($itsspam) { print LOCALMAILER "$spamreason_msg"; } print LOCALMAILER "$spamreason_msg"; print LOCALMAILER $line; --- 333,337 ---- $xheaderdone = 1; print LOCALMAILER "X-Spam-Scanned: $progname $version - $url\n"; ! if ($spampassword eq $passcode) { print LOCALMAILER "X-Spam-Scanned: Executed command: $spamcmd\n"; } print LOCALMAILER "$spamreason_msg"; print LOCALMAILER $line; *************** *** 319,322 **** --- 344,501 ---- } + sub do_command { + if ($spamcommand =~ /^spam$/i) { + &analyze_spam; + &cmd_ok; + } elsif ($spamcommand =~ /^notspam$/i) { + &analyze_notspam; + &cmd_ok; + } elsif ($spamcommand =~ /^showdb$/i) { + &showdb; + &cmd_ok; + } else { + # command not recognized + &cmd_unknown; + } + } + + sub analyze_spam { + # do analyze + $dbfile = $filterdir . "spamscan"; + dbmopen(%phrasedb, "$dbfile", 0664) or die "dbmopen: $! ($dbfile)"; + + @words = split/\s+/,$learntext; + + $maxdepth = 4; + $maxsize = ($#words + 1); + # this loop does all the 1,2, or 3 word combos etc + for ($i=0; $i < $maxdepth; $i++) { + # this is the "starting element" loop + for ($j=0; $j < $maxsize; $j++) { + $end = $j + $i; + # this would be the printing loop + for ($k=$j; $k <= $end; $k++) { + $wordling = "$wordling " . "$words[$k]"; + } + $wordling =~ s/\s+$//; + $wordcksum = unpack ("%32C*", $wordling) % 32767; + # $phrasedb{$wordcksum}++; + $phrasedb{$wordling}++; + $wordling = ""; + } + } + + # write to file + untie %phrasedb; + $xheaderdone = 0; + foreach $line (@completemailmsg) { + if (($xheaderdone == 0) && ($line =~ /\A\r?\n\Z/)) { + $xheaderdone = 1; + push (@newcompletemailmsg, $line); + push (@newcompletemailmsg, $line); + next; + } + if ($xheaderdone == 0) { + push (@newcompletemailmsg, $line); + } + } + } + + sub analyze_notspam { + # do analyze + $dbfile = $filterdir . "spamscan"; + dbmopen(%phrasedb, "$dbfile", 0664) or die "dbmopen: $! ($dbfile)"; + + @words = split/\s+/,$learntext; + + $maxdepth = 4; + $maxsize = ($#words + 1); + + # this loop does all the 1,2, or 3 word combos etc + for ($i=0; $i < $maxdepth; $i++) { + # this is the "starting element" loop + for ($j=0; $j < $maxsize; $j++) { + $end = $j + $i; + # this would be the printing loop + for ($k=$j; $k <= $end; $k++) { + $wordling = "$wordling " . "$words[$k]"; + } + $wordling =~ s/\s+$//; + $wordcksum = unpack ("%32C*", $wordling) % 32767; + # $phrasedb{$wordcksum}--; + $phrasedb{$wordling}--; + $wordling = ""; + } + } + $phrasedb{$fromemail}--; + + + # write to file + untie %phrasedb; + $xheaderdone = 0; + foreach $line (@completemailmsg) { + if (($xheaderdone == 0) && ($line =~ /\A\r?\n\Z/)) { + $xheaderdone = 1; + push (@newcompletemailmsg, $line); + push (@newcompletemailmsg, $line); + next; + } + if ($xheaderdone == 0) { + push (@newcompletemailmsg, $line); + } + } + } + + sub showdb { + # open db file, read it, and write to email file + $xheaderdone = 0; + foreach $line (@completemailmsg) { + if (($xheaderdone == 0) && ($line =~ /\A\r?\n\Z/)) { + $xheaderdone = 1; + push (@newcompletemailmsg, "X-Spam-Scanned: SHOWDB - $spampassword $spamcommand\n"); + push (@newcompletemailmsg, $line); + push (@newcompletemailmsg, $line); + next; + } + if ($xheaderdone == 0) { + push (@newcompletemailmsg, $line); + } + } + + $dbfile = $filterdir . "spamscan"; + dbmopen(%phrasedb, "$dbfile", 0644) or die "dbmopen: $! ($dbfile)"; + foreach $wordling (sort keys %phrasedb) { + $newline = "$wordling -> $phrasedb{$wordling}\n"; + push (@newcompletemailmsg, $newline); + } + } + + sub cmd_ok { + # write out "command successful" message to email file + $newline = "Spamscan command completed succesfully.\n\n$spamcmd\n\n"; + push (@newcompletemailmsg, $newline); + @completemailmsg = @newcompletemailmsg; + } + + sub cmd_unknown { + $xheaderdone = 0; + foreach $line (@completemailmsg) { + if (($xheaderdone == 0) && ($line =~ /\A\r?\n\Z/)) { + $xheaderdone = 1; + push (@newcompletemailmsg, $line); + push (@newcompletemailmsg, $line); + next; + } + if ($xheaderdone == 0) { + push (@newcompletemailmsg, $line); + } + } + + + # write out "command unknown" message to email file + $newline = "Unknown Spamscan command:\n\n$spamcmd\n\n"; + push (@newcompletemailmsg, $newline); + @completemailmsg = @newcompletemailmsg; + } |