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

Revision 229, 30.4 kB (checked in by yumileroy, 17 years ago)

[svn] *** Source: MaNGOS ***
* Fixed build extractor at Windows Vista. Author: Vladimir
* Fixed comment text and code indentifiers spelling. Author: Vladimir & Paradox.
* Access cached member lists in guild handlers instead of querying the DB. Author: Hunuza
* Small fixes in send/received packet and simple code cleanup also. Author: Vladimir
* Not output error at loading empty character_ticket table. Author: Vladimir
* Not reset display model at shapeshift aura remove if it not set at apply. Author: Arthorius
* Applied props to few files.

Original author: visagalis
Date: 2008-11-14 16:28:45-06: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
191            if (mailItem.item->HasFlag(ITEM_FIELD_FLAGS, ITEM_FLAGS_CONJURED) || mailItem.item->GetUInt32Value(ITEM_FIELD_DURATION))
192            {
193                pl->SendMailResult(0, 0, MAIL_ERR_INTERNAL_ERROR);
194                return;
195            }
196
197                        if(COD && mailItem.item->HasFlag(ITEM_FIELD_FLAGS, ITEM_FLAGS_WRAPPED))
198                        {
199                                pl->SendMailResult(0, 0, MAIL_ERR_CANT_SEND_WRAPPED_COD);
200                                return;
201                        }
202        }
203    }
204    pl->SendMailResult(0, 0, MAIL_OK);
205
206    uint32 itemTextId = 0;
207    if (!body.empty())
208    {
209        itemTextId = objmgr.CreateItemText( body );
210    }
211
212    pl->ModifyMoney( -int32(reqmoney) );
213
214    bool needItemDelay = false;
215
216    if(items_count > 0 || money > 0)
217    {
218        uint32 rc_account = 0;
219        if(receive)
220            rc_account = receive->GetSession()->GetAccountId();
221        else
222            rc_account = objmgr.GetPlayerAccountIdByGUID(rc);
223
224        if (items_count > 0)
225        {
226            for(MailItemMap::iterator mailItemIter = mi.begin(); mailItemIter != mi.end(); ++mailItemIter)
227            {
228                MailItem& mailItem = mailItemIter->second;
229                if(!mailItem.item)
230                    continue;
231
232                mailItem.item_template = mailItem.item ? mailItem.item->GetEntry() : 0;
233
234                if( GetSecurity() > SEC_PLAYER && sWorld.getConfig(CONFIG_GM_LOG_TRADE) )
235                {
236                    sLog.outCommand("GM %s (Account: %u) mail item: %s (Entry: %u Count: %u) to player: %s (Account: %u)",
237                        GetPlayerName(), GetAccountId(), mailItem.item->GetProto()->Name1, mailItem.item->GetEntry(), mailItem.item->GetCount(), receiver.c_str(), rc_account);
238                }
239
240                pl->MoveItemFromInventory(mailItem.item->GetBagSlot(), mailItem.item->GetSlot(), true);
241                CharacterDatabase.BeginTransaction();
242                mailItem.item->DeleteFromInventoryDB();     //deletes item from character's inventory
243                mailItem.item->SaveToDB();                  // recursive and not have transaction guard into self, item not in inventory and can be save standalone
244                // owner in data will set at mail receive and item extracting
245                CharacterDatabase.PExecute("UPDATE item_instance SET owner_guid = '%u' WHERE guid='%u'", GUID_LOPART(rc), mailItem.item->GetGUIDLow());
246                CharacterDatabase.CommitTransaction();
247            }
248
249            // if item send to character at another account, then apply item delivery delay
250            needItemDelay = pl->GetSession()->GetAccountId() != rc_account;
251        }
252
253        if(money > 0 &&  GetSecurity() > SEC_PLAYER && sWorld.getConfig(CONFIG_GM_LOG_TRADE))
254        {
255            sLog.outCommand("GM %s (Account: %u) mail money: %u to player: %s (Account: %u)",
256                GetPlayerName(), GetAccountId(), money, receiver.c_str(), rc_account);
257        }
258    }
259
260    // If theres is an item, there is a one hour delivery delay if sent to another account's character.
261    uint32 deliver_delay = needItemDelay ? sWorld.getConfig(CONFIG_MAIL_DELIVERY_DELAY) : 0;
262
263    // will delete item or place to receiver mail list
264    WorldSession::SendMailTo(receive, MAIL_NORMAL, MAIL_STATIONERY_NORMAL, pl->GetGUIDLow(), GUID_LOPART(rc), subject, itemTextId, &mi, money, COD, MAIL_CHECK_MASK_NONE, deliver_delay);
265
266    CharacterDatabase.BeginTransaction();
267    pl->SaveInventoryAndGoldToDB();
268    CharacterDatabase.CommitTransaction();
269}
270
271//called when mail is read
272void WorldSession::HandleMarkAsRead(WorldPacket & recv_data )
273{
274    CHECK_PACKET_SIZE(recv_data,8+4);
275
276    uint64 mailbox;
277    uint32 mailId;
278    recv_data >> mailbox;
279    recv_data >> mailId;
280    Player *pl = _player;
281    Mail *m = pl->GetMail(mailId);
282    if (m)
283    {
284        if (pl->unReadMails)
285            --pl->unReadMails;
286        m->checked = m->checked | MAIL_CHECK_MASK_READ;
287        // m->expire_time = time(NULL) + (30 * DAY);  // Expire time do not change at reading mail
288        pl->m_mailsUpdated = true;
289        m->state = MAIL_STATE_CHANGED;
290    }
291}
292
293//called when client deletes mail
294void WorldSession::HandleMailDelete(WorldPacket & recv_data )
295{
296    CHECK_PACKET_SIZE(recv_data,8+4);
297
298    uint64 mailbox;
299    uint32 mailId;
300    recv_data >> mailbox;
301    recv_data >> mailId;
302    Player* pl = _player;
303    pl->m_mailsUpdated = true;
304    Mail *m = pl->GetMail(mailId);
305    if(m)
306        m->state = MAIL_STATE_DELETED;
307    pl->SendMailResult(mailId, MAIL_DELETED, 0);
308}
309
310void WorldSession::HandleReturnToSender(WorldPacket & recv_data )
311{
312    CHECK_PACKET_SIZE(recv_data,8+4);
313
314    uint64 mailbox;
315    uint32 mailId;
316    recv_data >> mailbox;
317    recv_data >> mailId;
318    Player *pl = _player;
319    Mail *m = pl->GetMail(mailId);
320    if(!m || m->state == MAIL_STATE_DELETED || m->deliver_time > time(NULL))
321    {
322        pl->SendMailResult(mailId, MAIL_RETURNED_TO_SENDER, MAIL_ERR_INTERNAL_ERROR);
323        return;
324    }
325    //we can return mail now
326    //so firstly delete the old one
327    CharacterDatabase.BeginTransaction();
328    CharacterDatabase.PExecute("DELETE FROM mail WHERE id = '%u'", mailId);
329                                                            // needed?
330    CharacterDatabase.PExecute("DELETE FROM mail_items WHERE mail_id = '%u'", mailId);
331    CharacterDatabase.CommitTransaction();
332    pl->RemoveMail(mailId);
333
334    MailItemsInfo mi;
335
336    if(m->HasItems())
337    {
338        for(std::vector<MailItemInfo>::iterator itr2 = m->items.begin(); itr2 != m->items.end(); ++itr2)
339        {
340            Item *item = pl->GetMItem(itr2->item_guid);
341            if(item)
342                mi.AddItem(item->GetGUIDLow(), item->GetEntry(), item);
343            else
344            {
345                //WTF?
346            }
347
348            pl->RemoveMItem(itr2->item_guid);
349        }
350    }
351
352    SendReturnToSender(MAIL_NORMAL, GetAccountId(), m->receiver, m->sender, m->subject, m->itemTextId, &mi, m->money, 0, m->mailTemplateId);
353
354    delete m;                                               //we can deallocate old mail
355    pl->SendMailResult(mailId, MAIL_RETURNED_TO_SENDER, 0);
356}
357
358void 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 )
359{
360    if(messageType != MAIL_NORMAL)                          // return only to players
361    {
362        mi->deleteIncludedItems(true);
363        return;
364    }
365
366    Player *receiver = objmgr.GetPlayer(MAKE_NEW_GUID(receiver_guid, 0, HIGHGUID_PLAYER));
367
368    uint32 rc_account = 0;
369    if(!receiver)
370        rc_account = objmgr.GetPlayerAccountIdByGUID(MAKE_NEW_GUID(receiver_guid, 0, HIGHGUID_PLAYER));
371
372    if(!receiver && !rc_account)                            // sender not exist
373    {
374        mi->deleteIncludedItems(true);
375        return;
376    }
377
378    // prepare mail and send in other case
379    bool needItemDelay = false;
380
381    if(mi && !mi->empty())
382    {
383        // if item send to character at another account, then apply item delivery delay
384        needItemDelay = sender_acc != rc_account;
385
386        // set owner to new receiver (to prevent delete item with sender char deleting)
387        CharacterDatabase.BeginTransaction();
388        for(MailItemMap::iterator mailItemIter = mi->begin(); mailItemIter != mi->end(); ++mailItemIter)
389        {
390            MailItem& mailItem = mailItemIter->second;
391            mailItem.item->SaveToDB();                  // item not in inventory and can be save standalone
392            // owner in data will set at mail receive and item extracting
393            CharacterDatabase.PExecute("UPDATE item_instance SET owner_guid = '%u' WHERE guid='%u'", receiver_guid, mailItem.item->GetGUIDLow());
394        }
395        CharacterDatabase.CommitTransaction();
396    }
397
398    // If theres is an item, there is a one hour delivery delay.
399    uint32 deliver_delay = needItemDelay ? sWorld.getConfig(CONFIG_MAIL_DELIVERY_DELAY) : 0;
400
401    // will delete item or place to receiver mail list
402    WorldSession::SendMailTo(receiver, MAIL_NORMAL, MAIL_STATIONERY_NORMAL, sender_guid, receiver_guid, subject, itemTextId, mi, money, 0, MAIL_CHECK_MASK_RETURNED,deliver_delay,mailTemplateId);
403}
404
405//called when player takes item attached in mail
406void WorldSession::HandleTakeItem(WorldPacket & recv_data )
407{
408    CHECK_PACKET_SIZE(recv_data,8+4+4);
409
410    uint64 mailbox;
411    uint32 mailId;
412    uint32 itemId;
413    recv_data >> mailbox;
414    recv_data >> mailId;
415    recv_data >> itemId;                                    // item guid low?
416    Player* pl = _player;
417
418    Mail* m = pl->GetMail(mailId);
419    if(!m || m->state == MAIL_STATE_DELETED || m->deliver_time > time(NULL))
420    {
421        pl->SendMailResult(mailId, MAIL_ITEM_TAKEN, MAIL_ERR_INTERNAL_ERROR);
422        return;
423    }
424
425    // prevent cheating with skip client money check
426    if(pl->GetMoney() < m->COD)
427    {
428        pl->SendMailResult(mailId, MAIL_ITEM_TAKEN, MAIL_ERR_NOT_ENOUGH_MONEY);
429        return;
430    }
431
432    Item *it = pl->GetMItem(itemId);
433
434    ItemPosCountVec dest;
435    uint8 msg = _player->CanStoreItem( NULL_BAG, NULL_SLOT, dest, it, false );
436    if( msg == EQUIP_ERR_OK )
437    {
438        m->RemoveItem(itemId);
439        m->removedItems.push_back(itemId);
440
441        if (m->COD > 0)                                     //if there is COD, take COD money from player and send them to sender by mail
442        {
443            uint64 sender_guid = MAKE_NEW_GUID(m->sender, 0, HIGHGUID_PLAYER);
444            Player *receive = objmgr.GetPlayer(sender_guid);
445
446            uint32 sender_accId = 0;
447
448            if( GetSecurity() > SEC_PLAYER && sWorld.getConfig(CONFIG_GM_LOG_TRADE) )
449            {
450                std::string sender_name;
451                if(receive)
452                {
453                    sender_accId = receive->GetSession()->GetAccountId();
454                    sender_name = receive->GetName();
455                }
456                else
457                {
458                    // can be calculated early
459                    sender_accId = objmgr.GetPlayerAccountIdByGUID(sender_guid);
460
461                    if(!objmgr.GetPlayerNameByGUID(sender_guid,sender_name))
462                        sender_name = objmgr.GetTrinityStringForDBCLocale(LANG_UNKNOWN);
463                }
464                sLog.outCommand("GM %s (Account: %u) receive mail item: %s (Entry: %u Count: %u) and send COD money: %u to player: %s (Account: %u)",
465                    GetPlayerName(),GetAccountId(),it->GetProto()->Name1,it->GetEntry(),it->GetCount(),m->COD,sender_name.c_str(),sender_accId);
466            }
467            else if(!receive)
468                sender_accId = objmgr.GetPlayerAccountIdByGUID(sender_guid);
469
470            // check player existence
471            if(receive || sender_accId)
472            {
473                WorldSession::SendMailTo(receive, MAIL_NORMAL, MAIL_STATIONERY_NORMAL, m->receiver, m->sender, m->subject, 0, NULL, m->COD, 0, MAIL_CHECK_MASK_COD_PAYMENT);
474            }
475
476            pl->ModifyMoney( -int32(m->COD) );
477        }
478        m->COD = 0;
479        m->state = MAIL_STATE_CHANGED;
480        pl->m_mailsUpdated = true;
481        pl->RemoveMItem(it->GetGUIDLow());
482
483        uint32 count = it->GetCount();                      // save counts before store and possible merge with deleting
484        pl->MoveItemToInventory(dest,it,true);
485
486        CharacterDatabase.BeginTransaction();
487        pl->SaveInventoryAndGoldToDB();
488        pl->_SaveMail();
489        CharacterDatabase.CommitTransaction();
490
491        pl->SendMailResult(mailId, MAIL_ITEM_TAKEN, MAIL_OK, 0, itemId, count);
492    }
493    else
494        pl->SendMailResult(mailId, MAIL_ITEM_TAKEN, MAIL_ERR_BAG_FULL, msg);
495}
496
497void WorldSession::HandleTakeMoney(WorldPacket & recv_data )
498{
499    CHECK_PACKET_SIZE(recv_data,8+4);
500
501    uint64 mailbox;
502    uint32 mailId;
503    recv_data >> mailbox;
504    recv_data >> mailId;
505    Player *pl = _player;
506
507    Mail* m = pl->GetMail(mailId);
508    if(!m || m->state == MAIL_STATE_DELETED || m->deliver_time > time(NULL))
509    {
510        pl->SendMailResult(mailId, MAIL_MONEY_TAKEN, MAIL_ERR_INTERNAL_ERROR);
511        return;
512    }
513
514    pl->SendMailResult(mailId, MAIL_MONEY_TAKEN, 0);
515
516    pl->ModifyMoney(m->money);
517    m->money = 0;
518    m->state = MAIL_STATE_CHANGED;
519    pl->m_mailsUpdated = true;
520
521    // save money and mail to prevent cheating
522    CharacterDatabase.BeginTransaction();
523    pl->SetUInt32ValueInDB(PLAYER_FIELD_COINAGE,pl->GetMoney(),pl->GetGUID());
524    pl->_SaveMail();
525    CharacterDatabase.CommitTransaction();
526}
527
528//called when player lists his received mails
529void WorldSession::HandleGetMail(WorldPacket & recv_data )
530{
531    CHECK_PACKET_SIZE(recv_data,8);
532
533    uint64 mailbox;
534    recv_data >> mailbox;
535
536    //GameObject* obj = ObjectAccessor::GetGameObject(_player, mailbox);
537    //if(!obj || !obj->IsMailBox())
538    //    return;
539
540    Player* pl = _player;
541
542    //load players mails, and mailed items
543    if(!pl->m_mailsLoaded)
544        pl ->_LoadMail();
545
546    // client can't work with packets > max int16 value
547    const uint32 maxPacketSize = 32767;
548
549    uint32 mails_count = 0;                                 // real send to client mails amount
550
551    WorldPacket data(SMSG_MAIL_LIST_RESULT, (200));         // guess size
552    data << uint8(0);                                       // mail's count
553    time_t cur_time = time(NULL);
554
555    for(PlayerMails::iterator itr = pl->GetmailBegin(); itr != pl->GetmailEnd(); ++itr)
556    {
557        // skip deleted or not delivered (deliver delay not expired) mails
558        if ((*itr)->state == MAIL_STATE_DELETED || cur_time < (*itr)->deliver_time)
559            continue;
560
561        uint8 item_count = (*itr)->items.size();            // max count is MAX_MAIL_ITEMS (12)
562
563        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);
564
565        if(data.wpos()+next_mail_size > maxPacketSize)
566            break;
567
568        data << (uint16) 0x0040;                            // unknown 2.3.0, different values
569        data << (uint32) (*itr)->messageID;                 // Message ID
570        data << (uint8) (*itr)->messageType;                // Message Type
571
572        switch((*itr)->messageType)
573        {
574            case MAIL_NORMAL:                               // sender guid
575                data << uint64(MAKE_NEW_GUID((*itr)->sender, 0, HIGHGUID_PLAYER));
576                break;
577            case MAIL_CREATURE:
578            case MAIL_GAMEOBJECT:
579            case MAIL_AUCTION:
580                data << (uint32) (*itr)->sender;            // creature/gameobject entry, auction id
581                break;
582            case MAIL_ITEM:                                 // item entry (?) sender = "Unknown", NYI
583                break;
584        }
585
586        data << (uint32) (*itr)->COD;                       // COD
587        data << (uint32) (*itr)->itemTextId;                // sure about this
588        data << (uint32) 0;                                 // unknown
589        data << (uint32) (*itr)->stationery;                // stationery (Stationery.dbc)
590        data << (uint32) (*itr)->money;                     // Gold
591        data << (uint32) 0x04;                              // unknown, 0x4 - auction, 0x10 - normal
592                                                            // Time
593        data << (float)  ((*itr)->expire_time-time(NULL))/DAY;
594        data << (uint32) (*itr)->mailTemplateId;            // mail template (MailTemplate.dbc)
595        data << (*itr)->subject;                            // Subject string - once 00, when mail type = 3
596
597        data << (uint8) item_count;
598
599        for(uint8 i = 0; i < item_count; ++i)
600        {
601            Item *item = pl->GetMItem((*itr)->items[i].item_guid);
602            // item index (0-6?)
603            data << (uint8)  i;
604            // item guid low?
605            data << (uint32) (item ? item->GetGUIDLow() : 0);
606            // entry
607            data << (uint32) (item ? item->GetEntry() : 0);
608            for(uint8 j = 0; j < 6; ++j)
609            {
610                // unsure
611                data << (uint32) (item ? item->GetEnchantmentCharges((EnchantmentSlot)j) : 0);
612                // unsure
613                data << (uint32) (item ? item->GetEnchantmentDuration((EnchantmentSlot)j) : 0);
614                // unsure
615                data << (uint32) (item ? item->GetEnchantmentId((EnchantmentSlot)j) : 0);
616            }
617            // can be negative
618            data << (uint32) (item ? item->GetItemRandomPropertyId() : 0);
619            // unk
620            data << (uint32) (item ? item->GetItemSuffixFactor() : 0);
621            // stack count
622            data << (uint8)  (item ? item->GetCount() : 0);
623            // charges
624            data << (uint32) (item ? item->GetSpellCharges() : 0);
625            // durability
626            data << (uint32) (item ? item->GetUInt32Value(ITEM_FIELD_MAXDURABILITY) : 0);
627            // durability
628            data << (uint32) (item ? item->GetUInt32Value(ITEM_FIELD_DURABILITY) : 0);
629        }
630
631        mails_count += 1;
632    }
633
634    data.put<uint8>(0, mails_count);                        // set real send mails to client
635    SendPacket(&data);
636
637    // recalculate m_nextMailDelivereTime and unReadMails
638    _player->UpdateNextMailTimeAndUnreads();
639}
640
641///this function is called when client needs mail message body, or when player clicks on item which has ITEM_FIELD_ITEM_TEXT_ID > 0
642void WorldSession::HandleItemTextQuery(WorldPacket & recv_data )
643{
644    CHECK_PACKET_SIZE(recv_data,4+4+4);
645
646    uint32 itemTextId;
647    uint32 mailId;                                          //this value can be item id in bag, but it is also mail id
648    uint32 unk;                                             //maybe something like state - 0x70000000
649
650    recv_data >> itemTextId >> mailId >> unk;
651
652    //some check needed, if player has item with guid mailId, or has mail with id mailId
653
654    sLog.outDebug("CMSG_ITEM_TEXT_QUERY itemguid: %u, mailId: %u, unk: %u", itemTextId, mailId, unk);
655
656    WorldPacket data(SMSG_ITEM_TEXT_QUERY_RESPONSE, (4+10));// guess size
657    data << itemTextId;
658    data << objmgr.GetItemText( itemTextId );
659    SendPacket(&data);
660}
661
662//used when player copies mail body to his inventory
663void WorldSession::HandleMailCreateTextItem(WorldPacket & recv_data )
664{
665    CHECK_PACKET_SIZE(recv_data,8+4);
666
667    uint64 mailbox;
668    uint32 mailId;
669
670    recv_data >> mailbox >> mailId;
671
672    Player *pl = _player;
673
674    Mail* m = pl->GetMail(mailId);
675    if(!m || !m->itemTextId || m->state == MAIL_STATE_DELETED || m->deliver_time > time(NULL))
676    {
677        pl->SendMailResult(mailId, MAIL_MADE_PERMANENT, MAIL_ERR_INTERNAL_ERROR);
678        return;
679    }
680
681    Item *bodyItem = new Item;                              // This is not bag and then can be used new Item.
682    if(!bodyItem->Create(objmgr.GenerateLowGuid(HIGHGUID_ITEM), MAIL_BODY_ITEM_TEMPLATE, pl))
683    {
684        delete bodyItem;
685        return;
686    }
687
688    bodyItem->SetUInt32Value( ITEM_FIELD_ITEM_TEXT_ID , m->itemTextId );
689    bodyItem->SetUInt32Value( ITEM_FIELD_CREATOR, m->sender);
690
691    sLog.outDetail("HandleMailCreateTextItem mailid=%u",mailId);
692
693    ItemPosCountVec dest;
694    uint8 msg = _player->CanStoreItem( NULL_BAG, NULL_SLOT, dest, bodyItem, false );
695    if( msg == EQUIP_ERR_OK )
696    {
697        m->itemTextId = 0;
698        m->state = MAIL_STATE_CHANGED;
699        pl->m_mailsUpdated = true;
700
701        pl->StoreItem(dest, bodyItem, true);
702        //bodyItem->SetState(ITEM_NEW, pl); is set automatically
703        pl->SendMailResult(mailId, MAIL_MADE_PERMANENT, 0);
704    }
705    else
706    {
707        pl->SendMailResult(mailId, MAIL_MADE_PERMANENT, MAIL_ERR_BAG_FULL, msg);
708        delete bodyItem;
709    }
710}
711
712//TODO Fix me! ... this void has probably bad condition, but good data are sent
713void WorldSession::HandleMsgQueryNextMailtime(WorldPacket & /*recv_data*/ )
714{
715    WorldPacket data(MSG_QUERY_NEXT_MAIL_TIME, 8);
716
717    if(!_player->m_mailsLoaded)
718        _player->_LoadMail();
719
720    if( _player->unReadMails > 0 )
721    {
722        data << (uint32) 0;                                 // float
723        data << (uint32) 0;                                 // count
724        uint32 count = 0;
725        for(PlayerMails::iterator itr = _player->GetmailBegin(); itr != _player->GetmailEnd(); ++itr)
726        {
727            Mail *m = (*itr);
728            // not checked yet, already must be delivered
729            if((m->checked & MAIL_CHECK_MASK_READ)==0 && (m->deliver_time <= time(NULL)))
730            {
731                ++count;
732
733                if(count > 2)
734                {
735                    count = 2;
736                    break;
737                }
738
739                data << (uint64) m->sender;                 // sender guid
740
741                switch(m->messageType)
742                {
743                    case MAIL_AUCTION:
744                        data << (uint32) 2;
745                        data << (uint32) 2;
746                        data << (uint32) m->stationery;
747                        break;
748                    default:
749                        data << (uint32) 0;
750                        data << (uint32) 0;
751                        data << (uint32) m->stationery;
752                        break;
753                }
754                data << (uint32) 0xC6000000;                // float unk, time or something
755            }
756        }
757        data.put<uint32>(4, count);
758    }
759    else
760    {
761        data << (uint32) 0xC7A8C000;
762        data << (uint32) 0x00000000;
763    }
764    SendPacket(&data);
765}
766
767void 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)
768{
769    uint32 mailId = objmgr.GenerateMailID();
770
771    time_t deliver_time = time(NULL) + deliver_delay;
772
773    //expire time if COD 3 days, if no COD 30 days, if auction sale pending 1 hour
774    uint32 expire_delay;
775    if(messageType == MAIL_AUCTION && !mi && !money)        // auction mail without any items and money
776        expire_delay = HOUR;
777    else
778        expire_delay = (COD > 0) ? 3*DAY : 30*DAY;
779
780    time_t expire_time = deliver_time + expire_delay;
781
782    if(mailTemplateId && !sMailTemplateStore.LookupEntry(mailTemplateId))
783    {
784        sLog.outError( "WorldSession::SendMailTo - Mail have not existed MailTemplateId (%u), remove at send", mailTemplateId);
785        mailTemplateId = 0;
786    }
787
788    if(receiver)
789    {
790        receiver->AddNewMailDeliverTime(deliver_time);
791
792        if ( receiver->IsMailsLoaded() )
793        {
794            Mail * m = new Mail;
795            m->messageID = mailId;
796            m->messageType = messageType;
797            m->stationery = stationery;
798            m->mailTemplateId = mailTemplateId;
799            m->sender = sender_guidlow_or_entry;
800            m->receiver = receiver->GetGUIDLow();
801            m->subject = subject;
802            m->itemTextId = itemTextId;
803
804            if(mi)
805                m->AddAllItems(*mi);
806
807            m->expire_time = expire_time;
808            m->deliver_time = deliver_time;
809            m->money = money;
810            m->COD = COD;
811            m->checked = checked;
812            m->state = MAIL_STATE_UNCHANGED;
813
814            receiver->AddMail(m);                           //to insert new mail to beginning of maillist
815
816            if(mi)
817            {
818                for(MailItemMap::iterator mailItemIter = mi->begin(); mailItemIter != mi->end(); ++mailItemIter)
819                {
820                    MailItem& mailItem = mailItemIter->second;
821                    if(mailItem.item)
822                        receiver->AddMItem(mailItem.item);
823                }
824            }
825        }
826        else if(mi)
827            mi->deleteIncludedItems();
828    }
829    else if(mi)
830        mi->deleteIncludedItems();
831
832    CharacterDatabase.BeginTransaction();
833    CharacterDatabase.escape_string(subject);
834    CharacterDatabase.PExecute("INSERT INTO mail (id,messageType,stationery,mailTemplateId,sender,receiver,subject,itemTextId,has_items,expire_time,deliver_time,money,cod,checked) "
835        "VALUES ('%u', '%u', '%u', '%u', '%u', '%u', '%s', '%u', '%u', '" I64FMTD "','" I64FMTD "', '%u', '%u', '%d')",
836        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);
837
838    if(mi)
839    {
840        for(MailItemMap::const_iterator mailItemIter = mi->begin(); mailItemIter != mi->end(); ++mailItemIter)
841        {
842            MailItem const& mailItem = mailItemIter->second;
843            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);
844        }
845    }
846    CharacterDatabase.CommitTransaction();
847}
Note: See TracBrowser for help on using the browser.