Exemple #1
0
  void Adhoc::handleIqID( const IQ& iq, int context )
  {
    if( context != ExecuteAdhocCommand )
      return;

    m_adhocTrackMapMutex.lock();
    AdhocTrackMap::iterator it = m_adhocTrackMap.find( iq.id() );
    bool haveIdHandler = ( it != m_adhocTrackMap.end() );
    m_adhocTrackMapMutex.unlock();
    if( !haveIdHandler || (*it).second.context != context
        || (*it).second.remote != iq.from() )
      return;

    switch( iq.subtype() )
    {
      case IQ::Error:
        (*it).second.ah->handleAdhocError( iq.from(), iq.error(), (*it).second.handlerContext );
        break;
      case IQ::Result:
      {
        const Adhoc::Command* ac = iq.findExtension<Adhoc::Command>( ExtAdhocCommand );
        if( ac )
          (*it).second.ah->handleAdhocExecutionResult( iq.from(), *ac, (*it).second.handlerContext );
        break;
      }
      default:
        break;
    }
    m_adhocTrackMapMutex.lock();
    m_adhocTrackMap.erase( it );
    m_adhocTrackMapMutex.unlock();
  }
  void ClientBase::send( IQ& iq, IqHandler* ih, int ctx )
  {
    const PrivateXML::Query* q = iq.findExtension<PrivateXML::Query>( ExtPrivateXML );
    if( !q )
      return;

    Tag* tag = q->tag();
    switch( m_test )
    {
      case 1:
      {
        if( iq.subtype() == IQ::Set
            && tag && *(tag->findChild( "foo", "xmlns", "test" )) == *t1 )
        {
          IQ re( IQ::Result, iq.from(), iq.id() );
          re.addExtension( new PrivateXML::Query() );
          ih->handleIqID( re, ctx );
        }
        break;
      }
      case 2:
      {
        if( iq.subtype() == IQ::Get
            && tag && tag->hasChild( "foo", "xmlns", "test" ) )
        {
          IQ re( IQ::Result, iq.from(), iq.id() );
          re.addExtension( new PrivateXML::Query( t1->clone() ) );
          ih->handleIqID( re, ctx );
        }
        break;
      }
    }
    delete tag;
  }
Exemple #3
0
void Search::handleIqID( const IQ& iq, int context )
{
    TrackMap::iterator it = m_track.find( iq.id() );
    if( it != m_track.end() )
    {
        switch( iq.subtype() )
        {
        case IQ::Result:
        {
            const Query* q = iq.findExtension<Query>( ExtSearch );
            if( !q )
                return;

            switch( context )
            {
            case FetchSearchFields:
            {
                if( q->form() )
                {
                    (*it).second->handleSearchFields( iq.from(), q->form() );
                }
                else
                {
                    (*it).second->handleSearchFields( iq.from(), q->fields(), q->instructions() );
                }
                break;
            }
            case DoSearch:
            {
                if( q->form() )
                {
                    (*it).second->handleSearchResult( iq.from(), q->form() );
                }
                else
                {
                    (*it).second->handleSearchResult( iq.from(), q->result() );
                }
                break;
            }
            }
            break;
        }
        case IQ::Error:
            (*it).second->handleSearchError( iq.from(), iq.error() );
            break;

        default:
            break;
        }

        m_track.erase( it );
    }

    return;
}
Exemple #4
0
    bool Session::handleIq( const IQ& iq )
    {
      const Jingle* j = iq.findExtension<Jingle>( ExtJingle );
      if( !j || j->sid() != m_sid || !m_handler || !m_parent )
        return false;

      switch( j->action() )
      {
        case SessionAccept:
          m_state = Active;
          m_responder = j->responder();
          break;
        case SessionInitiate:
          m_state = Pending;
          m_initiator = j->initiator();
          if( !m_responder )
            m_responder = m_parent->jid();
          break;
        case SessionTerminate:
          m_state = Ended;
          break;
        default:
          break;
      }

      IQ re( IQ::Result, iq.from(), iq.id() );
      m_parent->send( re );

      m_handler->handleSessionAction( j->action(), this, j );

      return true;
    }
Exemple #5
0
  void LastActivity::handleIqID( const IQ& iq, int /*context*/ )
  {
    if( !m_lastActivityHandler )
      return;

    if( iq.subtype() == IQ::Result )
    {
      const Query* q = iq.findExtension<Query>( ExtLastActivity );
      if( !q || q->seconds() < 0 )
        return;

      m_lastActivityHandler->handleLastActivityResult( iq.from(), q->seconds(), q->status() );
    }
    else if( iq.subtype() == IQ::Error && iq.error() )
      m_lastActivityHandler->handleLastActivityError( iq.from(), iq.error()->error() );
  }
Exemple #6
0
  bool InBandBytestream::handleIq( const IQ& iq ) // data or open request, always 'set'
  {
    const IBB* i = iq.findExtension<IBB>( ExtIBB );
    if( !i || !m_handler || iq.subtype() != IQ::Set )
      return false;

    if( !m_open )
    {
      if( i->type() == IBBOpen )
      {
        returnResult( iq.from(), iq.id() );
        m_open = true;
        m_handler->handleBytestreamOpen( this );
        return true;
      }
      return false;
    }

    if( i->type() == IBBClose )
    {
      returnResult( iq.from(), iq.id() );
      closed();
      return true;
    }

    if( ( m_lastChunkReceived + 1 ) != i->seq() )
    {
      m_open = false;
      returnError( iq.from(), iq.id(), StanzaErrorTypeModify, StanzaErrorItemNotFound );
      return false;
    }

    if( i->data().empty() )
    {
      m_open = false;
      returnError( iq.from(), iq.id(), StanzaErrorTypeModify, StanzaErrorBadRequest );
      return false;
    }

    returnResult( iq.from(), iq.id() );
    m_handler->handleBytestreamData( this, i->data() );
    m_lastChunkReceived++;
    return true;
  }
Exemple #7
0
  bool LastActivity::handleIq( const IQ& iq )
  {
    const Query* q = iq.findExtension<Query>( ExtLastActivity );
    if( !q || iq.subtype() != IQ::Get )
      return false;

    IQ re( IQ::Result, iq.from(), iq.id() );
    re.addExtension( new Query( EmptyString, (long)( time( 0 ) - m_active ) ) );
    m_parent->send( re );

    return true;
  }
Exemple #8
0
  void Disco::handleIqID( const IQ& iq, int context )
  {
    DiscoHandlerMap::iterator it = m_track.find( iq.id() );
    if( it != m_track.end() && (*it).second.dh )
    {
      switch( iq.subtype() )
      {
        case IQ::Result:
          switch( context )
          {
            case GetDiscoInfo:
            {
              const Info* di = iq.findExtension<Info>( ExtDiscoInfo );
              if( di )
                (*it).second.dh->handleDiscoInfo( iq.from(), *di, (*it).second.context );
              break;
            }
            case GetDiscoItems:
            {
              const Items* di = iq.findExtension<Items>( ExtDiscoItems );
              if( di )
                (*it).second.dh->handleDiscoItems( iq.from(), *di, (*it).second.context );
              break;
            }
          }
          break;

        case IQ::Error:
        {
          (*it).second.dh->handleDiscoError( iq.from(), iq.error(), (*it).second.context );
          break;
        }

        default:
          break;
      }

      m_track.erase( it );
    }
  }
Exemple #9
0
void DiscoTest::send( const IQ& iq, IqHandler*, int ctx )
{
  Tag* q = 0;
  if( m_test == 8 )
  {
    IQ re( IQ::Result, iq.from(), iq.id() );
    q = new Tag( "query" );
    q->setXmlns( XMLNS_DISCO_INFO );
    new Tag( q, "feature", "var", XMLNS_DISCO_INFO );
    new Tag( q, "feature", "var", "foofeature" );
    new Tag( q, "feature", "var", "foofeature2" );
    re.addExtension( new Disco::Info( q ) );
    m_disco->handleIqID( re, ctx );
  }
  else if( m_test == 9 )
  {
    IQ re( IQ::Result, iq.from(), iq.id() );
    q = new Tag( "query" );
    q->setXmlns( XMLNS_DISCO_ITEMS );
    q->addAttribute( "node", "foonode" );
    Tag* i = new Tag( q, "item", "jid", "jid1" );
    i->addAttribute( "node", "node1" );
    i->addAttribute( "name", "name1" );
    i = new Tag( q, "item", "jid", "jid2" );
    i->addAttribute( "node", "node2" );
    i->addAttribute( "name", "name2" );
    re.addExtension( new Disco::Items( q ) );
    m_disco->handleIqID( re, ctx );
  }
  else if( m_test == 10 )
  {
    IQ re( IQ::Error, iq.from(), iq.id() );
    re.addExtension( new Error( StanzaErrorTypeCancel, StanzaErrorItemNotFound ) );
    m_disco->handleIqID( re, ctx );
  }

  delete q;
}
Exemple #10
0
  void VCardManager::handleIqID( const IQ& iq, int context )
  {
    TrackMap::iterator it = m_trackMap.find( iq.id() );
    if( it != m_trackMap.end() )
    {
      switch( iq.subtype() )
      {
        case IQ::Result:
        {
          switch( context )
          {
            case VCardHandler::FetchVCard:
            {
              const VCard* v = iq.findExtension<VCard>( ExtVCard );
              (*it).second->handleVCard( iq.from(), v );
              break;
            }
            case VCardHandler::StoreVCard:
              (*it).second->handleVCardResult( VCardHandler::StoreVCard, iq.from() );
              break;
          }
        }
        break;
        case IQ::Error:
        {
          (*it).second->handleVCardResult( (VCardHandler::VCardContext)context,
                                           iq.from(),
                                           iq.error() ? iq.error()->error()
                                                       : StanzaErrorUndefined );
          break;
        }
        default:
          break;
      }

      m_trackMap.erase( it );
    }
  }
bool SOCKS5BytestreamManager::handleIq( const IQ& iq )
{
    const Query* q = iq.findExtension<Query>( ExtS5BQuery );
    if( !q || !m_socks5BytestreamHandler
            || m_trackMap.find( iq.id() ) != m_trackMap.end() )
        return false;

    switch( iq.subtype() )
    {
    case IQ::Set:
    {
        const std::string& sid = q->sid();
// FIXME What is haveStream() good for?
        if( /*haveStream( iq.from() ) ||*/ sid.empty() || q->mode() == S5BUDP )
        {
            rejectSOCKS5Bytestream( iq.from(), iq.id(), StanzaErrorNotAcceptable );
            return true;
        }
        AsyncS5BItem asi;
        asi.sHosts = q->hosts();
        asi.id = iq.id();
        asi.from = iq.from();
        asi.to = iq.to();
        asi.incoming = true;
        m_asyncTrackMap[sid] = asi;
        m_socks5BytestreamHandler->handleIncomingBytestreamRequest( sid, iq.from() );
        break;
    }
    case IQ::Error:
        m_socks5BytestreamHandler->handleBytestreamError( iq, EmptyString );
        break;
    default:
        break;
    }

    return true;
}
Exemple #12
0
  void SIManager::handleIqID( const IQ& iq, int context )
  {
    switch( iq.subtype() )
    {
      case IQ::Result:
        if( context == OfferSI )
        {
          TrackMap::iterator it = m_track.find( iq.id() );
          if( it != m_track.end() )
          {
            const SI* si = iq.findExtension<SI>( ExtSI );
            if( !si /*|| si->profile().empty()*/ )
              return;

//             Tag* si = iq.query();
//             Tag* ptag = 0;
//             Tag* fneg = 0;
//             if( si && si->name() == "si" && si->xmlns() == XMLNS_SI )
//             {
//               ptag = si->findChildWithAttrib( XMLNS, (*it).second.profile );
//               fneg = si->findChild( "feature", XMLNS, XMLNS_FEATURE_NEG );
//             }

            // FIXME: remove above commented code and
            // check corectness of last 3 params!
            (*it).second.sih->handleSIRequestResult( iq.from(), iq.to(), (*it).second.sid, *si );
            m_track.erase( it );
          }
        }
        break;
      case IQ::Error:
        if( context == OfferSI )
        {
          TrackMap::iterator it = m_track.find( iq.id() );
          if( it != m_track.end() )
          {
            (*it).second.sih->handleSIRequestError( iq, (*it).second.sid );
            m_track.erase( it );
          }
        }
        break;
      default:
        break;
    }
  }
Exemple #13
0
  bool SIManager::handleIq( const IQ& iq )
  {
    TrackMap::iterator itt = m_track.find( iq.id() );
    if( itt != m_track.end() )
      return false;

    const SI* si = iq.findExtension<SI>( ExtSI );
    if( !si || si->profile().empty() )
      return false;

    HandlerMap::const_iterator it = m_handlers.find( si->profile() );
    if( it != m_handlers.end() && (*it).second )
    {
      (*it).second->handleSIRequest( iq.from(), iq.to(), iq.id(), *si );
      return true;
    }

    return false;
  }
Exemple #14
0
AdhocTag::AdhocTag(const IQ &stanza) : Tag("command") {
    m_from = stanza.from().full();
    m_id = stanza.id();
    xdata = NULL;
    Tag *iq = stanza.tag();
    Tag *command = iq->findChild("command");
    if (command) {
        const Tag::AttributeList & attributes = command->attributes();
        for (Tag::AttributeList::const_iterator it = attributes.begin(); it != attributes.end(); it++) {
            addAttribute(std::string((*it)->name()), std::string((*it)->value()));
        }
        Tag *x = command->findChildWithAttrib("xmlns","jabber:x:data");
        if (x) {
            xdata = x->clone();
            addChild(xdata);
        }
    }
    delete iq;
}
Exemple #15
0
  bool Adhoc::handleIq( const IQ& iq )
  {
    if( iq.subtype() != IQ::Set )
      return false;

    const Adhoc::Command* ac = iq.findExtension<Adhoc::Command>( ExtAdhocCommand );
    if( !ac || ac->node().empty())
      return false;

    AdhocCommandProviderMap::const_iterator it = m_adhocCommandProviders.find( ac->node() );
    if( it != m_adhocCommandProviders.end() )
    {
      const std::string& sess = ac->sessionID().empty() ? m_parent->getID() : ac->sessionID();
      m_activeSessions[sess] = iq.id();
      (*it).second->handleAdhocCommand( iq.from(), *ac, sess );
      return true;
    }

    return false;
  }
bool SessionManager::handleIq( const IQ& iq )
{
    const Session::Jingle* j = iq.findExtension<Session::Jingle>( ExtJingle );
    if( !j )
        return false;

    m_factory.addPlugins( const_cast<Session::Jingle&>( *j ), j->embeddedTag() );

    SessionList::iterator it = m_sessions.begin();
    for( ; it != m_sessions.end() && (*it)->sid() != j->sid(); ++it ) ;
    if( it == m_sessions.end() )
    {
        Session* s = new Session( m_parent, iq.from(), j, m_handler );
        m_sessions.push_back( s );
        m_handler->handleIncomingSession( s );
        s->handleIq( iq );
    }
    else
    {
        (*it)->handleIq( iq );
    }
    return true;
}
void IBBTest::send( const IQ& iq, IqHandler* ih, int ctx )
{
  if( m_test == 1 )
  {
    const InBandBytestream::IBB* i = iq.findExtension<InBandBytestream::IBB>( ExtIBB );
    if( i && i->type() == InBandBytestream::IBBOpen )
      m_result++;
  }
  else if( m_test == 2 )
  {
    const InBandBytestream::IBB* i = iq.findExtension<InBandBytestream::IBB>( ExtIBB );
    if( i && i->type() == InBandBytestream::IBBData )
      m_result++;
  }
  else if( m_test == 3 )
  {
    const InBandBytestream::IBB* i = iq.findExtension<InBandBytestream::IBB>( ExtIBB );
    if( i && i->type() == InBandBytestream::IBBClose )
      m_result++;
  }

  IQ re( IQ::Result, iq.from(), iq.id() );
  ih->handleIqID( re, ctx );
}
Exemple #18
0
void AdhocTest::send( const IQ& iq, IqHandler*, int ctx )
{
  switch( m_test )
  {
    case 1: // getSupport()
    {
      Disco::Info i;
      i.m_features.push_back( XMLNS_ADHOC_COMMANDS );
      m_adhoc->handleDiscoInfo( g_jid, i, Adhoc::CheckAdhocSupport );
      break;
    }
    case 2: // getSupport() fails
    {
      Disco::Info i;
      m_adhoc->handleDiscoInfo( g_jid, i, Adhoc::CheckAdhocSupport );
      break;
    }
    case 3: // getCommands()
    {
      Disco::ItemList il;
      il.push_back( new Disco::Item( g_jid, "node", "name" ) );
      Disco::Items i;
      i.setItems( il );
      m_adhoc->handleDiscoItems( g_jid, i, Adhoc::FetchAdhocCommands );
      break;
    }
    case 4: // execute single stage command
    {
      IQ re( IQ::Result, iq.from(), iq.id() );
      re.setFrom( g_jid );
      re.addExtension( new Adhoc::Command( "foocmd", "somesess", Adhoc::Command::Completed, 0 ) );
      m_adhoc->handleIqID( re, ctx );
      break;
    }
  }
}
	void whistleVcardHandler::handleIqID( const IQ& iq, int context )
	{
		switch( iq.subtype() )
		{
		case IQ::Result:
			{
				switch( context )
				{
				case VCardHandler::FetchVCard:
					{
						json::jobject jobj;
						json::jobject status = json::jobject();
						if (iq.findExtension( ExtVCard ))
						{
							boost::shared_ptr<gloox::Tag> ptag(iq.findExtension( ExtVCard )->tag());
							for (TagList::const_iterator cit = ptag->children().begin(); cit != ptag->children().end(); ++cit) {
								Tag* ptag = *cit;
								if (ptag->name() == s_VcardPrivacy)
								{
									jobj[ptag->name()] = json::jobject(ptag->cdata());
								}
								else if (ptag->name() == s_VcardStatus)
								{
									//获取vcard协议改变 新的解析iq
									Tag* presource = ptag->findChild("resource");
									Tag* pshow = ptag->findChild("show");
									if ( presource && pshow)
									{
										json::jobject itemdata;
										itemdata["resource"] = presource->cdata();
										itemdata["show"] = pshow->cdata();
										status.arr_push(itemdata);
									}
								}
								else
								{
									jobj[ptag->name()] = ptag->cdata();
								}
							}
							jobj[s_VcardJid] = iq.from().bare();

							gwhistleVcard::instance().handleVCard(iq.from(), jobj, status, waitCallback_);
						}
						break;
					}
				case VCardHandler::StoreVCard:
					gwhistleVcard::instance().handleVCardResult(VCardHandler::StoreVCard, iq.from());
					break;
				}
			}
			break;
		case IQ::Error:
			{
				switch( context )
				{
				case VCardHandler::FetchVCard:
					if (!waitCallback_.empty())
					{
						waitCallback_();
					}
					else
					{
						gwhistleVcard::instance().updatedVcard(iq.from());
					}
					break;
				case VCardHandler::StoreVCard:
					gwhistleVcard::instance().handleVCardResult( (VCardHandler::VCardContext)context,
						iq.from(),
						iq.error() ? iq.error()->error()
						: StanzaErrorUndefined );
					break;
				}
			}
		default:
			break;
		}
	}
Exemple #20
0
  void Registration::handleIqID( const IQ& iq, int context )
  {
    if( !m_registrationHandler )
      return;

    if( iq.subtype() == IQ::Result )
    {
      switch( context )
      {
        case FetchRegistrationFields:
        {
          const Query* q = iq.findExtension<Query>( ExtRegistration );
          if( !q )
            return;

          if( q->registered() )
            m_registrationHandler->handleAlreadyRegistered( iq.from() );

          if( q->form() )
            m_registrationHandler->handleDataForm( iq.from(), *(q->form()) );

          if( q->oob() )
            m_registrationHandler->handleOOB( iq.from(), *(q->oob()) );

          m_registrationHandler->handleRegistrationFields( iq.from(), q->fields(), q->instructions() );
          break;
        }

        case CreateAccount:
        case ChangePassword:
        case RemoveAccount:
          m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationSuccess );
          break;
      }
    }
    else if( iq.subtype() == IQ::Error )
    {
      const Error* e = iq.error();
      if( !e )
        return;

      switch( e->error() )
      {
        case StanzaErrorConflict:
          m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationConflict );
          break;
        case StanzaErrorNotAcceptable:
          m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationNotAcceptable );
          break;
        case StanzaErrorBadRequest:
          m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationBadRequest );
          break;
        case StanzaErrorForbidden:
          m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationForbidden );
          break;
        case StanzaErrorRegistrationRequired:
          m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationRequired );
          break;
        case StanzaErrorUnexpectedRequest:
          m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationUnexpectedRequest );
          break;
        case StanzaErrorNotAuthorized:
          m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationNotAuthorized );
          break;
        case StanzaErrorNotAllowed:
          m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationNotAllowed );
          break;
        default:
          m_registrationHandler->handleRegistrationResult( iq.from(), RegistrationUnknownError );
          break;

      }
    }

  }
void SOCKS5BytestreamManager::handleIqID( const IQ& iq, int context )
{
    StringMap::iterator it = m_trackMap.find( iq.id() );
    if( it == m_trackMap.end() )
        return;

    switch( context )
    {
    case S5BOpenStream:
    {
        switch( iq.subtype() )
        {
        case IQ::Result:
        {
            const Query* q = iq.findExtension<Query>( ExtS5BQuery );
            if( q && m_socks5BytestreamHandler )
            {
                const std::string& proxy = q->jid().full();
                const StreamHost* sh = findProxy( iq.from(), proxy, (*it).second );
                if( sh )
                {
                    SOCKS5Bytestream* s5b = 0;
                    bool selfProxy = ( proxy == m_parent->jid().full() && m_server );
                    if( selfProxy )
                    {
                        SHA sha;
                        sha.feed( (*it).second );
                        sha.feed( iq.to().full() );
                        sha.feed( iq.from().full() );
                        s5b = new SOCKS5Bytestream( this, m_server->getConnection( sha.hex() ),
                                                    m_parent->logInstance(),
                                                    iq.to(), iq.from(),
                                                    (*it).second );
                    }
                    else
                    {
                        s5b = new SOCKS5Bytestream( this, m_parent->connectionImpl()->newInstance(),
                                                    m_parent->logInstance(),
                                                    iq.to(), iq.from(),
                                                    (*it).second );
                        s5b->setStreamHosts( StreamHostList( 1, *sh ) );
                    }
                    m_s5bMap[(*it).second] = s5b;
                    m_socks5BytestreamHandler->handleOutgoingBytestream( s5b );
                    if( selfProxy )
                        s5b->activate();
                }
            }
            break;
        }
        case IQ::Error:
            m_socks5BytestreamHandler->handleBytestreamError( iq, (*it).second );
            break;
        default:
            break;
        }
        break;
    }
    case S5BActivateStream:
    {
        switch( iq.subtype() )
        {
        case IQ::Result:
        {
            S5BMap::const_iterator it5 = m_s5bMap.find( (*it).second );
            if( it5 != m_s5bMap.end() )
                (*it5).second->activate();
            break;
        }
        case IQ::Error:
            m_socks5BytestreamHandler->handleBytestreamError( iq, (*it).second );
            break;
        default:
            break;
        }
        break;
    }
    default:
        break;
    }
    m_trackMap.erase( it );
}
Exemple #22
0
int main( int /*argc*/, char** /*argv*/ )
{
  int fail = 0;
  std::string name;
  Tag *iq = new Tag( "iq" );
  iq->addAttribute( "from", "[email protected]/gloox" );
  iq->addAttribute( "to", "[email protected]/gloox" );
  iq->addAttribute( "id", "id1" );
  iq->addAttribute( "type", "set" );
  IQ* i = 0;

  // -------
  name = "parse IQ set";
  i = new IQ( iq );
  if( i->subtype() != IQ::Set || i->from().full() != "[email protected]/gloox"
        || i->to().full() != "[email protected]/gloox" || i->id() != "id1" )
  {
    ++fail;
    printf( "test '%s' failed\n", name.c_str() );
  }
  delete i;
  i = 0;

  // -------
  name = "parse IQ get";
  iq->addAttribute( "type", "get" );
  i = new IQ( iq );
  if( i->subtype() != IQ::Get || i->from().full() != "[email protected]/gloox"
        || i->to().full() != "[email protected]/gloox" || i->id() != "id1" )
  {
    ++fail;
    printf( "test '%s' failed\n", name.c_str() );
  }
  delete i;
  i = 0;

  // -------
  name = "parse IQ error";
  iq->addAttribute( "type", "error" );
  i = new IQ( iq );
  if( i->subtype() != IQ::Error || i->from().full() != "[email protected]/gloox"
        || i->to().full() != "[email protected]/gloox" || i->id() != "id1" )
  {
    ++fail;
    printf( "test '%s' failed\n", name.c_str() );
  }
  delete i;
  i = 0;

  // -------
  name = "parse IQ result";
  iq->addAttribute( "type", "result" );
  i = new IQ( iq );
  if( i->subtype() != IQ::Result || i->from().full() != "[email protected]/gloox"
        || i->to().full() != "[email protected]/gloox" || i->id() != "id1" )
  {
    ++fail;
    printf( "test '%s' failed\n", name.c_str() );
  }
  delete i;
  i = 0;

  // -------
  {
    name = "new simple IQ error";
    IQ iq( IQ::Error, JID( "[email protected]/blah" ), "id2" );
    Tag* i = iq.tag();
    if( !i->hasAttribute( "type", "error" ) || !i->hasAttribute( "id", "id2" )
        || !i->hasAttribute( "to", "[email protected]/blah" ) )
    {
      ++fail;
      printf( "test '%s' failed: %s\n", name.c_str(), i->xml().c_str() );
    }
    delete i;
  }

  // -------
  {
    name = "new simple IQ result";
    IQ iq( IQ::Result, JID( "[email protected]/blah" ), "id2" );
    Tag* i = iq.tag();
    if( !i->hasAttribute( "type", "result" ) || !i->hasAttribute( "id", "id2" )
        || !i->hasAttribute( "to", "[email protected]/blah" ) )
    {
      ++fail;
      printf( "test '%s' failed: %s\n", name.c_str(), i->xml().c_str() );
    }
    delete i;
  }

  // -------
  {
    name = "new simple IQ get";
    IQ iq( IQ::Get, JID( "[email protected]/blah" ), "id2" );
    Tag* i = iq.tag();
    if( !i->hasAttribute( "type", "get" ) || !i->hasAttribute( "id", "id2" )
        || !i->hasAttribute( "to", "[email protected]/blah" ) )
    {
      ++fail;
      printf( "test '%s' failed: %s\n", name.c_str(), i->xml().c_str() );
    }
    delete i;
  }

  // -------
  {
    name = "new simple IQ set 1";
    IQ iq( IQ::Set, JID( "[email protected]/blah" ), "id2" );
    Tag* i = iq.tag();
    if( !i->hasAttribute( "type", "set" ) || !i->hasAttribute( "id", "id2" )
        || !i->hasAttribute( "to", "[email protected]/blah" ) )
    {
      ++fail;
      printf( "test '%s' failed: %s\n", name.c_str(), i->xml().c_str() );
    }
    delete i;
  }

// FIXME these need to use SEs, as IQ::query() will go away eventually
//   // -------
//   {
//     name = "new simple IQ set 2";
//     IQ iq( IQ::Set, JID( "[email protected]/blah" ), "id2", "mynamespace" );
//     Tag* i = iq.tag();
//     if( !i->hasAttribute( "type", "set" ) || !i->hasAttribute( "id", "id2" )
//         || !i->hasAttribute( "to", "[email protected]/blah" ) || !i->hasChild( "query", "xmlns", "mynamespace" ) )
//     {
//       ++fail;
//       printf( "test '%s' failed: %s\n", name.c_str(), i->xml().c_str() );
//     }
//     delete i;
//   }
//
//   // -------
//   {
//     name = "new simple IQ set 3";
//     IQ iq( IQ::Set, JID( "[email protected]/blah" ), "id2", "mynamespace", "testtag" );
//     Tag* i = iq.tag();
//     if( !i->hasAttribute( "type", "set" ) || !i->hasAttribute( "id", "id2" )
//         || !i->hasAttribute( "to", "[email protected]/blah" )
//         || !i->hasChild( "testtag", "xmlns", "mynamespace" ) )
//     {
//       ++fail;
//       printf( "test '%s' failed: %s\n", name.c_str(), i->xml().c_str() );
//     }
//     delete i;
//   }
//
//   // -------
//   {
//     name = "new simple IQ set 4";
//     IQ iq( IQ::Set, JID( "[email protected]/blah" ), "id2", "mynamespace", "testtag",
//                 JID( "[email protected]/foo" ) );
//     Tag* i = iq.tag();
//     if( !i->hasAttribute( "type", "set" ) || !i->hasAttribute( "id", "id2" )
//         || !i->hasAttribute( "to", "[email protected]/blah" ) || !i->hasChild( "testtag", "xmlns", "mynamespace" )
//         || !i->hasAttribute( "from", "[email protected]/foo" ) )
//     {
//       ++fail;
//       printf( "test '%s' failed: %s\n", name.c_str(), i->xml().c_str() );
//     }
//     delete i;
//   }

// FIXME fix the following test. how to test private functions, ctors, etc?
//   // -------
//   name = "rip off";
//   i = new IQ( iq );
//   if( !i->hasAttribute( "type", "result" ) || !i->hasAttribute( "id", "id1" )
//        || !i->hasAttribute( "to", "[email protected]/gloox" ) || !i->hasChild( "query", "xmlns", "mynamespace" )
//        || !i->hasAttribute( "from", "[email protected]/gloox" )
//        || iq->children().size() != 0 )
//   {
//     ++fail;
//     printf( "test '%s' failed: %s\n", name.c_str(), i->xml().c_str() );
//   }
//   delete i;
//   i = 0;







  delete iq;
  iq = 0;

  if( fail == 0 )
  {
    printf( "IQ: OK\n" );
    return 0;
  }
  else
  {
    printf( "IQ: %d test(s) failed\n", fail );
    return 1;
  }

}
Exemple #23
0
  bool Disco::handleIq( const IQ& iq )
  {
    switch( iq.subtype() )
    {
      case IQ::Get:
      {
        IQ re( IQ::Result, iq.from(), iq.id() );
        re.setFrom( iq.to() );

        const SoftwareVersion* sv = iq.findExtension<SoftwareVersion>( ExtVersion );
        if( sv )
        {
          re.addExtension( new SoftwareVersion( m_versionName, m_versionVersion, m_versionOs ) );
          m_parent->send( re );
          return true;
        }

        const Info *info = iq.findExtension<Info>( ExtDiscoInfo );
        if( info )
        {
          Info *i = new Info( EmptyString, true );
          if( !info->node().empty() )
          {
            i->setNode( info->node() );
            IdentityList identities;
            StringList features;
            DiscoNodeHandlerMap::const_iterator it = m_nodeHandlers.find( info->node() );
            if( it == m_nodeHandlers.end() )
            {
              delete i;
              IQ re( IQ::Error, iq.from(), iq.id() );
              re.addExtension( new Error( StanzaErrorTypeCancel, StanzaErrorItemNotFound ) );
              m_parent->send( re );
              return true;
            }
            else
            {
              DiscoNodeHandlerList::const_iterator in = (*it).second.begin();
              for( ; in != (*it).second.end(); ++in )
              {
                IdentityList il = (*in)->handleDiscoNodeIdentities( iq.from(), info->node() );
                il.sort(); // needed on win32
                identities.merge( il );
                StringList fl = (*in)->handleDiscoNodeFeatures( iq.from(), info->node() );
                fl.sort();  // needed on win32
                features.merge( fl );
              }
            }
            i->setIdentities( identities );
            i->setFeatures( features );
          }
          else
          {
            IdentityList il;
            IdentityList::const_iterator it = m_identities.begin();
            for( ; it != m_identities.end(); ++it )
            {
              il.push_back( new Identity( *(*it) ) );
            }
            i->setIdentities( il );
            i->setFeatures( m_features );
            if( m_form )
              i->setForm( new DataForm( *m_form ) );
          }

          re.addExtension( i );
          m_parent->send( re );
          return true;
        }

        const Items *items = iq.findExtension<Items>( ExtDiscoItems );
        if( items )
        {
          Items *i = new Items( items->node() );
          if( !items->node().empty() )
          {
            DiscoNodeHandlerMap::const_iterator it = m_nodeHandlers.find( items->node() );
            if( it == m_nodeHandlers.end() )
            {
              delete i;
              IQ re( IQ::Error, iq.from(), iq.id() );
              re.addExtension( new Error( StanzaErrorTypeCancel, StanzaErrorItemNotFound ) );
              m_parent->send( re );
              return true;
            }
            else
            {
              ItemList itemlist;
              DiscoNodeHandlerList::const_iterator in = (*it).second.begin();
              for( ; in != (*it).second.end(); ++in )
              {
                ItemList il = (*in)->handleDiscoNodeItems( iq.from(), iq.to(), items->node() );
                il.sort(); // needed on win32
                itemlist.merge( il );
              }
              i->setItems( itemlist );
            }
          }

          re.addExtension( i );
          m_parent->send( re );
          return true;
        }
        break;
      }

      case IQ::Set:
      {
        bool res = false;
        DiscoHandlerList::const_iterator it = m_discoHandlers.begin();
        for( ; it != m_discoHandlers.end(); ++it )
        {
          if( (*it)->handleDiscoSet( iq ) )
            res = true;
        }
        return res;
        break;
      }

      default:
        break;
    }
    return false;
  }
