From e471a1f93ee23f0f729d64068504cb892481b56f Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 16 Jun 2022 05:53:18 +0200 Subject: 4chan: show error from server when posting fails (when error is not one of the predefined ones) --- src/QuickMedia.cpp | 18 +++++++++--------- src/plugins/Fourchan.cpp | 37 +++++++++++++++++++++++++++---------- 2 files changed, 36 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 494e03d..a17b721 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -4364,7 +4364,7 @@ namespace QuickMedia { auto post_comment = [this, &comment_input, &selected_file_for_upload, &navigation_stage, &thread_page, &captcha_post_id, &captcha_solution, &last_posted_time, &has_post_timeout](std::string comment_to_post, std::string file_to_upload) { comment_input.set_editable(false); PostResult post_result = thread_page->post_comment(captcha_post_id, captcha_solution, comment_to_post, file_to_upload); - if(post_result == PostResult::OK) { + if(post_result.type == PostResult::Type::OK) { show_notification("QuickMedia", "Comment posted!"); navigation_stage = NavigationStage::VIEWING_COMMENTS; comment_input.set_text(""); @@ -4377,30 +4377,30 @@ namespace QuickMedia { Path last_posted_time_filepath; if(get_image_board_last_posted_filepath(plugin_name, last_posted_time_filepath)) file_overwrite_atomic(last_posted_time_filepath, std::to_string(last_posted_time)); - } else if(post_result == PostResult::TRY_AGAIN) { + } else if(post_result.type == PostResult::Type::TRY_AGAIN) { show_notification("QuickMedia", "Please wait before posting again"); navigation_stage = NavigationStage::VIEWING_COMMENTS; - } else if(post_result == PostResult::INVALID_CAPTCHA) { + } else if(post_result.type == PostResult::Type::INVALID_CAPTCHA) { show_notification("QuickMedia", "Invalid captcha, please try again"); navigation_stage = NavigationStage::REQUESTING_CAPTCHA; // TODO: Need to wait before requesting need captcha? - } else if(post_result == PostResult::BANNED) { + } else if(post_result.type == PostResult::Type::BANNED) { show_notification("QuickMedia", "Failed to post comment because you are banned", Urgency::CRITICAL); navigation_stage = NavigationStage::VIEWING_COMMENTS; //} else if(post_result == PostResult::FILE_TOO_LARGE) { // show_notification("QuickMedia", "Failed to post comment because the file you are trying to upload is larger than " + std::to_string((double)thread_page->get_max_upload_file_size() * 1024.0 * 1024.0) + " mb", Urgency::CRITICAL); // navigation_stage = NavigationStage::VIEWING_COMMENTS; - } else if(post_result == PostResult::NO_SUCH_FILE) { + } else if(post_result.type == PostResult::Type::NO_SUCH_FILE) { show_notification("QuickMedia", "Failed to post comment because the file you are trying to upload no longer exists", Urgency::CRITICAL); navigation_stage = NavigationStage::VIEWING_COMMENTS; - } else if(post_result == PostResult::FILE_TYPE_NOT_ALLOWED) { + } else if(post_result.type == PostResult::Type::FILE_TYPE_NOT_ALLOWED) { show_notification("QuickMedia", "Failed to post comment because you are trying to upload a file of a type that is not allowed", Urgency::CRITICAL); navigation_stage = NavigationStage::VIEWING_COMMENTS; - } else if(post_result == PostResult::UPLOAD_FAILED) { + } else if(post_result.type == PostResult::Type::UPLOAD_FAILED) { show_notification("QuickMedia", "Failed to post comment because file upload failed", Urgency::CRITICAL); navigation_stage = NavigationStage::VIEWING_COMMENTS; - } else if(post_result == PostResult::ERR) { - show_notification("QuickMedia", "Failed to post comment", Urgency::CRITICAL); + } else if(post_result.type == PostResult::Type::ERR) { + show_notification("QuickMedia", "Failed to post comment, reason: " + post_result.err_msg, Urgency::CRITICAL); navigation_stage = NavigationStage::VIEWING_COMMENTS; } else { assert(false && "Unhandled post result"); diff --git a/src/plugins/Fourchan.cpp b/src/plugins/Fourchan.cpp index 4522a56..c11e13e 100644 --- a/src/plugins/Fourchan.cpp +++ b/src/plugins/Fourchan.cpp @@ -581,7 +581,7 @@ namespace QuickMedia { Path cookies_filepath; if(get_cookies_filepath(cookies_filepath, SERVICE_NAME) != 0) { fprintf(stderr, "Failed to get 4chan cookies filepath\n"); - return PostResult::ERR; + return PostResult::Type::ERR; } std::string url = "https://sys.4chan.org/" + board_id + "/post"; @@ -608,22 +608,39 @@ namespace QuickMedia { std::string response; if(download_to_string(url, response, additional_args, true) != DownloadResult::OK) - return PostResult::ERR; + return PostResult::Type::ERR; if(response.find("successful") != std::string::npos) - return PostResult::OK; + return PostResult::Type::OK; if(response.find("banned") != std::string::npos) - return PostResult::BANNED; + return PostResult::Type::BANNED; if(response.find("mistyped the CAPTCHA") != std::string::npos || response.find("No valid captcha") != std::string::npos) - return PostResult::INVALID_CAPTCHA; + return PostResult::Type::INVALID_CAPTCHA; if(response.find("Audio streams are not allowed") != std::string::npos) - return PostResult::FILE_TYPE_NOT_ALLOWED; + return PostResult::Type::FILE_TYPE_NOT_ALLOWED; if(response.find("Error: Upload failed") != std::string::npos) - return PostResult::UPLOAD_FAILED; + return PostResult::Type::UPLOAD_FAILED; if(response.find("try again") != std::string::npos) - return PostResult::TRY_AGAIN; - - return PostResult::ERR; + return PostResult::Type::TRY_AGAIN; + + size_t errmsg_start = response.find("id=\"errmsg\""); + if(errmsg_start == std::string::npos) + return PostResult::Type::ERR; + + errmsg_start += 11; + errmsg_start = response.find('>', errmsg_start); + if(errmsg_start == std::string::npos) + return PostResult::Type::ERR; + + errmsg_start += 1; + const size_t errmsg_end = response.find('<', errmsg_start); + if(errmsg_end == std::string::npos) + return PostResult::Type::ERR; + + PostResult result(PostResult::Type::ERR); + result.err_msg = response.substr(errmsg_start, errmsg_end - errmsg_start); + html_unescape_sequences(result.err_msg); + return result; } const std::string& FourchanThreadPage::get_pass_id() { -- cgit v1.2.3