#!/usr/bin/perl

use Net::Kismet;
use DBI;

my $dbh = DBI->connect("DBI:mysql:database=wireless;host=127.0.0.1",
                         "USERNAME", "PASSWORD",
                         {'RaiseError' => 1});

$k = new Net::Kismet('localhost', 2501);
$k->register('SSID', \&ssidhandler, '*');
$k->register('BSSID', \&bssidhandler, '*');
$k->register('CLIENT', \&clienthandler, '*');
#$k->timer_register(\&timer, 5);
KismetRun($k);


sub ssidhandler {
   my $vars = shift;
   my $ssid = "<cloked>";
   if ($vars->{'ssid'} eq "") { 
     $ssid = "<CLOKED>";
   } else {
     $ssid = $vars->{'ssid'};
   }
   #print $vars->{'mac'}."\n";
   #print "lt: ".$vars->{'lasttime'}."\n";
   #print "INSERT INTO ssid VALUES ('". $ssid. "', '" . $vars->{'mac'} . "', " . $vars->{'lasttime'} . ")";
   #print "\n";
   my $sth = $dbh->prepare("SELECT LASTSEEN from ssid where SSID=? and MAC='" . $vars->{'mac'} . "'");
   $sth->execute($ssid);
   if ($sth->rows() > 0) {
     $sth = $dbh->prepare("UPDATE ssid set LASTSEEN=" . $vars->{'lasttime'} . " where SSID=? and MAC = '" . $vars->{'mac'} . "'");
   } else {
     $sth = $dbh->prepare("INSERT INTO ssid VALUES (?, '" . $vars->{'mac'} . "', " . $vars->{'lasttime'} . ")");
   }
   $sth->execute($ssid);
}

sub bssidhandler {
   my $vars = shift;
   #print $vars->{'bssid'}."\n";
   #print "lt: ".$vars->{'lasttime'}."\n";
   #print "INSERT INTO bssid VALUES ('". $vars->{'bssid'}. "', " . $vars->{'lasttime'} . ")";
   my $sth = $dbh->prepare("SELECT LASTSEEN from bssid where BSSID='" . $vars->{'bssid'} . "'");
   $sth->execute();
   if ($sth->rows() > 0) {
     $dbh->do("UPDATE bssid set LASTSEEN=" . $vars->{'lasttime'} . " where BSSID = '" . $vars->{'bssid'} . "'");
   } else {
     $dbh->do("INSERT INTO bssid VALUES ('". $vars->{'bssid'}. "', " . $vars->{'lasttime'} . ")");
   }
   #$dbh->do("INSERT INTO bssid VALUES ('". $vars->{'bssid'}. "', " . $vars->{'lasttime'} . ")");
}

sub clienthandler {
   my $vars = shift;
   #print $vars->{'bssid'}."\n";
   #print $vars->{'mac'}."\n";
   #print "lt: ".$vars->{'lasttime'}."\n";
   my $sth = $dbh->prepare("SELECT LASTSEEN from clients where BSSID='". $vars->{'bssid'}. "' and MAC='" . $vars->{'mac'} . "'");
   $sth->execute();
   if ($sth->rows() > 0) {
     $dbh->do("UPDATE clients set LASTSEEN=" . $vars->{'lasttime'} . " where BSSID = '". $vars->{'bssid'}. "' and MAC = '" . $vars->{'mac'} . "'");
   } else {
     $dbh->do("INSERT INTO clients VALUES ('". $vars->{'bssid'}. "', '" . $vars->{'mac'} . "', " . $vars->{'lasttime'} . ")");
     #$dbh->do("INSERT INTO clients VALUES ('". $ssid. "', '" . $vars->{'mac'} . "', " . $vars->{'lasttime'} . ")");
   }

   #print "INSERT INTO clients VALUES ('". $vars->{'bssid'}. "', '" . $vars->{'mac'} . "', " . $vars->{'lasttime'} . ")";
   #print "\n";
   #$dbh->do("INSERT INTO clients VALUES ('". $vars->{'bssid'}. "', '" . $vars->{'mac'} . "', " . $vars->{'lasttime'} . ")");
}



sub timer {
   print "<<TIMER CALLBACK CALLED>>\n";
	# check for lame threads and kill if hung
	my $rin = "";
	vec ( $rin, fileno ("PREAD00"), 1 ) = 1;
	#while (1) {
		if ( $nfound = select($rout=$rin, undef, undef, 0) ) {
			$line = <PREAD00>;
			#print "TIMER: $line";
		}
	#}
}

sub print_hash {
    my $href = shift;
    while( my( $key, $val ) = each %{$href} ) {
        print "$key\t=>$val\n";
    }
}
