views:

1199

answers:

8

Hello, 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
}
}
?>
A: 

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.

Forrest Marvez
A: 

Don't worry about the pass, It's already been changed, I change it everytime I reboot the bot. :)

Thanks for caring though, I really do appreciate it. :)

+1  A: 

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

$result = fwrite($socket, "$command \r\n");

or consequently use your loop and maybe add a status flag to know how to treat the next execution of its body.

Karsten
A: 

I did one, when I was young.

I used that kind of loop :

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

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

Hope this helps.

Aif
A: 

Ok, so now I have this, which just throws fgets() invalid stream and feof() invalid stream

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


 if(strpos($line, "PRIVMSG ".$channel." :+oB\r\n")>0) {
 $name = "BwaddArr";
 $command = "NickServ ACC $name";
 $accr = fwrite($socket, "$command \r\n");
 $result = strstr($accr, "$name", true);
 $result = str_replace("$accr ", "", $result );
 $result = trim($result, "\x00..\x1F");
    $accr = strpos($line, "$result \r\n");
 $loggd = str_replace("3","three","$accr");
 if($loggd != "three") {
 fwrite($socket, "PRIVMSG ".$channel." :$name is not logged in. \r\n");
}
 if($loggd == "three") {
 fwrite($socket, "MODE ".$channel." +ov $name\r\n");
}
 fwrite($socket, "PRIVMSG ".$channel." :Samcal sees this for var.loggd: $loggd \r\n");
 fwrite($socket, "PRIVMSG ".$channel." :Samcal sees this for var.result: $result \r\n");
 fwrite($socket, "PRIVMSG ".$channel." :Samcal sees this for var.command: $command \r\n");
 fwrite($socket, "PRIVMSG ".$channel." :Samcal sees this for var.accr: $accr \r\n");
}

}
You should edit this into your question, please
Karsten
A: 

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.

Karsten
A: 

Firstly, I don't use this script anymore, but more so, $socket is $socket=fsockopen($server,'6667'); and it used everytime you want the bot to say/do something.

never use the "answers" section like this, you can edit your own question or comment on other peoples answers
pablasso
A: 

Nice buddy