]> ruin.nu Git - NDIRC.git/blobdiff - Delling.pm
Add megahal support to delling
[NDIRC.git] / Delling.pm
index 26e97842a453872cc5e18fe00a054c09b3ddcfd9..224a6572650bfccd2109e34016d87a8d96ece066 100644 (file)
@@ -1,5 +1,5 @@
 #**************************************************************************
-#   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
+#   Copyright (C) 2009 by Michael Andreen <harvATruinDOTnu>               *
 #                                                                         *
 #   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  *
@@ -22,23 +22,37 @@ use strict;
 use warnings;
 use feature ':5.10';
 
+use Moose;
+extends 'NDIRC::Bot';
+
 use POE::Session;
-use NDIRC::Misc;
 use ND::DB;
 
-my $DBH = DB();
-my $TICK = $DBH->selectrow_array('SELECT tick()');
+use AI::MegaHAL;
+
+has hal => (
+       is => 'ro',
+       isa => 'Object',
+       lazy_build => 1,
+);
 
 sub irc_public {
-       my ($sender, $heap, $who, $where, $msg) = @_[SENDER, HEAP, ARG0 .. ARG2];
+       my ($self,$sender, $who, $where, $msg) = @_[OBJECT,SENDER, ARG0 .. ARG2];
        my ($nick,$username,$address) = ( split /[!@]/, $who );
        my $channel = $where->[0];
 
        my $irc = $sender->get_heap();
 
        #$irc->yield(privmsg => $channel, "Authed? " . $irc->is_nick_authed($nick));
+       my $dbh = DB();
+       my $seen = $dbh->prepare_cached(q{UPDATE users SET laston = NOW() WHERE hostmask = ?});
+       $seen->execute($address);
 
-       if ($msg =~ /^(\S+): (.+)$/ && $heap->{disp}->has_command('anon',$channel)){
+       my $nickname = $irc->nick_name;
+       if ($msg =~ /^$nickname: (.*)/i){
+               my $text = $self->hal->do_reply($1);
+               $irc->yield(privmsg => $channel, "$nick: $text");
+       }elsif ($msg =~ /^(\S+): (.+)$/ && $self->disp->has_command('anon',$channel)){
                my $_ = $1;
                my $text = $2;
                unless ($irc->is_channel_member($channel,$1) || /(Constructing|Researching)/){
@@ -46,29 +60,37 @@ sub irc_public {
                }
 
        }
-       if (parseCommand($msg,$irc,$nick,$address,$channel,$heap->{disp},DB())){
+       if ($self->parseCommand($msg,$irc,$nick,$address,$channel,$dbh)){
                #Command parsed and run successfully
        }
 }
 
-
 sub irc_msg {
-       my ($sender, $heap, $who, $where, $msg) = @_[SENDER, HEAP, ARG0 .. ARG2];
+       my ($self,$sender, $who, $where, $msg) = @_[OBJECT,SENDER, ARG0 .. ARG2];
        my ($nick,$username,$address) = ( split /[!@]/, $who );
        my $irc = $sender->get_heap();
 
-       if (parseCommand($msg,$irc,$nick,$address,'pm',$heap->{disp},DB())){
+       my $dbh = DB();
+       my $seen = $dbh->prepare_cached(q{UPDATE users SET laston = NOW() WHERE hostmask = ?});
+       $seen->execute($address);
+
+       if ($self->parseCommand($msg,$irc,$nick,$address,'pm',$dbh)){
                #Command parsed and run successfully
+       }else{
+               $irc->yield(notice => $nick, "unknown command");
        }
 }
 
 sub irc_join {
-       my ($sender, $heap, $who, $channel) = @_[SENDER, HEAP, ARG0 .. ARG1];
+       my ($self,$sender, $who, $channel) = @_[OBJECT,SENDER, ARG0 .. ARG1];
        my ($nick,$username,$address) = ( split /[!@]/, $who );
        my $irc = $sender->get_heap();
 
-       if($heap->{disp}->has_command('voice',$channel)){
-               my $dbh = DB();
+       my $dbh = DB();
+       my $seen = $dbh->prepare_cached(q{UPDATE users SET laston = NOW() WHERE hostmask = ?});
+       $seen->execute($address);
+
+       if($self->disp->has_command('voice',$channel)){
                my $flags = $dbh->prepare_cached(q{
 SELECT DISTINCT flag
 FROM users u
@@ -90,9 +112,39 @@ WHERE u.hostmask = $1 AND channel = $2 AND flag IN ('o','v');
 
 sub refresh {
        my ($kernel,$heap) = @_[KERNEL,HEAP];
-       print 'Time: ' . time() . ' Lag: ' . $heap->{connector}->lag() . "\n";
        $kernel->delay( refresh => 60 );
+       print 'Time: ' . time() . ' Lag: ' . $heap->{connector}->lag() . "\n";
+
+       my $dbh = DB();
+       my $scans = $dbh->prepare(q{SELECT s.scan_id
+                       ,array_agg(sr.nick) AS nick
+                       ,array_agg(sr.id) AS id
+               FROM scan_requests sr
+                       JOIN scans s USING (pid,type)
+               WHERE sr.time > NOW() - '30 min'::INTERVAL
+                       AND s.tick >= sr.tick AND NOT sr.sent
+               GROUP BY scan_id
+               });
+       my $sentscan = $dbh->prepare(q{UPDATE scan_requests
+               SET sent = TRUE WHERE id = ANY($1)
+               });
+       $scans->execute;
+       while (my $scan = $scans->fetchrow_hashref){
+               $heap->{irc}->yield(notice => $scan->{nick}, "http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}");
+               $sentscan->execute($scan->{id});
+       }
        return;
 }
 
+sub _build_hal {
+       my $hal = AI::MegaHAL->new('Path' => '.', 'Banner' => 0, 'Prompt' => 0, 'Wrap' => 0, 'AutoSave' => 1);
+       return $hal;
+}
+
+after sig_usr2 => sub {
+       my $self = shift;
+       say 'Saving brain!';
+       $self->hal->_cleanup;
+};
+
 1;