static void saveMessage(const QXmppMessage &message, const QDateTime &now, bool received) { const QString localJid = QXmppUtils::jidToBareJid(received ? message.to() : message.from()); const QString remoteJid = QXmppUtils::jidToBareJid(received ? message.from() : message.to()); // get or create collection int chatId; QDjangoQuerySet<ArchiveMessage> qs; qs = qs.filter(QDjangoWhere("chat__jid", QDjangoWhere::Equals, localJid)); qs = qs.filter(QDjangoWhere("chat__with", QDjangoWhere::Equals, remoteJid)); qs = qs.orderBy(QStringList() << "-date").limit(0, 1); ArchiveMessage tmp; if (qs.size() > 0 && qs.at(0, &tmp) && tmp.date().secsTo(now) < 3600) { chatId = tmp.property("chat_id").toInt(); } else { ArchiveChat chat; chat.setJid(localJid); chat.setWith(remoteJid); chat.setStart(now); chat.save(); chatId = chat.pk().toInt(); } // save outgoing message ArchiveMessage msg; msg.setProperty("chat_id", chatId); msg.setBody(message.body()); msg.setDate(now); msg.setReceived(received); msg.save(); }
void tst_QXmppMessage::testMessageReceipt() { const QByteArray xml( "<message id=\"richard2-4.1.247\" to=\"[email protected]/throne\" from=\"[email protected]/westminster\" type=\"normal\">" "<body>My lord, dispatch; read o'er these articles.</body>" "<request xmlns=\"urn:xmpp:receipts\"/>" "</message>"); QXmppMessage message; parsePacket(message, xml); QCOMPARE(message.id(), QString("richard2-4.1.247")); QCOMPARE(message.to(), QString("[email protected]/throne")); QCOMPARE(message.from(), QString("[email protected]/westminster")); QVERIFY(message.extendedAddresses().isEmpty()); QCOMPARE(message.type(), QXmppMessage::Normal); QCOMPARE(message.body(), QString("My lord, dispatch; read o'er these articles.")); QCOMPARE(message.isAttentionRequested(), false); QCOMPARE(message.isReceiptRequested(), true); QCOMPARE(message.receiptId(), QString()); serializePacket(message, xml); const QByteArray receiptXml( "<message id=\"bi29sg183b4v\" to=\"[email protected]/westminster\" from=\"[email protected]/throne\" type=\"normal\">" "<received xmlns=\"urn:xmpp:receipts\" id=\"richard2-4.1.247\"/>" "</message>"); QXmppMessage receipt; parsePacket(receipt, receiptXml); QCOMPARE(receipt.id(), QString("bi29sg183b4v")); QCOMPARE(receipt.to(), QString("[email protected]/westminster")); QCOMPARE(receipt.from(), QString("[email protected]/throne")); QVERIFY(receipt.extendedAddresses().isEmpty()); QCOMPARE(receipt.type(), QXmppMessage::Normal); QCOMPARE(receipt.body(), QString()); QCOMPARE(receipt.isAttentionRequested(), false); QCOMPARE(receipt.isReceiptRequested(), false); QCOMPARE(receipt.receiptId(), QString("richard2-4.1.247")); serializePacket(receipt, receiptXml); const QByteArray oldXml( "<message id=\"richard2-4.1.247\" to=\"[email protected]/westminster\" from=\"[email protected]/throne\" type=\"normal\">" "<received xmlns=\"urn:xmpp:receipts\"/>" "</message>"); QXmppMessage old; parsePacket(old, oldXml); QCOMPARE(old.id(), QString("richard2-4.1.247")); QCOMPARE(old.to(), QString("[email protected]/westminster")); QCOMPARE(old.from(), QString("[email protected]/throne")); QVERIFY(old.extendedAddresses().isEmpty()); QCOMPARE(old.type(), QXmppMessage::Normal); QCOMPARE(old.body(), QString()); QCOMPARE(old.isAttentionRequested(), false); QCOMPARE(old.isReceiptRequested(), false); QCOMPARE(old.receiptId(), QString("richard2-4.1.247")); }
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; }
void tst_QXmppMessage::testBasic() { QFETCH(QByteArray, xml); QFETCH(int, type); QFETCH(QString, body); QFETCH(QString, subject); QFETCH(QString, thread); QXmppMessage message; parsePacket(message, xml); QCOMPARE(message.to(), QString("[email protected]/QXmpp")); QCOMPARE(message.from(), QString("[email protected]/QXmpp")); QVERIFY(message.extendedAddresses().isEmpty()); QCOMPARE(int(message.type()), type); QCOMPARE(message.body(), body); QCOMPARE(message.subject(), subject); QCOMPARE(message.thread(), thread); QCOMPARE(message.state(), QXmppMessage::None); QCOMPARE(message.isAttentionRequested(), false); QCOMPARE(message.isReceiptRequested(), false); QCOMPARE(message.hasForwarded(), false); QCOMPARE(message.receiptId(), QString()); QCOMPARE(message.xhtml(), QString()); serializePacket(message, xml); }
void tst_QXmppMessage::testForwarding() { const QByteArray xml("<message type=\"normal\">" "<body>hi!</body>" "<forwarded xmlns=\"urn:xmpp:forward:0\">" "<delay xmlns=\"urn:xmpp:delay\" stamp=\"2010-06-29T08:23:06Z\"/>" "<message xmlns=\"jabber:client\" " "type=\"chat\" " "from=\"[email protected]/QXmpp\" " "to=\"[email protected]/QXmpp\">" "<body>ABC</body>" "</message>" "</forwarded>" "</message>"); QXmppMessage message; parsePacket(message, xml); QCOMPARE(message.hasForwarded(), true); QXmppMessage fwd = message.forwarded(); QCOMPARE(fwd.stamp(), QDateTime(QDate(2010, 06, 29), QTime(8, 23, 6), Qt::UTC)); QCOMPARE(fwd.body(), QString("ABC")); QCOMPARE(fwd.to(), QString("[email protected]/QXmpp")); QCOMPARE(fwd.from(), QString("[email protected]/QXmpp")); }
void CFrmGroupChat::slotMessageReceived(const QXmppMessage &message) { LOG_MODEL_DEBUG("Group chat", "CFrmGroupChat::slotMessageReceived:type:%d;state:%d;from:%s;to:%s;body:%s", message.type(), message.state(), //消息的状态 0:消息内容,其它值表示这个消息的状态 qPrintable(message.from()), qPrintable(message.to()), qPrintable(message.body()) ); if(QXmppUtils::jidToBareJid(message.from()) != QXmppUtils::jidToBareJid(m_pRoom->jid())) { LOG_MODEL_DEBUG("Group chat", "the room is %s, from %s received", m_pRoom->jid().toStdString().c_str(), message.from().toStdString().c_str()); return; } if(message.body().isEmpty()) return; QString nick; nick = QXmppUtils::jidToResource(message.from()); if(nick.isEmpty()) nick = tr("System"); AppendMessageToList(message.body(), nick); }
QXmppMessage JabberChatStateService::withSentChatState(QXmppMessage message) { auto jid = Jid::parse(message.to()); auto contact = ContactManager::instance()->byId(account(), jid.bare(), ActionCreateAndAdd); message.setState(QXmppMessage::State::Active); contact.addProperty("jabber:sent-chat-state", static_cast<int>(QXmppMessage::State::Active), CustomProperties::NonStorable); return message; }
void TestPackets::testMessage() { const QByteArray xml( "<message to=\"[email protected]/QXmpp\" from=\"[email protected]/QXmpp\" type=\"normal\"/>"); QXmppMessage message; parsePacket(message, xml); QCOMPARE(message.to(), QString("[email protected]/QXmpp")); QCOMPARE(message.from(), QString("[email protected]/QXmpp")); serializePacket(message, xml); }
void MyXmppClient::messageReceivedSlot( const QXmppMessage &xmppMsg ) { QString bareJid_from = MyXmppClient::getBareJidByJid( xmppMsg.from() ); QString bareJid_to = MyXmppClient::getBareJidByJid( xmppMsg.to() ); if( xmppMsg.state() == QXmppMessage::Active ) qDebug() << "Msg state is QXmppMessage::Active"; else if( xmppMsg.state() == QXmppMessage::Inactive ) qDebug() << "Msg state is QXmppMessage::Inactive"; else if( xmppMsg.state() == QXmppMessage::Gone ) qDebug() << "Msg state is QXmppMessage::Gone"; else if( xmppMsg.state() == QXmppMessage::Composing ) { if (bareJid_from != "") { m_flTyping = true; emit typingChanged(m_accountId,bareJid_from, true); qDebug() << bareJid_from << " is composing."; } } else if( xmppMsg.state() == QXmppMessage::Paused ) { if (bareJid_from != "") { m_flTyping = false; emit typingChanged(m_accountId,bareJid_from, false); qDebug() << bareJid_from << " paused."; } } else { if( xmppMsg.isAttentionRequested() ) { //qDebug() << "ZZZ: attentionRequest !!! from:" <<xmppMsg.from(); //msgWrapper->attention( bareJid_from, false ); } qDebug() << "MessageWrapper::messageReceived(): xmppMsg.state():" << xmppMsg.state(); } if ( !( xmppMsg.body().isEmpty() || xmppMsg.body().isNull() || bareJid_from == m_myjid ) ) { m_bareJidLastMessage = getBareJidByJid(xmppMsg.from()); m_resourceLastMessage = getResourceByJid(xmppMsg.from()); this->openChat( bareJid_from ); RosterItemModel *item = (RosterItemModel*)cachedRoster->find( bareJid_from ); if( item != 0 ) { int cnt = item->unreadMsg(); item->setUnreadMsg( ++cnt ); } else { RosterItemModel *itemModel = new RosterItemModel( ); itemModel->setPresence( this->getPicPresence( QXmppPresence::Unavailable ) ); itemModel->setContactName( bareJid_from ); itemModel->setJid( bareJid_from ); itemModel->setUnreadMsg( 1 ); itemModel->setStatusText( ""); cachedRoster->append(itemModel); itemModel = 0; delete itemModel; } item = 0; delete item; emit insertMessage(m_accountId,this->getBareJidByJid(xmppMsg.from()),xmppMsg.body(),QDateTime::currentDateTime().toString("dd-MM-yy hh:mm"),0); } }
void tst_QXmppMessage::testMessageAttention() { const QByteArray xml( "<message to=\"[email protected]/QXmpp\" from=\"[email protected]/QXmpp\" type=\"normal\">" "<attention xmlns=\"urn:xmpp:attention:0\"/>" "</message>"); QXmppMessage message; parsePacket(message, xml); QCOMPARE(message.to(), QString("[email protected]/QXmpp")); QCOMPARE(message.from(), QString("[email protected]/QXmpp")); QVERIFY(message.extendedAddresses().isEmpty()); QCOMPARE(message.type(), QXmppMessage::Normal); QCOMPARE(message.body(), QString()); QCOMPARE(message.isAttentionRequested(), true); QCOMPARE(message.isReceiptRequested(), false); QCOMPARE(message.receiptId(), QString()); serializePacket(message, xml); }
void TestPackets::testMessageFull() { const QByteArray xml( "<message to=\"[email protected]/QXmpp\" from=\"[email protected]/QXmpp\" type=\"normal\">" "<subject>test subject</subject>" "<body>test body & stuff</body>" "<thread>test thread</thread>" "<composing xmlns=\"http://jabber.org/protocol/chatstates\"/>" "</message>"); QXmppMessage message; parsePacket(message, xml); QCOMPARE(message.to(), QString("[email protected]/QXmpp")); QCOMPARE(message.from(), QString("[email protected]/QXmpp")); QCOMPARE(message.type(), QXmppMessage::Normal); QCOMPARE(message.body(), QString("test body & stuff")); QCOMPARE(message.subject(), QString("test subject")); QCOMPARE(message.thread(), QString("test thread")); QCOMPARE(message.state(), QXmppMessage::Composing); serializePacket(message, xml); }
void CFrmUserList::slotClientMessageReceived(const QXmppMessage &message) { LOG_MODEL_DEBUG("Roster", "CFrmUserList::slotClientMessageReceived:type:%d;state:%d;from:%s;to:%s;body:%s", message.type(), message.state(), //消息的状态 0:消息内容,其它值表示这个消息的状态 qPrintable(message.from()), qPrintable(message.to()), qPrintable(message.body()) ); m_LastUser = message.from();//保存接收到最后消息的用户 QMap<QString, CRoster*>::iterator it; it = m_Rosters.find(QXmppUtils::jidToBareJid(message.from())); if(m_Rosters.end() != it) { if(QXmppMessage::None == message.state()) { it.value()->AppendMessage(message.body()); } //TODO:消息输入状态显示 } }
void tst_QXmppMessage::testMessageCarbons() { const QByteArray xml("<message type=\"normal\">" "<body>hi!</body>" "<sent xmlns='urn:xmpp:carbons:2'>" "<forwarded xmlns=\"urn:xmpp:forward:0\">" "<delay xmlns=\"urn:xmpp:delay\" stamp=\"2010-06-29T08:23:06Z\"/>" "<message xmlns=\"jabber:client\" " "type=\"chat\" " "from=\"[email protected]/QXmpp\" " "to=\"[email protected]/QXmpp\">" "<body>ABC</body>" "</message>" "</forwarded>" "</sent>" "</message>"); QXmppMessage message; parsePacket(message, xml); QCOMPARE(message.hasMessageCarbon(), true); QXmppMessage fwd = message.carbonMessage(); QCOMPARE(fwd.stamp(), QDateTime(QDate(2010, 06, 29), QTime(8, 23, 6), Qt::UTC)); QCOMPARE(fwd.body(), QString("ABC")); QCOMPARE(fwd.to(), QString("[email protected]/QXmpp")); QCOMPARE(fwd.from(), QString("[email protected]/QXmpp")); const QByteArray carbonXml("<iq id=\"id1\"" " type=\"set\">" "<enable xmlns=\"urn:xmpp:carbons:2\"/>" "</iq>"); QXmppMessageCarbonsIq carbonIq; carbonIq.setId("id1"); serializePacket(carbonIq, carbonXml); }