GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/tmwa/chathandler.cpp Lines: 1 98 1.0 %
Date: 2017-11-29 Branches: 2 106 1.9 %

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-2017  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/tmwa/chathandler.h"
24
25
#include "being/localplayer.h"
26
27
#include "const/gui/chat.h"
28
29
#include "net/ea/chatrecv.h"
30
31
#include "net/tmwa/messageout.h"
32
#include "net/tmwa/protocolout.h"
33
34
#include "utils/gmfunctions.h"
35
#include "utils/stringutils.h"
36
37
#include "debug.h"
38
39
extern unsigned int tmwServerVersion;
40
41
namespace TmwAthena
42
{
43
44
ChatHandler::ChatHandler() :
45
    Ea::ChatHandler()
46
{
47
    chatHandler = this;
48
}
49
50
ChatHandler::~ChatHandler()
51
{
52
    chatHandler = nullptr;
53
}
54
55
void ChatHandler::talk(const std::string &restrict text,
56
                       const std::string &restrict channel A_UNUSED) 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, GENERAL_CHANNEL);
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
154
void ChatHandler::processRaw(MessageOut &restrict outMsg,
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
183
void ChatHandler::ignoreAll() const
184
{
185
}
186
187
void ChatHandler::unIgnoreAll() const
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
205
void ChatHandler::requestIgnoreList() const
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
220
void ChatHandler::joinChat(const ChatObject *const chat A_UNUSED,
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,
234
                          const std::string &restrict channel) const
235
{
236
    // here need string duplication
237
    std::string action = strprintf("\302\202\303 %s", text.c_str());
238
    talk(action, channel);
239
}
240
241
void ChatHandler::leaveChatRoom() const
242
{
243
}
244
245
void ChatHandler::setChatRoomOptions(const int limit A_UNUSED,
246
                                     const bool isPublict A_UNUSED,
247
                                     const std::string &passwordt A_UNUSED,
248
                                     const std::string &titlet A_UNUSED) const
249
{
250
}
251
252
void ChatHandler::setChatRoomOwner(const std::string &nick A_UNUSED) const
253
{
254
}
255
256
void ChatHandler::kickFromChatRoom(const std::string &nick A_UNUSED) const
257
{
258
}
259
260

6
}  // namespace TmwAthena