Cannot access a certain channel via PHP script

I am testing a PHP script that logs me into the Twitch IRC, and it works fine; however, when I test it on twitchplayspokemon, it fails. The channel does have rules regarding bot spam and scripting, but I wrote my script to view channels only and do nothing else.

By reading the excerpt from the channel’s Info section about bots and scripts below, can anyone tell if the channel is really preventing my kind of script to be executed?

  • No using scripts to make unattended or artificial-seeming inputs

Botting and scripting

With some exceptions there is to be no active participation in chat from bots. Passive and reactive botting such as listening to chat and responding to whispers is permitted.

Behaviors exceptionally permitted by bots and scripting are:

  • Scraping for information by querying tpp and processing the response
  • Processing messages said in chat and #tpp
  • Betting on matches using tokens (pokeyen betting is not permitted)
  • Trading badges

Otherwise ask for permission first or your bot will be immediately banned.

1 Like

There isn’t really a way to tell. The rules do state that you need to ask for permission first or risk getting banned. You should talk to the channel creator and see about permission.

You say it fails, how does it fail?

I’m saying that I’m not able to access the channel at all using my script, therefore not working for some reason.

Just to see that I understand you correctly, you are trying to join the ttp irc chat, and when you try that, you get that error in return?

A part of my script prints success and error messages on a text file, but I am not getting any errors.

Provide script please

Channels can’t stop their chat from being read in any way. If your script is failing to read the chat, it’s a problem with the script. Lots of knowledgeable people here can help, if you provide the code.

I just tried to join that channel trough IRC and it worked fine, I don’t see why you should have a problem doing so.

If your script is made just to read the channel, try it with a justinfan-login instead.

Since you guys asked for my script, here it is. Just copy and paste this into a new PHP file, save it as irc.php, then run it from your command line program. I provided the command syntax in the code.

<?php
// enable $argv
ini_set('register_argc_argv', 1);

/*
$argv values
1 — Twitch account
2 — Channel to access
3 — Account token (password)

Use this syntax in your command line to run:
php <path to script> <your username> <channel> <token>
*/

// Text file for login output
$lgnFileLoc = "./login.txt";

if (!$argv[1] || !$argv[2] || !$argv[3]) {
    // log IRC attempt with error
    $lgnFile = fopen($lgnFileLoc, "a");
    fwrite($lgnFile, "Script arguments could not be supplied\r\n");
    fclose($lgnFile);

} else {
    // log IRC with arguments
    $lgnFile = fopen($lgnFileLoc, "a");
    fwrite($lgnFile, "Accessing ".$argv[2]."'s channel as ".$argv[1]." with a token of ".$argv[3]."\r\n");
    fclose($lgnFile);

    // begin IRC
    irc_action($argv[1], $argv[2], $argv[3]);
}

function irc_action($user, $ch, $pass) {

set_time_limit(30);

function IRC($name, $channel, $pwd) {
    $config = array(
            'server'  => 'irc.twitch.tv', 
            'port'    => 6667, 
            'channel' => '#'.$channel,
            'name'    => $name,
            'nick'    => $name,
            'pass'    => 'oauth:'.$pwd
    );

    $chatServer = array();
    $chatServer['connect'] = @fsockopen($config['server'], $config['port'], $errno, $errstr, 15);
    $cs = $chatServer['connect'];

    // log file
    $lgnFileLoc = $GLOBALS['lgnFileLoc'];

    if ($cs) {
        // login success
        fwrite($cs, "PASS " . $config['pass'] . "\r\n");
        fwrite($cs, "NICK " . $config['nick'] . "\r\n");
        fwrite($cs, "USER " . $config['nick'] . "\r\n");
        fwrite($cs, "JOIN " . $config['channel'] . "\r\n");

        $lgnFile = fopen($lgnFileLoc, "a");
        fwrite($lgnFile, "IRC login succeeded at ".date("H:i:s Y-m-d").".\r\n");
        fclose($lgnFile);

        echo "IRC login succeeded.\r\n";

        $done = false;
        $tries = 0;

        while (!$done && $tries < 15) {
            $line = fgets($cs,128);
            $tries++;
            $done = strpos($line,">");
        }

        $done = false;
        while (!feof($cs) && !$done) {
            $line = fgets($cs,128);
            $done = strpos($line,"End of");
            flush();
        }

        while (!feof($cs)) {
            $line = fgets($cs,128);
            if (strlen($line) > 0) {
                $tokens = explode(' ',$line);

                if ($tokens[0] == 'PING') {
                    fwrite($cs,"PONG " . $tokens[1]);

                } else {
                    fwrite($cs, "PART ". $config['channel'] . "\r\n");
                    exit();
                    
                }
                flush();
            }
            sleep(1);
        }     
    } else {

        $lgnFile = fopen($lgnFileLoc, "a");
        fwrite($lgnFile, "IRC login failed at ".date("H:i:s Y-m-d").". Error ".$errno.": ".$errstr."\r\n");
        fclose($lgnFile);

        echo "IRC login failed.\r\n";

        exit();
    }
}

IRC($user, $ch, $pass);
}
?>

Well, played with your code quite a bit and found that it doesn’t get past this:

while (!$done && $tries < 15) {

not sure if it is different on twitch, but at this point on another IRC server it’s expecting a reply to a ping here.

The reason your code doesn’t work is because you’re not properly parsing from IRC. If you are not experienced in programming or the RFC then use a pre-existing library rather than attempting to create your own.

Your solution is hardcoded to check for words in specific response lines, coupled with predictions on the maximum number of lines you might receive until you find what you’re looking for. I suspect that these statements are the cause of your troubles.

I’d suggest making a single loop that checks for data on the socket, and properly parsing said data in correspondence with the IRC RFC.

Pretty sure it would work if he was to remove the first 2 while loops, and disable the PART command when the received line isn’t PING. I think that’s all it to me to get it working, but don’t remember now, was 2 days ago when I first saw this thread. :stuck_out_tongue:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.