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 => [['irc.netgamers.org'], ['underworld.no.eu.netgamers.org']
125 ,['firefly.no.eu.netgamers.org'], ['underworld.ca.us.netgamers.org'] ]
127 $irc->plugin_add( 'Connector' => $heap->{connector} );
129 $irc->yield( register => 'all' );
130 $irc->yield( connect => { server => 'irc.netgamers.org' } );
132 $kernel->delay( refresh => 60 );
134 if (my $f = new IO::File 'discord'){
140 $self->discord(Mojo::Discord->new(
143 'url' => 'https://nd.ruin.nu',
146 'READY' => sub { $self->discord_ready(@_) },
147 'MESSAGE_CREATE' => sub { $self->discord_message_create(@_) },
148 'GUILD_CREATE' => sub { $self->discord_guild_create(@_) },
149 'CHANNEL_CREATE' => sub { $self->discord_channel_create(@_) },
154 $self->discord->init();
162 if (my $f = new IO::File 'auth'){
167 $heap->{irc}->yield(qbot_auth => $user => $pass);
172 my ($kernel,$heap) = @_[KERNEL,HEAP];
174 $kernel->yield( 'refresh' );
177 sub clear_constraint {
181 if (ref $tc eq 'MooseX::Meta::TypeConstraint::Structured'){
182 for my $t (@{$tc->{type_constraints}}){
183 clear_constraint($t);
187 if (ref $tc eq 'Moose::Meta::TypeConstraint::Parameterized'){
188 clear_constraint($tc->{type_parameter});
190 last if ref $tc eq 'HASH';
191 last if ref $tc eq '';
192 if (defined $tc->{_type_constraint}){
193 $tc = $tc->{_type_constraint};
194 }elsif(defined $tc->{__type_constraint}){
195 $tc = $tc->{__type_constraint};
205 for my $a (@{$ins->{attributes}}){
206 for my $m (@{$a->{associated_methods}}){
209 clear_constraint($a->{isa});
216 for my $m (values %{$c->meta->{methods}}){
217 clear_constraint($m->{type_constraint});
219 my $ps = $m->{parsed_signature};
220 for my $p (@{$ps->{_positional_params}->{params}}){
221 clear_metains($p->{__MOP__}->{_meta_instance});
226 clear_metains($c->meta->{_meta_instance});
233 for my $c (values %{$self->disp->commands}){
237 $self->disp($self->_build_disp);
242 my $disp = new NDIRC::Dispatcher;
244 if (my $commands = new IO::File 'commands'){
245 my @commands = split /\W+/, do{local $/; <$commands>};
246 say "Loading commands from: @commands";
247 $disp->load(@commands);
250 %{$self->targets} = ();
251 my $channels = new IO::File 'channels' or die $!;;
253 my ($chan, @types) = split /\s+/;
254 say "$chan - @types";
255 if ($chan =~ /^(.*):(.*)$/){
257 $self->targets->{$2} = [] unless exists $self->targets->{$2};
258 push @{$self->targets->{$2}},$chan;
259 say "$2 - @{$self->targets->{$2}}";
261 $disp->add_channel($chan,\@types);
268 my( $kernel,$sig, $ex ) = @_[ KERNEL,ARG0, ARG1 ];
269 say "DIED!!!!!!!!!!!!!!";
271 # $ex is the exception hash
272 warn "$$: error in event: $ex->{error_str}";
273 $kernel->sig_handled();
275 # Send the signal to session that sent the original event.
276 #if( $ex->{source_session} ne $_[SESSION] ) {
277 #$kernel->signal( $ex->{source_session}, 'DIE', $sig, $ex );
282 my ($kernel, $signal_name, $heap) = @_[KERNEL, ARG0, HEAP];
283 print "First session caught SIG$signal_name\n";
287 exit unless $heap->{irc}->connected;
289 $heap->{irc}->yield(quit => 'Bye!');
290 $kernel->sig_handled();
293 #$kernel->sig_handled();
296 sub irc_disconnected {
297 my ($sender,$heap) = @_[SENDER,HEAP];
299 exit if $heap->{INT};
303 my ($self,$sender,$kernel) = @_[OBJECT,SENDER,KERNEL];
305 # Since this is an irc_* event, we can get the component's object by
306 # accessing the heap of the sender. Then we register and connect to the
308 my $irc = $sender->get_heap();
310 print "Connected to ", $irc->server_name(), "\n";
312 $kernel->yield( 'auth' );
313 $irc->yield( mode => $irc->nick_name, '+ix');
315 # we join our channels
316 $irc->yield( join => $_ ) for grep /^#/, keys %{$self->disp->channels};
321 my ($self,$sender, $who, $channel) = @_[OBJECT,SENDER, ARG0 .. ARG1];
322 my $irc = $sender->get_heap();
324 $irc->yield( join => $_ ) for grep /^$channel$/i, keys %{$self->disp->channels}
328 $_[ARG2] = irc_to_utf8 $_[ARG2];
332 $_[ARG2] = irc_to_utf8 $_[ARG2];
344 $self->discord_id($hash->{user}{id});
345 $self->discord_name($hash->{user}{username});
347 say localtime(time) . " - Connected to Discord. $self->{discord_id}";
350 sub discord_message_create {
353 sub discord_guild_create {
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 {
367 for my $key (keys %{$chan}) {
368 say localtime(time) . " - $key - $chan->{$key}";
370 $self->discord_channels->{$chan->{id}} = $chan;
374 my ($self, $msg, $server, $nick, $address, $channel, $model) = @_;
376 return if $channel !~ /^#/ && $msg =~ /^~/;
377 $msg = ".$msg" if $channel !~ /^#/ && $msg =~ /^[^.!]/;
379 my ($p,$command,$args) = ($msg =~ /^([.!~])(\S+)(?: (.+))?/);
381 if ($msg =~ m{https?://[\w.]+/.+?scan(_id|_grp)?=(\w+)}){
382 if (!$command || $command =~ m{^https?://}){
383 ($p,$command,$args) = ('.','addscan',$msg);
384 }elsif($command ne 'addscan'){
385 $self->parseCommand (".addscan $msg", $server, $nick, $address, $channel, $model)
389 return 0 unless $self->disp->has_command($command,$channel);
394 $reply_string = "privmsg $nick";
397 $reply_string = "privmsg $channel";
400 $reply_string = "notice $nick";
404 $address =~ s/.*@(.*)/$1/;
405 my $c = NDIRC::IrcContext->new({
413 reply_string => $reply_string,
416 return $self->disp->run_command($c,$command,$args);
420 my ($self, $target, $msg) = @_;
422 return unless exists $self->targets->{$target};
424 $self->message($msg, @{$self->targets->{$target}});
429 my ($self, $msg, @targets) = @_;
433 $self->discordMessage($1, $msg);
436 $self->ircMessage(privmsg => $_, $msg);
442 my ($self, $command, $target, $msg) = @_;
444 $msg =~ s`<b>(.*?)</b>`${\(chr(2))}$1${\(chr(15))}`gi;
445 $msg =~ s`<c(\d+)>(.*?)</c>`${\(chr(3))}$1$2${\(chr(15))}`gi;
447 $self->irc->yield($command, $target, $msg);
451 my ($self, $target, $msg) = @_;
453 $msg =~ s`<b>(.*?)</b>`**$1**`gi;
454 $msg =~ s`<c(\d+)>(.*?)</c>`*$2*`gi;
456 $self->discord->send_message($target, $msg );