]> ruin.nu Git - NDIRC.git/commitdiff
Basic conversion to POE::Component::IRC
authorMichael Andreen <harv@ruin.nu>
Sun, 16 Aug 2009 16:19:13 +0000 (18:19 +0200)
committerMichael Andreen <harv@ruin.nu>
Sun, 16 Aug 2009 16:19:13 +0000 (18:19 +0200)
Commands/Channel.pm
Commands/Def.pm
Commands/Quotes.pm
Commands/Scans.pm
CommonStates.pm [new file with mode: 0644]
Context.pm
Misc.pm
ndawn.pl [changed mode: 0644->0755]

index d309ce00ae1ac4e533f08f66f90101db2fe28159..9207b0c4cadf71645339da18cab0eaec2756c1bc 100644 (file)
@@ -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, $_);
        }
 }
 
index 80b02fdf454d560b161857085461c3defe577062..e13758d4797964e6bdd0d8293c8f101c1a0717b8 100644 (file)
@@ -37,8 +37,8 @@ sub anon
 
        my ($target,$mess) = $msg =~ /^(\S+) (.*)$/ or die 'ARGS';
 
-       $c->message("msg $target", "<b>$mess</b>");
-       $c->message("msg ".$c->channel, "<c03>$target << $mess</c>");
+       $c->message(privmsg => $target, "<b>$mess</b>");
+       $c->message(privmsg => $c->channel, "<c03>$target << $mess</c>");
 }
 
 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");
 }
 
index c2625a8b12fa10491d93414bfa3c071a3d1080d2..3353eb6a112df54326dd6690d6aae155823d1fa6 100644 (file)
@@ -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 <b>$pattern.</b>");
        }
index 31eea44fb9f940800b58132568c4d9cfc673f0a2..acfe9a90cbecb27f1b5012c24a7376be2093f6d5 100644 (file)
@@ -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
                                ,"<b>$id</b> 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", "<b>$mess</b> <c04>(reply with /msg "
+       $c->message(privmsg => $target, "<b>$mess</b> <c04>(reply with /msg "
                .$c->channel.")</c>");
-       $c->message("msg ".$c->channel, "<c03>$target << $mess</c>");
+       $c->message(privmsg => $c->channel, "<c03>$target << $mess</c>");
 }
 
 sub addscan
diff --git a/CommonStates.pm b/CommonStates.pm
new file mode 100644 (file)
index 0000000..655bbba
--- /dev/null
@@ -0,0 +1,123 @@
+#**************************************************************************
+#   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
+#                                                                         *
+#   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;
index abe59b24cf538b477f794153241a11649a1865b7..02aac95a7ba84819a49e6713acc7c241bec48d85 100644 (file)
@@ -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`<b>(.*?)</b>`${\(chr(2))}$1${\(chr(15))}`gi;
        $msg =~ s`<c(\d+)>(.*?)</c>`${\(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 a08ae7632f87cf096051b170302b4bb7ef5944ed..bada45151b3df42dc46c3c60f289a7937021727d 100644 (file)
--- 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";
old mode 100644 (file)
new mode 100755 (executable)
index 0de8351..e1b08a5
--- a/ndawn.pl
+++ b/ndawn.pl
@@ -1,3 +1,5 @@
+#!/usr/bin/perl -w
+
 #**************************************************************************
 #   Copyright (C) 2009 by Michael Andreen <harvATruinDOTnu>               *
 #                                                                         *
 #   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 (<CHANNELS>){
+               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');
-
-