rust/chat_sanitizer/src/bad_words.rs
author unc0rr
Thu, 27 Dec 2018 23:43:54 +0100
changeset 14503 831ecafd74c6
child 14505 ba29aa03db87
permissions -rw-r--r--
Start chat_sanitizer package inspired by chat bot on pokerth server
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14503
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
     1
use crate::{normalized_message, MessageChecker, Severity};
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
     2
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
     3
use std::marker::PhantomData;
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
     4
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
     5
struct BadWordsChecker<T> {
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
     6
    blacklist: Vec<String>,
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
     7
    whitelist: Vec<String>,
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
     8
    player_id_type: PhantomData<T>,
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
     9
}
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    10
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    11
impl<T> BadWordsChecker<T> {
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    12
    pub fn new(blacklist: &[&str], whitelist: &[&str]) -> Self {
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    13
        Self {
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    14
            blacklist: blacklist.iter().map(|s| normalized_message(*s)).collect(),
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    15
            whitelist: whitelist.iter().map(|s| normalized_message(*s)).collect(),
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    16
            player_id_type: PhantomData,
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    17
        }
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    18
    }
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    19
}
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    20
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    21
impl<T> MessageChecker<T> for BadWordsChecker<T> {
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    22
    fn check(&self, player_id: T, message: &str) -> Severity {
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    23
        let msg = normalized_message(message);
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    24
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    25
        // silly implementation, allows bad messages with a single good word
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    26
        for badword in &self.blacklist {
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    27
            if msg.contains(badword) {
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    28
                if !self.whitelist.iter().any(|goodword| msg.contains(goodword)) {
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    29
                    return Severity::Warn;
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    30
                }
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    31
            }
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    32
        }
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    33
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    34
        Severity::Pass
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    35
    }
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    36
}
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    37
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    38
#[cfg(test)]
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    39
mod tests {
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    40
    use super::*;
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    41
    #[test]
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    42
    fn it_works() {
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    43
        let checker = BadWordsChecker::new(&["fsck", "poop"], &["fsck -y"]);
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    44
        assert_eq!(checker.check(0, "group hug"), Severity::Pass);
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    45
        assert_eq!(checker.check(0, "fpoopf"), Severity::Warn);
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    46
        assert_eq!(checker.check(0, "PooP"), Severity::Warn);
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    47
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    48
        // this one fails
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    49
        //assert_eq!(checker.check(0, "poop 'fsck -y' poop"), Severity::Warn);
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    50
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    51
        // ideally this one shouldn't fail
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    52
        // assert_eq!(checker.check(0, "P00P"), Severity::Warn);
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    53
    }
831ecafd74c6 Start chat_sanitizer package inspired by chat bot on pokerth server
unc0rr
parents:
diff changeset
    54
}