Line data Source code
1 : /*
2 : * Copyright (C) 2004-2026 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 : /** Called when a device becomes reachable, i.e. gets its first message channel. */
35 : using OnDeviceConnected = std::function<void(const std::string&, const DeviceId&)>;
36 : MessageChannelHandler(dhtnet::ConnectionManager& cm,
37 : OnMessage onMessage,
38 : OnPeerStateChanged onPeer,
39 : OnDeviceConnected onDeviceConnected);
40 : ~MessageChannelHandler();
41 :
42 : /**
43 : * Ask for a new message channel
44 : * @param deviceId The device to connect
45 : * @param name (Unused, generated from deviceId)
46 : * @param cb The callback to call when connected (can be immediate if already connected)
47 : * @param connectionType for iOS notifications
48 : * @param forceNewConnection If we want a new SIP connection
49 : */
50 : void connect(const DeviceId& deviceId,
51 : const std::string&,
52 : ConnectCb&& cb,
53 : const std::string& connectionType,
54 : bool forceNewConnection = false) override;
55 :
56 : std::shared_ptr<dhtnet::ChannelSocket> getChannel(const std::string& peer, const DeviceId& deviceId) const;
57 : std::vector<std::shared_ptr<dhtnet::ChannelSocket>> getChannels(const std::string& peer) const;
58 :
59 : /**
60 : * Determine if we accept or not the message request
61 : * @param deviceId Device who asked
62 : * @param name Name asked
63 : * @return if the channel is for a valid conversation and device not banned
64 : */
65 : bool onRequest(const std::shared_ptr<dht::crypto::Certificate>& peer, const std::string& name) override;
66 :
67 : /**
68 : * Launch message process
69 : * @param deviceId Device who asked
70 : * @param name Name asked
71 : * @param channel Channel used to message
72 : */
73 : void onReady(const std::shared_ptr<dht::crypto::Certificate>& peer,
74 : const std::string& name,
75 : std::shared_ptr<dhtnet::ChannelSocket> channel) override;
76 :
77 : void closeChannel(const std::string& peer,
78 : const DeviceId& device,
79 : const std::shared_ptr<dhtnet::ChannelSocket>& conn);
80 :
81 : struct Message
82 : {
83 : uint64_t id {0}; /* Message ID */
84 : std::string t; /* Message type */
85 : std::string c; /* Message content */
86 : std::unique_ptr<ConversationRequest> req; /* Conversation request */
87 24796 : MSGPACK_DEFINE_MAP(id, t, c, req)
88 : };
89 :
90 : static bool sendMessage(const std::shared_ptr<dhtnet::ChannelSocket>&, const Message& message);
91 :
92 : private:
93 : struct Impl;
94 : std::shared_ptr<Impl> pimpl_;
95 : };
96 :
97 : } // namespace jami
|