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