00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <algorithm>
00023 #include <sstream>
00024
00025 #include "chat-server/chatchannel.hpp"
00026 #include "chat-server/chatclient.hpp"
00027
00028 ChatChannel::ChatChannel(int id,
00029 const std::string &name,
00030 const std::string &announcement,
00031 const std::string &password,
00032 bool joinable):
00033 mId(id),
00034 mName(name),
00035 mAnnouncement(announcement),
00036 mPassword(password),
00037 mJoinable(joinable)
00038 {
00039 }
00040
00041 bool ChatChannel::addUser(ChatClient *user)
00042 {
00043
00044 if (mRegisteredUsers.size() < 1)
00045 {
00046 mOwner = user->characterName;
00047 setUserMode(user, 'o');
00048 }
00049
00050
00051 ChannelUsers::const_iterator i = mRegisteredUsers.begin(),
00052 i_end = mRegisteredUsers.end();
00053 if (std::find(i, i_end, user) != i_end)
00054 return false;
00055
00056 mRegisteredUsers.push_back(user);
00057 user->channels.push_back(this);
00058
00059
00060 setUserMode(user, 'l');
00061
00062
00063 if (user->characterName == mOwner)
00064 {
00065 setUserMode(user, 'o');
00066 }
00067
00068 return true;
00069 }
00070
00071 bool ChatChannel::removeUser(ChatClient *user)
00072 {
00073 ChannelUsers::iterator i_end = mRegisteredUsers.end(),
00074 i = std::find(mRegisteredUsers.begin(), i_end, user);
00075 if (i == i_end) return false;
00076 mRegisteredUsers.erase(i);
00077 std::vector< ChatChannel * > &channels = user->channels;
00078 channels.erase(std::find(channels.begin(), channels.end(), this));
00079 std::map<ChatChannel*,std::string> &modes = user->userModes;
00080 modes.erase(modes.begin(), modes.end());
00081 return true;
00082 }
00083
00084 void ChatChannel::removeAllUsers()
00085 {
00086 for (ChannelUsers::const_iterator i = mRegisteredUsers.begin(),
00087 i_end = mRegisteredUsers.end(); i != i_end; ++i)
00088 {
00089 std::vector< ChatChannel * > &channels = (*i)->channels;
00090 channels.erase(std::find(channels.begin(), channels.end(), this));
00091 std::map<ChatChannel*,std::string> &modes = (*i)->userModes;
00092 modes.erase(modes.begin(), modes.end());
00093 }
00094 mRegisteredUsers.clear();
00095 }
00096
00097 bool ChatChannel::canJoin() const
00098 {
00099 return mJoinable;
00100 }
00101
00102 void ChatChannel::setUserMode(ChatClient *user, unsigned char mode)
00103 {
00104 std::map<ChatChannel*, std::string>::iterator itr = user->userModes.find(this);
00105 if (itr != user->userModes.end())
00106 {
00107 itr->second += mode;
00108 }
00109 else
00110 {
00111 std::stringstream ss; ss << mode;
00112 user->userModes.insert(std::pair<ChatChannel*, std::string>(this, ss.str()));
00113 }
00114 }
00115
00116 std::string ChatChannel::getUserMode(ChatClient *user)
00117 {
00118 std::map<ChatChannel*, std::string>::iterator itr = user->userModes.find(this);
00119 if (itr != user->userModes.end())
00120 {
00121 return itr->second;
00122 }
00123
00124 return 0;
00125 }