/** * Handles the Packet sent by the client when returning a mail to sender. * This method is called when a player chooses to return a mail to its sender. * It will create a new MailDraft and add the items, money, etc. associated with the mail * and then send the mail to the original sender. * * @param recv_data The packet containing information about the mail being returned. * */ void WorldSession::HandleMailReturnToSender(WorldPacket & recv_data ) { uint64 mailbox; uint32 mailId; recv_data >> mailbox; recv_data >> mailId; recv_data.read_skip<uint64>(); // original sender GUID for return to, not used if (!GetPlayer()->GetGameObjectIfCanInteractWith(mailbox, GAMEOBJECT_TYPE_MAILBOX)) return; Player *pl = _player; Mail *m = pl->GetMail(mailId); if(!m || m->state == MAIL_STATE_DELETED || m->deliver_time > time(NULL)) { pl->SendMailResult(mailId, MAIL_RETURNED_TO_SENDER, MAIL_ERR_INTERNAL_ERROR); return; } //we can return mail now //so firstly delete the old one CharacterDatabase.BeginTransaction(); CharacterDatabase.PExecute("DELETE FROM mail WHERE id = '%u'", mailId); // needed? CharacterDatabase.PExecute("DELETE FROM mail_items WHERE mail_id = '%u'", mailId); CharacterDatabase.CommitTransaction(); pl->RemoveMail(mailId); // send back only to existing players and simple drop for other cases if (m->messageType == MAIL_NORMAL && m->sender) { MailDraft draft; if (m->mailTemplateId) draft.SetMailTemplate(m->mailTemplateId, false);// items already included else draft.SetSubjectAndBody(m->subject, m->body); if(m->HasItems()) { for(MailItemInfoVec::iterator itr2 = m->items.begin(); itr2 != m->items.end(); ++itr2) { if(Item *item = pl->GetMItem(itr2->item_guid)) draft.AddItem(item); pl->RemoveMItem(itr2->item_guid); } } draft.SetMoney(m->money).SendReturnToSender(GetAccountId(), m->receiverGuid, ObjectGuid(HIGHGUID_PLAYER, m->sender)); } delete m; // we can deallocate old mail pl->SendMailResult(mailId, MAIL_RETURNED_TO_SENDER, MAIL_OK); }
void WorldSession::SendExternalMails() { sLog.outDebug("External Mail - Send Mails from Queue..."); QueryResult* result = CharacterDatabase.Query("SELECT id,receiver,subject,message,money,item,item_count FROM mail_external"); if (!result) { sLog.outDebug("External Mail - No Mails in Queue..."); return; } else { do { Field *fields = result->Fetch(); uint32 id = fields[0].GetUInt32(); Player *pReceiver = sObjectMgr.GetPlayer(fields[1].GetUInt64()); std::string subject = fields[2].GetString(); std::string message = fields[3].GetString(); uint32 money = fields[4].GetUInt32(); uint32 ItemID = fields[5].GetUInt32(); uint32 ItemCount = fields[6].GetUInt32(); if (pReceiver) { MailDraft draft; draft.SetSubjectAndBody(subject, message); MailSender sender(MAIL_NORMAL, pReceiver ? pReceiver->GetObjectGuid().GetCounter() : 0, MAIL_STATIONERY_GM); MailReceiver reciever(pReceiver, pReceiver->GetObjectGuid()); sLog.outDebug("External Mail - Sending mail to %u, Item:%u", pReceiver->GetObjectGuid().GetCounter(), ItemID); uint32 itemTextId = !message.empty() ? sObjectMgr.CreateItemText(message) : 0; if (ItemID != 0) { Item* ToMailItem = Item::CreateItem(ItemID, ItemCount, pReceiver); if (ToMailItem) { ToMailItem->SaveToDB(); draft.AddItem(ToMailItem); } draft.SetMoney(money); } else draft.SetMoney(money); draft.SendMailTo(reciever,sender,MAIL_CHECK_MASK_RETURNED); CharacterDatabase.PExecute("DELETE FROM mail_external WHERE id=%u", id); } else sLog.outDebug("External Mail - Mail with unknown player GUID %u in mail_external",fields[1].GetUInt32()); } while(result->NextRow()); } sLog.outDebug("External Mail - All Mails Sent..."); }