2023-09-17 15:25:17 +01:00
|
|
|
use lettre::{
|
|
|
|
message::{header, MultiPart, SinglePart},
|
|
|
|
transport::smtp::{self, authentication::Credentials},
|
|
|
|
Message, SmtpTransport, Transport,
|
|
|
|
};
|
|
|
|
use maud::html;
|
|
|
|
use serenity::{
|
|
|
|
builder::CreateApplicationCommand,
|
|
|
|
client::Context,
|
|
|
|
model::{
|
|
|
|
application::interaction::application_command::ApplicationCommandInteraction,
|
|
|
|
prelude::{command::CommandOptionType, interaction::application_command::CommandDataOptionValue},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
use skynet_discord_bot::{get_now_iso, random_string, Config, DataBase, Wolves, WolvesVerify};
|
2023-09-16 22:47:26 +01:00
|
|
|
use sqlx::{Pool, Sqlite};
|
|
|
|
|
2023-09-17 15:35:41 +01:00
|
|
|
pub(crate) mod link {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
|
|
|
|
let db_lock = {
|
|
|
|
let data_read = ctx.data.read().await;
|
|
|
|
data_read.get::<DataBase>().expect("Expected Databse in TypeMap.").clone()
|
|
|
|
};
|
|
|
|
let db = db_lock.read().await;
|
|
|
|
|
|
|
|
let config_lock = {
|
|
|
|
let data_read = ctx.data.read().await;
|
|
|
|
data_read.get::<Config>().expect("Expected Config in TypeMap.").clone()
|
|
|
|
};
|
|
|
|
let config = config_lock.read().await;
|
|
|
|
|
|
|
|
if get_server_member_discord(&db, &command.user.name).await.is_some() {
|
|
|
|
return "Already linked".to_string();
|
|
|
|
}
|
2023-09-17 15:25:17 +01:00
|
|
|
|
2023-09-17 18:00:05 +01:00
|
|
|
db_pending_clear_expired(&db).await;
|
|
|
|
|
|
|
|
if get_verify_from_db(&db, &command.user.name).await.is_some() {
|
|
|
|
return "Linking already in process, please check email.".to_string();
|
|
|
|
}
|
|
|
|
|
2023-09-17 15:35:41 +01:00
|
|
|
let option = command
|
|
|
|
.data
|
|
|
|
.options
|
|
|
|
.get(0)
|
|
|
|
.expect("Expected email option")
|
|
|
|
.resolved
|
|
|
|
.as_ref()
|
|
|
|
.expect("Expected email object");
|
|
|
|
|
|
|
|
let email = if let CommandDataOptionValue::String(email) = option {
|
|
|
|
email
|
|
|
|
} else {
|
|
|
|
return "Please provide a valid user".to_string();
|
|
|
|
};
|
|
|
|
|
|
|
|
// check if email exists
|
|
|
|
let details = match get_server_member_email(&db, email).await {
|
|
|
|
None => return "Please check is the same as on https://ulwolves.ie/".to_string(),
|
|
|
|
Some(x) => x,
|
|
|
|
};
|
|
|
|
|
|
|
|
if details.verified {
|
|
|
|
return "Email already verified".to_string();
|
|
|
|
}
|
2023-09-17 15:25:17 +01:00
|
|
|
|
2023-09-17 15:35:41 +01:00
|
|
|
// generate a auth key
|
|
|
|
let auth = random_string(20);
|
|
|
|
match send_mail(&config, &details, &auth, &command.user.name) {
|
|
|
|
Ok(_) => match save_to_db(&db, &details, &auth, &command.user.name).await {
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(e) => {
|
|
|
|
return format!("Unable to save to db {} {e:?}", &details.email);
|
|
|
|
}
|
|
|
|
},
|
2023-09-17 15:25:17 +01:00
|
|
|
Err(e) => {
|
2023-09-17 15:35:41 +01:00
|
|
|
return format!("Unable to send mail to {} {e:?}", &details.email);
|
2023-09-17 15:25:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-17 18:00:05 +01:00
|
|
|
format!("Verification email sent to {}", email)
|
2023-09-17 15:35:41 +01:00
|
|
|
}
|
2023-09-16 22:47:26 +01:00
|
|
|
|
2023-09-17 15:35:41 +01:00
|
|
|
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
|
|
|
|
command
|
|
|
|
.name("link")
|
|
|
|
.description("Set Wolves Email")
|
|
|
|
.create_option(|option| option.name("email").description("UL Wolves Email").kind(CommandOptionType::String).required(true))
|
|
|
|
}
|
2023-09-16 22:47:26 +01:00
|
|
|
|
2023-09-17 15:35:41 +01:00
|
|
|
async fn get_server_member_discord(db: &Pool<Sqlite>, user: &str) -> Option<Wolves> {
|
|
|
|
sqlx::query_as::<_, Wolves>(
|
|
|
|
r#"
|
2023-09-17 15:25:17 +01:00
|
|
|
SELECT *
|
|
|
|
FROM wolves
|
2023-09-17 18:00:05 +01:00
|
|
|
WHERE discord = ?
|
2023-09-17 15:25:17 +01:00
|
|
|
"#,
|
2023-09-17 15:35:41 +01:00
|
|
|
)
|
|
|
|
.bind(user)
|
|
|
|
.fetch_one(db)
|
|
|
|
.await
|
|
|
|
.ok()
|
|
|
|
}
|
2023-09-17 15:25:17 +01:00
|
|
|
|
2023-09-17 15:35:41 +01:00
|
|
|
async fn get_server_member_email(db: &Pool<Sqlite>, email: &str) -> Option<Wolves> {
|
|
|
|
sqlx::query_as::<_, Wolves>(
|
|
|
|
r#"
|
2023-09-17 00:14:50 +01:00
|
|
|
SELECT *
|
2023-09-17 15:25:17 +01:00
|
|
|
FROM wolves
|
|
|
|
WHERE email = ?
|
2023-09-17 00:14:50 +01:00
|
|
|
"#,
|
2023-09-17 15:35:41 +01:00
|
|
|
)
|
|
|
|
.bind(email)
|
|
|
|
.fetch_one(db)
|
|
|
|
.await
|
|
|
|
.ok()
|
|
|
|
}
|
2023-09-17 15:25:17 +01:00
|
|
|
|
2023-09-17 15:35:41 +01:00
|
|
|
fn send_mail(config: &Config, email: &Wolves, auth: &str, user: &str) -> Result<smtp::response::Response, smtp::Error> {
|
|
|
|
let mail = &email.email;
|
|
|
|
let discord = "https://discord.skynet.ie";
|
|
|
|
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 " (user) "," }
|
|
|
|
p {
|
2023-09-17 18:00:05 +01:00
|
|
|
"Please use " pre { "/verify code:" (auth)} " to verify your discord account."
|
2023-09-17 15:35:41 +01:00
|
|
|
}
|
|
|
|
p {
|
|
|
|
"If you have issues please refer to our Discord server:"
|
|
|
|
br;
|
|
|
|
a href=(discord) { (discord) }
|
|
|
|
}
|
|
|
|
p {
|
|
|
|
"Skynet Team"
|
|
|
|
br;
|
|
|
|
"UL Computer Society"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let body_text = format!(
|
|
|
|
r#"
|
2023-09-17 15:25:17 +01:00
|
|
|
Hi {user}
|
|
|
|
|
2023-09-17 18:00:05 +01:00
|
|
|
Please use "/verify code:{auth}" to verify your discord account.
|
2023-09-17 15:25:17 +01:00
|
|
|
|
|
|
|
If you have issues please refer to our Discord server:
|
|
|
|
{discord}
|
|
|
|
|
|
|
|
Skynet Team
|
|
|
|
UL Computer Society
|
|
|
|
"#
|
2023-09-17 15:35:41 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Build the message.
|
|
|
|
let email = Message::builder()
|
|
|
|
.from(sender.parse().unwrap())
|
|
|
|
.to(mail.parse().unwrap())
|
|
|
|
.subject("Skynet-Discord: Link Wolves.")
|
|
|
|
.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");
|
|
|
|
|
|
|
|
let creds = Credentials::new(config.mail_user.clone(), config.mail_pass.clone());
|
|
|
|
|
|
|
|
// Open a remote connection to gmail using STARTTLS
|
|
|
|
let mailer = SmtpTransport::starttls_relay(&config.mail_smtp).unwrap().credentials(creds).build();
|
|
|
|
|
|
|
|
// Send the email
|
|
|
|
mailer.send(&email)
|
|
|
|
}
|
2023-09-17 15:25:17 +01:00
|
|
|
|
2023-09-17 18:00:05 +01:00
|
|
|
pub async fn db_pending_clear_expired(pool: &Pool<Sqlite>) -> Option<WolvesVerify> {
|
2023-09-17 15:35:41 +01:00
|
|
|
sqlx::query_as::<_, WolvesVerify>(
|
|
|
|
r#"
|
2023-09-17 15:25:17 +01:00
|
|
|
DELETE
|
|
|
|
FROM wolves_verify
|
|
|
|
WHERE date_expiry < ?
|
|
|
|
"#,
|
2023-09-17 15:35:41 +01:00
|
|
|
)
|
|
|
|
.bind(get_now_iso(true))
|
|
|
|
.fetch_one(pool)
|
|
|
|
.await
|
|
|
|
.ok()
|
|
|
|
}
|
2023-09-17 15:25:17 +01:00
|
|
|
|
2023-09-17 18:00:05 +01:00
|
|
|
pub async fn get_verify_from_db(db: &Pool<Sqlite>, user: &str) -> Option<WolvesVerify> {
|
2023-09-17 15:35:41 +01:00
|
|
|
sqlx::query_as::<_, WolvesVerify>(
|
|
|
|
r#"
|
2023-09-17 15:25:17 +01:00
|
|
|
SELECT *
|
|
|
|
FROM wolves_verify
|
2023-09-17 18:00:05 +01:00
|
|
|
WHERE discord = ?
|
2023-09-17 15:25:17 +01:00
|
|
|
"#,
|
2023-09-17 15:35:41 +01:00
|
|
|
)
|
|
|
|
.bind(user)
|
|
|
|
.fetch_one(db)
|
|
|
|
.await
|
|
|
|
.ok()
|
|
|
|
}
|
2023-09-17 15:25:17 +01:00
|
|
|
|
2023-09-17 15:35:41 +01:00
|
|
|
async fn save_to_db(db: &Pool<Sqlite>, record: &Wolves, auth: &str, user: &str) -> Result<Option<WolvesVerify>, sqlx::Error> {
|
|
|
|
sqlx::query_as::<_, WolvesVerify>(
|
|
|
|
"
|
2023-09-17 15:25:17 +01:00
|
|
|
INSERT INTO wolves_verify (email, discord, auth_code, date_expiry)
|
|
|
|
VALUES (?1, ?2, ?3, ?4)
|
|
|
|
",
|
2023-09-17 15:35:41 +01:00
|
|
|
)
|
|
|
|
.bind(record.email.to_owned())
|
|
|
|
.bind(user)
|
|
|
|
.bind(auth.to_owned())
|
|
|
|
.bind(get_now_iso(false))
|
|
|
|
.fetch_optional(db)
|
|
|
|
.await
|
|
|
|
}
|
2023-09-17 00:14:50 +01:00
|
|
|
}
|
2023-09-17 18:00:05 +01:00
|
|
|
|
|
|
|
pub(crate) mod verify {
|
|
|
|
use super::*;
|
|
|
|
use crate::commands::link_email::link::{db_pending_clear_expired, get_verify_from_db};
|
|
|
|
use sqlx::Error;
|
|
|
|
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
|
|
|
|
let db_lock = {
|
|
|
|
let data_read = ctx.data.read().await;
|
|
|
|
data_read.get::<DataBase>().expect("Expected Databse in TypeMap.").clone()
|
|
|
|
};
|
|
|
|
let db = db_lock.read().await;
|
|
|
|
|
|
|
|
// check if user has used /link
|
|
|
|
let details = if let Some(x) = get_verify_from_db(&db, &command.user.name).await {
|
|
|
|
x
|
|
|
|
} else {
|
|
|
|
return "Please use /link first".to_string();
|
|
|
|
};
|
|
|
|
|
|
|
|
let option = command
|
|
|
|
.data
|
|
|
|
.options
|
|
|
|
.get(0)
|
|
|
|
.expect("Expected code option")
|
|
|
|
.resolved
|
|
|
|
.as_ref()
|
|
|
|
.expect("Expected code object");
|
|
|
|
|
|
|
|
let code = if let CommandDataOptionValue::String(code) = option {
|
|
|
|
code
|
|
|
|
} else {
|
|
|
|
return "Please provide a verification code".to_string();
|
|
|
|
};
|
|
|
|
|
|
|
|
db_pending_clear_expired(&db).await;
|
|
|
|
|
|
|
|
if &details.auth_code != code {
|
|
|
|
return "Invalid verification code".to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
match db_pending_clear_successful(&db, &command.user.name).await {
|
|
|
|
Ok(_) => {
|
|
|
|
return match set_discord(&db, &command.user.name, &details.email).await {
|
|
|
|
Ok(_) => "Discord username linked to Wolves".to_string(),
|
|
|
|
Err(e) => {
|
|
|
|
println!("{:?}", e);
|
|
|
|
"Failed to save, please try /link again".to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => println!("{:?}", e),
|
|
|
|
}
|
|
|
|
|
|
|
|
"Failed to verify".to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
|
|
|
|
command.name("verify").description("Verify Wolves Email").create_option(|option| {
|
|
|
|
option
|
|
|
|
.name("code")
|
|
|
|
.description("Code from verification email")
|
|
|
|
.kind(CommandOptionType::String)
|
|
|
|
.required(true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn db_pending_clear_successful(pool: &Pool<Sqlite>, user: &str) -> Result<Option<WolvesVerify>, Error> {
|
|
|
|
sqlx::query_as::<_, WolvesVerify>(
|
|
|
|
r#"
|
|
|
|
DELETE
|
|
|
|
FROM wolves_verify
|
|
|
|
WHERE discord = ?
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.bind(user)
|
|
|
|
.fetch_optional(pool)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn set_discord(db: &Pool<Sqlite>, discord: &str, email: &str) -> Result<Option<Wolves>, Error> {
|
|
|
|
sqlx::query_as::<_, Wolves>(
|
|
|
|
"
|
|
|
|
UPDATE wolves
|
|
|
|
SET discord = ?
|
|
|
|
WHERE email = ?
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.bind(discord)
|
|
|
|
.bind(email)
|
|
|
|
.fetch_optional(db)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
}
|