ManaPlus
mail2handler.cpp
Go to the documentation of this file.
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 
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"
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 
47 {
48  mail2Handler = this;
49 }
50 
52 {
53  mail2Handler = nullptr;
55  while (!Mail2Recv::mMailQueue.empty())
56  {
57  MailQueue *const mail = Mail2Recv::mMailQueue.front();
58  delete mail;
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 
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 
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 
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 
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 
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 
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 
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 
238 {
239  if (packetVersion < 20131211)
240  return;
241  createOutPacket(CMSG_MAIL2_CLOSE_MAILBOX);
242 }
243 
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 {
262 }
263 
264 } // namespace EAthena
#define CAST_S16
Definition: cast.h:28
#define CAST_S32
Definition: cast.h:30
#define reportAlways(...)
Definition: checkutils.h:253
const std::string & getName() const
Definition: being.h:232
void requestMoney(const MailOpenTypeT openType, const int64_t mailId) const
void removeItem(const int index, const int amount) const
void queueCheckName(const MailQueueTypeT type, const std::string &to, const std::string &title, const std::string &body, const int64_t &money) const
void nextPage(const MailOpenTypeT openType, const int64_t mailId) const
void readMail(const MailOpenTypeT openType, const int64_t mailId) const
void cancelWriteMail() const
void openMailBox(const MailOpenTypeT openType) const
std::string getCheckedName() const
void addItem(const Item *const item, const int amount) const
void deleteMail(const MailOpenTypeT openType, const int64_t mailId) const
void openWriteMail(const std::string &receiver) const
void sendMail(const std::string &to, const std::string &title, const std::string &body, const int64_t &money) const
void requestItems(const MailOpenTypeT openType, const int64_t mailId) const
void refreshMailList(const MailOpenTypeT openType, const int64_t mailId) const
void requestCheckName(const std::string &name) const
void closeMailBox() const
Definition: item.h:50
int getInvIndex() const
Definition: item.h:165
static const int INVENTORY_OFFSET
Definition: inventory.h:27
int serverVersion
Definition: client.cpp:124
int packetVersion
Definition: client.cpp:125
#define toInt(val, name)
Definition: intdefines.h:47
LocalPlayer * localPlayer
Net::Mail2Handler * mail2Handler
Definition: net.cpp:113
MailOpenType ::T MailOpenTypeT
Definition: mailopentype.h:33
MailQueueType ::T MailQueueTypeT
Definition: mailqueuetype.h:34
#define createOutPacket(name)
Definition: messageout.h:37
std::string mCheckedName
Definition: mail2recv.cpp:64
std::queue< MailQueue * > mMailQueue
Definition: mail2recv.cpp:63
std::string empty
Definition: podict.cpp:26
std::string title
Definition: mailqueue.h:44
int64_t money
Definition: mailqueue.h:46
std::string body
Definition: mailqueue.h:45
MailQueueTypeT type
Definition: mailqueue.h:47
std::string to
Definition: mailqueue.h:43