139 lines
3.4 KiB
Rust
139 lines
3.4 KiB
Rust
use std::borrow::Cow;
|
|
use std::io::{Error, Read, Write};
|
|
use std::net::TcpStream;
|
|
|
|
pub struct Client {
|
|
stream: TcpStream,
|
|
config: Config,
|
|
pub has_identified: bool,
|
|
}
|
|
|
|
pub struct Config {
|
|
username: String,
|
|
nickname: String,
|
|
mode: String,
|
|
}
|
|
|
|
pub enum CapMode {
|
|
LS,
|
|
END,
|
|
}
|
|
|
|
pub enum Command {
|
|
PING(String),
|
|
CAP(CapMode),
|
|
USER(String, String, String, String),
|
|
NICK(String),
|
|
}
|
|
|
|
impl Command {
|
|
fn from_str(s: &str) -> Option<Self> {
|
|
let new = s.trim();
|
|
|
|
if new.starts_with("PING") {
|
|
let command: String = String::from(new.split_whitespace().collect::<Vec<&str>>()[1]);
|
|
return Some(Self::PING(command));
|
|
}
|
|
|
|
None
|
|
}
|
|
}
|
|
|
|
impl Client {
|
|
pub fn new(host: &str, port: u16, config: Config) -> Result<Self, Error> {
|
|
let stream = TcpStream::connect(format!("{}:{}", host, port))?;
|
|
|
|
Ok(Self {
|
|
stream,
|
|
config,
|
|
has_identified: false,
|
|
})
|
|
}
|
|
|
|
pub fn try_identify(&mut self) -> Result<(), Error> {
|
|
if !self.has_identified {
|
|
self.write_command(Command::CAP(CapMode::LS))?;
|
|
self.write_command(Command::USER(
|
|
self.config.username.clone(),
|
|
"*".into(),
|
|
"*".into(),
|
|
self.config.username.clone(),
|
|
))?;
|
|
self.write_command(Command::NICK(self.config.nickname.clone()))?;
|
|
self.write_command(Command::CAP(CapMode::END))?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
// temporarily pub, change this later
|
|
pub fn read_string(&mut self) -> Option<String> {
|
|
let mut buffer = [0u8; 512];
|
|
|
|
match self.stream.read(&mut buffer) {
|
|
Ok(_) => {}
|
|
Err(e) => {
|
|
println!("error occured {}", e);
|
|
|
|
return None;
|
|
}
|
|
};
|
|
|
|
Some(String::from_utf8_lossy(&buffer).into())
|
|
}
|
|
|
|
pub fn read(&mut self) -> Option<Command> {
|
|
if let Some(string) = self.read_string() {
|
|
return Command::from_str(&string);
|
|
}
|
|
|
|
None // if it's none, there's no new messages
|
|
}
|
|
|
|
fn write(&mut self, data: &str) -> Result<(), Error> {
|
|
let bytes = self.stream.write(data.as_bytes())?;
|
|
println!("Wrote {} bytes", bytes);
|
|
// self.stream.flush()?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn write_command(&mut self, command: Command) -> Result<(), Error> {
|
|
use Command::*;
|
|
let computed = match command {
|
|
CAP(mode) => {
|
|
use CapMode::*;
|
|
Cow::Borrowed(match mode {
|
|
LS => "CAP LS 302",
|
|
END => "CAP END",
|
|
}) as Cow<str>
|
|
}
|
|
USER(username, s1, s2, realname) => {
|
|
let formatted = format!("USER {} {} {} :{}", username, s1, s2, realname);
|
|
|
|
Cow::Owned(formatted) as Cow<str>
|
|
}
|
|
NICK(nickname) => {
|
|
let formatted = format!("NICK {}", nickname);
|
|
|
|
Cow::Owned(formatted) as Cow<str>
|
|
}
|
|
_ => Cow::Borrowed("") as Cow<str>,
|
|
};
|
|
|
|
self.write(&computed)?;
|
|
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl Config {
|
|
pub fn new(username: &str, nickname: Option<&str>, mode: Option<&str>) -> Self {
|
|
Self {
|
|
username: username.into(),
|
|
nickname: nickname.unwrap_or(username).into(),
|
|
mode: mode.unwrap_or("").into(),
|
|
}
|
|
}
|
|
}
|