]> ruin.nu Git - NDIRC.git/blob - Delling.pm
Only allow chattr in certain channels
[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 my ($tick,$stattick) = (0,0);
32 my $last_announcement = 0;
33
34 after irc_public => sub {
35         my ($self,$sender, $who, $where, $msg) = @_[OBJECT,SENDER, ARG0 .. ARG2];
36         my ($nick,$username,$address) = ( split /[!@]/, $who );
37         my $channel = $where->[0];
38
39         my $irc = $sender->get_heap();
40
41         #$irc->yield(privmsg => $channel, "Authed? " . $irc->is_nick_authed($nick));
42         my $dbh = DB();
43         my $seen = $dbh->prepare_cached(q{UPDATE users SET laston = NOW() WHERE hostmask = ?});
44         $seen->execute($address);
45
46         my $nickname = $irc->nick_name;
47         if ($msg =~ /^(\S+): (.+)$/ && $self->disp->has_command('anon',$channel)){
48                 my $_ = $1;
49                 my $text = $2;
50                 unless ($irc->is_channel_member($channel,$1) || /(Constructing|Researching)/){
51                         $msg = ".anon $_ $text";
52                         $self->parseCommand($msg,$irc,$nick,$address,$channel,$dbh);
53                 }
54         }elsif ($self->parseCommand($msg,$irc,$nick,$address,$channel,$dbh)){
55                 #Command parsed and run successfully
56         }
57 };
58
59 after irc_msg => sub {
60         my ($self,$sender, $who, $where, $msg) = @_[OBJECT,SENDER, ARG0 .. ARG2];
61         my ($nick,$username,$address) = ( split /[!@]/, $who );
62         my $irc = $sender->get_heap();
63
64         my $dbh = DB();
65         my $seen = $dbh->prepare_cached(q{UPDATE users SET laston = NOW() WHERE hostmask = ?});
66         $seen->execute($address);
67
68         if ($self->parseCommand($msg,$irc,$nick,$address,'pm',$dbh)){
69                 #Command parsed and run successfully
70         }else{
71                 $irc->yield(notice => $nick, "unknown command");
72         }
73 };
74
75 sub irc_join {
76         my ($self,$sender, $who, $channel) = @_[OBJECT,SENDER, ARG0 .. ARG1];
77         my ($nick,$username,$address) = ( split /[!@]/, $who );
78         my $irc = $sender->get_heap();
79         my $disp = $self->disp;
80
81         my $dbh = DB();
82         my $seen = $dbh->prepare_cached(q{UPDATE users SET laston = NOW() WHERE hostmask = ?});
83         $seen->execute($address);
84
85         if($self->disp->has_command('voice',$channel)){
86                 my $flags = $dbh->prepare_cached(q{
87 SELECT DISTINCT flag
88 FROM users u
89         JOIN groupmembers g USING (uid)
90         JOIN channel_group_flags gf USING (gid)
91 WHERE u.hostmask = $1 AND channel = $2 AND flag IN ('o','v');
92                 });
93                 $flags->execute($address, $channel);
94                 my $mode = '';
95                 my @who;
96                 while (my ($flag) = $flags->fetchrow()){
97                         $mode .= $flag;
98                         push @who, $nick;
99                 }
100                 say "$mode - @who";
101                 $irc->yield(mode => $channel, $mode, @who) if $mode;
102         }
103
104         if (lc $channel ~~ lc $disp->targets->{members}){
105                 if (time - $last_announcement < 1){
106                         $last_announcement = time;
107                         return;
108                 }
109                 my $user = $dbh->selectrow_hashref(q{
110 SELECT uid,pid,hostmask,password
111         ,(SELECT max(time) FROM forum_thread_visits WHERE uid = u.uid) AS last_forum_visit
112 FROM users u WHERE hostmask = ?
113                 }, undef, $address);
114                 if ($user){
115                         if ($user->{password} eq ''){
116                                 $irc->yield(privmsg => $disp->targets->{members}, "$nick: Get a new random password with /msg delling !getpass . If you don't know your username, then you can get it with .points");
117                         }
118                         if ($tick > 12 && not defined $user->{pid}){
119                                 $irc->yield(privmsg => $disp->targets->{members}, "$nick: go to https://nd.ruin.nu/ and enter your coords.");
120                         }
121
122                         if (not defined $user->{last_forum_visit}){
123                                 $irc->yield(privmsg => $disp->targets->{members}, "$nick: Go read the forum! https://nd.ruin.nu/forum");
124                         }else {
125                                 my $unread = $dbh->selectrow_hashref(q{SELECT * FROM unread_posts($1)},undef,$user->{uid});
126                                 if ($unread && $unread->{new}){
127                                         $irc->yield(notice => $nick, "$unread->{new} posts since your last forum visit ($unread->{unread} unread posts in total) https://nd.ruin.nu/forum/allUnread");
128                                 }
129                         }
130                 }
131                 if (time - $last_announcement < 2){
132                         $last_announcement = time;
133                         return;
134                 }
135                 $last_announcement = time;
136         }
137 }
138
139 sub refresh {
140         my ($self,$kernel,$heap) = @_[OBJECT,KERNEL,HEAP];
141         $kernel->delay( refresh => 60 );
142         print 'Time: ' . time() . ' Lag: ' . $heap->{connector}->lag() . "\n";
143
144         my $irc = $heap->{irc};
145         my $disp = $self->disp;
146         my $dbh = DB();
147         my $scans = $dbh->prepare(q{SELECT s.scan_id
148                         ,coords(x,y,z),type
149                         ,array_agg(sr.nick) AS nick
150                         ,array_agg(sr.id) AS id
151                 FROM scan_requests sr
152                         JOIN scans s USING (pid,type)
153                         JOIN current_planet_stats USING (pid)
154                 WHERE sr.time > NOW() - '30 min'::INTERVAL
155                         AND s.tick >= sr.tick AND NOT sr.sent
156                 GROUP BY scan_id,x,y,z,type
157                 });
158         my $sentscan = $dbh->prepare(q{UPDATE scan_requests
159                 SET sent = TRUE WHERE id = ANY($1)
160                 });
161         $scans->execute;
162         while (my $scan = $scans->fetchrow_hashref){
163                 $heap->{irc}->yield(notice => $scan->{nick}, "($scan->{coords} $scan->{type})"
164                         ." http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}");
165                 $sentscan->execute($scan->{id});
166         }
167
168         my @row = $dbh->selectrow_array(q{SELECT tick(), max(tick) FROM planet_stats});
169         if ($tick != $row[0]){
170                 $tick = $row[0];
171                 $irc->yield(privmsg => $disp->targets->{def}, "New tick: $tick");
172         }
173         if (defined $row[1] && $stattick != $row[1]){
174                 $stattick = $row[1];
175                 $irc->yield(privmsg => $disp->targets->{members}, "New tick: $stattick");
176         }
177         my $ircreqs = $dbh->prepare(q{SELECT id,username,message,channel FROM irc_requests NATURAL JOIN users WHERE not sent});
178         my $upircreq = $dbh->prepare(q{UPDATE irc_requests SET sent = TRUE WHERE id = ?});
179         $ircreqs->execute;
180         while (my $req = $ircreqs->fetchrow_hashref){
181                 if ($req->{channel} eq 'def'){
182                         $irc->yield(privmsg => $disp->targets->{def}, chr(3)."04 ## $req->{username} via webbie ## >> $req->{message}");
183                         $self->parseCommand("~report_incs $req->{message}",$irc,$req->{username},' BATCH ',$disp->targets->{def},$dbh);
184                 }elsif(exists $disp->targets->{$req->{channel}}){
185                         $irc->yield(privmsg => $disp->targets->{$req->{channel}}, "<$req->{username} via webbie> $req->{message}");
186                 }
187                 $upircreq->execute($req->{id});
188         }
189         my $defmissions = $dbh->prepare(q{
190 SELECT username,call,tick,dm.fleet,p.value
191         ,100 * SUM(fs.amount * (metal + crystal + eonium) / 100) / p.value AS value
192 FROM users u
193         JOIN current_planet_stats p USING (pid)
194         JOIN launch_confirmations USING (uid)
195         JOIN fleets f USING (fid)
196         JOIN fleet_ships fs USING (fid)
197         JOIN defense_missions dm ON f.fid = dm.fleet
198         JOIN ship_stats ss USING (ship)
199 WHERE NOT dm.announced
200 GROUP BY username,call,tick,dm.fleet,p.value
201                 });
202         my $updefmis = $dbh->prepare(q{UPDATE defense_missions SET announced = TRUE WHERE fleet = ?});
203         $defmissions->execute();
204         while (my $mission = $defmissions->fetchrow_hashref){
205                 $irc->yield(privmsg => $disp->targets->{def}, chr(3)."06 $mission->{username} sent def to call $mission->{call}, $mission->{value}% of value (tick $mission->{tick}) https://nd.ruin.nu/calls/edit/$mission->{call}");
206                 $updefmis->execute($mission->{fleet});
207         }
208
209 }
210
211 after sig_usr2 => sub {
212         my $self = shift;
213 };
214
215 after _start => sub {
216  ($tick,$stattick) = DB()->selectrow_array(q{SELECT tick(),max(tick) FROM planet_stats});
217 };
218
219 1;