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