Index: trunk/instant_messenger/inc/Jabber.abstract.php
===================================================================
--- trunk/instant_messenger/inc/Jabber.abstract.php	(revision 64)
+++ trunk/instant_messenger/inc/Jabber.abstract.php	(revision 151)
@@ -1,18 +1,18 @@
 <?php
 /*
-+-------------------------------------------------------------------------+
-|                                                                         |
-| @FILE:        Jabber.abstract.php                                       |
-| @ABSTRACT:    Jabber                                                    |
-| @EXTENDS:     Socket (Socket.class.php)                                 |
-|                                                                         |
-| @DATE:        Wed May,16 2007 08:18:59                                  |
-| @LAST_CHANGE: Wed May,16 2007 09:03:28                                  |
-|                                                                         |
-| @AUTHOR:      [NUTS] Rodrigo Souza - rodsouza@celepar.pr.gov.br         |
-|                                                                         |
-| @BRIEF:       Abstract class for connect with any Jabber server         |
-|                                                                         |
-+-------------------------------------------------------------------------+
++---------------------------------------------------------------------------------+
+|                                                                                 |
+| @FILE:        Jabber.abstract.php                                               |
+| @ABSTRACT:    Jabber                                                            |
+| @EXTENDS:     Socket (Socket.class.php)                                         |
+|                                                                                 |
+| @DATE:        Wed May,16 2007 08:18:59                                          |
+| @LAST_CHANGE: Wed May,16 2007 09:03:28                                          |
+|                                                                                 |
+| @AUTHOR:      [NUTS] Rodrigo Souza - rodrigosouzadossantos [at] gmail [dot] com |
+|                                                                                 |
+| @BRIEF:       Abstract class for connect with any Jabber server                 |
+|                                                                                 |
++---------------------------------------------------------------------------------+
 */
 
