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(@_) },
141 $self->discord->init();
149 if (my $f = new IO::File 'auth'){
154 $heap->{irc}->yield(qbot_auth => $user => $pass);
159 my ($kernel,$heap) = @_[KERNEL,HEAP];
161 $kernel->yield( 'refresh' );
164 sub clear_constraint {
168 if (ref $tc eq 'MooseX::Meta::TypeConstraint::Structured'){
169 for my $t (@{$tc->{type_constraints}}){
170 clear_constraint($t);
174 if (ref $tc eq 'Moose::Meta::TypeConstraint::Parameterized'){
175 clear_constraint($tc->{type_parameter});
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};
192 for my $a (@{$ins->{attributes}}){
193 for my $m (@{$a->{associated_methods}}){
196 clear_constraint($a->{isa});
203 for my $m (values %{$c->meta->{methods}}){
204 clear_constraint($m->{type_constraint});
206 my $ps = $m->{parsed_signature};
207 for my $p (@{$ps->{_positional_params}->{params}}){
208 clear_metains($p->{__MOP__}->{_meta_instance});
213 clear_metains($c->meta->{_meta_instance});
220 for my $c (values %{$self->disp->commands}){
224 $self->disp($self->_build_disp);
229 my $disp = new NDIRC::Dispatcher;
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);
237 my $channels = new IO::File 'channels' or die $!;;
239 my ($chan, @types) = split /\s+/;
240 say "$chan - @types";
241 if ($chan =~ /^(.*):(.*)$/){
243 $disp->set_target($2,$chan);
245 $disp->add_channel($chan,\@types);
252 my( $kernel,$sig, $ex ) = @_[ KERNEL,ARG0, ARG1 ];
253 say "DIED!!!!!!!!!!!!!!";
255 # $ex is the exception hash
256 warn "$$: error in event: $ex->{error_str}";
257 $kernel->sig_handled();
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 );
266 my ($kernel, $signal_name, $heap) = @_[KERNEL, ARG0, HEAP];
267 print "First session caught SIG$signal_name\n";
271 exit unless $heap->{irc}->connected;
273 $heap->{irc}->yield(quit => 'Bye!');
274 $kernel->sig_handled();
277 #$kernel->sig_handled();
280 sub irc_disconnected {
281 my ($sender,$heap) = @_[SENDER,HEAP];
283 exit if $heap->{INT};
287 my ($self,$sender,$kernel) = @_[OBJECT,SENDER,KERNEL];
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
292 my $irc = $sender->get_heap();
294 print "Connected to ", $irc->server_name(), "\n";
296 $kernel->yield( 'auth' );
297 $irc->yield( mode => $irc->nick_name, '+ix');
299 # we join our channels
300 $irc->yield( join => $_ ) for grep /^#/, keys %{$self->disp->channels};
305 my ($self,$sender, $who, $channel) = @_[OBJECT,SENDER, ARG0 .. ARG1];
306 my $irc = $sender->get_heap();
308 $irc->yield( join => $_ ) for grep /^$channel$/i, keys %{$self->disp->channels}
312 $_[ARG2] = irc_to_utf8 $_[ARG2];
316 $_[ARG2] = irc_to_utf8 $_[ARG2];
328 $self->discord_id($hash->{user}{id});
329 $self->discord_name($hash->{user}{username});
331 say localtime(time) . " - Connected to Discord. $self->{discord_id}";
334 sub discord_message_create {
337 sub discord_guild_create {
341 for my $chan (@{$hash->{channels}}) {
342 say localtime(time) . "$chan->{id} - $chan->{name}";
343 $self->discord_channels->{$chan->{id}} = $chan->{name};
348 my ($self, $msg, $server, $nick, $address, $channel, $model) = @_;
350 return if $channel !~ /^#/ && $msg =~ /^~/;
351 $msg = ".$msg" if $channel !~ /^#/ && $msg =~ /^[^.!]/;
353 my ($p,$command,$args) = ($msg =~ /^([.!~])(\S+)(?: (.+))?/);
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)
363 return 0 unless $self->disp->has_command($command,$channel);
368 $reply_string = "privmsg $nick";
371 $reply_string = "privmsg $channel";
374 $reply_string = "notice $nick";
378 $address =~ s/.*@(.*)/$1/;
379 my $c = NDIRC::IrcContext->new({
386 reply_string => $reply_string,
389 return $self->disp->run_command($c,$command,$args);