ManaPlus
mailrecv.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 
22 #include "net/eathena/mailrecv.h"
23 
24 #include "itemcolormanager.h"
25 #include "notifymanager.h"
26 
28 
30 
31 #include "gui/mailmessage.h"
32 
34 #include "gui/windows/mailwindow.h"
35 
36 #include "net/mailhandler.h"
37 #include "net/messagein.h"
38 
39 #include "utils/checkutils.h"
40 #include "utils/gettext.h"
41 #include "utils/stringutils.h"
42 
44 
45 #include "debug.h"
46 
47 namespace EAthena
48 {
49 
51 {
52  const int flag = msg.readInt32("flag");
53  switch (flag)
54  {
55  case 0: // open window
56  if (mailWindow != nullptr)
57  {
61  }
62  break;
63 
64  case 1: // close window
65  if (mailWindow != nullptr)
66  {
69  }
70  break;
71 
72  default:
74  break;
75  }
76 }
77 
79 {
80  const int count = (msg.readInt16("len") - 8) / 73;
81  const int amount = msg.readInt32("amount");
82  if (count != amount)
83  logger->log("error: wrong mails count");
84  mailWindow->clear();
85  for (int f = 0; f < count; f ++)
86  {
87  MailMessage *const mail = new MailMessage;
88  mail->id = msg.readInt32("message id");
89  mail->title = msg.readString(40, "title");
90  mail->read = msg.readUInt8("unread flag") != 0U ? true : false;
91  mail->sender = msg.readString(24, "sender name");
92  mail->time = msg.readInt32("time stamp");
93  mail->strTime = timeToStr(mail->time);
94  mailWindow->addMail(mail);
95  }
96 }
97 
99 {
100  const int sz = msg.readInt16("len") - 101;
101  MailMessage *const mail = new MailMessage;
102  mail->id = msg.readInt32("message id");
103  mail->title = msg.readString(40, "title");
104  mail->sender = msg.readString(24, "sender name");
105  msg.readInt32("unused");
106  mail->money = msg.readInt32("money");
107 
108  const int amount = msg.readInt32("item amount");
109  const int itemId = msg.readInt16("item id");
110  const ItemTypeT itemType = static_cast<ItemTypeT>(
111  msg.readInt16("item type"));
112  const uint8_t identify = msg.readUInt8("identify");
113  const Damaged damaged = fromBool(msg.readUInt8("attribute"), Damaged);
114  const uint8_t refine = msg.readUInt8("refine");
115  int cards[maxCards];
116  for (int d = 0; d < maxCards; d ++)
117  cards[d] = msg.readUInt16("card");
118  const int msgLen = msg.readUInt8("msg len");
119  if (msgLen != sz)
120  logger->log("error: wrong message size");
121  mail->text = msg.readString(sz, "message");
122  msg.readUInt8("zero");
123  mail->strTime = timeToStr(mail->time);
124  if (mailWindow == nullptr)
125  {
126  reportAlways("Mail window not created")
127  delete mail;
128  return;
129  }
130  mailWindow->showMessage(mail, itemId != 0 ? 1 : 0);
131  if (mailViewWindow == nullptr)
132  {
133  reportAlways("Mail view window not created")
134  return;
135  }
136 
137  Inventory *const inventory = mailViewWindow->getInventory();
138  if (inventory == nullptr)
139  {
140  reportAlways("Mail view window missing inventory")
141  return;
142  }
143 
144  const int slot = inventory->addItem(itemId,
145  itemType,
146  amount,
147  refine,
149  fromBool(identify, Identified),
150  damaged,
152  Equipm_false,
154  if (slot != -1)
155  inventory->setCards(slot, cards, maxCards);
156 
158 }
159 
161 {
162  const uint8_t flag = msg.readUInt8("flag");
163  switch (flag)
164  {
165  case 0:
167  break;
168  case 1:
170  break;
171  case 2:
173  break;
174  default:
176  break;
177  }
178 }
179 
181 {
182  const uint8_t flag = msg.readUInt8("fail flag");
183  switch (flag)
184  {
185  case 0:
187  break;
188  case 1:
190  break;
191  default:
193  break;
194  }
195 }
196 
198 {
199  msg.readInt32("message id");
200  const std::string subj = msg.readString(40, "title");
201  const std::string sender = msg.readString(24, "sender name");
203  // TRANSLATORS: mail message notification
204  strprintf(_("You have new mail from %s with subject %s"),
205  sender.c_str(), subj.c_str()));
206  mailHandler->refresh();
207 }
208 
210 {
211  const int index = msg.readInt16("index");
212  const int flag = msg.readUInt8("flag");
213  if (flag != 0)
214  {
215  if (index != 0)
217  else
219  }
220 }
221 
223 {
224  const int mail = msg.readInt32("message id");
225  const int flag = msg.readInt16("fail flag");
226  if (flag != 0)
227  {
229  }
230  else
231  {
233  mailWindow->removeMail(mail);
234  }
235 }
236 
238 {
239  const int mail = msg.readInt32("message id");
240  const int flag = msg.readInt16("fail flag");
241  if (flag != 0)
242  {
244  }
245  else
246  {
248  mailWindow->removeMail(mail);
249  }
250 }
251 
252 } // namespace EAthena
#define fromBool(val, name)
Definition: booldefines.h:49
#define maxCards
Definition: cards.h:25
#define reportAlways(...)
Definition: checkutils.h:253
void setCards(const int index, const int *const cards, const int size) const
Definition: inventory.cpp:194
int addItem(const int id, const ItemTypeT type, const int quantity, const uint8_t refine, const ItemColor color, const Identified identified, const Damaged damaged, const Favorite favorite, const Equipm equipment, const Equipped equipped)
Definition: inventory.cpp:115
static ItemColor getColorFromCards(const int *const cards)
void log(const char *const log_text,...)
Definition: logger.cpp:269
Inventory * getInventory() const
void clear()
Definition: mailwindow.cpp:184
void removeMail(const int64_t id)
Definition: mailwindow.cpp:233
void addMail(MailMessage *const message)
Definition: mailwindow.cpp:224
void showMessage(MailMessage *const mail, const int itemsCount)
Definition: mailwindow.cpp:260
virtual void refresh() const =0
virtual void requestMoveToTop()
Definition: widget.cpp:213
virtual void setVisible(Visible visible)
Definition: window.cpp:778
bool isWindowVisible() const
Definition: window.h:484
bool Damaged
Definition: damaged.h:30
const bool Equipm_false
Definition: equipm.h:30
const bool Equipped_false
Definition: equipped.h:30
const bool Favorite_false
Definition: favorite.h:30
#define _(s)
Definition: gettext.h:35
bool Identified
Definition: identified.h:30
ItemType ::T ItemTypeT
Definition: itemtype.h:43
Logger * logger
Definition: logger.cpp:89
#define UNIMPLEMENTEDPACKETFIELD(field)
Definition: logger.h:59
Net::MailHandler * mailHandler
Definition: net.cpp:114
MailViewWindow * mailViewWindow
MailWindow * mailWindow
Definition: mailwindow.cpp:54
bool msg(InputEvent &event)
Definition: chat.cpp:39
void processSendMailAck(Net::MessageIn &msg)
Definition: mailrecv.cpp:180
void processNewMail(Net::MessageIn &msg)
Definition: mailrecv.cpp:197
void processSetAttachmentAck(Net::MessageIn &msg)
Definition: mailrecv.cpp:209
void processMailOpen(Net::MessageIn &msg)
Definition: mailrecv.cpp:50
void processReadMail(Net::MessageIn &msg)
Definition: mailrecv.cpp:98
void processMailReturn(Net::MessageIn &msg)
Definition: mailrecv.cpp:237
void processDeleteAck(Net::MessageIn &msg)
Definition: mailrecv.cpp:222
void processMailList(Net::MessageIn &msg)
Definition: mailrecv.cpp:78
void processGetAttachment(Net::MessageIn &msg)
Definition: mailrecv.cpp:160
void notify(const unsigned int message)
@ MAIL_GET_ATTACH_ERROR
Definition: notifytypes.h:175
@ MAIL_ATTACH_MONEY_ERROR
Definition: notifytypes.h:169
@ MAIL_GET_ATTACH_TOO_MANY_ITEMS
Definition: notifytypes.h:176
@ MAIL_ATTACH_ITEM_ERROR
Definition: notifytypes.h:168
@ MAIL_GET_ATTACH_OK
Definition: notifytypes.h:174
std::string strprintf(const char *const format,...)
std::string timeToStr(const uint32_t time)
int64_t money
Definition: mailmessage.h:57
std::string sender
Definition: mailmessage.h:51
int64_t id
Definition: mailmessage.h:54
std::string strTime
Definition: mailmessage.h:52
std::string title
Definition: mailmessage.h:50
std::string text
Definition: mailmessage.h:53
const bool Visible_false
Definition: visible.h:30
const bool Visible_true
Definition: visible.h:30