Implemented basic IRC message handling.
Fun fact: The functionality is now similar to 6f3f8614d7
.
This commit is contained in:
parent
d8ee914654
commit
66308f678f
2 changed files with 60 additions and 46 deletions
|
@ -1 +1 @@
|
||||||
Subproject commit 5d134f716dfe205c6ebef627f08d8330073cbf1d
|
Subproject commit bf53e62f1713dfe83198f75992cc0e889de398ff
|
104
src/main.rs
104
src/main.rs
|
@ -65,10 +65,13 @@ fn get_config<P: AsRef<Path>>(config_path: P)
|
||||||
Ok(Value::from(config))
|
Ok(Value::from(config))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_tcp_stream (config: &Value) -> Result<TcpStream, Box<dyn std::error::Error>> {
|
fn get_tcp_stream (config: &Value)
|
||||||
|
-> Result<TcpStream, Box<dyn std::error::Error>> {
|
||||||
let address = format!("{}:{}",
|
let address = format!("{}:{}",
|
||||||
config["server"].as_str().ok_or("Could not get server adress from config")?,
|
config.get("server").unwrap().as_str()
|
||||||
config["port"].as_str().ok_or("Could not get port from config")?
|
.ok_or("Could not get server adress from config")?,
|
||||||
|
config.get("port").unwrap().as_str()
|
||||||
|
.ok_or("Could not get port from config")?
|
||||||
);
|
);
|
||||||
println!("Connectiing to: {}", address);
|
println!("Connectiing to: {}", address);
|
||||||
Ok(TcpStream::connect(address)?)
|
Ok(TcpStream::connect(address)?)
|
||||||
|
@ -85,14 +88,10 @@ fn get_tls_stream (config: &Value)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_irc_identify_messages (
|
fn get_irc_identify_messages (config: &Value)
|
||||||
queue: VecDeque<Message>,
|
-> Result<VecDeque<Message>, Box<dyn std::error::Error>> {
|
||||||
pass: Option<String>,
|
let mut queue = VecDeque::new();
|
||||||
nick: String,
|
|
||||||
user: String,
|
|
||||||
real_name: String
|
|
||||||
) -> VecDeque<Message>{
|
|
||||||
let mut queue = queue;
|
|
||||||
queue.push_back(Message::from(
|
queue.push_back(Message::from(
|
||||||
Command::CAP(
|
Command::CAP(
|
||||||
None,
|
None,
|
||||||
|
@ -100,22 +99,40 @@ fn get_irc_identify_messages (
|
||||||
None,
|
None,
|
||||||
None
|
None
|
||||||
)));
|
)));
|
||||||
match pass {
|
|
||||||
|
match config.get("password") {
|
||||||
Some(p) => queue.push_back(Message::from(
|
Some(p) => queue.push_back(Message::from(
|
||||||
Command::PASS(p)
|
Command::PASS(
|
||||||
|
String::from(p.as_str().ok_or("Could not parse password.")?)
|
||||||
|
)
|
||||||
)),
|
)),
|
||||||
None => ()
|
None => ()
|
||||||
}
|
}
|
||||||
queue.push_back(Message::from(
|
|
||||||
Command::NICK(nick)
|
let nick: String;
|
||||||
));
|
match config.get("nickname") {
|
||||||
queue.push_back(Message::from(
|
Some(n) => {
|
||||||
Command::USER(
|
nick = String::from(n.as_str().ok_or("Could not parse nickname.")?);
|
||||||
user,
|
queue.push_back(Message::from(Command::NICK(nick.clone())));
|
||||||
"0".to_owned(),
|
},
|
||||||
real_name)
|
None => return Err("No nickname supplied!".into()),
|
||||||
));
|
}
|
||||||
queue
|
|
||||||
|
match config.get("username") {
|
||||||
|
Some(u) => queue.push_back(Message::from(
|
||||||
|
Command::USER(
|
||||||
|
String::from(u.as_str().ok_or("Could not parse username.")?),
|
||||||
|
"0".to_owned(),
|
||||||
|
String::from(u.as_str().ok_or("Could not parse username.")?)
|
||||||
|
)
|
||||||
|
)),
|
||||||
|
None => queue.push_back(Message::from(
|
||||||
|
// nick.clone() is only used once because the value
|
||||||
|
// can be moved the second time
|
||||||
|
Command::USER(nick.clone(), "0".to_owned(), nick)
|
||||||
|
))
|
||||||
|
}
|
||||||
|
Ok(queue)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -135,20 +152,14 @@ fn main() {
|
||||||
stream = Box::new(IrcStream::new(get_tcp_stream(&config).unwrap()));
|
stream = Box::new(IrcStream::new(get_tcp_stream(&config).unwrap()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut message_queue: VecDeque<Message> = VecDeque::new();
|
let mut message_queue = get_irc_identify_messages(&config).unwrap();
|
||||||
|
|
||||||
message_queue = get_irc_identify_messages(
|
|
||||||
message_queue,
|
|
||||||
None,
|
|
||||||
config["nickname"].to_string(),
|
|
||||||
config["nickname"].to_string(),
|
|
||||||
config["nickname"].to_string()
|
|
||||||
);
|
|
||||||
|
|
||||||
while let Some(message) = message_queue.pop_front() {
|
while let Some(message) = message_queue.pop_front() {
|
||||||
stream.write(message).unwrap();
|
stream.write(message).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stream.flush().unwrap();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let message = match stream.read() {
|
let message = match stream.read() {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
|
@ -159,20 +170,23 @@ fn main() {
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
// let sender = match message.source_nickname() {
|
let sender = match message.clone().source_nickname() {
|
||||||
// Some(s) => s.clone(),
|
Some(s) => String::from(s),
|
||||||
// None => "anonymous"
|
None => String::from("anonymous")
|
||||||
// };
|
};
|
||||||
match message.command {
|
match message.clone().command {
|
||||||
Command::PING(_s1, _s2) => stream.write(Message::from(
|
Command::PING(ref data, _) => {
|
||||||
Command::PONG(String::from("PONG"), None)
|
stream.write(Message::from(
|
||||||
)).unwrap(),
|
Command::PONG(data.to_owned(), None)
|
||||||
Command::PRIVMSG(rec, msg) => println!(
|
)).unwrap();
|
||||||
"someone -> {}: {}",
|
},
|
||||||
&rec,
|
Command::PRIVMSG(ref rec, ref msg) => println!(
|
||||||
&msg
|
"{} -> {}: {}",
|
||||||
|
rec,
|
||||||
|
msg,
|
||||||
|
sender
|
||||||
),
|
),
|
||||||
_ => ()
|
_ => println!("Other message: {}", message.clone().to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue