2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
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.
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.
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
20 #include "WorldPacket.h"
21 #include "WorldSession.h"
25 #include "ObjectMgr.h"
27 #include "UpdateMask.h"
30 #include "AuctionHouseBot.h"
31 #include "DBCStores.h"
33 void MailItem::deleteItem( bool inDB
)
38 CharacterDatabase
.PExecute("DELETE FROM item_instance WHERE guid='%u'", item
->GetGUIDLow());
45 void WorldSession::HandleSendMail(WorldPacket
& recv_data
)
47 CHECK_PACKET_SIZE(recv_data
,8+1+1+1+4+4+1+4+4+8+1);
50 std::string receiver
, subject
, body
;
51 uint32 unk1
, unk2
, money
, COD
;
54 recv_data
>> receiver
;
56 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
60 CHECK_PACKET_SIZE(recv_data
, 8+(receiver
.size()+1)+1+1+4+4+1+4+4+8+1);
65 CHECK_PACKET_SIZE(recv_data
, 8+(receiver
.size()+1)+(subject
.size()+1)+1+4+4+1+4+4+8+1);
70 CHECK_PACKET_SIZE(recv_data
, 8+(receiver
.size()+1)+(subject
.size()+1)+(body
.size()+1)+4+4+1+4+4+8+1);
72 recv_data
>> unk1
; // stationery?
73 recv_data
>> unk2
; // 0x00000000
78 recv_data
>> items_count
; // attached items count
80 if(items_count
> 12) // client limit
84 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);
88 for(uint8 i
= 0; i
< items_count
; ++i
)
92 recv_data
>> item_slot
;
93 recv_data
>> item_guid
;
94 mi
.AddItem(GUID_LOPART(item_guid
), item_slot
);
98 recv_data
>> money
>> COD
; // money and cod
99 recv_data
>> unk3
; // const 0
100 recv_data
>> unk4
; // const 0
102 items_count
= mi
.size(); // this is the real size after the duplicates have been removed
104 if (receiver
.empty())
107 Player
* pl
= _player
;
110 if(normalizePlayerName(receiver
))
111 rc
= objmgr
.GetPlayerGUIDByName(receiver
);
115 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",
116 pl
->GetGUIDLow(), receiver
.c_str(), subject
.c_str(), body
.c_str(), items_count
, money
, COD
, unk1
, unk2
);
117 pl
->SendMailResult(0, 0, MAIL_ERR_RECIPIENT_NOT_FOUND
);
121 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
);
123 if(pl
->GetGUID() == rc
)
125 pl
->SendMailResult(0, 0, MAIL_ERR_CANNOT_SEND_TO_SELF
);
129 uint32 cost
= items_count
? 30 * items_count
: 30; // price hardcoded in client
131 uint32 reqmoney
= cost
+ money
;
133 if (pl
->GetMoney() < reqmoney
)
135 pl
->SendMailResult(0, 0, MAIL_ERR_NOT_ENOUGH_MONEY
);
139 Player
*receive
= objmgr
.GetPlayer(rc
);
142 uint8 mails_count
= 0; //do not allow to send to one player more than 100 mails
146 rc_team
= receive
->GetTeam();
147 mails_count
= receive
->GetMailSize();
151 rc_team
= objmgr
.GetPlayerTeamByGUID(rc
);
152 QueryResult
* result
= CharacterDatabase
.PQuery("SELECT COUNT(*) FROM mail WHERE receiver = '%u'", GUID_LOPART(rc
));
155 Field
*fields
= result
->Fetch();
156 mails_count
= fields
[0].GetUInt32();
160 //do not allow to have more than 100 mails in mailbox.. mails count is in opcode uint8!!! - so max can be 255..
161 if (mails_count
> 100)
163 pl
->SendMailResult(0, 0, MAIL_ERR_INTERNAL_ERROR
);
166 // test the receiver's Faction...
167 if (!sWorld
.getConfig(CONFIG_ALLOW_TWO_SIDE_INTERACTION_MAIL
) && pl
->GetTeam() != rc_team
&& GetSecurity() == SEC_PLAYER
)
169 pl
->SendMailResult(0, 0, MAIL_ERR_NOT_YOUR_TEAM
);
175 for(MailItemMap::iterator mailItemIter
= mi
.begin(); mailItemIter
!= mi
.end(); ++mailItemIter
)
177 MailItem
& mailItem
= mailItemIter
->second
;
179 if(!mailItem
.item_guidlow
)
181 pl
->SendMailResult(0, 0, MAIL_ERR_INTERNAL_ERROR
);
185 mailItem
.item
= pl
->GetItemByGuid(MAKE_NEW_GUID(mailItem
.item_guidlow
, 0, HIGHGUID_ITEM
));
186 // prevent sending bag with items (cheat: can be placed in bag after adding equipped empty bag to mail)
187 if(!mailItem
.item
|| !mailItem
.item
->CanBeTraded())
189 pl
->SendMailResult(0, 0, MAIL_ERR_INTERNAL_ERROR
);
192 if (mailItem
.item
->HasFlag(ITEM_FIELD_FLAGS
, ITEM_FLAGS_CONJURED
) || mailItem
.item
->GetUInt32Value(ITEM_FIELD_DURATION
))
194 pl
->SendMailResult(0, 0, MAIL_ERR_INTERNAL_ERROR
);
198 if(COD
&& mailItem
.item
->HasFlag(ITEM_FIELD_FLAGS
, ITEM_FLAGS_WRAPPED
))
200 pl
->SendMailResult(0, 0, MAIL_ERR_CANT_SEND_WRAPPED_COD
);
205 pl
->SendMailResult(0, 0, MAIL_OK
);
207 uint32 itemTextId
= 0;
210 itemTextId
= objmgr
.CreateItemText( body
);
213 pl
->ModifyMoney( -int32(reqmoney
) );
214 pl
->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_GOLD_SPENT_FOR_MAIL
, cost
);
216 bool needItemDelay
= false;
218 if(items_count
> 0 || money
> 0)
220 uint32 rc_account
= 0;
222 rc_account
= receive
->GetSession()->GetAccountId();
224 rc_account
= objmgr
.GetPlayerAccountIdByGUID(rc
);
228 for(MailItemMap::iterator mailItemIter
= mi
.begin(); mailItemIter
!= mi
.end(); ++mailItemIter
)
230 MailItem
& mailItem
= mailItemIter
->second
;
234 mailItem
.item_template
= mailItem
.item
? mailItem
.item
->GetEntry() : 0;
236 if( GetSecurity() > SEC_PLAYER
&& sWorld
.getConfig(CONFIG_GM_LOG_TRADE
) )
238 sLog
.outCommand(GetAccountId(), "GM %s (Account: %u) mail item: %s (Entry: %u Count: %u) to player: %s (Account: %u)",
239 GetPlayerName(), GetAccountId(), mailItem
.item
->GetProto()->Name1
, mailItem
.item
->GetEntry(), mailItem
.item
->GetCount(), receiver
.c_str(), rc_account
);
242 pl
->MoveItemFromInventory(mailItem
.item
->GetBagSlot(), mailItem
.item
->GetSlot(), true);
243 CharacterDatabase
.BeginTransaction();
244 mailItem
.item
->DeleteFromInventoryDB(); //deletes item from character's inventory
245 mailItem
.item
->SaveToDB(); // recursive and not have transaction guard into self, item not in inventory and can be save standalone
246 // owner in data will set at mail receive and item extracting
247 CharacterDatabase
.PExecute("UPDATE item_instance SET owner_guid = '%u' WHERE guid='%u'", GUID_LOPART(rc
), mailItem
.item
->GetGUIDLow());
248 CharacterDatabase
.CommitTransaction();
251 // if item send to character at another account, then apply item delivery delay
252 needItemDelay
= pl
->GetSession()->GetAccountId() != rc_account
;
255 if(money
> 0 && GetSecurity() > SEC_PLAYER
&& sWorld
.getConfig(CONFIG_GM_LOG_TRADE
))
257 sLog
.outCommand(GetAccountId(),"GM %s (Account: %u) mail money: %u to player: %s (Account: %u)",
258 GetPlayerName(), GetAccountId(), money
, receiver
.c_str(), rc_account
);
262 // If theres is an item, there is a one hour delivery delay if sent to another account's character.
263 uint32 deliver_delay
= needItemDelay
? sWorld
.getConfig(CONFIG_MAIL_DELIVERY_DELAY
) : 0;
265 // will delete item or place to receiver mail list
266 WorldSession::SendMailTo(receive
, MAIL_NORMAL
, MAIL_STATIONERY_NORMAL
, pl
->GetGUIDLow(), GUID_LOPART(rc
), subject
, itemTextId
, &mi
, money
, COD
, MAIL_CHECK_MASK_NONE
, deliver_delay
);
268 CharacterDatabase
.BeginTransaction();
269 pl
->SaveInventoryAndGoldToDB();
270 CharacterDatabase
.CommitTransaction();
273 //called when mail is read
274 void WorldSession::HandleMailMarkAsRead(WorldPacket
& recv_data
)
276 CHECK_PACKET_SIZE(recv_data
,8+4);
280 recv_data
>> mailbox
;
282 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
286 Player
*pl
= _player
;
287 Mail
*m
= pl
->GetMail(mailId
);
292 m
->checked
= m
->checked
| MAIL_CHECK_MASK_READ
;
293 // m->expire_time = time(NULL) + (30 * DAY); // Expire time do not change at reading mail
294 pl
->m_mailsUpdated
= true;
295 m
->state
= MAIL_STATE_CHANGED
;
299 //called when client deletes mail
300 void WorldSession::HandleMailDelete(WorldPacket
& recv_data
)
302 CHECK_PACKET_SIZE(recv_data
,8+4);
306 recv_data
>> mailbox
;
309 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
312 Player
* pl
= _player
;
313 pl
->m_mailsUpdated
= true;
314 Mail
*m
= pl
->GetMail(mailId
);
316 m
->state
= MAIL_STATE_DELETED
;
317 pl
->SendMailResult(mailId
, MAIL_DELETED
, 0);
320 void WorldSession::HandleMailReturnToSender(WorldPacket
& recv_data
)
322 CHECK_PACKET_SIZE(recv_data
,8+4);
326 recv_data
>> mailbox
;
328 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
332 Player
*pl
= _player
;
333 Mail
*m
= pl
->GetMail(mailId
);
334 if(!m
|| m
->state
== MAIL_STATE_DELETED
|| m
->deliver_time
> time(NULL
))
336 pl
->SendMailResult(mailId
, MAIL_RETURNED_TO_SENDER
, MAIL_ERR_INTERNAL_ERROR
);
339 //we can return mail now
340 //so firstly delete the old one
341 CharacterDatabase
.BeginTransaction();
342 CharacterDatabase
.PExecute("DELETE FROM mail WHERE id = '%u'", mailId
);
344 CharacterDatabase
.PExecute("DELETE FROM mail_items WHERE mail_id = '%u'", mailId
);
345 CharacterDatabase
.CommitTransaction();
346 pl
->RemoveMail(mailId
);
352 for(std::vector
<MailItemInfo
>::iterator itr2
= m
->items
.begin(); itr2
!= m
->items
.end(); ++itr2
)
354 Item
*item
= pl
->GetMItem(itr2
->item_guid
);
356 mi
.AddItem(item
->GetGUIDLow(), item
->GetEntry(), item
);
362 pl
->RemoveMItem(itr2
->item_guid
);
366 if (m
->sender
== AHBplayerGUID
)
368 SendReturnToSender(MAIL_CREATURE
, GetAccountId(), m
->receiver
, m
->sender
, m
->subject
, m
->itemTextId
, &mi
, m
->money
, m
->mailTemplateId
);
372 SendReturnToSender(MAIL_NORMAL
, GetAccountId(), m
->receiver
, m
->sender
, m
->subject
, m
->itemTextId
, &mi
, m
->money
, m
->mailTemplateId
);
375 delete m
; //we can deallocate old mail
376 pl
->SendMailResult(mailId
, MAIL_RETURNED_TO_SENDER
, 0);
379 void WorldSession::SendReturnToSender(uint8 messageType
, uint32 sender_acc
, uint32 sender_guid
, uint32 receiver_guid
, const std::string
& subject
, uint32 itemTextId
, MailItemsInfo
*mi
, uint32 money
, uint16 mailTemplateId
)
381 if(messageType
!= MAIL_NORMAL
) // return only to players
383 mi
->deleteIncludedItems(true);
387 Player
*receiver
= objmgr
.GetPlayer(MAKE_NEW_GUID(receiver_guid
, 0, HIGHGUID_PLAYER
));
389 uint32 rc_account
= 0;
391 rc_account
= objmgr
.GetPlayerAccountIdByGUID(MAKE_NEW_GUID(receiver_guid
, 0, HIGHGUID_PLAYER
));
393 if(!receiver
&& !rc_account
) // sender not exist
395 mi
->deleteIncludedItems(true);
399 // prepare mail and send in other case
400 bool needItemDelay
= false;
402 if(mi
&& !mi
->empty())
404 // if item send to character at another account, then apply item delivery delay
405 needItemDelay
= sender_acc
!= rc_account
;
407 // set owner to new receiver (to prevent delete item with sender char deleting)
408 CharacterDatabase
.BeginTransaction();
409 for(MailItemMap::iterator mailItemIter
= mi
->begin(); mailItemIter
!= mi
->end(); ++mailItemIter
)
411 MailItem
& mailItem
= mailItemIter
->second
;
412 mailItem
.item
->SaveToDB(); // item not in inventory and can be save standalone
413 // owner in data will set at mail receive and item extracting
414 CharacterDatabase
.PExecute("UPDATE item_instance SET owner_guid = '%u' WHERE guid='%u'", receiver_guid
, mailItem
.item
->GetGUIDLow());
416 CharacterDatabase
.CommitTransaction();
419 // If theres is an item, there is a one hour delivery delay.
420 uint32 deliver_delay
= needItemDelay
? sWorld
.getConfig(CONFIG_MAIL_DELIVERY_DELAY
) : 0;
422 // will delete item or place to receiver mail list
423 WorldSession::SendMailTo(receiver
, MAIL_NORMAL
, MAIL_STATIONERY_NORMAL
, sender_guid
, receiver_guid
, subject
, itemTextId
, mi
, money
, 0, MAIL_CHECK_MASK_RETURNED
,deliver_delay
,mailTemplateId
);
426 //called when player takes item attached in mail
427 void WorldSession::HandleMailTakeItem(WorldPacket
& recv_data
)
429 CHECK_PACKET_SIZE(recv_data
,8+4+4);
434 recv_data
>> mailbox
;
436 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
440 recv_data
>> itemId
; // item guid low?
441 Player
* pl
= _player
;
443 Mail
* m
= pl
->GetMail(mailId
);
444 if(!m
|| m
->state
== MAIL_STATE_DELETED
|| m
->deliver_time
> time(NULL
))
446 pl
->SendMailResult(mailId
, MAIL_ITEM_TAKEN
, MAIL_ERR_INTERNAL_ERROR
);
450 // prevent cheating with skip client money check
451 if(pl
->GetMoney() < m
->COD
)
453 pl
->SendMailResult(mailId
, MAIL_ITEM_TAKEN
, MAIL_ERR_NOT_ENOUGH_MONEY
);
457 Item
*it
= pl
->GetMItem(itemId
);
459 ItemPosCountVec dest
;
460 uint8 msg
= _player
->CanStoreItem( NULL_BAG
, NULL_SLOT
, dest
, it
, false );
461 if( msg
== EQUIP_ERR_OK
)
463 m
->RemoveItem(itemId
);
464 m
->removedItems
.push_back(itemId
);
466 if (m
->COD
> 0) //if there is COD, take COD money from player and send them to sender by mail
468 uint64 sender_guid
= MAKE_NEW_GUID(m
->sender
, 0, HIGHGUID_PLAYER
);
469 Player
*receive
= objmgr
.GetPlayer(sender_guid
);
471 uint32 sender_accId
= 0;
473 if( GetSecurity() > SEC_PLAYER
&& sWorld
.getConfig(CONFIG_GM_LOG_TRADE
) )
475 std::string sender_name
;
478 sender_accId
= receive
->GetSession()->GetAccountId();
479 sender_name
= receive
->GetName();
483 // can be calculated early
484 sender_accId
= objmgr
.GetPlayerAccountIdByGUID(sender_guid
);
486 if(!objmgr
.GetPlayerNameByGUID(sender_guid
,sender_name
))
487 sender_name
= objmgr
.GetMangosStringForDBCLocale(LANG_UNKNOWN
);
489 sLog
.outCommand(GetAccountId(),"GM %s (Account: %u) receive mail item: %s (Entry: %u Count: %u) and send COD money: %u to player: %s (Account: %u)",
490 GetPlayerName(),GetAccountId(),it
->GetProto()->Name1
,it
->GetEntry(),it
->GetCount(),m
->COD
,sender_name
.c_str(),sender_accId
);
493 sender_accId
= objmgr
.GetPlayerAccountIdByGUID(sender_guid
);
495 // check player existence
496 if(receive
|| sender_accId
)
498 WorldSession::SendMailTo(receive
, MAIL_NORMAL
, MAIL_STATIONERY_NORMAL
, m
->receiver
, m
->sender
, m
->subject
, 0, NULL
, m
->COD
, 0, MAIL_CHECK_MASK_COD_PAYMENT
);
501 pl
->ModifyMoney( -int32(m
->COD
) );
504 m
->state
= MAIL_STATE_CHANGED
;
505 pl
->m_mailsUpdated
= true;
506 pl
->RemoveMItem(it
->GetGUIDLow());
508 uint32 count
= it
->GetCount(); // save counts before store and possible merge with deleting
509 pl
->MoveItemToInventory(dest
,it
,true);
511 CharacterDatabase
.BeginTransaction();
512 pl
->SaveInventoryAndGoldToDB();
514 CharacterDatabase
.CommitTransaction();
516 pl
->SendMailResult(mailId
, MAIL_ITEM_TAKEN
, MAIL_OK
, 0, itemId
, count
);
519 pl
->SendMailResult(mailId
, MAIL_ITEM_TAKEN
, MAIL_ERR_BAG_FULL
, msg
);
522 void WorldSession::HandleMailTakeMoney(WorldPacket
& recv_data
)
524 CHECK_PACKET_SIZE(recv_data
,8+4);
528 recv_data
>> mailbox
;
531 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
534 Player
*pl
= _player
;
536 Mail
* m
= pl
->GetMail(mailId
);
537 if(!m
|| m
->state
== MAIL_STATE_DELETED
|| m
->deliver_time
> time(NULL
))
539 pl
->SendMailResult(mailId
, MAIL_MONEY_TAKEN
, MAIL_ERR_INTERNAL_ERROR
);
543 pl
->SendMailResult(mailId
, MAIL_MONEY_TAKEN
, 0);
545 pl
->ModifyMoney(m
->money
);
547 m
->state
= MAIL_STATE_CHANGED
;
548 pl
->m_mailsUpdated
= true;
550 // save money and mail to prevent cheating
551 CharacterDatabase
.BeginTransaction();
552 pl
->SaveDataFieldToDB(); // contains money
554 CharacterDatabase
.CommitTransaction();
557 //called when player lists his received mails
558 void WorldSession::HandleGetMailList(WorldPacket
& recv_data
)
560 CHECK_PACKET_SIZE(recv_data
,8);
563 recv_data
>> mailbox
;
565 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
568 Player
* pl
= _player
;
570 //load players mails, and mailed items
571 if(!pl
->m_mailsLoaded
)
574 // client can't work with packets > max int16 value
575 const uint32 maxPacketSize
= 32767;
577 uint32 mails_count
= 0; // real send to client mails amount
579 WorldPacket
data(SMSG_MAIL_LIST_RESULT
, (200)); // guess size
580 data
<< uint8(0); // mail's count
581 time_t cur_time
= time(NULL
);
583 for(PlayerMails::iterator itr
= pl
->GetmailBegin(); itr
!= pl
->GetmailEnd(); ++itr
)
585 // skip deleted or not delivered (deliver delay not expired) mails
586 if ((*itr
)->state
== MAIL_STATE_DELETED
|| cur_time
< (*itr
)->deliver_time
)
589 uint8 item_count
= (*itr
)->items
.size(); // max count is MAX_MAIL_ITEMS (12)
591 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);
593 if(data
.wpos()+next_mail_size
> maxPacketSize
)
596 data
<< (uint16
) 0x0040; // unknown 2.3.0, different values
597 data
<< (uint32
) (*itr
)->messageID
; // Message ID
598 data
<< (uint8
) (*itr
)->messageType
; // Message Type
600 switch((*itr
)->messageType
)
602 case MAIL_NORMAL
: // sender guid
603 data
<< uint64(MAKE_NEW_GUID((*itr
)->sender
, 0, HIGHGUID_PLAYER
));
606 case MAIL_GAMEOBJECT
:
608 data
<< (uint32
) (*itr
)->sender
; // creature/gameobject entry, auction id
610 case MAIL_ITEM
: // item entry (?) sender = "Unknown", NYI
614 data
<< (uint32
) (*itr
)->COD
; // COD
615 data
<< (uint32
) (*itr
)->itemTextId
; // sure about this
616 data
<< (uint32
) 0; // unknown
617 data
<< (uint32
) (*itr
)->stationery
; // stationery (Stationery.dbc)
618 data
<< (uint32
) (*itr
)->money
; // Gold
619 data
<< (uint32
) 0x04; // unknown, 0x4 - auction, 0x10 - normal
621 data
<< (float) ((*itr
)->expire_time
-time(NULL
))/DAY
;
622 data
<< (uint32
) (*itr
)->mailTemplateId
; // mail template (MailTemplate.dbc)
623 data
<< (*itr
)->subject
; // Subject string - once 00, when mail type = 3
625 data
<< (uint8
) item_count
; // client limit is 0x10
627 for(uint8 i
= 0; i
< item_count
; ++i
)
629 Item
*item
= pl
->GetMItem((*itr
)->items
[i
].item_guid
);
633 data
<< (uint32
) (item
? item
->GetGUIDLow() : 0);
635 data
<< (uint32
) (item
? item
->GetEntry() : 0);
636 for(uint8 j
= 0; j
< MAX_INSPECTED_ENCHANTMENT_SLOT
; ++j
)
639 data
<< (uint32
) (item
? item
->GetEnchantmentCharges((EnchantmentSlot
)j
) : 0);
641 data
<< (uint32
) (item
? item
->GetEnchantmentDuration((EnchantmentSlot
)j
) : 0);
643 data
<< (uint32
) (item
? item
->GetEnchantmentId((EnchantmentSlot
)j
) : 0);
646 data
<< (uint32
) (item
? item
->GetItemRandomPropertyId() : 0);
648 data
<< (uint32
) (item
? item
->GetItemSuffixFactor() : 0);
650 data
<< (uint32
) (item
? item
->GetCount() : 0);
652 data
<< (uint32
) (item
? item
->GetSpellCharges() : 0);
654 data
<< (uint32
) (item
? item
->GetUInt32Value(ITEM_FIELD_MAXDURABILITY
) : 0);
656 data
<< (uint32
) (item
? item
->GetUInt32Value(ITEM_FIELD_DURABILITY
) : 0);
664 data
.put
<uint8
>(0, mails_count
); // set real send mails to client
667 // recalculate m_nextMailDelivereTime and unReadMails
668 _player
->UpdateNextMailTimeAndUnreads();
671 ///this function is called when client needs mail message body, or when player clicks on item which has ITEM_FIELD_ITEM_TEXT_ID > 0
672 void WorldSession::HandleItemTextQuery(WorldPacket
& recv_data
)
674 CHECK_PACKET_SIZE(recv_data
,4+4+4);
677 uint32 mailId
; //this value can be item id in bag, but it is also mail id
678 uint32 unk
; //maybe something like state - 0x70000000
680 recv_data
>> itemTextId
>> mailId
>> unk
;
682 //some check needed, if player has item with guid mailId, or has mail with id mailId
684 sLog
.outDebug("CMSG_ITEM_TEXT_QUERY itemguid: %u, mailId: %u, unk: %u", itemTextId
, mailId
, unk
);
686 WorldPacket
data(SMSG_ITEM_TEXT_QUERY_RESPONSE
, (4+10));// guess size
688 data
<< objmgr
.GetItemText( itemTextId
);
692 //used when player copies mail body to his inventory
693 void WorldSession::HandleMailCreateTextItem(WorldPacket
& recv_data
)
695 CHECK_PACKET_SIZE(recv_data
,8+4);
700 recv_data
>> mailbox
>> mailId
;
702 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
705 Player
*pl
= _player
;
707 Mail
* m
= pl
->GetMail(mailId
);
708 if(!m
|| !m
->itemTextId
|| m
->state
== MAIL_STATE_DELETED
|| m
->deliver_time
> time(NULL
))
710 pl
->SendMailResult(mailId
, MAIL_MADE_PERMANENT
, MAIL_ERR_INTERNAL_ERROR
);
714 Item
*bodyItem
= new Item
; // This is not bag and then can be used new Item.
715 if(!bodyItem
->Create(objmgr
.GenerateLowGuid(HIGHGUID_ITEM
), MAIL_BODY_ITEM_TEMPLATE
, pl
))
721 bodyItem
->SetUInt32Value( ITEM_FIELD_ITEM_TEXT_ID
, m
->itemTextId
);
722 bodyItem
->SetUInt32Value( ITEM_FIELD_CREATOR
, m
->sender
);
724 sLog
.outDetail("HandleMailCreateTextItem mailid=%u",mailId
);
726 ItemPosCountVec dest
;
727 uint8 msg
= _player
->CanStoreItem( NULL_BAG
, NULL_SLOT
, dest
, bodyItem
, false );
728 if( msg
== EQUIP_ERR_OK
)
731 m
->state
= MAIL_STATE_CHANGED
;
732 pl
->m_mailsUpdated
= true;
734 pl
->StoreItem(dest
, bodyItem
, true);
735 //bodyItem->SetState(ITEM_NEW, pl); is set automatically
736 pl
->SendMailResult(mailId
, MAIL_MADE_PERMANENT
, 0);
740 pl
->SendMailResult(mailId
, MAIL_MADE_PERMANENT
, MAIL_ERR_BAG_FULL
, msg
);
745 //TODO Fix me! ... this void has probably bad condition, but good data are sent
746 void WorldSession::HandleQueryNextMailTime(WorldPacket
& /*recv_data*/ )
748 WorldPacket
data(MSG_QUERY_NEXT_MAIL_TIME
, 8);
750 if(!_player
->m_mailsLoaded
)
751 _player
->_LoadMail();
753 if( _player
->unReadMails
> 0 )
755 data
<< (uint32
) 0; // float
756 data
<< (uint32
) 0; // count
758 for(PlayerMails::iterator itr
= _player
->GetmailBegin(); itr
!= _player
->GetmailEnd(); ++itr
)
761 // not checked yet, already must be delivered
762 if((m
->checked
& MAIL_CHECK_MASK_READ
)==0 && (m
->deliver_time
<= time(NULL
)))
772 data
<< (uint64
) m
->sender
; // sender guid
774 switch(m
->messageType
)
779 data
<< (uint32
) m
->stationery
;
784 data
<< (uint32
) m
->stationery
;
787 data
<< (uint32
) 0xC6000000; // float unk, time or something
790 data
.put
<uint32
>(4, count
);
794 data
<< (uint32
) 0xC7A8C000;
795 data
<< (uint32
) 0x00000000;
800 void 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
)
802 if (receiver_guidlow
== AHBplayerGUID
)
804 if(messageType
== MAIL_AUCTION
&& mi
) // auction mail with items
806 mi
->deleteIncludedItems(true);
810 uint32 mailId
= objmgr
.GenerateMailID();
812 time_t deliver_time
= time(NULL
) + deliver_delay
;
814 //expire time if COD 3 days, if no COD 30 days, if auction sale pending 1 hour
816 if(messageType
== MAIL_AUCTION
&& !mi
&& !money
) // auction mail without any items and money
819 expire_delay
= (COD
> 0) ? 3*DAY
: 30*DAY
;
821 time_t expire_time
= deliver_time
+ expire_delay
;
823 if(mailTemplateId
&& !sMailTemplateStore
.LookupEntry(mailTemplateId
))
825 sLog
.outError( "WorldSession::SendMailTo - Mail have not existed MailTemplateId (%u), remove at send", mailTemplateId
);
831 receiver
->AddNewMailDeliverTime(deliver_time
);
833 if ( receiver
->IsMailsLoaded() )
836 m
->messageID
= mailId
;
837 m
->messageType
= messageType
;
838 m
->stationery
= stationery
;
839 m
->mailTemplateId
= mailTemplateId
;
840 m
->sender
= sender_guidlow_or_entry
;
841 m
->receiver
= receiver
->GetGUIDLow();
842 m
->subject
= subject
;
843 m
->itemTextId
= itemTextId
;
848 m
->expire_time
= expire_time
;
849 m
->deliver_time
= deliver_time
;
852 m
->checked
= checked
;
853 m
->state
= MAIL_STATE_UNCHANGED
;
855 receiver
->AddMail(m
); //to insert new mail to beginning of maillist
859 for(MailItemMap::iterator mailItemIter
= mi
->begin(); mailItemIter
!= mi
->end(); ++mailItemIter
)
861 MailItem
& mailItem
= mailItemIter
->second
;
863 receiver
->AddMItem(mailItem
.item
);
868 mi
->deleteIncludedItems();
871 mi
->deleteIncludedItems();
873 CharacterDatabase
.BeginTransaction();
874 CharacterDatabase
.escape_string(subject
);
875 CharacterDatabase
.PExecute("INSERT INTO mail (id,messageType,stationery,mailTemplateId,sender,receiver,subject,itemTextId,has_items,expire_time,deliver_time,money,cod,checked) "
876 "VALUES ('%u', '%u', '%u', '%u', '%u', '%u', '%s', '%u', '%u', '" I64FMTD
"','" I64FMTD
"', '%u', '%u', '%d')",
877 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
);
881 for(MailItemMap::const_iterator mailItemIter
= mi
->begin(); mailItemIter
!= mi
->end(); ++mailItemIter
)
883 MailItem
const& mailItem
= mailItemIter
->second
;
884 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
);
887 CharacterDatabase
.CommitTransaction();