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"
34 MAIL_SHOW_UNK0
= 0x0001,
35 MAIL_SHOW_DELETE
= 0x0002, // forced show delete button instead return button
36 MAIL_SHOW_AUCTION
= 0x0004, // from old comment
37 MAIL_SHOW_UNK2
= 0x0008, // unknown, COD will be shown even without that flag
38 MAIL_SHOW_RETURN
= 0x0010,
41 void MailItem::deleteItem( bool inDB
)
46 CharacterDatabase
.PExecute("DELETE FROM item_instance WHERE guid='%u'", item
->GetGUIDLow());
53 void WorldSession::HandleSendMail(WorldPacket
& recv_data
)
56 std::string receiver
, subject
, body
;
57 uint32 unk1
, unk2
, money
, COD
;
60 recv_data
>> receiver
;
62 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
69 recv_data
>> unk1
; // stationery?
70 recv_data
>> unk2
; // 0x00000000
75 recv_data
>> items_count
; // attached items count
77 if(items_count
> 12) // client limit
79 GetPlayer()->SendMailResult(0, MAIL_SEND
, MAIL_ERR_TOO_MANY_ATTACHMENTS
);
85 for(uint8 i
= 0; i
< items_count
; ++i
)
89 recv_data
>> item_slot
;
90 recv_data
>> item_guid
;
91 mi
.AddItem(GUID_LOPART(item_guid
), item_slot
);
95 recv_data
>> money
>> COD
; // money and cod
96 recv_data
>> unk3
; // const 0
97 recv_data
>> unk4
; // const 0
99 items_count
= mi
.size(); // this is the real size after the duplicates have been removed
101 if (receiver
.empty())
104 Player
* pl
= _player
;
107 if(normalizePlayerName(receiver
))
108 rc
= objmgr
.GetPlayerGUIDByName(receiver
);
112 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",
113 pl
->GetGUIDLow(), receiver
.c_str(), subject
.c_str(), body
.c_str(), items_count
, money
, COD
, unk1
, unk2
);
114 pl
->SendMailResult(0, MAIL_SEND
, MAIL_ERR_RECIPIENT_NOT_FOUND
);
118 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 if(pl
->GetGUID() == rc
)
122 pl
->SendMailResult(0, MAIL_SEND
, MAIL_ERR_CANNOT_SEND_TO_SELF
);
126 uint32 cost
= items_count
? 30 * items_count
: 30; // price hardcoded in client
128 uint32 reqmoney
= cost
+ money
;
130 if (pl
->GetMoney() < reqmoney
)
132 pl
->SendMailResult(0, MAIL_SEND
, MAIL_ERR_NOT_ENOUGH_MONEY
);
136 Player
*receive
= objmgr
.GetPlayer(rc
);
139 uint8 mails_count
= 0; //do not allow to send to one player more than 100 mails
143 rc_team
= receive
->GetTeam();
144 mails_count
= receive
->GetMailSize();
148 rc_team
= objmgr
.GetPlayerTeamByGUID(rc
);
149 QueryResult
* result
= CharacterDatabase
.PQuery("SELECT COUNT(*) FROM mail WHERE receiver = '%u'", GUID_LOPART(rc
));
152 Field
*fields
= result
->Fetch();
153 mails_count
= fields
[0].GetUInt32();
157 //do not allow to have more than 100 mails in mailbox.. mails count is in opcode uint8!!! - so max can be 255..
158 if (mails_count
> 100)
160 pl
->SendMailResult(0, MAIL_SEND
, MAIL_ERR_RECIPIENT_CAP_REACHED
);
163 // test the receiver's Faction...
164 if (!sWorld
.getConfig(CONFIG_ALLOW_TWO_SIDE_INTERACTION_MAIL
) && pl
->GetTeam() != rc_team
&& GetSecurity() == SEC_PLAYER
)
166 pl
->SendMailResult(0, MAIL_SEND
, MAIL_ERR_NOT_YOUR_TEAM
);
170 uint32 rc_account
= 0;
172 rc_account
= receive
->GetSession()->GetAccountId();
174 rc_account
= objmgr
.GetPlayerAccountIdByGUID(rc
);
178 for(MailItemMap::iterator mailItemIter
= mi
.begin(); mailItemIter
!= mi
.end(); ++mailItemIter
)
180 MailItem
& mailItem
= mailItemIter
->second
;
182 if(!mailItem
.item_guidlow
)
184 pl
->SendMailResult(0, MAIL_SEND
, MAIL_ERR_MAIL_ATTACHMENT_INVALID
);
188 mailItem
.item
= pl
->GetItemByGuid(MAKE_NEW_GUID(mailItem
.item_guidlow
, 0, HIGHGUID_ITEM
));
189 // prevent sending bag with items (cheat: can be placed in bag after adding equipped empty bag to mail)
192 pl
->SendMailResult(0, MAIL_SEND
, MAIL_ERR_MAIL_ATTACHMENT_INVALID
);
196 if(!mailItem
.item
->CanBeTraded(true))
198 pl
->SendMailResult(0, MAIL_SEND
, MAIL_ERR_EQUIP_ERROR
, EQUIP_ERR_MAIL_BOUND_ITEM
);
202 if(mailItem
.item
->IsBoundAccountWide() && mailItem
.item
->IsSoulBound() && pl
->GetSession()->GetAccountId() != rc_account
)
204 pl
->SendMailResult(0, MAIL_SEND
, MAIL_ERR_EQUIP_ERROR
, EQUIP_ERR_ARTEFACTS_ONLY_FOR_OWN_CHARACTERS
);
208 if (mailItem
.item
->HasFlag(ITEM_FIELD_FLAGS
, ITEM_FLAGS_CONJURED
) || mailItem
.item
->GetUInt32Value(ITEM_FIELD_DURATION
))
210 pl
->SendMailResult(0, MAIL_SEND
, MAIL_ERR_EQUIP_ERROR
, EQUIP_ERR_MAIL_BOUND_ITEM
);
214 if(COD
&& mailItem
.item
->HasFlag(ITEM_FIELD_FLAGS
, ITEM_FLAGS_WRAPPED
))
216 pl
->SendMailResult(0, MAIL_SEND
, MAIL_ERR_CANT_SEND_WRAPPED_COD
);
221 pl
->SendMailResult(0, MAIL_SEND
, MAIL_OK
);
223 uint32 itemTextId
= 0;
226 itemTextId
= objmgr
.CreateItemText( body
);
229 pl
->ModifyMoney( -int32(reqmoney
) );
230 pl
->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_GOLD_SPENT_FOR_MAIL
, cost
);
232 bool needItemDelay
= false;
234 if(items_count
> 0 || money
> 0)
238 for(MailItemMap::iterator mailItemIter
= mi
.begin(); mailItemIter
!= mi
.end(); ++mailItemIter
)
240 MailItem
& mailItem
= mailItemIter
->second
;
244 mailItem
.item_template
= mailItem
.item
? mailItem
.item
->GetEntry() : 0;
246 if( GetSecurity() > SEC_PLAYER
&& sWorld
.getConfig(CONFIG_GM_LOG_TRADE
) )
248 sLog
.outCommand(GetAccountId(), "GM %s (Account: %u) mail item: %s (Entry: %u Count: %u) to player: %s (Account: %u)",
249 GetPlayerName(), GetAccountId(), mailItem
.item
->GetProto()->Name1
, mailItem
.item
->GetEntry(), mailItem
.item
->GetCount(), receiver
.c_str(), rc_account
);
252 pl
->MoveItemFromInventory(mailItem
.item
->GetBagSlot(), mailItem
.item
->GetSlot(), true);
253 CharacterDatabase
.BeginTransaction();
254 mailItem
.item
->DeleteFromInventoryDB(); //deletes item from character's inventory
255 mailItem
.item
->SaveToDB(); // recursive and not have transaction guard into self, item not in inventory and can be save standalone
256 // owner in data will set at mail receive and item extracting
257 CharacterDatabase
.PExecute("UPDATE item_instance SET owner_guid = '%u' WHERE guid='%u'", GUID_LOPART(rc
), mailItem
.item
->GetGUIDLow());
258 CharacterDatabase
.CommitTransaction();
261 // if item send to character at another account, then apply item delivery delay
262 needItemDelay
= pl
->GetSession()->GetAccountId() != rc_account
;
265 if(money
> 0 && GetSecurity() > SEC_PLAYER
&& sWorld
.getConfig(CONFIG_GM_LOG_TRADE
))
267 sLog
.outCommand(GetAccountId(),"GM %s (Account: %u) mail money: %u to player: %s (Account: %u)",
268 GetPlayerName(), GetAccountId(), money
, receiver
.c_str(), rc_account
);
272 // If theres is an item, there is a one hour delivery delay if sent to another account's character.
273 uint32 deliver_delay
= needItemDelay
? sWorld
.getConfig(CONFIG_MAIL_DELIVERY_DELAY
) : 0;
275 // will delete item or place to receiver mail list
276 WorldSession::SendMailTo(receive
, MAIL_NORMAL
, MAIL_STATIONERY_NORMAL
, pl
->GetGUIDLow(), GUID_LOPART(rc
), subject
, itemTextId
, &mi
, money
, COD
, MAIL_CHECK_MASK_NONE
, deliver_delay
);
278 CharacterDatabase
.BeginTransaction();
279 pl
->SaveInventoryAndGoldToDB();
280 CharacterDatabase
.CommitTransaction();
283 //called when mail is read
284 void WorldSession::HandleMailMarkAsRead(WorldPacket
& recv_data
)
288 recv_data
>> mailbox
;
290 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
294 Player
*pl
= _player
;
295 Mail
*m
= pl
->GetMail(mailId
);
300 m
->checked
= m
->checked
| MAIL_CHECK_MASK_READ
;
301 // m->expire_time = time(NULL) + (30 * DAY); // Expire time do not change at reading mail
302 pl
->m_mailsUpdated
= true;
303 m
->state
= MAIL_STATE_CHANGED
;
307 //called when client deletes mail
308 void WorldSession::HandleMailDelete(WorldPacket
& recv_data
)
312 recv_data
>> mailbox
;
315 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
318 Player
* pl
= _player
;
319 pl
->m_mailsUpdated
= true;
320 Mail
*m
= pl
->GetMail(mailId
);
323 // delete shouldn't show up for COD mails
326 pl
->SendMailResult(mailId
, MAIL_DELETED
, MAIL_ERR_INTERNAL_ERROR
);
330 m
->state
= MAIL_STATE_DELETED
;
332 pl
->SendMailResult(mailId
, MAIL_DELETED
, MAIL_OK
);
335 void WorldSession::HandleMailReturnToSender(WorldPacket
& recv_data
)
339 recv_data
>> mailbox
;
341 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
345 Player
*pl
= _player
;
346 Mail
*m
= pl
->GetMail(mailId
);
347 if(!m
|| m
->state
== MAIL_STATE_DELETED
|| m
->deliver_time
> time(NULL
))
349 pl
->SendMailResult(mailId
, MAIL_RETURNED_TO_SENDER
, MAIL_ERR_INTERNAL_ERROR
);
352 //we can return mail now
353 //so firstly delete the old one
354 CharacterDatabase
.BeginTransaction();
355 CharacterDatabase
.PExecute("DELETE FROM mail WHERE id = '%u'", mailId
);
357 CharacterDatabase
.PExecute("DELETE FROM mail_items WHERE mail_id = '%u'", mailId
);
358 CharacterDatabase
.CommitTransaction();
359 pl
->RemoveMail(mailId
);
365 for(std::vector
<MailItemInfo
>::iterator itr2
= m
->items
.begin(); itr2
!= m
->items
.end(); ++itr2
)
367 Item
*item
= pl
->GetMItem(itr2
->item_guid
);
369 mi
.AddItem(item
->GetGUIDLow(), item
->GetEntry(), item
);
375 pl
->RemoveMItem(itr2
->item_guid
);
379 SendReturnToSender(MAIL_NORMAL
, GetAccountId(), m
->receiver
, m
->sender
, m
->subject
, m
->itemTextId
, &mi
, m
->money
, m
->mailTemplateId
);
381 delete m
; //we can deallocate old mail
382 pl
->SendMailResult(mailId
, MAIL_RETURNED_TO_SENDER
, MAIL_OK
);
385 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
)
387 if(messageType
!= MAIL_NORMAL
) // return only to players
389 mi
->deleteIncludedItems(true);
393 Player
*receiver
= objmgr
.GetPlayer(MAKE_NEW_GUID(receiver_guid
, 0, HIGHGUID_PLAYER
));
395 uint32 rc_account
= 0;
397 rc_account
= objmgr
.GetPlayerAccountIdByGUID(MAKE_NEW_GUID(receiver_guid
, 0, HIGHGUID_PLAYER
));
399 if(!receiver
&& !rc_account
) // sender not exist
401 mi
->deleteIncludedItems(true);
405 // prepare mail and send in other case
406 bool needItemDelay
= false;
408 if(mi
&& !mi
->empty())
410 // if item send to character at another account, then apply item delivery delay
411 needItemDelay
= sender_acc
!= rc_account
;
413 // set owner to new receiver (to prevent delete item with sender char deleting)
414 CharacterDatabase
.BeginTransaction();
415 for(MailItemMap::iterator mailItemIter
= mi
->begin(); mailItemIter
!= mi
->end(); ++mailItemIter
)
417 MailItem
& mailItem
= mailItemIter
->second
;
418 mailItem
.item
->SaveToDB(); // item not in inventory and can be save standalone
419 // owner in data will set at mail receive and item extracting
420 CharacterDatabase
.PExecute("UPDATE item_instance SET owner_guid = '%u' WHERE guid='%u'", receiver_guid
, mailItem
.item
->GetGUIDLow());
422 CharacterDatabase
.CommitTransaction();
425 // If theres is an item, there is a one hour delivery delay.
426 uint32 deliver_delay
= needItemDelay
? sWorld
.getConfig(CONFIG_MAIL_DELIVERY_DELAY
) : 0;
428 // will delete item or place to receiver mail list
429 WorldSession::SendMailTo(receiver
, MAIL_NORMAL
, MAIL_STATIONERY_NORMAL
, sender_guid
, receiver_guid
, subject
, itemTextId
, mi
, money
, 0, MAIL_CHECK_MASK_RETURNED
,deliver_delay
,mailTemplateId
);
432 //called when player takes item attached in mail
433 void WorldSession::HandleMailTakeItem(WorldPacket
& recv_data
)
438 recv_data
>> mailbox
;
440 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
444 recv_data
>> itemId
; // item guid low?
445 Player
* pl
= _player
;
447 Mail
* m
= pl
->GetMail(mailId
);
448 if(!m
|| m
->state
== MAIL_STATE_DELETED
|| m
->deliver_time
> time(NULL
))
450 pl
->SendMailResult(mailId
, MAIL_ITEM_TAKEN
, MAIL_ERR_INTERNAL_ERROR
);
454 // prevent cheating with skip client money check
455 if(pl
->GetMoney() < m
->COD
)
457 pl
->SendMailResult(mailId
, MAIL_ITEM_TAKEN
, MAIL_ERR_NOT_ENOUGH_MONEY
);
461 Item
*it
= pl
->GetMItem(itemId
);
463 ItemPosCountVec dest
;
464 uint8 msg
= _player
->CanStoreItem( NULL_BAG
, NULL_SLOT
, dest
, it
, false );
465 if( msg
== EQUIP_ERR_OK
)
467 m
->RemoveItem(itemId
);
468 m
->removedItems
.push_back(itemId
);
470 if (m
->COD
> 0) //if there is COD, take COD money from player and send them to sender by mail
472 uint64 sender_guid
= MAKE_NEW_GUID(m
->sender
, 0, HIGHGUID_PLAYER
);
473 Player
*receive
= objmgr
.GetPlayer(sender_guid
);
475 uint32 sender_accId
= 0;
477 if( GetSecurity() > SEC_PLAYER
&& sWorld
.getConfig(CONFIG_GM_LOG_TRADE
) )
479 std::string sender_name
;
482 sender_accId
= receive
->GetSession()->GetAccountId();
483 sender_name
= receive
->GetName();
487 // can be calculated early
488 sender_accId
= objmgr
.GetPlayerAccountIdByGUID(sender_guid
);
490 if(!objmgr
.GetPlayerNameByGUID(sender_guid
,sender_name
))
491 sender_name
= objmgr
.GetMangosStringForDBCLocale(LANG_UNKNOWN
);
493 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)",
494 GetPlayerName(),GetAccountId(),it
->GetProto()->Name1
,it
->GetEntry(),it
->GetCount(),m
->COD
,sender_name
.c_str(),sender_accId
);
497 sender_accId
= objmgr
.GetPlayerAccountIdByGUID(sender_guid
);
499 // check player existence
500 if(receive
|| sender_accId
)
502 WorldSession::SendMailTo(receive
, MAIL_NORMAL
, MAIL_STATIONERY_NORMAL
, m
->receiver
, m
->sender
, m
->subject
, 0, NULL
, m
->COD
, 0, MAIL_CHECK_MASK_COD_PAYMENT
);
505 pl
->ModifyMoney( -int32(m
->COD
) );
508 m
->state
= MAIL_STATE_CHANGED
;
509 pl
->m_mailsUpdated
= true;
510 pl
->RemoveMItem(it
->GetGUIDLow());
512 uint32 count
= it
->GetCount(); // save counts before store and possible merge with deleting
513 pl
->MoveItemToInventory(dest
,it
,true);
515 CharacterDatabase
.BeginTransaction();
516 pl
->SaveInventoryAndGoldToDB();
518 CharacterDatabase
.CommitTransaction();
520 pl
->SendMailResult(mailId
, MAIL_ITEM_TAKEN
, MAIL_OK
, 0, itemId
, count
);
523 pl
->SendMailResult(mailId
, MAIL_ITEM_TAKEN
, MAIL_ERR_EQUIP_ERROR
, msg
);
526 void WorldSession::HandleMailTakeMoney(WorldPacket
& recv_data
)
530 recv_data
>> mailbox
;
533 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
536 Player
*pl
= _player
;
538 Mail
* m
= pl
->GetMail(mailId
);
539 if(!m
|| m
->state
== MAIL_STATE_DELETED
|| m
->deliver_time
> time(NULL
))
541 pl
->SendMailResult(mailId
, MAIL_MONEY_TAKEN
, MAIL_ERR_INTERNAL_ERROR
);
545 pl
->SendMailResult(mailId
, MAIL_MONEY_TAKEN
, MAIL_OK
);
547 pl
->ModifyMoney(m
->money
);
549 m
->state
= MAIL_STATE_CHANGED
;
550 pl
->m_mailsUpdated
= true;
552 // save money and mail to prevent cheating
553 CharacterDatabase
.BeginTransaction();
556 CharacterDatabase
.CommitTransaction();
559 //called when player lists his received mails
560 void WorldSession::HandleGetMailList(WorldPacket
& recv_data
)
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 uint32 show_flags
= 0;
597 if ((*itr
)->messageType
!= MAIL_NORMAL
)
598 show_flags
|= MAIL_SHOW_DELETE
;
599 if ((*itr
)->messageType
== MAIL_AUCTION
)
600 show_flags
|= MAIL_SHOW_AUCTION
;
601 if ((*itr
)->HasItems() && (*itr
)->messageType
== MAIL_NORMAL
)
602 show_flags
|= MAIL_SHOW_RETURN
;
604 data
<< (uint16
) 0x0040; // unknown 2.3.0, different values
605 data
<< (uint32
) (*itr
)->messageID
; // Message ID
606 data
<< (uint8
) (*itr
)->messageType
; // Message Type
608 switch((*itr
)->messageType
)
610 case MAIL_NORMAL
: // sender guid
611 data
<< uint64(MAKE_NEW_GUID((*itr
)->sender
, 0, HIGHGUID_PLAYER
));
614 case MAIL_GAMEOBJECT
:
616 data
<< (uint32
) (*itr
)->sender
; // creature/gameobject entry, auction id
618 case MAIL_ITEM
: // item entry (?) sender = "Unknown", NYI
622 data
<< (uint32
) (*itr
)->COD
; // COD
623 data
<< (uint32
) (*itr
)->itemTextId
; // sure about this
624 data
<< (uint32
) 0; // unknown
625 data
<< (uint32
) (*itr
)->stationery
; // stationery (Stationery.dbc)
626 data
<< (uint32
) (*itr
)->money
; // Gold
627 data
<< (uint32
) show_flags
; // unknown, 0x4 - auction, 0x10 - normal
629 data
<< (float) ((*itr
)->expire_time
-time(NULL
))/DAY
;
630 data
<< (uint32
) (*itr
)->mailTemplateId
; // mail template (MailTemplate.dbc)
631 data
<< (*itr
)->subject
; // Subject string - once 00, when mail type = 3
633 data
<< (uint8
) item_count
; // client limit is 0x10
635 for(uint8 i
= 0; i
< item_count
; ++i
)
637 Item
*item
= pl
->GetMItem((*itr
)->items
[i
].item_guid
);
641 data
<< (uint32
) (item
? item
->GetGUIDLow() : 0);
643 data
<< (uint32
) (item
? item
->GetEntry() : 0);
644 for(uint8 j
= 0; j
< MAX_INSPECTED_ENCHANTMENT_SLOT
; ++j
)
647 data
<< (uint32
) (item
? item
->GetEnchantmentCharges((EnchantmentSlot
)j
) : 0);
649 data
<< (uint32
) (item
? item
->GetEnchantmentDuration((EnchantmentSlot
)j
) : 0);
651 data
<< (uint32
) (item
? item
->GetEnchantmentId((EnchantmentSlot
)j
) : 0);
654 data
<< (uint32
) (item
? item
->GetItemRandomPropertyId() : 0);
656 data
<< (uint32
) (item
? item
->GetItemSuffixFactor() : 0);
658 data
<< (uint32
) (item
? item
->GetCount() : 0);
660 data
<< (uint32
) (item
? item
->GetSpellCharges() : 0);
662 data
<< (uint32
) (item
? item
->GetUInt32Value(ITEM_FIELD_MAXDURABILITY
) : 0);
664 data
<< (uint32
) (item
? item
->GetUInt32Value(ITEM_FIELD_DURABILITY
) : 0);
672 data
.put
<uint8
>(0, mails_count
); // set real send mails to client
675 // recalculate m_nextMailDelivereTime and unReadMails
676 _player
->UpdateNextMailTimeAndUnreads();
679 ///this function is called when client needs mail message body, or when player clicks on item which has ITEM_FIELD_ITEM_TEXT_ID > 0
680 void WorldSession::HandleItemTextQuery(WorldPacket
& recv_data
)
683 uint32 mailId
; //this value can be item id in bag, but it is also mail id
684 uint32 unk
; //maybe something like state - 0x70000000
686 recv_data
>> itemTextId
>> mailId
>> unk
;
688 //some check needed, if player has item with guid mailId, or has mail with id mailId
690 sLog
.outDebug("CMSG_ITEM_TEXT_QUERY itemguid: %u, mailId: %u, unk: %u", itemTextId
, mailId
, unk
);
692 WorldPacket
data(SMSG_ITEM_TEXT_QUERY_RESPONSE
, (4+10));// guess size
694 data
<< objmgr
.GetItemText( itemTextId
);
698 //used when player copies mail body to his inventory
699 void WorldSession::HandleMailCreateTextItem(WorldPacket
& recv_data
)
704 recv_data
>> mailbox
>> mailId
;
706 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
709 Player
*pl
= _player
;
711 Mail
* m
= pl
->GetMail(mailId
);
712 if(!m
|| !m
->itemTextId
|| m
->state
== MAIL_STATE_DELETED
|| m
->deliver_time
> time(NULL
))
714 pl
->SendMailResult(mailId
, MAIL_MADE_PERMANENT
, MAIL_ERR_INTERNAL_ERROR
);
718 Item
*bodyItem
= new Item
; // This is not bag and then can be used new Item.
719 if(!bodyItem
->Create(objmgr
.GenerateLowGuid(HIGHGUID_ITEM
), MAIL_BODY_ITEM_TEMPLATE
, pl
))
725 bodyItem
->SetUInt32Value( ITEM_FIELD_ITEM_TEXT_ID
, m
->itemTextId
);
726 bodyItem
->SetUInt32Value( ITEM_FIELD_CREATOR
, m
->sender
);
728 sLog
.outDetail("HandleMailCreateTextItem mailid=%u",mailId
);
730 ItemPosCountVec dest
;
731 uint8 msg
= _player
->CanStoreItem( NULL_BAG
, NULL_SLOT
, dest
, bodyItem
, false );
732 if( msg
== EQUIP_ERR_OK
)
735 m
->state
= MAIL_STATE_CHANGED
;
736 pl
->m_mailsUpdated
= true;
738 pl
->StoreItem(dest
, bodyItem
, true);
739 //bodyItem->SetState(ITEM_NEW, pl); is set automatically
740 pl
->SendMailResult(mailId
, MAIL_MADE_PERMANENT
, MAIL_OK
);
744 pl
->SendMailResult(mailId
, MAIL_MADE_PERMANENT
, MAIL_ERR_EQUIP_ERROR
, msg
);
749 //TODO Fix me! ... this void has probably bad condition, but good data are sent
750 void WorldSession::HandleQueryNextMailTime(WorldPacket
& /*recv_data*/ )
752 WorldPacket
data(MSG_QUERY_NEXT_MAIL_TIME
, 8);
754 if(!_player
->m_mailsLoaded
)
755 _player
->_LoadMail();
757 if( _player
->unReadMails
> 0 )
759 data
<< (uint32
) 0; // float
760 data
<< (uint32
) 0; // count
763 time_t now
= time(NULL
);
764 for(PlayerMails::iterator itr
= _player
->GetmailBegin(); itr
!= _player
->GetmailEnd(); ++itr
)
767 // must be not checked yet
768 if(m
->checked
& MAIL_CHECK_MASK_READ
)
771 // and already delivered
772 if(now
< m
->deliver_time
)
775 data
<< (uint64
) m
->sender
; // sender guid
777 switch(m
->messageType
)
782 data
<< (uint32
) m
->stationery
;
787 data
<< (uint32
) m
->stationery
;
790 data
<< (uint32
) 0xC6000000; // float unk, time or something
793 if(count
== 2) // do not display more than 2 mails
796 data
.put
<uint32
>(4, count
);
800 data
<< (uint32
) 0xC7A8C000;
801 data
<< (uint32
) 0x00000000;
806 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
)
808 uint32 mailId
= objmgr
.GenerateMailID();
810 time_t deliver_time
= time(NULL
) + deliver_delay
;
812 //expire time if COD 3 days, if no COD 30 days, if auction sale pending 1 hour
814 if(messageType
== MAIL_AUCTION
&& !mi
&& !money
) // auction mail without any items and money
817 expire_delay
= (COD
> 0) ? 3*DAY
: 30*DAY
;
819 time_t expire_time
= deliver_time
+ expire_delay
;
821 if(mailTemplateId
&& !sMailTemplateStore
.LookupEntry(mailTemplateId
))
823 sLog
.outError( "WorldSession::SendMailTo - Mail have not existed MailTemplateId (%u), remove at send", mailTemplateId
);
829 receiver
->AddNewMailDeliverTime(deliver_time
);
831 if ( receiver
->IsMailsLoaded() )
834 m
->messageID
= mailId
;
835 m
->messageType
= messageType
;
836 m
->stationery
= stationery
;
837 m
->mailTemplateId
= mailTemplateId
;
838 m
->sender
= sender_guidlow_or_entry
;
839 m
->receiver
= receiver
->GetGUIDLow();
840 m
->subject
= subject
;
841 m
->itemTextId
= itemTextId
;
846 m
->expire_time
= expire_time
;
847 m
->deliver_time
= deliver_time
;
850 m
->checked
= checked
;
851 m
->state
= MAIL_STATE_UNCHANGED
;
853 receiver
->AddMail(m
); //to insert new mail to beginning of maillist
857 for(MailItemMap::iterator mailItemIter
= mi
->begin(); mailItemIter
!= mi
->end(); ++mailItemIter
)
859 MailItem
& mailItem
= mailItemIter
->second
;
861 receiver
->AddMItem(mailItem
.item
);
866 mi
->deleteIncludedItems();
869 mi
->deleteIncludedItems();
871 CharacterDatabase
.BeginTransaction();
872 CharacterDatabase
.escape_string(subject
);
873 CharacterDatabase
.PExecute("INSERT INTO mail (id,messageType,stationery,mailTemplateId,sender,receiver,subject,itemTextId,has_items,expire_time,deliver_time,money,cod,checked) "
874 "VALUES ('%u', '%u', '%u', '%u', '%u', '%u', '%s', '%u', '%u', '" UI64FMTD
"','" UI64FMTD
"', '%u', '%u', '%d')",
875 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
);
879 for(MailItemMap::const_iterator mailItemIter
= mi
->begin(); mailItemIter
!= mi
->end(); ++mailItemIter
)
881 MailItem
const& mailItem
= mailItemIter
->second
;
882 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
);
885 CharacterDatabase
.CommitTransaction();