GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/chathandler.cpp Lines: 1 153 0.7 %
Date: 2018-05-19 03:07:18 Branches: 0 208 0.0 %

Line Branch Exec Source
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-2018  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "net/eathena/chathandler.h"
24
25
#include "being/localplayer.h"
26
27
#include "net/serverfeatures.h"
28
29
#include "net/ea/chatrecv.h"
30
31
#include "net/eathena/chatrecv.h"
32
#include "net/eathena/messageout.h"
33
#include "net/eathena/protocolout.h"
34
35
#include "resources/chatobject.h"
36
37
#include "utils/stringutils.h"
38
39
#include "debug.h"
40
41
extern int packetVersion;
42
43
namespace EAthena
44
{
45
46
ChatHandler::ChatHandler() :
47
    Ea::ChatHandler()
48
{
49
    chatHandler = this;
50
}
51
52
ChatHandler::~ChatHandler()
53
{
54
    chatHandler = nullptr;
55
}
56
57
void ChatHandler::talk(const std::string &restrict text) const
58
{
59
    if (localPlayer == nullptr)
60
        return;
61
62
    const std::string mes = std::string(localPlayer->getName()).append(
63
        " : ").append(text);
64
65
    createOutPacket(CMSG_CHAT_MESSAGE);
66
    if (packetVersion >= 20151001)
67
    {
68
        outMsg.writeInt16(CAST_S16(mes.length() + 4), "len");
69
        outMsg.writeString(mes, CAST_S32(mes.length()), "message");
70
    }
71
    else
72
    {
73
        // Added + 1 in order to let eAthena parse admin commands correctly
74
        outMsg.writeInt16(CAST_S16(mes.length() + 4 + 1), "len");
75
        outMsg.writeString(mes, CAST_S32(mes.length() + 1), "message");
76
    }
77
}
78
79
void ChatHandler::talkRaw(const std::string &mes) const
80
{
81
    createOutPacket(CMSG_CHAT_MESSAGE);
82
    outMsg.writeInt16(CAST_S16(mes.length() + 4), "len");
83
    outMsg.writeString(mes, CAST_S32(mes.length()), "message");
84
}
85
86
void ChatHandler::privateMessage(const std::string &restrict recipient,
87
                                 const std::string &restrict text) const
88
{
89
    createOutPacket(CMSG_CHAT_WHISPER);
90
    if (packetVersion >= 20151001)
91
    {
92
        outMsg.writeInt16(CAST_S16(text.length() + 28), "len");
93
        outMsg.writeString(recipient, 24, "recipient nick");
94
        outMsg.writeString(text, CAST_S32(text.length()), "message");
95
    }
96
    else
97
    {
98
        outMsg.writeInt16(CAST_S16(text.length() + 28 + 1), "len");
99
        outMsg.writeString(recipient, 24, "recipient nick");
100
        outMsg.writeString(text, CAST_S32(text.length()), "message");
101
        outMsg.writeInt8(0, "null char");
102
    }
103
    Ea::ChatRecv::mSentWhispers.push(recipient);
104
}
105
106
void ChatHandler::channelMessage(const std::string &restrict channel,
107
                                 const std::string &restrict text) const
108
{
109
    privateMessage(channel, text);
110
}
111
112
void ChatHandler::who() const
113
{
114
    createOutPacket(CMSG_WHO_REQUEST);
115
}
116
117
void ChatHandler::sendRaw(const std::string &args) const
118
{
119
    std::string line = args;
120
    std::string str;
121
    MessageOut *outMsg = nullptr;
122
123
    if (line.empty())
124
        return;
125
126
    size_t pos = line.find(' ');
127
    if (pos != std::string::npos)
128
    {
129
        str = line.substr(0, pos);
130
131
        const int16_t id = CAST_S16(parseNumber(str));
132
        outMsg = new MessageOut(id);
133
        outMsg->writeInt16(id, "packet id");
134
        line = line.substr(pos + 1);
135
        pos = line.find(' ');
136
    }
137
    else
138
    {
139
        const int16_t id = CAST_S16(parseNumber(line));
140
        outMsg = new MessageOut(id);
141
        outMsg->writeInt16(id, "packet id");
142
        delete outMsg;
143
        return;
144
    }
145
146
    while (pos != std::string::npos)
147
    {
148
        str = line.substr(0, pos);
149
        processRaw(*outMsg, str);
150
        line = line.substr(pos + 1);
151
        pos = line.find(' ');
152
    }
153
    if (!line.empty())
154
        processRaw(*outMsg, line);
155
    delete outMsg;
156
}
157
158
void ChatHandler::processRaw(MessageOut &restrict outMsg,
159
                             const std::string &restrict line)
160
{
161
    if (line.size() < 2)
162
        return;
163
164
    const uint32_t i = parseNumber(line.substr(1));
165
    switch (tolower(line[0]))
166
    {
167
        case 'b':
168
        {
169
            outMsg.writeInt8(CAST_U8(i), "raw");
170
            break;
171
        }
172
        case 'w':
173
        {
174
            outMsg.writeInt16(CAST_S16(i), "raw");
175
            break;
176
        }
177
        case 'l':
178
        {
179
            outMsg.writeInt32(CAST_S32(i), "raw");
180
            break;
181
        }
182
        default:
183
            break;
184
    }
185
}
186
187
void ChatHandler::ignoreAll() const
188
{
189
    createOutPacket(CMSG_IGNORE_ALL);
190
    outMsg.writeInt8(0, "flag");
191
}
192
193
void ChatHandler::unIgnoreAll() const
194
{
195
    createOutPacket(CMSG_IGNORE_ALL);
196
    outMsg.writeInt8(1, "flag");
197
}
198
199
200
void ChatHandler::ignore(const std::string &nick) const
201
{
202
    createOutPacket(CMSG_IGNORE_NICK);
203
    outMsg.writeString(nick, 24, "nick");
204
    outMsg.writeInt8(0, "flag");
205
}
206
207
void ChatHandler::unIgnore(const std::string &nick) const
208
{
209
    createOutPacket(CMSG_IGNORE_NICK);
210
    outMsg.writeString(nick, 24, "nick");
211
    outMsg.writeInt8(1, "flag");
212
}
213
214
void ChatHandler::requestIgnoreList() const
215
{
216
    createOutPacket(CMSG_REQUEST_IGNORE_LIST);
217
}
218
219
void ChatHandler::createChatRoom(const std::string &title,
220
                                 const std::string &password,
221
                                 const int limit,
222
                                 const bool isPublic) const
223
{
224
    createOutPacket(CMSG_CREAYE_CHAT_ROOM);
225
    outMsg.writeInt16(CAST_S16(
226
        7 + 8 + 36), "len");
227
    outMsg.writeInt16(CAST_S16(limit), "limit");
228
    outMsg.writeInt8(CAST_S8(isPublic ? 1 : 0), "public");
229
    outMsg.writeString(password, 8, "password");
230
    outMsg.writeString(title, 36, "title");
231
    ChatRecv::mChatRoom = title;
232
}
233
234
void ChatHandler::battleTalk(const std::string &text) const
235
{
236
    if (localPlayer == nullptr)
237
        return;
238
239
    const std::string mes = std::string(localPlayer->getName()).append(
240
        " : ").append(text);
241
242
    createOutPacket(CMSG_BATTLE_CHAT_MESSAGE);
243
    if (packetVersion >= 20151001)
244
    {
245
        outMsg.writeInt16(CAST_S16(mes.length() + 4), "len");
246
        outMsg.writeString(mes, CAST_S32(mes.length()), "message");
247
    }
248
    else
249
    {
250
        // Added + 1 in order to let eAthena parse admin commands correctly
251
        outMsg.writeInt16(CAST_S16(mes.length() + 4 + 1), "len");
252
        outMsg.writeString(mes, CAST_S32(mes.length() + 1), "message");
253
    }
254
}
255
256
void ChatHandler::joinChat(const ChatObject *const chat,
257
                           const std::string &password) const
258
{
259
    if (chat == nullptr)
260
        return;
261
262
    createOutPacket(CMSG_CHAT_ROOM_JOIN);
263
    outMsg.writeInt32(chat->chatId, "chat id");
264
    outMsg.writeString(password, 8, "password");
265
}
266
267
void ChatHandler::joinChannel(const std::string &channel) const
268
{
269
    if (serverFeatures->haveJoinChannel())
270
    {
271
        createOutPacket(CMSG_CHAT_JOIN_CHANNEL);
272
        outMsg.writeString(channel, 24, "channel name");
273
    }
274
    else
275
    {
276
        channelMessage(channel, "\302\202\302");
277
    }
278
}
279
280
void ChatHandler::partChannel(const std::string &channel) const
281
{
282
    if (serverFeatures->haveJoinChannel())
283
    {
284
        createOutPacket(CMSG_CHAT_PART_CHANNEL);
285
        outMsg.writeString(channel, 24, "channel name");
286
    }
287
}
288
289
void ChatHandler::talkPet(const std::string &restrict text) const
290
{
291
    if (text.empty())
292
        return;
293
    std::string msg = text;
294
    if (msg.size() > 500)
295
        msg = msg.substr(0, 500);
296
    const size_t sz = msg.size();
297
298
    createOutPacket(CMSG_PET_TALK);
299
    outMsg.writeInt16(CAST_S16(sz + 4 + 1), "len");
300
    outMsg.writeString(msg, CAST_S32(sz), "message");
301
    outMsg.writeInt8(0, "zero byte");
302
}
303
304
void ChatHandler::leaveChatRoom() const
305
{
306
    createOutPacket(CMSG_LEAVE_CHAT_ROOM);
307
}
308
309
void ChatHandler::setChatRoomOptions(const int limit,
310
                                     const bool isPublic,
311
                                     const std::string &password,
312
                                     const std::string &title) const
313
{
314
    createOutPacket(CMSG_SET_CHAT_ROOM_OPTIONS);
315
    const int sz = CAST_S32(title.size());
316
    outMsg.writeInt16(CAST_S16(15 + sz), "len");
317
    outMsg.writeInt16(CAST_S16(limit), "limit");
318
    outMsg.writeInt8(CAST_S8(isPublic ? 1 : 0), "type");
319
    outMsg.writeString(password, 8, "password");
320
    outMsg.writeString(title, sz, "title");
321
}
322
323
void ChatHandler::setChatRoomOwner(const std::string &nick) const
324
{
325
    createOutPacket(CMSG_SET_CHAT_ROOM_OWNER);
326
    outMsg.writeInt32(0, "role (unused)");
327
    outMsg.writeString(nick, 24, "nick");
328
}
329
330
void ChatHandler::kickFromChatRoom(const std::string &nick) const
331
{
332
    createOutPacket(CMSG_KICK_FROM_CHAT_ROOM);
333
    outMsg.writeString(nick, 24, "nick");
334
}
335
336
4
}  // namespace EAthena