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