Registration has been disabled and the moderation extension has been turned off.
Contact an admin on Discord or EDF if you want an account. Also fuck bots.

User:Interstellar Shipping & Trading/weaselbot

From Encyclopedia Dramatica
This is an old revision of this page, as edited by imported>Schnookums at 06:43, 17 November 2013. It may differ significantly from the current revision.
Jump to navigation Jump to search

These scripts require PHP and sqlite3. Launch rc.php and bot.php from the terminal. Weaselbot's job is to monitor recent changes. It is a bit slow because it retrieves a copy of Special:RecentChanges at regular intervals. So until we see the return of wikibot this will do.

irc://irc.wtfux.org/rc or join #rc

rc.php


<?php
require "common.php";
$url = "https://encyclopediadramatica.es/index.php?title=Special:RecentChanges&limit=10";
$prefixes = array (
"(Molestation log);",
"(Move log);",
"(Plumbing log);",
"(Upload log);",
"(User creation log);",
"(diff | hist)",
);
$diffhist = array (
" . . Nm ",
" . . m ",
" . . ",
);
function loop ($url) {
    global $diffhist, $prefixes, $db;
    $html = @file_get_contents ($url);
    if ($html !== false) {
        $x = strpos ($html, "<ul class=\"special\">");
        if ($x !== false) {
            $y = strpos ($html, "<div class=\"printfooter\">", $x);
            $html = substr ($html, $x, $y - $x);
            $list = explode ("\n", $html);
            $list = array_reverse ($list);
            foreach ($list as $html) {
                $data = strip_tags ($html);
                $data = trim ($data);
                $data = htmlspecialchars_decode ($data, ENT_QUOTES);
                $data = str_replace ("(Talk | contribs)", "", $data);
                $data = str_replace ("  ", " ", $data);
                if (strlen ($data) < 20 ) {
                    continue;
                }
                $url = "";
                foreach ($prefixes as $prefix) {
                    if ($prefix) {
                        $len = strlen ($prefix);
                        $p = substr ($data, 0, $len);
                        if ($p == $prefix) {
                            switch ($p) {
                                case "(diff | hist)":
                                    $data = substr ($data, $len - strlen ($data));
                                    foreach ($diffhist as $p) {
                                        $len = strlen ($p);
                                        if (substr ($data, 0, $len) == $p) {
                                            $data = substr ($data, $len - strlen ($data));
                                            break;
                                        }
                                    }
                                    $pos1 = strpos ($html, "<a href=\"/");
                                    $pos1 = strpos ($html, "<a href=\"/", $pos1 + 1);
                                    $pos1 = strpos ($html, "<a href=\"/", $pos1 + 1);
                                    $pos2 = strpos ($html, "\" title=", $pos1);
                                    $len1 = strlen ("<a href=\"/");
                                    $url = substr ($html, $pos1 + $len1, $pos2 - ($pos1 + $len1));
                                    break;
                            }
                            break;
                        }
                    }
                }
                $sid = md5 ($data);
                print ("$data\n");
                if ($url) {
                    print ("$url\n");
                }
                $result = dbquery (__LINE__, $db, "SELECT COUNT(sid) FROM rc WHERE sid=:sid", array ("sid"=>$sid));
                $array = $result->fetchArray (SQLITE3_NUM);
                if (!$array[0]) {
                    dbexec (__LINE__, $db, "INSERT INTO rc (sid,data,url) VALUES (:sid,:data,:url)", array ("sid"=>$sid,"data"=>$data,"url"=>$url));
                }
                $url = "";
            }
        }
    }
}
while (true) {
    loop ($url);
    sleep (5);
}
?>

bot.php


<?php
require "common.php";
$nick = "weaselbot";
$chan = "#rc";
$joinchan = "MODE $nick";
$joinedchan = "JOIN :$chan";
$server = "tcp://irc.wtfux.org:6667";
function loop () {
    global $nick, $chan, $joinchan, $joinedchan, $server, $db;
    $time = time ();
    $status = 0;
    $stream = stream_socket_client ($server, $errid, $errstr, 30);
    if (!$stream) {
        print ("$errid $errstr\n");
        return;
    }
    function send ($socket, $data) {
        fwrite ($socket, "$data\r\n");
        printf (">$data\n");
    }
    send ($stream, "NICK $nick");
    send ($stream, "USER $nick 8 * :$nick");
    while (!feof ($stream)) {
        $data = fgets ($stream, 2048);
        $args = explode (" ", $data);
        if ($args[0] == "PING") {
            send ($stream, "PONG " . $args[1]);
        }
        switch ($status) {
            case 0:
                $pos = strpos ($data, $joinchan);
                if ($pos !== false) {
                    send ($stream, "JOIN $chan");
                    $status = 1;
                }
                break;
            case 1:
                $pos = strpos ($data, $joinedchan);
                if ($pos !== false) {
                    send ($stream, "PRIVMSG NickServ IDENTIFY $nickserv");
                    $status = 2;
                }
                break;
            case 2:
                $result = dbquery (__LINE__, $db, "SELECT * FROM rc WHERE status=:status ORDER BY uid ASC LIMIT 5", array ("status"=>0));
                while ($array = $result->fetchArray (SQLITE3_ASSOC)) {
                    dbexec (__LINE__, $db, "UPDATE rc SET status=:status WHERE uid=:uid", array ("status"=>1,"uid"=>$array["uid"]));
                    if ($array["url"]) {
                        $array["data"] .= (" https://encyclopediadramatica.es/" . $array["url"]);
                    }
                    print $array["data"] . "\n";
                    send ($stream, "PRIVMSG $chan " . $array["data"] . "\r\n");
                }
                break;
        }
        echo $data;
    }
}
while (true) {
    loop ();
    sleep (60); // wait 60 secs before reconnecting
}
?>

common.php


<?php
require "login.php";
error_reporting (E_ALL);
$db = new SQLite3 ("/Volumes/User/patrol/data.db");
if (!$db) {
    die ($db->lastErrorMsg () . "\n");
}
function dbbindvalues ($db, $stmt, $bindvalues) {
    foreach ($bindvalues as $key => $val) {
        $stmt->bindValue (":$key", $val);
    }
}
function dbexec ($line, $db, $sql, $bindvalues) {
    $stmt = @$db->prepare ($sql);
    if (!$stmt) {
        die (__LINE__ . " " . $db->lastErrorMsg () . "\n");
    }
    dbbindvalues ($db, $stmt, $bindvalues);
    $result = $stmt->execute ();
    if (!$result) {
        die (__LINE__ . " " . $db->lastErrorMsg () . "\n");
    }
    return $result;
}
function dbquery ($line, $db, $sql, $bindvalues) {
    $result = dbexec ($line, $db, $sql, $bindvalues);
    return $result;
}
dbexec (__LINE__, $db, "CREATE TABLE IF NOT EXISTS rc (uid INTEGER PRIMARY KEY, sid CHAR(32) UNIQUE, status INT DEFAULT 0, data TEXT, url TEXT)", array ());
?>