PDA

View Full Version : PHP IRC BOTs



vuontinh
14-04-2011, 09:13 PM
In this tutorial , I will explain how to create an IRC bot using PHP

First , let's explain some things !


set_time_limit() :
Set the number of seconds a script is allowed to run. If this is reached, the script returns a fatal error. The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php.ini.
When called, set_time_limit() restarts the timeout counter from zero. In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out.

Warning :
This function has no effect when PHP is running in safe mode. There is no workaround other than turning off safe mode or changing the time limit in the php.ini.


socket_open:
Opens a new connection to hostname:port and returns its handler.


fsockopen:
Initiates a socket connection to the resource specified by hostname .

Warning :
UDP sockets will sometimes appear to have opened without an error, even if the remote host is unreachable. The error will only become apparent when you read or write data to/from the socket. The reason for this is because UDP is a "connectionless" protocol, which means that the operating system does not try to establish a link for the socket until it actually needs to send or receive data.


sleep() :
Stops execution for a few seconds


fwrite() :
writes the contents of string to the file stream pointed to by handle .


fgets() :
Gets a line from file pointer.

explode() :
Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter .

str_replace() :
This function returns a string or an array with all occurrences of search in subject replaced with the given replace value.

eregi() :
This function is identical to ereg() except that it ignores case distinction when matching alphabetic characters.

Now , I finish explaining the functions used in this bot ,
I initialise two commands in my bot :
Command 1 : !RooT => it will show you a message contain Members
Command 2 : !quit => Make the bot quit your channel



Well , now i think that PHP c0der can create an IRC BOT using PHP

To run the bot you have to configure it , So you have to change this variables :


$name_server = "irc.domain.net"; ==> change irc.indoirc.net to your server
$port_server = 6667; ==> change 6667 to the port used by your server
$name_bot = "ug.boots"; ==> please, don' t change this name
$channel = "#kusanghi"; ==> change meister to your channel
$password = ""; ==> here put the password of your channel


<?php

set_time_limit(0);

$name_server = "irc.domain.net";
$port_server = 6667;
$name_bot = "ug.boots"; // please, don' t change this name :)
$channel = "#kusanghi";
$password = "";

if (!($socket_open = fsockopen($name_server, $port_server))) {
echo "Impossible de se connecter!";
}
else {
$header = "NICK {$name_bot}\n";
sleep(0.2);
$header .= "USER {$name_bot} 127.0.0.1 {$name_server} : {$name_bot}\n";
sleep(0.2);
$header .= "IDENTIFY {$password}\n";
sleep(0.2);
$header .= "JOIN {$channel}\n";
}

fwrite($socket_open, $header);

while(true) {
while($response = fgets($socket_open, 4096)) {
flush();

$explode = explode(" ", $response);
if($explode[0] == "PING") {
fwrite($socket_open, "PONG :{$explode[1]}\n");
}
sleep(0.1);

$command = str_replace(array(chr(10), chr(13)), "", $explode[3]);
if ($command == ":!RooT") {
fwrite($socket_open, "PRIVMSG {$explode[2]} R00t - Kusanghi - Checker.im : df-pro.net\n\r");
}
sleep(0.1);

if (eregi("meister", $explode[0]) && $command == ":!quit") { // change "meister" with your name :)
fwrite($socket_open, "QUIT :{$explode[2]} It's a nice exemple , Try t0 mAke iT BetteR\n\r");
exit();
}
sleep(0.1);
}
}