Actually respond to PINGs
This commit is contained in:
parent
213aa0c62f
commit
f1eb3c337c
30
src/lib.rs
30
src/lib.rs
|
@ -13,28 +13,32 @@ pub struct Config {
|
||||||
mode: String,
|
mode: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum CapMode {
|
pub enum CapMode {
|
||||||
LS,
|
LS,
|
||||||
END,
|
END,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum Command {
|
pub enum Command {
|
||||||
PING(String),
|
PING(String),
|
||||||
|
PONG(String),
|
||||||
CAP(CapMode),
|
CAP(CapMode),
|
||||||
USER(String, String, String, String),
|
USER(String, String, String, String),
|
||||||
NICK(String),
|
NICK(String),
|
||||||
|
OTHER(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Command {
|
impl Command {
|
||||||
fn from_str(s: &str) -> Option<Self> {
|
fn from_str(s: &str) -> Self {
|
||||||
let new = s.trim();
|
let new = s.trim();
|
||||||
|
|
||||||
if new.starts_with("PING") {
|
if new.starts_with("PING") {
|
||||||
let command: String = String::from(new.split_whitespace().collect::<Vec<&str>>()[1]);
|
let command: String = String::from(new.split_whitespace().collect::<Vec<&str>>()[1]);
|
||||||
return Some(Self::PING(command));
|
return Self::PING(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
Self::OTHER(new.to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,12 +74,12 @@ impl Client {
|
||||||
Some(String::from_utf8_lossy(&buffer).into())
|
Some(String::from_utf8_lossy(&buffer).into())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read(&mut self) -> Option<Command> {
|
pub fn read(&mut self) -> Result<Command, ()> {
|
||||||
if let Some(string) = self.read_string() {
|
if let Some(string) = self.read_string() {
|
||||||
return Command::from_str(&string);
|
return Ok(Command::from_str(&string));
|
||||||
}
|
}
|
||||||
|
|
||||||
None // if it's none, there's no new messages
|
Err(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write(&mut self, data: &str) -> Result<(), Error> {
|
fn write(&mut self, data: &str) -> Result<(), Error> {
|
||||||
|
@ -107,10 +111,20 @@ impl Client {
|
||||||
let formatted = format!("NICK {}", nickname);
|
let formatted = format!("NICK {}", nickname);
|
||||||
Cow::Owned(formatted) as Cow<str>
|
Cow::Owned(formatted) as Cow<str>
|
||||||
}
|
}
|
||||||
PING(ping) => {
|
PING(code) => {
|
||||||
let formatted = format!("PONG {}", ping);
|
let formatted = format!("PING {}", code);
|
||||||
Cow::Owned(formatted) as Cow<str>
|
Cow::Owned(formatted) as Cow<str>
|
||||||
}
|
}
|
||||||
|
PONG(code) => {
|
||||||
|
let formatted = format!("PONG {}", code);
|
||||||
|
Cow::Owned(formatted) as Cow<str>
|
||||||
|
}
|
||||||
|
OTHER(_) => {
|
||||||
|
return Err(std::io::Error::new(
|
||||||
|
std::io::ErrorKind::Other,
|
||||||
|
"Cannot write commands of type OTHER",
|
||||||
|
));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
self.write(&computed)?;
|
self.write(&computed)?;
|
||||||
|
|
13
src/main.rs
13
src/main.rs
|
@ -1,4 +1,4 @@
|
||||||
use irsc::{Client, Config};
|
use irsc::{Client, Command, Config};
|
||||||
|
|
||||||
#[allow(unused_macros)]
|
#[allow(unused_macros)]
|
||||||
macro_rules! loop_n {
|
macro_rules! loop_n {
|
||||||
|
@ -12,12 +12,17 @@ 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"));
|
||||||
let mut client =
|
let mut client =
|
||||||
Client::new("192.168.178.100", 6667, config).expect("Unable to connect to IRC server");
|
Client::new("192.168.1.28", 6667, config).expect("Unable to connect to IRC server");
|
||||||
client.identify()?;
|
client.identify()?;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if let Some(line) = client.read_string() {
|
if let Ok(ref command) = client.read() {
|
||||||
print!("{}", line);
|
if let Command::PING(code) = command {
|
||||||
|
client.write_command(Command::PONG(code.to_string()))?;
|
||||||
|
}
|
||||||
|
if let Command::OTHER(line) = command {
|
||||||
|
print!("{}", line);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue