Skip to content

Commit

Permalink
Implement IRCv3 away-notify
Browse files Browse the repository at this point in the history
  • Loading branch information
progval authored and spb committed Sep 3, 2023
1 parent 8fd0f8d commit 1a8fbe5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions sable_ircd/src/capability/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ define_capabilities! (
Batch: 0x10 => ("batch", true),
LabeledResponse: 0x20 => ("labeled-response", true),
UserhostInNames: 0x40 => ("userhost-in-names", true),
AwayNotify: 0x80 => ("away-notify", true),

ChatHistory: 0x100 => ("draft/chathistory", true),
PersistentSession: 0x200 => ("sable.libera.chat/persistent-session", true),
Expand Down
2 changes: 2 additions & 0 deletions sable_ircd/src/messages/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use super::*;
use sable_macros::define_messages;

define_messages! {
Away => { (source, reason: &str) => ":{source} AWAY :{reason}" },
Unaway => { (source) => ":{source} AWAY" },
Cap => { (source, target, subcmd: &str, text: &str) => ":{source} CAP {target} {subcmd} :{text}" },
Nick => { (source, newnick: &Nickname) => ":{source} NICK {newnick}" },
Join => { (source, chan: &ChannelName) => ":{source} JOIN {chan}" },
Expand Down
18 changes: 17 additions & 1 deletion sable_ircd/src/messages/send_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,22 @@ impl SendHistoryItem for update::NewUser {
}

impl SendHistoryItem for update::UserAwayChange {
fn send_to(&self, conn: impl MessageSink, _from_entry: &HistoryLogEntry) -> HandleResult {
fn send_to(&self, conn: impl MessageSink, from_entry: &HistoryLogEntry) -> HandleResult {
if Some(self.user.user.id) == conn.user_id() {
// Echo back to the user
let message = match self.new_reason {
None => numeric::Unaway::new(),
Some(_) => numeric::NowAway::new(),
};
conn.send(message.format_for(&self.user, &self.user));
} else {
// Tell other users sharing a channel if they enabled away-notify
let message = match self.new_reason {
None => message::Unaway::new(&self.user),
Some(reason) => message::Away::new(&self.user, reason.value()),
};
let message = message.with_tags_from(from_entry);
conn.send(message.with_required_capabilities(ClientCapability::AwayNotify));
}

Ok(())
Expand Down Expand Up @@ -193,6 +202,13 @@ impl SendHistoryItem for update::ChannelJoin {
conn.send(msg);
}

if let Some(away_reason) = self.user.user.away_reason {
let message =
message::Away::new(&self.user, &away_reason.value()).with_tags_from(from_entry);

conn.send(message.with_required_capabilities(ClientCapability::AwayNotify));
}

Ok(())
}
}
Expand Down
17 changes: 16 additions & 1 deletion sable_network/src/node/update_receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,22 @@ impl<Policy: crate::policy::PolicyService> NetworkNode<Policy> {
entry: &HistoryLogEntry,
detail: &update::UserAwayChange,
) -> HandleResult {
self.notify_user(detail.user.user.id, entry.id);
let net = self.network();
let source = net.user(detail.user.user.id)?;

let mut notified = HashSet::new();

// Notify the source user themselves, even if they are not in any channel
notified.insert(detail.user.user.id);

for m1 in source.channels() {
let chan = m1.channel()?;
for m2 in chan.members() {
notified.insert(m2.user_id());
}
}

self.notify_users(notified, entry.id);
Ok(())
}

Expand Down

0 comments on commit 1a8fbe5

Please sign in to comment.