void NonSaslAuthTest::send( const IQ& iq, IqHandler* ih, int ctx )
{
  if( m_test == 1 )
  {
    const NonSaslAuth::Query* q = iq.findExtension<NonSaslAuth::Query>( ExtNonSaslAuth );
    if( q )
    {
      m_result++;
      m_test = 2;
      IQ re( IQ::Result, iq.from(), iq.id() );
      Tag* d = new Tag( "query" );
      d->setXmlns( XMLNS_AUTH );
      new Tag( d, "username" );
      new Tag( d, "password" );
      new Tag( d, "resource" );
      re.addExtension( new NonSaslAuth::Query( d ) );
      ih->handleIqID( re, ctx );
      delete d;
    }
  }
  else if( m_test == 2 )
  {
    const NonSaslAuth::Query* q = iq.findExtension<NonSaslAuth::Query>( ExtNonSaslAuth );
    if( q )
    {
      Tag* d = q->tag();
      if( d->xml() == "<query xmlns='" + XMLNS_AUTH + "'>"
                      "<username>user</username>"
                      "<password>pwd</password>"
                      "<resource>resource</resource>"
                      "</query>" )
      {
        m_result++;
        IQ re( IQ::Result, iq.from(), iq.id() );
        ih->handleIqID( re, ctx );
      }
      delete d;
    }
  }
  else if( m_test == 3 )
  {
    const NonSaslAuth::Query* q = iq.findExtension<NonSaslAuth::Query>( ExtNonSaslAuth );
    if( q )
    {
      m_result++;
      m_test = 4;
      IQ re( IQ::Result, iq.from(), iq.id() );
      Tag* d = new Tag( "query" );
      d->setXmlns( XMLNS_AUTH );
      new Tag( d, "username" );
      new Tag( d, "digest" );
      new Tag( d, "password" );
      new Tag( d, "resource" );
      re.addExtension( new NonSaslAuth::Query( d ) );
      ih->handleIqID( re, ctx );
      delete d;
    }
  }
  else if( m_test == 4 )
  {
    const NonSaslAuth::Query* q = iq.findExtension<NonSaslAuth::Query>( ExtNonSaslAuth );
    if( q )
    {
      Tag* d = q->tag();
      SHA sha;
      sha.feed( "sid2" );
      sha.feed( "pwd" );
      if( d->xml() == "<query xmlns='" + XMLNS_AUTH + "'>"
          "<username>user</username>"
          "<digest>" + sha.hex() + "</digest>"
          "<resource>resource</resource>"
          "</query>" )
      {
        m_result += 2;
        IQ re( IQ::Result, iq.from(), iq.id() );
        ih->handleIqID( re, ctx );
      }
      delete d;
    }
  }
}