GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/tmwa/chathandler.cpp Lines: 1 98 1.0 %
Date: 2018-05-19 03:07:18 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-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/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) const
56
{
57
    if (localPlayer == nullptr)
58
        return;
59
60
    if (tmwServerVersion >= 0x100408)
61
    {
62
        createOutPacket(CMSG_CHAT_MESSAGE);
63
        // Added + 1 in order to let eAthena parse admin commands correctly
64
        outMsg.writeInt16(CAST_S16(text.length() + 4 + 1), "len");
65
        outMsg.writeString(text, CAST_S32(text.length() + 1), "message");
66
    }
67
    else
68
    {
69
        const std::string mes = std::string(localPlayer->getName()).append(
70
            " : ").append(text);
71
72
        createOutPacket(CMSG_CHAT_MESSAGE);
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
    outMsg.writeInt16(CAST_S16(text.length() + 28), "len");
91
    outMsg.writeString(recipient, 24, "recipient nick");
92
    outMsg.writeString(text, CAST_S32(text.length()), "message");
93
    Ea::ChatRecv::mSentWhispers.push(recipient);
94
}
95
96
void ChatHandler::channelMessage(const std::string &restrict channel,
97
                                 const std::string &restrict text) const
98
{
99
    if (channel == TRADE_CHANNEL)
100
        talk("\302\202" + text);
101
    else if (channel == GM_CHANNEL)
102
        Gm::runCommand("wgm", text);
103
}
104
105
void ChatHandler::who() const
106
{
107
    if (tmwServerVersion >= 0x0e0b0b)
108
        return;
109
110
    createOutPacket(CMSG_WHO_REQUEST);
111
}
112
113
void ChatHandler::sendRaw(const std::string &args) const
114
{
115
    std::string line = args;
116
    std::string str;
117
    MessageOut *outMsg = nullptr;
118
119
    if (line.empty())
120
        return;
121
122
    size_t pos = line.find(' ');
123
    if (pos != std::string::npos)
124
    {
125
        str = line.substr(0, pos);
126
        const int16_t id = CAST_S16(parseNumber(str));
127
        outMsg = new MessageOut(id);
128
        outMsg->writeInt16(id, "packet id");
129
        line = line.substr(pos + 1);
130
        pos = line.find(' ');
131
    }
132
    else
133
    {
134
        const int16_t id = CAST_S16(parseNumber(line));
135
        outMsg = new MessageOut(id);
136
        outMsg->writeInt16(id, "packet id");
137
        delete outMsg;
138
        return;
139
    }
140
141
    while (pos != std::string::npos)
142
    {
143
        str = line.substr(0, pos);
144
        processRaw(*outMsg, str);
145
        line = line.substr(pos + 1);
146
        pos = line.find(' ');
147
    }
148
    if (!line.empty())
149
        processRaw(*outMsg, line);
150
    delete outMsg;
151
}
152
153
void ChatHandler::processRaw(MessageOut &restrict outMsg,
154
                             const std::string &restrict line)
155
{
156
    if (line.size() < 2)
157
        return;
158
159
    const uint32_t i = parseNumber(line.substr(1));
160
    switch (tolower(line[0]))
161
    {
162
        case 'b':
163
        {
164
            outMsg.writeInt8(CAST_U8(i), "raw");
165
            break;
166
        }
167
        case 'w':
168
        {
169
            outMsg.writeInt16(CAST_S16(i), "raw");
170
            break;
171
        }
172
        case 'l':
173
        {
174
            outMsg.writeInt32(CAST_S32(i), "raw");
175
            break;
176
        }
177
        default:
178
            break;
179
    }
180
}
181
182
void ChatHandler::ignoreAll() const
183
{
184
}
185
186
void ChatHandler::unIgnoreAll() const
187
{
188
}
189
190
void ChatHandler::ignore(const std::string &nick) const
191
{
192
    createOutPacket(CMSG_IGNORE_NICK);
193
    outMsg.writeString(nick, 24, "nick");
194
    outMsg.writeInt8(0, "flag");
195
}
196
197
void ChatHandler::unIgnore(const std::string &nick) const
198
{
199
    createOutPacket(CMSG_IGNORE_NICK);
200
    outMsg.writeString(nick, 24, "nick");
201
    outMsg.writeInt8(1, "flag");
202
}
203
204
void ChatHandler::requestIgnoreList() const
205
{
206
}
207
208
void ChatHandler::createChatRoom(const std::string &title A_UNUSED,
209
                                 const std::string &password A_UNUSED,
210
                                 const int limit A_UNUSED,
211
                                 const bool isPublic A_UNUSED) const
212
{
213
}
214
215
void ChatHandler::battleTalk(const std::string &text A_UNUSED) const
216
{
217
}
218
219
void ChatHandler::joinChat(const ChatObject *const chat A_UNUSED,
220
                           const std::string &password A_UNUSED) const
221
{
222
}
223
224
void ChatHandler::joinChannel(const std::string &channel A_UNUSED) const
225
{
226
}
227
228
void ChatHandler::partChannel(const std::string &channel A_UNUSED) const
229
{
230
}
231
232
void ChatHandler::talkPet(const std::string &restrict text) const
233
{
234
    // here need string duplication
235
    std::string action = strprintf("\302\202\303 %s", text.c_str());
236
    talk(action);
237
}
238
239
void ChatHandler::leaveChatRoom() const
240
{
241
}
242
243
void ChatHandler::setChatRoomOptions(const int limit A_UNUSED,
244
                                     const bool isPublict A_UNUSED,
245
                                     const std::string &passwordt A_UNUSED,
246
                                     const std::string &titlet A_UNUSED) const
247
{
248
}
249
250
void ChatHandler::setChatRoomOwner(const std::string &nick A_UNUSED) const
251
{
252
}
253
254
void ChatHandler::kickFromChatRoom(const std::string &nick A_UNUSED) const
255
{
256
}
257
258

6
}  // namespace TmwAthena