]> ruin.nu Git - NDIRC.git/blob - Delling.pm
Add megahal support to delling
[NDIRC.git] / Delling.pm
1 #**************************************************************************
2 #   Copyright (C) 2009 by Michael Andreen <harvATruinDOTnu>               *
3 #                                                                         *
4 #   This program is free software; you can redistribute it and/or modify  *
5 #   it under the terms of the GNU General Public License as published by  *
6 #   the Free Software Foundation; either version 2 of the License, or     *
7 #   (at your option) any later version.                                   *
8 #                                                                         *
9 #   This program is distributed in the hope that it will be useful,       *
10 #   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12 #   GNU General Public License for more details.                          *
13 #                                                                         *
14 #   You should have received a copy of the GNU General Public License     *
15 #   along with this program; if not, write to the                         *
16 #   Free Software Foundation, Inc.,                                       *
17 #   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
18 #**************************************************************************/
19 package NDIRC::Delling;
20
21 use strict;
22 use warnings;
23 use feature ':5.10';
24
25 use Moose;
26 extends 'NDIRC::Bot';
27
28 use POE::Session;
29 use ND::DB;
30
31 use AI::MegaHAL;
32
33 has hal => (
34         is => 'ro',
35         isa => 'Object',
36         lazy_build => 1,
37 );
38
39 sub irc_public {
40         my ($self,$sender, $who, $where, $msg) = @_[OBJECT,SENDER, ARG0 .. ARG2];
41         my ($nick,$username,$address) = ( split /[!@]/, $who );
42         my $channel = $where->[0];
43
44         my $irc = $sender->get_heap();
45
46         #$irc->yield(privmsg => $channel, "Authed? " . $irc->is_nick_authed($nick));
47         my $dbh = DB();
48         my $seen = $dbh->prepare_cached(q{UPDATE users SET laston = NOW() WHERE hostmask = ?});
49         $seen->execute($address);
50
51         my $nickname = $irc->nick_name;
52         if ($msg =~ /^$nickname: (.*)/i){
53                 my $text = $self->hal->do_reply($1);
54                 $irc->yield(privmsg => $channel, "$nick: $text");
55         }elsif ($msg =~ /^(\S+): (.+)$/ && $self->disp->has_command('anon',$channel)){
56                 my $_ = $1;
57                 my $text = $2;
58                 unless ($irc->is_channel_member($channel,$1) || /(Constructing|Researching)/){
59                         $msg = ".anon $_ $text";
60                 }
61
62         }
63         if ($self->parseCommand($msg,$irc,$nick,$address,$channel,$dbh)){
64                 #Command parsed and run successfully
65         }
66 }
67
68 sub irc_msg {
69         my ($self,$sender, $who, $where, $msg) = @_[OBJECT,SENDER, ARG0 .. ARG2];
70         my ($nick,$username,$address) = ( split /[!@]/, $who );
71         my $irc = $sender->get_heap();
72
73         my $dbh = DB();
74         my $seen = $dbh->prepare_cached(q{UPDATE users SET laston = NOW() WHERE hostmask = ?});
75         $seen->execute($address);
76
77         if ($self->parseCommand($msg,$irc,$nick,$address,'pm',$dbh)){
78                 #Command parsed and run successfully
79         }else{
80                 $irc->yield(notice => $nick, "unknown command");
81         }
82 }
83
84 sub irc_join {
85         my ($self,$sender, $who, $channel) = @_[OBJECT,SENDER, ARG0 .. ARG1];
86         my ($nick,$username,$address) = ( split /[!@]/, $who );
87         my $irc = $sender->get_heap();
88
89         my $dbh = DB();
90         my $seen = $dbh->prepare_cached(q{UPDATE users SET laston = NOW() WHERE hostmask = ?});
91         $seen->execute($address);
92
93         if($self->disp->has_command('voice',$channel)){
94                 my $flags = $dbh->prepare_cached(q{
95 SELECT DISTINCT flag
96 FROM users u
97         JOIN groupmembers g USING (uid)
98         JOIN channel_group_flags gf USING (gid)
99 WHERE u.hostmask = $1 AND channel = $2 AND flag IN ('o','v');
100                 });
101                 $flags->execute($address, $channel);
102                 my $mode = '';
103                 my @who;
104                 while (my ($flag) = $flags->fetchrow()){
105                         $mode .= $flag;
106                         push @who, $nick;
107                 }
108                 say "$mode - @who";
109                 $irc->yield(mode => $channel, $mode, @who) if $mode;
110         }
111 }
112
113 sub refresh {
114         my ($kernel,$heap) = @_[KERNEL,HEAP];
115         $kernel->delay( refresh => 60 );
116         print 'Time: ' . time() . ' Lag: ' . $heap->{connector}->lag() . "\n";
117
118         my $dbh = DB();
119         my $scans = $dbh->prepare(q{SELECT s.scan_id
120                         ,array_agg(sr.nick) AS nick
121                         ,array_agg(sr.id) AS id
122                 FROM scan_requests sr
123                         JOIN scans s USING (pid,type)
124                 WHERE sr.time > NOW() - '30 min'::INTERVAL
125                         AND s.tick >= sr.tick AND NOT sr.sent
126                 GROUP BY scan_id
127                 });
128         my $sentscan = $dbh->prepare(q{UPDATE scan_requests
129                 SET sent = TRUE WHERE id = ANY($1)
130                 });
131         $scans->execute;
132         while (my $scan = $scans->fetchrow_hashref){
133                 $heap->{irc}->yield(notice => $scan->{nick}, "http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}");
134                 $sentscan->execute($scan->{id});
135         }
136         return;
137 }
138
139 sub _build_hal {
140         my $hal = AI::MegaHAL->new('Path' => '.', 'Banner' => 0, 'Prompt' => 0, 'Wrap' => 0, 'AutoSave' => 1);
141         return $hal;
142 }
143
144 after sig_usr2 => sub {
145         my $self = shift;
146         say 'Saving brain!';
147         $self->hal->_cleanup;
148 };
149
150 1;