Thank you for taking the time to visit my personal website. Hopefully the information I have presented on this site can be useful and there is knowledge.

Thursday, July 14, 2011

Bot Internet Relay Chat (IRC) using PHP

In this paper, the authors do not discuss in detail about the PHP script, because the purpose for writing this script is targeted to readers who already understand a bit about PHP. PHP is a web programming language that attach to the HTML (hipertext markup language). Most syntak PHP is taken from the C language, Java, and Perl, so it is very easy for programmers to understand language on top of PHP. Unlike other languages, PHP (hypertext preprocessor) has advantages in speed, powerful and cheap (free). PHP said to be fast because the PHP module runs in its own memory space. 

In the case of making a connection using fsockopen PHP used a command that allows you to open a socket on the specified server and port, in addition to running the script is certainly no specific requirement that you use the server must support PHP scripts, then allow you to use the fsockopen command. Next create a new file named form.html and place the code below: 


HTML Code:
 <html>
 <head>
     <title> IRC bot by www.hdteam.net </ title>
 </ Head>
 <body>
     <form method= "post" action= "irc.lib.php">
         Nick: <input type= "text" name= "nick"> <br>
         User name: <input type= "text" name= "user"> <br>
         Real name: <input type= "text" name= "nama"> <br>
         Server: <input type= "text" name= "server"> <br>
         Port: <input type= "text" name= "port"> <br>
         Channel: <input type= "text" name= "channel"> <br>
         <input type= "submit" value= "join" name= "submit">
     </ Form>
 </ Body>
 </ Html> 
Next paste the following code into the file irc.lib.php: 

PHP Code:
<?php
set_time_limit
 ( 0 ); define ( 'CRLF' , "\r\n" ); $nick = $_POST [ 'nick' ]; $user = $_POST [ 'user' ]; $localhost = '127.0.0.1 ';
$server = $_POST['
 server '];
$port = $_POST['
 port '];
$nama = $_POST['
 nama '];
$channel = $_POST['
 channel '];
$fp = fsockopen($server,$port, &$err_num, &$err_msg, 30);

if(!$fp) {
echo "Maaf, tidak bisa menghubungi server $server";
exit;
}

$data = '
 NICK ' . $nick . CRLF;
$data .= '
 USER ' . $user . ' ' . $localhost . ' ' . $server . ' : ' . $nama . CRLF;
fputs($fp, $data);
$response = '';
while (!feof($fp)) {
$response .= fgets($fp, 1024);
while (substr_count($response,CRLF) != 0) {
$offset = strpos($response, CRLF);
$data = substr($response,0,$offset);
$response = substr($response,$offset+2);

if ( substr($data,0,1) == '
 : ' ) {
$offsetA = strpos($data, ' ');
$dFrom = substr($data,1,$offsetA-1);
$offsetB = strpos($data, '
 : ');
$dCommand = substr($data,$offsetA+1,$offsetB-$offsetA-1);
$dText = substr($data,$offsetB+2);

if (substr($dCommand,0,3) == '
 004 ' ) {
fputs($fp,'
 JOIN ' . $channel . CRLF);
}
elseif ( substr($dCommand,0,7) == '
 PRIVMSG ' ) {
if ( Ord(substr($dText,0,1)) == 1 ) {
if ( substr($dText,1,4) == '
 PING ' ) {
fputs($fp,'
 : ' . $nick . ' NOTICE ' . $dFrom . ' : ' .
chr(1) . '
 PING ' . substr($dText,6) . chr(1) . CRLF);
}
elseif ( substr($dText,1,7) == '
 VERSION ' ) {
fputs($fp,'
 : ' . $nick . ' NOTICE ' . $dFrom . ' : ' .
chr(1) . '
 VERSION PHPirc ' . chr(1) . CRLF);
}
}
else {
fputs($fp,$dText . CRLF);
}
}
}
elseif ( substr($data,0,4) == '
 PING ' ) {
fputs($fp,'
 PONG . substr ( $data , 5 ) . CRLF );
}
}
fclose ( $fp ); ?>
After the two files above are made ​​now open your browser and navigate to the file form.html, for examplehttp://www.domain.com/irc/form.html and fill your identity specifications. Now you can see the nick that you fill join the channel you specify. Actually the script above is the beginning of your learning to make a bot on IRC. Please tries. 

The author suggest that you try this script only on a free hosting server only, because it feared if you use a hosting server you buy, then your account can be removed unilaterally by the management server. 

No comments:

Post a Comment