diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2007-07-17 18:34:48 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2007-07-17 18:34:48 +0000 |
commit | b41074c5c51ed9574bceddf94a727bbcd0513bd9 (patch) | |
tree | 5f3982506e0ca1fbb6068daf80be195e9db3286d /src/chat-server | |
parent | 28010750064bdf3bc8aea36565d788d8a068ef2a (diff) | |
download | manaserv-b41074c5c51ed9574bceddf94a727bbcd0513bd9.tar.gz manaserv-b41074c5c51ed9574bceddf94a727bbcd0513bd9.tar.bz2 manaserv-b41074c5c51ed9574bceddf94a727bbcd0513bd9.tar.xz manaserv-b41074c5c51ed9574bceddf94a727bbcd0513bd9.zip |
Some more cleanup of chat handler related code.
Diffstat (limited to 'src/chat-server')
-rw-r--r-- | src/chat-server/chatchannel.cpp | 84 | ||||
-rw-r--r-- | src/chat-server/chatchannel.hpp | 204 | ||||
-rw-r--r-- | src/chat-server/chatchannelmanager.cpp | 25 | ||||
-rw-r--r-- | src/chat-server/chatchannelmanager.hpp | 48 | ||||
-rw-r--r-- | src/chat-server/chathandler.cpp | 41 | ||||
-rw-r--r-- | src/chat-server/chathandler.hpp | 24 |
6 files changed, 202 insertions, 224 deletions
diff --git a/src/chat-server/chatchannel.cpp b/src/chat-server/chatchannel.cpp index 98143750..bf8bce50 100644 --- a/src/chat-server/chatchannel.cpp +++ b/src/chat-server/chatchannel.cpp @@ -23,82 +23,45 @@ #include "chat-server/chatchannel.hpp" -ChatChannel::ChatChannel(const std::string &channelName, - const std::string &channelAnnouncement = "None", - const std::string &channelPassword = "None", - bool channelPrivacy = true): - mChannelName(channelName), - mChannelAnnouncement(channelAnnouncement), - mChannelPassword(channelPassword), - mChannelPrivate(channelPrivacy) +ChatChannel::ChatChannel(const std::string &name, + const std::string &announcement, + const std::string &password, + bool privacy): + mName(name), + mAnnouncement(announcement), + mPassword(password), + mPrivate(privacy) { - if (channelAnnouncement == "") - mChannelAnnouncement = "None"; - if (channelPassword == "") - mChannelPassword = "None"; - mRegisteredUsers.clear(); -} - -ChatChannel::~ChatChannel() -{ - mRegisteredUsers.clear(); -} - - -const std::string& -ChatChannel::getName() const -{ - return mChannelName; -} - -const std::string& -ChatChannel::getAnnouncement() const -{ - return mChannelAnnouncement; -} - -const std::string& -ChatChannel::getPassword() const -{ - return mChannelPassword; -} - -bool -ChatChannel::getPrivacy() const -{ - return mChannelPrivate; + if (announcement == "") + mAnnouncement = "None"; + if (password == "") + mPassword = "None"; } void -ChatChannel::setName(const std::string &channelName) +ChatChannel::setName(const std::string &name) { - mChannelName = channelName; + mName = name; } void -ChatChannel::setAnnouncement(const std::string &channelAnnouncement) +ChatChannel::setAnnouncement(const std::string &announcement) { - if (channelAnnouncement == "") - mChannelAnnouncement = "None"; + if (announcement == "") + mAnnouncement = "None"; else - mChannelAnnouncement = channelAnnouncement; + mAnnouncement = announcement; } void -ChatChannel::setPassword(const std::string &channelPassword) +ChatChannel::setPassword(const std::string &password) { - if (channelPassword == "") - mChannelPassword = "None"; + if (password == "") + mPassword = "None"; else - mChannelPassword = channelPassword; + mPassword = password; } -ChatChannel::ChannelUsers const &ChatChannel::getUserList() const -{ - return mRegisteredUsers; -} - - bool ChatChannel::addUserInChannel(std::string const &user) { // Check if the user already exists in the channel @@ -109,7 +72,6 @@ bool ChatChannel::addUserInChannel(std::string const &user) return true; } - bool ChatChannel::removeUserFromChannel(std::string const &user) { ChannelUsers::iterator i_end = mRegisteredUsers.end(), @@ -119,7 +81,7 @@ bool ChatChannel::removeUserFromChannel(std::string const &user) return true; } -void ChatChannel::removeEveryUsersFromChannel() +void ChatChannel::removeAllUsersFromChannel() { mRegisteredUsers.clear(); } diff --git a/src/chat-server/chatchannel.hpp b/src/chat-server/chatchannel.hpp index 268f1355..fd2bff35 100644 --- a/src/chat-server/chatchannel.hpp +++ b/src/chat-server/chatchannel.hpp @@ -27,104 +27,112 @@ #include <string> #include <vector> -class ChatChannel { - public: - typedef std::vector< std::string > ChannelUsers; - - /** - * Constructors - */ - ChatChannel(const std::string &channelName, - const std::string &channelAnnouncement, - const std::string &channelPassword, - bool channelPrivacy); - - /** - * Destructor - */ - ~ChatChannel(); - - /** - * Get the name of the channel - */ - const std::string& getName() const; - - /** - * Get the Announcement string of the channel - */ - const std::string& getAnnouncement() const; - - /** - * Get the password of the channel - */ - const std::string& getPassword() const; - - /** - * Get the password of the channel - */ - bool getPrivacy() const; - - /** - * Set the name of the channel - */ - void setName(const std::string &channelName); - - /** - * Set the Announcement string of the channel - */ - void setAnnouncement(const std::string &channelAnnouncement); - - /** - * Set the password of the channel - */ - void setPassword(const std::string &channelPassword); - - /** - * Get the list of the users registered in the channel - */ - ChannelUsers const &getUserList() const; - - /** - * Add a user in the channel - */ - bool addUserInChannel(std::string const &); - - /** - * Remove a user from the channel. - */ - bool removeUserFromChannel(std::string const &); - - /** - * Empties a channel from its users (admin included). - */ - void removeEveryUsersFromChannel(); - - private: - /** - * The Channel's name. - */ - std::string mChannelName; - - /** - * The Channel's announcement. - */ - std::string mChannelAnnouncement; - - /** - * The Channel's password. - */ - std::string mChannelPassword; - - /** - * The registered user list - */ - ChannelUsers mRegisteredUsers; - - /** - * Whether the channel is private - */ - bool mChannelPrivate; - +/** + * A chat channel. Optionally a channel is private, in which case a password is + * required to join it. + * + * No logic is currently associated with a chat channel except for making sure + * that no user joins the channel twice and checking that a user who leaves + * actually existed in the channel. + * + * TODO: b_lindeijer: It would be nicer when some more logic could be placed + * in this class to remove some weight from the ChatHandler. Referencing + * ChatClient instances would also be nicer than to store only the names + * of the characters. + */ +class ChatChannel +{ + public: + typedef std::vector<std::string> ChannelUsers; + + /** + * Constructor. + * + * TODO: b_lindeijer: I would say a channel can be defined as private + * when a non-empty password is set, in which case we can get + * rid of the privacy parameter. + * + * @param name the name of the channel. + * @param announcement a welcome message. + * @param password password (for private channels). + * @param privacy whether this channel is private. + */ + ChatChannel(const std::string &name, + const std::string &announcement = "", + const std::string &password = "", + bool privacy = false); + + /** + * Get the name of the channel. + */ + const std::string& getName() const + { return mName; } + + /** + * Get the announcement string of the channel. + */ + const std::string& getAnnouncement() const + { return mAnnouncement; } + + /** + * Get the password of the channel. + */ + const std::string& getPassword() const + { return mPassword; } + + /** + * Returns whether this channel is private. + */ + bool isPrivate() const + { return mPrivate; } + + /** + * Sets the name of the channel. + */ + void setName(const std::string &channelName); + + /** + * Sets the announcement string of the channel. + */ + void setAnnouncement(const std::string &channelAnnouncement); + + /** + * Sets the password of the channel. + */ + void setPassword(const std::string &channelPassword); + + /** + * Gets the list of the users registered in the channel. + */ + const ChannelUsers& getUserList() const + { return mRegisteredUsers; } + + /** + * Adds a user in the channel. + * + * @return whether the user was successfully added + */ + bool addUserInChannel(std::string const &); + + /** + * Removes a user from the channel. + * + * @return whether the user was successfully removed + */ + bool removeUserFromChannel(std::string const &); + + /** + * Empties a channel from its users (admin included). + */ + void removeAllUsersFromChannel(); + + private: + std::string mName; /**< The name of the channel. */ + std::string mAnnouncement; /**< Welcome message. */ + std::string mPassword; /**< The channel password. */ + ChannelUsers mRegisteredUsers; /**< Users in this channel. */ + + bool mPrivate; /**< Whether the channel is private. */ }; #endif diff --git a/src/chat-server/chatchannelmanager.cpp b/src/chat-server/chatchannelmanager.cpp index 6150769c..77f1c397 100644 --- a/src/chat-server/chatchannelmanager.cpp +++ b/src/chat-server/chatchannelmanager.cpp @@ -21,7 +21,7 @@ * $Id$ */ -#include <map> +#include <list> #include "account-server/storage.hpp" #include "chat-server/chatchannelmanager.hpp" @@ -92,25 +92,26 @@ bool ChatChannelManager::removeChannel(short channelId) { ChatChannelIterator i = mChatChannels.find(channelId); if (i == mChatChannels.end()) return false; - i->second.removeEveryUsersFromChannel(); + i->second.removeAllUsersFromChannel(); mChatChannels.erase(i); return true; } -std::string ChatChannelManager::getPublicChannelNames(short &numChannels) +std::list<std::string> +ChatChannelManager::getPublicChannelNames() { - std::string channels; + std::list<std::string> channels; + for (ChatChannels::const_iterator i = mChatChannels.begin(), i_end = mChatChannels.end(); i != i_end; ++i) { - if (!i->second.getPrivacy()) + if (!i->second.isPrivate()) { - channels.append(i->second.getName()); - channels += " "; - numChannels++; + channels.push_back(i->second.getName()); } } + return channels; } @@ -141,19 +142,19 @@ std::string ChatChannelManager::getChannelName(short channelId) std::string ChatChannelManager::getChannelAnnouncement(short channelId) { ChatChannelIterator i = mChatChannels.find(channelId); - return (i != mChatChannels.end()) ? i->second.getAnnouncement() : std::string(); + return (i != mChatChannels.end()) ? i->second.getAnnouncement() : ""; } std::string ChatChannelManager::getChannelPassword(short channelId) { ChatChannelIterator i = mChatChannels.find(channelId); - return (i != mChatChannels.end()) ? i->second.getPassword() : std::string(); + return (i != mChatChannels.end()) ? i->second.getPassword() : ""; } bool ChatChannelManager::getChannelPrivacy(short channelId) { ChatChannelIterator i = mChatChannels.find(channelId); - return (i != mChatChannels.end()) ? i->second.getPrivacy() : true; + return (i != mChatChannels.end()) ? i->second.isPrivate() : true; } bool ChatChannelManager::setChannelAnnouncement(short channelId, std::string const &channelAnnouncement) @@ -214,7 +215,7 @@ ChatChannelManager::getUserListInChannel(short channelId) return emptyList; } -bool ChatChannelManager::isChannelRegistered(short channelId) +bool ChatChannelManager::channelExists(short channelId) { ChatChannelIterator i = mChatChannels.find(channelId); return i != mChatChannels.end(); diff --git a/src/chat-server/chatchannelmanager.hpp b/src/chat-server/chatchannelmanager.hpp index 63a626fc..ecc12525 100644 --- a/src/chat-server/chatchannelmanager.hpp +++ b/src/chat-server/chatchannelmanager.hpp @@ -28,6 +28,10 @@ #include "chat-server/chatchannel.hpp" +/** + * The chat channel manager takes care of registering and removing public and + * private chat channels. Every channel gets a unique channel ID. + */ class ChatChannelManager { public: @@ -58,9 +62,9 @@ class ChatChannelManager * channels has already been reached or when a channel with the same * name already exists. * - * TODO: Pretty much the same as registering public channel. Maybe they - * should be merged and private/public should be passed as a - * boolean? + * TODO: b_lindeijer: Pretty much the same as registering public + * channel. Maybe they should be merged and private/public + * should be passed as a boolean? * * @return the ID of the registered channel, or 0 if the registering * was unsuccessful. @@ -75,19 +79,17 @@ class ChatChannelManager bool removeChannel(short channelId); /** - * Get all public channels. - * TODO: Why not return an actual std::list? + * Returns a list containing the names of all public channels. * - * @param numChannels The number of channels returned is stored here - * @return a list of channel names + * @return a list of public channel names */ - std::string getPublicChannelNames(short &numChannels); + std::list<std::string> getPublicChannelNames(); /** - * Get the number of channels that have been registered. - * TODO: Documentation doesn't match function name, needs fixing. + * Get the number of users that have joined a channel. * - * @return the number of registered channels + * @param channelName the name of the channel + * @return the number of users in the channel */ short getNumberOfChannelUsers(const std::string &channelName); @@ -100,53 +102,51 @@ class ChatChannelManager /** * Get the name of a channel from its id. - * TODO: Can probably return a const std::string& - * TODO: Possibly throw exception when id doesn't exist * * @return the name of the channel + * @deprecated Use ChatChannel::getName instead */ std::string getChannelName(short channelId); /** * Get the announcement string of a channel from its id. - * TODO: Can probably return a const std::string& - * TODO: Possibly throw exception when id doesn't exist * * @return the announcement string of the channel + * @deprecated Use ChatChannel::getAnnouncement instead */ std::string getChannelAnnouncement(short channelId); /** * Set the announcement string of a channel from its id. - * TODO: Documentation about returned value is broken * - * @return the announcement string of the channel + * @return whether the channel exists + * @deprecated Use ChatChannel::setAnnouncement instead */ bool setChannelAnnouncement(short channelId, std::string const &channelAnnouncement); /** * Set the password of a channel by its id. - * TODO: Documentation about returned value is broken * - * @return the password of the channel + * @return whether the channel exists + * @deprecated Use ChatChannel::setPassword instead */ bool setChannelPassword(short channelId, const std::string &channelPassword); /** * Get the password of a channel from its id. - * TODO: Can probably return a const std::string & * * @return the password of the channel + * @deprecated Use ChatChannel::getPassword instead */ std::string getChannelPassword(short channelId); /** * Get the privacy of the channel from its id. - * TODO: Rename to isPrivate? * * @return the privacy of the channel + * @deprecated Use ChatChannel::isPrivate instead */ bool getChannelPrivacy(short channelId); @@ -180,9 +180,11 @@ class ChatChannelManager std::vector<std::string> const &getUserListInChannel(short channelId); /** - * Tells if a channel exists. + * Returns whether a channel exists. + * + * @param channelId a channel ID */ - bool isChannelRegistered(short channelId); + bool channelExists(short channelId); private: typedef std::map<short, ChatChannel> ChatChannels; diff --git a/src/chat-server/chathandler.cpp b/src/chat-server/chathandler.cpp index 61f71c11..64138816 100644 --- a/src/chat-server/chathandler.cpp +++ b/src/chat-server/chathandler.cpp @@ -21,6 +21,8 @@ * $Id$ */ +#include <list> + #include "defines.h" #include "account-server/characterdata.hpp" #include "account-server/guild.hpp" @@ -37,7 +39,7 @@ #include "utils/tokendispenser.hpp" -class ChatClient: public NetComputer +class ChatClient : public NetComputer { public: /** @@ -159,7 +161,7 @@ void ChatHandler::processMessage(NetComputer *comp, MessageIn &message) pendingClients.insert(std::make_pair(magic_token, &computer)); return; } - + computer.characterName = i->second.character; computer.accountLevel = i->second.level; pendingLogins.erase(i); @@ -280,8 +282,8 @@ void ChatHandler::processMessage(NetComputer *comp, MessageIn &message) result.writeByte(ERRMSG_INVALID_ARGUMENT); break; } - - if(guildManager->doesExist(channelName)) + + if (guildManager->doesExist(channelName)) { result.writeByte(ERRMSG_INVALID_ARGUMENT); break; @@ -344,11 +346,11 @@ void ChatHandler::processMessage(NetComputer *comp, MessageIn &message) // Get character based on name. CharacterPtr character = serverHandler->getCharacter(computer.characterName); - if (!chatChannelManager->isChannelRegistered(channelId)) + if (!chatChannelManager->channelExists(channelId)) { result.writeByte(ERRMSG_INVALID_ARGUMENT); } - else if (channelId < (signed)MAX_PUBLIC_CHANNELS_RANGE) + else if (channelId < (signed) MAX_PUBLIC_CHANNELS_RANGE) { // Public channel if (computer.accountLevel == AL_ADMIN || computer.accountLevel == AL_GM) { @@ -408,7 +410,7 @@ void ChatHandler::processMessage(NetComputer *comp, MessageIn &message) std::string channelName = message.readString(); std::string givenPassword = message.readString(); short channelId = chatChannelManager->getChannelId(channelName); - if (channelId != 0 && chatChannelManager->isChannelRegistered(channelId)) + if (channelId != 0 && chatChannelManager->channelExists(channelId)) { std::string channelPassword = chatChannelManager->getChannelPassword(channelId); if (!channelPassword.empty()) @@ -469,7 +471,7 @@ void ChatHandler::processMessage(NetComputer *comp, MessageIn &message) short channelId = message.readShort(); std::string channelName = chatChannelManager->getChannelName(channelId); - if (channelId != 0 && chatChannelManager->isChannelRegistered(channelId)) + if (channelId != 0 && chatChannelManager->channelExists(channelId)) { if (chatChannelManager->removeUserFromChannel(computer.characterName, channelId)) { @@ -502,19 +504,16 @@ void ChatHandler::processMessage(NetComputer *comp, MessageIn &message) { result.writeShort(CPMSG_LIST_CHANNELS_RESPONSE); - short numberOfPublicChannels; - std::istringstream channels( - chatChannelManager->getPublicChannelNames( - numberOfPublicChannels)); + std::list<std::string> publicChannels = + chatChannelManager->getPublicChannelNames(); + std::list<std::string>::iterator i, i_end; - for (int i = 0; i < numberOfPublicChannels; ++i) + for (i = publicChannels.begin(), i_end = publicChannels.end(); + i != i_end; ++i) { - std::string channel; - channels >> channel; - // Send only public channels - result.writeString(channel); - result.writeShort( - chatChannelManager->getNumberOfChannelUsers(channel)); + short users = chatChannelManager->getNumberOfChannelUsers(*i); + result.writeString(*i); + result.writeShort(users); } } break; @@ -678,7 +677,7 @@ void ChatHandler::sendGuildEnterChannel(const MessageOut &msg, const std::string (*i)->send(msg); break; } - } + } } void ChatHandler::sendGuildInvite(const std::string &invitedName, const std::string &inviterName, @@ -701,7 +700,7 @@ void ChatHandler::sendGuildRejoin(ChatClient &computer) { // Get character based on name. CharacterPtr character = serverHandler->getCharacter(computer.characterName); - + // Get list of guilds and check what rights they have. std::vector<std::string> guilds = character->getGuilds(); for(unsigned int i = 0; i != guilds.size(); ++i) diff --git a/src/chat-server/chathandler.hpp b/src/chat-server/chathandler.hpp index cda3f27e..19d2fc31 100644 --- a/src/chat-server/chathandler.hpp +++ b/src/chat-server/chathandler.hpp @@ -31,7 +31,11 @@ class ChatClient; /** - * Manages all chat related + * Manages chat related things like private messaging, chat channel handling + * as well as guild chat. The only form of chat not handled by this server is + * local chat, which is handled by the game server. + * + * TODO: Extend with handling of team chat once teams are implemented. */ class ChatHandler : public ConnectionHandler { @@ -49,9 +53,9 @@ class ChatHandler : public ConnectionHandler */ bool startListen(enet_uint16 port); - + /** - * Tell a list of user about an event in a chatchannel about a player. + * Tell a list of users about an event in a chatchannel about a player. */ void warnUsersAboutPlayerEventInChat(short channelId, std::string const &userName, @@ -61,12 +65,14 @@ class ChatHandler : public ConnectionHandler * Send Chat and Guild Info to chat client, so that they can * join the correct channels. */ - void sendGuildEnterChannel(const MessageOut &msg, const std::string &name); - + void sendGuildEnterChannel(const MessageOut &msg, + const std::string &name); + /** * Send guild invite. */ - void sendGuildInvite(const std::string &invitedName, const std::string &inviterName, + void sendGuildInvite(const std::string &invitedName, + const std::string &inviterName, const std::string &guildName); protected: @@ -76,7 +82,7 @@ class ChatHandler : public ConnectionHandler void processMessage(NetComputer *computer, MessageIn &message); NetComputer *computerConnected(ENetPeer *); void computerDisconnected(NetComputer *); - + /** * Send messages for each guild the character belongs to. */ @@ -120,12 +126,12 @@ class ChatHandler : public ConnectionHandler * still haven't sent in their magic token. */ void removeOutdatedPending(); - + /** * Send user joined message. */ void sendUserJoined(short channelId, const std::string &name); - + /** * Send user left message. */ |