LCOV - code coverage report
Current view: top level - src/jamidht - transfer_channel_handler.cpp (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 90 107 84.1 %
Date: 2024-05-01 08:46:49 Functions: 6 7 85.7 %

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

Generated by: LCOV version 1.14