Added QUIT and PART

This commit is contained in:
famfo 2021-11-07 20:10:01 +01:00 committed by Yash Karandikar
parent ae1074c601
commit ef57dc3ba3
3 changed files with 90 additions and 4 deletions

View file

@ -1,4 +1,4 @@
channels = ["#main", "#main2"] channels = ["#main", "#circe"]
host = "192.168.1.28" host = "192.168.1.28"
mode = "+B" mode = "+B"
nickname = "circe" nickname = "circe"

View file

@ -89,6 +89,14 @@ pub enum Command {
NICK(String), NICK(String),
/// Everything that is not a command /// Everything that is not a command
OTHER(String), OTHER(String),
/// Leaves channels
/// ```no_run
/// # use circe::*
/// # let mut client = Client::new(Config::from_toml("config.toml")?)?;
/// client.write_part(Command::PART("#main".to_string()))?;
/// # Ok::<(), std::io::Error>(())
/// ```
PART(String),
#[doc(hidden)] #[doc(hidden)]
PING(String), PING(String),
#[doc(hidden)] #[doc(hidden)]
@ -106,6 +114,14 @@ pub enum Command {
/// Message /// Message
String, String,
), ),
/// Leaves the IRC and shuts down the TCP stream
/// ```no_run
/// # use circe::*
/// # let mut client = Client::new(Config::from_toml("config.toml")?)?;
/// client.write_command(Command::QUIT("Leaving...".to_string()))?;
/// # Ok::<(), std::io::Error>(())
/// ```
QUIT(String),
#[doc(hidden)] #[doc(hidden)]
USER(String, String, String, String), USER(String, String, String, String),
} }
@ -285,6 +301,10 @@ impl Client {
"Cannot write commands of type OTHER", "Cannot write commands of type OTHER",
)); ));
} }
PART(target) => {
let formatted = format!("PART {}", target);
Cow::Owned(formatted) as Cow<str>
}
PING(target) => { PING(target) => {
let formatted = format!("PING {}", target); let formatted = format!("PING {}", target);
Cow::Owned(formatted) as Cow<str> Cow::Owned(formatted) as Cow<str>
@ -297,6 +317,10 @@ impl Client {
let formatted = format!("PRIVMSG {} {}", target, message); let formatted = format!("PRIVMSG {} {}", target, message);
Cow::Owned(formatted) as Cow<str> Cow::Owned(formatted) as Cow<str>
} }
QUIT(message) => {
let formatted = format!("QUIT {}", message);
Cow::Owned(formatted) as Cow<str>
}
USER(username, s1, s2, realname) => { USER(username, s1, s2, realname) => {
let formatted = format!("USER {} {} {} :{}", username, s1, s2, realname); let formatted = format!("USER {} {} {} :{}", username, s1, s2, realname);
Cow::Owned(formatted) as Cow<str> Cow::Owned(formatted) as Cow<str>
@ -378,6 +402,59 @@ impl Client {
} }
Ok(()) Ok(())
} }
/// Helper function for leaving channels.
/// This makes
/// ```no_run
/// # use circe::*;
/// # let mut client = Client::new(Config::from_toml("config.toml")?)?;
/// client.send_part("#main")?;
/// # Ok::<(), std::io::Error>(())
/// ```
///
/// equivalent to
///
/// ```no_run
/// # use circe::*
/// # let mut client = Client::new(Config::from_toml("config.toml")?)?;
/// client.write_part(Command::PART("#main".to_string()))?;
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn send_part(&mut self, target: &str) -> Result<(), Error> {
self.write_command(Command::PART(target.to_string()))?;
Ok(())
}
/// Helper function for leaving the IRC server and shutting down the TCP stream afterwards.
/// This makes
/// ```no_run
/// # use circe::*;
/// # let mut client = Client::new(Config::from_toml("config.toml")?)?;
/// client.send_quit(None)?;
/// # Ok::<(), std::io::Error>(())
/// ```
///
/// equivalent to
///
/// ```no_run
/// # use circe::*
/// # let mut client = Client::new(Config::from_toml("config.toml")?)?;
/// client.write_part(Command::QUIT("Leaving...".to_string()))?;
/// client.stream.shutdown(std::net::Shutdown::Both)?;
/// # Ok::<(), std::io::Error>(())
/// ```
pub fn send_quit(&mut self, message: Option<&str>) -> Result<(), Error> {
if let Some(message) = message {
self.write_command(Command::QUIT(message.to_string()))?;
} else {
self.write_command(Command::QUIT(
"circe 0.1.2 (https://crates.io/crates/circe)".to_string(),
))?;
}
self.stream.shutdown(std::net::Shutdown::Both)?;
Ok(())
}
} }
impl Config { impl Config {
@ -386,7 +463,7 @@ impl Config {
/// ```rust /// ```rust
/// # use circe::*; /// # use circe::*;
/// let config = Config::new( /// let config = Config::new(
/// vec!["#main", "#main2"], /// vec!["#main", "#circe"],
/// "192.168.178.100", /// "192.168.178.100",
/// Some("+B"), /// Some("+B"),
/// Some("IRSC"), /// Some("IRSC"),

View file

@ -12,10 +12,10 @@ macro_rules! loop_n {
fn main() -> Result<(), std::io::Error> { fn main() -> Result<(), std::io::Error> {
//let config = Config::from_toml("config.toml")?; //let config = Config::from_toml("config.toml")?;
let config = Config::new( let config = Config::new(
vec!["#main", "#main2"], vec!["#main", "#circe"],
"192.168.178.100", "192.168.178.100",
Some("+B"), Some("+B"),
Some("IRSC"), Some("circe"),
6667, 6667,
"circe", "circe",
); );
@ -28,8 +28,17 @@ fn main() -> Result<(), std::io::Error> {
print!("{}", line); print!("{}", line);
} }
if let Command::PRIVMSG(channel, message) = command { if let Command::PRIVMSG(channel, message) = command {
if message.contains("!quit") {
client.send_quit(None)?;
break;
}
if message.contains("!close") {
client.send_part("#main")?;
}
println!("PRIVMSG received: {} {}", channel, message); println!("PRIVMSG received: {} {}", channel, message);
} }
} }
} }
Ok(())
} }