Implement peer-to-peer messaging
Diff
src/channel.rs | 1 +
src/client.rs | 12 ++++++++++--
src/messages.rs | 10 ++++++++++
src/server.rs | 36 +++++++++++++++++++++++++++++++++++-
4 files changed, 54 insertions(+), 5 deletions(-)
@@ -116,6 +116,7 @@
let nick = sender.to_nick();
self.persistence
.do_send(crate::persistence::events::ChannelMessage {
channel_id: self.channel_id,
@@ -16,8 +16,8 @@
messages::{
Broadcast, ChannelFetchTopic, ChannelInvite, ChannelJoin, ChannelKickUser, ChannelList,
ChannelMemberList, ChannelMessage, ChannelPart, ChannelSetMode, ChannelUpdateTopic,
FetchClientDetails, ServerDisconnect, ServerFetchMotd, UserKickedFromChannel,
UserNickChange, UserNickChangeInternal,
FetchClientDetails, PeerToPeerMessage, ServerDisconnect, ServerFetchMotd,
UserKickedFromChannel, UserNickChange, UserNickChangeInternal,
},
persistence::{
events::{FetchUnseenMessages, FetchUserChannels, ReserveNick},
@@ -511,7 +511,12 @@
Command::PRIVMSG(target, message) => {
if !target.is_channel_name() {
error!("Private messages not implemented");
self.server.do_send(PeerToPeerMessage {
destination: target,
message,
from: ctx.address(),
span: Span::current(),
});
} else if let Some(channel) = self.channels.get(&target) {
channel.do_send(ChannelMessage {
client: ctx.address(),
@@ -586,6 +591,7 @@
Command::SAJOIN(_, _) => {}
Command::SAMODE(_, _, _) => {}
Command::SANICK(old_nick, new_nick) => {
self.server.do_send(UserNickChangeInternal {
old_nick,
new_nick,
@@ -168,3 +168,13 @@
pub struct FetchClientByNick {
pub nick: String,
}
#[derive(Message)]
#[rtype(result = "()")]
pub struct PeerToPeerMessage {
pub destination: String,
pub message: String,
pub from: Addr<Client>,
pub span: Span,
}
@@ -21,8 +21,8 @@
connection::InitiatedConnection,
messages::{
Broadcast, ChannelFetchTopic, ChannelJoin, ChannelList, ChannelMemberList,
FetchClientByNick, ServerDisconnect, ServerFetchMotd, UserConnected, UserNickChange,
UserNickChangeInternal,
FetchClientByNick, PeerToPeerMessage, ServerDisconnect, ServerFetchMotd, UserConnected,
UserNickChange, UserNickChangeInternal,
},
persistence::Persistence,
server::response::Motd,
@@ -250,6 +250,38 @@
});
Box::pin(fut)
}
}
impl Handler<PeerToPeerMessage> for Server {
type Result = ();
#[instrument(parent = &msg.span, skip_all)]
fn handle(&mut self, msg: PeerToPeerMessage, _ctx: &mut Self::Context) -> Self::Result {
let Some(source) = self.clients.get(&msg.from) else {
return;
};
let target = self
.clients
.iter()
.find(|(_handle, connection)| connection.nick == msg.destination);
let Some((target, _)) = target else {
return;
};
target.do_send(Broadcast {
message: Message {
tags: None,
prefix: Some(source.to_nick()),
command: Command::PRIVMSG(msg.destination, msg.message),
},
span: msg.span,
});
}
}