summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog12
-rw-r--r--src/accounthandler.cpp165
-rw-r--r--src/accounthandler.h6
-rw-r--r--src/chathandler.cpp6
-rw-r--r--src/defines.h128
-rw-r--r--src/main.cpp78
-rw-r--r--src/utils/slangsfilter.cpp96
-rw-r--r--src/utils/slangsfilter.h52
8 files changed, 410 insertions, 133 deletions
diff --git a/ChangeLog b/ChangeLog
index 0b0b4dae..21eb92f6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,14 @@
-2005-12-29 Yohann Ferreira <bjorn@lindeijer.nl>
+2005-12-31 Yohann Ferreira <bertram@cegetel.net>
+
+ * src/main.cpp, src/defines.h, src/utils/slangsfilter.h,
+ src/utils/slangsfilter.cpp, src/accounthandler.h,
+ src/accounthandler.cpp, src/chathandler.cpp: Adding the possibility to
+ change Email, and password. Turned Slangs Filter into a class. Use
+ some ifdefs for future configure options handling. Still has to update
+ the client. (It isn't compiled fully with make; Removing makeclient.sh
+ was not a good idea, maybe.)
+
+2005-12-29 Yohann Ferreira <bertram@cegetel.net>
* src/main.cpp, src/connectionhandler.cpp, src/defines.h,
src/chathandler.cpp: Adding changes thought by Elven and an option for
diff --git a/src/accounthandler.cpp b/src/accounthandler.cpp
index fff2223f..d99340b6 100644
--- a/src/accounthandler.cpp
+++ b/src/accounthandler.cpp
@@ -151,7 +151,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message)
std::string email = message.readString();
// Checking if the Name is slang's free.
- if (!tmwserv::utils::filterContent(username))
+ if (!slangsFilter->filterContent(username))
{
result.writeShort(SMSG_REGISTER_RESPONSE);
result.writeByte(REGISTER_INVALID_USERNAME);
@@ -161,34 +161,6 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message)
// Checking conditions for having a good account.
LOG_INFO(username << " is trying to register.", 1)
- bool emailValid = false;
- // Testing Email validity
- if ( (email.length() < MIN_EMAIL_LENGTH) || (email.length() > MAX_EMAIL_LENGTH))
- {
- result.writeShort(SMSG_REGISTER_RESPONSE);
- result.writeByte(REGISTER_INVALID_EMAIL);
- LOG_INFO(email << ": Email too short or too long.", 1)
- break;
- }
- if (store.doesEmailAlreadyExists(email)) // Search if Email already exists
- {
- result.writeShort(SMSG_REGISTER_RESPONSE);
- result.writeByte(REGISTER_EXISTS_EMAIL);
- LOG_INFO(email << ": Email already exists.", 1)
- break;
- }
- if ((email.find_first_of('@') != std::string::npos)) // Searching for an @.
- {
- int atpos = email.find_first_of('@');
- if (email.find_first_of('.', atpos) != std::string::npos) // Searching for a '.' after the @.
- {
- if (email.find_first_of(' ') == std::string::npos) // Searching if there's no spaces.
- {
- emailValid = true;
- }
- }
- }
-
// see if the account exists
tmwserv::AccountPtr accPtr = store.getAccount(username);
if ( accPtr.get() ) // Account already exists.
@@ -209,12 +181,18 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message)
result.writeByte(REGISTER_INVALID_PASSWORD);
LOG_INFO(email << ": Password too short or too long.", 1)
}
- else if (!emailValid)
+ else if (!isEmailValid(email))
{
result.writeShort(SMSG_REGISTER_RESPONSE);
result.writeByte(REGISTER_INVALID_EMAIL);
LOG_INFO(email << ": Email Invalid, only a@b.c format is accepted.", 1)
}
+ else if (store.doesEmailAlreadyExists(email)) // Search if Email already exists
+ {
+ result.writeShort(SMSG_REGISTER_RESPONSE);
+ result.writeByte(REGISTER_EXISTS_EMAIL);
+ LOG_INFO(email << ": Email already exists.", 1)
+ }
else
{
AccountPtr acc(new Account(username, password, email));
@@ -272,6 +250,102 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message)
}
break;
+ case CMSG_EMAIL_CHANGE:
+ {
+ if (computer.getAccount().get() == NULL) {
+ result.writeShort(SMSG_EMAIL_CHANGE_RESPONSE);
+ result.writeByte(EMAILCHG_NOLOGIN);
+ LOG_INFO("Not logged in. Can't change your Account's Email.", 1)
+ break;
+ }
+
+ std::string email = message.readString();
+ if ( !isEmailValid(email) )
+ {
+ result.writeShort(SMSG_EMAIL_CHANGE_RESPONSE);
+ result.writeByte(EMAILCHG_INVALID);
+ LOG_INFO(email << ": Invalid format, cannot change Email for " <<
+ computer.getAccount()->getName(), 1)
+ }
+ else if (store.doesEmailAlreadyExists(email)) // Search if Email already exists
+ {
+ result.writeShort(SMSG_EMAIL_CHANGE_RESPONSE);
+ result.writeByte(EMAILCHG_EXISTS_EMAIL);
+ LOG_INFO(email << ": Email already exists.", 1)
+ }
+ else
+ {
+ computer.getAccount()->setEmail(email);
+ result.writeShort(SMSG_EMAIL_CHANGE_RESPONSE);
+ result.writeByte(EMAILCHG_OK);
+ LOG_INFO(computer.getAccount()->getName() << ": Email changed to: " <<
+ email, 1)
+ }
+ }
+ break;
+
+ case CMSG_EMAIL_GET:
+ {
+ if (computer.getAccount().get() == NULL) {
+ result.writeShort(SMSG_EMAIL_GET_RESPONSE);
+ result.writeByte(EMAILGET_NOLOGIN);
+ LOG_INFO("Not logged in. Can't get your Account's current Email.", 1)
+ break;
+ }
+ else
+ {
+ result.writeShort(SMSG_EMAIL_GET_RESPONSE);
+ result.writeByte(EMAILGET_OK);
+ result.writeString(computer.getAccount()->getEmail());
+ }
+ }
+ break;
+
+ case CMSG_PASSWORD_CHANGE:
+ {
+ if (computer.getAccount().get() == NULL)
+ {
+ result.writeShort(SMSG_PASSWORD_CHANGE_RESPONSE);
+ result.writeByte(PASSCHG_NOLOGIN);
+ LOG_INFO("Not logged in. Can't change your Account's Password.", 1)
+ break;
+ }
+ std::string oldPassword = message.readString();
+ std::string password1 = message.readString();
+ std::string password2 = message.readString();
+ if ( password1.length() < MIN_PASSWORD_LENGTH ||
+ password1.length() > MAX_PASSWORD_LENGTH )
+ {
+ result.writeShort(SMSG_PASSWORD_CHANGE_RESPONSE);
+ result.writeByte(PASSCHG_INVALID);
+ LOG_INFO(computer.getAccount()->getName() <<
+ ": New password too long or too short.", 1)
+ }
+ else if ( password1 != password2 )
+ {
+ result.writeShort(SMSG_PASSWORD_CHANGE_RESPONSE);
+ result.writeByte(PASSCHG_MISMATCH);
+ LOG_INFO(computer.getAccount()->getName() <<
+ ": New password mismatched confirmation password.", 1)
+ }
+ else if ( oldPassword != computer.getAccount()->getPassword() )
+ {
+ result.writeShort(SMSG_PASSWORD_CHANGE_RESPONSE);
+ result.writeByte(PASSCHG_MISMATCH);
+ LOG_INFO(computer.getAccount()->getName() <<
+ ": Old password is wrong.", 1)
+ }
+ else
+ {
+ computer.getAccount()->setPassword(password1);
+ result.writeShort(SMSG_PASSWORD_CHANGE_RESPONSE);
+ result.writeByte(PASSCHG_OK);
+ LOG_INFO(computer.getAccount()->getName() <<
+ ": The password was changed.", 1)
+ }
+ }
+ break;
+
case CMSG_CHAR_CREATE:
{
if (computer.getAccount().get() == NULL) {
@@ -293,6 +367,14 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message)
}
std::string name = message.readString();
+ // Checking if the Name is slang's free.
+ if (!slangsFilter->filterContent(name))
+ {
+ result.writeShort(SMSG_CHAR_CREATE_RESPONSE);
+ result.writeByte(CREATE_INVALID_NAME);
+ LOG_INFO(name << ": Character has got bad words in it.", 1)
+ break;
+ }
// Check if the character's name already exists
if (store.doesCharacterNameExists(name))
{
@@ -516,3 +598,26 @@ AccountHandler::assignAccount(NetComputer &computer, tmwserv::Account *account)
return TMW_SUCCESS;
}
+
+bool
+AccountHandler::isEmailValid(std::string& email)
+{
+ // Testing Email validity
+ if ( (email.length() < MIN_EMAIL_LENGTH) || (email.length() > MAX_EMAIL_LENGTH))
+ {
+ LOG_INFO(email << ": Email too short or too long.", 1)
+ return false;
+ }
+ if ((email.find_first_of('@') != std::string::npos)) // Searching for an @.
+ {
+ int atpos = email.find_first_of('@');
+ if (email.find_first_of('.', atpos) != std::string::npos) // Searching for a '.' after the @.
+ {
+ if (email.find_first_of(' ') == std::string::npos) // Searching if there's no spaces.
+ {
+ return true;
+ }
+ }
+ }
+ return false;
+}
diff --git a/src/accounthandler.h b/src/accounthandler.h
index 14f13cf3..f42b424b 100644
--- a/src/accounthandler.h
+++ b/src/accounthandler.h
@@ -55,6 +55,12 @@ class AccountHandler : public MessageHandler
* Account assignment.
*/
int assignAccount(NetComputer &computer, tmwserv::Account *account);
+
+ /**
+ * Check an Email Validity
+ */
+ bool
+ isEmailValid(std::string& email);
};
#endif
diff --git a/src/chathandler.cpp b/src/chathandler.cpp
index 3e5f6010..3d109274 100644
--- a/src/chathandler.cpp
+++ b/src/chathandler.cpp
@@ -62,7 +62,7 @@ void ChatHandler::receiveMessage(NetComputer &computer, MessageIn &message)
// chat to people around area
std::string text = message.readString();
// If it's slang clean,
- if (tmwserv::utils::filterContent(text))
+ if (slangsFilter->filterContent(text))
{
short channel = message.readShort();
LOG_INFO("Say: (Channel " << channel << "): " << text, 2)
@@ -100,7 +100,7 @@ void ChatHandler::receiveMessage(NetComputer &computer, MessageIn &message)
{
std::string text = message.readString();
// If it's slang's free.
- if (tmwserv::utils::filterContent(text))
+ if (slangsFilter->filterContent(text))
{
// We send the message to every players in the default channel
// as it is an annouce.
@@ -117,7 +117,7 @@ void ChatHandler::receiveMessage(NetComputer &computer, MessageIn &message)
{
std::string user = message.readString();
std::string text = message.readString();
- if (tmwserv::utils::filterContent(text))
+ if (slangsFilter->filterContent(text))
{
// We seek the player to whom the message is told
// and send it to her/him.
diff --git a/src/defines.h b/src/defines.h
index 8bc7218c..a997ace9 100644
--- a/src/defines.h
+++ b/src/defines.h
@@ -91,65 +91,73 @@ enum {
*/
enum {
// Login/Register
- CMSG_REGISTER = 0x0000,
- CMSG_ENCRYPTED_REGISTER = 0x0001,
- SMSG_REGISTER_RESPONSE = 0x0002,
- CMSG_UNREGISTER = 0x0003,
- SMSG_UNREGISTER_RESPONSE = 0x0004,
- CMSG_LOGIN = 0x0010,
- CMSG_ENCRYPTED_LOGIN = 0x0011,
- SMSG_LOGIN_ERROR = 0x0012,
- SMSG_LOGIN_CONFIRM = 0x0013,
- CMSG_LOGOUT = 0x0014,
- SMSG_LOGOUT_ERROR = 0x0015,
- SMSG_LOGOUT_CONFIRM = 0x0016,
- CMSG_CHAR_CREATE = 0x0020,
- SMSG_CHAR_CREATE_RESPONSE = 0x0021,
- CMSG_CHAR_DELETE = 0x0022,
- SMSG_CHAR_DELETE_RESPONSE = 0x0023,
- CMSG_CHAR_LIST = 0x0024, // this is required after char creation
- SMSG_CHAR_LIST_RESPONSE = 0x0025,
- CMSG_CHAR_SELECT = 0x0030,
- SMSG_CHAR_SELECT_RESPONSE = 0x0031,
+ CMSG_REGISTER = 0x0000,
+ CMSG_ENCRYPTED_REGISTER = 0x0001,
+ SMSG_REGISTER_RESPONSE = 0x0002,
+ CMSG_UNREGISTER = 0x0003,
+ SMSG_UNREGISTER_RESPONSE = 0x0004,
+ CMSG_LOGIN = 0x0010,
+ CMSG_ENCRYPTED_LOGIN = 0x0011,
+ SMSG_LOGIN_ERROR = 0x0012,
+ SMSG_LOGIN_CONFIRM = 0x0013,
+ CMSG_LOGOUT = 0x0014,
+ SMSG_LOGOUT_ERROR = 0x0015,
+ SMSG_LOGOUT_CONFIRM = 0x0016,
+ CMSG_CHAR_CREATE = 0x0020,
+ SMSG_CHAR_CREATE_RESPONSE = 0x0021,
+ CMSG_CHAR_DELETE = 0x0022,
+ SMSG_CHAR_DELETE_RESPONSE = 0x0023,
+ CMSG_CHAR_LIST = 0x0024, // this is required after char creation
+ SMSG_CHAR_LIST_RESPONSE = 0x0025,
+ CMSG_CHAR_SELECT = 0x0026,
+ SMSG_CHAR_SELECT_RESPONSE = 0x0027,
+ CMSG_EMAIL_CHANGE = 0x0030,
+ SMSG_EMAIL_CHANGE_RESPONSE = 0x0031,
+ CMSG_EMAIL_GET = 0x0032,
+ SMSG_EMAIL_GET_RESPONSE = 0x0033,
+ CMSG_FORGOT_PASSWORD = 0x0040,
+ SMSG_FORGOT_PASSWORD_RESPONSE = 0x0041,
+ CMSG_PASSWORD_CHANGE = 0x0050,
+ SMSG_PASSWORD_CHANGE_RESPONSE = 0x0051,
// Objects
- SMSG_NEW_OBJECT = 0x0100,
- SMSG_REMOVE_OBJECT = 0x0101,
- SMSG_CHANGE_OBJECT = 0x0102,
- CMSG_PICKUP = 0x0110,
- SMSG_PICKUP_RESPONSE = 0x0111,
- CMSG_USE_OBJECT = 0x0120,
- SMSG_USE_RESPONSE = 0x0121,
+ SMSG_NEW_OBJECT = 0x0100,
+ SMSG_REMOVE_OBJECT = 0x0101,
+ SMSG_CHANGE_OBJECT = 0x0102,
+ CMSG_PICKUP = 0x0110,
+ SMSG_PICKUP_RESPONSE = 0x0111,
+ CMSG_USE_OBJECT = 0x0120,
+ SMSG_USE_RESPONSE = 0x0121,
// Beings
- SMSG_NEW_BEING = 0x0200,
- SMSG_REMOVE_BEING = 0x0201,
- SMSG_INVENTORY_UPD = 0x0210,
- SMSG_EQUIPMENT_UPD = 0x0220,
- SMSG_ATTACK = 0x0230,
- SMSG_PATH = 0x0240,
- CMSG_TARGET = 0x0250,
- CMSG_WALK = 0x0260,
- CMSG_START_TRADE = 0x0270,
- CMSG_START_TALK = 0x0280,
- CMSG_REQ_TRADE = 0x0290,
+ SMSG_NEW_BEING = 0x0200,
+ SMSG_REMOVE_BEING = 0x0201,
+ SMSG_INVENTORY_UPD = 0x0210,
+ SMSG_EQUIPMENT_UPD = 0x0220,
+ SMSG_ATTACK = 0x0230,
+ SMSG_PATH = 0x0240,
+ CMSG_TARGET = 0x0250,
+ CMSG_WALK = 0x0260,
+ CMSG_START_TRADE = 0x0270,
+ CMSG_START_TALK = 0x0280,
+ CMSG_REQ_TRADE = 0x0290,
// Items
- CMSG_USE_ITEM = 0x0300,
- CMSG_EQUIP = 0x0301,
- SMSG_EQUIP_RESPONSE = 0x0302,
+ CMSG_USE_ITEM = 0x0300,
+ CMSG_EQUIP = 0x0301,
+ SMSG_EQUIP_RESPONSE = 0x0302,
// Chat
- SMSG_SYSTEM = 0x0400,
- SMSG_CHAT = 0x0401,
- SMSG_ANNOUNCEMENT = 0x0402,
- SMSG_PRIVMSG = 0x0403,
- CMSG_SAY = 0x0410,
- CMSG_ANNOUNCE = 0x0411,
- CMSG_PRIVMSG = 0x0412,
+ SMSG_SYSTEM = 0x0400,
+ SMSG_CHAT = 0x0401,
+ SMSG_ANNOUNCEMENT = 0x0402,
+ SMSG_PRIVMSG = 0x0403,
+ CMSG_SAY = 0x0410,
+ CMSG_ANNOUNCE = 0x0411,
+ CMSG_PRIVMSG = 0x0412,
// Other
- SMSG_LOAD_MAP = 0x0500,
+ SMSG_LOAD_MAP = 0x0500,
// NOTE: We will need more messages for in-game control (eg. moving a client to a new map/position etc.). Currently the protocol only caters for the bare basics.
};
@@ -226,6 +234,28 @@ enum {
CHAR_LIST_NOLOGIN
};
+// Email change return values
+enum {
+ EMAILCHG_OK = 0,
+ EMAILCHG_NOLOGIN,
+ EMAILCHG_INVALID,
+ EMAILCHG_EXISTS_EMAIL
+};
+
+// Get Email return values
+enum {
+ EMAILGET_OK = 0,
+ EMAILGET_NOLOGIN
+};
+
+// Password change return values
+enum {
+ PASSCHG_OK = 0,
+ PASSCHG_NOLOGIN,
+ PASSCHG_INVALID,
+ PASSCHG_MISMATCH
+};
+
// Chat errors return values
enum {
// CHAT_OK = 0,
diff --git a/src/main.cpp b/src/main.cpp
index 766ca33c..b6b0659b 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -45,7 +45,7 @@
#include "storage.h"
#include "utils/logger.h"
-
+#include "utils/slangsfilter.h"
// Scripting
#ifdef SCRIPT_SUPPORT
@@ -69,12 +69,8 @@ std::string scriptLanugage = "none";
#endif // SCRIPT_SUPPORT
// Default options that automake should be able to override.
-#ifndef LOG_FILE
-#define LOG_FILE "tmwserv.log"
-#endif
-#ifndef CONFIG_FILE
-#define CONFIG_FILE "tmwserv.xml"
-#endif
+#define DEFAULT_LOG_FILE "tmwserv.log"
+#define DEFAULT_CONFIG_FILE "tmwserv.xml"
#ifndef DEFAULT_SERVER_PORT
#define DEFAULT_SERVER_PORT 9601
#endif
@@ -89,6 +85,8 @@ Skill skillTree("base"); /**< Skill tree */
Configuration config; /**< XML config reader */
+tmwserv::utils::SlangsFilter *slangsFilter; /**< Slang's Filter */
+
/** Account message handler */
AccountHandler *accountHandler;
@@ -123,12 +121,55 @@ Uint32 worldTick(Uint32 interval, void *param)
*/
void initialize()
{
+
+/**
+ * If the path values aren't defined, we set the default
+ * depending on the platform.
+ */
+// The config path
+#if defined CONFIG_FILE
+ std::string configPath = CONFIG_FILE;
+#else
+
+#ifdef __USE_UNIX98
+ std::string configPath = getenv("HOME");
+ configPath += "/.";
+ configPath += DEFAULT_CONFIG_FILE;
+#else // Win32, ...
+ std::string configPath = DEFAULT_CONFIG_FILE;
+#endif
+
+#endif // defined CONFIG_FILE
+
+// The log path
+#if defined LOG_FILE
+ std::string logPath = LOG_FILE;
+#else
+
+#ifdef __USE_UNIX98
+ std::string logPath = getenv("HOME");
+ logPath += "/.";
+ logPath += DEFAULT_LOG_FILE;
+#else // Win32, ...
+ std::string logPath = DEFAULT_LOG_FILE;
+#endif
+
+#endif // defined LOG_FILE
+
// initialize the logger.
using namespace tmwserv::utils;
- Logger::instance().setLogFile(LOG_FILE);
+ Logger::instance().setLogFile(logPath);
+
// write the messages to both the screen and the log file.
Logger::instance().setTeeMode(true);
+ config.init(configPath);
+ LOG_INFO("Using Config File: " << configPath, 0)
+ LOG_INFO("Using Log File: " << logPath, 0)
+
+ // Initialize the slang's filter.
+ slangsFilter = new SlangsFilter(&config);
+
// Initialize the global handlers
// FIXME: Make the global handlers global vars or part of a bigger
// singleton or a local vatiable in the event-loop
@@ -194,21 +235,6 @@ void initialize()
config.setValue("dbpass", "");
config.setValue("dbhost", "");
-#ifdef __USE_UNIX98
- std::string configPath = getenv("HOME");
- configPath += "/.";
- configPath += CONFIG_FILE;
- std::string logPath = getenv("HOME");
- logPath += "/.";
- logPath += LOG_FILE;
-#else
- std::string configPath = CONFIG_FILE;
- std::string logPath = LOG_FILE;
-#endif
- config.init(configPath);
- LOG_INFO("Using Config File: " << configPath, 0)
- LOG_INFO("Using Log File: " << logPath, 0)
-
// Initialize PhysicsFS
PHYSFS_init("");
@@ -224,6 +250,7 @@ void initialize()
*/
void deinitialize()
{
+ delete slangsFilter;
// Write configuration file
config.write();
@@ -260,7 +287,8 @@ void printHelp()
std::cout << "tmwserv" << std::endl << std::endl
<< "Options: " << std::endl
<< " -h --help : Display this help" << std::endl
- << " --verbosity <n> : Set the verbosity level" << std::endl;
+ << " --verbosity <n> : Set the verbosity level" << std::endl
+ << " --port <n> : Set the default port to listen on" << std::endl;
exit(0);
}
@@ -274,7 +302,7 @@ void parseOptions(int argc, char *argv[])
const struct option long_options[] = {
{ "help", no_argument, 0, 'h' },
{ "verbosity", required_argument, 0, 'v' },
- { "port", required_argument, 0, 'p' },
+ { "port", required_argument, 0, 'p' },
0
};
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