]> ruin.nu Git - NDIRC.git/blob - Delling.pm
Use Moose and make the bots object-oriented with Bot as base class
[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 NDIRC::Misc;
30 use ND::DB;
31
32 sub irc_public {
33         my ($sender, $heap, $who, $where, $msg) = @_[SENDER, HEAP, ARG0 .. ARG2];
34         my ($nick,$username,$address) = ( split /[!@]/, $who );
35         my $channel = $where->[0];
36
37         my $irc = $sender->get_heap();
38
39         #$irc->yield(privmsg => $channel, "Authed? " . $irc->is_nick_authed($nick));
40         my $dbh = DB();
41         my $seen = $dbh->prepare_cached(q{UPDATE users SET laston = NOW() WHERE hostmask = ?});
42         $seen->execute($address);
43
44         if ($msg =~ /^(\S+): (.+)$/ && $heap->{disp}->has_command('anon',$channel)){
45                 my $_ = $1;
46                 my $text = $2;
47                 unless ($irc->is_channel_member($channel,$1) || /(Constructing|Researching)/){
48                         $msg = ".anon $_ $text";
49                 }
50
51         }
52         if (parseCommand($msg,$irc,$nick,$address,$channel,$heap->{disp},$dbh)){
53                 #Command parsed and run successfully
54         }
55 }
56
57
58 sub irc_msg {
59         my ($sender, $heap, $who, $where, $msg) = @_[SENDER, HEAP, ARG0 .. ARG2];
60         my ($nick,$username,$address) = ( split /[!@]/, $who );
61         my $irc = $sender->get_heap();
62
63         my $dbh = DB();
64         my $seen = $dbh->prepare_cached(q{UPDATE users SET laston = NOW() WHERE hostmask = ?});
65         $seen->execute($address);
66
67         if (parseCommand($msg,$irc,$nick,$address,'pm',$heap->{disp},$dbh)){
68                 #Command parsed and run successfully
69         }else{
70                 $irc->yield(notice => $nick, "unknown command");
71         }
72 }
73
74 sub irc_join {
75         my ($sender, $heap, $who, $channel) = @_[SENDER, HEAP, ARG0 .. ARG1];
76         my ($nick,$username,$address) = ( split /[!@]/, $who );
77         my $irc = $sender->get_heap();
78
79         my $dbh = DB();
80         my $seen = $dbh->prepare_cached(q{UPDATE users SET laston = NOW() WHERE hostmask = ?});
81         $seen->execute($address);
82
83         if($heap->{disp}->has_command('voice',$channel)){
84                 my $flags = $dbh->prepare_cached(q{
85 SELECT DISTINCT flag
86 FROM users u
87         JOIN groupmembers g USING (uid)
88         JOIN channel_group_flags gf USING (gid)
89 WHERE u.hostmask = $1 AND channel = $2 AND flag IN ('o','v');
90                 });
91                 $flags->execute($address, $channel);
92                 my $mode = '';
93                 my @who;
94                 while (my ($flag) = $flags->fetchrow()){
95                         $mode .= $flag;
96                         push @who, $nick;
97                 }
98                 say "$mode - @who";
99                 $irc->yield(mode => $channel, $mode, @who) if $mode;
100         }
101 }
102
103 sub refresh {
104         my ($kernel,$heap) = @_[KERNEL,HEAP];
105         $kernel->delay( refresh => 60 );
106         print 'Time: ' . time() . ' Lag: ' . $heap->{connector}->lag() . "\n";
107
108         my $dbh = DB();
109         my $scans = $dbh->prepare(q{SELECT s.scan_id
110                         ,array_agg(sr.nick) AS nick
111                         ,array_agg(sr.id) AS id
112                 FROM scan_requests sr
113                         JOIN scans s USING (pid,type)
114                 WHERE sr.time > NOW() - '30 min'::INTERVAL
115                         AND s.tick >= sr.tick AND NOT sr.sent
116                 GROUP BY scan_id
117                 });
118         my $sentscan = $dbh->prepare(q{UPDATE scan_requests
119                 SET sent = TRUE WHERE id = ANY($1)
120                 });
121         $scans->execute;
122         while (my $scan = $scans->fetchrow_hashref){
123                 $heap->{irc}->yield(notice => $scan->{nick}, "http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}");
124                 $sentscan->execute($scan->{id});
125         }
126         return;
127 }
128
129 1;