]> ruin.nu Git - NDIRC.git/blob - Bot.pm
Need to send a real value to execute when the role isn't available
[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         ));
84
85         $heap->{connector} = POE::Component::IRC::Plugin::Connector->new(
86                 servers => [['irc.netgamers.org'], ['underworld.no.eu.netgamers.org']
87                         ,['firefly.no.eu.netgamers.org'], ['underworld.ca.us.netgamers.org'] ]
88         );
89         $irc->plugin_add( 'Connector' => $heap->{connector} );
90
91         $irc->yield( register => 'all' );
92         $irc->yield( connect => { server => 'irc.netgamers.org' } );
93
94         $kernel->delay( refresh => 60 );
95         return;
96 }
97
98 sub auth {
99         my $heap = $_[HEAP];
100
101         if (my $f =  new IO::File 'auth'){
102                 my $user = <$f>;
103                 chomp $user;
104                 my $pass = <$f>;
105                 chomp $pass;
106                 $heap->{irc}->yield(qbot_auth => $user => $pass);
107         }
108 }
109
110 sub sig_usr1 {
111         my ($kernel,$heap) = @_[KERNEL,HEAP];
112
113         $kernel->yield( 'refresh' );
114 }
115
116 sub sig_usr2 {
117         my $self = shift @_;
118
119         $self->disp($self->_build_disp);
120 }
121
122 sub _build_disp {
123         my ($self) = @_;
124         my $disp = new NDIRC::Dispatcher;
125
126         if (my $commands = new IO::File 'commands'){
127                 my @commands = split /\W+/, do{local $/; <$commands>};
128                 say "Loading commands from: @commands";
129                 $disp->load(@commands);
130         }
131
132         my $channels = new IO::File 'channels' or die $!;;
133         while (<$channels>){
134                 my ($chan, @types) = split /\s+/;
135                 say "$chan - @types";
136                 if ($chan =~ /^(.*):(.*)$/){
137                         $chan = $1;
138                         $disp->set_target($2,$chan);
139                 }
140                 $disp->add_channel($chan,\@types);
141         }
142
143         return $disp;
144 }
145
146 sub sig_DIE {
147         my( $kernel,$sig, $ex ) = @_[ KERNEL,ARG0, ARG1 ];
148         say "DIED!!!!!!!!!!!!!!";
149         # $sig is 'DIE'
150         # $ex is the exception hash
151         warn "$$: error in event: $ex->{error_str}";
152         $kernel->sig_handled();
153
154         # Send the signal to session that sent the original event.
155         #if( $ex->{source_session} ne $_[SESSION] ) {
156         #$kernel->signal( $ex->{source_session}, 'DIE', $sig, $ex );
157         #}
158 }
159
160 sub signal_handler {
161         my ($kernel, $signal_name, $heap) = @_[KERNEL, ARG0, HEAP];
162         print "First session caught SIG$signal_name\n";
163
164         given($signal_name){
165                 when ('INT') {
166                         exit unless $heap->{irc}->connected;
167                         $heap->{INT} = 1;
168                         $heap->{irc}->yield(quit => 'Bye!');
169                         $kernel->sig_handled();
170                 }
171         }
172         #$kernel->sig_handled();
173 }
174
175 sub irc_disconnected {
176         my ($sender,$heap) = @_[SENDER,HEAP];
177
178         exit if $heap->{INT};
179 }
180
181 sub irc_001 {
182         my ($self,$sender,$kernel) = @_[OBJECT,SENDER,KERNEL];
183
184         # Since this is an irc_* event, we can get the component's object by
185         # accessing the heap of the sender. Then we register and connect to the
186         # specified server.
187         my $irc = $sender->get_heap();
188
189         print "Connected to ", $irc->server_name(), "\n";
190
191         $kernel->yield( 'auth' );
192         $irc->yield( mode => $irc->nick_name, '+ix');
193
194         # we join our channels
195         $irc->yield( join => $_ ) for grep /^#/, keys %{$self->disp->channels};
196         return;
197 }
198
199 sub irc_invite {
200         my ($self,$sender, $who, $channel) = @_[OBJECT,SENDER, ARG0 .. ARG1];
201         my $irc = $sender->get_heap();
202
203         $irc->yield( join => $_ ) for grep /^$channel$/i, keys %{$self->disp->channels}
204 }
205
206 sub irc_public {
207         $_[ARG2] = irc_to_utf8 $_[ARG2];
208 }
209
210 sub irc_msg {
211         $_[ARG2] = irc_to_utf8 $_[ARG2];
212 }
213
214 sub  refresh {
215 }
216
217 sub irc_join {
218 }
219
220 sub parseCommand {
221         my ($self, $msg, $server, $nick, $address, $channel, $model) = @_;
222
223         return if $channel !~ /^#/ && $msg =~ /^~/;
224         $msg = ".$msg"  if $channel !~ /^#/ && $msg =~ /^[^.!]/;
225
226         my ($p,$command,$args) = ($msg =~ /^([.!~])(\S+)(?: (.+))?/);
227
228         if ($msg =~ m{http://[\w.]+/.+?scan(_id|_grp)?=(\w+)}){
229                 if (!$command || $command =~ m{^http://}){
230                         ($p,$command,$args) = ('.','addscan',$msg);
231                 }elsif($command ne 'addscan'){
232                         $self->parseCommand (".addscan $msg", $server, $nick, $address, $channel, $model)
233                 }
234         }
235
236         return 0 unless $self->disp->has_command($command,$channel);
237
238         my $reply_string;
239         given ($p){
240                 when ('!'){
241                         $reply_string = "privmsg $nick";
242                 }
243                 when ('~'){
244                         $reply_string = "privmsg $channel";
245                 }
246                 default {
247                         $reply_string = "notice $nick";
248                 }
249         }
250
251         $address =~ s/.*@(.*)/$1/;
252         my $c = NDIRC::Context->new({
253                         host => $address,
254                         nick => $nick,
255                         channel => $channel,
256                         disp => $self->disp,
257                         model => $model,
258                         server => $server,
259                         reply_string => $reply_string,
260                 });
261
262         return $self->disp->run_command($c,$command,$args);
263 }
264
265 1;