Advertisement
Guest User

Untitled

a guest
May 8th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.34 KB | None | 0 0
  1. <?php
  2. //Script Settings
  3. error_reporting ( 0 );
  4. set_time_limit ( 0 );
  5.  
  6. //Unused Config Lines
  7. //$readbuffer="";
  8. //$realname = "SQLBot";
  9. //$oldcat = sxGetCat("Requests for unblock", 3);
  10. //$newcat = $oldcat;
  11. //$ident="SQLBot";
  12. //$chan="#wikipedia-en-unblock";
  13. //$chan="#SQLTest";
  14.  
  15.  
  16. //Initilize errno and errstr variables with the empty string
  17. $errno = '';
  18. $errstr = '';
  19.  
  20. //Credit for this code goes out to User:SQL for giving me an example of a working IRC bot :D
  21. //Config Lines
  22. $host = 'irc.freenode.net'; //What server do you want the bot to connect to? (IP or Hostname)
  23. $host2 = 'browne.wikimedia.org';
  24. $port = 6667; //What port do you want the bot to run on?
  25. $nick = 'NNBot'; //What do you want the bot to be called?
  26. $host1_chan = '#nnbot'; //channel on IRC server you'd like to join
  27. $host2_chan = '#en.wikipedia';
  28. $client_address = '74.238.41.78'; //IP or hostname of where you are connecting from (Used in USER command)
  29. $nickserv_registered = 'No'; //Is the nick your using registered with nickserv and would you like to login? (If yes must be "Yes")
  30. $nickserv_pass = ''; //What is the password of your nick?
  31. $mysql_server = 'localhost:3306'; //IP Address and port of mysql server to connect to
  32. $mysql_user = 'nnbot';
  33. $mysql_pass = '********';
  34. $mysql_dbname = 'nnbot'; //What is the name of the database which is going to be storing the bot info?
  35. $fp = fsockopen ( $host, $port, $errno, $errstr, 30 ); //lets try to connect to the IRC server
  36.  
  37.  
  38. //Lets connect to a database for our parsing of teh STREAMZ0RZ
  39. mysql_connect ( $mysql_server, $mysql_user, $mysql_pass ) or die ( 'Database Connection Error' . mysql_error () );
  40. echo "Sucessfully Connected to MySQL Server \n";
  41. mysql_select_db ( 'nnbot' ) or die ( "Can't Find Database:" . mysql_error () );
  42. echo "I am ready to write to the database\n";
  43.  
  44. if (! $fp) {
  45.     //if there was a problem connecting we want to know what is going on
  46.     echo $errstr . " (" . $errno . ")<br />\n";
  47. } else {
  48.     //No Error Connecting? We must be Connected Successfully
  49.     echo "Successfully Connected to $host \n";
  50.    
  51.     //Lets give the IRC Server Our Nick and wait 5 seconds to give the IRC server some time to process it
  52.     fwrite ( $fp, "USER " . $nick . " 74.237.227.159 * :" . $nick . "\r\n" );
  53.     fwrite ( $fp, "NICK " . $nick . "\r\n" );
  54.     sleep ( 2 );
  55.    
  56.     //Lets login to nickserv if we are registered
  57.     if ($nickserv_registered == 'Yes') {
  58.         fwrite ( $fp, "PRIVMSG Nickserv :identify" . $nickserv_pass . "\r\n" );
  59.     }
  60.    
  61.     //Lets join our channel
  62.     fwrite ( $fp, "JOIN :" . $host1_chan . "\r\n" );
  63.     echo "Joined Channel $host1_chan \n";
  64.     sleep ( 1 );
  65.    
  66.     /*
  67.     fwrite ( $fp, "JOIN : ##addshore \r\n" );
  68.     echo "Joined Channel ##addshore \n";
  69.     sleep ( 1 );
  70.     */
  71.     /*
  72.     //Lets let everyone know we are here
  73.     fwrite ( $fp, "PRIVMSG " . $host1_chan . " : nn123645: I'm Here!\r\n" );
  74.     echo "I said something \n";
  75.     //sleep ( 2 );*/
  76.    
  77.     //Lets try to connect to server number 2
  78.     $fp2 = fsockopen ( $host2, $port, $errno, $errstr, 30 );
  79.    
  80.     if (! $fp2) {
  81.         //if there was a problem connecting we want to know what is going on
  82.         echo $errstr . " (" . $errno . ")<br />\n";
  83.     } else {
  84.         //No Error Connecting? We must be Connected Successfully
  85.         echo "Successfully Connected to $host2 \n";
  86.        
  87.         //Lets give the IRC Server Our Nick and wait 5 seconds to give the IRC server some time to process it
  88.         fwrite ( $fp2, "USER " . $nick . " 74.237.227.159 * :" . $nick . "\r\n" );
  89.         fwrite ( $fp2, "NICK " . $nick . "\r\n" );
  90.         sleep ( 2 );
  91.        
  92.         //Lets join our channel
  93.         fwrite ( $fp2, "JOIN :" . $host2_chan . "\r\n" );
  94.         echo "Joined Channel $host2_chan \n";
  95.         //sleep ( 1 );
  96.    
  97.  
  98.     /*
  99.         //Lets Let My Master Know I'm here
  100.         fwrite ( $fp2, "PRIVMSG nn123645 : Hey I'm connected! \r\n" );
  101.         echo "Messaged Owner \n";
  102.         //sleep ( 3 );*/
  103.     }
  104.    
  105.     //Lets make a variable for the log name based on the date
  106.     $log1_name = 'logs/' . date ( 'm-d-Y' ) . '_wikimedia.txt';
  107.     $log2_name = 'logs/' . date ( 'm-d-Y' ) . '_freenode.txt';
  108.     $log3_name = 'logs/' . date ( 'm-d-Y' ) . '_wikimedia_test.txt';
  109.    
  110.     //Lets check if the logfile for stream 1 exists
  111.     if (file_exists ( "$log1_name" )) {
  112.         $logfile1 = fopen ( "$log1_name", "a" );
  113.         echo "Opened Log for Wikimedia \n";
  114.     } else {
  115.         $logfile1 = fopen ( "$log1_name", "x" );
  116.         echo "Created Log for Wikimedia \n";
  117.     }
  118.    
  119.     //Lets check if the logfile for stream 2 exists
  120.     if (file_exists ( "$log2_name" )) {
  121.         $logfile2 = fopen ( "$log2_name", "a" );
  122.         echo "Opened Log for Freenode \n";
  123.     } else {
  124.         $logfile2 = fopen ( "$log2_name", "x" );
  125.         echo "Created Log for Freenode \n";
  126.     }
  127.    
  128.     //Lets make another log file for testing the parseing stream
  129.     if (file_exists ( "$log3_name" )) {
  130.         $logfile3 = fopen ( "$log3_name", "a" );
  131.         echo "Opened Log for Wikimedia Test Output 1 \n";
  132.     } else {
  133.         $logfile3 = fopen ( "$log3_name", "x" );
  134.         echo "Created Log for Wikimedia Test Output 1 \n";
  135.     }
  136.    
  137.     //We don't want PHP to wait around for something to complete in the sock stream (the irc feed)
  138.     //so lets make sure we have non-blocking mode set
  139.     stream_set_blocking ( $fp, 0 );
  140.     //Lets do it for the other stream
  141.     stream_set_blocking ( $fp2, 0 );
  142.    
  143.     //fwrite($logfile, "================================================================\n
  144.     //=============================New Bot Run======================================\n
  145.     //==============================================================================\n");
  146.    
  147.  
  148.     //Lets force an (almost) infinite loop to keep the bot running
  149.     while ( TRUE ) {
  150.         /*
  151.         //Lets Check to make sure there aren't any connection errors
  152.         if (! feof ( $fp )) {
  153.             fclose($fp);
  154.             echo "Close Stream 1 \n";
  155.             fclose($fp2);
  156.             echo "Close Stream 2 \n";
  157.             die('Connection Error');
  158.         }elseif(! feof ( $fp2 )){
  159.             fclose($fp);
  160.             echo "Close Stream 1 \n";
  161.             fclose($fp2);
  162.             echo "Close Stream 2 \n";
  163.             die('Connection Error');
  164.         }
  165.         */
  166.         //Lets write a log for wikimedia
  167.         $ircline1 = fgets ( $fp2, 1024 );
  168.         //fwrite ( $logfile1, $ircline1 );
  169.        
  170.         //Lets write a log for freenode
  171.         $ircline2 = fgets ( $fp, 1024 );
  172.         //fwrite ( $logfile2, $ircline2 );
  173.        
  174.         //Lets explode the stream and look for pings so we don't get disconnected
  175.         $ex1 = explode ( ' ', $ircline1 );
  176.         if ($ex1 [0] == 'PING') {
  177.             fwrite ( $fp2, 'PONG ' . $ex1 [1] );
  178.             echo "Answerered Wikimedia Ping \n";
  179.         }
  180.        
  181.         //Now lets do it for freenode
  182.         $ex2 = explode ( ' ', $ircline2 );
  183.         if ($ex2 [0] == 'PING') {
  184.             fwrite ( $fp, 'PONG ' . $ex2 [1] );
  185.             echo "Answerered Freenode Ping \n";
  186.         }
  187.        
  188.         //fwrite ( $logfile3, $ircline1 );
  189.         //These next four lines of code come from cluebot, thanks cobi :)
  190.         //Lets parse and split the line into something that PHP can actually use
  191.         $parsedline = str_replace ( '\n', '', $ircline1 );
  192.         $parsedline = str_replace ( '\r', '', $ircline1 );
  193.         $parsedline = str_replace ( '\002', '', $ircline1 );
  194.         $parsedline = preg_split ( '%\003(\d\d?(,\d\d?)?)?%', $parsedline );
  195.         // Lets Write a log file of the parsed feed (used for testing)
  196.         $parsedlinenum = 1;
  197.         while ( isset ( $parsedline ["$parsedlinenum"] ) ) {
  198.             fwrite ( $logfile3, "Value Number $parsedlinenum: $parsedline[$parsedlinenum] \n" );
  199.             $parsedlinenum ++;
  200.         }
  201.        
  202.         //Lets check if the page is new, and if it is lets make an SQL query to add it to database
  203.         if (preg_match ( '%N%', $parsedline [4] )) {
  204.             //When I get a chance I need to change this to boolean rather than integers
  205.             //Lets check to see if the edit is minor
  206.             if (preg_match ( '%M%', $parsedline [4] )) {
  207.                 $minor_edit = 1;
  208.             } else {
  209.                 $minor_edit = 0;
  210.             }
  211.            
  212.             //Lets check to see if the edit was made by a bot flagged account
  213.             if (preg_match ( '%B%', $parsedline [4] )) {
  214.                 $bot_edit = 1;
  215.             } else {
  216.                 $bot_edit = 0;
  217.             }
  218.            
  219.             //Lets check to make sure that the page is not in any space other than the mainspace
  220.             switch ( TRUE) {
  221.                 case preg_match ( '%User talk:%', $parsedline [2] ) :
  222.                     $in_mainspace = FALSE;
  223.                     break;
  224.                 case preg_match ( '%Talk:%', $parsedline [2] ) :
  225.                     $in_mainspace = FALSE;
  226.                     break;
  227.                 case preg_match ( '%User:%', $parsedline [2] ) :
  228.                     $in_mainspace = FALSE;
  229.                     break;
  230.                 case preg_match ( '%Wikipedia:%', $parsedline [2] ) :
  231.                     $in_mainspace = FALSE;
  232.                     break;
  233.                 case preg_match ( '%Wikipedia talk:%', $parsedline [2] ) :
  234.                     $in_mainspace = FALSE;
  235.                     break;
  236.                 case preg_match ( '%Image:%', $parsedline [2] ) :
  237.                     $in_mainspace = FALSE;
  238.                     break;
  239.                 case preg_match ( '%Image talk:%', $parsedline [2] ) :
  240.                     $in_mainspace = FALSE;
  241.                     break;
  242.                 case preg_match ( '%Template:%', $parsedline [2] ) :
  243.                     $in_mainspace = FALSE;
  244.                     break;
  245.                 case preg_match ( '%Template talk:%', $parsedline [2] ) :
  246.                     $in_mainspace = FALSE;
  247.                     break;
  248.                 case preg_match ( '%Category:%', $parsedline [2] ) :
  249.                     $in_mainspace = FALSE;
  250.                     break;
  251.                 case preg_match ( '%Category talk:%', $parsedline [2] ) :
  252.                     $in_mainspace = FALSE;
  253.                     break;
  254.                 case preg_match ( '%Portal:%', $parsedline [2] ) :
  255.                     $in_mainspace = FALSE;
  256.                     break;
  257.                 case preg_match ( '%Portal talk:%', $parsedline [2] ) :
  258.                     $in_mainspace = FALSE;
  259.                     break;
  260.                 case preg_match ( '%Mediwiki:%', $parsedline [2] ) :
  261.                     $in_mainspace = FALSE;
  262.                     break;
  263.                 case preg_match ( '%Mediawiki talk:%', $parsedline [2] ) :
  264.                     $in_mainspace = FALSE;
  265.                     break;
  266.                 case preg_match ( '%Help:%', $parsedline [2] ) :
  267.                     $in_mainspace = FALSE;
  268.                     break;
  269.                 case preg_match ( '%Help talk:%', $parsedline [2] ) :
  270.                     $in_mainspace = FALSE;
  271.                     break;
  272.                 default :
  273.                     $in_mainspace = TRUE;
  274.             }
  275.            
  276.             //If its in the mainspace lets write an SQL query
  277.             //if its not, lets ignore it and continue happily
  278.             if ($in_mainspace) {
  279.                 //Since the IRC feed doesn't have the time in it lets use the current unix time stamp
  280.                 $current_time = time ();
  281.                
  282.                 //We don't want SQL Injection to be a problem, lets escape our string
  283.                 $parsedline [2] = mysql_escape_string ( $parsedline [2] );
  284.                 $parsedline [10] = mysql_escape_string ( $parsedline [10] );
  285.                
  286.                 $query = "INSERT INTO pending (page_name, page_create_time, page_creator, bot_is_creator, edit_is_minor)
  287.                 VALUES (\"$parsedline[2]\", $current_time, \"$parsedline[10]\", $bot_edit, $minor_edit)";
  288.                
  289.                 echo "$query \n";
  290.                 if (! mysql_query ( $query )) {
  291.                     echo mysql_error ();
  292.                 }
  293.                
  294.             //echo "Added Row to Database \n";
  295.             //fwrite ( $logfile3, "Wrote Database Query \n" );
  296.             }
  297.         }
  298.        
  299.         //If the log has delete in it lets remove the page that was deleted
  300.         //provided it is in the mainspace
  301.         if (preg_match ( '%delete%', $parsedline [4] ) ) {
  302.            
  303.             //Lets check to make sure that the page is not in any space other than the mainspace
  304.             switch ( TRUE) {
  305.                 case preg_match ( '%User talk:%', $parsedline [15] ) :
  306.                     $in_mainspace = FALSE;
  307.                     break;
  308.                 case preg_match ( '%Talk:%', $parsedline [15] ) :
  309.                     $in_mainspace = FALSE;
  310.                     break;
  311.                 case preg_match ( '%User:%', $parsedline [15] ) :
  312.                     $in_mainspace = FALSE;
  313.                     break;
  314.                 case preg_match ( '%Wikipedia:%', $parsedline [15] ) :
  315.                     $in_mainspace = FALSE;
  316.                     break;
  317.                 case preg_match ( '%Wikipedia talk:%', $parsedline [15] ) :
  318.                     $in_mainspace = FALSE;
  319.                     break;
  320.                 case preg_match ( '%Image:%', $parsedline [15] ) :
  321.                     $in_mainspace = FALSE;
  322.                     break;
  323.                 case preg_match ( '%Image talk:%', $parsedline [15] ) :
  324.                     $in_mainspace = FALSE;
  325.                     break;
  326.                 case preg_match ( '%Template:%', $parsedline [15] ) :
  327.                     $in_mainspace = FALSE;
  328.                     break;
  329.                 case preg_match ( '%Template talk:%', $parsedline [15] ) :
  330.                     $in_mainspace = FALSE;
  331.                     break;
  332.                 case preg_match ( '%Category:%', $parsedline [15] ) :
  333.                     $in_mainspace = FALSE;
  334.                     break;
  335.                 case preg_match ( '%Category talk:%', $parsedline [15] ) :
  336.                     $in_mainspace = FALSE;
  337.                     break;
  338.                 case preg_match ( '%Portal:%', $parsedline [15] ) :
  339.                     $in_mainspace = FALSE;
  340.                     break;
  341.                 case preg_match ( '%Portal talk:%', $parsedline [15] ) :
  342.                     $in_mainspace = FALSE;
  343.                     break;
  344.                 case preg_match ( '%Mediwiki:%', $parsedline [15] ) :
  345.                     $in_mainspace = FALSE;
  346.                     break;
  347.                 case preg_match ( '%Mediawiki talk:%', $parsedline [15] ) :
  348.                     $in_mainspace = FALSE;
  349.                     break;
  350.                 case preg_match ( '%Help:%', $parsedline [15] ) :
  351.                     $in_mainspace = FALSE;
  352.                     break;
  353.                 case preg_match ( '%Help talk:%', $parsedline [15] ) :
  354.                     $in_mainspace = FALSE;
  355.                     break;
  356.                 default :
  357.                     $in_mainspace = TRUE;
  358.             }
  359.             if ($in_mainspace) {
  360.                
  361.                 $parsedline [15] = mysql_escape_string ( $parsedline [15] );
  362.                
  363.                 $query = "DELETE FROM pending
  364.                 WHERE page_name = \"$parsedline[15]\"";
  365.                
  366.                 echo "$query \n";
  367.                 if (! mysql_query ( $query )) {
  368.                     echo mysql_error ();
  369.                 }
  370.                
  371.             //echo "Removed Row from Database \n";
  372.             //fwrite ( $logfile3, "Wrote Database Query \n" );
  373.             }
  374.         }
  375.        
  376.         if (preg_match ( '%restore%', $parsedline [4] )) {
  377.            
  378.             //Lets check to make sure that the page is not in any space other than the mainspace
  379.             switch ( TRUE ) {
  380.                 case preg_match ( '%User talk:%', $parsedline [15] ) :
  381.                     $in_mainspace = FALSE;
  382.                     break;
  383.                 case preg_match ( '%Talk:%', $parsedline [15] ) :
  384.                     $in_mainspace = FALSE;
  385.                     break;
  386.                 case preg_match ( '%User:%', $parsedline [15] ) :
  387.                     $in_mainspace = FALSE;
  388.                     break;
  389.                 case preg_match ( '%Wikipedia:%', $parsedline [15] ) :
  390.                     $in_mainspace = FALSE;
  391.                     break;
  392.                 case preg_match ( '%Wikipedia talk:%', $parsedline [15] ) :
  393.                     $in_mainspace = FALSE;
  394.                     break;
  395.                 case preg_match ( '%Image:%', $parsedline [15] ) :
  396.                     $in_mainspace = FALSE;
  397.                     break;
  398.                 case preg_match ( '%Image talk:%', $parsedline [15] ) :
  399.                     $in_mainspace = FALSE;
  400.                     break;
  401.                 case preg_match ( '%Template:%', $parsedline [15] ) :
  402.                     $in_mainspace = FALSE;
  403.                     break;
  404.                 case preg_match ( '%Template talk:%', $parsedline [15] ) :
  405.                     $in_mainspace = FALSE;
  406.                     break;
  407.                 case preg_match ( '%Category:%', $parsedline [15] ) :
  408.                     $in_mainspace = FALSE;
  409.                     break;
  410.                 case preg_match ( '%Category talk:%', $parsedline [15] ) :
  411.                     $in_mainspace = FALSE;
  412.                     break;
  413.                 case preg_match ( '%Portal:%', $parsedline [15] ) :
  414.                     $in_mainspace = FALSE;
  415.                     break;
  416.                 case preg_match ( '%Portal talk:%', $parsedline [15] ) :
  417.                     $in_mainspace = FALSE;
  418.                     break;
  419.                 case preg_match ( '%Mediwiki:%', $parsedline [15] ) :
  420.                     $in_mainspace = FALSE;
  421.                     break;
  422.                 case preg_match ( '%Mediawiki talk:%', $parsedline [15] ) :
  423.                     $in_mainspace = FALSE;
  424.                     break;
  425.                 case preg_match ( '%Help:%', $parsedline [15] ) :
  426.                     $in_mainspace = FALSE;
  427.                     break;
  428.                 case preg_match ( '%Help talk:%', $parsedline [15] ) :
  429.                     $in_mainspace = FALSE;
  430.                     break;
  431.                 default :
  432.                     $in_mainspace = TRUE;
  433.             }
  434.             if ($in_mainspace) {
  435.                
  436.                 //If its in the mainspace lets write an SQL query
  437.                 //if its not, lets ignore it and continue happily
  438.                 if ($in_mainspace) {
  439.                     //Since the IRC feed doesn't have the time in it lets use the current unix time stamp
  440.                     $current_time = time ();
  441.                    
  442.                     //We don't want SQL Injection to be a problem, lets escape our string
  443.                     $parsedline [15] = mysql_escape_string ( $parsedline [15-] );
  444.                     $parsedline [10] = mysql_escape_string ( $parsedline [10] );
  445.                    
  446.                     $query = "INSERT INTO pending (page_name, page_create_time, page_creator, bot_is_creator, edit_is_minor)
  447.                     VALUES (\"$parsedline[2]\", $current_time, \"$parsedline[10]\", $bot_edit, $minor_edit)";
  448.                    
  449.                     echo "Restored $query \n";
  450.                     if (! mysql_query ( $query )) {
  451.                         echo mysql_error ();
  452.                     }
  453.                    
  454.                 //echo "Added Row to Database \n";
  455.                 //fwrite ( $logfile3, "Wrote Database Query \n" );
  456.                 }
  457.             }
  458.         }
  459.        
  460.         /*
  461.         $ex3 = explode ( ' ', $parsedline );
  462.         $ex3num = 1;
  463.         while ( isset ( $ex3 ["$ex3num"] ) ) {
  464.             fwrite ( $logfile3, "Value Number $ex3num: $ex3[$ex3num] \n" );
  465.             echo "$ex3num \n";
  466.             $ex3num ++;
  467.         }
  468.  
  469.         fwrite( $logfile3 , "$ex3[1]");
  470.         echo "$ex3[1] \n";
  471.         */
  472.         usleep ( 200000 );
  473.     }
  474.    
  475.     //Stuff after here doesn't really get used, just extra code that I need to sort later
  476.     //Loop Ended? There is a connection problem with one of the servers
  477.     //Lets Leave properly
  478.     fwrite ( $fp, "QUIT : NNBot is Disconnecting \r\n" );
  479.     fwrite ( $fp2, "QUIT : NNBot is Disconnecting \r\n" );
  480. }
  481.  
  482. //Lets Close our connection the the IRC servers and print out a message
  483. fclose ( $fp );
  484. fclose ( $fp2 );
  485. echo 'Disconnected from server';
  486.  
  487. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement