00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "net/ea/chathandler.h"
00023
00024 #include "net/ea/protocol.h"
00025
00026 #include "net/messagein.h"
00027 #include "net/messageout.h"
00028
00029 #include "being.h"
00030 #include "beingmanager.h"
00031 #include "game.h"
00032 #include "localplayer.h"
00033 #include "player_relations.h"
00034
00035 #include "gui/widgets/chattab.h"
00036
00037 #include "utils/gettext.h"
00038 #include "utils/stringutils.h"
00039 #include "utils/strprintf.h"
00040
00041 #include <string>
00042
00043 #define SERVER_NAME "Server"
00044
00045 Net::ChatHandler *chatHandler;
00046
00047 namespace EAthena {
00048
00049 ChatHandler::ChatHandler()
00050 {
00051 static const Uint16 _messages[] = {
00052 SMSG_BEING_CHAT,
00053 SMSG_PLAYER_CHAT,
00054 SMSG_WHISPER,
00055 SMSG_WHISPER_RESPONSE,
00056 SMSG_GM_CHAT,
00057 SMSG_MVP,
00058 0
00059 };
00060 handledMessages = _messages;
00061 chatHandler = this;
00062 }
00063
00064 void ChatHandler::handleMessage(MessageIn &msg)
00065 {
00066 Being *being;
00067 std::string chatMsg;
00068 std::string nick;
00069 int chatMsgLength;
00070
00071 switch (msg.getId())
00072 {
00073 case SMSG_WHISPER_RESPONSE:
00074 switch (msg.readInt8())
00075 {
00076 case 0x00:
00077
00078
00079 break;
00080 case 0x01:
00081 localChatTab->chatLog(_("Whisper could not be sent, user is offline"), BY_SERVER);
00082 break;
00083 case 0x02:
00084 localChatTab->chatLog(_("Whisper could not be sent, ignored by user"), BY_SERVER);
00085 break;
00086 }
00087 break;
00088
00089
00090 case SMSG_WHISPER:
00091 chatMsgLength = msg.readInt16() - 28;
00092 nick = msg.readString(24);
00093
00094 if (chatMsgLength <= 0)
00095 break;
00096
00097 chatMsg = msg.readString(chatMsgLength);
00098
00099 if (nick != SERVER_NAME)
00100 {
00101 if (player_relations.hasPermission(nick, PlayerRelation::WHISPER))
00102 chatWindow->whisper(nick, chatMsg);
00103 }
00104 else
00105 {
00106 localChatTab->chatLog(chatMsg, BY_SERVER);
00107 }
00108
00109 break;
00110
00111
00112 case SMSG_BEING_CHAT: {
00113 chatMsgLength = msg.readInt16() - 8;
00114 being = beingManager->findBeing(msg.readInt32());
00115
00116 if (!being || chatMsgLength <= 0)
00117 break;
00118
00119 chatMsg = msg.readString(chatMsgLength);
00120
00121 std::string::size_type pos = chatMsg.find(" : ", 0);
00122 std::string sender_name = ((pos == std::string::npos)
00123 ? ""
00124 : chatMsg.substr(0, pos));
00125
00126
00127
00128 if (player_relations.checkPermissionSilently(sender_name, PlayerRelation::SPEECH_LOG))
00129 localChatTab->chatLog(chatMsg, BY_OTHER);
00130
00131 chatMsg.erase(0, pos + 3);
00132 trim(chatMsg);
00133
00134 if (player_relations.hasPermission(sender_name, PlayerRelation::SPEECH_FLOAT))
00135 being->setSpeech(chatMsg, SPEECH_TIME);
00136 break;
00137 }
00138
00139 case SMSG_PLAYER_CHAT:
00140 case SMSG_GM_CHAT: {
00141 chatMsgLength = msg.readInt16() - 4;
00142
00143 if (chatMsgLength <= 0)
00144 break;
00145
00146 chatMsg = msg.readString(chatMsgLength);
00147 std::string::size_type pos = chatMsg.find(" : ", 0);
00148
00149 if (msg.getId() == SMSG_PLAYER_CHAT)
00150 {
00151 if (localChatTab) localChatTab->chatLog(chatMsg, BY_PLAYER);
00152
00153 if (pos != std::string::npos)
00154 chatMsg.erase(0, pos + 3);
00155
00156 trim(chatMsg);
00157
00158 player_node->setSpeech(chatMsg, SPEECH_TIME);
00159 }
00160 else
00161 {
00162 localChatTab->chatLog(chatMsg, BY_GM);
00163 }
00164 break;
00165 }
00166
00167 case SMSG_MVP:
00168
00169 msg.readInt32();
00170 localChatTab->chatLog("MVP player", BY_SERVER);
00171 break;
00172 }
00173 }
00174
00175 void ChatHandler::talk(const std::string &text)
00176 {
00177 std::string mes = player_node->getName() + " : " + text;
00178
00179 MessageOut outMsg(CMSG_CHAT_MESSAGE);
00180
00181 outMsg.writeInt16(mes.length() + 4 + 1);
00182 outMsg.writeString(mes, mes.length() + 1);
00183 }
00184
00185 void ChatHandler::me(const std::string &text)
00186 {
00187 std::string action = strprintf("*%s*", text.c_str());
00188
00189 talk(action);
00190 }
00191
00192 void ChatHandler::privateMessage(const std::string &recipient,
00193 const std::string &text)
00194 {
00195 MessageOut outMsg(CMSG_CHAT_WHISPER);
00196 outMsg.writeInt16(text.length() + 28);
00197 outMsg.writeString(recipient, 24);
00198 outMsg.writeString(text, text.length());
00199 }
00200
00201 void ChatHandler::channelList()
00202 {
00203 localChatTab->chatLog(_("Channels are not supported!"), BY_SERVER);
00204 }
00205
00206 void ChatHandler::enterChannel(const std::string &channel,
00207 const std::string &password)
00208 {
00209 localChatTab->chatLog(_("Channels are not supported!"), BY_SERVER);
00210 }
00211
00212 void ChatHandler::quitChannel(int channelId)
00213 {
00214 localChatTab->chatLog(_("Channels are not supported!"), BY_SERVER);
00215 }
00216
00217 void ChatHandler::sendToChannel(int channelId, const std::string &text)
00218 {
00219 localChatTab->chatLog(_("Channels are not supported!"), BY_SERVER);
00220 }
00221
00222 void ChatHandler::userList(const std::string &channel)
00223 {
00224 localChatTab->chatLog(_("Channels are not supported!"), BY_SERVER);
00225 }
00226
00227 void ChatHandler::setChannelTopic(int channelId, const std::string &text)
00228 {
00229 localChatTab->chatLog(_("Channels are not supported!"), BY_SERVER);
00230 }
00231
00232 void ChatHandler::setUserMode(int channelId, const std::string &name, int mode)
00233 {
00234 localChatTab->chatLog(_("Channels are not supported!"), BY_SERVER);
00235 }
00236
00237 void ChatHandler::kickUser(int channelId, const std::string &name)
00238 {
00239 localChatTab->chatLog(_("Channels are not supported!"), BY_SERVER);
00240 }
00241
00242 void ChatHandler::who()
00243 {
00244 MessageOut outMsg(CMSG_WHO_REQUEST);
00245 }
00246
00247 }