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 : #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 : MessageChannelHandler(const std::shared_ptr<JamiAccount>& acc, dhtnet::ConnectionManager& cm); 32 : ~MessageChannelHandler(); 33 : 34 : /** 35 : * Ask for a new message channel 36 : * @param deviceId The device to connect 37 : * @param name (Unused, generated from deviceId) 38 : * @param cb The callback to call when connected (can be immediate if already connected) 39 : */ 40 : void connect(const DeviceId& deviceId, const std::string&, ConnectCb&& cb) override; 41 : 42 : std::shared_ptr<dhtnet::ChannelSocket> getChannel(const std::string& peer, const DeviceId& deviceId) const; 43 : std::vector<std::shared_ptr<dhtnet::ChannelSocket>> getChannels(const std::string& peer) const; 44 : 45 : /** 46 : * Determine if we accept or not the message request 47 : * @param deviceId Device who asked 48 : * @param name Name asked 49 : * @return if the channel is for a valid conversation and device not banned 50 : */ 51 : bool onRequest(const std::shared_ptr<dht::crypto::Certificate>& peer, const std::string& name) override; 52 : 53 : /** 54 : * Launch message process 55 : * @param deviceId Device who asked 56 : * @param name Name asked 57 : * @param channel Channel used to message 58 : */ 59 : void onReady(const std::shared_ptr<dht::crypto::Certificate>& peer, 60 : const std::string& name, 61 : std::shared_ptr<dhtnet::ChannelSocket> channel) override; 62 : 63 : struct Message { 64 : std::string t; /* Message type */ 65 : std::string c; /* Message content */ 66 : std::unique_ptr<ConversationRequest> req; /* Conversation request */ 67 56175 : MSGPACK_DEFINE_MAP(t, c, req) 68 : }; 69 : 70 : static bool sendMessage(const std::shared_ptr<dhtnet::ChannelSocket>&, const Message& message); 71 : 72 : private: 73 : struct Impl; 74 : std::shared_ptr<Impl> pimpl_; 75 : }; 76 : 77 : } // namespace jami