root/trunk/src/game/Mail.cpp @ 119

Revision 102, 30.2 kB (checked in by yumileroy, 17 years ago)

[svn] Fixed copyright notices to comply with GPL.

Original author: w12x
Date: 2008-10-23 03:29:52-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
4 * Copyright (C) 2008 Trinity <http://www.trinitycore.org/>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include "Mail.h"
22#include "WorldPacket.h"
23#include "WorldSession.h"
24#include "Opcodes.h"
25#include "Log.h"
26#include "World.h"
27#include "ObjectMgr.h"
28#include "Player.h"
29#include "UpdateMask.h"
30#include "Unit.h"
31#include "Language.h"
32#include "Database/DBCStores.h"
33
34void MailItem::deleteItem( bool inDB )
35{
36    if(item)
37    {
38        if(inDB)
39            CharacterDatabase.PExecute("DELETE FROM item_instance WHERE guid='%u'", item->GetGUIDLow());
40
41        delete item;
42        item=NULL;
43    }
44}
45
46void WorldSession::HandleSendMail(WorldPacket & recv_data )
47{
48    CHECK_PACKET_SIZE(recv_data,8+1+1+1+4+4+1+4+4+8+1);
49
50    uint64 mailbox, unk3;
51    std::string receiver, subject, body;
52    uint32 unk1, unk2, money, COD;
53    uint8 unk4;
54    recv_data >> mailbox;
55    recv_data >> receiver;
56
57    // recheck
58    CHECK_PACKET_SIZE(recv_data, 8+(receiver.size()+1)+1+1+4+4+1+4+4+8+1);
59
60    recv_data >> subject;
61
62    // recheck
63    CHECK_PACKET_SIZE(recv_data, 8+(receiver.size()+1)+(subject.size()+1)+1+4+4+1+4+4+8+1);
64
65    recv_data >> body;
66
67    // recheck
68    CHECK_PACKET_SIZE(recv_data, 8+(receiver.size()+1)+(subject.size()+1)+(body.size()+1)+4+4+1+4+4+8+1);
69
70    recv_data >> unk1;                                      // stationery?
71    recv_data >> unk2;                                      // 0x00000000
72
73    MailItemsInfo mi;
74
75    uint8 items_count;
76    recv_data >> items_count;                               // attached items count
77
78    if(items_count > 12)                                    // client limit
79        return;
80
81    // recheck
82    CHECK_PACKET_SIZE(recv_data, 8+(receiver.size()+1)+(subject.size()+1)+(body.size()+1)+4+4+1+items_count*(1+8)+4+4+8+1);
83
84    if(items_count)
85    {
86        for(uint8 i = 0; i < items_count; ++i)
87        {
88            uint8  item_slot;
89            uint64 item_guid;
90            recv_data >> item_slot;
91            recv_data >> item_guid;
92            mi.AddItem(GUID_LOPART(item_guid), item_slot);
93        }
94    }
95
96    recv_data >> money >> COD;                              // money and cod
97    recv_data >> unk3;                                      // const 0
98    recv_data >> unk4;                                      // const 0
99
100    items_count = mi.size();                                // this is the real size after the duplicates have been removed
101
102    if (receiver.empty())
103        return;
104
105    Player* pl = _player;
106
107    uint64 rc = 0;
108    if(normalizePlayerName(receiver))
109        rc = objmgr.GetPlayerGUIDByName(receiver);
110
111    if (!rc)
112    {
113        sLog.outDetail("Player %u is sending mail to %s (GUID: not existed!) with subject %s and body %s includes %u items, %u copper and %u COD copper with unk1 = %u, unk2 = %u",
114            pl->GetGUIDLow(), receiver.c_str(), subject.c_str(), body.c_str(), items_count, money, COD, unk1, unk2);
115        pl->SendMailResult(0, 0, MAIL_ERR_RECIPIENT_NOT_FOUND);
116        return;
117    }
118
119    sLog.outDetail("Player %u is sending mail to %s (GUID: %u) with subject %s and body %s includes %u items, %u copper and %u COD copper with unk1 = %u, unk2 = %u", pl->GetGUIDLow(), receiver.c_str(), GUID_LOPART(rc), subject.c_str(), body.c_str(), items_count, money, COD, unk1, unk2);
120
121    if(pl->GetGUID() == rc)
122    {
123        pl->SendMailResult(0, 0, MAIL_ERR_CANNOT_SEND_TO_SELF);
124        return;
125    }
126
127    uint32 reqmoney = money + 30;
128    if (items_count)
129        reqmoney = money + (30 * items_count);
130
131    if (pl->GetMoney() < reqmoney)
132    {
133        pl->SendMailResult(0, 0, MAIL_ERR_NOT_ENOUGH_MONEY);
134        return;
135    }
136
137    Player *receive = objmgr.GetPlayer(rc);
138
139    uint32 rc_team = 0;
140    uint8 mails_count = 0;                                  //do not allow to send to one player more than 100 mails
141
142    if(receive)
143    {
144        rc_team = receive->GetTeam();
145        mails_count = receive->GetMailSize();
146    }
147    else
148    {
149        rc_team = objmgr.GetPlayerTeamByGUID(rc);
150        QueryResult* result = CharacterDatabase.PQuery("SELECT COUNT(*) FROM mail WHERE receiver = '%u'", GUID_LOPART(rc));
151        if(result)
152        {
153            Field *fields = result->Fetch();
154            mails_count = fields[0].GetUInt32();
155            delete result;
156        }
157    }
158    //do not allow to have more than 100 mails in mailbox.. mails count is in opcode uint8!!! - so max can be 255..
159    if (mails_count > 100)
160    {
161        pl->SendMailResult(0, 0, MAIL_ERR_INTERNAL_ERROR);
162        return;
163    }
164    // test the receiver's Faction...
165    if (!sWorld.getConfig(CONFIG_ALLOW_TWO_SIDE_INTERACTION_MAIL) && pl->GetTeam() != rc_team && GetSecurity() == SEC_PLAYER)
166    {
167        pl->SendMailResult(0, 0, MAIL_ERR_NOT_YOUR_TEAM);
168        return;
169    }
170
171    if (items_count)
172    {
173        for(MailItemMap::iterator mailItemIter = mi.begin(); mailItemIter != mi.end(); ++mailItemIter)
174        {
175            MailItem& mailItem = mailItemIter->second;
176
177            if(!mailItem.item_guidlow)
178            {
179                pl->SendMailResult(0, 0, MAIL_ERR_INTERNAL_ERROR);
180                return;
181            }
182
183            mailItem.item = pl->GetItemByGuid(MAKE_NEW_GUID(mailItem.item_guidlow, 0, HIGHGUID_ITEM));
184            // prevent sending bag with items (cheat: can be placed in bag after adding equipped empty bag to mail)
185            if(!mailItem.item || !mailItem.item->CanBeTraded())
186            {
187                pl->SendMailResult(0, 0, MAIL_ERR_INTERNAL_ERROR);
188                return;
189            }
190            if (mailItem.item->HasFlag(ITEM_FIELD_FLAGS, ITEM_FLAGS_CONJURED) || mailItem.item->GetUInt32Value(ITEM_FIELD_DURATION))
191            {
192                pl->SendMailResult(0, 0, MAIL_ERR_INTERNAL_ERROR);
193                return;
194            }
195        }
196    }
197    pl->SendMailResult(0, 0, MAIL_OK);
198
199    uint32 itemTextId = 0;
200    if (!body.empty())
201    {
202        itemTextId = objmgr.CreateItemText( body );
203    }
204
205    pl->ModifyMoney( -int32(reqmoney) );
206
207    bool needItemDelay = false;
208
209    if(items_count > 0 || money > 0)
210    {
211        uint32 rc_account = 0;
212        if(receive)
213            rc_account = receive->GetSession()->GetAccountId();
214        else
215            rc_account = objmgr.GetPlayerAccountIdByGUID(rc);
216
217        if (items_count > 0)
218        {
219            for(MailItemMap::iterator mailItemIter = mi.begin(); mailItemIter != mi.end(); ++mailItemIter)
220            {
221                MailItem& mailItem = mailItemIter->second;
222                if(!mailItem.item)
223                    continue;
224
225                mailItem.item_template = mailItem.item ? mailItem.item->GetEntry() : 0;
226
227                if( GetSecurity() > SEC_PLAYER && sWorld.getConfig(CONFIG_GM_LOG_TRADE) )
228                {
229                    sLog.outCommand("GM %s (Account: %u) mail item: %s (Entry: %u Count: %u) to player: %s (Account: %u)",
230                        GetPlayerName(), GetAccountId(), mailItem.item->GetProto()->Name1, mailItem.item->GetEntry(), mailItem.item->GetCount(), receiver.c_str(), rc_account);
231                }
232
233                pl->MoveItemFromInventory(mailItem.item->GetBagSlot(), mailItem.item->GetSlot(), true);
234                CharacterDatabase.BeginTransaction();
235                mailItem.item->DeleteFromInventoryDB();     //deletes item from character's inventory
236                mailItem.item->SaveToDB();                  // recursive and not have transaction guard into self, item not in inventory and can be save standalone
237                // owner in data will set at mail receive and item extracting
238                CharacterDatabase.PExecute("UPDATE item_instance SET owner_guid = '%u' WHERE guid='%u'", GUID_LOPART(rc), mailItem.item->GetGUIDLow());
239                CharacterDatabase.CommitTransaction();
240            }
241
242            // if item send to character at another account, then apply item delivery delay
243            needItemDelay = pl->GetSession()->GetAccountId() != rc_account;
244        }
245
246        if(money > 0 &&  GetSecurity() > SEC_PLAYER && sWorld.getConfig(CONFIG_GM_LOG_TRADE))
247        {
248            sLog.outCommand("GM %s (Account: %u) mail money: %u to player: %s (Account: %u)",
249                GetPlayerName(), GetAccountId(), money, receiver.c_str(), rc_account);
250        }
251    }
252
253    // If theres is an item, there is a one hour delivery delay if sent to another account's character.
254    uint32 deliver_delay = needItemDelay ? sWorld.getConfig(CONFIG_MAIL_DELIVERY_DELAY) : 0;
255
256    // will delete item or place to receiver mail list
257    WorldSession::SendMailTo(receive, MAIL_NORMAL, MAIL_STATIONERY_NORMAL, pl->GetGUIDLow(), GUID_LOPART(rc), subject, itemTextId, &mi, money, COD, MAIL_CHECK_MASK_NONE, deliver_delay);
258
259    CharacterDatabase.BeginTransaction();
260    pl->SaveInventoryAndGoldToDB();
261    CharacterDatabase.CommitTransaction();
262}
263
264//called when mail is read
265void WorldSession::HandleMarkAsRead(WorldPacket & recv_data )
266{
267    CHECK_PACKET_SIZE(recv_data,8+4);
268
269    uint64 mailbox;
270    uint32 mailId;
271    recv_data >> mailbox;
272    recv_data >> mailId;
273    Player *pl = _player;
274    Mail *m = pl->GetMail(mailId);
275    if (m)
276    {
277        if (pl->unReadMails)
278            --pl->unReadMails;
279        m->checked = m->checked | MAIL_CHECK_MASK_READ;
280        // m->expire_time = time(NULL) + (30 * DAY);  // Expire time do not change at reading mail
281        pl->m_mailsUpdated = true;
282        m->state = MAIL_STATE_CHANGED;
283    }
284}
285
286//called when client deletes mail
287void WorldSession::HandleMailDelete(WorldPacket & recv_data )
288{
289    CHECK_PACKET_SIZE(recv_data,8+4);
290
291    uint64 mailbox;
292    uint32 mailId;
293    recv_data >> mailbox;
294    recv_data >> mailId;
295    Player* pl = _player;
296    pl->m_mailsUpdated = true;
297    Mail *m = pl->GetMail(mailId);
298    if(m)
299        m->state = MAIL_STATE_DELETED;
300    pl->SendMailResult(mailId, MAIL_DELETED, 0);
301}
302
303void WorldSession::HandleReturnToSender(WorldPacket & recv_data )
304{
305    CHECK_PACKET_SIZE(recv_data,8+4);
306
307    uint64 mailbox;
308    uint32 mailId;
309    recv_data >> mailbox;
310    recv_data >> mailId;
311    Player *pl = _player;
312    Mail *m = pl->GetMail(mailId);
313    if(!m || m->state == MAIL_STATE_DELETED || m->deliver_time > time(NULL))
314    {
315        pl->SendMailResult(mailId, MAIL_RETURNED_TO_SENDER, MAIL_ERR_INTERNAL_ERROR);
316        return;
317    }
318    //we can return mail now
319    //so firstly delete the old one
320    CharacterDatabase.BeginTransaction();
321    CharacterDatabase.PExecute("DELETE FROM mail WHERE id = '%u'", mailId);
322                                                            // needed?
323    CharacterDatabase.PExecute("DELETE FROM mail_items WHERE mail_id = '%u'", mailId);
324    CharacterDatabase.CommitTransaction();
325    pl->RemoveMail(mailId);
326
327    MailItemsInfo mi;
328
329    if(m->HasItems())
330    {
331        for(std::vector<MailItemInfo>::iterator itr2 = m->items.begin(); itr2 != m->items.end(); ++itr2)
332        {
333            Item *item = pl->GetMItem(itr2->item_guid);
334            if(item)
335                mi.AddItem(item->GetGUIDLow(), item->GetEntry(), item);
336            else
337            {
338                //WTF?
339            }
340
341            pl->RemoveMItem(itr2->item_guid);
342        }
343    }
344
345    SendReturnToSender(MAIL_NORMAL, GetAccountId(), m->receiver, m->sender, m->subject, m->itemTextId, &mi, m->money, 0, m->mailTemplateId);
346
347    delete m;                                               //we can deallocate old mail
348    pl->SendMailResult(mailId, MAIL_RETURNED_TO_SENDER, 0);
349}
350
351void WorldSession::SendReturnToSender(uint8 messageType, uint32 sender_acc, uint32 sender_guid, uint32 receiver_guid, std::string subject, uint32 itemTextId, MailItemsInfo *mi, uint32 money, uint32 COD, uint16 mailTemplateId )
352{
353    if(messageType != MAIL_NORMAL)                          // return only to players
354    {
355        mi->deleteIncludedItems(true);
356        return;
357    }
358
359    Player *receiver = objmgr.GetPlayer(MAKE_NEW_GUID(receiver_guid, 0, HIGHGUID_PLAYER));
360
361    uint32 rc_account = 0;
362    if(!receiver)
363        rc_account = objmgr.GetPlayerAccountIdByGUID(MAKE_NEW_GUID(receiver_guid, 0, HIGHGUID_PLAYER));
364
365    if(!receiver && !rc_account)                            // sender not exist
366    {
367        mi->deleteIncludedItems(true);
368        return;
369    }
370
371    // preper mail and send in other case
372    bool needItemDelay = false;
373
374    if(mi && !mi->empty())
375    {
376        // if item send to character at another account, then apply item delivery delay
377        needItemDelay = sender_acc != rc_account;
378
379        // set owner to new receiver (to prevent delete item with sender char deleting)
380        CharacterDatabase.BeginTransaction();
381        for(MailItemMap::iterator mailItemIter = mi->begin(); mailItemIter != mi->end(); ++mailItemIter)
382        {
383            MailItem& mailItem = mailItemIter->second;
384            mailItem.item->SaveToDB();                  // item not in inventory and can be save standalone
385            // owner in data will set at mail receive and item extracting
386            CharacterDatabase.PExecute("UPDATE item_instance SET owner_guid = '%u' WHERE guid='%u'", receiver_guid, mailItem.item->GetGUIDLow());
387        }
388        CharacterDatabase.CommitTransaction();
389    }
390
391    // If theres is an item, there is a one hour delivery delay.
392    uint32 deliver_delay = needItemDelay ? sWorld.getConfig(CONFIG_MAIL_DELIVERY_DELAY) : 0;
393
394    // will delete item or place to receiver mail list
395    WorldSession::SendMailTo(receiver, MAIL_NORMAL, MAIL_STATIONERY_NORMAL, sender_guid, receiver_guid, subject, itemTextId, mi, money, 0, MAIL_CHECK_MASK_RETURNED,deliver_delay,mailTemplateId);
396}
397
398//called when player takes item attached in mail
399void WorldSession::HandleTakeItem(WorldPacket & recv_data )
400{
401    CHECK_PACKET_SIZE(recv_data,8+4+4);
402
403    uint64 mailbox;
404    uint32 mailId;
405    uint32 itemId;
406    recv_data >> mailbox;
407    recv_data >> mailId;
408    recv_data >> itemId;                                    // item guid low?
409    Player* pl = _player;
410
411    Mail* m = pl->GetMail(mailId);
412    if(!m || m->state == MAIL_STATE_DELETED || m->deliver_time > time(NULL))
413    {
414        pl->SendMailResult(mailId, MAIL_ITEM_TAKEN, MAIL_ERR_INTERNAL_ERROR);
415        return;
416    }
417
418    // prevent cheating with skip client money check
419    if(pl->GetMoney() < m->COD)
420    {
421        pl->SendMailResult(mailId, MAIL_ITEM_TAKEN, MAIL_ERR_NOT_ENOUGH_MONEY);
422        return;
423    }
424
425    Item *it = pl->GetMItem(itemId);
426
427    ItemPosCountVec dest;
428    uint8 msg = _player->CanStoreItem( NULL_BAG, NULL_SLOT, dest, it, false );
429    if( msg == EQUIP_ERR_OK )
430    {
431        m->RemoveItem(itemId);
432        m->removedItems.push_back(itemId);
433
434        if (m->COD > 0)                                     //if there is COD, take COD money from player and send them to sender by mail
435        {
436            uint64 sender_guid = MAKE_NEW_GUID(m->sender, 0, HIGHGUID_PLAYER);
437            Player *receive = objmgr.GetPlayer(sender_guid);
438
439            uint32 sender_accId = 0;
440
441            if( GetSecurity() > SEC_PLAYER && sWorld.getConfig(CONFIG_GM_LOG_TRADE) )
442            {
443                std::string sender_name;
444                if(receive)
445                {
446                    sender_accId = receive->GetSession()->GetAccountId();
447                    sender_name = receive->GetName();
448                }
449                else
450                {
451                    // can be calculated early
452                    sender_accId = objmgr.GetPlayerAccountIdByGUID(sender_guid);
453
454                    if(!objmgr.GetPlayerNameByGUID(sender_guid,sender_name))
455                        sender_name = objmgr.GetTrinityStringForDBCLocale(LANG_UNKNOWN);
456                }
457                sLog.outCommand("GM %s (Account: %u) receive mail item: %s (Entry: %u Count: %u) and send COD money: %u to player: %s (Account: %u)",
458                    GetPlayerName(),GetAccountId(),it->GetProto()->Name1,it->GetEntry(),it->GetCount(),m->COD,sender_name.c_str(),sender_accId);
459            }
460            else if(!receive)
461                sender_accId = objmgr.GetPlayerAccountIdByGUID(sender_guid);
462
463            // check player existanse
464            if(receive || sender_accId)
465            {
466                WorldSession::SendMailTo(receive, MAIL_NORMAL, MAIL_STATIONERY_NORMAL, m->receiver, m->sender, m->subject, 0, NULL, m->COD, 0, MAIL_CHECK_MASK_COD_PAYMENT);
467            }
468
469            pl->ModifyMoney( -int32(m->COD) );
470        }
471        m->COD = 0;
472        m->state = MAIL_STATE_CHANGED;
473        pl->m_mailsUpdated = true;
474        pl->RemoveMItem(it->GetGUIDLow());
475
476        uint32 count = it->GetCount();                      // save counts before store and possible merge with deleting
477        pl->MoveItemToInventory(dest,it,true);
478
479        CharacterDatabase.BeginTransaction();
480        pl->SaveInventoryAndGoldToDB();
481        pl->_SaveMail();
482        CharacterDatabase.CommitTransaction();
483
484        pl->SendMailResult(mailId, MAIL_ITEM_TAKEN, MAIL_OK, 0, itemId, count);
485    }
486    else
487        pl->SendMailResult(mailId, MAIL_ITEM_TAKEN, MAIL_ERR_BAG_FULL, msg);
488}
489
490void WorldSession::HandleTakeMoney(WorldPacket & recv_data )
491{
492    CHECK_PACKET_SIZE(recv_data,8+4);
493
494    uint64 mailbox;
495    uint32 mailId;
496    recv_data >> mailbox;
497    recv_data >> mailId;
498    Player *pl = _player;
499
500    Mail* m = pl->GetMail(mailId);
501    if(!m || m->state == MAIL_STATE_DELETED || m->deliver_time > time(NULL))
502    {
503        pl->SendMailResult(mailId, MAIL_MONEY_TAKEN, MAIL_ERR_INTERNAL_ERROR);
504        return;
505    }
506
507    pl->SendMailResult(mailId, MAIL_MONEY_TAKEN, 0);
508
509    pl->ModifyMoney(m->money);
510    m->money = 0;
511    m->state = MAIL_STATE_CHANGED;
512    pl->m_mailsUpdated = true;
513
514    // save money and mail to prevent cheating
515    CharacterDatabase.BeginTransaction();
516    pl->SetUInt32ValueInDB(PLAYER_FIELD_COINAGE,pl->GetMoney(),pl->GetGUID());
517    pl->_SaveMail();
518    CharacterDatabase.CommitTransaction();
519}
520
521//called when player lists his received mails
522void WorldSession::HandleGetMail(WorldPacket & recv_data )
523{
524    CHECK_PACKET_SIZE(recv_data,8);
525
526    uint64 mailbox;
527    recv_data >> mailbox;
528
529    //GameObject* obj = ObjectAccessor::GetGameObject(_player, mailbox);
530    //if(!obj || !obj->IsMailBox())
531    //    return;
532
533    Player* pl = _player;
534
535    //load players mails, and mailed items
536    if(!pl->m_mailsLoaded)
537        pl ->_LoadMail();
538
539    // client can't work with packets > max int16 value
540    const uint32 maxPacketSize = 32767;
541
542    uint32 mails_count = 0;                                 // real send to client mails amount
543
544    WorldPacket data(SMSG_MAIL_LIST_RESULT, (200));         // guess size
545    data << uint8(0);                                       // mail's count
546    time_t cur_time = time(NULL);
547
548    for(PlayerMails::iterator itr = pl->GetmailBegin(); itr != pl->GetmailEnd(); ++itr)
549    {
550        // skip deleted or not delivered (deliver delay not expired) mails
551        if ((*itr)->state == MAIL_STATE_DELETED || cur_time < (*itr)->deliver_time)
552            continue;
553
554        uint8 item_count = (*itr)->items.size();            // max count is MAX_MAIL_ITEMS (12)
555
556        size_t next_mail_size = 2+4+1+8+4*8+((*itr)->subject.size()+1)+1+item_count*(1+4+4+6*3*4+4+4+1+4+4+4);
557
558        if(data.wpos()+next_mail_size > maxPacketSize)
559            break;
560
561        data << (uint16) 0x0040;                            // unknown 2.3.0, different values
562        data << (uint32) (*itr)->messageID;                 // Message ID
563        data << (uint8) (*itr)->messageType;                // Message Type
564
565        switch((*itr)->messageType)
566        {
567            case MAIL_NORMAL:                               // sender guid
568                data << uint64(MAKE_NEW_GUID((*itr)->sender, 0, HIGHGUID_PLAYER));
569                break;
570            case MAIL_CREATURE:
571            case MAIL_GAMEOBJECT:
572            case MAIL_AUCTION:
573                data << (uint32) (*itr)->sender;            // creature/gameobject entry, auction id
574                break;
575            case MAIL_ITEM:                                 // item entry (?) sender = "Unknown", NYI
576                break;
577        }
578
579        data << (uint32) (*itr)->COD;                       // COD
580        data << (uint32) (*itr)->itemTextId;                // sure about this
581        data << (uint32) 0;                                 // unknown
582        data << (uint32) (*itr)->stationery;                // stationery (Stationery.dbc)
583        data << (uint32) (*itr)->money;                     // Gold
584        data << (uint32) 0x04;                              // unknown, 0x4 - auction, 0x10 - normal
585                                                            // Time
586        data << (float)  ((*itr)->expire_time-time(NULL))/DAY;
587        data << (uint32) (*itr)->mailTemplateId;            // mail template (MailTemplate.dbc)
588        data << (*itr)->subject;                            // Subject string - once 00, when mail type = 3
589
590        data << (uint8) item_count;
591
592        for(uint8 i = 0; i < item_count; ++i)
593        {
594            Item *item = pl->GetMItem((*itr)->items[i].item_guid);
595            // item index (0-6?)
596            data << (uint8)  i;
597            // item guid low?
598            data << (uint32) (item ? item->GetGUIDLow() : 0);
599            // entry
600            data << (uint32) (item ? item->GetEntry() : 0);
601            for(uint8 j = 0; j < 6; ++j)
602            {
603                // unsure
604                data << (uint32) (item ? item->GetEnchantmentCharges((EnchantmentSlot)j) : 0);
605                // unsure
606                data << (uint32) (item ? item->GetEnchantmentDuration((EnchantmentSlot)j) : 0);
607                // unsure
608                data << (uint32) (item ? item->GetEnchantmentId((EnchantmentSlot)j) : 0);
609            }
610            // can be negative
611            data << (uint32) (item ? item->GetItemRandomPropertyId() : 0);
612            // unk
613            data << (uint32) (item ? item->GetItemSuffixFactor() : 0);
614            // stack count
615            data << (uint8)  (item ? item->GetCount() : 0);
616            // charges
617            data << (uint32) (item ? item->GetSpellCharges() : 0);
618            // durability
619            data << (uint32) (item ? item->GetUInt32Value(ITEM_FIELD_MAXDURABILITY) : 0);
620            // durability
621            data << (uint32) (item ? item->GetUInt32Value(ITEM_FIELD_DURABILITY) : 0);
622        }
623
624        mails_count += 1;
625    }
626
627    data.put<uint8>(0, mails_count);                        // set real send mails to client
628    SendPacket(&data);
629
630    // recalculate m_nextMailDelivereTime and unReadMails
631    _player->UpdateNextMailTimeAndUnreads();
632}
633
634///this function is called when client needs mail message body, or when player clicks on item which has ITEM_FIELD_ITEM_TEXT_ID > 0
635void WorldSession::HandleItemTextQuery(WorldPacket & recv_data )
636{
637    CHECK_PACKET_SIZE(recv_data,4+4+4);
638
639    uint32 itemTextId;
640    uint32 mailId;                                          //this value can be item id in bag, but it is also mail id
641    uint32 unk;                                             //maybe something like state - 0x70000000
642
643    recv_data >> itemTextId >> mailId >> unk;
644
645    //some check needed, if player has item with guid mailId, or has mail with id mailId
646
647    sLog.outDebug("CMSG_ITEM_TEXT_QUERY itemguid: %u, mailId: %u, unk: %u", itemTextId, mailId, unk);
648
649    WorldPacket data(SMSG_ITEM_TEXT_QUERY_RESPONSE, (4+10));// guess size
650    data << itemTextId;
651    data << objmgr.GetItemText( itemTextId );
652    SendPacket(&data);
653}
654
655//used when player copies mail body to his inventory
656void WorldSession::HandleMailCreateTextItem(WorldPacket & recv_data )
657{
658    CHECK_PACKET_SIZE(recv_data,8+4);
659
660    uint64 mailbox;
661    uint32 mailId;
662
663    recv_data >> mailbox >> mailId;
664
665    Player *pl = _player;
666
667    Mail* m = pl->GetMail(mailId);
668    if(!m || !m->itemTextId || m->state == MAIL_STATE_DELETED || m->deliver_time > time(NULL))
669    {
670        pl->SendMailResult(mailId, MAIL_MADE_PERMANENT, MAIL_ERR_INTERNAL_ERROR);
671        return;
672    }
673
674    Item *bodyItem = new Item;                              // This is not bag and then can be used new Item.
675    if(!bodyItem->Create(objmgr.GenerateLowGuid(HIGHGUID_ITEM), MAIL_BODY_ITEM_TEMPLATE, pl))
676    {
677        delete bodyItem;
678        return;
679    }
680
681    bodyItem->SetUInt32Value( ITEM_FIELD_ITEM_TEXT_ID , m->itemTextId );
682    bodyItem->SetUInt32Value( ITEM_FIELD_CREATOR, m->sender);
683
684    sLog.outDetail("HandleMailCreateTextItem mailid=%u",mailId);
685
686    ItemPosCountVec dest;
687    uint8 msg = _player->CanStoreItem( NULL_BAG, NULL_SLOT, dest, bodyItem, false );
688    if( msg == EQUIP_ERR_OK )
689    {
690        m->itemTextId = 0;
691        m->state = MAIL_STATE_CHANGED;
692        pl->m_mailsUpdated = true;
693
694        pl->StoreItem(dest, bodyItem, true);
695        //bodyItem->SetState(ITEM_NEW, pl); is set automatically
696        pl->SendMailResult(mailId, MAIL_MADE_PERMANENT, 0);
697    }
698    else
699    {
700        pl->SendMailResult(mailId, MAIL_MADE_PERMANENT, MAIL_ERR_BAG_FULL, msg);
701        delete bodyItem;
702    }
703}
704
705//TODO Fix me! ... this void has probably bad condition, but good data are sent
706void WorldSession::HandleMsgQueryNextMailtime(WorldPacket & /*recv_data*/ )
707{
708    WorldPacket data(MSG_QUERY_NEXT_MAIL_TIME, 8);
709
710    if(!_player->m_mailsLoaded)
711        _player->_LoadMail();
712
713    if( _player->unReadMails > 0 )
714    {
715        data << (uint32) 0;                                 // float
716        data << (uint32) 0;                                 // count
717        uint32 count = 0;
718        for(PlayerMails::iterator itr = _player->GetmailBegin(); itr != _player->GetmailEnd(); ++itr)
719        {
720            Mail *m = (*itr);
721            // not checked yet, already must be delivered
722            if((m->checked & MAIL_CHECK_MASK_READ)==0 && (m->deliver_time <= time(NULL)))
723            {
724                ++count;
725
726                if(count > 2)
727                {
728                    count = 2;
729                    break;
730                }
731
732                data << (uint64) m->sender;                 // sender guid
733
734                switch(m->messageType)
735                {
736                    case MAIL_AUCTION:
737                        data << (uint32) 2;
738                        data << (uint32) 2;
739                        data << (uint32) m->stationery;
740                        break;
741                    default:
742                        data << (uint32) 0;
743                        data << (uint32) 0;
744                        data << (uint32) m->stationery;
745                        break;
746                }
747                data << (uint32) 0xC6000000;                // float unk, time or something
748            }
749        }
750        data.put<uint32>(4, count);
751    }
752    else
753    {
754        data << (uint32) 0xC7A8C000;
755        data << (uint32) 0x00000000;
756    }
757    SendPacket(&data);
758}
759
760void WorldSession::SendMailTo(Player* receiver, uint8 messageType, uint8 stationery, uint32 sender_guidlow_or_entry, uint32 receiver_guidlow, std::string subject, uint32 itemTextId, MailItemsInfo* mi, uint32 money, uint32 COD, uint32 checked, uint32 deliver_delay, uint16 mailTemplateId)
761{
762    uint32 mailId = objmgr.GenerateMailID();
763
764    time_t deliver_time = time(NULL) + deliver_delay;
765
766    //expire time if COD 3 days, if no COD 30 days, if auction sale pending 1 hour
767    uint32 expire_delay;
768    if(messageType == MAIL_AUCTION && !mi && !money)        // auction mail without any items and money
769        expire_delay = HOUR;
770    else
771        expire_delay = (COD > 0) ? 3*DAY : 30*DAY;
772
773    time_t expire_time = deliver_time + expire_delay;
774
775    if(mailTemplateId && !sMailTemplateStore.LookupEntry(mailTemplateId))
776    {
777        sLog.outError( "WorldSession::SendMailTo - Mail have not existed MailTemplateId (%u), remove at send", mailTemplateId);
778        mailTemplateId = 0;
779    }
780
781    if(receiver)
782    {
783        receiver->AddNewMailDeliverTime(deliver_time);
784
785        if ( receiver->IsMailsLoaded() )
786        {
787            Mail * m = new Mail;
788            m->messageID = mailId;
789            m->messageType = messageType;
790            m->stationery = stationery;
791            m->mailTemplateId = mailTemplateId;
792            m->sender = sender_guidlow_or_entry;
793            m->receiver = receiver->GetGUIDLow();
794            m->subject = subject;
795            m->itemTextId = itemTextId;
796
797            if(mi)
798                m->AddAllItems(*mi);
799
800            m->expire_time = expire_time;
801            m->deliver_time = deliver_time;
802            m->money = money;
803            m->COD = COD;
804            m->checked = checked;
805            m->state = MAIL_STATE_UNCHANGED;
806
807            receiver->AddMail(m);                           //to insert new mail to beginning of maillist
808
809            if(mi)
810            {
811                for(MailItemMap::iterator mailItemIter = mi->begin(); mailItemIter != mi->end(); ++mailItemIter)
812                {
813                    MailItem& mailItem = mailItemIter->second;
814                    if(mailItem.item)
815                        receiver->AddMItem(mailItem.item);
816                }
817            }
818        }
819        else if(mi)
820            mi->deleteIncludedItems();
821    }
822    else if(mi)
823        mi->deleteIncludedItems();
824
825    CharacterDatabase.BeginTransaction();
826    CharacterDatabase.escape_string(subject);
827    CharacterDatabase.PExecute("INSERT INTO mail (id,messageType,stationery,mailTemplateId,sender,receiver,subject,itemTextId,has_items,expire_time,deliver_time,money,cod,checked) "
828        "VALUES ('%u', '%u', '%u', '%u', '%u', '%u', '%s', '%u', '%u', '" I64FMTD "','" I64FMTD "', '%u', '%u', '%d')",
829        mailId, messageType, stationery, mailTemplateId, sender_guidlow_or_entry, receiver_guidlow, subject.c_str(), itemTextId, (mi && !mi->empty() ? 1 : 0), (uint64)expire_time, (uint64)deliver_time, money, COD, checked);
830
831    if(mi)
832    {
833        for(MailItemMap::const_iterator mailItemIter = mi->begin(); mailItemIter != mi->end(); ++mailItemIter)
834        {
835            MailItem const& mailItem = mailItemIter->second;
836            CharacterDatabase.PExecute("INSERT INTO mail_items (mail_id,item_guid,item_template,receiver) VALUES ('%u', '%u', '%u','%u')", mailId, mailItem.item_guidlow, mailItem.item_template,receiver_guidlow);
837        }
838    }
839    CharacterDatabase.CommitTransaction();
840}
Note: See TracBrowser for help on using the browser.