📄 client.cpp
字号:
if( stanza->hasChild( "register", "xmlns", XMLNS_STREAM_IQREGISTER ) ) features |= StreamFeatureIqRegister; if( stanza->hasChild( "compression", "xmlns", XMLNS_STREAM_COMPRESS ) ) features |= getCompressionMethods( stanza->findChild( "compression" ) ); if( features == 0 ) features = StreamFeatureIqAuth; return features; } int Client::getSaslMechs( Tag *tag ) { int mechs = SaslMechNone; if( tag->hasChildWithCData( "mechanism", "DIGEST-MD5" ) ) mechs |= SaslMechDigestMd5; if( tag->hasChildWithCData( "mechanism", "PLAIN" ) ) mechs |= SaslMechPlain; if( tag->hasChildWithCData( "mechanism", "ANONYMOUS" ) ) mechs |= SaslMechAnonymous; if( tag->hasChildWithCData( "mechanism", "EXTERNAL" ) ) mechs |= SaslMechExternal; if( tag->hasChildWithCData( "mechanism", "GSSAPI" ) ) mechs |= SaslMechGssapi; return mechs; } int Client::getCompressionMethods( Tag *tag ) { int meths = 0; if( tag->hasChildWithCData( "method", "zlib" ) ) meths |= StreamFeatureCompressZlib; if( tag->hasChildWithCData( "method", "lzw" ) ) meths |= StreamFeatureCompressDclz; return meths; } void Client::bindResource() { if( !m_resourceBound ) { Tag *iq = new Tag( "iq" ); iq->addAttribute( "type", "set" ); iq->addAttribute( "id", "bind" ); Tag *b = new Tag( iq, "bind" ); b->addAttribute( "xmlns", XMLNS_STREAM_BIND ); if( !resource().empty() ) new Tag( b, "resource", resource() ); send( iq ); } } void Client::processResourceBind( Stanza *stanza ) { switch( stanza->subtype() ) { case StanzaIqResult: { Tag *bind = stanza->findChild( "bind" ); Tag *jid = bind->findChild( "jid" ); m_jid.setJID( jid->cdata() ); m_resourceBound = true; if( m_streamFeatures & StreamFeatureSession ) createSession(); else connected(); break; } case StanzaIqError: { Tag *error = stanza->findChild( "error" ); if( stanza->hasChild( "error", "type", "modify" ) && error->hasChild( "bad-request", "xmlns", XMLNS_XMPP_STANZAS ) ) { notifyOnResourceBindError( RbErrorBadRequest ); } else if( stanza->hasChild( "error", "type", "cancel" ) ) { if( error->hasChild( "not-allowed", "xmlns", XMLNS_XMPP_STANZAS ) ) notifyOnResourceBindError( RbErrorNotAllowed ); else if( error->hasChild( "conflict", "xmlns", XMLNS_XMPP_STANZAS ) ) notifyOnResourceBindError( RbErrorConflict ); else notifyOnResourceBindError( RbErrorUnknownError ); } else notifyOnResourceBindError( RbErrorUnknownError ); break; } default: break; } } void Client::createSession() { notifyStreamEvent( StreamEventSessionCreation ); Tag *iq = new Tag( "iq" ); iq->addAttribute( "type", "set" ); iq->addAttribute( "id", "session" ); Tag *s = new Tag( iq, "session" ); s->addAttribute( "xmlns", XMLNS_STREAM_SESSION ); send( iq ); } void Client::processCreateSession( Stanza *stanza ) { switch( stanza->subtype() ) { case StanzaIqResult: { connected(); break; } case StanzaIqError: { Tag *error = stanza->findChild( "error" ); if( stanza->hasChild( "error", "type", "wait" ) && error->hasChild( "internal-server-error", "xmlns", XMLNS_XMPP_STANZAS ) ) { notifyOnSessionCreateError( ScErrorInternalServerError ); } else if( stanza->hasChild( "error", "type", "auth" ) && error->hasChild( "forbidden", "xmlns", XMLNS_XMPP_STANZAS ) ) { notifyOnSessionCreateError( ScErrorForbidden ); } else if( stanza->hasChild( "error", "type", "cancel" ) && error->hasChild( "conflict", "xmlns", XMLNS_XMPP_STANZAS ) ) { notifyOnSessionCreateError( ScErrorConflict ); } else notifyOnSessionCreateError( ScErrorUnknownError ); break; } default: break; } } void Client::negotiateCompression( StreamFeature method ) { Tag *t = new Tag( "compress" ); t->addAttribute( "xmlns", XMLNS_COMPRESSION ); if( method == StreamFeatureCompressZlib ) new Tag( t, "method", "zlib" ); if( method == StreamFeatureCompressDclz ) new Tag( t, "method", "lzw" ); send( t ); } void Client::addPresenceExtension( StanzaExtension *se ) { m_presenceExtensions.push_back( se ); } void Client::removePresenceExtensions() { StanzaExtensionList::iterator it = m_presenceExtensions.begin(); for( ; it != m_presenceExtensions.end(); ++it ) { delete (*it); } m_presenceExtensions.clear(); } void Client::setPresence( Presence presence, int priority, const std::string& status ) { m_presence = presence; m_status = status; if( priority < -128 ) m_priority = -128; if( priority > 127 ) m_priority = 127; else m_priority = priority; sendPresence(); } void Client::disableRoster() { m_manageRoster = false; delete m_rosterManager; m_rosterManager = 0; } void Client::nonSaslLogin() { if( !m_auth ) m_auth = new NonSaslAuth( this ); m_auth->doAuth( m_sid ); } void Client::sendPresence() { if( m_presence != PresenceUnknown && state() >= StateConnected ) { JID jid; Stanza *p = Stanza::createPresenceStanza( jid, m_status, m_presence );#ifdef _WIN32_WCE char tmp[5]; tmp[4] = '\0'; sprintf( tmp, "%s", m_priority ); new Tag( p, "priority", tmp );#else std::ostringstream oss; oss << m_priority; new Tag( p, "priority", oss.str() );#endif StanzaExtensionList::const_iterator it = m_presenceExtensions.begin(); for( ; it != m_presenceExtensions.end(); ++it ) { p->addChild( (*it)->tag() ); } send( p ); } } void Client::connected() { if( m_authed ) { if( m_manageRoster ) { notifyStreamEvent( StreamEventRoster ); m_rosterManager->fill(); } else rosterFilled(); } else { notifyStreamEvent( StreamEventFinished ); notifyOnConnect(); } } void Client::rosterFilled() { sendPresence(); notifyStreamEvent( StreamEventFinished ); notifyOnConnect(); } void Client::disconnect() { disconnect( ConnUserDisconnected ); } void Client::disconnect( ConnectionError reason ) { m_resourceBound = false; m_authed = false; m_streamFeatures = 0; ClientBase::disconnect( reason ); } void Client::cleanup() { m_authed = false; m_resourceBound = false; m_streamFeatures = 0; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -