210 lines
5.5 KiB
Rust
210 lines
5.5 KiB
Rust
use std::borrow::Cow;
|
|
use std::fs::File;
|
|
use std::io::{Error, Read, Write};
|
|
use std::net::TcpStream;
|
|
use std::path::Path;
|
|
|
|
use serde_derive::Deserialize;
|
|
|
|
pub struct Client {
|
|
stream: TcpStream,
|
|
config: Config,
|
|
}
|
|
|
|
#[derive(Clone, Deserialize, Default)]
|
|
pub struct Config {
|
|
username: String,
|
|
nickname: Option<String>,
|
|
mode: Option<String>,
|
|
channels: Box<[String]>,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum CapMode {
|
|
LS,
|
|
END,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum Command {
|
|
PING(String),
|
|
PONG(String),
|
|
CAP(CapMode),
|
|
USER(String, String, String, String),
|
|
NICK(String),
|
|
JOIN(String),
|
|
MODE(String, Option<String>),
|
|
OTHER(String),
|
|
}
|
|
|
|
impl Command {
|
|
fn from_str(s: &str) -> Self {
|
|
let new = s.trim();
|
|
|
|
if new.starts_with("PING") {
|
|
let command: String = String::from(new.split_whitespace().collect::<Vec<&str>>()[1]);
|
|
return Self::PING(command);
|
|
}
|
|
|
|
Self::OTHER(new.to_string())
|
|
}
|
|
}
|
|
|
|
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 })
|
|
}
|
|
|
|
pub fn identify(&mut self) -> Result<(), Error> {
|
|
self.write_command(Command::CAP(CapMode::END))?;
|
|
self.write_command(Command::USER(
|
|
self.config.username.clone(),
|
|
"*".into(),
|
|
"*".into(),
|
|
self.config.username.clone(),
|
|
))?;
|
|
if let Some(nick) = self.config.nickname.clone() {
|
|
self.write_command(Command::NICK(nick))?;
|
|
} else {
|
|
self.write_command(Command::NICK(self.config.username.clone()))?;
|
|
}
|
|
|
|
loop {
|
|
if let Ok(ref command) = self.read() {
|
|
if let Command::PING(code) = command {
|
|
self.write_command(Command::PONG(code.to_string()))?;
|
|
continue;
|
|
}
|
|
if let Command::OTHER(line) = command {
|
|
if line.contains("001") {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
let config = self.config.clone();
|
|
self.write_command(Command::MODE(config.username, config.mode))?;
|
|
for channel in config.channels.iter() {
|
|
self.write_command(Command::JOIN(channel.to_string()))?;
|
|
}
|
|
|
|
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(_) => return None,
|
|
};
|
|
|
|
Some(String::from_utf8_lossy(&buffer).into())
|
|
}
|
|
|
|
pub fn read(&mut self) -> Result<Command, ()> {
|
|
if let Some(string) = self.read_string() {
|
|
return Ok(Command::from_str(&string));
|
|
}
|
|
|
|
Err(())
|
|
}
|
|
|
|
fn write(&mut self, data: &str) -> Result<(), Error> {
|
|
let formatted = {
|
|
let new = format!("{}\r\n", data);
|
|
|
|
Cow::Owned(new) as Cow<str>
|
|
};
|
|
self.stream.write(formatted.as_bytes())?;
|
|
|
|
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>
|
|
}
|
|
PING(code) => {
|
|
let formatted = format!("PING {}", code);
|
|
Cow::Owned(formatted) as Cow<str>
|
|
}
|
|
PONG(code) => {
|
|
let formatted = format!("PONG {}", code);
|
|
Cow::Owned(formatted) as Cow<str>
|
|
}
|
|
JOIN(channel) => {
|
|
let formatted = format!("JOIN {}", channel);
|
|
Cow::Owned(formatted) as Cow<str>
|
|
}
|
|
MODE(target, mode) => {
|
|
let formatted = {
|
|
if let Some(mode) = mode {
|
|
format!("MODE {} {}", target, mode)
|
|
} else {
|
|
format!("MODE {}", target)
|
|
}
|
|
};
|
|
|
|
Cow::Owned(formatted) as Cow<str>
|
|
}
|
|
OTHER(_) => {
|
|
return Err(Error::new(
|
|
std::io::ErrorKind::Other,
|
|
"Cannot write commands of type OTHER",
|
|
));
|
|
}
|
|
};
|
|
|
|
self.write(&computed)?;
|
|
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl Config {
|
|
pub fn new(
|
|
username: &str,
|
|
nickname: Option<String>,
|
|
mode: Option<String>,
|
|
channels: Box<[String]>,
|
|
) -> Self {
|
|
Self {
|
|
username: username.into(),
|
|
nickname,
|
|
mode,
|
|
channels,
|
|
}
|
|
}
|
|
|
|
pub fn from_toml<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
|
|
let mut file = File::open(&path)?;
|
|
let mut data = String::new();
|
|
file.read_to_string(&mut data)?;
|
|
|
|
toml::from_str(&data).map_err(|e| {
|
|
use std::io::ErrorKind;
|
|
|
|
Error::new(ErrorKind::Other, format!("Invalid TOML: {}", e))
|
|
})
|
|
}
|
|
}
|