summaryrefslogtreecommitdiff
path: root/yc.cpp
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi92@disroot.org>2026-02-03 16:19:53 +0100
committerXavier Del Campo Romero <xavi92@disroot.org>2026-02-03 16:26:52 +0100
commit0311bed34d80abe518f11838271e39d1615ebf67 (patch)
treed89854d97ba7b326909e10027d863d677dc5b15d /yc.cpp
parentfdb64c59865e4db76addfb8222f6421443e25240 (diff)
downloadyachat6-0311bed34d80abe518f11838271e39d1615ebf67.tar.gz
Import xxcc.{cpp,h} as yc.{cpp,h}
Diffstat (limited to 'yc.cpp')
-rw-r--r--yc.cpp194
1 files changed, 194 insertions, 0 deletions
diff --git a/yc.cpp b/yc.cpp
new file mode 100644
index 0000000..fc132c1
--- /dev/null
+++ b/yc.cpp
@@ -0,0 +1,194 @@
+#include "yc.h"
+#include "client.h"
+#include "direction.h"
+#include <QXmppMessage.h>
+#include <QXmppOmemoElement_p.h>
+#include <QXmppRosterManager.h>
+#include <QXmppUtils.h>
+#include <iostream>
+#include <stdexcept>
+#include <utility>
+#include <variant>
+
+void Yc::connectAccounts(const QList<Credentials::Pair> &pairs)
+{
+ for (const auto &p : pairs)
+ {
+ QXmppConfiguration cfg;
+
+ cfg.setStreamSecurityMode(QXmppConfiguration::TLSRequired);
+ cfg.setJid(p.first);
+ cfg.setPassword(p.second);
+ cfg.setAutoReconnectionEnabled(true);
+
+ const auto client = new Client(p.first);
+
+ addAccount(client);
+ client->connectToServer(cfg);
+ }
+}
+
+void Yc::startChat(const QString from, const QString to)
+{
+ bool found = false;
+
+#if 0
+ for (int i = 0; i < ui.conversations_list->count(); i++)
+ {
+ const auto it =
+ static_cast<const Conversation *>(ui.conversations_list->item(i));
+
+ if (it->from == from && it->to == to)
+ {
+ found = true;
+ break;
+ }
+ }
+
+ if (!found)
+ new Conversation(from, to, ui.conversations_list);
+
+ for (const auto c : clients)
+ if (c->jidBare() == from)
+ {
+ selected = c;
+ break;
+ }
+
+ ui.sw->setCurrentIndex(Tab::Chat);
+ ui.jid->setText(to);
+ ui.messages->scrollToBottom();
+#endif
+}
+
+void Yc::addInMessage(const QXmppMessage &msg)
+{
+#if 0
+ new Message(msg.body(), msg.stamp().toLocalTime(), Direction::In,
+ ui.messages);
+#endif
+}
+
+void Yc::addOutMessage(const QString &msg, const QDateTime &dt)
+{
+#if 0
+ new Message(msg, dt.toLocalTime(), Direction::Out,
+ ui.messages);
+#endif
+}
+
+void Yc::addAccount(Client *const c)
+{
+ c->configuration().setAutoReconnectionEnabled(true);
+ clients.append(c);
+
+ c->connect(c, &Client::messageReceived, this,
+ [this] (QXmppMessage msg)
+ {
+ if (msg.body().isEmpty())
+ return;
+ else if (msg.stamp().isNull())
+ msg.setStamp(QDateTime::currentDateTimeUtc());
+
+ storeMessage(msg, Direction::In);
+
+ if (selected)
+ addInMessage(msg);
+ });
+
+ const auto roster = c->findExtension<QXmppRosterManager>();
+
+ if (roster)
+ roster->connect(roster, &QXmppRosterManager::rosterReceived, c,
+ [this, c, roster]
+ {
+ c->database().addToRoster(roster->getRosterBareJids());
+ });
+ else
+ throw std::runtime_error("Expected non-null QXmppRosterManager");
+}
+
+void Yc::storeMessage(const QString &from, const QString &to,
+ const QString &msg, const QDateTime &dt, const Direction dir) const
+{
+ QString jid, contact;
+
+ switch (dir)
+ {
+ case Direction::In:
+ jid = QXmppUtils::jidToBareJid(to);
+ contact = QXmppUtils::jidToBareJid(from);
+ break;
+
+ case Direction::Out:
+ jid = from;
+ contact = to;
+ break;
+ }
+
+ for (const auto c : clients)
+ {
+ if (c->jidBare() == jid)
+ {
+ const auto &db = c->database();
+ JidDb::Message m;
+
+ m.body = msg;
+ m.dt = dt;
+ m.direction = dir;
+ m.contact = contact;
+ db.store(m);
+ }
+ }
+}
+
+void Yc::storeMessage(const QXmppMessage &msg, const Direction dir) const
+{
+ storeMessage(msg.from(), msg.to(), msg.body(), msg.stamp(), dir);
+}
+
+void Yc::retrieveConversations() const
+{
+#if 0
+ for (const auto c : clients)
+ {
+ const auto &db = c->database();
+
+ for (const auto &conv : db.conversations())
+ new Conversation(db.jid, conv.to,
+ ui.conversations_list, conv.last_msg, conv.dt);
+ }
+#endif
+}
+
+void Yc::init()
+{
+ creds.load().then(this,
+ [=] (Credentials::PairListResult &&result)
+ {
+ if (std::holds_alternative<Credentials::PairList>(result))
+ {
+ const auto &pairs = std::get<Credentials::PairList>(result);
+
+ connectAccounts(pairs);
+ retrieveConversations();
+ }
+ else if (std::holds_alternative<Credentials::Error>(result))
+ {
+ const auto &error = std::get<Credentials::Error>(result);
+
+ std::cerr << qPrintable(error.description) << std::endl;
+ }
+ });
+}
+
+Yc::~Yc()
+{
+ for (const auto c : clients)
+ delete c;
+}
+
+Yc::Yc(QObject *parent) :
+ QObject(parent)
+{
+}