1 #**************************************************************************
2 # Copyright (C) 2009 by Michael Andreen <harvATruinDOTnu> *
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. *
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. *
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 #**************************************************************************/
27 use POE::Component::IRC::Common qw/irc_to_utf8/;
30 use POE::Component::IRC::Plugin::Logger;
31 use POE::Component::IRC::Plugin::BotTraffic;
32 use POE::Component::IRC::Plugin::Connector;
33 use POE::Component::IRC::Plugin::AutoJoin;
34 use POE::Component::IRC::Plugin::NickReclaim;
39 use NDIRC::Dispatcher;
40 use NDIRC::IrcContext;
65 has discord_channels => (
73 isa => 'HashRef[ArrayRef[Str]]',
77 # We registered for all events, this will produce some debug info.
79 my ($event, $args) = @_[ARG0 .. $#_];
80 my @output = ( "$event: " );
82 for my $arg (@$args) {
83 if ( ref $arg eq 'ARRAY' ) {
84 push( @output, '[' . join(', ', @$arg ) . ']' );
87 push ( @output, "'$arg'" );
90 print join ' ', @output, "\n";
100 my ($self,$kernel,$heap,$session) = @_[OBJECT,KERNEL,HEAP,SESSION];
102 # retrieve our component's object from the heap where we stashed it
104 $kernel->sig( DIE => 'sig_DIE' );
105 $kernel->sig( USR1 => 'sig_usr1' );
106 $kernel->sig( USR2 => 'sig_usr2' );
107 $kernel->sig( INT => 'signal_handler' );
109 $irc->plugin_add( 'NickReclaim', POE::Component::IRC::Plugin::NickReclaim->new() );
110 $irc->plugin_add( 'AutoJoin', POE::Component::IRC::Plugin::NickReclaim->new() );
111 $irc->plugin_add( 'BotTraffic', POE::Component::IRC::Plugin::BotTraffic->new() );
112 $irc->plugin_add('Logger', POE::Component::IRC::Plugin::Logger->new(
119 Strip_formatting => 1,
123 $heap->{connector} = POE::Component::IRC::Plugin::Connector->new(
124 servers => [['chronos.fr.eu.netgamers.org'], ['underworld.no.eu.netgamers.org']]
126 $irc->plugin_add( 'Connector' => $heap->{connector} );
128 $irc->yield( register => 'all' );
129 #$irc->yield( connect => { server => 'chronos.fr.eu.netgamers.org' } );
131 $kernel->delay( refresh => 60 );
133 if (my $f = new IO::File 'discord'){
139 $self->discord(Mojo::Discord->new(
142 'url' => 'https://nd.ruin.nu',
149 $self->discord->gw->on('MESSAGE_CREATE' => sub { $self->discord_message_create(@_) });
150 $self->discord->gw->on('MESSAGE_UPDATE' => sub { $self->discord_message_create(@_) });
151 $self->discord->gw->on('READY' => sub { $self->discord_ready(@_) });
152 $self->discord->gw->on('GUILD_CREATE' => sub { $self->discord_guild_create(@_) });
153 $self->discord->gw->on('CHANNEL_CREATE' => sub { $self->discord_channel_create(@_) });
155 $self->discord->init();
163 if (my $f = new IO::File 'auth'){
168 $heap->{irc}->yield(qbot_auth => $user => $pass);
173 my ($kernel,$heap) = @_[KERNEL,HEAP];
175 $kernel->yield( 'refresh' );
178 sub clear_constraint {
182 if (ref $tc eq 'MooseX::Meta::TypeConstraint::Structured'){
183 for my $t (@{$tc->{type_constraints}}){
184 clear_constraint($t);
188 if (ref $tc eq 'Moose::Meta::TypeConstraint::Parameterized'){
189 clear_constraint($tc->{type_parameter});
191 last if ref $tc eq 'HASH';
192 last if ref $tc eq '';
193 if (defined $tc->{_type_constraint}){
194 $tc = $tc->{_type_constraint};
195 }elsif(defined $tc->{__type_constraint}){
196 $tc = $tc->{__type_constraint};
206 for my $a (@{$ins->{attributes}}){
207 for my $m (@{$a->{associated_methods}}){
210 clear_constraint($a->{isa});
217 for my $m (values %{$c->meta->{methods}}){
218 clear_constraint($m->{type_constraint});
220 my $ps = $m->{parsed_signature};
221 for my $p (@{$ps->{_positional_params}->{params}}){
222 clear_metains($p->{__MOP__}->{_meta_instance});
227 clear_metains($c->meta->{_meta_instance});
234 for my $c (values %{$self->disp->commands}){
238 $self->disp($self->_build_disp);
243 my $disp = NDIRC::Dispatcher->new;
245 if (my $commands = IO::File->new('commands')){
246 my @commands = split /\W+/, do{local $/; <$commands>};
247 say "Loading commands from: @commands";
248 $disp->load(@commands);
251 %{$self->targets} = ();
252 my $channels = IO::File->new('channels') or die $!;;
254 my ($chan, @types) = split /\s+/;
255 say "$chan - @types";
256 if ($chan =~ /^(.*):(.*)$/){
258 $self->targets->{$2} = [] unless exists $self->targets->{$2};
259 push @{$self->targets->{$2}},lc $chan;
260 say "$2 - @{$self->targets->{$2}}";
262 $disp->add_channel($chan,\@types);
269 my( $kernel,$sig, $ex ) = @_[ KERNEL,ARG0, ARG1 ];
270 say "DIED!!!!!!!!!!!!!!";
272 # $ex is the exception hash
273 warn "$$: error in event: $ex->{error_str}";
274 $kernel->sig_handled();
276 # Send the signal to session that sent the original event.
277 #if( $ex->{source_session} ne $_[SESSION] ) {
278 #$kernel->signal( $ex->{source_session}, 'DIE', $sig, $ex );
283 my ($kernel, $signal_name, $heap) = @_[KERNEL, ARG0, HEAP];
284 print "First session caught SIG$signal_name\n";
288 exit unless $heap->{irc}->connected;
290 $heap->{irc}->yield(quit => 'Bye!');
291 $kernel->sig_handled();
294 #$kernel->sig_handled();
297 sub irc_disconnected {
298 my ($sender,$heap) = @_[SENDER,HEAP];
300 exit if $heap->{INT};
304 my ($self,$sender,$kernel) = @_[OBJECT,SENDER,KERNEL];
306 # Since this is an irc_* event, we can get the component's object by
307 # accessing the heap of the sender. Then we register and connect to the
309 my $irc = $sender->get_heap();
311 print "Connected to ", $irc->server_name(), "\n";
313 $kernel->yield( 'auth' );
314 $irc->yield( mode => $irc->nick_name, '+ix');
316 # we join our channels
317 $irc->yield( join => $_ ) for grep /^#/, keys %{$self->disp->channels};
322 my ($self,$sender, $who, $channel) = @_[OBJECT,SENDER, ARG0 .. ARG1];
323 my $irc = $sender->get_heap();
325 $irc->yield( join => $_ ) for grep /^$channel$/i, keys %{$self->disp->channels}
329 $_[ARG2] = irc_to_utf8 $_[ARG2];
333 $_[ARG2] = irc_to_utf8 $_[ARG2];
343 my ($self, $gw, $hash) = @_;
345 $self->discord_id($hash->{user}{id});
346 $self->discord_name($hash->{user}{username});
348 say localtime(time) . " - Connected to Discord. $self->{discord_id}";
351 sub discord_message_create {
354 sub discord_guild_create {
355 my ($self, $gw, $hash) = @_;
357 for my $chan (@{$hash->{channels}}) {
358 say localtime(time) . " - $chan->{id} - $chan->{name}";
359 $self->discord_channels->{$chan->{id}} = $chan;
363 sub discord_channel_create {
364 my ($self, $gw, $chan) = @_;
366 for my $key (keys %{$chan}) {
367 say localtime(time) . " - $key - $chan->{$key}";
369 $self->discord_channels->{$chan->{id}} = $chan;
373 my ($self, $c, $msg) = @_;
375 my ($p,$command,$args) = ($msg =~ /^([.!])(\S+)(?: (.+))?/);
377 if ($msg =~ m{https?://[\w.]+/.+?scan(_id|_grp)?=(\w+)}){
378 if (!$command || $command =~ m{^https?://}){
379 ($p,$command,$args) = ('.','addscan',$msg);
380 }elsif($command ne 'addscan'){
381 $self->handleCommand ($c, ".addscan $msg")
385 return 0 unless $self->disp->has_command($command,$c->channel);
387 say localtime(time) . " - $msg";
389 $c->dm_reply(1) if $p eq '!';
391 return $self->disp->run_command($c,$command,$args);
395 my ($self, $msg, $server, $nick, $address, $channel, $model) = @_;
397 return if $channel !~ /^#/ && $msg =~ /^~/;
398 $msg = ".$msg" if $channel !~ /^#/ && $msg =~ /^[^.!]/;
400 my ($p,$command,$args) = ($msg =~ /^([.!~])(\S+)(?: (.+))?/);
402 if ($msg =~ m{https?://[\w.]+/.+?scan(_id|_grp)?=(\w+)}){
403 if (!$command || $command =~ m{^https?://}){
404 ($p,$command,$args) = ('.','addscan',$msg);
405 }elsif($command ne 'addscan'){
406 $self->parseCommand (".addscan $msg", $server, $nick, $address, $channel, $model)
410 return 0 unless $self->disp->has_command($command,$channel);
415 $reply_string = "privmsg $nick";
418 $reply_string = "privmsg $channel";
421 $reply_string = "notice $nick";
425 $address =~ s/.*@(.*)/$1/;
426 my $c = NDIRC::IrcContext->new({
434 reply_string => $reply_string,
437 return $self->disp->run_command($c,$command,$args);
441 my ($self, $target, $msg) = @_;
443 return unless exists $self->targets->{$target};
445 $self->message($msg, @{$self->targets->{$target}});
450 my ($self, $msg, @targets) = @_;
453 when (/^D-(\d+)$/i) {
454 $self->discordMessage($1, $msg);
457 $self->ircMessage(privmsg => $_, $msg);
463 my ($self, $command, $target, $msg) = @_;
465 $msg =~ s`<b>(.*?)</b>`${\(chr(2))}$1${\(chr(15))}`gi;
466 $msg =~ s`<c(\d+)>(.*?)</c>`${\(chr(3))}$1$2${\(chr(15))}`gi;
468 $self->irc->yield($command, $target, $msg);
472 my ($self, $target, $msg) = @_;
474 $msg =~ s`<b>(.*?)</b>`**$1**`gi;
475 $msg =~ s`<c(\d+)>(.*?)</c>`*$2*`gi;
477 $self->discord->send_message($target, $msg );