GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/mail2recv.cpp Lines: 3 272 1.1 %
Date: 2017-11-29 Branches: 2 193 1.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2017  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
    if (packetVersion < 20170419)
341
    {
342
        mailWindow->setOpenType(fromInt(msg.readUInt8("open type"),
343
            MailOpenTypeT));
344
        const int cnt = msg.readUInt8("cnt");
345
        isEnd = msg.readUInt8("isEnd") != 0;
346
        for (int f = 0; f < cnt; f ++)
347
        {
348
            MailMessage *const mail = new MailMessage;
349
            mail->id = msg.readInt64("mail id");
350
            mail->read = msg.readUInt8("is read") != 0U ? true : false;
351
            mail->type = static_cast<MailMessageType::Type>(
352
                msg.readUInt8("type"));
353
            mail->sender = msg.readString(24, "sender name");
354
            if (packetVersion < 20170419)
355
            {
356
                mail->time = CAST_S32(cur_time - msg.readInt32("reg time"));
357
                mail->strTime = timeToStr(mail->time);
358
            }
359
            mail->expireTime = msg.readInt32("expire time");
360
            mail->title = msg.readString(-1, "title");
361
            mailWindow->addMail(mail);
362
        }
363
    }
364
    else
365
    {
366
        isEnd = msg.readUInt8("isEnd") != 0;
367
        while (msg.getUnreadLength() > 0)
368
        {
369
            MailMessage *const mail = new MailMessage;
370
            // +++ need save open type into MailMessage
371
            msg.readUInt8("open type");
372
            mail->id = msg.readInt64("mail id");
373
            mail->read = msg.readUInt8("is read") != 0U ? true : false;
374
            mail->type = static_cast<MailMessageType::Type>(
375
                msg.readUInt8("type"));
376
            mail->sender = msg.readString(24, "sender name");
377
            mail->strTime = "-";
378
            mail->expireTime = msg.readInt32("expire time");
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.readInt16("item id");
413
            msg.readUInt8("identify");
414
            msg.readUInt8("damaged");
415
            msg.readUInt8("refine");
416
            for (int d = 0; d < maxCards; d ++)
417
                msg.readUInt16("card");
418
            msg.readInt32("unknown");
419
            msg.readUInt8("type");
420
            msg.readInt32("unknown");
421
            for (int d = 0; d < 5; d ++)
422
            {
423
                msg.readInt16("option index");
424
                msg.readInt16("option value");
425
                msg.readUInt8("option param");
426
            }
427
        }
428
        return;
429
    }
430
431
    mail->money = money;
432
    mail->text = text;
433
    mailWindow->showMessage(mail, itemsCount);
434
435
    Inventory *const inventory = mailViewWindow->getInventory();
436
437
    for (int f = 0; f < itemsCount; f ++)
438
    {
439
        // server may send wrong items count, if items was removed from mail
440
        if (msg.getUnreadLength() == 0)
441
            break;
442
        const int amount = msg.readInt16("amount");
443
        const int itemId = msg.readInt16("item id");
444
        const uint8_t identify = msg.readUInt8("identify");
445
        const Damaged damaged = fromBool(msg.readUInt8("attribute"), Damaged);
446
        const uint8_t refine = msg.readUInt8("refine");
447
        int cards[maxCards];
448
        for (int d = 0; d < maxCards; d ++)
449
            cards[d] = msg.readUInt16("card");
450
        msg.readInt32("unknown");
451
        const ItemTypeT itemType = static_cast<ItemTypeT>(
452
            msg.readUInt8("item type"));
453
        msg.readInt32("unknown");
454
        ItemOptionsList *options = new ItemOptionsList(5);
455
        for (int d = 0; d < 5; d ++)
456
        {
457
            const uint16_t idx = msg.readInt16("option index");
458
            const uint16_t val = msg.readInt16("option value");
459
            msg.readUInt8("option param");
460
            options->add(idx, val);
461
        }
462
463
        const int slot = inventory->addItem(itemId,
464
            itemType,
465
            amount,
466
            refine,
467
            ItemColorManager::getColorFromCards(&cards[0]),
468
            fromBool(identify, Identified),
469
            damaged,
470
            Favorite_false,
471
            Equipm_false,
472
            Equipped_false);
473
        if (slot == -1)
474
        {
475
            delete options;
476
            continue;
477
        }
478
        inventory->setCards(slot, cards, maxCards);
479
        inventory->setOptions(slot, options);
480
        delete options;
481
    }
482
    mailViewWindow->updateItems();
483
}
484
485
void Mail2Recv::processMailDelete(Net::MessageIn &msg)
486
{
487
    msg.readUInt8("open type");
488
    const int64_t mailId = msg.readInt64("mail id");
489
    if (mailWindow == nullptr)
490
    {
491
        reportAlways("Mail window not created.");
492
        return;
493
    }
494
    mailWindow->removeMail(mailId);
495
}
496
497
void Mail2Recv::processRequestMoney(Net::MessageIn &msg)
498
{
499
    const int64_t mailId = msg.readInt64("mail id");
500
    msg.readUInt8("open type");
501
    const int res = msg.readUInt8("result");
502
    switch (res)
503
    {
504
        case 0:
505
            NotifyManager::notify(
506
                NotifyTypes::MAIL_GET_MONEY_OK);
507
            if (mailViewWindow != nullptr)
508
                mailViewWindow->removeMoney(mailId);
509
            break;
510
        case 1:
511
            NotifyManager::notify(
512
                NotifyTypes::MAIL_GET_MONEY_ERROR);
513
            break;
514
        case 2:
515
            NotifyManager::notify(
516
                NotifyTypes::MAIL_GET_MONEY_LIMIT_ERROR);
517
            break;
518
        default:
519
            UNIMPLEMENTEDPACKETFIELD(res);
520
            NotifyManager::notify(
521
                NotifyTypes::MAIL_GET_MONEY_ERROR);
522
            break;
523
    }
524
}
525
526
void Mail2Recv::processRequestItems(Net::MessageIn &msg)
527
{
528
    const int64_t mailId = msg.readInt64("mail id");
529
    msg.readUInt8("open type");
530
    const int res = msg.readUInt8("result");
531
    switch (res)
532
    {
533
        case 0:
534
            NotifyManager::notify(
535
                NotifyTypes::MAIL_GET_ATTACH_OK);
536
            if (mailViewWindow != nullptr)
537
                mailViewWindow->removeItems(mailId);
538
            break;
539
        case 1:
540
            NotifyManager::notify(
541
                NotifyTypes::MAIL_GET_ATTACH_ERROR);
542
            break;
543
        case 2:
544
            NotifyManager::notify(
545
                NotifyTypes::MAIL_GET_ATTACH_FULL_ERROR);
546
            break;
547
        default:
548
            UNIMPLEMENTEDPACKETFIELD(res);
549
            NotifyManager::notify(
550
                NotifyTypes::MAIL_GET_ATTACH_ERROR);
551
            break;
552
    }
553
}
554
555

6
}  // namespace EAthena