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 "DBCStores.h"
32 void MailItem::deleteItem( bool inDB
)
37 CharacterDatabase
.PExecute("DELETE FROM item_instance WHERE guid='%u'", item
->GetGUIDLow());
44 void WorldSession::HandleSendMail(WorldPacket
& recv_data
)
46 CHECK_PACKET_SIZE(recv_data
,8+1+1+1+4+4+1+4+4+8+1);
49 std::string receiver
, subject
, body
;
50 uint32 unk1
, unk2
, money
, COD
;
53 recv_data
>> receiver
;
55 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
59 CHECK_PACKET_SIZE(recv_data
, 8+(receiver
.size()+1)+1+1+4+4+1+4+4+8+1);
64 CHECK_PACKET_SIZE(recv_data
, 8+(receiver
.size()+1)+(subject
.size()+1)+1+4+4+1+4+4+8+1);
69 CHECK_PACKET_SIZE(recv_data
, 8+(receiver
.size()+1)+(subject
.size()+1)+(body
.size()+1)+4+4+1+4+4+8+1);
71 recv_data
>> unk1
; // stationery?
72 recv_data
>> unk2
; // 0x00000000
77 recv_data
>> items_count
; // attached items count
79 if(items_count
> 12) // client limit
83 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);
87 for(uint8 i
= 0; i
< items_count
; ++i
)
91 recv_data
>> item_slot
;
92 recv_data
>> item_guid
;
93 mi
.AddItem(GUID_LOPART(item_guid
), item_slot
);
97 recv_data
>> money
>> COD
; // money and cod
98 recv_data
>> unk3
; // const 0
99 recv_data
>> unk4
; // const 0
101 items_count
= mi
.size(); // this is the real size after the duplicates have been removed
103 if (receiver
.empty())
106 Player
* pl
= _player
;
109 if(normalizePlayerName(receiver
))
110 rc
= objmgr
.GetPlayerGUIDByName(receiver
);
114 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",
115 pl
->GetGUIDLow(), receiver
.c_str(), subject
.c_str(), body
.c_str(), items_count
, money
, COD
, unk1
, unk2
);
116 pl
->SendMailResult(0, 0, MAIL_ERR_RECIPIENT_NOT_FOUND
);
120 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
);
122 if(pl
->GetGUID() == rc
)
124 pl
->SendMailResult(0, 0, MAIL_ERR_CANNOT_SEND_TO_SELF
);
128 uint32 cost
= items_count
? 30 * items_count
: 30; // price hardcoded in client
130 uint32 reqmoney
= cost
+ money
;
132 if (pl
->GetMoney() < reqmoney
)
134 pl
->SendMailResult(0, 0, MAIL_ERR_NOT_ENOUGH_MONEY
);
138 Player
*receive
= objmgr
.GetPlayer(rc
);
141 uint8 mails_count
= 0; //do not allow to send to one player more than 100 mails
145 rc_team
= receive
->GetTeam();
146 mails_count
= receive
->GetMailSize();
150 rc_team
= objmgr
.GetPlayerTeamByGUID(rc
);
151 QueryResult
* result
= CharacterDatabase
.PQuery("SELECT COUNT(*) FROM mail WHERE receiver = '%u'", GUID_LOPART(rc
));
154 Field
*fields
= result
->Fetch();
155 mails_count
= fields
[0].GetUInt32();
159 //do not allow to have more than 100 mails in mailbox.. mails count is in opcode uint8!!! - so max can be 255..
160 if (mails_count
> 100)
162 pl
->SendMailResult(0, 0, MAIL_ERR_INTERNAL_ERROR
);
165 // test the receiver's Faction...
166 if (!sWorld
.getConfig(CONFIG_ALLOW_TWO_SIDE_INTERACTION_MAIL
) && pl
->GetTeam() != rc_team
&& GetSecurity() == SEC_PLAYER
)
168 pl
->SendMailResult(0, 0, MAIL_ERR_NOT_YOUR_TEAM
);
174 for(MailItemMap::iterator mailItemIter
= mi
.begin(); mailItemIter
!= mi
.end(); ++mailItemIter
)
176 MailItem
& mailItem
= mailItemIter
->second
;
178 if(!mailItem
.item_guidlow
)
180 pl
->SendMailResult(0, 0, MAIL_ERR_INTERNAL_ERROR
);
184 mailItem
.item
= pl
->GetItemByGuid(MAKE_NEW_GUID(mailItem
.item_guidlow
, 0, HIGHGUID_ITEM
));
185 // prevent sending bag with items (cheat: can be placed in bag after adding equipped empty bag to mail)
186 if(!mailItem
.item
|| !mailItem
.item
->CanBeTraded())
188 pl
->SendMailResult(0, 0, MAIL_ERR_INTERNAL_ERROR
);
191 if (mailItem
.item
->HasFlag(ITEM_FIELD_FLAGS
, ITEM_FLAGS_CONJURED
) || mailItem
.item
->GetUInt32Value(ITEM_FIELD_DURATION
))
193 pl
->SendMailResult(0, 0, MAIL_ERR_INTERNAL_ERROR
);
197 if(COD
&& mailItem
.item
->HasFlag(ITEM_FIELD_FLAGS
, ITEM_FLAGS_WRAPPED
))
199 pl
->SendMailResult(0, 0, MAIL_ERR_CANT_SEND_WRAPPED_COD
);
204 pl
->SendMailResult(0, 0, MAIL_OK
);
206 uint32 itemTextId
= 0;
209 itemTextId
= objmgr
.CreateItemText( body
);
212 pl
->ModifyMoney( -int32(reqmoney
) );
213 pl
->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_GOLD_SPENT_FOR_MAIL
, cost
);
215 bool needItemDelay
= false;
217 if(items_count
> 0 || money
> 0)
219 uint32 rc_account
= 0;
221 rc_account
= receive
->GetSession()->GetAccountId();
223 rc_account
= objmgr
.GetPlayerAccountIdByGUID(rc
);
227 for(MailItemMap::iterator mailItemIter
= mi
.begin(); mailItemIter
!= mi
.end(); ++mailItemIter
)
229 MailItem
& mailItem
= mailItemIter
->second
;
233 mailItem
.item_template
= mailItem
.item
? mailItem
.item
->GetEntry() : 0;
235 if( GetSecurity() > SEC_PLAYER
&& sWorld
.getConfig(CONFIG_GM_LOG_TRADE
) )
237 sLog
.outCommand(GetAccountId(), "GM %s (Account: %u) mail item: %s (Entry: %u Count: %u) to player: %s (Account: %u)",
238 GetPlayerName(), GetAccountId(), mailItem
.item
->GetProto()->Name1
, mailItem
.item
->GetEntry(), mailItem
.item
->GetCount(), receiver
.c_str(), rc_account
);
241 pl
->MoveItemFromInventory(mailItem
.item
->GetBagSlot(), mailItem
.item
->GetSlot(), true);
242 CharacterDatabase
.BeginTransaction();
243 mailItem
.item
->DeleteFromInventoryDB(); //deletes item from character's inventory
244 mailItem
.item
->SaveToDB(); // recursive and not have transaction guard into self, item not in inventory and can be save standalone
245 // owner in data will set at mail receive and item extracting
246 CharacterDatabase
.PExecute("UPDATE item_instance SET owner_guid = '%u' WHERE guid='%u'", GUID_LOPART(rc
), mailItem
.item
->GetGUIDLow());
247 CharacterDatabase
.CommitTransaction();
250 // if item send to character at another account, then apply item delivery delay
251 needItemDelay
= pl
->GetSession()->GetAccountId() != rc_account
;
254 if(money
> 0 && GetSecurity() > SEC_PLAYER
&& sWorld
.getConfig(CONFIG_GM_LOG_TRADE
))
256 sLog
.outCommand(GetAccountId(),"GM %s (Account: %u) mail money: %u to player: %s (Account: %u)",
257 GetPlayerName(), GetAccountId(), money
, receiver
.c_str(), rc_account
);
261 // If theres is an item, there is a one hour delivery delay if sent to another account's character.
262 uint32 deliver_delay
= needItemDelay
? sWorld
.getConfig(CONFIG_MAIL_DELIVERY_DELAY
) : 0;
264 // will delete item or place to receiver mail list
265 WorldSession::SendMailTo(receive
, MAIL_NORMAL
, MAIL_STATIONERY_NORMAL
, pl
->GetGUIDLow(), GUID_LOPART(rc
), subject
, itemTextId
, &mi
, money
, COD
, MAIL_CHECK_MASK_NONE
, deliver_delay
);
267 CharacterDatabase
.BeginTransaction();
268 pl
->SaveInventoryAndGoldToDB();
269 CharacterDatabase
.CommitTransaction();
272 //called when mail is read
273 void WorldSession::HandleMailMarkAsRead(WorldPacket
& recv_data
)
275 CHECK_PACKET_SIZE(recv_data
,8+4);
279 recv_data
>> mailbox
;
281 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
285 Player
*pl
= _player
;
286 Mail
*m
= pl
->GetMail(mailId
);
291 m
->checked
= m
->checked
| MAIL_CHECK_MASK_READ
;
292 // m->expire_time = time(NULL) + (30 * DAY); // Expire time do not change at reading mail
293 pl
->m_mailsUpdated
= true;
294 m
->state
= MAIL_STATE_CHANGED
;
298 //called when client deletes mail
299 void WorldSession::HandleMailDelete(WorldPacket
& recv_data
)
301 CHECK_PACKET_SIZE(recv_data
,8+4);
305 recv_data
>> mailbox
;
308 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
311 Player
* pl
= _player
;
312 pl
->m_mailsUpdated
= true;
313 Mail
*m
= pl
->GetMail(mailId
);
315 m
->state
= MAIL_STATE_DELETED
;
316 pl
->SendMailResult(mailId
, MAIL_DELETED
, 0);
319 void WorldSession::HandleMailReturnToSender(WorldPacket
& recv_data
)
321 CHECK_PACKET_SIZE(recv_data
,8+4);
325 recv_data
>> mailbox
;
327 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
331 Player
*pl
= _player
;
332 Mail
*m
= pl
->GetMail(mailId
);
333 if(!m
|| m
->state
== MAIL_STATE_DELETED
|| m
->deliver_time
> time(NULL
))
335 pl
->SendMailResult(mailId
, MAIL_RETURNED_TO_SENDER
, MAIL_ERR_INTERNAL_ERROR
);
338 //we can return mail now
339 //so firstly delete the old one
340 CharacterDatabase
.BeginTransaction();
341 CharacterDatabase
.PExecute("DELETE FROM mail WHERE id = '%u'", mailId
);
343 CharacterDatabase
.PExecute("DELETE FROM mail_items WHERE mail_id = '%u'", mailId
);
344 CharacterDatabase
.CommitTransaction();
345 pl
->RemoveMail(mailId
);
351 for(std::vector
<MailItemInfo
>::iterator itr2
= m
->items
.begin(); itr2
!= m
->items
.end(); ++itr2
)
353 Item
*item
= pl
->GetMItem(itr2
->item_guid
);
355 mi
.AddItem(item
->GetGUIDLow(), item
->GetEntry(), item
);
361 pl
->RemoveMItem(itr2
->item_guid
);
365 SendReturnToSender(MAIL_NORMAL
, GetAccountId(), m
->receiver
, m
->sender
, m
->subject
, m
->itemTextId
, &mi
, m
->money
, m
->mailTemplateId
);
367 delete m
; //we can deallocate old mail
368 pl
->SendMailResult(mailId
, MAIL_RETURNED_TO_SENDER
, 0);
371 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
)
373 if(messageType
!= MAIL_NORMAL
) // return only to players
375 mi
->deleteIncludedItems(true);
379 Player
*receiver
= objmgr
.GetPlayer(MAKE_NEW_GUID(receiver_guid
, 0, HIGHGUID_PLAYER
));
381 uint32 rc_account
= 0;
383 rc_account
= objmgr
.GetPlayerAccountIdByGUID(MAKE_NEW_GUID(receiver_guid
, 0, HIGHGUID_PLAYER
));
385 if(!receiver
&& !rc_account
) // sender not exist
387 mi
->deleteIncludedItems(true);
391 // prepare mail and send in other case
392 bool needItemDelay
= false;
394 if(mi
&& !mi
->empty())
396 // if item send to character at another account, then apply item delivery delay
397 needItemDelay
= sender_acc
!= rc_account
;
399 // set owner to new receiver (to prevent delete item with sender char deleting)
400 CharacterDatabase
.BeginTransaction();
401 for(MailItemMap::iterator mailItemIter
= mi
->begin(); mailItemIter
!= mi
->end(); ++mailItemIter
)
403 MailItem
& mailItem
= mailItemIter
->second
;
404 mailItem
.item
->SaveToDB(); // item not in inventory and can be save standalone
405 // owner in data will set at mail receive and item extracting
406 CharacterDatabase
.PExecute("UPDATE item_instance SET owner_guid = '%u' WHERE guid='%u'", receiver_guid
, mailItem
.item
->GetGUIDLow());
408 CharacterDatabase
.CommitTransaction();
411 // If theres is an item, there is a one hour delivery delay.
412 uint32 deliver_delay
= needItemDelay
? sWorld
.getConfig(CONFIG_MAIL_DELIVERY_DELAY
) : 0;
414 // will delete item or place to receiver mail list
415 WorldSession::SendMailTo(receiver
, MAIL_NORMAL
, MAIL_STATIONERY_NORMAL
, sender_guid
, receiver_guid
, subject
, itemTextId
, mi
, money
, 0, MAIL_CHECK_MASK_RETURNED
,deliver_delay
,mailTemplateId
);
418 //called when player takes item attached in mail
419 void WorldSession::HandleMailTakeItem(WorldPacket
& recv_data
)
421 CHECK_PACKET_SIZE(recv_data
,8+4+4);
426 recv_data
>> mailbox
;
428 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
432 recv_data
>> itemId
; // item guid low?
433 Player
* pl
= _player
;
435 Mail
* m
= pl
->GetMail(mailId
);
436 if(!m
|| m
->state
== MAIL_STATE_DELETED
|| m
->deliver_time
> time(NULL
))
438 pl
->SendMailResult(mailId
, MAIL_ITEM_TAKEN
, MAIL_ERR_INTERNAL_ERROR
);
442 // prevent cheating with skip client money check
443 if(pl
->GetMoney() < m
->COD
)
445 pl
->SendMailResult(mailId
, MAIL_ITEM_TAKEN
, MAIL_ERR_NOT_ENOUGH_MONEY
);
449 Item
*it
= pl
->GetMItem(itemId
);
451 ItemPosCountVec dest
;
452 uint8 msg
= _player
->CanStoreItem( NULL_BAG
, NULL_SLOT
, dest
, it
, false );
453 if( msg
== EQUIP_ERR_OK
)
455 m
->RemoveItem(itemId
);
456 m
->removedItems
.push_back(itemId
);
458 if (m
->COD
> 0) //if there is COD, take COD money from player and send them to sender by mail
460 uint64 sender_guid
= MAKE_NEW_GUID(m
->sender
, 0, HIGHGUID_PLAYER
);
461 Player
*receive
= objmgr
.GetPlayer(sender_guid
);
463 uint32 sender_accId
= 0;
465 if( GetSecurity() > SEC_PLAYER
&& sWorld
.getConfig(CONFIG_GM_LOG_TRADE
) )
467 std::string sender_name
;
470 sender_accId
= receive
->GetSession()->GetAccountId();
471 sender_name
= receive
->GetName();
475 // can be calculated early
476 sender_accId
= objmgr
.GetPlayerAccountIdByGUID(sender_guid
);
478 if(!objmgr
.GetPlayerNameByGUID(sender_guid
,sender_name
))
479 sender_name
= objmgr
.GetMangosStringForDBCLocale(LANG_UNKNOWN
);
481 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)",
482 GetPlayerName(),GetAccountId(),it
->GetProto()->Name1
,it
->GetEntry(),it
->GetCount(),m
->COD
,sender_name
.c_str(),sender_accId
);
485 sender_accId
= objmgr
.GetPlayerAccountIdByGUID(sender_guid
);
487 // check player existence
488 if(receive
|| sender_accId
)
490 WorldSession::SendMailTo(receive
, MAIL_NORMAL
, MAIL_STATIONERY_NORMAL
, m
->receiver
, m
->sender
, m
->subject
, 0, NULL
, m
->COD
, 0, MAIL_CHECK_MASK_COD_PAYMENT
);
493 pl
->ModifyMoney( -int32(m
->COD
) );
496 m
->state
= MAIL_STATE_CHANGED
;
497 pl
->m_mailsUpdated
= true;
498 pl
->RemoveMItem(it
->GetGUIDLow());
500 uint32 count
= it
->GetCount(); // save counts before store and possible merge with deleting
501 pl
->MoveItemToInventory(dest
,it
,true);
503 CharacterDatabase
.BeginTransaction();
504 pl
->SaveInventoryAndGoldToDB();
506 CharacterDatabase
.CommitTransaction();
508 pl
->SendMailResult(mailId
, MAIL_ITEM_TAKEN
, MAIL_OK
, 0, itemId
, count
);
511 pl
->SendMailResult(mailId
, MAIL_ITEM_TAKEN
, MAIL_ERR_BAG_FULL
, msg
);
514 void WorldSession::HandleMailTakeMoney(WorldPacket
& recv_data
)
516 CHECK_PACKET_SIZE(recv_data
,8+4);
520 recv_data
>> mailbox
;
523 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
526 Player
*pl
= _player
;
528 Mail
* m
= pl
->GetMail(mailId
);
529 if(!m
|| m
->state
== MAIL_STATE_DELETED
|| m
->deliver_time
> time(NULL
))
531 pl
->SendMailResult(mailId
, MAIL_MONEY_TAKEN
, MAIL_ERR_INTERNAL_ERROR
);
535 pl
->SendMailResult(mailId
, MAIL_MONEY_TAKEN
, 0);
537 pl
->ModifyMoney(m
->money
);
539 m
->state
= MAIL_STATE_CHANGED
;
540 pl
->m_mailsUpdated
= true;
542 // save money and mail to prevent cheating
543 CharacterDatabase
.BeginTransaction();
544 pl
->SaveDataFieldToDB(); // contains money
546 CharacterDatabase
.CommitTransaction();
549 //called when player lists his received mails
550 void WorldSession::HandleGetMailList(WorldPacket
& recv_data
)
552 CHECK_PACKET_SIZE(recv_data
,8);
555 recv_data
>> mailbox
;
557 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
560 Player
* pl
= _player
;
562 //load players mails, and mailed items
563 if(!pl
->m_mailsLoaded
)
566 // client can't work with packets > max int16 value
567 const uint32 maxPacketSize
= 32767;
569 uint32 mails_count
= 0; // real send to client mails amount
571 WorldPacket
data(SMSG_MAIL_LIST_RESULT
, (200)); // guess size
572 data
<< uint8(0); // mail's count
573 time_t cur_time
= time(NULL
);
575 for(PlayerMails::iterator itr
= pl
->GetmailBegin(); itr
!= pl
->GetmailEnd(); ++itr
)
577 // skip deleted or not delivered (deliver delay not expired) mails
578 if ((*itr
)->state
== MAIL_STATE_DELETED
|| cur_time
< (*itr
)->deliver_time
)
581 uint8 item_count
= (*itr
)->items
.size(); // max count is MAX_MAIL_ITEMS (12)
583 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);
585 if(data
.wpos()+next_mail_size
> maxPacketSize
)
588 data
<< (uint16
) 0x0040; // unknown 2.3.0, different values
589 data
<< (uint32
) (*itr
)->messageID
; // Message ID
590 data
<< (uint8
) (*itr
)->messageType
; // Message Type
592 switch((*itr
)->messageType
)
594 case MAIL_NORMAL
: // sender guid
595 data
<< uint64(MAKE_NEW_GUID((*itr
)->sender
, 0, HIGHGUID_PLAYER
));
598 case MAIL_GAMEOBJECT
:
600 data
<< (uint32
) (*itr
)->sender
; // creature/gameobject entry, auction id
602 case MAIL_ITEM
: // item entry (?) sender = "Unknown", NYI
606 data
<< (uint32
) (*itr
)->COD
; // COD
607 data
<< (uint32
) (*itr
)->itemTextId
; // sure about this
608 data
<< (uint32
) 0; // unknown
609 data
<< (uint32
) (*itr
)->stationery
; // stationery (Stationery.dbc)
610 data
<< (uint32
) (*itr
)->money
; // Gold
611 data
<< (uint32
) 0x04; // unknown, 0x4 - auction, 0x10 - normal
613 data
<< (float) ((*itr
)->expire_time
-time(NULL
))/DAY
;
614 data
<< (uint32
) (*itr
)->mailTemplateId
; // mail template (MailTemplate.dbc)
615 data
<< (*itr
)->subject
; // Subject string - once 00, when mail type = 3
617 data
<< (uint8
) item_count
; // client limit is 0x10
619 for(uint8 i
= 0; i
< item_count
; ++i
)
621 Item
*item
= pl
->GetMItem((*itr
)->items
[i
].item_guid
);
625 data
<< (uint32
) (item
? item
->GetGUIDLow() : 0);
627 data
<< (uint32
) (item
? item
->GetEntry() : 0);
628 for(uint8 j
= 0; j
< MAX_INSPECTED_ENCHANTMENT_SLOT
; ++j
)
631 data
<< (uint32
) (item
? item
->GetEnchantmentCharges((EnchantmentSlot
)j
) : 0);
633 data
<< (uint32
) (item
? item
->GetEnchantmentDuration((EnchantmentSlot
)j
) : 0);
635 data
<< (uint32
) (item
? item
->GetEnchantmentId((EnchantmentSlot
)j
) : 0);
638 data
<< (uint32
) (item
? item
->GetItemRandomPropertyId() : 0);
640 data
<< (uint32
) (item
? item
->GetItemSuffixFactor() : 0);
642 data
<< (uint32
) (item
? item
->GetCount() : 0);
644 data
<< (uint32
) (item
? item
->GetSpellCharges() : 0);
646 data
<< (uint32
) (item
? item
->GetUInt32Value(ITEM_FIELD_MAXDURABILITY
) : 0);
648 data
<< (uint32
) (item
? item
->GetUInt32Value(ITEM_FIELD_DURABILITY
) : 0);
656 data
.put
<uint8
>(0, mails_count
); // set real send mails to client
659 // recalculate m_nextMailDelivereTime and unReadMails
660 _player
->UpdateNextMailTimeAndUnreads();
663 ///this function is called when client needs mail message body, or when player clicks on item which has ITEM_FIELD_ITEM_TEXT_ID > 0
664 void WorldSession::HandleItemTextQuery(WorldPacket
& recv_data
)
666 CHECK_PACKET_SIZE(recv_data
,4+4+4);
669 uint32 mailId
; //this value can be item id in bag, but it is also mail id
670 uint32 unk
; //maybe something like state - 0x70000000
672 recv_data
>> itemTextId
>> mailId
>> unk
;
674 //some check needed, if player has item with guid mailId, or has mail with id mailId
676 sLog
.outDebug("CMSG_ITEM_TEXT_QUERY itemguid: %u, mailId: %u, unk: %u", itemTextId
, mailId
, unk
);
678 WorldPacket
data(SMSG_ITEM_TEXT_QUERY_RESPONSE
, (4+10));// guess size
680 data
<< objmgr
.GetItemText( itemTextId
);
684 //used when player copies mail body to his inventory
685 void WorldSession::HandleMailCreateTextItem(WorldPacket
& recv_data
)
687 CHECK_PACKET_SIZE(recv_data
,8+4);
692 recv_data
>> mailbox
>> mailId
;
694 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
697 Player
*pl
= _player
;
699 Mail
* m
= pl
->GetMail(mailId
);
700 if(!m
|| !m
->itemTextId
|| m
->state
== MAIL_STATE_DELETED
|| m
->deliver_time
> time(NULL
))
702 pl
->SendMailResult(mailId
, MAIL_MADE_PERMANENT
, MAIL_ERR_INTERNAL_ERROR
);
706 Item
*bodyItem
= new Item
; // This is not bag and then can be used new Item.
707 if(!bodyItem
->Create(objmgr
.GenerateLowGuid(HIGHGUID_ITEM
), MAIL_BODY_ITEM_TEMPLATE
, pl
))
713 bodyItem
->SetUInt32Value( ITEM_FIELD_ITEM_TEXT_ID
, m
->itemTextId
);
714 bodyItem
->SetUInt32Value( ITEM_FIELD_CREATOR
, m
->sender
);
716 sLog
.outDetail("HandleMailCreateTextItem mailid=%u",mailId
);
718 ItemPosCountVec dest
;
719 uint8 msg
= _player
->CanStoreItem( NULL_BAG
, NULL_SLOT
, dest
, bodyItem
, false );
720 if( msg
== EQUIP_ERR_OK
)
723 m
->state
= MAIL_STATE_CHANGED
;
724 pl
->m_mailsUpdated
= true;
726 pl
->StoreItem(dest
, bodyItem
, true);
727 //bodyItem->SetState(ITEM_NEW, pl); is set automatically
728 pl
->SendMailResult(mailId
, MAIL_MADE_PERMANENT
, 0);
732 pl
->SendMailResult(mailId
, MAIL_MADE_PERMANENT
, MAIL_ERR_BAG_FULL
, msg
);
737 //TODO Fix me! ... this void has probably bad condition, but good data are sent
738 void WorldSession::HandleQueryNextMailTime(WorldPacket
& /*recv_data*/ )
740 WorldPacket
data(MSG_QUERY_NEXT_MAIL_TIME
, 8);
742 if(!_player
->m_mailsLoaded
)
743 _player
->_LoadMail();
745 if( _player
->unReadMails
> 0 )
747 data
<< (uint32
) 0; // float
748 data
<< (uint32
) 0; // count
750 for(PlayerMails::iterator itr
= _player
->GetmailBegin(); itr
!= _player
->GetmailEnd(); ++itr
)
753 // not checked yet, already must be delivered
754 if((m
->checked
& MAIL_CHECK_MASK_READ
)==0 && (m
->deliver_time
<= time(NULL
)))
764 data
<< (uint64
) m
->sender
; // sender guid
766 switch(m
->messageType
)
771 data
<< (uint32
) m
->stationery
;
776 data
<< (uint32
) m
->stationery
;
779 data
<< (uint32
) 0xC6000000; // float unk, time or something
782 data
.put
<uint32
>(4, count
);
786 data
<< (uint32
) 0xC7A8C000;
787 data
<< (uint32
) 0x00000000;
792 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
)
794 uint32 mailId
= objmgr
.GenerateMailID();
796 time_t deliver_time
= time(NULL
) + deliver_delay
;
798 //expire time if COD 3 days, if no COD 30 days, if auction sale pending 1 hour
800 if(messageType
== MAIL_AUCTION
&& !mi
&& !money
) // auction mail without any items and money
803 expire_delay
= (COD
> 0) ? 3*DAY
: 30*DAY
;
805 time_t expire_time
= deliver_time
+ expire_delay
;
807 if(mailTemplateId
&& !sMailTemplateStore
.LookupEntry(mailTemplateId
))
809 sLog
.outError( "WorldSession::SendMailTo - Mail have not existed MailTemplateId (%u), remove at send", mailTemplateId
);
815 receiver
->AddNewMailDeliverTime(deliver_time
);
817 if ( receiver
->IsMailsLoaded() )
820 m
->messageID
= mailId
;
821 m
->messageType
= messageType
;
822 m
->stationery
= stationery
;
823 m
->mailTemplateId
= mailTemplateId
;
824 m
->sender
= sender_guidlow_or_entry
;
825 m
->receiver
= receiver
->GetGUIDLow();
826 m
->subject
= subject
;
827 m
->itemTextId
= itemTextId
;
832 m
->expire_time
= expire_time
;
833 m
->deliver_time
= deliver_time
;
836 m
->checked
= checked
;
837 m
->state
= MAIL_STATE_UNCHANGED
;
839 receiver
->AddMail(m
); //to insert new mail to beginning of maillist
843 for(MailItemMap::iterator mailItemIter
= mi
->begin(); mailItemIter
!= mi
->end(); ++mailItemIter
)
845 MailItem
& mailItem
= mailItemIter
->second
;
847 receiver
->AddMItem(mailItem
.item
);
852 mi
->deleteIncludedItems();
855 mi
->deleteIncludedItems();
857 CharacterDatabase
.BeginTransaction();
858 CharacterDatabase
.escape_string(subject
);
859 CharacterDatabase
.PExecute("INSERT INTO mail (id,messageType,stationery,mailTemplateId,sender,receiver,subject,itemTextId,has_items,expire_time,deliver_time,money,cod,checked) "
860 "VALUES ('%u', '%u', '%u', '%u', '%u', '%u', '%s', '%u', '%u', '" I64FMTD
"','" I64FMTD
"', '%u', '%u', '%d')",
861 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
);
865 for(MailItemMap::const_iterator mailItemIter
= mi
->begin(); mailItemIter
!= mi
->end(); ++mailItemIter
)
867 MailItem
const& mailItem
= mailItemIter
->second
;
868 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
);
871 CharacterDatabase
.CommitTransaction();