aboutsummaryrefslogtreecommitdiff
path: root/src/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins')
-rw-r--r--src/plugins/Fourchan.cpp159
-rw-r--r--src/plugins/ImageBoard.cpp7
-rw-r--r--src/plugins/Page.cpp9
3 files changed, 125 insertions, 50 deletions
diff --git a/src/plugins/Fourchan.cpp b/src/plugins/Fourchan.cpp
index d3d5a5c..51a3cc5 100644
--- a/src/plugins/Fourchan.cpp
+++ b/src/plugins/Fourchan.cpp
@@ -5,6 +5,7 @@
#include "../../include/NetUtils.hpp"
#include "../../include/Notification.hpp"
#include "../../external/cppcodec/base64_rfc4648.hpp"
+#include "../../include/QuickMedia.hpp"
#include <HtmlParser.h>
#include <json/reader.h>
#include <string.h>
@@ -35,6 +36,69 @@ namespace QuickMedia {
return strip(file_content.substr(pass_id_index, line_end - pass_id_index));
}
+ static bool is_logged_in() {
+ Path cookies_filepath;
+ if(get_cookies_filepath(cookies_filepath, SERVICE_NAME) != 0) {
+ fprintf(stderr, "Failed to get 4chan cookies filepath\n");
+ return false;
+ }
+
+ std::vector<CommandArg> additional_args = {
+ CommandArg{"-c", cookies_filepath.data},
+ CommandArg{"-b", cookies_filepath.data}
+ };
+
+ std::string website_data;
+ if(download_to_string("https://sys.4chan.org/auth", website_data, additional_args) != DownloadResult::OK) {
+ show_notification("QuickMedia", "Failed to check if you are logged in", Urgency::CRITICAL);
+ return false;
+ }
+
+ // TODO: Check if this is correct
+ return website_data.find("field-id") == std::string::npos;
+ }
+
+ static bool login(Page *page, const std::string &token, const std::string &pin, std::string &pass_id, std::string &response_msg) {
+ response_msg.clear();
+
+ Path cookies_filepath;
+ if(get_cookies_filepath(cookies_filepath, SERVICE_NAME) != 0) {
+ fprintf(stderr, "Failed to get 4chan cookies filepath\n");
+ return false;
+ }
+
+ std::vector<CommandArg> additional_args = {
+ CommandArg{"--form-string", "id=" + token},
+ CommandArg{"--form-string", "pin=" + pin},
+ CommandArg{"--form-string", "xhr=1"},
+ CommandArg{"-c", cookies_filepath.data}
+ };
+
+ Json::Value json_root;
+ DownloadResult result = page->download_json(json_root, "https://sys.4chan.org/auth", std::move(additional_args), true);
+ if(result != DownloadResult::OK) return false;
+
+ if(!json_root.isObject())
+ return false;
+
+ const Json::Value &status_json = json_root["status"];
+ if(!status_json.isNumeric())
+ return false;
+
+ const Json::Value &message_json = json_root["message"];
+ if(message_json.isString())
+ response_msg = message_json.asString();
+
+ if(status_json.asInt64() == 1) {
+ pass_id = get_pass_id_from_cookies_file(cookies_filepath);
+ if(pass_id.empty())
+ return false;
+ return true;
+ } else {
+ return false;
+ }
+ }
+
struct CommentPiece {
enum class Type {
TEXT,
@@ -230,7 +294,7 @@ namespace QuickMedia {
}
PluginResult FourchanBoardsPage::submit(const SubmitArgs &args, std::vector<Tab> &result_tabs) {
- result_tabs.push_back(Tab{create_body(false, true), std::make_unique<FourchanThreadListPage>(program, args.title, args.url), create_search_bar("Search...", SEARCH_DELAY_FILTER)});
+ result_tabs.push_back(Tab{create_body(false, true), std::make_unique<FourchanThreadListPage>(program, args.title, args.url, pass_id), create_search_bar("Search...", SEARCH_DELAY_FILTER)});
return PluginResult::OK;
}
@@ -271,6 +335,56 @@ namespace QuickMedia {
}
}
+ PluginResult FourchanLoginPage::submit(const SubmitArgs &args, std::vector<Tab>&) {
+ if(args.url == "logout") {
+ Path cookies_filepath;
+ if(get_cookies_filepath(cookies_filepath, SERVICE_NAME) == 0)
+ remove(cookies_filepath.data.c_str());
+
+ boards_page->pass_id.clear();
+ logged_in = LoggedIn::No;
+ needs_refresh = true;
+ return PluginResult::OK;
+ }
+
+ for(const auto &login_input : login_inputs->inputs) {
+ if(login_input->get_text().empty()) {
+ show_notification("QuickMedia", "All fields need to be filled in", Urgency::CRITICAL);
+ return PluginResult::OK;
+ }
+ }
+
+ std::string err_msg;
+ if(login(this, login_inputs->inputs[0]->get_text(), login_inputs->inputs[1]->get_text(), boards_page->pass_id, err_msg)) {
+ login_finish();
+ return PluginResult::OK;
+ } else {
+ show_notification("QuickMedia", "Failed to login, error: " + err_msg, Urgency::CRITICAL);
+ return PluginResult::OK;
+ }
+ }
+
+ PluginResult FourchanLoginPage::lazy_fetch(BodyItems &result_items) {
+ if(logged_in == LoggedIn::Yes || (logged_in == LoggedIn::Unknown && is_logged_in())) {
+ logged_in = LoggedIn::Yes;
+ auto logout_body_item = BodyItem::create("Logout");
+ logout_body_item->url = "logout";
+ result_items.push_back(std::move(logout_body_item));
+ } else {
+ logged_in = LoggedIn::No;
+ program->add_login_inputs(&tabs->at(tab_index), {
+ { "Token", SearchBarType::Text },
+ { "PIN", SearchBarType::Password }
+ });
+ }
+ return PluginResult::OK;
+ }
+
+ void FourchanLoginPage::login_finish() {
+ logged_in = LoggedIn::Yes;
+ needs_refresh = true;
+ }
+
// TODO: Merge with lazy fetch
PluginResult FourchanThreadListPage::submit(const SubmitArgs &args, std::vector<Tab> &result_tabs) {
Json::Value json_root;
@@ -366,7 +480,7 @@ namespace QuickMedia {
auto body = create_body(false);
body->set_items(std::move(result_items));
- result_tabs.push_back(Tab{std::move(body), std::make_unique<FourchanThreadPage>(program, board_id, args.url), nullptr});
+ result_tabs.push_back(Tab{std::move(body), std::make_unique<FourchanThreadPage>(program, board_id, args.url, pass_id), nullptr});
return PluginResult::OK;
}
@@ -454,47 +568,6 @@ namespace QuickMedia {
return PluginResult::OK;
}
- PluginResult FourchanThreadPage::login(const std::string &token, const std::string &pin, std::string &response_msg) {
- response_msg.clear();
-
- Path cookies_filepath;
- if(get_cookies_filepath(cookies_filepath, SERVICE_NAME) != 0) {
- fprintf(stderr, "Failed to get 4chan cookies filepath\n");
- return PluginResult::ERR;
- }
-
- std::vector<CommandArg> additional_args = {
- CommandArg{"--form-string", "id=" + token},
- CommandArg{"--form-string", "pin=" + pin},
- CommandArg{"--form-string", "xhr=1"},
- CommandArg{"-c", cookies_filepath.data}
- };
-
- Json::Value json_root;
- DownloadResult result = download_json(json_root, "https://sys.4chan.org/auth", std::move(additional_args), true);
- if(result != DownloadResult::OK) return download_result_to_plugin_result(result);
-
- if(!json_root.isObject())
- return PluginResult::ERR;
-
- const Json::Value &status_json = json_root["status"];
- if(!status_json.isNumeric())
- return PluginResult::ERR;
-
- const Json::Value &message_json = json_root["message"];
- if(message_json.isString())
- response_msg = message_json.asString();
-
- if(status_json.asInt64() == 1) {
- pass_id = get_pass_id_from_cookies_file(cookies_filepath);
- if(pass_id.empty())
- return PluginResult::ERR;
- return PluginResult::OK;
- } else {
- return PluginResult::ERR;
- }
- }
-
static std::string file_get_filename(const std::string &filepath) {
size_t index = filepath.rfind('/');
if(index == std::string::npos)
diff --git a/src/plugins/ImageBoard.cpp b/src/plugins/ImageBoard.cpp
index b027b9e..e1ee8fa 100644
--- a/src/plugins/ImageBoard.cpp
+++ b/src/plugins/ImageBoard.cpp
@@ -5,13 +5,6 @@ namespace QuickMedia {
set_clipboard(body_item->get_title());
}
- PluginResult ImageBoardThreadPage::login(const std::string &token, const std::string &pin, std::string &response_msg) {
- (void)token;
- (void)pin;
- response_msg = "Login is not supported on this image board";
- return PluginResult::ERR;
- }
-
const std::string& ImageBoardThreadPage::get_pass_id() {
static std::string empty_str;
return empty_str;
diff --git a/src/plugins/Page.cpp b/src/plugins/Page.cpp
index 654c983..f5867e3 100644
--- a/src/plugins/Page.cpp
+++ b/src/plugins/Page.cpp
@@ -137,4 +137,13 @@ namespace QuickMedia {
return PluginResult::OK;
}
+
+ void LoginPage::login_finish() {
+ login_finished = true;
+ program->set_go_to_previous_page();
+ }
+
+ bool LoginPage::logged_in() const {
+ return login_finished;
+ }
} \ No newline at end of file