Split the code up even more and added beginnings of the mailing process.

This commit is contained in:
Thelie 2021-05-18 21:35:57 +02:00
parent a8ddf20098
commit 620b901bc3
3 changed files with 57 additions and 2 deletions

View file

@ -18,6 +18,7 @@
pub mod connect; pub mod connect;
pub mod config; pub mod config;
pub mod servers; pub mod servers;
pub mod texts;
use std::{ use std::{
env::args, env::args,
@ -25,6 +26,11 @@ use std::{
sync::mpsc::channel, sync::mpsc::channel,
thread, thread,
}; };
use lettre::{
Message,
SendmailTransport,
Transport
};
fn main() { fn main() {
let mut server_conf_flag = false; let mut server_conf_flag = false;
@ -74,6 +80,8 @@ fn main() {
); );
} }
let mailer = SendmailTransport::new();
loop { loop {
match rx.recv() { match rx.recv() {
Ok(data) => println!("{},{},{},{}", Ok(data) => println!("{},{},{},{}",
@ -81,6 +89,15 @@ fn main() {
data[1], data[1],
data[2], data[2],
data[3]), 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) => (), Err(_e) => (),
} }
} }

View file

@ -100,7 +100,8 @@ fn get_irc_join_messages(config: &Value, queue: VecDeque<Message>)
Ok(queue) Ok(queue)
} }
fn handle_message(message: Message) -> (Option<Message>, Option<[String; 3]>) { fn handle_message(message: Message, nick: &String)
-> (Option<Message>, Option<[String; 3]>) {
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")
@ -115,6 +116,19 @@ fn handle_message(message: Message) -> (Option<Message>, Option<[String; 3]>) {
); );
}, },
Command::PRIVMSG(ref rec, ref msg) => { 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()])) return (None, Some([rec.clone(), sender.clone(), msg.clone()]))
}, },
_ => println!("{}", message.clone().to_string()) _ => println!("{}", message.clone().to_string())
@ -126,6 +140,9 @@ pub fn handle_server(config: Value, tx: Sender<[String; 4]>)
-> Result<(), Box<dyn std::error::Error>> { -> Result<(), Box<dyn std::error::Error>> {
let server_name = config.get("server").unwrap().as_str() let server_name = config.get("server").unwrap().as_str()
.ok_or("Could not get server adress from config")?; .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 stream = super::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();
@ -155,7 +172,7 @@ pub fn handle_server(config: Value, tx: Sender<[String; 4]>)
// Handle all incoming messages after joining the channels. // Handle all incoming messages after joining the channels.
loop { loop {
let message = stream.read()?; let message = stream.read()?;
let (answer, data) = handle_message(message); let (answer, data) = handle_message(message, &nick);
match answer { match answer {
Some(a) => stream.write(a).unwrap(), Some(a) => stream.write(a).unwrap(),
None => () None => ()

21
src/texts.rs Normal file
View file

@ -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 <https://www.gnu.org/licenses/>.
*/
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()
}