GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/mail2handler.cpp Lines: 1 143 0.7 %
Date: 2017-11-29 Branches: 0 190 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2017  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "net/eathena/mail2handler.h"
22
23
#include "const/net/inventory.h"
24
25
#include "being/localplayer.h"
26
27
#include "net/eathena/mail2recv.h"
28
#include "net/eathena/messageout.h"
29
#include "net/eathena/protocolout.h"
30
31
#include "utils/checkutils.h"
32
33
#include "resources/mailqueue.h"
34
35
#include "resources/item/item.h"
36
37
#include "debug.h"
38
39
extern int packetVersion;
40
extern int serverVersion;
41
42
namespace EAthena
43
{
44
45
Mail2Handler::Mail2Handler()
46
{
47
    mail2Handler = this;
48
}
49
50
Mail2Handler::~Mail2Handler()
51
{
52
    mail2Handler = nullptr;
53
    Mail2Recv::mCheckedName.clear();
54
    while (!Mail2Recv::mMailQueue.empty())
55
    {
56
        MailQueue *const mail = Mail2Recv::mMailQueue.front();
57
        delete mail;
58
        Mail2Recv::mMailQueue.pop();
59
    }
60
}
61
62
void Mail2Handler::openWriteMail(const std::string &receiver) const
63
{
64
    if (packetVersion < 20140416 ||
65
        (serverVersion < 19 && serverVersion != 0))
66
    {
67
        return;
68
    }
69
    createOutPacket(CMSG_MAIL2_OPEN_WRITE_MAIL);
70
    outMsg.writeString(receiver, 24, "receiver name");
71
}
72
73
void Mail2Handler::addItem(const Item *const item,
74
                           const int amount) const
75
{
76
    if (item == nullptr)
77
        return;
78
    if (packetVersion < 20140416 ||
79
        (serverVersion < 19 && serverVersion != 0))
80
    {
81
        return;
82
    }
83
84
    createOutPacket(CMSG_MAIL2_ADD_ITEM_TO_MAIL);
85
    outMsg.writeInt16(CAST_S16(
86
        item->getInvIndex() + INVENTORY_OFFSET), "index");
87
    outMsg.writeInt16(CAST_S16(amount), "amount");
88
}
89
90
void Mail2Handler::removeItem(const int index,
91
                              const int amount) const
92
{
93
    if (packetVersion < 20140416 ||
94
        (serverVersion < 19 && serverVersion != 0))
95
    {
96
        return;
97
    }
98
99
    createOutPacket(CMSG_MAIL2_REMOVE_ITEM_MAIL);
100
    outMsg.writeInt16(CAST_S16(index + INVENTORY_OFFSET), "index");
101
    outMsg.writeInt16(CAST_S16(amount), "amount");
102
}
103
104
void Mail2Handler::sendMail(const std::string &to,
105
                            const std::string &title,
106
                            const std::string &body,
107
                            const int64_t &money) const
108
{
109
    if (packetVersion < 20131230 ||
110
        (serverVersion < 19 && serverVersion != 0))
111
    {
112
        return;
113
    }
114
    if (localPlayer == nullptr)
115
        return;
116
117
    const std::string from = localPlayer->getName();
118
    const int titleSz = CAST_S32(title.size()) + 1;
119
    const int bodySz = CAST_S32(body.size()) + 1;
120
    int32_t sz = 2 + 2 + 24 + 24 + 8 + 2 + 2 + titleSz + bodySz;
121
    if (sz > 32767 - 4)
122
    {
123
        reportAlways("Mail message too big");
124
        return;
125
    }
126
    if (packetVersion >= 20160600)
127
        sz += 4;
128
129
    createOutPacket(CMSG_MAIL2_SEND_MAIL);
130
    outMsg.writeInt16(CAST_S16(sz), "len");
131
    outMsg.writeString(to, 24, "to");
132
    outMsg.writeString(from, 24, "from");
133
    outMsg.writeInt64(money, "money");
134
    outMsg.writeInt16(CAST_S16(titleSz), "title len");
135
    outMsg.writeInt16(CAST_S16(bodySz), "body len");
136
    if (packetVersion >= 20160600)
137
        outMsg.writeInt32(0, "to char id");
138
    outMsg.writeString(title, titleSz, "title");
139
    outMsg.writeString(body, bodySz, "body");
140
    Mail2Recv::mCheckedName.clear();
141
}
142
143
void Mail2Handler::queueCheckName(const MailQueueTypeT type,
144
                                  const std::string &to,
145
                                  const std::string &title,
146
                                  const std::string &body,
147
                                  const int64_t &money) const
148
{
149
    MailQueue *const mail = new MailQueue;
150
    mail->to = to;
151
    mail->title = title;
152
    mail->body = body;
153
    mail->money = money;
154
    mail->type = type;
155
    Mail2Recv::mMailQueue.push(mail);
156
    requestCheckName(to);
157
}
158
159
void Mail2Handler::nextPage(const MailOpenTypeT openType,
160
                            const int64_t mailId) const
161
{
162
    if (packetVersion < 20131218 ||
163
        (serverVersion < 19 && serverVersion != 0))
164
    {
165
        return;
166
    }
167
    createOutPacket(CMSG_MAIL2_NEXT_PAGE);
168
    outMsg.writeInt8(toInt(openType, int8_t), "open type");
169
    outMsg.writeInt64(mailId, "mail id");
170
}
171
172
void Mail2Handler::readMail(const MailOpenTypeT openType,
173
                            const int64_t mailId) const
174
{
175
    if (packetVersion < 20131223 ||
176
        (serverVersion < 19 && serverVersion != 0))
177
    {
178
        return;
179
    }
180
    createOutPacket(CMSG_MAIL2_READ_MAIL);
181
    outMsg.writeInt8(toInt(openType, int8_t), "open type");
182
    outMsg.writeInt64(mailId, "mail id");
183
}
184
185
void Mail2Handler::deleteMail(const MailOpenTypeT openType,
186
                              const int64_t mailId) const
187
{
188
    if (packetVersion < 20131218 ||
189
        (serverVersion < 19 && serverVersion != 0))
190
    {
191
        return;
192
    }
193
    createOutPacket(CMSG_MAIL2_DELETE_MAIL);
194
    outMsg.writeInt8(toInt(openType, int8_t), "open type");
195
    outMsg.writeInt64(mailId, "mail id");
196
}
197
198
void Mail2Handler::requestMoney(const MailOpenTypeT openType,
199
                                const int64_t mailId) const
200
{
201
    if (packetVersion < 20140326 ||
202
        (serverVersion < 19 && serverVersion != 0))
203
    {
204
        return;
205
    }
206
    createOutPacket(CMSG_MAIL2_REQUEST_MONEY);
207
    outMsg.writeInt64(mailId, "mail id");
208
    outMsg.writeInt8(toInt(openType, int8_t), "open type");
209
}
210
211
void Mail2Handler::requestItems(const MailOpenTypeT openType,
212
                                const int64_t mailId) const
213
{
214
    if (packetVersion < 20140326 ||
215
        (serverVersion < 19 && serverVersion != 0))
216
    {
217
        return;
218
    }
219
    createOutPacket(CMSG_MAIL2_REQUEST_ITEMS);
220
    outMsg.writeInt64(mailId, "mail id");
221
    outMsg.writeInt8(toInt(openType, int8_t), "open type");
222
}
223
224
void Mail2Handler::refreshMailList(const MailOpenTypeT openType,
225
                                   const int64_t mailId) const
226
{
227
    if (packetVersion < 20131218 ||
228
        (serverVersion < 19 && serverVersion != 0))
229
    {
230
        return;
231
    }
232
    createOutPacket(CMSG_MAIL2_REFRESH_MAIL_LIST);
233
    if (packetVersion >= 20170419)
234
    {
235
        outMsg.writeInt64(mailId, "mail id");
236
        outMsg.writeInt32(0, "unknown 1");
237
        outMsg.writeInt32(0, "unknown 2");
238
        outMsg.writeInt32(0, "unknown 3");
239
        outMsg.writeInt32(0, "unknown 4");
240
    }
241
    else
242
    {
243
        outMsg.writeInt8(toInt(openType, int8_t), "open type");
244
        outMsg.writeInt64(mailId, "mail id");
245
    }
246
}
247
248
void Mail2Handler::openMailBox(const MailOpenTypeT openType) const
249
{
250
    if (packetVersion < 20140212 ||
251
        (serverVersion < 19 && serverVersion != 0))
252
    {
253
        return;
254
    }
255
    createOutPacket(CMSG_MAIL2_OPEN_MAILBOX);
256
    if (packetVersion >= 20170419)
257
    {
258
        outMsg.writeInt64(0, "mail id");
259
        outMsg.writeInt32(0, "unknown 1");
260
        outMsg.writeInt32(0, "unknown 2");
261
        outMsg.writeInt32(0, "unknown 3");
262
        outMsg.writeInt32(0, "unknown 4");
263
    }
264
    else
265
    {
266
        outMsg.writeInt8(toInt(openType, int8_t), "open type");
267
        outMsg.writeInt64(0, "mail id");
268
    }
269
}
270
271
void Mail2Handler::closeMailBox() const
272
{
273
    if (packetVersion < 20131211 ||
274
        (serverVersion < 19 && serverVersion != 0))
275
    {
276
        return;
277
    }
278
    createOutPacket(CMSG_MAIL2_CLOSE_MAILBOX);
279
}
280
281
void Mail2Handler::cancelWriteMail() const
282
{
283
    if (packetVersion < 20140326 ||
284
        (serverVersion < 19 && serverVersion != 0))
285
    {
286
        return;
287
    }
288
    createOutPacket(CMSG_MAIL2_CANCEL_WRITE_MAIL);
289
}
290
291
void Mail2Handler::requestCheckName(const std::string &name) const
292
{
293
    if (packetVersion < 20140423 ||
294
        (serverVersion < 19 && serverVersion != 0))
295
    {
296
        return;
297
    }
298
    createOutPacket(CMSG_MAIL2_CHECK_NAME);
299
    outMsg.writeString(name, 24, "name");
300
}
301
302
std::string Mail2Handler::getCheckedName() const
303
{
304
    return Mail2Recv::mCheckedName;
305
}
306
307
4
}  // namespace EAthena