diff options
author | Yohann Ferreira <bertram@cegetel.net> | 2005-12-27 03:42:40 +0000 |
---|---|---|
committer | Yohann Ferreira <bertram@cegetel.net> | 2005-12-27 03:42:40 +0000 |
commit | 205579c3be58537060785d174f9f67c89444a21b (patch) | |
tree | 31c7c8e0af476e70259650e1c577eaf299f3fc78 /src/chathandler.cpp | |
parent | ad9ecbf35e0d986d704deba6f8f20ad72a94a99e (diff) | |
download | manaserv-205579c3be58537060785d174f9f67c89444a21b.tar.gz manaserv-205579c3be58537060785d174f9f67c89444a21b.tar.bz2 manaserv-205579c3be58537060785d174f9f67c89444a21b.tar.xz manaserv-205579c3be58537060785d174f9f67c89444a21b.zip |
Implemented common chat handling, except for chatting in channels. Also the Channel registering/unregistering isn't there yet and the commands needs to be implemented. Added a small slangs filter to reduce bad words in account names and in conversations a little.
Diffstat (limited to 'src/chathandler.cpp')
-rw-r--r-- | src/chathandler.cpp | 175 |
1 files changed, 168 insertions, 7 deletions
diff --git a/src/chathandler.cpp b/src/chathandler.cpp index 1a25ce57..73906275 100644 --- a/src/chathandler.cpp +++ b/src/chathandler.cpp @@ -21,14 +21,39 @@ * $Id$ */ +#include <cctype> #include "chathandler.h" +#include "state.h" +#include "being.h" #include "defines.h" -#include <iostream> +#include "utils/logger.h" +#include "utils/slangsfilter.h" void ChatHandler::receiveMessage(NetComputer &computer, MessageIn &message) { - if (computer.getCharacter().get() == NULL) - return; // character not selected + // If not logged in... + if (computer.getAccount().get() == NULL) + { + LOG_INFO("Not logged in, can't chat...", 2) + MessageOut result; + result.writeShort(SMSG_CHAT); + result.writeByte(CHAT_NOLOGIN); + computer.send(result.getPacket()); + return; + } + else + { + // If no character selected yet... + if (computer.getCharacter().get() == NULL) + { + MessageOut result; + result.writeShort(SMSG_CHAT); + result.writeByte(CHAT_NO_CHARACTER_SELECTED); + computer.send(result.getPacket()); + LOG_INFO("No character selected. Can't chat...", 2) + return; // character not selected + } + } switch (message.getId()) { @@ -36,15 +61,55 @@ void ChatHandler::receiveMessage(NetComputer &computer, MessageIn &message) { // chat to people around area std::string text = message.readString(); - short channel = message.readShort(); - std::cout << "Say (" << channel << "): " << text << std::endl; + // If it's slang clean, + if (tmwserv::utils::filterContent(text)) + { + short channel = message.readShort(); + LOG_INFO("Say: (Channel " << channel << "): " << text, 2) + if ( channel == 0 ) // Let's say that is the default channel for now. + { + if ( text.substr(0, 1) == "@" || text.substr(0, 1) == "#" || text.substr(0, 1) == "/" ) + { + // The message is a command. Deal with it. + handleCommand(computer, text); + } + else + { + // The default channel (0) is when the character speaks + // to the characters around him in the map. + // We, then, look for every characters around him and + // send the message to them. + // By 'around', let's say 10 tiles square wide for now. + sayAround(computer, text); + } + } + else + { + // We send the message to the players registered in the channel. + sayInChannel(computer, channel, text); + } + } + else + { + warnPlayerAboutBadWords(computer); + } } break; case CMSG_ANNOUNCE: { std::string text = message.readString(); - std::cout << "Announce: " << text << std::endl; + // If it's slang's free. + if (tmwserv::utils::filterContent(text)) + { + // We send the message to every players in the default channel + // as it is an annouce. + announce(computer, text); + } + else + { + warnPlayerAboutBadWords(computer); + } } break; @@ -52,10 +117,106 @@ void ChatHandler::receiveMessage(NetComputer &computer, MessageIn &message) { std::string user = message.readString(); std::string text = message.readString(); + if (tmwserv::utils::filterContent(text)) + { + // We seek the player to whom the message is told + // and send it to her/him. + sayToPlayer(computer, user, text); + } + else + { + warnPlayerAboutBadWords(computer); + } } break; default: - std::cout << "Invalid message type" << std::endl; + LOG_INFO("Chat: Invalid message type", 2) break; } } + +void ChatHandler::handleCommand(NetComputer &computer, std::string command) +{ + LOG_INFO("Chat: Recieved unhandled command: " << command, 2) + MessageOut result; + result.writeShort(SMSG_CHAT); + result.writeShort(0); // The Channel + result.writeString("SERVER: Unknown or unhandled command."); + computer.send(result.getPacket()); +} + +void ChatHandler::warnPlayerAboutBadWords(NetComputer &computer) +{ + // We could later count if the player is really often unpolite. + MessageOut result; + result.writeShort(SMSG_CHAT); + result.writeShort(0); // The Channel + result.writeString("SERVER: Take care not to use bad words when you speak..."); + computer.send(result.getPacket()); + + LOG_INFO(computer.getCharacter()->getName() << " says bad words.", 2) +} + +void ChatHandler::announce(NetComputer &computer, std::string text) +{ + MessageOut result; + if ( computer.getAccount()->getLevel() == (AccountLevels)AL_ADMIN || + computer.getAccount()->getLevel() == (AccountLevels)AL_GM ) + { + LOG_INFO("ANNOUNCE: " << text, 0) + // Send it to every beings. + result.writeShort(SMSG_ANNOUNCEMENT); + result.writeString(text); + connectionHandler->sendToEveryone(result); + } + else + { + result.writeShort(SMSG_SYSTEM); + result.writeString("Cannot make announcements. You have not enough rights."); + computer.send(result.getPacket()); + LOG_INFO(computer.getCharacter()->getName() << + " couldn't make an announcement due to insufficient rights.", 2) + } +} + +void ChatHandler::sayAround(NetComputer &computer, std::string text) +{ + MessageOut result; + LOG_INFO( computer.getCharacter()->getName() << " says: " << text, 2) + // Send it to every beings around + result.writeShort(SMSG_CHAT); + result.writeShort(0); // The default channel + std::string say = computer.getCharacter()->getName(); + say += ": "; + say += text; + result.writeString(say); + connectionHandler->sendAround(computer.getCharacter(), result); +} + +void ChatHandler::sayToPlayer(NetComputer &computer, std::string playerName, std::string text) +{ + MessageOut result; + LOG_INFO( computer.getCharacter()->getName() << " says to " << playerName + << ": " << text, 2) + // Send it to the being if the being exists + result.writeShort(SMSG_PRIVMSG); + std::string say = computer.getCharacter()->getName(); + say += ": "; + say += text; + result.writeString(say); + connectionHandler->sendTo(playerName, result); +} + +void ChatHandler::sayInChannel(NetComputer &computer, short channel, std::string text) +{ + MessageOut result; + LOG_INFO( computer.getCharacter()->getName() << " says in channel " << channel + << ": " << text, 2) + // TODO: Send it to every beings in channel + result.writeShort(SMSG_CHAT); + result.writeShort(channel); + std::string say = computer.getCharacter()->getName(); + say += ": "; + say += text; + result.writeString(say); +} |