From 480c78cf61a46f69fa7b92d6fbcfa681d3ab811e Mon Sep 17 00:00:00 2001 From: blue Date: Sun, 26 Jul 2020 22:41:30 +0300 Subject: [PATCH] non lower cased jids error handled --- CHANGELOG.md | 2 + core/account.cpp | 4 +- core/handlers/rosterhandler.cpp | 88 ++++++++++++++++++--------------- shared/message.cpp | 12 ++--- 4 files changed, 59 insertions(+), 47 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d61aaf2..e1f9543 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ - error with building on lower versions of QXmpp - error on the first access to the conference database - quit now actually quits the app +- history in MUC now works properly and requests messages from server +- error with handling non lower cased JIDs ## Squawk 0.1.4 (Apr 14, 2020) diff --git a/core/account.cpp b/core/account.cpp index 5ed8873..0c8c272 100644 --- a/core/account.cpp +++ b/core/account.cpp @@ -321,7 +321,7 @@ void Core::Account::onPresenceReceived(const QXmppPresence& p_presence) { QString id = p_presence.from(); QStringList comps = id.split("/"); - QString jid = comps.front(); + QString jid = comps.front().toLower(); QString resource = comps.back(); QString myJid = getLogin() + "@" + getServer(); @@ -682,7 +682,7 @@ void Core::Account::onVCardReceived(const QXmppVCardIq& card) { QString id = card.from(); QStringList comps = id.split("/"); - QString jid = comps.front(); + QString jid = comps.front().toLower(); QString resource(""); if (comps.size() > 1) { resource = comps.back(); diff --git a/core/handlers/rosterhandler.cpp b/core/handlers/rosterhandler.cpp index 44b2bda..82ca8c3 100644 --- a/core/handlers/rosterhandler.cpp +++ b/core/handlers/rosterhandler.cpp @@ -57,16 +57,17 @@ void Core::RosterHandler::onRosterReceived() QStringList bj = acc->rm->getRosterBareJids(); for (int i = 0; i < bj.size(); ++i) { const QString& jid = bj[i]; - addedAccount(jid); + addedAccount(jid.toLower()); } } void Core::RosterHandler::onRosterItemAdded(const QString& bareJid) { - addedAccount(bareJid); - std::map::const_iterator itr = queuedContacts.find(bareJid); + QString lcJid = bareJid.toLower(); + addedAccount(lcJid); + std::map::const_iterator itr = queuedContacts.find(lcJid); if (itr != queuedContacts.end()) { - acc->rm->subscribe(bareJid, itr->second); + acc->rm->subscribe(lcJid, itr->second); queuedContacts.erase(itr); } } @@ -172,16 +173,17 @@ void Core::RosterHandler::addContactRequest(const QString& jid, const QString& n void Core::RosterHandler::removeContactRequest(const QString& jid) { + QString lcJid = jid.toLower(); if (acc->state == Shared::ConnectionState::connected) { - std::set::const_iterator itr = outOfRosterContacts.find(jid); + std::set::const_iterator itr = outOfRosterContacts.find(lcJid); if (itr != outOfRosterContacts.end()) { outOfRosterContacts.erase(itr); - onRosterItemRemoved(jid); + onRosterItemRemoved(lcJid); } else { - acc->rm->removeItem(jid); + acc->rm->removeItem(lcJid); } } else { - qDebug() << "An attempt to remove contact " << jid << " from account " << acc->name << " but the account is not in the connected state, skipping"; + qDebug() << "An attempt to remove contact " << lcJid << " from account " << acc->name << " but the account is not in the connected state, skipping"; } } @@ -291,7 +293,7 @@ void Core::RosterHandler::addToGroup(const QString& jid, const QString& group) gItr = groups.insert(std::make_pair(group, std::set())).first; emit acc->addGroup(group); } - gItr->second.insert(jid); + gItr->second.insert(jid.toLower()); } void Core::RosterHandler::removeFromGroup(const QString& jid, const QString& group) @@ -303,7 +305,7 @@ void Core::RosterHandler::removeFromGroup(const QString& jid, const QString& gro return; } std::set contacts = itr->second; - std::set::const_iterator cItr = contacts.find(jid); + std::set::const_iterator cItr = contacts.find(jid.toLower()); if (cItr != contacts.end()) { contacts.erase(cItr); if (contacts.size() == 0) { @@ -324,11 +326,12 @@ void Core::RosterHandler::onContactHistoryResponse(const std::list::const_iterator citr = contacts.find(jid); + QString lcJid = jid.toLower(); + std::map::const_iterator citr = contacts.find(lcJid); if (citr != contacts.end()) { item = citr->second; } else { - std::map::const_iterator coitr = conferences.find(jid); + std::map::const_iterator coitr = conferences.find(lcJid); if (coitr != conferences.end()) { item = coitr->second; } @@ -339,7 +342,7 @@ Core::RosterItem * Core::RosterHandler::getRosterItem(const QString& jid) Core::Conference * Core::RosterHandler::getConference(const QString& jid) { Conference* item = 0; - std::map::const_iterator coitr = conferences.find(jid); + std::map::const_iterator coitr = conferences.find(jid.toLower()); if (coitr != conferences.end()) { item = coitr->second; } @@ -349,7 +352,7 @@ Core::Conference * Core::RosterHandler::getConference(const QString& jid) Core::Contact * Core::RosterHandler::getContact(const QString& jid) { Contact* item = 0; - std::map::const_iterator citr = contacts.find(jid); + std::map::const_iterator citr = contacts.find(jid.toLower()); if (citr != contacts.end()) { item = citr->second; } @@ -358,11 +361,12 @@ Core::Contact * Core::RosterHandler::getContact(const QString& jid) Core::Contact * Core::RosterHandler::addOutOfRosterContact(const QString& jid) { - Contact* cnt = new Contact(jid, acc->name); - contacts.insert(std::make_pair(jid, cnt)); - outOfRosterContacts.insert(jid); + QString lcJid = jid.toLower(); + Contact* cnt = new Contact(lcJid, acc->name); + contacts.insert(std::make_pair(lcJid, cnt)); + outOfRosterContacts.insert(lcJid); cnt->setSubscriptionState(Shared::SubscriptionState::unknown); - emit acc->addContact(jid, "", QMap({ + emit acc->addContact(lcJid, "", QMap({ {"state", QVariant::fromValue(Shared::SubscriptionState::unknown)} })); handleNewContact(cnt); @@ -371,13 +375,14 @@ Core::Contact * Core::RosterHandler::addOutOfRosterContact(const QString& jid) void Core::RosterHandler::onRosterItemChanged(const QString& bareJid) { - std::map::const_iterator itr = contacts.find(bareJid); + QString lcJid = bareJid.toLower(); + std::map::const_iterator itr = contacts.find(lcJid); if (itr == contacts.end()) { - qDebug() << "An attempt to change non existing contact" << bareJid << "from account" << acc->name << ", skipping"; + qDebug() << "An attempt to change non existing contact" << lcJid << "from account" << acc->name << ", skipping"; return; } Contact* contact = itr->second; - QXmppRosterIq::Item re = acc->rm->getRosterEntry(bareJid); + QXmppRosterIq::Item re = acc->rm->getRosterEntry(lcJid); Shared::SubscriptionState state = castSubscriptionState(re.subscriptionType()); @@ -388,18 +393,19 @@ void Core::RosterHandler::onRosterItemChanged(const QString& bareJid) void Core::RosterHandler::onRosterItemRemoved(const QString& bareJid) { - std::map::const_iterator itr = contacts.find(bareJid); + QString lcJid = bareJid.toLower(); + std::map::const_iterator itr = contacts.find(lcJid); if (itr == contacts.end()) { - qDebug() << "An attempt to remove non existing contact" << bareJid << "from account" << acc->name << ", skipping"; + qDebug() << "An attempt to remove non existing contact" << lcJid << "from account" << acc->name << ", skipping"; return; } Contact* contact = itr->second; contacts.erase(itr); QSet cGroups = contact->getGroups(); for (QSet::const_iterator itr = cGroups.begin(), end = cGroups.end(); itr != end; ++itr) { - removeFromGroup(bareJid, *itr); + removeFromGroup(lcJid, *itr); } - emit acc->removeContact(bareJid); + emit acc->removeContact(lcJid); contact->deleteLater(); } @@ -416,7 +422,7 @@ void Core::RosterHandler::bookmarksReceived(const QXmppBookmarkSet& bookmarks) for (QList::const_iterator itr = confs.begin(), end = confs.end(); itr != end; ++itr) { const QXmppBookmarkConference& c = *itr; - QString jid = c.jid(); + QString jid = c.jid().toLower(); std::map::const_iterator cItr = conferences.find(jid); if (cItr == conferences.end()) { addNewRoom(jid, c.nickName(), c.name(), c.autoJoin()); @@ -495,35 +501,38 @@ void Core::RosterHandler::clearConferences() void Core::RosterHandler::removeRoomRequest(const QString& jid) { - std::map::const_iterator itr = conferences.find(jid); + QString lcJid = jid.toLower(); + std::map::const_iterator itr = conferences.find(lcJid); if (itr == conferences.end()) { - qDebug() << "An attempt to remove non existing room" << jid << "from account" << acc->name << ", skipping"; + qDebug() << "An attempt to remove non existing room" << lcJid << "from account" << acc->name << ", skipping"; } itr->second->deleteLater(); conferences.erase(itr); - emit acc->removeRoom(jid); + emit acc->removeRoom(lcJid); storeConferences(); } void Core::RosterHandler::addRoomRequest(const QString& jid, const QString& nick, const QString& password, bool autoJoin) { - std::map::const_iterator cItr = conferences.find(jid); + QString lcJid = jid.toLower(); + std::map::const_iterator cItr = conferences.find(lcJid); if (cItr == conferences.end()) { - addNewRoom(jid, nick, "", autoJoin); + addNewRoom(lcJid, nick, "", autoJoin); storeConferences(); } else { - qDebug() << "An attempt to add a MUC " << jid << " which is already present in the rester, skipping"; + qDebug() << "An attempt to add a MUC " << lcJid << " which is already present in the rester, skipping"; } } void Core::RosterHandler::addContactToGroupRequest(const QString& jid, const QString& groupName) { - std::map::const_iterator itr = contacts.find(jid); + QString lcJid = jid.toLower(); + std::map::const_iterator itr = contacts.find(lcJid); if (itr == contacts.end()) { - qDebug() << "An attempt to add non existing contact" << jid << "of account" + qDebug() << "An attempt to add non existing contact" << lcJid << "of account" << acc->name << "to the group" << groupName << ", skipping"; } else { - QXmppRosterIq::Item item = acc->rm->getRosterEntry(jid); + QXmppRosterIq::Item item = acc->rm->getRosterEntry(lcJid); QSet groups = item.groups(); if (groups.find(groupName) == groups.end()) { //TODO need to change it, I guess that sort of code is better in qxmpp lib groups.insert(groupName); @@ -542,12 +551,13 @@ void Core::RosterHandler::addContactToGroupRequest(const QString& jid, const QSt void Core::RosterHandler::removeContactFromGroupRequest(const QString& jid, const QString& groupName) { - std::map::const_iterator itr = contacts.find(jid); + QString lcJid = jid.toLower(); + std::map::const_iterator itr = contacts.find(lcJid); if (itr == contacts.end()) { - qDebug() << "An attempt to remove non existing contact" << jid << "of account" + qDebug() << "An attempt to remove non existing contact" << lcJid << "of account" << acc->name << "from the group" << groupName << ", skipping"; } else { - QXmppRosterIq::Item item = acc->rm->getRosterEntry(jid); + QXmppRosterIq::Item item = acc->rm->getRosterEntry(lcJid); QSet groups = item.groups(); QSet::const_iterator gItr = groups.find(groupName); if (gItr != groups.end()) { @@ -559,7 +569,7 @@ void Core::RosterHandler::removeContactFromGroupRequest(const QString& jid, cons iq.addItem(item); acc->client.sendPacket(iq); } else { - qDebug() << "An attempt to remove contact" << jid << "of account" + qDebug() << "An attempt to remove contact" << lcJid << "of account" << acc->name << "from the group" << groupName << "but it's not in that group, skipping"; } } diff --git a/shared/message.cpp b/shared/message.cpp index fad10dc..af4f9e0 100644 --- a/shared/message.cpp +++ b/shared/message.cpp @@ -105,9 +105,9 @@ void Shared::Message::setFrom(const QString& from) { QStringList list = from.split("/"); if (list.size() == 1) { - jFrom = from; + jFrom = from.toLower(); } else { - jFrom = list.front(); + jFrom = list.front().toLower(); rFrom = list.back(); } } @@ -116,9 +116,9 @@ void Shared::Message::setTo(const QString& to) { QStringList list = to.split("/"); if (list.size() == 1) { - jTo = to; + jTo = to.toLower(); } else { - jTo = list.front(); + jTo = list.front().toLower(); rTo = list.back(); } } @@ -188,7 +188,7 @@ bool Shared::Message::getEdited() const void Shared::Message::setFromJid(const QString& from) { - jFrom = from; + jFrom = from.toLower(); } void Shared::Message::setFromResource(const QString& from) @@ -198,7 +198,7 @@ void Shared::Message::setFromResource(const QString& from) void Shared::Message::setToJid(const QString& to) { - jTo = to; + jTo = to.toLower(); } void Shared::Message::setToResource(const QString& to)