代码之家  ›  专栏  ›  技术社区  ›  user1079988

向IRC机器人发送私人消息

  •  1
  • user1079988  · 技术社区  · 13 年前

    我有一个php脚本来加入这个IRC服务器上的一个频道:IRC.worldnet.net 多亏了服务器的响应,我可以说我成功地加入了频道,并且机器人在用户列表中。但是,此命令:

    MSG bot_nickname hello my friend !
    

    发出“消息:未知命令”响应。 当我发送此行时:

    PRIVMSG bot_nickname : hello my friend !
    

    机器人没有响应(我没有任何错误消息)。 我发送的命令有什么问题吗?

    以下是我使用的php代码:

    <?php
    
    $channel_sent = false;
    $channel_joined = false;
    $msg_sent = false;
    set_time_limit(0);
    ini_set('display_errors', 'on');
    
    $config = array(
        'server' => 'irc.worldnet.net',
        'port' => 6667,
        'nick' => 'cesame'.rand(),
        'channel' => '#nc-irc-challs'
    );
    
    $server = array();
    $server['SOCKET'] = @fsockopen($config['server'], $config['port'], $errno, $errstr, 2); 
    
    if($server['SOCKET'])
    {
        SendCommand("PASS NOPASS\n\r");
        SendCommand("NICK " . $config['nick'] . "\n\r"); 
        SendCommand("USER " . $config['nick'] . " USING PHP IRC\n\r");    
        SendCommand("JOIN " . $config['channel'] . "\n\r");
    
        while(!feof($server['SOCKET'])) {
                ReadServer();
                flush();
                sleep(1);
        }
    }
    
    function ReadServer(){
        global $server;
        global $config;
        global $channel_joined;
        global $channel_sent;
        global $msg_sent;
        $server['READ_BUFFER'] = fgets($server['SOCKET'], 1024);
        echo "[RECEIVE] ".$server['READ_BUFFER']."<br>\n\r";  
    
        if(substr($server['READ_BUFFER'], 0, 6) == "PING :") { 
            SendCommand("PONG :".substr($server['READ_BUFFER'], 6)."\n\r"); 
        }
        if(strpos($server['READ_BUFFER'], "#nc-irc-challs :End of /NAMES list")){
            $channel_joined = true;
        }
        if(trim($server['READ_BUFFER']) == "" && $channel_joined &&!$msg_sent) {
            SendCommand('PRIVMSG Daneel : .challenge_caesar start' . "\n\r");
            $msg_sent = true;
        }
    }
    
    function SendCommand ($cmd)
    {
        global $server; //Extends our $server array to this function
        @fwrite($server['SOCKET'], $cmd, strlen($cmd)); //sends the command to the server
        echo "[SEND] $cmd <br>"; //displays it on the screen
    } 
    
    ?>
    

    所以我听从了你的建议。这是我得到的日志:

    [RECEIVE] :cesame1582!~cesame158@Wnet-59540.41.55.213.rev.sfr.net JOIN :#nc-irc-challs 
    [RECEIVE] :Vidar.IRC.Worldnet.Net 332 cesame1582 #nc-irc-challs :NewbieContest -- http://www.newbiecontest.org/ (channel officiel : #newbiecontest) -- Rappel : aucune épreuve ne peut se résoudre en bruteforce. 
    [RECEIVE] :Vidar.IRC.Worldnet.Net 333 cesame1582 #nc-irc-challs zours 1195848644 
    [RECEIVE] :Vidar.IRC.Worldnet.Net 353 cesame1582 = #nc-irc-challs :cesame1582 \o_ @Eole +Daneel 
    [RECEIVE] :Vidar.IRC.Worldnet.Net 366 cesame1582 #nc-irc-challs :End of /NAMES list. 
    [SEND] PRIVMSG Daneel : .challenge_caesar start 
    [RECEIVE] :Global!Services@Worldnet.Net NOTICE cesame1582 :[Logon News - Apr 07 2004] Use irc.worldnet.net to join our network, thanks | Dorenavant, utilisez irc.worldnet.net pour joindre notre reseau, merci. 
    [RECEIVE] :Global!Services@Worldnet.Net NOTICE cesame1582 :[Logon News - Jan 07 2007] If you see a connection on port 23, 1080, 3128 or 8080 from 194.117.194.78 this is NOT an attack! It's our insecure proxy detector. 
    [RECEIVE] :Global!Services@Worldnet.Net NOTICE cesame1582 :[Logon News - Feb 07 2007] Vous pouvez utiliser le port 7000 pour une connexion en SSL. You can use port 7000 with a secure SSL connection. 
    [RECEIVE] :Global!Services@Worldnet.Net NOTICE cesame1582 :[Logon News - Oct 14 2009] Salons officiels du reseau Worldnet : #worldnet - #help pour de l'aide sur IRC et l'informatique en general. - ##opers pour les problemes réseau spécifiques à Worldnet. 
    [RECEIVE] :Global!Services@Worldnet.Net NOTICE cesame1582 :[Random News - Apr 24 2004] Pour avoir de l'aide sur les commandes des services, visitez http://help.irc.worldnet.net 
    [RECEIVE] :NickServ!Services@Worldnet.Net NOTICE cesame1582 :Your nick isn't registered. 
    [RECEIVE] 
    [RECEIVE] PING :Vidar.IRC.Worldnet.Net 
    [SEND] PONG :Vidar.IRC.Worldnet.Net 
    [RECEIVE] 
    [RECEIVE] PING :Vidar.IRC.Worldnet.Net 
    [SEND] PONG :Vidar.IRC.Worldnet.Net 
    
    1 回复  |  直到 13 年前
        1
  •  3
  •   Madara's Ghost    13 年前

    IRC服务器通常不会发送空字符串。

    如果您希望在BOT在通道中后发出命令,则应在知道它在通道中时立即发送,即在 /NAMES 列表,或者一旦您收到 TOPIC


    此外,PRIVMSG的语法如下:

    PRIVMSG <NICKNAME> :<MESSAGE>
    

    没有后面的空格 : .如果接收端设置为响应 .command 但是得到 .command ,这可能就是问题所在。

    推荐文章