#
# Message-body checksum module for despam
# Copyright 1997 by Leslie M. Barstow III
#
# Check the body of a message against known spam by computing the
# 16-bit CRC and bytecount (using cksum), and the md5sum
#
package Checksum;
$CKSUM="/usr/bin/cksum";
$MD5SUM="/usr/bin/md5sum";
#
# Pre-comp section - compute all checksums before comparing against list
#
sub Setup
 {
  local($line,$tmpfile);
  ($tmpfile) = @_;
if ($::debug)
 {
  print "Initializing Checksum.\n";
 }
  open(TMPF,"$tmpfile");
  open(BTMPF,">$tmpfile.Checksum");
  while(<TMPF>)
   {
    last if (/^\s/);
   }
  while (<TMPF>)
   {
    print BTMPF;
   }
  close(TMPF,BTMPF);
  open(CKF,"$CKSUM < $tmpfile.Checksum|");
  $ckf = <CKF>;
  $ckf =~ /(\S*)\s*(\S*)/;
  $b_line = "$1 $2";
  close(CKF);
  open(CKF,"$MD5SUM < $tmpfile.Checksum|");
  $ckf = <CKF>;
  $ckf =~ /(\S*)\s*-/;
  $b_line .= " $1";
  close(CKF);
  unlink "$tmpfile.Checksum";
  return $b_line;
 } 

#
# Comparison section
#
sub Compare
 {
  local($SPAMLIST);
  ($SPAMLIST)=@_;
  open(SPAMF,"$SPAMLIST");
  while(<SPAMF>)
   {
    next if (/^#/);
    ($type, $line) = /(\S*)\s*(.*)/;
    if ($type ne "Checksum")
     {
      close(SPAMF);
if ($::debug)
 {
      print STDERR "despam: Non-checksum line in $SPAMLIST.\n";
      print STDERR "        Please report this to your administrator.\n";
 }
      return -1;
     }
    if ($line eq $b_line)
     {
      close(SPAMF);
      return 1;
     }
   }
  close(SPAMF);
  return 0;
 }

#
# Description section (returns a short description)
#
sub Description
 {
  return "Uses CRC-16, bytecount, and MD5 to compare message body";
 }

1;
