From ec23e72215fe690605d8c2b0a9349919d33d100c Mon Sep 17 00:00:00 2001 From: Gent S Date: Sun, 13 Dec 2020 19:51:00 -0500 Subject: [PATCH] Notify client of email send failure --- src/BuddyManager.cpp | 22 ++++++++++++++++++++-- src/Database.cpp | 7 ++++--- src/Database.hpp | 2 +- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/BuddyManager.cpp b/src/BuddyManager.cpp index 4a4b8f3..14c124e 100644 --- a/src/BuddyManager.cpp +++ b/src/BuddyManager.cpp @@ -742,7 +742,7 @@ void BuddyManager::emailSend(CNSocket* sock, CNPacketData* data) { // handle items std::vector attachments; - + std::vector attSlots; for (int i = 0; i < 4; i++) { sEmailItemInfoFromCL attachment = pkt->aItem[i]; resp.aItem[i] = attachment; @@ -750,6 +750,7 @@ void BuddyManager::emailSend(CNSocket* sock, CNPacketData* data) { || attachment.ItemInven.iID <= 0 || attachment.ItemInven.iType < 0) continue; // sanity check attachments.push_back(attachment.ItemInven); + attSlots.push_back(attachment.iSlotNum); // delete item plr->Inven[attachment.iSlotNum] = { 0, 0, 0, 0 }; } @@ -771,7 +772,24 @@ void BuddyManager::emailSend(CNSocket* sock, CNPacketData* data) { 0 // DeleteTime (unimplemented) }; - Database::sendEmail(&email, attachments); + if (!Database::sendEmail(&email, attachments)) { + plr->money += cost; // give money back + // give items back + while (!attachments.empty()) { + sItemBase attachment = attachments.back(); + plr->Inven[attSlots.back()] = attachment; + + attachments.pop_back(); + attSlots.pop_back(); + } + + // send error message + INITSTRUCT(sP_FE2CL_REP_PC_SEND_EMAIL_FAIL, errResp); + errResp.iErrorCode = 1; + errResp.iTo_PCUID = pkt->iTo_PCUID; + sock->sendPacket((void*)&errResp, P_FE2CL_REP_PC_SEND_EMAIL_FAIL, sizeof(sP_FE2CL_REP_PC_SEND_EMAIL_FAIL)); + return; + } // HACK: use set value packet to force GUI taros update INITSTRUCT(sP_FE2CL_GM_REP_PC_SET_VALUE, tarosResp); diff --git a/src/Database.cpp b/src/Database.cpp index 904eccd..3f35997 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -1739,7 +1739,7 @@ int Database::getNextEmailIndex(int playerID) { return (index > 0 ? index + 1 : 1); } -void Database::sendEmail(EmailData* data, std::vector attachments) { +bool Database::sendEmail(EmailData* data, std::vector attachments) { std::lock_guard lock(dbCrit); sqlite3_exec(db, "BEGIN TRANSACTION;", NULL, NULL, NULL); @@ -1770,7 +1770,7 @@ void Database::sendEmail(EmailData* data, std::vector attachments) { std::cout << "[WARN] Database: Failed to send email" << std::endl; sqlite3_exec(db, "ROLLBACK TRANSACTION;", NULL, NULL, NULL); sqlite3_finalize(stmt); - return; + return false; } // send attachments @@ -1796,11 +1796,12 @@ void Database::sendEmail(EmailData* data, std::vector attachments) { std::cout << "[WARN] Database: Failed to send email" << std::endl; sqlite3_exec(db, "ROLLBACK TRANSACTION;", NULL, NULL, NULL); sqlite3_finalize(stmt); - return; + return false; } sqlite3_reset(stmt); } sqlite3_finalize(stmt); sqlite3_exec(db, "COMMIT;", NULL, NULL, NULL); + return true; } diff --git a/src/Database.hpp b/src/Database.hpp index 278f25d..8564aee 100644 --- a/src/Database.hpp +++ b/src/Database.hpp @@ -86,5 +86,5 @@ namespace Database { void deleteEmailAttachments(int playerID, int index, int slot); void deleteEmails(int playerID, int64_t* indices); int getNextEmailIndex(int playerID); - void sendEmail(EmailData* data, std::vector attachments); + bool sendEmail(EmailData* data, std::vector attachments); }