Changed PING in from_str in Command
This commit is contained in:
parent
37f6c7885f
commit
91a7db6c48
42
Cargo.lock
generated
42
Cargo.lock
generated
|
@ -2,48 +2,6 @@
|
|||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "0.7.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "irsc"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.5.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.6.25"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
|
||||
|
|
|
@ -6,5 +6,4 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
regex = "1"
|
||||
lazy_static = "1.4.0"
|
||||
|
||||
|
|
42
src/lib.rs
42
src/lib.rs
|
@ -1,12 +1,6 @@
|
|||
use lazy_static::lazy_static;
|
||||
use regex::Regex;
|
||||
use std::borrow::Cow;
|
||||
use std::io::{Error, Read, Write};
|
||||
use std::net::TcpStream;
|
||||
use std::borrow::Cow;
|
||||
|
||||
lazy_static! {
|
||||
static ref PING_RE: Regex = Regex::new(r"^PING\s*:").unwrap();
|
||||
}
|
||||
|
||||
pub struct Client {
|
||||
stream: TcpStream,
|
||||
|
@ -22,23 +16,22 @@ pub struct Config {
|
|||
|
||||
pub enum CapMode {
|
||||
LS,
|
||||
END
|
||||
END,
|
||||
}
|
||||
|
||||
pub enum Command {
|
||||
PING(String),
|
||||
CAP(CapMode),
|
||||
USER(String, String, String, String),
|
||||
NICK(String)
|
||||
NICK(String),
|
||||
}
|
||||
|
||||
impl Command {
|
||||
fn from_str(s: &str) -> Option<Self> {
|
||||
let new = s.trim();
|
||||
|
||||
if new.contains("PING") {
|
||||
let command = String::from(PING_RE.replace_all(new, ""));
|
||||
|
||||
if new.starts_with("PING") {
|
||||
let command: String = String::from(new.split_whitespace().collect::<Vec<&str>>()[1]);
|
||||
return Some(Self::PING(command));
|
||||
}
|
||||
|
||||
|
@ -50,13 +43,22 @@ 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 })
|
||||
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::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))?;
|
||||
}
|
||||
|
@ -74,7 +76,7 @@ impl Client {
|
|||
println!("error occured {}", e);
|
||||
|
||||
return None;
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
Some(String::from_utf8_lossy(&buffer).into())
|
||||
|
@ -103,20 +105,20 @@ impl Client {
|
|||
use CapMode::*;
|
||||
Cow::Borrowed(match mode {
|
||||
LS => "CAP LS 302",
|
||||
END => "CAP END"
|
||||
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>
|
||||
}
|
||||
_ => Cow::Borrowed("") as Cow<str>,
|
||||
};
|
||||
|
||||
self.write(&computed)?;
|
||||
|
|
Loading…
Reference in a new issue