Page 3 of 15

Re: Enviando mensajes de WhatsApp

PostPosted: Tue Aug 14, 2012 3:02 pm
by benjiben143
hola antonio, buenos dias

ya probe el ejemplo y funciono muy bien

en ete caso estamos hablando del cliente ,,,,, si queremos usar como servidor cuales serian las instruciones de inicio

en harbour MANEJO estas funciones y me abre elpuerto como servidor y yo puede manipular todas las operaciones

hb_InetInit()

Socket := hb_InetServer( val( cPort ) )

ya con esta dos funciones ya puedo recibir la informacion de los clientes

y con hbsocket cuales serias las funciones para iniciar como servidor ,, revise el fuente del c y no le hayo todavia



saludos y gracias

Re: Enviando mensajes de WhatsApp

PostPosted: Tue Aug 14, 2012 4:36 pm
by José Vicente Beltrán
Gracias Antonio,
ya estoy compilando con el nuevo xHarbour, después de cambiar 'DESTRUCTOR Destroy()' por 'METHOD Destroy()' solo faltan tres funciones:

Error: Unresolved external '_HB_FUN_HB_NUMTOHEX' referenced from D:\TRABAJO32\COMUN\WHATSAPP\OBJ\WHATSAPP.OBJ
Error: Unresolved external '_HB_FUN_HB_STRFORMAT' referenced from D:\TRABAJO32\COMUN\WHATSAPP\OBJ\WHATSAPP.OBJ
Error: Unresolved external '_HB_FUN_HB_BASE64DECODE' referenced from D:\TRABAJO32\COMUN\WHATSAPP\OBJ\WHATSAPP.OBJ

¿Alguna/s librería/s faltante/s?
Saludos :shock:

Re: Enviando mensajes de WhatsApp

PostPosted: Tue Aug 14, 2012 7:30 pm
by Antonio Linares
José.

Enlaza la libreria tip.lib de xHarbour para HB_BASE64DECODE()

Re: Enviando mensajes de WhatsApp

PostPosted: Tue Aug 14, 2012 8:26 pm
by Antonio Linares
Nueva versión:

Code: Select all  Expand view
#include "FiveWin.ch"
#include "hbsocket.ch"

function Main()

   local oWA := HB_WhatsApp():New()

   ? oWA:Connect()
   oWA:Login()

return nil

/*
cmResponse = Chr( 0x01 )+Chr( 0x31 )+Chr( 0xF8 )+Chr( 0x04 )+Chr( 0x86 )+Chr( 0xBD )+;
Chr( 0xA7 )+Chr( 0xFD )+Chr( 0 )+Chr( 0x01 )+Chr( 0x28 )   


// this packet contains the name

cNext = Chr( 0 )+Chr( 0x12 )+Chr( 0xf8 )+Chr( 0x05 )+Chr( 0x74 )+Chr( 0xa2 )+Chr( 0xa3 )+Chr( 0x61 )+;
Chr( 0xFC )+Chr( 0x0A )+Chr( 0x41 )+Chr( 0x68 )+Chr( 0x6D )+Chr( 0x65 )+Chr( 0x64 )+Chr( 0x20 )+;
Chr( 0x4D )+Chr( 0x6F )+Chr( 0x68 )+Chr( 0x64 )+Chr( 0 )+Chr( 0x15 )+Chr( 0xF8 )+Chr( 0x06 )+;
Chr( 0x48 )+Chr( 0x43 )+Chr( 0x05 )+Chr( 0xA2 )+Chr( 0x3A )+Chr( 0xF8 )+Chr( 0x01 )+Chr( 0xF8 )+;
Chr( 0x04 )+Chr( 0x7B )+Chr( 0xBD )+Chr( 0x4D )+Chr( 0xF8 )+Chr( 0x01 )+Chr( 0xF8 )+Chr( 0x03 )+;
Chr( 0x55 )+Chr( 0x61 )+Chr( 0x24 )+Chr( 0 )+Chr( 0x12 )+Chr( 0xF8 )+Chr( 0x08 )+Chr( 0x48 )+Chr( 0x43 )+;
Chr( 0xFC )+Chr( 0x01 )+Chr( 0x32 )+Chr( 0xA2 )+Chr( 0x3A )+Chr( 0xA0 )+Chr( 0x8A )+Chr( 0xF8 )+Chr( 0x01 )+;
Chr( 0xF8 )+Chr( 0x03 )+Chr( 0x1F )+Chr( 0xBD )+Chr( 0xB1 )
*/


CLASS HB_WhatsApp

   DATA  cNumber, cPassword, cNickname
   DATA  pSocket
   DATA  cServer INIT "s.whatsapp.net"
   DATA  cHost   INIT "bin-short.whatsapp.net"
   DATA  nPort   INIT 5222
   DATA  cIP
   DATA  aResArray
   DATA  cMsg
   DATA  _Incomplete_message

   METHOD New( cNumber, cPassword, cNickname )
   METHOD Connect()
   METHOD Login()
   
   METHOD Read()
   METHOD Send( cData )

   METHOD _Identify( cStr )
   METHOD _Is_Full_Msg( cStr )

   DESTRUCTOR Destroy()

ENDCLASS

METHOD New( cNumber, cPassword, cNickname ) CLASS HB_WhatsApp

   ::cIP = hb_socketGetHosts( ::cHost )[ 1 ]
   ::pSocket = hb_socketOpen()

   ::cNumber   = cNumber
   ::cPassword = cPassword
   ::cNickname = cNickname

return self

METHOD Connect() CLASS HB_WhatsApp

return hb_socketConnect( ::pSocket, { HB_SOCKET_AF_INET, ::cIP, ::nPort } )

static function StrToHex( cStr )

   local n, cHex := ""
   
   for n = 1 to Len( cStr )
      cHex += "0x" + hb_NumToHex( Asc( SubStr( cStr, n, 1 ) ) )
      if n < Len( cStr )
         cHex += ", "
      endif  
   next
   
return cHex        

static function random_uuid()

return hb_strformat( "%04x%04x-%04x-%04x-%04x-%04x%04x%04x",;
                     hb_Random( 0, 0xffff ), hb_Random( 0, 0xffff ),;
                     hb_Random( 0, 0xffff ),;
                     hb_BitOr( hb_Random( 0, 0x0fff ), 0x4000 ),;
                     hb_BitOr( hb_Random( 0, 0x3fff ), 0x8000 ),;
                     hb_Random( 0, 0xffff ), hb_Random( 0, 0xffff ), hb_Random( 0, 0xffff ) )
             
METHOD Login() CLASS HB_WhatsApp

   local cBuffer, cResponse, aArrResponse, hAuthData, cValue, aResData

   ? ::Send( "WA" + Chr( 0x01 ) + Chr( 0 ) + Chr( 0 ) + ;
             Chr( 0x19 ) + Chr( 0xF8 ) + Chr( 0x05 ) + Chr( 0x01 ) + ;
             Chr( 0xA0 ) + Chr( 0x8A ) + Chr( 0x84 ) + Chr( 0xFC ) + ;
             Chr( 0x11 ) + "iPhone-2.6.9-5222" + ;
             Chr( 0 ) + Chr( 0x08 ) + Chr( 0xF8 ) + Chr( 0x02 ) + ;
             Chr( 0x96 ) + Chr( 0xF8 ) + Chr( 0x01 ) + Chr( 0xF8 ) + ;
             Chr( 0x01 ) + Chr( 0x7E ) + Chr( 0 ) + Chr( 0x07 ) + Chr( 0xF8 ) + ;
             Chr( 0x05 ) + Chr( 0x0F ) + Chr( 0x5A ) + Chr( 0x2A ) + ;
             Chr( 0xBD ) + Chr( 0xA7 ) )

   cBuffer = ::Read()
   cResponse = hb_base64decode( SubStr( cBuffer, 27 ) )
   aArrResponse = HB_ATokens( cResponse, "," )
   hAuthData = {=>}
   
   for each cValue in aArrResponse
      aResData = hb_ATokens( cValue, "=" )
      hAuthData[ aResData[ 1 ] ] = StrTran( aResData[ 2 ], '"', "" )
   next    
   
   ? hAuthData[ "nonce" ]
   ? cResponse
   // ? random_uuid()

return nil

METHOD Read() CLASS HB_WhatsApp

   local cBuffer := Space( 1024 ), cV, cRcvdType
   local nLen := hb_socketRecv( ::pSocket, @cBuffer )

   cBuffer = SubStr( cBuffer, 1, nLen )
   ::aResArray = HB_ATokens( cBuffer, Chr( 0 ) )
   // ? StrToHex( cBuffer )
   
   for each cV in ::aResArray
      cRcvdType = ::_Identify( cV )

      // ? cRcvdType
      // ? StrToHex( cV )

      do case
         case cRcvdType == "incomplete_msg"
              ::_incomplete_message = cV

         case cRcvdType == "msg"
              ::cMsg = ::parse_received_message( cV )
         
         case cRcvdType == "account_info"
              ::accinfo = ::parse_account_info( cV )

         case cRcvdType == "last_seen"
              ::lastseen = ::parse_last_seen( cV )
      endcase
   next

return cBuffer

METHOD Send( cData ) CLASS HB_WhatsApp

return hb_socketSend( ::pSocket, cData )

static function StartsWith( cStr, cCompare, nPos )

return SubStr( cStr, nPos, Len( cCompare ) ) == cCompare

static function EndsWith( cStr, cCompare )

return Right( cStr, Len( cCompare ) ) == cCompare

METHOD _Identify( cStr ) CLASS HB_WhatsApp

   local cMsg_identifier := Chr( 0x5D ) + Chr( 0x38 ) + Chr( 0xFA ) + Chr( 0xFC )
   local cServer_delivery_identifier := Chr( 0x8C ) 
   local cClient_delivery_identifier := Chr( 0x7F ) + Chr( 0xBD ) + Chr( 0xAD )
   local cAcc_info_iden := Chr( 0x99 ) + Chr( 0xBD ) + Chr( 0xA7 ) + Chr( 0x94 )   
   local cLast_seen_ident := Chr( 0x48 ) + Chr( 0x38 ) + Chr( 0xFA ) + Chr( 0xFC )
   local cLast_seen_ident2 := Chr( 0x7B ) + Chr( 0xBD ) + Chr( 0x4C ) + Chr( 0x8B )

   if ! ::_is_full_msg( cStr )
      return "incomplete_msg"

   elseif StartsWith( cStr, cMsg_identifier, 3 )

      if EndsWith( cStr, cServer_delivery_identifier )
         return "server_delivery_report"

      elseif EndsWith( cStr, cClient_delivery_identifier )
         return "client_delivery_report"

      else
         return "msg"

      endif

   elseif StartsWith( cStr, cAcc_info_iden, 3 )
      return "account_info"

   elseif StartsWith( cStr, cLast_seen_ident, 3 ) .and. cLast_seen_ident2 $ cStr
      return "last_seen"
   
   else
      return "other"

   endif

return nil

METHOD _Is_Full_Msg( cStr ) CLASS HB_WhatsApp

return Len( cStr ) == Asc( Left( cStr, 1 ) ) + 1

METHOD Destroy() CLASS HB_WhatsApp

   HB_SocketShutDown( ::pSocket )
   HB_SocketClose( ::pSocket )

   ::pSocket = nil

return nil

Re: Enviando mensajes de WhatsApp

PostPosted: Tue Aug 14, 2012 9:32 pm
by Antonio Linares
Nueva versión:

Code: Select all  Expand view
#include "FiveWin.ch"
#include "hbsocket.ch"

function Main()

   local oWA := HB_WhatsApp():New()

   ? oWA:Connect()
   oWA:Login()

return nil

/*
cmResponse = Chr( 0x01 )+Chr( 0x31 )+Chr( 0xF8 )+Chr( 0x04 )+Chr( 0x86 )+Chr( 0xBD )+;
Chr( 0xA7 )+Chr( 0xFD )+Chr( 0 )+Chr( 0x01 )+Chr( 0x28 )   


// this packet contains the name

cNext = Chr( 0 )+Chr( 0x12 )+Chr( 0xf8 )+Chr( 0x05 )+Chr( 0x74 )+Chr( 0xa2 )+Chr( 0xa3 )+Chr( 0x61 )+;
Chr( 0xFC )+Chr( 0x0A )+Chr( 0x41 )+Chr( 0x68 )+Chr( 0x6D )+Chr( 0x65 )+Chr( 0x64 )+Chr( 0x20 )+;
Chr( 0x4D )+Chr( 0x6F )+Chr( 0x68 )+Chr( 0x64 )+Chr( 0 )+Chr( 0x15 )+Chr( 0xF8 )+Chr( 0x06 )+;
Chr( 0x48 )+Chr( 0x43 )+Chr( 0x05 )+Chr( 0xA2 )+Chr( 0x3A )+Chr( 0xF8 )+Chr( 0x01 )+Chr( 0xF8 )+;
Chr( 0x04 )+Chr( 0x7B )+Chr( 0xBD )+Chr( 0x4D )+Chr( 0xF8 )+Chr( 0x01 )+Chr( 0xF8 )+Chr( 0x03 )+;
Chr( 0x55 )+Chr( 0x61 )+Chr( 0x24 )+Chr( 0 )+Chr( 0x12 )+Chr( 0xF8 )+Chr( 0x08 )+Chr( 0x48 )+Chr( 0x43 )+;
Chr( 0xFC )+Chr( 0x01 )+Chr( 0x32 )+Chr( 0xA2 )+Chr( 0x3A )+Chr( 0xA0 )+Chr( 0x8A )+Chr( 0xF8 )+Chr( 0x01 )+;
Chr( 0xF8 )+Chr( 0x03 )+Chr( 0x1F )+Chr( 0xBD )+Chr( 0xB1 )
*/


CLASS HB_WhatsApp

   DATA  cNumber, cPassword, cNickname
   DATA  pSocket
   DATA  cServer INIT "s.whatsapp.net"
   DATA  cHost   INIT "bin-short.whatsapp.net"
   DATA  cRealm  INIT "s.whatsapp.net"
   DATA  nPort   INIT 5222
   DATA  cIP
   DATA  cQop    INIT "auth"
   DATA  cDigest_Uri INIT "xmpp/s.whatsapp.net"
   DATA  aResArray
   DATA  cMsg
   DATA  _Incomplete_message

   METHOD New( cNumber, cPassword, cNickname )
   METHOD Connect()
   METHOD Login()
   
   METHOD Read()
   METHOD Send( cData )

   METHOD _Authenticate( cNonce, cNC )
   METHOD _Identify( cStr )
   METHOD _Is_Full_Msg( cStr )

   DESTRUCTOR Destroy()

ENDCLASS

METHOD New( cNumber, cPassword, cNickname ) CLASS HB_WhatsApp

   ::cIP = hb_socketGetHosts( ::cHost )[ 1 ]
   ::pSocket = hb_socketOpen()

   ::cNumber   = cNumber
   ::cPassword = cPassword
   ::cNickname = cNickname

return self

METHOD Connect() CLASS HB_WhatsApp

return hb_socketConnect( ::pSocket, { HB_SOCKET_AF_INET, ::cIP, ::nPort } )

static function StrToHex( cStr )

   local n, cHex := ""
   
   for n = 1 to Len( cStr )
      cHex += "0x" + hb_NumToHex( Asc( SubStr( cStr, n, 1 ) ) )
      if n < Len( cStr )
         cHex += ", "
      endif  
   next
   
return cHex        

static function random_uuid()

return hb_strformat( "%04x%04x-%04x-%04x-%04x-%04x%04x%04x",;
                     hb_Random( 0, 0xffff ), hb_Random( 0, 0xffff ),;
                     hb_Random( 0, 0xffff ),;
                     hb_BitOr( hb_Random( 0, 0x0fff ), 0x4000 ),;
                     hb_BitOr( hb_Random( 0, 0x3fff ), 0x8000 ),;
                     hb_Random( 0, 0xffff ), hb_Random( 0, 0xffff ), hb_Random( 0, 0xffff ) )
             
METHOD Login() CLASS HB_WhatsApp

   local cBuffer, cResponse, aArrResponse, hAuthData, cValue, aResData, cResData

   ? ::Send( "WA" + Chr( 0x01 ) + Chr( 0 ) + Chr( 0 ) + ;
             Chr( 0x19 ) + Chr( 0xF8 ) + Chr( 0x05 ) + Chr( 0x01 ) + ;
             Chr( 0xA0 ) + Chr( 0x8A ) + Chr( 0x84 ) + Chr( 0xFC ) + ;
             Chr( 0x11 ) + "iPhone-2.6.9-5222" + ;
             Chr( 0 ) + Chr( 0x08 ) + Chr( 0xF8 ) + Chr( 0x02 ) + ;
             Chr( 0x96 ) + Chr( 0xF8 ) + Chr( 0x01 ) + Chr( 0xF8 ) + ;
             Chr( 0x01 ) + Chr( 0x7E ) + Chr( 0 ) + Chr( 0x07 ) + Chr( 0xF8 ) + ;
             Chr( 0x05 ) + Chr( 0x0F ) + Chr( 0x5A ) + Chr( 0x2A ) + ;
             Chr( 0xBD ) + Chr( 0xA7 ) )

   cBuffer = ::Read()
   cResponse = hb_base64decode( SubStr( cBuffer, 27 ) )
   aArrResponse = HB_ATokens( cResponse, "," )
   hAuthData = {=>}
   
   for each cValue in aArrResponse
      aResData = hb_ATokens( cValue, "=" )
      hAuthData[ aResData[ 1 ] ] = StrTran( aResData[ 2 ], '"', "" )
   next    
   
   cResData = ::_Authenticate( hAuthData[ "nonce" ] )
   ? cResData
   cResponse = Chr( 0x01 ) + Chr( 0x31 ) + Chr( 0xF8 ) + Chr( 0x04 ) + Chr( 0x86 ) + ;
               Chr( 0xBD ) + Chr( 0xA7 ) + Chr( 0xFD ) + Chr( 0 ) + Chr( 1 ) + Chr( 0x28 ) + ;
               hb_base64encode( cResData )
   ::Send( cResponse )
   cBuffer = ::Read()
   ::Read()
   
   // ? hAuthData[ "nonce" ]
   // ? cResponse
   // ? random_uuid()

return nil

METHOD _Authenticate( cNonce, cNC ) CLASS HB_WhatsApp

   local cCNonce := random_uuid()
   local cA1 := hb_StrFormat( "%s:%s:%s", ::cNumber, ::cServer, ::cPassword )
   local cA2, cPassword

   if cNC == nil
      cNC = "00000001"
   endif
   
   cA1 = pack_h32( hb_md5( cA1 ) ) + ":" + cNonce + ":" + cCNonce  
   cA2 = "AUTHENTICATE:" + ::cDigest_Uri
   cPassword = hb_md5( cA1 ) + ":" + cNonce + ":" + cNC + ":" + cCNonce + ":" + ::cQop + ;
               ":" + hb_md5( cA2 )
   cPassword = hb_md5( cPassword )            
   
return hb_StrFormat( 'username="%s",realm="%s",nonce="%s",cnonce="%s",nc=%s,qop=%s,digest-uri="%s",response=%s,charset=utf-8',;
                     ::cNumber, ::cRealm, cNonce, cCnonce, cNC, ::cQop, ::cDigest_Uri, ::cPassword )  
       
METHOD Read() CLASS HB_WhatsApp

   local cBuffer := Space( 1024 ), cV, cRcvdType
   local nLen := hb_socketRecv( ::pSocket, @cBuffer )

   cBuffer = SubStr( cBuffer, 1, nLen )
   ::aResArray = HB_ATokens( cBuffer, Chr( 0 ) )
   // ? StrToHex( cBuffer )
   
   for each cV in ::aResArray
      cRcvdType = ::_Identify( cV )

      // ? cRcvdType
      // ? StrToHex( cV )

      do case
         case cRcvdType == "incomplete_msg"
              ::_incomplete_message = cV

         case cRcvdType == "msg"
              ::cMsg = ::parse_received_message( cV )
         
         case cRcvdType == "account_info"
              ::accinfo = ::parse_account_info( cV )

         case cRcvdType == "last_seen"
              ::lastseen = ::parse_last_seen( cV )
      endcase
   next

return cBuffer

METHOD Send( cData ) CLASS HB_WhatsApp

return hb_socketSend( ::pSocket, cData )

static function StartsWith( cStr, cCompare, nPos )

return SubStr( cStr, nPos, Len( cCompare ) ) == cCompare

static function EndsWith( cStr, cCompare )

return Right( cStr, Len( cCompare ) ) == cCompare

METHOD _Identify( cStr ) CLASS HB_WhatsApp

   local cMsg_identifier := Chr( 0x5D ) + Chr( 0x38 ) + Chr( 0xFA ) + Chr( 0xFC )
   local cServer_delivery_identifier := Chr( 0x8C ) 
   local cClient_delivery_identifier := Chr( 0x7F ) + Chr( 0xBD ) + Chr( 0xAD )
   local cAcc_info_iden := Chr( 0x99 ) + Chr( 0xBD ) + Chr( 0xA7 ) + Chr( 0x94 )   
   local cLast_seen_ident := Chr( 0x48 ) + Chr( 0x38 ) + Chr( 0xFA ) + Chr( 0xFC )
   local cLast_seen_ident2 := Chr( 0x7B ) + Chr( 0xBD ) + Chr( 0x4C ) + Chr( 0x8B )

   if ! ::_is_full_msg( cStr )
      return "incomplete_msg"

   elseif StartsWith( cStr, cMsg_identifier, 3 )

      if EndsWith( cStr, cServer_delivery_identifier )
         return "server_delivery_report"

      elseif EndsWith( cStr, cClient_delivery_identifier )
         return "client_delivery_report"

      else
         return "msg"

      endif

   elseif StartsWith( cStr, cAcc_info_iden, 3 )
      return "account_info"

   elseif StartsWith( cStr, cLast_seen_ident, 3 ) .and. cLast_seen_ident2 $ cStr
      return "last_seen"
   
   else
      return "other"

   endif

return nil

METHOD _Is_Full_Msg( cStr ) CLASS HB_WhatsApp

return Len( cStr ) == Asc( Left( cStr, 1 ) ) + 1

METHOD Destroy() CLASS HB_WhatsApp

   HB_SocketShutDown( ::pSocket )
   HB_SocketClose( ::pSocket )

   ::pSocket = nil

return nil

static function pack_h32( cString )
   
   local c := "", cLeter
   local nibbleshift := 4
   local n
   local nPos   := 0
   local aOut   := {}

   for each cLeter in cString
      n = asc( cLeter )
      if n >= asc( "0" ) .and. n <= asc( "9" )
         n -= asc( "0" )
      elseif n >= asc( "a" ) .and. n <= asc( "f" )
         n -= ( asc( "a" ) - 10 )
      elseif n >= asc( "F" ) .and. n <= asc( "F" )
         n -= ( asc( "A" ) - 10 )
      endif
     
      if cLeter:__enumindex() % 2 != 0
         AAdd( aOut, 0 )        
         nPos++
      endif
     
        aOut[ nPos ] = hb_BitOr( aOut[ nPos ], hb_BitShift( n, nibbleshift ) )
     
      nibbleshift = hb_BitAnd( ( nibbleshift + 4 ), 7 )

      if cLeter:__enumindex() % 2 == 0
         c += Chr( aOut[ nPos ] )
      endif      
     
   next

return c

Re: Enviando mensajes de WhatsApp

PostPosted: Tue Aug 14, 2012 11:38 pm
by FiveWiDi
Antonio Linares wrote:Nueva versión:

Code: Select all  Expand view
#include "FiveWin.ch"
#include "hbsocket.ch"

function Main()

   local oWA := HB_WhatsApp():New()

   ? oWA:Connect()
   oWA:Login()

return nil

/*
cmResponse = Chr( 0x01 )+Chr( 0x31 )+Chr( 0xF8 )+Chr( 0x04 )+Chr( 0x86 )+Chr( 0xBD )+;
Chr( 0xA7 )+Chr( 0xFD )+Chr( 0 )+Chr( 0x01 )+Chr( 0x28 )   


// this packet contains the name

cNext = Chr( 0 )+Chr( 0x12 )+Chr( 0xf8 )+Chr( 0x05 )+Chr( 0x74 )+Chr( 0xa2 )+Chr( 0xa3 )+Chr( 0x61 )+;
Chr( 0xFC )+Chr( 0x0A )+Chr( 0x41 )+Chr( 0x68 )+Chr( 0x6D )+Chr( 0x65 )+Chr( 0x64 )+Chr( 0x20 )+;
Chr( 0x4D )+Chr( 0x6F )+Chr( 0x68 )+Chr( 0x64 )+Chr( 0 )+Chr( 0x15 )+Chr( 0xF8 )+Chr( 0x06 )+;
Chr( 0x48 )+Chr( 0x43 )+Chr( 0x05 )+Chr( 0xA2 )+Chr( 0x3A )+Chr( 0xF8 )+Chr( 0x01 )+Chr( 0xF8 )+;
Chr( 0x04 )+Chr( 0x7B )+Chr( 0xBD )+Chr( 0x4D )+Chr( 0xF8 )+Chr( 0x01 )+Chr( 0xF8 )+Chr( 0x03 )+;
Chr( 0x55 )+Chr( 0x61 )+Chr( 0x24 )+Chr( 0 )+Chr( 0x12 )+Chr( 0xF8 )+Chr( 0x08 )+Chr( 0x48 )+Chr( 0x43 )+;
Chr( 0xFC )+Chr( 0x01 )+Chr( 0x32 )+Chr( 0xA2 )+Chr( 0x3A )+Chr( 0xA0 )+Chr( 0x8A )+Chr( 0xF8 )+Chr( 0x01 )+;
Chr( 0xF8 )+Chr( 0x03 )+Chr( 0x1F )+Chr( 0xBD )+Chr( 0xB1 )
*/


CLASS HB_WhatsApp

   DATA  cNumber, cPassword, cNickname
   DATA  pSocket
   DATA  cServer INIT "s.whatsapp.net"
   DATA  cHost   INIT "bin-short.whatsapp.net"
   DATA  cRealm  INIT "s.whatsapp.net"
   DATA  nPort   INIT 5222
   DATA  cIP
   DATA  cQop    INIT "auth"
   DATA  cDigest_Uri INIT "xmpp/s.whatsapp.net"
   DATA  aResArray
   DATA  cMsg
   DATA  _Incomplete_message

   METHOD New( cNumber, cPassword, cNickname )
   METHOD Connect()
   METHOD Login()
   
   METHOD Read()
   METHOD Send( cData )

   METHOD _Authenticate( cNonce, cNC )
   METHOD _Identify( cStr )
   METHOD _Is_Full_Msg( cStr )

   DESTRUCTOR Destroy()

ENDCLASS

METHOD New( cNumber, cPassword, cNickname ) CLASS HB_WhatsApp

   ::cIP = hb_socketGetHosts( ::cHost )[ 1 ]
   ::pSocket = hb_socketOpen()

   ::cNumber   = cNumber
   ::cPassword = cPassword
   ::cNickname = cNickname

return self

METHOD Connect() CLASS HB_WhatsApp

return hb_socketConnect( ::pSocket, { HB_SOCKET_AF_INET, ::cIP, ::nPort } )

static function StrToHex( cStr )

   local n, cHex := ""
   
   for n = 1 to Len( cStr )
      cHex += "0x" + hb_NumToHex( Asc( SubStr( cStr, n, 1 ) ) )
      if n < Len( cStr )
         cHex += ", "
      endif  
   next
   
return cHex        

static function random_uuid()

return hb_strformat( "%04x%04x-%04x-%04x-%04x-%04x%04x%04x",;
                     hb_Random( 0, 0xffff ), hb_Random( 0, 0xffff ),;
                     hb_Random( 0, 0xffff ),;
                     hb_BitOr( hb_Random( 0, 0x0fff ), 0x4000 ),;
                     hb_BitOr( hb_Random( 0, 0x3fff ), 0x8000 ),;
                     hb_Random( 0, 0xffff ), hb_Random( 0, 0xffff ), hb_Random( 0, 0xffff ) )
             
METHOD Login() CLASS HB_WhatsApp

   local cBuffer, cResponse, aArrResponse, hAuthData, cValue, aResData, cResData

   ? ::Send( "WA" + Chr( 0x01 ) + Chr( 0 ) + Chr( 0 ) + ;
             Chr( 0x19 ) + Chr( 0xF8 ) + Chr( 0x05 ) + Chr( 0x01 ) + ;
             Chr( 0xA0 ) + Chr( 0x8A ) + Chr( 0x84 ) + Chr( 0xFC ) + ;
             Chr( 0x11 ) + "iPhone-2.6.9-5222" + ;
             Chr( 0 ) + Chr( 0x08 ) + Chr( 0xF8 ) + Chr( 0x02 ) + ;
             Chr( 0x96 ) + Chr( 0xF8 ) + Chr( 0x01 ) + Chr( 0xF8 ) + ;
             Chr( 0x01 ) + Chr( 0x7E ) + Chr( 0 ) + Chr( 0x07 ) + Chr( 0xF8 ) + ;
             Chr( 0x05 ) + Chr( 0x0F ) + Chr( 0x5A ) + Chr( 0x2A ) + ;
             Chr( 0xBD ) + Chr( 0xA7 ) )

   cBuffer = ::Read()
   cResponse = hb_base64decode( SubStr( cBuffer, 27 ) )
   aArrResponse = HB_ATokens( cResponse, "," )
   hAuthData = {=>}
   
   for each cValue in aArrResponse
      aResData = hb_ATokens( cValue, "=" )
      hAuthData[ aResData[ 1 ] ] = StrTran( aResData[ 2 ], '"', "" )
   next    
   
   cResData = ::_Authenticate( hAuthData[ "nonce" ] )
   ? cResData
   cResponse = Chr( 0x01 ) + Chr( 0x31 ) + Chr( 0xF8 ) + Chr( 0x04 ) + Chr( 0x86 ) + ;
               Chr( 0xBD ) + Chr( 0xA7 ) + Chr( 0xFD ) + Chr( 0 ) + Chr( 1 ) + Chr( 0x28 ) + ;
               hb_base64encode( cResData )
   ::Send( cResponse )
   cBuffer = ::Read()
   ::Read()
   
   // ? hAuthData[ "nonce" ]
   // ? cResponse
   // ? random_uuid()

return nil

METHOD _Authenticate( cNonce, cNC ) CLASS HB_WhatsApp

   local cCNonce := random_uuid()
   local cA1 := hb_StrFormat( "%s:%s:%s", ::cNumber, ::cServer, ::cPassword )
   local cA2, cPassword

   if cNC == nil
      cNC = "00000001"
   endif
   
   cA1 = pack_h32( hb_md5( cA1 ) ) + ":" + cNonce + ":" + cCNonce  
   cA2 = "AUTHENTICATE:" + ::cDigest_Uri
   cPassword = hb_md5( cA1 ) + ":" + cNonce + ":" + cNC + ":" + cCNonce + ":" + ::cQop + ;
               ":" + hb_md5( cA2 )
   cPassword = hb_md5( cPassword )            
   
return hb_StrFormat( 'username="%s",realm="%s",nonce="%s",cnonce="%s",nc=%s,qop=%s,digest-uri="%s",response=%s,charset=utf-8',;
                     ::cNumber, ::cRealm, cNonce, cCnonce, cNC, ::cQop, ::cDigest_Uri, ::cPassword )  
       
METHOD Read() CLASS HB_WhatsApp

   local cBuffer := Space( 1024 ), cV, cRcvdType
   local nLen := hb_socketRecv( ::pSocket, @cBuffer )

   cBuffer = SubStr( cBuffer, 1, nLen )
   ::aResArray = HB_ATokens( cBuffer, Chr( 0 ) )
   // ? StrToHex( cBuffer )
   
   for each cV in ::aResArray
      cRcvdType = ::_Identify( cV )

      // ? cRcvdType
      // ? StrToHex( cV )

      do case
         case cRcvdType == "incomplete_msg"
              ::_incomplete_message = cV

         case cRcvdType == "msg"
              ::cMsg = ::parse_received_message( cV )
         
         case cRcvdType == "account_info"
              ::accinfo = ::parse_account_info( cV )

         case cRcvdType == "last_seen"
              ::lastseen = ::parse_last_seen( cV )
      endcase
   next

return cBuffer

METHOD Send( cData ) CLASS HB_WhatsApp

return hb_socketSend( ::pSocket, cData )

static function StartsWith( cStr, cCompare, nPos )

return SubStr( cStr, nPos, Len( cCompare ) ) == cCompare

static function EndsWith( cStr, cCompare )

return Right( cStr, Len( cCompare ) ) == cCompare

METHOD _Identify( cStr ) CLASS HB_WhatsApp

   local cMsg_identifier := Chr( 0x5D ) + Chr( 0x38 ) + Chr( 0xFA ) + Chr( 0xFC )
   local cServer_delivery_identifier := Chr( 0x8C ) 
   local cClient_delivery_identifier := Chr( 0x7F ) + Chr( 0xBD ) + Chr( 0xAD )
   local cAcc_info_iden := Chr( 0x99 ) + Chr( 0xBD ) + Chr( 0xA7 ) + Chr( 0x94 )   
   local cLast_seen_ident := Chr( 0x48 ) + Chr( 0x38 ) + Chr( 0xFA ) + Chr( 0xFC )
   local cLast_seen_ident2 := Chr( 0x7B ) + Chr( 0xBD ) + Chr( 0x4C ) + Chr( 0x8B )

   if ! ::_is_full_msg( cStr )
      return "incomplete_msg"

   elseif StartsWith( cStr, cMsg_identifier, 3 )

      if EndsWith( cStr, cServer_delivery_identifier )
         return "server_delivery_report"

      elseif EndsWith( cStr, cClient_delivery_identifier )
         return "client_delivery_report"

      else
         return "msg"

      endif

   elseif StartsWith( cStr, cAcc_info_iden, 3 )
      return "account_info"

   elseif StartsWith( cStr, cLast_seen_ident, 3 ) .and. cLast_seen_ident2 $ cStr
      return "last_seen"
   
   else
      return "other"

   endif

return nil

METHOD _Is_Full_Msg( cStr ) CLASS HB_WhatsApp

return Len( cStr ) == Asc( Left( cStr, 1 ) ) + 1

METHOD Destroy() CLASS HB_WhatsApp

   HB_SocketShutDown( ::pSocket )
   HB_SocketClose( ::pSocket )

   ::pSocket = nil

return nil

static function pack_h32( cString )
   
   local c := "", cLeter
   local nibbleshift := 4
   local n
   local nPos   := 0
   local aOut   := {}

   for each cLeter in cString
      n = asc( cLeter )
      if n >= asc( "0" ) .and. n <= asc( "9" )
         n -= asc( "0" )
      elseif n >= asc( "a" ) .and. n <= asc( "f" )
         n -= ( asc( "a" ) - 10 )
      elseif n >= asc( "F" ) .and. n <= asc( "F" )
         n -= ( asc( "A" ) - 10 )
      endif
     
      if cLeter:__enumindex() % 2 != 0
         AAdd( aOut, 0 )        
         nPos++
      endif
     
        aOut[ nPos ] = hb_BitOr( aOut[ nPos ], hb_BitShift( n, nibbleshift ) )
     
      nibbleshift = hb_BitAnd( ( nibbleshift + 4 ), 7 )

      if cLeter:__enumindex() % 2 == 0
         c += Chr( aOut[ nPos ] )
      endif      
     
   next

return c


Probado con W7 y FWH 1203 (y su Harbour) y parece que funciona !!!!!

Tengo un SIM acabado de recibir con tarifa llamadas+internet, dime como puedo probar que funciona para enviar un "Hola que tal?".

Gracias,

Re: Enviando mensajes de WhatsApp

PostPosted: Wed Aug 15, 2012 1:21 am
by Enrrique Vertiz
Saludos Antonio, compilando con el ultimo xHb me dan los mismos errores que a Jose Vicente

Error: Unresolved external '_HB_FUN_HB_NUMTOHEX' referenced from D:\TRABAJO32\COMUN\WHATSAPP\OBJ\WHATSAPP.OBJ
Error: Unresolved external '_HB_FUN_HB_STRFORMAT' referenced from D:\TRABAJO32\COMUN\WHATSAPP\OBJ\WHATSAPP.OBJ
Error: Unresolved external '_HB_FUN_HB_BASE64ENCODE' referenced from D:\TRABAJO32\COMUN\WHATSAPP\OBJ\WHATSAPP.OBJ
Error: Unresolved external '_HB_FUN_HB_BASE64DECODE' referenced from D:\TRABAJO32\COMUN\WHATSAPP\OBJ\WHATSAPP.OBJ

Debo mencionar que aumente la LIB que indicas :

echo %hdirl%\tip.lib + >> b32.bc

Pero tampoco, siguen los 4 mensajes de error arriba adjuntos, gracias

Re: Enviando mensajes de WhatsApp

PostPosted: Wed Aug 15, 2012 8:06 am
by José Vicente Beltrán
Gracias de nuevo por contestar, Antonio.

Pero después de añadir la lib confirmo lo de Enrrique.
:shock:

Re: Enviando mensajes de WhatsApp

PostPosted: Wed Aug 15, 2012 10:02 am
by mastintin
Antonio , uso whatsapi desde javascript .

https://github.com/aesedepece/Guasa

Re: Enviando mensajes de WhatsApp

PostPosted: Wed Aug 15, 2012 10:41 am
by Antonio Linares
Carlos,

Aun falta una parte del codigo para portalo a Harbour. De momento se trata de ir probando y ver que vayamos avanzando correctamente :-)

Re: Enviando mensajes de WhatsApp

PostPosted: Wed Aug 15, 2012 10:42 am
by Antonio Linares
Manuel,

Te funciona bien desde Javascript ?

En caso afirmativo, que datos le proporcionas ? Puedes mostrar unas capturas de pantallas ? gracias :-)

Re: Enviando mensajes de WhatsApp

PostPosted: Wed Aug 15, 2012 11:02 am
by fgondi
Para que compile correctamente en xharbour hace falta:

tip.lib base64. Daniel da un enlace para descargarla http://forums.fivetechsupport.com/viewtopic.php?f=3&t=23329

ws2_32.lib de bcc582

libmisc.lib de xharbour

Además de reemplazar hb_StrFormat por StrFormat y hb_NumToHex por NumToHex

Re: Enviando mensajes de WhatsApp

PostPosted: Wed Aug 15, 2012 11:20 am
by Antonio Linares
Fernando,

Muchas gracias! :-)

Re: Enviando mensajes de WhatsApp

PostPosted: Wed Aug 15, 2012 11:27 am
by mastintin
Antonio Linares wrote:Manuel,

Te funciona bien desde Javascript ?

En caso afirmativo, que datos le proporcionas ? Puedes mostrar unas capturas de pantallas ? gracias :-)


Estoy en ello . He montado el server apache y php . DE momento no me va . Por la tarde siguo con ello .

Re: Enviando mensajes de WhatsApp

PostPosted: Wed Aug 15, 2012 11:29 am
by Antonio Linares
Manuel,

Yo lo he intentado desde el hosting que usamos en USA pero parece que tienen cerrado el puerto 5222.

Asi que más razón aún para que intentemos usarlo desde nuestras propias aplicaciones :-)