diff --git a/server/src/lib.rs b/server/src/lib.rs index 5896c68fd7..74d10c43a7 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -340,35 +340,9 @@ impl Server { state.ecs_mut().register::(); state.ecs_mut().register::(); + let banned_words = settings.moderation.load_banned_words(data_dir); + //Alias validator - let banned_words_paths = &settings.moderation.banned_words_files; - let mut banned_words = Vec::new(); - for path in banned_words_paths { - let mut list = match std::fs::File::open(&path) { - Ok(file) => match ron::de::from_reader(&file) { - Ok(vec) => vec, - Err(error) => { - warn!(?error, ?file, "Couldn't deserialize banned words file"); - return Err(Error::Other(format!( - "Couldn't read banned words file \"{}\"", - path.to_string_lossy() - ))); - }, - }, - Err(error) => { - warn!(?error, ?path, "Couldn't open banned words file"); - return Err(Error::Other(format!( - "Couldn't open banned words file \"{}\". Error: {}", - path.to_string_lossy(), - error - ))); - }, - }; - banned_words.append(&mut list); - } - let banned_words_count = banned_words.len(); - debug!(?banned_words_count); - trace!(?banned_words); state .ecs_mut() .insert(AliasValidator::new(banned_words.clone())); diff --git a/server/src/settings.rs b/server/src/settings.rs index 44d503713d..896dc8df7f 100644 --- a/server/src/settings.rs +++ b/server/src/settings.rs @@ -105,6 +105,24 @@ pub struct ModerationSettings { pub admins_exempt: bool, } +impl ModerationSettings { + pub fn load_banned_words(&self, data_dir: &Path) -> Vec { + let mut banned_words = Vec::new(); + for fname in self.banned_words_files.iter() { + let mut path = with_config_dir(data_dir); + path.push(fname); + match std::fs::File::open(&path) { + Ok(file) => match ron::de::from_reader(&file) { + Ok(mut words) => banned_words.append(&mut words), + Err(error) => error!(?error, ?file, "Couldn't read banned words file"), + }, + Err(error) => error!(?error, ?path, "Couldn't open banned words file"), + } + } + banned_words + } +} + impl Default for ModerationSettings { fn default() -> Self { Self { @@ -286,7 +304,7 @@ impl Settings { } } -fn with_config_dir(path: &Path) -> PathBuf { +pub fn with_config_dir(path: &Path) -> PathBuf { let mut path = PathBuf::from(path); path.push(CONFIG_DIR); path