<?php
/*###################################################
Ross Markham's PHP bot v1.0         (^.^)____\/m    #
----------------------------------------------------#
                <-_,~about~,_->                     #
Ross Markham's PHP IRC bot was made for a challenge #
& pure fun. It has a few features. private message  #
the bot for a command list. It was written to be as #
easy as possable to update/improve. feel free to    #
use and modify as you see fit.                      #
###################################################*/


set_time_limit(0);  //Stop the script timing out

$server = "irc.somewhere.com";        //server to connect to
$channel = "#bottest";               //channel to connect to initialy
$password = "password";             //password for bot to login to irc
$pass2 = "password";               //password to make the bot do stuff
$users[0] = "0";                  //array of logged in users
$nickname = "[bot1]";            //Set the bot's nick here
$logger = FALSE;                //for the channel logger
$takeover = FALSE;             //for the auto banner


$socket=fsockopen($server,'6667') ; //Connect and join the channel

stream_set_timeout($socket, 300);  //Set a timeout value (so the bot quits if it's disconnected)
fwrite($socket, "NICK ".$nickname."\r\n");
fwrite($socket, "USER ".$nickname." 8 * ::\x01VERSON 1.0 Ross's bot\x01\n");  //read rfc 1459 to understand this line

  while ($line=fgets($socket)) {
     echo htmlentities($line)."<br>"; 
       if (strpos($line, "433")>0) die("error nick in use");  //Quit if bot's nick is already taken (irc code 433 is received)
 
       if (strpos($line, "004")>0) {
          fwrite($socket, "JOIN ".$channel."\r\n"); //Join the channel if everything is ok (irc code 004 is received)
          break;
       }
  }

  //Read messages and respond appropriately
  while ($line=fgets($socket)) {
     echo htmlentities($line)."<br>";  //output the line to the screen
   
     //Respond to pings
     if (strpos($line,"PING :")===0) {
         fwrite($socket, "PONG :".substr($line,6)."\r\n");
         continue;
     }      
     //respond to ctcp version
     if (strpos($line, "PRIVMSG ".$nickname." :\001VERSION\001\r\n")>0){
         $nick = substr($line, 0, strpos($line, "!"));
         $nick = str_replace(":", "", $nick );
         fwrite($socket, "NOTICE ".$nick." :\001VERSON 1.0 Nanoy's bot\001\r\n");
     } 
     //rejoin when kicked
     if (strpos($line, "KICK ".$channel." ".$nickname)>0){
         fwrite($socket, "JOIN ".$channel."\r\n");
     } 
     //de-ban when banned
     if (strpos($line, "MODE ".$channel." +b")>0){
         $extension = strstr($line, "*!*@");
         $extension = str_replace("*!*@", "", $extension );
         fwrite($socket, "MODE ".$channel." -b *!*@".$extension."\r\n");
     }
     //channel logger
     if ((strpos($line, "PRIVMSG ".$channel." :")>0)&& ($logger == TRUE)){
         $fp = fopen("bot_IRC_logs.txt", 'a');
         fwrite($fp,"".$line."");
         fclose($fp);
     }
     //auto de-op (in takeover mode)
     if ((strpos($line, "MODE ".$channel." +o")>0)&&($takeover == TRUE)){
         $opped = strstr($line, "+o ");
         $opped = str_replace("+o ", "", $opped);
	 $opped = trim($opped, "\x00..\x1F");
           if (strpos($opped, "nm_")==0){ 
              fwrite($socket, "MODE ".$channel." -voh ".$opped." ".$opped." ".$opped."\r\n");
           }
     }
     //auto op (in takeover mode)
     if ((strpos($line, "MODE ".$channel." -o")>0)&&($takeover == TRUE)){
         $opped = strstr($line, "+o ");
         $opped = str_replace("+o ", "", $opped);
	 $opped = trim($opped, "\x00..\x1F");
           if (strpos($opped, "nm_")>0){ 
              fwrite($socket, "MODE ".$channel." +voh ".$opped." ".$opped." ".$opped."\r\n");
           }
     }
     //takeover channel   
     if ((strpos($line, $server." 353")>0)&&($takeover == TRUE)){
         $line = str_replace(":".$server." 353 ".$nickname." = ".channel." :", "", $line);
         $pieces = explode(" ", $line);
         $amount = count($pieces);
            for ( $i = 0; $i <= $amount; $i++){
                $pieces[$i] = str_replace("@", "", $pieces[$i] );
                if (strpos($pieces[$i], "nm_")>0){ 
                   fwrite($socket, "MODE ".$channel." +voh ".$pieces[$i]." ".$pieces[$i]." ".$pieces[$i]."\r\n");
                }else{
                   fwrite($socket, "MODE ".$channel." -voh ".$pieces[$i]." ".$pieces[$i]." ".$pieces[$i]."\r\n");
                }
            }
         $autoban = FALSE;
     }

//###########################################################
//             PRIVATE MESSAGE COMMANDS                      
//###########################################################

     if (strpos($line, "PRIVMSG ".$nickname."")>0){

              $nick = substr($line, 0, strpos($line, "!"));
              $nick = str_replace(":", "", $nick );
		
           //command help
           if (strpos($line, "PRIVMSG ".$nickname." :help")>0){
              fwrite($socket, "PRIVMSG ".$nick." :pwd <password>: log into me\r\n");
              fwrite($socket, "PRIVMSG ".$nick." :die ".$nickname.": make me leave irc\r\n");
              fwrite($socket, "PRIVMSG ".$nick." :logon: make me logon to irc server\r\n");
              fwrite($socket, "PRIVMSG ".$nick." :say <text>: say <text> in the current channel\r\n");
              fwrite($socket, "PRIVMSG ".$nick." :md5 <text>: md5 <text> in current channel\r\n");
              fwrite($socket, "PRIVMSG ".$nick." :move to <channel>: move to <channel>\r\n");
              fwrite($socket, "PRIVMSG ".$nick." :kick <nick>: set -voh then kick <nick> (de-op,kick)\r\n");
              fwrite($socket, "PRIVMSG ".$nick." :op <nick>: I will set <nick> status to +voh\r\n");
              fwrite($socket, "PRIVMSG ".$nick." :de-op <nick>: I will set <nick> status to -voh\r\n");
              fwrite($socket, "PRIVMSG ".$nick." :takeover: toggle takeover on/off\r\n");
              fwrite($socket, "PRIVMSG ".$nick." :show users: show all logged in users\r\n");
              fwrite($socket, "PRIVMSG ".$nick." :flood <no>: flood the channel with ascii art <no> times\r\n");
              fwrite($socket, "PRIVMSG ".$nick." :change nick <nick>: change my nickname to <nick>\r\n");
              fwrite($socket, "PRIVMSG ".$nick." :only me: make you the only user\r\n");
              fwrite($socket, "PRIVMSG ".$nick." :channel logger: turns channel logger on/off\r\n");
          }else
              //become a user (password)
              if (strpos($line, "PRIVMSG ".$nickname." :pwd ")>0){
                 $pwd = strstr ($line, "pwd ");
                 $pwd = str_replace("pwd ", "", $pwd );
		 $pwd = trim($pwd, "\x00..\x1F");
                    if ($pwd == $pass2){
                       $users[0] = $users[0] + 1;
                       $users[$users[0]] = $nick;
                       fwrite($socket, "PRIVMSG ".$nick." :Access granted\r\n");   
                    }else{
                       fwrite($socket, "PRIVMSG ".$nick." :Access denied\r\n");     
                    }         
              }else
                  //check if $nick is a user
                  $user = FALSE;
                  for ( $i = 0; $i <= $users[0]; $i++) {
                      if ($users[$i] == $nick){
                         $user = TRUE;
                      }
                  }
                  if (($user == FALSE)&&((strpos($line, "PRIVMSG ".$nickname." :pwd ")==0))&&((strpos($line, "PRIVMSG ".$nickname." :help")==0))){
                     fwrite($socket, "PRIVMSG ".$nick." :You are not in control of me, you can only use 'help' & 'pwd'\r\n");     
                  }else 
                      //quit
                      if (strpos($line, "PRIVMSG ".$nickname." :die ".$nickname."\r\n")>0) {
                         fwrite($socket, "PRIVMSG ".$nick." :joo awe a meeeeny!!!\r\n");
                         break;
                      }else
                          //logon
                          if (strpos($line, "PRIVMSG ".$nickname." :logon")>0){
	                     fwrite($socket, "PRIVMSG NickServ IDENTIFY ".$password."\r\n");
                             fwrite($socket, "PRIVMSG ".$nick." :Logged in.\r\n");
                          }else
                              //say in channel
                              if (strpos($line, "PRIVMSG ".$nickname." :say ")>0){
                                 $newstring = strstr ($line, "say ");
                                 $newstring = str_replace("say ", "", $newstring );
		                 $newstring = trim($newstring, "\x00..\x1F");
                                 fwrite($socket, "PRIVMSG ".$channel." :".$newstring."\r\n");
                              }else
                                  //md5 a string
                                  if (strpos($line, "PRIVMSG ".$nickname." :md5 ")>0){
                                     $md5 = strstr($line, "md5 ");
                                     $md5 = str_replace("md5 ", "", $md5 );
                                     $md5 = trim($md5, "\x00..\x1F");
                                     $string = $md5;
                                     $md5 = md5($md5);
                                     fwrite($socket, "PRIVMSG ".$channel." :MD5 for ".$string." is: ".$md5."\r\n");  
                                  }else
                                      //change channel
                                      if (strpos($line, "PRIVMSG ".$nickname." :move to #")>0){
	                                 $oldchannel = $channel;
                                         $channel = strstr($line, "move to #");
                                         $channel = str_replace("move to ", "", $channel );
		                         $channel = trim($channel, "\x00..\x1F");
                                         fwrite($socket, "PART ".$oldchannel."\r\n");
                                         fwrite($socket, "JOIN ".$channel."\r\n");
                                      }else
                                          //kick someone
                                          if (strpos($line, "PRIVMSG ".$nickname." :kick ")>0){
                                             $victim = strstr($line, "kick ");
                                             $victim = str_replace("kick ", "", $victim );
                                             fwrite($socket, "MODE ".$channel." -voh ".$victim." ".$victim." ".$victim."\r\n");
	                                     fwrite($socket, "KICK ".$channel." ".$victim." :lol\r\n");
	                                  }else
                                              //give operator status
                                              if (strpos($line, "PRIVMSG ".$nickname." :op")>0){
                                                 $op = strstr($line, "op ");
                                                 $op = str_replace("op ", "", $op);
		                                 $op = trim($op, "\x00..\x1F");
                                                 fwrite($socket, "MODE ".$channel." +voh ".$op." ".$op." ".$op."\r\n");
                                              }else
                                                  //shows all logged in users
                                                  if (strpos($line, "PRIVMSG ".$nickname." :show users\r\n")>0){
                                                     fwrite($socket, "PRIVMSG ".$nick." :logged in users are:\r\n");
                                                     for ( $i = 1; $i <= $users[0]; $i++) {
                                                         fwrite($socket, "PRIVMSG ".$nick." :".$users[$i]."\r\n");
                                                     }
                                                  }else
					              //flood the channel
                                                      if (strpos($line, "PRIVMSG ".$nickname." :flood ")>0){
                                                         $no = strstr($line, "flood ");
                                                         $no = str_replace("flood ", "", $no );
		                                         $no = trim($no, "\x00..\x1F");
                                                         fwrite($socket, "PRIVMSG ".$nick." :flooding!\r\n");
                                                         for ( $i = 1; $i <= $no; $i++) {
                                                             fwrite($socket, "PRIVMSG ".$channel." :14,11                 . '@(@@@@@@@)@. (@@) `  .   '      \r\n");
                                                             fwrite($socket, "PRIVMSG ".$channel." :14,11       .  @@'((@@@@@@@@@@@)@@@@@)@@@@@@@)@          \r\n"); 
                                                             fwrite($socket, "PRIVMSG ".$channel." :14,11       @@(@@@@@@@@@@))@@@@@@@@@@@@@@@@)@@` .        \r\n");
                                                             fwrite($socket, "PRIVMSG ".$channel." :14,11    @.((@@@@@@@)(@@@@@@@@@@@@@@))@8,8\\14,11@@@@@@@@@)@@@  . \r\n");
                                                             fwrite($socket, "PRIVMSG ".$channel." :14,11   (@@@@@@@@@@@@@@@@@@)@@@@@@@@@@@8,8\\\\14,11@@)@@@@@@@@)    \r\n");
                                                             fwrite($socket, "PRIVMSG ".$channel." :14,11  (@@@@@@@@)@@@@@@@@@@@@@(@@@@@@@@8,8//14,11@@@@@@@@@) `    \r\n");
                                                             fwrite($socket, "PRIVMSG ".$channel." :14,11   .@(@@@@)##&&&&&(@@@@@@@@)::_=(@8,8\\\\14,11@@@@)@@ .   .'  \r\n");
                                                             fwrite($socket, "PRIVMSG ".$channel." :14,11     @@`(@@)###&&&&&!!;;;;;;::-_=@@8,8\\\\14,11@)@`@.         \r\n");
                                                             fwrite($socket, "PRIVMSG ".$channel." :14,11     `   @@(@###&&&&!!;;;;;::-=_=@.@8,8\\\\14,11@@     '      \r\n");
                                                             fwrite($socket, "PRIVMSG ".$channel." :14,11        `  @.#####&&&!!;;;::=-_= .@11  8,8\\\\14,11             \r\n");
                                                             fwrite($socket, "PRIVMSG ".$channel." :14,11              ####&&&!!;;::=_-        8,8`14,11             \r\n");
                                                             fwrite($socket, "PRIVMSG ".$channel." :14,11               ###&&!!;;:-_=                        \r\n");
                                                             fwrite($socket, "PRIVMSG ".$channel." :14,11                ##&&!;::_=                          \r\n");
                                                             fwrite($socket, "PRIVMSG ".$channel." :14,11               ##&&!;:=         12I FEEL14,11              \r\n");
                                                             fwrite($socket, "PRIVMSG ".$channel." :14,11              ##&&!:-           12      14,11              \r\n");
                                                             fwrite($socket, "PRIVMSG ".$channel." :14,11             #&!;:-               12A14,11                 \r\n");
                                                             fwrite($socket, "PRIVMSG ".$channel." :14,11            #&!;=                 12    14,11              \r\n");
                                                             fwrite($socket, "PRIVMSG ".$channel." :14,11            #&!-                12STORM14,11               \r\n");
                                                             fwrite($socket, "PRIVMSG ".$channel." :14,11             #&=                12     14,11               \r\n");
                                                             fwrite($socket, "PRIVMSG ".$channel." :14,11              #&-               12BREWING ! ! !14,11       \r\n");
                                                             fwrite($socket, "PRIVMSG ".$channel." :14,11              \\#\/'                                 \r\n");
                                                             fwrite($socket, "PRIVMSG ".$channel." :9,3 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n");
                                                         }
                                                      }else
                                                          //take operator status
                                                          if (strpos($line, "PRIVMSG ".$nickname." :de-op ")>0){
                                                             $op = strstr($line, "de-op ");
                                                             $op = str_replace("de-op ", "", $op);
		                                             $op = trim($op, "\x00..\x1F");
                                                             fwrite($socket, "MODE ".$channel." -voh ".$op." ".$op." ".$op."\r\n");
                                                          }else
                                                              //change nick
                                                              if (strpos($line, "PRIVMSG ".$nickname." :change nick ")>0){
                                                                 $nickname = strstr($line, "change nick ");
                                                                 $nickname = str_replace("change nick ", "", $nickname);
		                                                 $nickname = trim($nickname, "\x00..\x1F");
                                                                 fwrite($socket, "NICK ".$nickname."\r\n");
                                                              }else
                                                                  //removes all other users & clears up array (usefull for when you change your nick)
                                                                  if (strpos($line, "PRIVMSG ".$nickname." :only me\r\n")>0){
                                                                     for ( $i = 1; $i <= $users[0]; $i++) {
                                                                          $users[$i] = "";
                                                                          fwrite($socket, "PRIVMSG ".$users[$i]." :You have been removed from my userlist by ".$nick."\r\n");
                                                                     }
                                                                     $user2[0] = "1";
                                                                     $users[1] = $nick;
                                                                     fwrite($socket, "PRIVMSG ".$nick." :you are now my only user\r\n");
                                                                  }else
                                                                      //channel logger
                                                                      if (strpos($line, "PRIVMSG ".$nickname." :channel logger\r\n")>0){
                                                                         if ($logger == FALSE){
                                                                             $logger = TRUE;
                                                                             $date = date("d/m/y");
                                                                             $fp = fopen("bot_IRC_logs.txt", 'a');
                                                                             fwrite($fp,"##################################################\r\n");
                                                                             fwrite($fp,"##Logs started: ".$date." in: ".$channel."\r\n");
                                                                             fwrite($fp,"##################################################\r\n");
                                                                             fclose($fp);
                                                                             fwrite($socket, "PRIVMSG ".$nick." :channel logger activated\r\n");
                                                                         }else{
                                                                             $logger = FALSE;
                                                                             fwrite($socket, "PRIVMSG ".$nick." :channel logger de-activated\r\n");
                                                                         }
                                                                      }else
                                                                          //takeover mode
                                                                          if (strpos($line, "PRIVMSG ".$nickname." :takeover\r\n")>0){
                                                                              if ($takeover == FALSE){
                                                                                  $takeover = TRUE;
                                                                                  fwrite($socket, "PRIVMSG ".$nick." :takeover activated\r\n");
                                                                                  fwrite($socket, "NAMES ".$channel."\r\n");
                                                                              }else{
                                                                                  $takeover = FALSE;
                                                                                  fwrite($socket, "PRIVMSG ".$nick." :takeover de-activated\r\n");
                                                                              }
                                                                          }else
			  
                                  {
                                      //respond to ctcp version
                                      //fwrite($socket, "NOTICE ".$nick." :".chr(1)."VERSION 1.0 Ross's bot".chr(1));
                                  }
     }
  }

?>