]> ruin.nu Git - NDIRC.git/blob - Bot.pm
9bf39f9ae09c612b88bdca9100558b1a4f505955
[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 Mojo::Discord;
36 use Mojo::IOLoop;
37
38 use NDIRC::Dispatcher;
39 use NDIRC::Context;
40
41 use IO::File;
42
43 has disp => (
44         is => 'rw',
45         isa => 'Object',
46         lazy_build => 1
47 );
48
49 has discord => (
50         is => 'rw',
51         isa => 'Object'
52 );
53
54 has discord_name => (
55         is => 'rw',
56         isa => 'Str'
57 );
58
59 has discord_id => (
60         is => 'rw',
61         isa => 'Str'
62 );
63
64 has discord_channels => (
65         is => 'rw',
66         isa => 'HashRef',
67         default => sub { {} }
68 );
69
70 # We registered for all events, this will produce some debug info.
71 sub _default {
72         my ($event, $args) = @_[ARG0 .. $#_];
73         my @output = ( "$event: " );
74
75         for my $arg (@$args) {
76                 if ( ref $arg eq 'ARRAY' ) {
77                         push( @output, '[' . join(', ', @$arg ) . ']' );
78                 }
79                 else {
80                         push ( @output, "'$arg'" );
81                 }
82         }
83         print join ' ', @output, "\n";
84         return 0;
85 }
86
87 sub _start {
88         my ($self,$kernel,$heap,$session) = @_[OBJECT,KERNEL,HEAP,SESSION];
89
90         # retrieve our component's object from the heap where we stashed it
91         my $irc = $heap->{irc};
92         $kernel->sig( DIE => 'sig_DIE' );
93         $kernel->sig( USR1 => 'sig_usr1' );
94         $kernel->sig( USR2 => 'sig_usr2' );
95         $kernel->sig( INT => 'signal_handler' );
96
97         $irc->plugin_add( 'NickReclaim', POE::Component::IRC::Plugin::NickReclaim->new() );
98         $irc->plugin_add( 'AutoJoin', POE::Component::IRC::Plugin::NickReclaim->new() );
99         $irc->plugin_add( 'BotTraffic', POE::Component::IRC::Plugin::BotTraffic->new() );
100         $irc->plugin_add('Logger', POE::Component::IRC::Plugin::Logger->new(
101                 Path    => 'irclogs',
102                 DCC     => 0,
103                 Private => 1,
104                 Public  => 1,
105                 Sort_by_date => 1,
106                 Strip_color => 1,
107                 Strip_formatting => 1,
108                 Notices => 1,
109         ));
110
111         $heap->{connector} = POE::Component::IRC::Plugin::Connector->new(
112                 servers => [['irc.netgamers.org'], ['underworld.no.eu.netgamers.org']
113                         ,['firefly.no.eu.netgamers.org'], ['underworld.ca.us.netgamers.org'] ]
114         );
115         $irc->plugin_add( 'Connector' => $heap->{connector} );
116
117         $irc->yield( register => 'all' );
118         $irc->yield( connect => { server => 'irc.netgamers.org' } );
119
120         $kernel->delay( refresh => 60 );
121
122         if (my $f =  new IO::File 'discord'){
123                 my $user = <$f>;
124                 chomp $user;
125                 my $token = <$f>;
126                 chomp $token;
127
128                 $self->discord(Mojo::Discord->new(
129                                 'token'     => $token,
130                                 'name'      => $user,
131                                 'url'       => 'https://nd.ruin.nu',
132                                 'version'   => '1.0',
133                                 'callbacks' => {
134                                         'READY'          => sub { $self->discord_ready(@_) },
135                                         'MESSAGE_CREATE' => sub { $self->discord_message_create(@_) },
136                                         'GUILD_CREATE' => sub { $self->discord_guild_create(@_) },
137                                 },
138                                 'reconnect' => 1,
139                                 'verbose'   => 1,
140                         ));
141                 $self->discord->init();
142         }
143         return;
144 }
145
146 sub auth {
147         my $heap = $_[HEAP];
148
149         if (my $f =  new IO::File 'auth'){
150                 my $user = <$f>;
151                 chomp $user;
152                 my $pass = <$f>;
153                 chomp $pass;
154                 $heap->{irc}->yield(qbot_auth => $user => $pass);
155         }
156 }
157
158 sub sig_usr1 {
159         my ($kernel,$heap) = @_[KERNEL,HEAP];
160
161         $kernel->yield( 'refresh' );
162 }
163
164 sub clear_constraint {
165         my $tc = shift;
166
167         while (1) {
168                 if (ref $tc eq 'MooseX::Meta::TypeConstraint::Structured'){
169                         for my $t (@{$tc->{type_constraints}}){
170                                 clear_constraint($t);
171                         }
172
173                 }
174                 if (ref $tc eq 'Moose::Meta::TypeConstraint::Parameterized'){
175                         clear_constraint($tc->{type_parameter});
176                 }
177                 last if ref $tc eq 'HASH';
178                 last if ref $tc eq '';
179                 if (defined $tc->{_type_constraint}){
180                         $tc = $tc->{_type_constraint};
181                 }elsif(defined $tc->{__type_constraint}){
182                         $tc = $tc->{__type_constraint};
183                 }else{
184                         last;
185                 }
186         }
187 }
188
189 sub clear_metains {
190         my $ins = shift;
191
192         for my $a (@{$ins->{attributes}}){
193                 for my $m (@{$a->{associated_methods}}){
194                         $m->{body} = undef;
195                 }
196                 clear_constraint($a->{isa});
197         }
198 }
199
200 sub clear_cycles {
201         my $c = shift;
202
203         for my $m (values %{$c->meta->{methods}}){
204                 clear_constraint($m->{type_constraint});
205
206                 my $ps = $m->{parsed_signature};
207                 for my $p (@{$ps->{_positional_params}->{params}}){
208                         clear_metains($p->{__MOP__}->{_meta_instance});
209                 }
210
211                 $m->{body} = undef;
212         }
213         clear_metains($c->meta->{_meta_instance});
214 }
215
216
217 sub sig_usr2 {
218         my $self = shift @_;
219
220         for my $c (values %{$self->disp->commands}){
221                 clear_cycles($c);
222         }
223
224         $self->disp($self->_build_disp);
225 }
226
227 sub _build_disp {
228         my ($self) = @_;
229         my $disp = new NDIRC::Dispatcher;
230
231         if (my $commands = new IO::File 'commands'){
232                 my @commands = split /\W+/, do{local $/; <$commands>};
233                 say "Loading commands from: @commands";
234                 $disp->load(@commands);
235         }
236
237         my $channels = new IO::File 'channels' or die $!;;
238         while (<$channels>){
239                 my ($chan, @types) = split /\s+/;
240                 say "$chan - @types";
241                 if ($chan =~ /^(.*):(.*)$/){
242                         $chan = $1;
243                         $disp->set_target($2,$chan);
244                 }
245                 $disp->add_channel($chan,\@types);
246         }
247
248         return $disp;
249 }
250
251 sub sig_DIE {
252         my( $kernel,$sig, $ex ) = @_[ KERNEL,ARG0, ARG1 ];
253         say "DIED!!!!!!!!!!!!!!";
254         # $sig is 'DIE'
255         # $ex is the exception hash
256         warn "$$: error in event: $ex->{error_str}";
257         $kernel->sig_handled();
258
259         # Send the signal to session that sent the original event.
260         #if( $ex->{source_session} ne $_[SESSION] ) {
261         #$kernel->signal( $ex->{source_session}, 'DIE', $sig, $ex );
262         #}
263 }
264
265 sub signal_handler {
266         my ($kernel, $signal_name, $heap) = @_[KERNEL, ARG0, HEAP];
267         print "First session caught SIG$signal_name\n";
268
269         given($signal_name){
270                 when ('INT') {
271                         exit unless $heap->{irc}->connected;
272                         $heap->{INT} = 1;
273                         $heap->{irc}->yield(quit => 'Bye!');
274                         $kernel->sig_handled();
275                 }
276         }
277         #$kernel->sig_handled();
278 }
279
280 sub irc_disconnected {
281         my ($sender,$heap) = @_[SENDER,HEAP];
282
283         exit if $heap->{INT};
284 }
285
286 sub irc_001 {
287         my ($self,$sender,$kernel) = @_[OBJECT,SENDER,KERNEL];
288
289         # Since this is an irc_* event, we can get the component's object by
290         # accessing the heap of the sender. Then we register and connect to the
291         # specified server.
292         my $irc = $sender->get_heap();
293
294         print "Connected to ", $irc->server_name(), "\n";
295
296         $kernel->yield( 'auth' );
297         $irc->yield( mode => $irc->nick_name, '+ix');
298
299         # we join our channels
300         $irc->yield( join => $_ ) for grep /^#/, keys %{$self->disp->channels};
301         return;
302 }
303
304 sub irc_invite {
305         my ($self,$sender, $who, $channel) = @_[OBJECT,SENDER, ARG0 .. ARG1];
306         my $irc = $sender->get_heap();
307
308         $irc->yield( join => $_ ) for grep /^$channel$/i, keys %{$self->disp->channels}
309 }
310
311 sub irc_public {
312         $_[ARG2] = irc_to_utf8 $_[ARG2];
313 }
314
315 sub irc_msg {
316         $_[ARG2] = irc_to_utf8 $_[ARG2];
317 }
318
319 sub  refresh {
320 }
321
322 sub irc_join {
323 }
324
325 sub discord_ready {
326         my $self = shift;
327         my $hash = shift;
328         $self->discord_id($hash->{user}{id});
329         $self->discord_name($hash->{user}{username});
330
331         say localtime(time) . " - Connected to Discord. $self->{discord_id}";
332 }
333
334 sub discord_message_create {
335 }
336
337 sub discord_guild_create {
338         my $self = shift;
339         my $hash = shift;
340
341         for my $chan (@{$hash->{channels}}) {
342                 say localtime(time) . "$chan->{id} - $chan->{name}";
343                 $self->discord_channels->{$chan->{id}} = $chan->{name};
344         }
345 }
346
347 sub parseCommand {
348         my ($self, $msg, $server, $nick, $address, $channel, $model) = @_;
349
350         return if $channel !~ /^#/ && $msg =~ /^~/;
351         $msg = ".$msg"  if $channel !~ /^#/ && $msg =~ /^[^.!]/;
352
353         my ($p,$command,$args) = ($msg =~ /^([.!~])(\S+)(?: (.+))?/);
354
355         if ($msg =~ m{https?://[\w.]+/.+?scan(_id|_grp)?=(\w+)}){
356                 if (!$command || $command =~ m{^https?://}){
357                         ($p,$command,$args) = ('.','addscan',$msg);
358                 }elsif($command ne 'addscan'){
359                         $self->parseCommand (".addscan $msg", $server, $nick, $address, $channel, $model)
360                 }
361         }
362
363         return 0 unless $self->disp->has_command($command,$channel);
364
365         my $reply_string;
366         given ($p){
367                 when ('!'){
368                         $reply_string = "privmsg $nick";
369                 }
370                 when ('~'){
371                         $reply_string = "privmsg $channel";
372                 }
373                 default {
374                         $reply_string = "notice $nick";
375                 }
376         }
377
378         $address =~ s/.*@(.*)/$1/;
379         my $c = NDIRC::Context->new({
380                         host => $address,
381                         nick => $nick,
382                         channel => $channel,
383                         disp => $self->disp,
384                         model => $model,
385                         server => $server,
386                         reply_string => $reply_string,
387                 });
388
389         return $self->disp->run_command($c,$command,$args);
390 }
391
392 1;