All pastes #99431 Raw Edit

Something

public text v1 · immutable
#99431 ·published 2006-07-25 09:19 UTC
rendered paste body
#!/usr/bin/perl

use strict;
use IO::Socket::INET;
# à décommenter
# use Math::TrulyRandom;

my $master = "semial";
my $botip = "maison.dasheep.com";
my $server = "irc.geeknode.org";
my $channel = "#geekerie";
my $nick = "blah";
my $username = "blah";
my $realname = "Blah blah blah.";
my $version = "Blah, a wonderful bot.";
my $logfile = "$ENV{'HOME'}/irclogs/GeekNode/#geekerie.log";
my $debug = 0; 
my $irc_debug = 0;
my $talk = 1;
my $learning = 1;
my $replyrate = 100;

# à décommenter
# srand(truly_random_value()+time);

my %commands = ("PRIVMSG" => \&parse_privmsg, 
				"NOTICE" => \&parse_notice ,
				"JOIN" => \&parse_join,
				"QUIT" => \&parse_quit);

my $socket = new IO::Socket::INET (
                    PeerHost => $server ,
                    PeerPort => 6667 ,
                    Proto => 'tcp'
              );


ircprint("NICK $nick\n");
ircprint("USER  $username 8 *  : $realname\n");
ircprint("JOIN $channel\n");
say("blah :D");

while(<$socket>) {
	print;
	if($_ =~ /^:(\S+)\!(\S+) (\S+) (\S+) :(.*?)$/){
		$commands{$3}->($1,$2,$3,$4,$5);
	}
	if($_ =~ /^:(\S+)\!(\S+) (\S+) :(.*?)$/) {
		$commands{$3}->($1,$2,$3,$4);
	}
	if($_ =~ /^PING :\S+/) {
		ircprint("PONG :$botip\n");
	}

}

sub parse_join{
	ircprint("PRIVMSG $_[0] :\001VERSION\001\n");
	say("parsing a join") if($irc_debug);
}

sub parse_privmsg{
	say("parsing a privmsg...") if($irc_debug);
	my $sender_nick = $_[0];
	my $sender_username = $_[1];
	my $command = $_[2];
	my $dest = $_[3];
	my $text = $_[4];
	if($text =~ /^\!/ && $sender_nick eq $master) {
		my $user_command = $';
		if($user_command =~ /alaniche/) {
			quit();
		}
		elsif($user_command =~ /stfu/) {
			$talk = 0;
		}
	}
	elsif($text =~ /^\caVERSION\ca/) {
		ircprint("NOTICE $sender_nick :\001VERSION $version\001\n");
	}
	elsif($text =~ /^$nick(:|\>|,) (hello|re|yo|plop|yop|salut|bonjour|matin|\'jour|\'nuit|\'matin|\*coin\*|y0|coucou)/) { # respond to greetings :)
		say("$sender_nick$1 $2$'");
	}
	elsif($text =~ /^$nick(:|\>|,) gimme a link/) {
		my $links = `grep http:// $logfile`;
		my $loglenght = `grep http:// $logfile | wc -l`;
		$loglenght =~ s/(\d*) .*?/$1/;
		my $logindex = 0;
		my $logline = int rand($loglenght);
		my @links = split(/\n/, $links);
		my $log_ret;
		READLINKS: foreach(@links) {
			if($logindex == $logline) {
				$log_ret = $_;
				last READLINKS;
			}
			$logindex++;
		}
		$log_ret =~ s/.*?(http:\/\/\S+).*?/$1/;
		say("$sender_nick: $log_ret");
	}
	elsif($text =~ /^$nick(:|\>|,)/) {
		open LOG,"< $logfile";
		my $loglenght = `wc -l $logfile`;
		$loglenght =~ s/(\d*) .*?/$1/;
		my $logindex = 0;
		my $logline = int rand($loglenght);
		my $log_ret;
		while(!($log_ret =~ /.+/)){ # des fois le bot retourne rien, j'ai pas compris pourquoi, alors gruikfixed.
			READLOG: while(<LOG>) {
				if($logindex == $logline) {
					unless($_=~/^\d\d:\d\d -\!-/) {
						unless($_ =~ /^--- Log (clos|open)ed/){
							$log_ret = $_;
							last READLOG;
						}
					}
				}
				$logindex++;
			}
		}
		if($log_ret =~ /^\d\d:\d\d \<.*?\>/){
			$log_ret =~ s/^\d\d:\d\d \<.*?\> (.*?)/$1/;
		}
		if($log_ret =~ /^\d\d:\d\d  \* \S+ .*?/) {
			$log_ret =~ s/^\d\d:\d\d//;
		}
		$log_ret =~ s/^\S+(:|\>|,)//;
		say($sender_nick.": ".$log_ret);
	}
	if($text =~ /lol/) {
		say("LOLE $sender_nick");
	}
}

sub parse_notice{
	my $sender_nick = $_[0];
	my $sender_username = $_[1];
	my $command = $_[2];
	my $dest = $_[3];
	my $text = $_[4];
	if($text =~ /\caVERSION/){
		if($text =~ /\caVERSION.*?(mIRC|Windows)/) {
			say("$sender_nick, shame on you.");
		}
		$text =~ s/\caVERSION (.*?)\ca/$1/;
		say("$sender_nick is running $text");
	}
	elsif($text =~ /quote pong (........)/) {
		sleep (2);
		ircprint("pong $1\n");
	}
	say("parsing a notice") if($irc_debug);
}

sub ircprint{
	print $socket $_[0];
}

sub parse_quit{
}

sub say{
	my $ligne = $_[0];
	ircprint("PRIVMSG $channel :$ligne \n") if($talk);
}

sub action{
	ircprint("PRIVMSG $channel :\caACTION $_[0]\n");
}

sub dprint{
	print "\n$_[0]\n\n" if($debug);
}

sub quit{
	ircprint("QUIT Wouf wouf\n");
	sleep(2);
	exit;
}

close ( $socket );