Description:
Bot irc écrit en perl.
Code:#!/usr/local/bin/perl -w
# irc.pl
# A simple IRC robot.
# Usage: perl irc.pl
# use strict;
# use utf8;
# We will use a raw socket to connect to the IRC server.
use IO::Socket;
use WWW::Mechanize;
use DBI();
# The server to connect to and our details.
my $server = "irc.code-libre.org";
my $nick = "Zuni";
my $login = "Zuni";
my $mech = WWW::Mechanize->new();
# Some usefull functions
sub speak
{
my($sock, $dest, $msg) = @_;
print $sock "PRIVMSG $dest $msg\r\n";
}
sub notice
{
my($sock, $dest, $msg) = @_;
print $sock "NOTICE $dest $msg\r\n";
}
sub action
{
my($sock, $chan, $action) = @_;
print $sock "PRIVMSG $chan :".chr(001)."ACTION $action".chr(001)."\r\n";
}
sub kick
{
my($sock, $chan, $user, $motif) = @_;
print $sock "KICK $chan $user : $motif\r\n";
}
# The channel which the bot will join.
my @channel = ("#bot", "#code-libre");
# Connect to the IRC server.
my $sock = new IO::Socket::INET(PeerAddr => $server,
PeerPort => 6667,
Proto => 'tcp') or
die "Can't connect\n";
# Log on to the server.
print $sock "NICK $nick\r\n";
print $sock "USER $login 8 * :Perl IRC Hacks Robot\r\n";
# Read lines from the server until it tells us we have connected.
while (my $input = <$sock>)
{
# Check the numerical responses from the server.
if ($input =~ /004/) {
# We are now logged in.
last;
}
elsif ($input =~ /433/) {
die "Nickname is already in use.";
}
}
# Join channels.
foreach my $key (@channel)
{
print $sock "JOIN $key\r\n";
# print $sock "JOIN channel\r\n";
}
# action($sock, $channel, "teste :)");
# Keep reading lines from the server.
my $word;
my @values;
my @qu;
%voc = (
"netiquette" => "Respectez la netiquette !",
"ssl" => "4242 !",
"ntm" => "Nique ta mère.",
"sslhowto" => "Aller dans la liste de serveurs, Choisir ce serveur, cocher : utiliser ssl sur ce serveur ET accepter les certificats non valides.",
"cnil" => "conformément à la loi sur l'informatique et les libertées individuelles .. non rien en fait.",
"ipot" => "IPoT : IP over Time (http://kadreg.org/ipot/)."
);
my($user, $bis, $host, $chan, $msg);
# action($sock, $channel, "va encore merder");
# quote($sock, $channel, "fser");
while (my $input = <$sock>)
{
if ($input =~ /^PING(.*)$/i)
{
# We must respond to PINGs to avoid being disconnected.
print $sock "PONG $1\r\n";
}
if(@_ = $input =~ /^[:]*(.*)!(.*)@(.*)\sPRIVMSG\s(.*?):(.*)$/is)
{
($user, $bis, $host, $chan, $msg)= @_;
print $msg;
if ($msg =~ /^!(.*?)\n/)
{
$word = $1;
chop $word;
if(exists($voc{$word}))
{
print "saibon\n";
speak($sock, $chan, $voc{$word});
}
elsif ($msg =~ /^!add (.*?)[\s]*:[\s]*(.*)/)
{
$voc{$1} = $2;
speak($sock, $chan, "$1 ajoute");
}
elsif ($msg =~ /^!dance\s+$/)
{
speak($sock, $chan, ".o/ _o/ _o_ \\o_ o_ _o o \\o/");
speak($sock, $chan, "/| | | | /| |\\ /|\\ |");
speak($sock, $chan, "/ \\ / \\ / \\ / \\ / \\ / \\ / \\ / \\");
}
elsif ($msg =~ /^!tagueule\s+$/)
{
speak($sock, $chan, "ta gueule $user !");
}
elsif ($msg =~ /^!tagueule\s+(.*?)\s+$/)
{
speak($sock, $chan, "ta gueule $1!");
}
elsif ($msg =~ /^!date\s+$/)
{
my $t = localtime;
speak($sock, $chan, $t);
}
elsif( @qu = $msg =~ /^!quote\s+add\s+([a-zA-Z0-9]*)\s+([a-zA-Z0-9 '"àéèê,\!\:\@().<>ç?]*)/g)
# elsif( @qu = $msg =~ /^!quote\s+add\s+([a-zA-Z0-9]*)\s+(.*)\s/g)
{
quote_add($sock, $chan, $1, $2);
}
elsif( @qu = $msg =~ /^!quote\s+([a-zA-Z0-9]*)\s+([0-9]*)?/g )
{
quote($sock, $chan, $1, $2);
}
else
{
#continue here
@_ = split(' ', $msg);
print "@_\n";
}
}
}
elsif(@_ = $input =~ /^[:]*(.*)!(.*)@(.*)\sKICK\s(.*?)\s(.*?):\s+(.*)$/is)
{
my $kicke;
($user, $bis, $host, $chan,$kicke, $msg)= @_;
if($user =~ /dotbot/i)
{
kick($sock, $chan, $user, $msg);
}
}
}
sub quote
{
my($sock, $dest, $pseudo, $id) = @_;
my $dbh = DBI->connect("DBI:mysql:database=fser;host=fser.info",
"fser", "pass",
{'RaiseError' => 1});
my $end;
$end = "AND 1";
if($id)
{
$end = "AND id=$id";
}
my $sth = $dbh->prepare("SELECT pseudo, quote FROM quotes WHERE pseudo like \"$pseudo%\" " . $end . " order by md5(rand()) limit 1");
$sth->execute();
my $ref = $sth->fetchrow_hashref();
if(!$sth->rows)
{
speak($sock, $dest, "Pas de citation pour $pseudo.");
}
else
{
speak($sock, $dest, "“ $ref->{'quote'} ” -- $ref->{'pseudo'}");
}
$sth->finish();
$dbh->disconnect();
}
sub quote_add
{
my($sock, $dest, $pseudo, $citation) = @_;
my $dbh = DBI->connect("DBI:mysql:database=fser;host=fser.info",
"fser", "pass",
{'RaiseError' => 1});
$sth = $dbh->prepare(qq{
INSERT INTO quotes (pseudo,quote) VALUES (?, ?)});
$sth->execute($pseudo, $citation);
my $id = $dbh->last_insert_id(undef, undef, qw(quote id));
speak($sock, $dest, "Citation ajoutée ($id)");
$dbh->disconnect();
}
Todo:
Faire faire des actions au bot (parler, quitter, noticer, "/me"er) ...