wantzel.pl 7.91 KB
Newer Older
Mindiell's avatar
Mindiell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
#!/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 cre 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 vnements :
$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, dsol ...");
		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 vnement dans les dbats  l'Assemble Nationale, tapez !tc<espace>suivi du texte decrivant l'vnement : n'oubliez pas le Nom de l'intervenant ainsi que le verbe dcrivant son action.");
    $conn->privmsg($channel, "Par exemple : !tc Albanel parle de fabrication d'adresses IP");
    $conn->privmsg($channel, "Ou encore : !tc Billard / Brard dfendent les auteurs plutt 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