From 77ed51898157d99112be7550471ec06e32344c9e Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 11 Oct 2020 21:35:37 +0200 Subject: Refactor plugin into seperate pages TODO: Readd 4chan login page, manganelo creators page, autocomplete --- src/plugins/Matrix.cpp | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) (limited to 'src/plugins/Matrix.cpp') diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index 2107812..dec4a68 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -1,5 +1,6 @@ #include "../../plugins/Matrix.hpp" #include "../../include/Storage.hpp" +#include "../../include/StringUtils.hpp" #include #include #include @@ -18,6 +19,8 @@ // TODO: Verify if this class really is thread-safe (for example room data fields, user fields, message fields; etc that are updated in /sync) +static const char* SERVICE_NAME = "matrix"; + namespace QuickMedia { std::shared_ptr RoomData::get_user_by_id(const std::string &user_id) { std::lock_guard lock(room_mutex); @@ -99,10 +102,6 @@ namespace QuickMedia { return messages; } - Matrix::Matrix() : Plugin("matrix") { - - } - PluginResult Matrix::sync(RoomSyncMessages &room_messages) { std::vector additional_args = { { "-H", "Authorization: Bearer " + access_token }, @@ -819,10 +818,6 @@ namespace QuickMedia { return PluginResult::OK; } - SearchResult Matrix::search(const std::string&, BodyItems&) { - return SearchResult::OK; - } - static bool generate_random_characters(char *buffer, int buffer_size) { int fd = open("/dev/urandom", O_RDONLY); if(fd == -1) { @@ -1443,7 +1438,7 @@ namespace QuickMedia { // TODO: Handle well_known field. The spec says clients SHOULD handle it if its provided - Path session_path = get_storage_dir().join(name); + Path session_path = get_storage_dir().join(SERVICE_NAME); if(create_directory_recursive(session_path) == 0) { session_path.join("session.json"); if(!save_json_to_file_atomic(session_path, json_root)) { @@ -1457,7 +1452,7 @@ namespace QuickMedia { } PluginResult Matrix::logout() { - Path session_path = get_storage_dir().join(name).join("session.json"); + Path session_path = get_storage_dir().join(SERVICE_NAME).join("session.json"); remove(session_path.data.c_str()); std::vector additional_args = { @@ -1530,7 +1525,7 @@ namespace QuickMedia { } PluginResult Matrix::load_and_verify_cached_session() { - Path session_path = get_storage_dir().join(name).join("session.json"); + Path session_path = get_storage_dir().join(SERVICE_NAME).join("session.json"); std::string session_json_content; if(file_get_content(session_path, session_json_content) != 0) { fprintf(stderr, "Info: failed to read matrix session from %s. Either its missing or we failed to read the file\n", session_path.data.c_str()); @@ -1721,4 +1716,28 @@ namespace QuickMedia { std::lock_guard lock(room_data_mutex); room_data_by_id.insert(std::make_pair(room->id, room)); } + + DownloadResult Matrix::download_json(Json::Value &result, const std::string &url, std::vector additional_args, bool use_browser_useragent, std::string *err_msg) const { + std::string server_response; + if(download_to_string(url, server_response, std::move(additional_args), use_tor, use_browser_useragent, err_msg == nullptr) != DownloadResult::OK) { + if(err_msg) + *err_msg = server_response; + return DownloadResult::NET_ERR; + } + + if(server_response.empty()) + return DownloadResult::OK; + + Json::CharReaderBuilder json_builder; + std::unique_ptr json_reader(json_builder.newCharReader()); + std::string json_errors; + if(!json_reader->parse(&server_response[0], &server_response[server_response.size()], &result, &json_errors)) { + fprintf(stderr, "download_json error: %s\n", json_errors.c_str()); + if(err_msg) + *err_msg = std::move(json_errors); + return DownloadResult::ERR; + } + + return DownloadResult::OK; + } } \ No newline at end of file -- cgit v1.2.3