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";
95 my ($self,$kernel,$heap,$session) = @_[OBJECT,KERNEL,HEAP,SESSION];
97 # retrieve our component's object from the heap where we stashed it
98 my $irc = $heap->{irc};
99 $kernel->sig( DIE => 'sig_DIE' );
100 $kernel->sig( USR1 => 'sig_usr1' );
101 $kernel->sig( USR2 => 'sig_usr2' );
102 $kernel->sig( INT => 'signal_handler' );
104 $irc->plugin_add( 'NickReclaim', POE::Component::IRC::Plugin::NickReclaim->new() );
105 $irc->plugin_add( 'AutoJoin', POE::Component::IRC::Plugin::NickReclaim->new() );
106 $irc->plugin_add( 'BotTraffic', POE::Component::IRC::Plugin::BotTraffic->new() );
107 $irc->plugin_add('Logger', POE::Component::IRC::Plugin::Logger->new(
114 Strip_formatting => 1,
118 $heap->{connector} = POE::Component::IRC::Plugin::Connector->new(
119 servers => [['irc.netgamers.org'], ['underworld.no.eu.netgamers.org']
120 ,['firefly.no.eu.netgamers.org'], ['underworld.ca.us.netgamers.org'] ]
122 $irc->plugin_add( 'Connector' => $heap->{connector} );
124 $irc->yield( register => 'all' );
125 $irc->yield( connect => { server => 'irc.netgamers.org' } );
127 $kernel->delay( refresh => 60 );
129 if (my $f = new IO::File 'discord'){
135 $self->discord(Mojo::Discord->new(
138 'url' => 'https://nd.ruin.nu',
141 'READY' => sub { $self->discord_ready(@_) },
142 'MESSAGE_CREATE' => sub { $self->discord_message_create(@_) },
143 'GUILD_CREATE' => sub { $self->discord_guild_create(@_) },
144 'CHANNEL_CREATE' => sub { $self->discord_channel_create(@_) },
149 $self->discord->init();
157 if (my $f = new IO::File 'auth'){
162 $heap->{irc}->yield(qbot_auth => $user => $pass);
167 my ($kernel,$heap) = @_[KERNEL,HEAP];
169 $kernel->yield( 'refresh' );
172 sub clear_constraint {
176 if (ref $tc eq 'MooseX::Meta::TypeConstraint::Structured'){
177 for my $t (@{$tc->{type_constraints}}){
178 clear_constraint($t);
182 if (ref $tc eq 'Moose::Meta::TypeConstraint::Parameterized'){
183 clear_constraint($tc->{type_parameter});
185 last if ref $tc eq 'HASH';
186 last if ref $tc eq '';
187 if (defined $tc->{_type_constraint}){
188 $tc = $tc->{_type_constraint};
189 }elsif(defined $tc->{__type_constraint}){
190 $tc = $tc->{__type_constraint};
200 for my $a (@{$ins->{attributes}}){
201 for my $m (@{$a->{associated_methods}}){
204 clear_constraint($a->{isa});
211 for my $m (values %{$c->meta->{methods}}){
212 clear_constraint($m->{type_constraint});
214 my $ps = $m->{parsed_signature};
215 for my $p (@{$ps->{_positional_params}->{params}}){
216 clear_metains($p->{__MOP__}->{_meta_instance});
221 clear_metains($c->meta->{_meta_instance});
228 for my $c (values %{$self->disp->commands}){
232 $self->disp($self->_build_disp);
237 my $disp = new NDIRC::Dispatcher;
239 if (my $commands = new IO::File 'commands'){
240 my @commands = split /\W+/, do{local $/; <$commands>};
241 say "Loading commands from: @commands";
242 $disp->load(@commands);
245 %{$self->targets} = ();
246 my $channels = new IO::File 'channels' or die $!;;
248 my ($chan, @types) = split /\s+/;
249 say "$chan - @types";
250 if ($chan =~ /^(.*):(.*)$/){
252 $self->targets->{$2} = [] unless exists $self->targets->{$2};
253 push @{$self->targets->{$2}},$chan;
254 say "$2 - @{$self->targets->{$2}}";
256 $disp->add_channel($chan,\@types);
263 my( $kernel,$sig, $ex ) = @_[ KERNEL,ARG0, ARG1 ];
264 say "DIED!!!!!!!!!!!!!!";
266 # $ex is the exception hash
267 warn "$$: error in event: $ex->{error_str}";
268 $kernel->sig_handled();
270 # Send the signal to session that sent the original event.
271 #if( $ex->{source_session} ne $_[SESSION] ) {
272 #$kernel->signal( $ex->{source_session}, 'DIE', $sig, $ex );
277 my ($kernel, $signal_name, $heap) = @_[KERNEL, ARG0, HEAP];
278 print "First session caught SIG$signal_name\n";
282 exit unless $heap->{irc}->connected;
284 $heap->{irc}->yield(quit => 'Bye!');
285 $kernel->sig_handled();
288 #$kernel->sig_handled();
291 sub irc_disconnected {
292 my ($sender,$heap) = @_[SENDER,HEAP];
294 exit if $heap->{INT};
298 my ($self,$sender,$kernel) = @_[OBJECT,SENDER,KERNEL];
300 # Since this is an irc_* event, we can get the component's object by
301 # accessing the heap of the sender. Then we register and connect to the
303 my $irc = $sender->get_heap();
305 print "Connected to ", $irc->server_name(), "\n";
307 $kernel->yield( 'auth' );
308 $irc->yield( mode => $irc->nick_name, '+ix');
310 # we join our channels
311 $irc->yield( join => $_ ) for grep /^#/, keys %{$self->disp->channels};
316 my ($self,$sender, $who, $channel) = @_[OBJECT,SENDER, ARG0 .. ARG1];
317 my $irc = $sender->get_heap();
319 $irc->yield( join => $_ ) for grep /^$channel$/i, keys %{$self->disp->channels}
323 $_[ARG2] = irc_to_utf8 $_[ARG2];
327 $_[ARG2] = irc_to_utf8 $_[ARG2];
339 $self->discord_id($hash->{user}{id});
340 $self->discord_name($hash->{user}{username});
342 say localtime(time) . " - Connected to Discord. $self->{discord_id}";
345 sub discord_message_create {
348 sub discord_guild_create {
352 for my $chan (@{$hash->{channels}}) {
353 say localtime(time) . " - $chan->{id} - $chan->{name}";
354 $self->discord_channels->{$chan->{id}} = $chan;
358 sub discord_channel_create {
362 for my $key (keys %{$chan}) {
363 say localtime(time) . " - $key - $chan->{$key}";
365 $self->discord_channels->{$chan->{id}} = $chan;
369 my ($self, $msg, $server, $nick, $address, $channel, $model) = @_;
371 return if $channel !~ /^#/ && $msg =~ /^~/;
372 $msg = ".$msg" if $channel !~ /^#/ && $msg =~ /^[^.!]/;
374 my ($p,$command,$args) = ($msg =~ /^([.!~])(\S+)(?: (.+))?/);
376 if ($msg =~ m{https?://[\w.]+/.+?scan(_id|_grp)?=(\w+)}){
377 if (!$command || $command =~ m{^https?://}){
378 ($p,$command,$args) = ('.','addscan',$msg);
379 }elsif($command ne 'addscan'){
380 $self->parseCommand (".addscan $msg", $server, $nick, $address, $channel, $model)
384 return 0 unless $self->disp->has_command($command,$channel);
389 $reply_string = "privmsg $nick";
392 $reply_string = "privmsg $channel";
395 $reply_string = "notice $nick";
399 $address =~ s/.*@(.*)/$1/;
400 my $c = NDIRC::IrcContext->new({
407 reply_string => $reply_string,
410 return $self->disp->run_command($c,$command,$args);
414 my ($self, $target, $msg) = @_;
416 return unless exists $self->targets->{$target};
418 my $session = $poe_kernel->get_active_session();
419 my $heap = $session->get_heap();
420 my $irc = $heap->{irc};
423 for (@{$self->targets->{$target}}) {
425 $irc->yield(privmsg => $_, $msg);
428 $self->discord->send_message($1, $msg );