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 : #pragma once 18 : 19 : #include "jamidht/channel_handler.h" 20 : #include "jamidht/jamiaccount.h" 21 : #include <dhtnet/connectionmanager.h> 22 : 23 : namespace jami { 24 : 25 : /** 26 : * Manages channels for exchanging messages between peers 27 : */ 28 : class MessageChannelHandler : public ChannelHandlerInterface 29 : { 30 : public: 31 : using OnMessage 32 : = std::function<void(const std::shared_ptr<dht::crypto::Certificate>&, std::string&, const std::string&)>; 33 : using OnPeerStateChanged = std::function<void(const std::string&, bool)>; 34 : MessageChannelHandler(dhtnet::ConnectionManager& cm, OnMessage onMessage, OnPeerStateChanged onPeer); 35 : ~MessageChannelHandler(); 36 : 37 : /** 38 : * Ask for a new message channel 39 : * @param deviceId The device to connect 40 : * @param name (Unused, generated from deviceId) 41 : * @param cb The callback to call when connected (can be immediate if already connected) 42 : * @param connectionType for iOS notifications 43 : * @param forceNewConnection If we want a new SIP connection 44 : */ 45 : void connect(const DeviceId& deviceId, 46 : const std::string&, 47 : ConnectCb&& cb, 48 : const std::string& connectionType, 49 : bool forceNewConnection = false) override; 50 : 51 : std::shared_ptr<dhtnet::ChannelSocket> getChannel(const std::string& peer, const DeviceId& deviceId) const; 52 : std::vector<std::shared_ptr<dhtnet::ChannelSocket>> getChannels(const std::string& peer) const; 53 : 54 : /** 55 : * Determine if we accept or not the message request 56 : * @param deviceId Device who asked 57 : * @param name Name asked 58 : * @return if the channel is for a valid conversation and device not banned 59 : */ 60 : bool onRequest(const std::shared_ptr<dht::crypto::Certificate>& peer, const std::string& name) override; 61 : 62 : /** 63 : * Launch message process 64 : * @param deviceId Device who asked 65 : * @param name Name asked 66 : * @param channel Channel used to message 67 : */ 68 : void onReady(const std::shared_ptr<dht::crypto::Certificate>& peer, 69 : const std::string& name, 70 : std::shared_ptr<dhtnet::ChannelSocket> channel) override; 71 : 72 : void closeChannel(const std::string& peer, 73 : const DeviceId& device, 74 : const std::shared_ptr<dhtnet::ChannelSocket>& conn); 75 : 76 : struct Message 77 : { 78 : uint64_t id {0}; /* Message ID */ 79 : std::string t; /* Message type */ 80 : std::string c; /* Message content */ 81 : std::unique_ptr<ConversationRequest> req; /* Conversation request */ 82 25086 : MSGPACK_DEFINE_MAP(id, t, c, req) 83 : }; 84 : 85 : static bool sendMessage(const std::shared_ptr<dhtnet::ChannelSocket>&, const Message& message); 86 : 87 : private: 88 : struct Impl; 89 : std::shared_ptr<Impl> pimpl_; 90 : }; 91 : 92 : } // namespace jami