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

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

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

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