PHPBuilder - Basic Class for Yahoo! Chat / Messenger Interface



RSS Twitter
Snippets Networking

Basic Class for Yahoo! Chat / Messenger Interface

by: Mathew Eis
|
November 16, 2001

Version: 1.0

Type: Class

Category: Networking

License: GNU General Public License

Description: This is a basic class based peice of code I wrote to send myself an Yahoo! instant message whenever someone would come to my website. Although it is not complete in itself, among other things, it could be used with the new PHP GTK+ to build a basic chat client. For those who understand the packet definitions, and are interested in a bit of a challenge, here is a list of the rest of the packet definitions I have figured out: LOGIN = 0; LOGOUT = 1; ROOMENTER = 2; ROOMLEAVE = 3; INVITE = 4; AWAY = 5; SPEECH = 6; THINK = 7; EMOTE = 8; ADVERTISEMENT = 9; PM = 10; PING = 11; BUDDY = 12; YMAIL = 13; YMESSAGE = 14; GOTO = 15; GRAFITI = 16; { 0x00, 0x00, 0x00, 0x01 }, { 0x00, 0x00, 0x00, 0x02 }, { 0x00, 0x00, 0x00, 0x11 }, { 0x00, 0x00, 0x00, 0x12 }, { 0x00, 0x00, 0x00, 0x17 }, { 0x00, 0x00, 0x00, 0x21 }, { 0x00, 0x00, 0x00, 0x41 }, { 0x00, 0x00, 0x00, 0x42 }, { 0x00, 0x00, 0x00, 0x43 }, { 0x00, 0x00, 0x00, 0x44 }, { 0x00, 0x00, 0x00, 0x45 }, { 0x00, 0x00, 0x00, 0x62 }, { 0x00, 0x00, 0x00, 0x68 }, { 0x00, 0x00, 0x00, 0x69 }, { 0x00, 0x00, 0x00, 0x70 }, { 0x00, 0x00, 0x00, 0x71 }, { 0x00, 0x00, 0x00, 0x80 }



<?php

/************ Example ************\

This example will send an instant message

$connection = new YahooConnection("yshoo_id","password");

$pm = new YahooPacket(3);

$pm->addContent("Yahoo_id_from");
$pm->addContent($pm->del1);
$pm->addContent("yahoo_id_to_send_to");
$pm->addContent($pm->del1);
$pm->addContent("This is a test!!");

$connection->openConnection("cs7.chat.yahoo.com",8002);
$connection->sendLoginPacket();
$connection->sendPacket($pm);
$connection->closeConnection();

\************ Example ************/

/********************** Class Yahoo Connection *************************/

class YahooConnection
{
	var	$yahoosocket;
	var	$username;
	var	$password;
	var	$cookie;

	function YahooConnection($username,$password)
	{
		$this->username = $username;
		$this->password = $password;
		$this->cookie = $this->getCookie($username,$password);
	}

	function getCookie($username,$password)
	{
		$urltext = "/config/ncclogin?.src=bl&login=".$username."&passwd=".$password."&n=1";

		$fp = fsockopen ("edit.my.yahoo.com",80,$errno,$errstr,30);

		$data = "";

		if(!$fp)
		{
		return "Error: ".$errno." ".$errstr;
		}
		else
		{
			fputs($fp, "GET ".$urltext." HTTP/1.0\r\n\r\n");
			while (!feof($fp))
			{
			$data.=fgets($fp,128);
			}
			fclose($fp);
		}
	
		$response = split("\n",$data);

		while(list($num,$line) = each($response))
		{
			if(strpos($line,"Set-Cookie: ") !== false)
			{
				$cookie = substr($line,12,-1);
				if(substr($cookie,0,2) == "Y=")
				{
				$cookie = substr($cookie,2,strpos($cookie,";"));
				return $cookie;
				}
			}
		}
		return "";
	}

	function openConnection($host,$port)
	{
		$this->yahoosocket = fsockopen($host,$port,$errno,$errstr,30);
	}

	function closeConnection()
	{
		fclose($this->yahoosocket);
	}

	function sendLoginPacket()
	{
		$yp=new YahooPacket(1);

		$yp->content = $this->username;
		$yp->addContent($yp->del1);
		$yp->addContent($this->cookie);

		$this->sendPacket($yp);
		$yp = $this->receivePacket();
	}

	function receivePacket()
	{
		$buffer = fread($this->yahoosocket,16);

		$rcvd = new YahooPacket();
		$rcvd->packetType = substr($buffer,8,4);
		$rcvd->flags = substr($buffer,12,2);

		$length = $buffer{15} + ( $buffer{14}*256 );

		$rcvd->content = fread($this->yahoosocket,$length);
	}

	function sendPacket($sendthis)
	{
		$buffer = chr(0x59).chr(0x43).chr(0x48).chr(0x54).chr(0x00).chr(0x00).chr(0x01).chr(0x00);

		$buffer .= $sendthis->packetType;
		$buffer .= $sendthis->flags;
		$buffer .= chr(strlen($sendthis->content) / 256);
		$buffer .= chr(strlen($sendthis->content) % 256);
		$buffer .= $sendthis->content;
		$error = fwrite($this->yahoosocket,$buffer);
	}
}

/********************** Class Yahoo Packet *************************/

class YahooPacket
{
	var	$packetType;
	var	$flags;
	var	$content;

	var	$strings;

	var	$del1;
	var	$del2;

	var	$LOGIN;
	var	$LOGOUT;
	var	$PM;

	var	$types;

	function YahooPacket($type = 0)
	{
		$this->del1 = chr(0x01);
		$this->del2 = chr(0xC0).chr(0x80);

		$this->NEW = 1;
		$this->LOGIN = 1;
		$this->LOGOUT = 2;
		$this->PM = 3;

		$this->types = array();

		$packstart = chr(0x00).chr(0x00).chr(0x00);

		$this->types[0] = $packstart.chr(0x00);
		$this->types[1] = $packstart.chr(0x01);
		$this->types[2] = $packstart.chr(0x02);
		$this->types[3] = $packstart.chr(0x45);

		$this->packetType = $this->types[$type];
		$this->flags = chr(0x00).chr(0x00);
		$this->content = "";
		
		$this->strings = array();
	}

	function addContent($toadd)
	{
		$this->content .= $toadd;
	}


	function parseType()
	{
		for($i=0; $i < sizeof($this->types); $i++)
		{
			if($this->packetType == $this->content)
			{
			return $i;
			}
		}
		return -1;
	}

    	function parseContent()
	{
		$type = $this->parseType();
	}
}

?>

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Mathew Eis

Comment:



Comment:

(Maximum characters: 1200). You have characters left.