Exemplo n.º 1
0
  void PrivateXML::handleIqID( const IQ& iq, int context )
  {
    TrackMap::iterator t = m_track.find( iq.id() );
    if( t == m_track.end() )
      return;

    if( iq.subtype() == IQ::Result )
    {
      if( context == RequestXml )
      {
        const Query* q = iq.findExtension<Query>( ExtPrivateXML );
        if( q )
          (*t).second->handlePrivateXML( q->privateXML() );
      }
      else if( context == StoreXml )
        (*t).second->handlePrivateXMLResult( iq.id(), PrivateXMLHandler::PxmlStoreOk );
    }
    else if( iq.subtype() == IQ::Error )
    {
      if( context == RequestXml )
        (*t).second->handlePrivateXMLResult( iq.id(), PrivateXMLHandler::PxmlRequestError );
      else if( context == StoreXml )
        (*t).second->handlePrivateXMLResult( iq.id(), PrivateXMLHandler::PxmlStoreError );
    }

    m_track.erase( t );
  }
Exemplo n.º 2
0
  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;
  }
Exemplo n.º 3
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;
    }
Exemplo n.º 4
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();
  }
Exemplo n.º 5
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;
    }
  }
Exemplo n.º 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;
  }
Exemplo n.º 7
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;
  }
Exemplo n.º 8
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;
}
Exemplo n.º 9
0
  bool PrivacyManager::handleIq( const IQ& iq )
  {
    const Query* q = iq.findExtension<Query>( ExtPrivacy );
    if( iq.subtype() != IQ::Set || !m_privacyListHandler
        || !q || q->name().empty() )
      return false;

    m_privacyListHandler->handlePrivacyListChanged( q->name() );
    IQ re( IQ::Result, JID(), iq.id() );
    m_parent->send( re );
    return true;
  }
Exemplo n.º 10
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;
  }
Exemplo n.º 11
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;
}
Exemplo n.º 12
0
  bool RosterManager::handleIq( const IQ& iq )
  {
    if( iq.subtype() != IQ::Set ) // FIXME add checks for 'from' attribute (empty or bare self jid?)
      return false;

    // single roster item push
    const Query* q = iq.findExtension<Query>( ExtRoster );
    if( q && q->roster().size() )
      mergePush( q->roster() );

    IQ re( IQ::Result, JID(), iq.id() );
    m_parent->send( re );
    return true;
  }
Exemplo n.º 13
0
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;
}
Exemplo n.º 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;
}
Exemplo n.º 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;
  }
Exemplo n.º 16
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 );
    }
  }
Exemplo n.º 17
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 );
    }
  }
Exemplo n.º 18
0
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 );
}
Exemplo n.º 19
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;
    }
  }
}
Exemplo n.º 20
0
  void PrivacyManager::handleIqID( const IQ& iq, int context )
  {
    if( !m_privacyListHandler )
      return;

    switch( iq.subtype() )
    {
      case IQ::Result:
        switch( context )
        {
          case PLStore:
            m_privacyListHandler->handlePrivacyListResult( iq.id(), ResultStoreSuccess );
            break;
          case PLActivate:
            m_privacyListHandler->handlePrivacyListResult( iq.id(), ResultActivateSuccess );
            break;
          case PLDefault:
            m_privacyListHandler->handlePrivacyListResult( iq.id(), ResultDefaultSuccess );
            break;
          case PLRemove:
            m_privacyListHandler->handlePrivacyListResult( iq.id(), ResultRemoveSuccess );
            break;
          case PLRequestNames:
          {
            const Query* q = iq.findExtension<Query>( ExtPrivacy );
            if( !q )
              return;
            m_privacyListHandler->handlePrivacyListNames( q->def(), q->active(),
                                                          q->names() );
            break;
          }
          case PLRequestList:
          {
            const Query* q = iq.findExtension<Query>( ExtPrivacy );
            if( !q )
              return;
            m_privacyListHandler->handlePrivacyList( q->name(), q->items() );
            break;
          }
        }
        break;

      case IQ::Error:
      {
        switch( iq.error()->error() )
        {
          case StanzaErrorConflict:
            m_privacyListHandler->handlePrivacyListResult( iq.id(), ResultConflict );
            break;
          case StanzaErrorItemNotFound:
            m_privacyListHandler->handlePrivacyListResult( iq.id(), ResultItemNotFound );
            break;
          case StanzaErrorBadRequest:
            m_privacyListHandler->handlePrivacyListResult( iq.id(), ResultBadRequest );
            break;
          default:
            m_privacyListHandler->handlePrivacyListResult( iq.id(), ResultUnknownError );
            break;
        }
        break;
      }

      default:
        break;
    }
  }
Exemplo n.º 21
0
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 );
}
Exemplo n.º 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;
  }

}
Exemplo n.º 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;
    }
  }
}