LCOV - code coverage report
Current view: top level - foo/src/jamidht - transfer_channel_handler.cpp (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 97 112 86.6 %
Date: 2025-12-18 10:07:43 Functions: 8 13 61.5 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2004-2025 Savoir-faire Linux Inc.
       3             :  *
       4             :  *  This program is free software: you can redistribute it and/or modify
       5             :  *  it under the terms of the GNU General Public License as published by
       6             :  *  the Free Software Foundation, either version 3 of the License, or
       7             :  *  (at your option) any later version.
       8             :  *
       9             :  *  This program is distributed in the hope that it will be useful,
      10             :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      12             :  *  GNU General Public License for more details.
      13             :  *
      14             :  *  You should have received a copy of the GNU General Public License
      15             :  *  along with this program. If not, see <https://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "jamidht/transfer_channel_handler.h"
      19             : 
      20             : #include <opendht/thread_pool.h>
      21             : #include <charconv>
      22             : 
      23             : #include "fileutils.h"
      24             : 
      25             : namespace jami {
      26             : 
      27         669 : TransferChannelHandler::TransferChannelHandler(const std::shared_ptr<JamiAccount>& account,
      28         669 :                                                dhtnet::ConnectionManager& cm)
      29             :     : ChannelHandlerInterface()
      30         669 :     , account_(account)
      31         669 :     , connectionManager_(cm)
      32             : {
      33         669 :     if (auto acc = account_.lock())
      34         669 :         idPath_ = fileutils::get_data_dir() / acc->getAccountID();
      35         669 : }
      36             : 
      37        1338 : TransferChannelHandler::~TransferChannelHandler() {}
      38             : 
      39             : void
      40           0 : TransferChannelHandler::connect(const DeviceId& deviceId,
      41             :                                 const std::string& channelName,
      42             :                                 ConnectCb&& cb,
      43             :                                 const std::string& connectionType,
      44             :                                 bool forceNewConnection)
      45           0 : {}
      46             : 
      47             : bool
      48          68 : TransferChannelHandler::onRequest(const std::shared_ptr<dht::crypto::Certificate>& cert, const std::string& name)
      49             : {
      50          68 :     auto acc = account_.lock();
      51          68 :     if (!acc || !cert || !cert->issuer)
      52           0 :         return false;
      53          68 :     auto cm = acc->convModule(true);
      54          68 :     if (!cm)
      55           0 :         return false;
      56          68 :     auto uri = cert->issuer->getId().toString();
      57             :     // Else, check if it's a profile or file in a conversation.
      58          68 :     auto idstr = std::string_view(name).substr(DATA_TRANSFER_SCHEME.size());
      59             :     // Remove arguments for now
      60          68 :     auto sep = idstr.find_last_of('?');
      61          68 :     idstr = idstr.substr(0, sep);
      62          68 :     if (idstr == "profile.vcf") {
      63             :         // If it's our profile from another device
      64           2 :         return uri == acc->getUsername();
      65             :     }
      66          66 :     sep = idstr.find('/');
      67          66 :     auto lastSep = idstr.find_last_of('/');
      68          66 :     auto conversationId = std::string(idstr.substr(0, sep));
      69          66 :     auto fileHost = idstr.substr(sep + 1, lastSep - sep - 1);
      70          66 :     auto fileId = idstr.substr(lastSep + 1);
      71          66 :     if (fileHost == acc->currentDeviceId())
      72           0 :         return false;
      73             : 
      74             :     // Check if peer is member of the conversation
      75         132 :     if (fileId == fmt::format("{}.vcf", acc->getUsername()) || fileId == "profile.vcf") {
      76             :         // Or a member from the conversation
      77          42 :         auto members = cm->getConversationMembers(conversationId);
      78         123 :         return std::find_if(members.begin(), members.end(), [&](auto m) { return m["uri"] == uri; }) != members.end();
      79          66 :     } else if (fileHost == "profile") {
      80             :         // If a profile is sent, check if it's from another device
      81          11 :         return uri == acc->getUsername();
      82             :     }
      83             : 
      84          13 :     return cm->onFileChannelRequest(conversationId, uri, std::string(fileId), acc->sha3SumVerify());
      85          68 : }
      86             : 
      87             : void
      88         128 : TransferChannelHandler::onReady(const std::shared_ptr<dht::crypto::Certificate>&,
      89             :                                 const std::string& name,
      90             :                                 std::shared_ptr<dhtnet::ChannelSocket> channel)
      91             : {
      92         128 :     auto acc = account_.lock();
      93         128 :     if (!acc)
      94           0 :         return;
      95             : 
      96             :     // Remove scheme
      97         128 :     auto idstr = name.substr(DATA_TRANSFER_SCHEME.size());
      98             :     // Parse arguments
      99         128 :     auto sep = idstr.find_last_of('?');
     100         128 :     std::string arguments;
     101         128 :     if (sep != std::string::npos) {
     102          16 :         arguments = idstr.substr(sep + 1);
     103          16 :         idstr = idstr.substr(0, sep);
     104             :     }
     105             : 
     106         128 :     auto start = 0u, end = 0u;
     107         128 :     uint64_t lastModified = 0;
     108         128 :     std::string sha3Sum;
     109         160 :     for (const auto arg : split_string(arguments, '&')) {
     110          32 :         auto keyVal = split_string(arg, '=');
     111          32 :         if (keyVal.size() == 2) {
     112          32 :             if (keyVal[0] == "start") {
     113           4 :                 start = to_int<unsigned>(keyVal[1]);
     114          28 :             } else if (keyVal[0] == "end") {
     115           4 :                 end = to_int<unsigned>(keyVal[1]);
     116          24 :             } else if (keyVal[0] == "sha3") {
     117          12 :                 sha3Sum = keyVal[1];
     118          12 :             } else if (keyVal[0] == "modified") {
     119             :                 try {
     120          12 :                     lastModified = to_int<uint64_t>(keyVal[1]);
     121           0 :                 } catch (const std::exception& e) {
     122           0 :                     JAMI_WARNING("TransferChannel: Unable to parse modified date: {}: {}", keyVal[1], e.what());
     123           0 :                 }
     124             :             }
     125             :         }
     126         160 :     }
     127             : 
     128             :     // Check if profile
     129         128 :     if (idstr == "profile.vcf") {
     130           8 :         dht::ThreadPool::io().run(
     131           4 :             [wacc = acc->weak(), path = idPath_ / "profile.vcf", channel, idstr, lastModified, sha3Sum] {
     132           4 :                 if (auto acc = wacc.lock()) {
     133           4 :                     if (!channel->isInitiator()) {
     134             :                         // Only accept newest profiles
     135           2 :                         if (lastModified == 0 || lastModified > fileutils::lastWriteTimeInSeconds(acc->profilePath()))
     136           2 :                             acc->dataTransfer()->onIncomingProfile(channel, sha3Sum);
     137             :                         else
     138           0 :                             channel->shutdown();
     139             :                     } else {
     140             :                         // If it's a profile from sync
     141           2 :                         acc->dataTransfer()->transferFile(channel, idstr, "", path.string());
     142             :                     }
     143           4 :                 }
     144           4 :             });
     145           4 :         return;
     146             :     }
     147             : 
     148         124 :     auto splitted_id = split_string(idstr, '/');
     149         124 :     if (splitted_id.size() < 3) {
     150           0 :         JAMI_ERROR("Unsupported ID detected {}", name);
     151           0 :         channel->shutdown();
     152           0 :         return;
     153             :     }
     154             : 
     155             :     // convId/fileHost/fileId or convId/profile/fileId
     156         124 :     auto conversationId = std::string(splitted_id[0]);
     157         124 :     auto fileHost = std::string(splitted_id[1]);
     158         124 :     auto isContactProfile = splitted_id[1] == "profile";
     159         123 :     auto fileId = std::string(splitted_id[splitted_id.size() - 1]);
     160         123 :     if (channel->isInitiator())
     161          61 :         return;
     162             : 
     163             :     // Profile for a member in the conversation
     164         126 :     dht::ThreadPool::io().run([wacc = acc->weak(),
     165          63 :                                profilePath = idPath_ / "profile.vcf",
     166             :                                channel,
     167             :                                conversationId,
     168             :                                fileId,
     169             :                                isContactProfile,
     170             :                                idstr,
     171             :                                start,
     172             :                                end,
     173          39 :                                sha3Sum] {
     174          63 :         if (auto acc = wacc.lock()) {
     175         126 :             if (fileId == fmt::format("{}.vcf", acc->getUsername())) {
     176          36 :                 acc->dataTransfer()->transferFile(channel, fileId, "", profilePath.string());
     177          51 :                 return;
     178          27 :             } else if (isContactProfile && fileId.find(".vcf") != std::string::npos) {
     179          22 :                 auto path = acc->dataTransfer()->profilePath(fileId.substr(0, fileId.size() - 4));
     180          11 :                 acc->dataTransfer()->transferFile(channel, fileId, "", path.string());
     181          11 :                 return;
     182          27 :             } else if (fileId == "profile.vcf") {
     183           4 :                 acc->dataTransfer()->onIncomingProfile(channel, sha3Sum);
     184           4 :                 return;
     185             :             }
     186             :             // Check if it's a file in a conversation
     187          12 :             auto dt = acc->dataTransfer(conversationId);
     188          12 :             auto sep = fileId.find('_');
     189          12 :             if (!dt or sep == std::string::npos) {
     190           0 :                 channel->shutdown();
     191           0 :                 return;
     192             :             }
     193          12 :             auto interactionId = fileId.substr(0, sep);
     194          12 :             auto path = dt->path(fileId);
     195          12 :             dt->transferFile(channel, fileId, interactionId, path.string(), start, end);
     196          75 :         }
     197             :     });
     198         567 : }
     199             : 
     200             : } // namespace jami

Generated by: LCOV version 1.14