@@ -21,174 +21,198 @@
 abstract class Jabber extends Socket
 {
-   const J_FILE    = '/var/log/expresso/Jabber.log';
-   const J_ALL     = false;
-   const J_ERROR   = true;
-   const J_WARNING = true;
-   const J_NOTICE  = true;
-
-   const ALL     = 'ALL';
-   const ERROR   = 'ERROR';
-   const WARNING = 'WARNING';
-   const NOTICE  = 'NOTICE';
-
-   private final function _connect( $pUser = false, $pPassword = false )
-   {
-      try
-      {	
-         if ( !preg_match('/^(.+)@(.+)\/(.+):(\d+)$/',$pUser, $matches) )
-            throw new Exception('[connect] #1 ' . $pUser . ' must be [USER]@[DOMAIN]/[RESOURCE]:[PORT] and [PORT] is integer. File: ' . __FILE__ . '  ::  ' . __LINE__);
-
-         list($subject, $USER, $SERVER, $RESOURCE, $PORT) = $matches;
-
-         if ( !($socket = $this->open('tcp://' . $SERVER . ':' . $PORT, 1, 1)) )
-            throw new Exception('[connect] #2 can\'t access tcp://' . $SERVER . ':' . $PORT . '. File: ' . __FILE__ . '  ::  ' . __LINE__);
-
-         $this->_socket = $socket;
-
-         $xml  = "<?xml version='1.0' encoding='UTF-8'?>";
-         $xml .= "<stream:stream to='" . $SERVER . "' xmlns='jabber:client' ";
-         $xml .= "xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>";
-
-         if ( $this->write($xml) === false )
-            throw new Exception('[connect] #3 it isn\'t possible write in the socket ' . $socket . '. File: ' . __FILE__ . '  ::  ' . __LINE__);
-
-         $this->_server = $SERVER;
-         $this->_username = $USER;
-         $this->_password = $pPassword;
-         $this->_resource = $RESOURCE;
-      }
-      catch(Exception $e)
-      {
-         $this->writeLog('ERROR', $e->getMessage());
-         return false;
-      }
-   }
-
-   protected function connect( $pUser = false, $pPassword = false )
-   {   
-      try
-      {
-         $this->_connect($pUser, $pPassword);
+	const J_FILE    = 'Jabber.log';
+	const J_ALL     = false;
+	const J_ERROR   = true;
+	const J_WARNING = true;
+	const J_NOTICE  = true;
+
+	const ALL     = 'ALL';
+	const ERROR   = 'ERROR';
+	const WARNING = 'WARNING';
+	const NOTICE  = 'NOTICE';
+
+	private final function _connect( $pUser = false, $pPassword = false )
+	{
+		try
+		{
+			if ( !preg_match('/^(.+)@(.+)\/(.+):(\d+)$/',$pUser, $matches) )
+				throw new Exception('[connect] #1 ' . $pUser . ' must be [USER]@[DOMAIN]/[RESOURCE]:[PORT] and [PORT] is integer. File: ' . __FILE__ . '  ::  ' . __LINE__);
+
+			list($subject, $USER, $SERVER, $RESOURCE, $PORT) = $matches;
+
+			if ( !($socket = $this->open('tcp://' . $SERVER . ':' . $PORT, 1, 1)) )
+				throw new Exception('[connect] #2 can\'t access tcp://' . $SERVER . ':' . $PORT . '. File: ' . __FILE__ . '  ::  ' . __LINE__);
+
+			$this->_socket = $socket;
+
+			$return = fread($socket, 4096);
+			fwrite($socket, $pUser);
+
+			$return = fread($socket, 4096);
+			fwrite($socket, $pPassword);
+
+			$return = fread($socket, 4096);
+			#echo __FUNCTION__ . "<br>\n";
+			#var_dump($return);
+			#exit;
+
+			if ( $return == "new" )
+			{
+				$xml  = "<?xml version='1.0' encoding='UTF-8'?>";
+				$xml .= "<stream:stream to='" . $SERVER . "' xmlns='jabber:client' ";
+				$xml .= "xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>";
+
+				if ( $this->write($xml) === false )
+					throw new Exception('[connect] #4 it isn\'t possible connect in the server becase exists a client connected with same user. File: ' . __FILE__ . '  ::  ' . __LINE__);
+			}
+
+			$this->_server = $SERVER;
+			$this->_username = $USER;
+			$this->_password = $pPassword;
+			$this->_resource = $RESOURCE;
+
+			#var_dump($return);
+			#exit;
+			return $return;
+		}
+		catch(Exception $e)
+		{
+			$this->writeLog('ERROR', $e->getMessage());
+			return false;
+		}
+	}
+
+	protected function connect( $pUser = false, $pPassword = false )
+	{
+		try
+		{
+			$_connect = $this->_connect($pUser, $pPassword);
 			
-         if ( ($xml = $this->read()) === false )
-            throw new Exception('[connect] #1 it isn\'t possible read the socket. File: ' . __FILE__ . '  ::  ' . __LINE__);
-         
-         if ( preg_match('/(<starttls .*<required\/><\/starttls>)/', $xml, $matches) )
-         {
-            if ( !$this->starttls() )
-               throw new Exception('[connect] #2 it isn\'t possible start tls. File: ' . __FILE__ . '  ::  ' . __LINE__);
-
-            # $this->writeLog('NOTICE', 'Connected TLS');
-         }
-         else
-	 		{
-            if ( !$this->_plain() )
-               throw new Exception('[connect] #3 it isn\'t possible carry out the verification. File: ' . __FILE__ . '  ::  ' . __LINE__);
-	 		}
-
-         return true;
-      }
-      catch(Exception $e)
-      {
-         $this->writeLog('ERROR', $e->getMessage());
-         $this->_disconnect();
-         return false;
-      }
-   }
-
-   protected final function _disconnect()
-   {
-      try
-      {
-         $xml = "</stream:stream>";
-
-         if ( $this->write($xml) === false )
-            throw new Exception('[disconnect] #1 Cannot write to socket (' . $this->_socket . '). File: ' . __FILE__ . '  ::  ' . __LINE__);
-
-         if ( ($xml = $this->read()) === false )
-            throw new Exception('[disconnect] #2 it isn\'t possible read the socket. File: ' . __FILE__ . '  ::  ' . __LINE__);
-      }
-      catch(Exception $e)
-      {
-         $this->writeLog('ERROR', $e->getMessage());
-         return false;
-      }
-   }
-
-   private final function starttls()
-   {
-      try
-      {
-         $xml = "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>";
-         if ( $this->write($xml) === false )
-            throw new Exception('[starttls] #1 Cannot write to socket (' . $this->_socket . '). File: ' . __FILE__ . '  ::  ' . __LINE__);
-
-         if ( ($xml = $this->read()) === false )
-            throw new Exception('[starttls] #2 it isn\'t possible read the socket. File: ' . __FILE__ . '  ::  ' . __LINE__);
-
-         if ( !preg_match('/<proceed.*\/>/', $xml, $matches) )
-            throw new Exception('[starttls] #3 can\'t start tls in the socket ' .$this->_socket . '. File: ' . __FILE__ . '  ::  ' . __LINE__);
-
-		   stream_socket_enable_crypto($this->_socket, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
-
-         $xml  = "<?xml version='1.0' encoding='UTF-8'?>";
-         $xml .= "<stream:stream to='" . $this->_server . "' xmlns='jabber:client' ";
-         $xml .= "xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>";
-         if ( $this->write($xml) === false )
-            throw new Exception('[starttls] #4 Cannot write to socket (' . $this->_socket . '). File: ' . __FILE__ . '  ::  ' . __LINE__);
-
-         usleep(90000);
-         $this->blocking($this->_socket, 0);
-
-         if ( ($xml = $this->read()) === false )
-            throw new Exception('[starttls] #5 it isn\'t possible read the socket. File: ' . __FILE__ . '  ::  ' . __LINE__);
-
-         if ( !$this->_plain() )
-            throw new Exception('[starttls] #6 it isn\'t possible carry out the verification. File: ' . __FILE__ . '  ::  ' . __LINE__);
-
-         return true;
-      }
-      catch(Exception $e)
-      {
-         $this->writeLog('ERROR', $e->getMessage());
-         return false;
-      }
-   }
-
-   private final function _plain()
-   {
-      try
-      {
-         $this->blocking($this->_socket, 0);
-
-         if ( ($xml = $this->read()) === false )
-            throw new Exception('[_plain] #1 it isn\'t possible read the socket. File: ' . __FILE__ . '  ::  ' . __LINE__);
-
-         $xml  = "<username>" . $this->_username . "</username>";
-         $xml .= "<password>" . $this->_password . "</password>";
-         $xml .= "<resource>" . $this->_resource . "</resource>";
-
-         unset($this->_password);
-
-         if ( !$this->iq('set', 'auth_1', NULL, NULL, "jabber:iq:auth", $xml) )
-            throw new Exception('[_plain] #2 it isn\'t possible carry out the verification. File: ' . __FILE__ . '  ::  ' . __LINE__);
-
-         while ( !preg_match("/<iq xmlns='jabber:client' id='auth_1' type='(result|error)'/", ($readSocket = $this->read()), $matches) )
-         	   usleep(10000);
+			if ( !$_connect )
+				throw new Exception('[connect] #0. File: ' . __FILE__ . '  ::  ' . __LINE__);
+
+			if ( $_connect == "new" )
+			{
+				if ( ($xml = $this->read()) === false )
+					throw new Exception('[connect] #1 it isn\'t possible read the socket. File: ' . __FILE__ . '  ::  ' . __LINE__);
+
+				if ( preg_match('/(<starttls .*<required\/><\/starttls>)/', $xml, $matches) )
+				{
+					if ( !$this->starttls() )
+						throw new Exception('[connect] #2 it isn\'t possible start tls. File: ' . __FILE__ . '  ::  ' . __LINE__);
+
+					# $this->writeLog('NOTICE', 'Connected TLS');
+				}
+				else
+				{
+					if ( !$this->_plain() )
+						throw new Exception('[connect] #3 it isn\'t possible carry out the verification. File: ' . __FILE__ . '  ::  ' . __LINE__);
+				}
+			}
+
+			return $_connect;
+		}
+		catch(Exception $e)
+		{
+			$this->writeLog('ERROR', $e->getMessage());
+			$this->_disconnect();
+			return false;
+		}
+	}
+
+	protected final function _disconnect()
+	{
+		try
+		{
+			$xml = "</stream:stream>";
+
+			if ( $this->write($xml) === false )
+				throw new Exception('[disconnect] #1 Cannot write to socket (' . $this->_socket . '). File: ' . __FILE__ . '  ::  ' . __LINE__);
+
+			if ( ($xml = $this->read()) === false )
+				throw new Exception('[disconnect] #2 it isn\'t possible read the socket. File: ' . __FILE__ . '  ::  ' . __LINE__);
+		}
+		catch(Exception $e)
+		{
+			$this->writeLog('ERROR', $e->getMessage());
+			return false;
+		}
+	}
+
+	private final function starttls()
+	{
+		try
+		{
+			$xml = "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>";
+			if ( $this->write($xml) === false )
+				throw new Exception('[starttls] #1 Cannot write to socket (' . $this->_socket . '). File: ' . __FILE__ . '  ::  ' . __LINE__);
+
+			if ( ($xml = $this->read()) === false )
+				throw new Exception('[starttls] #2 it isn\'t possible read the socket. File: ' . __FILE__ . '  ::  ' . __LINE__);
+
+			if ( !preg_match('/<proceed.*\/>/', $xml, $matches) )
+				throw new Exception('[starttls] #3 can\'t start tls in the socket ' .$this->_socket . '. File: ' . __FILE__ . '  ::  ' . __LINE__);
+
+			stream_socket_enable_crypto($this->_socket, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
+
+			$xml  = "<?xml version='1.0' encoding='UTF-8'?>";
+			$xml .= "<stream:stream to='" . $this->_server . "' xmlns='jabber:client' ";
+			$xml .= "xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>";
+			if ( $this->write($xml) === false )
+				throw new Exception('[starttls] #4 Cannot write to socket (' . $this->_socket . '). File: ' . __FILE__ . '  ::  ' . __LINE__);
+
+			usleep(90000);
+			$this->blocking($this->_socket, 0);
+
+			if ( ($xml = $this->read()) === false )
+				throw new Exception('[starttls] #5 it isn\'t possible read the socket. File: ' . __FILE__ . '  ::  ' . __LINE__);
+
+			if ( !$this->_plain() )
+				throw new Exception('[starttls] #6 it isn\'t possible carry out the verification. File: ' . __FILE__ . '  ::  ' . __LINE__);
+
+			return true;
+		}
+		catch(Exception $e)
+		{
+			$this->writeLog('ERROR', $e->getMessage());
+			return false;
+		}
+	}
+
+	private final function _plain()
+	{
+		try
+		{
+			$this->blocking($this->_socket, 0);
+
+			if ( ($xml = $this->read()) === false )
+				throw new Exception('[_plain] #1 it isn\'t possible read the socket. File: ' . __FILE__ . '  ::  ' . __LINE__);
+
+			$xml  = "<username>" . $this->_username . "</username>";
+			$xml .= "<password>" . $this->_password . "</password>";
+			$xml .= "<resource>" . $this->_resource . "</resource>";
+
+			unset($this->_password);
+
+			if ( !$this->iq('set', 'auth_1', NULL, NULL, "jabber:iq:auth", $xml) )
+				throw new Exception('[_plain] #2 it isn\'t possible carry out the verification. File: ' . __FILE__ . '  ::  ' . __LINE__);
+
+			while ( !preg_match("/<iq xmlns='jabber:client' id='auth_1' type='(result|error)'/", ($readSocket = $this->read()), $matches) )
+				usleep(10000);
 
 			if($matches[1] == "error")
 				return false;
 
-         if ( ($xml = $this->read()) === false )
-            throw new Exception('[_plain] #3 it isn\'t possible read the socket. File: ' . __FILE__ . '  ::  ' . __LINE__);
-
-         return true;
-      }
-      catch(Exception $e)
-      {
-         $this->writeLog('ERROR', $e->getMessage());
-         return false;
-      }
-   }
+			if ( ($xml = $this->read()) === false )
+				throw new Exception('[_plain] #3 it isn\'t possible read the socket. File: ' . __FILE__ . '  ::  ' . __LINE__);
+
+			return true;
+		}
+		catch(Exception $e)
+		{
+			$this->writeLog('ERROR', $e->getMessage());
+			return false;
+		}
+	}
 
 	private final function _iq($pType = false, $pId = false, $pTo = false, $pFrom = false, $pXmlns = false, $pLoad = false )
@@ -199,10 +223,10 @@
 
 		if ( $pXmlns == "vcard-temp" )
-      {
+		{
 			$xml .= ">";
 			$xml .= $pLoad;
 		}
-      else
-      {
+		else
+		{
 			$xml .= "><query xmlns='" . $pXmlns . "'";
 			$xml .= ( $pLoad ) ? ">" . $pLoad . "</query>" : "/>";
@@ -210,107 +234,110 @@
 
 		$xml .= "</iq>";
-		
-		return $xml;	
-	}
-
-   protected final function iq($pType = false, $pId = false, $pTo = false, $pFrom = false, $pXmlns = false, $pLoad = false)
-   {
-      try
-      {
-         if ( !preg_match("/^(get|set|result|error)$/i", $pType, $matches) )
-            throw new Exception('[iq] #1 type must be GET, SET, RESULT or ERROR. File: ' . __FILE__ . '  ::  ' . __LINE__);
-
-		   if ( $this->write($this->_iq($pType, $pId, $pTo, $pFrom, $pXmlns, $pLoad)) === false )
-            throw new Exception('[iq] #2 Cannot write to socket (' . $this->_socket . '). File: ' . __FILE__ . '  ::  ' . __LINE__);
-         else
-            return true;
-      }
-      catch(Exception $e)
-      {
-         $this->writeLog('ERROR', $e->getMessage());
-         return false;
-      }
-   }
+
+		return $xml;
+	}
+
+	protected final function iq($pType = false, $pId = false, $pTo = false, $pFrom = false, $pXmlns = false, $pLoad = false)
+	{
+		try
+		{
+			if ( !preg_match("/^(get|set|result|error)$/i", $pType, $matches) )
+				throw new Exception('[iq] #1 type must be GET, SET, RESULT or ERROR. File: ' . __FILE__ . '  ::  ' . __LINE__);
+
+			if ( $this->write($this->_iq($pType, $pId, $pTo, $pFrom, $pXmlns, $pLoad)) === false )
+				throw new Exception('[iq] #2 Cannot write to socket (' . $this->_socket . '). File: ' . __FILE__ . '  ::  ' . __LINE__);
+			else
+				return true;
+		}
+		catch(Exception $e)
+		{
+			$this->writeLog('ERROR', $e->getMessage());
+			return false;
+		}
+	}
 
 	protected final function presence($pType = false, $pTo = false, $pShow = false, $pStatus = false, $pPriority = false)
 	{
-      try
-      {
-         $xml  = "<presence";
-         $xml .= ($pTo)   ? " to='{$pTo}'" : '';
-         $xml .= ($pType) ? " type='{$pType}'" : '';
-         
-         //$xml .= ($pStatus || $pShow || $pPriority) ? ">" : "/>";
-         if ( !($pStatus || $pShow || $pPriority) )
+		try
+		{
+			$xml  = "<presence";
+			$xml .= ($pTo)   ? " to='{$pTo}'" : '';
+			$xml .= ($pType) ? " type='{$pType}'" : '';
+
+			if ( !($pStatus || $pShow || $pPriority) )
 				$xml .= "/>";
 			else
 			{
-			 $xml .= ">";
-	         $xml .= ($pStatus) ? "<status>" . $pStatus . "</status>" : '';
-	         $xml .= ($pShow) ? "<show>" . $pShow . "</show>" : '';
-	         $xml .= ($pPriority) ? "<priority>" . $pPriority . "</priority>" : '';
-	         $xml .= ($pStatus || $pShow || $pPriority) ? "</presence>" : '';
+				$xml .= ">";
+				$xml .= ($pStatus) ? "<status>" . $pStatus . "</status>" : '';
+				$xml .= ($pShow) ? "<show>" . $pShow . "</show>" : '';
+				$xml .= ($pPriority) ? "<priority>" . $pPriority . "</priority>" : '';
+				$xml .= ($pStatus || $pShow || $pPriority) ? "</presence>" : '';
 			}
-
-         if ( $this->write($xml) === false )
-            throw new Exception('[presence] #1 it isn\'t possible send presence for ' . $this->_server . '. File: ' . __FILE__ . '  ::  ' . __LINE__);
-
-         return true;
-      }
-      catch(Exception $e)
-      {
-         $this->writeLog('ERROR', $e->getMessage());
-         return false;
-      }
-	}
-
-   protected final function read()
-   {
-      $return = NULL;
-      do
-      {
-         $line = NULL;
-         $line = utf8_decode(parent::read($this->_socket, 4096));
-
-         if ( $line === false )
-         {
-            $return = false;
-            break;
-         }
-
-         if ( $line != NULL )
-            $return .= $line;// . "\n";
-      }
-      while ( $line != NULL );
-
-      return $return;
-   }
+			
+			if ( $this->write($xml) === false )
+				throw new Exception('[presence] #1 it isn\'t possible send presence for ' . $this->_server . '. File: ' . __FILE__ . '  ::  ' . __LINE__);
+
+			return true;
+		}
+		catch(Exception $e)
+		{
+			$this->writeLog('ERROR', $e->getMessage());
+			return false;
+		}
+	}
+
+	protected final function read()
+	{
+		$return = NULL;
+		do
+		{
+			$line = NULL;
+			$line = utf8_decode(parent::read($this->_socket, 4096));
+
+			if ( $line === false )
+			{
+				$return = false;
+				break;
+			}
+
+			if ( $line != NULL )
+				$return .= $line;// . "\n";
+		}
+		while ( $line != NULL );
+
+		return $return;
+	}
 
 	protected final function write($pData = false)
-   {
-      if ( !$pData )
-         return false;
-
-      return parent::write($this->_socket, utf8_encode($pData));
-   }
-
-   protected final function writeLog($pType = false, $pLog = false)
-   {
-      if ( !defined('self::'.$pType) && !$pLog )
-         return false;
-
-      if ( !(bool)constant('self::J_'.$pType) && !(bool)constant('self::J_ALL') )
-         return false;
-
-      $log  = date('m/d/Y H:i:s');
-      $log .= ' [' . constant('self::'.$pType) . '] :: ';
-      $log .= $pLog . "\n";
-      if ( $fp = fopen (self::J_FILE, "a+") )
-      {
-         fwrite($fp, $log);
-         fclose($fp);
-      }
-   }
+	{
+		if ( !$pData )
+			return false;
+
+		return parent::write($this->_socket, utf8_encode($pData));
+	}
+
+	function __destruct()
+	{
+		$this->close($this->_socket);
+	}
+
+	protected final function writeLog($pType = false, $pLog = false)
+	{
+		if ( !defined('self::'.$pType) && !$pLog )
+			return false;
+
+		if ( !(bool)constant('self::J_'.$pType) && !(bool)constant('self::J_ALL') )
+			return false;
+
+		$log  = date('m/d/Y H:i:s');
+		$log .= ' [' . constant('self::'.$pType) . '] :: ';
+		$log .= $pLog . "\n";
+		if ( $fp = fopen (self::J_FILE, "a+") )
+		{
+			fwrite($fp, $log);
+			fclose($fp);
+		}
+	}
 }
-
 ?>
