]> ruin.nu Git - NDIRC.git/blob - Bot.pm
Log notices, need POE::Component::IRC 6.16
[NDIRC.git] / Bot.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::Bot;
20
21 use strict;
22 use warnings;
23 use feature ':5.10';
24
25 use Moose;
26
27 use POE::Component::IRC::Common qw/irc_to_utf8/;
28 use POE::Session;
29 use POE::Component::IRC::Plugin::Logger;
30 use POE::Component::IRC::Plugin::BotTraffic;
31 use POE::Component::IRC::Plugin::Connector;
32 use POE::Component::IRC::Plugin::AutoJoin;
33 use POE::Component::IRC::Plugin::NickReclaim;
34
35 use NDIRC::Dispatcher;
36
37 use IO::File;
38
39 has disp => (
40         is => 'rw',
41         isa => 'Object',
42         lazy_build => 1
43 );
44
45 # We registered for all events, this will produce some debug info.
46 sub _default {
47         my ($event, $args) = @_[ARG0 .. $#_];
48         my @output = ( "$event: " );
49
50         for my $arg (@$args) {
51                 if ( ref $arg eq 'ARRAY' ) {
52                         push( @output, '[' . join(', ', @$arg ) . ']' );
53                 }
54                 else {
55                         push ( @output, "'$arg'" );
56                 }
57         }
58         print join ' ', @output, "\n";
59         return 0;
60 }
61
62 sub _start {
63         my ($kernel,$heap,$session) = @_[KERNEL,HEAP,SESSION];
64
65         # retrieve our component's object from the heap where we stashed it
66         my $irc = $heap->{irc};
67         $kernel->sig( DIE => 'sig_DIE' );
68         $kernel->sig( USR1 => 'sig_usr1' );
69         $kernel->sig( USR2 => 'sig_usr2' );
70         $kernel->sig( INT => 'signal_handler' );
71
72         $irc->plugin_add( 'NickReclaim', POE::Component::IRC::Plugin::NickReclaim->new() );
73         $irc->plugin_add( 'AutoJoin', POE::Component::IRC::Plugin::NickReclaim->new() );
74         $irc->plugin_add( 'BotTraffic', POE::Component::IRC::Plugin::BotTraffic->new() );
75         $irc->plugin_add('Logger', POE::Component::IRC::Plugin::Logger->new(
76                 Path    => 'irclogs',
77                 DCC     => 0,
78                 Private => 1,
79                 Public  => 1,
80                 Sort_by_date => 1,
81                 Strip_color => 1,
82                 Strip_formatting => 1,
83                 Notices => 1,
84         ));
85
86         $heap->{connector} = POE::Component::IRC::Plugin::Connector->new(
87                 servers => [['irc.netgamers.org'], ['underworld.no.eu.netgamers.org']
88                         ,['firefly.no.eu.netgamers.org'], ['underworld.ca.us.netgamers.org'] ]
89         );
90         $irc->plugin_add( 'Connector' => $heap->{connector} );
91
92         $irc->yield( register => 'all' );
93         $irc->yield( connect => { server => 'irc.netgamers.org' } );
94
95         $kernel->delay( refresh => 60 );
96         return;
97 }
98
99 sub auth {
100         my $heap = $_[HEAP];
101
102         if (my $f =  new IO::File 'auth'){
103                 my $user = <$f>;
104                 chomp $user;
105                 my $pass = <$f>;
106                 chomp $pass;
107                 $heap->{irc}->yield(qbot_auth => $user => $pass);
108         }
109 }
110
111 sub sig_usr1 {
112         my ($kernel,$heap) = @_[KERNEL,HEAP];
113
114         $kernel->yield( 'refresh' );
115 }
116
117 sub sig_usr2 {
118         my $self = shift @_;
119
120         $self->disp($self->_build_disp);
121 }
122
123 sub _build_disp {
124         my ($self) = @_;
125         my $disp = new NDIRC::Dispatcher;
126
127         if (my $commands = new IO::File 'commands'){
128                 my @commands = split /\W+/, do{local $/; <$commands>};
129                 say "Loading commands from: @commands";
130                 $disp->load(@commands);
131         }
132
133         my $channels = new IO::File 'channels' or die $!;;
134         while (<$channels>){
135                 my ($chan, @types) = split /\s+/;
136                 say "$chan - @types";
137                 if ($chan =~ /^(.*):(.*)$/){
138                         $chan = $1;
139                         $disp->set_target($2,$chan);
140                 }
141                 $disp->add_channel($chan,\@types);
142         }
143
144         return $disp;
145 }
146
147 sub sig_DIE {
148         my( $kernel,$sig, $ex ) = @_[ KERNEL,ARG0, ARG1 ];
149         say "DIED!!!!!!!!!!!!!!";
150         # $sig is 'DIE'
151         # $ex is the exception hash
152         warn "$$: error in event: $ex->{error_str}";
153         $kernel->sig_handled();
154
155         # Send the signal to session that sent the original event.
156         #if( $ex->{source_session} ne $_[SESSION] ) {
157         #$kernel->signal( $ex->{source_session}, 'DIE', $sig, $ex );
158         #}
159 }
160
161 sub signal_handler {
162         my ($kernel, $signal_name, $heap) = @_[KERNEL, ARG0, HEAP];
163         print "First session caught SIG$signal_name\n";
164
165         given($signal_name){
166                 when ('INT') {
167                         exit unless $heap->{irc}->connected;
168                         $heap->{INT} = 1;
169                         $heap->{irc}->yield(quit => 'Bye!');
170                         $kernel->sig_handled();
171                 }
172         }
173         #$kernel->sig_handled();
174 }
175
176 sub irc_disconnected {
177         my ($sender,$heap) = @_[SENDER,HEAP];
178
179         exit if $heap->{INT};
180 }
181
182 sub irc_001 {
183         my ($self,$sender,$kernel) = @_[OBJECT,SENDER,KERNEL];
184
185         # Since this is an irc_* event, we can get the component's object by
186         # accessing the heap of the sender. Then we register and connect to the
187         # specified server.
188         my $irc = $sender->get_heap();
189
190         print "Connected to ", $irc->server_name(), "\n";
191
192         $kernel->yield( 'auth' );
193         $irc->yield( mode => $irc->nick_name, '+ix');
194
195         # we join our channels
196         $irc->yield( join => $_ ) for grep /^#/, keys %{$self->disp->channels};
197         return;
198 }
199
200 sub irc_invite {
201         my ($self,$sender, $who, $channel) = @_[OBJECT,SENDER, ARG0 .. ARG1];
202         my $irc = $sender->get_heap();
203
204         $irc->yield( join => $_ ) for grep /^$channel$/i, keys %{$self->disp->channels}
205 }
206
207 sub irc_public {
208         $_[ARG2] = irc_to_utf8 $_[ARG2];
209 }
210
211 sub irc_msg {
212         $_[ARG2] = irc_to_utf8 $_[ARG2];
213 }
214
215 sub  refresh {
216 }
217
218 sub irc_join {
219 }
220
221 sub parseCommand {
222         my ($self, $msg, $server, $nick, $address, $channel, $model) = @_;
223
224         return if $channel !~ /^#/ && $msg =~ /^~/;
225         $msg = ".$msg"  if $channel !~ /^#/ && $msg =~ /^[^.!]/;
226
227         my ($p,$command,$args) = ($msg =~ /^([.!~])(\S+)(?: (.+))?/);
228
229         if ($msg =~ m{http://[\w.]+/.+?scan(_id|_grp)?=(\w+)}){
230                 if (!$command || $command =~ m{^http://}){
231                         ($p,$command,$args) = ('.','addscan',$msg);
232                 }elsif($command ne 'addscan'){
233                         $self->parseCommand (".addscan $msg", $server, $nick, $address, $channel, $model)
234                 }
235         }
236
237         return 0 unless $self->disp->has_command($command,$channel);
238
239         my $reply_string;
240         given ($p){
241                 when ('!'){
242                         $reply_string = "privmsg $nick";
243                 }
244                 when ('~'){
245                         $reply_string = "privmsg $channel";
246                 }
247                 default {
248                         $reply_string = "notice $nick";
249                 }
250         }
251
252         $address =~ s/.*@(.*)/$1/;
253         my $c = NDIRC::Context->new({
254                         host => $address,
255                         nick => $nick,
256                         channel => $channel,
257                         disp => $self->disp,
258                         model => $model,
259                         server => $server,
260                         reply_string => $reply_string,
261                 });
262
263         return $self->disp->run_command($c,$command,$args);
264 }
265
266 1;