User:SQLBot/rfubot.php

From Wikipedia, the free encyclopedia

<?
#we need teh sxwiki to alert me.
include("SxWiki.php");
include("config.php");
 
#FIXME! Blocking admin detection does NOT reliably work. And, I gave up on duration and reason detection :(
function GetBlock($user) {
        $bturl = '/w/index.php?title=Special%3AIpblocklist&ip=';
        $user = rtrim($user);
        $user = ltrim($user);
        $test_url = $bturl . $user;
        $fp = fsockopen("en.wikipedia.org", 80, $errno, $errstr, 30);
        $out = "GET $test_url HTTP/1.1\r\n";
        $out .= "Host: en.wikipedia.org\r\n";
        $out .= "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20021204\r\n";
        $out .= "Connection: Close\r\n\r\n";
        fwrite($fp, $out);
        $f_input = "";
        while (!feof($fp)) {
                $f_input = fgets($fp, 512);
                $cpage = $cpage . $f_input;
        }
        fclose($fp);
        $findblock = '/The requested IP address or username is not blocked\./';
        $is_blocked = preg_match($findblock, $cpage);
        if($is_blocked != 0) {
                        return(FALSE);
        } else {
                $whoreg = '/<li>[0-9]{2}:[0-9]{2}, [0-9]{1,2} (?:January|February|March|April|May|June|July|August|September|October|November|December) [0-9]{4}, <a href="\/wiki\/(User:.*)" title="User:.*">.*<\/a> \(<a href="\/wiki\/User_talk:.*" title="User talk:.*">Talk<\/a> \| <a href="\/wiki\/Special:Contributions\/.*" title="Special:Contributions\/.*">contribs<\/a>\) blocked/i';
                preg_match($whoreg, $cpage, $who);
                return($who[1]);
        }
}
function GetMore($username) {
        $apiurl = "http://en.wikipedia.org/w/api.php?action=query&list=logevents&letype=block&letitle=" . urlencode($username) . "&lelimit=1&format=php";
        $blocked = file_get_contents($apiurl);
        $blocked = unserialize($blocked);
        $duration = $blocked[query][logevents][0][block][duration];
        $admin = $blocked[query][logevents][0][user];
        $reason = $blocked[query][logevents][0][comment];
        $result = array();
        $result[duration] = $duration;
        $result[admin] = $admin;
        $result[reason] = $reason;
        return($result);
}
function CompareCats($oldcat, $newcat) {
        $added = array();
        $removed = array();
        $diff = array();
        $removed = array_diff($oldcat, $newcat);
        $added = array_diff($newcat, $oldcat);
        $diff['new'] = $added;
        $diff['handled'] = $removed;
        return($diff);
}
 
