Improved banned words loading

This commit is contained in:
Joshua Barretto 2022-08-07 17:09:12 +01:00
parent 52bd7b2485
commit 4d110a542c
2 changed files with 21 additions and 29 deletions

View File

@ -340,35 +340,9 @@ impl Server {
state.ecs_mut().register::<login_provider::PendingLogin>();
state.ecs_mut().register::<RepositionOnChunkLoad>();
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()));

View File

@ -105,6 +105,24 @@ pub struct ModerationSettings {
pub admins_exempt: bool,
}
impl ModerationSettings {
pub fn load_banned_words(&self, data_dir: &Path) -> Vec<String> {
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