Commit dc75d421 authored by spookydonut's avatar spookydonut

fixes

parent 1cbb7d41
Pipeline #191 failed with stages
# 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
......@@ -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 {
......
......@@ -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> {
......
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()
......
......@@ -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);
}
}
}
......@@ -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) => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment