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