diff options
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/slangsfilter.cpp | 96 | ||||
-rw-r--r-- | src/utils/slangsfilter.h | 52 |
2 files changed, 123 insertions, 25 deletions
diff --git a/src/utils/slangsfilter.cpp b/src/utils/slangsfilter.cpp index e950243b..a729bb93 100644 --- a/src/utils/slangsfilter.cpp +++ b/src/utils/slangsfilter.cpp @@ -21,46 +21,102 @@ */ #include "slangsfilter.h" +#include "logger.h" namespace tmwserv { namespace utils { -/** - * The slang table. Don't be surprised. We need to know about bad words in order - * to keep them out of the players' conversations. -*/ -std::string slangs[] = { -"fuck","shit","slut","whore","bitch", -"END" // This is the terminator, don't remove it. -}; +SlangsFilter::SlangsFilter(Configuration *config) + : mInitialized(false), + mConfig(config) +{ + mSlangs.clear(); + loadFilterList(); +} -bool filterContent(std::string text) +SlangsFilter::~SlangsFilter() { - bool good = true; - unsigned int i = 0; - while ( !(slangs[i] == "END") ) + writeFilterList(); + mSlangs.clear(); +} + +bool SlangsFilter::loadFilterList() +{ + mInitialized = false; + std::string slangsList = mConfig->getValue("SlangsList", ""); + if ( slangsList != "") { - for (unsigned int j = 0; j <= text.length(); j++) + // Getting the words from the list. + unsigned int i = 0; // this is the latest comma position keeper + for (unsigned int j = 0; j < slangsList.length(); j++) + { + if (slangsList[j] == ',') + { + if (i == 0) + mSlangs.push_back(slangsList.substr(i, j-i)); + else + mSlangs.push_back(slangsList.substr(i+1, j-i-1)); + + i = j; + } + } + // Getting the last word + mSlangs.push_back(slangsList.substr(i+1, slangsList.length() - 1)); + mInitialized = true; + return true; + } + return false; +} + +void SlangsFilter::writeFilterList() +{ + // Write the list to config + std::string slangsList = ""; + for (std::list<std::string>::iterator i = mSlangs.begin(); i != mSlangs.end(); ) + { + slangsList += *i; + ++i; + if (i != mSlangs.end()) slangsList += ","; + } + //mConfig->setValue("SlangsList", slangsList); +} + +bool SlangsFilter::filterContent(const std::string& text) +{ + if (mInitialized) + { + bool isContentClean = true; + + for (std::list<std::string>::iterator i = mSlangs.begin(); i != mSlangs.end(); ) { // We look for slangs into the sentence. std::string upcasedText = text; - std::string upcasedSlang = slangs[i]; + std::string upcasedSlang = *i; std::transform(upcasedText.begin(), upcasedText.end(), upcasedText.begin(), (int(*)(int))std::toupper); std::transform(upcasedSlang.begin(), upcasedSlang.end(), upcasedSlang.begin(), (int(*)(int))std::toupper); - if ( upcasedText.substr(j, upcasedSlang.length()) == upcasedSlang ) + + for ( unsigned int j = 0; j < text.length(); j++) { - good = false; + if ( upcasedText.substr(j, upcasedSlang.length()) == upcasedSlang ) + { + isContentClean = false; + break; + } } + if (!isContentClean) break; + ++i; } - // Next slang - i++; + return isContentClean; + } + else + { + return true; + LOG_INFO("Slangs List is not initialized.", 2) } - - return good; } } // ::utils diff --git a/src/utils/slangsfilter.h b/src/utils/slangsfilter.h index cb4d2ecc..f7ccc33f 100644 --- a/src/utils/slangsfilter.h +++ b/src/utils/slangsfilter.h @@ -25,19 +25,61 @@ #define _TMWSERV_SLANGSFILTER_H_ #include <string> +#include "../configuration.h" namespace tmwserv { namespace utils { - /** - * Useful to filter slangs automatically, by instance. - * @return true if the sentence is slangs clear. - */ - bool filterContent(std::string text); +/** + * Used to filter content containing bad words. Like username, character's names, chat, ... + */ +class SlangsFilter +{ + public: + /** + * ctors. + */ + SlangsFilter(Configuration *config); + + ~SlangsFilter(); + + /** + * Load slang list from the config file. + * + * @return true is the config is loaded succesfully + * + */ + bool + loadFilterList(); + + /** + * Write slang list to the config file. + * + * @return true is the config is loaded succesfully + * + */ + void + writeFilterList(); + + /** + * Useful to filter slangs automatically, by instance. + * @return true if the sentence is slangs clear. + */ + bool + filterContent(const std::string& text); + + + private: + std::list<std::string> mSlangs; /**< the formatted Slangs list */ + bool mInitialized; /**< Set if the list is loaded */ + Configuration *mConfig; /**< The config instance */ +}; } // ::utils } // ::tmwserv +extern tmwserv::utils::SlangsFilter *slangsFilter; + #endif |