diff --git a/src/main.rs b/src/main.rs index 852e25a..7d3df5f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,7 @@ pub mod connect; pub mod config; pub mod servers; +pub mod texts; use std::{ env::args, @@ -25,6 +26,11 @@ use std::{ sync::mpsc::channel, thread, }; +use lettre::{ + Message, + SendmailTransport, + Transport +}; fn main() { let mut server_conf_flag = false; @@ -73,6 +79,8 @@ fn main() { }) ); } + + let mailer = SendmailTransport::new(); loop { match rx.recv() { @@ -81,6 +89,15 @@ fn main() { 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), + // } Err(_e) => (), } } diff --git a/src/servers.rs b/src/servers.rs index 0e319dc..b40a097 100644 --- a/src/servers.rs +++ b/src/servers.rs @@ -100,7 +100,8 @@ fn get_irc_join_messages(config: &Value, queue: VecDeque) Ok(queue) } -fn handle_message(message: Message) -> (Option, Option<[String; 3]>) { +fn handle_message(message: Message, nick: &String) + -> (Option, Option<[String; 3]>) { let sender = match message.clone().source_nickname() { Some(s) => String::from(s), None => String::from("anonymous") @@ -115,6 +116,19 @@ fn handle_message(message: Message) -> (Option, Option<[String; 3]>) { ); }, Command::PRIVMSG(ref rec, ref msg) => { + // Send bot info on receiving a private message. + if rec.as_str() == nick.as_str() { + return ( + Some(Message::from( + Command::PRIVMSG( + sender.clone(), + super::texts::get_bot_info(), + ) + ) + ), + None + ) + } return (None, Some([rec.clone(), sender.clone(), msg.clone()])) }, _ => println!("{}", message.clone().to_string()) @@ -126,6 +140,9 @@ pub fn handle_server(config: Value, tx: Sender<[String; 4]>) -> 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 mut stream = super::connect::connect_irc(&config).unwrap(); let mut message_queue = get_irc_identify_messages(&config).unwrap(); @@ -155,7 +172,7 @@ 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); + let (answer, data) = handle_message(message, &nick); match answer { Some(a) => stream.write(a).unwrap(), None => () diff --git a/src/texts.rs b/src/texts.rs new file mode 100644 index 0000000..782a63c --- /dev/null +++ b/src/texts.rs @@ -0,0 +1,21 @@ +/* Copyright 2021 Daniel Mowitz + * This file is part of Mention2Mail. + * + * Mention2Mail is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Mention2Mail is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Mention2Mail. If not, see . + */ + +pub fn get_bot_info() -> String{ +"Hello! I'm an instance of Mention2Mail. I am licensed under the AGPLv3. You can find my source code at: https://gitea.chaostreff-alzey.de/Thelie/Mention2Mail/".to_owned() +} +