]> ruin.nu Git - NDIRC.git/blob - Eos.pm
Remove the use of global defchan in report_incs, also allow the command in def channel
[NDIRC.git] / Eos.pm
1 #**************************************************************************
2 #   Copyright (C) 2006 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::Eos;
20
21 use strict;
22 use warnings;
23 use feature ':5.10';
24
25 use POE::Session;
26 use NDIRC::Misc;
27 use ND::DB;
28
29 my ($tick,$stattick) = DB()->selectrow_array(q{SELECT tick(),max(tick) FROM planet_stats});
30 my $last_announcement = 0;
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         if ($msg =~ /^(\S+): (.+)$/ && $heap->{disp}->has_command('anon',$channel)){
40                 my $_ = $1;
41                 my $text = $2;
42                 unless ($irc->is_channel_member($channel,$1) || /(Constructing|Researching)/){
43                         $msg = ".anon $_ $text";
44                 }
45
46         }
47         if (parseCommand($msg,$irc,$nick,$address,$channel,$heap->{disp},DB())){
48                 #Command parsed and run successfully
49         }
50 }
51
52
53 sub irc_msg {
54         my ($sender, $heap, $who, $where, $msg) = @_[SENDER, HEAP, ARG0 .. ARG2];
55         my ($nick,$username,$address) = ( split /[!@]/, $who );
56         my $irc = $sender->get_heap();
57
58         if (parseCommand($msg,$irc,$nick,$address,'pm',$heap->{disp},DB())){
59                 #Command parsed and run successfully
60         }else{
61                 $irc->yield(privmsg => $ND::defchan, chr(3)."04 $nick >> $msg");
62                 parseCommand("~report_incs $msg",$irc,$nick,' BATCH ',$ND::defchan,$heap->{disp},DB());
63         }
64 }
65
66 sub irc_join {
67         my ($sender, $heap, $who, $channel) = @_[SENDER, HEAP, ARG0 .. ARG1];
68         my ($nick,$username,$address) = ( split /[!@]/, $who );
69         my $irc = $sender->get_heap();
70
71         my $dbh = DB();
72
73         if ($channel =~ /^$ND::memchan$/i){
74                 if (time - $last_announcement < 1){
75                         $last_announcement = time;
76                         return;
77                 }
78                 my $user = $dbh->selectrow_hashref(q{
79 SELECT uid,pid,hostmask,password
80         ,(SELECT max(time) FROM forum_thread_visits WHERE uid = u.uid) AS last_forum_visit
81 FROM users u WHERE hostmask = ?
82                 }, undef, $address);
83                 if ($user){
84                         if ($user->{password} eq ''){
85                                 $irc->yield(privmsg => $ND::memchan, "$nick: Get a new random password with /msg delling !getpass . If you don't know your username, then you can get it with .points");
86                         }
87                         if ($tick > 36 && not defined $user->{pid}){
88                                 $irc->yield(privmsg => $ND::memchan, "$nick: go to https://nd.ruin.nu/ and enter your coords.");
89                         }
90
91                         if (not defined $user->{last_forum_visit}){
92                                 $irc->yield(privmsg => $ND::memchan, "$nick: Go read the forum! https://nd.ruin.nu/forum");
93                         }else {
94                                 my $unread = $dbh->selectrow_hashref(q{SELECT * FROM unread_posts($1)},undef,$user->{uid});
95                                 if ($unread && $unread->{new}){
96                                         $irc->yield(notice => $nick, "$unread->{new} posts since your last forum visit ($unread->{unread} unread posts in total) https://nd.ruin.nu/forum/allUnread");
97                                 }
98                         }
99                 }
100                 if (time - $last_announcement < 2){
101                         $last_announcement = time;
102                         return;
103                 }
104                 $last_announcement = time;
105         }
106 }
107
108 sub refresh {
109         my ($kernel,$heap) = @_[KERNEL,HEAP];
110         $kernel->delay( refresh => 60 );
111         print 'Time: ' . time() . ' Lag: ' . $heap->{connector}->lag() . "\n";
112
113         my $irc = $heap->{irc};
114         my $dbh = DB();
115
116         my @row = $dbh->selectrow_array(q{SELECT tick(), max(tick) FROM planet_stats});
117         if ($tick != $row[0]){
118                 $tick = $row[0];
119                 $irc->yield(privmsg => $ND::defchan, "New tick: $tick");
120         }
121         if (defined $row[1] && $stattick != $row[1]){
122                 $stattick = $row[1];
123                 $irc->yield(privmsg => $ND::memchan, "New tick: $stattick");
124         }
125         my $ircreqs = $dbh->prepare(q{SELECT id,username,message,channel FROM irc_requests NATURAL JOIN users WHERE not sent});
126         my $upircreq = $dbh->prepare(q{UPDATE irc_requests SET sent = TRUE WHERE id = ?});
127         $ircreqs->execute;
128         while (my $req = $ircreqs->fetchrow_hashref){
129                 if ($req->{channel} eq 'def'){
130                         $irc->yield(privmsg => $ND::defchan, chr(3)."04 ## $req->{username} via webbie ## >> $req->{message}");
131                         parseCommand("~report_incs $req->{message}",$irc,$req->{username},' BATCH ',$ND::defchan,$heap->{disp},$dbh);
132                 }elsif($req->{channel} eq 'scan'){
133                         $irc->yield(privmsg => $ND::scanchan, "<$req->{username} via webbie> $req->{message}");
134                 }elsif($req->{channel} eq 'members'){
135                         $irc->yield(privmsg => $ND::memchan, "<$req->{username} via webbie> $req->{message}");
136                 }
137                 $upircreq->execute($req->{id});
138         }
139         my $defmissions = $dbh->prepare(q{
140 SELECT username,call,tick,dm.fleet,p.value
141         ,100 * SUM(fs.amount * (metal + crystal + eonium) / 100) / p.value AS value
142 FROM users u
143         JOIN current_planet_stats p USING (pid)
144         JOIN launch_confirmations USING (uid)
145         JOIN fleets f USING (fid)
146         JOIN fleet_ships fs USING (fid)
147         JOIN defense_missions dm ON f.fid = dm.fleet
148         JOIN ship_stats ss ON fs.ship = ss.name
149 WHERE NOT dm.announced
150 GROUP BY username,call,tick,dm.fleet,p.value
151                 });
152         my $updefmis = $dbh->prepare(q{UPDATE defense_missions SET announced = TRUE WHERE fleet = ?});
153         $defmissions->execute();
154         while (my $mission = $defmissions->fetchrow_hashref){
155                 $irc->yield(privmsg => $ND::defchan, 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}");
156                 $updefmis->execute($mission->{fleet});
157         }
158 }
159
160 1;