Skip to content

Commit

Permalink
add HELP command
Browse files Browse the repository at this point in the history
  • Loading branch information
internet-catte committed Aug 10, 2024
1 parent 9de3995 commit 877a453
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
74 changes: 74 additions & 0 deletions sable_ircd/src/command/handlers/help.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use super::*;

use itertools::Itertools;

#[command_handler("HELP", "UHELP")]
/// HELP [<topic>]
///
/// HELP displays information for topic requested.
/// If no topic is requested, it will list available
/// help topics.
fn help_handler(
command: &dyn Command,
response: &dyn CommandResponse,
server: &ClientServer,
source: UserSource,
topic: Option<&str>,
) -> CommandResult {
// TODO: oper help? (and if oper help is on the same command, UHELP like solanum?)
// TODO: non-command help topics
let is_oper = command.command().to_ascii_uppercase() != "UHELP" && source.is_oper();

match topic {
Some(s) => {
let topic = s.to_ascii_uppercase();
let topic = topic
.split_once(' ')
.map_or(topic.clone(), |(t, _)| t.to_string());

if let Some(cmd) = server.get_command(&topic) {
if cmd.docs.len() > 0 {
// TODO
if cmd.restricted && is_oper {
response.numeric(make_numeric!(HelpNotFound, &topic));
return Ok(());
}
let mut lines = cmd.docs.iter();
response.numeric(make_numeric!(
HelpStart,
&topic,
lines.next().unwrap_or(&topic.as_str())
));
for line in lines {
response.numeric(make_numeric!(HelpText, &topic, line));
}
response.numeric(make_numeric!(EndOfHelp, &topic));
return Ok(());
}
}
response.numeric(make_numeric!(HelpNotFound, &topic));
}
None => {
let topic = "*";
response.numeric(make_numeric!(HelpStart, topic, "Available help topics:"));
response.numeric(make_numeric!(HelpText, topic, ""));
for chunk in &server
.iter_commands()
.filter_map(|(k, v)| {
if !v.restricted || is_oper {
Some(k.to_ascii_uppercase())
} else {
None
}
})
.sorted()
.chunks(4)
{
let line = format!("{:16}", chunk.format(" "));
response.numeric(make_numeric!(HelpText, topic, &line));
}
response.numeric(make_numeric!(EndOfHelp, topic));
}
};
Ok(())
}
1 change: 1 addition & 0 deletions sable_ircd/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ mod handlers {
mod ban;
mod cap;
mod chathistory;
mod help;
mod info;
mod invite;
mod join;
Expand Down
6 changes: 6 additions & 0 deletions sable_ircd/src/messages/numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ define_messages! {
371(Info) => { (line: &str) => ":{line}" },
374(EndOfInfo) => { () => ":End of /INFO list" },

704(HelpStart) => { (subj: &str, line: &str) => "{subj} :{line}" },
705(HelpText) => { (subj: &str, line: &str) => "{subj} :{line}" },
706(EndOfHelp) => { (subj: &str) => "{subj} :End of /HELP" },


401(NoSuchTarget) => { (unknown: &str) => "{unknown} :No such nick/channel" },
402(NoSuchServer) => { (server_name: &ServerName) => "{server_name} :No such server" },
Expand Down Expand Up @@ -125,6 +129,8 @@ define_messages! {

440(ServicesNotAvailable) => { () => ":Services are not available"},

524(HelpNotFound) => { (subj: &str) => "{subj} :No help available on this topic" },

// https://ircv3.net/specs/extensions/monitor
730(MonOnline) => { (content: &str ) => ":{content}" },
731(MonOffline) => { (content: &str ) => ":{content}" },
Expand Down

0 comments on commit 877a453

Please sign in to comment.