#!/usr/bin/perl

# host -t txt 2.3.3.updates.spamassassin.org

use strict;
use warnings;
use Net::DNS;
use POSIX qw(strftime);
use LWP::Simple;

############################### Checking updates

my $updatelog = '/home/darxus/progs/sa/updatever.txt';
my @versions = ('3.3.0','3.3.1','3.3.2');
my $debug = 0;

my $founderror = 0;
my $report = '';

print "Checking DNS records.\n";

our $res = Net::DNS::Resolver->new;
my $answer = '';
my %vers = ();
my $currentdate = strftime "%F", localtime;

for my $saver (@versions) {
  my $dnsver = join('.',reverse(split(/\./,$saver)));
  $vers{$saver} = dns("$dnsver.updates.spamassassin.org.",'txt');
  print "Latest update for SA $saver: $vers{$saver}, $currentdate\n" if $debug;
}

my %oldmatch = ();
if (-e $updatelog) {
  open IN, "<$updatelog" or die "Couldn't read $updatelog: $!";
  while (my $line = <IN>) {
    chomp $line;
    my ($date, $saver, $updatever) = split(' ', $line);
    print "date: $date, SA version: $saver, update version: $updatever\n" if $debug;
    if ($vers{$saver} eq $updatever and !exists $oldmatch{$saver}) {
      $oldmatch{$saver} = $date;
    }
  }
  close IN;
} else {
  print "No record of previous versions, creating new.\n";
}

my $body = '';
if (scalar keys %oldmatch > 0) {
  for my $saver (sort keys %oldmatch) {
    print "SpamAssassin version $saver has not had a rule update since $oldmatch{$saver}.\n" if $debug;
    $body .= "SpamAssassin version $saver has not had a rule update since $oldmatch{$saver}.\n";
    $founderror = 1;
  }
}

open OUT, ">>$updatelog" or die "Couldn't write to $updatelog: $!";
for my $version (sort keys %vers) {
  print OUT "$currentdate $version $vers{$version}\n";
}
close OUT;

print "Done with DNS.\n";

############################### Checking thresholds

$body .= "\n" unless ($body eq '');


my $baseurl = 'http://ruleqa.spamassassin.org/?daterev=';

my $datestamp = `date -d'yesterday' +%Y%m%d`;
chomp $datestamp;
my $dow = `date -d'yesterday' +%A`;
chomp $dow;

#print "datestamp: $datestamp, $dow\n";

my @datestamps;
push @datestamps, $datestamp;

#if ($dow ne 'Saturday') {
#  $datestamp = `date +%Y%m%d -d"last saturday"`;
#  chomp $datestamp;
#  push @datestamps, $datestamp;
#}

our $url = '';
for my $datestamp (@datestamps) {
  $url = $baseurl . $datestamp;
  print "Retrieving url: $url\n";
  my $content = get($url);
  print "Retrieved.\n";
  my $hamcount = 'not_found';
  my $spamcount = 'not_found';
  for my $line (split "\n", $content) {
    if ($line =~ m#\(all messages\)#) {
      ($spamcount,$hamcount) = (split(' ', $line))[1,2];
    }
  }
  print "spamcount / hamcount: $spamcount / $hamcount\n";
  if ($spamcount eq 'not_found' or $hamcount eq 'not-found') {
    $founderror = 1;
    $body .= "$datestamp:  Could not find the ham / spam counts, probably an http error:  $url\n";
  } elsif ($spamcount < 150000 or $hamcount < 150000) {
    $founderror = 1;
    $body .= "$datestamp:  Spam or ham is below threshold of 150,000:  $url\n";
  } else {
    $body .= "$datestamp:  Spam and ham are above threshold of 150,000:  $url\n";
  }
  $body .= "$datestamp:  Spam: $spamcount, Ham: $hamcount\n";
}


############################### Reporting

print $body;

if ($founderror) {

  $body .= "\n\nThe spam and ham counts on which this script alerts are from\n$url\nClick \"(source details)\" (it's tiny and low contrast).\nIt's from the second and third columns of the line that ends with\n\"(all messages)\"\n\nThe source to this script is\nhttp://www.chaosreigns.com/sa/update-version-mon.pl\n\nIt looks like both the weekly and nightly masschecks need to have sufficient\ncorpora in order for an update to be generated.\n";

#open OUT, "| /usr/bin/mail -s 'Rule updates are too old - $currentdate' darxus\@chaosreigns.com,dev\@spamassassin.apache.org,ruleqa\@spamassassin.apache.org";
  open OUT, "| /usr/bin/mail -s 'Rule updates are too old - $currentdate' darxus\@chaosreigns.com,dev\@spamassassin.apache.org";
#  open OUT, "| /usr/bin/mail -s 'Rule updates are too old - $currentdate' darxus\@chaosreigns.com";
  print OUT $body;
#  print OUT "\nLog of update versions:\n";
#  open IN, "<$updatelog";
#  while (my $line = <IN>) {
#    print OUT $line;
#  }
#  close IN;
  close OUT;
} else {
  print "No problems.\n";
}

exit; ######################################################################

sub dns {
  my ($name, $class) = @_;
  my $answer = $res->query($name, $class);
  my @answers = $answer->answer;
  my @values = ();
  for my $txtdata (@answers) {
    push @values, $txtdata->txtdata;
  }
  return $values[0];
}
