From 415e7736f4d831634fe37524bca897fd2aaa3a8c Mon Sep 17 00:00:00 2001
From: Ash <spook123@gmail.com>
Date: Fri, 27 Mar 2020 15:19:34 +0800
Subject: [PATCH] fixes

---
 BUILDING.md            |   8 +++
 src/config.rs          |   7 +++
 src/database.rs        |   6 +-
 src/ldap.rs            |   5 +-
 src/user_management.rs | 121 +++++++++++++++++++----------------------
 src/util.rs            |   1 +
 6 files changed, 78 insertions(+), 70 deletions(-)
 create mode 100644 BUILDING.md

diff --git a/BUILDING.md b/BUILDING.md
new file mode 100644
index 0000000..d529630
--- /dev/null
+++ b/BUILDING.md
@@ -0,0 +1,8 @@
+
+# Packages required to build
+
+On ubuntu (such as using ubuntu with WSL) you will need these packages to build:
+
+* libsqlite3-dev
+* pkg-config
+* libssl-dev
diff --git a/src/config.rs b/src/config.rs
index 659da0a..86287a5 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -30,6 +30,13 @@ pub struct UccbotConfig {
     pub disapprove_react: String,
     pub unsure_react: String,
     pub react_role_messages: Vec<ReactionMapping>,
+    #[serde(default = "ldap_bind_address")]
+    pub bind_address: String,
+    pub ldap_pass: String,
+}
+
+pub fn ldap_bind_address() -> String {
+    "ldaps://samson.ucc.asn.au:636".to_string()
 }
 
 impl UccbotConfig {
diff --git a/src/database.rs b/src/database.rs
index 1498c38..5a0cdc0 100644
--- a/src/database.rs
+++ b/src/database.rs
@@ -65,6 +65,7 @@ pub fn add_member(discord_id: &u64, username: &str) -> Member {
     new_member
 }
 
+#[allow(dead_code)] // remove this if you start using it
 pub fn update_member(discord_id: &u64, member: Member) -> Result<usize, Error> {
     diesel::update(members::table.find(*discord_id as i64))
         .set(&member)
@@ -72,10 +73,7 @@ pub fn update_member(discord_id: &u64, member: Member) -> Result<usize, Error> {
 }
 
 pub fn username_exists(username: &str) -> bool {
-    match get_member_info_from_username(username) {
-        Ok(_) => true,
-        Err(_) => false,
-    }
+    get_member_info_from_username(username).is_ok()
 }
 
 pub fn get_member_info(discord_id: &u64) -> Result<Member, Error> {
diff --git a/src/ldap.rs b/src/ldap.rs
index da9cc3e..bf9aa12 100644
--- a/src/ldap.rs
+++ b/src/ldap.rs
@@ -1,4 +1,5 @@
 use ldap3::{LdapConn, LdapConnSettings, Scope, SearchEntry};
+use crate::config::CONFIG;
 
 #[derive(Debug)]
 pub struct LDAPUser {
@@ -9,11 +10,11 @@ pub struct LDAPUser {
 
 pub fn ldap_search(username: &str) -> Option<LDAPUser> {
     let settings = LdapConnSettings::new().set_no_tls_verify(true);
-    let ldap = LdapConn::with_settings(settings, "ldaps://samson.ucc.asn.au:636")
+    let ldap = LdapConn::with_settings(settings, &CONFIG.bind_address)
         .expect("Unable to connect to LDAP");
     ldap.simple_bind(
         "cn=ucc-discord-bot,cn=Users,dc=ad,dc=ucc,dc=gu,dc=uwa,dc=edu,dc=au",
-        include_str!("../ldap_pass").trim_end(),
+        &CONFIG.ldap_pass,
     )
     .expect("Unable to attempt to bind to LDAP")
     .success()
diff --git a/src/user_management.rs b/src/user_management.rs
index 2bf3d2e..5747005 100644
--- a/src/user_management.rs
+++ b/src/user_management.rs
@@ -58,6 +58,17 @@ pub const RANDOM_SASS: &[&str] = &[
     "I never treated you this badly.",
 ];
 
+pub const RESERVED_NAMES: &[&str] = &[
+    "committee",
+    "committee-only",
+    "ucc",
+    "ucc-announce",
+    "tech",
+    "wheel",
+    "door",
+    "coke",
+];
+
 pub struct Commands;
 impl Commands {
     pub fn register(ctx: Context, msg: Message, account_name: &str) {
@@ -69,17 +80,7 @@ impl Commands {
             );
             return;
         }
-        if vec![
-            "committee",
-            "committee-only",
-            "ucc",
-            "ucc-announce",
-            "tech",
-            "wheel",
-            "door",
-            "coke",
-        ]
-        .contains(&account_name)
+        if RESERVED_NAMES.contains(&account_name)
             || database::username_exists(account_name)
         {
             send_message!(
@@ -285,19 +286,13 @@ impl Commands {
         let mut value = info_content[1].to_string();
 
         if vec!["git", "photo", "web"].contains(&property.as_str()) {
-            if match Url::parse(&value) {
-                Err(_) => true,
-                Ok(_) => false,
-            } {
+            if Url::parse(&value).is_err() {
                 let user_regex = Regex::new(r"^\w+$").unwrap();
                 if property == "git" && user_regex.is_match(&value) {
                     value = format!("github.com/{}", value);
                 }
                 value = format!("https://{}", value);
-                if match Url::parse(&value) {
-                    Err(_) => true,
-                    Ok(_) => false,
-                } {
+                if Url::parse(&value).is_err() {
                     send_message!(
                         msg.channel_id,
                         &ctx.http,
@@ -307,50 +302,7 @@ impl Commands {
                 }
             }
         }
-        if let Ok(member) = database::get_member_info(&msg.author.id.0) {
-            let set_property = match property.as_str() {
-                "bio" => database::set_member_bio(&msg.author.id.0, &value),
-                "git" => database::set_member_git(&msg.author.id.0, &value),
-                "photo" => database::set_member_photo(&msg.author.id.0, &value),
-                "web" => database::set_member_website(&msg.author.id.0, &value),
-                _ => Err(diesel::result::Error::NotFound),
-            };
-            match set_property {
-                Ok(_) => {
-                    if property == "git" && member.photo == None {
-                        let git_url = Url::parse(&value).unwrap(); // we parsed this earlier and it was fine
-                        match git_url.host_str() {
-                            Some("github.com") => {
-                                if let Some(mut path_segments) = git_url.path_segments() {
-                                    database::set_member_photo(
-                                        &msg.author.id.0,
-                                        format!(
-                                            "https://github.com/{}.png",
-                                            path_segments.next().expect("URL doesn't have a path")
-                                        )
-                                        .as_str(),
-                                    )
-                                    .expect("Attempt to set member photo failed");
-                                } else {
-                                    info!("Git path added (2), {}", git_url.path());
-                                }
-                            }
-                            _ => info!("Git path added, {}", git_url.path()),
-                        }
-                    }
-                }
-                Err(why) => {
-                    error!(
-                        "Umable to set property {} to {} in DB {:?}",
-                        property, value, why
-                    );
-                    send_message!(msg.channel_id, &ctx.http, "Failed to set property. Ooops.");
-                }
-            }
-            if let Err(why) = msg.delete(&ctx) {
-                error!("Error deleting set profile property: {:?}", why);
-            }
-        } else {
+        guard!(let Ok(member) = database::get_member_info(&msg.author.id.0) else {
             send_message!(
                 msg.channel_id,
                 &ctx.http,
@@ -358,7 +310,48 @@ impl Commands {
                     "You don't seem to have a profile. {}register to get one",
                     CONFIG.command_prefix
                 )
-            )
+            );
+            return
+        });
+        let set_property = match property.as_str() {
+            "bio" => database::set_member_bio(&msg.author.id.0, &value),
+            "git" => database::set_member_git(&msg.author.id.0, &value),
+            "photo" => database::set_member_photo(&msg.author.id.0, &value),
+            "web" => database::set_member_website(&msg.author.id.0, &value),
+            _ => Err(diesel::result::Error::NotFound),
+        };
+        match set_property {
+            Ok(_) => if property == "git" && member.photo == None {
+                let git_url = Url::parse(&value).unwrap(); // we parsed this earlier and it was fine
+                match git_url.host_str() {
+                    Some("github.com") => {
+                        if let Some(mut path_segments) = git_url.path_segments() {
+                            database::set_member_photo(
+                                &msg.author.id.0,
+                                format!(
+                                    "https://github.com/{}.png",
+                                    path_segments.next().expect("URL doesn't have a path")
+                                )
+                                .as_str(),
+                            )
+                            .expect("Attempt to set member photo failed");
+                        } else {
+                            info!("Git path added (2), {}", git_url.path());
+                        }
+                    }
+                    _ => info!("Git path added, {}", git_url.path()),
+                }
+            }
+            Err(why) => {
+                error!(
+                    "Umable to set property {} to {} in DB {:?}",
+                    property, value, why
+                );
+                send_message!(msg.channel_id, &ctx.http, "Failed to set property. Ooops.");
+            }
+        }
+        if let Err(why) = msg.delete(&ctx) {
+            error!("Error deleting set profile property: {:?}", why);
         }
     }
 }
diff --git a/src/util.rs b/src/util.rs
index bdf9a13..43e5703 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -42,6 +42,7 @@ macro_rules! send_message {
     };
 }
 
+#[allow(unused_macros)] // remove this if you start using it
 #[macro_use]
 macro_rules! send_delete_message {
     ($chan:expr, $context:expr, $message:expr) => {
-- 
GitLab