1 |
|
|
/* |
2 |
|
|
* The ManaPlus Client |
3 |
|
|
* Copyright (C) 2011-2019 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 |
|
|
return; |
66 |
|
|
createOutPacket(CMSG_MAIL2_OPEN_WRITE_MAIL); |
67 |
|
|
outMsg.writeString(receiver, 24, "receiver name"); |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
void Mail2Handler::addItem(const Item *const item, |
71 |
|
|
const int amount) const |
72 |
|
|
{ |
73 |
|
|
if (item == nullptr) |
74 |
|
|
return; |
75 |
|
|
if (packetVersion < 20140416) |
76 |
|
|
return; |
77 |
|
|
|
78 |
|
|
createOutPacket(CMSG_MAIL2_ADD_ITEM_TO_MAIL); |
79 |
|
|
outMsg.writeInt16(CAST_S16( |
80 |
|
|
item->getInvIndex() + INVENTORY_OFFSET), "index"); |
81 |
|
|
outMsg.writeInt16(CAST_S16(amount), "amount"); |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
void Mail2Handler::removeItem(const int index, |
85 |
|
|
const int amount) const |
86 |
|
|
{ |
87 |
|
|
if (packetVersion < 20140416) |
88 |
|
|
return; |
89 |
|
|
|
90 |
|
|
createOutPacket(CMSG_MAIL2_REMOVE_ITEM_MAIL); |
91 |
|
|
outMsg.writeInt16(CAST_S16(index + INVENTORY_OFFSET), "index"); |
92 |
|
|
outMsg.writeInt16(CAST_S16(amount), "amount"); |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
void Mail2Handler::sendMail(const std::string &to, |
96 |
|
|
const std::string &title, |
97 |
|
|
const std::string &body, |
98 |
|
|
const int64_t &money) const |
99 |
|
|
{ |
100 |
|
|
if (packetVersion < 20131230) |
101 |
|
|
return; |
102 |
|
|
if (localPlayer == nullptr) |
103 |
|
|
return; |
104 |
|
|
|
105 |
|
|
const std::string from = localPlayer->getName(); |
106 |
|
|
const int titleSz = CAST_S32(title.size()) + 1; |
107 |
|
|
const int bodySz = CAST_S32(body.size()) + 1; |
108 |
|
|
int32_t sz = 2 + 2 + 24 + 24 + 8 + 2 + 2 + titleSz + bodySz; |
109 |
|
|
if (sz > 32767 - 4) |
110 |
|
|
{ |
111 |
|
|
reportAlways("Mail message too big") |
112 |
|
|
return; |
113 |
|
|
} |
114 |
|
|
if (packetVersion >= 20160600) |
115 |
|
|
sz += 4; |
116 |
|
|
|
117 |
|
|
createOutPacket(CMSG_MAIL2_SEND_MAIL); |
118 |
|
|
outMsg.writeInt16(CAST_S16(sz), "len"); |
119 |
|
|
outMsg.writeString(to, 24, "to"); |
120 |
|
|
outMsg.writeString(from, 24, "from"); |
121 |
|
|
outMsg.writeInt64(money, "money"); |
122 |
|
|
outMsg.writeInt16(CAST_S16(titleSz), "title len"); |
123 |
|
|
outMsg.writeInt16(CAST_S16(bodySz), "body len"); |
124 |
|
|
if (packetVersion >= 20160600) |
125 |
|
|
outMsg.writeInt32(0, "to char id"); |
126 |
|
|
outMsg.writeString(title, titleSz, "title"); |
127 |
|
|
outMsg.writeString(body, bodySz, "body"); |
128 |
|
|
Mail2Recv::mCheckedName.clear(); |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
void Mail2Handler::queueCheckName(const MailQueueTypeT type, |
132 |
|
|
const std::string &to, |
133 |
|
|
const std::string &title, |
134 |
|
|
const std::string &body, |
135 |
|
|
const int64_t &money) const |
136 |
|
|
{ |
137 |
|
|
MailQueue *const mail = new MailQueue; |
138 |
|
|
mail->to = to; |
139 |
|
|
mail->title = title; |
140 |
|
|
mail->body = body; |
141 |
|
|
mail->money = money; |
142 |
|
|
mail->type = type; |
143 |
|
|
Mail2Recv::mMailQueue.push(mail); |
144 |
|
|
requestCheckName(to); |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
void Mail2Handler::nextPage(const MailOpenTypeT openType, |
148 |
|
|
const int64_t mailId) const |
149 |
|
|
{ |
150 |
|
|
if (packetVersion < 20131218) |
151 |
|
|
return; |
152 |
|
|
createOutPacket(CMSG_MAIL2_NEXT_PAGE); |
153 |
|
|
outMsg.writeInt8(toInt(openType, int8_t), "open type"); |
154 |
|
|
outMsg.writeInt64(mailId, "mail id"); |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
void Mail2Handler::readMail(const MailOpenTypeT openType, |
158 |
|
|
const int64_t mailId) const |
159 |
|
|
{ |
160 |
|
|
if (packetVersion < 20131223) |
161 |
|
|
return; |
162 |
|
|
createOutPacket(CMSG_MAIL2_READ_MAIL); |
163 |
|
|
outMsg.writeInt8(toInt(openType, int8_t), "open type"); |
164 |
|
|
outMsg.writeInt64(mailId, "mail id"); |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
void Mail2Handler::deleteMail(const MailOpenTypeT openType, |
168 |
|
|
const int64_t mailId) const |
169 |
|
|
{ |
170 |
|
|
if (packetVersion < 20131218) |
171 |
|
|
return; |
172 |
|
|
createOutPacket(CMSG_MAIL2_DELETE_MAIL); |
173 |
|
|
outMsg.writeInt8(toInt(openType, int8_t), "open type"); |
174 |
|
|
outMsg.writeInt64(mailId, "mail id"); |
175 |
|
|
} |
176 |
|
|
|
177 |
|
|
void Mail2Handler::requestMoney(const MailOpenTypeT openType, |
178 |
|
|
const int64_t mailId) const |
179 |
|
|
{ |
180 |
|
|
if (packetVersion < 20140326) |
181 |
|
|
return; |
182 |
|
|
createOutPacket(CMSG_MAIL2_REQUEST_MONEY); |
183 |
|
|
outMsg.writeInt64(mailId, "mail id"); |
184 |
|
|
outMsg.writeInt8(toInt(openType, int8_t), "open type"); |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
void Mail2Handler::requestItems(const MailOpenTypeT openType, |
188 |
|
|
const int64_t mailId) const |
189 |
|
|
{ |
190 |
|
|
if (packetVersion < 20140326) |
191 |
|
|
return; |
192 |
|
|
createOutPacket(CMSG_MAIL2_REQUEST_ITEMS); |
193 |
|
|
outMsg.writeInt64(mailId, "mail id"); |
194 |
|
|
outMsg.writeInt8(toInt(openType, int8_t), "open type"); |
195 |
|
|
} |
196 |
|
|
|
197 |
|
|
void Mail2Handler::refreshMailList(const MailOpenTypeT openType, |
198 |
|
|
const int64_t mailId) const |
199 |
|
|
{ |
200 |
|
|
if (packetVersion < 20131218) |
201 |
|
|
return; |
202 |
|
|
createOutPacket(CMSG_MAIL2_REFRESH_MAIL_LIST); |
203 |
|
|
if (packetVersion >= 20170419) |
204 |
|
|
{ |
205 |
|
|
outMsg.writeInt64(mailId, "mail id"); |
206 |
|
|
outMsg.writeInt32(0, "unknown 1"); |
207 |
|
|
outMsg.writeInt32(0, "unknown 2"); |
208 |
|
|
outMsg.writeInt32(0, "unknown 3"); |
209 |
|
|
outMsg.writeInt32(0, "unknown 4"); |
210 |
|
|
} |
211 |
|
|
else |
212 |
|
|
{ |
213 |
|
|
outMsg.writeInt8(toInt(openType, int8_t), "open type"); |
214 |
|
|
outMsg.writeInt64(mailId, "mail id"); |
215 |
|
|
} |
216 |
|
|
} |
217 |
|
|
|
218 |
|
|
void Mail2Handler::openMailBox(const MailOpenTypeT openType) const |
219 |
|
|
{ |
220 |
|
|
if (packetVersion < 20140212) |
221 |
|
|
return; |
222 |
|
|
createOutPacket(CMSG_MAIL2_OPEN_MAILBOX); |
223 |
|
|
if (packetVersion >= 20170419) |
224 |
|
|
{ |
225 |
|
|
outMsg.writeInt64(0, "char mail id"); |
226 |
|
|
outMsg.writeInt64(0, "return mail id"); |
227 |
|
|
outMsg.writeInt64(0, "account mail id"); |
228 |
|
|
} |
229 |
|
|
else |
230 |
|
|
{ |
231 |
|
|
outMsg.writeInt8(toInt(openType, int8_t), "open type"); |
232 |
|
|
outMsg.writeInt64(0, "mail id"); |
233 |
|
|
} |
234 |
|
|
} |
235 |
|
|
|
236 |
|
|
void Mail2Handler::closeMailBox() const |
237 |
|
|
{ |
238 |
|
|
if (packetVersion < 20131211) |
239 |
|
|
return; |
240 |
|
|
createOutPacket(CMSG_MAIL2_CLOSE_MAILBOX); |
241 |
|
|
} |
242 |
|
|
|
243 |
|
|
void Mail2Handler::cancelWriteMail() const |
244 |
|
|
{ |
245 |
|
|
if (packetVersion < 20140326) |
246 |
|
|
return; |
247 |
|
|
createOutPacket(CMSG_MAIL2_CANCEL_WRITE_MAIL); |
248 |
|
|
} |
249 |
|
|
|
250 |
|
|
void Mail2Handler::requestCheckName(const std::string &name) const |
251 |
|
|
{ |
252 |
|
|
if (packetVersion < 20140423) |
253 |
|
|
return; |
254 |
|
|
createOutPacket(CMSG_MAIL2_CHECK_NAME); |
255 |
|
|
outMsg.writeString(name, 24, "name"); |
256 |
|
|
} |
257 |
|
|
|
258 |
|
|
std::string Mail2Handler::getCheckedName() const |
259 |
|
|
{ |
260 |
|
|
return Mail2Recv::mCheckedName; |
261 |
|
|
} |
262 |
|
|
|
263 |
|
2 |
} // namespace EAthena |