Added basic message handling, and fixed a bug in message parsing.

This commit is contained in:
Aaron Weiss 2014-11-03 02:54:40 -05:00
parent 4479c59e79
commit 16f29f179a
3 changed files with 16 additions and 4 deletions

View file

@ -21,6 +21,6 @@ fn main() {
let server = IrcServer::from_config(config).unwrap();
identify(&server).unwrap();
for message in server.iter() {
println!("{}", message.into_string())
println!("{}", message);
}
}

View file

@ -64,7 +64,7 @@ impl FromStr for Message {
None
};
let suffix = if state.contains(":") {
let suffix = state.find(':').map(|i| state[i+1..state.len()-1]);
let suffix = state.find(':').map(|i| state[i+1..state.len()-2]);
state = state.find(':').map_or("", |i| state[..i]);
suffix
} else {

View file

@ -2,7 +2,7 @@
#![experimental]
use std::io::{BufferedReader, BufferedWriter, IoResult, TcpStream};
use conn::Connection;
use data::command::Command;
use data::command::{Command, PONG};
use data::config::Config;
use data::kinds::{IrcReader, IrcWriter};
use data::message::Message;
@ -69,6 +69,14 @@ impl<'a, T, U> IrcServer<'a, T, U> where T: IrcWriter, U: IrcReader {
config: config
})
}
fn handle_message(&self, message: &Message) {
if message.command[] == "PING" {
self.send(PONG(message.args[0][], None)).unwrap();
println!("PING! PONG!");
}
/* TODO: implement more message handling */
}
}
/// An Iterator over an IrcServer's incoming Messages
@ -92,7 +100,11 @@ impl<'a, T, U> Iterator<Message> for ServerIterator<'a, T, U> where T: IrcWriter
let line = self.server.conn.recv();
match line {
Err(_) => None,
Ok(msg) => from_str(msg[])
Ok(msg) => {
let message = from_str(msg[]);
self.server.handle_message(message.as_ref().unwrap());
message
}
}
}
}