fmt: clippy and fmt

This commit is contained in:
silver 2023-07-30 00:48:15 +01:00
parent 2b96d498a7
commit e21af089d1
2 changed files with 62 additions and 99 deletions

View file

@ -1,18 +1,15 @@
use chrono::{DateTime, SecondsFormat, Utc};
use skynet_ldap_backend::{get_config, Config, db_init, Accounts, AccountsNew};
use std::error::Error;
use sqlx::{Pool, Sqlite};
use rand::{thread_rng, Rng};
use rand::distributions::Alphanumeric;
use lettre::{
message::{header, MultiPart, SinglePart},
SmtpTransport, Message, Transport,
transport::smtp::{
authentication::Credentials,
response::Response
},
transport::smtp::{authentication::Credentials, response::Response},
Message, SmtpTransport, Transport,
};
use maud::html;
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use skynet_ldap_backend::{db_init, get_config, Accounts, AccountsNew, Config};
use sqlx::{Pool, Sqlite};
use std::error::Error;
#[async_std::main]
async fn main() {
@ -20,7 +17,7 @@ async fn main() {
let db = db_init(&config).await.unwrap();
let now = Utc::now();
if let Ok(records) = read_csv(&config){
if let Ok(records) = read_csv(&config) {
for record in records {
// check if the email is already in the db
if !check(&db, &record.email).await {
@ -31,14 +28,12 @@ async fn main() {
let auth = generate_auth();
match send_mail(&config, &record, &auth) {
Ok(_) => {
match save_to_db(&db, now, &record, &auth).await {
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);
}
@ -47,7 +42,6 @@ async fn main() {
}
}
#[derive(Debug, serde::Deserialize)]
struct Record {
#[serde(rename = "MemID")]
@ -72,7 +66,7 @@ fn read_csv(config: &Config) -> Result<Vec<Record>, Box<dyn Error>> {
// Notice that we need to provide a type hint for automatic
// deserialization.
let record: Record = result?;
if record.mem_id == "" {
if record.mem_id.is_empty() {
continue;
}
records.push(record);
@ -116,11 +110,7 @@ async fn check_pending(db: &Pool<Sqlite>, mail: &str) -> bool {
// 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 {
thread_rng()
.sample_iter(&Alphanumeric)
.take(30)
.map(char::from)
.collect()
thread_rng().sample_iter(&Alphanumeric).take(30).map(char::from).collect()
}
// using https://github.com/lettre/lettre/blob/57886c367d69b4d66300b322c94bd910b1eca364/examples/maud_html.rs
@ -196,27 +186,15 @@ fn send_mail(config: &Config, record: &Record, auth: &str) -> Result<Response, l
// 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()),
),
.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();
let mailer = SmtpTransport::starttls_relay(&config.mail_smtp).unwrap().credentials(creds).build();
// Send the email
mailer.send(&email)
@ -238,8 +216,3 @@ async fn save_to_db(db: &Pool<Sqlite>, now: DateTime<Utc>, record: &Record, aut
.fetch_optional(db)
.await
}

View file

@ -96,9 +96,7 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
.execute(&pool)
.await?;
sqlx::query("CREATE INDEX IF NOT EXISTS index_uid_number ON accounts (uid)")
.execute(&pool)
.await?;
sqlx::query("CREATE INDEX IF NOT EXISTS index_uid_number ON accounts (uid)").execute(&pool).await?;
update_accounts(&pool, config).await;
@ -190,12 +188,7 @@ async fn update_accounts(pool: &Pool<Sqlite>, config: &Config) {
ldap.simple_bind(&config.ldap_admin, &config.ldap_admin_pw).unwrap().success().unwrap();
// use this to pre load a large chunk of data
if let Ok(x) = ldap.search(
"ou=users,dc=skynet,dc=ie",
Scope::OneLevel,
"(objectClass=*)",
vec!["uid", "uidNumber", "skDiscord", "skMemberOf", "mail", "skID", "skSecure"]
) {
if let Ok(x) = ldap.search("ou=users,dc=skynet,dc=ie", Scope::OneLevel, "(objectClass=*)", vec!["uid", "uidNumber", "skDiscord", "skMemberOf", "mail", "skID", "skSecure"]) {
if let Ok((rs, _res)) = x.success() {
for entry in rs {
let tmp = SearchEntry::construct(entry);
@ -226,10 +219,7 @@ async fn update_accounts(pool: &Pool<Sqlite>, config: &Config) {
if tmp.attrs.contains_key("skID") && !tmp.attrs["skID"].is_empty() {
tmp_account.student_id = tmp.attrs["skID"][0].clone();
}
if tmp.attrs.contains_key("skMemberOf")
&& !tmp.attrs["skMemberOf"].is_empty()
&& tmp.attrs["skMemberOf"].contains(&String::from("cn=skynet-users-linux,ou=groups,dc=skynet,dc=ie"))
{
if tmp.attrs.contains_key("skMemberOf") && !tmp.attrs["skMemberOf"].is_empty() && tmp.attrs["skMemberOf"].contains(&String::from("cn=skynet-users-linux,ou=groups,dc=skynet,dc=ie")) {
tmp_account.enabled = true;
}
if tmp.attrs.contains_key("skSecure") && !tmp.attrs["skSecure"].is_empty() {