squawk/core/account.cpp

559 lines
19 KiB
C++
Raw Normal View History

2019-03-29 14:54:34 +00:00
#include "account.h"
2019-04-09 15:04:08 +00:00
#include <qxmpp/QXmppMessage.h>
2019-04-07 14:02:41 +00:00
#include <QDateTime>
2019-03-29 14:54:34 +00:00
using namespace Core;
2019-03-30 20:13:13 +00:00
Account::Account(const QString& p_login, const QString& p_server, const QString& p_password, const QString& p_name, QObject* parent):
2019-03-29 14:54:34 +00:00
QObject(parent),
2019-03-30 20:13:13 +00:00
name(p_name),
2019-04-13 20:38:20 +00:00
achiveQueries(),
client(),
config(),
presence(),
2019-04-03 21:23:51 +00:00
state(Shared::disconnected),
2019-04-12 15:22:10 +00:00
groups(),
2019-04-13 20:38:20 +00:00
cm(new QXmppCarbonManager()),
am(new QXmppMamManager()),
contacts()
2019-03-29 14:54:34 +00:00
{
config.setUser(p_login);
config.setDomain(p_server);
config.setPassword(p_password);
QObject::connect(&client, SIGNAL(connected()), this, SLOT(onClientConnected()));
QObject::connect(&client, SIGNAL(disconnected()), this, SLOT(onClientDisconnected()));
2019-04-07 14:02:41 +00:00
QObject::connect(&client, SIGNAL(presenceReceived(const QXmppPresence&)), this, SLOT(onPresenceReceived(const QXmppPresence&)));
2019-04-09 15:04:08 +00:00
QObject::connect(&client, SIGNAL(messageReceived(const QXmppMessage&)), this, SLOT(onMessageReceived(const QXmppMessage&)));
2019-04-03 21:23:51 +00:00
QXmppRosterManager& rm = client.rosterManager();
2019-04-06 10:14:32 +00:00
2019-04-03 21:23:51 +00:00
QObject::connect(&rm, SIGNAL(rosterReceived()), this, SLOT(onRosterReceived()));
2019-04-06 10:14:32 +00:00
QObject::connect(&rm, SIGNAL(itemAdded(const QString&)), this, SLOT(onRosterItemAdded(const QString&)));
QObject::connect(&rm, SIGNAL(itemRemoved(const QString&)), this, SLOT(onRosterItemRemoved(const QString&)));
QObject::connect(&rm, SIGNAL(itemChanged(const QString&)), this, SLOT(onRosterItemChanged(const QString&)));
2019-04-07 14:02:41 +00:00
//QObject::connect(&rm, SIGNAL(presenceChanged(const QString&, const QString&)), this, SLOT(onRosterPresenceChanged(const QString&, const QString&)));
2019-04-12 15:22:10 +00:00
client.addExtension(cm);
QObject::connect(cm, SIGNAL(messageReceived(const QXmppMessage&)), this, SLOT(onCarbonMessageReceived(const QXmppMessage&)));
QObject::connect(cm, SIGNAL(messageSent(const QXmppMessage&)), this, SLOT(onCarbonMessageSent(const QXmppMessage&)));
2019-04-13 20:38:20 +00:00
client.addExtension(am);
QObject::connect(am, SIGNAL(archivedMessageReceived(const QString&, const QXmppMessage&)), this, SLOT(onMamMessageReceived(const QString&, const QXmppMessage&)));
QObject::connect(am, SIGNAL(resultsRecieved(const QString&, const QXmppResultSetReply&, bool)),
this, SLOT(onMamResultsReceived(const QString&, const QXmppResultSetReply&, bool)));
2019-03-29 14:54:34 +00:00
}
Account::~Account()
{
2019-04-19 09:12:12 +00:00
for (std::map<QString, Contact*>::const_iterator itr = contacts.begin(), end = contacts.end(); itr != end; ++itr) {
delete itr->second;
}
}
2019-03-29 14:54:34 +00:00
Shared::ConnectionState Core::Account::getState() const
{
return state;
2019-03-29 14:54:34 +00:00
}
void Core::Account::connect()
{
if (state == Shared::disconnected) {
client.connectToServer(config, presence);
state = Shared::connecting;
emit connectionStateChanged(state);
} else {
qDebug("An attempt to connect an account which is already connected, skipping");
}
}
void Core::Account::disconnect()
{
if (state != Shared::disconnected) {
client.disconnectFromServer();
state = Shared::disconnected;
emit connectionStateChanged(state);
}
}
void Core::Account::onClientConnected()
{
if (state == Shared::connecting) {
state = Shared::connected;
2019-04-12 15:22:10 +00:00
cm->setCarbonsEnabled(true);
emit connectionStateChanged(state);
} else {
qDebug("Something weird had happened - xmpp client reported about successful connection but account wasn't in connecting state");
}
}
2019-04-02 15:46:18 +00:00
void Core::Account::onClientDisconnected()
{
if (state != Shared::disconnected) {
state = Shared::disconnected;
emit connectionStateChanged(state);
} else {
2019-04-03 18:15:36 +00:00
//qDebug("Something weird had happened - xmpp client reported about being disconnection but account was already in disconnected state");
}
}
QString Core::Account::getName() const
{
return name;
}
QString Core::Account::getLogin() const
{
return config.user();
}
QString Core::Account::getPassword() const
{
return config.password();
}
QString Core::Account::getServer() const
{
return config.domain();
}
2019-04-03 21:23:51 +00:00
void Core::Account::onRosterReceived()
{
QXmppRosterManager& rm = client.rosterManager();
QStringList bj = rm.getRosterBareJids();
for (int i = 0; i < bj.size(); ++i) {
const QString& jid = bj[i];
2019-04-06 10:14:32 +00:00
addedAccount(jid);
}
}
void Core::Account::onRosterItemAdded(const QString& bareJid)
{
addedAccount(bareJid);
}
void Core::Account::onRosterItemChanged(const QString& bareJid)
{
2019-04-19 09:12:12 +00:00
std::map<QString, Contact*>::const_iterator itr = contacts.find(bareJid);
if (itr == contacts.end()) {
qDebug() << "An attempt to change non existing contact" << bareJid << "from account" << name << ", skipping";
return;
}
Contact* contact = itr->second;
2019-04-06 10:14:32 +00:00
QXmppRosterManager& rm = client.rosterManager();
QXmppRosterIq::Item re = rm.getRosterEntry(bareJid);
2019-04-07 14:02:41 +00:00
QStringList res = rm.getResources(bareJid);
2019-04-19 09:12:12 +00:00
Shared::SubscriptionState state = castSubscriptionState(re.subscriptionType());
contact->setGroups(re.groups());
contact->setSubscriptionState(state);
contact->setName(name);
2019-04-06 10:14:32 +00:00
}
void Core::Account::onRosterItemRemoved(const QString& bareJid)
{
2019-04-19 09:12:12 +00:00
std::map<QString, Contact*>::const_iterator itr = contacts.find(bareJid);
if (itr == contacts.end()) {
qDebug() << "An attempt to remove non existing contact" << bareJid << "from account" << name << ", skipping";
return;
2019-04-06 10:14:32 +00:00
}
2019-04-19 09:12:12 +00:00
Contact* contact = itr->second;
QSet<QString> cGroups = contact->getGroups();
for (QSet<QString>::const_iterator itr = cGroups.begin(), end = cGroups.end(); itr != end; ++itr) {
removeFromGroup(bareJid, *itr);
2019-04-06 10:14:32 +00:00
}
2019-04-19 09:12:12 +00:00
emit removeContact(bareJid);
2019-04-06 10:14:32 +00:00
}
void Core::Account::addedAccount(const QString& jid)
{
QXmppRosterManager& rm = client.rosterManager();
std::map<QString, Contact*>::const_iterator itr = contacts.find(jid);
2019-04-19 09:12:12 +00:00
QXmppRosterIq::Item re = rm.getRosterEntry(jid);
Contact* contact;
bool newContact = false;
if (itr == contacts.end()) {
2019-04-19 09:12:12 +00:00
newContact = true;
contact = new Contact(jid, name);
contacts.insert(std::make_pair(jid, contact));
2019-04-19 09:12:12 +00:00
} else {
contact = itr->second;
}
2019-04-06 10:14:32 +00:00
QSet<QString> gr = re.groups();
2019-04-19 09:12:12 +00:00
Shared::SubscriptionState state = castSubscriptionState(re.subscriptionType());
contact->setGroups(gr);
contact->setSubscriptionState(state);
contact->setName(re.name());
2019-04-06 10:14:32 +00:00
2019-04-19 09:12:12 +00:00
if (newContact) {
QMap<QString, QVariant> cData({
{"name", re.name()},
{"state", state}
});
int grCount = 0;
for (QSet<QString>::const_iterator itr = gr.begin(), end = gr.end(); itr != end; ++itr) {
const QString& groupName = *itr;
addToGroup(jid, groupName);
emit addContact(jid, groupName, cData);
grCount++;
}
if (grCount == 0) {
emit addContact(jid, "", cData);
}
QObject::connect(contact, SIGNAL(groupAdded(const QString&)), this, SLOT(onContactGroupAdded(const QString&)));
QObject::connect(contact, SIGNAL(groupRemoved(const QString&)), this, SLOT(onContactGroupRemoved(const QString&)));
QObject::connect(contact, SIGNAL(nameChanged(const QString&)), this, SLOT(onContactNameChanged(const QString&)));
QObject::connect(contact, SIGNAL(subscriptionStateChanged(Shared::SubscriptionState)),
this, SLOT(onContactSubscriptionStateChanged(Shared::SubscriptionState)));
2019-04-03 21:23:51 +00:00
}
}
2019-04-07 14:02:41 +00:00
void Core::Account::onPresenceReceived(const QXmppPresence& presence)
{
QString id = presence.from();
QStringList comps = id.split("/");
QString jid = comps.front();
QString resource = comps.back();
QString myJid = getLogin() + "@" + getServer();
if (jid == myJid) {
if (resource == getResource()) {
emit availabilityChanged(presence.availableStatusType());
} else {
qDebug() << "Received a presence for another resource of my " << name << " account, skipping";
}
}
2019-04-07 14:02:41 +00:00
switch (presence.type()) {
case QXmppPresence::Error:
qDebug() << "An error reported by presence from " << id;
break;
case QXmppPresence::Available:{
QDateTime lastInteraction = presence.lastUserInteraction();
if (!lastInteraction.isValid()) {
lastInteraction = QDateTime::currentDateTime();
}
emit addPresence(jid, resource, {
{"lastActivity", lastInteraction},
{"availability", presence.availableStatusType()}, //TODO check and handle invisible
{"status", presence.statusText()}
});
}
break;
case QXmppPresence::Unavailable:
emit removePresence(jid, resource);
break;
case QXmppPresence::Subscribe:
qDebug("xmpp presence \"subscribe\" received, do not yet know what to do, skipping");
case QXmppPresence::Subscribed:
qDebug("xmpp presence \"subscribed\" received, do not yet know what to do, skipping");
case QXmppPresence::Unsubscribe:
qDebug("xmpp presence \"unsubscribe\" received, do not yet know what to do, skipping");
case QXmppPresence::Unsubscribed:
qDebug("xmpp presence \"unsubscribed\" received, do not yet know what to do, skipping");
case QXmppPresence::Probe:
qDebug("xmpp presence \"probe\" received, do not yet know what to do, skipping");
break;
}
}
void Core::Account::onRosterPresenceChanged(const QString& bareJid, const QString& resource)
{
//not used for now;
qDebug() << "presence changed for " << bareJid << " resource " << resource;
const QXmppPresence& presence = client.rosterManager().getPresence(bareJid, resource);
}
void Core::Account::setLogin(const QString& p_login)
{
config.setUser(p_login);
}
void Core::Account::setName(const QString& p_name)
{
name = p_name;
}
void Core::Account::setPassword(const QString& p_password)
{
config.setPassword(p_password);
}
void Core::Account::setServer(const QString& p_server)
{
config.setDomain(p_server);
}
Shared::Availability Core::Account::getAvailability() const
{
QXmppPresence::AvailableStatusType pres = presence.availableStatusType();
return static_cast<Shared::Availability>(pres); //TODO that's a shame! gotta do something about it
}
void Core::Account::setAvailability(Shared::Availability avail)
{
QXmppPresence::AvailableStatusType pres = static_cast<QXmppPresence::AvailableStatusType>(avail);
presence.setAvailableStatusType(pres);
if (state != Shared::disconnected) { //TODO not sure how to do here - changing state may cause connection or disconnection
client.setClientPresence(presence);
}
}
QString Core::Account::getResource() const
{
return config.resource();
}
void Core::Account::setResource(const QString& p_resource)
{
config.setResource(p_resource);
}
2019-04-09 15:04:08 +00:00
2019-04-09 22:01:25 +00:00
void Core::Account::onMessageReceived(const QXmppMessage& msg)
2019-04-09 15:04:08 +00:00
{
2019-04-09 22:01:25 +00:00
bool handled = false;
switch (msg.type()) {
case QXmppMessage::Normal:
qDebug() << "received a message with type \"Normal\", not sure what to do with it now, skipping";
break;
2019-04-12 15:22:10 +00:00
case QXmppMessage::Chat:
handled = handleChatMessage(msg);
2019-04-09 22:01:25 +00:00
break;
case QXmppMessage::GroupChat:
qDebug() << "received a message with type \"GroupChat\", not sure what to do with it now, skipping";
break;
case QXmppMessage::Error:
qDebug() << "received a message with type \"Error\", not sure what to do with it now, skipping";
break;
case QXmppMessage::Headline:
qDebug() << "received a message with type \"Headline\", not sure what to do with it now, skipping";
break;
}
if (!handled) {
qDebug() << "Message wasn't handled: ";
2019-04-12 15:22:10 +00:00
qDebug() << "- from: " << msg.from();
qDebug() << "- to: " << msg.to();
2019-04-09 22:01:25 +00:00
qDebug() << "- body: " << msg.body();
qDebug() << "- type: " << msg.type();
qDebug() << "- state: " << msg.state();
qDebug() << "- stamp: " << msg.stamp();
qDebug() << "- id: " << msg.id();
qDebug() << "- isAttentionRequested: " << msg.isAttentionRequested();
qDebug() << "- isReceiptRequested: " << msg.isReceiptRequested();
qDebug() << "- receiptId: " << msg.receiptId();
qDebug() << "- subject: " << msg.subject();
qDebug() << "- thread: " << msg.thread();
qDebug() << "- isMarkable: " << msg.isMarkable();
qDebug() << "==============================";
}
2019-04-09 15:04:08 +00:00
}
2019-04-09 22:01:25 +00:00
QString Core::Account::getFullJid() const
{
return getLogin() + "@" + getServer() + "/" + getResource();
}
void Core::Account::sendMessage(const Shared::Message& data)
2019-04-10 20:53:42 +00:00
{
if (state == Shared::connected) {
2019-04-12 15:22:10 +00:00
QXmppMessage msg(data.getFrom(), data.getTo(), data.getBody(), data.getThread());
msg.setId(data.getId());
msg.setType(static_cast<QXmppMessage::Type>(data.getType())); //it is safe here, my type is compatible
client.sendPacket(msg);
2019-04-10 20:53:42 +00:00
} else {
qDebug() << "An attempt to send message with not connected account " << name << ", skipping";
}
}
2019-04-12 15:22:10 +00:00
void Core::Account::onCarbonMessageReceived(const QXmppMessage& msg)
{
2019-04-13 20:38:20 +00:00
handleChatMessage(msg, false, true);
2019-04-12 15:22:10 +00:00
}
void Core::Account::onCarbonMessageSent(const QXmppMessage& msg)
{
2019-04-13 20:38:20 +00:00
handleChatMessage(msg, true, true);
2019-04-12 15:22:10 +00:00
}
2019-04-13 20:38:20 +00:00
bool Core::Account::handleChatMessage(const QXmppMessage& msg, bool outgoing, bool forwarded, bool guessing)
2019-04-12 15:22:10 +00:00
{
QString body(msg.body());
if (body.size() != 0) {
QString id(msg.id());
QDateTime time(msg.stamp());
Shared::Message sMsg(Shared::Message::chat);
sMsg.setId(id);
sMsg.setFrom(msg.from());
sMsg.setTo(msg.to());
sMsg.setBody(body);
sMsg.setForwarded(forwarded);
2019-04-13 20:38:20 +00:00
if (guessing) {
if (sMsg.getFromJid() == getLogin() + "@" + getServer()) {
outgoing = true;
} else {
outgoing = false;
}
}
2019-04-12 15:22:10 +00:00
sMsg.setOutgoing(outgoing);
if (time.isValid()) {
sMsg.setTime(time);
}
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;
}
2019-04-13 20:38:20 +00:00
void Core::Account::onMamMessageReceived(const QString& bareJid, const QXmppMessage& msg)
{
handleChatMessage(msg, false, true, true);
}
2019-04-21 19:17:04 +00:00
void Core::Account::requestArchive(const QString& jid, int count, const QString& before)
2019-04-13 20:38:20 +00:00
{
2019-04-21 19:17:04 +00:00
std::map<QString, Contact*>::const_iterator itr = contacts.find(jid);
if (itr == contacts.end()) {
qDebug() << "An attempt to request archive for" << jid << "in account" << name << ", but the contact with such id wasn't found, skipping";
return;
}
Contact* contact = itr->second;
contact->requestHistory(count, before);
Contact::ArchiveState as = contact->getArchiveState();
switch (as) {
case Contact::empty:
break;
case Contact::beginning:
break;
case Contact::chunk:
break;
case Contact::complete:
break;
case Contact::end:
break;
}
2019-04-13 20:38:20 +00:00
QXmppResultSetQuery query;
query.setMax(100);
QDateTime from = QDateTime::currentDateTime().addDays(-7);
QString q = am->retrieveArchivedMessages("", "", jid, from, QDateTime(), query);
achiveQueries.insert(std::make_pair(q, jid));
}
void Core::Account::onMamResultsReceived(const QString& queryId, const QXmppResultSetReply& resultSetReply, bool complete)
{
std::map<QString, QString>::const_iterator itr = achiveQueries.find(queryId);
QString jid = itr->second;
achiveQueries.erase(itr);
if (!complete) {
QXmppResultSetQuery q;
q.setAfter(resultSetReply.last());
q.setMax(100);
QString nQ = am->retrieveArchivedMessages("", "", jid, QDateTime::currentDateTime().addDays(-7), QDateTime(), q);
achiveQueries.insert(std::make_pair(nQ, jid));
}
}
2019-04-19 09:12:12 +00:00
void Core::Account::onContactGroupAdded(const QString& group)
{
Contact* contact = static_cast<Contact*>(sender());
if (contact->groupsCount() == 1) {
// not sure i need to handle it here, the situation with grouped and ungrouped contacts handled on the client anyway
}
QMap<QString, QVariant> cData({
{"name", contact->getName()},
{"state", contact->getSubscriptionState()}
});
addToGroup(contact->jid, group);
emit addContact(contact->jid, group, cData);
}
void Core::Account::onContactGroupRemoved(const QString& group)
{
Contact* contact = static_cast<Contact*>(sender());
if (contact->groupsCount() == 0) {
// not sure i need to handle it here, the situation with grouped and ungrouped contacts handled on the client anyway
}
emit removeContact(contact->jid, group);
removeFromGroup(contact->jid, group);
}
void Core::Account::onContactNameChanged(const QString& cname)
{
Contact* contact = static_cast<Contact*>(sender());
QMap<QString, QVariant> cData({
{"name", cname},
});
emit changeContact(contact->jid, cData);
}
void Core::Account::onContactSubscriptionStateChanged(Shared::SubscriptionState cstate)
{
Contact* contact = static_cast<Contact*>(sender());
QMap<QString, QVariant> cData({
{"state", cstate},
});
emit changeContact(contact->jid, cData);
}
void Core::Account::addToGroup(const QString& jid, const QString& group)
{
std::map<QString, std::set<QString>>::iterator gItr = groups.find(group);
if (gItr == groups.end()) {
gItr = groups.insert(std::make_pair(group, std::set<QString>())).first;
emit addGroup(group);
}
gItr->second.insert(jid);
}
void Core::Account::removeFromGroup(const QString& jid, const QString& group)
{
QSet<QString> toRemove;
std::map<QString, std::set<QString>>::iterator itr = groups.find(group);
if (itr == groups.end()) {
qDebug() << "An attempt to remove contact" << jid << "of account" << name << "from non existing group" << group << ", skipping";
return;
}
std::set<QString> contacts = itr->second;
std::set<QString>::const_iterator cItr = contacts.find(jid);
if (cItr != contacts.end()) {
contacts.erase(cItr);
if (contacts.size() == 0) {
emit removeGroup(group);
groups.erase(group);
}
}
}
Shared::SubscriptionState Core::Account::castSubscriptionState(QXmppRosterIq::Item::SubscriptionType qs) const
{
Shared::SubscriptionState state;
if (qs == QXmppRosterIq::Item::NotSet) {
state = Shared::unknown;
} else {
state = static_cast<Shared::SubscriptionState>(qs);
}
return state;
}