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 : #include "jamidht/conversation_channel_handler.h"
18 :
19 : namespace jami {
20 :
21 672 : ConversationChannelHandler::ConversationChannelHandler(const std::shared_ptr<JamiAccount>& acc,
22 672 : dhtnet::ConnectionManager& cm)
23 : : ChannelHandlerInterface()
24 672 : , account_(acc)
25 672 : , connectionManager_(cm)
26 672 : {}
27 :
28 1344 : ConversationChannelHandler::~ConversationChannelHandler() {}
29 :
30 : void
31 0 : ConversationChannelHandler::connect(const DeviceId& deviceId,
32 : const std::string& channelName,
33 : ConnectCb&& cb,
34 : const std::string& /*connectionType*/,
35 : bool /*forceNewConnection*/)
36 : {
37 0 : connectionManager_.connectDevice(deviceId, "git://" + deviceId.toString() + "/" + channelName, std::move(cb));
38 0 : }
39 :
40 : bool
41 985 : ConversationChannelHandler::onRequest(const std::shared_ptr<dht::crypto::Certificate>& cert, const std::string& name)
42 : {
43 985 : auto acc = account_.lock();
44 985 : if (!cert || !cert->issuer || !acc)
45 0 : return false;
46 : // Pre-check before acceptance. Sometimes, another device can start a conversation
47 : // which is still not synced. So, here we decline channel's request in this case
48 : // to avoid the other device to want to sync with us if we are not ready.
49 985 : auto sep = name.find_last_of('/');
50 985 : auto conversationId = name.substr(sep + 1);
51 :
52 985 : if (auto acc = account_.lock()) {
53 985 : if (auto convModule = acc->convModule(true)) {
54 985 : const auto issuerUri = cert->issuer->getId().toString();
55 985 : const auto deviceId = cert->getLongId().toString();
56 985 : return convModule->isPeerAuthorized(conversationId, issuerUri, deviceId, true);
57 985 : } else {
58 0 : JAMI_ERROR("Received ConversationChannel request but conversation module is unavailable");
59 : }
60 985 : }
61 0 : return false;
62 985 : }
63 :
64 : void
65 0 : ConversationChannelHandler::onReady(const std::shared_ptr<dht::crypto::Certificate>&,
66 : const std::string&,
67 : std::shared_ptr<dhtnet::ChannelSocket>)
68 0 : {}
69 :
70 : } // namespace jami
|