Skip to content

Commit

Permalink
darkirc: remove redundant checks and edit comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dasman committed Jan 15, 2025
1 parent 59326d2 commit f861a0d
Showing 1 changed file with 29 additions and 38 deletions.
67 changes: 29 additions & 38 deletions bin/darkirc/src/irc/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use smol::{
};

use super::{
server::{IrcServer, MAX_MSG_LEN, MAX_NICK_LEN},
server::{IrcServer, MAX_MSG_LEN},
Msg, NickServ, OldPrivmsg, SERVER_NAME,
};

Expand Down Expand Up @@ -245,9 +245,10 @@ impl Client {
continue
}

// If the privmsg is intented for any of the given
// channels, contacts or oursleves, add it as a reply and
// mark it as seen in the seen_events tree.
// If the privmsg is not intented for any of the given
// channels or contacts, ignore it
// otherwise add it as a reply and mark it as seen
// in the seen_events tree.
let channels = self.channels.read().await;
let contacts = self.server.contacts.read().await;
if !channels.contains(&privmsg.channel) &&
Expand All @@ -256,45 +257,35 @@ impl Client {
continue
}

// If we have this channel, or it's a DM, forward it to the client.
// As a DM, we consider something that is <= MAX_NICK_LEN, and does not
// start with the '#' character. With ChaCha, the ciphertext should be
// longer than our MAX_NICK_LEN, so in case it is garbled, it should be
// skipped by this code.
let have_channel = channels.contains(&privmsg.channel);
let is_dm = !privmsg.channel.starts_with('#') && privmsg.channel.len() <= MAX_NICK_LEN;

if have_channel || is_dm {
// Add the nickname to the list of nicks on the channel, if it's a channel.
let mut chans_lock = self.server.channels.write().await;
if let Some(chan) = chans_lock.get_mut(&privmsg.channel) {
chan.nicks.insert(privmsg.nick.clone());
}
drop(chans_lock);
// Add the nickname to the list of nicks on the channel, if it's a channel.
let mut chans_lock = self.server.channels.write().await;
if let Some(chan) = chans_lock.get_mut(&privmsg.channel) {
chan.nicks.insert(privmsg.nick.clone());
}
drop(chans_lock);

// Handle message lines individually
for line in privmsg.msg.lines() {
// Skip empty lines
if line.is_empty() {
continue
}
// Handle message lines individually
for line in privmsg.msg.lines() {
// Skip empty lines
if line.is_empty() {
continue
}

// Format the message
let msg = format!("PRIVMSG {} :{}", privmsg.channel, line);
// Format the message
let msg = format!("PRIVMSG {} :{}", privmsg.channel, line);

// Send it to the client
let reply = ReplyType::Client((privmsg.nick.clone(), msg));
if let Err(e) = self.reply(&mut writer, &reply).await {
error!("[IRC CLIENT] Failed writing PRIVMSG to client: {}", e);
continue
}
// Send it to the client
let reply = ReplyType::Client((privmsg.nick.clone(), msg));
if let Err(e) = self.reply(&mut writer, &reply).await {
error!("[IRC CLIENT] Failed writing PRIVMSG to client: {}", e);
continue
}
}

// Mark the message as seen for this USER
if let Err(e) = self.mark_seen(&event_id).await {
error!("[IRC CLIENT] (multiplex_connection) self.mark_seen({}) failed: {}", event_id, e);
return Err(e)
}
// Mark the message as seen for this USER
if let Err(e) = self.mark_seen(&event_id).await {
error!("[IRC CLIENT] (multiplex_connection) self.mark_seen({}) failed: {}", event_id, e);
return Err(e)
}
}
}
Expand Down

0 comments on commit f861a0d

Please sign in to comment.