dongyue0225 2010-11-18 00:12
浏览 35
已采纳

PHP - IRC连接

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
");
    fwrite($ircSocket, "NICK Rawr" . rand() . "
");
    fwrite($ircSocket, "JOIN " . $ircChannel . "
");

    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]."
");
            }
        }
    }
} 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.

  • 写回答

1条回答 默认 最新

  • duanbo6482 2010-11-18 00:36
    关注

    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 . "
    ")
    }
    
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错