Ring Daemon 16.0.0
Loading...
Searching...
No Matches
jami_contact.h
Go to the documentation of this file.
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
33namespace jami {
34
35struct Contact
36{
39
42
44 bool confirmed {false};
45
47 bool banned {false};
48
50 std::string conversationId {};
51
53 bool isActive() const { return added > removed; }
54 bool isBanned() const { return not isActive() and banned; }
55
56 Contact() = default;
57 Contact(const Json::Value& json)
58 {
59 added = json["added"].asLargestUInt();
60 removed = json["removed"].asLargestUInt();
61 confirmed = json["confirmed"].asBool();
62 banned = json["banned"].asBool();
63 conversationId = json["conversationId"].asString();
64 }
65
70 bool update(const Contact& c)
71 {
72 const auto copy = *this;
73 if (c.added > added) {
74 added = c.added;
76 }
77 if (c.removed > removed) {
78 removed = c.removed;
79 banned = c.banned;
80 }
81 if (c.confirmed != confirmed) {
83 }
84 if (isActive()) {
85 removed = 0;
86 banned = 0;
87 }
88 if (c.isActive() and conversationId.empty() and not c.conversationId.empty()) {
90 }
91 return hasDifferentState(copy);
92 }
93 bool hasDifferentState(const Contact& other) const
94 {
95 return other.isActive() != isActive() or other.isBanned() != isBanned()
96 or other.confirmed != confirmed;
97 }
98
99 Json::Value toJson() const
100 {
101 Json::Value json;
102 json["added"] = Json::Int64(added);
103 if (removed) {
104 json["removed"] = Json::Int64(removed);
105 }
106 if (confirmed)
107 json["confirmed"] = confirmed;
108 if (banned)
109 json["banned"] = banned;
110 json["conversationId"] = conversationId;
111 return json;
112 }
113
114 std::map<std::string, std::string> toMap() const
115 {
116 std::map<std::string, std::string> result {{"added", std::to_string(added)},
117 {"removed", std::to_string(removed)},
118 {"conversationId", conversationId}};
119
120 if (isActive())
121 result.emplace("confirmed", confirmed ? TRUE_STR : FALSE_STR);
122 if (isBanned())
123 result.emplace("banned", TRUE_STR);
124
125 return result;
126 }
127
128 MSGPACK_DEFINE_MAP(added, removed, confirmed, banned, conversationId)
129};
130
132{
133 std::shared_ptr<dht::crypto::PublicKey> device;
134 std::string conversationId;
136 std::vector<uint8_t> payload;
137 MSGPACK_DEFINE_MAP(device, conversationId, received, payload)
138};
139
140struct DeviceAnnouncement : public dht::SignedValue<DeviceAnnouncement>
141{
142private:
143 using BaseClass = dht::SignedValue<DeviceAnnouncement>;
144
145public:
146 static const constexpr dht::ValueType& TYPE = dht::ValueType::USER_DATA;
147 dht::InfoHash dev;
148 std::shared_ptr<dht::crypto::PublicKey> pk;
149 MSGPACK_DEFINE_MAP(dev, pk)
150};
151
153 std::string name;
154 dht::InfoHash sha1;
155 MSGPACK_DEFINE_MAP(name, sha1)
156};
157
158struct DeviceSync : public dht::EncryptedValue<DeviceSync>
159{
160 static const constexpr dht::ValueType& TYPE = dht::ValueType::USER_DATA;
162 std::string device_name;
163 std::map<dht::InfoHash, std::string> devices_known; // Legacy
164 std::map<dht::PkId, KnownDeviceSync> devices;
165 std::map<dht::InfoHash, Contact> peers;
166 std::map<dht::InfoHash, TrustRequest> trust_requests;
168};
169
171{
172 using clock = std::chrono::system_clock;
173 using time_point = clock::time_point;
174
176 std::shared_ptr<dht::crypto::Certificate> certificate;
177
179 std::string name {};
180
182 time_point last_sync {time_point::min()};
183
184 KnownDevice(const std::shared_ptr<dht::crypto::Certificate>& cert,
185 const std::string& n = {},
186 time_point sync = time_point::min())
188 , name(n)
189 , last_sync(sync)
190 {}
191};
192
193} // namespace jami
Definition Address.h:25
static constexpr const char TRUE_STR[]
void emitSignal(Args... args)
Definition ring_signal.h:64
static constexpr const char FALSE_STR[]
bool confirmed
True if we got confirmation that this contact also added us.
time_t added
Time of contact addition.
bool isBanned() const
Contact()=default
bool isActive() const
True if the contact is an active contact (not banned nor removed)
time_t removed
Time of contact removal.
Json::Value toJson() const
bool banned
True if the contact is banned (if not active)
bool hasDifferentState(const Contact &other) const
std::string conversationId
Non empty if a swarm is linked.
Contact(const Json::Value &json)
std::map< std::string, std::string > toMap() const
bool update(const Contact &c)
Update this contact using other known contact information, return true if contact state was changed.
std::shared_ptr< dht::crypto::PublicKey > pk
static const constexpr dht::ValueType & TYPE
std::map< dht::InfoHash, Contact > peers
static const constexpr dht::ValueType & TYPE
std::map< dht::InfoHash, TrustRequest > trust_requests
std::string device_name
std::map< dht::InfoHash, std::string > devices_known
std::map< dht::PkId, KnownDeviceSync > devices
std::shared_ptr< dht::crypto::Certificate > certificate
Device certificate.
clock::time_point time_point
time_point last_sync
Time of last received device sync.
std::string name
Device name.
std::chrono::system_clock clock
KnownDevice(const std::shared_ptr< dht::crypto::Certificate > &cert, const std::string &n={}, time_point sync=time_point::min())
std::vector< uint8_t > payload
std::shared_ptr< dht::crypto::PublicKey > device
std::string conversationId