From e5d4c719a52d03bfe9bfb4ea2fea0570842985bc Mon Sep 17 00:00:00 2001 From: Roderic Morris Date: Mon, 7 Jul 2008 14:50:10 +0000 Subject: get rid of channel registering --- src/chat-server/chatchannelmanager.cpp | 30 ++++++++++++ src/chat-server/chatchannelmanager.hpp | 7 +++ src/chat-server/chathandler.cpp | 85 +++++----------------------------- src/chat-server/chathandler.hpp | 6 --- src/defines.h | 2 - 5 files changed, 48 insertions(+), 82 deletions(-) (limited to 'src') diff --git a/src/chat-server/chatchannelmanager.cpp b/src/chat-server/chatchannelmanager.cpp index 2b312b75..33db65ca 100644 --- a/src/chat-server/chatchannelmanager.cpp +++ b/src/chat-server/chatchannelmanager.cpp @@ -29,6 +29,8 @@ #include "account-server/dalstorage.hpp" #include "chat-server/chatclient.hpp" #include "chat-server/chathandler.hpp" +#include "chat-server/guildmanager.hpp" +#include "utils/stringfilter.h" ChatChannelManager::ChatChannelManager() : mNextChannelId(1) { @@ -57,6 +59,34 @@ ChatChannelManager::createNewChannel(const std::string &channelName, return channelId; } +bool ChatChannelManager::tryNewPublicChannel(const std::string &name) +{ + if (!stringFilter->filterContent(name)) + { + return false; + } + + // Checking strings for length and double quotes + if (name.empty() || + name.length() > MAX_CHANNEL_NAME || + stringFilter->findDoubleQuotes(name)) + { + return false; + } + else if (guildManager->doesExist(name) || + channelExists(name)) + { + // Channel already exists + return false; + } + else + { + // We attempt to create a new channel + short id = createNewChannel(name, "", "", true); + return id ? true : false; + } +} + bool ChatChannelManager::removeChannel(int channelId) { ChatChannelIterator i = mChatChannels.find(channelId); diff --git a/src/chat-server/chatchannelmanager.hpp b/src/chat-server/chatchannelmanager.hpp index 05761506..a0342d84 100644 --- a/src/chat-server/chatchannelmanager.hpp +++ b/src/chat-server/chatchannelmanager.hpp @@ -57,6 +57,13 @@ class ChatChannelManager const std::string &channelPassword, bool joinable); + /** + * Try to create a new public channel with the given name. + * + * @return true if created successfully, false otherwise. + */ + bool tryNewPublicChannel(const std::string &name); + /** * Remove a channel. */ diff --git a/src/chat-server/chathandler.cpp b/src/chat-server/chathandler.cpp index 189fbe57..f2c48e33 100644 --- a/src/chat-server/chathandler.cpp +++ b/src/chat-server/chathandler.cpp @@ -153,10 +153,6 @@ void ChatHandler::processMessage(NetComputer *comp, MessageIn &message) handlePrivMsgMessage(computer, message); break; - case PCMSG_REGISTER_CHANNEL: - handleRegisterChannelMessage(computer, message); - break; - case PCMSG_ENTER_CHANNEL: handleEnterChannelMessage(computer, message); break; @@ -321,84 +317,21 @@ ChatHandler::handlePrivMsgMessage(ChatClient &client, MessageIn &msg) sayToPlayer(client, user, text); } -void -ChatHandler::handleRegisterChannelMessage(ChatClient &client, MessageIn &msg) -{ - MessageOut reply(CPMSG_REGISTER_CHANNEL_RESPONSE); - - std::string channelName = msg.readString(); - std::string channelAnnouncement = msg.readString(); - std::string channelPassword = msg.readString(); - - if (!stringFilter->filterContent(channelName) || - !stringFilter->filterContent(channelAnnouncement)) - { - warnPlayerAboutBadWords(client); - return; - } - - // Checking strings for length and double quotes - if (channelName.empty() || - channelName.length() > MAX_CHANNEL_NAME || - stringFilter->findDoubleQuotes(channelName) || - channelAnnouncement.length() > MAX_CHANNEL_ANNOUNCEMENT || - stringFilter->findDoubleQuotes(channelAnnouncement) || - channelPassword.length() > MAX_CHANNEL_PASSWORD || - stringFilter->findDoubleQuotes(channelPassword)) - { - reply.writeByte(ERRMSG_INVALID_ARGUMENT); - } - else if (guildManager->doesExist(channelName) || - chatChannelManager->channelExists(channelName)) - { - // Channel already exists - reply.writeByte(ERRMSG_ALREADY_TAKEN); - } - else - { - // We attempt to create a new channel - short channelId; - - channelId = chatChannelManager->createNewChannel( - channelName, - channelAnnouncement, - channelPassword, - true); - - if (channelId) - { - // We add the player as admin of this channel as he created it. The - // user registering a private channel is the only one to be able to - // update the password and the announcement in it and also to - // remove it. - ChatChannel *channel = chatChannelManager->getChannel(channelId); - channel->addUser(&client); - - reply.writeByte(ERRMSG_OK); - reply.writeShort(channelId); - reply.writeString(channelName); - reply.writeString(channelAnnouncement); - } - else - { - reply.writeByte(ERRMSG_FAILURE); - } - } - - client.send(reply); -} - void ChatHandler::handleEnterChannelMessage(ChatClient &client, MessageIn &msg) { MessageOut reply(CPMSG_ENTER_CHANNEL_RESPONSE); std::string channelName = msg.readString(); std::string givenPassword = msg.readString(); - - ChatChannel *channel = chatChannelManager->getChannel(channelName); + ChatChannel *channel = NULL; + if(chatChannelManager->channelExists(channelName) || + chatChannelManager->tryNewPublicChannel(channelName)) + { + channel = chatChannelManager->getChannel(channelName); + } if (!channel) - { + { reply.writeByte(ERRMSG_INVALID_ARGUMENT); } else if (!channel->getPassword().empty() && @@ -470,6 +403,10 @@ ChatHandler::handleQuitChannelMessage(ChatClient &client, MessageIn &msg) warnUsersAboutPlayerEventInChat(channel, client.characterName, CHAT_EVENT_LEAVING_PLAYER); + if(channel->getUserList().empty()) + { + chatChannelManager->removeChannel(channel->getId()); + } } client.send(reply); diff --git a/src/chat-server/chathandler.hpp b/src/chat-server/chathandler.hpp index 7469e9e1..73ca90cd 100644 --- a/src/chat-server/chathandler.hpp +++ b/src/chat-server/chathandler.hpp @@ -155,12 +155,6 @@ class ChatHandler : public ConnectionHandler void handlePrivMsgMessage(ChatClient &client, MessageIn &msg); - /** - * Deal with channel registration. - */ - void - handleRegisterChannelMessage(ChatClient &client, MessageIn &msg); - /** * Deal with player entering channel. */ diff --git a/src/defines.h b/src/defines.h index e425f554..b893970a 100644 --- a/src/defines.h +++ b/src/defines.h @@ -218,8 +218,6 @@ enum { PCMSG_ANNOUNCE = 0x0411, // S text PCMSG_PRIVMSG = 0x0412, // S user, S text // -- Channeling - PCMSG_REGISTER_CHANNEL = 0x0420, // S name, S topic, S password - CPMSG_REGISTER_CHANNEL_RESPONSE = 0x0421, // B error, W id, S name, S topic CPMSG_CHANNEL_EVENT = 0x0430, // W channel, B event, S info PCMSG_ENTER_CHANNEL = 0x0440, // S channel, S password CPMSG_ENTER_CHANNEL_RESPONSE = 0x0441, // B error, W id, S name, S topic, S userlist -- cgit v1.2.3-60-g2f50