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

Revision 260, 30.5 kB (checked in by yumileroy, 17 years ago)

*DB script table stucture change. Source Mangos. Also fix some bugs. Hopefully this rev will make program usable again.

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