#!/usr/bin/perl use strict; use diagnostics; use warnings; use Config::Simple; use Net::IRC; use DBI; my %config; Config::Simple->import_from('/var/www/site/rp/bots/wantzel.ini', \%config); my $dsn = $config{'dsn'}; my $db_user_name = $config{'db_user_name'}; my $db_password = $config{'db_password'}; my $dbh = 0; # Configuration des options de connexion (serveur, login) : my $server = $config{'server'}; my $nick = $config{'nick'}; # Informations concernant le Bot : my $ircname = $config{'ircname'}; my $username = $config{'username'}; my $version = $config{'version'}; # Channel sur lequel on veut que le Bot aille : my $channel = $config{'channel'}; my $irc = new Net::IRC; # On crée l'objet de connexion à IRC : my $conn = $irc->newconn( 'Server' => $server, 'Port' => 6667, 'Nick' => $nick, 'Ircname' => $ircname, 'Username' => $username ); # On installe les fonctions de Hook : $conn->add_handler('376', \&on_connect); # Fin du MOTD => on est connecté $conn->add_handler('public', \&on_public); # Sur le chan $conn->add_handler('msg', \&on_private); # En privé # On lance la connexion et la boucle de gestion des événements : $irc->start(); sub on_connect { my ($conn, $event) = @_; $conn->join($channel); $conn->privmsg($channel, 'Bonjour, je suis le bot de la Quadrature du Net, vous pouvez me demander de l\'aide si besoin. (wantzel help)'); print "<$nick>\t| Hi all, Pi\'s salutations\n"; $conn->{'connected'} = 1; } # Fin on_connect sub on_public { my ($conn, $event) = @_; my $text = $event->{'args'}[0]; print "<" . $event->{'nick'} . ">\t| $text\n"; # Search for a url and store it ;) my $s=$text; my $who=$event->{'nick'}; if ($s =~ "wantzel" && ($s =~ "help" || $s =~ "aide")) { $conn->privmsg($channel, "Toute l'aide se trouve ici : http://www.laquadrature.net/wiki/Wantzel"); } if ($s =~ "!rp") { if ($s =~ /(http[^\s)<\"]*)/) { my $url=$1; $url =~ s/\$/\\\$/g; # Let's get rid of the trackers part to ensure uniq urls $url =~ s/[?&](utm_medium|utm_source|utm_campaign|xtor)=[^&]*//g; # Remove /#!/ part $url =~ s=/#!/=/=g; # Remove anchors $url =~ s/#[^#]$//; print "READ URL : $url\n"; my $sql=q{ SELECT * FROM presse WHERE url= ? ; }; my $dbh = DBI->connect($dsn, $db_user_name, $db_password); my $sth = $dbh->prepare($sql); if (! $sth->execute($url) ) { $conn->privmsg($channel, "oups, je n'ai pas pu la mettre en base, désolé ..."); die "Fatal: cannot RECONNECT !\n"; } my @found = $sth->fetchrow_array(); $sth->finish(); # we're done with this query if (!@found) { my $cite=0; if ($s =~ "!rpc" || $s =~ "!rpdc") { $cite+=2; } if ($s =~ "!rpp" || $s =~ "!rpcp") { $cite+=4; } $sth = $dbh->prepare(q{ INSERT INTO presse SET url= ?, provenance= ?, cite= ?, note= ?, datec=NOW(); }); my $note = 0; if (($who =~ '^vinci$') || ($who =~ '^gibus') || ($who =~ 'theo.rite$') || # ($who =~ '^tekoo') || # ($who =~ '^ali?xc?$') || ($who =~ '^Siltaar$') || ($who =~ '^yost.d$') || 0 ) { $note=3; } if (($who =~ '^.?jz$') || 0 ) { $note=42; } # to archive the url if ($s =~ "!rppa" || $s =~ "!rpcpa" || $s =~ "!rpa" || $s =~ '!rpca') { $note=-2; } $sth->execute($url,"IRC(".$event->{'nick'}.")",$cite,$note); my $id=$dbh->last_insert_id(undef, undef, undef, undef); print "NOT FOUND, INSERTED AS $id\n"; if ($who =~ "alxc") { $conn->privmsg($channel, "Merci alix, j'ai ajoute ce bel article a notre fabuleuse memoire presse !"); } else { $conn->privmsg($channel, 'Merci, cette url a ete ajoutee a la revue de presse ;)'); } } else { if ($found['provenance'] != "IRC(".$event->{'nick'}.")") { $sth = $dbh->prepare(q{ UPDATE presse SET note=note+1 WHERE id = ? }); $sth->execute($found['id']); } print "FOUND\n"; if ($who =~ "alxc") { $conn->privmsg($channel, "Merci alix, j'avais deja cet article dans la revue de presse, mais merci tout de meme de me l'avoir signale a nouveau, c'est utile."); } else { $conn->privmsg($channel, 'Merci, mais cette url etait deja dans la revue de presse.'); } } $url=~s/[\?\/\\\(\)\+]/./g; $s=~s/$url//; $dbh->disconnect(); } # cherche les urls } # a-t-on "!rp" dans le message elsif ($s =~ "!stats") { my @period=(1, 3, 7, 15); my @note=(0, 3, 4); my $msg='stats rp'; my $notnull=0; my $somethingatall=0; my $tmpmsg=''; foreach my $note (@note) { $notnull=0; $tmpmsg=' => note >= '.$note; foreach my $period (@period) { my $sql='SELECT COUNT(id) AS cid FROM presse WHERE nid=0 AND datec>(NOW()-INTERVAL ? DAY) AND note>=?'; my $dbh = DBI->connect($dsn, $db_user_name, $db_password); my $sth = $dbh->prepare($sql); die "Fatal: cannot RECONNECT !\n" unless ($sth->execute($period, $note)); my @found=$sth->fetchrow_array(); $sth->finish(); # we're done with this query if ($found['cid']>0) { $tmpmsg.=", ${period}j:$found['cid']"; $notnull=1; $somethingatall=1; } } if ($notnull!=0) { $msg.=$tmpmsg; $msg.='\n'; } } if ($somethingatall==0) { $msg.=" Bravo les gars, rien en retard depuis ces ".$period[$#period-1]." derniers jours ! felicitation !"; } $conn->privmsg($channel, $msg); } # !stats if ($s =~ '!tc help') { $conn->privmsg($channel, "Pour signaler un évènement dans les débats à l'Assemblée Nationale, tapez !tcsuivi du texte decrivant l'évènement : n'oubliez pas le Nom de l'intervenant ainsi que le verbe décrivant son action."); $conn->privmsg($channel, "Par exemple : !tc Albanel parle de fabrication d'adresses IP"); $conn->privmsg($channel, "Ou encore : !tc Billard / Brard défendent les auteurs plutôt que les majors"); } else { if ($s =~ '!tc (.*)$') { my $TC=$1; print "READ TIMECODE : $TC\n"; my $dbh = DBI->connect($dsn, $db_user_name, $db_password); my $sth = $dbh->prepare(q{ INSERT INTO tclive SET user= ?, description= ?; }); $sth->execute($event->{'nick'},$TC); $conn->privmsg($channel, 'timecode recorded'); } # a-t-on "!tc" dans le message if ($s =~ /^\!quote/) { if (my($quote, $author, $url, $source)=($s =~ /^\!quote\s+"(.+)"\s+(.+)\s+(https?\:\/\/[\S]+)\s+(.+)/)) { #$conn->privmsg($channel, "quote: $quote"); #$conn->privmsg($channel, "auteur: $auteur"); #$conn->privmsg($channel, "url: $url"); #$conn->privmsg($channel, "source: $source"); my $sql='INSERT INTO quotes (submited_by, quote, author, url, source, date, note) VALUES ( ?, ?, ?, ?, ?, NOW(), ?)'; my $dbh = DBI->connect($dsn, $db_user_name, $db_password); my $sth = $dbh->prepare($sql); die "Fatal: cannot RECONNECT !\n" unless ($sth->execute($who, $quote, $author, $url, $source,1)); $sth->finish(); $conn->privmsg($channel, "C'est dans la boite !"); } else { $conn->privmsg($channel, "Pour ajouter une citation tapez : !quote \"la citation\" l'auteur http://url la source"); } } # a-t-on "!quote" dans le message } } # Fin on_public sub on_private { my ($conn, $event) = @_; my $text = $event->{'args'}[0]; $conn->print("PRIVE<" . $event->nick() . ">\t| $text"); } # Fin on_private