Merge branch '310' of git://github.com/mangos/mangos into 310
[AHbot.git] / src / game / Mail.cpp
blobabed97d1b5681e783c3429889609728d374aab42
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 "AuctionHouseBot.h"
31 #include "DBCStores.h"
33 void MailItem::deleteItem( bool inDB )
35 if(item)
37 if(inDB)
38 CharacterDatabase.PExecute("DELETE FROM item_instance WHERE guid='%u'", item->GetGUIDLow());
40 delete item;
41 item=NULL;
45 void WorldSession::HandleSendMail(WorldPacket & recv_data )
47 CHECK_PACKET_SIZE(recv_data,8+1+1+1+4+4+1+4+4+8+1);
49 uint64 mailbox, unk3;
50 std::string receiver, subject, body;
51 uint32 unk1, unk2, money, COD;
52 uint8 unk4;
53 recv_data >> mailbox;
54 recv_data >> receiver;
56 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox, GAMEOBJECT_TYPE_MAILBOX))
57 return;
59 // recheck
60 CHECK_PACKET_SIZE(recv_data, 8+(receiver.size()+1)+1+1+4+4+1+4+4+8+1);
62 recv_data >> subject;
64 // recheck
65 CHECK_PACKET_SIZE(recv_data, 8+(receiver.size()+1)+(subject.size()+1)+1+4+4+1+4+4+8+1);
67 recv_data >> body;
69 // recheck
70 CHECK_PACKET_SIZE(recv_data, 8+(receiver.size()+1)+(subject.size()+1)+(body.size()+1)+4+4+1+4+4+8+1);
72 recv_data >> unk1; // stationery?
73 recv_data >> unk2; // 0x00000000
75 MailItemsInfo mi;
77 uint8 items_count;
78 recv_data >> items_count; // attached items count
80 if(items_count > 12) // client limit
81 return;
83 // recheck
84 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);
86 if(items_count)
88 for(uint8 i = 0; i < items_count; ++i)
90 uint8 item_slot;
91 uint64 item_guid;
92 recv_data >> item_slot;
93 recv_data >> item_guid;
94 mi.AddItem(GUID_LOPART(item_guid), item_slot);
98 recv_data >> money >> COD; // money and cod
99 recv_data >> unk3; // const 0
100 recv_data >> unk4; // const 0
102 items_count = mi.size(); // this is the real size after the duplicates have been removed
104 if (receiver.empty())
105 return;
107 Player* pl = _player;
109 uint64 rc = 0;
110 if(normalizePlayerName(receiver))
111 rc = objmgr.GetPlayerGUIDByName(receiver);
113 if (!rc)
115 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",
116 pl->GetGUIDLow(), receiver.c_str(), subject.c_str(), body.c_str(), items_count, money, COD, unk1, unk2);
117 pl->SendMailResult(0, 0, MAIL_ERR_RECIPIENT_NOT_FOUND);
118 return;
121 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);
123 if(pl->GetGUID() == rc)
125 pl->SendMailResult(0, 0, MAIL_ERR_CANNOT_SEND_TO_SELF);
126 return;
129 uint32 cost = items_count ? 30 * items_count : 30; // price hardcoded in client
131 uint32 reqmoney = cost + money;
133 if (pl->GetMoney() < reqmoney)
135 pl->SendMailResult(0, 0, MAIL_ERR_NOT_ENOUGH_MONEY);
136 return;
139 Player *receive = objmgr.GetPlayer(rc);
141 uint32 rc_team = 0;
142 uint8 mails_count = 0; //do not allow to send to one player more than 100 mails
144 if(receive)
146 rc_team = receive->GetTeam();
147 mails_count = receive->GetMailSize();
149 else
151 rc_team = objmgr.GetPlayerTeamByGUID(rc);
152 QueryResult* result = CharacterDatabase.PQuery("SELECT COUNT(*) FROM mail WHERE receiver = '%u'", GUID_LOPART(rc));
153 if(result)
155 Field *fields = result->Fetch();
156 mails_count = fields[0].GetUInt32();
157 delete result;
160 //do not allow to have more than 100 mails in mailbox.. mails count is in opcode uint8!!! - so max can be 255..
161 if (mails_count > 100)
163 pl->SendMailResult(0, 0, MAIL_ERR_INTERNAL_ERROR);
164 return;
166 // test the receiver's Faction...
167 if (!sWorld.getConfig(CONFIG_ALLOW_TWO_SIDE_INTERACTION_MAIL) && pl->GetTeam() != rc_team && GetSecurity() == SEC_PLAYER)
169 pl->SendMailResult(0, 0, MAIL_ERR_NOT_YOUR_TEAM);
170 return;
173 if (items_count)
175 for(MailItemMap::iterator mailItemIter = mi.begin(); mailItemIter != mi.end(); ++mailItemIter)
177 MailItem& mailItem = mailItemIter->second;
179 if(!mailItem.item_guidlow)
181 pl->SendMailResult(0, 0, MAIL_ERR_INTERNAL_ERROR);
182 return;
185 mailItem.item = pl->GetItemByGuid(MAKE_NEW_GUID(mailItem.item_guidlow, 0, HIGHGUID_ITEM));
186 // prevent sending bag with items (cheat: can be placed in bag after adding equipped empty bag to mail)
187 if(!mailItem.item || !mailItem.item->CanBeTraded())
189 pl->SendMailResult(0, 0, MAIL_ERR_INTERNAL_ERROR);
190 return;
192 if (mailItem.item->HasFlag(ITEM_FIELD_FLAGS, ITEM_FLAGS_CONJURED) || mailItem.item->GetUInt32Value(ITEM_FIELD_DURATION))
194 pl->SendMailResult(0, 0, MAIL_ERR_INTERNAL_ERROR);
195 return;
198 if(COD && mailItem.item->HasFlag(ITEM_FIELD_FLAGS, ITEM_FLAGS_WRAPPED))
200 pl->SendMailResult(0, 0, MAIL_ERR_CANT_SEND_WRAPPED_COD);
201 return;
205 pl->SendMailResult(0, 0, MAIL_OK);
207 uint32 itemTextId = 0;
208 if (!body.empty())
210 itemTextId = objmgr.CreateItemText( body );
213 pl->ModifyMoney( -int32(reqmoney) );
214 pl->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_GOLD_SPENT_FOR_MAIL, cost);
216 bool needItemDelay = false;
218 if(items_count > 0 || money > 0)
220 uint32 rc_account = 0;
221 if(receive)
222 rc_account = receive->GetSession()->GetAccountId();
223 else
224 rc_account = objmgr.GetPlayerAccountIdByGUID(rc);
226 if (items_count > 0)
228 for(MailItemMap::iterator mailItemIter = mi.begin(); mailItemIter != mi.end(); ++mailItemIter)
230 MailItem& mailItem = mailItemIter->second;
231 if(!mailItem.item)
232 continue;
234 mailItem.item_template = mailItem.item ? mailItem.item->GetEntry() : 0;
236 if( GetSecurity() > SEC_PLAYER && sWorld.getConfig(CONFIG_GM_LOG_TRADE) )
238 sLog.outCommand(GetAccountId(), "GM %s (Account: %u) mail item: %s (Entry: %u Count: %u) to player: %s (Account: %u)",
239 GetPlayerName(), GetAccountId(), mailItem.item->GetProto()->Name1, mailItem.item->GetEntry(), mailItem.item->GetCount(), receiver.c_str(), rc_account);
242 pl->MoveItemFromInventory(mailItem.item->GetBagSlot(), mailItem.item->GetSlot(), true);
243 CharacterDatabase.BeginTransaction();
244 mailItem.item->DeleteFromInventoryDB(); //deletes item from character's inventory
245 mailItem.item->SaveToDB(); // recursive and not have transaction guard into self, item not in inventory and can be save standalone
246 // owner in data will set at mail receive and item extracting
247 CharacterDatabase.PExecute("UPDATE item_instance SET owner_guid = '%u' WHERE guid='%u'", GUID_LOPART(rc), mailItem.item->GetGUIDLow());
248 CharacterDatabase.CommitTransaction();
251 // if item send to character at another account, then apply item delivery delay
252 needItemDelay = pl->GetSession()->GetAccountId() != rc_account;
255 if(money > 0 && GetSecurity() > SEC_PLAYER && sWorld.getConfig(CONFIG_GM_LOG_TRADE))
257 sLog.outCommand(GetAccountId(),"GM %s (Account: %u) mail money: %u to player: %s (Account: %u)",
258 GetPlayerName(), GetAccountId(), money, receiver.c_str(), rc_account);
262 // If theres is an item, there is a one hour delivery delay if sent to another account's character.
263 uint32 deliver_delay = needItemDelay ? sWorld.getConfig(CONFIG_MAIL_DELIVERY_DELAY) : 0;
265 // will delete item or place to receiver mail list
266 WorldSession::SendMailTo(receive, MAIL_NORMAL, MAIL_STATIONERY_NORMAL, pl->GetGUIDLow(), GUID_LOPART(rc), subject, itemTextId, &mi, money, COD, MAIL_CHECK_MASK_NONE, deliver_delay);
268 CharacterDatabase.BeginTransaction();
269 pl->SaveInventoryAndGoldToDB();
270 CharacterDatabase.CommitTransaction();
273 //called when mail is read
274 void WorldSession::HandleMailMarkAsRead(WorldPacket & recv_data )
276 CHECK_PACKET_SIZE(recv_data,8+4);
278 uint64 mailbox;
279 uint32 mailId;
280 recv_data >> mailbox;
282 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox, GAMEOBJECT_TYPE_MAILBOX))
283 return;
285 recv_data >> mailId;
286 Player *pl = _player;
287 Mail *m = pl->GetMail(mailId);
288 if (m)
290 if (pl->unReadMails)
291 --pl->unReadMails;
292 m->checked = m->checked | MAIL_CHECK_MASK_READ;
293 // m->expire_time = time(NULL) + (30 * DAY); // Expire time do not change at reading mail
294 pl->m_mailsUpdated = true;
295 m->state = MAIL_STATE_CHANGED;
299 //called when client deletes mail
300 void WorldSession::HandleMailDelete(WorldPacket & recv_data )
302 CHECK_PACKET_SIZE(recv_data,8+4);
304 uint64 mailbox;
305 uint32 mailId;
306 recv_data >> mailbox;
307 recv_data >> mailId;
309 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox, GAMEOBJECT_TYPE_MAILBOX))
310 return;
312 Player* pl = _player;
313 pl->m_mailsUpdated = true;
314 Mail *m = pl->GetMail(mailId);
315 if(m)
316 m->state = MAIL_STATE_DELETED;
317 pl->SendMailResult(mailId, MAIL_DELETED, 0);
320 void WorldSession::HandleMailReturnToSender(WorldPacket & recv_data )
322 CHECK_PACKET_SIZE(recv_data,8+4);
324 uint64 mailbox;
325 uint32 mailId;
326 recv_data >> mailbox;
328 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox, GAMEOBJECT_TYPE_MAILBOX))
329 return;
331 recv_data >> mailId;
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);
337 return;
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);
343 // needed?
344 CharacterDatabase.PExecute("DELETE FROM mail_items WHERE mail_id = '%u'", mailId);
345 CharacterDatabase.CommitTransaction();
346 pl->RemoveMail(mailId);
348 MailItemsInfo mi;
350 if(m->HasItems())
352 for(std::vector<MailItemInfo>::iterator itr2 = m->items.begin(); itr2 != m->items.end(); ++itr2)
354 Item *item = pl->GetMItem(itr2->item_guid);
355 if(item)
356 mi.AddItem(item->GetGUIDLow(), item->GetEntry(), item);
357 else
359 //WTF?
362 pl->RemoveMItem(itr2->item_guid);
366 if (m->sender == AHBplayerGUID)
368 SendReturnToSender(MAIL_CREATURE, GetAccountId(), m->receiver, m->sender, m->subject, m->itemTextId, &mi, m->money, m->mailTemplateId);
370 else
372 SendReturnToSender(MAIL_NORMAL, GetAccountId(), m->receiver, m->sender, m->subject, m->itemTextId, &mi, m->money, m->mailTemplateId);
375 delete m; //we can deallocate old mail
376 pl->SendMailResult(mailId, MAIL_RETURNED_TO_SENDER, 0);
379 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 )
381 if(messageType != MAIL_NORMAL) // return only to players
383 mi->deleteIncludedItems(true);
384 return;
387 Player *receiver = objmgr.GetPlayer(MAKE_NEW_GUID(receiver_guid, 0, HIGHGUID_PLAYER));
389 uint32 rc_account = 0;
390 if(!receiver)
391 rc_account = objmgr.GetPlayerAccountIdByGUID(MAKE_NEW_GUID(receiver_guid, 0, HIGHGUID_PLAYER));
393 if(!receiver && !rc_account) // sender not exist
395 mi->deleteIncludedItems(true);
396 return;
399 // prepare mail and send in other case
400 bool needItemDelay = false;
402 if(mi && !mi->empty())
404 // if item send to character at another account, then apply item delivery delay
405 needItemDelay = sender_acc != rc_account;
407 // set owner to new receiver (to prevent delete item with sender char deleting)
408 CharacterDatabase.BeginTransaction();
409 for(MailItemMap::iterator mailItemIter = mi->begin(); mailItemIter != mi->end(); ++mailItemIter)
411 MailItem& mailItem = mailItemIter->second;
412 mailItem.item->SaveToDB(); // item not in inventory and can be save standalone
413 // owner in data will set at mail receive and item extracting
414 CharacterDatabase.PExecute("UPDATE item_instance SET owner_guid = '%u' WHERE guid='%u'", receiver_guid, mailItem.item->GetGUIDLow());
416 CharacterDatabase.CommitTransaction();
419 // If theres is an item, there is a one hour delivery delay.
420 uint32 deliver_delay = needItemDelay ? sWorld.getConfig(CONFIG_MAIL_DELIVERY_DELAY) : 0;
422 // will delete item or place to receiver mail list
423 WorldSession::SendMailTo(receiver, MAIL_NORMAL, MAIL_STATIONERY_NORMAL, sender_guid, receiver_guid, subject, itemTextId, mi, money, 0, MAIL_CHECK_MASK_RETURNED,deliver_delay,mailTemplateId);
426 //called when player takes item attached in mail
427 void WorldSession::HandleMailTakeItem(WorldPacket & recv_data )
429 CHECK_PACKET_SIZE(recv_data,8+4+4);
431 uint64 mailbox;
432 uint32 mailId;
433 uint32 itemId;
434 recv_data >> mailbox;
436 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox, GAMEOBJECT_TYPE_MAILBOX))
437 return;
439 recv_data >> mailId;
440 recv_data >> itemId; // item guid low?
441 Player* pl = _player;
443 Mail* m = pl->GetMail(mailId);
444 if(!m || m->state == MAIL_STATE_DELETED || m->deliver_time > time(NULL))
446 pl->SendMailResult(mailId, MAIL_ITEM_TAKEN, MAIL_ERR_INTERNAL_ERROR);
447 return;
450 // prevent cheating with skip client money check
451 if(pl->GetMoney() < m->COD)
453 pl->SendMailResult(mailId, MAIL_ITEM_TAKEN, MAIL_ERR_NOT_ENOUGH_MONEY);
454 return;
457 Item *it = pl->GetMItem(itemId);
459 ItemPosCountVec dest;
460 uint8 msg = _player->CanStoreItem( NULL_BAG, NULL_SLOT, dest, it, false );
461 if( msg == EQUIP_ERR_OK )
463 m->RemoveItem(itemId);
464 m->removedItems.push_back(itemId);
466 if (m->COD > 0) //if there is COD, take COD money from player and send them to sender by mail
468 uint64 sender_guid = MAKE_NEW_GUID(m->sender, 0, HIGHGUID_PLAYER);
469 Player *receive = objmgr.GetPlayer(sender_guid);
471 uint32 sender_accId = 0;
473 if( GetSecurity() > SEC_PLAYER && sWorld.getConfig(CONFIG_GM_LOG_TRADE) )
475 std::string sender_name;
476 if(receive)
478 sender_accId = receive->GetSession()->GetAccountId();
479 sender_name = receive->GetName();
481 else
483 // can be calculated early
484 sender_accId = objmgr.GetPlayerAccountIdByGUID(sender_guid);
486 if(!objmgr.GetPlayerNameByGUID(sender_guid,sender_name))
487 sender_name = objmgr.GetMangosStringForDBCLocale(LANG_UNKNOWN);
489 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)",
490 GetPlayerName(),GetAccountId(),it->GetProto()->Name1,it->GetEntry(),it->GetCount(),m->COD,sender_name.c_str(),sender_accId);
492 else if(!receive)
493 sender_accId = objmgr.GetPlayerAccountIdByGUID(sender_guid);
495 // check player existence
496 if(receive || sender_accId)
498 WorldSession::SendMailTo(receive, MAIL_NORMAL, MAIL_STATIONERY_NORMAL, m->receiver, m->sender, m->subject, 0, NULL, m->COD, 0, MAIL_CHECK_MASK_COD_PAYMENT);
501 pl->ModifyMoney( -int32(m->COD) );
503 m->COD = 0;
504 m->state = MAIL_STATE_CHANGED;
505 pl->m_mailsUpdated = true;
506 pl->RemoveMItem(it->GetGUIDLow());
508 uint32 count = it->GetCount(); // save counts before store and possible merge with deleting
509 pl->MoveItemToInventory(dest,it,true);
511 CharacterDatabase.BeginTransaction();
512 pl->SaveInventoryAndGoldToDB();
513 pl->_SaveMail();
514 CharacterDatabase.CommitTransaction();
516 pl->SendMailResult(mailId, MAIL_ITEM_TAKEN, MAIL_OK, 0, itemId, count);
518 else
519 pl->SendMailResult(mailId, MAIL_ITEM_TAKEN, MAIL_ERR_BAG_FULL, msg);
522 void WorldSession::HandleMailTakeMoney(WorldPacket & recv_data )
524 CHECK_PACKET_SIZE(recv_data,8+4);
526 uint64 mailbox;
527 uint32 mailId;
528 recv_data >> mailbox;
529 recv_data >> mailId;
531 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox, GAMEOBJECT_TYPE_MAILBOX))
532 return;
534 Player *pl = _player;
536 Mail* m = pl->GetMail(mailId);
537 if(!m || m->state == MAIL_STATE_DELETED || m->deliver_time > time(NULL))
539 pl->SendMailResult(mailId, MAIL_MONEY_TAKEN, MAIL_ERR_INTERNAL_ERROR);
540 return;
543 pl->SendMailResult(mailId, MAIL_MONEY_TAKEN, 0);
545 pl->ModifyMoney(m->money);
546 m->money = 0;
547 m->state = MAIL_STATE_CHANGED;
548 pl->m_mailsUpdated = true;
550 // save money and mail to prevent cheating
551 CharacterDatabase.BeginTransaction();
552 pl->SaveDataFieldToDB(); // contains money
553 pl->_SaveMail();
554 CharacterDatabase.CommitTransaction();
557 //called when player lists his received mails
558 void WorldSession::HandleGetMailList(WorldPacket & recv_data )
560 CHECK_PACKET_SIZE(recv_data,8);
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 data << (uint16) 0x0040; // unknown 2.3.0, different values
597 data << (uint32) (*itr)->messageID; // Message ID
598 data << (uint8) (*itr)->messageType; // Message Type
600 switch((*itr)->messageType)
602 case MAIL_NORMAL: // sender guid
603 data << uint64(MAKE_NEW_GUID((*itr)->sender, 0, HIGHGUID_PLAYER));
604 break;
605 case MAIL_CREATURE:
606 case MAIL_GAMEOBJECT:
607 case MAIL_AUCTION:
608 data << (uint32) (*itr)->sender; // creature/gameobject entry, auction id
609 break;
610 case MAIL_ITEM: // item entry (?) sender = "Unknown", NYI
611 break;
614 data << (uint32) (*itr)->COD; // COD
615 data << (uint32) (*itr)->itemTextId; // sure about this
616 data << (uint32) 0; // unknown
617 data << (uint32) (*itr)->stationery; // stationery (Stationery.dbc)
618 data << (uint32) (*itr)->money; // Gold
619 data << (uint32) 0x04; // unknown, 0x4 - auction, 0x10 - normal
620 // Time
621 data << (float) ((*itr)->expire_time-time(NULL))/DAY;
622 data << (uint32) (*itr)->mailTemplateId; // mail template (MailTemplate.dbc)
623 data << (*itr)->subject; // Subject string - once 00, when mail type = 3
625 data << (uint8) item_count; // client limit is 0x10
627 for(uint8 i = 0; i < item_count; ++i)
629 Item *item = pl->GetMItem((*itr)->items[i].item_guid);
630 // item index (0-6?)
631 data << (uint8) i;
632 // item guid low?
633 data << (uint32) (item ? item->GetGUIDLow() : 0);
634 // entry
635 data << (uint32) (item ? item->GetEntry() : 0);
636 for(uint8 j = 0; j < MAX_INSPECTED_ENCHANTMENT_SLOT; ++j)
638 // unsure
639 data << (uint32) (item ? item->GetEnchantmentCharges((EnchantmentSlot)j) : 0);
640 // unsure
641 data << (uint32) (item ? item->GetEnchantmentDuration((EnchantmentSlot)j) : 0);
642 // unsure
643 data << (uint32) (item ? item->GetEnchantmentId((EnchantmentSlot)j) : 0);
645 // can be negative
646 data << (uint32) (item ? item->GetItemRandomPropertyId() : 0);
647 // unk
648 data << (uint32) (item ? item->GetItemSuffixFactor() : 0);
649 // stack count
650 data << (uint32) (item ? item->GetCount() : 0);
651 // charges
652 data << (uint32) (item ? item->GetSpellCharges() : 0);
653 // durability
654 data << (uint32) (item ? item->GetUInt32Value(ITEM_FIELD_MAXDURABILITY) : 0);
655 // durability
656 data << (uint32) (item ? item->GetUInt32Value(ITEM_FIELD_DURABILITY) : 0);
657 // unknown wotlk
658 data << (uint8) 0;
661 mails_count += 1;
664 data.put<uint8>(0, mails_count); // set real send mails to client
665 SendPacket(&data);
667 // recalculate m_nextMailDelivereTime and unReadMails
668 _player->UpdateNextMailTimeAndUnreads();
671 ///this function is called when client needs mail message body, or when player clicks on item which has ITEM_FIELD_ITEM_TEXT_ID > 0
672 void WorldSession::HandleItemTextQuery(WorldPacket & recv_data )
674 CHECK_PACKET_SIZE(recv_data,4+4+4);
676 uint32 itemTextId;
677 uint32 mailId; //this value can be item id in bag, but it is also mail id
678 uint32 unk; //maybe something like state - 0x70000000
680 recv_data >> itemTextId >> mailId >> unk;
682 //some check needed, if player has item with guid mailId, or has mail with id mailId
684 sLog.outDebug("CMSG_ITEM_TEXT_QUERY itemguid: %u, mailId: %u, unk: %u", itemTextId, mailId, unk);
686 WorldPacket data(SMSG_ITEM_TEXT_QUERY_RESPONSE, (4+10));// guess size
687 data << itemTextId;
688 data << objmgr.GetItemText( itemTextId );
689 SendPacket(&data);
692 //used when player copies mail body to his inventory
693 void WorldSession::HandleMailCreateTextItem(WorldPacket & recv_data )
695 CHECK_PACKET_SIZE(recv_data,8+4);
697 uint64 mailbox;
698 uint32 mailId;
700 recv_data >> mailbox >> mailId;
702 if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox, GAMEOBJECT_TYPE_MAILBOX))
703 return;
705 Player *pl = _player;
707 Mail* m = pl->GetMail(mailId);
708 if(!m || !m->itemTextId || m->state == MAIL_STATE_DELETED || m->deliver_time > time(NULL))
710 pl->SendMailResult(mailId, MAIL_MADE_PERMANENT, MAIL_ERR_INTERNAL_ERROR);
711 return;
714 Item *bodyItem = new Item; // This is not bag and then can be used new Item.
715 if(!bodyItem->Create(objmgr.GenerateLowGuid(HIGHGUID_ITEM), MAIL_BODY_ITEM_TEMPLATE, pl))
717 delete bodyItem;
718 return;
721 bodyItem->SetUInt32Value( ITEM_FIELD_ITEM_TEXT_ID , m->itemTextId );
722 bodyItem->SetUInt32Value( ITEM_FIELD_CREATOR, m->sender);
724 sLog.outDetail("HandleMailCreateTextItem mailid=%u",mailId);
726 ItemPosCountVec dest;
727 uint8 msg = _player->CanStoreItem( NULL_BAG, NULL_SLOT, dest, bodyItem, false );
728 if( msg == EQUIP_ERR_OK )
730 m->itemTextId = 0;
731 m->state = MAIL_STATE_CHANGED;
732 pl->m_mailsUpdated = true;
734 pl->StoreItem(dest, bodyItem, true);
735 //bodyItem->SetState(ITEM_NEW, pl); is set automatically
736 pl->SendMailResult(mailId, MAIL_MADE_PERMANENT, 0);
738 else
740 pl->SendMailResult(mailId, MAIL_MADE_PERMANENT, MAIL_ERR_BAG_FULL, msg);
741 delete bodyItem;
745 //TODO Fix me! ... this void has probably bad condition, but good data are sent
746 void WorldSession::HandleQueryNextMailTime(WorldPacket & /*recv_data*/ )
748 WorldPacket data(MSG_QUERY_NEXT_MAIL_TIME, 8);
750 if(!_player->m_mailsLoaded)
751 _player->_LoadMail();
753 if( _player->unReadMails > 0 )
755 data << (uint32) 0; // float
756 data << (uint32) 0; // count
757 uint32 count = 0;
758 for(PlayerMails::iterator itr = _player->GetmailBegin(); itr != _player->GetmailEnd(); ++itr)
760 Mail *m = (*itr);
761 // not checked yet, already must be delivered
762 if((m->checked & MAIL_CHECK_MASK_READ)==0 && (m->deliver_time <= time(NULL)))
764 ++count;
766 if(count > 2)
768 count = 2;
769 break;
772 data << (uint64) m->sender; // sender guid
774 switch(m->messageType)
776 case MAIL_AUCTION:
777 data << (uint32) 2;
778 data << (uint32) 2;
779 data << (uint32) m->stationery;
780 break;
781 default:
782 data << (uint32) 0;
783 data << (uint32) 0;
784 data << (uint32) m->stationery;
785 break;
787 data << (uint32) 0xC6000000; // float unk, time or something
790 data.put<uint32>(4, count);
792 else
794 data << (uint32) 0xC7A8C000;
795 data << (uint32) 0x00000000;
797 SendPacket(&data);
800 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)
802 if (receiver_guidlow == AHBplayerGUID)
804 if(messageType == MAIL_AUCTION && mi) // auction mail with items
806 mi->deleteIncludedItems(true);
808 return;
810 uint32 mailId = objmgr.GenerateMailID();
812 time_t deliver_time = time(NULL) + deliver_delay;
814 //expire time if COD 3 days, if no COD 30 days, if auction sale pending 1 hour
815 uint32 expire_delay;
816 if(messageType == MAIL_AUCTION && !mi && !money) // auction mail without any items and money
817 expire_delay = HOUR;
818 else
819 expire_delay = (COD > 0) ? 3*DAY : 30*DAY;
821 time_t expire_time = deliver_time + expire_delay;
823 if(mailTemplateId && !sMailTemplateStore.LookupEntry(mailTemplateId))
825 sLog.outError( "WorldSession::SendMailTo - Mail have not existed MailTemplateId (%u), remove at send", mailTemplateId);
826 mailTemplateId = 0;
829 if(receiver)
831 receiver->AddNewMailDeliverTime(deliver_time);
833 if ( receiver->IsMailsLoaded() )
835 Mail * m = new Mail;
836 m->messageID = mailId;
837 m->messageType = messageType;
838 m->stationery = stationery;
839 m->mailTemplateId = mailTemplateId;
840 m->sender = sender_guidlow_or_entry;
841 m->receiver = receiver->GetGUIDLow();
842 m->subject = subject;
843 m->itemTextId = itemTextId;
845 if(mi)
846 m->AddAllItems(*mi);
848 m->expire_time = expire_time;
849 m->deliver_time = deliver_time;
850 m->money = money;
851 m->COD = COD;
852 m->checked = checked;
853 m->state = MAIL_STATE_UNCHANGED;
855 receiver->AddMail(m); //to insert new mail to beginning of maillist
857 if(mi)
859 for(MailItemMap::iterator mailItemIter = mi->begin(); mailItemIter != mi->end(); ++mailItemIter)
861 MailItem& mailItem = mailItemIter->second;
862 if(mailItem.item)
863 receiver->AddMItem(mailItem.item);
867 else if(mi)
868 mi->deleteIncludedItems();
870 else if(mi)
871 mi->deleteIncludedItems();
873 CharacterDatabase.BeginTransaction();
874 CharacterDatabase.escape_string(subject);
875 CharacterDatabase.PExecute("INSERT INTO mail (id,messageType,stationery,mailTemplateId,sender,receiver,subject,itemTextId,has_items,expire_time,deliver_time,money,cod,checked) "
876 "VALUES ('%u', '%u', '%u', '%u', '%u', '%u', '%s', '%u', '%u', '" I64FMTD "','" I64FMTD "', '%u', '%u', '%d')",
877 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 if(mi)
881 for(MailItemMap::const_iterator mailItemIter = mi->begin(); mailItemIter != mi->end(); ++mailItemIter)
883 MailItem const& mailItem = mailItemIter->second;
884 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);
887 CharacterDatabase.CommitTransaction();