2 * Copyright (C) 2005-2010 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"
26 #include "ObjectDefines.h"
28 #include "UpdateMask.h"
31 #include "DBCStores.h"
32 #include "BattleGroundMgr.h"
34 #include "AuctionHouseMgr.h"
38 MAIL_SHOW_UNK0
= 0x0001,
39 MAIL_SHOW_DELETE
= 0x0002, // forced show delete button instead return button
40 MAIL_SHOW_AUCTION
= 0x0004, // from old comment
41 MAIL_SHOW_UNK2
= 0x0008, // unknown, COD will be shown even without that flag
42 MAIL_SHOW_RETURN
= 0x0010,
45 void WorldSession::HandleSendMail(WorldPacket
& recv_data
)
48 std::string receiver
, subject
, body
;
49 uint32 unk1
, unk2
, money
, COD
;
52 recv_data
>> receiver
;
58 recv_data
>> unk1
; // stationery?
59 recv_data
>> unk2
; // 0x00000000
62 recv_data
>> items_count
; // attached items count
64 if (items_count
> MAX_MAIL_ITEMS
) // client limit
66 GetPlayer()->SendMailResult(0, MAIL_SEND
, MAIL_ERR_TOO_MANY_ATTACHMENTS
);
67 recv_data
.rpos(recv_data
.wpos()); // set to end to avoid warnings spam
71 uint64 itemGUIDs
[MAX_MAIL_ITEMS
];
73 for(uint8 i
= 0; i
< items_count
; ++i
)
75 recv_data
.read_skip
<uint8
>(); // item slot in mail, not used
76 recv_data
>> itemGUIDs
[i
];
79 recv_data
>> money
>> COD
; // money and cod
80 recv_data
>> unk3
; // const 0
81 recv_data
>> unk4
; // const 0
83 // packet read complete, now do check
85 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
94 if (normalizePlayerName(receiver
))
95 rc
= sObjectMgr
.GetPlayerGUIDByName(receiver
);
99 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",
100 pl
->GetGUIDLow(), receiver
.c_str(), subject
.c_str(), body
.c_str(), items_count
, money
, COD
, unk1
, unk2
);
101 pl
->SendMailResult(0, MAIL_SEND
, MAIL_ERR_RECIPIENT_NOT_FOUND
);
105 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
);
107 if (pl
->GetGUID() == rc
)
109 pl
->SendMailResult(0, MAIL_SEND
, MAIL_ERR_CANNOT_SEND_TO_SELF
);
113 uint32 cost
= items_count
? 30 * items_count
: 30; // price hardcoded in client
115 uint32 reqmoney
= cost
+ money
;
117 if (pl
->GetMoney() < reqmoney
)
119 pl
->SendMailResult(0, MAIL_SEND
, MAIL_ERR_NOT_ENOUGH_MONEY
);
123 Player
*receive
= sObjectMgr
.GetPlayer(rc
);
126 uint8 mails_count
= 0; // do not allow to send to one player more than 100 mails
130 rc_team
= receive
->GetTeam();
131 mails_count
= receive
->GetMailSize();
135 rc_team
= sObjectMgr
.GetPlayerTeamByGUID(rc
);
136 if (QueryResult
* result
= CharacterDatabase
.PQuery("SELECT COUNT(*) FROM mail WHERE receiver = '%u'", GUID_LOPART(rc
)))
138 Field
*fields
= result
->Fetch();
139 mails_count
= fields
[0].GetUInt32();
144 //do not allow to have more than 100 mails in mailbox.. mails count is in opcode uint8!!! - so max can be 255..
145 if (mails_count
> 100)
147 pl
->SendMailResult(0, MAIL_SEND
, MAIL_ERR_RECIPIENT_CAP_REACHED
);
151 // check the receiver's Faction...
152 if (!sWorld
.getConfig(CONFIG_ALLOW_TWO_SIDE_INTERACTION_MAIL
) && pl
->GetTeam() != rc_team
&& GetSecurity() == SEC_PLAYER
)
154 pl
->SendMailResult(0, MAIL_SEND
, MAIL_ERR_NOT_YOUR_TEAM
);
158 uint32 rc_account
= receive
159 ? receive
->GetSession()->GetAccountId()
160 : sObjectMgr
.GetPlayerAccountIdByGUID(rc
);
162 Item
* items
[MAX_MAIL_ITEMS
];
164 for(uint8 i
= 0; i
< items_count
; ++i
)
168 pl
->SendMailResult(0, MAIL_SEND
, MAIL_ERR_MAIL_ATTACHMENT_INVALID
);
172 Item
* item
= pl
->GetItemByGuid(itemGUIDs
[i
]);
174 // prevent sending bag with items (cheat: can be placed in bag after adding equipped empty bag to mail)
177 pl
->SendMailResult(0, MAIL_SEND
, MAIL_ERR_MAIL_ATTACHMENT_INVALID
);
181 if (!item
->CanBeTraded(true))
183 pl
->SendMailResult(0, MAIL_SEND
, MAIL_ERR_EQUIP_ERROR
, EQUIP_ERR_MAIL_BOUND_ITEM
);
187 if (item
->IsBoundAccountWide() && item
->IsSoulBound() && pl
->GetSession()->GetAccountId() != rc_account
)
189 pl
->SendMailResult(0, MAIL_SEND
, MAIL_ERR_EQUIP_ERROR
, EQUIP_ERR_ARTEFACTS_ONLY_FOR_OWN_CHARACTERS
);
193 if (item
->HasFlag(ITEM_FIELD_FLAGS
, ITEM_FLAGS_CONJURED
) || item
->GetUInt32Value(ITEM_FIELD_DURATION
))
195 pl
->SendMailResult(0, MAIL_SEND
, MAIL_ERR_EQUIP_ERROR
, EQUIP_ERR_MAIL_BOUND_ITEM
);
199 if (COD
&& item
->HasFlag(ITEM_FIELD_FLAGS
, ITEM_FLAGS_WRAPPED
))
201 pl
->SendMailResult(0, MAIL_SEND
, MAIL_ERR_CANT_SEND_WRAPPED_COD
);
208 pl
->SendMailResult(0, MAIL_SEND
, MAIL_OK
);
210 uint32 itemTextId
= !body
.empty() ? sObjectMgr
.CreateItemText( body
) : 0;
212 pl
->ModifyMoney( -int32(reqmoney
) );
213 pl
->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_GOLD_SPENT_FOR_MAIL
, cost
);
215 bool needItemDelay
= false;
217 MailDraft
draft(subject
, itemTextId
);
219 if (items_count
> 0 || money
> 0)
223 for(uint8 i
= 0; i
< items_count
; ++i
)
225 Item
* item
= items
[i
];
226 if (GetSecurity() > SEC_PLAYER
&& sWorld
.getConfig(CONFIG_GM_LOG_TRADE
))
228 sLog
.outCommand(GetAccountId(), "GM %s (Account: %u) mail item: %s (Entry: %u Count: %u) to player: %s (Account: %u)",
229 GetPlayerName(), GetAccountId(), item
->GetProto()->Name1
, item
->GetEntry(), item
->GetCount(), receiver
.c_str(), rc_account
);
232 pl
->MoveItemFromInventory(items
[i
]->GetBagSlot(), item
->GetSlot(), true);
233 CharacterDatabase
.BeginTransaction();
234 item
->DeleteFromInventoryDB(); // deletes item from character's inventory
235 item
->SaveToDB(); // recursive and not have transaction guard into self, item not in inventory and can be save standalone
236 // owner in data will set at mail receive and item extracting
237 CharacterDatabase
.PExecute("UPDATE item_instance SET owner_guid = '%u' WHERE guid='%u'", GUID_LOPART(rc
), item
->GetGUIDLow());
238 CharacterDatabase
.CommitTransaction();
243 // if item send to character at another account, then apply item delivery delay
244 needItemDelay
= pl
->GetSession()->GetAccountId() != rc_account
;
247 if (money
> 0 && GetSecurity() > SEC_PLAYER
&& sWorld
.getConfig(CONFIG_GM_LOG_TRADE
))
249 sLog
.outCommand(GetAccountId(),"GM %s (Account: %u) mail money: %u to player: %s (Account: %u)",
250 GetPlayerName(), GetAccountId(), money
, receiver
.c_str(), rc_account
);
254 // If theres is an item, there is a one hour delivery delay if sent to another account's character.
255 uint32 deliver_delay
= needItemDelay
? sWorld
.getConfig(CONFIG_MAIL_DELIVERY_DELAY
) : 0;
257 // will delete item or place to receiver mail list
261 .SendMailTo(MailReceiver(receive
, GUID_LOPART(rc
)), pl
, MAIL_CHECK_MASK_NONE
, deliver_delay
);
263 CharacterDatabase
.BeginTransaction();
264 pl
->SaveInventoryAndGoldToDB();
265 CharacterDatabase
.CommitTransaction();
268 //called when mail is read
269 void WorldSession::HandleMailMarkAsRead(WorldPacket
& recv_data
)
273 recv_data
>> mailbox
;
276 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
279 Player
*pl
= _player
;
280 Mail
*m
= pl
->GetMail(mailId
);
285 m
->checked
= m
->checked
| MAIL_CHECK_MASK_READ
;
286 // m->expire_time = time(NULL) + (30 * DAY); // Expire time do not change at reading mail
287 pl
->m_mailsUpdated
= true;
288 m
->state
= MAIL_STATE_CHANGED
;
292 //called when client deletes mail
293 void WorldSession::HandleMailDelete(WorldPacket
& recv_data
)
297 recv_data
>> mailbox
;
299 recv_data
.read_skip
<uint32
>(); // mailTemplateId
301 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
304 Player
* pl
= _player
;
305 pl
->m_mailsUpdated
= true;
306 Mail
*m
= pl
->GetMail(mailId
);
309 // delete shouldn't show up for COD mails
312 pl
->SendMailResult(mailId
, MAIL_DELETED
, MAIL_ERR_INTERNAL_ERROR
);
316 m
->state
= MAIL_STATE_DELETED
;
318 pl
->SendMailResult(mailId
, MAIL_DELETED
, MAIL_OK
);
321 void WorldSession::HandleMailReturnToSender(WorldPacket
& recv_data
)
325 recv_data
>> mailbox
;
327 recv_data
.read_skip
<uint64
>(); // original sender GUID for return to, not used
329 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
);
348 // send back only to players and simple drop for other cases
349 if (m
->messageType
== MAIL_NORMAL
)
351 MailDraft
draft(m
->subject
, m
->itemTextId
);
352 if (m
->mailTemplateId
)
353 draft
= MailDraft(m
->mailTemplateId
,false); // items already included
357 for(std::vector
<MailItemInfo
>::iterator itr2
= m
->items
.begin(); itr2
!= m
->items
.end(); ++itr2
)
359 Item
*item
= pl
->GetMItem(itr2
->item_guid
);
367 pl
->RemoveMItem(itr2
->item_guid
);
371 draft
.AddMoney(m
->money
).SendReturnToSender(GetAccountId(), m
->receiver
, m
->sender
);
374 delete m
; // we can deallocate old mail
375 pl
->SendMailResult(mailId
, MAIL_RETURNED_TO_SENDER
, MAIL_OK
);
378 //called when player takes item attached in mail
379 void WorldSession::HandleMailTakeItem(WorldPacket
& recv_data
)
384 recv_data
>> mailbox
;
386 recv_data
>> itemId
; // item guid low
388 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
391 Player
* pl
= _player
;
393 Mail
* m
= pl
->GetMail(mailId
);
394 if(!m
|| m
->state
== MAIL_STATE_DELETED
|| m
->deliver_time
> time(NULL
))
396 pl
->SendMailResult(mailId
, MAIL_ITEM_TAKEN
, MAIL_ERR_INTERNAL_ERROR
);
400 // prevent cheating with skip client money check
401 if(pl
->GetMoney() < m
->COD
)
403 pl
->SendMailResult(mailId
, MAIL_ITEM_TAKEN
, MAIL_ERR_NOT_ENOUGH_MONEY
);
407 Item
*it
= pl
->GetMItem(itemId
);
409 ItemPosCountVec dest
;
410 uint8 msg
= _player
->CanStoreItem( NULL_BAG
, NULL_SLOT
, dest
, it
, false );
411 if( msg
== EQUIP_ERR_OK
)
413 m
->RemoveItem(itemId
);
414 m
->removedItems
.push_back(itemId
);
416 if (m
->COD
> 0) // if there is COD, take COD money from player and send them to sender by mail
418 uint64 sender_guid
= MAKE_NEW_GUID(m
->sender
, 0, HIGHGUID_PLAYER
);
419 Player
*receive
= sObjectMgr
.GetPlayer(sender_guid
);
421 uint32 sender_accId
= 0;
423 if( GetSecurity() > SEC_PLAYER
&& sWorld
.getConfig(CONFIG_GM_LOG_TRADE
) )
425 std::string sender_name
;
428 sender_accId
= receive
->GetSession()->GetAccountId();
429 sender_name
= receive
->GetName();
433 // can be calculated early
434 sender_accId
= sObjectMgr
.GetPlayerAccountIdByGUID(sender_guid
);
436 if(!sObjectMgr
.GetPlayerNameByGUID(sender_guid
, sender_name
))
437 sender_name
= sObjectMgr
.GetMangosStringForDBCLocale(LANG_UNKNOWN
);
439 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)",
440 GetPlayerName(), GetAccountId(), it
->GetProto()->Name1
, it
->GetEntry(), it
->GetCount(), m
->COD
, sender_name
.c_str(), sender_accId
);
443 sender_accId
= sObjectMgr
.GetPlayerAccountIdByGUID(sender_guid
);
445 // check player existence
446 if(receive
|| sender_accId
)
448 MailDraft(m
->subject
)
450 .SendMailTo(MailReceiver(receive
,m
->sender
),MailSender(MAIL_NORMAL
,m
->receiver
), MAIL_CHECK_MASK_COD_PAYMENT
);
453 pl
->ModifyMoney( -int32(m
->COD
) );
456 m
->state
= MAIL_STATE_CHANGED
;
457 pl
->m_mailsUpdated
= true;
458 pl
->RemoveMItem(it
->GetGUIDLow());
460 uint32 count
= it
->GetCount(); // save counts before store and possible merge with deleting
461 pl
->MoveItemToInventory(dest
, it
, true);
463 CharacterDatabase
.BeginTransaction();
464 pl
->SaveInventoryAndGoldToDB();
466 CharacterDatabase
.CommitTransaction();
468 pl
->SendMailResult(mailId
, MAIL_ITEM_TAKEN
, MAIL_OK
, 0, itemId
, count
);
471 pl
->SendMailResult(mailId
, MAIL_ITEM_TAKEN
, MAIL_ERR_EQUIP_ERROR
, msg
);
474 void WorldSession::HandleMailTakeMoney(WorldPacket
& recv_data
)
478 recv_data
>> mailbox
;
481 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
484 Player
*pl
= _player
;
486 Mail
* m
= pl
->GetMail(mailId
);
487 if(!m
|| m
->state
== MAIL_STATE_DELETED
|| m
->deliver_time
> time(NULL
))
489 pl
->SendMailResult(mailId
, MAIL_MONEY_TAKEN
, MAIL_ERR_INTERNAL_ERROR
);
493 pl
->SendMailResult(mailId
, MAIL_MONEY_TAKEN
, MAIL_OK
);
495 pl
->ModifyMoney(m
->money
);
497 m
->state
= MAIL_STATE_CHANGED
;
498 pl
->m_mailsUpdated
= true;
500 // save money and mail to prevent cheating
501 CharacterDatabase
.BeginTransaction();
504 CharacterDatabase
.CommitTransaction();
507 //called when player lists his received mails
508 void WorldSession::HandleGetMailList(WorldPacket
& recv_data
)
511 recv_data
>> mailbox
;
513 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
516 // client can't work with packets > max int16 value
517 const uint32 maxPacketSize
= 32767;
519 uint32 mailsCount
= 0; // send to client mails amount
520 uint32 realCount
= 0; // real mails amount
522 WorldPacket
data(SMSG_MAIL_LIST_RESULT
, 200); // guess size
523 data
<< uint32(0); // real mail's count
524 data
<< uint8(0); // mail's count
525 time_t cur_time
= time(NULL
);
527 for(PlayerMails::iterator itr
= _player
->GetMailBegin(); itr
!= _player
->GetMailEnd(); ++itr
)
529 // packet send mail count as uint8, prevent overflow
530 if(mailsCount
>= 254)
536 // skip deleted or not delivered (deliver delay not expired) mails
537 if ((*itr
)->state
== MAIL_STATE_DELETED
|| cur_time
< (*itr
)->deliver_time
)
540 uint8 item_count
= (*itr
)->items
.size(); // max count is MAX_MAIL_ITEMS (12)
542 size_t next_mail_size
= 2+4+1+((*itr
)->messageType
== MAIL_NORMAL
? 8 : 4)+4*8+((*itr
)->subject
.size()+1)+1+item_count
*(1+4+4+7*3*4+4+4+4+4+4+4+1);
544 if(data
.wpos()+next_mail_size
> maxPacketSize
)
550 uint32 show_flags
= 0;
551 if ((*itr
)->messageType
!= MAIL_NORMAL
)
552 show_flags
|= MAIL_SHOW_DELETE
;
553 if ((*itr
)->messageType
== MAIL_AUCTION
)
554 show_flags
|= MAIL_SHOW_AUCTION
;
555 if ((*itr
)->HasItems() && (*itr
)->messageType
== MAIL_NORMAL
)
556 show_flags
|= MAIL_SHOW_RETURN
;
558 data
<< uint16(next_mail_size
); // Message size
559 data
<< uint32((*itr
)->messageID
); // Message ID
560 data
<< uint8((*itr
)->messageType
); // Message Type
562 switch((*itr
)->messageType
)
564 case MAIL_NORMAL
: // sender guid
565 data
<< uint64(MAKE_NEW_GUID((*itr
)->sender
, 0, HIGHGUID_PLAYER
));
568 case MAIL_GAMEOBJECT
:
570 data
<< uint32((*itr
)->sender
); // creature/gameobject entry, auction id
572 case MAIL_ITEM
: // item entry (?) sender = "Unknown", NYI
573 data
<< uint32(0); // item entry
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(show_flags
); // unknown, 0x4 - auction, 0x10 - normal
583 data
<< float(((*itr
)->expire_time
-time(NULL
))/DAY
);// Time
584 data
<< uint32((*itr
)->mailTemplateId
); // mail template (MailTemplate.dbc)
585 data
<< (*itr
)->subject
; // Subject string - once 00, when mail type = 3
587 data
<< uint8(item_count
); // client limit is 0x10
589 for(uint8 i
= 0; i
< item_count
; ++i
)
591 Item
*item
= _player
->GetMItem((*itr
)->items
[i
].item_guid
);
595 data
<< uint32(item
? item
->GetGUIDLow() : 0);
597 data
<< uint32(item
? item
->GetEntry() : 0);
598 for(uint8 j
= 0; j
< MAX_INSPECTED_ENCHANTMENT_SLOT
; ++j
)
601 data
<< uint32(item
? item
->GetEnchantmentCharges((EnchantmentSlot
)j
) : 0);
603 data
<< uint32(item
? item
->GetEnchantmentDuration((EnchantmentSlot
)j
) : 0);
605 data
<< uint32(item
? item
->GetEnchantmentId((EnchantmentSlot
)j
) : 0);
608 data
<< uint32(item
? item
->GetItemRandomPropertyId() : 0);
610 data
<< uint32(item
? item
->GetItemSuffixFactor() : 0);
612 data
<< uint32(item
? item
->GetCount() : 0);
614 data
<< uint32(item
? item
->GetSpellCharges() : 0);
616 data
<< uint32(item
? item
->GetUInt32Value(ITEM_FIELD_MAXDURABILITY
) : 0);
618 data
<< uint32(item
? item
->GetUInt32Value(ITEM_FIELD_DURABILITY
) : 0);
627 data
.put
<uint32
>(0, realCount
); // this will display warning about undelivered mail to player if realCount > mailsCount
628 data
.put
<uint8
>(4, mailsCount
); // set real send mails to client
631 // recalculate m_nextMailDelivereTime and unReadMails
632 _player
->UpdateNextMailTimeAndUnreads();
635 ///this function is called when client needs mail message body, or when player clicks on item which has ITEM_FIELD_ITEM_TEXT_ID > 0
636 void WorldSession::HandleItemTextQuery(WorldPacket
& recv_data
)
639 uint32 mailId
; // this value can be item id in bag, but it is also mail id
640 uint32 unk
; // maybe something like state - 0x70000000
642 recv_data
>> itemTextId
>> mailId
>> unk
;
644 //some check needed, if player has item with guid mailId, or has mail with id mailId
646 sLog
.outDebug("CMSG_ITEM_TEXT_QUERY itemguid: %u, mailId: %u, unk: %u", itemTextId
, mailId
, unk
);
648 WorldPacket
data(SMSG_ITEM_TEXT_QUERY_RESPONSE
, (4+10));// guess size
650 data
<< sObjectMgr
.GetItemText( itemTextId
);
654 //used when player copies mail body to his inventory
655 void WorldSession::HandleMailCreateTextItem(WorldPacket
& recv_data
)
660 recv_data
>> mailbox
;
662 recv_data
.read_skip
<uint32
>(); // mailTemplateId, non need, Mail store own 100% correct value anyway
664 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox
, GAMEOBJECT_TYPE_MAILBOX
))
667 Player
*pl
= _player
;
669 Mail
* m
= pl
->GetMail(mailId
);
670 if (!m
|| (!m
->itemTextId
&& !m
->mailTemplateId
) || m
->state
== MAIL_STATE_DELETED
|| m
->deliver_time
> time(NULL
))
672 pl
->SendMailResult(mailId
, MAIL_MADE_PERMANENT
, MAIL_ERR_INTERNAL_ERROR
);
676 uint32 itemTextId
= m
->itemTextId
;
678 // in mail template case we need create new text id
681 MailTemplateEntry
const* mailTemplateEntry
= sMailTemplateStore
.LookupEntry(m
->mailTemplateId
);
682 if(!mailTemplateEntry
)
684 pl
->SendMailResult(mailId
, MAIL_MADE_PERMANENT
, MAIL_ERR_INTERNAL_ERROR
);
688 itemTextId
= sObjectMgr
.CreateItemText(mailTemplateEntry
->content
[GetSessionDbcLocale()]);
691 Item
*bodyItem
= new Item
; // This is not bag and then can be used new Item.
692 if(!bodyItem
->Create(sObjectMgr
.GenerateLowGuid(HIGHGUID_ITEM
), MAIL_BODY_ITEM_TEMPLATE
, pl
))
698 bodyItem
->SetUInt32Value( ITEM_FIELD_ITEM_TEXT_ID
, itemTextId
);
699 bodyItem
->SetUInt32Value( ITEM_FIELD_CREATOR
, m
->sender
);
701 sLog
.outDetail("HandleMailCreateTextItem mailid=%u",mailId
);
703 ItemPosCountVec dest
;
704 uint8 msg
= _player
->CanStoreItem( NULL_BAG
, NULL_SLOT
, dest
, bodyItem
, false );
705 if( msg
== EQUIP_ERR_OK
)
708 m
->state
= MAIL_STATE_CHANGED
;
709 pl
->m_mailsUpdated
= true;
711 pl
->StoreItem(dest
, bodyItem
, true);
712 //bodyItem->SetState(ITEM_NEW, pl); is set automatically
713 pl
->SendMailResult(mailId
, MAIL_MADE_PERMANENT
, MAIL_OK
);
717 pl
->SendMailResult(mailId
, MAIL_MADE_PERMANENT
, MAIL_ERR_EQUIP_ERROR
, msg
);
722 //TODO Fix me! ... this void has probably bad condition, but good data are sent
723 void WorldSession::HandleQueryNextMailTime(WorldPacket
& /*recv_data*/ )
725 WorldPacket
data(MSG_QUERY_NEXT_MAIL_TIME
, 8);
727 if( _player
->unReadMails
> 0 )
729 data
<< uint32(0); // float
730 data
<< uint32(0); // count
733 time_t now
= time(NULL
);
734 for(PlayerMails::iterator itr
= _player
->GetMailBegin(); itr
!= _player
->GetMailEnd(); ++itr
)
737 // must be not checked yet
738 if(m
->checked
& MAIL_CHECK_MASK_READ
)
741 // and already delivered
742 if(now
< m
->deliver_time
)
745 data
<< uint64(m
->sender
); // sender guid
747 switch(m
->messageType
)
750 data
<< uint32(m
->sender
); // auction house id
751 data
<< uint32(MAIL_AUCTION
); // message type
759 data
<< uint32(m
->stationery
);
760 data
<< uint32(0xC6000000); // float unk, time or something
763 if(count
== 2) // do not display more than 2 mails
766 data
.put
<uint32
>(4, count
);
770 data
<< uint32(0xC7A8C000);
771 data
<< uint32(0x00000000);
776 MailSender::MailSender( Object
* sender
, MailStationery stationery
) : m_stationery(stationery
)
778 switch(sender
->GetTypeId())
781 m_messageType
= MAIL_CREATURE
;
782 m_senderId
= sender
->GetEntry();
784 case TYPEID_GAMEOBJECT
:
785 m_messageType
= MAIL_GAMEOBJECT
;
786 m_senderId
= sender
->GetEntry();
789 m_messageType
= MAIL_ITEM
;
790 m_senderId
= sender
->GetEntry();
793 m_messageType
= MAIL_NORMAL
;
794 m_senderId
= sender
->GetGUIDLow();
797 m_messageType
= MAIL_NORMAL
;
798 m_senderId
= 0; // will show mail from not existed player
799 sLog
.outError( "MailSender::MailSender - Mail have unexpected sender typeid (%u)", sender
->GetTypeId());
804 MailSender::MailSender( AuctionEntry
* sender
)
805 : m_messageType(MAIL_AUCTION
), m_senderId(sender
->GetHouseId()), m_stationery(MAIL_STATIONERY_AUCTION
)
810 MailReceiver::MailReceiver( Player
* receiver
) : m_receiver(receiver
), m_receiver_lowguid(receiver
->GetGUIDLow())
814 MailReceiver::MailReceiver( Player
* receiver
,uint32 receiver_lowguid
) : m_receiver(receiver
), m_receiver_lowguid(receiver_lowguid
)
816 ASSERT(!receiver
|| receiver
->GetGUIDLow() == receiver_lowguid
);
819 MailDraft
& MailDraft::AddItem( Item
* item
)
821 m_items
[item
->GetGUIDLow()] = item
; return *this;
824 void MailDraft::prepareItems(Player
* receiver
)
826 if (!m_mailTemplateId
|| !m_mailTemplateItemsNeed
)
829 m_mailTemplateItemsNeed
= false;
834 mailLoot
.FillLoot(m_mailTemplateId
, LootTemplates_Mail
, receiver
, true, true);
836 uint32 max_slot
= mailLoot
.GetMaxSlotInLootFor(receiver
);
837 for(uint32 i
= 0; m_items
.size() < MAX_MAIL_ITEMS
&& i
< max_slot
; ++i
)
839 if (LootItem
* lootitem
= mailLoot
.LootItemInSlot(i
,receiver
))
841 if (Item
* item
= Item::CreateItem(lootitem
->itemid
,lootitem
->count
,receiver
))
843 item
->SaveToDB(); // save for prevent lost at next mail load, if send fail then item will deleted
850 void MailDraft::deleteIncludedItems( bool inDB
/*= false*/ )
852 for(MailItemMap::iterator mailItemIter
= m_items
.begin(); mailItemIter
!= m_items
.end(); ++mailItemIter
)
854 Item
* item
= mailItemIter
->second
;
857 CharacterDatabase
.PExecute("DELETE FROM item_instance WHERE guid='%u'", item
->GetGUIDLow());
865 void MailDraft::SendReturnToSender(uint32 sender_acc
, uint32 sender_guid
, uint32 receiver_guid
)
867 Player
*receiver
= sObjectMgr
.GetPlayer(MAKE_NEW_GUID(receiver_guid
, 0, HIGHGUID_PLAYER
));
869 uint32 rc_account
= 0;
871 rc_account
= sObjectMgr
.GetPlayerAccountIdByGUID(MAKE_NEW_GUID(receiver_guid
, 0, HIGHGUID_PLAYER
));
873 if(!receiver
&& !rc_account
) // sender not exist
875 deleteIncludedItems(true);
879 // prepare mail and send in other case
880 bool needItemDelay
= false;
884 // if item send to character at another account, then apply item delivery delay
885 needItemDelay
= sender_acc
!= rc_account
;
887 // set owner to new receiver (to prevent delete item with sender char deleting)
888 CharacterDatabase
.BeginTransaction();
889 for(MailItemMap::iterator mailItemIter
= m_items
.begin(); mailItemIter
!= m_items
.end(); ++mailItemIter
)
891 Item
* item
= mailItemIter
->second
;
892 item
->SaveToDB(); // item not in inventory and can be save standalone
893 // owner in data will set at mail receive and item extracting
894 CharacterDatabase
.PExecute("UPDATE item_instance SET owner_guid = '%u' WHERE guid='%u'", receiver_guid
, item
->GetGUIDLow());
896 CharacterDatabase
.CommitTransaction();
899 // If theres is an item, there is a one hour delivery delay.
900 uint32 deliver_delay
= needItemDelay
? sWorld
.getConfig(CONFIG_MAIL_DELIVERY_DELAY
) : 0;
902 // will delete item or place to receiver mail list
903 SendMailTo(MailReceiver(receiver
,receiver_guid
), MailSender(MAIL_NORMAL
, sender_guid
), MAIL_CHECK_MASK_RETURNED
, deliver_delay
);
906 void MailDraft::SendMailTo(MailReceiver
const& receiver
, MailSender
const& sender
, MailCheckMask checked
, uint32 deliver_delay
)
908 Player
* pReceiver
= receiver
.GetPlayer(); // can be NULL
911 prepareItems(pReceiver
); // generate mail template items
914 uint32 mailId
= sObjectMgr
.GenerateMailID();
916 time_t deliver_time
= time(NULL
) + deliver_delay
;
919 // auction mail without any items and money (auction sale note) pending 1 hour
920 if (sender
.GetMailMessageType() == MAIL_AUCTION
&& m_items
.empty() && !m_money
)
922 // mail from battlemaster (rewardmarks) should last only one day
923 else if (sender
.GetMailMessageType() == MAIL_CREATURE
&& sBattleGroundMgr
.GetBattleMasterBG(sender
.GetSenderId()) != BATTLEGROUND_TYPE_NONE
)
925 // default case: expire time if COD 3 days, if no COD 30 days
927 expire_delay
= (m_COD
> 0) ? 3 * DAY
: 30 * DAY
;
929 time_t expire_time
= deliver_time
+ expire_delay
;
932 std::string safe_subject
= GetSubject();
934 CharacterDatabase
.BeginTransaction();
935 CharacterDatabase
.escape_string(safe_subject
);
936 CharacterDatabase
.PExecute("INSERT INTO mail (id,messageType,stationery,mailTemplateId,sender,receiver,subject,itemTextId,has_items,expire_time,deliver_time,money,cod,checked) "
937 "VALUES ('%u', '%u', '%u', '%u', '%u', '%u', '%s', '%u', '%u', '" UI64FMTD
"','" UI64FMTD
"', '%u', '%u', '%d')",
938 mailId
, sender
.GetMailMessageType(), sender
.GetStationery(), GetMailTemplateId(), sender
.GetSenderId(), receiver
.GetPlayerGUIDLow(), safe_subject
.c_str(), GetBodyId(), (m_items
.empty() ? 0 : 1), (uint64
)expire_time
, (uint64
)deliver_time
, m_money
, m_COD
, checked
);
940 for(MailItemMap::const_iterator mailItemIter
= m_items
.begin(); mailItemIter
!= m_items
.end(); ++mailItemIter
)
942 Item
* item
= mailItemIter
->second
;
943 CharacterDatabase
.PExecute("INSERT INTO mail_items (mail_id,item_guid,item_template,receiver) VALUES ('%u', '%u', '%u','%u')", mailId
, item
->GetGUIDLow(), item
->GetEntry(), receiver
.GetPlayerGUIDLow());
945 CharacterDatabase
.CommitTransaction();
947 // For online receiver update in game mail status and data
950 pReceiver
->AddNewMailDeliverTime(deliver_time
);
953 m
->messageID
= mailId
;
954 m
->mailTemplateId
= GetMailTemplateId();
955 m
->subject
= GetSubject();
956 m
->itemTextId
= GetBodyId();
957 m
->money
= GetMoney();
960 for(MailItemMap::const_iterator mailItemIter
= m_items
.begin(); mailItemIter
!= m_items
.end(); ++mailItemIter
)
962 Item
* item
= mailItemIter
->second
;
963 m
->AddItem(item
->GetGUIDLow(), item
->GetEntry());
966 m
->messageType
= sender
.GetMailMessageType();
967 m
->stationery
= sender
.GetStationery();
968 m
->sender
= sender
.GetSenderId();
969 m
->receiver
= receiver
.GetPlayerGUIDLow();
970 m
->expire_time
= expire_time
;
971 m
->deliver_time
= deliver_time
;
972 m
->checked
= checked
;
973 m
->state
= MAIL_STATE_UNCHANGED
;
975 pReceiver
->AddMail(m
); // to insert new mail to beginning of maillist
977 if (!m_items
.empty())
979 for(MailItemMap::iterator mailItemIter
= m_items
.begin(); mailItemIter
!= m_items
.end(); ++mailItemIter
)
980 pReceiver
->AddMItem(mailItemIter
->second
);
983 else if (!m_items
.empty())
984 deleteIncludedItems();