Multi-server IRC Bot

Questions about programming languages and debugging
Post Reply
User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

Multi-server IRC Bot

Post by Gogeta70 »

I have to thank n3rd for this idea, it gave me something to code, excluding my forums. I really feel like a genius after coding this bot, because i was about to pull out hair trying to get php to multithread... what i did here was kind of emulate multithreading. Anyway, at first i simply tried to manage several connections in an array, but simultaneous connections seemed to react slowly, so i had to find something else, which is why the buffer and connection variables are arrays. I was too lazy to change them.. I'm still debating whether to implement this into G7 or not, since he's kind of THE "suck-o bot"... Anyway, here's the code. Enjoy.


File: msbot.php (not microsoft bot, multi server bot...)

Code: Select all

<pre>
<?PHP

//### Multi-Server IRC bot By Gogeta70 ###//


set_time_limit(0);
$server = $argv[1];
if(empty($server))
  $server = "irc.suck-oold.com";
$port = 6667;
$nick = "G7-multi_srvr";
$channel = $argv[2];
if(empty($channel))
  $channel = "#suck-o";
$buffer = array();
$first = TRUE;

$allowed = str_split("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.#-");
$servertmp = $server;
$channeltemp = $channel;
for($a = 0; $a < count($allowed); $a++)
{
$servertemp = str_replace($allowed[$a], NULL, $servertemp);
$channeltemp = str_replace($allowed[$a], NULL, $channeltemp);
}
if(!empty($channeltemp) || !empty($servertemp))
  exit(1);

$con = array();

if(!$con[0] = fsockopen($server, $port))
  die("Unable to connect to server.");

send("USER $nick fatalh.sytes.net fatalh.sytes.net :$nick", 0);
send("NICK $nick", 0);

while(!feof($con[0]))
{

for($a = 0; $a < count($con); $a++)
{
$buffer[$a] = trim(fgets($con[$a], 4096));
echo " <<< " . $buffer[$a] . chr(10);
}

for($a = 0; $a < count($buffer); $a++)
{
if(substr($buffer[$a], 0, 4) == "PING")
{
send("PONG :" . substr($buffer[$a], 6), $a);
}
}

if($first === TRUE)
{
send("JOIN $channel", 0);
$first = FALSE;
}

for($a = 0; $a < count($buffer); $a++)
{
if($buffer[$a] != NULL)
{
commands($a);
$buffer[$a] = NULL;
}
}

}

function send($cmd, $a)
{ global $con;

//echo "Con$a[". date("g:ia") ."] >>> $cmd\n";
echo " >>> " . $cmd . chr(10);
fputs($con[$a], $cmd . "\r\n");

}

function commands($ofs)
{ global $buffer, $con, $nick;

$chan = explode(" ", $buffer[$ofs]);
@$chan = $chan[2];
$said = explode("$chan :", $buffer[$ofs]);
@$said = $said[1];
$usr = explode("!", $buffer[$ofs]);
@$usr = substr($usr[0], 1);

if(substr($said, 0, 4) == "!say")
{
$what2say = substr($said, 5);
send("PRIVMSG $chan :$what2say", $ofs);
}

if(substr($said, 0, 7) == "!server")
{
$srvr = substr($said, 8);
$srvr = explode(" ", $srvr);
if(!empty($srvr[0])
  exec("C:\php\php E:\msbot.php ". $srvr[0] . " " . $srvr[1]);
send("PRIVMSG $chan :Connecting... bot will join channel when connected.", 0);

}

if(substr($said, 0, 5) == "!join")
{
$chnl = substr($said, 6);
send("JOIN $chnl", $ofs);
}

if($said == "!disconnect")
{
if($ofs == 0)
send("QUIT:Multi server bot, Created by Gogeta70", $ofs);
unset($con[$ofs]);
unset($buffer[$ofs]);
}
}
Edit: I've noticed something that you may want to change if you use this code. Remove the array aspects on the $con and $buffer variables, and the command() and send() functions, as this seems to make the bot ping out occasionally.
¯\_(ツ)_/¯ It works on my machine...

User avatar
n3rd
Staff Member
Staff Member
Posts: 1474
Joined: 15 Nov 2005, 17:00
18
Location: my own perfect world in ma head :)
Contact:

Post by n3rd »

Sweet, didnt bother to go over the entire code but does it have the same functionalities as G7?
[img]http://img580.imageshack.us/img580/8009/userbar2k.png[/img]

User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

Post by Gogeta70 »

No, this is just a skeleton bot really, it only has 3 functions: !server, !disconnect, and !say.

G7 is over 1,000 lines of code :lol:
¯\_(ツ)_/¯ It works on my machine...

rhysh
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 767
Joined: 15 Nov 2006, 17:00
17
Contact:

Post by rhysh »

hmmmm.nice.lol usefull for bigbotnets...in the thousands

or if you have multiple ones and cant import them together...dont really know how to explain it

Post Reply