From 66ca846ebb3acf757c63e298b389a832fe5a3f6f Mon Sep 17 00:00:00 2001 From: Thelie Date: Sat, 22 May 2021 20:32:41 +0200 Subject: [PATCH] Basic email sending implemented. --- Cargo.lock | 8 ++++---- src/main.rs | 37 ++++++++++++++++++------------------- src/servers.rs | 39 ++++++++++++++++++++++++++++----------- 3 files changed, 50 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bffb171..d135573 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -309,9 +309,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "lettre" -version = "0.9.5" +version = "0.9.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "338d9a248c4b3ef60c51941c678bb8f64e244c0a98f1eb71db027d1e777a5700" +checksum = "86ed8677138975b573ab4949c35613931a4addeadd0a8a6aa0327e2a979660de" dependencies = [ "base64", "bufstream", @@ -583,9 +583,9 @@ checksum = "558dc50e1a5a5fa7112ca2ce4effcb321b0300c0d4ccf0776a9f60cd89031171" [[package]] name = "serde_derive" -version = "1.0.125" +version = "1.0.126" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b093b7a2bb58203b5da3056c05b4ec1fed827dcfdb37347a8841695263b3d06d" +checksum = "963a7dbc9895aeac7ac90e74f34a5d5261828f79df35cbed41e10189d3804d43" dependencies = [ "proc-macro2", "quote", diff --git a/src/main.rs b/src/main.rs index 7d3df5f..31728ac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,9 +26,8 @@ use std::{ sync::mpsc::channel, thread, }; -use lettre::{ - Message, - SendmailTransport, +use lettre::{ + SendmailTransport, Transport }; @@ -80,27 +79,27 @@ fn main() { ); } - let mailer = SendmailTransport::new(); + let mut mailer = SendmailTransport::new(); loop { match rx.recv() { - Ok(data) => println!("{},{},{},{}", - data[0], - data[1], - data[2], - data[3]), - // let mail = Message::Builder() - // .from("m2m.chaostreff-alzye.de".parse().unwrap()) - // .to("Put adress here") - // .subject("You were mentioned in Channel on Server") - // .message("Put mention here"); - // match mailer.send(&email) { - // Ok(_) => println!("Email sent successfully!"), - // Err(e) => panic!("Could not send email: {:?}", e), - // } + Ok(data) => { + let mail = lettre::SendableEmail::new( + lettre::Envelope::new( + Some("m2m.chaostreff-alzye.de".parse().unwrap()), + vec![data[0].parse().unwrap()] + ).unwrap(), + format!("You were mentioned in {} on {}", data[2], data[1]) + .parse().unwrap(), + format!("{}: {}", data[3], data[4]).into() + ); + match mailer.send(mail) { + Ok(_) => println!("Email sent successfully!"), + Err(e) => panic!("Could not send email: {:?}", e), + } + } Err(_e) => (), } } - } diff --git a/src/servers.rs b/src/servers.rs index 48eded4..cc7081f 100644 --- a/src/servers.rs +++ b/src/servers.rs @@ -23,7 +23,10 @@ use irc_proto::command::{ CapSubCommand, Command, }; -use toml::map::Map; +use toml::map::{ + Map, + Keys +}; use irc_proto::message::Message; use toml::value::Value; @@ -101,8 +104,8 @@ fn get_irc_join_messages(config: &Value, queue: VecDeque) Ok(queue) } -fn handle_message(message: Message, nick: &String) - -> (Option, Option<[String; 3]>) { +fn handle_message(message: Message, nick: &String, client_names: Keys) + -> (Option, Option<[String; 4]>) { let sender = match message.clone().source_nickname() { Some(s) => String::from(s), None => String::from("anonymous") @@ -130,21 +133,34 @@ fn handle_message(message: Message, nick: &String) None ) } - return (None, Some([rec.clone(), sender.clone(), msg.clone()])) + for c_name in client_names { + if msg.contains(c_name) { + return (None, Some([c_name.clone(), + rec.clone(), + sender.clone(), + msg.clone() + ])) + } + } }, _ => println!("{}", message.clone().to_string()) } return (None, None) } -pub fn handle_server(config: Value, tx: Sender<[String; 4]>) +pub fn handle_server(config: Value, tx: Sender<[String; 5]>) -> Result<(), Box> { let server_name = config.get("server").unwrap().as_str() .ok_or("Could not get server adress from config")?; let nick = config.get("nickname").unwrap().as_str() .ok_or("Could not get nickname from config")?.to_owned(); - let clients = config.get("clients").unwrap().as_table() - .get_or_insert(&Map::new()); + + let clients; + match config.get("clients").unwrap().as_table() { + Some(t) => clients = t.clone(), + None => clients = Map::new(), + } + let mut stream = super::connect::connect_irc(&config).unwrap(); let mut message_queue = get_irc_identify_messages(&config).unwrap(); @@ -174,20 +190,21 @@ pub fn handle_server(config: Value, tx: Sender<[String; 4]>) // Handle all incoming messages after joining the channels. loop { let message = stream.read()?; - let (answer, data) = handle_message(message, &nick); + let (answer, data) = handle_message(message, &nick, clients.keys()); match answer { Some(a) => stream.write(a).unwrap(), None => () } match data { - Some(d) => match clients.get(d[0].clone()) { + Some(d) => match clients.get(&d[0]) { Some(addr) => { // There must be a better way to do this… let out = [ + addr.to_string(), server_name.to_owned(), - d[0].clone(), d[1].clone(), - d[2].clone(), + d[2].clone(), + d[3].clone(), ]; tx.send(out)? },