bool MyXmppClient::sendMyMessage(QString bareJid, QString resource, QString msgBody) //Q_INVOKABLE
{
    if (msgBody == "" || m_stateConnect != Connected) return false; // if message is empty or user not connected - BREAK

    QXmppMessage xmppMsg;

    QString jid_from = bareJid;
    if( resource == "" ) jid_from += "/resource"; else jid_from += "/" + resource;

    xmppMsg.setTo( jid_from );
    QString jid_to = m_myjid + "/" + xmppClient->configuration().resource();
    xmppMsg.setFrom( jid_to );

    xmppMsg.setBody( msgBody );

    xmppMsg.setState( QXmppMessage::Active );

    xmppClient->sendPacket( xmppMsg );

    this->messageReceivedSlot( xmppMsg );

    emit insertMessage(m_accountId,this->getBareJidByJid(xmppMsg.to()),msgBody,QDateTime::currentDateTime().toString("dd-MM-yy hh:mm"),1);

    return true;
}
Exemple #2
0
void tst_QXmppMessage::testReplaceWithEmptyMessage()
{
    const QByteArray replaceXml(
                "<message to='[email protected]/balcony' id='good1'>"
                  "<body/>"
                  "<replace id='bad1' xmlns='urn:xmpp:message-correct:0'/>"
                "</message>");
    QXmppMessage replaceMessage;
    parsePacket(replaceMessage, replaceXml);
    QCOMPARE(replaceMessage.isReplace(), true);
    QCOMPARE(replaceMessage.replaceId(), QString("bad1"));
    QCOMPARE(replaceMessage.body(), QString(""));

    const QByteArray replaceSerialisation(
                "<message id=\"good1\" to=\"[email protected]/balcony\" type=\"chat\">"
                  "<body/>"
                  "<replace id=\"bad1\" xmlns=\"urn:xmpp:message-correct:0\"/>"
                "</message>");

    QXmppMessage serialisationMessage;
    serialisationMessage.setTo("[email protected]/balcony");
    serialisationMessage.setId("good1");
    serialisationMessage.setBody("");
    serialisationMessage.setReplace("bad1");

    serializePacket(serialisationMessage, replaceSerialisation);
}
Exemple #3
0
bool Conversation::sendMessage(const QString &body)
{
    if (body.isEmpty() || m_jid.isEmpty() || !m_client || !m_client->isConnected())
        return false;

    // send message
    QXmppMessage message;
    message.setTo(m_jid);
    message.setBody(body);
    message.setState(QXmppMessage::Active);
    if (!m_client->sendPacket(message))
        return false;

    // update state
    if (m_localState != QXmppMessage::Active) {
        m_localState = QXmppMessage::Active;
        emit localStateChanged(m_localState);
    }

    // add message to history
    if (m_historyModel) {
        HistoryMessage message;
        message.body = body;
        message.date = m_client->serverTime();
        message.jid = m_client->configuration().jidBare();
        message.received = false;
        m_historyModel->addMessage(message);
    }

    return true;
}
Exemple #4
0
bool ChatWindow::eventFilter(QObject* sender, QEvent* event) {
    if (sender == ui->messagePlainTextEdit) {
        if (event->type() != QEvent::KeyPress) {
            return false;
        }
        QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
        if ((keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter) &&
            (keyEvent->modifiers() & Qt::ShiftModifier) == 0) {
            QString messageText = ui->messagePlainTextEdit->document()->toPlainText().trimmed();
            if (!messageText.isEmpty()) {
#ifdef HAVE_QXMPP
                const QXmppMucRoom* publicChatRoom = XmppClient::getInstance().getPublicChatRoom();
                QXmppMessage message;
                message.setTo(publicChatRoom->jid());
                message.setType(QXmppMessage::GroupChat);
                message.setBody(messageText);
                XmppClient::getInstance().getXMPPClient().sendPacket(message);
#endif // HAVE_QXMPP
                QTextCursor cursor = ui->messagePlainTextEdit->textCursor();
                cursor.select(QTextCursor::Document);
                cursor.removeSelectedText();
            }
            return true;
        }
    } else if (event->type() == QEvent::MouseButtonRelease) {
        QVariant userVar = sender->property("user");
        if (userVar.isValid()) {
            AddressManager::getInstance().goToUser(userVar.toString());
            return true;
        }
    }
    return QWidget::eventFilter(sender, event);
}
bool QXmppMessageReceiptManager::handleStanza(const QDomElement &stanza)
{
    if (stanza.tagName() != "message")
        return false;

    QXmppMessage message;
    message.parse(stanza);

    // Handle receipts and cancel any further processing.
    if (!message.receiptId().isEmpty()) {
        Q_EMIT messageDelivered(message.from(), message.receiptId());
        return true;
    }

    // If requested, send a receipt.
    if (message.isReceiptRequested()
        && !message.from().isEmpty()
        && !message.id().isEmpty()) {
        QXmppMessage receipt;
        receipt.setTo(message.from());
        receipt.setReceiptId(message.id());
        client()->sendPacket(receipt);
    }

    // Continue processing.
    return false;
}
bool XmppServerArchive::handleStanza(const QDomElement &element)
{
    const QString domain = server()->domain();
    const QString from = element.attribute("from");
    const QString to = element.attribute("to");

    if (element.tagName() == "message" &&
        to != domain &&
        (QXmppUtils::jidToDomain(from) == domain || QXmppUtils::jidToDomain(to) == domain) &&
        element.attribute("type") != "error" &&
        element.attribute("type") != "groupchat" &&
        element.attribute("type") != "headline" &&
        !element.firstChildElement("body").text().isEmpty())
    {
        const QDateTime now = QDateTime::currentDateTime().toUTC();
        QXmppMessage message;
        message.parse(element);

        if (QXmppUtils::jidToDomain(from) == domain)
            saveMessage(message, now, false);

        if (QXmppUtils::jidToDomain(to) == domain) {
            saveMessage(message, now, true);

            // offline messages
            bool found = false;

            XmppServerPresence *presenceExtension = XmppServerPresence::instance(server());
            Q_ASSERT(presenceExtension);
            foreach (const QXmppPresence &presence, presenceExtension->availablePresences(QXmppUtils::jidToBareJid(to))) {
                if (QXmppUtils::jidToResource(to).isEmpty() || presence.from() == to) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                message.setStamp(now);
                message.setState(QXmppMessage::None);
                message.setTo(QXmppUtils::jidToBareJid(to));

                QBuffer buffer;
                buffer.open(QIODevice::WriteOnly);
                QXmlStreamWriter writer(&buffer);
                message.toXml(&writer);

                OfflineMessage offline;
                offline.setData(QString::fromUtf8(buffer.data()));
                offline.setJid(QXmppUtils::jidToBareJid(to));
                offline.setStamp(now);
                offline.save();
                return true;
            }
        }

        return false;

    } else if (element.tagName() == "presence" &&
Exemple #7
0
	void EntryBase::DrawAttention (const QString& text, const QString& variant)
	{
		const QString& to = variant.isEmpty () ?
				GetJID () :
				GetJID () + '/' + variant;
		QXmppMessage msg;
		msg.setBody (text);
		msg.setTo (to);
		msg.setType (QXmppMessage::Headline);
		msg.setAttentionRequested (true);
		Account_->GetClientConnection ()->GetClient ()->sendPacket (msg);
	}
void QXmppDeliveryReceiptsManager::sendReceipt(const QString &jid, const QString &id)
{
    QXmppMessage msg;
    msg.setTo(jid);

    QXmppElement elem;
    elem.setTagName("received");
    elem.setAttribute("xmlns", ns_message_receipts);
    elem.setAttribute("id", id);

    msg.setExtensions(QXmppElementList(elem));

    client()->sendPacket(msg);
}
Exemple #9
0
void Conversation::setLocalState(int state)
{
    if (state != m_localState) {
        m_localState = static_cast<QXmppMessage::State>(state);

        // notify state change
        if (m_client) {
            QXmppMessage message;
            message.setTo(m_jid);
            message.setState(m_localState);
            m_client->sendPacket(message);
        }

        emit localStateChanged(m_localState);
    }
}
/* it sends information about typing : typing is stoped */
void MyXmppClient::typingStop(QString bareJid, QString resource) {
    qDebug() << bareJid << " " << "stop typing...";
    QXmppMessage xmppMsg;

    QString jid_to = bareJid;
    if( resource == "" ) jid_to += "/resource"; else jid_to += "/" + resource;
    xmppMsg.setTo( jid_to );

    QString jid_from = m_myjid + "/" + xmppClient->configuration().resource();
    xmppMsg.setFrom( jid_from );

    xmppMsg.setReceiptRequested( false );

    QDateTime currTime = QDateTime::currentDateTime();
    xmppMsg.setStamp( currTime );

    xmppMsg.setState( QXmppMessage::Paused );

    xmppClient->sendPacket( xmppMsg );
}
void MyXmppClient::attentionSend( QString bareJid, QString resource ) {
    qDebug() << "MyXmppClient::attentionSend(" << bareJid << ";" << resource << ")";
    QXmppMessage xmppMsg;

    QString jid_to = bareJid;
    if( resource == "" ) {
        jid_to += "/resource";
    } else {
        jid_to += "/" + resource;
    }
    xmppMsg.setTo( jid_to );

    QString jid_from = m_myjid + "/" + xmppClient->configuration().resource();
    xmppMsg.setFrom( jid_from );

    xmppMsg.setReceiptRequested( false );

    xmppMsg.setState( QXmppMessage::None );
    xmppMsg.setType( QXmppMessage::Headline );
    xmppMsg.setAttentionRequested( true );

    xmppClient->sendPacket( xmppMsg );
}
Exemple #12
0
void tst_QXmppMessage::testChatMarkers()
{
    const QByteArray markableXml(
                "<message "
                    "from='[email protected]/westminster' "
                    "id='message-1' "
                    "to='[email protected]/throne'>"
                 "<thread>sleeping</thread>"
                 "<body>My lord, dispatch; read o'er these articles.</body>"
                 "<markable xmlns='urn:xmpp:chat-markers:0'/>"
                "</message>");

    QXmppMessage markableMessage;
    parsePacket(markableMessage, markableXml);
    QCOMPARE(markableMessage.isMarkable(), true);
    QCOMPARE(markableMessage.marker(), QXmppMessage::NoMarker);
    QCOMPARE(markableMessage.id(), QString("message-1"));
    QCOMPARE(markableMessage.markedId(), QString());
    QCOMPARE(markableMessage.thread(), QString("sleeping"));
    QCOMPARE(markableMessage.markedThread(), QString());

    const QByteArray receivedXml(
                "<message "
                    "from='[email protected]/throne' "
                    "id='message-2' "
                    "to='[email protected]/westminster'>"
                  "<received xmlns='urn:xmpp:chat-markers:0' "
                               "id='message-1' "
                           "thread='sleeping'/>"
                "</message>");

    QXmppMessage receivedMessage;
    parsePacket(receivedMessage, receivedXml);
    QCOMPARE(receivedMessage.isMarkable(), false);
    QCOMPARE(receivedMessage.marker(), QXmppMessage::Received);
    QCOMPARE(receivedMessage.id(), QString("message-2"));
    QCOMPARE(receivedMessage.markedId(), QString("message-1"));
    QCOMPARE(receivedMessage.thread(), QString());
    QCOMPARE(receivedMessage.markedThread(), QString("sleeping"));

    const QByteArray displayedXml(
                "<message "
                    "from='[email protected]/throne' "
                    "id='message-2' "
                    "to='[email protected]/westminster'>"
                  "<displayed xmlns='urn:xmpp:chat-markers:0' "
                               "id='message-1' "
                           "thread='sleeping'/>"
                "</message>");

    QXmppMessage displayedMessage;
    parsePacket(displayedMessage, displayedXml);
    QCOMPARE(displayedMessage.isMarkable(), false);
    QCOMPARE(displayedMessage.marker(), QXmppMessage::Displayed);
    QCOMPARE(displayedMessage.id(), QString("message-2"));
    QCOMPARE(displayedMessage.markedId(), QString("message-1"));
    QCOMPARE(displayedMessage.thread(), QString());
    QCOMPARE(displayedMessage.markedThread(), QString("sleeping"));

    const QByteArray acknowledgedXml(
                "<message "
                    "from='[email protected]/throne' "
                    "id='message-2' "
                    "to='[email protected]/westminster'>"
                  "<acknowledged xmlns='urn:xmpp:chat-markers:0' "
                               "id='message-1' "
                           "thread='sleeping'/>"
                "</message>");

    QXmppMessage acknowledgedMessage;
    parsePacket(acknowledgedMessage, acknowledgedXml);
    QCOMPARE(acknowledgedMessage.isMarkable(), false);
    QCOMPARE(acknowledgedMessage.marker(), QXmppMessage::Acknowledged);
    QCOMPARE(acknowledgedMessage.id(), QString("message-2"));
    QCOMPARE(acknowledgedMessage.markedId(), QString("message-1"));
    QCOMPARE(acknowledgedMessage.thread(), QString());
    QCOMPARE(acknowledgedMessage.markedThread(), QString("sleeping"));

    const QByteArray emptyThreadXml(
                "<message "
                    "from='[email protected]/throne' "
                    "id='message-2' "
                    "to='[email protected]/westminster'>"
                  "<received xmlns='urn:xmpp:chat-markers:0' "
                               "id='message-1'/>"
                "</message>");

    QXmppMessage emptyThreadMessage;
    parsePacket(emptyThreadMessage, emptyThreadXml);
    QCOMPARE(emptyThreadMessage.isMarkable(), false);
    QCOMPARE(emptyThreadMessage.marker(), QXmppMessage::Received);
    QCOMPARE(emptyThreadMessage.id(), QString("message-2"));
    QCOMPARE(emptyThreadMessage.markedId(), QString("message-1"));
    QCOMPARE(emptyThreadMessage.thread(), QString());
    QCOMPARE(emptyThreadMessage.markedThread(), QString());

    const QByteArray notMarkableSerialisation(
                "<message "
                    "id=\"message-3\" "
                    "to=\"[email protected]/westminster\" "
                    "from=\"[email protected]/throne\" "
                    "type=\"chat\"/>");

    QXmppMessage serialisationMessage;
    serialisationMessage.setFrom("[email protected]/throne");
    serialisationMessage.setTo("[email protected]/westminster");
    serialisationMessage.setId("message-3");
    serialisationMessage.setMarkable(false);
    serializePacket(serialisationMessage, notMarkableSerialisation);

    const QByteArray markableSerialisation(
                "<message "
                    "id=\"message-3\" "
                    "to=\"[email protected]/westminster\" "
                    "from=\"[email protected]/throne\" "
                    "type=\"chat\">"
                    "<markable xmlns=\"urn:xmpp:chat-markers:0\"/>"
                "</message>");

    serialisationMessage.setMarkable(true);
    serializePacket(serialisationMessage, markableSerialisation);

    const QByteArray receivedSerialisation(
                "<message "
                    "id=\"message-3\" "
                    "to=\"[email protected]/westminster\" "
                    "from=\"[email protected]/throne\" "
                    "type=\"chat\">"
                    "<received xmlns=\"urn:xmpp:chat-markers:0\" "
                               "id=\"message-2\"/>"
                "</message>");

    serialisationMessage.setMarkable(false);
    serialisationMessage.setMarker(QXmppMessage::Received, "message-2");
    serializePacket(serialisationMessage, receivedSerialisation);

    const QByteArray receivedThreadSerialisation(
                "<message "
                    "id=\"message-3\" "
                    "to=\"[email protected]/westminster\" "
                    "from=\"[email protected]/throne\" "
                    "type=\"chat\">"
                    "<received xmlns=\"urn:xmpp:chat-markers:0\" "
                               "id=\"message-2\" "
                               "thread=\"sleeping\"/>"
                "</message>");

    serialisationMessage.setMarker(QXmppMessage::Received, "message-2", "sleeping");
    serializePacket(serialisationMessage, receivedThreadSerialisation);

    const QByteArray displayedThreadSerialisation(
                "<message "
                    "id=\"message-3\" "
                    "to=\"[email protected]/westminster\" "
                    "from=\"[email protected]/throne\" "
                    "type=\"chat\">"
                    "<displayed xmlns=\"urn:xmpp:chat-markers:0\" "
                               "id=\"message-2\" "
                               "thread=\"sleeping\"/>"
                "</message>");

    serialisationMessage.setMarker(QXmppMessage::Displayed, "message-2", "sleeping");
    serializePacket(serialisationMessage, displayedThreadSerialisation);

    const QByteArray acknowledgedThreadSerialisation(
                "<message "
                    "id=\"message-3\" "
                    "to=\"[email protected]/westminster\" "
                    "from=\"[email protected]/throne\" "
                    "type=\"chat\">"
                    "<acknowledged xmlns=\"urn:xmpp:chat-markers:0\" "
                               "id=\"message-2\" "
                               "thread=\"sleeping\"/>"
                "</message>");

    serialisationMessage.setMarker(QXmppMessage::Acknowledged, "message-2", "sleeping");
    serializePacket(serialisationMessage, acknowledgedThreadSerialisation);
}
    bool XMPPChatMarkerCommand::executeCommand(QStringList * const arguments)
    {
        bool ret = false;
        if (arguments->length() not_eq 2)
        {
            this->client->write(tr("ERROR: xmppChatMarker <resource> <marker>") + "\r\n");
        }
        else
        {
            const QString resource = arguments->first();
            QXmppClient * const client =
                    XMPPResourceStore::instance()->getFromStore(resource);
            arguments->removeFirst();
            if (client)
            {
                QSharedPointer<QXmppStanza> lastStanza
                        = XMPPResourceStore::instance()->getLastMessageReceived(
                            client);
                if (lastStanza)
                {
                    const QSharedPointer<QXmppMessage> lastMsg
                            = lastStanza.dynamicCast<QXmppMessage>();
                    if (lastMsg and lastMsg->isMarkable())
                    {
                        bool markerOk = true;
                        QXmppMessage::Marker msgMarker;
                        const QString marker = arguments->first();
                        arguments->removeFirst();

                        if (marker == "received")
                        {
                            msgMarker = QXmppMessage::Received;
                        }
                        else if (marker == "displayed")
                        {
                            msgMarker = QXmppMessage::Displayed;
                        }
                        else if (marker == "acknowledged")
                        {
                            msgMarker = QXmppMessage::Acknowledged;
                        }
                        else
                        {
                            markerOk = false;
                        }

                        if (markerOk)
                        {
                            QXmppMessage markerMessage;
                            markerMessage.setId(QUuid::createUuid().toString());
                            markerMessage.setThread("");
                            markerMessage.setMarkable(false);
                            markerMessage.setTo(lastMsg->from());
                            markerMessage.setMarker(msgMarker, lastMsg->id());
                            markerMessage.addHint(QXmppMessage::AllowPermantStorage);
                            if (XMPPDebugCommand::isDebugEnabled())
                            {
                                XMPPPrintCommand::printMessage(
                                            true,
                                            &markerMessage);
                            }
                            ret = client->sendPacket(markerMessage);
                            if (not ret)
                            {
                                this->client->write(tr("ERROR: Failed to send packet") + "\r\n");
                            }
                            else
                            {
                                XMPPResourceStore::instance()->setLastMessageSent(
                                            client,
                                            markerMessage);
                            }
                        }
                        else
                        {
                            this->client->write(tr("ERROR: Invalid marker") + "\r\n");
                        }
                    }
                    else
                    {
                        this->client->write(tr("ERROR: The last message isn't Markable") + "\r\n");
                    }
                }
                else
                {
                    this->client->write(tr("ERROR: Can't find the last message received to mark")
                                        + "\r\n");
                }
            }
            else
            {
                this->client->write(tr("ERROR: Unknown resource") + "\r\n");
            }
        }
        return ret;
    }