mod response;
use std::collections::HashMap;
use actix::{Actor, Addr, AsyncContext, Context, Handler, MessageResult};
use irc_proto::Command;
use tracing::{error, info, instrument, Span};
use crate::{
channel::response::{ChannelNamesList, ChannelTopic},
client::Client,
connection::InitiatedConnection,
messages::{
Broadcast, ChannelJoin, ChannelList, ChannelMessage, ChannelPart, ServerDisconnect,
},
};
use crate::messages::UserNickChange;
pub struct Channel {
pub name: String,
pub clients: HashMap<Addr<Client>, InitiatedConnection>,
}
impl Actor for Channel {
type Context = Context<Self>;
}
impl Handler<Broadcast> for Channel {
type Result = ();
#[instrument(parent = &msg.span, skip_all)]
fn handle(&mut self, msg: Broadcast, _ctx: &mut Self::Context) -> Self::Result {
for client in self.clients.keys() {
client.try_send(msg.clone()).unwrap();
}
}
}
impl Handler<ChannelList> for Channel {
type Result = MessageResult<ChannelList>;
#[instrument(parent = &msg.span, skip_all)]
fn handle(&mut self, msg: ChannelList, _ctx: &mut Self::Context) -> Self::Result {
MessageResult(self.clients.values().cloned().collect())
}
}
impl Handler<ChannelMessage> for Channel {
type Result = ();
#[instrument(parent = &msg.span, skip_all)]
fn handle(&mut self, msg: ChannelMessage, _ctx: &mut Self::Context) -> Self::Result {
let Some(sender) = self.clients.get(&msg.client) else {
error!("Received message from user not in channel");
return;
};
let nick = sender.to_nick();
for client in self.clients.keys() {
if client == &msg.client {
continue;
}
client.do_send(Broadcast {
span: Span::current(),
message: irc_proto::Message {
tags: None,
prefix: Some(nick.clone()),
command: Command::PRIVMSG(self.name.to_string(), msg.message.clone()),
},
});
}
}
}
impl Handler<UserNickChange> for Channel {
type Result = ();
fn handle(&mut self, msg: UserNickChange, _ctx: &mut Self::Context) -> Self::Result {
let Some(sender) = self.clients.get_mut(&msg.client) else {
return;
};
*sender = msg.connection;
sender.nick = msg.new_nick;
}
}
impl Handler<ChannelJoin> for Channel {
type Result = MessageResult<ChannelJoin>;
#[instrument(parent = &msg.span, skip_all)]
fn handle(&mut self, msg: ChannelJoin, ctx: &mut Self::Context) -> Self::Result {
info!(self.name, msg.connection.nick, "User is joining channel");
self.clients
.insert(msg.client.clone(), msg.connection.clone());
for client in self.clients.keys() {
client.do_send(Broadcast {
span: Span::current(),
message: irc_proto::Message {
tags: None,
prefix: Some(msg.connection.to_nick()),
command: Command::JOIN(self.name.to_string(), None, None),
},
});
}
msg.client.do_send(Broadcast {
message: ChannelTopic::new(self).into_message(self.name.to_string()),
span: Span::current(),
});
for message in ChannelNamesList::new(self).into_messages(msg.connection.nick.to_string()) {
msg.client.do_send(Broadcast {
message,
span: Span::current(),
});
}
MessageResult(Ok(ctx.address()))
}
}
impl Handler<ChannelPart> for Channel {
type Result = ();
#[instrument(parent = &msg.span, skip_all)]
fn handle(&mut self, msg: ChannelPart, ctx: &mut Self::Context) -> Self::Result {
let Some(client_info) = self.clients.remove(&msg.client) else {
return;
};
let message = Broadcast {
message: irc_proto::Message {
tags: None,
prefix: Some(client_info.to_nick()),
command: Command::PART(self.name.to_string(), msg.message),
},
span: Span::current(),
};
msg.client.do_send(message.clone());
ctx.notify(message);
}
}
impl Handler<ServerDisconnect> for Channel {
type Result = ();
#[instrument(parent = &msg.span, skip_all)]
fn handle(&mut self, msg: ServerDisconnect, ctx: &mut Self::Context) -> Self::Result {
let Some(client_info) = self.clients.remove(&msg.client) else {
return;
};
let message = Broadcast {
span: Span::current(),
message: irc_proto::Message {
tags: None,
prefix: Some(client_info.to_nick()),
command: Command::QUIT(msg.message),
},
};
ctx.notify(message);
}
}