async-circe/src/commands.rs

280 lines
7.7 KiB
Rust
Raw Permalink Normal View History

2022-01-03 09:42:26 -06:00
//! IRC commands
//! - commands that can be recived from a server
//! - commands that can be send to the server
#[doc(hidden)]
#[derive(Debug)]
pub enum CapMode {
LS,
END,
}
2022-01-03 09:42:26 -06:00
/// Commands that can be send or recived from an IRC server.
#[derive(Debug)]
pub enum Command {
// TODO:
// SERVICE <nickname> <reserved> <distribution> <type> <reserved> <info>
// SQUIT <server> <comment>
//
2022-01-03 09:42:26 -06:00
/// Request information about the admin of a given server.
2022-01-05 04:53:57 -06:00
/// ```run_fut
2022-01-03 09:42:26 -06:00
/// # let config = Default::default();
/// # let mut client = Client::new(config).await?;
/// # client.identify().await?;
/// client.admin("libera.chat").await?;
/// # Ok(())
/// ```
2022-01-03 09:42:26 -06:00
/// # Errors
/// Returns IO errors from the TcpStream.
ADMIN(
/// Target
String,
),
2022-01-03 09:42:26 -06:00
/// Set the status of the client.
2022-01-05 04:53:57 -06:00
/// ```run_fut
2022-01-03 09:42:26 -06:00
/// # let config = Default::default();
/// # let mut client = Client::new(config).await?;
/// # client.identify().await?;
/// client.away("afk").await?;
/// # Ok(())
/// ```
AWAY(
/// Message
String,
),
#[doc(hidden)]
CAP(CapMode),
2022-01-03 09:42:26 -06:00
/// Invite someone to a channel.
2022-01-05 04:53:57 -06:00
/// ```run_fut
2022-01-03 09:42:26 -06:00
/// # let config = Default::default();
/// # let mut client = Client::new(config).await?;
/// # client.identify().await?;
/// client.invite("liblemonirc", "#async-circe").await?;
2022-01-03 09:42:26 -06:00
/// # Ok(())
/// ```
INVITE(
/// User
String,
/// Channel
String,
),
2022-01-03 09:42:26 -06:00
/// Join a channel.
2022-01-05 04:53:57 -06:00
/// ```run_fut
2022-01-03 09:42:26 -06:00
/// # let config = Default::default();
/// # let mut client = Client::new(config).await?;
/// # client.identify().await?;
/// client.join("#chaos").await?;
2022-01-03 09:42:26 -06:00
/// # Ok(())
/// ```
JOIN(
/// Channel
String,
),
2022-01-03 09:42:26 -06:00
/// List available channels on an IRC, or users in a channel.
2022-01-05 04:53:57 -06:00
/// ```run_fut
2022-01-03 09:42:26 -06:00
/// # let config = Default::default();
/// # let mut client = Client::new(config).await?;
/// # client.identify().await?;
2021-12-27 12:45:53 -06:00
/// client.list(None, None).await?;
2022-01-03 09:42:26 -06:00
/// # Ok(())
/// ```
LIST(
/// Channel
Option<String>,
/// Server to foreward request to
Option<String>,
),
2022-01-03 09:42:26 -06:00
/// Set the mode for a user.
2022-01-05 04:53:57 -06:00
/// ```run_fut
2022-01-03 09:42:26 -06:00
/// # let config = Default::default();
/// # let mut client = Client::new(config).await?;
/// # client.identify().await?;
2021-12-27 12:45:53 -06:00
/// client.mode("test", Some("+B")).await?;
2022-01-03 09:42:26 -06:00
/// # Ok(())
/// ```
MODE(
/// Channel
String,
/// Mode
Option<String>,
),
2022-01-03 09:42:26 -06:00
/// Get all the people online in channels.
2022-01-05 04:53:57 -06:00
/// ```run_fut
2022-01-03 09:42:26 -06:00
/// # let config = Default::default();
/// # let mut client = Client::new(config).await?;
/// # client.identify().await?;
/// client.names("#chaos,#async-circe", None).await?;
2022-01-03 09:42:26 -06:00
/// # Ok(())
/// ```
NAMES(
/// Channel
String,
2022-01-11 04:16:06 -06:00
/// User
String,
),
2022-01-03 09:42:26 -06:00
/// Change your nickname on a server.
2022-01-05 04:53:57 -06:00
/// ```run_fut
2022-01-03 09:42:26 -06:00
/// # let config = Default::default();
/// # let mut client = Client::new(config).await?;
/// # client.identify().await?;
/// client.nick("Not async-circe").await?;
/// # Ok(())
/// ```
NICK(
/// Nickname
String,
),
/// Authentificate as an operator on a server.
2022-01-05 04:53:57 -06:00
/// ```run_fut
2022-01-03 09:42:26 -06:00
/// # let config = Default::default();
/// # let mut client = Client::new(config).await?;
/// # client.identify().await?;
2021-12-27 12:45:53 -06:00
/// client.oper("username", "password").await?;
2022-01-03 09:42:26 -06:00
/// # Ok(())
/// ```
OPER(
/// Username
String,
/// Password
String,
),
/// Everything that is not a command
OTHER(String),
2022-01-03 09:42:26 -06:00
/// Leave a channel.
2022-01-05 04:53:57 -06:00
/// ```run_fut
2022-01-03 09:42:26 -06:00
/// # let config = Default::default();
/// # let mut client = Client::new(config).await?;
/// # client.identify().await?;
/// client.part("#chaos").await?;
2022-01-03 09:42:26 -06:00
/// # Ok(())
/// ```
PART(
/// Target
String,
),
#[doc(hidden)]
PASS(String),
2022-01-03 09:42:26 -06:00
/// Tests the presence of a connection to a server.
2022-01-05 04:53:57 -06:00
/// ```run_fut
2022-01-03 09:42:26 -06:00
/// # let config = Default::default();
/// # let mut client = Client::new(config).await?;
/// # client.identify().await?;
/// client.ping("libera.chat", None).await?;
/// # Ok(())
/// ```
PING(String),
#[doc(hidden)]
2022-01-11 04:16:06 -06:00
PONG(String, String),
/// Message send in a channel
PRIVMSG(
2021-11-11 11:32:32 -06:00
/// Source Nickname
String,
/// Channel
String,
/// Message
String,
),
2022-01-03 09:42:26 -06:00
/// Leave the IRC server you are connected to.
2022-01-05 04:53:57 -06:00
/// ```run_fut
2022-01-03 09:42:26 -06:00
/// # let config = Default::default();
/// # let mut client = Client::new(config).await?;
/// # client.identify().await?;
/// client.quit(None).await?;
/// # Ok(())
/// ```
QUIT(
/// Leave message
String,
),
2022-01-03 09:42:26 -06:00
/// Get the topic of a channel.
/// # Example
2022-01-05 04:53:57 -06:00
/// ```run_fut
2022-01-03 09:42:26 -06:00
/// # let config = Default::default();
/// # let mut client = Client::new(config).await?;
/// # client.identify().await?;
/// client.topic("#chaos", None).await?;
2022-01-03 09:42:26 -06:00
/// # Ok(())
/// ```
/// Set the topic of a channel.
/// # Example
2022-01-05 04:53:57 -06:00
/// ```run_fut
2022-01-03 09:42:26 -06:00
/// # let config = Default::default();
/// # let mut client = Client::new(config).await?;
/// # client.identify().await?;
/// client.topic("#chaos", Some("main channel")).await?;
2022-01-03 09:42:26 -06:00
/// # Ok(())
/// ```
2022-01-03 09:42:26 -06:00
/// # Errors
/// Returns IO errors from the TcpStream.
TOPIC(
/// Channel
String,
/// Topic
2022-01-10 10:33:35 -06:00
String,
),
2022-01-11 04:16:06 -06:00
/// User the set the topic in a channel at a time
TOPIC_BY(String, String),
2022-01-10 10:33:35 -06:00
#[doc(hidden)]
USER(String, String, String, String),
2022-01-14 07:17:33 -06:00
#[doc(hidden)]
WELCOME(),
}
impl Command {
2022-01-11 11:50:44 -06:00
/// Creates a Command from a `&str`. Currently `[PRIVMSG]` `[TOPIC]` `[NAMES]` and `[PONG]` are supported.
2021-11-16 15:18:22 -06:00
///
/// # Panics
2021-12-06 09:08:29 -06:00
/// This function will panic if the ``IRCd`` sends malformed messages. Please contact the
/// maintainer of your ``IRCd`` if this happens.
2022-01-11 11:50:44 -06:00
pub async fn command_from_str(s: String) -> Self {
let new = s.trim();
tracing::trace!("{}", new);
let parts: Vec<&str> = new.split_whitespace().collect();
2021-11-16 15:18:22 -06:00
if parts.get(0) == Some(&"PING") {
2022-01-12 13:56:10 -06:00
return Self::PING(parts[1].to_string());
2022-01-11 07:31:05 -06:00
}
match parts.get(1) {
Some(&"PRIVMSG") => {
let nick_realname = parts[0];
let nick: String;
2021-12-28 08:06:59 -06:00
2022-01-11 07:31:05 -06:00
let index = nick_realname.chars().position(|c| c == '!');
if let Some(index) = index {
if index > 0 {
2022-01-12 13:56:10 -06:00
nick = (nick_realname[1..index]).to_string();
2022-01-11 07:31:05 -06:00
} else {
nick = String::new();
}
2021-12-27 12:24:31 -06:00
} else {
2021-12-28 08:06:59 -06:00
nick = String::new();
2021-12-27 12:24:31 -06:00
}
2021-12-28 08:06:59 -06:00
2022-01-11 07:31:05 -06:00
let msg = parts[3..].join(" ");
2022-01-12 13:56:10 -06:00
Self::PRIVMSG(nick, parts[2].to_string(), (msg[1..]).to_string())
2022-01-11 07:31:05 -06:00
}
Some(&"331") | Some(&"332") => {
let topic = parts[4..].join(" ");
2022-01-12 13:56:10 -06:00
Self::TOPIC(parts[3].to_string(), (topic[1..]).to_string())
2022-01-11 07:31:05 -06:00
}
Some(&"333") => {
2022-01-12 13:56:10 -06:00
Self::TOPIC_BY(parts[4].to_string(), parts[5].to_string())
2022-01-11 07:31:05 -06:00
}
Some(&"353") => {
let user = parts[5..].join(" ");
2022-01-12 13:56:10 -06:00
Self::NAMES(parts[4].to_string(), (user[1..]).to_string())
2022-01-11 07:31:05 -06:00
}
Some(&"PONG") => {
let server = parts[3];
2022-01-12 13:56:10 -06:00
Self::PONG(parts[2].to_string(), (server[1..]).to_string())
2022-01-11 07:31:05 -06:00
}
2022-01-14 07:17:33 -06:00
Some(&"001") => {
Self::WELCOME()
}
2022-01-11 07:31:05 -06:00
_ => Self::OTHER(new.to_string()),
}
}
}