From: Michael Andreen Date: Sun, 16 Aug 2009 16:19:13 +0000 (+0200) Subject: Basic conversion to POE::Component::IRC X-Git-Url: https://ruin.nu/git/?p=NDIRC.git;a=commitdiff_plain;h=e8c94cdebefdc428ea92fde6db63f18d3e8399b2 Basic conversion to POE::Component::IRC --- diff --git a/Commands/Channel.pm b/Commands/Channel.pm index d309ce0..9207b0c 100644 --- a/Commands/Channel.pm +++ b/Commands/Channel.pm @@ -33,8 +33,9 @@ sub op { my ($self,$c,$msg) = @_; - $self->name =~ /(op|voice)/; - my @access = ($1); + $self->name =~ /(de)?(op|voice)/; + my @access = ($2); + my $mod = $1 ? '-' : ''; if ($msg =~ /^\s*$/){ $msg = $c->nick; @@ -42,19 +43,19 @@ sub op } my $mode = q{ -SELECT DISTINCT channel FROM users u +SELECT DISTINCT LOWER(flag) FROM users u JOIN groupmembers g USING (uid) JOIN channel_group_flags gf USING (gid) JOIN channel_flags f USING (flag) WHERE u.hostmask ILIKE $1 AND channel = $2 AND f.name = ANY($3); }; if ($c->check_user_roles(qw/irc_masterop/)){ - $mode = 1; + $mode = substr $access[0], 0,1; }else{ ($mode) = $c->model->selectrow_array($mode,undef,$c->host,$c->channel,\@access); } if ($mode){ - $c->server->command($self->name . " " . $c->channel . " $msg"); + $c->command(mode => $c->channel, "$mod$mode", $msg); }else{ $c->reply("No access to " . $self->name . " in this channel"); } @@ -88,7 +89,7 @@ WHERE u.hostmask ILIKE $1 AND COALESCE(channel = $2,TRUE) } } for (@channels){ - $c->server->command("invite ". $c->nick ." $_"); + $c->command(invite => $c->nick, $_); } } diff --git a/Commands/Def.pm b/Commands/Def.pm index 80b02fd..e13758d 100644 --- a/Commands/Def.pm +++ b/Commands/Def.pm @@ -37,8 +37,8 @@ sub anon my ($target,$mess) = $msg =~ /^(\S+) (.*)$/ or die 'ARGS'; - $c->message("msg $target", "$mess"); - $c->message("msg ".$c->channel, "$target << $mess"); + $c->message(privmsg => $target, "$mess"); + $c->message(privmsg => $c->channel, "$target << $mess"); } sub defcall @@ -74,8 +74,8 @@ ORDER BY c.landing_tick; return; } } - $c->message("notice $ND::memchan", "DEFENSE REQUIRED!! WAKE UP!!"); - $c->message("msg $ND::memchan", "DEFENSE REQUIRED $mess $callinfo MSG " + $c->message(notice => $ND::memchan, "DEFENSE REQUIRED!! WAKE UP!!"); + $c->message(privmsg => $ND::memchan, "DEFENSE REQUIRED $mess $callinfo MSG " .$c->nick." TO RESPOND"); } diff --git a/Commands/Quotes.pm b/Commands/Quotes.pm index c2625a8..3353eb6 100644 --- a/Commands/Quotes.pm +++ b/Commands/Quotes.pm @@ -92,7 +92,7 @@ sub findquote } if ($match){ close $file; - $c->message("dcc send ".$c->nick, $file); + $c->command(dcc => $c->nick => SEND => $file); }else{ $c->reply("No quotes matching $pattern."); } diff --git a/Commands/Scans.pm b/Commands/Scans.pm index 31eea44..acfe9a9 100644 --- a/Commands/Scans.pm +++ b/Commands/Scans.pm @@ -93,7 +93,7 @@ VALUES((SELECT uid FROM users WHERE hostmask ILIKE $1),$2,$3,$4) RETURNING (id) } if ($id){ - $c->message("msg $ND::scanchan" + $c->message(privmsg => $ND::scanchan ,"$id http://game.planetarion.com/waves.pl?id=$typeid&x=$x&y=$y&z=$z" . " ($x:$y:$z $type) | <".$c->nick."> $msg" ); @@ -198,9 +198,9 @@ sub anon my ($self, $c, $msg) = @_; my ($target,$mess) = $msg =~ /^(\S+) (.*)$/ or die 'ARGS'; - $c->message("msg $target", "$mess (reply with /msg " + $c->message(privmsg => $target, "$mess (reply with /msg " .$c->channel.")"); - $c->message("msg ".$c->channel, "$target << $mess"); + $c->message(privmsg => $c->channel, "$target << $mess"); } sub addscan diff --git a/CommonStates.pm b/CommonStates.pm new file mode 100644 index 0000000..655bbba --- /dev/null +++ b/CommonStates.pm @@ -0,0 +1,123 @@ +#************************************************************************** +# Copyright (C) 2006 by Michael Andreen * +# * +# This program is free software; you can redistribute it and/or modify * +# it under the terms of the GNU General Public License as published by * +# the Free Software Foundation; either version 2 of the License, or * +# (at your option) any later version. * +# * +# This program is distributed in the hope that it will be useful, * +# but WITHOUT ANY WARRANTY; without even the implied warranty of * +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +# GNU General Public License for more details. * +# * +# You should have received a copy of the GNU General Public License * +# along with this program; if not, write to the * +# Free Software Foundation, Inc., * +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * +#**************************************************************************/ +package NDIRC::CommonStates; + +use strict; +use warnings; +use feature ':5.10'; + +use POE::Session; + +# We registered for all events, this will produce some debug info. +sub _default { + my ($event, $args) = @_[ARG0 .. $#_]; + my @output = ( "$event: " ); + + for my $arg (@$args) { + if ( ref $arg eq 'ARRAY' ) { + push( @output, '[' . join(', ', @$arg ) . ']' ); + } + else { + push ( @output, "'$arg'" ); + } + } + print join ' ', @output, "\n"; + return 0; +} + +sub _start { + my ($kernel,$heap,$session) = @_[KERNEL,HEAP,SESSION]; + + # retrieve our component's object from the heap where we stashed it + my $irc = $heap->{irc}; + $kernel->sig( DIE => 'sig_DIE' ); + $kernel->sig( USR1 => 'sig_usr1' ); + $kernel->sig( USR2 => 'sig_usr2' ); + $kernel->sig( INT => 'signal_handler' ); + + $heap->{connector} = POE::Component::IRC::Plugin::Connector->new(); + $irc->plugin_add( 'Connector' => $heap->{connector} ); + + $kernel->signal($session => 'USR2'); + + $irc->yield( register => 'all' ); + $irc->yield( connect => { } ); + + $kernel->delay( refresh => 60 ); + return; +} + +sub sig_DIE { + my( $kernel,$sig, $ex ) = @_[ KERNEL,ARG0, ARG1 ]; + say "DIED!!!!!!!!!!!!!!"; + # $sig is 'DIE' + # $ex is the exception hash + warn "$$: error in event: $ex->{error_str}"; + $kernel->sig_handled(); + + # Send the signal to session that sent the original event. + #if( $ex->{source_session} ne $_[SESSION] ) { + #$kernel->signal( $ex->{source_session}, 'DIE', $sig, $ex ); + #} +} + +sub signal_handler { + my ($kernel, $signal_name, $heap) = @_[KERNEL, ARG0, HEAP]; + print "First session caught SIG$signal_name\n"; + + $heap->{irc}->yield(privmsg => '#testarlite', "SIGNAL $signal_name!"); + + given($signal_name){ + when ('INT') { + $heap->{irc}->yield(quit => 'Bye!'); + $kernel->sig_handled(); + } + } + #$kernel->sig_handled(); +} + +sub irc_disconnected { + my ($sender,$heap) = @_[SENDER,HEAP]; + + exit; +} + +sub irc_001 { + my ($sender,$heap) = @_[SENDER,HEAP]; + + # Since this is an irc_* event, we can get the component's object by + # accessing the heap of the sender. Then we register and connect to the + # specified server. + my $irc = $sender->get_heap(); + + print "Connected to ", $irc->server_name(), "\n"; + + # we join our channels + $irc->yield( join => $_ ) for grep /^#/, keys %{$heap->{disp}->channels}; + return; +} + +sub irc_invite { + my ($sender, $heap, $who, $channel) = @_[SENDER, HEAP, ARG0 .. ARG1]; + my $irc = $sender->get_heap(); + + $irc->yield( join => $_ ) for grep /^$channel$/i, keys %{$heap->{disp}->channels} +} + +1; diff --git a/Context.pm b/Context.pm index abe59b2..02aac95 100644 --- a/Context.pm +++ b/Context.pm @@ -97,16 +97,23 @@ sub check_user_roles { sub reply { my ($self,$msg) = @_; - $self->message($self->reply_string , $msg); + my @command = split / /, $self->reply_string; + $self->message(@command, $msg); } sub message { - my ($self,$command,$msg) = @_; + my ($self,$command, $target, $msg) = @_; $msg =~ s`(.*?)`${\(chr(2))}$1${\(chr(15))}`gi; $msg =~ s`(.*?)`${\(chr(3))}$1$2${\(chr(15))}`gi; - $self->server->command("$command $msg"); + $self->command($command, $target, $msg); +} + +sub command { + my ($self,@command) = @_; + + $self->server->yield(@command); } sub intel_log { diff --git a/Misc.pm b/Misc.pm index a08ae76..bada451 100644 --- a/Misc.pm +++ b/Misc.pm @@ -61,10 +61,10 @@ sub parseCommand { my $reply_string; given ($p){ when ('!'){ - $reply_string = "msg $nick"; + $reply_string = "privmsg $nick"; } when ('~'){ - $reply_string = "msg $channel"; + $reply_string = "privmsg $channel"; } default { $reply_string = "notice $nick"; diff --git a/ndawn.pl b/ndawn.pl old mode 100644 new mode 100755 index 0de8351..e1b08a5 --- a/ndawn.pl +++ b/ndawn.pl @@ -1,3 +1,5 @@ +#!/usr/bin/perl -w + #************************************************************************** # Copyright (C) 2009 by Michael Andreen * # * @@ -15,96 +17,152 @@ # along with this program; if not, write to the * # Free Software Foundation, Inc., * # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * -#**************************************************************************/ +#************************************************************************** use strict; use warnings; -use vars qw($VERSION %IRSSI); - -use Irssi; -use Irssi::Irc; - -$VERSION = "0.1"; -%IRSSI = ( - authors => "harv", - contact => "harv\@ruin.nu", - name => "NewDawn testbot script", - description => "Testbot used for implementing commands for nd bots", - license => "GPL-2 or (at your option) any later version", - url => "", - changed => "", +use feature ':5.10'; +use POE qw(Component::IRC::Qnet::State); + +use POE::Component::IRC::Plugin::Logger; +use POE::Component::IRC::Plugin::BotTraffic; +use POE::Component::IRC::Plugin::Connector; +use POE::Component::IRC::Plugin::DCC; + +use ND::DB; +use ND::Include; +use NDIRC::Dispatcher; +use NDIRC::Context; +use NDIRC::Command; +use NDIRC::Misc; +use NDIRC::CommonStates; + +my $nickname = 'ndbot'; +my $ircname = 'ND test bot'; +my $server = 'irc.netgamers.org'; + +# We create a new PoCo-IRC object +my $irc = POE::Component::IRC::Qnet::State->spawn( + nick => $nickname, + ircname => $ircname, + server => $server, + servers => [$server, 'underworld.no.eu.netgamers.org', ] +) or die "Oh noooo! $!"; + +$irc->service_bots(QBOT => 'P@cservice.netgamers.org'); + +$irc->plugin_add( 'BotTraffic', POE::Component::IRC::Plugin::BotTraffic->new() ); +$irc->plugin_add( 'DCC', POE::Component::IRC::Plugin::DCC->new() ); +$irc->plugin_add('Logger', POE::Component::IRC::Plugin::Logger->new( + Path => 'irclogs', + DCC => 0, + Private => 1, + Public => 1, + Sort_by_date => 1, + Strip_color => 1, + Strip_formatting => 1, +)); + +my $DBH = DB(); +my $TICK = $DBH->selectrow_array('SELECT tick()'); + +$ND::scanchan = '#testarmer'; +$ND::defchan = '#testarlite'; +$ND::memchan = '#testarmer'; + +POE::Session->create( + package_states => [ + 'NDIRC::CommonStates' => [ qw(_default _start irc_001 sig_DIE signal_handler irc_disconnected irc_invite) ], + main => [ qw(irc_public irc_msg refresh sig_usr2 irc_join) ], + ], + heap => { irc => $irc}, ); -$ND::refresh = 0; +$poe_kernel->run(); -$SIG{USR1} = sub{ - print "USR1"; - $ND::refresh = 1; -}; +sub sig_usr2 { + my $heap = $_[HEAP]; -use again 'ND::DB'; -use again 'ND::Include'; -use again 'NDIRC::Dispatcher'; -use again 'NDIRC::Context'; -use again 'NDIRC::Command'; -use again 'NDIRC::Misc'; + my $disp = new NDIRC::Dispatcher; -my $DBH = DB(); -my $TICK = $DBH->selectrow_array('SELECT tick()'); + $disp->load(qw/Basic SMS Channel Def Intel Members PA Quotes Usermgm/); -my $disp = new NDIRC::Dispatcher; + open CHANNELS, 'channels'; + while (){ + print; + my ($chan, @types) = split /\s+/; + say "$chan - @types"; + $disp->add_channel($chan,\@types); + } + close CHANNELS; -$disp->load(qw/Basic SMS Channel Def Intel Members PA Quotes Scans Usermgm/); + $heap->{disp} = $disp; +} -$ND::scanchan = '#testarmer'; -$ND::defchan = '#testarlite'; -$ND::memchan = '#testarmer'; -$disp->add_channel($ND::defchan, ['pub','help','channel','def']); -$disp->add_channel($ND::scanchan, ['pub','help','channel','scan','member']); -$disp->add_channel('pm', ['pub','help','pm']); - -sub event_pubmsg { - my ($server, $msg, $nick, $address, $channel) = @_; - - eval { - if ($msg =~ /^(\S+): (.+)$/ && $disp->has_command('anon',$channel)){ - my $_ = $1; - my $text = $2; - my $channel = $server->channel_find($channel); - my $nick = $channel->nick_find($1); - unless ($nick || /(Constructing|Researching)/){ - print ".anon $_ $text"; - $msg = ".anon $_ $text"; - } +sub irc_public { + my ($sender, $heap, $who, $where, $msg) = @_[SENDER, HEAP, ARG0 .. ARG2]; + my ($nick,$username,$address) = ( split /[!@]/, $who ); + my $channel = $where->[0]; + + my $irc = $sender->get_heap(); + + #$irc->yield(privmsg => $channel, "Authed? " . $irc->is_nick_authed($nick)); + + if ($msg =~ /^(\S+): (.+)$/ && $heap->{disp}->has_command('anon',$channel)){ + my $_ = $1; + my $text = $2; + unless ($irc->is_channel_member($channel,$1) || /(Constructing|Researching)/){ + $msg = ".anon $_ $text"; } - if (parseCommand($msg,$server,$nick,$address,$channel,$disp,DB())){ - #Command parsed and run successfully - } - }; - print $@ if $@; + + } + if (parseCommand($msg,$irc,$nick,$address,$channel,$heap->{disp},DB())){ + #Command parsed and run successfully + } } -sub event_privmsg { - my ($server, $msg, $nick, $address) = @_; - eval { - if (parseCommand($msg,$server,$nick,$address,'pm',$disp,DB())){ - #Command parsed and run successfully +sub irc_msg { + my ($sender, $heap, $who, $where, $msg) = @_[SENDER, HEAP, ARG0 .. ARG2]; + my ($nick,$username,$address) = ( split /[!@]/, $who ); + my $irc = $sender->get_heap(); + + if (parseCommand($msg,$irc,$nick,$address,'pm',$heap->{disp},DB())){ + #Command parsed and run successfully + } +} + +sub irc_join { + my ($sender, $heap, $who, $channel) = @_[SENDER, HEAP, ARG0 .. ARG1]; + my ($nick,$username,$address) = ( split /[!@]/, $who ); + my $irc = $sender->get_heap(); + + if($heap->{disp}->has_command('voice',$channel)){ + my $dbh = DB(); + my $flags = $dbh->prepare_cached(q{ +SELECT DISTINCT flag +FROM users u + JOIN groupmembers g USING (uid) + JOIN channel_group_flags gf USING (gid) +WHERE u.hostmask = $1 AND channel = $2 AND flag IN ('o','v'); + }); + $flags->execute($address, $channel); + my $mode = ''; + my @who; + while (my ($flag) = $flags->fetchrow()){ + $mode .= $flag; + push @who, $nick; } - }; - print $@ if $@; + say "$mode - @who"; + $irc->yield(mode => $channel, $mode, @who) if $mode; + } } sub refresh { - print "SiG" if $ND::refresh; - $ND::refresh = 0; + my ($kernel,$heap) = @_[KERNEL,HEAP]; + print 'Time: ' . time() . ' Lag: ' . $heap->{connector}->lag() . "\n"; + $kernel->delay( refresh => 60 ); + return; } - -Irssi::timeout_add(60*1000, 'refresh', undef); -Irssi::timeout_add(1000, sub{refresh if $ND::refresh}, undef); -Irssi::signal_add('message public','event_pubmsg'); -Irssi::signal_add('message private','event_privmsg'); - -