Added threading and beautified the code a bit.
This commit is contained in:
parent
8ab294277b
commit
500d87d65b
1 changed files with 42 additions and 35 deletions
75
src/main.rs
75
src/main.rs
|
@ -117,16 +117,19 @@ fn get_irc_join_messages(config: &Value, queue: VecDeque<Message>)
|
||||||
Ok(queue)
|
Ok(queue)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_message(message: Message) -> Option<Message> {
|
fn handle_message(message: Message) -> (Option<Message>, Option<[String; 4]>) {
|
||||||
let sender = match message.clone().source_nickname() {
|
let sender = match message.clone().source_nickname() {
|
||||||
Some(s) => String::from(s),
|
Some(s) => String::from(s),
|
||||||
None => String::from("anonymous")
|
None => String::from("anonymous")
|
||||||
};
|
};
|
||||||
match message.clone().command {
|
match message.clone().command {
|
||||||
Command::PING(ref data, _) => {
|
Command::PING(ref data, _) => {
|
||||||
return Some(Message::from(
|
return (
|
||||||
|
Some(Message::from(
|
||||||
Command::PONG(data.to_owned(), None)
|
Command::PONG(data.to_owned(), None)
|
||||||
));
|
)),
|
||||||
|
None
|
||||||
|
);
|
||||||
},
|
},
|
||||||
Command::PRIVMSG(ref rec, ref msg) => println!(
|
Command::PRIVMSG(ref rec, ref msg) => println!(
|
||||||
"{} -> {}: {}",
|
"{} -> {}: {}",
|
||||||
|
@ -136,10 +139,11 @@ fn handle_message(message: Message) -> Option<Message> {
|
||||||
),
|
),
|
||||||
_ => println!("{}", message.clone().to_string())
|
_ => println!("{}", message.clone().to_string())
|
||||||
}
|
}
|
||||||
return None
|
return (None, None)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_server(config: Value, tx: Sender<[String; 4]>) {
|
fn handle_server(config: Value, tx: Sender<[String; 4]>)
|
||||||
|
-> Result<(), Box<dyn std::error::Error>> {
|
||||||
let mut stream = connect::connect_irc(&config).unwrap();
|
let mut stream = connect::connect_irc(&config).unwrap();
|
||||||
|
|
||||||
let mut message_queue = get_irc_identify_messages(&config).unwrap();
|
let mut message_queue = get_irc_identify_messages(&config).unwrap();
|
||||||
|
@ -151,17 +155,8 @@ fn handle_server(config: Value, tx: Sender<[String; 4]>) {
|
||||||
message_queue = get_irc_join_messages(&config, message_queue).unwrap();
|
message_queue = get_irc_join_messages(&config, message_queue).unwrap();
|
||||||
|
|
||||||
// Wait for first ping and join channels after sending pong.
|
// Wait for first ping and join channels after sending pong.
|
||||||
// TODO this approach is not very DRY!
|
|
||||||
loop {
|
loop {
|
||||||
let message = match stream.read() {
|
let message = stream.read()?;
|
||||||
Ok(m) => m,
|
|
||||||
Err(e) => Message::from(
|
|
||||||
Command::PRIVMSG(
|
|
||||||
String::from("Error"),
|
|
||||||
format!("{}", e)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
};
|
|
||||||
match message.command {
|
match message.command {
|
||||||
Command::PING(ref data, _) => {
|
Command::PING(ref data, _) => {
|
||||||
stream.write(Message::from(
|
stream.write(Message::from(
|
||||||
|
@ -172,27 +167,22 @@ fn handle_server(config: Value, tx: Sender<[String; 4]>) {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
_ => match handle_message(message) {
|
_ => ()
|
||||||
Some(m) => stream.write(m).unwrap(),
|
|
||||||
None => ()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle all incoming messages after joining the channels.
|
||||||
loop {
|
loop {
|
||||||
let message = match stream.read() {
|
let message = stream.read()?;
|
||||||
Ok(m) => m,
|
let (answer, data) = handle_message(message);
|
||||||
Err(e) => Message::from(
|
match answer {
|
||||||
Command::PRIVMSG(
|
Some(a) => stream.write(a).unwrap(),
|
||||||
String::from("Error"),
|
|
||||||
format!("{}", e)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
};
|
|
||||||
match handle_message(message) {
|
|
||||||
Some(m) => stream.write(m).unwrap(),
|
|
||||||
None => ()
|
None => ()
|
||||||
}
|
}
|
||||||
|
match data {
|
||||||
|
Some(d) => tx.send(d)?,
|
||||||
|
None => (),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -223,19 +213,36 @@ fn main() {
|
||||||
.expect("Could not get default config in /etc/Mention2Mail/default.toml"),
|
.expect("Could not get default config in /etc/Mention2Mail/default.toml"),
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut server_configs;
|
let server_configs;
|
||||||
|
|
||||||
match server_conf_path {
|
match server_conf_path {
|
||||||
Some(p) => server_configs = config::get_server_configs(config, PathBuf::from(p))
|
Some(p) => server_configs = config::get_server_configs(config, PathBuf::from(p))
|
||||||
.expect("Could not get server config."),
|
.expect("Could not get server config."),
|
||||||
None => (),
|
None => server_configs = vec![config],
|
||||||
}
|
}
|
||||||
|
|
||||||
let (tx,rx) = channel();
|
let (tx,rx) = channel();
|
||||||
|
let mut server_threads = vec![];
|
||||||
|
|
||||||
for s_conf in server_configs {
|
for s_conf in server_configs {
|
||||||
// TODO: create channel and spawn server threads
|
let t = tx.clone();
|
||||||
handle_server(s_conf, tx.clone());
|
server_threads.push(thread::Builder::new()
|
||||||
|
.name("server name here".to_string())
|
||||||
|
.spawn(move || {
|
||||||
|
handle_server(s_conf, t).unwrap();
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
loop {
|
||||||
|
match rx.recv() {
|
||||||
|
Ok(data) => println!("{},{},{},{}",
|
||||||
|
data[0],
|
||||||
|
data[1],
|
||||||
|
data[2],
|
||||||
|
data[3]),
|
||||||
|
Err(_e) => (),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue