1 #**************************************************************************
2 # Copyright (C) 2009 by Michael Andreen <harvATruinDOTnu> *
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. *
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. *
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;
31 use NDIRC::DiscordContext;
33 my ($tick,$stattick) = (0,0);
34 my $last_announcement = 0;
36 after irc_public => sub {
37 my ($self,$sender, $who, $where, $msg) = @_[OBJECT,SENDER, ARG0 .. ARG2];
38 my ($nick,$username,$address) = ( split /[!@]/, $who );
39 my $channel = $where->[0];
41 my $irc = $sender->get_heap();
43 #$irc->yield(privmsg => $channel, "Authed? " . $irc->is_nick_authed($nick));
45 my $seen = $dbh->prepare_cached(q{UPDATE users SET laston = NOW() WHERE hostmask = ?});
46 $seen->execute($address);
48 my $nickname = $irc->nick_name;
49 if ($msg =~ /^(\S+): (.+)$/ && $self->disp->has_command('anon',$channel)){
52 unless ($irc->is_channel_member($channel,$1) || /(Constructing|Researching)/){
53 $msg = ".anon $_ $text";
54 $self->parseCommand($msg,$irc,$nick,$address,$channel,$dbh);
56 }elsif ($self->parseCommand($msg,$irc,$nick,$address,$channel,$dbh)){
57 #Command parsed and run successfully
61 after irc_msg => sub {
62 my ($self,$sender, $who, $where, $msg) = @_[OBJECT,SENDER, ARG0 .. ARG2];
63 my ($nick,$username,$address) = ( split /[!@]/, $who );
64 my $irc = $sender->get_heap();
67 my $seen = $dbh->prepare_cached(q{UPDATE users SET laston = NOW() WHERE hostmask = ?});
68 $seen->execute($address);
70 if ($self->parseCommand($msg,$irc,$nick,$address,'pm',$dbh)){
71 #Command parsed and run successfully
73 $irc->yield(notice => $nick, "unknown command");
78 my ($self,$sender, $who, $channel) = @_[OBJECT,SENDER, ARG0 .. ARG1];
79 my ($nick,$username,$address) = ( split /[!@]/, $who );
80 my $irc = $sender->get_heap();
81 my $disp = $self->disp;
84 my $seen = $dbh->prepare_cached(q{UPDATE users SET laston = NOW() WHERE hostmask = ?});
85 $seen->execute($address);
87 if($self->disp->has_command('voice',$channel)){
88 my $flags = $dbh->prepare_cached(q{
91 JOIN groupmembers g USING (uid)
92 JOIN channel_group_flags gf USING (gid)
93 WHERE u.hostmask = $1 AND channel = $2 AND flag IN ('o','v');
95 $flags->execute($address, $channel);
98 while (my ($flag) = $flags->fetchrow()){
103 $irc->yield(mode => $channel, $mode, @who) if $mode;
106 if (lc $channel ~~ @{$self->targets->{members}}){
107 if (time - $last_announcement < 1){
108 $last_announcement = time;
111 my $user = $dbh->selectrow_hashref(q{
112 SELECT uid,pid,hostmask,password
113 ,(SELECT max(time) FROM forum_thread_visits WHERE uid = u.uid) AS last_forum_visit
114 FROM users u WHERE hostmask = ?
117 unless ($user->{password}) {
118 $irc->yield(privmsg => $channel, "$nick: Get a new random password with /msg delling !getpass . If you don't know your username, then you can get it with .points");
121 if (time - $last_announcement < 2){
122 $last_announcement = time;
125 $last_announcement = time;
130 my ($self,$kernel,$heap) = @_[OBJECT,KERNEL,HEAP];
131 $kernel->delay( refresh => 60 );
132 print 'Time: ' . time() . ' Lag: ' . $heap->{connector}->lag() . "\n";
134 my $irc = $heap->{irc};
135 my $disp = $self->disp;
137 my $scans = $dbh->prepare(q{SELECT s.scan_id
139 ,array_agg(sr.nick) AS nick
140 ,array_agg(sr.id) AS id
141 FROM scan_requests sr
142 JOIN scans s USING (pid,type)
143 JOIN current_planet_stats USING (pid)
144 WHERE sr.time > NOW() - '30 min'::INTERVAL
145 AND s.tick >= sr.tick AND NOT sr.sent
146 GROUP BY scan_id,x,y,z,type
148 my $sentscan = $dbh->prepare(q{UPDATE scan_requests
149 SET sent = TRUE WHERE id = ANY($1)
153 while (my $scan = $scans->fetchrow_hashref){
154 $self->message("($scan->{coords} $scan->{type})"
155 ." http://game.planetarion.com/showscan.pl?scan_id=$scan->{scan_id}"
157 $sentscan->execute($scan->{id});
158 push @scanreqs, @{$scan->{id}};
161 if (scalar @scanreqs > 0)
163 my $scanreqs = join(', ', @scanreqs);
164 $self->toTarget(scan => "Handled the following requests: $scanreqs");
167 my @row = $dbh->selectrow_array(q{SELECT tick(), max(tick) FROM planet_stats});
168 if ($tick != $row[0]){
171 if (defined $row[1] && $stattick != $row[1]){
173 $self->toTarget(spam => "New tick: $stattick");
174 my $raids = $dbh->prepare(q{UPDATE raids SET open = true WHERE NOT open AND NOT removed AND open_tick <= $1 RETURNING id});
175 $raids->execute($stattick);
176 while (my $raid = $raids->fetchrow_hashref) {
177 $self->toTarget(members => "\@everyone Raid is now open: https://nd.ruin.nu/raids/view/$raid->{id}");
180 my $ircreqs = $dbh->prepare(q{SELECT id,username,message,channel FROM irc_requests NATURAL JOIN users WHERE not sent});
181 my $upircreq = $dbh->prepare(q{UPDATE irc_requests SET sent = TRUE WHERE id = ?});
183 while (my $req = $ircreqs->fetchrow_hashref){
184 if ($req->{channel} eq 'def'){
185 $self->toTarget(def => "> __**## $req->{username} via webbie ##**__ >> $req->{message}");
187 $self->toTarget($req->{channel} => "> <$req->{username} via webbie> $req->{message}");
189 $upircreq->execute($req->{id});
191 my $defmissions = $dbh->prepare(q{
192 SELECT username,call,tick,dm.fleet,p.value
193 ,100 * SUM(fs.amount * (metal + crystal + eonium) / 100) / p.value AS value
195 JOIN current_planet_stats p USING (pid)
196 JOIN launch_confirmations USING (uid)
197 JOIN fleets f USING (fid)
198 JOIN fleet_ships fs USING (fid)
199 JOIN defense_missions dm ON f.fid = dm.fleet
200 JOIN ship_stats ss USING (ship)
201 WHERE NOT dm.announced
202 GROUP BY username,call,tick,dm.fleet,p.value
204 my $updefmis = $dbh->prepare(q{UPDATE defense_missions SET announced = TRUE WHERE fleet = ?});
205 $defmissions->execute();
206 while (my $mission = $defmissions->fetchrow_hashref){
207 $self->toTarget(def => "<c06>$mission->{username} sent def</c> to call $mission->{call}, $mission->{value}% of value (tick $mission->{tick}) https://nd.ruin.nu/calls/edit/$mission->{call}");
208 $updefmis->execute($mission->{fleet});
213 after sig_usr2 => sub {
217 after _start => sub {
218 ($tick,$stattick) = DB()->selectrow_array(q{SELECT tick(),max(tick) FROM planet_stats});
221 after discord_message_create => sub {
222 my ($self, $gw, $hash) = @_;
224 my $author = $hash->{author};
225 my $msg = $hash->{content};
226 my $channel_id = $hash->{channel_id};
227 my $author_name = $author->{username}.'#'.$author->{discriminator};
228 my $author_id = $author->{id};
229 return if $author->{'id'} eq $self->discord_id; # Ignore my own messages
231 my $channel = "D-".$channel_id;
232 if (exists $self->discord_channels->{$channel_id}) {
233 $channel = 'dm' if ($self->discord_channels->{$channel_id}->{type} == 1);
239 say localtime(time) . " - $channel_id $channel $author_name $author_id";
240 if ($channel eq 'dm')
245 my $c = NDIRC::DiscordContext->new({
246 discord_id => $author_name,
247 channel_id => $channel_id,
248 author_id => $author_id,
253 discord => $self->discord,
256 return $self->handleCommand($c,$msg);