[8449] Deprecate healing/damage item mods and merge internal data in to spell power.
[getmangos.git] / src / game / Mail.cpp
blobbf7b14ee15d937a6667e72ba636ab6b617d0ddd8
1 /*
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
19 #include "Mail.h"
20 #include "WorldPacket.h"
21 #include "WorldSession.h"
22 #include "Opcodes.h"
23 #include "Log.h"
24 #include "World.h"
25 #include "ObjectMgr.h"
26 #include "Player.h"
27 #include "UpdateMask.h"
28 #include "Unit.h"
29 #include "Language.h"
30 #include "DBCStores.h"
32 enum MailShowFlags
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 )
43 if(item)
45 if(inDB)
46 CharacterDatabase.PExecute("DELETE FROM item_instance WHERE guid='%u'", item->GetGUIDLow());
48 delete item;
49 item=NULL;
53 void WorldSession::HandleSendMail(WorldPacket & recv_data )
55 uint64 mailbox, unk3;
56 std::string receiver, subject, body;
57 uint32 unk1, unk2, money, COD;
58 uint8 unk4;
59 recv_data >> mailbox;
60 recv_data >> receiver;
62 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox, GAMEOBJECT_TYPE_MAILBOX))
63 return;
65 recv_data >> subject;
67 recv_data >> body;
69 recv_data >> unk1; // stationery?
70 recv_data >> unk2; // 0x00000000
72 MailItemsInfo mi;
74 uint8 items_count;
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);
80 return;
83 if(items_count)
85 for(uint8 i = 0; i < items_count; ++i)
87 uint8 item_slot;
88 uint64 item_guid;
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())
102 return;
104 Player* pl = _player;
106 uint64 rc = 0;
107 if(normalizePlayerName(receiver))
108 rc = objmgr.GetPlayerGUIDByName(receiver);
110 if (!rc)
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);
115 return;
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);
123 return;
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);
133 return;
136 Player *receive = objmgr.GetPlayer(rc);
138 uint32 rc_team = 0;
139 uint8 mails_count = 0; //do not allow to send to one player more than 100 mails
141 if(receive)
143 rc_team = receive->GetTeam();
144 mails_count = receive->GetMailSize();
146 else
148 rc_team = objmgr.GetPlayerTeamByGUID(rc);
149 QueryResult* result = CharacterDatabase.PQuery("SELECT COUNT(*) FROM mail WHERE receiver = '%u'", GUID_LOPART(rc));
150 if(result)
152 Field *fields = result->Fetch();
153 mails_count = fields[0].GetUInt32();
154 delete result;
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);
161 return;
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);
167 return;
170 uint32 rc_account = 0;
171 if(receive)
172 rc_account = receive->GetSession()->GetAccountId();
173 else
174 rc_account = objmgr.GetPlayerAccountIdByGUID(rc);
176 if (items_count)
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);
185 return;
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)
190 if(!mailItem.item)
192 pl->SendMailResult(0, MAIL_SEND, MAIL_ERR_MAIL_ATTACHMENT_INVALID);
193 return;
196 if(!mailItem.item->CanBeTraded(true))
198 pl->SendMailResult(0, MAIL_SEND, MAIL_ERR_EQUIP_ERROR, EQUIP_ERR_MAIL_BOUND_ITEM);
199 return;
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);
205 return;
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);
211 return;
214 if(COD && mailItem.item->HasFlag(ITEM_FIELD_FLAGS, ITEM_FLAGS_WRAPPED))
216 pl->SendMailResult(0, MAIL_SEND, MAIL_ERR_CANT_SEND_WRAPPED_COD);
217 return;
221 pl->SendMailResult(0, MAIL_SEND, MAIL_OK);
223 uint32 itemTextId = 0;
224 if (!body.empty())
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)
236 if (items_count > 0)
238 for(MailItemMap::iterator mailItemIter = mi.begin(); mailItemIter != mi.end(); ++mailItemIter)
240 MailItem& mailItem = mailItemIter->second;
241 if(!mailItem.item)
242 continue;
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 )
286 uint64 mailbox;
287 uint32 mailId;
288 recv_data >> mailbox;
290 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox, GAMEOBJECT_TYPE_MAILBOX))
291 return;
293 recv_data >> mailId;
294 Player *pl = _player;
295 Mail *m = pl->GetMail(mailId);
296 if (m)
298 if (pl->unReadMails)
299 --pl->unReadMails;
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 )
310 uint64 mailbox;
311 uint32 mailId;
312 recv_data >> mailbox;
313 recv_data >> mailId;
315 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox, GAMEOBJECT_TYPE_MAILBOX))
316 return;
318 Player* pl = _player;
319 pl->m_mailsUpdated = true;
320 Mail *m = pl->GetMail(mailId);
321 if(m)
323 // delete shouldn't show up for COD mails
324 if (m->COD)
326 pl->SendMailResult(mailId, MAIL_DELETED, MAIL_ERR_INTERNAL_ERROR);
327 return;
330 m->state = MAIL_STATE_DELETED;
332 pl->SendMailResult(mailId, MAIL_DELETED, MAIL_OK);
335 void WorldSession::HandleMailReturnToSender(WorldPacket & recv_data )
337 uint64 mailbox;
338 uint32 mailId;
339 recv_data >> mailbox;
341 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox, GAMEOBJECT_TYPE_MAILBOX))
342 return;
344 recv_data >> mailId;
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);
350 return;
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);
356 // needed?
357 CharacterDatabase.PExecute("DELETE FROM mail_items WHERE mail_id = '%u'", mailId);
358 CharacterDatabase.CommitTransaction();
359 pl->RemoveMail(mailId);
361 MailItemsInfo mi;
363 if(m->HasItems())
365 for(std::vector<MailItemInfo>::iterator itr2 = m->items.begin(); itr2 != m->items.end(); ++itr2)
367 Item *item = pl->GetMItem(itr2->item_guid);
368 if(item)
369 mi.AddItem(item->GetGUIDLow(), item->GetEntry(), item);
370 else
372 //WTF?
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);
390 return;
393 Player *receiver = objmgr.GetPlayer(MAKE_NEW_GUID(receiver_guid, 0, HIGHGUID_PLAYER));
395 uint32 rc_account = 0;
396 if(!receiver)
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);
402 return;
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 )
435 uint64 mailbox;
436 uint32 mailId;
437 uint32 itemId;
438 recv_data >> mailbox;
440 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox, GAMEOBJECT_TYPE_MAILBOX))
441 return;
443 recv_data >> mailId;
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);
451 return;
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);
458 return;
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;
480 if(receive)
482 sender_accId = receive->GetSession()->GetAccountId();
483 sender_name = receive->GetName();
485 else
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);
496 else if(!receive)
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) );
507 m->COD = 0;
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();
517 pl->_SaveMail();
518 CharacterDatabase.CommitTransaction();
520 pl->SendMailResult(mailId, MAIL_ITEM_TAKEN, MAIL_OK, 0, itemId, count);
522 else
523 pl->SendMailResult(mailId, MAIL_ITEM_TAKEN, MAIL_ERR_EQUIP_ERROR, msg);
526 void WorldSession::HandleMailTakeMoney(WorldPacket & recv_data )
528 uint64 mailbox;
529 uint32 mailId;
530 recv_data >> mailbox;
531 recv_data >> mailId;
533 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox, GAMEOBJECT_TYPE_MAILBOX))
534 return;
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);
542 return;
545 pl->SendMailResult(mailId, MAIL_MONEY_TAKEN, MAIL_OK);
547 pl->ModifyMoney(m->money);
548 m->money = 0;
549 m->state = MAIL_STATE_CHANGED;
550 pl->m_mailsUpdated = true;
552 // save money and mail to prevent cheating
553 CharacterDatabase.BeginTransaction();
554 pl->SaveGoldToDB();
555 pl->_SaveMail();
556 CharacterDatabase.CommitTransaction();
559 //called when player lists his received mails
560 void WorldSession::HandleGetMailList(WorldPacket & recv_data )
562 uint64 mailbox;
563 recv_data >> mailbox;
565 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox, GAMEOBJECT_TYPE_MAILBOX))
566 return;
568 Player* pl = _player;
570 //load players mails, and mailed items
571 if(!pl->m_mailsLoaded)
572 pl ->_LoadMail();
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)
587 continue;
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)
594 break;
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));
612 break;
613 case MAIL_CREATURE:
614 case MAIL_GAMEOBJECT:
615 case MAIL_AUCTION:
616 data << (uint32) (*itr)->sender; // creature/gameobject entry, auction id
617 break;
618 case MAIL_ITEM: // item entry (?) sender = "Unknown", NYI
619 break;
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
628 // Time
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);
638 // item index (0-6?)
639 data << (uint8) i;
640 // item guid low?
641 data << (uint32) (item ? item->GetGUIDLow() : 0);
642 // entry
643 data << (uint32) (item ? item->GetEntry() : 0);
644 for(uint8 j = 0; j < MAX_INSPECTED_ENCHANTMENT_SLOT; ++j)
646 // unsure
647 data << (uint32) (item ? item->GetEnchantmentCharges((EnchantmentSlot)j) : 0);
648 // unsure
649 data << (uint32) (item ? item->GetEnchantmentDuration((EnchantmentSlot)j) : 0);
650 // unsure
651 data << (uint32) (item ? item->GetEnchantmentId((EnchantmentSlot)j) : 0);
653 // can be negative
654 data << (uint32) (item ? item->GetItemRandomPropertyId() : 0);
655 // unk
656 data << (uint32) (item ? item->GetItemSuffixFactor() : 0);
657 // stack count
658 data << (uint32) (item ? item->GetCount() : 0);
659 // charges
660 data << (uint32) (item ? item->GetSpellCharges() : 0);
661 // durability
662 data << (uint32) (item ? item->GetUInt32Value(ITEM_FIELD_MAXDURABILITY) : 0);
663 // durability
664 data << (uint32) (item ? item->GetUInt32Value(ITEM_FIELD_DURABILITY) : 0);
665 // unknown wotlk
666 data << (uint8) 0;
669 mails_count += 1;
672 data.put<uint8>(0, mails_count); // set real send mails to client
673 SendPacket(&data);
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 )
682 uint32 itemTextId;
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
693 data << itemTextId;
694 data << objmgr.GetItemText( itemTextId );
695 SendPacket(&data);
698 //used when player copies mail body to his inventory
699 void WorldSession::HandleMailCreateTextItem(WorldPacket & recv_data )
701 uint64 mailbox;
702 uint32 mailId;
704 recv_data >> mailbox >> mailId;
706 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox, GAMEOBJECT_TYPE_MAILBOX))
707 return;
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);
715 return;
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))
721 delete bodyItem;
722 return;
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 )
734 m->itemTextId = 0;
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);
742 else
744 pl->SendMailResult(mailId, MAIL_MADE_PERMANENT, MAIL_ERR_EQUIP_ERROR, msg);
745 delete bodyItem;
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
762 uint32 count = 0;
763 time_t now = time(NULL);
764 for(PlayerMails::iterator itr = _player->GetmailBegin(); itr != _player->GetmailEnd(); ++itr)
766 Mail *m = (*itr);
767 // must be not checked yet
768 if(m->checked & MAIL_CHECK_MASK_READ)
769 continue;
771 // and already delivered
772 if(now < m->deliver_time)
773 continue;
775 data << (uint64) m->sender; // sender guid
777 switch(m->messageType)
779 case MAIL_AUCTION:
780 data << (uint32) 2;
781 data << (uint32) 2;
782 data << (uint32) m->stationery;
783 break;
784 default:
785 data << (uint32) 0;
786 data << (uint32) 0;
787 data << (uint32) m->stationery;
788 break;
790 data << (uint32) 0xC6000000; // float unk, time or something
792 ++count;
793 if(count == 2) // do not display more than 2 mails
794 break;
796 data.put<uint32>(4, count);
798 else
800 data << (uint32) 0xC7A8C000;
801 data << (uint32) 0x00000000;
803 SendPacket(&data);
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
813 uint32 expire_delay;
814 if(messageType == MAIL_AUCTION && !mi && !money) // auction mail without any items and money
815 expire_delay = HOUR;
816 else
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);
824 mailTemplateId = 0;
827 if(receiver)
829 receiver->AddNewMailDeliverTime(deliver_time);
831 if ( receiver->IsMailsLoaded() )
833 Mail * m = new Mail;
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;
843 if(mi)
844 m->AddAllItems(*mi);
846 m->expire_time = expire_time;
847 m->deliver_time = deliver_time;
848 m->money = money;
849 m->COD = COD;
850 m->checked = checked;
851 m->state = MAIL_STATE_UNCHANGED;
853 receiver->AddMail(m); //to insert new mail to beginning of maillist
855 if(mi)
857 for(MailItemMap::iterator mailItemIter = mi->begin(); mailItemIter != mi->end(); ++mailItemIter)
859 MailItem& mailItem = mailItemIter->second;
860 if(mailItem.item)
861 receiver->AddMItem(mailItem.item);
865 else if(mi)
866 mi->deleteIncludedItems();
868 else if(mi)
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);
877 if(mi)
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();