Ray
Ray

Reputation: 439

PHP - IRC Connection

Here is my script:

$ircServer = "";
$ircPort = "6667";
$ircChannel = "#";

set_time_limit(0);

$ircSocket = fsockopen($ircServer, $ircPort, $eN, $eS);

if ($ircSocket) {

    fwrite($ircSocket, "USER Lost rawr.test lol :code\n");
    fwrite($ircSocket, "NICK Rawr" . rand() . "\n");
    fwrite($ircSocket, "JOIN " . $ircChannel . "\n");

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

            // Separate all data
            $exData = explode(' ', $data);

            // Send PONG back to the server
            if($exData[0] == "PING") {
                fwrite($ircSocket, "PONG ".$exData[1]."\n");
            }
        }
    }
} else {
    echo $eS . ": " . $eN;
}

I am having problem's adding a function that will Private message everyone on the IRC channel. I tried $read and other methods it does not work and IRC hangs.

NOTE: This is for educational/private purposes no harm is done or made.

Upvotes: 1

Views: 4960

Answers (1)

Ruel
Ruel

Reputation: 15780

I've written several IRC bots years ago in Perl, and to be honest, I can't remember them anymore. Anyway, to send a private message to all the users, first you need to get all the users in the channel.

Anyway, the command to send a private message is:

PRIVMSG #channel :Sup?

Yeah, it will echo "Sup?" at the #channel. Then same goes for a user:

PRIVMSG John :Sup?

All you need to do is get all the users. To do this:

NAMES #channel

The code is up to you. Good luck.

EDIT: To get a percentage of the users, simply load them in an array, then use shuffle() shuffle($array); if you want to randomize their positions. Then use count() $size = count($array); for the array size, multiply the size by the percentage. $target = $size * 0.10; for 10%. Then use round() to get the rounded-off number.

Now, loop the array of users and set the limit to $target. There you have it.

EDIT: Here's a sample code (the rest of the code is up to you ofc:

...

$msg = $_POST['message'];
$pr = $_POST['percentage'];
$pr /= $100; // if the input is already 0.10 or something, no need to do this.

...

shuffle($users);
$size = count($users);
$target = $size * $pr;
$target = $round($target);

for ($i = 0; $i <= $target; $i++) {
    fwrite($ircSocket, "PRIVMSG " . $users[$i] . " :" . $msg . "\n")
}

...

Upvotes: 4

Related Questions