From 937f46fb74c72e829a015ed196bb87c86fee3435 Mon Sep 17 00:00:00 2001 From: erdgeist <> Date: Thu, 21 Jul 2005 02:59:52 +0000 Subject: erster datenbankshiat: userhandling --- bot.pl | 49 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/bot.pl b/bot.pl index cdf6845..f9a0c4e 100644 --- a/bot.pl +++ b/bot.pl @@ -21,6 +21,7 @@ POE::Session->new ( _start => \&irc_start, irc_376 => \&irc_connect, #end of motd irc_372 => \&irc_motd, irc_353 => \&irc_names, + irc_311 => \&irc_whois, irc_public => \&irc_pub_msg, irc_msg => \&irc_priv_msg, ); @@ -60,12 +61,18 @@ sub irc_motd { } sub irc_names { + my $kernel = $_[KERNEL]; my $names = (split /:/, $_[ARG1])[1]; my $channel = (split /:/, $_[ARG1])[0]; $channel =~ s/[@|=] (.*?) /$1/; print "#-> Users on $channel [ $names ]\n"; + + for my $user (split / /, $names) { + $user =~ s/^@|%|\+//; + $kernel->post( 'irc_client', 'whois', $user); + } } #nick change @@ -73,6 +80,10 @@ sub irc_nick { my $oldnick = (split /!/, $_[ARG0])[0]; my $newnick = $_[ARG1]; + if( $newnick ne $current_nick ) { + $dbh->do( "UPDATE users SET nick = '$newnick' WHERE nick = '$oldnick'" ); + } + print "#-> $oldnick is now known as $newnick\n"; } @@ -89,12 +100,19 @@ sub irc_join { my $kernel = $_[KERNEL]; my $nick = (split /!/, $_[ARG0])[0]; my $channel = $_[ARG1]; + my $host = (split /@/, $_[ARG0])[1]; if( $nick eq $current_nick ) { - print "I JOIN JOIN JOIN\n"; $kernel->post( 'irc_client', 'privmsg', $channel, 'Hier bin ich!'); + } else { + my ($cnt) = $dbh->selectrow_array( "SELECT COUNT(*) FROM users WHERE nick='$nick'" ); + if ( $cnt == 0 ) { + $dbh->do( "INSERT INTO users(id, nick, words, lines, lastlogin, lasthost) VALUES ( '$nick', '$nick', 0, 0, now(), '$host' )"); + } else { + $dbh->do( "UPDATE users SET lasthost = '$host', lastlogin = now() WHERE nick = '$nick'" ); + } } - + print "#-> $nick has joined $channel\n"; } @@ -107,10 +125,15 @@ sub irc_quit { } sub irc_pub_msg{ - my $kernel = $_[KERNEL]; - my $nick = (split /!/, $_[ARG0])[0]; + my $kernel = $_[KERNEL]; + my $nick = (split /!/, $_[ARG0])[0]; my $channel = $_[ARG1]->[0]; - my $msg = $_[ARG2]; + my $msg = $_[ARG2]; + my $words = (split / /, $msg); + + if( $nick ne $current_nick ) { + $dbh->do( "UPDATE users SET lines = lines + 1, words = words + $words WHERE nick = '$nick'" ); + } print "$channel: <$nick> $msg\n"; } @@ -122,10 +145,22 @@ sub irc_priv_msg{ $kernel->post( 'irc_client', 'privmsg', $nick, 'Ich dich auch!'); - $dbh->do( "INSERT INTO strings VALUES ('$nick', '$msg')" ); - print "PRIV: [$nick] $msg\n"; } +sub irc_whois{ + my $nick = (split / /, $_[ARG1])[0]; + my $host = (split / /, $_[ARG1])[2]; + + if( $nick ne $current_nick ) { + my ($cnt) = $dbh->selectrow_array( "SELECT COUNT(*) FROM users WHERE nick='$nick'" ); + if ( $cnt == 0 ) { + $dbh->do( "INSERT INTO users(id, nick, words, lines, lastlogin, lasthost) VALUES ( '', '$nick', 0, 0, now(), '$host' )"); + } else { + $dbh->do( "UPDATE users SET lasthost = '$host' WHERE nick = '$nick'" ); + } + } +} + #start everything $poe_kernel->run(); -- cgit v1.2.3