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/;
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;
38 use NDIRC::Dispatcher;
39 use NDIRC::IrcContext;
64 has discord_channels => (
70 # We registered for all events, this will produce some debug info.
72 my ($event, $args) = @_[ARG0 .. $#_];
73 my @output = ( "$event: " );
75 for my $arg (@$args) {
76 if ( ref $arg eq 'ARRAY' ) {
77 push( @output, '[' . join(', ', @$arg ) . ']' );
80 push ( @output, "'$arg'" );
83 print join ' ', @output, "\n";
88 my ($self,$kernel,$heap,$session) = @_[OBJECT,KERNEL,HEAP,SESSION];
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' );
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(
107 Strip_formatting => 1,
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'] ]
115 $irc->plugin_add( 'Connector' => $heap->{connector} );
117 $irc->yield( register => 'all' );
118 $irc->yield( connect => { server => 'irc.netgamers.org' } );
120 $kernel->delay( refresh => 60 );
122 if (my $f = new IO::File 'discord'){
128 $self->discord(Mojo::Discord->new(
131 'url' => 'https://nd.ruin.nu',
134 'READY' => sub { $self->discord_ready(@_) },
135 'MESSAGE_CREATE' => sub { $self->discord_message_create(@_) },
136 'GUILD_CREATE' => sub { $self->discord_guild_create(@_) },
137 'CHANNEL_CREATE' => sub { $self->discord_channel_create(@_) },
142 $self->discord->init();
150 if (my $f = new IO::File 'auth'){
155 $heap->{irc}->yield(qbot_auth => $user => $pass);
160 my ($kernel,$heap) = @_[KERNEL,HEAP];
162 $kernel->yield( 'refresh' );
165 sub clear_constraint {
169 if (ref $tc eq 'MooseX::Meta::TypeConstraint::Structured'){
170 for my $t (@{$tc->{type_constraints}}){
171 clear_constraint($t);
175 if (ref $tc eq 'Moose::Meta::TypeConstraint::Parameterized'){
176 clear_constraint($tc->{type_parameter});
178 last if ref $tc eq 'HASH';
179 last if ref $tc eq '';
180 if (defined $tc->{_type_constraint}){
181 $tc = $tc->{_type_constraint};
182 }elsif(defined $tc->{__type_constraint}){
183 $tc = $tc->{__type_constraint};
193 for my $a (@{$ins->{attributes}}){
194 for my $m (@{$a->{associated_methods}}){
197 clear_constraint($a->{isa});
204 for my $m (values %{$c->meta->{methods}}){
205 clear_constraint($m->{type_constraint});
207 my $ps = $m->{parsed_signature};
208 for my $p (@{$ps->{_positional_params}->{params}}){
209 clear_metains($p->{__MOP__}->{_meta_instance});
214 clear_metains($c->meta->{_meta_instance});
221 for my $c (values %{$self->disp->commands}){
225 $self->disp($self->_build_disp);
230 my $disp = new NDIRC::Dispatcher;
232 if (my $commands = new IO::File 'commands'){
233 my @commands = split /\W+/, do{local $/; <$commands>};
234 say "Loading commands from: @commands";
235 $disp->load(@commands);
238 my $channels = new IO::File 'channels' or die $!;;
240 my ($chan, @types) = split /\s+/;
241 say "$chan - @types";
242 if ($chan =~ /^(.*):(.*)$/){
244 $disp->set_target($2,$chan);
246 $disp->add_channel($chan,\@types);
253 my( $kernel,$sig, $ex ) = @_[ KERNEL,ARG0, ARG1 ];
254 say "DIED!!!!!!!!!!!!!!";
256 # $ex is the exception hash
257 warn "$$: error in event: $ex->{error_str}";
258 $kernel->sig_handled();
260 # Send the signal to session that sent the original event.
261 #if( $ex->{source_session} ne $_[SESSION] ) {
262 #$kernel->signal( $ex->{source_session}, 'DIE', $sig, $ex );
267 my ($kernel, $signal_name, $heap) = @_[KERNEL, ARG0, HEAP];
268 print "First session caught SIG$signal_name\n";
272 exit unless $heap->{irc}->connected;
274 $heap->{irc}->yield(quit => 'Bye!');
275 $kernel->sig_handled();
278 #$kernel->sig_handled();
281 sub irc_disconnected {
282 my ($sender,$heap) = @_[SENDER,HEAP];
284 exit if $heap->{INT};
288 my ($self,$sender,$kernel) = @_[OBJECT,SENDER,KERNEL];
290 # Since this is an irc_* event, we can get the component's object by
291 # accessing the heap of the sender. Then we register and connect to the
293 my $irc = $sender->get_heap();
295 print "Connected to ", $irc->server_name(), "\n";
297 $kernel->yield( 'auth' );
298 $irc->yield( mode => $irc->nick_name, '+ix');
300 # we join our channels
301 $irc->yield( join => $_ ) for grep /^#/, keys %{$self->disp->channels};
306 my ($self,$sender, $who, $channel) = @_[OBJECT,SENDER, ARG0 .. ARG1];
307 my $irc = $sender->get_heap();
309 $irc->yield( join => $_ ) for grep /^$channel$/i, keys %{$self->disp->channels}
313 $_[ARG2] = irc_to_utf8 $_[ARG2];
317 $_[ARG2] = irc_to_utf8 $_[ARG2];
329 $self->discord_id($hash->{user}{id});
330 $self->discord_name($hash->{user}{username});
332 say localtime(time) . " - Connected to Discord. $self->{discord_id}";
335 sub discord_message_create {
338 sub discord_guild_create {
342 for my $chan (@{$hash->{channels}}) {
343 say localtime(time) . " - $chan->{id} - $chan->{name}";
344 $self->discord_channels->{$chan->{id}} = $chan;
348 sub discord_channel_create {
352 for my $key (keys %{$chan}) {
353 say localtime(time) . " - $key - $chan->{$key}";
355 $self->discord_channels->{$chan->{id}} = $chan;
359 my ($self, $msg, $server, $nick, $address, $channel, $model) = @_;
361 return if $channel !~ /^#/ && $msg =~ /^~/;
362 $msg = ".$msg" if $channel !~ /^#/ && $msg =~ /^[^.!]/;
364 my ($p,$command,$args) = ($msg =~ /^([.!~])(\S+)(?: (.+))?/);
366 if ($msg =~ m{https?://[\w.]+/.+?scan(_id|_grp)?=(\w+)}){
367 if (!$command || $command =~ m{^https?://}){
368 ($p,$command,$args) = ('.','addscan',$msg);
369 }elsif($command ne 'addscan'){
370 $self->parseCommand (".addscan $msg", $server, $nick, $address, $channel, $model)
374 return 0 unless $self->disp->has_command($command,$channel);
379 $reply_string = "privmsg $nick";
382 $reply_string = "privmsg $channel";
385 $reply_string = "notice $nick";
389 $address =~ s/.*@(.*)/$1/;
390 my $c = NDIRC::IrcContext->new({
397 reply_string => $reply_string,
400 return $self->disp->run_command($c,$command,$args);