ManaPlus
chathandler.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "net/tmwa/chathandler.h"
25 
26 #include "being/localplayer.h"
27 
28 #include "const/gui/chat.h"
29 
30 #include "net/ea/chatrecv.h"
31 
32 #include "net/tmwa/messageout.h"
33 #include "net/tmwa/protocolout.h"
34 
35 #include "utils/gmfunctions.h"
36 #include "utils/stringutils.h"
37 
38 #include "debug.h"
39 
40 extern unsigned int tmwServerVersion;
41 
42 namespace TmwAthena
43 {
44 
46  Ea::ChatHandler()
47 {
48  chatHandler = this;
49 }
50 
52 {
53  chatHandler = nullptr;
54 }
55 
56 void ChatHandler::talk(const std::string &restrict text) const
57 {
58  if (localPlayer == nullptr)
59  return;
60 
61  if (tmwServerVersion >= 0x100408)
62  {
63  createOutPacket(CMSG_CHAT_MESSAGE);
64  // Added + 1 in order to let eAthena parse admin commands correctly
65  outMsg.writeInt16(CAST_S16(text.length() + 4 + 1), "len");
66  outMsg.writeString(text, CAST_S32(text.length() + 1), "message");
67  }
68  else
69  {
70  const std::string mes = std::string(localPlayer->getName()).append(
71  " : ").append(text);
72 
73  createOutPacket(CMSG_CHAT_MESSAGE);
74  // Added + 1 in order to let eAthena parse admin commands correctly
75  outMsg.writeInt16(CAST_S16(mes.length() + 4 + 1), "len");
76  outMsg.writeString(mes, CAST_S32(mes.length() + 1), "message");
77  }
78 }
79 
80 void ChatHandler::talkRaw(const std::string &mes) const
81 {
82  createOutPacket(CMSG_CHAT_MESSAGE);
83  outMsg.writeInt16(CAST_S16(mes.length() + 4), "len");
84  outMsg.writeString(mes, CAST_S32(mes.length()), "message");
85 }
86 
87 void ChatHandler::privateMessage(const std::string &restrict recipient,
88  const std::string &restrict text) const
89 {
90  createOutPacket(CMSG_CHAT_WHISPER);
91  outMsg.writeInt16(CAST_S16(text.length() + 28), "len");
92  outMsg.writeString(recipient, 24, "recipient nick");
93  outMsg.writeString(text, CAST_S32(text.length()), "message");
94  Ea::ChatRecv::mSentWhispers.push(recipient);
95 }
96 
97 void ChatHandler::channelMessage(const std::string &restrict channel,
98  const std::string &restrict text) const
99 {
100  if (channel == TRADE_CHANNEL)
101  talk("\302\202" + text);
102  else if (channel == GM_CHANNEL)
103  Gm::runCommand("wgm", text);
104 }
105 
106 void ChatHandler::who() const
107 {
108  if (tmwServerVersion >= 0x0e0b0b)
109  return;
110 
111  createOutPacket(CMSG_WHO_REQUEST);
112 }
113 
114 void ChatHandler::sendRaw(const std::string &args) const
115 {
116  std::string line = args;
117  std::string str;
118  MessageOut *outMsg = nullptr;
119 
120  if (line.empty())
121  return;
122 
123  size_t pos = line.find(' ');
124  if (pos != std::string::npos)
125  {
126  str = line.substr(0, pos);
127  const int16_t id = CAST_S16(parseNumber(str));
128  outMsg = new MessageOut(id);
129  outMsg->writeInt16(id, "packet id");
130  line = line.substr(pos + 1);
131  pos = line.find(' ');
132  }
133  else
134  {
135  const int16_t id = CAST_S16(parseNumber(line));
136  outMsg = new MessageOut(id);
137  outMsg->writeInt16(id, "packet id");
138  delete outMsg;
139  return;
140  }
141 
142  while (pos != std::string::npos)
143  {
144  str = line.substr(0, pos);
145  processRaw(*outMsg, str);
146  line = line.substr(pos + 1);
147  pos = line.find(' ');
148  }
149  if (!line.empty())
150  processRaw(*outMsg, line);
151  delete outMsg;
152 }
153 
155  const std::string &restrict line)
156 {
157  if (line.size() < 2)
158  return;
159 
160  const uint32_t i = parseNumber(line.substr(1));
161  switch (tolower(line[0]))
162  {
163  case 'b':
164  {
165  outMsg.writeInt8(CAST_U8(i), "raw");
166  break;
167  }
168  case 'w':
169  {
170  outMsg.writeInt16(CAST_S16(i), "raw");
171  break;
172  }
173  case 'l':
174  {
175  outMsg.writeInt32(CAST_S32(i), "raw");
176  break;
177  }
178  default:
179  break;
180  }
181 }
182 
184 {
185 }
186 
188 {
189 }
190 
191 void ChatHandler::ignore(const std::string &nick) const
192 {
193  createOutPacket(CMSG_IGNORE_NICK);
194  outMsg.writeString(nick, 24, "nick");
195  outMsg.writeInt8(0, "flag");
196 }
197 
198 void ChatHandler::unIgnore(const std::string &nick) const
199 {
200  createOutPacket(CMSG_IGNORE_NICK);
201  outMsg.writeString(nick, 24, "nick");
202  outMsg.writeInt8(1, "flag");
203 }
204 
206 {
207 }
208 
209 void ChatHandler::createChatRoom(const std::string &title A_UNUSED,
210  const std::string &password A_UNUSED,
211  const int limit A_UNUSED,
212  const bool isPublic A_UNUSED) const
213 {
214 }
215 
216 void ChatHandler::battleTalk(const std::string &text A_UNUSED) const
217 {
218 }
219 
221  const std::string &password A_UNUSED) const
222 {
223 }
224 
225 void ChatHandler::joinChannel(const std::string &channel A_UNUSED) const
226 {
227 }
228 
229 void ChatHandler::partChannel(const std::string &channel A_UNUSED) const
230 {
231 }
232 
233 void ChatHandler::talkPet(const std::string &restrict text) const
234 {
235  // here need string duplication
236  std::string action = strprintf("\302\202\303 %s", text.c_str());
237  talk(action);
238 }
239 
241 {
242 }
243 
245  const bool isPublict A_UNUSED,
246  const std::string &passwordt A_UNUSED,
247  const std::string &titlet A_UNUSED) const
248 {
249 }
250 
251 void ChatHandler::setChatRoomOwner(const std::string &nick A_UNUSED) const
252 {
253 }
254 
255 void ChatHandler::kickFromChatRoom(const std::string &nick A_UNUSED) const
256 {
257 }
258 
259 } // namespace TmwAthena
#define CAST_S16
Definition: cast.h:28
#define CAST_S32
Definition: cast.h:30
#define CAST_U8
Definition: cast.h:27
Net::ChatHandler * chatHandler
Definition: net.cpp:86
const std::string & getName() const
Definition: being.h:232
void writeInt16(const int16_t value, const char *const str)
Definition: messageout.cpp:71
void privateMessage(const std::string &recipient, const std::string &text) const
Definition: chathandler.cpp:87
void joinChat(const ChatObject *const chat, const std::string &password) const
void ignore(const std::string &nick) const
void talkPet(const std::string &text) const
void unIgnoreAll() const
void leaveChatRoom() const
void unIgnore(const std::string &nick) const
void talk(const std::string &text) const
Definition: chathandler.cpp:56
static void processRaw(MessageOut &outMsg, const std::string &line)
void createChatRoom(const std::string &title, const std::string &password, const int limit, const bool isPublic) const
void requestIgnoreList() const
void channelMessage(const std::string &channel, const std::string &text) const
Definition: chathandler.cpp:97
void setChatRoomOwner(const std::string &nick) const
void joinChannel(const std::string &channel) const
void talkRaw(const std::string &text) const
Definition: chathandler.cpp:80
void sendRaw(const std::string &args) const
void kickFromChatRoom(const std::string &nick) const
void partChannel(const std::string &channel) const
void battleTalk(const std::string &text) const
void setChatRoomOptions(const int limit, const bool isPublic, const std::string &password, const std::string &title) const
const std::string TRADE_CHANNEL
Definition: chat.h:31
const std::string GM_CHANNEL
Definition: chat.h:30
#define restrict
Definition: localconsts.h:165
#define A_UNUSED
Definition: localconsts.h:160
LocalPlayer * localPlayer
#define createOutPacket(name)
Definition: messageout.h:37
WhisperQueue mSentWhispers
Definition: chatrecv.cpp:49
void runCommand(const std::string &command, const std::string &params)
Definition: gmfunctions.cpp:35
std::string strprintf(const char *const format,...)
uint32_t parseNumber(const std::string &str)
unsigned int tmwServerVersion
Definition: client.cpp:134