GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/mail2handler.cpp Lines: 1 127 0.8 %
Date: 2021-03-17 Branches: 0 158 0.0 %

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