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