diff --git a/BUILDING.md b/BUILDING.md new file mode 100644 index 0000000000000000000000000000000000000000..d5296308624b3ea20985ad206bc7cc4832136623 --- /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 659da0a3c825965339fa83ac12945fdb61674161..86287a5e16e6bc1403d372bdb82b5fade0c4e802 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 1498c38c0288645095b804e017e67da88251cfee..5a0cdc07fc2cbf107740b3ed4ec90d5a5d4e8adb 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 da9cc3eba184e67ff3a4c7a50478997ef0ee2e7f..bf9aa123d44d788ba7b5883d43ed9cbac72fa296 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 2bf3d2edd592b573ad33bdd5c64fcadc16d85781..57470053c3620ca5e611976c24b294c6023e1e66 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 bdf9a1356430890edc76f822675e64e505d6cf0c..43e5703cabdcbb81fc7c92306aeb2a7815e1e16b 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) => {