重新启动与IRC bot的连接

时间:2014-05-16 21:12:14

标签: php irc

我的PHP脚本始终是连接(IRC Bot)但是说僵尸程序遇到错误并且断开连接我希望它重新连接到服务器。

现在机器人正在本地运行,所以我可以使用bat文件重新启动僵尸程序,但是如果我将它托管在哪里,我将如何启动重新连接?

例如我的代码现在是:

<?php

set_time_limit(0);

include 'config.php'; 


fputs($socket,"USER TFBot TFBot.PugBot TFBot :The TitanFall.Pug Bot\n");


fputs($socket,"NICK $bnick\n");


$commands = array (
"${cmdsym}commands",
"${cmdsym}add",
"${cmdsym}join",
"${cmdsym}last",
"${cmdsym}coms",
"${cmdsym}voip",
"${cmdsym}promote",
"${cmdsym}need",
"${cmdsym}list",
"${cmdsym}pick",
"${cmdsym}remove",
);

while (1) {

    while($data=fgets($socket,128)) {
        $get = explode(' ', $data);

        if (stripos( $get[1], ':Closing' ) !== false) {
        echo "Reconnecting in 10 seconds\n";
        sleep(10);
        passthru("Startbot.bat");
        }
        if (stripos( $get[1], 'NICK' ) !== false) {
            $nick = explode(':',$get[0]);
            $nick = explode('!',$nick[1]);
            $nick = $nick[0]; //User who entered the command
            $nuser = $nick;
            $wnuser = preg_replace('/^\\:/','', $get[2]);
            $tnuser = rtrim($wnuser);

                if (chnickchange("tready.txt",$nuser)) {
                    fputs($socket,"CNOTICE $tnuser $jchan : Your name was updated from: $nuser to: $tnuser on the PUG List. - $chan.\n");
                nickchange("tready.txt",$nuser,$tnuser);    
                nickchange("pready.txt",$nuser,$tnuser);
                nickchange("sready.txt",$nuser,$tnuser);
                nickchange("militia.txt",$nuser,$tnuser);
                nickchange("imc.txt",$nuser,$tnuser);
                nickchange("captains.txt",$nuser,$tnuser);
                nickchange("pick.txt",$nuser,$tnuser);
                }
            }
            if (stripos( $get[1], 'QUIT' ) !== false) {
                    $nick = explode(':',$get[0]);
                    $nick = explode('!',$nick[1]);
                    $nick = $nick[0]; //User who entered the command
                    $partuser = $nick;
                    $line = file("tready.txt", FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
                        if (array_search($partuser, $line) !== FALSE) {
                            fputs($socket,"PRIVMSG $chan : $nick was removed from the PUG List. - QUIT.\n");
                                $DELETE = $partuser; 
                                $datap = file("tready.txt"); 
                                $out = array(); 

                                foreach($datap as $line) { 
                                    if(trim($line) != $DELETE) { 
                                        $out[] = $line; 
                                    } 
                                } 

                                $fp = fopen("tready.txt", "w+"); 
                                flock($fp, LOCK_EX); 
                                foreach($out as $line) { 
                                    fwrite($fp, $line); 
                                } 
                                flock($fp, LOCK_UN); 
                                fclose($fp);

                                $datap = file("sready.txt"); 
                                $out = array(); 

                                foreach($datap as $line) { 
                                    if(trim($line) != $DELETE) { 
                                        $out[] = $line; 
                                    } 
                                } 

                                $fp = fopen("sready.txt", "w+"); 
                                flock($fp, LOCK_EX); 
                                foreach($out as $line) { 
                                    fwrite($fp, $line); 
                                } 
                                flock($fp, LOCK_UN); 
                                fclose($fp); 
                        sleep(1);
                        $listnp = file("tready.txt", FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
                        $nplayers = $pugmax - count($listnp);                               
                        if ($nplayers !== 0) {
                            fputs($socket,"PRIVMSG $chan : ${afcolor}(${scolor}$nplayers needed to begin${afcolor})\n");
                        }                               
                }
            }

        if ($get[0] == "PING") {
            fputs ($socket, "PONG ".$get[1]."\n");
        }
        if (stripos( $data, 'Nickname is already in use.' ) !== false) {
        $bnick = $bnick . "|2";
        fputs($socket,"NICK $bnick\n");
        }
        if (stripos( $data, 'End of /MOTD command.' ) !== false) {

        fputs($socket,"PRIVMSG Q@CServe.quakenet.org : AUTH $authname $authpass\n");
        sleep(1);
        fputs($socket,"MODE $bnick +x\n");
        //Enter the channel you want to use your bot on.
        sleep(2);
        fputs($socket,"JOIN $jchan\n");
        }


        if (substr_count($get[2],"#")) {
            $nick = explode(':',$get[0]);
            $nick = explode('!',$nick[1]);
            $nick = $nick[0]; //User who entered the command
            $nhost = explode('!', $get[0]); //User Hostname for private commands
            $nhost = $nhost[1];
            $chan = $get[2]; //the channel the bot is in
            $num = 3; //If you observe the array format, actually text starts from 3rd index.
            if ($num == 3) {
                $split = explode(':',$get[3],2);
                $text = rtrim($split[1]); //trimming is important. never forget.
            // Bot Public Commands
            /* if (stripos( $get[1], 'PART' ) !== false) {
                    $partuser = $nick;
                    removeuser($partuser,"tready.txt","0");
                    removeuser($partuser,"pready.txt","1");
                    removeuser($partuser,"militia.txt","1");
                    removeuser($partuser,"imc.txt","1");
                    removeuser($partuser,"captains.txt","2");
                    removeuser($partuser,"pick.txt","2");
            } */
            if (stripos( $get[1], 'PART' ) !== false) {
                    $partuser = $nick;
                    $line = file("tready.txt", FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
                        if (array_search($partuser, $line) !== FALSE) {
                            fputs($socket,"NOTICE $partuser : You were removed from the PUG List. - $chan.\n");
                            fputs($socket,"PRIVMSG $chan : ${afcolor}$partuser ${scolor}was removed from the pug list${afcolor}. - ${scolor}Reason${afcolor}: PART.\n");
                            $DELETE = $partuser; 
                                $datap = file("tready.txt"); 
                                $out = array(); 

                                foreach($datap as $line) { 
                                    if(trim($line) != $DELETE) { 
                                        $out[] = $line; 
                                    } 
                                } 

                                $fp = fopen("tready.txt", "w+"); 
                                flock($fp, LOCK_EX); 
                                foreach($out as $line) { 
                                    fwrite($fp, $line); 
                                } 
                                flock($fp, LOCK_UN); 
                                fclose($fp);

                                $datap = file("sready.txt"); 
                                $out = array(); 

                                foreach($datap as $line) { 
                                    if(trim($line) != $DELETE) { 
                                        $out[] = $line; 
                                    } 
                                } 

                                $fp = fopen("sready.txt", "w+"); 
                                flock($fp, LOCK_EX); 
                                foreach($out as $line) { 
                                    fwrite($fp, $line); 
                                } 
                                flock($fp, LOCK_UN); 
                                fclose($fp);  

                        sleep(1);
                        $listnp = file("tready.txt", FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
                        $nplayers = $pugmax - count($listnp);                               
                        if ($nplayers !== 0) {
                            fputs($socket,"PRIVMSG $chan : ${afcolor}(${scolor}$nplayers needed to begin${afcolor})\n");
                        }
                }
            }

1 个答案:

答案 0 :(得分:1)

好像你已经启用了passthru。

一个简单的脚本是:

<?PHP
passthru('c:/path/to/file/startbot.bat');

你已经在做什么了。这将调用批处理文件,然后重新启动脚本。

或者,

<?PHP
passthru('c:/path/to/php/php c:/path/to/script/script.php');