Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //Script Settings
- error_reporting ( 0 );
- set_time_limit ( 0 );
- //Unused Config Lines
- //$readbuffer="";
- //$realname = "SQLBot";
- //$oldcat = sxGetCat("Requests for unblock", 3);
- //$newcat = $oldcat;
- //$ident="SQLBot";
- //$chan="#wikipedia-en-unblock";
- //$chan="#SQLTest";
- //Initilize errno and errstr variables with the empty string
- $errno = '';
- $errstr = '';
- //Credit for this code goes out to User:SQL for giving me an example of a working IRC bot :D
- //Config Lines
- $host = 'irc.freenode.net'; //What server do you want the bot to connect to? (IP or Hostname)
- $host2 = 'browne.wikimedia.org';
- $port = 6667; //What port do you want the bot to run on?
- $nick = 'NNBot'; //What do you want the bot to be called?
- $host1_chan = '#nnbot'; //channel on IRC server you'd like to join
- $host2_chan = '#en.wikipedia';
- $client_address = '74.238.41.78'; //IP or hostname of where you are connecting from (Used in USER command)
- $nickserv_registered = 'No'; //Is the nick your using registered with nickserv and would you like to login? (If yes must be "Yes")
- $nickserv_pass = ''; //What is the password of your nick?
- $mysql_server = 'localhost:3306'; //IP Address and port of mysql server to connect to
- $mysql_user = 'nnbot';
- $mysql_pass = '********';
- $mysql_dbname = 'nnbot'; //What is the name of the database which is going to be storing the bot info?
- $fp = fsockopen ( $host, $port, $errno, $errstr, 30 ); //lets try to connect to the IRC server
- //Lets connect to a database for our parsing of teh STREAMZ0RZ
- mysql_connect ( $mysql_server, $mysql_user, $mysql_pass ) or die ( 'Database Connection Error' . mysql_error () );
- echo "Sucessfully Connected to MySQL Server \n";
- mysql_select_db ( 'nnbot' ) or die ( "Can't Find Database:" . mysql_error () );
- echo "I am ready to write to the database\n";
- if (! $fp) {
- //if there was a problem connecting we want to know what is going on
- echo $errstr . " (" . $errno . ")<br />\n";
- } else {
- //No Error Connecting? We must be Connected Successfully
- echo "Successfully Connected to $host \n";
- //Lets give the IRC Server Our Nick and wait 5 seconds to give the IRC server some time to process it
- fwrite ( $fp, "USER " . $nick . " 74.237.227.159 * :" . $nick . "\r\n" );
- fwrite ( $fp, "NICK " . $nick . "\r\n" );
- sleep ( 2 );
- //Lets login to nickserv if we are registered
- if ($nickserv_registered == 'Yes') {
- fwrite ( $fp, "PRIVMSG Nickserv :identify" . $nickserv_pass . "\r\n" );
- }
- //Lets join our channel
- fwrite ( $fp, "JOIN :" . $host1_chan . "\r\n" );
- echo "Joined Channel $host1_chan \n";
- sleep ( 1 );
- /*
- fwrite ( $fp, "JOIN : ##addshore \r\n" );
- echo "Joined Channel ##addshore \n";
- sleep ( 1 );
- */
- /*
- //Lets let everyone know we are here
- fwrite ( $fp, "PRIVMSG " . $host1_chan . " : nn123645: I'm Here!\r\n" );
- echo "I said something \n";
- //sleep ( 2 );*/
- //Lets try to connect to server number 2
- $fp2 = fsockopen ( $host2, $port, $errno, $errstr, 30 );
- if (! $fp2) {
- //if there was a problem connecting we want to know what is going on
- echo $errstr . " (" . $errno . ")<br />\n";
- } else {
- //No Error Connecting? We must be Connected Successfully
- echo "Successfully Connected to $host2 \n";
- //Lets give the IRC Server Our Nick and wait 5 seconds to give the IRC server some time to process it
- fwrite ( $fp2, "USER " . $nick . " 74.237.227.159 * :" . $nick . "\r\n" );
- fwrite ( $fp2, "NICK " . $nick . "\r\n" );
- sleep ( 2 );
- //Lets join our channel
- fwrite ( $fp2, "JOIN :" . $host2_chan . "\r\n" );
- echo "Joined Channel $host2_chan \n";
- //sleep ( 1 );
- /*
- //Lets Let My Master Know I'm here
- fwrite ( $fp2, "PRIVMSG nn123645 : Hey I'm connected! \r\n" );
- echo "Messaged Owner \n";
- //sleep ( 3 );*/
- }
- //Lets make a variable for the log name based on the date
- $log1_name = 'logs/' . date ( 'm-d-Y' ) . '_wikimedia.txt';
- $log2_name = 'logs/' . date ( 'm-d-Y' ) . '_freenode.txt';
- $log3_name = 'logs/' . date ( 'm-d-Y' ) . '_wikimedia_test.txt';
- //Lets check if the logfile for stream 1 exists
- if (file_exists ( "$log1_name" )) {
- $logfile1 = fopen ( "$log1_name", "a" );
- echo "Opened Log for Wikimedia \n";
- } else {
- $logfile1 = fopen ( "$log1_name", "x" );
- echo "Created Log for Wikimedia \n";
- }
- //Lets check if the logfile for stream 2 exists
- if (file_exists ( "$log2_name" )) {
- $logfile2 = fopen ( "$log2_name", "a" );
- echo "Opened Log for Freenode \n";
- } else {
- $logfile2 = fopen ( "$log2_name", "x" );
- echo "Created Log for Freenode \n";
- }
- //Lets make another log file for testing the parseing stream
- if (file_exists ( "$log3_name" )) {
- $logfile3 = fopen ( "$log3_name", "a" );
- echo "Opened Log for Wikimedia Test Output 1 \n";
- } else {
- $logfile3 = fopen ( "$log3_name", "x" );
- echo "Created Log for Wikimedia Test Output 1 \n";
- }
- //We don't want PHP to wait around for something to complete in the sock stream (the irc feed)
- //so lets make sure we have non-blocking mode set
- stream_set_blocking ( $fp, 0 );
- //Lets do it for the other stream
- stream_set_blocking ( $fp2, 0 );
- //fwrite($logfile, "================================================================\n
- //=============================New Bot Run======================================\n
- //==============================================================================\n");
- //Lets force an (almost) infinite loop to keep the bot running
- while ( TRUE ) {
- /*
- //Lets Check to make sure there aren't any connection errors
- if (! feof ( $fp )) {
- fclose($fp);
- echo "Close Stream 1 \n";
- fclose($fp2);
- echo "Close Stream 2 \n";
- die('Connection Error');
- }elseif(! feof ( $fp2 )){
- fclose($fp);
- echo "Close Stream 1 \n";
- fclose($fp2);
- echo "Close Stream 2 \n";
- die('Connection Error');
- }
- */
- //Lets write a log for wikimedia
- $ircline1 = fgets ( $fp2, 1024 );
- //fwrite ( $logfile1, $ircline1 );
- //Lets write a log for freenode
- $ircline2 = fgets ( $fp, 1024 );
- //fwrite ( $logfile2, $ircline2 );
- //Lets explode the stream and look for pings so we don't get disconnected
- $ex1 = explode ( ' ', $ircline1 );
- if ($ex1 [0] == 'PING') {
- fwrite ( $fp2, 'PONG ' . $ex1 [1] );
- echo "Answerered Wikimedia Ping \n";
- }
- //Now lets do it for freenode
- $ex2 = explode ( ' ', $ircline2 );
- if ($ex2 [0] == 'PING') {
- fwrite ( $fp, 'PONG ' . $ex2 [1] );
- echo "Answerered Freenode Ping \n";
- }
- //fwrite ( $logfile3, $ircline1 );
- //These next four lines of code come from cluebot, thanks cobi :)
- //Lets parse and split the line into something that PHP can actually use
- $parsedline = str_replace ( '\n', '', $ircline1 );
- $parsedline = str_replace ( '\r', '', $ircline1 );
- $parsedline = str_replace ( '\002', '', $ircline1 );
- $parsedline = preg_split ( '%\003(\d\d?(,\d\d?)?)?%', $parsedline );
- // Lets Write a log file of the parsed feed (used for testing)
- $parsedlinenum = 1;
- while ( isset ( $parsedline ["$parsedlinenum"] ) ) {
- fwrite ( $logfile3, "Value Number $parsedlinenum: $parsedline[$parsedlinenum] \n" );
- $parsedlinenum ++;
- }
- //Lets check if the page is new, and if it is lets make an SQL query to add it to database
- if (preg_match ( '%N%', $parsedline [4] )) {
- //When I get a chance I need to change this to boolean rather than integers
- //Lets check to see if the edit is minor
- if (preg_match ( '%M%', $parsedline [4] )) {
- $minor_edit = 1;
- } else {
- $minor_edit = 0;
- }
- //Lets check to see if the edit was made by a bot flagged account
- if (preg_match ( '%B%', $parsedline [4] )) {
- $bot_edit = 1;
- } else {
- $bot_edit = 0;
- }
- //Lets check to make sure that the page is not in any space other than the mainspace
- switch ( TRUE) {
- case preg_match ( '%User talk:%', $parsedline [2] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Talk:%', $parsedline [2] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%User:%', $parsedline [2] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Wikipedia:%', $parsedline [2] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Wikipedia talk:%', $parsedline [2] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Image:%', $parsedline [2] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Image talk:%', $parsedline [2] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Template:%', $parsedline [2] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Template talk:%', $parsedline [2] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Category:%', $parsedline [2] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Category talk:%', $parsedline [2] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Portal:%', $parsedline [2] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Portal talk:%', $parsedline [2] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Mediwiki:%', $parsedline [2] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Mediawiki talk:%', $parsedline [2] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Help:%', $parsedline [2] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Help talk:%', $parsedline [2] ) :
- $in_mainspace = FALSE;
- break;
- default :
- $in_mainspace = TRUE;
- }
- //If its in the mainspace lets write an SQL query
- //if its not, lets ignore it and continue happily
- if ($in_mainspace) {
- //Since the IRC feed doesn't have the time in it lets use the current unix time stamp
- $current_time = time ();
- //We don't want SQL Injection to be a problem, lets escape our string
- $parsedline [2] = mysql_escape_string ( $parsedline [2] );
- $parsedline [10] = mysql_escape_string ( $parsedline [10] );
- $query = "INSERT INTO pending (page_name, page_create_time, page_creator, bot_is_creator, edit_is_minor)
- VALUES (\"$parsedline[2]\", $current_time, \"$parsedline[10]\", $bot_edit, $minor_edit)";
- echo "$query \n";
- if (! mysql_query ( $query )) {
- echo mysql_error ();
- }
- //echo "Added Row to Database \n";
- //fwrite ( $logfile3, "Wrote Database Query \n" );
- }
- }
- //If the log has delete in it lets remove the page that was deleted
- //provided it is in the mainspace
- if (preg_match ( '%delete%', $parsedline [4] ) ) {
- //Lets check to make sure that the page is not in any space other than the mainspace
- switch ( TRUE) {
- case preg_match ( '%User talk:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Talk:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%User:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Wikipedia:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Wikipedia talk:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Image:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Image talk:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Template:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Template talk:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Category:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Category talk:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Portal:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Portal talk:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Mediwiki:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Mediawiki talk:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Help:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Help talk:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- default :
- $in_mainspace = TRUE;
- }
- if ($in_mainspace) {
- $parsedline [15] = mysql_escape_string ( $parsedline [15] );
- $query = "DELETE FROM pending
- WHERE page_name = \"$parsedline[15]\"";
- echo "$query \n";
- if (! mysql_query ( $query )) {
- echo mysql_error ();
- }
- //echo "Removed Row from Database \n";
- //fwrite ( $logfile3, "Wrote Database Query \n" );
- }
- }
- if (preg_match ( '%restore%', $parsedline [4] )) {
- //Lets check to make sure that the page is not in any space other than the mainspace
- switch ( TRUE ) {
- case preg_match ( '%User talk:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Talk:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%User:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Wikipedia:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Wikipedia talk:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Image:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Image talk:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Template:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Template talk:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Category:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Category talk:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Portal:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Portal talk:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Mediwiki:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Mediawiki talk:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Help:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- case preg_match ( '%Help talk:%', $parsedline [15] ) :
- $in_mainspace = FALSE;
- break;
- default :
- $in_mainspace = TRUE;
- }
- if ($in_mainspace) {
- //If its in the mainspace lets write an SQL query
- //if its not, lets ignore it and continue happily
- if ($in_mainspace) {
- //Since the IRC feed doesn't have the time in it lets use the current unix time stamp
- $current_time = time ();
- //We don't want SQL Injection to be a problem, lets escape our string
- $parsedline [15] = mysql_escape_string ( $parsedline [15-] );
- $parsedline [10] = mysql_escape_string ( $parsedline [10] );
- $query = "INSERT INTO pending (page_name, page_create_time, page_creator, bot_is_creator, edit_is_minor)
- VALUES (\"$parsedline[2]\", $current_time, \"$parsedline[10]\", $bot_edit, $minor_edit)";
- echo "Restored $query \n";
- if (! mysql_query ( $query )) {
- echo mysql_error ();
- }
- //echo "Added Row to Database \n";
- //fwrite ( $logfile3, "Wrote Database Query \n" );
- }
- }
- }
- /*
- $ex3 = explode ( ' ', $parsedline );
- $ex3num = 1;
- while ( isset ( $ex3 ["$ex3num"] ) ) {
- fwrite ( $logfile3, "Value Number $ex3num: $ex3[$ex3num] \n" );
- echo "$ex3num \n";
- $ex3num ++;
- }
- fwrite( $logfile3 , "$ex3[1]");
- echo "$ex3[1] \n";
- */
- usleep ( 200000 );
- }
- //Stuff after here doesn't really get used, just extra code that I need to sort later
- //Loop Ended? There is a connection problem with one of the servers
- //Lets Leave properly
- fwrite ( $fp, "QUIT : NNBot is Disconnecting \r\n" );
- fwrite ( $fp2, "QUIT : NNBot is Disconnecting \r\n" );
- }
- //Lets Close our connection the the IRC servers and print out a message
- fclose ( $fp );
- fclose ( $fp2 );
- echo 'Disconnected from server';
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement