Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>IRC Bot</title>
- </head>
- <body>
- <?php
- require('api_calls.php'); // functions for communication with other servers
- require('irc_commands.php'); // functions for communction bewteen the IRC server and bot only
- /*
- *
- * IRC Bot by Phil, modified by Xenios
- * Copyright Phil 2010
- *
- */
- // Bot may only be run from a command line interface.
- $sapi_type = php_sapi_name();
- if (substr($sapi_type, 0, 3) == 'cli')
- {
- // Ensure all errors are reported
- error_reporting(E_ALL);
- // Ensure we don't time out the script
- set_time_limit(0);
- // Variable setting
- $read = null; // For reading in data
- $sock = null; // Socket
- // Timer definitions
- // Timer for sending timeout checks
- $timers = array();
- $timers['ping'] = 0;
- // Functions
- // Send message to server
- function send($message, $output = true) {
- global $sock;
- fputs($sock, "{$message}\n");
- if ($output) {
- echo "-> {$message}\n";
- }
- }
- // Formatting - Change these and they'll break
- define('C_BOLD', chr(2));
- define('C_COLOR', chr(3));
- define('C_ITALIC', chr(29));
- define('C_REVERSE', chr(22));
- define('C_UNDERLINE', chr(31));
- // Config
- define('SERVER', 'serverName'); // Server to connect to (irc.rizon.no or irc.rizon.net)
- define('PORT', 'serverPort'); // Port on server
- define('BOT_NICK', 'botName'); // Bot's nick
- define('BOT_ANICK', 'botAnick'); // Nick to use if main is taken
- define('IDENT', 'botIdent'); // Ident
- define('GECOS', 'botGecos'); // Real name
- define('NSPASS', 'botPassword'); // NickServ password
- define('CHANS', 'channelName'); // Channels to join (, separated list)
- // The juice
- // Loop forever
- while (1) {
- // Connect
- $sock = fsockopen(SERVER, PORT);
- // Bitch, Rinse and Repeat if it's broken
- if (!$sock) {
- echo 'Error connecting to ' . SERVER . ' on port ' . PORT . '! Trying again in 30 seconds';
- sleep(30);
- }
- else {
- // Stop the stream blocking, sets timeout value (so the bot quits if it's disconnected)
- stream_set_timeout($sock, 0, 1);
- stream_set_blocking($sock, 0);
- // Send connect stuff
- send('USER ' . IDENT . ' 0 * :' . GECOS);
- send('NICK ' . BOT_NICK);
- // Identify to services - change this if this isn't the correct command
- send('NS IDENTIFY ' . NSPASS);
- // Join channels
- send('JOIN ' . CHANS);
- // Loop until socket dies
- while (!feof($sock)) {
- // Read delay of 0.1 seconds
- // Reduce this to increase the responsiveness of the bot in busy channels
- // Be aware that it'll raise CPU load
- usleep(100000);
- // Increment all timers
- foreach ($timers as $key => $timer) {
- $timers[$key]++;
- }
- // Timer events
- if ($timers['ping'] == 600) {
- send('PING :TIMEOUTCHECK', false);
- $timers['ping'] = 0;
- }
- // Reading data
- $read .= fread($sock, 512);
- // Loop the individual lines
- while (substr_count($read, "\n") != 0) {
- // Get end of line
- $offset = strpos($read, "\n");
- // Get data
- $data = substr($read, 0, $offset);
- // Remove data from end of line
- $read = substr($read, $offset + 1);
- // Print the data for all but PONG
- if (strpos($data, 'PONG') === false) {
- echo "-> {$data}\n";
- }
- // Split the data
- $dataSplit = explode(' ', $data);
- // Handle the data
- // PINGs
- if ($dataSplit[0] == 'PING') {
- send('PONG ' . substr($data, 5));
- }
- // Regular message handler
- elseif ($dataSplit[1] == 'PRIVMSG') {
- // Get the required info from the message
- $message = trim(ltrim(implode(' ', array_slice($dataSplit, 3)), ':'));
- $words = explode(' ', trim($message));
- $to = $dataSplit[2];
- $nick = explode('!', $dataSplit[0]);
- $nick = ltrim($nick[0], ':');
- // Private messages
- if (strtolower($to) == strtolower(BOT_NICK)) {
- if ($words[0] == '!hi') {
- send("PRIVMSG {$nick} :Hi {$nick}!");
- }
- }
- // Channel messages
- else
- {
- // Trigger to a specific command if command is in the begin of $words.
- if (strpos($words[0], '!call') === 0)
- {
- // reads
- $temp = get_data_call($words, '!call');
- if ($temp !== FALSE)
- {
- send ("NAMES CHANS");
- // the next echo lines are for debugging
- echo "\n\n\n";
- echo "\nwords\n";
- print_r($words);
- echo "\nmessage\n";
- print_r($message);
- echo "\ndata\n";
- print_r($data);
- echo "\ndatasplit\n";
- print_r($dataSplit);
- echo "\nread names\n";
- // the above echo lines are for debugging
- while (!feof($sock))
- {
- $readCall = fread($sock, 9024);
- if ($readCall === true)
- {
- while ($readCall == ':irc.rizon.no')
- {
- echo "irc.rizon.no found!";
- continue;
- }
- }
- else
- {
- echo "readCall returns false.";
- }
- }
- }
- else
- {
- echo "Something went wrong.";
- }
- }
- // Private messages
- if (strtolower($to) == strtolower(BOT_NICK)) {
- if ($words[0] == '!die') {
- send ("QUIT : quit message");
- fclose($sock);
- exit;
- }
- }
- // Channel messages
- else {
- if ($words[0] == '!die') {
- send ("QUIT : quit message");
- fclose($sock);
- exit;
- }
- }
- }
- }
- // Notice handler
- elseif ($dataSplit[1] == 'NOTICE') {
- // Get the required info from the message
- $message = trim(ltrim(implode(' ', array_slice($dataSplit, 3)), ':'));
- $words = explode(' ', trim($message));
- $to = $dataSplit[2];
- $nick = explode('!', $dataSplit[0]);
- $nick = ltrim($nick[0], ':');
- // Private notices
- if (strtolower($to) == strtolower(BOT_NICK)) {
- // no code yet
- }
- // Channel notices
- else {
- // no code yet
- }
- }
- }
- }
- }
- }
- }
- else
- {
- echo "This script can only be run in a CLI. ";
- }
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement