Browse Source

Added threading and beautified the code a bit.

pull/2/head
Thelie 8 months ago
parent
commit
500d87d65b
  1. 77
      src/main.rs

77
src/main.rs

@ -117,16 +117,19 @@ fn get_irc_join_messages(config: &Value, queue: VecDeque<Message>)
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() {
Some(s) => String::from(s),
None => String::from("anonymous")
};
match message.clone().command {
Command::PING(ref data, _) => {
return Some(Message::from(
Command::PONG(data.to_owned(), None)
));
return (
Some(Message::from(
Command::PONG(data.to_owned(), None)
)),
None
);
},
Command::PRIVMSG(ref rec, ref msg) => println!(
"{} -> {}: {}",
@ -136,10 +139,11 @@ fn handle_message(message: Message) -> Option<Message> {
),
_ => 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 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();
// Wait for first ping and join channels after sending pong.
// TODO this approach is not very DRY!
loop {
let message = match stream.read() {
Ok(m) => m,
Err(e) => Message::from(
Command::PRIVMSG(
String::from("Error"),
format!("{}", e)
)
)
};
let message = stream.read()?;
match message.command {
Command::PING(ref data, _) => {
stream.write(Message::from(
@ -172,27 +167,22 @@ fn handle_server(config: Value, tx: Sender<[String; 4]>) {
}
break;
}
_ => match handle_message(message) {
Some(m) => stream.write(m).unwrap(),
None => ()
}
_ => ()
}
}
// Handle all incoming messages after joining the channels.
loop {
let message = match stream.read() {
Ok(m) => m,
Err(e) => Message::from(
Command::PRIVMSG(
String::from("Error"),
format!("{}", e)
)
)
};
match handle_message(message) {
Some(m) => stream.write(m).unwrap(),
let message = stream.read()?;
let (answer, data) = handle_message(message);
match answer {
Some(a) => stream.write(a).unwrap(),
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"),
}
let mut server_configs;
let server_configs;
match server_conf_path {
Some(p) => server_configs = config::get_server_configs(config, PathBuf::from(p))
.expect("Could not get server config."),
None => (),
None => server_configs = vec![config],
}
let (tx,rx) = channel();
let mut server_threads = vec![];
for s_conf in server_configs {
// TODO: create channel and spawn server threads
handle_server(s_conf, tx.clone());
let t = 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…
Cancel
Save