From 6784fd930ef6156335e0e94d2de41c7cc2011242 Mon Sep 17 00:00:00 2001 From: erdgeist <> Date: Mon, 25 Jul 2005 05:20:15 +0000 Subject: Syntax verbessert, SQL insertion schwerer gemacht --- bot.pl | 116 +++++++++++++++++++++++++++-------------------------------------- 1 file changed, 48 insertions(+), 68 deletions(-) diff --git a/bot.pl b/bot.pl index b216b17..267c107 100644 --- a/bot.pl +++ b/bot.pl @@ -3,7 +3,6 @@ use strict; use DBI; - # IRC Server stuff use POE; use POE::Component::IRC; @@ -11,8 +10,7 @@ use POE::Component::IRC; my $current_nick = "francoise"; my $channel = '#test'; -POE::Component::IRC->new("irc_client"); - +POE::Component::IRC->new("francoise"); POE::Session->new ( _start => \&irc_start, irc_join => \&irc_join, irc_part => \&irc_part, @@ -25,12 +23,12 @@ POE::Session->new ( _start => \&irc_start, irc_public => \&irc_pub_msg, irc_msg => \&irc_priv_msg, irc_ctcp_action => \&irc_action, - _default => \&_default, +# _default => \&_default, ); sub _default { if ( $_[ARG0] =~ /^irc_(.*)$/ ) { -# print "IRC $1 received\n"; + print "IRC $1 received\n"; } } @@ -54,60 +52,53 @@ my $dbh = DBI->connect("DBI:Pg:dbname='francoise'", 'francoise', 'kiffer') or die "ohoh, datenbank b0rken: $!"; sub irc_start { - # KERNEL, HEAP, and SESSION are constants exported by POE - my $kernel = $_[KERNEL]; - my $heap = $_[HEAP]; - my $session = $_[SESSION]; + my ( $kernel, $heap, $session ) = @_[KERNEL, HEAP, SESSION ]; $kernel->refcount_increment( $session->ID(), "my bot"); - $kernel->post(irc_client=> register=> "all"); - - $kernel->post(irc_client=>connect=> { Nick => $current_nick, - Username => 'francoise', - Ircname => 'francoise', - Server => 'irc.kiffer.de', - Port => '6667', - } + $kernel->post(francoise=> register=> "all"); + + $kernel->post(francoise=>connect=> { Nick => $current_nick, + Username => 'francoise', + Ircname => 'francoise', + Server => 'irc.kiffer.de', + Port => '6667', + } ); } sub irc_connect { my $kernel = $_[KERNEL]; - - $kernel->post(irc_client=>join=>$channel); + $kernel->post(francoise=>join=>$channel); } sub irc_motd { my $msg = $_[ARG1]; - print "MOTD: $msg\n"; } sub irc_names { my $kernel = $_[KERNEL]; - my $names = (split /:/, $_[ARG1])[1]; - my $channel = (split /:/, $_[ARG1])[0]; + my ( $channel, $names ) = (split /:/, $_[ARG1]); $channel =~ s/[@|=] (.*?) /$1/; - print "#-> Users on $channel [ $names ]\n"; for my $user (split / /, $names) { $user =~ s/^[@%+]//; - $kernel->post( 'irc_client', 'whois', $user); + $kernel->post( 'francoise', 'whois', $user); } } #nick change sub irc_nick { + my ( $kernel, $newnick_ ) = @_[ KERNEL, ARG1 ]; my $oldnick_ = (split /!/, $_[ARG0])[0]; - my $newnick_ = $_[ARG1]; my $oldnick = francoise_getbasenick( $oldnick_ ); my $newnick = francoise_getbasenick( $newnick_ ); if( $newnick_ ne $current_nick && $newnick ne $oldnick ) { - francoise_verifyuser( $newnick_, '', 0 ); + francoise_verifyuser( $newnick_, '', 0, $kernel ); } print "#-> $oldnick_ is now known as $newnick_\n"; @@ -117,19 +108,17 @@ sub irc_nick { sub irc_part { my $nick = (split /!/, $_[ARG0])[0]; my $channel = $_[ARG1]; - print "#-> $nick has parted $channel\n"; } #user joined sub irc_join { - my $kernel = $_[KERNEL]; + my ( $kernel, $channel ) = @_[KERNEL, ARG1]; my $nick = (split /!/, $_[ARG0])[0]; - my $channel = $_[ARG1]; my $host = (split /@/, $_[ARG0])[1]; if( $nick eq $current_nick ) { - $kernel->post( 'irc_client', 'privmsg', $channel, 'Hier bin ich!'); + $kernel->post( 'francoise', 'privmsg', $channel, 'Hier bin ich!'); } else { francoise_verifyuser( $nick, $host, 1 ); } @@ -141,15 +130,13 @@ sub irc_join { sub irc_quit { my $nick = $_[ARG0]; my $reason = $_[ARG1]; - print "#-> $nick has quit ($reason)\n"; } sub irc_pub_msg{ - my $kernel = $_[KERNEL]; - my $nick_ = (split /!/, $_[ARG0])[0]; + my ( $kernel, $msg ) = @_[KERNEL, ARG2]; my $channel = $_[ARG1]->[0]; - my $msg = $_[ARG2]; + my $nick_ = (split /!/, $_[ARG0])[0]; my @words = (split / /, $msg); my $numwords = $#words; @@ -162,12 +149,12 @@ sub irc_pub_msg{ if( $msg =~ /^wo ist (\S+)\??$/i ) { my $wois_ = $1; my $wois = francoise_getbasenick( $1 ); - my $sth = $dbh->prepare( "SELECT awaymsg FROM users WHERE nick = '$wois' AND isaway = 'true'"); - $sth->execute(); + my $sth = $dbh->prepare( "SELECT awaymsg FROM users WHERE nick = ? AND isaway = 'true'"); + $sth->execute( $wois ); if ( my @awaymsg = $sth->fetchrow_array ) { - $kernel->post( 'irc_client', 'privmsg', $channel, "$wois_ ist $awaymsg[0]" ); + $kernel->post( 'francoise', 'privmsg', $channel, "$wois_ ist $awaymsg[0]" ); } else { - $kernel->post( 'irc_client', 'privmsg', $channel, "Ich weiss nicht, wo $wois_ ist." ); + $kernel->post( 'francoise', 'privmsg', $channel, "Ich weiss nicht, wo $wois_ ist." ); } } elsif( $msg =~ /(.+?) ($hv) (.*)$/i ) { my $trigger = $1; @@ -181,15 +168,16 @@ sub irc_pub_msg{ $sth->execute( $msg ); if( my ($trigger, $hilfsverb, $reply) = $sth->fetchrow_array ) { if( $reply =~ /^(.*)$/ ) { - $kernel->post( 'irc_client', 'privmsg', $channel, "$1"); + $kernel->post( 'francoise', 'privmsg', $channel, "$1"); } else { - $kernel->post( 'irc_client', 'privmsg', $channel, "$trigger $hilfsverb $reply"); + $kernel->post( 'francoise', 'privmsg', $channel, "$trigger $hilfsverb $reply"); } } } if( $nick ne $current_nick ) { - $dbh->do( "UPDATE users SET lines = lines + 1, words = words + $numwords + 1, isaway = false WHERE nick = '$nick'" ); + my $sth = $dbh->do( "UPDATE users SET lines = lines + 1, words = words + $numwords + 1, isaway = false WHERE nick = ?" ); + $sth->execute( $nick ); } print "$channel: <$nick> $msg\n"; @@ -203,8 +191,8 @@ sub irc_action{ if( $msg =~ /^ist (.+)$/ ) { my $awaymsg = $1; - my $sth = $dbh->prepare( "UPDATE users SET isaway = true, awaymsg = ? WHERE nick = '$nick'"); - $sth->execute( $awaymsg ); + my $sth = $dbh->prepare( "UPDATE users SET isaway = true, awaymsg = ? WHERE nick = ?"); + $sth->execute( $awaymsg, $nick ); } } @@ -217,14 +205,13 @@ sub irc_priv_msg{ } if( $msg =~ /^!say (.*)$/ ) { - $kernel->post( 'irc_client', 'privmsg', $channel, $1); } + $kernel->post( 'francoise', 'privmsg', $channel, $1); } print "PRIV: [$nick] $msg\n"; } sub irc_whois{ - my $nick = (split / /, $_[ARG1])[0]; - my $host = (split / /, $_[ARG1])[2]; + my ( $nick, $host ) = (split / /, $_[ARG1])[0,2]; if( $nick ne $current_nick ) { francoise_verifyuser( $nick, $host, 0 ); @@ -234,12 +221,11 @@ sub irc_whois{ sub francoise_getbasenick{ my $nick = $_[0]; - $nick = ( split /_/, $nick )[0] || $nick; $nick = ( split /\|/, $nick )[0] || $nick; - my $sth = $dbh->prepare( "SELECT nick FROM aliases WHERE alias = '$nick'" ); - $sth->execute(); + my $sth = $dbh->prepare( "SELECT nick FROM aliases WHERE alias = ?" ); + $sth->execute($nick); if( my @row = $sth->fetchrow_array ) { $nick = $row[0]; } @@ -248,11 +234,7 @@ sub francoise_getbasenick{ } sub francoise_verifyuser { - my $nick_ = $_[0]; - my $host = $_[1]; - my $updtime = $_[2]; - my $kernel = $_[3]; - + my ( $nick_, $host, $updtime, $kernel ) = @_; my $nick = francoise_getbasenick( $nick_ ); if( $host ) { @@ -263,23 +245,22 @@ sub francoise_verifyuser { $dbh->do( "UPDATE users SET lasthost = '$host'" . ( $updtime ? ", lastlogin = now()" : "" ) . " WHERE nick = '$nick'" ); } } else { - $kernel->post( 'irc_client', 'whois', $nick_); + $kernel->post( 'francoise', 'whois', $nick_); } return $nick; } sub francoise_help { - my $kernel = $_[0]; - my $dest = $_[1]; - - $kernel->post( 'irc_client', 'privmsg', $dest, 'Hallo, ich bin Francoise, der freundliche Kifferchat-Bot.'); - $kernel->post( 'irc_client', 'privmsg', $dest, 'Ich kann folgende Kommandos ausfuehren:'); - $kernel->post( 'irc_client', 'privmsg', $dest, '!help Diese Hilfe anzeigen'); - $kernel->post( 'irc_client', 'privmsg', $dest, '!stat Informationen ueber mich ausgeben'); - $kernel->post( 'irc_client', 'privmsg', $dest, '!topten Die aktivsten Chatter anzeigen'); - $kernel->post( 'irc_client', 'privmsg', $dest, '!topten0r Die 13370rsten Chatter anzeigen'); - $kernel->post( 'irc_client', 'privmsg', $dest, '!forget Einen Trigger vergessen'); + my ( $kernel, $dest ) = @_; + + $kernel->post( 'francoise', 'privmsg', $dest, 'Hallo, ich bin Francoise, der freundliche Kifferchat-Bot.'); + $kernel->post( 'francoise', 'privmsg', $dest, 'Ich kann folgende Kommandos ausfuehren:'); + $kernel->post( 'francoise', 'privmsg', $dest, '!help Diese Hilfe anzeigen'); + $kernel->post( 'francoise', 'privmsg', $dest, '!stat Informationen ueber mich ausgeben'); + $kernel->post( 'francoise', 'privmsg', $dest, '!topten Die aktivsten Chatter anzeigen'); + $kernel->post( 'francoise', 'privmsg', $dest, '!topten0r Die 13370rsten Chatter anzeigen'); + $kernel->post( 'francoise', 'privmsg', $dest, '!forget Einen Trigger vergessen'); } sub francoise_stat { @@ -287,13 +268,12 @@ sub francoise_stat { } sub francoise_topten { - my $kernel = $_[0]; - my $dest = $_[1]; + my ($kernel, $dest ) = @_; my $sth = $dbh->prepare( "SELECT words, nick FROM users ORDER BY words DESC LIMIT 10" ); $sth->execute(); while ( my @row = $sth->fetchrow_array ) { - $kernel->post( 'irc_client', 'privmsg', $dest, "$row[0] $row[1]" ); + $kernel->post( 'francoise', 'privmsg', $dest, "$row[0] $row[1]" ); } } -- cgit v1.2.3