GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/mail2recv.cpp Lines: 3 274 1.1 %
Date: 2018-05-19 03:07:18 Branches: 2 193 1.0 %

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

6
}  // namespace EAthena