Search code examples
phpjoincommandircbots

PHP IRC Bot - Wait for 001


Okay. I know the problem, and I'll try to describe it, but I cannot for the life of me find the answer, to the question.

Okay, so, my bot will connect to the server fine. But this specific server you have to wait for this:

:vervet.foonetic.net 001 Eve :Welcome to the Foonetic IRC Network Eve!~Eve@*-*-*-*.*.*.*.net<br />
:vervet.foonetic.net 002 Eve :Your host is vervet.foonetic.net, running version Unreal3.2.8.1<br />
:vervet.foonetic.net 003 Eve :This server was created Thu Feb 24 2011 at 00:34:28 EST<br />

Pay attention to the first line. "001". My bot needs to wait for that line to show up before it can run the JOIN command. It won't D: I've tried making a while(){} before the while(1){}, I've tried a do{}while(), and a simple if().

Here's my code.

<?php
    set_time_limit(0);

    $conf = array('server' => 'irc.foonetic.net', 'port' => '6667', 'channel' => '#lingubender', 'nick' => 'Eve', 'name' => 'EveBot_1.0', 'description' => 'An IRC bot created by PHP scripter Kinz');
    $replies = array('ver' => 'Eve 1.0', 'part' => 'Good-bye!', 'quit' => 'Shutting down. Talk to you all later!', 'regUsername' => '[email protected]', 'regPassword' => 'Example');   

    $socket = fsockopen($conf['server'], $conf['port']);
    fputs($socket, "USER ".$conf['nick'].' '.$conf['description'].' '.$conf['nick'].' :'.$conf['name']."\n");
    fputs($socket, "NICK ".$conf['nick']."\n");
    fputs($socket, "JOIN ".$conf['channel']."\n");

    while(1) {
        while($data = fgets($socket, 128)) {
            echo nl2br($data);
            flush();

            $grip = explode(' ', $data);



            if ($grip[0] == "PING") {
                fputs($socket, "PONG ".$grip[1]."\n");
            }

            $chan = $grip[2];
            $command = str_replace(array(chr(10), chr(13)), '', $grip[3]);
            strtolower($command);

            if ($command == ":eve" || $command == ":eve,") {
                fputs($socket, "PRIVMSG ".$chan." :Name recognition acknowledged.\n");

                $ex = $grip[4];
                strtolower($ex);

                switch($ex) {
                    case "shutdown":
                        fputs($socket, "QUIT ".$replies['quit']."\n");
                        break;
                    case "version":
                        fputs($socket, "PRIVMSG ".$chan[2]." :".$replies['ver'].' : '.$conf['description']." \n");
                        break;
                }
            } else {
                NULL;
            }
        }
    }
?>

Solution

  • Well, in this particular example, test if $grip[1] == "001".

    Also, don't loop for while (1) as it will never end. Loop for while (!feof($socket)), which will terminate the loop the moment the link is dead.