initial functionality of mucs

This commit is contained in:
Blue 2019-08-28 14:40:55 +03:00
parent e2cc1bae2e
commit 023494de0b
23 changed files with 347 additions and 119 deletions

View file

@ -436,7 +436,7 @@ void Core::Account::onMessageReceived(const QXmppMessage& msg)
handled = handleChatMessage(msg);
break;
case QXmppMessage::GroupChat:
qDebug() << "received a message with type \"GroupChat\", not sure what to do with it now, skipping";
handled = handleGroupMessage(msg);
break;
case QXmppMessage::Error:
qDebug() << "received a message with type \"Error\", not sure what to do with it now, skipping";
@ -482,11 +482,25 @@ void Core::Account::sendMessage(const Shared::Message& data)
msg.setId(data.getId());
msg.setType(static_cast<QXmppMessage::Type>(data.getType())); //it is safe here, my type is compatible
RosterItem* ri = 0;
std::map<QString, Contact*>::const_iterator itr = contacts.find(data.getPenPalJid());
if (itr != contacts.end()) {
ri = itr->second;
} else {
std::map<QString, Conference*>::const_iterator ritr = conferences.find(data.getPenPalJid());
if (ritr != conferences.end()) {
ri = ritr->second;
}
}
itr->second->appendMessageToArchive(data);
if (ri != 0) {
if (!ri->isMuc()) {
ri->appendMessageToArchive(data);
}
}
client.sendPacket(msg);
} else {
qDebug() << "An attempt to send message with not connected account " << name << ", skipping";
}
@ -541,6 +555,39 @@ bool Core::Account::handleChatMessage(const QXmppMessage& msg, bool outgoing, bo
return false;
}
bool Core::Account::handleGroupMessage(const QXmppMessage& msg, bool outgoing, bool forwarded, bool guessing)
{
const QString& body(msg.body());
if (body.size() != 0) {
const QString& id(msg.id());
Shared::Message sMsg(Shared::Message::groupChat);
initializeMessage(sMsg, msg, outgoing, forwarded, guessing);
QString jid = sMsg.getPenPalJid();
std::map<QString, Conference*>::const_iterator itr = conferences.find(jid);
Conference* cnt;
if (itr != conferences.end()) {
cnt = itr->second;
} else {
return false;
}
cnt->appendMessageToArchive(sMsg);
emit message(sMsg);
if (!forwarded && !outgoing) {
if (msg.isReceiptRequested() && id.size() > 0) {
QXmppMessage receipt(getFullJid(), msg.from(), "");
receipt.setReceiptId(id);
client.sendPacket(receipt);
}
}
return true;
}
return false;
}
void Core::Account::initializeMessage(Shared::Message& target, const QXmppMessage& source, bool outgoing, bool forwarded, bool guessing) const
{
const QDateTime& time(source.stamp());
@ -595,17 +642,20 @@ void Core::Account::requestArchive(const QString& jid, int count, const QString&
qDebug() << "An archive request for " << jid << ", before " << before;
RosterItem* contact = 0;
std::map<QString, Contact*>::const_iterator itr = contacts.find(jid);
bool gr = false;
if (itr != contacts.end()) {
contact = itr->second;
} else {
std::map<QString, Conference*>::const_iterator citr = conferences.find(jid);
if (citr != conferences.end()) {
contact = citr->second;
gr = true;
}
}
if (contact == 0) {
qDebug() << "An attempt to request archive for" << jid << "in account" << name << ", but the contact with such id wasn't found, skipping";
emit responseArchive(contact->jid, std::list<Shared::Message>());
return;
}
@ -613,7 +663,11 @@ void Core::Account::requestArchive(const QString& jid, int count, const QString&
QXmppMessage msg(getFullJid(), jid, "", "");
QString last = Shared::generateUUID();
msg.setId(last);
msg.setType(QXmppMessage::Chat);
if (gr) {
msg.setType(QXmppMessage::GroupChat);
} else {
msg.setType(QXmppMessage::Chat);
}
msg.setState(QXmppMessage::Active);
client.sendPacket(msg);
QTimer* timer = new QTimer;
@ -632,6 +686,7 @@ void Core::Account::requestArchive(const QString& jid, int count, const QString&
void Core::Account::onContactNeedHistory(const QString& before, const QString& after, const QDateTime& at)
{
RosterItem* contact = static_cast<RosterItem*>(sender());
QString to = "";
QXmppResultSetQuery query;
query.setMax(100);
if (before.size() > 0) {
@ -648,7 +703,7 @@ void Core::Account::onContactNeedHistory(const QString& before, const QString& a
qDebug() << "Remote query from" << after << ", to" << before;
QString q = am->retrieveArchivedMessages("", "", contact->jid, start, QDateTime(), query);
QString q = am->retrieveArchivedMessages(to, "", contact->jid, start, QDateTime(), query);
achiveQueries.insert(std::make_pair(q, contact->jid));
}
@ -939,6 +994,7 @@ void Core::Account::bookmarksReceived(const QXmppBookmarkSet& bookmarks)
QXmppMucRoom* room = mm->addRoom(jid);
QString nick = c.nickName();
Conference* conf = new Conference(jid, getName(), c.autoJoin(), c.name(), nick == "" ? getName() : nick, room);
conferences.insert(std::make_pair(jid, conf));
handleNewConference(conf);

View file

@ -151,6 +151,7 @@ private:
void handleNewRosterItem(RosterItem* contact);
void handleNewConference(Conference* contact);
bool handleChatMessage(const QXmppMessage& msg, bool outgoing = false, bool forwarded = false, bool guessing = false);
bool handleGroupMessage(const QXmppMessage& msg, bool outgoing = false, bool forwarded = false, bool guessing = false);
void addToGroup(const QString& jid, const QString& group);
void removeFromGroup(const QString& jid, const QString& group);
void initializeMessage(Shared::Message& target, const QXmppMessage& source, bool outgoing = false, bool forwarded = false, bool guessing = false) const;

View file

@ -270,6 +270,7 @@ unsigned int Core::Archive::addElements(const std::list<Shared::Message>& messag
if (rc) {
qDebug() << "An element couldn't be inserted into the index, aborting the transaction" << mdb_strerror(rc);
} else {
//qDebug() << "element added with id" << message.getId() << "stamp" << message.getTime();
success++;
}
} else {
@ -321,9 +322,10 @@ std::list<Shared::Message> Core::Archive::getBefore(int count, const QString& id
rc = mdb_cursor_open(txn, order, &cursor);
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_LAST);
if (rc) {
qDebug() << "Error getting before " << mdb_strerror(rc) << ", id:" << id;
qDebug() << "Error getting before" << mdb_strerror(rc) << ", id:" << id;
mdb_cursor_close(cursor);
mdb_txn_abort(txn);
throw Empty(jid.toStdString());
}
} else {
@ -334,7 +336,6 @@ std::list<Shared::Message> Core::Archive::getBefore(int count, const QString& id
if (rc) {
qDebug() <<"Error getting before: no reference message" << mdb_strerror(rc) << ", id:" << id;
mdb_txn_abort(txn);
printKeys();
throw NotFound(stdId, jid.toStdString());
} else {
QByteArray ba((char*)lmdbData.mv_data, lmdbData.mv_size);

View file

@ -27,6 +27,7 @@ Core::Conference::Conference(const QString& p_jid, const QString& p_account, boo
joined(false),
autoJoin(p_autoJoin)
{
muc = true;
name = p_name;
connect(room, SIGNAL(joined()), this, SLOT(onRoomJoined()));

View file

@ -32,13 +32,15 @@ Core::RosterItem::RosterItem(const QString& pJid, const QString& account, QObjec
hisoryCache(),
appendCache(),
responseCache(),
requestCache()
requestCache(),
muc(false)
{
archive->open(account);
if (archive->isFromTheBeginning()) {
archiveState = beginning;
} else {
if (archive->size() != 0) {
if (archive->size() != 0) {
if (archive->isFromTheBeginning()) {
archiveState = beginning;
} else {
archiveState = chunk;
}
}
@ -231,7 +233,7 @@ void Core::RosterItem::flushMessagesToArchive(bool finished, const QString& firs
case beginning:
if (finished) {
archiveState = complete;
archive->addElements(appendCache);
added += archive->addElements(appendCache);
appendCache.clear();
nextRequest();
} else {
@ -241,7 +243,7 @@ void Core::RosterItem::flushMessagesToArchive(bool finished, const QString& firs
case chunk:
if (finished) {
archiveState = end;
archive->addElements(appendCache);
added += archive->addElements(appendCache);
appendCache.clear();
nextRequest();
} else {
@ -252,6 +254,8 @@ void Core::RosterItem::flushMessagesToArchive(bool finished, const QString& firs
wasEmpty = true;
archiveState = end;
case end:
added += archive->addElements(appendCache);
appendCache.clear();
if (finished && (added > 0 || !wasEmpty)) {
archiveState = complete;
archive->setFromTheBeginning(true);
@ -317,3 +321,13 @@ void Core::RosterItem::requestFromEmpty(int count, const QString& before)
}
}
QString Core::RosterItem::getServer() const
{
QStringList lst = jid.split("@");
return lst.back();
}
bool Core::RosterItem::isMuc() const
{
return muc;
}

View file

@ -50,6 +50,8 @@ public:
ArchiveState getArchiveState() const;
QString getName() const;
void setName(const QString& n);
QString getServer() const;
bool isMuc() const;
void addMessageToArchive(const Shared::Message& msg);
void appendMessageToArchive(const Shared::Message& msg);
@ -78,6 +80,7 @@ protected:
std::list<Shared::Message> appendCache;
std::list<Shared::Message> responseCache;
std::list<std::pair<int, QString>> requestCache;
bool muc;
private:
void nextRequest();