PHP IRC机器人,发送命令,但现在我需要机器人来读取命令

时间:2009-01-06 02:22:42

标签: php command irc prompt robot

我有一个PHP IRC机器人,我在我的频道中使用,我需要它来为我的脚本中的特定设置用户制作OP。无论如何,我希望机器人检查用户是否登录NickServ以防止任何形式的欺诈或任何事情。

无论如何,这是我的连接和DO事务代码,接下来是我真正需要的帮助。所有帮助表示赞赏。 :) 在Freenode上,键入/ NS ACC [user]将返回[user]是否使用数值登录,他们决定登录3和0-2作为某种未登录。

所以这是机器人如何登录我的IRC频道...(随意加入freenode上的#tyreus,请求BwaddArr(或他的电子邮件))

<?php
set_time_limit(0);  //Stop the script timing out

$server = "irc.freenode.net";        //server to connect to
$channel = "#tyreus";               //channel to connect to initialy
$password = "sumpass";             //password for bot to login to irc
$pass2 = "anotherpass";               //password to make the bot do stuff
$users[0] = "0";                  //array of logged in users
$nickname = "Samcal";            //Set the bot's nick here
$logger = FALSE;                //for the channel logger
$takeover = FALSE;             //for the auto banner

$socket=fsockopen($server,'6667') ; //Connect and join the channel

stream_set_timeout($socket, 300);  //Set a timeout value (so the bot quits if it's disconnected)
fwrite($socket, "NICK ".$nickname."\r\n");
fwrite($socket, "USER ".$nickname." 8 * ::\x01VERSON 1.0 Brad's bot\x01\n");  //read rfc 1459 to understand this line

  while ($line=fgets($socket)) {
     echo htmlentities($line)."<br>"; 
       if (strpos($line, "433")>0) die("error nick in use");  //Quit if bot's nick is already taken (irc code 433 is received)

       if (strpos($line, "004")>0) {
          fwrite($socket, "JOIN ".$channel."\r\n"); //Join the channel if everything is ok (irc code 004 is received)
          fwrite($socket, "NickServ IDENTIFY ".$nickname." ".$password."\r\n");
          fwrite($socket, "ChanServ OP ".$channel." Samcal\r\n");
          fwrite($socket, "MODE ".$channel." +v Samcal \r\n");
          break;
       }
  }

这是我真正需要所有帮助的地方! :)

 if(strpos($line, "PRIVMSG ".$channel." :+oB\r\n")>0) { //Command to make the bot run the command
    $name = "BwaddArr"; // my username, this can be easily changed to the other users who will need opping
    $command = "NickServ ACC $name"; // the NickServ command I was talking about
    $result = fwrite($socket, "$command \r\n"); // my attempt at retrieving the result
    $accr = readline(strpos($line, "$result \r\n")); //part 2 of my failure to retrieve a result
    $loggd = str_replace("3","three","$accr"); // replace '3' with 'three'
 if($loggd != "three") { // if result is not three do below
    fwrite($socket, "PRIVMSG ".$channel." :$name is not logged in. \r\n"); // write into the chat that the user is not logged in
}
 if($loggd == "three") { // OP the user if they are logged in
    fwrite($socket, "MODE ".$channel." +ov $name\r\n"); // sends the OPping command
}
}
?>

4 个答案:

答案 0 :(得分:1)

我假设第二个片段位于while(fgets())循环内。

您将不会在使用fwrite()的循环中获得结果。在

之后添加另一个fgets()
$result = fwrite($socket, "$command \r\n");

或者因此使用你的循环并且可能添加状态标志以了解如何处理其身体的下一次执行。

答案 1 :(得分:0)

首先,删除机器人的密码。(已修复)

给你的IRC一些提示,祝你好运,你现在正走在正确的轨道上。

答案 2 :(得分:0)

我小时候做过一次。

我使用了那种循环:

$irc = fsockopen($server, $port);
// ...
while(!feof($irc))
{
     $line = fgets($irc, 2048);

     // ...
     // Parsing $line here
     // ...
}

希望这有帮助。

答案 3 :(得分:0)

在上一个代码段中,打开名为$irc的套接字并写入名为$socket的套接字。 要么修复此问题,要么向我们展示您使用/引入$socket的更多代码。