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;
64 # We registered for all events, this will produce some debug info.
66 my ($event, $args) = @_[ARG0 .. $#_];
67 my @output = ( "$event: " );
69 for my $arg (@$args) {
70 if ( ref $arg eq 'ARRAY' ) {
71 push( @output, '[' . join(', ', @$arg ) . ']' );
74 push ( @output, "'$arg'" );
77 print join ' ', @output, "\n";
82 my ($self,$kernel,$heap,$session) = @_[OBJECT,KERNEL,HEAP,SESSION];
84 # retrieve our component's object from the heap where we stashed it
85 my $irc = $heap->{irc};
86 $kernel->sig( DIE => 'sig_DIE' );
87 $kernel->sig( USR1 => 'sig_usr1' );
88 $kernel->sig( USR2 => 'sig_usr2' );
89 $kernel->sig( INT => 'signal_handler' );
91 $irc->plugin_add( 'NickReclaim', POE::Component::IRC::Plugin::NickReclaim->new() );
92 $irc->plugin_add( 'AutoJoin', POE::Component::IRC::Plugin::NickReclaim->new() );
93 $irc->plugin_add( 'BotTraffic', POE::Component::IRC::Plugin::BotTraffic->new() );
94 $irc->plugin_add('Logger', POE::Component::IRC::Plugin::Logger->new(
101 Strip_formatting => 1,
105 $heap->{connector} = POE::Component::IRC::Plugin::Connector->new(
106 servers => [['irc.netgamers.org'], ['underworld.no.eu.netgamers.org']
107 ,['firefly.no.eu.netgamers.org'], ['underworld.ca.us.netgamers.org'] ]
109 $irc->plugin_add( 'Connector' => $heap->{connector} );
111 $irc->yield( register => 'all' );
112 $irc->yield( connect => { server => 'irc.netgamers.org' } );
114 $kernel->delay( refresh => 60 );
116 if (my $f = new IO::File 'discord'){
122 $self->discord(Mojo::Discord->new(
125 'url' => 'https://nd.ruin.nu',
128 'READY' => sub { $self->discord_ready(@_) },
129 'MESSAGE_CREATE' => sub { $self->discord_message_create(@_) },
130 'GUILD_CREATE' => sub { $self->discord_guild_create(@_) },
135 $self->discord->init();
143 if (my $f = new IO::File 'auth'){
148 $heap->{irc}->yield(qbot_auth => $user => $pass);
153 my ($kernel,$heap) = @_[KERNEL,HEAP];
155 $kernel->yield( 'refresh' );
158 sub clear_constraint {
162 if (ref $tc eq 'MooseX::Meta::TypeConstraint::Structured'){
163 for my $t (@{$tc->{type_constraints}}){
164 clear_constraint($t);
168 if (ref $tc eq 'Moose::Meta::TypeConstraint::Parameterized'){
169 clear_constraint($tc->{type_parameter});
171 last if ref $tc eq 'HASH';
172 last if ref $tc eq '';
173 if (defined $tc->{_type_constraint}){
174 $tc = $tc->{_type_constraint};
175 }elsif(defined $tc->{__type_constraint}){
176 $tc = $tc->{__type_constraint};
186 for my $a (@{$ins->{attributes}}){
187 for my $m (@{$a->{associated_methods}}){
190 clear_constraint($a->{isa});
197 for my $m (values %{$c->meta->{methods}}){
198 clear_constraint($m->{type_constraint});
200 my $ps = $m->{parsed_signature};
201 for my $p (@{$ps->{_positional_params}->{params}}){
202 clear_metains($p->{__MOP__}->{_meta_instance});
207 clear_metains($c->meta->{_meta_instance});
214 for my $c (values %{$self->disp->commands}){
218 $self->disp($self->_build_disp);
223 my $disp = new NDIRC::Dispatcher;
225 if (my $commands = new IO::File 'commands'){
226 my @commands = split /\W+/, do{local $/; <$commands>};
227 say "Loading commands from: @commands";
228 $disp->load(@commands);
231 my $channels = new IO::File 'channels' or die $!;;
233 my ($chan, @types) = split /\s+/;
234 say "$chan - @types";
235 if ($chan =~ /^(.*):(.*)$/){
237 $disp->set_target($2,$chan);
239 $disp->add_channel($chan,\@types);
246 my( $kernel,$sig, $ex ) = @_[ KERNEL,ARG0, ARG1 ];
247 say "DIED!!!!!!!!!!!!!!";
249 # $ex is the exception hash
250 warn "$$: error in event: $ex->{error_str}";
251 $kernel->sig_handled();
253 # Send the signal to session that sent the original event.
254 #if( $ex->{source_session} ne $_[SESSION] ) {
255 #$kernel->signal( $ex->{source_session}, 'DIE', $sig, $ex );
260 my ($kernel, $signal_name, $heap) = @_[KERNEL, ARG0, HEAP];
261 print "First session caught SIG$signal_name\n";
265 exit unless $heap->{irc}->connected;
267 $heap->{irc}->yield(quit => 'Bye!');
268 $kernel->sig_handled();
271 #$kernel->sig_handled();
274 sub irc_disconnected {
275 my ($sender,$heap) = @_[SENDER,HEAP];
277 exit if $heap->{INT};
281 my ($self,$sender,$kernel) = @_[OBJECT,SENDER,KERNEL];
283 # Since this is an irc_* event, we can get the component's object by
284 # accessing the heap of the sender. Then we register and connect to the
286 my $irc = $sender->get_heap();
288 print "Connected to ", $irc->server_name(), "\n";
290 $kernel->yield( 'auth' );
291 $irc->yield( mode => $irc->nick_name, '+ix');
293 # we join our channels
294 $irc->yield( join => $_ ) for grep /^#/, keys %{$self->disp->channels};
299 my ($self,$sender, $who, $channel) = @_[OBJECT,SENDER, ARG0 .. ARG1];
300 my $irc = $sender->get_heap();
302 $irc->yield( join => $_ ) for grep /^$channel$/i, keys %{$self->disp->channels}
306 $_[ARG2] = irc_to_utf8 $_[ARG2];
310 $_[ARG2] = irc_to_utf8 $_[ARG2];
322 $self->discord_id($hash->{user}{id});
323 $self->discord_name($hash->{user}{username});
325 say localtime(time) . " - Connected to Discord. $self->{discord_id}";
328 sub discord_message_create {
331 sub discord_guild_create {
335 my ($self, $msg, $server, $nick, $address, $channel, $model) = @_;
337 return if $channel !~ /^#/ && $msg =~ /^~/;
338 $msg = ".$msg" if $channel !~ /^#/ && $msg =~ /^[^.!]/;
340 my ($p,$command,$args) = ($msg =~ /^([.!~])(\S+)(?: (.+))?/);
342 if ($msg =~ m{https?://[\w.]+/.+?scan(_id|_grp)?=(\w+)}){
343 if (!$command || $command =~ m{^https?://}){
344 ($p,$command,$args) = ('.','addscan',$msg);
345 }elsif($command ne 'addscan'){
346 $self->parseCommand (".addscan $msg", $server, $nick, $address, $channel, $model)
350 return 0 unless $self->disp->has_command($command,$channel);
355 $reply_string = "privmsg $nick";
358 $reply_string = "privmsg $channel";
361 $reply_string = "notice $nick";
365 $address =~ s/.*@(.*)/$1/;
366 my $c = NDIRC::Context->new({
373 reply_string => $reply_string,
376 return $self->disp->run_command($c,$command,$args);