LCOV - code coverage report
Current view: top level - foo/src/jamidht - jami_contact.h (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 53 58 91.4 %
Date: 2025-12-18 10:07:43 Functions: 21 21 100.0 %

          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             : #pragma once
      18             : 
      19             : #include "string_utils.h"
      20             : 
      21             : #include <opendht/infohash.h>
      22             : #include <opendht/value.h>
      23             : #include <opendht/default_types.h>
      24             : 
      25             : #include <msgpack.hpp>
      26             : #include <json/json.h>
      27             : 
      28             : #include <map>
      29             : #include <string>
      30             : #include <ctime>
      31             : #include <ciso646>
      32             : 
      33             : namespace jami {
      34             : 
      35             : struct Contact
      36             : {
      37             :     /** Time of contact addition */
      38             :     time_t added {0};
      39             : 
      40             :     /** Time of contact removal */
      41             :     time_t removed {0};
      42             : 
      43             :     /** True if we got confirmation that this contact also added us */
      44             :     bool confirmed {false};
      45             : 
      46             :     /** True if the contact is banned (if not active) */
      47             :     bool banned {false};
      48             : 
      49             :     /** Non empty if a swarm is linked */
      50             :     std::string conversationId {};
      51             : 
      52             :     /** True if the contact is an active contact (not banned nor removed) */
      53         501 :     bool isActive() const { return added > removed; }
      54         165 :     bool isBanned() const { return not isActive() and banned; }
      55             : 
      56         168 :     Contact() = default;
      57           3 :     Contact(const Json::Value& json)
      58           3 :     {
      59           3 :         added = json["added"].asLargestUInt();
      60           3 :         removed = json["removed"].asLargestUInt();
      61           3 :         confirmed = json["confirmed"].asBool();
      62           3 :         banned = json["banned"].asBool();
      63           3 :         conversationId = json["conversationId"].asString();
      64           3 :     }
      65             : 
      66             :     /**
      67             :      * Update this contact using other known contact information,
      68             :      * return true if contact state was changed.
      69             :      */
      70          51 :     bool update(const Contact& c)
      71             :     {
      72          51 :         const auto copy = *this;
      73          51 :         auto isMoreRecent = std::max(c.added, c.removed) > std::max(added, removed);
      74          51 :         if (isMoreRecent) {
      75           5 :             added = c.added;
      76           5 :             removed = c.removed;
      77           5 :             banned = c.banned;
      78           5 :             conversationId = c.conversationId;
      79           5 :             confirmed = c.confirmed;
      80          46 :         } else if (isActive() && added == c.added) {
      81          37 :             confirmed = confirmed or c.confirmed;
      82             :         }
      83         102 :         return hasDifferentState(copy);
      84          51 :     }
      85             : 
      86          51 :     bool hasDifferentState(const Contact& other) const
      87             :     {
      88          51 :         return other.isActive() != isActive() or other.isBanned() != isBanned() or other.confirmed != confirmed;
      89             :     }
      90             : 
      91           5 :     Json::Value toJson() const
      92             :     {
      93           5 :         Json::Value json;
      94           5 :         json["added"] = Json::Int64(added);
      95           5 :         if (removed) {
      96           0 :             json["removed"] = Json::Int64(removed);
      97             :         }
      98           5 :         if (confirmed)
      99           3 :             json["confirmed"] = confirmed;
     100           5 :         if (banned)
     101           0 :             json["banned"] = banned;
     102           5 :         json["conversationId"] = conversationId;
     103           5 :         return json;
     104           0 :     }
     105             : 
     106           9 :     std::map<std::string, std::string> toMap() const
     107             :     {
     108           9 :         std::map<std::string, std::string> result {{"added", std::to_string(added)},
     109          18 :                                                    {"removed", std::to_string(removed)},
     110          63 :                                                    {"conversationId", conversationId}};
     111             : 
     112           9 :         if (isActive())
     113           8 :             result.emplace("confirmed", confirmed ? TRUE_STR : FALSE_STR);
     114           9 :         if (isBanned())
     115           0 :             result.emplace("banned", TRUE_STR);
     116             : 
     117           9 :         return result;
     118           0 :     }
     119             : 
     120         374 :     MSGPACK_DEFINE_MAP(added, removed, confirmed, banned, conversationId)
     121             : };
     122             : 
     123             : struct TrustRequest
     124             : {
     125             :     std::shared_ptr<dht::crypto::PublicKey> device;
     126             :     std::string conversationId;
     127             :     time_t received;
     128             :     std::vector<uint8_t> payload;
     129          96 :     MSGPACK_DEFINE_MAP(device, conversationId, received, payload)
     130             : };
     131             : 
     132             : struct DeviceAnnouncement : public dht::SignedValue<DeviceAnnouncement>
     133             : {
     134             : private:
     135             :     using BaseClass = dht::SignedValue<DeviceAnnouncement>;
     136             : 
     137             : public:
     138             :     static const constexpr dht::ValueType& TYPE = dht::ValueType::USER_DATA;
     139             :     dht::InfoHash dev;
     140             :     std::shared_ptr<dht::crypto::PublicKey> pk;
     141        5635 :     MSGPACK_DEFINE_MAP(dev, pk)
     142             : };
     143             : 
     144             : struct KnownDeviceSync
     145             : {
     146             :     std::string name;
     147         803 :     MSGPACK_DEFINE_MAP(name)
     148             : };
     149             : 
     150             : struct DeviceSync : public dht::EncryptedValue<DeviceSync>
     151             : {
     152             :     static const constexpr dht::ValueType& TYPE = dht::ValueType::USER_DATA;
     153             :     uint64_t date;
     154             :     std::string device_name;
     155             :     std::map<dht::PkId, KnownDeviceSync> devices;
     156             :     std::map<dht::InfoHash, Contact> peers;
     157             :     std::map<dht::InfoHash, TrustRequest> trust_requests;
     158         726 :     MSGPACK_DEFINE_MAP(date, device_name, devices, peers, trust_requests)
     159             : };
     160             : 
     161             : struct KnownDevice
     162             : {
     163             :     using clock = std::chrono::system_clock;
     164             :     using time_point = clock::time_point;
     165             : 
     166             :     /** Device certificate */
     167             :     std::shared_ptr<dht::crypto::Certificate> certificate;
     168             : 
     169             :     /** Device name */
     170             :     std::string name {};
     171             : 
     172             :     /** Time of last received device sync */
     173             :     time_point last_sync {time_point::min()};
     174             : 
     175        3937 :     KnownDevice(const std::shared_ptr<dht::crypto::Certificate>& cert,
     176             :                 const std::string& n = {},
     177             :                 time_point sync = time_point::min())
     178        3937 :         : certificate(cert)
     179        3937 :         , name(n)
     180        3937 :         , last_sync(sync)
     181        3937 :     {}
     182             : };
     183             : 
     184             : } // namespace jami

Generated by: LCOV version 1.14