Jump to content

IRC Socket Connection - Works on local computer


djcubez

Recommended Posts

Alright so I found an older script that opens up a socket connection to an IRC server as a user. You could use it for simple scripts, a bot or even as a chat client. All I want to do is have it join a specific channel, get the users and spit them out in a  list. I'm doing this to generate a list of users in a chat for a website. So I bet you're all wonder what the problem is right?

 

The script works perfect on my home server. Executes fine, joins fine, grabs the information fine. It's flawless. However, whenever I put it on any web host all I get is a blank page. Does anyone know why?

 

I've heard it's because some hosts block socket connections because they can't monitor that bandwidth usage but I don't know how reliable that is. I'll post the code below but if anyone has any suggestions I'd love to hear them.

 

<?php

//First lets set the timeout limit to 0 so the page wont time out.
set_time_limit(0);
//Also inclue our config file
include_once("config.php");
//Second lets grab our data from our form.
$gotusers = "0";
$randomnumber = rand(1000, 9999);
$nickname = "PackersHome" . $randomnumber;
//Now lets check to see if there is a nickname set.

    //Ok, We have a nickname, now lets connect.
    $server = array(); //we will use an array to store all the server data.
    //Open the socket connection to the IRC server
    $server['SOCKET'] = @fsockopen($server_host, $server_port, $errno, $errstr, 2);
    if($server['SOCKET'])
    {
        //Ok, we have connected to the server, now we have to send the login commands.
        SendCommand("PASS NOPASS\n\r"); //Sends the password not needed for most servers
          SendCommand("NICK $nickname\n\r"); //sends the nickname
          SendCommand("USER $nickname USING PHP IRC\n\r"); //sends the user must have 4 paramters
        while(!feof($server['SOCKET'])) //while we are connected to the server
        {
            $server['READ_BUFFER'] = fgets($server['SOCKET'], 1024); //get a line of data from the server
            //echo "[RECIVE] ".$server['READ_BUFFER']."<br>\n\r"; //display the recived data from the server
            
            /*
            IRC Sends a "PING" command to the client which must be anwsered with a "PONG"
            Or the client gets Disconnected
            */
            //Now lets check to see if we have joined the server
            if(strpos($server['READ_BUFFER'], "376")) //422 is the message number of the MOTD for the server (The last thing displayed after a successful connection)
            {
                //If we have joined the server
                
                SendCommand("JOIN $server_chan\n\r"); //Join the chanel
            }
            if(substr($server['READ_BUFFER'], 0, 6) == "PING :") //If the server has sent the ping command
            {
                SendCommand("PONG :".substr($server['READ_BUFFER'], 6)."\n\r"); //Reply with pong
                //As you can see i dont have it reply with just "PONG"
                //It sends PONG and the data recived after the "PING" text on that recived line
                //Reason being is some irc servers have a "No Spoof" feature that sends a key after the PING
                //Command that must be replied with PONG and the same key sent.
            }
		if(strpos($server['READ_BUFFER'], "353")) 
		{
			// Displayed users
			// Get strpos
			$chunk = $server['READ_BUFFER'];
			$startpos = strpos($chunk, "353");
			$endpos = strpos($chunk, "366");
			$lengthpos = $endpos - $startpos;
			$line = substr($chunk, $startpos, $lengthpos);
			$line_parts = explode("353", $line);
			$line_parts = explode(":", $line_parts[1]);
			$users = explode(" ", $line_parts[1]);
			$usercount = count($users);
			echo "<br /><b>Users</b>: " . $usercount . "<br />";
			for($i = 0; $i < $usercount; $i++) {
				echo $users[$i] . "<br />";
			}
			//echo "<b>$chunk</b><br />";
			$gotusers = "1";

		}
            flush(); //This flushes the output buffer forcing the text in the while loop to be displayed "On demand"

		if($gotusers == "1") {
			fclose($server['SOCKET']);
			exit(); // got everything
		}

        }
    }

function SendCommand ($cmd)
{
    global $server; //Extends our $server array to this function
    @fwrite($server['SOCKET'], $cmd, strlen($cmd)); //sends the command to the server
    echo "[sEND] $cmd <br>"; //displays it on the screen
}


?> 

 

CONFIG.PHP

<?php
//The server host is the IP or DNS of the IRC server.
$server_host = "chat.freenode.net";
//Server Port, this is the port that the irc server is running on. Deafult: 6667
$server_port = 6667;
//Server Chanel, After connecting to the IRC server this is the channel it will join.
$server_chan = "#PackersHome";
?> 

 

FYI: I found a web host that lets you use socket connections but it still doesn't work there: http://djcubez.hostoi.com/scripts/irc/ircconnect.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.