TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   mirc php script (http://www.talkphp.com/general/3326-mirc-php-script.html)

sarmenhb 09-05-2008 09:14 PM

mirc php script
 
anyone know of a php script that is like mirc?
-that shows the users logged in

thanks

xenon 09-06-2008 05:07 PM

Perhaps you mean a chat room, and that can't be done using php only.

sarmenhb 09-06-2008 08:05 PM

Quote:

Originally Posted by xenon (Post 18251)
Perhaps you mean a chat room, and that can't be done using php only.

do you know of a open source version i can use?

xenon 09-06-2008 08:10 PM

How about PHPOpenChat, PHP based free live Chat Server Software?

Salathe 09-06-2008 09:23 PM

Quote:

Originally Posted by xenon (Post 18251)
... that can't be done using php only.

Oh really?

To help guide the original question, in order to display the logged in users on an IRC server all that you need to do is connect to the server and send along a specially formatted command that the server understands, nothing complicated at all. There are various implementations in PHP which make talking to IRC servers easier, faster, object-oriented and so on (a Google search should suffice) or you can learn to use PHP's built-in code to do things in a more low-level way (through sockets and such like, which most of the abstractions do anyway).

sketchMedia 09-07-2008 10:21 AM

I've even made an IRC bot in the past with PHP, so PHP is more than capable of doing it. I'll see if I can dig up the code and show you the IRC commands you need to post through a socket to the IRC server.

xenon 09-07-2008 10:35 AM

Sorry guys, I didn't mean that it can't be done literally, what I meant is that nobody likes a chat room where the page need to be refreshed when posting a message or when a user logs in/out. That's why I said "php only". In order to do a decent chat room you also need to make use of some javascript (ajax) and SQL. Obviously, there's more than php there. I, personally, would also use a Java server to do all the server-side work and let PHP handle the client part. Simply because it's more efficient than PHP when talking about sockets and streaming (see threads and buffering).

But, I guess I missed the point a little bit, because now I see that he wants to send messages to an IRC server rather than create a chat server.

sketchMedia 09-07-2008 01:12 PM

I have quickly knocked this together

PHP Code:

<?php
set_time_limit
(0);//so we dont timeout

class IRC
{
    const 
SERVER  'b0rk.uk.quakenet.org';
    const 
PORT    6667;
    const 
CHANNEL '#sketch';
    const 
NICK    'sketchmedia2';

    private static 
$pSocket;
    private static 
$szBuffer;

    public static function 
getUsers()
    {
        if(!
self::$pSocket fsockopen(self::SERVERself::PORT$iError$szError2))
        {
            die(
$iError ' - ' .$szError);
        }
        
$szUsers self::mainLoop();
        return 
explode(' 'trim(str_replace(':'''substr($szUsersstrpos($szUsersself::CHANNEL) + strlen(self::CHANNEL)))));
    }
    private static function 
mainLoop()
    {
        
self::sendCmd('PASS NOPASS');
        
self::sendCmd('NICK 'self::NICK);
        
self::sendCmd('USER 'self::NICK ' USING TALKPHP IRC');
         while(!
feof(self::$pSocket))
        {
            
self::$szBuffer fgets(self::$pSocket1024);
            if(
strpos(self::$szBuffer'376'))
            {
                
self::sendCmd('JOIN ' self::CHANNEL);
            }
            if(
substr(self::$szBuffer06) == 'PING :')
            {
                
self::sendCmd('PONG :'.substr(self::$szBuffer6));
            }
            if(
strpos(self::$szBuffer'353'))
            {
                
fclose(self::$pSocket);
                return 
self::$szBuffer;
            }
            
//echo self::$szBuffer , '<br />';
        
}
    }
    private static function 
sendCmd($szCmd)
    {
        
$szCmd .= "\n\r";
        
fwrite(self::$pSocket$szCmdstrlen($szCmd)) or die('Error'); //sends the command to the server
    
}
}
echo 
'<pre>'var_dump(IRC::getUsers()), '</pre>';

quite simple, a little explanation though:
PHP Code:

 if(strpos(self::$szBuffer'376'))
 {
      
self::sendCmd('JOIN ' self::CHANNEL);
  } 

this checks to see if the MOTD has finished, meaning we really are connected and we can send the JOIN command

PHP Code:

 if(substr(self::$szBuffer06) == 'PING :')
{
     
self::sendCmd('PONG :'.substr(self::$szBuffer6));


this line responds to PING requests sent by the IRC server, this allows each computer to know if the opposite end is still present and available, you will be disconnected if you don't respond to PING with the message sent, this line takes care of that.

PHP Code:

if(strpos(self::$szBuffer'353'))
{
    
fclose(self::$pSocket);

    return 
self::$szBuffer;


Reads the user list, and returns the line.

You may need to change the numbers or make a regex that does it, but i dont have the time currently, un-comment the echo in the main loop to see the server responses.

The script outputs :
Code:


array(3) {  [0]=>  string(12) "sketchmedia2"  [1]=>  string(5) "sam__"  [2]=>  string(10) "@GBH|Johno" }



All times are GMT. The time now is 09:14 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0