#Config goes here.
$host = "irc.freenode.net";
$port=6667;
$nick="SQLBot";
$ident="SQLBot";
$chan="#wikipedia-en-unblock";
#$chan="#SQLTest";
$readbuffer="";
$realname = "SQLBot";
$oldcat = sxGetCat("Requests for unblock", 3);
$newcat = $oldcat;
$fp = fsockopen($host, $port, $erno, $errstr, 30);
if (!$fp) {
    echo $errstr." (".$errno.")<br />\n";
} else {
    fwrite($fp, "NICK ".$nick."\r\n");
    fwrite($fp, "USER ".$ident." ".$host." bla :".$realname."\r\n");
    sleep(1);
    #ID to NS
    fwrite($fp, "PRIVMSG Nickserv :identify password\r\n");
    sleep(5);
    #Join wikipedia-unblock-en
    fwrite($fp, "JOIN :".$chan."\r\n");
    echo "Identified to Nickserv.\n";
    #HELLO, World! :)
    fwrite($fp, "PRIVMSG ".$chan." :SQLBot active and ready for unblock parsing.\r\n");
    echo "Joined $chan\n";
 
     $autotimer = 0;
     #Keep the bot running.
     while (!feof($fp)) {
        stream_set_blocking($fp, 0);
        usleep(2500);
        $autotimer++;
        #echo "$autotimer\n";
        if ($autotimer >= 40000) {
                $newcat = sxGetCat("Requests for unblock", 3);
                echo "Checking for unblock changes...\n";
                $autotimer = 0;
                $catchanges = CompareCats($oldcat, $newcat);
                if ($shutup != 1 || $enabled != 0) {
                        foreach ($catchanges['new'] as $newunblock) {
                                preg_match("/User( |_)talk:(.*)/", $newunblock, $user);
                                $moreinfo = GetMore("User:$user[2]");
                                fwrite($fp, "PRIVMSG ".$chan." :New unblock request: [[$newunblock]], Blocked by [[User:" . $moreinfo[admin] . "]] until " . $moreinfo[duration] . " for " .  $moreinfo[reason] . " \r\n");
                                usleep(500000);
                        }         
                        foreach ($catchanges[handled] as $handledunblock) {
                                fwrite($fp, "PRIVMSG ".$chan." :Handled: [[$handledunblock]]\r\n");
                                usleep(500000);
                        }
                } else {
                                echo "I was told to shut up. Staying quiet. Enabled = $enabled, Shutup = $shutup\n";
                        }
                #print_r($catchanges); //DEBUG
                $oldcat = $newcat;
 
        }
        $line =  fgets($fp, 128);
        #echo $line."\n"; //DEBUG
        if(stristr($line, " PRIVMSG $chan :SQLBot: Quiet") != FALSE) { //quiet trigger
                $shutup = 1;
                echo "I was told to STFU!\n";
                fwrite($fp, "PRIVMSG ".$chan." :Ok, shutting up now, until told otherwise.\r\n");
                usleep(500000);
        }
        if(stristr($line, " PRIVMSG $chan :SQLBot: Disable") != FALSE) { //Disable trigger
                $enabled = 0;
                echo "I was disabled!\n";
                fwrite($fp, "PRIVMSG ".$chan." :Ok, Disabled auto-updates.\r\n");
                usleep(500000);
        }
        if(stristr($line, " PRIVMSG $chan :SQLBot: Enable") != FALSE) { //enable
                $enabled = 1;
                echo "I was told to enabled!\n";
                fwrite($fp, "PRIVMSG ".$chan." :Ok, Enabled auto-updates.\r\n");
                usleep(500000);
        }
        if(stristr($line, " PRIVMSG $chan :SQLBot: Speak") != FALSE) { //speak trigger
                $shutup = 0;
                echo "I was told I can talk again!\n";
                fwrite($fp, "PRIVMSG ".$chan." :Ok, entering chatterbox mode.\r\n");
                usleep(500000);
        }
        if(stristr($line, " PRIVMSG $chan :!help") != FALSE) { //help trigger
                fwrite($fp, "PRIVMSG ".$chan." :My manual can be found at [[User:SQLBot/RFUBot Instructions]].\r\n");
                echo "Help requested!\n";
                usleep(500000);
        }
        if(stristr($line, " PRIVMSG $chan :!unblock") != FALSE) { //!unblock trigger
                echo "Received !unblock\n";
                $rfucat = sxGetCat("Requests for unblock", 3);
                foreach ($rfucat as $rfu) {
                        preg_match("/User( |_)talk:(.*)/", $rfu, $user);
                        $isblocked = GetBlock($user[2]);
                        $moreinfo = GetMore("User:$user[2]");
                        if ($moreinfo[admin] != "") {
                                $tochan = "PRIVMSG ".$chan." :[[$rfu]] - Blocked by [[User:" . $moreinfo[admin] . "]] until " . $moreinfo[duration] . " for " .  $moreinfo[reason] . " \r\n";
                        } else {
                                $tochan = "PRIVMSG ".$chan." :[[$rfu]] - No block detected!\r\n";
                        }
                        if ($shutup != 1) { fwrite($fp, $tochan); } else { echo "I was told to shut up. Staying quiet.\n"; }
                        sleep(1);
                }
        }        
        if(stristr($line, " PRIVMSG $chan :!die") != FALSE) { //!die trigger
                        $totalk = sxGetPage("User talk:SQL");
                        $totalk = $totalk . "\n==Bot down!==\nOh noes! Someone in unblock-en killed meh! ~~~~\n";
                        sxPutPage("User talk:SQL", "Bot Down!", $totalk, NULL, NULL);
                        fwrite($fp, "PRIVMSG ".$chan." :Ok, dying. Alerting SQL.\r\n");
                        sleep(1);
                        die("Killed by request\n");
        }        
        $line = explode(":ping ", $line); 
 
        #echo $line[0]."\n"; //DEBUG
 
        if ($line[1]) { //Still doesen't work right. Not sure I need it.
           echo "PONG!\n"; 
           fwrite($fp, "PONG ".$line[1]."\r\n"); 
        }
 
 
    }
 
    fclose($fp); //Goodnight!
}
?>