Just a few renames.
[getmangos.git] / src / game / Mail.cpp
blobb1eb9ca44acacd4faac2de799e3d5080d657b1a6
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 void MailItem::deleteItem( bool inDB )
34 if(item)
36 if(inDB)
37 CharacterDatabase.PExecute("DELETE FROM item_instance WHERE guid='%u'", item->GetGUIDLow());
39 delete item;
40 item=NULL;
44 void WorldSession::HandleSendMail(WorldPacket & recv_data )
46 CHECK_PACKET_SIZE(recv_data,8+1+1+1+4+4+1+4+4+8+1);
48 uint64 mailbox, unk3;
49 std::string receiver, subject, body;
50 uint32 unk1, unk2, money, COD;
51 uint8 unk4;
52 recv_data >> mailbox;
53 recv_data >> receiver;
55 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox, GAMEOBJECT_TYPE_MAILBOX))
56 return;
58 // recheck
59 CHECK_PACKET_SIZE(recv_data, 8+(receiver.size()+1)+1+1+4+4+1+4+4+8+1);
61 recv_data >> subject;
63 // recheck
64 CHECK_PACKET_SIZE(recv_data, 8+(receiver.size()+1)+(subject.size()+1)+1+4+4+1+4+4+8+1);
66 recv_data >> body;
68 // recheck
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
74 MailItemsInfo mi;
76 uint8 items_count;
77 recv_data >> items_count; // attached items count
79 if(items_count > 12) // client limit
80 return;
82 // recheck
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);
85 if(items_count)
87 for(uint8 i = 0; i < items_count; ++i)
89 uint8 item_slot;
90 uint64 item_guid;
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())
104 return;
106 Player* pl = _player;
108 uint64 rc = 0;
109 if(normalizePlayerName(receiver))
110 rc = objmgr.GetPlayerGUIDByName(receiver);
112 if (!rc)
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);
117 return;
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);
125 return;
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);
135 return;
138 Player *receive = objmgr.GetPlayer(rc);
140 uint32 rc_team = 0;
141 uint8 mails_count = 0; //do not allow to send to one player more than 100 mails
143 if(receive)
145 rc_team = receive->GetTeam();
146 mails_count = receive->GetMailSize();
148 else
150 rc_team = objmgr.GetPlayerTeamByGUID(rc);
151 QueryResult* result = CharacterDatabase.PQuery("SELECT COUNT(*) FROM mail WHERE receiver = '%u'", GUID_LOPART(rc));
152 if(result)
154 Field *fields = result->Fetch();
155 mails_count = fields[0].GetUInt32();
156 delete result;
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);
163 return;
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);
169 return;
172 if (items_count)
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);
181 return;
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);
189 return;
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);
194 return;
197 if(COD && mailItem.item->HasFlag(ITEM_FIELD_FLAGS, ITEM_FLAGS_WRAPPED))
199 pl->SendMailResult(0, 0, MAIL_ERR_CANT_SEND_WRAPPED_COD);
200 return;
204 pl->SendMailResult(0, 0, MAIL_OK);
206 uint32 itemTextId = 0;
207 if (!body.empty())
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;
220 if(receive)
221 rc_account = receive->GetSession()->GetAccountId();
222 else
223 rc_account = objmgr.GetPlayerAccountIdByGUID(rc);
225 if (items_count > 0)
227 for(MailItemMap::iterator mailItemIter = mi.begin(); mailItemIter != mi.end(); ++mailItemIter)
229 MailItem& mailItem = mailItemIter->second;
230 if(!mailItem.item)
231 continue;
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);
277 uint64 mailbox;
278 uint32 mailId;
279 recv_data >> mailbox;
281 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox, GAMEOBJECT_TYPE_MAILBOX))
282 return;
284 recv_data >> mailId;
285 Player *pl = _player;
286 Mail *m = pl->GetMail(mailId);
287 if (m)
289 if (pl->unReadMails)
290 --pl->unReadMails;
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);
303 uint64 mailbox;
304 uint32 mailId;
305 recv_data >> mailbox;
306 recv_data >> mailId;
308 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox, GAMEOBJECT_TYPE_MAILBOX))
309 return;
311 Player* pl = _player;
312 pl->m_mailsUpdated = true;
313 Mail *m = pl->GetMail(mailId);
314 if(m)
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);
323 uint64 mailbox;
324 uint32 mailId;
325 recv_data >> mailbox;
327 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox, GAMEOBJECT_TYPE_MAILBOX))
328 return;
330 recv_data >> mailId;
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);
336 return;
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);
342 // needed?
343 CharacterDatabase.PExecute("DELETE FROM mail_items WHERE mail_id = '%u'", mailId);
344 CharacterDatabase.CommitTransaction();
345 pl->RemoveMail(mailId);
347 MailItemsInfo mi;
349 if(m->HasItems())
351 for(std::vector<MailItemInfo>::iterator itr2 = m->items.begin(); itr2 != m->items.end(); ++itr2)
353 Item *item = pl->GetMItem(itr2->item_guid);
354 if(item)
355 mi.AddItem(item->GetGUIDLow(), item->GetEntry(), item);
356 else
358 //WTF?
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);
376 return;
379 Player *receiver = objmgr.GetPlayer(MAKE_NEW_GUID(receiver_guid, 0, HIGHGUID_PLAYER));
381 uint32 rc_account = 0;
382 if(!receiver)
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);
388 return;
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);
423 uint64 mailbox;
424 uint32 mailId;
425 uint32 itemId;
426 recv_data >> mailbox;
428 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox, GAMEOBJECT_TYPE_MAILBOX))
429 return;
431 recv_data >> mailId;
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);
439 return;
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);
446 return;
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;
468 if(receive)
470 sender_accId = receive->GetSession()->GetAccountId();
471 sender_name = receive->GetName();
473 else
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);
484 else if(!receive)
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) );
495 m->COD = 0;
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();
505 pl->_SaveMail();
506 CharacterDatabase.CommitTransaction();
508 pl->SendMailResult(mailId, MAIL_ITEM_TAKEN, MAIL_OK, 0, itemId, count);
510 else
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);
518 uint64 mailbox;
519 uint32 mailId;
520 recv_data >> mailbox;
521 recv_data >> mailId;
523 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox, GAMEOBJECT_TYPE_MAILBOX))
524 return;
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);
532 return;
535 pl->SendMailResult(mailId, MAIL_MONEY_TAKEN, 0);
537 pl->ModifyMoney(m->money);
538 m->money = 0;
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
545 pl->_SaveMail();
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);
554 uint64 mailbox;
555 recv_data >> mailbox;
557 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox, GAMEOBJECT_TYPE_MAILBOX))
558 return;
560 Player* pl = _player;
562 //load players mails, and mailed items
563 if(!pl->m_mailsLoaded)
564 pl ->_LoadMail();
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)
579 continue;
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)
586 break;
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));
596 break;
597 case MAIL_CREATURE:
598 case MAIL_GAMEOBJECT:
599 case MAIL_AUCTION:
600 data << (uint32) (*itr)->sender; // creature/gameobject entry, auction id
601 break;
602 case MAIL_ITEM: // item entry (?) sender = "Unknown", NYI
603 break;
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
612 // Time
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);
622 // item index (0-6?)
623 data << (uint8) i;
624 // item guid low?
625 data << (uint32) (item ? item->GetGUIDLow() : 0);
626 // entry
627 data << (uint32) (item ? item->GetEntry() : 0);
628 for(uint8 j = 0; j < MAX_INSPECTED_ENCHANTMENT_SLOT; ++j)
630 // unsure
631 data << (uint32) (item ? item->GetEnchantmentCharges((EnchantmentSlot)j) : 0);
632 // unsure
633 data << (uint32) (item ? item->GetEnchantmentDuration((EnchantmentSlot)j) : 0);
634 // unsure
635 data << (uint32) (item ? item->GetEnchantmentId((EnchantmentSlot)j) : 0);
637 // can be negative
638 data << (uint32) (item ? item->GetItemRandomPropertyId() : 0);
639 // unk
640 data << (uint32) (item ? item->GetItemSuffixFactor() : 0);
641 // stack count
642 data << (uint32) (item ? item->GetCount() : 0);
643 // charges
644 data << (uint32) (item ? item->GetSpellCharges() : 0);
645 // durability
646 data << (uint32) (item ? item->GetUInt32Value(ITEM_FIELD_MAXDURABILITY) : 0);
647 // durability
648 data << (uint32) (item ? item->GetUInt32Value(ITEM_FIELD_DURABILITY) : 0);
649 // unknown wotlk
650 data << (uint8) 0;
653 mails_count += 1;
656 data.put<uint8>(0, mails_count); // set real send mails to client
657 SendPacket(&data);
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);
668 uint32 itemTextId;
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
679 data << itemTextId;
680 data << objmgr.GetItemText( itemTextId );
681 SendPacket(&data);
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);
689 uint64 mailbox;
690 uint32 mailId;
692 recv_data >> mailbox >> mailId;
694 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox, GAMEOBJECT_TYPE_MAILBOX))
695 return;
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);
703 return;
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))
709 delete bodyItem;
710 return;
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 )
722 m->itemTextId = 0;
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);
730 else
732 pl->SendMailResult(mailId, MAIL_MADE_PERMANENT, MAIL_ERR_BAG_FULL, msg);
733 delete bodyItem;
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
749 uint32 count = 0;
750 for(PlayerMails::iterator itr = _player->GetmailBegin(); itr != _player->GetmailEnd(); ++itr)
752 Mail *m = (*itr);
753 // not checked yet, already must be delivered
754 if((m->checked & MAIL_CHECK_MASK_READ)==0 && (m->deliver_time <= time(NULL)))
756 ++count;
758 if(count > 2)
760 count = 2;
761 break;
764 data << (uint64) m->sender; // sender guid
766 switch(m->messageType)
768 case MAIL_AUCTION:
769 data << (uint32) 2;
770 data << (uint32) 2;
771 data << (uint32) m->stationery;
772 break;
773 default:
774 data << (uint32) 0;
775 data << (uint32) 0;
776 data << (uint32) m->stationery;
777 break;
779 data << (uint32) 0xC6000000; // float unk, time or something
782 data.put<uint32>(4, count);
784 else
786 data << (uint32) 0xC7A8C000;
787 data << (uint32) 0x00000000;
789 SendPacket(&data);
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
799 uint32 expire_delay;
800 if(messageType == MAIL_AUCTION && !mi && !money) // auction mail without any items and money
801 expire_delay = HOUR;
802 else
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);
810 mailTemplateId = 0;
813 if(receiver)
815 receiver->AddNewMailDeliverTime(deliver_time);
817 if ( receiver->IsMailsLoaded() )
819 Mail * m = new Mail;
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;
829 if(mi)
830 m->AddAllItems(*mi);
832 m->expire_time = expire_time;
833 m->deliver_time = deliver_time;
834 m->money = money;
835 m->COD = COD;
836 m->checked = checked;
837 m->state = MAIL_STATE_UNCHANGED;
839 receiver->AddMail(m); //to insert new mail to beginning of maillist
841 if(mi)
843 for(MailItemMap::iterator mailItemIter = mi->begin(); mailItemIter != mi->end(); ++mailItemIter)
845 MailItem& mailItem = mailItemIter->second;
846 if(mailItem.item)
847 receiver->AddMItem(mailItem.item);
851 else if(mi)
852 mi->deleteIncludedItems();
854 else if(mi)
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);
863 if(mi)
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();