2023-07-30 00:36:30 +01:00
|
|
|
use chrono::{DateTime, SecondsFormat, Utc};
|
|
|
|
use lettre::{
|
|
|
|
message::{header, MultiPart, SinglePart},
|
2023-07-30 00:48:15 +01:00
|
|
|
transport::smtp::{authentication::Credentials, response::Response},
|
|
|
|
Message, SmtpTransport, Transport,
|
2023-07-30 00:36:30 +01:00
|
|
|
};
|
|
|
|
use maud::html;
|
2023-07-30 01:19:19 +01:00
|
|
|
use rand::{distributions::Alphanumeric, thread_rng, Rng};
|
|
|
|
use skynet_ldap_backend::{db_init, get_config, read_csv, Accounts, AccountsNew, Config, Record};
|
2023-07-30 00:48:15 +01:00
|
|
|
use sqlx::{Pool, Sqlite};
|
2023-07-30 00:36:30 +01:00
|
|
|
|
|
|
|
#[async_std::main]
|
|
|
|
async fn main() {
|
|
|
|
let config = get_config();
|
|
|
|
let db = db_init(&config).await.unwrap();
|
|
|
|
let now = Utc::now();
|
|
|
|
|
2023-07-30 00:48:15 +01:00
|
|
|
if let Ok(records) = read_csv(&config) {
|
2023-07-30 00:36:30 +01:00
|
|
|
for record in records {
|
|
|
|
// check if the email is already in the db
|
|
|
|
if !check(&db, &record.email).await {
|
|
|
|
continue;
|
|
|
|
}
|
2023-07-30 00:48:15 +01:00
|
|
|
|
2023-07-30 00:36:30 +01:00
|
|
|
// generate a auth key
|
|
|
|
let auth = generate_auth();
|
2023-07-30 00:48:15 +01:00
|
|
|
|
2023-07-30 00:36:30 +01:00
|
|
|
match send_mail(&config, &record, &auth) {
|
2023-07-30 00:48:15 +01:00
|
|
|
Ok(_) => match save_to_db(&db, now, &record, &auth).await {
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(e) => {
|
|
|
|
println!("Unable to save to db {} {e:?}", &record.email);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
println!("Unable to send mail to {} {e:?}", &record.email);
|
2023-07-30 00:36:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn check(db: &Pool<Sqlite>, mail: &str) -> bool {
|
|
|
|
check_pending(db, mail).await && check_users(db, mail).await
|
|
|
|
}
|
|
|
|
async fn check_users(db: &Pool<Sqlite>, mail: &str) -> bool {
|
|
|
|
sqlx::query_as::<_, Accounts>(
|
|
|
|
r#"
|
|
|
|
SELECT *
|
|
|
|
FROM accounts
|
|
|
|
WHERE mail == ?
|
|
|
|
"#,
|
|
|
|
)
|
2023-07-30 00:48:15 +01:00
|
|
|
.bind(mail)
|
|
|
|
.fetch_all(db)
|
|
|
|
.await
|
|
|
|
.unwrap_or(vec![])
|
|
|
|
.is_empty()
|
2023-07-30 00:36:30 +01:00
|
|
|
}
|
|
|
|
async fn check_pending(db: &Pool<Sqlite>, mail: &str) -> bool {
|
|
|
|
sqlx::query_as::<_, Accounts>(
|
|
|
|
r#"
|
|
|
|
SELECT *
|
|
|
|
FROM accounts_new
|
|
|
|
WHERE mail == ?
|
|
|
|
"#,
|
|
|
|
)
|
2023-07-30 00:48:15 +01:00
|
|
|
.bind(mail)
|
|
|
|
.fetch_all(db)
|
|
|
|
.await
|
|
|
|
.unwrap_or(vec![])
|
|
|
|
.is_empty()
|
2023-07-30 00:36:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// from https://rust-lang-nursery.github.io/rust-cookbook/algorithms/randomness.html#create-random-passwords-from-a-set-of-alphanumeric-characters
|
|
|
|
fn generate_auth() -> String {
|
2023-07-30 00:48:15 +01:00
|
|
|
thread_rng().sample_iter(&Alphanumeric).take(30).map(char::from).collect()
|
2023-07-30 00:36:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// using https://github.com/lettre/lettre/blob/57886c367d69b4d66300b322c94bd910b1eca364/examples/maud_html.rs
|
|
|
|
fn send_mail(config: &Config, record: &Record, auth: &str) -> Result<Response, lettre::transport::smtp::Error> {
|
|
|
|
let recipient = &record.name_first;
|
|
|
|
let mail = &record.email;
|
|
|
|
let url_base = "https://sso.skynet.ie";
|
|
|
|
let link_new = format!("{url_base}/register?auth={auth}");
|
|
|
|
let link_mod = format!("{url_base}/modify");
|
|
|
|
let discord = "https://discord.gg/mkuKJkCuyM";
|
|
|
|
let sender = format!("UL Computer Society <{}>", &config.mail_user);
|
|
|
|
|
|
|
|
// Create the html we want to send.
|
|
|
|
let html = html! {
|
|
|
|
head {
|
|
|
|
title { "Hello from Skynet!" }
|
|
|
|
style type="text/css" {
|
|
|
|
"h2, h4 { font-family: Arial, Helvetica, sans-serif; }"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
div style="display: flex; flex-direction: column; align-items: center;" {
|
|
|
|
h2 { "Hello from Skynet!" }
|
|
|
|
// Substitute in the name of our recipient.
|
|
|
|
p { "Hi " (recipient) "," }
|
2023-07-30 00:48:15 +01:00
|
|
|
p {
|
2023-07-30 00:36:30 +01:00
|
|
|
"If you are a new member please use the following link:"
|
|
|
|
br;
|
|
|
|
a href=(link_new) { (link_new) }
|
|
|
|
}
|
|
|
|
p {
|
|
|
|
"If you are a returning user please set an email for your account at:"
|
|
|
|
br;
|
|
|
|
a href=(link_mod) { (link_mod) }
|
|
|
|
}
|
|
|
|
p {
|
|
|
|
"If you have issues please refer to our Discord server:"
|
|
|
|
br;
|
|
|
|
a href=(discord) { (discord) }
|
|
|
|
}
|
2023-07-30 00:48:15 +01:00
|
|
|
|
2023-07-30 00:36:30 +01:00
|
|
|
p {
|
|
|
|
"UL Computer Society"
|
|
|
|
br;
|
|
|
|
"Skynet Team"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2023-07-30 00:48:15 +01:00
|
|
|
|
2023-07-30 00:36:30 +01:00
|
|
|
let body_text = format!(
|
|
|
|
r#"
|
|
|
|
Hi {recipient}
|
|
|
|
|
|
|
|
If you are a new member please use the following link:
|
|
|
|
{link_new}
|
|
|
|
|
|
|
|
If you are a returning user please set an email for your account at:
|
|
|
|
{link_mod}
|
|
|
|
|
|
|
|
If you have issues please refer to our Discord server:
|
|
|
|
{discord}
|
|
|
|
|
|
|
|
UL Computer Society
|
|
|
|
Skynet Team
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
// Build the message.
|
|
|
|
let email = Message::builder()
|
2023-07-30 00:48:15 +01:00
|
|
|
.from(sender.parse().unwrap())
|
|
|
|
.to(mail.parse().unwrap())
|
|
|
|
.subject("Skynet: New Account.")
|
|
|
|
.multipart(
|
|
|
|
// This is composed of two parts.
|
|
|
|
// also helps not trip spam settings (uneven number of url's
|
|
|
|
MultiPart::alternative()
|
|
|
|
.singlepart(SinglePart::builder().header(header::ContentType::TEXT_PLAIN).body(body_text))
|
|
|
|
.singlepart(SinglePart::builder().header(header::ContentType::TEXT_HTML).body(html.into_string())),
|
|
|
|
)
|
|
|
|
.expect("failed to build email");
|
2023-07-30 00:36:30 +01:00
|
|
|
|
|
|
|
let creds = Credentials::new(config.mail_user.clone(), config.mail_pass.clone());
|
|
|
|
|
|
|
|
// Open a remote connection to gmail using STARTTLS
|
2023-07-30 00:48:15 +01:00
|
|
|
let mailer = SmtpTransport::starttls_relay(&config.mail_smtp).unwrap().credentials(creds).build();
|
2023-07-30 00:36:30 +01:00
|
|
|
|
|
|
|
// Send the email
|
|
|
|
mailer.send(&email)
|
|
|
|
}
|
|
|
|
|
2023-07-30 00:48:15 +01:00
|
|
|
async fn save_to_db(db: &Pool<Sqlite>, now: DateTime<Utc>, record: &Record, auth: &str) -> Result<Option<AccountsNew>, sqlx::Error> {
|
2023-07-30 00:36:30 +01:00
|
|
|
sqlx::query_as::<_, AccountsNew>(
|
|
|
|
"
|
|
|
|
INSERT OR REPLACE INTO accounts_new (mail, auth_code, date_iso, date_expiry, name_first, name_surname)
|
|
|
|
VALUES (?1, ?2, ?3, ?4, ?5, ?6)
|
|
|
|
",
|
|
|
|
)
|
2023-07-30 00:48:15 +01:00
|
|
|
.bind(record.email.to_owned())
|
|
|
|
.bind(auth.to_owned())
|
|
|
|
.bind(now.to_rfc3339_opts(SecondsFormat::Millis, true))
|
|
|
|
.bind(record.expiry.to_owned())
|
|
|
|
.bind(record.name_first.to_owned())
|
|
|
|
.bind(record.name_second.to_owned())
|
|
|
|
.fetch_optional(db)
|
|
|
|
.await
|
2023-07-30 00:36:30 +01:00
|
|
|
}
|