[6844] Daily quest fixes.
[getmangos.git] / src / game / Mail.cpp
blob7b29d20dbc7fcd828830e984673c251d84455978
1 /*
2 * Copyright (C) 2005-2008 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 "Database/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 // recheck
56 CHECK_PACKET_SIZE(recv_data, 8+(receiver.size()+1)+1+1+4+4+1+4+4+8+1);
58 recv_data >> subject;
60 // recheck
61 CHECK_PACKET_SIZE(recv_data, 8+(receiver.size()+1)+(subject.size()+1)+1+4+4+1+4+4+8+1);
63 recv_data >> body;
65 // recheck
66 CHECK_PACKET_SIZE(recv_data, 8+(receiver.size()+1)+(subject.size()+1)+(body.size()+1)+4+4+1+4+4+8+1);
68 recv_data >> unk1; // stationery?
69 recv_data >> unk2; // 0x00000000
71 MailItemsInfo mi;
73 uint8 items_count;
74 recv_data >> items_count; // attached items count
76 if(items_count > 12) // client limit
77 return;
79 // recheck
80 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);
82 if(items_count)
84 for(uint8 i = 0; i < items_count; ++i)
86 uint8 item_slot;
87 uint64 item_guid;
88 recv_data >> item_slot;
89 recv_data >> item_guid;
90 mi.AddItem(GUID_LOPART(item_guid), item_slot);
94 recv_data >> money >> COD; // money and cod
95 recv_data >> unk3; // const 0
96 recv_data >> unk4; // const 0
98 items_count = mi.size(); // this is the real size after the duplicates have been removed
100 if (receiver.empty())
101 return;
103 Player* pl = _player;
105 uint64 rc = 0;
106 if(normalizePlayerName(receiver))
107 rc = objmgr.GetPlayerGUIDByName(receiver);
109 if (!rc)
111 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",
112 pl->GetGUIDLow(), receiver.c_str(), subject.c_str(), body.c_str(), items_count, money, COD, unk1, unk2);
113 pl->SendMailResult(0, 0, MAIL_ERR_RECIPIENT_NOT_FOUND);
114 return;
117 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);
119 if(pl->GetGUID() == rc)
121 pl->SendMailResult(0, 0, MAIL_ERR_CANNOT_SEND_TO_SELF);
122 return;
125 uint32 reqmoney = money + 30;
126 if (items_count)
127 reqmoney = money + (30 * items_count);
129 if (pl->GetMoney() < reqmoney)
131 pl->SendMailResult(0, 0, MAIL_ERR_NOT_ENOUGH_MONEY);
132 return;
135 Player *receive = objmgr.GetPlayer(rc);
137 uint32 rc_team = 0;
138 uint8 mails_count = 0; //do not allow to send to one player more than 100 mails
140 if(receive)
142 rc_team = receive->GetTeam();
143 mails_count = receive->GetMailSize();
145 else
147 rc_team = objmgr.GetPlayerTeamByGUID(rc);
148 QueryResult* result = CharacterDatabase.PQuery("SELECT COUNT(*) FROM mail WHERE receiver = '%u'", GUID_LOPART(rc));
149 if(result)
151 Field *fields = result->Fetch();
152 mails_count = fields[0].GetUInt32();
153 delete result;
156 //do not allow to have more than 100 mails in mailbox.. mails count is in opcode uint8!!! - so max can be 255..
157 if (mails_count > 100)
159 pl->SendMailResult(0, 0, MAIL_ERR_INTERNAL_ERROR);
160 return;
162 // test the receiver's Faction...
163 if (!sWorld.getConfig(CONFIG_ALLOW_TWO_SIDE_INTERACTION_MAIL) && pl->GetTeam() != rc_team && GetSecurity() == SEC_PLAYER)
165 pl->SendMailResult(0, 0, MAIL_ERR_NOT_YOUR_TEAM);
166 return;
169 if (items_count)
171 for(MailItemMap::iterator mailItemIter = mi.begin(); mailItemIter != mi.end(); ++mailItemIter)
173 MailItem& mailItem = mailItemIter->second;
175 if(!mailItem.item_guidlow)
177 pl->SendMailResult(0, 0, MAIL_ERR_INTERNAL_ERROR);
178 return;
181 mailItem.item = pl->GetItemByGuid(MAKE_NEW_GUID(mailItem.item_guidlow, 0, HIGHGUID_ITEM));
182 // prevent sending bag with items (cheat: can be placed in bag after adding equipped empty bag to mail)
183 if(!mailItem.item || !mailItem.item->CanBeTraded())
185 pl->SendMailResult(0, 0, MAIL_ERR_INTERNAL_ERROR);
186 return;
188 if (mailItem.item->HasFlag(ITEM_FIELD_FLAGS, ITEM_FLAGS_CONJURED) || mailItem.item->GetUInt32Value(ITEM_FIELD_DURATION))
190 pl->SendMailResult(0, 0, MAIL_ERR_INTERNAL_ERROR);
191 return;
194 if(COD && mailItem.item->HasFlag(ITEM_FIELD_FLAGS, ITEM_FLAGS_WRAPPED))
196 pl->SendMailResult(0, 0, MAIL_ERR_CANT_SEND_WRAPPED_COD);
197 return;
201 pl->SendMailResult(0, 0, MAIL_OK);
203 uint32 itemTextId = 0;
204 if (!body.empty())
206 itemTextId = objmgr.CreateItemText( body );
209 pl->ModifyMoney( -int32(reqmoney) );
211 bool needItemDelay = false;
213 if(items_count > 0 || money > 0)
215 uint32 rc_account = 0;
216 if(receive)
217 rc_account = receive->GetSession()->GetAccountId();
218 else
219 rc_account = objmgr.GetPlayerAccountIdByGUID(rc);
221 if (items_count > 0)
223 for(MailItemMap::iterator mailItemIter = mi.begin(); mailItemIter != mi.end(); ++mailItemIter)
225 MailItem& mailItem = mailItemIter->second;
226 if(!mailItem.item)
227 continue;
229 mailItem.item_template = mailItem.item ? mailItem.item->GetEntry() : 0;
231 if( GetSecurity() > SEC_PLAYER && sWorld.getConfig(CONFIG_GM_LOG_TRADE) )
233 sLog.outCommand("GM %s (Account: %u) mail item: %s (Entry: %u Count: %u) to player: %s (Account: %u)",
234 GetPlayerName(), GetAccountId(), mailItem.item->GetProto()->Name1, mailItem.item->GetEntry(), mailItem.item->GetCount(), receiver.c_str(), rc_account);
237 pl->MoveItemFromInventory(mailItem.item->GetBagSlot(), mailItem.item->GetSlot(), true);
238 CharacterDatabase.BeginTransaction();
239 mailItem.item->DeleteFromInventoryDB(); //deletes item from character's inventory
240 mailItem.item->SaveToDB(); // recursive and not have transaction guard into self, item not in inventory and can be save standalone
241 // owner in data will set at mail receive and item extracting
242 CharacterDatabase.PExecute("UPDATE item_instance SET owner_guid = '%u' WHERE guid='%u'", GUID_LOPART(rc), mailItem.item->GetGUIDLow());
243 CharacterDatabase.CommitTransaction();
246 // if item send to character at another account, then apply item delivery delay
247 needItemDelay = pl->GetSession()->GetAccountId() != rc_account;
250 if(money > 0 && GetSecurity() > SEC_PLAYER && sWorld.getConfig(CONFIG_GM_LOG_TRADE))
252 sLog.outCommand("GM %s (Account: %u) mail money: %u to player: %s (Account: %u)",
253 GetPlayerName(), GetAccountId(), money, receiver.c_str(), rc_account);
257 // If theres is an item, there is a one hour delivery delay if sent to another account's character.
258 uint32 deliver_delay = needItemDelay ? sWorld.getConfig(CONFIG_MAIL_DELIVERY_DELAY) : 0;
260 // will delete item or place to receiver mail list
261 WorldSession::SendMailTo(receive, MAIL_NORMAL, MAIL_STATIONERY_NORMAL, pl->GetGUIDLow(), GUID_LOPART(rc), subject, itemTextId, &mi, money, COD, MAIL_CHECK_MASK_NONE, deliver_delay);
263 CharacterDatabase.BeginTransaction();
264 pl->SaveInventoryAndGoldToDB();
265 CharacterDatabase.CommitTransaction();
268 //called when mail is read
269 void WorldSession::HandleMarkAsRead(WorldPacket & recv_data )
271 CHECK_PACKET_SIZE(recv_data,8+4);
273 uint64 mailbox;
274 uint32 mailId;
275 recv_data >> mailbox;
276 recv_data >> mailId;
277 Player *pl = _player;
278 Mail *m = pl->GetMail(mailId);
279 if (m)
281 if (pl->unReadMails)
282 --pl->unReadMails;
283 m->checked = m->checked | MAIL_CHECK_MASK_READ;
284 // m->expire_time = time(NULL) + (30 * DAY); // Expire time do not change at reading mail
285 pl->m_mailsUpdated = true;
286 m->state = MAIL_STATE_CHANGED;
290 //called when client deletes mail
291 void WorldSession::HandleMailDelete(WorldPacket & recv_data )
293 CHECK_PACKET_SIZE(recv_data,8+4);
295 uint64 mailbox;
296 uint32 mailId;
297 recv_data >> mailbox;
298 recv_data >> mailId;
299 Player* pl = _player;
300 pl->m_mailsUpdated = true;
301 Mail *m = pl->GetMail(mailId);
302 if(m)
303 m->state = MAIL_STATE_DELETED;
304 pl->SendMailResult(mailId, MAIL_DELETED, 0);
307 void WorldSession::HandleReturnToSender(WorldPacket & recv_data )
309 CHECK_PACKET_SIZE(recv_data,8+4);
311 uint64 mailbox;
312 uint32 mailId;
313 recv_data >> mailbox;
314 recv_data >> mailId;
315 Player *pl = _player;
316 Mail *m = pl->GetMail(mailId);
317 if(!m || m->state == MAIL_STATE_DELETED || m->deliver_time > time(NULL))
319 pl->SendMailResult(mailId, MAIL_RETURNED_TO_SENDER, MAIL_ERR_INTERNAL_ERROR);
320 return;
322 //we can return mail now
323 //so firstly delete the old one
324 CharacterDatabase.BeginTransaction();
325 CharacterDatabase.PExecute("DELETE FROM mail WHERE id = '%u'", mailId);
326 // needed?
327 CharacterDatabase.PExecute("DELETE FROM mail_items WHERE mail_id = '%u'", mailId);
328 CharacterDatabase.CommitTransaction();
329 pl->RemoveMail(mailId);
331 MailItemsInfo mi;
333 if(m->HasItems())
335 for(std::vector<MailItemInfo>::iterator itr2 = m->items.begin(); itr2 != m->items.end(); ++itr2)
337 Item *item = pl->GetMItem(itr2->item_guid);
338 if(item)
339 mi.AddItem(item->GetGUIDLow(), item->GetEntry(), item);
340 else
342 //WTF?
345 pl->RemoveMItem(itr2->item_guid);
349 SendReturnToSender(MAIL_NORMAL, GetAccountId(), m->receiver, m->sender, m->subject, m->itemTextId, &mi, m->money, m->mailTemplateId);
351 delete m; //we can deallocate old mail
352 pl->SendMailResult(mailId, MAIL_RETURNED_TO_SENDER, 0);
355 void WorldSession::SendReturnToSender(uint8 messageType, uint32 sender_acc, uint32 sender_guid, uint32 receiver_guid, std::string subject, uint32 itemTextId, MailItemsInfo *mi, uint32 money, uint16 mailTemplateId )
357 if(messageType != MAIL_NORMAL) // return only to players
359 mi->deleteIncludedItems(true);
360 return;
363 Player *receiver = objmgr.GetPlayer(MAKE_NEW_GUID(receiver_guid, 0, HIGHGUID_PLAYER));
365 uint32 rc_account = 0;
366 if(!receiver)
367 rc_account = objmgr.GetPlayerAccountIdByGUID(MAKE_NEW_GUID(receiver_guid, 0, HIGHGUID_PLAYER));
369 if(!receiver && !rc_account) // sender not exist
371 mi->deleteIncludedItems(true);
372 return;
375 // prepare mail and send in other case
376 bool needItemDelay = false;
378 if(mi && !mi->empty())
380 // if item send to character at another account, then apply item delivery delay
381 needItemDelay = sender_acc != rc_account;
383 // set owner to new receiver (to prevent delete item with sender char deleting)
384 CharacterDatabase.BeginTransaction();
385 for(MailItemMap::iterator mailItemIter = mi->begin(); mailItemIter != mi->end(); ++mailItemIter)
387 MailItem& mailItem = mailItemIter->second;
388 mailItem.item->SaveToDB(); // item not in inventory and can be save standalone
389 // owner in data will set at mail receive and item extracting
390 CharacterDatabase.PExecute("UPDATE item_instance SET owner_guid = '%u' WHERE guid='%u'", receiver_guid, mailItem.item->GetGUIDLow());
392 CharacterDatabase.CommitTransaction();
395 // If theres is an item, there is a one hour delivery delay.
396 uint32 deliver_delay = needItemDelay ? sWorld.getConfig(CONFIG_MAIL_DELIVERY_DELAY) : 0;
398 // will delete item or place to receiver mail list
399 WorldSession::SendMailTo(receiver, MAIL_NORMAL, MAIL_STATIONERY_NORMAL, sender_guid, receiver_guid, subject, itemTextId, mi, money, 0, MAIL_CHECK_MASK_RETURNED,deliver_delay,mailTemplateId);
402 //called when player takes item attached in mail
403 void WorldSession::HandleTakeItem(WorldPacket & recv_data )
405 CHECK_PACKET_SIZE(recv_data,8+4+4);
407 uint64 mailbox;
408 uint32 mailId;
409 uint32 itemId;
410 recv_data >> mailbox;
411 recv_data >> mailId;
412 recv_data >> itemId; // item guid low?
413 Player* pl = _player;
415 Mail* m = pl->GetMail(mailId);
416 if(!m || m->state == MAIL_STATE_DELETED || m->deliver_time > time(NULL))
418 pl->SendMailResult(mailId, MAIL_ITEM_TAKEN, MAIL_ERR_INTERNAL_ERROR);
419 return;
422 // prevent cheating with skip client money check
423 if(pl->GetMoney() < m->COD)
425 pl->SendMailResult(mailId, MAIL_ITEM_TAKEN, MAIL_ERR_NOT_ENOUGH_MONEY);
426 return;
429 Item *it = pl->GetMItem(itemId);
431 ItemPosCountVec dest;
432 uint8 msg = _player->CanStoreItem( NULL_BAG, NULL_SLOT, dest, it, false );
433 if( msg == EQUIP_ERR_OK )
435 m->RemoveItem(itemId);
436 m->removedItems.push_back(itemId);
438 if (m->COD > 0) //if there is COD, take COD money from player and send them to sender by mail
440 uint64 sender_guid = MAKE_NEW_GUID(m->sender, 0, HIGHGUID_PLAYER);
441 Player *receive = objmgr.GetPlayer(sender_guid);
443 uint32 sender_accId = 0;
445 if( GetSecurity() > SEC_PLAYER && sWorld.getConfig(CONFIG_GM_LOG_TRADE) )
447 std::string sender_name;
448 if(receive)
450 sender_accId = receive->GetSession()->GetAccountId();
451 sender_name = receive->GetName();
453 else
455 // can be calculated early
456 sender_accId = objmgr.GetPlayerAccountIdByGUID(sender_guid);
458 if(!objmgr.GetPlayerNameByGUID(sender_guid,sender_name))
459 sender_name = objmgr.GetMangosStringForDBCLocale(LANG_UNKNOWN);
461 sLog.outCommand("GM %s (Account: %u) receive mail item: %s (Entry: %u Count: %u) and send COD money: %u to player: %s (Account: %u)",
462 GetPlayerName(),GetAccountId(),it->GetProto()->Name1,it->GetEntry(),it->GetCount(),m->COD,sender_name.c_str(),sender_accId);
464 else if(!receive)
465 sender_accId = objmgr.GetPlayerAccountIdByGUID(sender_guid);
467 // check player existence
468 if(receive || sender_accId)
470 WorldSession::SendMailTo(receive, MAIL_NORMAL, MAIL_STATIONERY_NORMAL, m->receiver, m->sender, m->subject, 0, NULL, m->COD, 0, MAIL_CHECK_MASK_COD_PAYMENT);
473 pl->ModifyMoney( -int32(m->COD) );
475 m->COD = 0;
476 m->state = MAIL_STATE_CHANGED;
477 pl->m_mailsUpdated = true;
478 pl->RemoveMItem(it->GetGUIDLow());
480 uint32 count = it->GetCount(); // save counts before store and possible merge with deleting
481 pl->MoveItemToInventory(dest,it,true);
483 CharacterDatabase.BeginTransaction();
484 pl->SaveInventoryAndGoldToDB();
485 pl->_SaveMail();
486 CharacterDatabase.CommitTransaction();
488 pl->SendMailResult(mailId, MAIL_ITEM_TAKEN, MAIL_OK, 0, itemId, count);
490 else
491 pl->SendMailResult(mailId, MAIL_ITEM_TAKEN, MAIL_ERR_BAG_FULL, msg);
494 void WorldSession::HandleTakeMoney(WorldPacket & recv_data )
496 CHECK_PACKET_SIZE(recv_data,8+4);
498 uint64 mailbox;
499 uint32 mailId;
500 recv_data >> mailbox;
501 recv_data >> mailId;
502 Player *pl = _player;
504 Mail* m = pl->GetMail(mailId);
505 if(!m || m->state == MAIL_STATE_DELETED || m->deliver_time > time(NULL))
507 pl->SendMailResult(mailId, MAIL_MONEY_TAKEN, MAIL_ERR_INTERNAL_ERROR);
508 return;
511 pl->SendMailResult(mailId, MAIL_MONEY_TAKEN, 0);
513 pl->ModifyMoney(m->money);
514 m->money = 0;
515 m->state = MAIL_STATE_CHANGED;
516 pl->m_mailsUpdated = true;
518 // save money and mail to prevent cheating
519 CharacterDatabase.BeginTransaction();
520 pl->SetUInt32ValueInDB(PLAYER_FIELD_COINAGE,pl->GetMoney(),pl->GetGUID());
521 pl->_SaveMail();
522 CharacterDatabase.CommitTransaction();
525 //called when player lists his received mails
526 void WorldSession::HandleGetMail(WorldPacket & recv_data )
528 CHECK_PACKET_SIZE(recv_data,8);
530 uint64 mailbox;
531 recv_data >> mailbox;
533 //GameObject* obj = ObjectAccessor::GetGameObject(_player, mailbox);
534 //if(!obj || !obj->IsMailBox())
535 // return;
537 Player* pl = _player;
539 //load players mails, and mailed items
540 if(!pl->m_mailsLoaded)
541 pl ->_LoadMail();
543 // client can't work with packets > max int16 value
544 const uint32 maxPacketSize = 32767;
546 uint32 mails_count = 0; // real send to client mails amount
548 WorldPacket data(SMSG_MAIL_LIST_RESULT, (200)); // guess size
549 data << uint8(0); // mail's count
550 time_t cur_time = time(NULL);
552 for(PlayerMails::iterator itr = pl->GetmailBegin(); itr != pl->GetmailEnd(); ++itr)
554 // skip deleted or not delivered (deliver delay not expired) mails
555 if ((*itr)->state == MAIL_STATE_DELETED || cur_time < (*itr)->deliver_time)
556 continue;
558 uint8 item_count = (*itr)->items.size(); // max count is MAX_MAIL_ITEMS (12)
560 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);
562 if(data.wpos()+next_mail_size > maxPacketSize)
563 break;
565 data << (uint16) 0x0040; // unknown 2.3.0, different values
566 data << (uint32) (*itr)->messageID; // Message ID
567 data << (uint8) (*itr)->messageType; // Message Type
569 switch((*itr)->messageType)
571 case MAIL_NORMAL: // sender guid
572 data << uint64(MAKE_NEW_GUID((*itr)->sender, 0, HIGHGUID_PLAYER));
573 break;
574 case MAIL_CREATURE:
575 case MAIL_GAMEOBJECT:
576 case MAIL_AUCTION:
577 data << (uint32) (*itr)->sender; // creature/gameobject entry, auction id
578 break;
579 case MAIL_ITEM: // item entry (?) sender = "Unknown", NYI
580 break;
583 data << (uint32) (*itr)->COD; // COD
584 data << (uint32) (*itr)->itemTextId; // sure about this
585 data << (uint32) 0; // unknown
586 data << (uint32) (*itr)->stationery; // stationery (Stationery.dbc)
587 data << (uint32) (*itr)->money; // Gold
588 data << (uint32) 0x04; // unknown, 0x4 - auction, 0x10 - normal
589 // Time
590 data << (float) ((*itr)->expire_time-time(NULL))/DAY;
591 data << (uint32) (*itr)->mailTemplateId; // mail template (MailTemplate.dbc)
592 data << (*itr)->subject; // Subject string - once 00, when mail type = 3
594 data << (uint8) item_count;
596 for(uint8 i = 0; i < item_count; ++i)
598 Item *item = pl->GetMItem((*itr)->items[i].item_guid);
599 // item index (0-6?)
600 data << (uint8) i;
601 // item guid low?
602 data << (uint32) (item ? item->GetGUIDLow() : 0);
603 // entry
604 data << (uint32) (item ? item->GetEntry() : 0);
605 for(uint8 j = 0; j < 6; ++j)
607 // unsure
608 data << (uint32) (item ? item->GetEnchantmentCharges((EnchantmentSlot)j) : 0);
609 // unsure
610 data << (uint32) (item ? item->GetEnchantmentDuration((EnchantmentSlot)j) : 0);
611 // unsure
612 data << (uint32) (item ? item->GetEnchantmentId((EnchantmentSlot)j) : 0);
614 // can be negative
615 data << (uint32) (item ? item->GetItemRandomPropertyId() : 0);
616 // unk
617 data << (uint32) (item ? item->GetItemSuffixFactor() : 0);
618 // stack count
619 data << (uint8) (item ? item->GetCount() : 0);
620 // charges
621 data << (uint32) (item ? item->GetSpellCharges() : 0);
622 // durability
623 data << (uint32) (item ? item->GetUInt32Value(ITEM_FIELD_MAXDURABILITY) : 0);
624 // durability
625 data << (uint32) (item ? item->GetUInt32Value(ITEM_FIELD_DURABILITY) : 0);
628 mails_count += 1;
631 data.put<uint8>(0, mails_count); // set real send mails to client
632 SendPacket(&data);
634 // recalculate m_nextMailDelivereTime and unReadMails
635 _player->UpdateNextMailTimeAndUnreads();
638 ///this function is called when client needs mail message body, or when player clicks on item which has ITEM_FIELD_ITEM_TEXT_ID > 0
639 void WorldSession::HandleItemTextQuery(WorldPacket & recv_data )
641 CHECK_PACKET_SIZE(recv_data,4+4+4);
643 uint32 itemTextId;
644 uint32 mailId; //this value can be item id in bag, but it is also mail id
645 uint32 unk; //maybe something like state - 0x70000000
647 recv_data >> itemTextId >> mailId >> unk;
649 //some check needed, if player has item with guid mailId, or has mail with id mailId
651 sLog.outDebug("CMSG_ITEM_TEXT_QUERY itemguid: %u, mailId: %u, unk: %u", itemTextId, mailId, unk);
653 WorldPacket data(SMSG_ITEM_TEXT_QUERY_RESPONSE, (4+10));// guess size
654 data << itemTextId;
655 data << objmgr.GetItemText( itemTextId );
656 SendPacket(&data);
659 //used when player copies mail body to his inventory
660 void WorldSession::HandleMailCreateTextItem(WorldPacket & recv_data )
662 CHECK_PACKET_SIZE(recv_data,8+4);
664 uint64 mailbox;
665 uint32 mailId;
667 recv_data >> mailbox >> mailId;
669 Player *pl = _player;
671 Mail* m = pl->GetMail(mailId);
672 if(!m || !m->itemTextId || m->state == MAIL_STATE_DELETED || m->deliver_time > time(NULL))
674 pl->SendMailResult(mailId, MAIL_MADE_PERMANENT, MAIL_ERR_INTERNAL_ERROR);
675 return;
678 Item *bodyItem = new Item; // This is not bag and then can be used new Item.
679 if(!bodyItem->Create(objmgr.GenerateLowGuid(HIGHGUID_ITEM), MAIL_BODY_ITEM_TEMPLATE, pl))
681 delete bodyItem;
682 return;
685 bodyItem->SetUInt32Value( ITEM_FIELD_ITEM_TEXT_ID , m->itemTextId );
686 bodyItem->SetUInt32Value( ITEM_FIELD_CREATOR, m->sender);
688 sLog.outDetail("HandleMailCreateTextItem mailid=%u",mailId);
690 ItemPosCountVec dest;
691 uint8 msg = _player->CanStoreItem( NULL_BAG, NULL_SLOT, dest, bodyItem, false );
692 if( msg == EQUIP_ERR_OK )
694 m->itemTextId = 0;
695 m->state = MAIL_STATE_CHANGED;
696 pl->m_mailsUpdated = true;
698 pl->StoreItem(dest, bodyItem, true);
699 //bodyItem->SetState(ITEM_NEW, pl); is set automatically
700 pl->SendMailResult(mailId, MAIL_MADE_PERMANENT, 0);
702 else
704 pl->SendMailResult(mailId, MAIL_MADE_PERMANENT, MAIL_ERR_BAG_FULL, msg);
705 delete bodyItem;
709 //TODO Fix me! ... this void has probably bad condition, but good data are sent
710 void WorldSession::HandleMsgQueryNextMailtime(WorldPacket & /*recv_data*/ )
712 WorldPacket data(MSG_QUERY_NEXT_MAIL_TIME, 8);
714 if(!_player->m_mailsLoaded)
715 _player->_LoadMail();
717 if( _player->unReadMails > 0 )
719 data << (uint32) 0; // float
720 data << (uint32) 0; // count
721 uint32 count = 0;
722 for(PlayerMails::iterator itr = _player->GetmailBegin(); itr != _player->GetmailEnd(); ++itr)
724 Mail *m = (*itr);
725 // not checked yet, already must be delivered
726 if((m->checked & MAIL_CHECK_MASK_READ)==0 && (m->deliver_time <= time(NULL)))
728 ++count;
730 if(count > 2)
732 count = 2;
733 break;
736 data << (uint64) m->sender; // sender guid
738 switch(m->messageType)
740 case MAIL_AUCTION:
741 data << (uint32) 2;
742 data << (uint32) 2;
743 data << (uint32) m->stationery;
744 break;
745 default:
746 data << (uint32) 0;
747 data << (uint32) 0;
748 data << (uint32) m->stationery;
749 break;
751 data << (uint32) 0xC6000000; // float unk, time or something
754 data.put<uint32>(4, count);
756 else
758 data << (uint32) 0xC7A8C000;
759 data << (uint32) 0x00000000;
761 SendPacket(&data);
764 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)
766 uint32 mailId = objmgr.GenerateMailID();
768 time_t deliver_time = time(NULL) + deliver_delay;
770 //expire time if COD 3 days, if no COD 30 days, if auction sale pending 1 hour
771 uint32 expire_delay;
772 if(messageType == MAIL_AUCTION && !mi && !money) // auction mail without any items and money
773 expire_delay = HOUR;
774 else
775 expire_delay = (COD > 0) ? 3*DAY : 30*DAY;
777 time_t expire_time = deliver_time + expire_delay;
779 if(mailTemplateId && !sMailTemplateStore.LookupEntry(mailTemplateId))
781 sLog.outError( "WorldSession::SendMailTo - Mail have not existed MailTemplateId (%u), remove at send", mailTemplateId);
782 mailTemplateId = 0;
785 if(receiver)
787 receiver->AddNewMailDeliverTime(deliver_time);
789 if ( receiver->IsMailsLoaded() )
791 Mail * m = new Mail;
792 m->messageID = mailId;
793 m->messageType = messageType;
794 m->stationery = stationery;
795 m->mailTemplateId = mailTemplateId;
796 m->sender = sender_guidlow_or_entry;
797 m->receiver = receiver->GetGUIDLow();
798 m->subject = subject;
799 m->itemTextId = itemTextId;
801 if(mi)
802 m->AddAllItems(*mi);
804 m->expire_time = expire_time;
805 m->deliver_time = deliver_time;
806 m->money = money;
807 m->COD = COD;
808 m->checked = checked;
809 m->state = MAIL_STATE_UNCHANGED;
811 receiver->AddMail(m); //to insert new mail to beginning of maillist
813 if(mi)
815 for(MailItemMap::iterator mailItemIter = mi->begin(); mailItemIter != mi->end(); ++mailItemIter)
817 MailItem& mailItem = mailItemIter->second;
818 if(mailItem.item)
819 receiver->AddMItem(mailItem.item);
823 else if(mi)
824 mi->deleteIncludedItems();
826 else if(mi)
827 mi->deleteIncludedItems();
829 CharacterDatabase.BeginTransaction();
830 CharacterDatabase.escape_string(subject);
831 CharacterDatabase.PExecute("INSERT INTO mail (id,messageType,stationery,mailTemplateId,sender,receiver,subject,itemTextId,has_items,expire_time,deliver_time,money,cod,checked) "
832 "VALUES ('%u', '%u', '%u', '%u', '%u', '%u', '%s', '%u', '%u', '" I64FMTD "','" I64FMTD "', '%u', '%u', '%d')",
833 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);
835 if(mi)
837 for(MailItemMap::const_iterator mailItemIter = mi->begin(); mailItemIter != mi->end(); ++mailItemIter)
839 MailItem const& mailItem = mailItemIter->second;
840 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);
843 CharacterDatabase.CommitTransaction();