PHP IRC机器人,发送命令,但现在我需要机器人来读取命令
我有一个在我的频道中使用的 PHP IRC 机器人,我需要它来对我在脚本中设置的特定用户进行操作。 无论如何,我希望机器人检查用户是否登录 NickServ 以防止任何形式的欺诈或任何事情。
无论如何,这是我的连接和执行操作代码,下面是我真正需要帮助的内容。 感谢所有帮助。 :) 在 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
}
}
?>
I have a PHP IRC Robot that I use in my channel and I need it to make OPs to specific set users in the script by me. Anyways I want the robot to check if the user is logged into NickServ to prevent any sort of fraud or anything.
Anyways, here is my connect and DO things code, followed by what I really need help with below it. All help is appreciated. :)
On Freenode, typing /NS ACC [user] will return whether or not the [user] is logged in with a numerical value, they decided 3 would be logged in. and 0-2 as some sort of not logged in.
So here is how the bot logs into my IRC channel... (feel free to join #tyreus on freenode, ask for BwaddArr (or his email))
<?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;
}
}
And this is where i really need all the help! :)
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
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我假设第二个片段位于 while(fgets()) 循环内。
然后,您将不会在使用 fwrite() 的循环中得到结果。 在之后添加另一个 fgets()
或随后使用循环,并可能添加一个状态标志以了解如何处理其主体的下一次执行。
I assume the second snippet resides inside your while(fgets()) loop.
You won't have your result in the loop where you use fwrite() then. Either add another fgets() after
or consequently use your loop and maybe add a status flag to know how to treat the next execution of its body.
首先,删除你的机器人的密码。(已修复)
在你的 IRC 上给了一些提示,祝你好运,你现在走在正确的道路上。
First of all, remove the password for your bot.(fixed)
Gave some tips on your IRC, good luck, you're on the right track now.
我年轻的时候就做过一件事。
我使用了这种循环:
希望这有帮助。
I did one, when I was young.
I used that kind of loop :
Hope this helps.
在最后一个代码片段中,您打开名为
$irc
的套接字并写入名为$socket
的套接字。要么修复此问题,要么向我们展示更多使用/引入
$socket
的代码。In your last code snippet you open your socket named
$irc
and write to a socket called$socket
.Either fix this or show us some more of your code where you use/introduce
$socket
.