Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions sable_ircd/src/capability/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ macro_rules! define_capabilities {
(
$typename:ident
{
$( $cap:ident : $val:literal => ($name:literal, $def:literal) ),* $(,)?
$( $cap:ident : $bitmask:literal => ($name:literal, $value:expr, $def:literal) ),* $(,)?
}
) => {
#[derive(Clone,Copy,Debug,PartialEq,Eq,Serialize,Deserialize)]
#[derive(EnumIter)]
#[repr(u64)]
pub enum $typename
{
$( $cap = $val ),*
$( $cap = $bitmask ),*
}

impl $typename
{
/// Exhaustive list of all known capabilities
const ALL: &'static [ClientCapability] = &[ $(Self::$cap),* ];
/// Exhaustive list of all known capabilities and their IRCv3.2 value
const ALL: &'static [(ClientCapability, &'static [&'static str])] = &[ $((Self::$cap, $value)),* ];

/// On-the-wire name of the capability
pub fn name(self) -> &'static str
Expand All @@ -58,6 +58,10 @@ macro_rules! define_capabilities {
{
*self as u64
}

pub fn all() -> &'static [(ClientCapability, &'static [&'static str])] {
Self::ALL
}
}
};
}
Expand All @@ -66,20 +70,20 @@ define_capabilities! (
ClientCapability
{
// Stable caps
ServerTime: 0x02 => ("server-time", true),
EchoMessage: 0x04 => ("echo-message", true),
Sasl: 0x08 => ("sasl", false),
Batch: 0x10 => ("batch", true),
LabeledResponse: 0x20 => ("labeled-response", true),
UserhostInNames: 0x40 => ("userhost-in-names", true),
AwayNotify: 0x80 => ("away-notify", true),
AccountTag: 0x100 => ("account-tag", true),
ServerTime: 0x02 => ("server-time", &[], true),
EchoMessage: 0x04 => ("echo-message", &[], true),
Sasl: 0x08 => ("sasl", &[], false),
Batch: 0x10 => ("batch", &[], true),
LabeledResponse: 0x20 => ("labeled-response", &[], true),
UserhostInNames: 0x40 => ("userhost-in-names", &[], true),
AwayNotify: 0x80 => ("away-notify", &[], true),
AccountTag: 0x100 => ("account-tag", &[], true),

// Draft and experimental caps
ChatHistory: 0x1_0000 => ("draft/chathistory", true),
PersistentSession: 0x2_0000 => ("sable.libera.chat/persistent-session", true),
AccountRegistration: 0x4_0000 => ("draft/account-registration", true),
ChannelRename: 0x8_0000 => ("draft/channel-rename", true),
ChatHistory: 0x1_0000 => ("draft/chathistory", &[], true),
PersistentSession: 0x2_0000 => ("sable.libera.chat/persistent-session", &[], true),
AccountRegistration: 0x4_0000 => ("draft/account-registration", &[], true),
ChannelRename: 0x8_0000 => ("draft/channel-rename", &[], true),
}
);

Expand Down Expand Up @@ -118,6 +122,7 @@ impl ClientCapabilitySet {
pub fn iter<'a>(&'a self) -> impl Iterator<Item = ClientCapability> + 'a {
ClientCapability::ALL
.iter()
.map(|(cap, _value)| cap)
.cloned()
.filter(|cap| self.has(*cap))
}
Expand Down Expand Up @@ -157,6 +162,7 @@ impl AtomicCapabilitySet {
pub fn iter<'a>(&'a self) -> impl Iterator<Item = ClientCapability> + 'a {
ClientCapability::ALL
.iter()
.map(|(cap, _value)| cap)
.cloned()
.filter(|cap| self.has(*cap))
}
Expand Down
5 changes: 2 additions & 3 deletions sable_ircd/src/capability/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use itertools::Itertools;
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::sync::{atomic::AtomicBool, Arc};
use strum::IntoEnumIterator;

#[derive(Debug, Serialize, Deserialize)]
struct CapabilityEntry {
Expand All @@ -24,10 +23,10 @@ impl CapabilityRepository {
pub fn new() -> Self {
let mut supported_caps = Vec::new();

for cap in ClientCapability::iter() {
for &(cap, values) in ClientCapability::all().iter() {
supported_caps.push(CapabilityEntry {
cap,
values: RwLock::new(Vec::new()),
values: RwLock::new(values.iter().map(|v| v.to_string()).collect()),
available: AtomicBool::new(cap.is_default()),
});
}
Expand Down