a basic IRC Bot with a few commands.
Commands are:
!sayit - The bot just repeats your message.
!md5 - The bot just says the md5 sum of your message.
How to run it, you change the variables to which i said to edit. then you save it as whatever you want.php then just double click it on windows, or on linux type php filename.php...
you must have php installed.
<?php
// Prevent PHP from stopping the script after 30 sec
set_time_limit(0);
// Edit these settings
$chan = "#php";
$server = "127.0.0.1";
$port = 6667;
$nick = "PHP_Bot";
// STOP EDITTING NOW.
$socket = fsockopen("$server", $port);
fputs($socket,"USER $nick $nick $nick $nick :$nick\n");
fputs($socket,"NICK $nick\n");
fputs($socket,"JOIN ".$chan."\n");
while(1) {
while($data = fgets($socket)) {
echo nl2br($data);
flush();
$ex = explode(' ', $data);
$rawcmd = explode(':', $ex[3]);
$oneword = explode('<br>', $rawcmd);
$channel = $ex[2];
$nicka = explode('@', $ex[0]);
$nickb = explode('!', $nicka[0]);
$nickc = explode(':', $nickb[0]);
$host = $nicka[1];
$nick = $nickc[1];
if($ex[0] == "PING"){
fputs($socket, "PONG ".$ex[1]."\n");
}
$args = NULL; for ($i = 4; $i < count($ex); $i++) { $args .= $ex[$i] . ' '; }
if ($rawcmd[1] == "!sayit") {
fputs($socket, "PRIVMSG ".$channel." :".$args." \n");
}
elseif ($rawcmd[1] == "!md5") {
fputs($socket, "PRIVMSG ".$channel." :MD5 ".md5($args)."\n");
}
}
}
?>
Can someone tell me how to start in in Windows? The thing is, that i cant just dubbleclick it, then I get the texteditor. And if i open it in the browser (I got Xampp), it connects to IRC (quakenet), but not to the chan, and it requires me to always keep my browser open. Not an option as you might figure :P
by closing the socket. try using socket_ functions in php. example here http://www.hawkee.com/snippet/6076/
by the way killwithme, if you launch the script with a .bat file it uses cmd.exe as the bot window.
some networks do not allow instant channel join so you would have to use a while loop before you join the channel with a sleep in it and some pong back inorder not to get disconnected from server
while($logincount < 10) {
$logincount++;
$data = fgets($socket, 128);
echo nl2br($data);
// Separate all data
$ex = explode(' ', $data);
// Send PONG back to the server
if($ex[0] == "PING"){
fputs($socket, "PONG ".$ex[1]."\n");
}
flush();
}
sleep(1);
this should be placed right under the :
fputs($socket,"NICK $nick\n");
NOTE to furby :I guess you should use some sleep commands inorder to save the pain from cpu leaks and server pressure a "sleep(1);" would be good but if you want you can use "usleep():" for milliseconds:)