void XmppSipPlugin::onNewMessage( const Jreen::Message& message ) { if ( m_state != Account::Connected ) return; QString from = message.from().full(); QString msg = message.body(); if ( msg.isEmpty() ) return; if ( message.subtype() == Jreen::Message::Error ) { tDebug() << Q_FUNC_INFO << "Received error message from" << from << ", not answering... (Condition:" << ( message.error().isNull() ? -1 : message.error()->condition() ) << ")"; return; } // FIXME: We do not sent SipInfo in JSON via XMPP, why do we receive it here? SipInfo info = SipInfo::fromJson( msg ); if ( !info.isValid() ) { QString to = from; QString response = QString( tr( "I'm sorry -- I'm just an automatic presence used by Tomahawk Player" " (http://gettomahawk.com). If you are getting this message, the person you" " are trying to reach is probably not signed on, so please try again later!" ) ); // this is not a sip message, so we send it directly through the client m_client->send( Jreen::Message ( Jreen::Message::Error, Jreen::JID( to ), response) ); return; } qDebug() << Q_FUNC_INFO << "From:" << message.from().full() << ":" << message.body(); }
QDebug operator<< ( QDebug dbg, const SipInfo& info ) { if( !info.isValid() ) dbg.nospace() << "info is invalid"; else dbg.nospace() << info.toJson(); return dbg.maybeSpace(); }
void SipHandler::onPeerOnline( const QString& peerId ) { // qDebug() << Q_FUNC_INFO; tDebug() << "SIP online:" << peerId; SipPlugin* sip = qobject_cast<SipPlugin*>(sender()); SipInfo info; if( Servent::instance()->visibleExternally() ) { QString key = uuid(); ControlConnection* conn = new ControlConnection( Servent::instance(), QString() ); const QString& nodeid = Database::instance()->impl()->dbid(); conn->setName( peerId.left( peerId.indexOf( "/" ) ) ); conn->setId( nodeid ); Servent::instance()->registerOffer( key, conn ); info.setVisible( true ); info.setHost( Servent::instance()->externalAddress() ); info.setPort( Servent::instance()->externalPort() ); info.setKey( key ); info.setUniqname( nodeid ); tDebug() << "Asking them to connect to us:" << info; } else { info.setVisible( false ); tDebug() << "We are not visible externally:" << info; } sip->sendMsg( peerId, info ); }
const SipInfo SipInfo::fromJson( QString json ) { SipInfo info; QJson::Parser parser; bool ok; QVariant v = parser.parse( json.toLatin1(), &ok ); if ( !ok || v.type() != QVariant::Map ) { qDebug() << Q_FUNC_INFO << "Invalid JSON: " << json; return info; } QVariantMap m = v.toMap(); info.setVisible( m["visible"].toBool() ); if ( m["visible"].toBool() ) { info.setHost( m["host"].toString() ); info.setPort( m["port"].toInt() ); info.setNodeId( m["uniqname"].toString() ); info.setKey( m["key"].toString() ); } return info; }
void XmppSipPlugin::sendSipInfo( const Tomahawk::peerinfo_ptr& receiver, const SipInfo& info ) { tDebug( LOGVERBOSE ) << Q_FUNC_INFO << receiver << info; if ( !m_client ) return; TomahawkXmppMessage *sipMessage; if ( info.isVisible() ) { sipMessage = new TomahawkXmppMessage( info.host(), info.port(), info.nodeId(), info.key() ); } else sipMessage = new TomahawkXmppMessage(); qDebug() << "Send sip messsage to" << receiver; Jreen::IQ iq( Jreen::IQ::Set, receiver->id() ); iq.addExtension( sipMessage ); Jreen::IQReply *reply = m_client->send( iq ); reply->setData( SipMessageSent ); connect( reply, SIGNAL( received( Jreen::IQ ) ), SLOT( onNewIq( Jreen::IQ ) ) ); }
void SipHandler::onSipInfo( const QString& peerId, const SipInfo& info ) { tDebug() << Q_FUNC_INFO << "SIP Message:" << peerId << info; QString barePeerId = peerId.left( peerId.indexOf( "/" ) ); //FIXME: We should probably be using barePeerId in the connectToPeer call below. //But, verify this doesn't cause any problems (there is still a uniquename after all) /* If only one party is externally visible, connection is obvious If both are, peer with lowest IP address initiates the connection. This avoids dupe connections. */ if ( info.isVisible() ) { if( !Servent::instance()->visibleExternally() || Servent::instance()->externalAddress() < info.host() || ( Servent::instance()->externalAddress() == info.host() && Servent::instance()->externalPort() < info.port() ) ) { tDebug() << "Initiate connection to" << peerId << "at" << info.host(); Servent::instance()->connectToPeer( info.host(), info.port(), info.key(), peerId, info.uniqname() ); } else { tDebug() << Q_FUNC_INFO << "They should be conecting to us..."; } } else { tDebug() << Q_FUNC_INFO << "They are not visible, doing nothing atm"; } m_peersSipInfos.insert( peerId, info ); }
void DiagnosticsDialog::updateLogView() { QString log; log.append( QString("TOMAHAWK DIAGNOSTICS LOG -%1 \n\n") .arg( QDateTime::currentDateTime().toString() ) ); // network log.append( "TOMAHAWK-VERSION: " TOMAHAWK_VERSION "\n\n\n" ); // network log.append( "NETWORK:\n" " General:\n" ); if( Servent::instance()->visibleExternally() ) { log.append( QString( " visible: true\n" " host: %1\n" " port: %2\n" "\n" ).arg( Servent::instance()->externalAddress() ) .arg( Servent::instance()->externalPort() ) ); } else { log.append( QString( " visible: false" ) ); } log.append("\n\n"); // Peers log.append("SIP PLUGINS:\n"); QList< Tomahawk::source_ptr > sources = SourceList::instance()->sources( true ); Q_FOREACH(SipPlugin *sip, SipHandler::instance()->allPlugins()) { Q_ASSERT(sip); QString stateString; switch( sip->connectionState() ) { case SipPlugin::Connecting: stateString = "Connecting"; break; case SipPlugin::Connected: stateString = "Connected"; break; case SipPlugin::Disconnected: stateString = "Disconnected"; break; case SipPlugin::Disconnecting: stateString = "Disconnecting"; } log.append( QString(" %2 (%1): %3 (%4)\n") .arg(sip->name()) .arg(sip->friendlyName()) .arg(sip->accountName()) .arg(stateString) ); Q_FOREACH( const QString &peerId, sip->peersOnline() ) { /* enable this again, when we check the source has this peerId bool connected = false; Q_FOREACH( const Tomahawk::source_ptr &source, sources ) { if( source->controlConnection() ) { connected = true; break; } }*/ QString versionString = SipHandler::instance()->versionString( peerId ); SipInfo sipInfo = SipHandler::instance()->sipInfo( peerId ); if( !sipInfo.isValid() ) log.append( QString(" %1: %2 %3" /*"(%4)"*/ "\n") .arg( peerId ) .arg( "sipinfo invalid" ) .arg( versionString ) // .arg( connected ? "connected" : "not connected") ); else if( sipInfo.isVisible() ) log.append( QString(" %1: %2:%3 %4" /*" (%5)"*/ "\n") .arg( peerId ) .arg( sipInfo.host().hostName() ) .arg( sipInfo.port() ) .arg( versionString ) // .arg( connected ? "connected" : "not connected") ); else log.append( QString(" %1: visible: false %2" /*" (%3)"*/ "\n") .arg( peerId ) .arg( versionString ) // .arg( connected ? "connected" : "not connected") ); } log.append("\n"); }
bool operator==( const SipInfo& one, const SipInfo& two ) { // check valid/invalid combinations first, so we don't try to access any invalid sipInfos (->assert) if ( ( one.isValid() && !two.isValid() ) || ( !one.isValid() && two.isValid() ) ) { return false; } else if ( one.isValid() && two.isValid() ) { if ( one.isVisible() == two.isVisible() && one.host() == two.host() && one.port() == two.port() && one.nodeId() == two.nodeId() && one.key() == two.key() ) { return true; } } return false; }