void MailSystem::UpdateMessages() { if((++loopcount % 1200)) return; QueryResult *result = CharacterDatabase.Query("SELECT * FROM mailbox WHERE expiry_time > 0 and expiry_time <= %u and deleted_flag = 0",(uint32)UNIXTIME); if(!result) return; MailMessage msg; do { if (msg.LoadFromDB(result->Fetch())) { if (/*msg.Expired() &&*/ msg.items.size() == 0 && msg.money == 0) { if(msg.copy_made) { msg.deleted_flag = true; msg.SaveToDB(); } else { CharacterDatabase.WaitExecute("DELETE FROM mailbox WHERE message_id = %u", msg.message_id); } } else { ReturnToSender(&msg); } } } while(result->NextRow()); delete result; }
void Mailbox::OnMessageCopyDeleted(uint32 msg_id) { MailMessage * msg = GetMessage(msg_id); if(msg == 0) return; msg->copy_made = false; if(msg->deleted_flag) // we've deleted from inbox DeleteMessage(msg); // wipe the message else msg->SaveToDB(); }
void WorldSession::HandleTakeMoney(WorldPacket & recv_data ) { uint64 mailbox; uint32 message_id; recv_data >> mailbox >> message_id; WorldPacket data(SMSG_SEND_MAIL_RESULT, 12); data << message_id << uint32(MAIL_RES_MONEY_TAKEN); MailMessage * message = _player->m_mailBox->GetMessage(message_id); if(message == 0 || message->Expired() || !message->money) { data << uint32(MAIL_ERR_INTERNAL_ERROR); SendPacket(&data); return; } // add the money to the player if((_player->GetUInt32Value(PLAYER_FIELD_COINAGE) + message->money) >= PLAYER_MAX_GOLD ) { data << uint32(MAIL_ERR_INTERNAL_ERROR); SendPacket(&data); return; } else _player->ModUnsigned32Value(PLAYER_FIELD_COINAGE, message->money); // force save _player->SaveToDB(false); // message no longer has any money message->money = 0; if ((message->items.size() == 0) && (message->money == 0)) { // mail now has a 3 day expiry time if(!sMailSystem.MailOption(MAIL_FLAG_NO_EXPIRY)) message->expire_time = (uint32)UNIXTIME + (TIME_DAY * 3); } // update in sql! message->SaveToDB(); // send result data << uint32(MAIL_OK); SendPacket(&data); }
void WorldSession::HandleMailCreateTextItem(WorldPacket & recv_data ) { uint64 mailbox; uint32 message_id; recv_data >> mailbox >> message_id; WorldPacket data(SMSG_SEND_MAIL_RESULT, 12); data << message_id << uint32(MAIL_RES_MADE_PERMANENT); ItemPrototype * proto = ItemPrototypeStorage.LookupEntry(8383); MailMessage * message = _player->m_mailBox->GetMessage(message_id); if(message == 0 || message->Expired() || message->copy_made || !proto) { data << uint32(MAIL_ERR_INTERNAL_ERROR); SendPacket(&data); return; } SlotResult result = _player->GetItemInterface()->FindFreeInventorySlot(proto); if(result.Result == 0) { data << uint32(MAIL_ERR_INTERNAL_ERROR); SendPacket(&data); return; } ItemPointer pItem = objmgr.CreateItem(8383, _player); pItem->SetUInt32Value(ITEM_FIELD_ITEM_TEXT_ID, message_id); if( _player->GetItemInterface()->AddItemToFreeSlot(pItem) ) { // mail now has an item after it message->copy_made = true; // update in sql message->SaveToDB(); data << uint32(MAIL_OK); SendPacket(&data); } else { pItem->Destructor(); pItem = NULLITEM; } }
void WorldSession::HandleMarkAsRead(WorldPacket & recv_data ) { uint64 mailbox; uint32 message_id; recv_data >> mailbox >> message_id; MailMessage * message = _player->m_mailBox->GetMessage(message_id); if(message == 0 || message->Expired()) return; message->read_flag = true; if(!message->returned_flag) { // mail now has a 3 day expiry time if(!sMailSystem.MailOption(MAIL_FLAG_NO_EXPIRY)) message->expire_time = (uint32)UNIXTIME + (TIME_DAY * 3); } message->SaveToDB(); }
void WorldSession::HandleTakeItem(WorldPacket & recv_data ) { uint64 mailbox; uint32 message_id; uint32 lowguid; vector< uint64 >::iterator itr; recv_data >> mailbox >> message_id >> lowguid; WorldPacket data(SMSG_SEND_MAIL_RESULT, 12); data << message_id << uint32(MAIL_RES_ITEM_TAKEN); MailMessage * message = _player->m_mailBox->GetMessage(message_id); if(message == 0 || message->Expired() || message->items.empty()) { data << uint32(MAIL_ERR_INTERNAL_ERROR); SendPacket(&data); return; } for( itr = message->items.begin( ); itr != message->items.end( ); ++itr ) { if ( (*itr) == lowguid ) break; } if( itr == message->items.end( ) ) { data << uint32(MAIL_ERR_INTERNAL_ERROR); SendPacket(&data); return; } // check for cod credit if(message->cod > 0) { if(_player->GetUInt32Value(PLAYER_FIELD_COINAGE) < message->cod) { data << uint32(MAIL_ERR_NOT_ENOUGH_MONEY); SendPacket(&data); return; } } // grab the item ItemPointer item = objmgr.LoadItem( *itr ); if(item == 0) { // doesn't exist data << uint32(MAIL_ERR_INTERNAL_ERROR); SendPacket(&data); return; } // find a free bag slot SlotResult result = _player->GetItemInterface()->FindFreeInventorySlot(item->GetProto()); if(result.Result == 0) { // no free slots left! data << uint32(MAIL_ERR_BAG_FULL); SendPacket(&data); item->Destructor(); item = NULLITEM; return; } item->m_isDirty = true; if( !_player->GetItemInterface()->SafeAddItem(item, result.ContainerSlot, result.Slot) ) { if( !_player->GetItemInterface()->AddItemToFreeSlot(item) ) { // no free slots left! data << uint32(MAIL_ERR_BAG_FULL); SendPacket(&data); item->Destructor(); item = NULLITEM; return; } } else // true so it waitexecute's item->SaveToDB(result.ContainerSlot, result.Slot, true, NULL); // send complete packet data << uint32(MAIL_OK); data << item->GetUInt32Value(OBJECT_FIELD_GUID); data << item->GetUInt32Value(ITEM_FIELD_STACK_COUNT); message->items.erase( itr ); if ((message->items.size() == 0) && (message->money == 0)) { // mail now has a 3 day expiry time if(!sMailSystem.MailOption(MAIL_FLAG_NO_EXPIRY)) message->expire_time = (uint32)UNIXTIME + (TIME_DAY * 3); } if( message->cod > 0 ) { _player->ModUnsigned32Value(PLAYER_FIELD_COINAGE, -int32(message->cod)); string subject = "COD Payment: "; subject += message->subject; sMailSystem.DeliverMessage(NORMAL, message->player_guid, message->sender_guid, subject, "", message->cod, 0, 0, 1, true); message->cod = 0; } // re-save (update the items field) message->SaveToDB();// sMailSystem.SaveMessageToSQL( message); SendPacket(&data); // prolly need to send an item push here }