GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/mail2recv.cpp Lines: 3 251 1.2 %
Date: 2018-09-20 Branches: 2 215 0.9 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2018  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/mail2recv.h"
22
23
#include "itemcolormanager.h"
24
#include "notifymanager.h"
25
26
#include "const/net/inventory.h"
27
28
#include "being/playerinfo.h"
29
30
#include "enums/resources/notifytypes.h"
31
32
#include "gui/mailmessage.h"
33
34
#include "gui/windows/maileditwindow.h"
35
#include "gui/windows/mailviewwindow.h"
36
#include "gui/windows/mailwindow.h"
37
38
#include "net/mail2handler.h"
39
#include "net/messagein.h"
40
41
#include "resources/mailqueue.h"
42
43
#include "resources/inventory/inventory.h"
44
45
#include "resources/item/item.h"
46
#include "resources/item/itemoptionslist.h"
47
48
#include "utils/checkutils.h"
49
#include "utils/gettext.h"
50
#include "utils/stringutils.h"
51
#include "utils/timer.h"
52
53
#include "debug.h"
54
55
extern int packetVersion;
56
57
namespace EAthena
58
{
59
60
namespace Mail2Recv
61
{
62
3
    std::queue<MailQueue*> mMailQueue;
63
1
    std::string mCheckedName;
64
}  // namespace Mail2Recv
65
66
void Mail2Recv::processMailIcon(Net::MessageIn &msg)
67
{
68
    // ignored, because if has new mail, server send chat message already.
69
    msg.readUInt8("has new mail");
70
}
71
72
void Mail2Recv::processOpenNewMailWindow(Net::MessageIn &msg)
73
{
74
    UNIMPLEMENTEDPACKET;
75
    msg.readString(24, "receiver");
76
    msg.readUInt8("result");
77
}
78
79
void Mail2Recv::processAddItemResult(Net::MessageIn &msg)
80
{
81
    const int res = msg.readUInt8("result");
82
    const int index = msg.readInt16("index") - INVENTORY_OFFSET;
83
    const int amount = msg.readInt16("amount");
84
    const int itemId = msg.readItemId("item id");
85
    const ItemTypeT itemType = static_cast<ItemTypeT>(
86
        msg.readUInt8("item type"));
87
    const uint8_t identify = msg.readUInt8("identify");
88
    const Damaged damaged = fromBool(msg.readUInt8("attribute"), Damaged);
89
    const uint8_t refine = msg.readUInt8("refine");
90
    int cards[maxCards];
91
    for (int f = 0; f < maxCards; f++)
92
        cards[f] = msg.readItemId("card");
93
    ItemOptionsList *options = new ItemOptionsList(5);
94
    for (int f = 0; f < 5; f ++)
95
    {
96
        const uint16_t idx = msg.readInt16("option index");
97
        const uint16_t val = msg.readInt16("option value");
98
        msg.readUInt8("option param");
99
        options->add(idx, val);
100
    }
101
    msg.readInt16("weight");
102
    Favorite favorite = fromBool(msg.readUInt8("favorite"), Favorite);
103
    msg.readInt32("location");
104
105
    if (mailEditWindow == nullptr)
106
    {
107
        reportAlways("Mail edit window not created");
108
        delete options;
109
        return;
110
    }
111
    Inventory *const inventory = mailEditWindow->getInventory();
112
    if (inventory == nullptr)
113
    {
114
        reportAlways("Mail edit window inventory not exists");
115
        delete options;
116
        return;
117
    }
118
119
    if (res != 0)
120
    {
121
        switch (res)
122
        {
123
            case 1:
124
                NotifyManager::notify(
125
                    NotifyTypes::MAIL_ATTACH_ITEM_WEIGHT_ERROR);
126
                break;
127
            case 2:
128
                NotifyManager::notify(
129
                    NotifyTypes::MAIL_ATTACH_ITEM_FATAL_ERROR);
130
                break;
131
            case 3:
132
                NotifyManager::notify(
133
                    NotifyTypes::MAIL_ATTACH_ITEM_NO_SPACE);
134
                break;
135
            case 4:
136
                NotifyManager::notify(
137
                    NotifyTypes::MAIL_ATTACH_ITEM_NOT_TRADEABLE);
138
                break;
139
            default:
140
                NotifyManager::notify(
141
                    NotifyTypes::MAIL_ATTACH_ITEM_UNKNOWN_ERROR);
142
                UNIMPLEMENTEDPACKETFIELD(res);
143
                break;
144
        }
145
        delete options;
146
        return;
147
    }
148
149
    Item *const item = inventory->findItemByTag(index);
150
    if (item == nullptr)
151
    {
152
        const int slot = inventory->addItem(itemId,
153
            itemType,
154
            amount,
155
            refine,
156
            ItemColorManager::getColorFromCards(&cards[0]),
157
            fromBool(identify, Identified),
158
            damaged,
159
            favorite,
160
            Equipm_false,
161
            Equipped_false);
162
        if (slot == -1)
163
        {
164
            delete options;
165
            return;
166
        }
167
        inventory->setCards(slot, cards, maxCards);
168
        inventory->setOptions(slot, options);
169
        inventory->setTag(slot, index);
170
    }
171
    else
172
    {
173
        item->increaseQuantity(amount);
174
    }
175
176
    mailEditWindow->updateItems();
177
    delete options;
178
}
179
180
void Mail2Recv::processRemoveItemResult(Net::MessageIn &msg)
181
{
182
    const int result = msg.readUInt8("result");
183
    const int index = msg.readInt16("index") - INVENTORY_OFFSET;
184
    const int amount = msg.readInt16("count");
185
    msg.readInt16("weight");
186
187
    if (result == 0)
188
    {
189
        const Inventory *const inv = PlayerInfo::getInventory();
190
        if (inv == nullptr)
191
        {
192
            reportAlways("Player inventory not exists");
193
            return;
194
        }
195
        std::string itemName;
196
        const Item *const item = inv->getItem(index);
197
        if (item != nullptr)
198
        {
199
            itemName = item->getName();
200
        }
201
        else
202
        {
203
            // TRANSLATORS: unknown item name
204
            itemName = _("Unknown item");
205
        }
206
207
        NotifyManager::notify(
208
            NotifyTypes::MAIL_REMOVE_ITEM_ERROR,
209
            itemName);
210
        return;
211
    }
212
    if (mailEditWindow == nullptr)
213
    {
214
        reportAlways("Mail edit window not created");
215
        return;
216
    }
217
    Inventory *const inventory = mailEditWindow->getInventory();
218
    if (inventory == nullptr)
219
    {
220
        reportAlways("Mail edit window inventory not exists");
221
        return;
222
    }
223
    const int index2 = inventory->findIndexByTag(index);
224
    if (index2 == -1)
225
    {
226
        reportAlways("Item not exists in mail edit window.");
227
        return;
228
    }
229
    Item *const item = inventory->getItem(index2);
230
    if (item == nullptr)
231
    {
232
        reportAlways("Item not exists.");
233
        return;
234
    }
235
236
    item->increaseQuantity(-amount);
237
    mailEditWindow->updateItems();
238
}
239
240
void Mail2Recv::processCheckNameResult(Net::MessageIn &msg)
241
{
242
    const int charId = msg.readInt32("char id");
243
    msg.readInt16("class");
244
    msg.readInt16("level");
245
    if (msg.getVersion() >= 20160316)
246
        msg.readString(24, "name");
247
    // +++ in future if name received, need use it in map
248
    if (mMailQueue.empty())
249
    {
250
        reportAlways("Mail2Recv::processCheckNameResult no names in queue."
251
            "Char id: %d", charId);
252
        return;
253
    }
254
    MailQueue *const mail = mMailQueue.front();
255
    mMailQueue.pop();
256
    if (charId == 0)
257
    {
258
        NotifyManager::notify(NotifyTypes::MAIL_NAME_VALIDATION_ERROR,
259
            mail->to);
260
        delete mail;
261
        return;
262
    }
263
    mCheckedName = mail->to;
264
    switch (mail->type)
265
    {
266
        case MailQueueType::SendMail:
267
            mail2Handler->sendMail(mail->to,
268
                mail->title,
269
                mail->body,
270
                mail->money);
271
            break;
272
        case MailQueueType::EditMail:
273
            if (mailWindow == nullptr)
274
            {
275
                reportAlways("Mail window not created");
276
            }
277
            else
278
            {
279
                mailWindow->createMail(mail->to);
280
            }
281
            break;
282
        case MailQueueType::ValidateTo:
283
            if (mailEditWindow == nullptr)
284
            {
285
                reportAlways("Mail edit window not created");
286
            }
287
            else
288
            {
289
                mailEditWindow->validatedTo();
290
            }
291
            break;
292
        case MailQueueType::Unknown:
293
        default:
294
            reportAlways("Not implemented yet.");
295
            break;
296
    }
297
    delete mail;
298
}
299
300
void Mail2Recv::processSendResult(Net::MessageIn &msg)
301
{
302
    const int res = msg.readUInt8("result");
303
    switch (res)
304
    {
305
        case 0:
306
            NotifyManager::notify(NotifyTypes::MAIL_SEND_OK);
307
            if (mailEditWindow != nullptr)
308
                mailEditWindow->close();
309
            break;
310
        case 1:
311
            NotifyManager::notify(NotifyTypes::MAIL_SEND_FATAL_ERROR);
312
            break;
313
        case 2:
314
            NotifyManager::notify(NotifyTypes::MAIL_SEND_COUNT_ERROR);
315
            break;
316
        case 3:
317
            NotifyManager::notify(NotifyTypes::MAIL_SEND_ITEM_ERROR);
318
            break;
319
        case 4:
320
            NotifyManager::notify(NotifyTypes::MAIL_SEND_RECEIVER_ERROR);
321
            break;
322
        default:
323
            NotifyManager::notify(NotifyTypes::MAIL_SEND_ERROR);
324
            break;
325
    }
326
}
327
328
void Mail2Recv::processMailListPage(Net::MessageIn &msg)
329
{
330
    if (mailWindow == nullptr)
331
    {
332
        reportAlways("mail window not created");
333
        return;
334
    }
335
    msg.readInt16("len");
336
    bool isEnd = true;
337
338
    if (packetVersion < 20170419)
339
    {
340
        mailWindow->setOpenType(fromInt(msg.readUInt8("open type"),
341
            MailOpenTypeT));
342
        const int cnt = msg.readUInt8("cnt");
343
        isEnd = msg.readUInt8("isEnd") != 0;
344
        for (int f = 0; f < cnt; f ++)
345
        {
346
            MailMessage *const mail = new MailMessage;
347
            mail->id = msg.readInt64("mail id");
348
            mail->read = msg.readUInt8("is read") != 0U ? true : false;
349
            mail->type = static_cast<MailMessageType::Type>(
350
                msg.readUInt8("type"));
351
            mail->sender = msg.readString(24, "sender name");
352
            if (packetVersion < 20170419)
353
            {
354
                mail->time = CAST_S32(cur_time - msg.readInt32("reg time"));
355
                mail->strTime = timeToStr(mail->time);
356
            }
357
            mail->expireTime = msg.readInt32("expire time") + cur_time;
358
            mail->expired = mail->expireTime <= 0;
359
            mail->title = msg.readString(-1, "title");
360
            mailWindow->addMail(mail);
361
        }
362
    }
363
    else
364
    {
365
        isEnd = msg.readUInt8("isEnd") != 0;
366
        while (msg.getUnreadLength() > 0)
367
        {
368
            MailMessage *const mail = new MailMessage;
369
            // +++ need save open type into MailMessage
370
            msg.readUInt8("open type");
371
            mail->id = msg.readInt64("mail id");
372
            mail->read = msg.readUInt8("is read") != 0U ? true : false;
373
            mail->type = static_cast<MailMessageType::Type>(
374
                msg.readUInt8("type"));
375
            mail->sender = msg.readString(24, "sender name");
376
            mail->strTime = "-";
377
            mail->expireTime = msg.readInt32("expire time") + cur_time;
378
            mail->expired = mail->expireTime <= 0;
379
            mail->title = msg.readString(-1, "title");
380
            mailWindow->addMail(mail);
381
        }
382
    }
383
384
    if (isEnd)
385
        mailWindow->setLastPage();
386
}
387
388
void Mail2Recv::processReadMail(Net::MessageIn &msg)
389
{
390
    msg.readInt16("len");
391
    const MailOpenTypeT openType = static_cast<MailOpenTypeT>(
392
        msg.readUInt8("open type"));
393
    const int64_t mailId = msg.readInt64("mail id");
394
    const int textLen = msg.readInt16("text len");
395
    const int64_t money = msg.readInt64("money");
396
    const int itemsCount = msg.readUInt8("item count");
397
    const std::string text = msg.readString(textLen, "text message");
398
    MailMessage *mail = nullptr;
399
400
    if (mailWindow != nullptr &&
401
        openType == mailWindow->getOpenType())
402
    {
403
        mail = mailWindow->findMail(mailId);
404
    }
405
406
    if (mail == nullptr)
407
    {
408
        reportAlways("Mail message not found");
409
        for (int f = 0; f < itemsCount; f ++)
410
        {
411
            msg.readInt16("amount");
412
            msg.readItemId("item id");
413
            msg.readUInt8("identify");
414
            msg.readUInt8("damaged");
415
            msg.readUInt8("refine");
416
            for (int d = 0; d < maxCards; d ++)
417
                msg.readItemId("card");
418
            msg.readInt32("location");
419
            msg.readUInt8("type");
420
            msg.readInt16("view sprite");
421
            msg.readInt16("bind on equip");
422
            for (int d = 0; d < 5; d ++)
423
            {
424
                msg.readInt16("option index");
425
                msg.readInt16("option value");
426
                msg.readUInt8("option param");
427
            }
428
        }
429
        return;
430
    }
431
432
    mail->money = money;
433
    mail->text = text;
434
    mailWindow->showMessage(mail, itemsCount);
435
436
    Inventory *const inventory = mailViewWindow->getInventory();
437
438
    for (int f = 0; f < itemsCount; f ++)
439
    {
440
        // server may send wrong items count, if items was removed from mail
441
        if (msg.getUnreadLength() == 0)
442
            break;
443
        const int amount = msg.readInt16("amount");
444
        const int itemId = msg.readItemId("item id");
445
        const uint8_t identify = msg.readUInt8("identify");
446
        const Damaged damaged = fromBool(msg.readUInt8("attribute"), Damaged);
447
        const uint8_t refine = msg.readUInt8("refine");
448
        int cards[maxCards];
449
        for (int d = 0; d < maxCards; d ++)
450
            cards[d] = msg.readItemId("card");
451
        msg.readInt32("location");
452
        const ItemTypeT itemType = static_cast<ItemTypeT>(
453
            msg.readUInt8("item type"));
454
        msg.readInt16("view sprite");
455
        msg.readInt16("bind on equip");
456
        ItemOptionsList *options = new ItemOptionsList(5);
457
        for (int d = 0; d < 5; d ++)
458
        {
459
            const uint16_t idx = msg.readInt16("option index");
460
            const uint16_t val = msg.readInt16("option value");
461
            msg.readUInt8("option param");
462
            options->add(idx, val);
463
        }
464
465
        const int slot = inventory->addItem(itemId,
466
            itemType,
467
            amount,
468
            refine,
469
            ItemColorManager::getColorFromCards(&cards[0]),
470
            fromBool(identify, Identified),
471
            damaged,
472
            Favorite_false,
473
            Equipm_false,
474
            Equipped_false);
475
        if (slot == -1)
476
        {
477
            delete options;
478
            continue;
479
        }
480
        inventory->setCards(slot, cards, maxCards);
481
        inventory->setOptions(slot, options);
482
        delete options;
483
    }
484
    mailViewWindow->updateItems();
485
}
486
487
void Mail2Recv::processMailDelete(Net::MessageIn &msg)
488
{
489
    msg.readUInt8("open type");
490
    const int64_t mailId = msg.readInt64("mail id");
491
    if (mailWindow == nullptr)
492
    {
493
        reportAlways("Mail window not created.");
494
        return;
495
    }
496
    mailWindow->removeMail(mailId);
497
}
498
499
void Mail2Recv::processRequestMoney(Net::MessageIn &msg)
500
{
501
    const int64_t mailId = msg.readInt64("mail id");
502
    msg.readUInt8("open type");
503
    const int res = msg.readUInt8("result");
504
    switch (res)
505
    {
506
        case 0:
507
            NotifyManager::notify(
508
                NotifyTypes::MAIL_GET_MONEY_OK);
509
            if (mailViewWindow != nullptr)
510
                mailViewWindow->removeMoney(mailId);
511
            break;
512
        case 1:
513
            NotifyManager::notify(
514
                NotifyTypes::MAIL_GET_MONEY_ERROR);
515
            break;
516
        case 2:
517
            NotifyManager::notify(
518
                NotifyTypes::MAIL_GET_MONEY_LIMIT_ERROR);
519
            break;
520
        default:
521
            UNIMPLEMENTEDPACKETFIELD(res);
522
            NotifyManager::notify(
523
                NotifyTypes::MAIL_GET_MONEY_ERROR);
524
            break;
525
    }
526
}
527
528
void Mail2Recv::processRequestItems(Net::MessageIn &msg)
529
{
530
    const int64_t mailId = msg.readInt64("mail id");
531
    msg.readUInt8("open type");
532
    const int res = msg.readUInt8("result");
533
    switch (res)
534
    {
535
        case 0:
536
            NotifyManager::notify(
537
                NotifyTypes::MAIL_GET_ATTACH_OK);
538
            if (mailViewWindow != nullptr)
539
                mailViewWindow->removeItems(mailId);
540
            break;
541
        case 1:
542
            NotifyManager::notify(
543
                NotifyTypes::MAIL_GET_ATTACH_ERROR);
544
            break;
545
        case 2:
546
            NotifyManager::notify(
547
                NotifyTypes::MAIL_GET_ATTACH_FULL_ERROR);
548
            break;
549
        default:
550
            UNIMPLEMENTEDPACKETFIELD(res);
551
            NotifyManager::notify(
552
                NotifyTypes::MAIL_GET_ATTACH_ERROR);
553
            break;
554
    }
555
}
556
557

3
}  // namespace EAthena