1
0
forked from blue/squawk
squawk/core/handlers/discoveryhandler.cpp

136 lines
5.5 KiB
C++
Raw Normal View History

// Squawk messenger.
// Copyright (C) 2019 Yury Gubich <blue@macaw.me>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "discoveryhandler.h"
#include "core/account.h"
#include <QDebug>
Core::DiscoveryHandler::DiscoveryHandler(Core::Account* account):
QObject(),
acc(account)
{
QObject::connect(acc->dm, &QXmppDiscoveryManager::itemsReceived, this, &DiscoveryHandler::onItemsReceived);
QObject::connect(acc->dm, &QXmppDiscoveryManager::infoReceived, this, &DiscoveryHandler::onInfoReceived);
acc->dm->setClientType("pc");
acc->dm->setClientCategory("client");
acc->dm->setClientName(qApp->applicationDisplayName() + " " + qApp->applicationVersion());
acc->dm->setClientCapabilitiesNode("https://git.macaw.me/blue/squawk");
}
void Core::DiscoveryHandler::onItemsReceived(const QXmppDiscoveryIq& items)
{
QString server = acc->getServer();
if (items.from() == server) {
std::set<QString> needToRequest;
qDebug() << "Server items list received for account " << acc->getName() << ":";
for (QXmppDiscoveryIq::Item item : items.items()) {
QString jid = item.jid();
if (jid != server) {
qDebug() << " Node" << jid;
needToRequest.insert(jid);
} else {
qDebug() << " " << item.node().toStdString().c_str();
}
}
for (const QString& jid : needToRequest) {
acc->dm->requestInfo(jid);
}
}
}
void Core::DiscoveryHandler::onInfoReceived(const QXmppDiscoveryIq& info)
{
QString from = info.from();
QString server = acc->getServer();
QString accName = acc->getName();
QString bareJid = acc->getBareJid();
if (from == server) {
bool enableCC = false;
qDebug() << "Server info received for account" << accName;
QStringList features = info.features();
qDebug() << "List of supported features of the server " << server << ":";
for (const QString& feature : features) {
qDebug() << " " << feature.toStdString().c_str();
if (feature == "urn:xmpp:carbons:2") {
enableCC = true;
}
}
if (enableCC) {
qDebug() << "Enabling carbon copies for account" << accName;
acc->cm->setCarbonsEnabled(true);
}
qDebug() << "Requesting account" << accName << "capabilities";
acc->dm->requestInfo(bareJid);
} else if (from == bareJid) {
qDebug() << "Received capabilities for account" << accName << ":";
QList<QXmppDiscoveryIq::Identity> identities = info.identities();
bool pepSupported = false;
for (const QXmppDiscoveryIq::Identity& identity : identities) {
QString type = identity.type();
QString category = identity.category();
qDebug() << " " << category << type;
if (type == "pep" && category == "pubsub") {
pepSupported = true;
}
}
acc->setPepSupport(pepSupported ? Shared::Support::supported : Shared::Support::unsupported);
} else {
qDebug() << "Received info for account" << accName << "about" << from;
QString node = info.queryNode();
if (!node.isEmpty()) {
QStringList feats = info.features();
std::set<Shared::Identity> identities;
std::set<QString> features(feats.begin(), feats.end());
QList<QXmppDiscoveryIq::Identity> idents = info.identities();
for (const QXmppDiscoveryIq::Identity& ident : idents) {
Shared::Identity identity;
identity.category = ident.category();
identity.language = ident.language();
identity.name = ident.name();
identity.type = ident.type();
identities.insert(identity);
qDebug() << " " << identity.name << identity.category << identity.type;
}
for (const QString& feat : features) {
qDebug() << " " << feat;
}
emit acc->infoDiscovered(from, node, identities, features);
} else {
Contact* cont = acc->rh->getContact(from);
if (cont != nullptr) {
qDebug() << "Received info for account" << accName << "about" << from;
QList<QXmppDiscoveryIq::Identity> identities = info.identities();
bool pepSupported = false;
for (const QXmppDiscoveryIq::Identity& identity : identities) {
QString type = identity.type();
QString category = identity.category();
qDebug() << " " << category << type;
if (type == "pep" && category == "pubsub") {
pepSupported = true;
}
}
cont->setPepSupport(pepSupported ? Shared::Support::supported : Shared::Support::unsupported);
}
}
}
}