// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL // KOutNet - One peer's profile picture, exchanged over the pinned TLS channel #include "AvatarExchange.h" #include "Protocol.h" #include #include #include #include #include #include #include namespace koutnet { namespace { constexpr int kMaxAvatarBytes = 4 * 1024 * 1024; } AvatarExchange::AvatarExchange(ChannelWriter channelWriter, Seats seats, PeerInfoRecorder peerInfoRecorder, QObject *parent) : QObject(parent) , m_channelWriter(std::move(channelWriter)) , m_seats(std::move(seats)) , m_peerInfoRecorder(std::move(peerInfoRecorder)) { } void AvatarExchange::setAvatar(const QString &avatarPath) { if (m_avatarPath == avatarPath) return; m_avatarPath = avatarPath; m_avatarData.clear(); m_avatarSha.clear(); if (!avatarPath.isEmpty()) { QFile file(avatarPath); if (file.exists() && file.open(QIODevice::ReadOnly)) { m_avatarData = file.readAll(); if (m_avatarData.size() > kMaxAvatarBytes) { m_avatarData.clear(); } else { m_avatarSha = QString::fromLatin1(QCryptographicHash::hash(m_avatarData, QCryptographicHash::Sha256).toHex()); } } } // Whoever already holds our avatar sees the same recognisable change this // side does: a sha they have not seen yet is sent again, everything else is // skipped by the per-seat dedup below. broadcastAvatar(); } void AvatarExchange::onPeerAuthenticated(const QString &address) { // The peer just proved who it is, so it can finally see our picture: every // channel-up is the one moment an avatar push can be missed. if (m_lastAvatarSent.value(address) != m_avatarSha) sendAvatarToPeer(address); } void AvatarExchange::sendAvatarToPeer(const QString &targetAddress) { if (!m_channelWriter) return; QJsonObject payload; payload[QStringLiteral("type")] = protocol::kMsgAvatar; payload[QStringLiteral("sha")] = m_avatarSha; payload[QStringLiteral("ext")] = QFileInfo(m_avatarPath).suffix().toLower(); payload[QStringLiteral("data")] = QString::fromLatin1(m_avatarData.toBase64()); if (!m_channelWriter(targetAddress, payload)) return; // no channel to that seat; do not record a push that never landed m_lastAvatarSent[targetAddress] = m_avatarSha; } void AvatarExchange::broadcastAvatar() { if (!m_seats) return; const QSet seats = m_seats(); for (const QString &seat : seats) { if (m_lastAvatarSent.value(seat) != m_avatarSha) sendAvatarToPeer(seat); } } void AvatarExchange::onFrame(const QString &host, const QJsonObject &msg) { const QString sha = msg.value(QStringLiteral("sha")).toString(); const QByteArray data = QByteArray::fromBase64(msg.value(QStringLiteral("data")).toString().toLatin1()); if (data.size() > kMaxAvatarBytes) return; QString source; if (!data.isEmpty()) { if (sha.size() != 64) { return; // a sha is the integrity collar the file name is cut from } // A local file, not a URL: the sender's picture is bytes on the wire, // so nothing about it depends on either side's network view. The name // carries the sha so an unchanged avatar resolves to the same entry. QString ext = msg.value(QStringLiteral("ext")).toString().toLower(); static const QStringList kAllowed = {QStringLiteral("png"), QStringLiteral("jpg"), QStringLiteral("jpeg"), QStringLiteral("gif"), QStringLiteral("webp"), QStringLiteral("bmp")}; if (!kAllowed.contains(ext)) ext = QStringLiteral("png"); const QString fileName = QStringLiteral("peer-%1-%2.%3").arg(host, sha.left(8), ext); const QString dir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/peer-avatars"); QDir().mkpath(dir); const QString path = dir + QLatin1Char('/') + fileName; QSaveFile out(path); if (out.open(QIODevice::WriteOnly)) { out.write(data); if (out.commit()) source = QUrl::fromLocalFile(path).toString(); } if (source.isEmpty()) return; // the writer has gone read-only: keep the old picture } if (m_peerInfoRecorder) m_peerInfoRecorder(host, source); Q_EMIT peerAvatar(host, source); } } // namespace koutnet