Radix Relay
Hybrid mesh communications with Signal Protocol encryption
Loading...
Searching...
No Matches
event_handler.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <core/events.hpp>
6#include <memory>
7#include <string>
8#include <string_view>
9
10namespace radix_relay::core {
11
20template<concepts::command_handler CmdHandler> struct event_handler
21{
22 using command_handler_t = CmdHandler;
23
24 // Type traits for standard_processor
26
27 // No output queues - forwards to command_handler
29 {
30 };
31
38 explicit event_handler(std::shared_ptr<CmdHandler> command_handler, const out_queues_t & /*queues*/)
39 : command_handler_(command_handler)
40 {}
41
47 auto handle(const events::raw_command &event) const -> void
48 {
49 const auto &input = event.input;
50
51 if (input == "/help") {
52 command_handler_->handle(events::help{});
53 return;
54 }
55 if (input == "/peers") {
56 command_handler_->handle(events::peers{});
57 return;
58 }
59 if (input == "/status") {
60 command_handler_->handle(events::status{});
61 return;
62 }
63 if (input == "/sessions") {
64 command_handler_->handle(events::sessions{});
65 return;
66 }
67 if (input == "/scan") {
68 command_handler_->handle(events::scan{});
69 return;
70 }
71 if (input == "/version") {
72 command_handler_->handle(events::version{});
73 return;
74 }
75 if (input == "/identities") {
76 command_handler_->handle(events::identities{});
77 return;
78 }
79 if (input == "/publish") {
80 command_handler_->handle(events::publish_identity{});
81 return;
82 }
83 if (input == "/unpublish") {
84 command_handler_->handle(events::unpublish_identity{});
85 return;
86 }
87
88 constexpr auto mode_cmd = "/mode ";
89 if (input.starts_with(mode_cmd)) {
90 command_handler_->handle(events::mode{ .new_mode = input.substr(std::string_view(mode_cmd).length()) });
91 return;
92 }
93
94 constexpr auto send_cmd = "/send ";
95 if (input.starts_with(send_cmd)) {
96 const auto args = input.substr(std::string_view(send_cmd).length());
97 const auto first_space = args.find(' ');
98 if (first_space != std::string::npos and not args.empty()) {
99 command_handler_->handle(
100 events::send{ .peer = args.substr(0, first_space), .message = args.substr(first_space + 1) });
101 } else {
102 command_handler_->handle(events::send{ .peer = "", .message = "" });
103 }
104 return;
105 }
106
107 constexpr auto broadcast_cmd = "/broadcast ";
108 if (input.starts_with(broadcast_cmd)) {
109 command_handler_->handle(events::broadcast{ .message = input.substr(std::string_view(broadcast_cmd).length()) });
110 return;
111 }
112
113 constexpr auto connect_cmd = "/connect ";
114 if (input.starts_with(connect_cmd)) {
115 command_handler_->handle(events::connect{ .relay = input.substr(std::string_view(connect_cmd).length()) });
116 return;
117 }
118
119 if (input == "/disconnect") {
120 command_handler_->handle(events::disconnect{});
121 return;
122 }
123
124 constexpr auto trust_cmd = "/trust ";
125 if (input.starts_with(trust_cmd)) {
126 const auto args = input.substr(std::string_view(trust_cmd).length());
127 const auto first_space = args.find(' ');
128 if (first_space != std::string::npos and not args.empty()) {
129 command_handler_->handle(
130 events::trust{ .peer = args.substr(0, first_space), .alias = args.substr(first_space + 1) });
131 } else {
132 command_handler_->handle(events::trust{ .peer = args, .alias = "" });
133 }
134 return;
135 }
136
137 constexpr auto verify_cmd = "/verify ";
138 if (input.starts_with(verify_cmd)) {
139 command_handler_->handle(events::verify{ .peer = input.substr(std::string_view(verify_cmd).length()) });
140 return;
141 }
142
143 constexpr auto chat_cmd = "/chat ";
144 if (input.starts_with(chat_cmd)) {
145 command_handler_->handle(events::chat{ .contact = input.substr(std::string_view(chat_cmd).length()) });
146 return;
147 }
148
149 if (input == "/leave") {
150 command_handler_->handle(events::leave{});
151 return;
152 }
153 }
154
155private:
156 std::shared_ptr<CmdHandler> command_handler_;
157};
158
159}// namespace radix_relay::core
Thread-safe asynchronous queue for message passing between coroutines.
Handles typed command events and coordinates with subsystems.
Parses raw command strings into typed command events.
event_handler(std::shared_ptr< CmdHandler > command_handler, const out_queues_t &)
Constructs an event handler with the given command handler.
auto handle(const events::raw_command &event) const -> void
Parses and handles a raw command string.
Broadcast message to all peers.
Definition events.hpp:63
std::string message
Message content to broadcast.
Definition events.hpp:64
Enter chat mode with a specific contact.
Definition events.hpp:103
std::string contact
RDX fingerprint, Nostr pubkey, or alias.
Definition events.hpp:104
std::string relay
Relay URL to connect to.
Definition events.hpp:70
Disconnect from current relay.
Definition events.hpp:75
Request display of available commands.
Definition events.hpp:15
Request list of discovered identities.
Definition events.hpp:35
Change operational mode.
Definition events.hpp:50
std::string new_mode
The new mode to switch to.
Definition events.hpp:51
Request list of connected peers.
Definition events.hpp:20
Publish identity bundle to the network.
Definition events.hpp:80
Raw unparsed command input.
Definition events.hpp:136
Request scan for nearby peers.
Definition events.hpp:40
Send encrypted message to a specific peer.
Definition events.hpp:56
std::string peer
RDX fingerprint or alias of recipient.
Definition events.hpp:57
Request list of active sessions.
Definition events.hpp:30
Request current system status.
Definition events.hpp:25
Establish trust with a peer and assign an alias.
Definition events.hpp:90
std::string peer
RDX fingerprint of peer to trust.
Definition events.hpp:91
Remove identity bundle from the network.
Definition events.hpp:85
Verify identity fingerprint of a peer.
Definition events.hpp:97
std::string peer
RDX fingerprint to verify.
Definition events.hpp:98
Request application version information.
Definition events.hpp:45