#!/usr/bin/perl -w
########################################################################
#
## $Id: npp_active,v 0.1 2004/09/16 15:21:45 paul $
#
#  This file runs in a crontab to check if there is (or
#  has been) a spammer caught in the TarPOP tarpit.
#
# Copyright (C) 2004 Paul Grosse
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA  02111-1307  USA.
#
########################################################################
use Tk;
# look to see if this program is already running
&detectmutex(0);
# find out if our program is running at all
$procrunning = readpipe "ps -e | grep nopop";
# if it is, there will be some length to the result of the 'ps'
if (length $procrunning){
  # the nopop tarpit program is running somewhere
  # this is the first time so we are going to draw the window
  my $mw = MainWindow->new;
  $mw-> title("POP3 TarPit");
  $mt = substr(localtime, 4, -5);
  $tt = "\n   Spammer trapped in POP3 TarPit!   \n$mt";
  $text1 = $mw->Label(-text => $tt);
  $text1->pack;
  $button1 = $mw->Button(-text => "Ok", -command =>sub{&delmutex});
  $button1->pack;
  $text2 = $mw->Label(-text => " ");
  $text2->pack;
  &writemutex(0);
  MainLoop;
} else {
  exit(0);
}

############### subroutines ##################

sub writemutex {
  open(NEM, ">/var/log/pophp/nemutex");
    print NEM '1';
  close(NEM);
}

sub delmutex {
  open(NEM, ">/var/log/pophp/nemutex");
    print NEM '0';
  close(NEM);
  exit(0);
}

sub detectmutex {
  open(NEM, "/var/log/pophp/nemutex");
    if(eof(NEM)){
      #file never been written to before
      $mutex=0
    }else{
      # input the file contents to $mutex
      read NEM, $mutex, 1
    }
  close(NEM);
  if($mutex){
    # already detected but not acknowleded.
    exit(0)
  }
}
