00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "net/tmwserv/chathandler.h"
00023
00024 #include "net/tmwserv/connection.h"
00025 #include "net/tmwserv/protocol.h"
00026
00027 #include "net/tmwserv/chatserver/chatserver.h"
00028 #include "net/tmwserv/chatserver/internal.h"
00029
00030 #include "net/tmwserv/gameserver/internal.h"
00031 #include "net/tmwserv/gameserver/player.h"
00032
00033 #include "net/messagein.h"
00034 #include "net/messageout.h"
00035
00036 #include "being.h"
00037 #include "beingmanager.h"
00038 #include "game.h"
00039 #include "channel.h"
00040 #include "channelmanager.h"
00041
00042 #include "gui/widgets/channeltab.h"
00043 #include "gui/chat.h"
00044 #include "gui/guildwindow.h"
00045
00046 #include <string>
00047 #include <iostream>
00048
00049 #include "utils/gettext.h"
00050
00051 extern Being *player_node;
00052
00053 Net::ChatHandler *chatHandler;
00054
00055 namespace TmwServ {
00056
00057 ChatHandler::ChatHandler()
00058 {
00059 static const Uint16 _messages[] = {
00060 GPMSG_SAY,
00061 CPMSG_ENTER_CHANNEL_RESPONSE,
00062 CPMSG_LIST_CHANNELS_RESPONSE,
00063 CPMSG_PUBMSG,
00064 CPMSG_ANNOUNCEMENT,
00065 CPMSG_PRIVMSG,
00066 CPMSG_QUIT_CHANNEL_RESPONSE,
00067 CPMSG_LIST_CHANNELUSERS_RESPONSE,
00068 CPMSG_CHANNEL_EVENT,
00069 CPMSG_WHO_RESPONSE,
00070 0
00071 };
00072 handledMessages = _messages;
00073 chatHandler = this;
00074 }
00075
00076 void ChatHandler::handleMessage(MessageIn &msg)
00077 {
00078 switch (msg.getId())
00079 {
00080 case GPMSG_SAY:
00081 handleGameChatMessage(msg);
00082 break;
00083
00084 case CPMSG_ENTER_CHANNEL_RESPONSE:
00085 handleEnterChannelResponse(msg);
00086 break;
00087
00088 case CPMSG_LIST_CHANNELS_RESPONSE:
00089 handleListChannelsResponse(msg);
00090 break;
00091
00092 case CPMSG_PRIVMSG:
00093 handlePrivateMessage(msg);
00094 break;
00095
00096 case CPMSG_ANNOUNCEMENT:
00097 handleAnnouncement(msg);
00098 break;
00099
00100 case CPMSG_PUBMSG:
00101 handleChatMessage(msg);
00102 break;
00103
00104 case CPMSG_QUIT_CHANNEL_RESPONSE:
00105 handleQuitChannelResponse(msg);
00106 break;
00107
00108 case CPMSG_LIST_CHANNELUSERS_RESPONSE:
00109 handleListChannelUsersResponse(msg);
00110 break;
00111
00112 case CPMSG_CHANNEL_EVENT:
00113 handleChannelEvent(msg);
00114 break;
00115
00116 case CPMSG_WHO_RESPONSE:
00117 handleWhoResponse(msg);
00118 break;
00119 }
00120 }
00121
00122 void ChatHandler::handleGameChatMessage(MessageIn &msg)
00123 {
00124 short id = msg.readInt16();
00125 std::string chatMsg = msg.readString();
00126
00127 if (id == 0)
00128 {
00129 localChatTab->chatLog(chatMsg, BY_SERVER);
00130 return;
00131 }
00132
00133 Being *being = beingManager->findBeing(id);
00134
00135 std::string mes;
00136 if (being)
00137 {
00138 mes = being->getName() + " : " + chatMsg;
00139 being->setSpeech(chatMsg, SPEECH_TIME);
00140 }
00141 else
00142 mes = "Unknown : " + chatMsg;
00143
00144 localChatTab->chatLog(mes, being == player_node ? BY_PLAYER : BY_OTHER);
00145 }
00146
00147 void ChatHandler::handleEnterChannelResponse(MessageIn &msg)
00148 {
00149 if(msg.readInt8() == ERRMSG_OK)
00150 {
00151 short channelId = msg.readInt16();
00152 std::string channelName = msg.readString();
00153 std::string announcement = msg.readString();
00154 Channel *channel = new Channel(channelId, channelName, announcement);
00155 channelManager->addChannel(channel);
00156 ChatTab *tab = channel->getTab();
00157 tab->chatLog(_("Topic: ") + announcement, BY_CHANNEL);
00158
00159 std::string user;
00160 std::string userModes;
00161 tab->chatLog("Players in this channel:", BY_CHANNEL);
00162 while(msg.getUnreadLength())
00163 {
00164 user = msg.readString();
00165 if (user == "")
00166 return;
00167 userModes = msg.readString();
00168 if (userModes.find('o') != std::string::npos)
00169 {
00170 user = "@" + user;
00171 }
00172 tab->chatLog(user, BY_CHANNEL);
00173 }
00174
00175 }
00176 else
00177 {
00178 localChatTab->chatLog("Error joining channel", BY_SERVER);
00179 }
00180 }
00181
00182 void ChatHandler::handleListChannelsResponse(MessageIn &msg)
00183 {
00184 localChatTab->chatLog("Listing Channels", BY_SERVER);
00185 while(msg.getUnreadLength())
00186 {
00187 std::string channelName = msg.readString();
00188 if (channelName == "")
00189 return;
00190 std::ostringstream numUsers;
00191 numUsers << msg.readInt16();
00192 channelName += " - ";
00193 channelName += numUsers.str();
00194 localChatTab->chatLog(channelName, BY_SERVER);
00195 }
00196 localChatTab->chatLog("End of channel list", BY_SERVER);
00197 }
00198
00199 void ChatHandler::handlePrivateMessage(MessageIn &msg)
00200 {
00201 std::string userNick = msg.readString();
00202 std::string chatMsg = msg.readString();
00203
00204 chatWindow->whisper(userNick, chatMsg);
00205 }
00206
00207 void ChatHandler::handleAnnouncement(MessageIn &msg)
00208 {
00209 std::string chatMsg = msg.readString();
00210 localChatTab->chatLog(chatMsg, BY_GM);
00211 }
00212
00213 void ChatHandler::handleChatMessage(MessageIn &msg)
00214 {
00215 short channelId = msg.readInt16();
00216 std::string userNick = msg.readString();
00217 std::string chatMsg = msg.readString();
00218
00219 Channel *channel = channelManager->findById(channelId);
00220 channel->getTab()->chatLog(userNick, chatMsg);
00221 }
00222
00223 void ChatHandler::handleQuitChannelResponse(MessageIn &msg)
00224 {
00225 if(msg.readInt8() == ERRMSG_OK)
00226 {
00227 short channelId = msg.readInt16();
00228 Channel *channel = channelManager->findById(channelId);
00229 channelManager->removeChannel(channel);
00230 }
00231 }
00232
00233 void ChatHandler::handleListChannelUsersResponse(MessageIn &msg)
00234 {
00235 std::string channelName = msg.readString();
00236 std::string userNick;
00237 std::string userModes;
00238 Channel *channel = channelManager->findByName(channelName);
00239 channel->getTab()->chatLog("Players in this channel:", BY_CHANNEL);
00240 while(msg.getUnreadLength())
00241 {
00242 userNick = msg.readString();
00243 if (userNick == "")
00244 {
00245 break;
00246 }
00247 userModes = msg.readString();
00248 if (userModes.find('o') != std::string::npos)
00249 {
00250 userNick = "@" + userNick;
00251 }
00252 localChatTab->chatLog(userNick, BY_CHANNEL, channel);
00253 }
00254 }
00255
00256 void ChatHandler::handleChannelEvent(MessageIn &msg)
00257 {
00258 short channelId = msg.readInt16();
00259 char eventId = msg.readInt8();
00260 std::string line = msg.readString();
00261 Channel *channel = channelManager->findById(channelId);
00262
00263 if(channel)
00264 {
00265 switch(eventId)
00266 {
00267 case CHAT_EVENT_NEW_PLAYER:
00268 line += " entered the channel.";
00269 break;
00270
00271 case CHAT_EVENT_LEAVING_PLAYER:
00272 line += " left the channel.";
00273 break;
00274
00275 case CHAT_EVENT_TOPIC_CHANGE:
00276 line = "Topic: " + line;
00277 break;
00278
00279 case CHAT_EVENT_MODE_CHANGE:
00280 {
00281 int first = line.find(":");
00282 int second = line.find(":", first+1);
00283 std::string user1 = line.substr(0, first);
00284 std::string user2 = line.substr(first+1, second);
00285 std::string mode = line.substr(second+1, line.length());
00286 line = user1 + " has set mode " + mode + " on user " + user2;
00287 } break;
00288
00289 case CHAT_EVENT_KICKED_PLAYER:
00290 {
00291 int first = line.find(":");
00292 std::string user1 = line.substr(0, first);
00293 std::string user2 = line.substr(first+1, line.length());
00294 line = user1 + " has kicked " + user2;
00295 } break;
00296
00297 default:
00298 line = "Unknown channel event.";
00299 }
00300
00301 channel->getTab()->chatLog(line, BY_CHANNEL);
00302 }
00303 }
00304
00305 void ChatHandler::handleWhoResponse(MessageIn &msg)
00306 {
00307 std::string userNick;
00308
00309 while(msg.getUnreadLength())
00310 {
00311 userNick = msg.readString();
00312 if (userNick == "")
00313 {
00314 break;
00315 }
00316 localChatTab->chatLog(userNick, BY_SERVER);
00317 }
00318 }
00319
00320 void ChatHandler::talk(const std::string &text)
00321 {
00322 MessageOut msg(PGMSG_SAY);
00323 msg.writeString(text);
00324 Net::GameServer::connection->send(msg);
00325 }
00326
00327 void ChatHandler::me(const std::string &text)
00328 {
00329
00330 }
00331
00332 void ChatHandler::privateMessage(const std::string &recipient,
00333 const std::string &text)
00334 {
00335 MessageOut msg(PCMSG_PRIVMSG);
00336 msg.writeString(recipient);
00337 msg.writeString(text);
00338 Net::ChatServer::connection->send(msg);
00339 }
00340
00341 void ChatHandler::channelList()
00342 {
00343 MessageOut msg(PCMSG_LIST_CHANNELS);
00344 Net::ChatServer::connection->send(msg);
00345 }
00346
00347 void ChatHandler::enterChannel(const std::string &channel,
00348 const std::string &password)
00349 {
00350 MessageOut msg(PCMSG_ENTER_CHANNEL);
00351 msg.writeString(channel);
00352 msg.writeString(password);
00353 Net::ChatServer::connection->send(msg);
00354 }
00355
00356 void ChatHandler::quitChannel(int channelId)
00357 {
00358 MessageOut msg(PCMSG_QUIT_CHANNEL);
00359 msg.writeInt16(channelId);
00360 Net::ChatServer::connection->send(msg);
00361 }
00362
00363 void ChatHandler::sendToChannel(int channelId, const std::string &text)
00364 {
00365 MessageOut msg(PCMSG_CHAT);
00366 msg.writeString(text);
00367 msg.writeInt16(channelId);
00368 Net::ChatServer::connection->send(msg);
00369 }
00370
00371 void ChatHandler::userList(const std::string &channel)
00372 {
00373 MessageOut msg(PCMSG_LIST_CHANNELUSERS);
00374 msg.writeString(channel);
00375 Net::ChatServer::connection->send(msg);
00376 }
00377
00378 void ChatHandler::setChannelTopic(int channelId, const std::string &text)
00379 {
00380 MessageOut msg(PCMSG_TOPIC_CHANGE);
00381 msg.writeInt16(channelId);
00382 msg.writeString(text);
00383 Net::ChatServer::connection->send(msg);
00384 }
00385
00386 void ChatHandler::setUserMode(int channelId, const std::string &name, int mode)
00387 {
00388 MessageOut msg(PCMSG_USER_MODE);
00389 msg.writeInt16(channelId);
00390 msg.writeString(name);
00391 msg.writeInt8(mode);
00392 Net::ChatServer::connection->send(msg);
00393 }
00394
00395 void ChatHandler::kickUser(int channelId, const std::string &name)
00396 {
00397 MessageOut msg(PCMSG_KICK_USER);
00398 msg.writeInt16(channelId);
00399 msg.writeString(name);
00400 Net::ChatServer::connection->send(msg);
00401 }
00402
00403 void ChatHandler::who()
00404 {
00405 MessageOut msg(PCMSG_WHO);
00406 Net::ChatServer::connection->send(msg);
00407 }
00408
00409 }