LCOV - code coverage report
Current view: top level - src/jamidht - contact_list.h (source / functions) Coverage Total Hit
Test: jami-coverage-filtered.info Lines: 70.0 % 10 7
Test Date: 2026-06-13 09:18:46 Functions: 66.7 % 6 4

            Line data    Source code
       1              : /*
       2              :  *  Copyright (C) 2004-2026 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              : #pragma once
      18              : 
      19              : #include "jami_contact.h"
      20              : 
      21              : #include <dhtnet/certstore.h>
      22              : #include <opendht/infohash.h>
      23              : #include <opendht/crypto.h>
      24              : 
      25              : #include <map>
      26              : #include <mutex>
      27              : #include <chrono>
      28              : 
      29              : namespace jami {
      30              : 
      31              : class ContactList
      32              : {
      33              : public:
      34              :     using clock = std::chrono::system_clock;
      35              :     using time_point = clock::time_point;
      36              :     using VerifyResult = dht::crypto::TrustList::VerifyResult;
      37              : 
      38              :     using OnContactAdded = std::function<void(const std::string&, bool)>;
      39              :     using OnContactRemoved = std::function<void(const std::string&, bool)>;
      40              :     using OnIncomingTrustRequest
      41              :         = std::function<void(const std::string&, const std::string&, const std::vector<uint8_t>&, time_t)>;
      42              :     using OnAcceptConversation = std::function<void(const std::string&, const std::string&)>;
      43              :     using OnConfirmation = std::function<void(const std::string&, const std::string&)>;
      44              :     using OnDevicesChanged = std::function<void(const std::map<dht::PkId, KnownDevice>&)>;
      45              : 
      46              :     struct OnChangeCallback
      47              :     {
      48              :         OnContactAdded contactAdded;
      49              :         OnContactRemoved contactRemoved;
      50              :         OnIncomingTrustRequest trustRequest;
      51              :         OnDevicesChanged devicesChanged;
      52              :         OnAcceptConversation acceptConversation;
      53              :         OnConfirmation onConfirmation;
      54              :     };
      55              : 
      56              :     ContactList(const std::string& accountId,
      57              :                 const std::shared_ptr<crypto::Certificate>& cert,
      58              :                 const std::filesystem::path& path,
      59              :                 OnChangeCallback cb);
      60              :     ~ContactList();
      61              : 
      62         6827 :     const std::string& accountId() const { return accountId_; }
      63              : 
      64              :     void load();
      65              :     void save();
      66              : 
      67              :     /* Contacts */
      68              :     std::map<std::string, std::string> getContactDetails(const dht::InfoHash&) const;
      69              :     std::optional<Contact> getContactInfo(const dht::InfoHash&) const;
      70              : 
      71              :     bool removeContact(const dht::InfoHash&, bool ban);
      72              :     bool removeContactConversation(const dht::InfoHash&);
      73              :     bool addContact(const dht::InfoHash&, bool confirmed = false, const std::string& conversationId = "");
      74              :     bool updateConversation(const dht::InfoHash& h, const std::string& conversationId, bool added = false);
      75              : 
      76              :     bool setCertificateStatus(const std::string& cert_id, const dhtnet::tls::TrustStore::PermissionStatus status);
      77              : 
      78              :     bool setCertificateStatus(const std::shared_ptr<crypto::Certificate>& cert,
      79              :                               dhtnet::tls::TrustStore::PermissionStatus status,
      80              :                               bool local = true);
      81              : 
      82        20950 :     dhtnet::tls::TrustStore::PermissionStatus getCertificateStatus(const std::string& cert_id) const
      83              :     {
      84        20950 :         return trust_->getCertificateStatus(cert_id);
      85              :     }
      86              : 
      87            0 :     std::vector<std::string> getCertificatesByStatus(dhtnet::tls::TrustStore::PermissionStatus status) const
      88              :     {
      89            0 :         return trust_->getCertificatesByStatus(status);
      90              :     }
      91              : 
      92          952 :     bool isAllowed(const crypto::Certificate& crt, bool allowPublic) { return trust_->isAllowed(crt, allowPublic); }
      93              : 
      94         1030 :     VerifyResult isValidAccountDevice(const crypto::Certificate& crt) const { return accountTrust_.verify(crt); }
      95              : 
      96              :     const std::map<dht::InfoHash, Contact>& getContacts() const;
      97              :     void setContacts(const std::map<dht::InfoHash, Contact>&);
      98              :     void updateContact(const dht::InfoHash&, const Contact&, bool emit = true);
      99              : 
     100              :     static std::map<dht::InfoHash, Contact> contactsFromPath(const std::filesystem::path& path);
     101              : 
     102              :     /** Should be called only after updateContact */
     103              :     void saveContacts() const;
     104              : 
     105              :     const std::filesystem::path& path() const { return path_; }
     106              : 
     107              :     /* Contact requests */
     108              : 
     109              :     /** Inform of a new contact request. Returns true if the request should be immediatly accepted
     110              :      * (already a contact) */
     111              :     bool onTrustRequest(const dht::InfoHash& peer_account,
     112              :                         const std::shared_ptr<dht::crypto::PublicKey>& peer_device,
     113              :                         time_t received,
     114              :                         bool confirm,
     115              :                         const std::string& conversationId,
     116              :                         std::vector<uint8_t>&& payload);
     117              :     std::vector<std::map<std::string, std::string>> getTrustRequests() const;
     118              :     std::map<std::string, std::string> getTrustRequest(const dht::InfoHash& from) const;
     119              :     void acceptConversation(const std::string& convId,
     120              :                             const std::string& deviceId = ""); // ToDO this is a bit dirty imho
     121              :     bool acceptTrustRequest(const dht::InfoHash& from);
     122              :     bool discardTrustRequest(const dht::InfoHash& from);
     123              : 
     124              :     /* Devices */
     125            0 :     const std::map<dht::PkId, KnownDevice>& getKnownDevices() const { return knownDevices_; }
     126              :     void foundAccountDevice(const dht::PkId& device,
     127              :                             const std::string& name = {},
     128         2000 :                             const time_point& last_sync = time_point::min());
     129              :     bool foundAccountDevice(const std::shared_ptr<dht::crypto::Certificate>& crt,
     130              :                             const std::string& name = {},
     131            2 :                             const time_point& last_sync = time_point::min(),
     132              :                             bool notify = true);
     133              :     bool removeAccountDevice(const dht::PkId& device);
     134              :     void setAccountDeviceName(const dht::PkId& device, const std::string& name);
     135              :     std::string getAccountDeviceName(const dht::PkId& device) const;
     136              : 
     137              :     DeviceSync getSyncData() const;
     138              :     bool syncDevice(const dht::PkId& device, const time_point& syncDate);
     139              : 
     140              : private:
     141              :     const std::string accountId_;
     142              :     const std::filesystem::path path_;
     143              :     mutable std::mutex mutex_;
     144              :     std::map<dht::InfoHash, Contact> contacts_;
     145              :     std::map<dht::InfoHash, TrustRequest> trustRequests_;
     146              :     std::map<dht::InfoHash, KnownDevice> knownDevicesLegacy_;
     147              : 
     148              :     std::map<dht::PkId, KnownDevice> knownDevices_;
     149              : 
     150              :     // Trust store with account main certificate as the only CA
     151              :     dht::crypto::TrustList accountTrust_;
     152              :     // Trust store for to match peer certificates
     153              :     std::unique_ptr<dhtnet::tls::TrustStore> trust_;
     154              :     std::string accountUri_;
     155              : 
     156              :     OnChangeCallback callbacks_;
     157              : 
     158              :     void loadContacts();
     159              :     void loadTrustRequests();
     160              : 
     161              :     void loadKnownDevices();
     162              :     void saveKnownDevices() const;
     163              : 
     164              :     /** Should be called only after onTrustRequest */
     165              :     void saveTrustRequests() const;
     166              : };
     167              : 
     168              : } // namespace jami
        

Generated by: LCOV version 2.0-1