void xmppClient::slotPresenceReceived(const QXmppPresence &presence) { const QLatin1String recipient("*****@*****.**"); // if we are the recipient, or if the presence is not from the recipient, // do nothing if (jidToBareJid(configuration().jid()) == recipient || jidToBareJid(presence.from()) != recipient || presence.type() != QXmppPresence::Available) return; // send the file and connect to the job's signals QXmppTransferJob *job = transferManager->sendFile(presence.from(), "xmppClient.cpp"); bool check = connect( job, SIGNAL(error(QXmppTransferJob::Error)), this, SLOT(slotError(QXmppTransferJob::Error)) ); Q_ASSERT(check); check = connect( job, SIGNAL(finished()), this, SLOT(slotFinished()) ); Q_ASSERT(check); check = connect( job, SIGNAL(progress(qint64,qint64)), this, SLOT(slotProgress(qint64,qint64)) ); Q_ASSERT(check); }
ChatWindow::ChatWindow(QString jid, QXmppClient *client, QWidget *parent) : QMainWindow(parent), m_jid(jid), m_client(client), m_selfState(QXmppMessage::Active), m_pausedTimer(new QTimer), m_inactiveTimer(new QTimer), m_goneTimer(new QTimer), m_statusBar(new QStatusBar), m_sendButton(new QPushButton), m_sendTip(new QLabel) { ui.setupUi(this); setWindowTitle(QString(tr("Contact: %1")).arg(m_jid)); m_editor = new MessageEdit(); QVBoxLayout *layout = new QVBoxLayout(); layout->addWidget(m_editor); layout->setMargin(0); ui.editorWarpwidget->setLayout(layout); m_editor->setFocus(); m_sendButton->setText(tr("Send")); m_sendButton->setFixedHeight(m_statusBar->sizeHint().height()); m_statusBar->addPermanentWidget(m_sendTip); m_statusBar->addPermanentWidget(m_sendButton); m_statusBar->setSizeGripEnabled(false); setStatusBar(m_statusBar); // init ui QXmppRoster::QXmppRosterEntry entry = m_client->getRoster().getRosterEntry(jidToBareJid(m_jid)); ui.name->setText(entry.name()); ui.jid->setText(m_jid); if (m_client->getRoster().getResources(jidToBareJid(m_jid)).isEmpty()) ui.photo->setPixmap(QPixmap(":/images/user-identity-grey-100.png")); else ui.photo->setPixmap(QPixmap(":/images/user-identity-100.png")); connect(m_sendButton, SIGNAL(clicked()), this, SLOT(sendMessage())); connect(m_editor, SIGNAL(textChanged()), this, SLOT(sendComposing())); connect(m_pausedTimer, SIGNAL(timeout()), this, SLOT(pausedTimeout())); connect(m_inactiveTimer, SIGNAL(timeout()), this, SLOT(inactiveTimeout())); connect(m_goneTimer, SIGNAL(timeout()), this, SLOT(goneTimeout())); connect(ui.detailButton, SIGNAL(clicked()), this, SLOT(openContactInfoDialog()) ); // action connect(ui.actionSendFile, SIGNAL(triggered()), this, SLOT(sendFileSlot()) ); setAttribute(Qt::WA_QuitOnClose, false); setAttribute(Qt::WA_DeleteOnClose, true); }
QList<QXmppMessage> UnreadMessageModel::take(QString jid) { QString resource = jidToResource(jid); QString bareJid = jidToBareJid(jid); QList<QXmppMessage> results; if (resource.isEmpty()) { results = m_messageStore.take(bareJid); } else { QList<QXmppMessage> mid = m_messageStore.take(bareJid); foreach (QXmppMessage message, mid) { if (jidToResource(message.from()) == resource) { results << message; } else { add(message); } } } if (m_messageStore.isEmpty()) { emit messageCleared(); } else { qDebug("no empty"); } reset(); return results; }
void QXmppRosterManager::presenceReceived(const QXmppPresence& presence) { QString jid = presence.from(); QString bareJid = jidToBareJid(jid); QString resource = jidToResource(jid); if (bareJid.isEmpty()) return; switch(presence.type()) { case QXmppPresence::Available: m_presences[bareJid][resource] = presence; emit presenceChanged(bareJid, resource); break; case QXmppPresence::Unavailable: m_presences[bareJid].remove(resource); emit presenceChanged(bareJid, resource); break; case QXmppPresence::Subscribe: if (m_stream->configuration().autoAcceptSubscriptions()) { QXmppPresence presence; presence.setTo(jid); presence.setType(QXmppPresence::Subscribed); m_stream->sendPacket(presence); } break; default: break; } }
void xmppClient::slotPresenceReceived(const QXmppPresence &presence) { const QLatin1String recipient("*****@*****.**"); // if we are the recipient, or if the presence is not from the recipient, // do nothing if (jidToBareJid(configuration().jid()) == recipient || jidToBareJid(presence.from()) != recipient || presence.type() != QXmppPresence::Available) return; // start the call and connect to the its signals QXmppCall *call = callManager->call(presence.from()); bool check = connect(call, SIGNAL(connected()), this, SLOT(slotConnected())); Q_ASSERT(check); check = connect(call, SIGNAL(finished()), this, SLOT(slotFinished())); Q_ASSERT(check); }
void TestUtils::testJid() { QCOMPARE(jidToBareJid("[email protected]/resource"), QLatin1String("*****@*****.**")); QCOMPARE(jidToBareJid("*****@*****.**"), QLatin1String("*****@*****.**")); QCOMPARE(jidToBareJid("example.com"), QLatin1String("example.com")); QCOMPARE(jidToBareJid(QString()), QString()); QCOMPARE(jidToDomain("[email protected]/resource"), QLatin1String("example.com")); QCOMPARE(jidToDomain("*****@*****.**"), QLatin1String("example.com")); QCOMPARE(jidToDomain("example.com"), QLatin1String("example.com")); QCOMPARE(jidToDomain(QString()), QString()); QCOMPARE(jidToResource("[email protected]/resource"), QLatin1String("resource")); QCOMPARE(jidToResource("*****@*****.**"), QString()); QCOMPARE(jidToResource("example.com"), QString()); QCOMPARE(jidToResource(QString()), QString()); QCOMPARE(jidToUser("[email protected]/resource"), QLatin1String("foo")); QCOMPARE(jidToUser("*****@*****.**"), QLatin1String("foo")); QCOMPARE(jidToUser("example.com"), QString()); QCOMPARE(jidToUser(QString()), QString()); }
void ChatExtension::HandleMessageReceived(const QXmppMessage &message) { if(message.type() == QXmppMessage::GroupChat) return; QString sender_jid = jidToBareJid(message.from()); QString msg = message.body(); LogInfo("XMPPModule: Received message. From: " + sender_jid.toStdString() + " Body: " + msg.toStdString()); emit MessageReceived(sender_jid, msg); }
void ChatWindow::changeSelfState(QXmppMessage::State state) { if (m_selfState != state) { m_selfState = state; QString resource = jidToResource(m_jid); QString bareJid = jidToBareJid(m_jid); XmppMessage message(m_client->getConfiguration().jid(), m_jid); message.setState(state); if (resource.isEmpty()) { // if breaJid at less have one resource if (!m_client->getRoster().getAllPresencesForBareJid(bareJid).isEmpty()) { m_client->sendPacket(message); } } else { // if resource no unavable if (!m_client->getRoster().getPresence(bareJid, resource).from().isEmpty()) { m_client->sendPacket(message); } } } }
bool UnreadMessageModel::hasUnread(const QString &jid) const { return m_messageStore.contains(jidToBareJid(jid)); }
void UnreadMessageModel::add(const QXmppMessage &message) { m_messageStore[jidToBareJid(message.from())] << message; reset(); }
void QXmppIncomingClient::handleStanza(const QDomElement &nodeRecv) { const QString ns = nodeRecv.namespaceURI(); if (d->idleTimer->interval()) d->idleTimer->start(); if (ns == ns_tls && nodeRecv.tagName() == "starttls") { sendData("<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>"); socket()->flush(); socket()->startServerEncryption(); return; } else if (ns == ns_sasl) { if (nodeRecv.tagName() == "auth") { const QString mechanism = nodeRecv.attribute("mechanism"); if (mechanism == "PLAIN") { QList<QByteArray> auth = QByteArray::fromBase64(nodeRecv.text().toAscii()).split('\0'); if (auth.size() != 3) { sendData("<failure xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><incorrect-encoding/></failure>"); disconnectFromHost(); return; } QXmppPasswordRequest request; request.setDomain(d->domain); request.setUsername(QString::fromUtf8(auth[1])); request.setPassword(QString::fromUtf8(auth[2])); if (!d->passwordChecker) { // FIXME: what type of failure? warning(QString("Cannot authenticate '%1', no password checker").arg(request.username())); sendData("<failure xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>"); disconnectFromHost(); return; } QXmppPasswordReply *reply = d->passwordChecker->checkPassword(request); reply->setParent(this); reply->setProperty("__sasl_username", request.username()); connect(reply, SIGNAL(finished()), this, SLOT(onPasswordReply())); } else if (mechanism == "DIGEST-MD5") { // generate nonce d->saslDigest.setNonce(QXmppSaslDigestMd5::generateNonce()); d->saslDigest.setQop("auth"); d->saslStep = 1; QMap<QByteArray, QByteArray> challenge; challenge["nonce"] = d->saslDigest.nonce(); challenge["realm"] = d->domain.toUtf8(); challenge["qop"] = d->saslDigest.qop(); challenge["charset"] = "utf-8"; challenge["algorithm"] = "md5-sess"; const QByteArray data = QXmppSaslDigestMd5::serializeMessage(challenge).toBase64(); sendData("<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>" + data +"</challenge>"); } else { // unsupported method sendData("<failure xmlns='urn:ietf:params:xml:ns:xmpp-sasl'></failure>"); disconnectFromHost(); return; } } else if (nodeRecv.tagName() == "response") { if (d->saslStep == 1) { const QByteArray raw = QByteArray::fromBase64(nodeRecv.text().toAscii()); QMap<QByteArray, QByteArray> saslResponse = QXmppSaslDigestMd5::parseMessage(raw); // check credentials const QString username = QString::fromUtf8(saslResponse.value("username")); if (!d->passwordChecker) { // FIXME: what type of failure? warning(QString("Cannot authenticate '%1', no password checker").arg(username)); sendData("<failure xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>"); disconnectFromHost(); return; } QXmppPasswordRequest request; request.setUsername(username); request.setDomain(d->domain); QXmppPasswordReply *reply = d->passwordChecker->getDigest(request); reply->setParent(this); reply->setProperty("__sasl_raw", raw); connect(reply, SIGNAL(finished()), this, SLOT(onDigestReply())); } else if (d->saslStep == 2) { // authentication succeeded d->saslStep = 3; info(QString("Authentication succeeded for '%1'").arg(d->username)); sendData("<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>"); } } } else if (ns == ns_client) { if (nodeRecv.tagName() == "iq") { const QString type = nodeRecv.attribute("type"); if (QXmppBindIq::isBindIq(nodeRecv) && type == "set") { QXmppBindIq bindSet; bindSet.parse(nodeRecv); d->resource = bindSet.resource().trimmed(); if (d->resource.isEmpty()) d->resource = generateStanzaHash(); QXmppBindIq bindResult; bindResult.setType(QXmppIq::Result); bindResult.setId(bindSet.id()); bindResult.setJid(jid()); sendPacket(bindResult); // bound emit connected(); return; } else if (QXmppSessionIq::isSessionIq(nodeRecv) && type == "set") { QXmppSessionIq sessionSet; sessionSet.parse(nodeRecv); QXmppIq sessionResult; sessionResult.setType(QXmppIq::Result); sessionResult.setId(sessionSet.id()); sessionResult.setTo(jid()); sendPacket(sessionResult); return; } } // check the sender is legitimate const QString from = nodeRecv.attribute("from"); if (!from.isEmpty() && from != jid() && from != jidToBareJid(jid())) { warning(QString("Received a stanza from unexpected JID %1").arg(from)); return; } // process unhandled stanzas if (nodeRecv.tagName() == "iq" || nodeRecv.tagName() == "message" || nodeRecv.tagName() == "presence") { QDomElement nodeFull(nodeRecv); // if the sender is empty, set it to the appropriate JID if (nodeFull.attribute("from").isEmpty()) { if (nodeFull.tagName() == "presence" && (nodeFull.attribute("type") == "subscribe" || nodeFull.attribute("type") == "subscribed")) nodeFull.setAttribute("from", jidToBareJid(jid())); else nodeFull.setAttribute("from", jid()); } // if the recipient is empty, set it to the local domain if (nodeFull.attribute("to").isEmpty()) nodeFull.setAttribute("to", d->domain); // emit stanza for processing by server emit elementReceived(nodeFull); } } }