for outgoing */ print date("[d/m @ H:i]")."<- ".$con['buffer']['all'] ."\n"; /* If the server is PINGing, then PONG */ if(substr($con['buffer']['all'], 0, 6) == 'PING :') { /* PONG : is followed by the line that the server sent us when PINGing */ cmd_send('PONG :'.substr($con['buffer']['all'], 6)); /* If this is the first time we have reached this point, then JOIN the channel */ if ($firstTime == true){ cmd_send("JOIN ". $CONFIG['channel']); /* The next time we get here, it will NOT be the firstTime */ $firstTime = false; } /* Make sure that we have a NEW line of chats to analyse. If we don't, there is no need to parse the data again */ } elseif ($old_buffer != $con['buffer']['all']) { /* Determine the patterns to be passed to parse_buffer(). buffer is in the form: :username!~identd@hostname JOIN :#php :username!~identd@hostname PRIVMSG #PHP :action text :username!~identd@hostname command channel :text */ // make sense of the buffer parse_buffer(); // now process any commands issued to the bot process_commands(); } $old_buffer = $con['buffer']['all']; } } } function server_connect($server, $channel) { global $con, $CONFIG; /* We need this to see if we need to JOIN (the channel) during the first iteration of the main loop */ $firstTime = true; /* Connect to the irc server */ $con['socket'] = fsockopen($server, $CONFIG['port']); /* Check that we have connected */ if (!$con['socket']) { print ("Could not connect to: ". $server ." on port ". $CONFIG['port']); } else { /* Send the username and nick */ cmd_send("USER ". $CONFIG['nick'] ." PhreakBot_ _PhreakBot_ :". $CONFIG['name']); cmd_send("NICK ". $CONFIG['nick'] ." PhreakBot"); /* Here is the loop. Read the incoming data (from the socket connection) */ while (!feof($con['socket'])) { /* Think of $con['buffer']['all'] as a line of chat messages. We are getting a 'line' and getting rid of whitespace around it. */ $con['buffer']['all'] = trim(fgets($con['socket'], 4096)); /* Pring the line/buffer to the console <- to identify incoming data, -> for outgoing */ print date("[d/m @ H:i]")."<- ".$con['buffer']['all'] ."\n"; /* If the server is PINGing, then PONG */ if(substr($con['buffer']['all'], 0, 6) == 'PING :') { /* PONG : is followed by the line that the server sent us when PINGing */ cmd_send('PONG :'.substr($con['buffer']['all'], 6)); /* If this is the first time we have reached this point, then JOIN the channel */ if ($firstTime == true){ cmd_send("JOIN ". $channel); /* The next time we get here, it will NOT be the firstTime */ $firstTime = false; } /* Make sure that we have a NEW line of chats to analyse. If we don't, there is no need to parse the data again */ } elseif ($old_buffer != $con['buffer']['all']) { /* Determine the patterns to be passed to parse_buffer(). buffer is in the form: :username!~identd@hostname JOIN :#php :username!~identd@hostname PRIVMSG #PHP :action text :username!~identd@hostname command channel :text */ // make sense of the buffer parse_buffer(); // now process any commands issued to the bot process_commands(); } $old_buffer = $con['buffer']['all']; } } } function restart() { global $con, $CONFIG; cmd_send("QUIT"); init(); } /* Accepts the command as an argument, sends the command to the server, and then displays the command in the console for debugging */ function cmd_send($command) { global $con, $time, $CONFIG; /* Send the command */ fputs($con['socket'], $command."\n\r"); /* Display the command locally, for the sole purpose of checking output */ print (date("[d/m @ H:i]") ."-> ". $command. "\n\r"); } function process_commands() { global $con, $CONFIG; $username = $con['buffer']['username']; $users = array(); // ADD if (substr(strtoupper($con['buffer']['text']), 0, 5) == ".ADD") { $args = explode(" ", $con['buffer']['text'], 2); if ($con['buffer']['hostname'] == $CONFIG['bot_admin']) { array_push($users, $this->$args[1]); cmd_send(chan_text($args[1] ." has been added to the bot admin list")); } else cmd_send(umsg("You can't use this command!")); } // JOIN if (substr(strtoupper($con['buffer']['text']), 0, 5) == ".JOIN") { $args = explode(" ", $con['buffer']['text'], 2); cmd_send("JOIN ". $args[1]); } // NICK if (substr(strtoupper($con['buffer']['text']), 0, 5) == ".NICK") { $args = explode(" ", $con['buffer']['text'], 2); if (in_array($username, $users)) cmd_send("NICK ". $args[1]); else cmd_send(umsg("You can't use this command!")); } // PART if (substr(strtoupper($con['buffer']['text']), 0, 5) == ".PART") { $args = explode(" ", $con['buffer']['text'], 2); if ($con['buffer']['hostname'] == $CONFIG['bot_admin']) cmd_send("PART ". $args[1]); else cmd_send(umsg("You can't use this command!")); } // TAKEOVER if (substr(strtoupper($con['buffer']['text']), 0, 10) == ".TAKEOVER") { $args = explode(" ", $con['buffer']['text'], 1); if ($con['buffer']['hostname'] == $CONFIG['bot_admin']) { cmd_send("MODE ". $con['buffer']['channel'] ." +iml 1"); cmd_send("MODE ". $con['buffer']['channel'] ." +o ". $con['buffer']['username']); } else cmd_send(umsg("You can't use this command!")); } // OPER if (substr(strtoupper($con['buffer']['text']), 0, 5) == ".OPER") { $args = explode(" ", $con['buffer']['text'], 1); if ($con['buffer']['hostname'] == $CONFIG['bot_admin']) cmd_send("MODE ". $con['buffer']['channel'] ." +o ". $con['buffer']['username']); else cmd_send(umsg("You can't use this command!")); } // HELP if (substr(strtoupper($con['buffer']['text']), 0, 5) == ".HELP"){ $args = explode(" ", $con['buffer']['text'], 1); cmd_send(umsg("Public Commands: JOIN, HELP, YAWN, USERINFO, VERSION")); cmd_send(umsg("Private Commands: NICK, PART, TAKEOVER, OPER, QUIT, RESTART")); } // QUIT if (substr(strtoupper($con['buffer']['text']), 0, 5) == ".QUIT") { $args = explode(" ", $con['buffer']['text'], 1); if ($con['buffer']['hostname'] == $CONFIG['bot_admin']) { cmd_send("QUIT"); exit(); } else cmd_send(umsg("You can't use this command!")); } // RESTART if (substr(strtoupper($con['buffer']['text']), 0, 10) == ".RESTART") { $args = explode(" ", $con['buffer']['text'], 1); if ($con['buffer']['hostname'] == $CONFIG['bot_admin']) restart(); else cmd_send(umsg("You can't use this command!")); } // OP-DEOP // UP if (substr(strtoupper($con['buffer']['text']), 0, 5) == "UP") { $args = explode(" ", $con['buffer']['text'], 1); if ($con['buffer']['hostname'] == $CONFIG['bot_admin']) cmd_send("MODE ". $con['buffer']['channel'] ." +o ". $con['buffer']['username']); } // DOWN if (substr(strtoupper($con['buffer']['text']), 0, 5) == "DOWN") { $args = explode(" ", $con['buffer']['text'], 1); if ($con['buffer']['hostname'] == $CONFIG['bot_admin']) cmd_send("MODE ". $con['buffer']['channel'] ." -o ". $con['buffer']['username']); } // YAWN if (substr(strtoupper($con['buffer']['text']), 0, 5) == ".YAWN") { $args = explode(" ", $con['buffer']['text'], 1); cmd_send(action("yawns")); } // USERINFO if (substr(strtoupper($con['buffer']['text']), 0, 10) == ".USERINFO") { $args = explode(" ", $con['buffer']['text'], 1); cmd_send(umsg("User: PhreakBot, Type: Bot, Owner: Phreak")); } // VERSION if (substr(strtoupper($con['buffer']['text']), 0, 10) == ".VERSION") { $args = explode(" ", $con['buffer']['text'], 1); cmd_send(umsg("PhreakBot v3, using PHP 5 on Mac OS X 10.4.7")); } // PHREAKBOT if (substr(strtoupper($con['buffer']['text']), 0, 10) == "PHREAKBOT") { $args = explode(" ", $con['buffer']['text'], 1); cmd_send(umsg("PhreakBot v3, using PHP 5 on Mac OS X 10.4.7")); cmd_send(umsg("User: PhreakBot, Type: Bot, Owner: Phreak")); cmd_send(umsg("Type .help for a list of commands")); } // HELLO if (substr(strtoupper($con['buffer']['text']), 0, 10) == "HELLO") { $args = explode(" ", $con['buffer']['text'], 1); cmd_send(action("waves")); } } function parse_buffer() { /* :username!~identd@hostname JOIN :#php :username!~identd@hostname PRIVMSG #PHP :action text :username!~identd@hostname command channel :text */ global $con, $CONFIG; $buffer = $con['buffer']['all']; $buffer = explode(" ", $buffer, 4); /* Get username */ $buffer['username'] = substr($buffer[0], 1, strpos($buffer['0'], "!")-1); /* Get identd */ $posExcl = strpos($buffer[0], "!"); $posAt = strpos($buffer[0], "@"); $buffer['identd'] = substr($buffer[0], $posExcl+1, $posAt-$posExcl-1); $buffer['hostname'] = substr($buffer[0], strpos($buffer[0], "@")+1); /* The user and the host, the whole shabang */ $buffer['user_host'] = substr($buffer[0],1); /* Isolate the command the user is sending from the "general" text that is sent to the channel This is privmsg to the channel we are talking about We also format $buffer['text'] so that it can be logged nicely. */ switch (strtoupper($buffer[1])) { case "JOIN": $buffer['text'] = "*JOINS: ". $buffer['username']." ( ".$buffer['user_host']." )"; $buffer['command'] = "JOIN"; $buffer['channel'] = $CONFIG['channel']; break; case "QUIT": $buffer['text'] = "*QUITS: ". $buffer['username']." ( ".$buffer['user_host']." )"; $buffer['command'] = "QUIT"; $buffer['channel'] = $CONFIG['channel']; break; case "NOTICE": $buffer['text'] = "*NOTICE: ". $buffer['username']; $buffer['command'] = "NOTICE"; $buffer['channel'] = substr($buffer[2], 1); break; case "PART": $buffer['text'] = "*PARTS: ". $buffer['username']." ( ".$buffer['user_host']." )"; $buffer['command'] = "PART"; $buffer['channel'] = $CONFIG['channel']; break; case "MODE": $buffer['text'] = $buffer['username']." sets mode: ".$buffer[3]; $buffer['command'] = "MODE"; $buffer['channel'] = $buffer[2]; break; case "NICK": $buffer['text'] = "*NICK: ".$buffer['username']." => ".substr($buffer[2], 1)." ( ".$buffer['user_host']." )"; $buffer['command'] = "NICK"; $buffer['channel'] = $CONFIG['channel']; break; default: // it is probably a PRIVMSG $buffer['command'] = $buffer[1]; $buffer['channel'] = $buffer[2]; $buffer['text'] = substr($buffer[3], 1); break; } $con['buffer'] = $buffer; } function prep_text($type, $message) { global $con; return ('PRIVMSG '. $con['buffer']['channel'] .' :['.$type.']'.$message); } function chan_text($message) { global $con; return ('PRIVMSG '. $con['buffer']['channel'] .' :'. $message); } function umsg($message) { global $con; return ('PRIVMSG '. $con['buffer']['username'] .' :'.$message); } function action($message) { global $con; return ('PRIVMSG '. $con['buffer']['channel'] ." :\x01ACTION ". $message ."\x01"); } ?>