]> ruin.nu Git - NDIRC.git/blob - Bot.pm
Only allow chattr in certain channels
[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 clear_constraint {
119         my $tc = shift;
120
121         while (1) {
122                 if (ref $tc eq 'MooseX::Meta::TypeConstraint::Structured'){
123                         for my $t (@{$tc->{type_constraints}}){
124                                 clear_constraint($t);
125                         }
126
127                 }
128                 if (ref $tc eq 'Moose::Meta::TypeConstraint::Parameterized'){
129                         clear_constraint($tc->{type_parameter});
130                 }
131                 last if ref $tc eq 'HASH';
132                 last if ref $tc eq '';
133                 if (defined $tc->{_type_constraint}){
134                         $tc = $tc->{_type_constraint};
135                 }elsif(defined $tc->{__type_constraint}){
136                         $tc = $tc->{__type_constraint};
137                 }else{
138                         last;
139                 }
140         }
141 }
142
143 sub clear_metains {
144         my $ins = shift;
145
146         for my $a (@{$ins->{attributes}}){
147                 for my $m (@{$a->{associated_methods}}){
148                         $m->{body} = undef;
149                 }
150                 clear_constraint($a->{isa});
151         }
152 }
153
154 sub clear_cycles {
155         my $c = shift;
156
157         for my $m (values %{$c->meta->{methods}}){
158                 clear_constraint($m->{type_constraint});
159
160                 my $ps = $m->{parsed_signature};
161                 for my $p (@{$ps->{_positional_params}->{params}}){
162                         clear_metains($p->{__MOP__}->{_meta_instance});
163                 }
164
165                 $m->{body} = undef;
166         }
167         clear_metains($c->meta->{_meta_instance});
168 }
169
170
171 sub sig_usr2 {
172         my $self = shift @_;
173
174         for my $c (values %{$self->disp->commands}){
175                 clear_cycles($c);
176         }
177
178         $self->disp($self->_build_disp);
179 }
180
181 sub _build_disp {
182         my ($self) = @_;
183         my $disp = new NDIRC::Dispatcher;
184
185         if (my $commands = new IO::File 'commands'){
186                 my @commands = split /\W+/, do{local $/; <$commands>};
187                 say "Loading commands from: @commands";
188                 $disp->load(@commands);
189         }
190
191         my $channels = new IO::File 'channels' or die $!;;
192         while (<$channels>){
193                 my ($chan, @types) = split /\s+/;
194                 say "$chan - @types";
195                 if ($chan =~ /^(.*):(.*)$/){
196                         $chan = $1;
197                         $disp->set_target($2,$chan);
198                 }
199                 $disp->add_channel($chan,\@types);
200         }
201
202         return $disp;
203 }
204
205 sub sig_DIE {
206         my( $kernel,$sig, $ex ) = @_[ KERNEL,ARG0, ARG1 ];
207         say "DIED!!!!!!!!!!!!!!";
208         # $sig is 'DIE'
209         # $ex is the exception hash
210         warn "$$: error in event: $ex->{error_str}";
211         $kernel->sig_handled();
212
213         # Send the signal to session that sent the original event.
214         #if( $ex->{source_session} ne $_[SESSION] ) {
215         #$kernel->signal( $ex->{source_session}, 'DIE', $sig, $ex );
216         #}
217 }
218
219 sub signal_handler {
220         my ($kernel, $signal_name, $heap) = @_[KERNEL, ARG0, HEAP];
221         print "First session caught SIG$signal_name\n";
222
223         given($signal_name){
224                 when ('INT') {
225                         exit unless $heap->{irc}->connected;
226                         $heap->{INT} = 1;
227                         $heap->{irc}->yield(quit => 'Bye!');
228                         $kernel->sig_handled();
229                 }
230         }
231         #$kernel->sig_handled();
232 }
233
234 sub irc_disconnected {
235         my ($sender,$heap) = @_[SENDER,HEAP];
236
237         exit if $heap->{INT};
238 }
239
240 sub irc_001 {
241         my ($self,$sender,$kernel) = @_[OBJECT,SENDER,KERNEL];
242
243         # Since this is an irc_* event, we can get the component's object by
244         # accessing the heap of the sender. Then we register and connect to the
245         # specified server.
246         my $irc = $sender->get_heap();
247
248         print "Connected to ", $irc->server_name(), "\n";
249
250         $kernel->yield( 'auth' );
251         $irc->yield( mode => $irc->nick_name, '+ix');
252
253         # we join our channels
254         $irc->yield( join => $_ ) for grep /^#/, keys %{$self->disp->channels};
255         return;
256 }
257
258 sub irc_invite {
259         my ($self,$sender, $who, $channel) = @_[OBJECT,SENDER, ARG0 .. ARG1];
260         my $irc = $sender->get_heap();
261
262         $irc->yield( join => $_ ) for grep /^$channel$/i, keys %{$self->disp->channels}
263 }
264
265 sub irc_public {
266         $_[ARG2] = irc_to_utf8 $_[ARG2];
267 }
268
269 sub irc_msg {
270         $_[ARG2] = irc_to_utf8 $_[ARG2];
271 }
272
273 sub  refresh {
274 }
275
276 sub irc_join {
277 }
278
279 sub parseCommand {
280         my ($self, $msg, $server, $nick, $address, $channel, $model) = @_;
281
282         return if $channel !~ /^#/ && $msg =~ /^~/;
283         $msg = ".$msg"  if $channel !~ /^#/ && $msg =~ /^[^.!]/;
284
285         my ($p,$command,$args) = ($msg =~ /^([.!~])(\S+)(?: (.+))?/);
286
287         if ($msg =~ m{http://[\w.]+/.+?scan(_id|_grp)?=(\w+)}){
288                 if (!$command || $command =~ m{^http://}){
289                         ($p,$command,$args) = ('.','addscan',$msg);
290                 }elsif($command ne 'addscan'){
291                         $self->parseCommand (".addscan $msg", $server, $nick, $address, $channel, $model)
292                 }
293         }
294
295         return 0 unless $self->disp->has_command($command,$channel);
296
297         my $reply_string;
298         given ($p){
299                 when ('!'){
300                         $reply_string = "privmsg $nick";
301                 }
302                 when ('~'){
303                         $reply_string = "privmsg $channel";
304                 }
305                 default {
306                         $reply_string = "notice $nick";
307                 }
308         }
309
310         $address =~ s/.*@(.*)/$1/;
311         my $c = NDIRC::Context->new({
312                         host => $address,
313                         nick => $nick,
314                         channel => $channel,
315                         disp => $self->disp,
316                         model => $model,
317                         server => $server,
318                         reply_string => $reply_string,
319                 });
320
321         return $self->disp->run_command($c,$command,$args);
322 }
323
324 1;