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 : 18 : #include "swarm_channel_handler.h" 19 : 20 : namespace jami { 21 : 22 669 : SwarmChannelHandler::SwarmChannelHandler(const std::shared_ptr<JamiAccount>& acc, dhtnet::ConnectionManager& cm) 23 : : ChannelHandlerInterface() 24 669 : , account_(acc) 25 669 : , connectionManager_(cm) 26 669 : {} 27 : 28 1338 : SwarmChannelHandler::~SwarmChannelHandler() {} 29 : 30 : void 31 0 : SwarmChannelHandler::connect(const DeviceId& deviceId, 32 : const std::string& conversationId, 33 : ConnectCb&& cb, 34 : const std::string& connectionType, 35 : bool forceNewConnection) 36 : { 37 : #ifdef LIBJAMI_TEST 38 0 : if (disableSwarmManager) 39 0 : return; 40 : #endif 41 0 : connectionManager_.connectDevice(deviceId, fmt::format("swarm://{}", conversationId), cb); 42 : } 43 : 44 : bool 45 354 : SwarmChannelHandler::onRequest(const std::shared_ptr<dht::crypto::Certificate>& cert, const std::string& name) 46 : { 47 : #ifdef LIBJAMI_TEST 48 354 : if (disableSwarmManager) 49 1 : return false; 50 : #endif 51 353 : auto acc = account_.lock(); 52 353 : if (!cert || !cert->issuer || !acc) 53 0 : return false; 54 : 55 353 : auto sep = name.find_last_of('/'); 56 353 : auto conversationId = name.substr(sep + 1); 57 353 : if (auto acc = account_.lock()) 58 353 : if (auto convModule = acc->convModule(true)) { 59 352 : auto res = !convModule->isBanned(conversationId, cert->issuer->getId().toString()); 60 352 : res &= !convModule->isBanned(conversationId, cert->getLongId().toString()); 61 352 : return res; 62 353 : } 63 1 : return false; 64 353 : } 65 : 66 : void 67 633 : SwarmChannelHandler::onReady(const std::shared_ptr<dht::crypto::Certificate>&, 68 : const std::string& uri, 69 : std::shared_ptr<dhtnet::ChannelSocket> socket) 70 : { 71 633 : auto sep = uri.find_last_of('/'); 72 633 : auto conversationId = uri.substr(sep + 1); 73 633 : if (auto acc = account_.lock()) { 74 632 : if (auto convModule = acc->convModule(true)) { 75 633 : convModule->addSwarmChannel(conversationId, socket); 76 : } 77 632 : } 78 633 : } 79 : } // namespace jami