Added JOIN to config

This commit is contained in:
famfo 2021-11-03 18:49:44 +01:00 committed by Yash Karandikar
parent f1eb3c337c
commit c399d2569b
2 changed files with 36 additions and 3 deletions

View file

@ -7,10 +7,12 @@ pub struct Client {
config: Config, config: Config,
} }
#[derive(Clone)]
pub struct Config { pub struct Config {
username: String, username: String,
nickname: String, nickname: String,
mode: String, mode: String,
channels: Box<[&'static str]>,
} }
#[derive(Debug)] #[derive(Debug)]
@ -26,6 +28,7 @@ pub enum Command {
CAP(CapMode), CAP(CapMode),
USER(String, String, String, String), USER(String, String, String, String),
NICK(String), NICK(String),
JOIN(String),
OTHER(String), OTHER(String),
} }
@ -59,6 +62,21 @@ impl Client {
))?; ))?;
self.write_command(Command::NICK(self.config.nickname.clone()))?; self.write_command(Command::NICK(self.config.nickname.clone()))?;
loop {
if let Ok(ref command) = self.read() {
if let Command::OTHER(line) = command {
if line.contains("001") {
break;
}
}
}
}
let config = self.config.clone();
for channel in config.channels.iter() {
self.write_command(Command::JOIN(channel.to_string()))?;
}
Ok(()) Ok(())
} }
@ -119,6 +137,10 @@ impl Client {
let formatted = format!("PONG {}", code); let formatted = format!("PONG {}", code);
Cow::Owned(formatted) as Cow<str> Cow::Owned(formatted) as Cow<str>
} }
JOIN(channel) => {
let formatted = format!("JOIN {}", channel);
Cow::Owned(formatted) as Cow<str>
}
OTHER(_) => { OTHER(_) => {
return Err(std::io::Error::new( return Err(std::io::Error::new(
std::io::ErrorKind::Other, std::io::ErrorKind::Other,
@ -134,11 +156,17 @@ impl Client {
} }
impl Config { impl Config {
pub fn new(username: &str, nickname: Option<&str>, mode: Option<&str>) -> Self { pub fn new(
username: &str,
nickname: Option<&str>,
mode: Option<&str>,
channels: Box<[&'static str]>,
) -> Self {
Self { Self {
username: username.into(), username: username.into(),
nickname: nickname.unwrap_or(username).into(), nickname: nickname.unwrap_or(username).into(),
mode: mode.unwrap_or("").into(), mode: mode.unwrap_or("").into(),
channels: channels.into(),
} }
} }
} }

View file

@ -10,9 +10,14 @@ macro_rules! loop_n {
} }
fn main() -> Result<(), std::io::Error> { fn main() -> Result<(), std::io::Error> {
let config = Config::new("test", Some("test"), Some("+B")); let config = Config::new(
"test",
Some("test"),
Some("+B"),
Box::new(["#main", "#main2"]),
);
let mut client = let mut client =
Client::new("192.168.1.28", 6667, config).expect("Unable to connect to IRC server"); Client::new("192.168.178.100", 6667, config).expect("Unable to connect to IRC server");
client.identify()?; client.identify()?;
loop { loop {